Fix offchain election to respect the weight (#7215)

* Mockup

* Fix offchain election to respect the weight

* Fix builds a bit

* Update frame/staking/src/offchain_election.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update frame/staking/src/offchain_election.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Make it build, binary search

* Fix a number of grumbles

* one more fix.

* remove unwrap.

* better alg.

* Better alg again.

* Final fixes

* Fix

* Rollback to normal

* Final touches.

* Better tests.

* Update frame/staking/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Proper maxExtWeight

* Final fix

* Final fix for the find_voter

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Kian Paimani
2020-10-02 15:45:17 +02:00
committed by GitHub
parent a772eb27a2
commit 4d036e0053
16 changed files with 585 additions and 82 deletions
+33 -8
View File
@@ -277,7 +277,12 @@ pub fn get_weak_solution<T: Trait>(
/// worker code.
pub fn get_seq_phragmen_solution<T: Trait>(
do_reduce: bool,
) -> (Vec<ValidatorIndex>, CompactAssignments, ElectionScore, ElectionSize) {
) -> (
Vec<ValidatorIndex>,
CompactAssignments,
ElectionScore,
ElectionSize,
) {
let iters = offchain_election::get_balancing_iters::<T>();
let sp_npos_elections::ElectionResult {
@@ -285,22 +290,42 @@ pub fn get_seq_phragmen_solution<T: Trait>(
assignments,
} = <Module<T>>::do_phragmen::<OffchainAccuracy>(iters).unwrap();
offchain_election::prepare_submission::<T>(assignments, winners, do_reduce).unwrap()
offchain_election::prepare_submission::<T>(
assignments,
winners,
do_reduce,
T::MaximumBlockWeight::get(),
)
.unwrap()
}
/// Returns a solution in which only one winner is elected with just a self vote.
pub fn get_single_winner_solution<T: Trait>(
winner: T::AccountId
) -> Result<(Vec<ValidatorIndex>, CompactAssignments, ElectionScore, ElectionSize), &'static str> {
winner: T::AccountId,
) -> Result<
(
Vec<ValidatorIndex>,
CompactAssignments,
ElectionScore,
ElectionSize,
),
&'static str,
> {
let snapshot_validators = <Module<T>>::snapshot_validators().unwrap();
let snapshot_nominators = <Module<T>>::snapshot_nominators().unwrap();
let val_index = snapshot_validators.iter().position(|x| *x == winner).ok_or("not a validator")?;
let nom_index = snapshot_nominators.iter().position(|x| *x == winner).ok_or("not a nominator")?;
let val_index = snapshot_validators
.iter()
.position(|x| *x == winner)
.ok_or("not a validator")?;
let nom_index = snapshot_nominators
.iter()
.position(|x| *x == winner)
.ok_or("not a nominator")?;
let stake = <Staking<T>>::slashable_balance_of(&winner);
let stake = <T::CurrencyToVote as Convert<BalanceOf<T>, VoteWeight>>::convert(stake)
as ExtendedBalance;
let stake =
<T::CurrencyToVote as Convert<BalanceOf<T>, VoteWeight>>::convert(stake) as ExtendedBalance;
let val_index = val_index as ValidatorIndex;
let nom_index = nom_index as NominatorIndex;