Better Handle Dead Accounts in Balances (#7843)

* Don't mutate storage when account is dead and should stay dead

* cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_balances --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/balances/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* more concrete storage noop

Co-authored-by: Parity Benchmarking Bot <admin@parity.io>
This commit is contained in:
Shawn Tabrizi
2021-01-07 13:42:37 -04:00
committed by GitHub
parent 779c4f8616
commit 93ecff9b7c
4 changed files with 59 additions and 30 deletions
+6 -3
View File
@@ -982,7 +982,7 @@ impl<T: Config<I>, I: Instance> Currency<T::AccountId> for Module<T, I> where
/// Slash a target account `who`, returning the negative imbalance created and any left over
/// amount that could not be slashed.
///
/// Is a no-op if `value` to be slashed is zero.
/// Is a no-op if `value` to be slashed is zero or the account does not exist.
///
/// NOTE: `slash()` prefers free balance, but assumes that reserve balance can be drawn
/// from in extreme circumstances. `can_slash()` should be used prior to `slash()` to avoid having
@@ -993,6 +993,7 @@ impl<T: Config<I>, I: Instance> Currency<T::AccountId> for Module<T, I> where
value: Self::Balance
) -> (Self::NegativeImbalance, Self::Balance) {
if value.is_zero() { return (NegativeImbalance::zero(), Zero::zero()) }
if Self::is_dead_account(&who) { return (NegativeImbalance::zero(), value) }
Self::mutate_account(who, |account| {
let free_slash = cmp::min(account.free, value);
@@ -1146,9 +1147,10 @@ impl<T: Config<I>, I: Instance> ReservableCurrency<T::AccountId> for Module<T, I
/// Unreserve some funds, returning any amount that was unable to be unreserved.
///
/// Is a no-op if the value to be unreserved is zero.
/// Is a no-op if the value to be unreserved is zero or the account does not exist.
fn unreserve(who: &T::AccountId, value: Self::Balance) -> Self::Balance {
if value.is_zero() { return Zero::zero() }
if Self::is_dead_account(&who) { return value }
let actual = Self::mutate_account(who, |account| {
let actual = cmp::min(account.reserved, value);
@@ -1166,12 +1168,13 @@ impl<T: Config<I>, I: Instance> ReservableCurrency<T::AccountId> for Module<T, I
/// Slash from reserved balance, returning the negative imbalance created,
/// and any amount that was unable to be slashed.
///
/// Is a no-op if the value to be slashed is zero.
/// Is a no-op if the value to be slashed is zero or the account does not exist.
fn slash_reserved(
who: &T::AccountId,
value: Self::Balance
) -> (Self::NegativeImbalance, Self::Balance) {
if value.is_zero() { return (NegativeImbalance::zero(), Zero::zero()) }
if Self::is_dead_account(&who) { return (NegativeImbalance::zero(), value) }
Self::mutate_account(who, |account| {
// underflow should never happen, but it if does, there's nothing to be done here.