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

Add upper limit on the number of overweight messages in the queue #6298

Merged
merged 2 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<
pallet_fast_unstake::migrations::v1::MigrateToV1<Runtime>,
// "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));
Copy link
Member

@gavofyork gavofyork Nov 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stuff like this doesn't work any more with 2D weights. It needs a proper benchmark. Indeed the API should be marked deprecated and removed ASAP.

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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same - this will not work with 2D weights. Needs a benchmark and API should be marked deprecated ASAP.


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<
pallet_fast_unstake::migrations::v1::MigrateToV1<Runtime>,
// "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 @@ -1463,6 +1463,7 @@ pub type Executive = frame_executive::Executive<
parachains_configuration::migration::v3::MigrateToV3<Runtime>,
// "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 @@ -1233,6 +1233,7 @@ pub type Executive = frame_executive::Executive<
pallet_fast_unstake::migrations::v1::MigrateToV1<Runtime>,
// "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