Skip to content

Commit

Permalink
staking: simplify end-epoch delegation pool adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanor committed Mar 15, 2024
1 parent 3defa63 commit 5a8f1aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
39 changes: 9 additions & 30 deletions crates/core/component/stake/src/component/epoch_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,40 +222,19 @@ pub trait EpochHandler: StateWriteExt + ConsensusIndexRead {
"net delegation change for validator's pool for the epoch"
);

// Delegations and undelegations created in the previous epoch were created
// with the prev_validator_rate. To compute the staking delta, we need to take
// an absolute value and then re-apply the sign, since .unbonded_amount operates
// on unsigned values.
let absolute_delegation_change = Amount::from(delegation_delta.unsigned_abs());

// Staking tokens are being delegated, so the staking token supply decreases and
// the delegation token supply increases.
let abs_delegation_change = Amount::from(delegation_delta.unsigned_abs());

// We need to either contract or expand the validator pool size,
// and panic if we encounter an under/overflow, because it can only
// happen if something has gone seriously wrong with the validator rate data.
if delegation_delta > 0 {
tracing::debug!(
validator = ?validator.identity_key,
"staking tokens are being delegated, so the staking token supply decreases and the delegation token supply increases");
self.increase_validator_pool_size(validator_identity, absolute_delegation_change)
self.increase_validator_pool_size(validator_identity, abs_delegation_change)
.await
.with_context(|| {
format!(
"failed to increase delegation token supply by {}",
absolute_delegation_change
)
})?;
.expect("overflow should be impossible");
} else if delegation_delta < 0 {
tracing::debug!(
validator = ?validator.identity_key,
"staking tokens are being undelegated, so the staking token supply increases and the delegation token supply decreases");
// Vice-versa: staking tokens are being undelegated, so the staking token supply
// increases and the delegation token supply decreases.
self.decrease_validator_pool_size(validator_identity, absolute_delegation_change)
self.decrease_validator_pool_size(validator_identity, abs_delegation_change)
.await
.with_context(|| {
format!(
"failed to decrease delegation token supply by {}",
absolute_delegation_change
)
})?;
.expect("underflow should be impossible");
} else {
tracing::debug!(
validator = ?validator.identity_key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ pub(crate) trait ValidatorPoolTracker: StateWrite {
.expect("no deserialization error expected")
.unwrap_or(Amount::zero());

tracing::debug!(validator_identity = %identity_key, ?add, ?old_supply, "expanding validator pool size");

if let Some(new_supply) = old_supply.checked_add(&add) {
self.put(state_path, new_supply);
Some(new_supply)
Expand All @@ -315,6 +317,8 @@ pub(crate) trait ValidatorPoolTracker: StateWrite {
.expect("no deserialization error expected")
.unwrap_or(Amount::zero());

tracing::debug!(validator_identity = %identity_key, ?sub, ?old_supply, "contracting validator pool size");

if let Some(new_supply) = old_supply.checked_sub(&sub) {
self.put(state_path, new_supply);
Some(new_supply)
Expand Down

0 comments on commit 5a8f1aa

Please sign in to comment.