Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Add upper limit on the number of overweight messages in the queue (#6298
Browse files Browse the repository at this point in the history
)

* Add upper limit on the number of ovwerweight messages in the queue

* Add newline
  • Loading branch information
KiChjang authored Dec 2, 2022
1 parent c34826f commit 31d5410
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,7 @@ pub type Executive = frame_executive::Executive<
(
// "Use 2D weights in XCM v3" <https://github.com/paritytech/polkadot/pull/6134>
pallet_xcm::migration::v1::MigrateToV1<Runtime>,
parachains_ump::migration::v1::MigrateToV1<Runtime>,
),
>;
/// The payload being signed in the transactions.
Expand Down
10 changes: 8 additions & 2 deletions runtime/parachains/src/ump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T>(_);

#[pallet::config]
Expand Down Expand Up @@ -326,7 +330,7 @@ pub mod pallet {
/// These messages stay there until manually dispatched.
#[pallet::storage]
pub type Overweight<T: Config> =
StorageMap<_, Twox64Concat, OverweightIndex, (ParaId, Vec<u8>), OptionQuery>;
CountedStorageMap<_, Twox64Concat, OverweightIndex, (ParaId, Vec<u8>), OptionQuery>;

/// The number of overweight messages ever recorded in `Overweight` (and thus the lowest free
/// index).
Expand Down Expand Up @@ -540,7 +544,9 @@ impl<T: Config> Pallet<T> {
let _ = queue_cache.consume_front::<T>(dispatchee);
},
Err((id, required)) => {
if required.any_gt(config.ump_max_individual_weight) {
let is_under_limit = Overweight::<T>::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::<T>(dispatchee).expect(
Expand Down
48 changes: 48 additions & 0 deletions runtime/parachains/src/ump/migration.rs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
fn on_runtime_upgrade() -> Weight {
if StorageVersion::get::<Pallet<T>>() == 0 {
let mut weight = T::DbWeight::get().reads(1);

let overweight_messages = Overweight::<T>::initialize_counter() as u64;

weight.saturating_accrue(T::DbWeight::get().reads_writes(overweight_messages, 1));

StorageVersion::new(1).put::<Pallet<T>>();

weight.saturating_add(T::DbWeight::get().writes(1))
} else {
log::warn!("skipping v1, should be removed");
T::DbWeight::get().reads(1)
}
}
}
}
1 change: 1 addition & 0 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,7 @@ pub type Executive = frame_executive::Executive<
(
// "Use 2D weights in XCM v3" <https://github.com/paritytech/polkadot/pull/6134>
pallet_xcm::migration::v1::MigrateToV1<Runtime>,
parachains_ump::migration::v1::MigrateToV1<Runtime>,
),
>;

Expand Down
1 change: 1 addition & 0 deletions runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,7 @@ pub type Executive = frame_executive::Executive<
(
// "Use 2D weights in XCM v3" <https://github.com/paritytech/polkadot/pull/6134>
pallet_xcm::migration::v1::MigrateToV1<Runtime>,
parachains_ump::migration::v1::MigrateToV1<Runtime>,
),
>;
/// The payload being signed in transactions.
Expand Down
1 change: 1 addition & 0 deletions runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ pub type Executive = frame_executive::Executive<
(
// "Use 2D weights in XCM v3" <https://github.com/paritytech/polkadot/pull/6134>
pallet_xcm::migration::v1::MigrateToV1<Runtime>,
parachains_ump::migration::v1::MigrateToV1<Runtime>,
),
>;
/// The payload being signed in transactions.
Expand Down

0 comments on commit 31d5410

Please sign in to comment.