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
+16 -8
View File
@@ -695,7 +695,7 @@ pub enum ElectionStatus<BlockNumber> {
/// Note that these values must reflect the __total__ number, not only those that are present in the
/// solution. In short, these should be the same size as the size of the values dumped in
/// `SnapshotValidators` and `SnapshotNominators`.
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, Default)]
#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, RuntimeDebug, Default)]
pub struct ElectionSize {
/// Number of validators in the snapshot of the current election round.
#[codec(compact)]
@@ -883,6 +883,13 @@ pub trait Trait: frame_system::Trait + SendTransactionTypes<Call<Self>> {
/// multiple pallets send unsigned transactions.
type UnsignedPriority: Get<TransactionPriority>;
/// Maximum weight that the unsigned transaction can have.
///
/// Chose this value with care. On one hand, it should be as high as possible, so the solution
/// can contain as many nominators/validators as possible. On the other hand, it should be small
/// enough to fit in the block.
type OffchainSolutionWeightLimit: Get<Weight>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
@@ -1294,6 +1301,7 @@ decl_module! {
consumed_weight += T::DbWeight::get().reads_writes(reads, writes);
consumed_weight += weight;
};
if
// if we don't have any ongoing offchain compute.
Self::era_election_status().is_closed() &&
@@ -1339,12 +1347,12 @@ decl_module! {
if Self::era_election_status().is_open_at(now) {
let offchain_status = set_check_offchain_execution_status::<T>(now);
if let Err(why) = offchain_status {
log!(debug, "skipping offchain worker in open election window due to [{}]", why);
log!(warn, "💸 skipping offchain worker in open election window due to [{}]", why);
} else {
if let Err(e) = compute_offchain_election::<T>() {
log!(error, "💸 Error in election offchain worker: {:?}", e);
} else {
log!(debug, "Executed offchain worker thread without errors.");
log!(debug, "💸 Executed offchain worker thread without errors.");
}
}
}
@@ -2141,7 +2149,7 @@ decl_module! {
/// transaction in the block.
///
/// # <weight>
/// See `crate::weight` module.
/// See [`submit_election_solution`].
/// # </weight>
#[weight = T::WeightInfo::submit_solution_better(
size.validators.into(),
@@ -2171,6 +2179,7 @@ decl_module! {
effectively depriving the validators from their authoring reward. Hence, this panic
is expected."
);
Ok(adjustments)
}
}
@@ -3082,7 +3091,6 @@ impl<T: Trait> Module<T> {
pub fn set_slash_reward_fraction(fraction: Perbill) {
SlashRewardFraction::put(fraction);
}
}
/// In this implementation `new_session(session)` must be called before `end_session(session-1)`
@@ -3367,13 +3375,13 @@ impl<T: Trait> frame_support::unsigned::ValidateUnsigned for Module<T> {
let invalid = to_invalid(error_with_post_info);
log!(
debug,
"validate unsigned pre dispatch checks failed due to error #{:?}.",
"💸 validate unsigned pre dispatch checks failed due to error #{:?}.",
invalid,
);
return invalid .into();
return invalid.into();
}
log!(debug, "validateUnsigned succeeded for a solution at era {}.", era);
log!(debug, "💸 validateUnsigned succeeded for a solution at era {}.", era);
ValidTransaction::with_tag_prefix("StakingOffchain")
// The higher the score[0], the better a solution is.