mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 09:21:05 +00:00
Staking ledger bonding fixes (#3639)
Currently, the staking logic does not prevent a controller from becoming a stash of *another* ledger (introduced by [removing this check](https://github.com/paritytech/polkadot-sdk/pull/1484/files#diff-3aa6ceab5aa4e0ab2ed73a7245e0f5b42e0832d8ca5b1ed85d7b2a52fb196524L850)). Given that the remaining of the code expects that never happens, bonding a ledger with a stash that is a controller of another ledger may lead to data inconsistencies and data losses in bonded ledgers. For more detailed explanation of this issue: https://hackmd.io/@gpestana/HJoBm2tqo/%2FTPdi28H7Qc2mNUqLSMn15w In a nutshell, when fetching a ledger with a given controller, we may be end up getting the wrong ledger which can lead to unexpected ledger states. This PR also ensures that `set_controller` does not lead to data inconsistencies in the staking ledger and bonded storage in the case when a controller of a stash is a stash of *another* ledger. and improves the staking `try-runtime` checks to catch potential issues with the storage preemptively. In summary, there are two important cases here: 1. **"Sane" double bonded ledger** When a controller of a ledger is a stash of *another* ledger. In this case, we have: ``` > Bonded(stash, controller) (A, B) // stash A with controller B (B, C) // B is also a stash of another ledger (C, D) > Ledger(controller) Ledger(B) = L_a (stash = A) Ledger(C) = L_b (stash = B) Ledger(D) = L_c (stash = C) ``` In this case, the ledgers can be mutated and all operations are OK. However, we should not allow `set_controller` to be called if it means it results in a "corrupt" double bonded ledger (see below). 3. **"Corrupt" double bonded ledger** ``` > Bonded(stash, controller) (A, B) // stash A with controller B (B, B) (C, D) ``` In this case, B is a stash and controller AND is corrupted, since B is responsible for 2 ledgers which is not correct and will lead to inconsistent states. Thus, in this case, in this PR we are preventing these ledgers from mutating (i.e. operations like bonding extra etc) until the ledger is brought back to a consistent state. --- **Changes**: - Checks if stash is already a controller when calling `Call::bond` (fixes the regression introduced by [removing this check](https://github.com/paritytech/polkadot-sdk/pull/1484/files#diff-3aa6ceab5aa4e0ab2ed73a7245e0f5b42e0832d8ca5b1ed85d7b2a52fb196524L850)); - Ensures that all fetching ledgers from storage are done through the `StakingLedger` API; - Ensures that -- when fetching a ledger from storage using the `StakingLedger` API --, a `Error::BadState` is returned if the ledger bonding is in a bad state. This prevents bad ledgers from mutating (e.g. `bond_extra`, `set_controller`, etc) its state and avoid further data inconsistencies. - Prevents stashes which are controllers or another ledger from calling `set_controller`, since that may lead to a bad state. - Adds further try-state runtime checks that check if there are ledgers in a bad state based on their bonded metadata. Related to https://github.com/paritytech/polkadot-sdk/issues/3245 --------- Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: kianenigma <kian@parity.io>
This commit is contained in:
@@ -1248,6 +1248,23 @@ fn bond_extra_works() {
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bond_extra_controller_bad_state_works() {
|
||||
ExtBuilder::default().try_state(false).build_and_execute(|| {
|
||||
assert_eq!(StakingLedger::<Test>::get(StakingAccount::Stash(31)).unwrap().stash, 31);
|
||||
|
||||
// simulate ledger in bad state: the controller 41 is associated to the stash 31 and 41.
|
||||
Bonded::<Test>::insert(31, 41);
|
||||
|
||||
// we confirm that the ledger is in bad state: 31 has 41 as controller and when fetching
|
||||
// the ledger associated with the controler 41, its stash is 41 (and not 31).
|
||||
assert_eq!(Ledger::<Test>::get(41).unwrap().stash, 41);
|
||||
|
||||
// if the ledger is in this bad state, the `bond_extra` should fail.
|
||||
assert_noop!(Staking::bond_extra(RuntimeOrigin::signed(31), 10), Error::<Test>::BadState);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bond_extra_and_withdraw_unbonded_works() {
|
||||
//
|
||||
@@ -6910,6 +6927,49 @@ mod ledger {
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_ledger_bad_state_fails() {
|
||||
ExtBuilder::default().has_stakers(false).try_state(false).build_and_execute(|| {
|
||||
setup_double_bonded_ledgers();
|
||||
|
||||
// Case 1: double bonded but not corrupted:
|
||||
// stash 2 has controller 3:
|
||||
assert_eq!(Bonded::<Test>::get(2), Some(3));
|
||||
assert_eq!(Ledger::<Test>::get(3).unwrap().stash, 2);
|
||||
|
||||
// stash 2 is also a controller of 1:
|
||||
assert_eq!(Bonded::<Test>::get(1), Some(2));
|
||||
assert_eq!(StakingLedger::<Test>::paired_account(StakingAccount::Stash(1)), Some(2));
|
||||
assert_eq!(Ledger::<Test>::get(2).unwrap().stash, 1);
|
||||
|
||||
// although 2 is double bonded (it is a controller and a stash of different ledgers),
|
||||
// we can safely retrieve the ledger and mutate it since the correct ledger is
|
||||
// returned.
|
||||
let ledger_result = StakingLedger::<Test>::get(StakingAccount::Stash(2));
|
||||
assert_eq!(ledger_result.unwrap().stash, 2); // correct ledger.
|
||||
|
||||
let ledger_result = StakingLedger::<Test>::get(StakingAccount::Controller(2));
|
||||
assert_eq!(ledger_result.unwrap().stash, 1); // correct ledger.
|
||||
|
||||
// fetching ledger 1 by its stash works.
|
||||
let ledger_result = StakingLedger::<Test>::get(StakingAccount::Stash(1));
|
||||
assert_eq!(ledger_result.unwrap().stash, 1);
|
||||
|
||||
// Case 2: corrupted ledger bonding.
|
||||
// in this case, we simulate what happens when fetching a ledger by stash returns a
|
||||
// ledger with a different stash. when this happens, we return an error instead of the
|
||||
// ledger to prevent ledger mutations.
|
||||
let mut ledger = Ledger::<Test>::get(2).unwrap();
|
||||
assert_eq!(ledger.stash, 1);
|
||||
ledger.stash = 2;
|
||||
Ledger::<Test>::insert(2, ledger);
|
||||
|
||||
// now, we are prevented from fetching the ledger by stash from 1. It's associated
|
||||
// controller (2) is now bonding a ledger with a different stash (2, not 1).
|
||||
assert!(StakingLedger::<Test>::get(StakingAccount::Stash(1)).is_err());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bond_works() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
@@ -6933,6 +6993,28 @@ mod ledger {
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bond_controller_cannot_be_stash_works() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
let (stash, controller) = testing_utils::create_unique_stash_controller::<Test>(
|
||||
0,
|
||||
10,
|
||||
RewardDestination::Staked,
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(Bonded::<Test>::get(stash), Some(controller));
|
||||
assert_eq!(Ledger::<Test>::get(controller).map(|l| l.stash), Some(stash));
|
||||
|
||||
// existing controller should not be able become a stash.
|
||||
assert_noop!(
|
||||
Staking::bond(RuntimeOrigin::signed(controller), 10, RewardDestination::Staked),
|
||||
Error::<Test>::AlreadyPaired,
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_bonded_works() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
@@ -7161,4 +7243,81 @@ mod ledger {
|
||||
assert_eq!(ledger_updated.stash, stash);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deprecate_controller_batch_with_bad_state_ok() {
|
||||
ExtBuilder::default().has_stakers(false).nominate(false).build_and_execute(|| {
|
||||
setup_double_bonded_ledgers();
|
||||
|
||||
// now let's deprecate all the controllers for all the existing ledgers.
|
||||
let bounded_controllers: BoundedVec<
|
||||
_,
|
||||
<Test as Config>::MaxControllersInDeprecationBatch,
|
||||
> = BoundedVec::try_from(vec![1, 2, 3, 4]).unwrap();
|
||||
|
||||
assert_ok!(Staking::deprecate_controller_batch(
|
||||
RuntimeOrigin::root(),
|
||||
bounded_controllers
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
*staking_events().last().unwrap(),
|
||||
Event::ControllerBatchDeprecated { failures: 0 }
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deprecate_controller_batch_with_bad_state_failures() {
|
||||
ExtBuilder::default().has_stakers(false).try_state(false).build_and_execute(|| {
|
||||
setup_double_bonded_ledgers();
|
||||
|
||||
// now let's deprecate all the controllers for all the existing ledgers.
|
||||
let bounded_controllers: BoundedVec<
|
||||
_,
|
||||
<Test as Config>::MaxControllersInDeprecationBatch,
|
||||
> = BoundedVec::try_from(vec![4, 3, 2, 1]).unwrap();
|
||||
|
||||
assert_ok!(Staking::deprecate_controller_batch(
|
||||
RuntimeOrigin::root(),
|
||||
bounded_controllers
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
*staking_events().last().unwrap(),
|
||||
Event::ControllerBatchDeprecated { failures: 2 }
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_controller_with_bad_state_ok() {
|
||||
ExtBuilder::default().has_stakers(false).nominate(false).build_and_execute(|| {
|
||||
setup_double_bonded_ledgers();
|
||||
|
||||
// in this case, setting controller works due to the ordering of the calls.
|
||||
assert_ok!(Staking::set_controller(RuntimeOrigin::signed(1)));
|
||||
assert_ok!(Staking::set_controller(RuntimeOrigin::signed(2)));
|
||||
assert_ok!(Staking::set_controller(RuntimeOrigin::signed(3)));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_controller_with_bad_state_fails() {
|
||||
ExtBuilder::default().has_stakers(false).try_state(false).build_and_execute(|| {
|
||||
setup_double_bonded_ledgers();
|
||||
|
||||
// setting the controller of ledger associated with stash 3 fails since its stash is a
|
||||
// controller of another ledger.
|
||||
assert_noop!(
|
||||
Staking::set_controller(RuntimeOrigin::signed(3)),
|
||||
Error::<Test>::BadState
|
||||
);
|
||||
assert_noop!(
|
||||
Staking::set_controller(RuntimeOrigin::signed(2)),
|
||||
Error::<Test>::BadState
|
||||
);
|
||||
assert_ok!(Staking::set_controller(RuntimeOrigin::signed(1)));
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user