Take into account size as well in weight limiting. (#7369)

* Take into account size as well in weight limiting.

* Fix logging.

* More logs.

* Remove randomized selection in provisioner

No longer supported by runtime.

* Fix and simplify weight calculation.

Random filtering of remote disputes got dropped.

* Make existing tests pass.

* Tests for size limiting.

* Fix provisioner.

* Remove rand dependency.

* Better default block length for tests.

* ".git/.scripts/commands/bench/bench.sh" runtime kusama runtime_parachains::paras_inherent

* ".git/.scripts/commands/bench/bench.sh" runtime polkadot runtime_parachains::paras_inherent

* ".git/.scripts/commands/bench/bench.sh" runtime westend runtime_parachains::paras_inherent

* Update runtime/parachains/src/paras_inherent/mod.rs

Co-authored-by: Tsvetomir Dimitrov <tsvetomir@parity.io>

* Update runtime/parachains/src/paras_inherent/mod.rs

Co-authored-by: Chris Sosnin <48099298+slumber@users.noreply.github.com>

* Add back missing line.

* Fix test.

* fmt fix.

* Add missing test annotation

---------

Co-authored-by: eskimor <eskimor@no-such-url.com>
Co-authored-by: command-bot <>
Co-authored-by: Tsvetomir Dimitrov <tsvetomir@parity.io>
Co-authored-by: Chris Sosnin <48099298+slumber@users.noreply.github.com>
This commit is contained in:
eskimor
2023-08-01 14:43:54 +02:00
committed by GitHub
parent 4980b64497
commit 367098bae5
13 changed files with 439 additions and 403 deletions
-1
View File
@@ -6983,7 +6983,6 @@ dependencies = [
"polkadot-node-subsystem-util", "polkadot-node-subsystem-util",
"polkadot-primitives", "polkadot-primitives",
"polkadot-primitives-test-helpers", "polkadot-primitives-test-helpers",
"rand 0.8.5",
"sp-application-crypto", "sp-application-crypto",
"sp-keystore", "sp-keystore",
"thiserror", "thiserror",
@@ -13,7 +13,6 @@ polkadot-primitives = { path = "../../../primitives" }
polkadot-node-primitives = { path = "../../primitives" } polkadot-node-primitives = { path = "../../primitives" }
polkadot-node-subsystem = { path = "../../subsystem" } polkadot-node-subsystem = { path = "../../subsystem" }
polkadot-node-subsystem-util = { path = "../../subsystem-util" } polkadot-node-subsystem-util = { path = "../../subsystem-util" }
rand = "0.8.5"
futures-timer = "3.0.2" futures-timer = "3.0.2"
fatality = "0.0.6" fatality = "0.0.6"
@@ -14,12 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>. // along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! The disputes module is responsible for selecting dispute votes to be sent with the inherent data. It contains two //! The disputes module is responsible for selecting dispute votes to be sent with the inherent data.
//! different implementations, extracted in two separate modules - `random_selection` and `prioritized_selection`. Which
//! implementation will be executed depends on the version of the runtime. Runtime v2 supports `random_selection`. Runtime
//! `v3` and above - `prioritized_selection`. The entrypoint to these implementations is the `select_disputes` function.
//! `prioritized_selection` is considered superior and will be the default one in the future. Refer to the documentation of
//! the modules for more details about each implementation.
use crate::LOG_TARGET; use crate::LOG_TARGET;
use futures::channel::oneshot; use futures::channel::oneshot;
@@ -49,5 +44,3 @@ async fn request_votes(
} }
pub(crate) mod prioritized_selection; pub(crate) mod prioritized_selection;
pub(crate) mod random_selection;
@@ -1,178 +0,0 @@
// Copyright (C) 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/>.
//! This module selects all RECENT disputes, fetches the votes for them from dispute-coordinator and
//! returns them as `MultiDisputeStatementSet`. If the RECENT disputes are more than
//! `MAX_DISPUTES_FORWARDED_TO_RUNTIME` constant - the ACTIVE disputes plus a random selection of
//! RECENT disputes (up to `MAX_DISPUTES_FORWARDED_TO_RUNTIME`) are returned instead.
//! If the ACTIVE disputes are also above `MAX_DISPUTES_FORWARDED_TO_RUNTIME` limit - a random selection
//! of them is generated.
use crate::{metrics, LOG_TARGET};
use futures::channel::oneshot;
use polkadot_node_subsystem::{messages::DisputeCoordinatorMessage, overseer};
use polkadot_primitives::{
CandidateHash, DisputeStatement, DisputeStatementSet, MultiDisputeStatementSet, SessionIndex,
};
use std::collections::HashSet;
/// The maximum number of disputes Provisioner will include in the inherent data.
/// Serves as a protection not to flood the Runtime with excessive data.
const MAX_DISPUTES_FORWARDED_TO_RUNTIME: usize = 1_000;
#[derive(Debug)]
enum RequestType {
/// Query recent disputes, could be an excessive amount.
Recent,
/// Query the currently active and very recently concluded disputes.
Active,
}
/// Request open disputes identified by `CandidateHash` and the `SessionIndex`.
/// Returns only confirmed/concluded disputes. The rest are filtered out.
async fn request_confirmed_disputes(
sender: &mut impl overseer::ProvisionerSenderTrait,
active_or_recent: RequestType,
) -> Vec<(SessionIndex, CandidateHash)> {
let (tx, rx) = oneshot::channel();
let msg = match active_or_recent {
RequestType::Recent => DisputeCoordinatorMessage::RecentDisputes(tx),
RequestType::Active => DisputeCoordinatorMessage::ActiveDisputes(tx),
};
sender.send_unbounded_message(msg);
let disputes = match rx.await {
Ok(r) => r,
Err(oneshot::Canceled) => {
gum::warn!(
target: LOG_TARGET,
"Channel closed: unable to gather {:?} disputes",
active_or_recent
);
Vec::new()
},
};
disputes
.into_iter()
.filter(|d| d.2.is_confirmed_concluded())
.map(|d| (d.0, d.1))
.collect()
}
/// Extend `acc` by `n` random, picks of not-yet-present in `acc` items of `recent` without repetition and additions of recent.
fn extend_by_random_subset_without_repetition(
acc: &mut Vec<(SessionIndex, CandidateHash)>,
extension: Vec<(SessionIndex, CandidateHash)>,
n: usize,
) {
use rand::Rng;
let lut = acc.iter().cloned().collect::<HashSet<(SessionIndex, CandidateHash)>>();
let mut unique_new =
extension.into_iter().filter(|recent| !lut.contains(recent)).collect::<Vec<_>>();
// we can simply add all
if unique_new.len() <= n {
acc.extend(unique_new)
} else {
acc.reserve(n);
let mut rng = rand::thread_rng();
for _ in 0..n {
let idx = rng.gen_range(0..unique_new.len());
acc.push(unique_new.swap_remove(idx));
}
}
// assure sorting stays candid according to session index
acc.sort_unstable_by(|a, b| a.0.cmp(&b.0));
}
pub async fn select_disputes<Sender>(
sender: &mut Sender,
metrics: &metrics::Metrics,
) -> MultiDisputeStatementSet
where
Sender: overseer::ProvisionerSenderTrait,
{
gum::trace!(target: LOG_TARGET, "Selecting disputes for inherent data using random selection");
// We use `RecentDisputes` instead of `ActiveDisputes` because redundancy is fine.
// It's heavier than `ActiveDisputes` but ensures that everything from the dispute
// window gets on-chain, unlike `ActiveDisputes`.
// In case of an overload condition, we limit ourselves to active disputes, and fill up to the
// upper bound of disputes to pass to wasm `fn create_inherent_data`.
// If the active ones are already exceeding the bounds, randomly select a subset.
let recent = request_confirmed_disputes(sender, RequestType::Recent).await;
let disputes = if recent.len() > MAX_DISPUTES_FORWARDED_TO_RUNTIME {
gum::warn!(
target: LOG_TARGET,
"Recent disputes are excessive ({} > {}), reduce to active ones, and selected",
recent.len(),
MAX_DISPUTES_FORWARDED_TO_RUNTIME
);
let mut active = request_confirmed_disputes(sender, RequestType::Active).await;
let n_active = active.len();
let active = if active.len() > MAX_DISPUTES_FORWARDED_TO_RUNTIME {
let mut picked = Vec::with_capacity(MAX_DISPUTES_FORWARDED_TO_RUNTIME);
extend_by_random_subset_without_repetition(
&mut picked,
active,
MAX_DISPUTES_FORWARDED_TO_RUNTIME,
);
picked
} else {
extend_by_random_subset_without_repetition(
&mut active,
recent,
MAX_DISPUTES_FORWARDED_TO_RUNTIME.saturating_sub(n_active),
);
active
};
active
} else {
recent
};
// Load all votes for all disputes from the coordinator.
let dispute_candidate_votes = super::request_votes(sender, disputes).await;
// Transform all `CandidateVotes` into `MultiDisputeStatementSet`.
dispute_candidate_votes
.into_iter()
.map(|(session_index, candidate_hash, votes)| {
let valid_statements = votes
.valid
.into_iter()
.map(|(i, (s, sig))| (DisputeStatement::Valid(s), i, sig));
let invalid_statements = votes
.invalid
.into_iter()
.map(|(i, (s, sig))| (DisputeStatement::Invalid(s), i, sig));
metrics.inc_valid_statements_by(valid_statements.len());
metrics.inc_invalid_statements_by(invalid_statements.len());
metrics.inc_dispute_statement_sets_by(1);
DisputeStatementSet {
candidate_hash,
session: session_index,
statements: valid_statements.chain(invalid_statements).collect(),
}
})
.collect()
}
+11 -10
View File
@@ -393,16 +393,17 @@ async fn send_inherent_data(
"Selecting disputes" "Selecting disputes"
); );
let disputes = match has_required_runtime( debug_assert!(
from_job, has_required_runtime(
leaf.hash, from_job,
PRIORITIZED_SELECTION_RUNTIME_VERSION_REQUIREMENT, leaf.hash,
) PRIORITIZED_SELECTION_RUNTIME_VERSION_REQUIREMENT,
.await )
{ .await,
true => disputes::prioritized_selection::select_disputes(from_job, metrics, leaf).await, "randomized selection no longer supported, please upgrade your runtime!"
false => disputes::random_selection::select_disputes(from_job, metrics).await, );
};
let disputes = disputes::prioritized_selection::select_disputes(from_job, metrics, leaf).await;
gum::trace!( gum::trace!(
target: LOG_TARGET, target: LOG_TARGET,
+1 -1
View File
@@ -1372,7 +1372,7 @@ impl AsRef<DisputeStatementSet> for DisputeStatementSet {
pub type MultiDisputeStatementSet = Vec<DisputeStatementSet>; pub type MultiDisputeStatementSet = Vec<DisputeStatementSet>;
/// A _checked_ set of dispute statements. /// A _checked_ set of dispute statements.
#[derive(Clone, PartialEq, RuntimeDebug)] #[derive(Clone, PartialEq, RuntimeDebug, Encode)]
pub struct CheckedDisputeStatementSet(DisputeStatementSet); pub struct CheckedDisputeStatementSet(DisputeStatementSet);
impl AsRef<DisputeStatementSet> for CheckedDisputeStatementSet { impl AsRef<DisputeStatementSet> for CheckedDisputeStatementSet {
@@ -17,27 +17,26 @@
//! Autogenerated weights for `runtime_parachains::paras_inherent` //! Autogenerated weights for `runtime_parachains::paras_inherent`
//! //!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-06-19, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! DATE: 2023-06-28, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000` //! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `runner-e8ezs4ez-project-163-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz` //! HOSTNAME: `runner-xerhrdyb-project-163-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("kusama-dev"), DB CACHE: 1024 //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("kusama-dev"), DB CACHE: 1024
// Executed Command: // Executed Command:
// ./target/production/polkadot // target/production/polkadot
// benchmark // benchmark
// pallet // pallet
// --chain=kusama-dev
// --steps=50 // --steps=50
// --repeat=20 // --repeat=20
// --no-storage-info
// --no-median-slopes
// --no-min-squares
// --pallet=runtime_parachains::paras_inherent
// --extrinsic=* // --extrinsic=*
// --execution=wasm // --execution=wasm
// --wasm-execution=compiled // --wasm-execution=compiled
// --heap-pages=4096
// --json-file=/builds/parity/mirrors/polkadot/.git/.artifacts/bench.json
// --pallet=runtime_parachains::paras_inherent
// --chain=kusama-dev
// --header=./file_header.txt // --header=./file_header.txt
// --output=./runtime/kusama/src/weights/runtime_parachains_paras_inherent.rs // --output=./runtime/kusama/src/weights/
#![cfg_attr(rustfmt, rustfmt_skip)] #![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)] #![allow(unused_parens)]
@@ -115,11 +114,11 @@ impl<T: frame_system::Config> runtime_parachains::paras_inherent::WeightInfo for
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `50671` // Measured: `50671`
// Estimated: `56611 + v * (23 ±0)` // Estimated: `56611 + v * (23 ±0)`
// Minimum execution time: 982_516_000 picoseconds. // Minimum execution time: 1_008_586_000 picoseconds.
Weight::from_parts(453_473_787, 0) Weight::from_parts(471_892_709, 0)
.saturating_add(Weight::from_parts(0, 56611)) .saturating_add(Weight::from_parts(0, 56611))
// Standard Error: 21_034 // Standard Error: 15_634
.saturating_add(Weight::from_parts(57_212_691, 0).saturating_mul(v.into())) .saturating_add(Weight::from_parts(56_433_120, 0).saturating_mul(v.into()))
.saturating_add(T::DbWeight::get().reads(27)) .saturating_add(T::DbWeight::get().reads(27))
.saturating_add(T::DbWeight::get().writes(15)) .saturating_add(T::DbWeight::get().writes(15))
.saturating_add(Weight::from_parts(0, 23).saturating_mul(v.into())) .saturating_add(Weight::from_parts(0, 23).saturating_mul(v.into()))
@@ -186,8 +185,8 @@ impl<T: frame_system::Config> runtime_parachains::paras_inherent::WeightInfo for
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `42504` // Measured: `42504`
// Estimated: `48444` // Estimated: `48444`
// Minimum execution time: 465_674_000 picoseconds. // Minimum execution time: 469_409_000 picoseconds.
Weight::from_parts(483_678_000, 0) Weight::from_parts(487_865_000, 0)
.saturating_add(Weight::from_parts(0, 48444)) .saturating_add(Weight::from_parts(0, 48444))
.saturating_add(T::DbWeight::get().reads(25)) .saturating_add(T::DbWeight::get().reads(25))
.saturating_add(T::DbWeight::get().writes(16)) .saturating_add(T::DbWeight::get().writes(16))
@@ -259,11 +258,11 @@ impl<T: frame_system::Config> runtime_parachains::paras_inherent::WeightInfo for
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `42540` // Measured: `42540`
// Estimated: `48480` // Estimated: `48480`
// Minimum execution time: 6_886_272_000 picoseconds. // Minimum execution time: 6_874_816_000 picoseconds.
Weight::from_parts(1_235_371_688, 0) Weight::from_parts(1_229_912_739, 0)
.saturating_add(Weight::from_parts(0, 48480)) .saturating_add(Weight::from_parts(0, 48480))
// Standard Error: 28_012 // Standard Error: 27_352
.saturating_add(Weight::from_parts(56_395_511, 0).saturating_mul(v.into())) .saturating_add(Weight::from_parts(56_137_302, 0).saturating_mul(v.into()))
.saturating_add(T::DbWeight::get().reads(28)) .saturating_add(T::DbWeight::get().reads(28))
.saturating_add(T::DbWeight::get().writes(15)) .saturating_add(T::DbWeight::get().writes(15))
} }
@@ -337,8 +336,8 @@ impl<T: frame_system::Config> runtime_parachains::paras_inherent::WeightInfo for
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `42567` // Measured: `42567`
// Estimated: `48507` // Estimated: `48507`
// Minimum execution time: 42_215_280_000 picoseconds. // Minimum execution time: 41_075_073_000 picoseconds.
Weight::from_parts(43_255_598_000, 0) Weight::from_parts(43_753_587_000, 0)
.saturating_add(Weight::from_parts(0, 48507)) .saturating_add(Weight::from_parts(0, 48507))
.saturating_add(T::DbWeight::get().reads(30)) .saturating_add(T::DbWeight::get().reads(30))
.saturating_add(T::DbWeight::get().writes(15)) .saturating_add(T::DbWeight::get().writes(15))
+5 -3
View File
@@ -32,6 +32,7 @@ use frame_support::{
weights::{Weight, WeightMeter}, weights::{Weight, WeightMeter},
}; };
use frame_support_test::TestRandomness; use frame_support_test::TestRandomness;
use frame_system::limits;
use parity_scale_codec::Decode; use parity_scale_codec::Decode;
use primitives::{ use primitives::{
AuthorityDiscoveryId, Balance, BlockNumber, CandidateHash, Moment, SessionIndex, UpwardMessage, AuthorityDiscoveryId, Balance, BlockNumber, CandidateHash, Moment, SessionIndex, UpwardMessage,
@@ -42,7 +43,7 @@ use sp_io::TestExternalities;
use sp_runtime::{ use sp_runtime::{
traits::{AccountIdConversion, BlakeTwo256, IdentityLookup}, traits::{AccountIdConversion, BlakeTwo256, IdentityLookup},
transaction_validity::TransactionPriority, transaction_validity::TransactionPriority,
BuildStorage, Permill, BuildStorage, Perbill, Permill,
}; };
use std::{cell::RefCell, collections::HashMap}; use std::{cell::RefCell, collections::HashMap};
@@ -81,10 +82,11 @@ where
parameter_types! { parameter_types! {
pub const BlockHashCount: u32 = 250; pub const BlockHashCount: u32 = 250;
pub BlockWeights: frame_system::limits::BlockWeights = pub static BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max( frame_system::limits::BlockWeights::simple_max(
Weight::from_parts(4 * 1024 * 1024, u64::MAX), Weight::from_parts(4 * 1024 * 1024, u64::MAX),
); );
pub static BlockLength: limits::BlockLength = limits::BlockLength::max_with_normal_ratio(u32::MAX, Perbill::from_percent(75));
} }
pub type AccountId = u64; pub type AccountId = u64;
@@ -92,7 +94,7 @@ pub type AccountId = u64;
impl frame_system::Config for Test { impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything; type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = BlockWeights; type BlockWeights = BlockWeights;
type BlockLength = (); type BlockLength = BlockLength;
type DbWeight = (); type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin; type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall; type RuntimeCall = RuntimeCall;
@@ -53,7 +53,6 @@ use rand::{seq::SliceRandom, SeedableRng};
use scale_info::TypeInfo; use scale_info::TypeInfo;
use sp_runtime::traits::{Header as HeaderT, One}; use sp_runtime::traits::{Header as HeaderT, One};
use sp_std::{ use sp_std::{
cmp::Ordering,
collections::{btree_map::BTreeMap, btree_set::BTreeSet}, collections::{btree_map::BTreeMap, btree_set::BTreeSet},
prelude::*, prelude::*,
vec::Vec, vec::Vec,
@@ -62,12 +61,13 @@ use sp_std::{
mod misc; mod misc;
mod weights; mod weights;
use self::weights::checked_multi_dispute_statement_sets_weight;
pub use self::{ pub use self::{
misc::{IndexedRetain, IsSortedBy}, misc::{IndexedRetain, IsSortedBy},
weights::{ weights::{
backed_candidate_weight, backed_candidates_weight, dispute_statement_set_weight, backed_candidate_weight, backed_candidates_weight, dispute_statement_set_weight,
multi_dispute_statement_sets_weight, paras_inherent_total_weight, signed_bitfields_weight, multi_dispute_statement_sets_weight, paras_inherent_total_weight, signed_bitfield_weight,
TestWeightInfo, WeightInfo, signed_bitfields_weight, TestWeightInfo, WeightInfo,
}, },
}; };
@@ -264,8 +264,8 @@ pub mod pallet {
#[pallet::weight(( #[pallet::weight((
paras_inherent_total_weight::<T>( paras_inherent_total_weight::<T>(
data.backed_candidates.as_slice(), data.backed_candidates.as_slice(),
data.bitfields.as_slice(), &data.bitfields,
data.disputes.as_slice(), &data.disputes,
), ),
DispatchClass::Mandatory, DispatchClass::Mandatory,
))] ))]
@@ -356,17 +356,47 @@ impl<T: Config> Pallet<T> {
let now = <frame_system::Pallet<T>>::block_number(); let now = <frame_system::Pallet<T>>::block_number();
let candidates_weight = backed_candidates_weight::<T>(&backed_candidates); let candidates_weight = backed_candidates_weight::<T>(&backed_candidates);
let bitfields_weight = signed_bitfields_weight::<T>(bitfields.len()); let bitfields_weight = signed_bitfields_weight::<T>(&bitfields);
let disputes_weight = multi_dispute_statement_sets_weight::<T, _, _>(&disputes); let disputes_weight = multi_dispute_statement_sets_weight::<T>(&disputes);
let max_block_weight = <T as frame_system::Config>::BlockWeights::get().max_block;
METRICS let all_weight_before = candidates_weight + bitfields_weight + disputes_weight;
.on_before_filter((candidates_weight + bitfields_weight + disputes_weight).ref_time());
METRICS.on_before_filter(all_weight_before.ref_time());
log::debug!(target: LOG_TARGET, "Size before filter: {}, candidates + bitfields: {}, disputes: {}", all_weight_before.proof_size(), candidates_weight.proof_size() + bitfields_weight.proof_size(), disputes_weight.proof_size());
log::debug!(target: LOG_TARGET, "Time weight before filter: {}, candidates + bitfields: {}, disputes: {}", all_weight_before.ref_time(), candidates_weight.ref_time() + bitfields_weight.ref_time(), disputes_weight.ref_time());
let current_session = <shared::Pallet<T>>::session_index(); let current_session = <shared::Pallet<T>>::session_index();
let expected_bits = <scheduler::Pallet<T>>::availability_cores().len(); let expected_bits = <scheduler::Pallet<T>>::availability_cores().len();
let validator_public = shared::Pallet::<T>::active_validator_keys(); let validator_public = shared::Pallet::<T>::active_validator_keys();
// We are assuming (incorrectly) to have all the weight (for the mandatory class or even
// full block) available to us. This can lead to slightly overweight blocks, which still
// works as the dispatch class for `enter` is `Mandatory`. By using the `Mandatory`
// dispatch class, the upper layers impose no limit on the weight of this inherent, instead
// we limit ourselves and make sure to stay within reasonable bounds. It might make sense
// to subtract BlockWeights::base_block to reduce chances of becoming overweight.
let max_block_weight = {
let dispatch_class = DispatchClass::Mandatory;
let max_block_weight_full = <T as frame_system::Config>::BlockWeights::get();
log::debug!(target: LOG_TARGET, "Max block weight: {}", max_block_weight_full.max_block);
// Get max block weight for the mandatory class if defined, otherwise total max weight of
// the block.
let max_weight = max_block_weight_full
.per_class
.get(dispatch_class)
.max_total
.unwrap_or(max_block_weight_full.max_block);
log::debug!(target: LOG_TARGET, "Used max block time weight: {}", max_weight);
let max_block_size_full = <T as frame_system::Config>::BlockLength::get();
let max_block_size = max_block_size_full.max.get(dispatch_class);
log::debug!(target: LOG_TARGET, "Used max block size: {}", max_block_size);
// Adjust proof size to max block size as we are tracking tx size.
max_weight.set_proof_size(*max_block_size as u64)
};
log::debug!(target: LOG_TARGET, "Used max block weight: {}", max_block_weight);
let entropy = compute_entropy::<T>(parent_hash); let entropy = compute_entropy::<T>(parent_hash);
let mut rng = rand_chacha::ChaChaRng::from_seed(entropy.into()); let mut rng = rand_chacha::ChaChaRng::from_seed(entropy.into());
@@ -388,10 +418,9 @@ impl<T: Config> Pallet<T> {
disputes, disputes,
dispute_statement_set_valid, dispute_statement_set_valid,
max_block_weight, max_block_weight,
&mut rng,
); );
let full_weight = if context == ProcessInherentDataContext::ProvideInherent { let all_weight_after = if context == ProcessInherentDataContext::ProvideInherent {
// Assure the maximum block weight is adhered, by limiting bitfields and backed // Assure the maximum block weight is adhered, by limiting bitfields and backed
// candidates. Dispute statement sets were already limited before. // candidates. Dispute statement sets were already limited before.
let non_disputes_weight = apply_weight_limit::<T>( let non_disputes_weight = apply_weight_limit::<T>(
@@ -401,36 +430,38 @@ impl<T: Config> Pallet<T> {
&mut rng, &mut rng,
); );
let full_weight = let all_weight_after =
non_disputes_weight.saturating_add(checked_disputes_sets_consumed_weight); non_disputes_weight.saturating_add(checked_disputes_sets_consumed_weight);
METRICS.on_after_filter(full_weight.ref_time()); METRICS.on_after_filter(all_weight_after.ref_time());
log::debug!(
target: LOG_TARGET,
"[process_inherent_data] after filter: bitfields.len(): {}, backed_candidates.len(): {}, checked_disputes_sets.len() {}",
bitfields.len(),
backed_candidates.len(),
checked_disputes_sets.len()
);
log::debug!(target: LOG_TARGET, "Size after filter: {}, candidates + bitfields: {}, disputes: {}", all_weight_after.proof_size(), non_disputes_weight.proof_size(), checked_disputes_sets_consumed_weight.proof_size());
log::debug!(target: LOG_TARGET, "Time weight after filter: {}, candidates + bitfields: {}, disputes: {}", all_weight_after.ref_time(), non_disputes_weight.ref_time(), checked_disputes_sets_consumed_weight.ref_time());
if full_weight.any_gt(max_block_weight) { if all_weight_after.any_gt(max_block_weight) {
log::warn!(target: LOG_TARGET, "Post weight limiting weight is still too large."); log::warn!(target: LOG_TARGET, "Post weight limiting weight is still too large, time: {}, size: {}", all_weight_after.ref_time(), all_weight_after.proof_size());
} }
all_weight_after
full_weight
} else { } else {
// We compute the weight for the unfiltered disputes for a stronger check, since `create_inherent`
// should already have filtered them out in block authorship.
let full_weight = candidates_weight
.saturating_add(bitfields_weight)
.saturating_add(disputes_weight);
// This check is performed in the context of block execution. Ensures inherent weight invariants guaranteed // This check is performed in the context of block execution. Ensures inherent weight invariants guaranteed
// by `create_inherent_data` for block authorship. // by `create_inherent_data` for block authorship.
if full_weight.any_gt(max_block_weight) { if all_weight_before.any_gt(max_block_weight) {
log::error!( log::error!(
"Overweight para inherent data reached the runtime {:?}: {} > {}", "Overweight para inherent data reached the runtime {:?}: {} > {}",
parent_hash, parent_hash,
full_weight, all_weight_before,
max_block_weight max_block_weight
); );
} }
ensure!(full_weight.all_lte(max_block_weight), Error::<T>::InherentOverweight); ensure!(all_weight_before.all_lte(max_block_weight), Error::<T>::InherentOverweight);
full_weight all_weight_before
}; };
// Note that `process_checked_multi_dispute_data` will iterate and import each // Note that `process_checked_multi_dispute_data` will iterate and import each
@@ -597,7 +628,7 @@ impl<T: Config> Pallet<T> {
let processed = let processed =
ParachainsInherentData { bitfields, backed_candidates, disputes, parent_header }; ParachainsInherentData { bitfields, backed_candidates, disputes, parent_header };
Ok((processed, Some(full_weight).into())) Ok((processed, Some(all_weight_after).into()))
} }
} }
@@ -702,7 +733,7 @@ fn apply_weight_limit<T: Config + inclusion::Config>(
) -> Weight { ) -> Weight {
let total_candidates_weight = backed_candidates_weight::<T>(candidates.as_slice()); let total_candidates_weight = backed_candidates_weight::<T>(candidates.as_slice());
let total_bitfields_weight = signed_bitfields_weight::<T>(bitfields.len()); let total_bitfields_weight = signed_bitfields_weight::<T>(&bitfields);
let total = total_bitfields_weight.saturating_add(total_candidates_weight); let total = total_bitfields_weight.saturating_add(total_candidates_weight);
@@ -734,6 +765,7 @@ fn apply_weight_limit<T: Config + inclusion::Config>(
|c| backed_candidate_weight::<T>(c), |c| backed_candidate_weight::<T>(c),
max_consumable_by_candidates, max_consumable_by_candidates,
); );
log::debug!(target: LOG_TARGET, "Indices Candidates: {:?}, size: {}", indices, candidates.len());
candidates.indexed_retain(|idx, _backed_candidate| indices.binary_search(&idx).is_ok()); candidates.indexed_retain(|idx, _backed_candidate| indices.binary_search(&idx).is_ok());
// pick all bitfields, and // pick all bitfields, and
// fill the remaining space with candidates // fill the remaining space with candidates
@@ -750,9 +782,10 @@ fn apply_weight_limit<T: Config + inclusion::Config>(
rng, rng,
&bitfields, &bitfields,
vec![], vec![],
|_| <<T as Config>::WeightInfo as WeightInfo>::enter_bitfields(), |bitfield| signed_bitfield_weight::<T>(&bitfield),
max_consumable_weight, max_consumable_weight,
); );
log::debug!(target: LOG_TARGET, "Indices Bitfields: {:?}, size: {}", indices, bitfields.len());
bitfields.indexed_retain(|idx, _bitfield| indices.binary_search(&idx).is_ok()); bitfields.indexed_retain(|idx, _bitfield| indices.binary_search(&idx).is_ok());
@@ -941,94 +974,41 @@ fn compute_entropy<T: Config>(parent_hash: T::Hash) -> [u8; 32] {
/// 2. If exceeded: /// 2. If exceeded:
/// 1. Check validity of all dispute statements sequentially /// 1. Check validity of all dispute statements sequentially
/// 2. If not exceeded: /// 2. If not exceeded:
/// 1. Sort the disputes based on locality and age, locality first.
/// 1. Split the array
/// 1. Prefer local ones over remote disputes
/// 1. If weight is exceeded by locals, pick the older ones (lower indices) /// 1. If weight is exceeded by locals, pick the older ones (lower indices)
/// until the weight limit is reached. /// until the weight limit is reached.
/// 1. If weight is exceeded by locals and remotes, pick remotes
/// randomly and check validity one by one.
/// ///
/// Returns the consumed weight amount, that is guaranteed to be less than the provided `max_consumable_weight`. /// Returns the consumed weight amount, that is guaranteed to be less than the provided `max_consumable_weight`.
fn limit_and_sanitize_disputes< fn limit_and_sanitize_disputes<
T: Config, T: Config,
CheckValidityFn: FnMut(DisputeStatementSet) -> Option<CheckedDisputeStatementSet>, CheckValidityFn: FnMut(DisputeStatementSet) -> Option<CheckedDisputeStatementSet>,
>( >(
mut disputes: MultiDisputeStatementSet, disputes: MultiDisputeStatementSet,
mut dispute_statement_set_valid: CheckValidityFn, mut dispute_statement_set_valid: CheckValidityFn,
max_consumable_weight: Weight, max_consumable_weight: Weight,
rng: &mut rand_chacha::ChaChaRng,
) -> (Vec<CheckedDisputeStatementSet>, Weight) { ) -> (Vec<CheckedDisputeStatementSet>, Weight) {
// The total weight if all disputes would be included // The total weight if all disputes would be included
let disputes_weight = multi_dispute_statement_sets_weight::<T, _, _>(&disputes); let disputes_weight = multi_dispute_statement_sets_weight::<T>(&disputes);
if disputes_weight.any_gt(max_consumable_weight) { if disputes_weight.any_gt(max_consumable_weight) {
log::debug!(target: LOG_TARGET, "Above max consumable weight: {}/{}", disputes_weight, max_consumable_weight);
let mut checked_acc = Vec::<CheckedDisputeStatementSet>::with_capacity(disputes.len()); let mut checked_acc = Vec::<CheckedDisputeStatementSet>::with_capacity(disputes.len());
// Since the disputes array is sorted, we may use binary search to find the beginning of
// remote disputes
let idx = disputes
.binary_search_by(|probe| {
if T::DisputesHandler::included_state(probe.session, probe.candidate_hash).is_some()
{
Ordering::Less
} else {
Ordering::Greater
}
})
// The above predicate will never find an item and therefore we are guaranteed to obtain
// an error, which we can safely unwrap. QED.
.unwrap_err();
// Due to the binary search predicate above, the index computed will constitute the beginning
// of the remote disputes sub-array `[Local, Local, Local, ^Remote, Remote]`.
let remote_disputes = disputes.split_off(idx);
// Accumualated weight of all disputes picked, that passed the checks. // Accumualated weight of all disputes picked, that passed the checks.
let mut weight_acc = Weight::zero(); let mut weight_acc = Weight::zero();
// Select disputes in-order until the remaining weight is attained // Select disputes in-order until the remaining weight is attained
disputes.iter().for_each(|dss| { disputes.into_iter().for_each(|dss| {
let dispute_weight = <<T as Config>::WeightInfo as WeightInfo>::enter_variable_disputes( let dispute_weight = dispute_statement_set_weight::<T, &DisputeStatementSet>(&dss);
dss.statements.len() as u32,
);
let updated = weight_acc.saturating_add(dispute_weight); let updated = weight_acc.saturating_add(dispute_weight);
if max_consumable_weight.all_gte(updated) { if max_consumable_weight.all_gte(updated) {
// only apply the weight if the validity check passes // Always apply the weight. Invalid data cost processing time too:
if let Some(checked) = dispute_statement_set_valid(dss.clone()) { weight_acc = updated;
if let Some(checked) = dispute_statement_set_valid(dss) {
checked_acc.push(checked); checked_acc.push(checked);
weight_acc = updated;
} }
} }
}); });
// Compute the statements length of all remote disputes
let d = remote_disputes.iter().map(|d| d.statements.len() as u32).collect::<Vec<u32>>();
// Select remote disputes at random until the block is full
let (_acc_remote_disputes_weight, mut indices) = random_sel::<u32, _>(
rng,
&d,
vec![],
|v| <<T as Config>::WeightInfo as WeightInfo>::enter_variable_disputes(*v),
max_consumable_weight.saturating_sub(weight_acc),
);
// Sort the indices, to retain the same sorting as the input.
indices.sort();
// Add the remote disputes after checking their validity.
checked_acc.extend(indices.into_iter().filter_map(|idx| {
dispute_statement_set_valid(remote_disputes[idx].clone()).map(|cdss| {
let weight = <<T as Config>::WeightInfo as WeightInfo>::enter_variable_disputes(
cdss.as_ref().statements.len() as u32,
);
weight_acc = weight_acc.saturating_add(weight);
cdss
})
}));
// Update the remaining weight
(checked_acc, weight_acc) (checked_acc, weight_acc)
} else { } else {
// Go through all of them, and just apply the filter, they would all fit // Go through all of them, and just apply the filter, they would all fit
@@ -1037,7 +1017,7 @@ fn limit_and_sanitize_disputes<
.filter_map(|dss| dispute_statement_set_valid(dss)) .filter_map(|dss| dispute_statement_set_valid(dss))
.collect::<Vec<CheckedDisputeStatementSet>>(); .collect::<Vec<CheckedDisputeStatementSet>>();
// some might have been filtered out, so re-calc the weight // some might have been filtered out, so re-calc the weight
let checked_disputes_weight = multi_dispute_statement_sets_weight::<T, _, _>(&checked); let checked_disputes_weight = checked_multi_dispute_statement_sets_weight::<T>(&checked);
(checked, checked_disputes_weight) (checked, checked_disputes_weight)
} }
} }
@@ -21,13 +21,16 @@ use super::*;
// weights for limiting data will fail, so we don't run them when using the benchmark feature. // weights for limiting data will fail, so we don't run them when using the benchmark feature.
#[cfg(not(feature = "runtime-benchmarks"))] #[cfg(not(feature = "runtime-benchmarks"))]
mod enter { mod enter {
use super::*; use super::*;
use crate::{ use crate::{
builder::{Bench, BenchBuilder}, builder::{Bench, BenchBuilder},
mock::{new_test_ext, MockGenesisConfig, Test}, mock::{new_test_ext, BlockLength, BlockWeights, MockGenesisConfig, Test},
}; };
use assert_matches::assert_matches; use assert_matches::assert_matches;
use frame_support::assert_ok; use frame_support::assert_ok;
use frame_system::limits;
use sp_runtime::Perbill;
use sp_std::collections::btree_map::BTreeMap; use sp_std::collections::btree_map::BTreeMap;
struct TestConfig { struct TestConfig {
@@ -300,6 +303,7 @@ mod enter {
// Ensure that when dispute data establishes an over weight block that we adequately // Ensure that when dispute data establishes an over weight block that we adequately
// filter out disputes according to our prioritization rule // filter out disputes according to our prioritization rule
fn limit_dispute_data() { fn limit_dispute_data() {
sp_tracing::try_init_simple();
new_test_ext(MockGenesisConfig::default()).execute_with(|| { new_test_ext(MockGenesisConfig::default()).execute_with(|| {
// Create the inherent data for this block // Create the inherent data for this block
let dispute_statements = BTreeMap::new(); let dispute_statements = BTreeMap::new();
@@ -486,7 +490,8 @@ mod enter {
assert_ne!(limit_inherent_data, expected_para_inherent_data); assert_ne!(limit_inherent_data, expected_para_inherent_data);
assert!(inherent_data_weight(&limit_inherent_data) assert!(inherent_data_weight(&limit_inherent_data)
.all_lte(inherent_data_weight(&expected_para_inherent_data))); .all_lte(inherent_data_weight(&expected_para_inherent_data)));
assert!(inherent_data_weight(&limit_inherent_data).all_lte(max_block_weight())); assert!(inherent_data_weight(&limit_inherent_data)
.all_lte(max_block_weight_proof_size_adjusted()));
// Three disputes is over weight (see previous test), so we expect to only see 2 disputes // Three disputes is over weight (see previous test), so we expect to only see 2 disputes
assert_eq!(limit_inherent_data.disputes.len(), 2); assert_eq!(limit_inherent_data.disputes.len(), 2);
@@ -565,17 +570,18 @@ mod enter {
}); });
} }
fn max_block_weight() -> Weight { fn max_block_weight_proof_size_adjusted() -> Weight {
<Test as frame_system::Config>::BlockWeights::get().max_block let raw_weight = <Test as frame_system::Config>::BlockWeights::get().max_block;
let block_length = <Test as frame_system::Config>::BlockLength::get();
raw_weight.set_proof_size(*block_length.max.get(DispatchClass::Mandatory) as u64)
} }
fn inherent_data_weight(inherent_data: &ParachainsInherentData) -> Weight { fn inherent_data_weight(inherent_data: &ParachainsInherentData) -> Weight {
use thousands::Separable; use thousands::Separable;
let multi_dispute_statement_sets_weight = let multi_dispute_statement_sets_weight =
multi_dispute_statement_sets_weight::<Test, _, _>(&inherent_data.disputes); multi_dispute_statement_sets_weight::<Test>(&inherent_data.disputes);
let signed_bitfields_weight = let signed_bitfields_weight = signed_bitfields_weight::<Test>(&inherent_data.bitfields);
signed_bitfields_weight::<Test>(inherent_data.bitfields.len());
let backed_candidates_weight = let backed_candidates_weight =
backed_candidates_weight::<Test>(&inherent_data.backed_candidates); backed_candidates_weight::<Test>(&inherent_data.backed_candidates);
@@ -622,7 +628,8 @@ mod enter {
}); });
let expected_para_inherent_data = scenario.data.clone(); let expected_para_inherent_data = scenario.data.clone();
assert!(max_block_weight().any_lt(inherent_data_weight(&expected_para_inherent_data))); assert!(max_block_weight_proof_size_adjusted()
.any_lt(inherent_data_weight(&expected_para_inherent_data)));
// Check the para inherent data is as expected: // Check the para inherent data is as expected:
// * 1 bitfield per validator (5 validators per core, 2 backed candidates, 3 disputes => 5*5 = 25) // * 1 bitfield per validator (5 validators per core, 2 backed candidates, 3 disputes => 5*5 = 25)
@@ -641,9 +648,10 @@ mod enter {
// Expect that inherent data is filtered to include only 1 backed candidate and 2 disputes // Expect that inherent data is filtered to include only 1 backed candidate and 2 disputes
assert!(limit_inherent_data != expected_para_inherent_data); assert!(limit_inherent_data != expected_para_inherent_data);
assert!( assert!(
max_block_weight().all_gte(inherent_data_weight(&limit_inherent_data)), max_block_weight_proof_size_adjusted()
.all_gte(inherent_data_weight(&limit_inherent_data)),
"Post limiting exceeded block weight: max={} vs. inherent={}", "Post limiting exceeded block weight: max={} vs. inherent={}",
max_block_weight(), max_block_weight_proof_size_adjusted(),
inherent_data_weight(&limit_inherent_data) inherent_data_weight(&limit_inherent_data)
); );
@@ -675,8 +683,199 @@ mod enter {
} }
#[test] #[test]
fn disputes_are_size_limited() {
BlockLength::set(limits::BlockLength::max_with_normal_ratio(
600,
Perbill::from_percent(75),
));
// Virtually no time based limit:
BlockWeights::set(frame_system::limits::BlockWeights::simple_max(Weight::from_parts(
u64::MAX,
u64::MAX,
)));
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
// Create the inherent data for this block
let mut dispute_statements = BTreeMap::new();
dispute_statements.insert(2, 7);
dispute_statements.insert(3, 7);
dispute_statements.insert(4, 7);
let backed_and_concluding = BTreeMap::new();
let scenario = make_inherent_data(TestConfig {
dispute_statements,
dispute_sessions: vec![2, 2, 1], // 3 cores with disputes
backed_and_concluding,
num_validators_per_core: 5,
code_upgrade: None,
});
let expected_para_inherent_data = scenario.data.clone();
assert!(max_block_weight_proof_size_adjusted()
.any_lt(inherent_data_weight(&expected_para_inherent_data)));
// Check the para inherent data is as expected:
// * 1 bitfield per validator (5 validators per core, 3 disputes => 3*5 = 15)
assert_eq!(expected_para_inherent_data.bitfields.len(), 15);
// * 2 backed candidates
assert_eq!(expected_para_inherent_data.backed_candidates.len(), 0);
// * 3 disputes.
assert_eq!(expected_para_inherent_data.disputes.len(), 3);
let mut inherent_data = InherentData::new();
inherent_data
.put_data(PARACHAINS_INHERENT_IDENTIFIER, &expected_para_inherent_data)
.unwrap();
let limit_inherent_data =
Pallet::<Test>::create_inherent_inner(&inherent_data.clone()).unwrap();
// Expect that inherent data is filtered to include only 1 backed candidate and 2 disputes
assert!(limit_inherent_data != expected_para_inherent_data);
assert!(
max_block_weight_proof_size_adjusted()
.all_gte(inherent_data_weight(&limit_inherent_data)),
"Post limiting exceeded block weight: max={} vs. inherent={}",
max_block_weight_proof_size_adjusted(),
inherent_data_weight(&limit_inherent_data)
);
// * 1 bitfields - gone
assert_eq!(limit_inherent_data.bitfields.len(), 0);
// * 2 backed candidates - still none.
assert_eq!(limit_inherent_data.backed_candidates.len(), 0);
// * 3 disputes - filtered.
assert_eq!(limit_inherent_data.disputes.len(), 1);
});
}
#[test]
fn bitfields_are_size_limited() {
BlockLength::set(limits::BlockLength::max_with_normal_ratio(
600,
Perbill::from_percent(75),
));
// Virtually no time based limit:
BlockWeights::set(frame_system::limits::BlockWeights::simple_max(Weight::from_parts(
u64::MAX,
u64::MAX,
)));
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
// Create the inherent data for this block
let dispute_statements = BTreeMap::new();
let mut backed_and_concluding = BTreeMap::new();
// 2 backed candidates shall be scheduled
backed_and_concluding.insert(0, 2);
backed_and_concluding.insert(1, 2);
let scenario = make_inherent_data(TestConfig {
dispute_statements,
dispute_sessions: Vec::new(),
backed_and_concluding,
num_validators_per_core: 5,
code_upgrade: None,
});
let expected_para_inherent_data = scenario.data.clone();
assert!(max_block_weight_proof_size_adjusted()
.any_lt(inherent_data_weight(&expected_para_inherent_data)));
// Check the para inherent data is as expected:
// * 1 bitfield per validator (5 validators per core, 2 backed candidates => 2*5 = 10)
assert_eq!(expected_para_inherent_data.bitfields.len(), 10);
// * 2 backed candidates
assert_eq!(expected_para_inherent_data.backed_candidates.len(), 2);
// * 3 disputes.
assert_eq!(expected_para_inherent_data.disputes.len(), 0);
let mut inherent_data = InherentData::new();
inherent_data
.put_data(PARACHAINS_INHERENT_IDENTIFIER, &expected_para_inherent_data)
.unwrap();
let limit_inherent_data =
Pallet::<Test>::create_inherent_inner(&inherent_data.clone()).unwrap();
// Expect that inherent data is filtered to include only 1 backed candidate and 2 disputes
assert!(limit_inherent_data != expected_para_inherent_data);
assert!(
max_block_weight_proof_size_adjusted()
.all_gte(inherent_data_weight(&limit_inherent_data)),
"Post limiting exceeded block weight: max={} vs. inherent={}",
max_block_weight_proof_size_adjusted(),
inherent_data_weight(&limit_inherent_data)
);
// * 1 bitfields have been filtered
assert_eq!(limit_inherent_data.bitfields.len(), 8);
// * 2 backed candidates have been filtered as well (not even space for bitfields)
assert_eq!(limit_inherent_data.backed_candidates.len(), 0);
// * 3 disputes. Still none.
assert_eq!(limit_inherent_data.disputes.len(), 0);
});
}
#[test]
fn candidates_are_size_limited() {
BlockLength::set(limits::BlockLength::max_with_normal_ratio(
1_300,
Perbill::from_percent(75),
));
// Virtually no time based limit:
BlockWeights::set(frame_system::limits::BlockWeights::simple_max(Weight::from_parts(
u64::MAX,
u64::MAX,
)));
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
let mut backed_and_concluding = BTreeMap::new();
// 2 backed candidates shall be scheduled
backed_and_concluding.insert(0, 2);
backed_and_concluding.insert(1, 2);
let scenario = make_inherent_data(TestConfig {
dispute_statements: BTreeMap::new(),
dispute_sessions: Vec::new(),
backed_and_concluding,
num_validators_per_core: 5,
code_upgrade: None,
});
let expected_para_inherent_data = scenario.data.clone();
assert!(max_block_weight_proof_size_adjusted()
.any_lt(inherent_data_weight(&expected_para_inherent_data)));
// Check the para inherent data is as expected:
// * 1 bitfield per validator (5 validators per core, 2 backed candidates, 0 disputes => 2*5 = 10)
assert_eq!(expected_para_inherent_data.bitfields.len(), 10);
// * 2 backed candidates
assert_eq!(expected_para_inherent_data.backed_candidates.len(), 2);
// * 0 disputes.
assert_eq!(expected_para_inherent_data.disputes.len(), 0);
let mut inherent_data = InherentData::new();
inherent_data
.put_data(PARACHAINS_INHERENT_IDENTIFIER, &expected_para_inherent_data)
.unwrap();
let limit_inherent_data =
Pallet::<Test>::create_inherent_inner(&inherent_data.clone()).unwrap();
// Expect that inherent data is filtered to include only 1 backed candidate and 2 disputes
assert!(limit_inherent_data != expected_para_inherent_data);
assert!(
max_block_weight_proof_size_adjusted()
.all_gte(inherent_data_weight(&limit_inherent_data)),
"Post limiting exceeded block weight: max={} vs. inherent={}",
max_block_weight_proof_size_adjusted(),
inherent_data_weight(&limit_inherent_data)
);
// * 1 bitfields - no filtering here
assert_eq!(limit_inherent_data.bitfields.len(), 10);
// * 2 backed candidates
assert_eq!(limit_inherent_data.backed_candidates.len(), 1);
// * 0 disputes.
assert_eq!(limit_inherent_data.disputes.len(), 0);
});
}
// Ensure that overweight parachain inherents are always rejected by the runtime. // Ensure that overweight parachain inherents are always rejected by the runtime.
// Runtime should panic and return `InherentOverweight` error. // Runtime should panic and return `InherentOverweight` error.
#[test]
fn inherent_create_weight_invariant() { fn inherent_create_weight_invariant() {
new_test_ext(MockGenesisConfig::default()).execute_with(|| { new_test_ext(MockGenesisConfig::default()).execute_with(|| {
// Create an overweight inherent and oversized block // Create an overweight inherent and oversized block
@@ -700,7 +899,8 @@ mod enter {
}); });
let expected_para_inherent_data = scenario.data.clone(); let expected_para_inherent_data = scenario.data.clone();
assert!(max_block_weight().any_lt(inherent_data_weight(&expected_para_inherent_data))); assert!(max_block_weight_proof_size_adjusted()
.any_lt(inherent_data_weight(&expected_para_inherent_data)));
// Check the para inherent data is as expected: // Check the para inherent data is as expected:
// * 1 bitfield per validator (5 validators per core, 30 backed candidates, 3 disputes => 5*33 = 165) // * 1 bitfield per validator (5 validators per core, 30 backed candidates, 3 disputes => 5*33 = 165)
@@ -713,7 +913,6 @@ mod enter {
inherent_data inherent_data
.put_data(PARACHAINS_INHERENT_IDENTIFIER, &expected_para_inherent_data) .put_data(PARACHAINS_INHERENT_IDENTIFIER, &expected_para_inherent_data)
.unwrap(); .unwrap();
let dispatch_error = Pallet::<Test>::enter( let dispatch_error = Pallet::<Test>::enter(
frame_system::RawOrigin::None.into(), frame_system::RawOrigin::None.into(),
expected_para_inherent_data, expected_para_inherent_data,
@@ -724,8 +923,6 @@ mod enter {
assert_eq!(dispatch_error, Error::<Test>::InherentOverweight.into()); assert_eq!(dispatch_error, Error::<Test>::InherentOverweight.into());
}); });
} }
// TODO: Test process inherent invariant
} }
fn default_header() -> primitives::Header { fn default_header() -> primitives::Header {
@@ -13,10 +13,20 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>. // along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use super::{
BackedCandidate, Config, DisputeStatementSet, UncheckedSignedAvailabilityBitfield, Weight, //! We use benchmarks to get time weights, for proof_size we manually use the size of the input
//! data, which will be part of the block. This is because we don't care about the storage proof on
//! the relay chain, but we do care about the size of the block, by putting the tx in the
//! proof_size we can use the already existing weight limiting code to limit the used size as well.
use parity_scale_codec::{Encode, WrapperTypeEncode};
use primitives::{
CheckedMultiDisputeStatementSet, MultiDisputeStatementSet, UncheckedSignedAvailabilityBitfield,
UncheckedSignedAvailabilityBitfields,
}; };
use super::{BackedCandidate, Config, DisputeStatementSet, Weight};
pub trait WeightInfo { pub trait WeightInfo {
/// Variant over `v`, the count of dispute statements in a dispute statement set. This gives the /// Variant over `v`, the count of dispute statements in a dispute statement set. This gives the
/// weight of a single dispute statement set. /// weight of a single dispute statement set.
@@ -72,51 +82,82 @@ impl WeightInfo for TestWeightInfo {
pub fn paras_inherent_total_weight<T: Config>( pub fn paras_inherent_total_weight<T: Config>(
backed_candidates: &[BackedCandidate<<T as frame_system::Config>::Hash>], backed_candidates: &[BackedCandidate<<T as frame_system::Config>::Hash>],
bitfields: &[UncheckedSignedAvailabilityBitfield], bitfields: &UncheckedSignedAvailabilityBitfields,
disputes: &[DisputeStatementSet], disputes: &MultiDisputeStatementSet,
) -> Weight { ) -> Weight {
backed_candidates_weight::<T>(backed_candidates) backed_candidates_weight::<T>(backed_candidates)
.saturating_add(signed_bitfields_weight::<T>(bitfields.len())) .saturating_add(signed_bitfields_weight::<T>(bitfields))
.saturating_add(multi_dispute_statement_sets_weight::<T, _, _>(disputes)) .saturating_add(multi_dispute_statement_sets_weight::<T>(disputes))
} }
pub fn dispute_statement_set_weight<T: Config, S: AsRef<DisputeStatementSet>>( pub fn multi_dispute_statement_sets_weight<T: Config>(
statement_set: S, disputes: &MultiDisputeStatementSet,
) -> Weight { ) -> Weight {
<<T as Config>::WeightInfo as WeightInfo>::enter_variable_disputes( set_proof_size_to_tx_size(
statement_set.as_ref().statements.len() as u32, disputes
.iter()
.map(|d| dispute_statement_set_weight::<T, _>(d))
.fold(Weight::zero(), |acc_weight, weight| acc_weight.saturating_add(weight)),
disputes,
) )
} }
pub fn multi_dispute_statement_sets_weight< pub fn checked_multi_dispute_statement_sets_weight<T: Config>(
T: Config, disputes: &CheckedMultiDisputeStatementSet,
D: AsRef<[S]>,
S: AsRef<DisputeStatementSet>,
>(
disputes: D,
) -> Weight { ) -> Weight {
disputes set_proof_size_to_tx_size(
.as_ref() disputes
.iter() .iter()
.map(|d| dispute_statement_set_weight::<T, &S>(d)) .map(|d| dispute_statement_set_weight::<T, _>(d))
.fold(Weight::zero(), |acc_weight, weight| acc_weight.saturating_add(weight)) .fold(Weight::zero(), |acc_weight, weight| acc_weight.saturating_add(weight)),
disputes,
)
} }
pub fn signed_bitfields_weight<T: Config>(bitfields_len: usize) -> Weight { /// Get time weights from benchmarks and set proof size to tx size.
<<T as Config>::WeightInfo as WeightInfo>::enter_bitfields() pub fn dispute_statement_set_weight<T, D>(statement_set: D) -> Weight
.saturating_mul(bitfields_len as u64) where
T: Config,
D: AsRef<DisputeStatementSet> + WrapperTypeEncode + Sized + Encode,
{
set_proof_size_to_tx_size(
<<T as Config>::WeightInfo as WeightInfo>::enter_variable_disputes(
statement_set.as_ref().statements.len() as u32,
),
statement_set,
)
}
pub fn signed_bitfields_weight<T: Config>(
bitfields: &UncheckedSignedAvailabilityBitfields,
) -> Weight {
set_proof_size_to_tx_size(
<<T as Config>::WeightInfo as WeightInfo>::enter_bitfields()
.saturating_mul(bitfields.len() as u64),
bitfields,
)
}
pub fn signed_bitfield_weight<T: Config>(bitfield: &UncheckedSignedAvailabilityBitfield) -> Weight {
set_proof_size_to_tx_size(
<<T as Config>::WeightInfo as WeightInfo>::enter_bitfields(),
bitfield,
)
} }
pub fn backed_candidate_weight<T: frame_system::Config + Config>( pub fn backed_candidate_weight<T: frame_system::Config + Config>(
candidate: &BackedCandidate<T::Hash>, candidate: &BackedCandidate<T::Hash>,
) -> Weight { ) -> Weight {
if candidate.candidate.commitments.new_validation_code.is_some() { set_proof_size_to_tx_size(
<<T as Config>::WeightInfo as WeightInfo>::enter_backed_candidate_code_upgrade() if candidate.candidate.commitments.new_validation_code.is_some() {
} else { <<T as Config>::WeightInfo as WeightInfo>::enter_backed_candidate_code_upgrade()
<<T as Config>::WeightInfo as WeightInfo>::enter_backed_candidates_variable( } else {
candidate.validity_votes.len() as u32, <<T as Config>::WeightInfo as WeightInfo>::enter_backed_candidates_variable(
) candidate.validity_votes.len() as u32,
} )
},
candidate,
)
} }
pub fn backed_candidates_weight<T: frame_system::Config + Config>( pub fn backed_candidates_weight<T: frame_system::Config + Config>(
@@ -127,3 +168,8 @@ pub fn backed_candidates_weight<T: frame_system::Config + Config>(
.map(|c| backed_candidate_weight::<T>(c)) .map(|c| backed_candidate_weight::<T>(c))
.fold(Weight::zero(), |acc, x| acc.saturating_add(x)) .fold(Weight::zero(), |acc, x| acc.saturating_add(x))
} }
/// Set proof_size component of `Weight` to tx size.
fn set_proof_size_to_tx_size<Arg: Encode>(weight: Weight, arg: Arg) -> Weight {
weight.set_proof_size(arg.encoded_size() as u64)
}
@@ -17,27 +17,26 @@
//! Autogenerated weights for `runtime_parachains::paras_inherent` //! Autogenerated weights for `runtime_parachains::paras_inherent`
//! //!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-06-19, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! DATE: 2023-06-29, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000` //! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `runner-e8ezs4ez-project-163-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz` //! HOSTNAME: `runner-xerhrdyb-project-163-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("polkadot-dev"), DB CACHE: 1024 //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("polkadot-dev"), DB CACHE: 1024
// Executed Command: // Executed Command:
// ./target/production/polkadot // target/production/polkadot
// benchmark // benchmark
// pallet // pallet
// --chain=polkadot-dev
// --steps=50 // --steps=50
// --repeat=20 // --repeat=20
// --no-storage-info
// --no-median-slopes
// --no-min-squares
// --pallet=runtime_parachains::paras_inherent
// --extrinsic=* // --extrinsic=*
// --execution=wasm // --execution=wasm
// --wasm-execution=compiled // --wasm-execution=compiled
// --heap-pages=4096
// --json-file=/builds/parity/mirrors/polkadot/.git/.artifacts/bench.json
// --pallet=runtime_parachains::paras_inherent
// --chain=polkadot-dev
// --header=./file_header.txt // --header=./file_header.txt
// --output=./runtime/polkadot/src/weights/runtime_parachains_paras_inherent.rs // --output=./runtime/polkadot/src/weights/
#![cfg_attr(rustfmt, rustfmt_skip)] #![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)] #![allow(unused_parens)]
@@ -117,11 +116,11 @@ impl<T: frame_system::Config> runtime_parachains::paras_inherent::WeightInfo for
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `50915` // Measured: `50915`
// Estimated: `56855 + v * (23 ±0)` // Estimated: `56855 + v * (23 ±0)`
// Minimum execution time: 999_775_000 picoseconds. // Minimum execution time: 999_704_000 picoseconds.
Weight::from_parts(461_856_558, 0) Weight::from_parts(455_751_887, 0)
.saturating_add(Weight::from_parts(0, 56855)) .saturating_add(Weight::from_parts(0, 56855))
// Standard Error: 15_669 // Standard Error: 14_301
.saturating_add(Weight::from_parts(56_847_986, 0).saturating_mul(v.into())) .saturating_add(Weight::from_parts(57_084_663, 0).saturating_mul(v.into()))
.saturating_add(T::DbWeight::get().reads(28)) .saturating_add(T::DbWeight::get().reads(28))
.saturating_add(T::DbWeight::get().writes(15)) .saturating_add(T::DbWeight::get().writes(15))
.saturating_add(Weight::from_parts(0, 23).saturating_mul(v.into())) .saturating_add(Weight::from_parts(0, 23).saturating_mul(v.into()))
@@ -190,8 +189,8 @@ impl<T: frame_system::Config> runtime_parachains::paras_inherent::WeightInfo for
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `42748` // Measured: `42748`
// Estimated: `48688` // Estimated: `48688`
// Minimum execution time: 457_800_000 picoseconds. // Minimum execution time: 485_153_000 picoseconds.
Weight::from_parts(482_446_000, 0) Weight::from_parts(504_774_000, 0)
.saturating_add(Weight::from_parts(0, 48688)) .saturating_add(Weight::from_parts(0, 48688))
.saturating_add(T::DbWeight::get().reads(26)) .saturating_add(T::DbWeight::get().reads(26))
.saturating_add(T::DbWeight::get().writes(16)) .saturating_add(T::DbWeight::get().writes(16))
@@ -265,11 +264,11 @@ impl<T: frame_system::Config> runtime_parachains::paras_inherent::WeightInfo for
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `42784` // Measured: `42784`
// Estimated: `48724` // Estimated: `48724`
// Minimum execution time: 6_889_257_000 picoseconds. // Minimum execution time: 6_906_795_000 picoseconds.
Weight::from_parts(1_240_166_857, 0) Weight::from_parts(1_315_944_667, 0)
.saturating_add(Weight::from_parts(0, 48724)) .saturating_add(Weight::from_parts(0, 48724))
// Standard Error: 23_642 // Standard Error: 31_132
.saturating_add(Weight::from_parts(56_311_928, 0).saturating_mul(v.into())) .saturating_add(Weight::from_parts(55_792_755, 0).saturating_mul(v.into()))
.saturating_add(T::DbWeight::get().reads(29)) .saturating_add(T::DbWeight::get().reads(29))
.saturating_add(T::DbWeight::get().writes(15)) .saturating_add(T::DbWeight::get().writes(15))
} }
@@ -345,8 +344,8 @@ impl<T: frame_system::Config> runtime_parachains::paras_inherent::WeightInfo for
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `42811` // Measured: `42811`
// Estimated: `48751` // Estimated: `48751`
// Minimum execution time: 41_983_250_000 picoseconds. // Minimum execution time: 44_487_810_000 picoseconds.
Weight::from_parts(43_216_188_000, 0) Weight::from_parts(46_317_208_000, 0)
.saturating_add(Weight::from_parts(0, 48751)) .saturating_add(Weight::from_parts(0, 48751))
.saturating_add(T::DbWeight::get().reads(31)) .saturating_add(T::DbWeight::get().reads(31))
.saturating_add(T::DbWeight::get().writes(15)) .saturating_add(T::DbWeight::get().writes(15))
@@ -17,27 +17,26 @@
//! Autogenerated weights for `runtime_parachains::paras_inherent` //! Autogenerated weights for `runtime_parachains::paras_inherent`
//! //!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-06-14, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! DATE: 2023-06-29, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000` //! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `runner--ss9ysm1-project-163-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz` //! HOSTNAME: `runner-xerhrdyb-project-163-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("westend-dev"), DB CACHE: 1024 //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("westend-dev"), DB CACHE: 1024
// Executed Command: // Executed Command:
// ./target/production/polkadot // target/production/polkadot
// benchmark // benchmark
// pallet // pallet
// --chain=westend-dev
// --steps=50 // --steps=50
// --repeat=20 // --repeat=20
// --no-storage-info
// --no-median-slopes
// --no-min-squares
// --pallet=runtime_parachains::paras_inherent
// --extrinsic=* // --extrinsic=*
// --execution=wasm // --execution=wasm
// --wasm-execution=compiled // --wasm-execution=compiled
// --heap-pages=4096
// --json-file=/builds/parity/mirrors/polkadot/.git/.artifacts/bench.json
// --pallet=runtime_parachains::paras_inherent
// --chain=westend-dev
// --header=./file_header.txt // --header=./file_header.txt
// --output=./runtime/westend/src/weights/runtime_parachains_paras_inherent.rs // --output=./runtime/westend/src/weights/
#![cfg_attr(rustfmt, rustfmt_skip)] #![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)] #![allow(unused_parens)]
@@ -115,11 +114,11 @@ impl<T: frame_system::Config> runtime_parachains::paras_inherent::WeightInfo for
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `50518` // Measured: `50518`
// Estimated: `56458 + v * (23 ±0)` // Estimated: `56458 + v * (23 ±0)`
// Minimum execution time: 992_257_000 picoseconds. // Minimum execution time: 998_338_000 picoseconds.
Weight::from_parts(473_224_562, 0) Weight::from_parts(468_412_001, 0)
.saturating_add(Weight::from_parts(0, 56458)) .saturating_add(Weight::from_parts(0, 56458))
// Standard Error: 50_055 // Standard Error: 20_559
.saturating_add(Weight::from_parts(57_274_046, 0).saturating_mul(v.into())) .saturating_add(Weight::from_parts(56_965_025, 0).saturating_mul(v.into()))
.saturating_add(T::DbWeight::get().reads(27)) .saturating_add(T::DbWeight::get().reads(27))
.saturating_add(T::DbWeight::get().writes(15)) .saturating_add(T::DbWeight::get().writes(15))
.saturating_add(Weight::from_parts(0, 23).saturating_mul(v.into())) .saturating_add(Weight::from_parts(0, 23).saturating_mul(v.into()))
@@ -186,8 +185,8 @@ impl<T: frame_system::Config> runtime_parachains::paras_inherent::WeightInfo for
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `42352` // Measured: `42352`
// Estimated: `48292` // Estimated: `48292`
// Minimum execution time: 445_965_000 picoseconds. // Minimum execution time: 457_404_000 picoseconds.
Weight::from_parts(476_329_000, 0) Weight::from_parts(485_416_000, 0)
.saturating_add(Weight::from_parts(0, 48292)) .saturating_add(Weight::from_parts(0, 48292))
.saturating_add(T::DbWeight::get().reads(25)) .saturating_add(T::DbWeight::get().reads(25))
.saturating_add(T::DbWeight::get().writes(16)) .saturating_add(T::DbWeight::get().writes(16))
@@ -259,11 +258,11 @@ impl<T: frame_system::Config> runtime_parachains::paras_inherent::WeightInfo for
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `42387` // Measured: `42387`
// Estimated: `48327` // Estimated: `48327`
// Minimum execution time: 6_877_099_000 picoseconds. // Minimum execution time: 6_864_029_000 picoseconds.
Weight::from_parts(1_267_644_471, 0) Weight::from_parts(1_237_704_892, 0)
.saturating_add(Weight::from_parts(0, 48327)) .saturating_add(Weight::from_parts(0, 48327))
// Standard Error: 96_443 // Standard Error: 33_413
.saturating_add(Weight::from_parts(56_535_707, 0).saturating_mul(v.into())) .saturating_add(Weight::from_parts(56_199_819, 0).saturating_mul(v.into()))
.saturating_add(T::DbWeight::get().reads(28)) .saturating_add(T::DbWeight::get().reads(28))
.saturating_add(T::DbWeight::get().writes(15)) .saturating_add(T::DbWeight::get().writes(15))
} }
@@ -337,8 +336,8 @@ impl<T: frame_system::Config> runtime_parachains::paras_inherent::WeightInfo for
// Proof Size summary in bytes: // Proof Size summary in bytes:
// Measured: `42414` // Measured: `42414`
// Estimated: `48354` // Estimated: `48354`
// Minimum execution time: 40_882_969_000 picoseconds. // Minimum execution time: 43_320_529_000 picoseconds.
Weight::from_parts(45_409_238_000, 0) Weight::from_parts(45_622_613_000, 0)
.saturating_add(Weight::from_parts(0, 48354)) .saturating_add(Weight::from_parts(0, 48354))
.saturating_add(T::DbWeight::get().reads(30)) .saturating_add(T::DbWeight::get().reads(30))
.saturating_add(T::DbWeight::get().writes(15)) .saturating_add(T::DbWeight::get().writes(15))