New sub-trait to mock staking miner (#11350)

* new separate config trait for staking miner

* fix some docs and stuff

* relax trait bounds

* some cleanup

* Update frame/election-provider-multi-phase/src/unsigned.rs

* add comment

* self review and fix build

* fix docs

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
This commit is contained in:
Kian Paimani
2022-05-11 18:45:59 +01:00
committed by GitHub
parent 479dc63af4
commit d06d20d65b
7 changed files with 479 additions and 465 deletions
@@ -39,15 +39,15 @@ fn solution_with_size<T: Config>(
size: SolutionOrSnapshotSize,
active_voters_count: u32,
desired_targets: u32,
) -> Result<RawSolution<SolutionOf<T>>, &'static str> {
) -> Result<RawSolution<SolutionOf<T::MinerConfig>>, &'static str> {
ensure!(size.targets >= desired_targets, "must have enough targets");
ensure!(
size.targets >= (<SolutionOf<T>>::LIMIT * 2) as u32,
size.targets >= (<SolutionOf<T::MinerConfig>>::LIMIT * 2) as u32,
"must have enough targets for unique votes."
);
ensure!(size.voters >= active_voters_count, "must have enough voters");
ensure!(
(<SolutionOf<T>>::LIMIT as u32) < desired_targets,
(<SolutionOf<T::MinerConfig>>::LIMIT as u32) < desired_targets,
"must have enough winners to give them votes."
);
@@ -74,10 +74,10 @@ fn solution_with_size<T: Config>(
// chose a random subset of winners.
let winner_votes: BoundedVec<_, _> = winners
.as_slice()
.choose_multiple(&mut rng, <SolutionOf<T>>::LIMIT)
.choose_multiple(&mut rng, <SolutionOf<T::MinerConfig>>::LIMIT)
.cloned()
.try_collect()
.expect("<SolutionOf<T>>::LIMIT is the correct bound; qed.");
.expect("<SolutionOf<T::MinerConfig>>::LIMIT is the correct bound; qed.");
let voter = frame_benchmarking::account::<T::AccountId>("Voter", i, SEED);
(voter, stake, winner_votes)
})
@@ -92,10 +92,10 @@ fn solution_with_size<T: Config>(
let rest_voters = (active_voters_count..size.voters)
.map(|i| {
let votes: BoundedVec<_, _> = (&non_winners)
.choose_multiple(&mut rng, <SolutionOf<T>>::LIMIT)
.choose_multiple(&mut rng, <SolutionOf<T::MinerConfig>>::LIMIT)
.cloned()
.try_collect()
.expect("<SolutionOf<T>>::LIMIT is the correct bound; qed.");
.expect("<SolutionOf<T::MinerConfig>>::LIMIT is the correct bound; qed.");
let voter = frame_benchmarking::account::<T::AccountId>("Voter", i, SEED);
(voter, stake, votes)
})
@@ -120,12 +120,12 @@ fn solution_with_size<T: Config>(
// down the road.
T::DataProvider::put_snapshot(all_voters.clone(), targets.clone(), Some(stake));
let cache = helpers::generate_voter_cache::<T>(&all_voters);
let stake_of = helpers::stake_of_fn::<T>(&all_voters, &cache);
let voter_index = helpers::voter_index_fn::<T>(&cache);
let target_index = helpers::target_index_fn::<T>(&targets);
let voter_at = helpers::voter_at_fn::<T>(&all_voters);
let target_at = helpers::target_at_fn::<T>(&targets);
let cache = helpers::generate_voter_cache::<T::MinerConfig>(&all_voters);
let stake_of = helpers::stake_of_fn::<T::MinerConfig>(&all_voters, &cache);
let voter_index = helpers::voter_index_fn::<T::MinerConfig>(&cache);
let target_index = helpers::target_index_fn::<T::MinerConfig>(&targets);
let voter_at = helpers::voter_at_fn::<T::MinerConfig>(&all_voters);
let target_at = helpers::target_at_fn::<T::MinerConfig>(&targets);
let assignments = active_voters
.iter()
@@ -143,7 +143,8 @@ fn solution_with_size<T: Config>(
.collect::<Vec<_>>();
let solution =
<SolutionOf<T>>::from_assignment(&assignments, &voter_index, &target_index).unwrap();
<SolutionOf<T::MinerConfig>>::from_assignment(&assignments, &voter_index, &target_index)
.unwrap();
let score = solution.clone().score(stake_of, voter_at, target_at).unwrap();
let round = <MultiPhase<T>>::round();
@@ -480,14 +481,14 @@ frame_benchmarking::benchmarks! {
let witness = SolutionOrSnapshotSize { voters: v, targets: t };
let RawSolution { solution, .. } = solution_with_size::<T>(witness, a, d)?;
let RoundSnapshot { voters, targets } = MultiPhase::<T>::snapshot().ok_or("snapshot missing")?;
let voter_at = helpers::voter_at_fn::<T>(&voters);
let target_at = helpers::target_at_fn::<T>(&targets);
let voter_at = helpers::voter_at_fn::<T::MinerConfig>(&voters);
let target_at = helpers::target_at_fn::<T::MinerConfig>(&targets);
let mut assignments = solution.into_assignment(voter_at, target_at).expect("solution generated by `solution_with_size` must be valid.");
// make a voter cache and some helper functions for access
let cache = helpers::generate_voter_cache::<T>(&voters);
let voter_index = helpers::voter_index_fn::<T>(&cache);
let target_index = helpers::target_index_fn::<T>(&targets);
let cache = helpers::generate_voter_cache::<T::MinerConfig>(&voters);
let voter_index = helpers::voter_index_fn::<T::MinerConfig>(&cache);
let target_index = helpers::target_index_fn::<T::MinerConfig>(&targets);
// sort assignments by decreasing voter stake
assignments.sort_by_key(|crate::unsigned::Assignment::<T> { who, .. }| {
@@ -504,21 +505,21 @@ frame_benchmarking::benchmarks! {
.collect::<Result<Vec<_>, _>>()
.unwrap();
let encoded_size_of = |assignments: &[IndexAssignmentOf<T>]| {
SolutionOf::<T>::try_from(assignments).map(|solution| solution.encoded_size())
let encoded_size_of = |assignments: &[IndexAssignmentOf<T::MinerConfig>]| {
SolutionOf::<T::MinerConfig>::try_from(assignments).map(|solution| solution.encoded_size())
};
let desired_size = Percent::from_percent(100 - f.saturated_into::<u8>())
.mul_ceil(encoded_size_of(index_assignments.as_slice()).unwrap());
log!(trace, "desired_size = {}", desired_size);
}: {
MultiPhase::<T>::trim_assignments_length(
crate::Miner::<T::MinerConfig>::trim_assignments_length(
desired_size.saturated_into(),
&mut index_assignments,
&encoded_size_of,
).unwrap();
} verify {
let solution = SolutionOf::<T>::try_from(index_assignments.as_slice()).unwrap();
let solution = SolutionOf::<T::MinerConfig>::try_from(index_assignments.as_slice()).unwrap();
let encoding = solution.encode();
log!(
trace,