Skip to content

Commit

Permalink
Some more doc and naming updates, migration checks WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
PolkadotDom committed Jan 7, 2025
1 parent b208d52 commit 606812b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,9 @@ impl pallet_salary::migration::v1::ConvertBlockNumber<SalaryLocalBlockNumber, Sa

/// The equivalent moment in time from the perspective of the relay chain, starting from a
/// local moment in time (system block number)
fn equivalent_moment_in_time(local: SalaryLocalBlockNumber) -> SalaryNewBlockNumber {
fn equivalent_moment_in_time(local_moment: SalaryLocalBlockNumber) -> SalaryNewBlockNumber {
let block_number = System::block_number();
let local_duration = block_number.saturating_sub(local);
let local_duration = block_number.saturating_sub(local_moment);
let relay_duration = Self::equivalent_block_duration(local_duration); //6s to 6s
let relay_block_number = ParachainSystem::last_relay_block_number();
relay_block_number.saturating_sub(relay_duration)
Expand All @@ -805,8 +805,8 @@ impl pallet_salary::migration::v1::ConvertBlockNumber<SalaryLocalBlockNumber, Sa
/// The equivalent duration from the perspective of the relay chain, starting from
/// a local duration (number of block). Identity function for Westend, since both
/// relay and collectives chain run 6s block times
fn equivalent_block_duration(local: SalaryLocalBlockNumber) -> SalaryNewBlockNumber {
local
fn equivalent_block_duration(local_duration: SalaryLocalBlockNumber) -> SalaryNewBlockNumber {
local_duration
}
}

Expand Down
47 changes: 32 additions & 15 deletions substrate/frame/salary/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub mod v1 {
/// ```rust,ignore
/// // Let's say both L and N are u32, then a simple identity will suffice.
/// fn convert(local: u32) -> u32 {
/// local
/// local
/// }
///
/// // But if L is u64 and N is u32, or some other problematic variation,
Expand All @@ -74,7 +74,7 @@ pub mod v1 {
/// ```
fn convert(local: L) -> N;

/// Converts to the new type and finds the equivalent moment in time as relative to the new
/// Converts to the new type and finds the equivalent moment in time as from the view of the new
/// block provider
///
/// For instance - if your new version uses the relay chain number, you'll want to
Expand All @@ -86,19 +86,19 @@ pub mod v1 {
/// ```rust,ignore
/// // Let's say you are a parachain and switching block providers to the relay chain.
/// // This will return what the relay block number was at the moment the previous provider's
/// // number was `local`.
/// fn equivalent_moment_in_time(local: u32) -> u32 {
/// // How long it's been since 'local'.
/// // number was `local_moment`.
/// fn equivalent_moment_in_time(local_moment: u32) -> u32 {
/// // How long it's been since 'local_moment' from the parachains pov.
/// let curr_block_number = System::block_number();
/// let local_duration = curr_block_number.saturating_sub(local);
/// // How many blocks that is in relay block time.
/// let local_duration = curr_block_number.saturating_sub(local_moment);
/// // How many blocks that is from the relay's pov.
/// let relay_duration = Self::equivalent_block_duration(local_duration);
/// // What the relay block number must have been at that moment.
/// // What the relay block number must have been at 'local_moment'.
/// let relay_block_number = ParachainSystem::last_relay_block_number();
/// relay_block_number.saturating_sub(relay_duration)
/// }
/// ```
fn equivalent_moment_in_time(local: L) -> N;
fn equivalent_moment_in_time(local_moment: L) -> N;

/// Returns the equivalent number of new blocks it would take to fulfill the same
/// amount of time in seconds as the old blocks.
Expand All @@ -112,25 +112,31 @@ pub mod v1 {
/// 12s
/// |--------------------|
///
/// ^ Two 6s blocks passed per one 12s block.
/// ^ Two 6s relay blocks passed per one 12s local block.
///
/// # Example Usage
///
/// ```rust,ignore
/// // If your previous provider updated the block number every 12 seconds and
/// // your new updates every 6 - 2 new blocks would have taken place per one old block.
/// fn equivalent_block_duration(local: u32) -> u32 {
/// local * 2
/// // Following the scenerio above.
/// fn equivalent_block_duration(local_duration: u32) -> u32 {
/// local_duration.saturating_mul(2)
/// }
/// ```
fn equivalent_block_duration(local: L) -> N;
fn equivalent_block_duration(local_duration: L) -> N;
}

pub struct MigrateToV1<T, BC, I = ()>(PhantomData<(T, BC, I)>);
impl<T: Config<I>, BC, I: 'static> UncheckedOnRuntimeUpgrade for MigrateToV1<T, BC, I>
where
BC: ConvertBlockNumber<LocalBlockNumberFor<T>, NewBlockNumberFor<T, I>>,
{
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
let status_exists = v0::Status::<T, I>::exists();
let claimant_count = v0::Claimant::<T, I>::iter().count();
Ok((status_exists, claimant_count).encode())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
let mut transactions = 0;

Expand Down Expand Up @@ -160,6 +166,17 @@ pub mod v1 {

T::DbWeight::get().reads_writes(transactions, transactions)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), TryRuntimeError> {
let (status_existed, pre_claimaint_count) : (v0::StatusOf<T, I>, v0::ClaimantStatusOf<T, I>) =
Decode::decode(&mut &state[..]).expect("pre_upgrade provides a valid state; qed");

ensure!(crate::Status::<T, I>::exists() == status_existed, "The Status' storage existence should remain the same before and after the upgrade.");
ensure!(crate::Claimant::<T, I>::iter().count() == pre_claimaint_count, "The Claimant count should remain the same before and after the upgrade.");
Ok(())
}

}
}

Expand Down

0 comments on commit 606812b

Please sign in to comment.