From 1bdba1050c3bdacf92b8478dbc8974120882e824 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 16 Nov 2022 13:19:32 +0900 Subject: [PATCH 1/2] Add upper limit on the number of ovwerweight messages in the queue --- runtime/kusama/src/lib.rs | 1 + runtime/parachains/src/ump.rs | 10 ++++-- runtime/parachains/src/ump/migration.rs | 48 +++++++++++++++++++++++++ runtime/polkadot/src/lib.rs | 1 + runtime/rococo/src/lib.rs | 1 + runtime/westend/src/lib.rs | 1 + 6 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 runtime/parachains/src/ump/migration.rs diff --git a/runtime/kusama/src/lib.rs b/runtime/kusama/src/lib.rs index 7488242764c6..ddbd4b3944ec 100644 --- a/runtime/kusama/src/lib.rs +++ b/runtime/kusama/src/lib.rs @@ -1456,6 +1456,7 @@ pub type Executive = frame_executive::Executive< pallet_fast_unstake::migrations::v1::MigrateToV1, // "Use 2D weights in XCM v3" pallet_xcm::migration::v1::MigrateToV1, + parachains_ump::migration::v1::MigrateToV1, ), >; /// The payload being signed in the transactions. diff --git a/runtime/parachains/src/ump.rs b/runtime/parachains/src/ump.rs index 2bd4056bdd45..41c5c70d327b 100644 --- a/runtime/parachains/src/ump.rs +++ b/runtime/parachains/src/ump.rs @@ -31,9 +31,12 @@ pub use pallet::*; /// This is used for benchmarking sanely bounding relevant storate items. It is expected from the `configurations` /// pallet to check these values before setting. pub const MAX_UPWARD_MESSAGE_SIZE_BOUND: u32 = 50 * 1024; +/// Maximum amount of overweight messages that can exist in the queue at any given time. +pub const MAX_OVERWEIGHT_MESSAGES: u32 = 1000; #[cfg(feature = "runtime-benchmarks")] mod benchmarking; +pub mod migration; #[cfg(test)] pub(crate) mod tests; @@ -213,6 +216,7 @@ pub mod pallet { #[pallet::pallet] #[pallet::generate_store(pub(super) trait Store)] #[pallet::without_storage_info] + #[pallet::storage_version(migration::STORAGE_VERSION)] pub struct Pallet(_); #[pallet::config] @@ -326,7 +330,7 @@ pub mod pallet { /// These messages stay there until manually dispatched. #[pallet::storage] pub type Overweight = - StorageMap<_, Twox64Concat, OverweightIndex, (ParaId, Vec), OptionQuery>; + CountedStorageMap<_, Twox64Concat, OverweightIndex, (ParaId, Vec), OptionQuery>; /// The number of overweight messages ever recorded in `Overweight` (and thus the lowest free /// index). @@ -540,7 +544,9 @@ impl Pallet { let _ = queue_cache.consume_front::(dispatchee); }, Err((id, required)) => { - if required.any_gt(config.ump_max_individual_weight) { + let is_under_limit = Overweight::::count() < MAX_OVERWEIGHT_MESSAGES; + weight_used.saturating_accrue(T::DbWeight::get().reads(1)); + if required.any_gt(config.ump_max_individual_weight) && is_under_limit { // overweight - add to overweight queue and continue with message // execution consuming the message. let upward_message = queue_cache.consume_front::(dispatchee).expect( diff --git a/runtime/parachains/src/ump/migration.rs b/runtime/parachains/src/ump/migration.rs new file mode 100644 index 000000000000..9fc0550be658 --- /dev/null +++ b/runtime/parachains/src/ump/migration.rs @@ -0,0 +1,48 @@ +// 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 . + +use crate::ump::{Config, Overweight, Pallet}; +use frame_support::{ + pallet_prelude::*, + traits::{OnRuntimeUpgrade, StorageVersion}, + weights::Weight, +}; + +pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + +pub mod v1 { + use super::*; + + pub struct MigrateToV1(sp_std::marker::PhantomData); + impl OnRuntimeUpgrade for MigrateToV1 { + fn on_runtime_upgrade() -> Weight { + if StorageVersion::get::>() == 0 { + let mut weight = T::DbWeight::get().reads(1); + + let overweight_messages = Overweight::::initialize_counter() as u64; + + weight.saturating_accrue(T::DbWeight::get().reads_writes(overweight_messages, 1)); + + StorageVersion::new(1).put::>(); + + weight.saturating_add(T::DbWeight::get().writes(1)) + } else { + log::warn!("skipping v1, should be removed"); + T::DbWeight::get().reads(1) + } + } + } +} \ No newline at end of file diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index 5ff0bb075a49..e86e32f8b13a 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -1612,6 +1612,7 @@ pub type Executive = frame_executive::Executive< pallet_fast_unstake::migrations::v1::MigrateToV1, // "Use 2D weights in XCM v3" pallet_xcm::migration::v1::MigrateToV1, + parachains_ump::migration::v1::MigrateToV1, ), >; diff --git a/runtime/rococo/src/lib.rs b/runtime/rococo/src/lib.rs index 9fd328c09304..772a75ec8954 100644 --- a/runtime/rococo/src/lib.rs +++ b/runtime/rococo/src/lib.rs @@ -1463,6 +1463,7 @@ pub type Executive = frame_executive::Executive< parachains_configuration::migration::v3::MigrateToV3, // "Use 2D weights in XCM v3" pallet_xcm::migration::v1::MigrateToV1, + parachains_ump::migration::v1::MigrateToV1, ), >; /// The payload being signed in transactions. diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index 5721020ce715..e8742bf480cf 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1233,6 +1233,7 @@ pub type Executive = frame_executive::Executive< pallet_fast_unstake::migrations::v1::MigrateToV1, // "Use 2D weights in XCM v3" pallet_xcm::migration::v1::MigrateToV1, + parachains_ump::migration::v1::MigrateToV1, ), >; /// The payload being signed in transactions. From 83b895a5db363d5e4fa078a1c8e277fb0707be86 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 16 Nov 2022 15:58:01 +0900 Subject: [PATCH 2/2] Add newline --- runtime/parachains/src/ump/migration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/parachains/src/ump/migration.rs b/runtime/parachains/src/ump/migration.rs index 9fc0550be658..d791aa863f61 100644 --- a/runtime/parachains/src/ump/migration.rs +++ b/runtime/parachains/src/ump/migration.rs @@ -45,4 +45,4 @@ pub mod v1 { } } } -} \ No newline at end of file +}