#![cfg_attr(not(feature = "std"), no_std)]
#![allow(deprecated)] extern crate alloc;
use migration::*;
pub use pallet::*;
mod benchmarking;
mod migration;
mod mock;
mod tests;
pub mod weights;
pub use weights::WeightInfo;
pub type MaxDmpMessageLenOf<T> =
<<T as Config>::DmpSink as frame_support::traits::HandleMessage>::MaxMessageLen;
#[frame_support::pallet]
#[deprecated(
note = "`cumulus-pallet-dmp-queue` will be removed after November 2024. It can be removed once its lazy migration completed. See <https://github.com/paritytech/polkadot-sdk/pull/1246>."
)]
pub mod pallet {
use super::*;
use frame_support::{pallet_prelude::*, traits::HandleMessage, weights::WeightMeter};
use frame_system::pallet_prelude::*;
use sp_io::hashing::twox_128;
const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type DmpSink: HandleMessage;
type WeightInfo: WeightInfo;
}
#[pallet::storage]
pub type MigrationStatus<T> = StorageValue<_, MigrationState, ValueQuery>;
#[derive(
codec::Encode, codec::Decode, Debug, PartialEq, Eq, Clone, MaxEncodedLen, TypeInfo,
)]
pub enum MigrationState {
NotStarted,
StartedExport {
next_begin_used: PageCounter,
},
CompletedExport,
StartedOverweightExport {
next_overweight_index: u64,
},
CompletedOverweightExport,
StartedCleanup { cursor: Option<BoundedVec<u8, ConstU32<1024>>> },
Completed,
}
impl Default for MigrationState {
fn default() -> Self {
Self::NotStarted
}
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
StartedExport,
Exported { page: PageCounter },
ExportFailed { page: PageCounter },
CompletedExport,
StartedOverweightExport,
ExportedOverweight { index: OverweightIndex },
ExportOverweightFailed { index: OverweightIndex },
CompletedOverweightExport,
StartedCleanup,
CleanedSome { keys_removed: u32 },
Completed { error: bool },
}
#[pallet::call]
impl<T: Config> Pallet<T> {}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn integrity_test() {
let w = Self::on_idle_weight();
assert!(w != Weight::zero());
assert!(w.all_lte(T::BlockWeights::get().max_block));
}
fn on_idle(now: BlockNumberFor<T>, limit: Weight) -> Weight {
let mut meter = WeightMeter::with_limit(limit);
if meter.try_consume(Self::on_idle_weight()).is_err() {
log::debug!(target: LOG, "Not enough weight for on_idle. {} < {}", Self::on_idle_weight(), limit);
return meter.consumed()
}
let state = MigrationStatus::<T>::get();
let index = PageIndex::<T>::get();
log::debug!(target: LOG, "on_idle: block={:?}, state={:?}, index={:?}", now, state, index);
match state {
MigrationState::NotStarted => {
log::debug!(target: LOG, "Init export at page {}", index.begin_used);
MigrationStatus::<T>::put(MigrationState::StartedExport {
next_begin_used: index.begin_used,
});
Self::deposit_event(Event::StartedExport);
},
MigrationState::StartedExport { next_begin_used } => {
log::debug!(target: LOG, "Exporting page {}", next_begin_used);
if next_begin_used == index.end_used {
MigrationStatus::<T>::put(MigrationState::CompletedExport);
log::debug!(target: LOG, "CompletedExport");
Self::deposit_event(Event::CompletedExport);
} else {
let res = migration::migrate_page::<T>(next_begin_used);
MigrationStatus::<T>::put(MigrationState::StartedExport {
next_begin_used: next_begin_used.saturating_add(1),
});
if let Ok(()) = res {
log::debug!(target: LOG, "Exported page {}", next_begin_used);
Self::deposit_event(Event::Exported { page: next_begin_used });
} else {
Self::deposit_event(Event::ExportFailed { page: next_begin_used });
}
}
},
MigrationState::CompletedExport => {
log::debug!(target: LOG, "Init export overweight at index 0");
MigrationStatus::<T>::put(MigrationState::StartedOverweightExport {
next_overweight_index: 0,
});
Self::deposit_event(Event::StartedOverweightExport);
},
MigrationState::StartedOverweightExport { next_overweight_index } => {
log::debug!(target: LOG, "Exporting overweight index {}", next_overweight_index);
if next_overweight_index == index.overweight_count {
MigrationStatus::<T>::put(MigrationState::CompletedOverweightExport);
log::debug!(target: LOG, "CompletedOverweightExport");
Self::deposit_event(Event::CompletedOverweightExport);
} else {
let res = migration::migrate_overweight::<T>(next_overweight_index);
MigrationStatus::<T>::put(MigrationState::StartedOverweightExport {
next_overweight_index: next_overweight_index.saturating_add(1),
});
if let Ok(()) = res {
log::debug!(target: LOG, "Exported overweight index {next_overweight_index}");
Self::deposit_event(Event::ExportedOverweight {
index: next_overweight_index,
});
} else {
Self::deposit_event(Event::ExportOverweightFailed {
index: next_overweight_index,
});
}
}
},
MigrationState::CompletedOverweightExport => {
log::debug!(target: LOG, "Init cleanup");
MigrationStatus::<T>::put(MigrationState::StartedCleanup { cursor: None });
Self::deposit_event(Event::StartedCleanup);
},
MigrationState::StartedCleanup { cursor } => {
log::debug!(target: LOG, "Cleaning up");
let hashed_prefix =
twox_128(<Pallet<T> as PalletInfoAccess>::name().as_bytes());
let result = frame_support::storage::unhashed::clear_prefix(
&hashed_prefix,
Some(2), cursor.as_ref().map(|c| c.as_ref()),
);
Self::deposit_event(Event::CleanedSome { keys_removed: result.backend });
if let Some(unbound_cursor) = result.maybe_cursor {
if let Ok(cursor) = unbound_cursor.try_into() {
log::debug!(target: LOG, "Next cursor: {:?}", &cursor);
MigrationStatus::<T>::put(MigrationState::StartedCleanup {
cursor: Some(cursor),
});
} else {
MigrationStatus::<T>::put(MigrationState::Completed);
log::error!(target: LOG, "Completed with error: could not bound cursor");
Self::deposit_event(Event::Completed { error: true });
}
} else {
MigrationStatus::<T>::put(MigrationState::Completed);
log::debug!(target: LOG, "Completed");
Self::deposit_event(Event::Completed { error: false });
}
},
MigrationState::Completed => {
log::debug!(target: LOG, "Idle; you can remove this pallet");
},
}
meter.consumed()
}
}
impl<T: Config> Pallet<T> {
pub fn on_idle_weight() -> Weight {
<T as crate::Config>::WeightInfo::on_idle_good_msg()
.max(<T as crate::Config>::WeightInfo::on_idle_large_msg())
}
}
}