Skip to content

feat(x/distribution): auto-stake bond denom rewards and commission#94

Draft
giunatale wants to merge 19 commits into
mainfrom
giunatale/feat/auto-stake-rewards-commission
Draft

feat(x/distribution): auto-stake bond denom rewards and commission#94
giunatale wants to merge 19 commits into
mainfrom
giunatale/feat/auto-stake-rewards-commission

Conversation

@giunatale

@giunatale giunatale commented May 7, 2026

Copy link
Copy Markdown
Collaborator

This PR is a variant of #93 that also adds auto-staking of validator's commission.

Commission auto-stake is not done at allocation time and is instead routed through the standard staking.Delegate path at two trigger points:

  1. MsgWithdrawValidatorCommission — operator-initiated. Auto-stakes bond
    denom commission, then pays out non-bond as before.
  2. AfterEpochEnd, gated on a new commission_auto_stake_epoch_identifier
    param — protocol-driven. Iterates bonded validators and auto-stakes
    each one's bond denom commission. Operators can still withdraw more
    often if they prefer; the epoch is a floor on cadence, not a ceiling.

The standard Delegate path keeps shares-based F1 accounting consistent because share changes go through the BeforeDelegationSharesModified / AfterDelegationModified hook flow, exactly as F1 expects. A different mechanism to do this at BeginBlock was explored but resulted in a much more cumbersome and invasive implementation, hence more prone to errors and bugs (because it had to circumvent the invariant by which new shares issuance had to go through the aforementioned hooks).

giunatale added 7 commits May 7, 2026 15:07
Adds a new keeper method in x/staking that increases a bonded validator's token balance without issuing new delegator shares, raising the per-share exchange rate for all existing delegators proportionally.
Modifies the allocation logic in x/distribution to route the bond denom (minus validator commission) through the auto-staking path while commission and other coins go through F1.
@giunatale giunatale force-pushed the giunatale/feat/auto-stake-rewards-commission branch from df912a6 to af5ec28 Compare May 7, 2026 17:58
giunatale added 6 commits May 8, 2026 01:41
The tokens-based F1 algorithm computes rewards-per-token using
val.GetTokens() and applies slash events to scale stake by (1-fraction).
This works when sum(startingStakes) == val.Tokens, but auto-staking
breaks that invariant: AddValidatorTokens raises val.Tokens without
issuing new shares, so per-token ratio dilutes existing delegators.
A sole self-delegator who should receive 100% of non-bond rewards
instead got rewards × old_tokens / new_tokens, with the residual
accumulating as unclaimable F1 dust on the validator.

Switching the ratio to rewards-per-share (val.GetDelegatorShares())
and storing the delegator's share count in startingInfo.Stake makes
the calculation invariant to per-share exchange-rate changes from
both auto-staking and slashing. Slash event handling in
calculateDelegationRewardsBetween becomes unnecessary - slashes
naturally reduce future rewards via reduced voting power, and the
shares-based ratio distributes whatever was earned proportionally.
@giunatale giunatale force-pushed the giunatale/feat/auto-stake-rewards-commission branch from af5ec28 to 4790a44 Compare May 8, 2026 15:36
giunatale added 6 commits May 10, 2026 17:06
there was no actual need to force-claim the commission at migration,
therefore this part of the migration code is here removed.
Implements commission auto-stake routed through the standard staking.Delegate
path at two trigger points:

1. MsgWithdrawValidatorCommission — operator-initiated. Auto-stakes bond
   denom commission, then pays out non-bond as before.
2. AfterEpochEnd, gated on a new commission_auto_stake_epoch_identifier
   param — protocol-driven. Iterates bonded validators and auto-stakes
   each one's bond denom commission. Operators can still withdraw more
   often if they prefer; the epoch is a floor on cadence, not a ceiling.

The standard Delegate path keeps shares-based F1 accounting consistent
because share changes go through the BeforeDelegationSharesModified /
AfterDelegationModified hook flow, exactly as F1 expects. A different
mechanism to do this at BeginBlock was explored but resulted in a much
more cumbersome and invasive implementation.

Validators that are unbonding, unbonded, or jailed are not in
GetBondedValidatorsByPower and are therefore skipped by the epoch
trigger; they no longer accrue new commission either, so commission
already accumulated waits in accumulatedCommission until validator
removal. The existing AfterValidatorRemoved force-payout handles any
residual at that point. The escape route is bounded by epoch length
(~one epoch's commission per validator lifetime) and is not a viable
incentive.
@giunatale giunatale force-pushed the giunatale/feat/auto-stake-rewards-commission branch from 4790a44 to 03eaa96 Compare May 10, 2026 15:52
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the Stale label Jun 10, 2026
@giunatale giunatale removed the Stale label Jun 10, 2026
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the Stale label Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant