Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
25bddc9
mockup
kianenigma Sep 11, 2020
7bef52f
Add files
kianenigma Sep 11, 2020
4f3dfa4
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Sep 23, 2020
bc30ce1
More mockup
kianenigma Sep 24, 2020
9e52fbc
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Oct 1, 2020
14ebc95
Lots more changes and tests. Initial design of signed is forseable now.
kianenigma Oct 2, 2020
0587fc9
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Oct 8, 2020
233d4e1
More changes for the signed phase, now going to the cluster-fu*k of g…
kianenigma Oct 9, 2020
9c880f3
Finally decoupled solution from the pallet.. such a PITA.
kianenigma Oct 9, 2020
0cb35eb
Much simplifications and cleanups.
kianenigma Oct 9, 2020
c1ff3ec
A skeleton for the unsigned version.
kianenigma Oct 9, 2020
85a346e
Master.into()
kianenigma Oct 13, 2020
6a2542d
Staking builds and tests compile, some tests fail
kianenigma Oct 13, 2020
c361911
All staking tests work
kianenigma Oct 14, 2020
218db10
Unsigned almost done, primitive crate added.
kianenigma Oct 15, 2020
5e21a79
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Oct 15, 2020
25c1bc3
Use decl_error
kianenigma Oct 15, 2020
d53785c
Add basic benchmark setup, needs more work
kianenigma Oct 16, 2020
525f6e0
Comment.
kianenigma Oct 16, 2020
3026316
Checkpoint.
kianenigma Oct 20, 2020
9841e3b
node-runtime builds.
kianenigma Oct 20, 2020
16c0550
Builds and all good?
kianenigma Dec 3, 2020
f197b6b
Undo some formatting mistakes
kianenigma Dec 3, 2020
5005281
Working on benchmarks'
kianenigma Dec 7, 2020
bd0d331
Fix bench
kianenigma Dec 8, 2020
67dd4d1
Strange merge -- everything builds.
kianenigma Dec 8, 2020
eac4571
Everything compiles and good -- need to fix benches.
kianenigma Dec 8, 2020
a7c4841
Some Staking stuff left to do.
kianenigma Dec 9, 2020
2ca3dcf
Master.into()
kianenigma Dec 14, 2020
4922897
Split the snapshot; type for CompactSolution and MaxNominations
kianenigma Dec 15, 2020
ef593a7
Fix a bunch more issues.
kianenigma Dec 15, 2020
8661814
check new design
kianenigma Dec 18, 2020
57f7294
Everything within the module works and compiles.
kianenigma Dec 21, 2020
af304d1
Stkaing also builds
kianenigma Dec 21, 2020
80e7450
Everything builds with the new scheme.
kianenigma Dec 25, 2020
4b4712a
Master.into()
kianenigma Dec 26, 2020
206933a
Simplify elect API.
kianenigma Dec 26, 2020
af9b97d
Fix all other tests.
kianenigma Dec 26, 2020
9663fea
make the staking api better and well tested.
kianenigma Jan 4, 2021
7f4f5b7
Almost done with conversion
kianenigma Jan 4, 2021
8fb11f1
Move to pallet V2 + construct_runtime
kianenigma Jan 5, 2021
bd6d4d9
Master.into()
kianenigma Jan 5, 2021
65f5ac1
Revert staking changes;
kianenigma Jan 5, 2021
3860885
Revert Staking fuzzer.
kianenigma Jan 5, 2021
392f35a
Fix fuzzers
kianenigma Jan 5, 2021
1337e1b
Update frame/election-providers/src/two_phase/mod.rs
kianenigma Jan 6, 2021
4f6ec0d
a round of self review
kianenigma Jan 6, 2021
b3113f7
A large number of bugfixes after local test and self-review
kianenigma Jan 7, 2021
abca039
Upstream.into()
kianenigma Jan 7, 2021
6bc578a
Master.into()
kianenigma Jan 7, 2021
876f666
Apply suggestions from code review
kianenigma Jan 7, 2021
81df1df
Update frame/election-providers/src/two_phase/benchmarking.rs
kianenigma Jan 7, 2021
e3862ce
Fix some review grumbles.
kianenigma Jan 8, 2021
ca06f8e
Fix some docs
kianenigma Jan 8, 2021
6206c32
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 8, 2021
95d7a3c
Master.into()
kianenigma Jan 8, 2021
63776ff
Maybe relocalte the code?
kianenigma Jan 8, 2021
4732854
Master.into()
kianenigma Jan 8, 2021
a922f0b
Witness -> SoltionSize
kianenigma Jan 8, 2021
6d8247a
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 8, 2021
e9f4437
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 8, 2021
02069e1
Update frame/session/src/lib.rs
kianenigma Jan 12, 2021
000ecdc
Revamp macros
kianenigma Jan 12, 2021
cb6f156
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 12, 2021
df04ef3
Merge remote-tracking branch 'origin/master' into kiz-election-provider
Jan 12, 2021
ee2a316
Master.into()
kianenigma Jan 12, 2021
0d92ed5
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 12, 2021
88a96a0
Fix build
kianenigma Jan 12, 2021
6480ce1
Merge remote-tracking branch 'origin/master' into kiz-election-provider
Jan 12, 2021
8a68915
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 12, 2021
f4e2c03
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Jan 12, 2021
c41ecb0
fix all the tests.
kianenigma Jan 13, 2021
b42336a
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 13, 2021
f5e23f9
Fix the strange issue with cargo lock
kianenigma Jan 13, 2021
56bb93c
revamp closures.
kianenigma Jan 14, 2021
257795c
Update frame/two-phase-election-provider/src/benchmarking.rs
kianenigma Jan 14, 2021
9c114bc
Merge remote-tracking branch 'origin/master' into kiz-election-provider
Jan 14, 2021
1ac97f5
Some review grumbles.
kianenigma Jan 14, 2021
366b15b
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Jan 14, 2021
afa5507
Upstream.into()
kianenigma Jan 14, 2021
5d206ab
Fix build
kianenigma Jan 14, 2021
ea193c7
Bring back benches
kianenigma Jan 14, 2021
60e57d7
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 14, 2021
77f53a3
Add a bunch of more fixes: signed weight check, tests etc.
kianenigma Jan 15, 2021
9ce9d6b
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 15, 2021
b30dfb3
Fix build
kianenigma Jan 15, 2021
6001d7d
Master.into()
kianenigma Jan 15, 2021
a066e4a
Last small tweaks
kianenigma Jan 15, 2021
10f6e7b
Fix all tests.
kianenigma Jan 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Working on benchmarks'
  • Loading branch information
kianenigma committed Dec 7, 2020
commit 50052817f8e8d16db39615ead9fdfcd178529054
3,047 changes: 1,533 additions & 1,514 deletions frame/election-providers/expanded.rs

Large diffs are not rendered by default.

228 changes: 103 additions & 125 deletions frame/election-providers/src/two_phase/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,145 +24,141 @@ pub use frame_benchmarking::{account, benchmarks, whitelist_account, whitelisted
use frame_support::assert_ok;
use rand::{seq::SliceRandom, thread_rng};
use sp_npos_elections::{ExtendedBalance, VoteWeight};
use sp_runtime::{InnerOf, PerU16};
use sp_runtime::InnerOf;
use std::convert::TryInto;

const SEED: u32 = 0;

// TODO: the entire snapshot can probably live in a single place.. we most often Read and write it
// at the same time.

/// Generate mock on-chain snapshots.
/// Creates a **valid** solution with exactly the given size.
///
/// This emulates the start of signed phase, where snapshots are received from an upstream crate.
fn mock_snapshot<T: Config>(
/// The snapshot is also created internally.
///
/// The snapshot size must be bigger, otherwise this will panic.
fn solution_with_size<T: Config>(
witness: WitnessData,
) -> (
Vec<T::AccountId>,
Vec<(T::AccountId, VoteWeight, Vec<T::AccountId>)>,
)
active_voters_count: u32,
winners_count: u32,
) -> RawSolution<CompactOf<T>>
where
ExtendedBalance: From<InnerOf<CompactAccuracyOf<T>>>,
<InnerOf<CompactAccuracyOf<T>> as std::convert::TryFrom<usize>>::Error: std::fmt::Debug,
{
assert!(witness.targets >= winners_count, "must have enough targets");
assert!(
witness.voters >= active_voters_count,
"must have enough voters"
);

let stake: u64 = 1000_000;
// first generates random targets.
let targets: Vec<T::AccountId> = (0..witness.targets)
.map(|i| account("Targets", i, SEED))
.collect();

// generate voters, each voting for a random subset of the targets.
let mut voters = (0..(witness.voters - witness.targets))
let mut rng = thread_rng();
// decide who are the winners.
let winners = targets
.as_slice()
.choose_multiple(&mut rng, winners_count as usize)
.cloned()
.collect::<Vec<_>>();

// generate first active voters who must vote for a subset of winners.
// TODO: this could lead to an underestimate, the active voters should only vote for winners to
// maximize all the iterations.
let active_voters = (0..active_voters_count)
.map(|i| {
let mut rng = thread_rng();
let to_vote = rand::random::<usize>() % <CompactOf<T>>::LIMIT + 1;
let votes = targets.as_slice().choose_multiple(&mut rng, to_vote).cloned().collect::<Vec<_>>();
// chose a random number of votes to give to the winners, and whatever is left is given
// to everyone.
let votes_to_winners = rand::random::<usize>() % <CompactOf<T>>::LIMIT + 1;
let votes_to_everyone = <CompactOf<T>>::LIMIT - votes_to_winners;

let winner_votes = winners
.as_slice()
.choose_multiple(&mut rng, votes_to_winners)
.cloned();
let rest_votes = targets
.as_slice()
.choose_multiple(&mut rng, votes_to_everyone as usize)
.cloned();

let votes = winner_votes.chain(rest_votes).collect::<Vec<_>>();
let voter = account::<T::AccountId>("Voter", i, SEED);
(voter, stake, votes)
})
.collect::<Vec<_>>();

// targets should have self vote. This is very helpful, because it ensure that by doing the trim,
// we almost never reduce the number of unique targets. For this cause, we also make the self
// vote heavier, to ensure that trimming only removes a voter here and there, not a target.
voters.extend(targets.iter().map(|t| (t.clone(), stake, vec![t.clone()])));
// rest of the voters. They can only vote for non-winners.
let non_winners = targets
.iter()
.filter(|t| !winners.contains(t))
.cloned()
.collect::<Vec<T::AccountId>>();
let rest_voters = (active_voters_count..witness.voters)
.map(|i| {
let votes = (&non_winners)
.choose_multiple(&mut rng, <CompactOf<T>>::LIMIT)
.cloned()
.collect::<Vec<T::AccountId>>();
let voter = account::<T::AccountId>("Voter", i, SEED);
(voter, stake, votes)
})
.collect::<Vec<_>>();

(targets, voters)
}
dbg!(active_voters.len(), rest_voters.len(), winners.len());
// active_voters.extend(rest_voters);
let mut all_voters = active_voters.clone();
all_voters.extend(rest_voters);

fn put_mock_snapshot<T: Config>(witness: WitnessData, desired_targets: u32)
where
ExtendedBalance: From<InnerOf<CompactAccuracyOf<T>>>,
{
let (targets, voters) = mock_snapshot::<T>(witness);
<SnapshotTargets<T>>::put(targets);
<SnapshotVoters<T>>::put(voters);
DesiredTargets::put(desired_targets);
}
assert_eq!(active_voters.len() as u32, active_voters_count);
assert_eq!(all_voters.len() as u32, witness.voters);
assert_eq!(winners.len() as u32, winners_count);

/// Creates a **valid** solution with exactly the given size.
///
/// The snapshot size must be bigger, otherwise this will panic.
fn solution_with_size<T: Config>(active_voters: u32, winners_count: u32) -> RawSolution<CompactOf<T>>
where
ExtendedBalance: From<InnerOf<CompactAccuracyOf<T>>>,
<InnerOf<CompactAccuracyOf<T>> as std::convert::TryFrom<usize>>::Error: std::fmt::Debug,
{
let voters = <TwoPhase<T>>::snapshot_voters().unwrap();
let targets = <TwoPhase<T>>::snapshot_targets().unwrap();
let voters = active_voters;

// else we cannot support this, self vote is a must.
assert!(active_voters >= winners_count);
// else we won't have enough voters.
assert!(voters.len() >= active_voters as usize);
// else we won't have enough winners
assert!(targets.len() >= winners_count as usize);
<Snapshot<T>>::put(RoundSnapshot {
desired_targets: winners_count,
voters: all_voters,
targets: targets.clone(),
});

let voter_index = crate::voter_index_fn!(voters, T::AccountId, T);
let voter_at = crate::voter_at_fn!(voters, T::AccountId, T);
let target_at = crate::target_at_fn!(targets, T::AccountId, T);
let target_index = crate::target_index_fn!(targets, T::AccountId, T);
let stake_of = crate::stake_of_fn!(voters, T::AccountId);

// First chose random winners.
let mut rng = rand::thread_rng();
let winners = targets
.as_slice()
.choose_multiple(&mut rng, winners_count as usize)
.cloned()
.collect::<Vec<_>>();

let mut assignments = winners
let assignments = voters
.iter()
.map(|w| Assignment {
who: w.clone(),
distribution: vec![(w.clone(), <CompactAccuracyOf<T>>::one())],
})
.collect::<Vec<_>>();

// all external voters who are not self vote.
let mut voters_pool = voters
.iter()
.filter(|(x, _, z)| *x != z[0])
.cloned()
.collect::<Vec<_>>();

// add from `voters_pool` to `assignments` until we have enough.
while assignments.len() < active_voters as usize {
// pop one of the voters.
let (who, _, votes) = voters_pool.remove(rand::random::<usize>() % voters_pool.len());
// see if it votes for any of the winners.
let winner_intersection = votes
.iter()
.filter(|v| winners.contains(v))
.cloned()
.collect::<Vec<_>>();

// if any, add assignment to all of them.
if winner_intersection.len() > 0 {
assignments.push(Assignment {
who,
distribution: winner_intersection
.map(|(voter, _stake, votes)| {
let percent_per_edge: InnerOf<CompactAccuracyOf<T>> =
(100 / votes.len()).try_into().unwrap();
Assignment {
who: voter.clone(),
distribution: votes
.iter()
.map(|w| {
let percent: InnerOf<CompactAccuracyOf<T>> = (100 / winner_intersection.len()).try_into().unwrap();
(
w.clone(),
<CompactAccuracyOf<T>>::from_percent(percent),
)
})
.map(|t| (t.clone(), <CompactAccuracyOf<T>>::from_percent(percent_per_edge)))
.collect::<Vec<_>>(),
})
}
}
}
})
.collect::<Vec<_>>();

let compact = <CompactOf<T>>::from_assignment(assignments, &voter_index, &target_index).unwrap();
let score = compact.clone().score(winners, stake_of, voter_at, target_at).unwrap();
let compact =
<CompactOf<T>>::from_assignment(assignments, &voter_index, &target_index).unwrap();
let score = compact
.clone()
.score(&winners, stake_of, voter_at, target_at)
.unwrap();

RawSolution { compact, score }
}

benchmarks! {
where_clause { where ExtendedBalance: From<InnerOf<CompactAccuracyOf<T>>>, }
where_clause {
where ExtendedBalance: From<InnerOf<CompactAccuracyOf<T>>>,
// TODO: do I really need this?
<InnerOf<CompactAccuracyOf<T>> as std::convert::TryFrom<usize>>::Error: std::fmt::Debug,
}
_{}

submit_signed {}: {} verify {}
Expand All @@ -172,42 +168,24 @@ benchmarks! {

// This is checking a valid solution. The worse case is indeed a valid solution.
feasibility_check {
// number of votes in snapshot.
// number of voters in snapshot.
let v in 200 .. 300;
// number of targets in snapshot.
let t in 50 .. 80;
// number of assignments, i.e. compact.len(). This means the active nominators, thus must be
// a subset of `v` component.
let a in 20 .. 80;
let t in 80 .. 140;
// number of assignments, i.e. compact.voters_count(). This means the active nominators,
// thus must be a subset of `v` component.
let a in 80 .. 140;
// number of desired targets. Must be a subset of `t` component.
let d in 20 .. 40;
let d in 30 .. 60;

println!("running v {}, t {}, a {}, d {}", v, t, a, d);

let witness = WitnessData { voters: v, targets: t };
put_mock_snapshot::<T>(witness, d);

let voters = <TwoPhase<T>>::snapshot_voters().unwrap();
let targets = <TwoPhase<T>>::snapshot_targets().unwrap();

let voter_index = crate::voter_index_fn!(voters, T::AccountId, T);
let voter_at = crate::voter_at_fn!(voters, T::AccountId, T);
let target_at = crate::target_at_fn!(targets, T::AccountId, T);
let stake_of = crate::stake_of_fn!(voters, T::AccountId);

// the score at this point is not usable -- It might change when we resize the compact.
let RawSolution { compact, score: _ } = <TwoPhase<T>>::mine_solution(0).unwrap();
let compact = <TwoPhase<T>>::trim_compact(a, compact, voter_index).unwrap();
let raw_solution = solution_with_size::<T>(witness, a, d);

assert_eq!(compact.len() as u32, a);
assert_eq!(compact.unique_targets().len() as u32, d);
assert_eq!(raw_solution.compact.voters_count() as u32, a);
assert_eq!(raw_solution.compact.unique_targets().len() as u32, d);

// re-calc score.
let winners = compact.unique_targets().iter().map(|i| target_at(*i).unwrap()).collect::<Vec<_>>();
let score = compact
.clone()
.score(&winners, stake_of, voter_at, target_at).unwrap();
let raw_solution = RawSolution { compact, score };
let compute = ElectionCompute::Unsigned;
}: {
assert_ok!(<TwoPhase<T>>::feasibility_check(raw_solution, compute));
Expand Down
12 changes: 7 additions & 5 deletions frame/election-providers/src/two_phase/mock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::*;
use frame_support::{parameter_types, traits::OnInitialize};
use parking_lot::RwLock;
use rand::seq::SliceRandom;
use sp_core::{
offchain::{
testing::{PoolState, TestOffchainExt, TestTransactionPoolExt},
Expand All @@ -12,7 +11,7 @@ use sp_core::{
use sp_election_providers::ElectionDataProvider;
use sp_npos_elections::{
assignment_ratio_to_staked_normalized, seq_phragmen, to_supports, to_without_backing,
Assignment, CompactSolution, ElectionResult, EvaluateSupport,
CompactSolution, ElectionResult, EvaluateSupport,
};
use sp_runtime::{
testing::Header,
Expand Down Expand Up @@ -54,9 +53,12 @@ pub fn balances(who: &AccountId) -> (Balance, Balance) {
///
/// This is a good example of what an offchain miner would do.
pub fn raw_solution() -> RawSolution<CompactOf<Runtime>> {
let voters = TwoPhase::snapshot_voters().unwrap();
let targets = TwoPhase::snapshot_targets().unwrap();
let desired = TwoPhase::desired_targets() as usize;
let RoundSnapshot {
voters,
targets,
desired_targets,
} = TwoPhase::snapshot().unwrap();
let desired = desired_targets as usize;

// closures
let voter_index = crate::voter_index_fn!(voters, AccountId, Runtime);
Expand Down
Loading