Bring back the on_finalize weight of staking. (#8463)

* Bring back the on_finalize weighg of stakin.

* Better logs

* Also make a few things pub

* Fix build

* Add assertions

* Add test.

* remove dbg

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

* Update frame/staking/src/tests.rs

* Fix

* Fix

* Update frame/election-provider-multi-phase/src/unsigned.rs
This commit is contained in:
Kian Paimani
2021-03-29 11:19:40 +02:00
committed by GitHub
parent d0eee4f1cb
commit 7b2cf33b73
5 changed files with 33 additions and 13 deletions
@@ -381,11 +381,11 @@ impl Default for ElectionCompute {
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)] #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
pub struct RawSolution<C> { pub struct RawSolution<C> {
/// Compact election edges. /// Compact election edges.
compact: C, pub compact: C,
/// The _claimed_ score of the solution. /// The _claimed_ score of the solution.
score: ElectionScore, pub score: ElectionScore,
/// The round at which this solution should be submitted. /// The round at which this solution should be submitted.
round: u32, pub round: u32,
} }
impl<C: Default> Default for RawSolution<C> { impl<C: Default> Default for RawSolution<C> {
@@ -402,13 +402,13 @@ pub struct ReadySolution<A> {
/// ///
/// This is target-major vector, storing each winners, total backing, and each individual /// This is target-major vector, storing each winners, total backing, and each individual
/// backer. /// backer.
supports: Supports<A>, pub supports: Supports<A>,
/// The score of the solution. /// The score of the solution.
/// ///
/// This is needed to potentially challenge the solution. /// This is needed to potentially challenge the solution.
score: ElectionScore, pub score: ElectionScore,
/// How this election was computed. /// How this election was computed.
compute: ElectionCompute, pub compute: ElectionCompute,
} }
/// A snapshot of all the data that is needed for en entire round. They are provided by /// A snapshot of all the data that is needed for en entire round. They are provided by
@@ -432,10 +432,10 @@ pub struct RoundSnapshot<A> {
pub struct SolutionOrSnapshotSize { pub struct SolutionOrSnapshotSize {
/// The length of voters. /// The length of voters.
#[codec(compact)] #[codec(compact)]
voters: u32, pub voters: u32,
/// The length of targets. /// The length of targets.
#[codec(compact)] #[codec(compact)]
targets: u32, pub targets: u32,
} }
/// Internal errors of the pallet. /// Internal errors of the pallet.
@@ -168,12 +168,16 @@ impl<T: Config> Pallet<T> {
size, size,
T::MinerMaxWeight::get(), T::MinerMaxWeight::get(),
); );
log!( log!(
debug, debug,
"miner: current compact solution voters = {}, maximum_allowed = {}", "initial solution voters = {}, snapshot = {:?}, maximum_allowed(capped) = {}",
compact.voter_count(), compact.voter_count(),
size,
maximum_allowed_voters, maximum_allowed_voters,
); );
// trim weight.
let compact = Self::trim_compact(maximum_allowed_voters, compact, &voter_index)?; let compact = Self::trim_compact(maximum_allowed_voters, compact, &voter_index)?;
// re-calc score. // re-calc score.
@@ -252,10 +256,12 @@ impl<T: Config> Pallet<T> {
} }
} }
log!(debug, "removed {} voter to meet the max weight limit.", to_remove);
Ok(compact) Ok(compact)
} }
_ => { _ => {
// nada, return as-is // nada, return as-is
log!(debug, "didn't remove any voter for weight limits.");
Ok(compact) Ok(compact)
} }
} }
@@ -298,6 +304,7 @@ impl<T: Config> Pallet<T> {
// First binary-search the right amount of voters // First binary-search the right amount of voters
let mut step = voters / 2; let mut step = voters / 2;
let mut current_weight = weight_with(voters); let mut current_weight = weight_with(voters);
while step > 0 { while step > 0 {
match next_voters(current_weight, voters, step) { match next_voters(current_weight, voters, step) {
// proceed with the binary search // proceed with the binary search
@@ -324,13 +331,14 @@ impl<T: Config> Pallet<T> {
voters -= 1; voters -= 1;
} }
let final_decision = voters.min(size.voters);
debug_assert!( debug_assert!(
weight_with(voters.min(size.voters)) <= max_weight, weight_with(final_decision) <= max_weight,
"weight_with({}) <= {}", "weight_with({}) <= {}",
voters.min(size.voters), final_decision,
max_weight, max_weight,
); );
voters.min(size.voters) final_decision
} }
/// Checks if an execution of the offchain worker is permitted at the given block number, or /// Checks if an execution of the offchain worker is permitted at the given block number, or
@@ -1308,7 +1308,6 @@ mod tests {
} }
fn has_lock(who: &u64) -> u64 { fn has_lock(who: &u64) -> u64 {
dbg!(Balances::locks(who));
Balances::locks(who) Balances::locks(who)
.get(0) .get(0)
.cloned() .cloned()
+5
View File
@@ -1205,6 +1205,11 @@ decl_module! {
} }
} }
fn on_initialize(_now: T::BlockNumber) -> Weight {
// just return the weight of the on_finalize.
T::DbWeight::get().reads(1)
}
fn on_finalize() { fn on_finalize() {
// Set the start of the first era. // Set the start of the first era.
if let Some(mut active_era) = Self::active_era() { if let Some(mut active_era) = Self::active_era() {
+8
View File
@@ -3827,6 +3827,14 @@ fn do_not_die_when_active_is_ed() {
}) })
} }
#[test]
fn on_finalize_weight_is_nonzero() {
ExtBuilder::default().build_and_execute(|| {
let on_finalize_weight = <Test as frame_system::Config>::DbWeight::get().reads(1);
assert!(Staking::on_initialize(1) >= on_finalize_weight);
})
}
mod election_data_provider { mod election_data_provider {
use super::*; use super::*;
use frame_election_provider_support::ElectionDataProvider; use frame_election_provider_support::ElectionDataProvider;