Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why this PR
When a block holding a timed delegation (expiry >=
legate_min_utc) is reverted — for instance an uncertain last block removed byremoveLastBlock()—WalletsCachereverts the target side, then the source side, and neither mirrors the apply:revertDelegationsForTarget()erases the delegation fromcurrentDelegations_[expiry]with an uncheckedstd::find(erasingend()when it is missing) and leaves theminingDelegations_record in place;revertDelegationsForSource()callsremoveSingleDelegation(), 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 indelegateTargets_.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:currentDelegations_andminingDelegations_only if present, and empty entries are dropped;TimeMoney(same expiry and amount) is removed fromdelegateTargets_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.