Skip to content

Commit 62425b6

Browse files
emostovDoordashconkianenigma
authored andcommitted
Make bags-list generic over node value and instantiable (paritytech#10997)
* make instantiable * update * cargo fmt * Clean up * bags-list: Make it generic over node value * Respond to some feedback * Apply suggestions from code review Co-authored-by: Kian Paimani <[email protected]> * Add back default impl for weight update worst case * Update to Score in more places' * Use VoteWeight, not u64 to reduce test diff * FMT * FullCodec implies Codec * formatting * Fixup bags list remote test Co-authored-by: doordashcon <[email protected]> Co-authored-by: Doordashcon <[email protected]> Co-authored-by: Kian Paimani <[email protected]>
1 parent 344af84 commit 62425b6

File tree

14 files changed

+428
-283
lines changed

14 files changed

+428
-283
lines changed

bin/node/runtime/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,10 @@ parameter_types! {
690690

691691
impl pallet_bags_list::Config for Runtime {
692692
type Event = Event;
693-
type VoteWeightProvider = Staking;
693+
type ScoreProvider = Staking;
694694
type WeightInfo = pallet_bags_list::weights::SubstrateWeight<Runtime>;
695695
type BagThresholds = BagThresholds;
696+
type Score = sp_npos_elections::VoteWeight;
696697
}
697698

698699
parameter_types! {

frame/bags-list/remote-tests/src/lib.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
//! Utilities for remote-testing pallet-bags-list.
1919
20+
use frame_election_provider_support::ScoreProvider;
2021
use sp_std::prelude::*;
2122

2223
/// A common log target to use.
@@ -55,8 +56,12 @@ pub fn display_and_check_bags<Runtime: RuntimeT>(currency_unit: u64, currency_na
5556
let mut rebaggable = 0;
5657
let mut active_bags = 0;
5758
for vote_weight_thresh in <Runtime as pallet_bags_list::Config>::BagThresholds::get() {
59+
let vote_weight_thresh_u64: u64 = (*vote_weight_thresh)
60+
.try_into()
61+
.map_err(|_| "runtime must configure score to at most u64 to use this test")
62+
.unwrap();
5863
// threshold in terms of UNITS (e.g. KSM, DOT etc)
59-
let vote_weight_thresh_as_unit = *vote_weight_thresh as f64 / currency_unit as f64;
64+
let vote_weight_thresh_as_unit = vote_weight_thresh_u64 as f64 / currency_unit as f64;
6065
let pretty_thresh = format!("Threshold: {}. {}", vote_weight_thresh_as_unit, currency_name);
6166

6267
let bag = match pallet_bags_list::Pallet::<Runtime>::list_bags_get(*vote_weight_thresh) {
@@ -70,9 +75,13 @@ pub fn display_and_check_bags<Runtime: RuntimeT>(currency_unit: u64, currency_na
7075
active_bags += 1;
7176

7277
for id in bag.std_iter().map(|node| node.std_id().clone()) {
73-
let vote_weight = pallet_staking::Pallet::<Runtime>::weight_of(&id);
78+
let vote_weight = <Runtime as pallet_bags_list::Config>::ScoreProvider::score(&id);
79+
let vote_weight_thresh_u64: u64 = (*vote_weight_thresh)
80+
.try_into()
81+
.map_err(|_| "runtime must configure score to at most u64 to use this test")
82+
.unwrap();
7483
let vote_weight_as_balance: pallet_staking::BalanceOf<Runtime> =
75-
vote_weight.try_into().map_err(|_| "can't convert").unwrap();
84+
vote_weight_thresh_u64.try_into().map_err(|_| "can't convert").unwrap();
7685

7786
if vote_weight_as_balance < min_nominator_bond {
7887
log::trace!(
@@ -87,13 +96,17 @@ pub fn display_and_check_bags<Runtime: RuntimeT>(currency_unit: u64, currency_na
8796
pallet_bags_list::Node::<Runtime>::get(&id).expect("node in bag must exist.");
8897
if node.is_misplaced(vote_weight) {
8998
rebaggable += 1;
99+
let notional_bag = pallet_bags_list::notional_bag_for::<Runtime, _>(vote_weight);
100+
let notional_bag_as_u64: u64 = notional_bag
101+
.try_into()
102+
.map_err(|_| "runtime must configure score to at most u64 to use this test")
103+
.unwrap();
90104
log::trace!(
91105
target: LOG_TARGET,
92106
"Account {:?} can be rebagged from {:?} to {:?}",
93107
id,
94108
vote_weight_thresh_as_unit,
95-
pallet_bags_list::notional_bag_for::<Runtime>(vote_weight) as f64 /
96-
currency_unit as f64
109+
notional_bag_as_u64 as f64 / currency_unit as f64
97110
);
98111
}
99112
}

frame/bags-list/src/benchmarks.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
use super::*;
2121
use crate::list::List;
2222
use frame_benchmarking::{account, whitelist_account, whitelisted_caller};
23-
use frame_election_provider_support::VoteWeightProvider;
23+
use frame_election_provider_support::ScoreProvider;
2424
use frame_support::{assert_ok, traits::Get};
2525
use frame_system::RawOrigin as SystemOrigin;
26+
use sp_runtime::traits::One;
2627

2728
frame_benchmarking::benchmarks! {
2829
rebag_non_terminal {
@@ -36,29 +37,29 @@ frame_benchmarking::benchmarks! {
3637

3738
// clear any pre-existing storage.
3839
// NOTE: safe to call outside block production
39-
List::<T>::unsafe_clear();
40+
List::<T, _>::unsafe_clear();
4041

4142
// define our origin and destination thresholds.
4243
let origin_bag_thresh = T::BagThresholds::get()[0];
4344
let dest_bag_thresh = T::BagThresholds::get()[1];
4445

4546
// seed items in the origin bag.
4647
let origin_head: T::AccountId = account("origin_head", 0, 0);
47-
assert_ok!(List::<T>::insert(origin_head.clone(), origin_bag_thresh));
48+
assert_ok!(List::<T, _>::insert(origin_head.clone(), origin_bag_thresh));
4849

4950
let origin_middle: T::AccountId = account("origin_middle", 0, 0); // the node we rebag (_R_)
50-
assert_ok!(List::<T>::insert(origin_middle.clone(), origin_bag_thresh));
51+
assert_ok!(List::<T, _>::insert(origin_middle.clone(), origin_bag_thresh));
5152

5253
let origin_tail: T::AccountId = account("origin_tail", 0, 0);
53-
assert_ok!(List::<T>::insert(origin_tail.clone(), origin_bag_thresh));
54+
assert_ok!(List::<T, _>::insert(origin_tail.clone(), origin_bag_thresh));
5455

5556
// seed items in the destination bag.
5657
let dest_head: T::AccountId = account("dest_head", 0, 0);
57-
assert_ok!(List::<T>::insert(dest_head.clone(), dest_bag_thresh));
58+
assert_ok!(List::<T, _>::insert(dest_head.clone(), dest_bag_thresh));
5859

5960
// the bags are in the expected state after initial setup.
6061
assert_eq!(
61-
List::<T>::get_bags(),
62+
List::<T, _>::get_bags(),
6263
vec![
6364
(origin_bag_thresh, vec![origin_head.clone(), origin_middle.clone(), origin_tail.clone()]),
6465
(dest_bag_thresh, vec![dest_head.clone()])
@@ -67,12 +68,12 @@ frame_benchmarking::benchmarks! {
6768

6869
let caller = whitelisted_caller();
6970
// update the weight of `origin_middle` to guarantee it will be rebagged into the destination.
70-
T::VoteWeightProvider::set_vote_weight_of(&origin_middle, dest_bag_thresh);
71+
T::ScoreProvider::set_score_of(&origin_middle, dest_bag_thresh);
7172
}: rebag(SystemOrigin::Signed(caller), origin_middle.clone())
7273
verify {
7374
// check the bags have updated as expected.
7475
assert_eq!(
75-
List::<T>::get_bags(),
76+
List::<T, _>::get_bags(),
7677
vec![
7778
(
7879
origin_bag_thresh,
@@ -104,18 +105,18 @@ frame_benchmarking::benchmarks! {
104105

105106
// seed items in the origin bag.
106107
let origin_head: T::AccountId = account("origin_head", 0, 0);
107-
assert_ok!(List::<T>::insert(origin_head.clone(), origin_bag_thresh));
108+
assert_ok!(List::<T, _>::insert(origin_head.clone(), origin_bag_thresh));
108109

109110
let origin_tail: T::AccountId = account("origin_tail", 0, 0); // the node we rebag (_R_)
110-
assert_ok!(List::<T>::insert(origin_tail.clone(), origin_bag_thresh));
111+
assert_ok!(List::<T, _>::insert(origin_tail.clone(), origin_bag_thresh));
111112

112113
// seed items in the destination bag.
113114
let dest_head: T::AccountId = account("dest_head", 0, 0);
114-
assert_ok!(List::<T>::insert(dest_head.clone(), dest_bag_thresh));
115+
assert_ok!(List::<T, _>::insert(dest_head.clone(), dest_bag_thresh));
115116

116117
// the bags are in the expected state after initial setup.
117118
assert_eq!(
118-
List::<T>::get_bags(),
119+
List::<T, _>::get_bags(),
119120
vec![
120121
(origin_bag_thresh, vec![origin_head.clone(), origin_tail.clone()]),
121122
(dest_bag_thresh, vec![dest_head.clone()])
@@ -124,12 +125,12 @@ frame_benchmarking::benchmarks! {
124125

125126
let caller = whitelisted_caller();
126127
// update the weight of `origin_tail` to guarantee it will be rebagged into the destination.
127-
T::VoteWeightProvider::set_vote_weight_of(&origin_tail, dest_bag_thresh);
128+
T::ScoreProvider::set_score_of(&origin_tail, dest_bag_thresh);
128129
}: rebag(SystemOrigin::Signed(caller), origin_tail.clone())
129130
verify {
130131
// check the bags have updated as expected.
131132
assert_eq!(
132-
List::<T>::get_bags(),
133+
List::<T, _>::get_bags(),
133134
vec![
134135
(origin_bag_thresh, vec![origin_head.clone()]),
135136
(dest_bag_thresh, vec![dest_head.clone(), origin_tail.clone()])
@@ -147,30 +148,30 @@ frame_benchmarking::benchmarks! {
147148

148149
// insert the nodes in order
149150
let lighter: T::AccountId = account("lighter", 0, 0);
150-
assert_ok!(List::<T>::insert(lighter.clone(), bag_thresh));
151+
assert_ok!(List::<T, _>::insert(lighter.clone(), bag_thresh));
151152

152153
let heavier_prev: T::AccountId = account("heavier_prev", 0, 0);
153-
assert_ok!(List::<T>::insert(heavier_prev.clone(), bag_thresh));
154+
assert_ok!(List::<T, _>::insert(heavier_prev.clone(), bag_thresh));
154155

155156
let heavier: T::AccountId = account("heavier", 0, 0);
156-
assert_ok!(List::<T>::insert(heavier.clone(), bag_thresh));
157+
assert_ok!(List::<T, _>::insert(heavier.clone(), bag_thresh));
157158

158159
let heavier_next: T::AccountId = account("heavier_next", 0, 0);
159-
assert_ok!(List::<T>::insert(heavier_next.clone(), bag_thresh));
160+
assert_ok!(List::<T, _>::insert(heavier_next.clone(), bag_thresh));
160161

161-
T::VoteWeightProvider::set_vote_weight_of(&lighter, bag_thresh - 1);
162-
T::VoteWeightProvider::set_vote_weight_of(&heavier, bag_thresh);
162+
T::ScoreProvider::set_score_of(&lighter, bag_thresh - One::one());
163+
T::ScoreProvider::set_score_of(&heavier, bag_thresh);
163164

164165
assert_eq!(
165-
List::<T>::iter().map(|n| n.id().clone()).collect::<Vec<_>>(),
166+
List::<T, _>::iter().map(|n| n.id().clone()).collect::<Vec<_>>(),
166167
vec![lighter.clone(), heavier_prev.clone(), heavier.clone(), heavier_next.clone()]
167168
);
168169

169170
whitelist_account!(heavier);
170171
}: _(SystemOrigin::Signed(heavier.clone()), lighter.clone())
171172
verify {
172173
assert_eq!(
173-
List::<T>::iter().map(|n| n.id().clone()).collect::<Vec<_>>(),
174+
List::<T, _>::iter().map(|n| n.id().clone()).collect::<Vec<_>>(),
174175
vec![heavier, lighter, heavier_prev, heavier_next]
175176
)
176177
}

0 commit comments

Comments
 (0)