From 92449cccc299313668d3018b9e0c373035a65876 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Fri, 9 Oct 2026 07:42:22 +0200 Subject: [PATCH] csnode: make the revert of a timed delegation mirror its apply 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 --- csnode/src/staking.cpp | 47 +++++++++++++++++++---- tests/csnode/staking_tests.cpp | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 tests/csnode/staking_tests.cpp diff --git a/csnode/src/staking.cpp b/csnode/src/staking.cpp index 7859b656f..647eeeb50 100644 --- a/csnode/src/staking.cpp +++ b/csnode/src/staking.cpp @@ -302,11 +302,31 @@ void Staking::revertDelegationsForTarget( if (it != timeMoneyVector.end()) { timeMoneyVector.erase(it); targetWallet.delegated_ -= amount; - currentDelegations_[tm.time].erase(std::find( - currentDelegations_[tm.time].begin(), - currentDelegations_[tm.time].end(), - std::make_tuple(sKey, tKey, trx_id) - )); + + // mirror of addDelegationsForTarget: forget the expiry and mining records of this delegation + auto timing = currentDelegations_.find(tm.time); + if (timing != currentDelegations_.end()) { + auto entry = std::find(timing->second.begin(), timing->second.end(), std::make_tuple(sKey, tKey, trx_id)); + if (entry != timing->second.end()) { + timing->second.erase(entry); + } + if (timing->second.empty()) { + currentDelegations_.erase(timing); + } + } + + auto mining = miningDelegations_.find(tKey); + if (mining != miningDelegations_.end()) { + auto record = std::find_if(mining->second.begin(), mining->second.end(), [&sKey, &tm](const auto& keyAndTimeMoney) { + return keyAndTimeMoney.first == sKey && keyAndTimeMoney.second.time == tm.time && keyAndTimeMoney.second.amount == tm.amount; + }); + if (record != mining->second.end()) { + mining->second.erase(record); + } + if (mining->second.empty()) { + miningDelegations_.erase(mining); + } + } } } else { @@ -435,8 +455,21 @@ void Staking::revertDelegationsForSource( } } else if (ufld.value() >= trx_uf::sp::de::legate_min_utc) { - //nothing to do if time of this transacton is already finished - removeSingleDelegation(ufld.value(), sKey, tKey, trx_id); + // mirror of addDelegationsForSource; if the delegation has already expired, the source was refunded + // by cleanObsoletteDelegations() and the record is gone, so there is nothing to give back + const uint64_t delTime = ufld.value(); + if (it != wallData.delegateTargets_->end()) { + auto itt = std::find_if(it->second.begin(), it->second.end(), [delTime, &amount](const cs::TimeMoney& tm) { + return tm.time == delTime && tm.amount == amount; + }); + if (itt != it->second.end()) { + it->second.erase(itt); + wallData.balance_ += amount; + if (it->second.empty()) { + wallData.delegateTargets_->erase(it); + } + } + } } else { cserror() << "Staking: error revertDelegationsForSource"; diff --git a/tests/csnode/staking_tests.cpp b/tests/csnode/staking_tests.cpp new file mode 100644 index 000000000..769e69e05 --- /dev/null +++ b/tests/csnode/staking_tests.cpp @@ -0,0 +1,68 @@ +#include + +#include + +#include +#include + +// a block with a timed delegation may be reverted (e.g. an uncertain last block) and applied again; +// WalletsCache reverts the target side first, then the source side +namespace { +struct Wallets { + std::map data; + + cs::WalletsCache::WalletData get(const cs::PublicKey& key) { + auto& w = data[key]; + w.key_ = key; + return w; + } + void put(const cs::WalletsCache::WalletData& w) { + data[w.key_] = w; + } +}; + +cs::PublicKey key(uint8_t b) { + cs::PublicKey k{}; + k.fill(b); + return k; +} + +constexpr uint64_t kStartMs = 1'700'000'000'000ULL; // block time, ms +#define ASSERT_AMOUNT(actual, expected) ASSERT_TRUE((actual) == (expected)) << (actual).to_string() + +constexpr uint64_t kExpiry = 1'700'000'000ULL + 10'000'000; // delegation end, s (> 90 days ahead) +} // namespace + +TEST(Staking, RevertOfTimedDelegationRefundsSource) { + Wallets wallets; + cs::Staking staking([&](const cs::PublicKey& k) { return wallets.get(k); }, + [&](const cs::WalletsCache::WalletData& w) { wallets.put(w); }); + + const auto source = key(1); + const auto target = key(2); + wallets.data[source].balance_ = csdb::Amount{ 1000 }; + + const csdb::UserField timed{ kExpiry }; + const csdb::Amount amount{ 300 }; + const csdb::TransactionID id(100, 0); + + staking.addDelegationsForTarget(timed, source, target, amount, id, kStartMs); + staking.addDelegationsForSource(timed, source, target, amount, kStartMs); + ASSERT_AMOUNT(wallets.data[source].balance_, csdb::Amount{ 700 }); + ASSERT_AMOUNT(wallets.data[target].delegated_, amount); + + staking.revertDelegationsForTarget(timed, source, target, amount, id, kStartMs); + staking.revertDelegationsForSource(timed, source, target, amount, id, kStartMs); + + ASSERT_AMOUNT(wallets.data[source].balance_, csdb::Amount{ 1000 }); + ASSERT_AMOUNT(wallets.data[target].delegated_, csdb::Amount{ 0 }); + ASSERT_TRUE(staking.getCurrentDelegations().empty() || staking.getCurrentDelegations().begin()->second.empty()); + ASSERT_EQ(staking.getMiningDelegations(target), nullptr); + + // applying the block again must give the same state as the first time + staking.addDelegationsForTarget(timed, source, target, amount, id, kStartMs); + staking.addDelegationsForSource(timed, source, target, amount, kStartMs); + ASSERT_AMOUNT(wallets.data[source].balance_, csdb::Amount{ 700 }); + ASSERT_AMOUNT(wallets.data[target].delegated_, amount); + ASSERT_EQ(staking.getMiningDelegations(target)->size(), 1u); +}