Skip to content

Commit

Permalink
chore: add migration for chain height addition
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezs committed Sep 25, 2023
1 parent 0f2f8d1 commit 6be3117
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
6 changes: 5 additions & 1 deletion state-chain/pallets/cf-broadcast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod benchmarking;
mod mock;
mod tests;

pub mod migrations;
pub mod weights;
use cf_primitives::{BroadcastId, ThresholdSignatureRequestId};
use cf_traits::{impl_pallet_safe_mode, GetBlockHeight};
Expand All @@ -23,7 +24,7 @@ use frame_support::{
dispatch::DispatchResultWithPostInfo,
pallet_prelude::DispatchResult,
sp_runtime::traits::Saturating,
traits::{Get, UnfilteredDispatchable},
traits::{Get, StorageVersion, UnfilteredDispatchable},
Twox64Concat,
};

Expand Down Expand Up @@ -66,6 +67,8 @@ pub enum PalletOffence {
FailedToBroadcastTransaction,
}

pub const PALLET_VERSION: StorageVersion = StorageVersion::new(1);

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -205,6 +208,7 @@ pub mod pallet {
pub struct Origin<T: Config<I>, I: 'static = ()>(pub(super) PhantomData<(T, I)>);

#[pallet::pallet]
#[pallet::storage_version(PALLET_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);

Expand Down
6 changes: 6 additions & 0 deletions state-chain/pallets/cf-broadcast/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod add_initiated_at;

use cf_runtime_upgrade_utilities::VersionedMigration;

pub type PalletMigration<T, I> =
(VersionedMigration<crate::Pallet<T, I>, add_initiated_at::Migration<T, I>, 0, 1>,);
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::*;
#[cfg(feature = "try-runtime")]
use frame_support::dispatch::DispatchError;
use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};
use sp_std::marker::PhantomData;

mod old {
use frame_support::pallet_prelude::OptionQuery;

use super::*;

#[frame_support::storage_alias]
pub type TransactionOutIdToBroadcastId<T: Config<I>, I: 'static> =
StorageMap<Pallet<T, I>, Twox64Concat, TransactionOutIdFor<T, I>, BroadcastId, OptionQuery>;
}

pub struct Migration<T: Config<I>, I: 'static>(PhantomData<(T, I)>);

impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for Migration<T, I> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
let chain_height = T::ChainTracking::get_block_height();

TransactionOutIdToBroadcastId::<T, I>::translate::<BroadcastId, _>(|_id, old| {
Some((old, chain_height))
});

Weight::zero()
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, DispatchError> {
use frame_support::ensure;

let chain_height = T::ChainTracking::get_block_height();
ensure!(chain_height > 0u32.into(), "chain_height is 0");
Ok(chain_height.encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), DispatchError> {
use frame_support::ensure;

let pre_upgrade_height = ChainBlockNumberFor::<T, I>::decode(&mut &state[..])
.map_err(|_| "Failed to decode pre-upgrade state.")?;

// If it's at 0 something went wrong with the initialisation. Also since initiated_at is the
// last thing being decoded,
for (_out_id, (_b_id, initiated_at)) in TransactionOutIdToBroadcastId::<T, I>::iter() {
ensure!(initiated_at > pre_upgrade_height, "initiated_at is 0");
}
Ok(())
}
}

0 comments on commit 6be3117

Please sign in to comment.