Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions csnode/src/staking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -435,8 +455,21 @@ void Staking::revertDelegationsForSource(
}
}
else if (ufld.value<uint64_t>() >= trx_uf::sp::de::legate_min_utc) {
//nothing to do if time of this transacton is already finished
removeSingleDelegation(ufld.value<uint64_t>(), 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<uint64_t>();
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";
Expand Down
68 changes: 68 additions & 0 deletions tests/csnode/staking_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <gtest/gtest.h>

#include <map>

#include <csnode/nodecore.hpp>
#include <csnode/staking.hpp>

// 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<cs::PublicKey, cs::WalletsCache::WalletData> 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);
}
Loading