Skip to content

Commit e62eccb

Browse files
committed
Change crediting policy when adding stake. Adds respective unit tests.
1 parent 9d1df84 commit e62eccb

2 files changed

Lines changed: 419 additions & 10 deletions

File tree

primitives/account/src/account/staking_contract/staker.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -303,23 +303,22 @@ impl StakingContract {
303303

304304
// All checks passed, not allowed to fail from here on!
305305

306-
// Create the receipt.
307306
// Update the staker's and staking contract's balances.
308-
// We want to credit the active balance only if it's the bigger of the non retired balance.
309-
// Otherwise we chose the balance with the most funds between inactive and retired.
310-
let credited_balance;
311-
if staker.active_balance > staker.inactive_balance {
307+
// We want to preferentially credit the active balance. Only if there is no active balance,
308+
// then we will choose the balance with the most funds between inactive and retired.
309+
let credited_balance = if !staker.active_balance.is_zero() {
312310
staker.active_balance += value;
313-
credited_balance = BalanceType::Active;
314-
} else if staker.inactive_balance > staker.retired_balance {
311+
BalanceType::Active
312+
} else if staker.inactive_balance >= staker.retired_balance {
315313
staker.inactive_balance += value;
316-
credited_balance = BalanceType::Inactive;
314+
BalanceType::Inactive
317315
} else {
318316
staker.retired_balance += value;
319-
credited_balance = BalanceType::Retired;
320-
}
317+
BalanceType::Retired
318+
};
321319
self.balance += value;
322320

321+
// Create the receipt.
323322
let receipt = AddStakeReceipt {
324323
credited_balance: credited_balance.clone(),
325324
};

0 commit comments

Comments
 (0)