Skip to content
lib.rs 69.1 KiB
Newer Older
mod clean_state_migration {
	use super::Runtime;
	use frame_support::{pallet_prelude::*, storage_alias, traits::OnRuntimeUpgrade};
	use pallet_state_trie_migration::MigrationLimits;

	#[cfg(not(feature = "std"))]
	use sp_std::prelude::*;

	#[storage_alias]
	type AutoLimits = StorageValue<StateTrieMigration, Option<MigrationLimits>, ValueQuery>;

	// Actual type of value is `MigrationTask<T>`, putting a dummy
	// one to avoid the trait constraint on T.
	// Since we only use `kill` it is fine.
	#[storage_alias]
	type MigrationProcess = StorageValue<StateTrieMigration, u32, ValueQuery>;

	#[storage_alias]
	type SignedMigrationMaxLimits = StorageValue<StateTrieMigration, MigrationLimits, OptionQuery>;

	/// Initialize an automatic migration process.
	pub struct CleanMigrate;

	impl OnRuntimeUpgrade for CleanMigrate {
		#[cfg(feature = "try-runtime")]
		fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
			Ok(Default::default())
		}

		fn on_runtime_upgrade() -> frame_support::weights::Weight {
			MigrationProcess::kill();
			AutoLimits::kill();
			SignedMigrationMaxLimits::kill();
			<Runtime as frame_system::Config>::DbWeight::get().writes(3)
		}

		#[cfg(feature = "try-runtime")]
		fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
			frame_support::ensure!(
				!AutoLimits::exists() && !SignedMigrationMaxLimits::exists(),
				"State migration clean.",