Skip to content

csnode: the revert of a timed delegation mirrors its apply - #89

Open
EnzinoBB wants to merge 1 commit into
CREDITSCOM:masterfrom
CreditsCommunity:official/delegation-revert
Open

EnzinoBB wants to merge 1 commit into
CREDITSCOM:masterfrom
CreditsCommunity:official/delegation-revert

Conversation

@EnzinoBB

@EnzinoBB EnzinoBB commented Nov 1, 2026

Copy link
Copy Markdown

Why this PR

When a block holding a timed delegation (expiry >= legate_min_utc) is reverted — for instance an uncertain last block removed by removeLastBlock() — WalletsCache reverts the target side, then the source side, and neither mirrors the apply:

  • revertDelegationsForTarget() erases the delegation from currentDelegations_[expiry] with an unchecked std::find (erasing end() when it is missing) and leaves the miningDelegations_ record in place;
  • revertDelegationsForSource() calls removeSingleDelegation(), which refunds only inside a loop over the other delegations with the same expiry; with a single delegation per expiry (the usual case) the source is not refunded and keeps its record in delegateTargets_.

When the block is applied again the source is debited a second time: that node holds the source's balance short by the delegated amount for good, plus a duplicate mining-delegation record, and its balances differ from the other nodes'.

What changes

csnode/src/staking.cpp:

  • target: the delegation is removed from currentDelegations_ and miningDelegations_ only if present, and empty entries are dropped;
  • source: the matching TimeMoney (same expiry and amount) is removed from delegateTargets_ and the amount refunded directly. If the delegation has already expired, cleanObsoletteDelegations() refunded and removed it, so nothing is refunded twice.

Effect on the network

No new rule and nothing on the wire: a node that reverts and re-applies such a block ends with the same balances as the nodes that applied it once.

Tests

tests/csnode/staking_tests.cpp: Staking.RevertOfTimedDelegationRefundsSource (apply, revert, apply again; the source stayed at 700 instead of 1000 before). Built and run on 4a95aaa (GCC 13).

Same change in the community fork: CreditsCommunity#11, running on mainnet community nodes.

When a block holding a timed delegation (expiry >= legate_min_utc) is
reverted, e.g. an uncertain last block removed by removeLastBlock(),
WalletsCache reverts the target side first, then the source side:

- revertDelegationsForTarget() erased the delegation from
  currentDelegations_[expiry] (std::find without a check, so erase(end())
  when it is missing). It left the miningDelegations_ record in place.
- revertDelegationsForSource() then called removeSingleDelegation(), which
  only refunds inside a loop over the OTHER delegations with the same expiry.
  The usual case is a single delegation per expiry timestamp, so the loop body
  never ran: the source was not refunded and kept the record in
  delegateTargets_.

When the block was applied again, the source was debited a second time. That
node then held the source balance short by the delegated amount for good,
plus a duplicate mining-delegation record. This is one of the ways honest
nodes diverge on balances (path E in the block 176'776'210 analysis).

Revert now mirrors apply:
- target: remove the delegation from currentDelegations_ and
  miningDelegations_ only if present, and drop empty entries;
- source: remove the matching TimeMoney (same expiry and amount) from
  delegateTargets_ and refund the amount directly. If the delegation has
  already expired, cleanObsoletteDelegations() refunded it and removed the
  record, so nothing is refunded twice.

Test: Staking.RevertOfTimedDelegationRefundsSource (apply, revert, apply
again) fails before the fix (source stays at 700 instead of 1000).
nodetests: 121/121 pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant