Skip to content

Commit

Permalink
add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
ordian committed Jun 12, 2024
1 parent afc9260 commit 329a390
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions polkadot/runtime/parachains/src/inclusion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ impl<T: Config> Pallet<T> {
descriptor: candidate.descriptor,
commitments: candidate.commitments,
};

let enact_weight = Self::enact_candidate(
candidate.relay_parent_number,
receipt,
Expand Down
8 changes: 6 additions & 2 deletions polkadot/runtime/parachains/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
scheduler::common::AssignmentProvider,
session_info, shared, ParaId,
};
use frame_support::pallet_prelude::*;
use frame_support::{pallet_prelude::*, weights::RuntimeDbWeight};
use polkadot_primitives::CoreIndex;

use codec::Decode;
Expand Down Expand Up @@ -105,6 +105,10 @@ parameter_types! {
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 static DbWeight : RuntimeDbWeight = RuntimeDbWeight {
read: 8_000_000,
write: 50_000_000,
};
}

pub type AccountId = u64;
Expand All @@ -114,7 +118,7 @@ impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = BlockWeights;
type BlockLength = BlockLength;
type DbWeight = ();
type DbWeight = DbWeight;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Nonce = u64;
Expand Down
8 changes: 8 additions & 0 deletions polkadot/runtime/parachains/src/paras_inherent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,12 +535,20 @@ impl<T: Config> Pallet<T> {
bitfields.clone(),
);
all_weight_after.saturating_accrue(enact_weight);
log::debug!(
target: LOG_TARGET,
"Enacting weight: {}, all weight: {}",
enact_weight.ref_time(),
all_weight_after.ref_time(),
);

// It's possible that that after the enacting the candidates, the total weight
// goes over the limit, however, we can't do anything about it at this point.
// By using the `Mandatory` weight, we ensure the block is still accepted,
// but no other (user) transactions can be included.
if all_weight_after.any_gt(max_block_weight) {
log::warn!(
target: LOG_TARGET,
"Overweight para inherent data after enacting the candidates {:?}: {} > {}",
parent_hash,
all_weight_after,
Expand Down
51 changes: 51 additions & 0 deletions polkadot/runtime/parachains/src/paras_inherent/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,57 @@ mod enter {
});
}

// Ensure that even if the block is over weight due to candidates enactment,
// we still can import it.
#[test]
fn overweight_candidates_enactment_is_fine() {
sp_tracing::try_init_simple();
new_test_ext(MockGenesisConfig::default()).execute_with(|| {
let mut backed_and_concluding = BTreeMap::new();
// The number of candidates is chosen to go over the weight limit
// of the mock runtime together with the `enact_candidate`s weight.
for i in 0..12 {
backed_and_concluding.insert(i, 2);
}

let num_validators_per_core: u32 = 5;
let num_backed = backed_and_concluding.len();
let bitfields_len = num_validators_per_core as usize * num_backed;

let scenario = make_inherent_data(TestConfig {
dispute_statements: BTreeMap::new(),
dispute_sessions: vec![],
backed_and_concluding,
num_validators_per_core,
code_upgrade: None,
fill_claimqueue: true,
elastic_paras: BTreeMap::new(),
unavailable_cores: vec![],
});

let expected_para_inherent_data = scenario.data.clone();

// Check the para inherent data is as expected:
assert_eq!(expected_para_inherent_data.bitfields.len(), bitfields_len);
assert_eq!(expected_para_inherent_data.backed_candidates.len(), num_backed);
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();
assert!(limit_inherent_data == expected_para_inherent_data);

assert_ok!(Pallet::<Test>::enter(
frame_system::RawOrigin::None.into(),
limit_inherent_data,
));
});
}

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();
Expand Down

0 comments on commit 329a390

Please sign in to comment.