Skip to content

Commit

Permalink
Stake tracker improvements (migration and try-state checks OK in Polk…
Browse files Browse the repository at this point in the history
…adot) (#4673)

Changes in the migration code introduced in this PR:
- Migration removes duplicate nominations in all nominators, if they
exist (changes by calling `fn do_add_nominator` with dedup nominations)
- Migration removes all the non active validator nominations to avoid
adding dangling nominations (changes by calling `fn do_add_nominator` if
necessary)
- Migration iterates through all `Nominators` map first and through all
the `Validators` map to insert all the validators that are not nominated
to the target list (with self-stake only).
- Moves stake-tracker related try-state checks to the staking pallet (no
try-state checks in the stake-tracker pallet anymore).
- Runs benchmarks for MMB migration step with bench bot. 

The migration code has been validated against the Polkadot using the
externalities tests in
[polkadot/runtime/westend/src/lib.rs](https://github.com/paritytech/polkadot-sdk/pull/4436/files/46a80f14ba8990b014d8f9df36727c77e2f3e929#diff-5264d812f7aadfc486efffd3322b11c8dee5e8ddf2e77e0a9e70b573f3d2afdd).
Upon running the migrations, we ensure that:
- All validators have been added to the target list with their correct
approvals score (as per the try-state checks).
- All nominations are "cleaned" (see def. of clean above)
- Try-state checks related to stake-tracker and approvals pass.

Note: Same as #4436 but
with a non-messed up git history. Merging into
`gpestana/stake-tracker_integration`.

---------

Co-authored-by: Ankan <[email protected]>
Co-authored-by: command-bot <>
  • Loading branch information
gpestana and Ank4n authored Jun 3, 2024
1 parent 80895ad commit 6219b32
Show file tree
Hide file tree
Showing 17 changed files with 1,390 additions and 1,270 deletions.
897 changes: 440 additions & 457 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ parameter_types! {
pub const VoterBagThresholds: &'static [u64] = &bag_thresholds::VOTER_THRESHOLDS;
pub const TargetBagThresholds: &'static [u128] = &bag_thresholds::TARGET_THRESHOLDS;

pub const VoterUpdateMode: pallet_stake_tracker::VoterUpdateMode = pallet_stake_tracker::VoterUpdateMode::Strict;
pub const VoterUpdateMode: pallet_stake_tracker::VoterUpdateMode = pallet_stake_tracker::VoterUpdateMode::Lazy;
}

type VoterBagsListInstance = pallet_bags_list::Instance1;
Expand Down Expand Up @@ -1685,8 +1685,7 @@ pub mod migrations {
}

/// Unreleased migrations. Add new ones here:
pub type Unreleased =
(pallet_staking::migrations::single_block::v15::MigrateV14ToV15<Runtime>,);
pub type Unreleased = ();
}

/// Unchecked extrinsic type as expected by this runtime.
Expand Down
549 changes: 351 additions & 198 deletions polkadot/runtime/westend/src/weights/pallet_staking.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ impl pallet_staking::Config for Runtime {
}

parameter_types! {
pub const VoterUpdateMode: pallet_stake_tracker::VoterUpdateMode = pallet_stake_tracker::VoterUpdateMode::Strict;
pub const VoterUpdateMode: pallet_stake_tracker::VoterUpdateMode = pallet_stake_tracker::VoterUpdateMode::Lazy;
}

impl pallet_stake_tracker::Config for Runtime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl pallet_bags_list::Config<TargetBagsListInstance> for Runtime {
}

parameter_types! {
pub static UpdateMode: VoterUpdateMode = VoterUpdateMode::Strict;
pub static UpdateMode: VoterUpdateMode = VoterUpdateMode::Lazy;
}

impl pallet_stake_tracker::Config for Runtime {
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pallet-session = { path = "../session", default-features = false, features = [
"historical",
] }
pallet-authorship = { path = "../authorship", default-features = false }
pallet-stake-tracker = { path = "./stake-tracker", default-features = false }
sp-application-crypto = { path = "../../primitives/application-crypto", default-features = false, features = ["serde"] }
frame-election-provider-support = { path = "../election-provider-support", default-features = false }
log = { workspace = true }
Expand All @@ -45,9 +46,8 @@ sp-tracing = { path = "../../primitives/tracing" }
sp-core = { path = "../../primitives/core" }
sp-npos-elections = { path = "../../primitives/npos-elections" }
pallet-timestamp = { path = "../timestamp" }
pallet-stake-tracker = { path = "./stake-tracker" }
pallet-staking-reward-curve = { path = "reward-curve" }
pallet-bags-list = { path = "../bags-list" }
pallet-bags-list = { path = "../bags-list", default-features = false, features = ["try-runtime"] }
pallet-migrations = { path = "../migrations" }
substrate-test-utils = { path = "../../test-utils" }
frame-benchmarking = { path = "../benchmarking" }
Expand Down
34 changes: 5 additions & 29 deletions substrate/frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ mod benchmarks {

assert_eq!(
Staking::<T>::status(&voter),
Ok(StakerStatus::Nominator(vec![dangling_target.clone(), other_target.clone()]))
Ok(StakerStatus::Nominator(vec![other_target.clone(), dangling_target.clone()]))
);

#[extrinsic_call]
Expand Down Expand Up @@ -1214,7 +1214,7 @@ mod benchmarks {

impl_benchmark_test_suite!(
Staking,
crate::mock::ExtBuilder::default().has_stakers(true).set_voter_list_lazy(),
crate::mock::ExtBuilder::default().has_stakers(true),
crate::mock::Test,
exec_name = build_and_execute
);
Expand All @@ -1228,7 +1228,7 @@ mod tests {

#[test]
fn create_validators_with_nominators_for_era_works() {
ExtBuilder::default().build_and_execute(|| {
ExtBuilder::default().try_state(false).build_and_execute(|| {
let v = 10;
let n = 100;

Expand All @@ -1254,7 +1254,7 @@ mod tests {

#[test]
fn create_validator_with_nominators_works() {
ExtBuilder::default().build_and_execute(|| {
ExtBuilder::default().try_state(false).build_and_execute(|| {
let n = 10;

let (validator_stash, nominators) = create_validator_with_nominators::<Test>(
Expand Down Expand Up @@ -1285,7 +1285,7 @@ mod tests {

#[test]
fn add_slashing_spans_works() {
ExtBuilder::default().build_and_execute(|| {
ExtBuilder::default().try_state(false).build_and_execute(|| {
let n = 10;

let (validator_stash, _nominators) = create_validator_with_nominators::<Test>(
Expand Down Expand Up @@ -1315,28 +1315,4 @@ mod tests {
}
});
}

// TODO: SelectedBenchmark not exposed by v2?
/*
#[test]
fn test_payout_all() {
ExtBuilder::default().build_and_execute(|| {
let v = 10;
let n = 100;
let selected_benchmark = SelectedBenchmark::payout_all;
let c = vec![
(frame_benchmarking::BenchmarkParameter::v, v),
(frame_benchmarking::BenchmarkParameter::n, n),
];
assert_ok!(
<SelectedBenchmark as frame_benchmarking::BenchmarkingSetup<Test>>::unit_test_instance(
&selected_benchmark,
&c,
)
);
});
}
*/
}
Loading

0 comments on commit 6219b32

Please sign in to comment.