use super::*;
use crate::{inclusion, ParaId};
use alloc::collections::btree_map::BTreeMap;
use core::cmp::{max, min};
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};
use frame_system::RawOrigin;
use polkadot_primitives::v8::GroupIndex;
use crate::builder::BenchBuilder;
benchmarks! {
enter_empty {
let scenario = BenchBuilder::<T>::new()
.build();
let mut benchmark = scenario.data.clone();
benchmark.bitfields.clear();
benchmark.backed_candidates.clear();
benchmark.disputes.clear();
}: enter(RawOrigin::None, benchmark)
verify {
assert!(Included::<T>::get().is_some());
}
enter_variable_disputes {
let v in 400..BenchBuilder::<T>::fallback_max_validators();
let scenario = BenchBuilder::<T>::new()
.set_dispute_sessions(&[2])
.build();
let mut benchmark = scenario.data.clone();
let dispute = benchmark.disputes.pop().unwrap();
benchmark.bitfields.clear();
benchmark.backed_candidates.clear();
benchmark.disputes.clear();
benchmark.disputes.push(dispute);
benchmark.disputes.get_mut(0).unwrap().statements.drain(v as usize..);
}: enter(RawOrigin::None, benchmark)
verify {
assert!(Included::<T>::get().is_some());
let onchain_votes = OnChainVotes::<T>::get();
assert!(onchain_votes.is_some());
let vote = onchain_votes.unwrap();
assert_eq!(vote.session, scenario._session);
}
enter_bitfields {
let cores_with_backed: BTreeMap<_, _>
= vec![(0, BenchBuilder::<T>::fallback_max_validators())]
.into_iter()
.collect();
let scenario = BenchBuilder::<T>::new()
.set_backed_and_concluding_paras(cores_with_backed)
.build();
let mut benchmark = scenario.data.clone();
let bitfield = benchmark.bitfields.pop().unwrap();
benchmark.bitfields.clear();
benchmark.backed_candidates.clear();
benchmark.disputes.clear();
benchmark.bitfields.push(bitfield);
}: enter(RawOrigin::None, benchmark)
verify {
assert!(Included::<T>::get().is_some());
let onchain_votes = OnChainVotes::<T>::get();
assert!(onchain_votes.is_some());
let vote = onchain_votes.unwrap();
assert_eq!(vote.session, scenario._session);
}
enter_backed_candidates_variable {
let v in (BenchBuilder::<T>::fallback_min_backing_votes())
.. max(BenchBuilder::<T>::fallback_min_backing_votes() + 1, BenchBuilder::<T>::fallback_max_validators_per_core());
let cores_with_backed: BTreeMap<_, _>
= vec![(0, v)] .into_iter()
.collect();
let scenario = BenchBuilder::<T>::new()
.set_backed_in_inherent_paras(cores_with_backed.clone())
.build();
let mut benchmark = scenario.data.clone();
assert_eq!(benchmark.backed_candidates.len(), 1);
let votes = min(scheduler::Pallet::<T>::group_validators(GroupIndex::from(0)).unwrap().len(), v as usize);
assert_eq!(benchmark.backed_candidates.get(0).unwrap().validity_votes().len(), votes);
benchmark.bitfields.clear();
benchmark.disputes.clear();
}: enter(RawOrigin::None, benchmark)
verify {
let max_validators_per_core = BenchBuilder::<T>::fallback_max_validators_per_core();
assert!(Included::<T>::get().is_some());
let onchain_votes = OnChainVotes::<T>::get();
assert!(onchain_votes.is_some());
let vote = onchain_votes.unwrap();
assert_eq!(vote.session, scenario._session);
let header = BenchBuilder::<T>::header(scenario._block_number);
for (para_id, backing_validators) in vote.backing_validators_per_candidate.iter().enumerate() {
let descriptor = backing_validators.0.descriptor();
assert_eq!(ParaId::from(para_id), descriptor.para_id());
assert_eq!(header.hash(), descriptor.relay_parent());
assert_eq!(backing_validators.1.len(), votes);
}
assert_eq!(
inclusion::PendingAvailability::<T>::iter().count(),
cores_with_backed.len()
);
}
enter_backed_candidate_code_upgrade {
let v = crate::configuration::ActiveConfig::<T>::get().max_code_size;
let cores_with_backed: BTreeMap<_, _>
= vec![(0, BenchBuilder::<T>::fallback_min_backing_votes())]
.into_iter()
.collect();
let scenario = BenchBuilder::<T>::new()
.set_backed_in_inherent_paras(cores_with_backed.clone())
.set_code_upgrade(v)
.build();
let mut benchmark = scenario.data.clone();
let votes = min(
scheduler::Pallet::<T>::group_validators(GroupIndex::from(0)).unwrap().len(),
BenchBuilder::<T>::fallback_min_backing_votes() as usize
);
assert_eq!(benchmark.backed_candidates.len(), 1);
assert_eq!(
benchmark.backed_candidates.get(0).unwrap().validity_votes().len(),
votes,
);
benchmark.bitfields.clear();
benchmark.disputes.clear();
crate::paras::benchmarking::generate_disordered_upgrades::<T>();
}: enter(RawOrigin::None, benchmark)
verify {
let max_validators_per_core = BenchBuilder::<T>::fallback_max_validators_per_core();
assert!(Included::<T>::get().is_some());
let onchain_votes = OnChainVotes::<T>::get();
assert!(onchain_votes.is_some());
let vote = onchain_votes.unwrap();
assert_eq!(vote.session, scenario._session);
let header = BenchBuilder::<T>::header(scenario._block_number);
for (para_id, backing_validators)
in vote.backing_validators_per_candidate.iter().enumerate() {
let descriptor = backing_validators.0.descriptor();
assert_eq!(ParaId::from(para_id), descriptor.para_id());
assert_eq!(header.hash(), descriptor.relay_parent());
assert_eq!(
backing_validators.1.len(),
votes,
);
}
assert_eq!(
inclusion::PendingAvailability::<T>::iter().count(),
cores_with_backed.len()
);
}
}
impl_benchmark_test_suite!(
Pallet,
crate::mock::new_test_ext(Default::default()),
crate::mock::Test
);