mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-18 14:11:02 +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:
@@ -32,8 +32,8 @@
|
||||
//! state consistency.
|
||||
|
||||
use frame_support::{
|
||||
defensive,
|
||||
traits::{LockableCurrency, WithdrawReasons},
|
||||
defensive, ensure,
|
||||
traits::{Defensive, LockableCurrency, WithdrawReasons},
|
||||
};
|
||||
use sp_staking::StakingAccount;
|
||||
use sp_std::prelude::*;
|
||||
@@ -106,18 +106,39 @@ impl<T: Config> StakingLedger<T> {
|
||||
/// This getter can be called with either a controller or stash account, provided that the
|
||||
/// account is properly wrapped in the respective [`StakingAccount`] variant. This is meant to
|
||||
/// abstract the concept of controller/stash accounts from the caller.
|
||||
///
|
||||
/// Returns [`Error::BadState`] when a bond is in "bad state". A bond is in a bad state when a
|
||||
/// stash has a controller which is bonding a ledger associated with another stash.
|
||||
pub(crate) fn get(account: StakingAccount<T::AccountId>) -> Result<StakingLedger<T>, Error<T>> {
|
||||
let controller = match account {
|
||||
StakingAccount::Stash(stash) => <Bonded<T>>::get(stash).ok_or(Error::<T>::NotStash),
|
||||
StakingAccount::Controller(controller) => Ok(controller),
|
||||
}?;
|
||||
let (stash, controller) = match account.clone() {
|
||||
StakingAccount::Stash(stash) =>
|
||||
(stash.clone(), <Bonded<T>>::get(&stash).ok_or(Error::<T>::NotStash)?),
|
||||
StakingAccount::Controller(controller) => (
|
||||
Ledger::<T>::get(&controller)
|
||||
.map(|l| l.stash)
|
||||
.ok_or(Error::<T>::NotController)?,
|
||||
controller,
|
||||
),
|
||||
};
|
||||
|
||||
<Ledger<T>>::get(&controller)
|
||||
let ledger = <Ledger<T>>::get(&controller)
|
||||
.map(|mut ledger| {
|
||||
ledger.controller = Some(controller.clone());
|
||||
ledger
|
||||
})
|
||||
.ok_or(Error::<T>::NotController)
|
||||
.ok_or(Error::<T>::NotController)?;
|
||||
|
||||
// if ledger bond is in a bad state, return error to prevent applying operations that may
|
||||
// further spoil the ledger's state. A bond is in bad state when the bonded controller is
|
||||
// associted with a different ledger (i.e. a ledger with a different stash).
|
||||
//
|
||||
// See <https://github.com/paritytech/polkadot-sdk/issues/3245> for more details.
|
||||
ensure!(
|
||||
Bonded::<T>::get(&stash) == Some(controller) && ledger.stash == stash,
|
||||
Error::<T>::BadState
|
||||
);
|
||||
|
||||
Ok(ledger)
|
||||
}
|
||||
|
||||
/// Returns the reward destination of a staking ledger, stored in [`Payee`].
|
||||
@@ -201,6 +222,30 @@ impl<T: Config> StakingLedger<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the ledger controller to its stash.
|
||||
pub(crate) fn set_controller_to_stash(self) -> Result<(), Error<T>> {
|
||||
let controller = self.controller.as_ref()
|
||||
.defensive_proof("Ledger's controller field didn't exist. The controller should have been fetched using StakingLedger.")
|
||||
.ok_or(Error::<T>::NotController)?;
|
||||
|
||||
ensure!(self.stash != *controller, Error::<T>::AlreadyPaired);
|
||||
|
||||
// check if the ledger's stash is a controller of another ledger.
|
||||
if let Some(bonded_ledger) = Ledger::<T>::get(&self.stash) {
|
||||
// there is a ledger bonded by the stash. In this case, the stash of the bonded ledger
|
||||
// should be the same as the ledger's stash. Otherwise fail to prevent data
|
||||
// inconsistencies. See <https://github.com/paritytech/polkadot-sdk/pull/3639> for more
|
||||
// details.
|
||||
ensure!(bonded_ledger.stash == self.stash, Error::<T>::BadState);
|
||||
}
|
||||
|
||||
<Ledger<T>>::remove(&controller);
|
||||
<Ledger<T>>::insert(&self.stash, &self);
|
||||
<Bonded<T>>::insert(&self.stash, &self.stash);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Clears all data related to a staking ledger and its bond in both [`Ledger`] and [`Bonded`]
|
||||
/// storage items and updates the stash staking lock.
|
||||
pub(crate) fn kill(stash: &T::AccountId) -> Result<(), Error<T>> {
|
||||
|
||||
Reference in New Issue
Block a user