From 251dbb30e3583a921cae3d500a71953bc454037f Mon Sep 17 00:00:00 2001 From: Nahu <39748285+nahuseyoum@users.noreply.github.com> Date: Tue, 19 Dec 2023 23:51:51 +0000 Subject: [PATCH] SYS-3801 Adds state migration for summary pallet (#343) Co-authored-by: aventus-ci-agent --- Cargo.lock | 37 ++++---- Cargo.toml | 2 +- pallets/parachain-staking/src/migration.rs | 7 +- pallets/summary/Cargo.toml | 2 +- pallets/summary/src/lib.rs | 44 ++------- pallets/summary/src/migration.rs | 102 +++++++++++++++++++++ runtime/avn/src/lib.rs | 28 ++++-- runtime/test/src/lib.rs | 2 +- 8 files changed, 160 insertions(+), 64 deletions(-) create mode 100644 pallets/summary/src/migration.rs diff --git a/Cargo.lock b/Cargo.lock index 6a3657fcd..22e005dcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -483,7 +483,7 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "avn-lower-rpc" -version = "4.1.0" +version = "4.2.0" dependencies = [ "avn-service", "futures", @@ -505,7 +505,7 @@ dependencies = [ [[package]] name = "avn-node-parachain" -version = "4.1.0" +version = "4.2.0" dependencies = [ "avn-lower-rpc", "avn-parachain-runtime", @@ -583,7 +583,7 @@ dependencies = [ [[package]] name = "avn-parachain-runtime" -version = "4.1.0" +version = "4.2.0" dependencies = [ "avn-runtime-common", "cumulus-pallet-aura-ext", @@ -663,7 +663,7 @@ dependencies = [ [[package]] name = "avn-parachain-test-runtime" -version = "4.1.0" +version = "4.2.0" dependencies = [ "avn-runtime-common", "cumulus-pallet-aura-ext", @@ -742,7 +742,7 @@ dependencies = [ [[package]] name = "avn-runtime-common" -version = "4.1.0" +version = "4.2.0" dependencies = [ "frame-support", "hex-literal", @@ -756,7 +756,7 @@ dependencies = [ [[package]] name = "avn-service" -version = "4.1.0" +version = "4.2.0" dependencies = [ "anyhow", "avn-parachain-runtime", @@ -6510,7 +6510,7 @@ dependencies = [ [[package]] name = "pallet-avn" -version = "4.1.0" +version = "4.2.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -6536,7 +6536,7 @@ dependencies = [ [[package]] name = "pallet-avn-offence-handler" -version = "4.1.0" +version = "4.2.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -6557,7 +6557,7 @@ dependencies = [ [[package]] name = "pallet-avn-proxy" -version = "4.1.0" +version = "4.2.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -6581,7 +6581,7 @@ dependencies = [ [[package]] name = "pallet-avn-transaction-payment" -version = "4.1.0" +version = "4.2.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -6863,7 +6863,7 @@ dependencies = [ [[package]] name = "pallet-eth-bridge" -version = "4.1.0" +version = "4.2.0" dependencies = [ "ethabi 13.0.0", "frame-benchmarking", @@ -6889,7 +6889,7 @@ dependencies = [ [[package]] name = "pallet-ethereum-events" -version = "4.1.0" +version = "4.2.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -7063,7 +7063,7 @@ dependencies = [ [[package]] name = "pallet-nft-manager" -version = "4.1.0" +version = "4.2.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -7189,7 +7189,7 @@ dependencies = [ [[package]] name = "pallet-parachain-staking" -version = "4.1.0" +version = "4.2.0" dependencies = [ "assert_matches", "frame-benchmarking", @@ -7451,7 +7451,7 @@ dependencies = [ [[package]] name = "pallet-summary" -version = "4.1.0" +version = "4.2.0" dependencies = [ "assert_matches", "frame-benchmarking", @@ -7459,6 +7459,7 @@ dependencies = [ "frame-system", "hex", "hex-literal", + "log", "node-primitives", "pallet-avn", "pallet-eth-bridge", @@ -7517,7 +7518,7 @@ dependencies = [ [[package]] name = "pallet-token-manager" -version = "4.1.0" +version = "4.2.0" dependencies = [ "enumflags2", "frame-benchmarking", @@ -7626,7 +7627,7 @@ dependencies = [ [[package]] name = "pallet-validators-manager" -version = "4.1.0" +version = "4.2.0" dependencies = [ "frame-benchmarking", "frame-election-provider-support", @@ -11938,7 +11939,7 @@ dependencies = [ [[package]] name = "sp-avn-common" -version = "4.1.0" +version = "4.2.0" dependencies = [ "byte-slice-cast", "derive_more", diff --git a/Cargo.toml b/Cargo.toml index 9e0339b24..a6d543957 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ lto = "fat" codegen-units = 1 [workspace.package] -version = "4.1.0" +version = "4.2.0" authors = ["Aventus systems team"] homepage = "https://www.aventus.io/" repository = "https://github.com/Aventus-Network-Services/avn-node-parachain/" diff --git a/pallets/parachain-staking/src/migration.rs b/pallets/parachain-staking/src/migration.rs index 6dfce9e1a..912016c1f 100644 --- a/pallets/parachain-staking/src/migration.rs +++ b/pallets/parachain-staking/src/migration.rs @@ -134,7 +134,12 @@ impl OnRuntimeUpgrade for EnableEthBridgeWireUp { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { - assert_eq!(true, storage_with_voting::VotingPeriod::::exists()); + let current = Pallet::::current_storage_version(); + let onchain = Pallet::::on_chain_storage_version(); + + if onchain == 2 && current == 3 { + assert_eq!(true, storage_with_voting::VotingPeriod::::exists()); + } Ok(vec![]) } diff --git a/pallets/summary/Cargo.toml b/pallets/summary/Cargo.toml index 95a0fccd4..f8611cc03 100644 --- a/pallets/summary/Cargo.toml +++ b/pallets/summary/Cargo.toml @@ -16,7 +16,7 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features = hex = { version = "0.4.3", default-features = false, features = ["alloc"] } hex-literal = { version = "0.3.4", default-features = false } scale-info = { version = "2.0", default-features = false, features = ["derive"] } - +log = { version = "0.4.17", default-features = false } sp-avn-common = { default-features = false, path = "../../primitives/avn-common" } pallet-avn = { path = "../avn", default-features = false } diff --git a/pallets/summary/src/lib.rs b/pallets/summary/src/lib.rs index 845984e92..db36dbad2 100644 --- a/pallets/summary/src/lib.rs +++ b/pallets/summary/src/lib.rs @@ -25,7 +25,10 @@ use sp_std::prelude::*; use avn::BridgeInterfaceNotification; use core::convert::TryInto; -use frame_support::{dispatch::DispatchResult, ensure, log, traits::Get, weights::Weight}; +use frame_support::{ + dispatch::DispatchResult, ensure, log, pallet_prelude::StorageVersion, traits::Get, + weights::Weight, +}; use frame_system::{ self as system, ensure_none, ensure_root, offchain::{SendTransactionTypes, SubmitTransaction}, @@ -45,6 +48,7 @@ use sp_application_crypto::RuntimeAppPublic; use sp_core::H256; use sp_staking::offence::ReportOffence; +pub mod migration; pub mod offence; use crate::offence::{create_and_report_summary_offence, SummaryOffence, SummaryOffenceType}; @@ -64,6 +68,8 @@ const MIN_VOTING_PERIOD: u32 = 100; // 5 MINUTES const MAX_VOTING_PERIOD: u32 = 28800; // 1 DAY const DEFAULT_VOTING_PERIOD: u32 = 600; // 30 MINUTES +const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + pub mod vote; use crate::vote::*; @@ -120,6 +126,7 @@ pub mod pallet { } #[pallet::pallet] + #[pallet::storage_version(STORAGE_VERSION)] #[pallet::generate_store(pub (super) trait Store)] pub struct Pallet(_); @@ -348,6 +355,8 @@ pub mod pallet { >::put( maybe_first_validator.expect("Validator is checked for none"), ); + + STORAGE_VERSION.put::>(); } } @@ -603,39 +612,6 @@ pub mod pallet { end_voting_if_required::(block_number, &this_validator); challenge_slot_if_required::(block_number, &this_validator); } - - // Note: this "special" function will run during every runtime upgrade. Any complicated - // migration logic should be done in a separate function so it can be tested - // properly. - fn on_runtime_upgrade() -> Weight { - let mut weight_write_counter = 0; - sp_runtime::runtime_logger::RuntimeLogger::init(); - log::info!("ℹ️ Summary pallet data migration invoked"); - - if Self::schedule_period() == 0u32.into() { - log::info!( - "ℹ️ Updating SchedulePeriod to a default value of {} blocks", - DEFAULT_SCHEDULE_PERIOD - ); - weight_write_counter += 1; - >::put(::BlockNumber::from( - DEFAULT_SCHEDULE_PERIOD, - )); - } - - if Self::voting_period() == 0u32.into() { - log::info!( - "ℹ️ Updating VotingPeriod to a default value of {} blocks", - DEFAULT_VOTING_PERIOD - ); - weight_write_counter += 1; - >::put(::BlockNumber::from( - DEFAULT_VOTING_PERIOD, - )); - } - - return T::DbWeight::get().writes(weight_write_counter as u64) - } } #[pallet::validate_unsigned] diff --git a/pallets/summary/src/migration.rs b/pallets/summary/src/migration.rs new file mode 100644 index 000000000..808bf9008 --- /dev/null +++ b/pallets/summary/src/migration.rs @@ -0,0 +1,102 @@ +// AvN is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Moonbeam. If not, see . + +use crate::{ + Clone, Config, Decode, Encode, EthereumTransactionId, Pallet, RootData, Roots, TypeInfo, H256, + STORAGE_VERSION, +}; + +use frame_support::{ + dispatch::GetStorageVersion, + pallet_prelude::*, + traits::{Get, OnRuntimeUpgrade}, + weights::Weight, +}; +use sp_std::prelude::*; + +#[derive(Clone, Encode, Decode, TypeInfo, MaxEncodedLen)] +pub struct OldRootData { + pub root_hash: H256, + pub added_by: Option, + pub is_validated: bool, + pub is_finalised: bool, + pub tx_id: Option, +} + +pub fn migrate_roots() -> Weight { + let mut consumed_weight: Weight = Weight::from_ref_time(0); + let mut add_weight = |reads, writes, weight: Weight| { + consumed_weight += T::DbWeight::get().reads_writes(reads, writes); + consumed_weight += weight; + }; + + let mut counter = 0; + + Roots::::translate::, _>( + |_range, _ingress, old_root_data: OldRootData| { + add_weight(1, 1, Weight::from_ref_time(0)); + + let new_transaction_id: Option = + old_root_data.tx_id.map(|value| value.try_into().ok()).flatten(); + + let new_root_data: RootData = RootData::new( + old_root_data.root_hash, + old_root_data.added_by.expect("Existing data will always have an added by"), + new_transaction_id, + ); + + counter += 1; + Some(new_root_data) + }, + ); + + //Write: [STORAGE_VERSION] + add_weight(0, 1, Weight::from_ref_time(0)); + STORAGE_VERSION.put::>(); + + log::info!( + "✅ Summary root data migration completed successfully. {:?} entries migrated", + counter + ); + + // add a bit extra as safety margin for computation + return consumed_weight + Weight::from_ref_time(25_000_000_000) +} + +/// Migration to enable staking pallet +pub struct MigrateSummaryRootData(PhantomData); +impl OnRuntimeUpgrade for MigrateSummaryRootData { + fn on_runtime_upgrade() -> Weight { + let current = Pallet::::current_storage_version(); + let onchain = Pallet::::on_chain_storage_version(); + + if onchain < 1 { + log::info!( + "🚧 Running summary migration. Current storage version: {:?}, on-chain version: {:?}", + current, + onchain + ); + return migrate_roots::() + } + + Weight::zero() + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + Ok(vec![]) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_input: Vec) -> Result<(), &'static str> { + let onchain_version = Pallet::::on_chain_storage_version(); + frame_support::ensure!(onchain_version == 1, "must_upgrade"); + + Ok(()) + } +} diff --git a/runtime/avn/src/lib.rs b/runtime/avn/src/lib.rs index eb0b8c77b..ce46e5910 100644 --- a/runtime/avn/src/lib.rs +++ b/runtime/avn/src/lib.rs @@ -160,6 +160,7 @@ pub type Executive = frame_executive::Executive< pallet_parachain_staking::migration::EnableEthBridgeWireUp, SeedBridgeTransactionAndDropEthTransactions, pallet_validators_manager::migration::RemovePalletVoting, + pallet_summary::migration::MigrateSummaryRootData, ), >; @@ -172,6 +173,7 @@ const STORAGE_ITEM_NAMES: &[&'static str] = &[ "Nonce", "StorageVersion", ]; + pub struct SeedBridgeTransactionAndDropEthTransactions; impl frame_support::traits::OnRuntimeUpgrade for SeedBridgeTransactionAndDropEthTransactions { @@ -182,7 +184,7 @@ impl frame_support::traits::OnRuntimeUpgrade for SeedBridgeTransactionAndDropEth if onchain < 1 { log::info!( - "🚧 Running migration. Current storage version: {:?}, on-chain version: {:?}", + "🚧 Running eth bridge migration. Current storage version: {:?}, on-chain version: {:?}", current, onchain ); @@ -208,16 +210,26 @@ impl frame_support::traits::OnRuntimeUpgrade for SeedBridgeTransactionAndDropEth #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { - let pre_upgrade_transaction_id: u64 = get_nonce_seed().unwrap(); - Ok((pre_upgrade_transaction_id + 1u64).encode()) + let onchain = pallet_eth_bridge::Pallet::::on_chain_storage_version(); + + if onchain < 1 { + let pre_upgrade_transaction_id: u64 = get_nonce_seed().unwrap(); + return Ok((pre_upgrade_transaction_id + 1u64).encode()) + } + + Ok(vec![]) } #[cfg(feature = "try-runtime")] fn post_upgrade(new_transaction_id_bytes: Vec) -> Result<(), &'static str> { - let next_tx_id: u32 = pallet_eth_bridge::Pallet::::get_next_tx_id(); - let expiry = as pallet_eth_bridge::Store>::EthTxLifetimeSecs::get(); - assert_eq!((next_tx_id as u64).encode(), new_transaction_id_bytes); - assert_eq!(expiry, 7200); + let current = pallet_eth_bridge::Pallet::::current_storage_version(); + + if current == 1 { + let next_tx_id: u32 = pallet_eth_bridge::Pallet::::get_next_tx_id(); + let expiry = as pallet_eth_bridge::Store>::EthTxLifetimeSecs::get(); + assert_eq!((next_tx_id as u64).encode(), new_transaction_id_bytes); + assert_eq!(expiry, 7200); + } Ok(()) } } @@ -284,7 +296,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("avn-parachain"), impl_name: create_runtime_str!("avn-parachain"), authoring_version: 1, - spec_version: 56, + spec_version: 57, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1, diff --git a/runtime/test/src/lib.rs b/runtime/test/src/lib.rs index 224f3f789..21d1a4355 100644 --- a/runtime/test/src/lib.rs +++ b/runtime/test/src/lib.rs @@ -268,7 +268,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("avn-test-parachain"), impl_name: create_runtime_str!("avn-test-parachain"), authoring_version: 1, - spec_version: 56, + spec_version: 57, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1,