Enable Offchain Equalise (#5683)

* Master.into()

* Remove debug stuff

* Better license

* Migrate away from SimpleDispatchInfo

* Fix test

* Revert "Migrate away from SimpleDispatchInfo"

This reverts commit dbdd27fa19948f16bd17defdc01d3dd32986df11.

* Move to offchain randomness

* Fix tests

* Fix tests more
This commit is contained in:
Kian Paimani
2020-04-27 18:51:46 +02:00
committed by GitHub
parent 33d00692d8
commit ee54eff488
20 changed files with 635 additions and 373 deletions
@@ -16,17 +16,18 @@
//! Helpers for offchain worker election.
use codec::Decode;
use crate::{
Call, CompactAssignments, Module, NominatorIndex, OffchainAccuracy, Trait, ValidatorIndex,
};
use frame_system::offchain::SubmitTransaction;
use sp_phragmen::{
build_support_map, evaluate_support, reduce, Assignment, ExtendedBalance, PhragmenResult,
PhragmenScore,
PhragmenScore, equalize,
};
use sp_runtime::offchain::storage::StorageValueRef;
use sp_runtime::PerThing;
use sp_runtime::RuntimeDebug;
use sp_runtime::{PerThing, RuntimeDebug, traits::{TrailingZeroInput, Zero}};
use frame_support::{debug, traits::Get};
use sp_std::{convert::TryInto, prelude::*};
/// Error types related to the offchain election machinery.
@@ -159,10 +160,7 @@ pub fn prepare_submission<T: Trait>(
};
// Clean winners.
let winners = winners
.into_iter()
.map(|(w, _)| w)
.collect::<Vec<T::AccountId>>();
let winners = sp_phragmen::to_without_backing(winners);
// convert into absolute value and to obtain the reduced version.
let mut staked = sp_phragmen::assignment_ratio_to_staked(
@@ -170,10 +168,33 @@ pub fn prepare_submission<T: Trait>(
<Module<T>>::slashable_balance_of_vote_weight,
);
// reduce
if do_reduce {
reduce(&mut staked);
}
let (mut support_map, _) = build_support_map::<T::AccountId>(&winners, &staked);
// equalize a random number of times.
let iterations_executed = match T::MaxIterations::get() {
0 => {
// Don't run equalize at all
0
}
iterations @ _ => {
let seed = sp_io::offchain::random_seed();
let iterations = <u32>::decode(&mut TrailingZeroInput::new(seed.as_ref()))
.expect("input is padded with zeroes; qed") % iterations.saturating_add(1);
equalize(
&mut staked,
&mut support_map,
Zero::zero(),
iterations as usize,
)
}
};
// Convert back to ratio assignment. This takes less space.
let low_accuracy_assignment = sp_phragmen::assignment_staked_to_ratio(staked);
@@ -215,5 +236,12 @@ pub fn prepare_submission<T: Trait>(
}
}
debug::native::debug!(
target: "staking",
"prepared solution after {} equalization iterations with score {:?}",
iterations_executed,
score,
);
Ok((winners_indexed, compact, score))
}