[runtime] follow up relay chain cleanups (#4657)

* fix miscalculation of remaining weight

* rename a var

* move out enforcing filtering by dropping inherents

* prepare for dispute statement validity check being split off

* refactor

* refactor, only check disputes we actually want to include

* more refactor and documentation

* refactor and minimize inherent checks

* chore: warnings

* fix a few tests

* fix dedup regression

* fix

* more asserts in tests

* remove some asserts

* chore: fmt

* skip signatures checks, some more

* undo unwatend changes

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

Co-authored-by: sandreim <54316454+sandreim@users.noreply.github.com>

* cleanups, checking CheckedDisputeStatments makes no sense

* integrity, if called create_inherent_inner, it shall do the checks, and not rely on enter_inner

* review comments

* use from impl rather than into

* remove outdated comment

* adjust tests accordingly

* assure no weight is lost

* address review comments

* remove unused import

* split error into two and document

* use assurance, O(n)

* Revert "adjust tests accordingly"

This reverts commit 3cc9a3c449f82db38cea22c48f4a21876603374b.

* fix comment

* fix sorting

* comment

Co-authored-by: sandreim <54316454+sandreim@users.noreply.github.com>
This commit is contained in:
Bernhard Schuster
2022-01-20 12:00:29 +01:00
committed by GitHub
parent 883b490cae
commit b7a05fd40b
14 changed files with 1215 additions and 544 deletions
@@ -26,6 +26,7 @@ mod enter {
builder::{Bench, BenchBuilder},
mock::{new_test_ext, MockGenesisConfig, Test},
};
use assert_matches::assert_matches;
use frame_support::assert_ok;
use sp_std::collections::btree_map::BTreeMap;
@@ -295,10 +296,10 @@ mod enter {
// The current schedule is empty prior to calling `create_inherent_enter`.
assert_eq!(<scheduler::Pallet<Test>>::scheduled(), vec![]);
assert_ok!(Pallet::<Test>::enter(
assert_matches!(Pallet::<Test>::enter(
frame_system::RawOrigin::None.into(),
expected_para_inherent_data,
));
), Err(e) => { dbg!(e) });
});
}
@@ -417,24 +418,26 @@ mod enter {
assert_eq!(<scheduler::Pallet<Test>>::scheduled(), vec![]);
// Ensure that calling enter with 3 disputes and 2 candidates is over weight
assert_ok!(Pallet::<Test>::enter(
assert_matches!(Pallet::<Test>::enter(
frame_system::RawOrigin::None.into(),
expected_para_inherent_data,
));
), Err(e) => {
dbg!(e)
});
assert_eq!(
// The length of this vec is equal to the number of candidates, so we know
// all of our candidates got filtered out
Pallet::<Test>::on_chain_votes().unwrap().backing_validators_per_candidate.len(),
0,
Pallet::<Test>::on_chain_votes(),
None,
);
});
}
#[test]
// Ensure that when a block is over weight due to disputes and bitfields, the bitfields are
// Ensure an overweight block with an excess amount of disputes and bitfields, the bitfields are
// filtered to accommodate the block size and no backed candidates are included.
fn limit_bitfields() {
fn limit_bitfields_some() {
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
// Create the inherent data for this block
let mut dispute_statements = BTreeMap::new();
@@ -477,7 +480,12 @@ mod enter {
// Nothing is filtered out (including the backed candidates.)
let limit_inherent_data =
Pallet::<Test>::create_inherent_inner(&inherent_data.clone()).unwrap();
assert!(limit_inherent_data != expected_para_inherent_data);
assert_ne!(limit_inherent_data, expected_para_inherent_data);
assert!(
inherent_data_weight(&limit_inherent_data) <=
inherent_data_weight(&expected_para_inherent_data)
);
assert!(inherent_data_weight(&limit_inherent_data) <= max_block_weight());
// Three disputes is over weight (see previous test), so we expect to only see 2 disputes
assert_eq!(limit_inherent_data.disputes.len(), 2);
@@ -551,20 +559,49 @@ mod enter {
// The current schedule is empty prior to calling `create_inherent_enter`.
assert_eq!(<scheduler::Pallet<Test>>::scheduled(), vec![]);
assert_ok!(Pallet::<Test>::enter(
assert_matches!(Pallet::<Test>::enter(
frame_system::RawOrigin::None.into(),
expected_para_inherent_data,
));
), Err(_e) => {
/* TODO */
});
assert_eq!(
// The length of this vec is equal to the number of candidates, so we know
// all of our candidates got filtered out
Pallet::<Test>::on_chain_votes().unwrap().backing_validators_per_candidate.len(),
0,
);
// The block was not included, as such, `on_chain_votes` _must_ return `None`.
assert_matches!(Pallet::<Test>::on_chain_votes(), None);
});
}
fn max_block_weight() -> Weight {
<Test as frame_system::Config>::BlockWeights::get().max_block
}
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());
let backed_candidates_weight =
backed_candidates_weight::<Test>(&inherent_data.backed_candidates);
let sum = multi_dispute_statement_sets_weight +
signed_bitfields_weight +
backed_candidates_weight;
println!(
"disputes({})={} + bitfields({})={} + candidates({})={} -> {}",
inherent_data.disputes.len(),
multi_dispute_statement_sets_weight.separate_with_underscores(),
inherent_data.bitfields.len(),
signed_bitfields_weight.separate_with_underscores(),
inherent_data.backed_candidates.len(),
backed_candidates_weight.separate_with_underscores(),
sum.separate_with_underscores()
);
sum
}
#[test]
// Ensure that when a block is over weight due to disputes and bitfields, we abort
fn limit_candidates_over_weight_1() {
@@ -591,6 +628,7 @@ mod enter {
});
let expected_para_inherent_data = scenario.data.clone();
assert!(max_block_weight() < 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)
@@ -608,6 +646,12 @@ mod enter {
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() >= inherent_data_weight(&limit_inherent_data),
"Post limiting exceeded block weight: max={} vs. inherent={}",
max_block_weight(),
inherent_data_weight(&limit_inherent_data)
);
// * 1 bitfields
assert_eq!(limit_inherent_data.bitfields.len(), 25);
@@ -668,17 +712,12 @@ mod enter {
// * 3 disputes.
assert_eq!(expected_para_inherent_data.disputes.len(), 3);
assert_ok!(Pallet::<Test>::enter(
assert_matches!(Pallet::<Test>::enter(
frame_system::RawOrigin::None.into(),
expected_para_inherent_data,
));
), Err(e) => { dbg!(e) });
assert_eq!(
// The length of this vec is equal to the number of candidates, so we know our 2
// backed candidates did not get filtered out
Pallet::<Test>::on_chain_votes().unwrap().backing_validators_per_candidate.len(),
0
);
assert_matches!(Pallet::<Test>::on_chain_votes(), None);
});
}
}