From 60ea69e999fbc57342b0a1982ed033f78d80b149 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 26 Jan 2023 11:00:29 +0100 Subject: [PATCH 1/5] Add migration for new storage version --- Cargo.lock | 5 + pallets/ctype/src/lib.rs | 2 +- runtimes/common/Cargo.toml | 10 ++ runtimes/common/src/migrations.rs | 188 +++++++++++++++++++++++++++++- runtimes/peregrine/src/lib.rs | 4 +- runtimes/spiritnet/src/lib.rs | 4 +- 6 files changed, 205 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7d2aec799..3b18c1ef1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8811,6 +8811,8 @@ dependencies = [ "attestation", "ctype", "cumulus-primitives-core", + "delegation", + "did", "frame-support", "frame-system", "kilt-asset-dids", @@ -8818,8 +8820,11 @@ dependencies = [ "log", "pallet-authorship", "pallet-balances", + "pallet-did-lookup", + "pallet-inflation", "pallet-membership", "pallet-transaction-payment", + "pallet-web3-names", "parachain-staking", "parity-scale-codec", "polkadot-parachain", diff --git a/pallets/ctype/src/lib.rs b/pallets/ctype/src/lib.rs index 61758d8a2..5f33bc7a4 100644 --- a/pallets/ctype/src/lib.rs +++ b/pallets/ctype/src/lib.rs @@ -73,7 +73,7 @@ pub mod pallet { use crate::ctype_entry::CtypeEntry; /// The current storage version. - const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + const STORAGE_VERSION: StorageVersion = StorageVersion::new(2); /// Type of a CType hash. pub type CtypeHashOf = ::Hash; diff --git a/runtimes/common/Cargo.toml b/runtimes/common/Cargo.toml index 91e85925b..e219b0a3a 100644 --- a/runtimes/common/Cargo.toml +++ b/runtimes/common/Cargo.toml @@ -24,9 +24,14 @@ smallvec.workspace = true # Internal dependencies attestation.workspace = true ctype.workspace = true +delegation.workspace = true +did.workspace = true +pallet-did-lookup.workspace = true +pallet-inflation.workspace = true kilt-support.workspace = true parachain-staking.workspace = true public-credentials.workspace = true +pallet-web3-names.workspace = true # Substrate dependencies frame-support.workspace = true @@ -97,11 +102,16 @@ std = [ ] try-runtime = [ "attestation/try-runtime", + "delegation/try-runtime", + "did/try-runtime", "frame-support/try-runtime", "frame-system/try-runtime", "kilt-support/try-runtime", "pallet-authorship/try-runtime", "pallet-balances/try-runtime", + "pallet-did-lookup/try-runtime", + "pallet-inflation/try-runtime", + "pallet-web3-names/try-runtime", "pallet-membership/try-runtime", "pallet-transaction-payment/try-runtime", "parachain-staking/try-runtime", diff --git a/runtimes/common/src/migrations.rs b/runtimes/common/src/migrations.rs index 76f2a8e6b..36db9b3e5 100644 --- a/runtimes/common/src/migrations.rs +++ b/runtimes/common/src/migrations.rs @@ -30,7 +30,9 @@ pub struct AddCTypeBlockNumber(PhantomData); impl OnRuntimeUpgrade for AddCTypeBlockNumber { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { - assert_eq!(ctype::Pallet::::on_chain_storage_version(), 0,); + // Missed the migration when v1 was introduced, so now Spiritnet and Peregrine + // are on v0 although they should be on v1. + assert!(ctype::Pallet::::on_chain_storage_version() <= 1,); // Use iter_keys() on new storage so it won't try to decode values. let ctypes_to_migrate = ctype::Ctypes::::iter_keys().count() as u64; @@ -44,7 +46,7 @@ impl OnRuntimeUpgrade for AddCTypeBlockNumber { let onchain = ctype::Pallet::::on_chain_storage_version(); log::info!( - "💰 Running migration with current storage version {:?} / onchain {:?}", + "💰 Running CType migration with current storage version {:?} / onchain {:?}", current, onchain ); @@ -67,7 +69,7 @@ impl OnRuntimeUpgrade for AddCTypeBlockNumber { #[cfg(feature = "try-runtime")] fn post_upgrade(state: Vec) -> Result<(), &'static str> { - assert_eq!(ctype::Pallet::::on_chain_storage_version(), 1); + assert_eq!(ctype::Pallet::::on_chain_storage_version(), 2); let initial_ctype_count = u64::from_be_bytes(state.try_into().expect("input state should be 8 bytes")); assert_eq!(initial_ctype_count, ctype::Ctypes::::iter().count() as u64); @@ -81,3 +83,183 @@ impl OnRuntimeUpgrade for AddCTypeBlockNumber { Ok(()) } } + +pub struct MigrateToNewStorageVersion(PhantomData); + +impl OnRuntimeUpgrade for MigrateToNewStorageVersion +where + R: attestation::Config + + delegation::Config + + did::Config + + pallet_did_lookup::Config + + pallet_inflation::Config + + pallet_web3_names::Config + + parachain_staking::Config + + public_credentials::Config, +{ + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + type AttestationPallet = attestation::Pallet; + type DelegationPallet = delegation::Pallet; + type DidPallet = did::Pallet; + type LookupPallet = pallet_did_lookup::Pallet; + type InflationPallet = pallet_inflation::Pallet; + type Web3NamesPallet = pallet_web3_names::Pallet; + type ParachainStakingPallet = parachain_staking::Pallet; + type PublicCredentialsPallet = public_credentials::Pallet; + + log::info!("💿 Storage version pre checks"); + + if AttestationPallet::::on_chain_storage_version() != AttestationPallet::::current_storage_version() { + log::warn!( + "🚨 Attestation pallet on chain version {:?} != declared storage version {:?}.", + AttestationPallet::::on_chain_storage_version(), + AttestationPallet::::current_storage_version() + ) + } + if DelegationPallet::::on_chain_storage_version() != DelegationPallet::::current_storage_version() { + log::warn!( + "🚨 Delegation pallet on chain version {:?} != declared storage version {:?}.", + DelegationPallet::::on_chain_storage_version(), + DelegationPallet::::current_storage_version() + ) + } + if DidPallet::::on_chain_storage_version() != DidPallet::::current_storage_version() { + log::warn!( + "🚨 Did pallet on chain version {:?} != declared storage version {:?}.", + DidPallet::::on_chain_storage_version(), + DidPallet::::current_storage_version() + ) + } + if LookupPallet::::on_chain_storage_version() != LookupPallet::::current_storage_version() { + log::warn!( + "🚨 Lookup pallet on chain version {:?} != declared storage version {:?}.", + LookupPallet::::on_chain_storage_version(), + LookupPallet::::current_storage_version() + ) + } + if InflationPallet::::on_chain_storage_version() != InflationPallet::::current_storage_version() { + log::warn!( + "🚨 Inflation pallet on chain version {:?} != declared storage version {:?}.", + InflationPallet::::on_chain_storage_version(), + InflationPallet::::current_storage_version() + ) + } + if Web3NamesPallet::::on_chain_storage_version() != Web3NamesPallet::::current_storage_version() { + log::warn!( + "🚨 Web3names pallet on chain version {:?} != declared storage version {:?}.", + Web3NamesPallet::::on_chain_storage_version(), + Web3NamesPallet::::current_storage_version() + ) + } + if ParachainStakingPallet::::on_chain_storage_version() + != ParachainStakingPallet::::current_storage_version() + { + log::warn!( + "🚨 Parachain staking pallet on chain version {:?} != declared storage version {:?}.", + ParachainStakingPallet::::on_chain_storage_version(), + ParachainStakingPallet::::current_storage_version() + ) + } + if PublicCredentialsPallet::::on_chain_storage_version() + != PublicCredentialsPallet::::current_storage_version() + { + log::warn!( + "🚨 Public credentials pallet on chain version {:?} != declared storage version {:?}.", + PublicCredentialsPallet::::on_chain_storage_version(), + PublicCredentialsPallet::::current_storage_version() + ) + } + + Ok(Vec::default()) + } + + fn on_runtime_upgrade() -> frame_support::weights::Weight { + type AttestationPallet = attestation::Pallet; + type InflationPallet = pallet_inflation::Pallet; + type Web3NamesPallet = pallet_web3_names::Pallet; + type PublicCredentialsPallet = public_credentials::Pallet; + + log::info!("RUNNINGGGGGG"); + + AttestationPallet::::current_storage_version().put::>(); + InflationPallet::::current_storage_version().put::>(); + Web3NamesPallet::::current_storage_version().put::>(); + PublicCredentialsPallet::::current_storage_version().put::>(); + + ::DbWeight::get().writes(4) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), &'static str> { + type AttestationPallet = attestation::Pallet; + type DelegationPallet = delegation::Pallet; + type DidPallet = did::Pallet; + type LookupPallet = pallet_did_lookup::Pallet; + type InflationPallet = pallet_inflation::Pallet; + type Web3NamesPallet = pallet_web3_names::Pallet; + type ParachainStakingPallet = parachain_staking::Pallet; + type PublicCredentialsPallet = public_credentials::Pallet; + + log::info!("💿 Storage version post checks"); + + assert_eq!( + AttestationPallet::::on_chain_storage_version(), + AttestationPallet::::current_storage_version(), + "Attestation pallet on chain version {:?} != declared storage version {:?}.", + AttestationPallet::::on_chain_storage_version(), + AttestationPallet::::current_storage_version() + ); + assert_eq!( + DelegationPallet::::on_chain_storage_version(), + DelegationPallet::::current_storage_version(), + "Delegation pallet on chain version {:?} != declared storage version {:?}.", + DelegationPallet::::on_chain_storage_version(), + DelegationPallet::::current_storage_version() + ); + assert_eq!( + DidPallet::::on_chain_storage_version(), + DidPallet::::current_storage_version(), + "Did pallet on chain version {:?} != declared storage version {:?}.", + DidPallet::::on_chain_storage_version(), + DidPallet::::current_storage_version() + ); + assert_eq!( + LookupPallet::::on_chain_storage_version(), + LookupPallet::::current_storage_version(), + "Lookup pallet on chain version {:?} != declared storage version {:?}.", + LookupPallet::::on_chain_storage_version(), + LookupPallet::::current_storage_version() + ); + assert_eq!( + InflationPallet::::on_chain_storage_version(), + InflationPallet::::current_storage_version(), + "Inflation pallet on chain version {:?} != declared storage version {:?}.", + InflationPallet::::on_chain_storage_version(), + InflationPallet::::current_storage_version() + ); + assert_eq!( + Web3NamesPallet::::on_chain_storage_version(), + Web3NamesPallet::::current_storage_version(), + "Web3names pallet on chain version {:?} != declared storage version {:?}.", + Web3NamesPallet::::on_chain_storage_version(), + Web3NamesPallet::::current_storage_version() + ); + assert_eq!( + ParachainStakingPallet::::on_chain_storage_version(), + ParachainStakingPallet::::current_storage_version(), + "Parachain staking pallet on chain version {:?} != declared storage version {:?}.", + ParachainStakingPallet::::on_chain_storage_version(), + ParachainStakingPallet::::current_storage_version() + ); + assert_eq!( + PublicCredentialsPallet::::on_chain_storage_version(), + PublicCredentialsPallet::::current_storage_version(), + "Public credentials pallet on chain version {:?} != declared storage version {:?}.", + PublicCredentialsPallet::::on_chain_storage_version(), + PublicCredentialsPallet::::current_storage_version() + ); + + Ok(()) + } +} diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 9c4309e8f..95099eee5 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -51,7 +51,7 @@ use xcm_executor::XcmExecutor; use delegation::DelegationAc; use kilt_support::traits::ItemFilter; -use pallet_did_lookup::{linkable_account::LinkableAccountId, migrations::EthereumMigration}; +use pallet_did_lookup::linkable_account::LinkableAccountId; pub use parachain_staking::InflationInfo; pub use public_credentials; @@ -1068,7 +1068,7 @@ pub type Executive = frame_executive::Executive< pallet_scheduler::migration::v3::MigrateToV4, pallet_democracy::migrations::v1::Migration, runtime_common::migrations::AddCTypeBlockNumber, - EthereumMigration, + runtime_common::migrations::MigrateToNewStorageVersion, ), >; diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index 871038318..316479f9b 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -51,7 +51,7 @@ use xcm_executor::XcmExecutor; use delegation::DelegationAc; use kilt_support::traits::ItemFilter; -use pallet_did_lookup::{linkable_account::LinkableAccountId, migrations::EthereumMigration}; +use pallet_did_lookup::linkable_account::LinkableAccountId; pub use parachain_staking::InflationInfo; pub use public_credentials; @@ -1064,7 +1064,7 @@ pub type Executive = frame_executive::Executive< pallet_scheduler::migration::v3::MigrateToV4, pallet_democracy::migrations::v1::Migration, runtime_common::migrations::AddCTypeBlockNumber, - EthereumMigration, + runtime_common::migrations::MigrateToNewStorageVersion, ), >; From 5edb757b261bc7487af5539a39fe3e532013aefd Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 26 Jan 2023 13:23:48 +0100 Subject: [PATCH 2/5] Finalize migration --- runtimes/common/src/migrations.rs | 20 ++++++++++++++------ runtimes/peregrine/src/lib.rs | 3 +++ runtimes/spiritnet/src/lib.rs | 3 +++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/runtimes/common/src/migrations.rs b/runtimes/common/src/migrations.rs index 36db9b3e5..b21b38b83 100644 --- a/runtimes/common/src/migrations.rs +++ b/runtimes/common/src/migrations.rs @@ -89,6 +89,7 @@ pub struct MigrateToNewStorageVersion(PhantomData); impl OnRuntimeUpgrade for MigrateToNewStorageVersion where R: attestation::Config + + ctype::Config + delegation::Config + did::Config + pallet_did_lookup::Config @@ -176,14 +177,11 @@ where fn on_runtime_upgrade() -> frame_support::weights::Weight { type AttestationPallet = attestation::Pallet; - type InflationPallet = pallet_inflation::Pallet; type Web3NamesPallet = pallet_web3_names::Pallet; type PublicCredentialsPallet = public_credentials::Pallet; - log::info!("RUNNINGGGGGG"); - AttestationPallet::::current_storage_version().put::>(); - InflationPallet::::current_storage_version().put::>(); + // Not an issue with Peregrine, but it is with Spiritnet. Web3NamesPallet::::current_storage_version().put::>(); PublicCredentialsPallet::::current_storage_version().put::>(); @@ -193,6 +191,7 @@ where #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), &'static str> { type AttestationPallet = attestation::Pallet; + type CTypePallet = ctype::Pallet; type DelegationPallet = delegation::Pallet; type DidPallet = did::Pallet; type LookupPallet = pallet_did_lookup::Pallet; @@ -201,8 +200,6 @@ where type ParachainStakingPallet = parachain_staking::Pallet; type PublicCredentialsPallet = public_credentials::Pallet; - log::info!("💿 Storage version post checks"); - assert_eq!( AttestationPallet::::on_chain_storage_version(), AttestationPallet::::current_storage_version(), @@ -210,6 +207,15 @@ where AttestationPallet::::on_chain_storage_version(), AttestationPallet::::current_storage_version() ); + // Although it's part of a different migration, we check that the CType pallet + // storage version is also consistent. + assert_eq!( + CTypePallet::::on_chain_storage_version(), + CTypePallet::::current_storage_version(), + "CType pallet on chain version {:?} != declared storage version {:?}.", + CTypePallet::::on_chain_storage_version(), + CTypePallet::::current_storage_version() + ); assert_eq!( DelegationPallet::::on_chain_storage_version(), DelegationPallet::::current_storage_version(), @@ -260,6 +266,8 @@ where PublicCredentialsPallet::::current_storage_version() ); + log::info!("💿 Storage version post checks ok ✅"); + Ok(()) } } diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 95099eee5..df4fd561a 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -1067,7 +1067,10 @@ pub type Executive = frame_executive::Executive< pallet_preimage::migration::v1::Migration, pallet_scheduler::migration::v3::MigrateToV4, pallet_democracy::migrations::v1::Migration, + pallet_did_lookup::migrations::EthereumMigration, runtime_common::migrations::AddCTypeBlockNumber, + // The migration above must be run as last since it checks that all pallets are using the new StorageVersion + // properly. runtime_common::migrations::MigrateToNewStorageVersion, ), >; diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index 316479f9b..13d3750ee 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -1063,7 +1063,10 @@ pub type Executive = frame_executive::Executive< pallet_preimage::migration::v1::Migration, pallet_scheduler::migration::v3::MigrateToV4, pallet_democracy::migrations::v1::Migration, + pallet_did_lookup::migrations::EthereumMigration, runtime_common::migrations::AddCTypeBlockNumber, + // The migration above must be run as last since it checks that all pallets are using the new StorageVersion + // properly. runtime_common::migrations::MigrateToNewStorageVersion, ), >; From e0ab2fdaee0eedacb38f5b89159937a2047dc644 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 26 Jan 2023 13:47:56 +0100 Subject: [PATCH 3/5] Make try-runtime dependencies optional --- runtimes/common/Cargo.toml | 16 ++++++------ runtimes/common/src/migrations.rs | 42 ++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/runtimes/common/Cargo.toml b/runtimes/common/Cargo.toml index e219b0a3a..31590e08a 100644 --- a/runtimes/common/Cargo.toml +++ b/runtimes/common/Cargo.toml @@ -24,10 +24,10 @@ smallvec.workspace = true # Internal dependencies attestation.workspace = true ctype.workspace = true -delegation.workspace = true -did.workspace = true -pallet-did-lookup.workspace = true -pallet-inflation.workspace = true +delegation = {workspace = true, optional = true} +did = {workspace = true, optional = true} +pallet-did-lookup = {workspace = true, optional = true} +pallet-inflation = {workspace = true, optional = true} kilt-support.workspace = true parachain-staking.workspace = true public-credentials.workspace = true @@ -102,15 +102,15 @@ std = [ ] try-runtime = [ "attestation/try-runtime", - "delegation/try-runtime", - "did/try-runtime", + "delegation", + "did", "frame-support/try-runtime", "frame-system/try-runtime", "kilt-support/try-runtime", "pallet-authorship/try-runtime", "pallet-balances/try-runtime", - "pallet-did-lookup/try-runtime", - "pallet-inflation/try-runtime", + "pallet-did-lookup", + "pallet-inflation", "pallet-web3-names/try-runtime", "pallet-membership/try-runtime", "pallet-transaction-payment/try-runtime", diff --git a/runtimes/common/src/migrations.rs b/runtimes/common/src/migrations.rs index b21b38b83..f9830b6d0 100644 --- a/runtimes/common/src/migrations.rs +++ b/runtimes/common/src/migrations.rs @@ -86,6 +86,25 @@ impl OnRuntimeUpgrade for AddCTypeBlockNumber { pub struct MigrateToNewStorageVersion(PhantomData); +impl MigrateToNewStorageVersion +where + R: attestation::Config + pallet_web3_names::Config + public_credentials::Config, +{ + fn migrate() -> frame_support::weights::Weight { + type AttestationPallet = attestation::Pallet; + type Web3NamesPallet = pallet_web3_names::Pallet; + type PublicCredentialsPallet = public_credentials::Pallet; + + AttestationPallet::::current_storage_version().put::>(); + // Not an issue with Peregrine, but it is with Spiritnet. + Web3NamesPallet::::current_storage_version().put::>(); + PublicCredentialsPallet::::current_storage_version().put::>(); + + ::DbWeight::get().writes(4) + } +} + +#[cfg(feature = "try-runtime")] impl OnRuntimeUpgrade for MigrateToNewStorageVersion where R: attestation::Config @@ -98,7 +117,6 @@ where + parachain_staking::Config + public_credentials::Config, { - #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { type AttestationPallet = attestation::Pallet; type DelegationPallet = delegation::Pallet; @@ -176,19 +194,9 @@ where } fn on_runtime_upgrade() -> frame_support::weights::Weight { - type AttestationPallet = attestation::Pallet; - type Web3NamesPallet = pallet_web3_names::Pallet; - type PublicCredentialsPallet = public_credentials::Pallet; - - AttestationPallet::::current_storage_version().put::>(); - // Not an issue with Peregrine, but it is with Spiritnet. - Web3NamesPallet::::current_storage_version().put::>(); - PublicCredentialsPallet::::current_storage_version().put::>(); - - ::DbWeight::get().writes(4) + Self::migrate() } - #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), &'static str> { type AttestationPallet = attestation::Pallet; type CTypePallet = ctype::Pallet; @@ -271,3 +279,13 @@ where Ok(()) } } + +#[cfg(not(feature = "try-runtime"))] +impl OnRuntimeUpgrade for MigrateToNewStorageVersion +where + R: attestation::Config + pallet_web3_names::Config + public_credentials::Config, +{ + fn on_runtime_upgrade() -> frame_support::weights::Weight { + Self::migrate() + } +} From 7172bec3be53d46a6248df635f5f923697c5fab7 Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 26 Jan 2023 13:48:45 +0100 Subject: [PATCH 4/5] Remove unused dependency --- runtimes/common/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/runtimes/common/Cargo.toml b/runtimes/common/Cargo.toml index 31590e08a..97b5fc6f3 100644 --- a/runtimes/common/Cargo.toml +++ b/runtimes/common/Cargo.toml @@ -111,7 +111,6 @@ try-runtime = [ "pallet-balances/try-runtime", "pallet-did-lookup", "pallet-inflation", - "pallet-web3-names/try-runtime", "pallet-membership/try-runtime", "pallet-transaction-payment/try-runtime", "parachain-staking/try-runtime", From ea0446db0453f8db3a2c1cec721e7dd2be748fcf Mon Sep 17 00:00:00 2001 From: Antonio Antonino Date: Thu, 26 Jan 2023 13:52:23 +0100 Subject: [PATCH 5/5] Update migration weight --- runtimes/common/src/migrations.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtimes/common/src/migrations.rs b/runtimes/common/src/migrations.rs index f9830b6d0..413e3e822 100644 --- a/runtimes/common/src/migrations.rs +++ b/runtimes/common/src/migrations.rs @@ -100,7 +100,7 @@ where Web3NamesPallet::::current_storage_version().put::>(); PublicCredentialsPallet::::current_storage_version().put::>(); - ::DbWeight::get().writes(4) + ::DbWeight::get().writes(3) } }