Repository navigation
csnode: make the revert of a timed delegation mirror its apply - #11
Merged
Merged
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 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.
Problem (path E of the divergence map)
When a block holding a timed delegation is reverted (e.g. an "uncertain" last block removed by
removeLastBlock()),WalletsCachereverts the target first, then the source:revertDelegationsForTarget()removed the delegation fromcurrentDelegations_[expiry]with an uncheckedstd::find(soerase(end())when missing), and left theminingDelegations_record in place.revertDelegationsForSource()calledremoveSingleDelegation(), which only refunds inside a loop over the other delegations with the same expiry. The usual case is a single delegation per expiry: the loop never ran and the source was not refunded.When the block was applied again, the source was debited a second time. On that node its balance stayed short by the delegated amount for good, and the mining record was duplicated.
Fix (
csnode/src/staking.cpp, +40/-7)The revert now mirrors the apply:
currentDelegations_andminingDelegations_only if present, and drop empty entries;TimeMoney(same expiry and amount) fromdelegateTargets_and refund the amount directly. If the delegation had already expired,cleanObsoletteDelegations()refunded and removed it, so nothing is refunded twice.removeSingleDelegation()is left without callers. It is not removed, to keep the diff small.Tests
New
tests/csnode/staking_tests.cpp: apply, revert and re-apply a timed delegation.nodetestsUpstream
A single commit that applies to CREDITSCOM/node (identical file).
🤖 Generated with Claude Code