mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 11:01:01 +00:00
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:
@@ -32,6 +32,7 @@ use frame_support::{
|
||||
weights::{Weight, WeightMeter},
|
||||
};
|
||||
use frame_support_test::TestRandomness;
|
||||
use frame_system::limits;
|
||||
use parity_scale_codec::Decode;
|
||||
use primitives::{
|
||||
AuthorityDiscoveryId, Balance, BlockNumber, CandidateHash, Moment, SessionIndex, UpwardMessage,
|
||||
@@ -42,7 +43,7 @@ use sp_io::TestExternalities;
|
||||
use sp_runtime::{
|
||||
traits::{AccountIdConversion, BlakeTwo256, IdentityLookup},
|
||||
transaction_validity::TransactionPriority,
|
||||
BuildStorage, Permill,
|
||||
BuildStorage, Perbill, Permill,
|
||||
};
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
|
||||
@@ -81,10 +82,11 @@ where
|
||||
|
||||
parameter_types! {
|
||||
pub const BlockHashCount: u32 = 250;
|
||||
pub BlockWeights: frame_system::limits::BlockWeights =
|
||||
pub static BlockWeights: frame_system::limits::BlockWeights =
|
||||
frame_system::limits::BlockWeights::simple_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;
|
||||
@@ -92,7 +94,7 @@ pub type AccountId = u64;
|
||||
impl frame_system::Config for Test {
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type BlockWeights = BlockWeights;
|
||||
type BlockLength = ();
|
||||
type BlockLength = BlockLength;
|
||||
type DbWeight = ();
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
|
||||
@@ -53,7 +53,6 @@ use rand::{seq::SliceRandom, SeedableRng};
|
||||
use scale_info::TypeInfo;
|
||||
use sp_runtime::traits::{Header as HeaderT, One};
|
||||
use sp_std::{
|
||||
cmp::Ordering,
|
||||
collections::{btree_map::BTreeMap, btree_set::BTreeSet},
|
||||
prelude::*,
|
||||
vec::Vec,
|
||||
@@ -62,12 +61,13 @@ use sp_std::{
|
||||
mod misc;
|
||||
mod weights;
|
||||
|
||||
use self::weights::checked_multi_dispute_statement_sets_weight;
|
||||
pub use self::{
|
||||
misc::{IndexedRetain, IsSortedBy},
|
||||
weights::{
|
||||
backed_candidate_weight, backed_candidates_weight, dispute_statement_set_weight,
|
||||
multi_dispute_statement_sets_weight, paras_inherent_total_weight, signed_bitfields_weight,
|
||||
TestWeightInfo, WeightInfo,
|
||||
multi_dispute_statement_sets_weight, paras_inherent_total_weight, signed_bitfield_weight,
|
||||
signed_bitfields_weight, TestWeightInfo, WeightInfo,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -264,8 +264,8 @@ pub mod pallet {
|
||||
#[pallet::weight((
|
||||
paras_inherent_total_weight::<T>(
|
||||
data.backed_candidates.as_slice(),
|
||||
data.bitfields.as_slice(),
|
||||
data.disputes.as_slice(),
|
||||
&data.bitfields,
|
||||
&data.disputes,
|
||||
),
|
||||
DispatchClass::Mandatory,
|
||||
))]
|
||||
@@ -356,17 +356,47 @@ impl<T: Config> Pallet<T> {
|
||||
let now = <frame_system::Pallet<T>>::block_number();
|
||||
|
||||
let candidates_weight = backed_candidates_weight::<T>(&backed_candidates);
|
||||
let bitfields_weight = signed_bitfields_weight::<T>(bitfields.len());
|
||||
let disputes_weight = multi_dispute_statement_sets_weight::<T, _, _>(&disputes);
|
||||
let max_block_weight = <T as frame_system::Config>::BlockWeights::get().max_block;
|
||||
let bitfields_weight = signed_bitfields_weight::<T>(&bitfields);
|
||||
let disputes_weight = multi_dispute_statement_sets_weight::<T>(&disputes);
|
||||
|
||||
METRICS
|
||||
.on_before_filter((candidates_weight + bitfields_weight + disputes_weight).ref_time());
|
||||
let all_weight_before = candidates_weight + bitfields_weight + disputes_weight;
|
||||
|
||||
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 expected_bits = <scheduler::Pallet<T>>::availability_cores().len();
|
||||
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 mut rng = rand_chacha::ChaChaRng::from_seed(entropy.into());
|
||||
|
||||
@@ -388,10 +418,9 @@ impl<T: Config> Pallet<T> {
|
||||
disputes,
|
||||
dispute_statement_set_valid,
|
||||
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
|
||||
// candidates. Dispute statement sets were already limited before.
|
||||
let non_disputes_weight = apply_weight_limit::<T>(
|
||||
@@ -401,36 +430,38 @@ impl<T: Config> Pallet<T> {
|
||||
&mut rng,
|
||||
);
|
||||
|
||||
let full_weight =
|
||||
let all_weight_after =
|
||||
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) {
|
||||
log::warn!(target: LOG_TARGET, "Post weight limiting weight is still too large.");
|
||||
if all_weight_after.any_gt(max_block_weight) {
|
||||
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());
|
||||
}
|
||||
|
||||
full_weight
|
||||
all_weight_after
|
||||
} 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
|
||||
// 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!(
|
||||
"Overweight para inherent data reached the runtime {:?}: {} > {}",
|
||||
parent_hash,
|
||||
full_weight,
|
||||
all_weight_before,
|
||||
max_block_weight
|
||||
);
|
||||
}
|
||||
|
||||
ensure!(full_weight.all_lte(max_block_weight), Error::<T>::InherentOverweight);
|
||||
full_weight
|
||||
ensure!(all_weight_before.all_lte(max_block_weight), Error::<T>::InherentOverweight);
|
||||
all_weight_before
|
||||
};
|
||||
|
||||
// Note that `process_checked_multi_dispute_data` will iterate and import each
|
||||
@@ -597,7 +628,7 @@ impl<T: Config> Pallet<T> {
|
||||
|
||||
let processed =
|
||||
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 {
|
||||
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);
|
||||
|
||||
@@ -734,6 +765,7 @@ fn apply_weight_limit<T: Config + inclusion::Config>(
|
||||
|c| backed_candidate_weight::<T>(c),
|
||||
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());
|
||||
// pick all bitfields, and
|
||||
// fill the remaining space with candidates
|
||||
@@ -750,9 +782,10 @@ fn apply_weight_limit<T: Config + inclusion::Config>(
|
||||
rng,
|
||||
&bitfields,
|
||||
vec![],
|
||||
|_| <<T as Config>::WeightInfo as WeightInfo>::enter_bitfields(),
|
||||
|bitfield| signed_bitfield_weight::<T>(&bitfield),
|
||||
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());
|
||||
|
||||
@@ -941,94 +974,41 @@ fn compute_entropy<T: Config>(parent_hash: T::Hash) -> [u8; 32] {
|
||||
/// 2. If exceeded:
|
||||
/// 1. Check validity of all dispute statements sequentially
|
||||
/// 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)
|
||||
/// 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`.
|
||||
fn limit_and_sanitize_disputes<
|
||||
T: Config,
|
||||
CheckValidityFn: FnMut(DisputeStatementSet) -> Option<CheckedDisputeStatementSet>,
|
||||
>(
|
||||
mut disputes: MultiDisputeStatementSet,
|
||||
disputes: MultiDisputeStatementSet,
|
||||
mut dispute_statement_set_valid: CheckValidityFn,
|
||||
max_consumable_weight: Weight,
|
||||
rng: &mut rand_chacha::ChaChaRng,
|
||||
) -> (Vec<CheckedDisputeStatementSet>, Weight) {
|
||||
// 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) {
|
||||
log::debug!(target: LOG_TARGET, "Above max consumable weight: {}/{}", disputes_weight, max_consumable_weight);
|
||||
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.
|
||||
let mut weight_acc = Weight::zero();
|
||||
|
||||
// Select disputes in-order until the remaining weight is attained
|
||||
disputes.iter().for_each(|dss| {
|
||||
let dispute_weight = <<T as Config>::WeightInfo as WeightInfo>::enter_variable_disputes(
|
||||
dss.statements.len() as u32,
|
||||
);
|
||||
disputes.into_iter().for_each(|dss| {
|
||||
let dispute_weight = dispute_statement_set_weight::<T, &DisputeStatementSet>(&dss);
|
||||
let updated = weight_acc.saturating_add(dispute_weight);
|
||||
if max_consumable_weight.all_gte(updated) {
|
||||
// only apply the weight if the validity check passes
|
||||
if let Some(checked) = dispute_statement_set_valid(dss.clone()) {
|
||||
// Always apply the weight. Invalid data cost processing time too:
|
||||
weight_acc = updated;
|
||||
if let Some(checked) = dispute_statement_set_valid(dss) {
|
||||
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)
|
||||
} else {
|
||||
// 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))
|
||||
.collect::<Vec<CheckedDisputeStatementSet>>();
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,13 +21,16 @@ use super::*;
|
||||
// weights for limiting data will fail, so we don't run them when using the benchmark feature.
|
||||
#[cfg(not(feature = "runtime-benchmarks"))]
|
||||
mod enter {
|
||||
|
||||
use super::*;
|
||||
use crate::{
|
||||
builder::{Bench, BenchBuilder},
|
||||
mock::{new_test_ext, MockGenesisConfig, Test},
|
||||
mock::{new_test_ext, BlockLength, BlockWeights, MockGenesisConfig, Test},
|
||||
};
|
||||
use assert_matches::assert_matches;
|
||||
use frame_support::assert_ok;
|
||||
use frame_system::limits;
|
||||
use sp_runtime::Perbill;
|
||||
use sp_std::collections::btree_map::BTreeMap;
|
||||
|
||||
struct TestConfig {
|
||||
@@ -300,6 +303,7 @@ mod enter {
|
||||
// Ensure that when dispute data establishes an over weight block that we adequately
|
||||
// filter out disputes according to our prioritization rule
|
||||
fn limit_dispute_data() {
|
||||
sp_tracing::try_init_simple();
|
||||
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
|
||||
// Create the inherent data for this block
|
||||
let dispute_statements = BTreeMap::new();
|
||||
@@ -486,7 +490,8 @@ mod enter {
|
||||
assert_ne!(limit_inherent_data, expected_para_inherent_data);
|
||||
assert!(inherent_data_weight(&limit_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
|
||||
assert_eq!(limit_inherent_data.disputes.len(), 2);
|
||||
@@ -565,17 +570,18 @@ mod enter {
|
||||
});
|
||||
}
|
||||
|
||||
fn max_block_weight() -> Weight {
|
||||
<Test as frame_system::Config>::BlockWeights::get().max_block
|
||||
fn max_block_weight_proof_size_adjusted() -> Weight {
|
||||
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 {
|
||||
use thousands::Separable;
|
||||
|
||||
let multi_dispute_statement_sets_weight =
|
||||
multi_dispute_statement_sets_weight::<Test, _, _>(&inherent_data.disputes);
|
||||
let signed_bitfields_weight =
|
||||
signed_bitfields_weight::<Test>(inherent_data.bitfields.len());
|
||||
multi_dispute_statement_sets_weight::<Test>(&inherent_data.disputes);
|
||||
let signed_bitfields_weight = signed_bitfields_weight::<Test>(&inherent_data.bitfields);
|
||||
let backed_candidates_weight =
|
||||
backed_candidates_weight::<Test>(&inherent_data.backed_candidates);
|
||||
|
||||
@@ -622,7 +628,8 @@ mod enter {
|
||||
});
|
||||
|
||||
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:
|
||||
// * 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
|
||||
assert!(limit_inherent_data != expected_para_inherent_data);
|
||||
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={}",
|
||||
max_block_weight(),
|
||||
max_block_weight_proof_size_adjusted(),
|
||||
inherent_data_weight(&limit_inherent_data)
|
||||
);
|
||||
|
||||
@@ -675,8 +683,199 @@ mod enter {
|
||||
}
|
||||
|
||||
#[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.
|
||||
// Runtime should panic and return `InherentOverweight` error.
|
||||
#[test]
|
||||
fn inherent_create_weight_invariant() {
|
||||
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
|
||||
// Create an overweight inherent and oversized block
|
||||
@@ -700,7 +899,8 @@ mod enter {
|
||||
});
|
||||
|
||||
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:
|
||||
// * 1 bitfield per validator (5 validators per core, 30 backed candidates, 3 disputes => 5*33 = 165)
|
||||
@@ -713,7 +913,6 @@ mod enter {
|
||||
inherent_data
|
||||
.put_data(PARACHAINS_INHERENT_IDENTIFIER, &expected_para_inherent_data)
|
||||
.unwrap();
|
||||
|
||||
let dispatch_error = Pallet::<Test>::enter(
|
||||
frame_system::RawOrigin::None.into(),
|
||||
expected_para_inherent_data,
|
||||
@@ -724,8 +923,6 @@ mod enter {
|
||||
assert_eq!(dispatch_error, Error::<Test>::InherentOverweight.into());
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: Test process inherent invariant
|
||||
}
|
||||
|
||||
fn default_header() -> primitives::Header {
|
||||
|
||||
@@ -13,10 +13,20 @@
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// 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 {
|
||||
/// Variant over `v`, the count of dispute statements in a dispute statement set. This gives the
|
||||
/// weight of a single dispute statement set.
|
||||
@@ -72,51 +82,82 @@ impl WeightInfo for TestWeightInfo {
|
||||
|
||||
pub fn paras_inherent_total_weight<T: Config>(
|
||||
backed_candidates: &[BackedCandidate<<T as frame_system::Config>::Hash>],
|
||||
bitfields: &[UncheckedSignedAvailabilityBitfield],
|
||||
disputes: &[DisputeStatementSet],
|
||||
bitfields: &UncheckedSignedAvailabilityBitfields,
|
||||
disputes: &MultiDisputeStatementSet,
|
||||
) -> Weight {
|
||||
backed_candidates_weight::<T>(backed_candidates)
|
||||
.saturating_add(signed_bitfields_weight::<T>(bitfields.len()))
|
||||
.saturating_add(multi_dispute_statement_sets_weight::<T, _, _>(disputes))
|
||||
.saturating_add(signed_bitfields_weight::<T>(bitfields))
|
||||
.saturating_add(multi_dispute_statement_sets_weight::<T>(disputes))
|
||||
}
|
||||
|
||||
pub fn dispute_statement_set_weight<T: Config, S: AsRef<DisputeStatementSet>>(
|
||||
statement_set: S,
|
||||
pub fn multi_dispute_statement_sets_weight<T: Config>(
|
||||
disputes: &MultiDisputeStatementSet,
|
||||
) -> Weight {
|
||||
<<T as Config>::WeightInfo as WeightInfo>::enter_variable_disputes(
|
||||
statement_set.as_ref().statements.len() as u32,
|
||||
set_proof_size_to_tx_size(
|
||||
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<
|
||||
T: Config,
|
||||
D: AsRef<[S]>,
|
||||
S: AsRef<DisputeStatementSet>,
|
||||
>(
|
||||
disputes: D,
|
||||
pub fn checked_multi_dispute_statement_sets_weight<T: Config>(
|
||||
disputes: &CheckedMultiDisputeStatementSet,
|
||||
) -> Weight {
|
||||
disputes
|
||||
.as_ref()
|
||||
.iter()
|
||||
.map(|d| dispute_statement_set_weight::<T, &S>(d))
|
||||
.fold(Weight::zero(), |acc_weight, weight| acc_weight.saturating_add(weight))
|
||||
set_proof_size_to_tx_size(
|
||||
disputes
|
||||
.iter()
|
||||
.map(|d| dispute_statement_set_weight::<T, _>(d))
|
||||
.fold(Weight::zero(), |acc_weight, weight| acc_weight.saturating_add(weight)),
|
||||
disputes,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn signed_bitfields_weight<T: Config>(bitfields_len: usize) -> Weight {
|
||||
<<T as Config>::WeightInfo as WeightInfo>::enter_bitfields()
|
||||
.saturating_mul(bitfields_len as u64)
|
||||
/// Get time weights from benchmarks and set proof size to tx size.
|
||||
pub fn dispute_statement_set_weight<T, D>(statement_set: D) -> Weight
|
||||
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>(
|
||||
candidate: &BackedCandidate<T::Hash>,
|
||||
) -> Weight {
|
||||
if candidate.candidate.commitments.new_validation_code.is_some() {
|
||||
<<T as Config>::WeightInfo as WeightInfo>::enter_backed_candidate_code_upgrade()
|
||||
} else {
|
||||
<<T as Config>::WeightInfo as WeightInfo>::enter_backed_candidates_variable(
|
||||
candidate.validity_votes.len() as u32,
|
||||
)
|
||||
}
|
||||
set_proof_size_to_tx_size(
|
||||
if candidate.candidate.commitments.new_validation_code.is_some() {
|
||||
<<T as Config>::WeightInfo as WeightInfo>::enter_backed_candidate_code_upgrade()
|
||||
} else {
|
||||
<<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>(
|
||||
@@ -127,3 +168,8 @@ pub fn backed_candidates_weight<T: frame_system::Config + Config>(
|
||||
.map(|c| backed_candidate_weight::<T>(c))
|
||||
.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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user