From 4f51dc611ae649d4b932a97301b46a3c20a603fd Mon Sep 17 00:00:00 2001 From: girazoki Date: Wed, 29 Jan 2025 18:43:48 +0100 Subject: [PATCH] migration code --- Cargo.lock | 2 + .../runtime/common/Cargo.toml | 5 +++ .../runtime/common/src/migrations.rs | 43 +++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e9d28823e..c423722c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18164,6 +18164,7 @@ dependencies = [ "pallet-beefy-mmr", "pallet-configuration", "pallet-data-preservers", + "pallet-external-validator-slashes", "pallet-external-validators", "pallet-foreign-asset-creator", "pallet-invulnerables", @@ -18178,6 +18179,7 @@ dependencies = [ "scale-info", "sp-core", "sp-runtime", + "sp-staking", "sp-std", "staging-xcm", ] diff --git a/chains/orchestrator-paras/runtime/common/Cargo.toml b/chains/orchestrator-paras/runtime/common/Cargo.toml index ebea16a06..8666e4742 100644 --- a/chains/orchestrator-paras/runtime/common/Cargo.toml +++ b/chains/orchestrator-paras/runtime/common/Cargo.toml @@ -22,6 +22,7 @@ scale-info = { workspace = true, features = [ "derive" ] } pallet-configuration = { workspace = true } pallet-data-preservers = { workspace = true } pallet-external-validators = { workspace = true } +pallet-external-validator-slashes = { workspace = true } pallet-foreign-asset-creator = { workspace = true } pallet-invulnerables = { workspace = true } pallet-pooled-staking = { workspace = true } @@ -50,6 +51,7 @@ staging-xcm = { workspace = true } sp-core = { workspace = true } sp-runtime = { workspace = true } +sp-staking = { workspace = true } sp-std = { workspace = true } # Cumulus @@ -70,6 +72,7 @@ std = [ "pallet-beefy-mmr/std", "pallet-configuration/std", "pallet-data-preservers/std", + "pallet-external-validator-slashes/std", "pallet-external-validators/std", "pallet-foreign-asset-creator/std", "pallet-invulnerables/std", @@ -84,6 +87,7 @@ std = [ "scale-info/std", "sp-core/std", "sp-runtime/std", + "sp-staking/std", "sp-std/std", "staging-xcm/std", ] @@ -97,6 +101,7 @@ runtime-benchmarks = [ "pallet-beefy-mmr/runtime-benchmarks", "pallet-configuration/runtime-benchmarks", "pallet-data-preservers/runtime-benchmarks", + "pallet-external-validator-slashes/runtime-benchmarks", "pallet-external-validators/runtime-benchmarks", "pallet-foreign-asset-creator/runtime-benchmarks", "pallet-invulnerables/runtime-benchmarks", diff --git a/chains/orchestrator-paras/runtime/common/src/migrations.rs b/chains/orchestrator-paras/runtime/common/src/migrations.rs index 940be3532..faca3817a 100644 --- a/chains/orchestrator-paras/runtime/common/src/migrations.rs +++ b/chains/orchestrator-paras/runtime/common/src/migrations.rs @@ -1024,6 +1024,45 @@ impl MigrateMMRLeafPallet { } } +pub struct BondedErasTimestampMigration(pub PhantomData); + +impl Migration for BondedErasTimestampMigration +where + Runtime: pallet_external_validator_slashes::Config, +{ + fn friendly_name(&self) -> &str { + "TM_ExternalValidatorSlashesBondedErasTimestampMigration" + } + + fn migrate(&self, _available_weight: Weight) -> Weight { + use frame_support::pallet_prelude::*; + + let bonded_eras: Vec<(sp_staking::EraIndex, sp_staking::SessionIndex)> = + frame_support::storage::unhashed::get( + &pallet_external_validator_slashes::BondedEras::::hashed_key(), + ) + .unwrap_or_default(); + let new_eras = bonded_eras + .iter() + .map(|(era, session)| (*era, *session, 0u64)) + .collect(); + pallet_external_validator_slashes::BondedEras::::set(new_eras); + + // One db read and one db write per element, plus the on-chain storage + Runtime::DbWeight::get().reads_writes(1, 1) + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade(&self) -> Result, sp_runtime::DispatchError> { + Ok(vec![]) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(&self, _state: Vec) -> Result<(), sp_runtime::DispatchError> { + Ok(()) + } +} + pub struct DancelightMigrations(PhantomData); impl GetMigrations for DancelightMigrations @@ -1034,6 +1073,7 @@ where Runtime: pallet_session::Config< ValidatorId = ::ValidatorId, >, + Runtime: pallet_external_validator_slashes::Config, { fn get_migrations() -> Vec> { let migrate_mmr_leaf_pallet = MigrateMMRLeafPallet::(Default::default()); @@ -1041,11 +1081,14 @@ where ExternalValidatorsInitialMigration::(Default::default()); let migrate_config_full_rotation_mode = MigrateConfigurationAddFullRotationMode::(Default::default()); + + let external_validator_slashes_bonded_eras_timestamp = BondedErasTimestampMigration::(Default::default()); vec![ Box::new(migrate_mmr_leaf_pallet), Box::new(migrate_external_validators), Box::new(migrate_config_full_rotation_mode), + Box::new(external_validator_slashes_bonded_eras_timestamp), ] } }