Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 13fd30f

Browse files
kianenigmashawntabrizi
authored andcommitted
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
1 parent 898d78a commit 13fd30f

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

frame/election-provider-multi-phase/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,11 @@ impl Default for ElectionCompute {
381381
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
382382
pub struct RawSolution<C> {
383383
/// Compact election edges.
384-
compact: C,
384+
pub compact: C,
385385
/// The _claimed_ score of the solution.
386-
score: ElectionScore,
386+
pub score: ElectionScore,
387387
/// The round at which this solution should be submitted.
388-
round: u32,
388+
pub round: u32,
389389
}
390390

391391
impl<C: Default> Default for RawSolution<C> {
@@ -402,13 +402,13 @@ pub struct ReadySolution<A> {
402402
///
403403
/// This is target-major vector, storing each winners, total backing, and each individual
404404
/// backer.
405-
supports: Supports<A>,
405+
pub supports: Supports<A>,
406406
/// The score of the solution.
407407
///
408408
/// This is needed to potentially challenge the solution.
409-
score: ElectionScore,
409+
pub score: ElectionScore,
410410
/// How this election was computed.
411-
compute: ElectionCompute,
411+
pub compute: ElectionCompute,
412412
}
413413

414414
/// 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> {
432432
pub struct SolutionOrSnapshotSize {
433433
/// The length of voters.
434434
#[codec(compact)]
435-
voters: u32,
435+
pub voters: u32,
436436
/// The length of targets.
437437
#[codec(compact)]
438-
targets: u32,
438+
pub targets: u32,
439439
}
440440

441441
/// Internal errors of the pallet.

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,16 @@ impl<T: Config> Pallet<T> {
168168
size,
169169
T::MinerMaxWeight::get(),
170170
);
171+
171172
log!(
172173
debug,
173-
"miner: current compact solution voters = {}, maximum_allowed = {}",
174+
"initial solution voters = {}, snapshot = {:?}, maximum_allowed(capped) = {}",
174175
compact.voter_count(),
176+
size,
175177
maximum_allowed_voters,
176178
);
179+
180+
// trim weight.
177181
let compact = Self::trim_compact(maximum_allowed_voters, compact, &voter_index)?;
178182

179183
// re-calc score.
@@ -252,10 +256,12 @@ impl<T: Config> Pallet<T> {
252256
}
253257
}
254258

259+
log!(debug, "removed {} voter to meet the max weight limit.", to_remove);
255260
Ok(compact)
256261
}
257262
_ => {
258263
// nada, return as-is
264+
log!(debug, "didn't remove any voter for weight limits.");
259265
Ok(compact)
260266
}
261267
}
@@ -298,6 +304,7 @@ impl<T: Config> Pallet<T> {
298304
// First binary-search the right amount of voters
299305
let mut step = voters / 2;
300306
let mut current_weight = weight_with(voters);
307+
301308
while step > 0 {
302309
match next_voters(current_weight, voters, step) {
303310
// proceed with the binary search
@@ -324,13 +331,14 @@ impl<T: Config> Pallet<T> {
324331
voters -= 1;
325332
}
326333

334+
let final_decision = voters.min(size.voters);
327335
debug_assert!(
328-
weight_with(voters.min(size.voters)) <= max_weight,
336+
weight_with(final_decision) <= max_weight,
329337
"weight_with({}) <= {}",
330-
voters.min(size.voters),
338+
final_decision,
331339
max_weight,
332340
);
333-
voters.min(size.voters)
341+
final_decision
334342
}
335343

336344
/// Checks if an execution of the offchain worker is permitted at the given block number, or

frame/elections-phragmen/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,6 @@ mod tests {
13081308
}
13091309

13101310
fn has_lock(who: &u64) -> u64 {
1311-
dbg!(Balances::locks(who));
13121311
Balances::locks(who)
13131312
.get(0)
13141313
.cloned()

frame/staking/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,11 @@ decl_module! {
12051205
}
12061206
}
12071207

1208+
fn on_initialize(_now: T::BlockNumber) -> Weight {
1209+
// just return the weight of the on_finalize.
1210+
T::DbWeight::get().reads(1)
1211+
}
1212+
12081213
fn on_finalize() {
12091214
// Set the start of the first era.
12101215
if let Some(mut active_era) = Self::active_era() {

frame/staking/src/tests.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3827,6 +3827,14 @@ fn do_not_die_when_active_is_ed() {
38273827
})
38283828
}
38293829

3830+
#[test]
3831+
fn on_finalize_weight_is_nonzero() {
3832+
ExtBuilder::default().build_and_execute(|| {
3833+
let on_finalize_weight = <Test as frame_system::Config>::DbWeight::get().reads(1);
3834+
assert!(Staking::on_initialize(1) >= on_finalize_weight);
3835+
})
3836+
}
3837+
38303838
mod election_data_provider {
38313839
use super::*;
38323840
use frame_election_provider_support::ElectionDataProvider;

0 commit comments

Comments
 (0)