Skip to content

Commit c1915af

Browse files
rockbmbbkchr
andauthored
Add minor improvements to chill_other test (#7553)
# Description open-web3-stack/polkadot-ecosystem-tests#174 showed the test for the `pallet_staking::chill_other` extrinsic could be more exhaustive. This PR adds those checks, and also a few more to another test related to `chill_other`, `pallet_staking::tests::change_of_absolute_max_nominations`. ## Integration N/A ## Review Notes N/A --------- Co-authored-by: Bastian Köcher <[email protected]>
1 parent 8779c60 commit c1915af

File tree

1 file changed

+129
-13
lines changed

1 file changed

+129
-13
lines changed

substrate/frame/staking/src/tests.rs

Lines changed: 129 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5789,6 +5789,21 @@ fn chill_other_works() {
57895789
//
57905790
// If any of these are missing, we do not have enough information to allow the
57915791
// `chill_other` to succeed from one user to another.
5792+
//
5793+
// Out of 8 possible cases, only one will allow the use of `chill_other`, which is
5794+
// when all 3 conditions are met.
5795+
5796+
// 1. No limits whatsoever
5797+
assert_ok!(Staking::set_staking_configs(
5798+
RuntimeOrigin::root(),
5799+
ConfigOp::Remove,
5800+
ConfigOp::Remove,
5801+
ConfigOp::Remove,
5802+
ConfigOp::Remove,
5803+
ConfigOp::Remove,
5804+
ConfigOp::Remove,
5805+
ConfigOp::Remove,
5806+
));
57925807

57935808
// Can't chill these users
57945809
assert_noop!(
@@ -5800,15 +5815,15 @@ fn chill_other_works() {
58005815
Error::<Test>::CannotChillOther
58015816
);
58025817

5803-
// Change the minimum bond... but no limits.
5818+
// 2. Change only the minimum bonds.
58045819
assert_ok!(Staking::set_staking_configs(
58055820
RuntimeOrigin::root(),
58065821
ConfigOp::Set(1_500),
58075822
ConfigOp::Set(2_000),
5808-
ConfigOp::Remove,
5809-
ConfigOp::Remove,
5810-
ConfigOp::Remove,
5811-
ConfigOp::Remove,
5823+
ConfigOp::Noop,
5824+
ConfigOp::Noop,
5825+
ConfigOp::Noop,
5826+
ConfigOp::Noop,
58125827
ConfigOp::Noop,
58135828
));
58145829

@@ -5822,11 +5837,11 @@ fn chill_other_works() {
58225837
Error::<Test>::CannotChillOther
58235838
);
58245839

5825-
// Add limits, but no threshold
5840+
// 3. Add nominator/validator count limits, but no other threshold.
58265841
assert_ok!(Staking::set_staking_configs(
58275842
RuntimeOrigin::root(),
5828-
ConfigOp::Noop,
5829-
ConfigOp::Noop,
5843+
ConfigOp::Remove,
5844+
ConfigOp::Remove,
58305845
ConfigOp::Set(10),
58315846
ConfigOp::Set(10),
58325847
ConfigOp::Noop,
@@ -5844,15 +5859,59 @@ fn chill_other_works() {
58445859
Error::<Test>::CannotChillOther
58455860
);
58465861

5847-
// Add threshold, but no limits
5862+
// 4. Add chil threshold, but no other limits
58485863
assert_ok!(Staking::set_staking_configs(
58495864
RuntimeOrigin::root(),
58505865
ConfigOp::Noop,
58515866
ConfigOp::Noop,
58525867
ConfigOp::Remove,
58535868
ConfigOp::Remove,
5869+
ConfigOp::Set(Percent::from_percent(75)),
58545870
ConfigOp::Noop,
58555871
ConfigOp::Noop,
5872+
));
5873+
5874+
// Still can't chill these users
5875+
assert_noop!(
5876+
Staking::chill_other(RuntimeOrigin::signed(1337), 0),
5877+
Error::<Test>::CannotChillOther
5878+
);
5879+
assert_noop!(
5880+
Staking::chill_other(RuntimeOrigin::signed(1337), 2),
5881+
Error::<Test>::CannotChillOther
5882+
);
5883+
5884+
// 5. Add bond and count limits, but no threshold
5885+
assert_ok!(Staking::set_staking_configs(
5886+
RuntimeOrigin::root(),
5887+
ConfigOp::Set(1_500),
5888+
ConfigOp::Set(2_000),
5889+
ConfigOp::Set(10),
5890+
ConfigOp::Set(10),
5891+
ConfigOp::Remove,
5892+
ConfigOp::Remove,
5893+
ConfigOp::Remove,
5894+
));
5895+
5896+
// Still can't chill these users
5897+
assert_noop!(
5898+
Staking::chill_other(RuntimeOrigin::signed(1337), 0),
5899+
Error::<Test>::CannotChillOther
5900+
);
5901+
assert_noop!(
5902+
Staking::chill_other(RuntimeOrigin::signed(1337), 2),
5903+
Error::<Test>::CannotChillOther
5904+
);
5905+
5906+
// 6. Add bond and threshold limits, but no count limits
5907+
assert_ok!(Staking::set_staking_configs(
5908+
RuntimeOrigin::root(),
5909+
ConfigOp::Noop,
5910+
ConfigOp::Noop,
5911+
ConfigOp::Remove,
5912+
ConfigOp::Remove,
5913+
ConfigOp::Set(Percent::from_percent(75)),
5914+
ConfigOp::Noop,
58565915
ConfigOp::Noop,
58575916
));
58585917

@@ -5866,11 +5925,33 @@ fn chill_other_works() {
58665925
Error::<Test>::CannotChillOther
58675926
);
58685927

5869-
// Add threshold and limits
5928+
// 7. Add count limits and a chill threshold, but no bond limits
58705929
assert_ok!(Staking::set_staking_configs(
58715930
RuntimeOrigin::root(),
5931+
ConfigOp::Remove,
5932+
ConfigOp::Remove,
5933+
ConfigOp::Set(10),
5934+
ConfigOp::Set(10),
5935+
ConfigOp::Set(Percent::from_percent(75)),
58725936
ConfigOp::Noop,
58735937
ConfigOp::Noop,
5938+
));
5939+
5940+
// Still can't chill these users
5941+
assert_noop!(
5942+
Staking::chill_other(RuntimeOrigin::signed(1337), 0),
5943+
Error::<Test>::CannotChillOther
5944+
);
5945+
assert_noop!(
5946+
Staking::chill_other(RuntimeOrigin::signed(1337), 2),
5947+
Error::<Test>::CannotChillOther
5948+
);
5949+
5950+
// 8. Add all limits
5951+
assert_ok!(Staking::set_staking_configs(
5952+
RuntimeOrigin::root(),
5953+
ConfigOp::Set(1_500),
5954+
ConfigOp::Set(2_000),
58745955
ConfigOp::Set(10),
58755956
ConfigOp::Set(10),
58765957
ConfigOp::Set(Percent::from_percent(75)),
@@ -5888,7 +5969,9 @@ fn chill_other_works() {
58885969
let b = 4 * i;
58895970
let d = 4 * i + 2;
58905971
assert_ok!(Staking::chill_other(RuntimeOrigin::signed(1337), b));
5972+
assert_eq!(*staking_events().last().unwrap(), Event::Chilled { stash: b });
58915973
assert_ok!(Staking::chill_other(RuntimeOrigin::signed(1337), d));
5974+
assert_eq!(*staking_events().last().unwrap(), Event::Chilled { stash: d });
58925975
}
58935976

58945977
// chill a nominator. Limit is not reached, not chill-able
@@ -6090,6 +6173,14 @@ fn change_of_absolute_max_nominations() {
60906173
);
60916174
assert_eq!(Staking::electing_voters(bounds).unwrap().len(), 3 + 3);
60926175

6176+
// No one can be chilled on account of non-decodable keys.
6177+
for k in Nominators::<Test>::iter_keys() {
6178+
assert_noop!(
6179+
Staking::chill_other(RuntimeOrigin::signed(1), k),
6180+
Error::<Test>::CannotChillOther
6181+
);
6182+
}
6183+
60936184
// abrupt change from 4 to 3, everyone should be fine.
60946185
AbsoluteMaxNominations::set(3);
60956186

@@ -6101,8 +6192,16 @@ fn change_of_absolute_max_nominations() {
61016192
);
61026193
assert_eq!(Staking::electing_voters(bounds).unwrap().len(), 3 + 3);
61036194

6195+
// As before, no one can be chilled on account of non-decodable keys.
6196+
for k in Nominators::<Test>::iter_keys() {
6197+
assert_noop!(
6198+
Staking::chill_other(RuntimeOrigin::signed(1), k),
6199+
Error::<Test>::CannotChillOther
6200+
);
6201+
}
6202+
61046203
// abrupt change from 3 to 2, this should cause some nominators to be non-decodable, and
6105-
// thus non-existent unless if they update.
6204+
// thus non-existent unless they update.
61066205
AbsoluteMaxNominations::set(2);
61076206

61086207
assert_eq!(
@@ -6111,7 +6210,16 @@ fn change_of_absolute_max_nominations() {
61116210
.collect::<Vec<_>>(),
61126211
vec![(101, 2), (61, 1)]
61136212
);
6114-
// 70 is still in storage..
6213+
6214+
// 101 and 61 still cannot be chilled by someone else.
6215+
for k in [101, 61].iter() {
6216+
assert_noop!(
6217+
Staking::chill_other(RuntimeOrigin::signed(1), *k),
6218+
Error::<Test>::CannotChillOther
6219+
);
6220+
}
6221+
6222+
// 71 is still in storage..
61156223
assert!(Nominators::<Test>::contains_key(71));
61166224
// but its value cannot be decoded and default is returned.
61176225
assert!(Nominators::<Test>::get(71).is_none());
@@ -6120,7 +6228,7 @@ fn change_of_absolute_max_nominations() {
61206228
assert!(Nominators::<Test>::contains_key(101));
61216229

61226230
// abrupt change from 2 to 1, this should cause some nominators to be non-decodable, and
6123-
// thus non-existent unless if they update.
6231+
// thus non-existent unless they update.
61246232
AbsoluteMaxNominations::set(1);
61256233

61266234
assert_eq!(
@@ -6129,6 +6237,13 @@ fn change_of_absolute_max_nominations() {
61296237
.collect::<Vec<_>>(),
61306238
vec![(61, 1)]
61316239
);
6240+
6241+
// 61 *still* cannot be chilled by someone else.
6242+
assert_noop!(
6243+
Staking::chill_other(RuntimeOrigin::signed(1), 61),
6244+
Error::<Test>::CannotChillOther
6245+
);
6246+
61326247
assert!(Nominators::<Test>::contains_key(71));
61336248
assert!(Nominators::<Test>::contains_key(61));
61346249
assert!(Nominators::<Test>::get(71).is_none());
@@ -6148,6 +6263,7 @@ fn change_of_absolute_max_nominations() {
61486263
assert!(Nominators::<Test>::contains_key(101));
61496264
assert!(Nominators::<Test>::get(101).is_none());
61506265
assert_ok!(Staking::chill_other(RuntimeOrigin::signed(71), 101));
6266+
assert_eq!(*staking_events().last().unwrap(), Event::Chilled { stash: 101 });
61516267
assert!(!Nominators::<Test>::contains_key(101));
61526268
assert!(Nominators::<Test>::get(101).is_none());
61536269
})

0 commit comments

Comments
 (0)