From bb159eea7a84300eb810c193427de9dbef650176 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Tue, 4 Oct 2022 02:08:23 +0800 Subject: [PATCH 1/5] Migrate Weights properly to v2 --- Cargo.lock | 1 + pallets/dmp-queue/Cargo.toml | 2 + pallets/dmp-queue/src/lib.rs | 23 ++++--- pallets/dmp-queue/src/migration.rs | 103 ++++++++++++++++++++++++++++ pallets/xcmp-queue/src/lib.rs | 20 +++--- pallets/xcmp-queue/src/migration.rs | 70 +++++++++---------- pallets/xcmp-queue/src/tests.rs | 22 +++--- 7 files changed, 172 insertions(+), 69 deletions(-) create mode 100644 pallets/dmp-queue/src/migration.rs diff --git a/Cargo.lock b/Cargo.lock index 198a6a37345..b69e97b4725 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1749,6 +1749,7 @@ dependencies = [ "cumulus-primitives-core", "frame-support", "frame-system", + "log", "parity-scale-codec", "scale-info", "sp-core", diff --git a/pallets/dmp-queue/Cargo.toml b/pallets/dmp-queue/Cargo.toml index 5f423e84fc3..dda503be7af 100644 --- a/pallets/dmp-queue/Cargo.toml +++ b/pallets/dmp-queue/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] codec = { package = "parity-scale-codec", version = "3.0.0", features = [ "derive" ], default-features = false } +log = { version = "0.4.17", default-features = false } scale-info = { version = "2.2.0", default-features = false, features = ["derive"] } # Substrate @@ -32,6 +33,7 @@ std = [ "scale-info/std", "frame-support/std", "frame-system/std", + "log/std", "sp-io/std", "sp-runtime/std", "sp-std/std", diff --git a/pallets/dmp-queue/src/lib.rs b/pallets/dmp-queue/src/lib.rs index cfcae5f3ad8..2d74c74df46 100644 --- a/pallets/dmp-queue/src/lib.rs +++ b/pallets/dmp-queue/src/lib.rs @@ -21,6 +21,8 @@ #![cfg_attr(not(feature = "std"), no_std)] +pub mod migration; + use codec::{Decode, DecodeLimit, Encode}; use cumulus_primitives_core::{relay_chain::BlockNumber as RelayBlockNumber, DmpMessageHandler}; use frame_support::{ @@ -31,7 +33,7 @@ pub use pallet::*; use scale_info::TypeInfo; use sp_runtime::RuntimeDebug; use sp_std::{convert::TryFrom, prelude::*}; -use xcm::{latest::prelude::*, VersionedXcm, MAX_XCM_DECODE_DEPTH}; +use xcm::{latest::{prelude::*, Weight as XcmWeight}, VersionedXcm, MAX_XCM_DECODE_DEPTH}; #[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)] pub struct ConfigData { @@ -78,6 +80,7 @@ pub mod pallet { #[pallet::pallet] #[pallet::generate_store(pub(super) trait Store)] + #[pallet::storage_version(migration::STORAGE_VERSION)] #[pallet::without_storage_info] pub struct Pallet(_); @@ -141,16 +144,16 @@ pub mod pallet { /// /// Events: /// - `OverweightServiced`: On success. - #[pallet::weight(weight_limit.saturating_add(Weight::from_ref_time(1_000_000)))] + #[pallet::weight(Weight::from_ref_time(weight_limit.saturating_add(1_000_000)))] pub fn service_overweight( origin: OriginFor, index: OverweightIndex, - weight_limit: Weight, + weight_limit: XcmWeight, ) -> DispatchResultWithPostInfo { T::ExecuteOverweightOrigin::ensure_origin(origin)?; let (sent_at, data) = Overweight::::get(index).ok_or(Error::::Unknown)?; - let weight_used = Self::try_service_message(weight_limit, sent_at, &data[..]) + let weight_used = Self::try_service_message(Weight::from_ref_time(weight_limit), sent_at, &data[..]) .map_err(|_| Error::::OverLimit)?; Overweight::::remove(index); Self::deposit_event(Event::OverweightServiced { overweight_index: index, weight_used }); @@ -747,7 +750,7 @@ mod tests { DmpQueue::service_overweight( RuntimeOrigin::signed(1), 0, - Weight::from_ref_time(20000) + 20000 ), BadOrigin ); @@ -755,25 +758,25 @@ mod tests { DmpQueue::service_overweight( RuntimeOrigin::root(), 1, - Weight::from_ref_time(20000) + 20000 ), Error::::Unknown ); assert_noop!( - DmpQueue::service_overweight(RuntimeOrigin::root(), 0, Weight::from_ref_time(9999)), + DmpQueue::service_overweight(RuntimeOrigin::root(), 0, 9999), Error::::OverLimit ); assert_eq!(take_trace(), vec![msg_limit_reached(10000)]); let base_weight = - super::Call::::service_overweight { index: 0, weight_limit: Weight::zero() } + super::Call::::service_overweight { index: 0, weight_limit: 0 } .get_dispatch_info() .weight; use frame_support::dispatch::GetDispatchInfo; let info = DmpQueue::service_overweight( RuntimeOrigin::root(), 0, - Weight::from_ref_time(20000), + 20000, ) .unwrap(); let actual_weight = info.actual_weight.unwrap(); @@ -785,7 +788,7 @@ mod tests { DmpQueue::service_overweight( RuntimeOrigin::root(), 0, - Weight::from_ref_time(20000) + 20000 ), Error::::Unknown ); diff --git a/pallets/dmp-queue/src/migration.rs b/pallets/dmp-queue/src/migration.rs new file mode 100644 index 00000000000..84b0f548340 --- /dev/null +++ b/pallets/dmp-queue/src/migration.rs @@ -0,0 +1,103 @@ +// Copyright 2022 Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot 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 Polkadot. If not, see . + +//! A module that is responsible for migration of storage. + +use crate::{Config, Pallet, Store}; +use frame_support::{pallet_prelude::*, traits::StorageVersion, weights::{constants::WEIGHT_PER_MILLIS, Weight}}; +use xcm::latest::Weight as XcmWeight; + +/// The current storage version. +pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + +/// Migrates the pallet storage to the most recent version, checking and setting the +/// `StorageVersion`. +pub fn migrate_to_latest() -> Weight { + let mut weight = Weight::zero(); + + if StorageVersion::get::>() == 0 { + weight += migrate_to_v1::(); + StorageVersion::new(1).put::>(); + } + + weight +} + +mod v0 { + use super::*; + use codec::{Decode, Encode}; + + #[derive(Decode, Encode, Debug)] + pub struct ConfigData { + pub max_individual: XcmWeight, + } + + impl Default for ConfigData { + fn default() -> Self { + ConfigData { + max_individual: 10u64 * WEIGHT_PER_MILLIS.ref_time(), + } + } + } +} + +/// Migrates `QueueConfigData` from v1 (using only reference time weights) to v2 (with +/// 2D weights). +/// +/// NOTE: Only use this function if you know what you're doing. Default to using +/// `migrate_to_latest`. +pub fn migrate_to_v1() -> Weight { + let translate = |pre: v0::ConfigData| -> super::ConfigData { + super::ConfigData { + max_individual: Weight::from_ref_time(pre.max_individual), + } + }; + + if let Err(_) = as Store>::Configuration::translate(|pre| pre.map(translate)) { + log::error!( + target: "dmp_queue", + "unexpected error when performing translation of the QueueConfig type during storage upgrade to v2" + ); + } + + T::DbWeight::get().reads_writes(1, 1) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::tests::{new_test_ext, Test}; + + #[test] + fn test_migration_to_v1() { + let v0 = v0::ConfigData { + max_individual: 30_000_000_000, + }; + + new_test_ext().execute_with(|| { + frame_support::storage::unhashed::put_raw( + &crate::Configuration::::hashed_key(), + &v0.encode(), + ); + + migrate_to_v1::(); + + let v1 = crate::Configuration::::get(); + + assert_eq!(v0.max_individual, v1.max_individual.ref_time()); + }); + } +} diff --git a/pallets/xcmp-queue/src/lib.rs b/pallets/xcmp-queue/src/lib.rs index 22d1d4c1bb6..0bf5941fa29 100644 --- a/pallets/xcmp-queue/src/lib.rs +++ b/pallets/xcmp-queue/src/lib.rs @@ -54,7 +54,7 @@ use rand_chacha::{ use scale_info::TypeInfo; use sp_runtime::{traits::Hash, RuntimeDebug}; use sp_std::{convert::TryFrom, prelude::*}; -use xcm::{latest::prelude::*, VersionedXcm, WrapVersion, MAX_XCM_DECODE_DEPTH}; +use xcm::{latest::{prelude::*, Weight as XcmWeight}, VersionedXcm, WrapVersion, MAX_XCM_DECODE_DEPTH}; use xcm_executor::traits::ConvertOrigin; pub use pallet::*; @@ -130,11 +130,11 @@ pub mod pallet { /// /// Events: /// - `OverweightServiced`: On success. - #[pallet::weight((weight_limit.saturating_add(Weight::from_ref_time(1_000_000)), DispatchClass::Operational,))] + #[pallet::weight((Weight::from_ref_time(weight_limit.saturating_add(1_000_000)), DispatchClass::Operational,))] pub fn service_overweight( origin: OriginFor, index: OverweightIndex, - weight_limit: Weight, + weight_limit: XcmWeight, ) -> DispatchResultWithPostInfo { T::ExecuteOverweightOrigin::ensure_origin(origin)?; @@ -145,7 +145,7 @@ pub mod pallet { &mut data.as_slice(), ) .map_err(|_| Error::::BadXcm)?; - let used = Self::handle_xcm_message(sender, sent_at, xcm, weight_limit) + let used = Self::handle_xcm_message(sender, sent_at, xcm, Weight::from_ref_time(weight_limit)) .map_err(|_| Error::::WeightOverLimit)?; Overweight::::remove(index); Self::deposit_event(Event::OverweightServiced { index, used }); @@ -222,9 +222,9 @@ pub mod pallet { /// - `origin`: Must pass `Root`. /// - `new`: Desired value for `QueueConfigData.threshold_weight` #[pallet::weight((T::WeightInfo::set_config_with_weight(), DispatchClass::Operational,))] - pub fn update_threshold_weight(origin: OriginFor, new: Weight) -> DispatchResult { + pub fn update_threshold_weight(origin: OriginFor, new: XcmWeight) -> DispatchResult { ensure_root(origin)?; - QueueConfig::::mutate(|data| data.threshold_weight = new); + QueueConfig::::mutate(|data| data.threshold_weight = Weight::from_ref_time(new)); Ok(()) } @@ -235,9 +235,9 @@ pub mod pallet { /// - `origin`: Must pass `Root`. /// - `new`: Desired value for `QueueConfigData.weight_restrict_decay`. #[pallet::weight((T::WeightInfo::set_config_with_weight(), DispatchClass::Operational,))] - pub fn update_weight_restrict_decay(origin: OriginFor, new: Weight) -> DispatchResult { + pub fn update_weight_restrict_decay(origin: OriginFor, new: XcmWeight) -> DispatchResult { ensure_root(origin)?; - QueueConfig::::mutate(|data| data.weight_restrict_decay = new); + QueueConfig::::mutate(|data| data.weight_restrict_decay = Weight::from_ref_time(new)); Ok(()) } @@ -250,10 +250,10 @@ pub mod pallet { #[pallet::weight((T::WeightInfo::set_config_with_weight(), DispatchClass::Operational,))] pub fn update_xcmp_max_individual_weight( origin: OriginFor, - new: Weight, + new: XcmWeight, ) -> DispatchResult { ensure_root(origin)?; - QueueConfig::::mutate(|data| data.xcmp_max_individual_weight = new); + QueueConfig::::mutate(|data| data.xcmp_max_individual_weight = Weight::from_ref_time(new)); Ok(()) } diff --git a/pallets/xcmp-queue/src/migration.rs b/pallets/xcmp-queue/src/migration.rs index bedd5ffd6ea..750032516a8 100644 --- a/pallets/xcmp-queue/src/migration.rs +++ b/pallets/xcmp-queue/src/migration.rs @@ -17,25 +17,26 @@ //! A module that is responsible for migration of storage. use crate::{Config, Pallet, Store}; -use frame_support::{pallet_prelude::*, traits::StorageVersion, weights::Weight}; +use frame_support::{pallet_prelude::*, traits::StorageVersion, weights::{constants::WEIGHT_PER_MILLIS, Weight}}; +use xcm::latest::Weight as XcmWeight; /// The current storage version. -pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); +pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(2); /// Migrates the pallet storage to the most recent version, checking and setting the /// `StorageVersion`. pub fn migrate_to_latest() -> Weight { let mut weight = Weight::zero(); - if StorageVersion::get::>() == 0 { - weight += migrate_to_v1::(); - StorageVersion::new(1).put::>(); + if StorageVersion::get::>() == 1 { + weight += migrate_to_v2::(); + StorageVersion::new(2).put::>(); } weight } -mod v0 { +mod v1 { use super::*; use codec::{Decode, Encode}; @@ -44,8 +45,9 @@ mod v0 { pub suspend_threshold: u32, pub drop_threshold: u32, pub resume_threshold: u32, - pub threshold_weight: Weight, - pub weight_restrict_decay: Weight, + pub threshold_weight: XcmWeight, + pub weight_restrict_decay: XcmWeight, + pub xcmp_max_individual_weight: XcmWeight, } impl Default for QueueConfigData { @@ -54,37 +56,35 @@ mod v0 { suspend_threshold: 2, drop_threshold: 5, resume_threshold: 1, - threshold_weight: Weight::from_ref_time(100_000), - weight_restrict_decay: Weight::from_ref_time(2), + threshold_weight: 100_000, + weight_restrict_decay: 2, + xcmp_max_individual_weight: 20u64 * WEIGHT_PER_MILLIS.ref_time(), } } } } -/// Migrates `QueueConfigData` from v0 (without the `xcmp_max_individual_weight` field) to v1 (with -/// max individual weight). -/// Uses the `Default` implementation of `QueueConfigData` to choose a value for -/// `xcmp_max_individual_weight`. +/// Migrates `QueueConfigData` from v1 (using only reference time weights) to v2 (with +/// 2D weights). /// /// NOTE: Only use this function if you know what you're doing. Default to using /// `migrate_to_latest`. -pub fn migrate_to_v1() -> Weight { - let translate = |pre: v0::QueueConfigData| -> super::QueueConfigData { +pub fn migrate_to_v2() -> Weight { + let translate = |pre: v1::QueueConfigData| -> super::QueueConfigData { super::QueueConfigData { suspend_threshold: pre.suspend_threshold, drop_threshold: pre.drop_threshold, resume_threshold: pre.resume_threshold, - threshold_weight: pre.threshold_weight, - weight_restrict_decay: pre.weight_restrict_decay, - xcmp_max_individual_weight: super::QueueConfigData::default() - .xcmp_max_individual_weight, + threshold_weight: Weight::from_ref_time(pre.threshold_weight), + weight_restrict_decay: Weight::from_ref_time(pre.weight_restrict_decay), + xcmp_max_individual_weight: Weight::from_ref_time(pre.xcmp_max_individual_weight), } }; if let Err(_) = as Store>::QueueConfig::translate(|pre| pre.map(translate)) { log::error!( target: super::LOG_TARGET, - "unexpected error when performing translation of the QueueConfig type during storage upgrade to v1" + "unexpected error when performing translation of the QueueConfig type during storage upgrade to v2" ); } @@ -97,32 +97,32 @@ mod tests { use crate::mock::{new_test_ext, Test}; #[test] - fn test_migration_to_v1() { - let v0 = v0::QueueConfigData { + fn test_migration_to_v2() { + let v1 = v1::QueueConfigData { suspend_threshold: 5, drop_threshold: 12, resume_threshold: 3, - threshold_weight: Weight::from_ref_time(333_333), - weight_restrict_decay: Weight::from_ref_time(1), + threshold_weight: 333_333, + weight_restrict_decay: 1, + xcmp_max_individual_weight: 10_000_000_000, }; new_test_ext().execute_with(|| { - // Put the v0 version in the state frame_support::storage::unhashed::put_raw( &crate::QueueConfig::::hashed_key(), - &v0.encode(), + &v1.encode(), ); - migrate_to_v1::(); + migrate_to_v2::(); - let v1 = crate::QueueConfig::::get(); + let v2 = crate::QueueConfig::::get(); - assert_eq!(v0.suspend_threshold, v1.suspend_threshold); - assert_eq!(v0.drop_threshold, v1.drop_threshold); - assert_eq!(v0.resume_threshold, v1.resume_threshold); - assert_eq!(v0.threshold_weight, v1.threshold_weight); - assert_eq!(v0.weight_restrict_decay, v1.weight_restrict_decay); - assert_eq!(v1.xcmp_max_individual_weight, Weight::from_ref_time(20_000_000_000)); + assert_eq!(v1.suspend_threshold, v2.suspend_threshold); + assert_eq!(v1.drop_threshold, v2.drop_threshold); + assert_eq!(v1.resume_threshold, v2.resume_threshold); + assert_eq!(v1.threshold_weight, v2.threshold_weight.ref_time()); + assert_eq!(v1.weight_restrict_decay, v2.weight_restrict_decay.ref_time()); + assert_eq!(v1.xcmp_max_individual_weight, v2.xcmp_max_individual_weight.ref_time()); }); } } diff --git a/pallets/xcmp-queue/src/tests.rs b/pallets/xcmp-queue/src/tests.rs index 1b6303ddaf1..269db1e4998 100644 --- a/pallets/xcmp-queue/src/tests.rs +++ b/pallets/xcmp-queue/src/tests.rs @@ -96,7 +96,7 @@ fn handle_invalid_data() { fn service_overweight_unknown() { new_test_ext().execute_with(|| { assert_noop!( - XcmpQueue::service_overweight(RuntimeOrigin::root(), 0, Weight::from_ref_time(1000)), + XcmpQueue::service_overweight(RuntimeOrigin::root(), 0, 1000), Error::::BadOverweightIndex, ); }); @@ -109,7 +109,7 @@ fn service_overweight_bad_xcm_format() { Overweight::::insert(0, (ParaId::from(1000), 0, bad_xcm)); assert_noop!( - XcmpQueue::service_overweight(RuntimeOrigin::root(), 0, Weight::from_ref_time(1000)), + XcmpQueue::service_overweight(RuntimeOrigin::root(), 0, 1000), Error::::BadXcm ); }); @@ -189,12 +189,12 @@ fn update_threshold_weight_works() { assert_eq!(data.threshold_weight, Weight::from_ref_time(100_000)); assert_ok!(XcmpQueue::update_threshold_weight( RuntimeOrigin::root(), - Weight::from_ref_time(10_000) + 10_000 )); assert_noop!( XcmpQueue::update_threshold_weight( RuntimeOrigin::signed(5), - Weight::from_ref_time(10_000_000) + 10_000_000 ), BadOrigin ); @@ -209,15 +209,9 @@ fn update_weight_restrict_decay_works() { new_test_ext().execute_with(|| { let data: QueueConfigData = >::get(); assert_eq!(data.weight_restrict_decay, Weight::from_ref_time(2)); - assert_ok!(XcmpQueue::update_weight_restrict_decay( - RuntimeOrigin::root(), - Weight::from_ref_time(5) - )); + assert_ok!(XcmpQueue::update_weight_restrict_decay(RuntimeOrigin::root(), 5)); assert_noop!( - XcmpQueue::update_weight_restrict_decay( - RuntimeOrigin::signed(6), - Weight::from_ref_time(4) - ), + XcmpQueue::update_weight_restrict_decay(RuntimeOrigin::signed(6), 4), BadOrigin ); let data: QueueConfigData = >::get(); @@ -233,12 +227,12 @@ fn update_xcmp_max_individual_weight() { assert_eq!(data.xcmp_max_individual_weight, 20u64 * WEIGHT_PER_MILLIS); assert_ok!(XcmpQueue::update_xcmp_max_individual_weight( RuntimeOrigin::root(), - 30u64 * WEIGHT_PER_MILLIS + 30u64 * WEIGHT_PER_MILLIS.ref_time() )); assert_noop!( XcmpQueue::update_xcmp_max_individual_weight( RuntimeOrigin::signed(3), - 10u64 * WEIGHT_PER_MILLIS + 10u64 * WEIGHT_PER_MILLIS.ref_time() ), BadOrigin ); From d8f6de61fcbae96f7788d5202859914f430a50d0 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Tue, 4 Oct 2022 12:49:07 +0800 Subject: [PATCH 2/5] Add missing on_runtime_upgrade implementation --- pallets/dmp-queue/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pallets/dmp-queue/src/lib.rs b/pallets/dmp-queue/src/lib.rs index 2d74c74df46..f29bf40ea61 100644 --- a/pallets/dmp-queue/src/lib.rs +++ b/pallets/dmp-queue/src/lib.rs @@ -124,6 +124,10 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { + fn on_runtime_upgrade() -> Weight { + migration::migrate_to_latest::() + } + fn on_idle(_now: T::BlockNumber, max_weight: Weight) -> Weight { // on_idle processes additional messages with any remaining block weight. Self::service_queue(max_weight) From 500e03086f58124d51cc53150e5b779adf5f47c3 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Tue, 4 Oct 2022 13:32:07 +0800 Subject: [PATCH 3/5] Fix benchmarks --- pallets/xcmp-queue/src/benchmarking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/xcmp-queue/src/benchmarking.rs b/pallets/xcmp-queue/src/benchmarking.rs index 5ac78bee763..5d34d499dbe 100644 --- a/pallets/xcmp-queue/src/benchmarking.rs +++ b/pallets/xcmp-queue/src/benchmarking.rs @@ -22,7 +22,7 @@ use frame_system::RawOrigin; benchmarks! { set_config_with_u32 {}: update_resume_threshold(RawOrigin::Root, 100) - set_config_with_weight {}: update_weight_restrict_decay(RawOrigin::Root, Weight::from_ref_time(3_000_000)) + set_config_with_weight {}: update_weight_restrict_decay(RawOrigin::Root, 3_000_000) } impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test); From 10b9cda2f9886e28f7b0312bb593c6e3e35fd6c3 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Tue, 11 Oct 2022 22:37:56 +0800 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: Oliver Tale-Yazdi --- pallets/dmp-queue/src/migration.rs | 2 +- pallets/xcmp-queue/src/migration.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/dmp-queue/src/migration.rs b/pallets/dmp-queue/src/migration.rs index 84b0f548340..2052fbe5e9b 100644 --- a/pallets/dmp-queue/src/migration.rs +++ b/pallets/dmp-queue/src/migration.rs @@ -26,7 +26,7 @@ pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); /// Migrates the pallet storage to the most recent version, checking and setting the /// `StorageVersion`. pub fn migrate_to_latest() -> Weight { - let mut weight = Weight::zero(); + let mut weight = T::DbWeight::get().reads(1); if StorageVersion::get::>() == 0 { weight += migrate_to_v1::(); diff --git a/pallets/xcmp-queue/src/migration.rs b/pallets/xcmp-queue/src/migration.rs index 750032516a8..16f579222b1 100644 --- a/pallets/xcmp-queue/src/migration.rs +++ b/pallets/xcmp-queue/src/migration.rs @@ -26,7 +26,7 @@ pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(2); /// Migrates the pallet storage to the most recent version, checking and setting the /// `StorageVersion`. pub fn migrate_to_latest() -> Weight { - let mut weight = Weight::zero(); + let mut weight = T::DbWeight::get().reads(1); if StorageVersion::get::>() == 1 { weight += migrate_to_v2::(); From 40cd501bab2393bfde673d842f809787d4da9688 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Tue, 11 Oct 2022 23:59:49 +0800 Subject: [PATCH 5/5] cargo fmt --- pallets/dmp-queue/src/lib.rs | 42 +++++++------------ pallets/dmp-queue/src/migration.rs | 42 +++++++++---------- pallets/xcmp-queue/src/lib.rs | 23 +++++++--- pallets/xcmp-queue/src/migration.rs | 6 ++- pallets/xcmp-queue/src/tests.rs | 10 +---- parachain-template/runtime/src/lib.rs | 3 +- parachains/common/src/lib.rs | 3 +- .../runtimes/starters/seedling/src/lib.rs | 3 +- parachains/runtimes/starters/shell/src/lib.rs | 3 +- parachains/runtimes/testing/penpal/src/lib.rs | 3 +- .../testing/rococo-parachain/src/lib.rs | 3 +- test/runtime/src/lib.rs | 3 +- 12 files changed, 72 insertions(+), 72 deletions(-) diff --git a/pallets/dmp-queue/src/lib.rs b/pallets/dmp-queue/src/lib.rs index f29bf40ea61..75ebe2c395f 100644 --- a/pallets/dmp-queue/src/lib.rs +++ b/pallets/dmp-queue/src/lib.rs @@ -33,7 +33,10 @@ pub use pallet::*; use scale_info::TypeInfo; use sp_runtime::RuntimeDebug; use sp_std::{convert::TryFrom, prelude::*}; -use xcm::{latest::{prelude::*, Weight as XcmWeight}, VersionedXcm, MAX_XCM_DECODE_DEPTH}; +use xcm::{ + latest::{prelude::*, Weight as XcmWeight}, + VersionedXcm, MAX_XCM_DECODE_DEPTH, +}; #[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)] pub struct ConfigData { @@ -157,8 +160,9 @@ pub mod pallet { T::ExecuteOverweightOrigin::ensure_origin(origin)?; let (sent_at, data) = Overweight::::get(index).ok_or(Error::::Unknown)?; - let weight_used = Self::try_service_message(Weight::from_ref_time(weight_limit), sent_at, &data[..]) - .map_err(|_| Error::::OverLimit)?; + let weight_used = + Self::try_service_message(Weight::from_ref_time(weight_limit), sent_at, &data[..]) + .map_err(|_| Error::::OverLimit)?; Overweight::::remove(index); Self::deposit_event(Event::OverweightServiced { overweight_index: index, weight_used }); Ok(Some(weight_used.saturating_add(Weight::from_ref_time(1_000_000))).into()) @@ -751,19 +755,11 @@ mod tests { assert_eq!(overweights(), vec![0]); assert_noop!( - DmpQueue::service_overweight( - RuntimeOrigin::signed(1), - 0, - 20000 - ), + DmpQueue::service_overweight(RuntimeOrigin::signed(1), 0, 20000), BadOrigin ); assert_noop!( - DmpQueue::service_overweight( - RuntimeOrigin::root(), - 1, - 20000 - ), + DmpQueue::service_overweight(RuntimeOrigin::root(), 1, 20000), Error::::Unknown ); assert_noop!( @@ -772,28 +768,18 @@ mod tests { ); assert_eq!(take_trace(), vec![msg_limit_reached(10000)]); - let base_weight = - super::Call::::service_overweight { index: 0, weight_limit: 0 } - .get_dispatch_info() - .weight; + let base_weight = super::Call::::service_overweight { index: 0, weight_limit: 0 } + .get_dispatch_info() + .weight; use frame_support::dispatch::GetDispatchInfo; - let info = DmpQueue::service_overweight( - RuntimeOrigin::root(), - 0, - 20000, - ) - .unwrap(); + let info = DmpQueue::service_overweight(RuntimeOrigin::root(), 0, 20000).unwrap(); let actual_weight = info.actual_weight.unwrap(); assert_eq!(actual_weight, base_weight + Weight::from_ref_time(10000)); assert_eq!(take_trace(), vec![msg_complete(10000)]); assert!(overweights().is_empty()); assert_noop!( - DmpQueue::service_overweight( - RuntimeOrigin::root(), - 0, - 20000 - ), + DmpQueue::service_overweight(RuntimeOrigin::root(), 0, 20000), Error::::Unknown ); }); diff --git a/pallets/dmp-queue/src/migration.rs b/pallets/dmp-queue/src/migration.rs index 2052fbe5e9b..cfd8a1f78e3 100644 --- a/pallets/dmp-queue/src/migration.rs +++ b/pallets/dmp-queue/src/migration.rs @@ -17,7 +17,11 @@ //! A module that is responsible for migration of storage. use crate::{Config, Pallet, Store}; -use frame_support::{pallet_prelude::*, traits::StorageVersion, weights::{constants::WEIGHT_PER_MILLIS, Weight}}; +use frame_support::{ + pallet_prelude::*, + traits::StorageVersion, + weights::{constants::WEIGHT_PER_MILLIS, Weight}, +}; use xcm::latest::Weight as XcmWeight; /// The current storage version. @@ -37,21 +41,19 @@ pub fn migrate_to_latest() -> Weight { } mod v0 { - use super::*; - use codec::{Decode, Encode}; - - #[derive(Decode, Encode, Debug)] - pub struct ConfigData { - pub max_individual: XcmWeight, - } - - impl Default for ConfigData { - fn default() -> Self { - ConfigData { - max_individual: 10u64 * WEIGHT_PER_MILLIS.ref_time(), - } - } - } + use super::*; + use codec::{Decode, Encode}; + + #[derive(Decode, Encode, Debug)] + pub struct ConfigData { + pub max_individual: XcmWeight, + } + + impl Default for ConfigData { + fn default() -> Self { + ConfigData { max_individual: 10u64 * WEIGHT_PER_MILLIS.ref_time() } + } + } } /// Migrates `QueueConfigData` from v1 (using only reference time weights) to v2 (with @@ -61,9 +63,7 @@ mod v0 { /// `migrate_to_latest`. pub fn migrate_to_v1() -> Weight { let translate = |pre: v0::ConfigData| -> super::ConfigData { - super::ConfigData { - max_individual: Weight::from_ref_time(pre.max_individual), - } + super::ConfigData { max_individual: Weight::from_ref_time(pre.max_individual) } }; if let Err(_) = as Store>::Configuration::translate(|pre| pre.map(translate)) { @@ -83,9 +83,7 @@ mod tests { #[test] fn test_migration_to_v1() { - let v0 = v0::ConfigData { - max_individual: 30_000_000_000, - }; + let v0 = v0::ConfigData { max_individual: 30_000_000_000 }; new_test_ext().execute_with(|| { frame_support::storage::unhashed::put_raw( diff --git a/pallets/xcmp-queue/src/lib.rs b/pallets/xcmp-queue/src/lib.rs index 0bf5941fa29..6745e192212 100644 --- a/pallets/xcmp-queue/src/lib.rs +++ b/pallets/xcmp-queue/src/lib.rs @@ -54,7 +54,10 @@ use rand_chacha::{ use scale_info::TypeInfo; use sp_runtime::{traits::Hash, RuntimeDebug}; use sp_std::{convert::TryFrom, prelude::*}; -use xcm::{latest::{prelude::*, Weight as XcmWeight}, VersionedXcm, WrapVersion, MAX_XCM_DECODE_DEPTH}; +use xcm::{ + latest::{prelude::*, Weight as XcmWeight}, + VersionedXcm, WrapVersion, MAX_XCM_DECODE_DEPTH, +}; use xcm_executor::traits::ConvertOrigin; pub use pallet::*; @@ -145,8 +148,9 @@ pub mod pallet { &mut data.as_slice(), ) .map_err(|_| Error::::BadXcm)?; - let used = Self::handle_xcm_message(sender, sent_at, xcm, Weight::from_ref_time(weight_limit)) - .map_err(|_| Error::::WeightOverLimit)?; + let used = + Self::handle_xcm_message(sender, sent_at, xcm, Weight::from_ref_time(weight_limit)) + .map_err(|_| Error::::WeightOverLimit)?; Overweight::::remove(index); Self::deposit_event(Event::OverweightServiced { index, used }); Ok(Some(used.saturating_add(Weight::from_ref_time(1_000_000))).into()) @@ -235,9 +239,14 @@ pub mod pallet { /// - `origin`: Must pass `Root`. /// - `new`: Desired value for `QueueConfigData.weight_restrict_decay`. #[pallet::weight((T::WeightInfo::set_config_with_weight(), DispatchClass::Operational,))] - pub fn update_weight_restrict_decay(origin: OriginFor, new: XcmWeight) -> DispatchResult { + pub fn update_weight_restrict_decay( + origin: OriginFor, + new: XcmWeight, + ) -> DispatchResult { ensure_root(origin)?; - QueueConfig::::mutate(|data| data.weight_restrict_decay = Weight::from_ref_time(new)); + QueueConfig::::mutate(|data| { + data.weight_restrict_decay = Weight::from_ref_time(new) + }); Ok(()) } @@ -253,7 +262,9 @@ pub mod pallet { new: XcmWeight, ) -> DispatchResult { ensure_root(origin)?; - QueueConfig::::mutate(|data| data.xcmp_max_individual_weight = Weight::from_ref_time(new)); + QueueConfig::::mutate(|data| { + data.xcmp_max_individual_weight = Weight::from_ref_time(new) + }); Ok(()) } diff --git a/pallets/xcmp-queue/src/migration.rs b/pallets/xcmp-queue/src/migration.rs index 16f579222b1..e44a053b761 100644 --- a/pallets/xcmp-queue/src/migration.rs +++ b/pallets/xcmp-queue/src/migration.rs @@ -17,7 +17,11 @@ //! A module that is responsible for migration of storage. use crate::{Config, Pallet, Store}; -use frame_support::{pallet_prelude::*, traits::StorageVersion, weights::{constants::WEIGHT_PER_MILLIS, Weight}}; +use frame_support::{ + pallet_prelude::*, + traits::StorageVersion, + weights::{constants::WEIGHT_PER_MILLIS, Weight}, +}; use xcm::latest::Weight as XcmWeight; /// The current storage version. diff --git a/pallets/xcmp-queue/src/tests.rs b/pallets/xcmp-queue/src/tests.rs index 269db1e4998..1586e313b5a 100644 --- a/pallets/xcmp-queue/src/tests.rs +++ b/pallets/xcmp-queue/src/tests.rs @@ -187,15 +187,9 @@ fn update_threshold_weight_works() { new_test_ext().execute_with(|| { let data: QueueConfigData = >::get(); assert_eq!(data.threshold_weight, Weight::from_ref_time(100_000)); - assert_ok!(XcmpQueue::update_threshold_weight( - RuntimeOrigin::root(), - 10_000 - )); + assert_ok!(XcmpQueue::update_threshold_weight(RuntimeOrigin::root(), 10_000)); assert_noop!( - XcmpQueue::update_threshold_weight( - RuntimeOrigin::signed(5), - 10_000_000 - ), + XcmpQueue::update_threshold_weight(RuntimeOrigin::signed(5), 10_000_000), BadOrigin ); let data: QueueConfigData = >::get(); diff --git a/parachain-template/runtime/src/lib.rs b/parachain-template/runtime/src/lib.rs index 3ad6a055e82..a52b4982636 100644 --- a/parachain-template/runtime/src/lib.rs +++ b/parachain-template/runtime/src/lib.rs @@ -217,7 +217,8 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5); const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); /// We allow for 0.5 of a second of compute with a 12 second average block time. -const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND.saturating_div(2) +const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND + .saturating_div(2) .set_proof_size(cumulus_primitives_core::relay_chain::v2::MAX_POV_SIZE as u64); /// The version information used to identify this runtime when compiled natively. diff --git a/parachains/common/src/lib.rs b/parachains/common/src/lib.rs index 1ad4dc2f85c..c192d6e2a7e 100644 --- a/parachains/common/src/lib.rs +++ b/parachains/common/src/lib.rs @@ -92,7 +92,8 @@ mod constants { pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); /// We allow for 0.5 seconds of compute with a 6 second average block time. - pub const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND.saturating_div(2) + pub const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND + .saturating_div(2) .set_proof_size(polkadot_primitives::v2::MAX_POV_SIZE as u64); } diff --git a/parachains/runtimes/starters/seedling/src/lib.rs b/parachains/runtimes/starters/seedling/src/lib.rs index 84327c4dc7d..67cc9dede21 100644 --- a/parachains/runtimes/starters/seedling/src/lib.rs +++ b/parachains/runtimes/starters/seedling/src/lib.rs @@ -85,7 +85,8 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10); /// by Operational extrinsics. const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); /// We allow for .5 seconds of compute with a 12 second average block time. -const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND.saturating_div(2) +const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND + .saturating_div(2) .set_proof_size(cumulus_primitives_core::relay_chain::v2::MAX_POV_SIZE as u64); parameter_types! { diff --git a/parachains/runtimes/starters/shell/src/lib.rs b/parachains/runtimes/starters/shell/src/lib.rs index 4eefd1befaa..2d91a0c015b 100644 --- a/parachains/runtimes/starters/shell/src/lib.rs +++ b/parachains/runtimes/starters/shell/src/lib.rs @@ -92,7 +92,8 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10); /// by Operational extrinsics. const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); /// We allow for .5 seconds of compute with a 12 second average block time. -const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND.saturating_div(2) +const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND + .saturating_div(2) .set_proof_size(cumulus_primitives_core::relay_chain::v2::MAX_POV_SIZE as u64); parameter_types! { diff --git a/parachains/runtimes/testing/penpal/src/lib.rs b/parachains/runtimes/testing/penpal/src/lib.rs index 0a18f70f0f4..dddb71773f6 100644 --- a/parachains/runtimes/testing/penpal/src/lib.rs +++ b/parachains/runtimes/testing/penpal/src/lib.rs @@ -230,7 +230,8 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5); const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); /// We allow for 0.5 of a second of compute with a 12 second average block time. -const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND.saturating_div(2) +const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND + .saturating_div(2) .set_proof_size(cumulus_primitives_core::relay_chain::v2::MAX_POV_SIZE as u64); /// The version information used to identify this runtime when compiled natively. diff --git a/parachains/runtimes/testing/rococo-parachain/src/lib.rs b/parachains/runtimes/testing/rococo-parachain/src/lib.rs index ec6a08b0b42..21d784aae65 100644 --- a/parachains/runtimes/testing/rococo-parachain/src/lib.rs +++ b/parachains/runtimes/testing/rococo-parachain/src/lib.rs @@ -134,7 +134,8 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10); /// by Operational extrinsics. const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); /// We allow for .5 seconds of compute with a 12 second average block time. -const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND.saturating_div(2) +const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND + .saturating_div(2) .set_proof_size(cumulus_primitives_core::relay_chain::v2::MAX_POV_SIZE as u64); parameter_types! { diff --git a/test/runtime/src/lib.rs b/test/runtime/src/lib.rs index 4ac173efef7..786590b8b91 100644 --- a/test/runtime/src/lib.rs +++ b/test/runtime/src/lib.rs @@ -140,7 +140,8 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10); /// by Operational extrinsics. const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); /// We allow for .5 seconds of compute with a 12 second average block time. -const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND.saturating_div(2) +const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND + .saturating_div(2) .set_proof_size(cumulus_primitives_core::relay_chain::v2::MAX_POV_SIZE as u64); parameter_types! {