From 1ca8445893d96f23bfe907bbb3bfbee45ab182ab Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Thu, 8 Oct 2026 13:29:31 +0200 Subject: [PATCH] csnode: don't keep an uncertain block's rewards when the trusted mask differs When a node's own last block turns out to be "uncertain" (its hash is not the one the network signed), BlockChain::storeBlock() fetches the canonical block. If testContentEqual() says the two have the same content, the node swaps it in place (deferredBlock_ = pool) without touching the wallet caches. testContentEqual() ignored the trusted mask (numberTrusted/realTrusted). That mask decides which confidants receive the block reward and the fees (WalletsCache::Updater: fundConfidantsWalletsWitnReward, fundConfidantsWalletsWithFee). So a trusted node that built block N with its own mask (e.g. a confidant whose stage-3 it lost marked as untrusted) kept the rewards and fees credited under that mask, while every other node credits them under the canonical mask. The difference is permanent and silent, and it accumulates on the wallets of frequent trusted nodes. This is a plausible source of the balance divergence that left nodes stuck at block 176'776'210 (a validator withdrawing its rewards was rejected by "graph validation" on nodes holding a lower balance). Include the trusted mask in the compared content. When it differs, storeBlock takes the existing "different content" branch: removeLastBlock() reverts the own block from the caches with its own mask, and the canonical block is then applied through the normal sync path. No protocol or consensus rule changes. testContentEqual() is made public so it can be unit-tested. Test: BlockChain.ContentEqualRequiresSameTrustedMask fails before the fix. nodetests: 119/119 pass. Co-Authored-By: Claude Opus 5.5 --- csnode/include/csnode/blockchain.hpp | 8 ++++---- csnode/src/blockchain.cpp | 2 ++ tests/csnode/blockchain_tests.cpp | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/csnode/include/csnode/blockchain.hpp b/csnode/include/csnode/blockchain.hpp index fe82eedd0..a40ef8fb8 100644 --- a/csnode/include/csnode/blockchain.hpp +++ b/csnode/include/csnode/blockchain.hpp @@ -408,6 +408,10 @@ public slots: void setStakingOn(bool stOn); void setTimeMinStage1(uint32_t timeStage1); + // compare only state content: transactions, new wallets, sequence, round fee, user fields, trusted mask + // true if both pools are not valid, or both pools have equal state content + static bool testContentEqual(const csdb::Pool& lhs, const csdb::Pool& rhs); + private: void createCachesPath(); @@ -545,10 +549,6 @@ public slots: cs::CachesSerializationManager* serializationManPtr_ = nullptr; - // compare only state content: transactions, new wallets, sequence, round fee, user fields - // true if both pools are not valid, or both pools have equal state content - static bool testContentEqual(const csdb::Pool& lhs, const csdb::Pool& rhs); - friend class cs::BlockChain_Serializer; static constexpr size_t kQuickStartSaveCachesInterval = 500'000; diff --git a/csnode/src/blockchain.cpp b/csnode/src/blockchain.cpp index 274d3a30c..0032ec5e9 100644 --- a/csnode/src/blockchain.cpp +++ b/csnode/src/blockchain.cpp @@ -2139,6 +2139,8 @@ namespace { for (const auto& it : confidants) { out << it; } + // rewards and fees are credited only to the confidants marked as trusted + out << block.numberTrusted() << block.realTrusted(); out << block.previous_hash(); } } diff --git a/tests/csnode/blockchain_tests.cpp b/tests/csnode/blockchain_tests.cpp index d2fdf23d1..49e471989 100644 --- a/tests/csnode/blockchain_tests.cpp +++ b/tests/csnode/blockchain_tests.cpp @@ -27,4 +27,18 @@ TEST(BlockChain, block_service_info) { ASSERT_TRUE(BlockChain::isBootstrap(block)); BlockChain::setBootstrap(block, false); ASSERT_FALSE(BlockChain::isBootstrap(block)); -} \ No newline at end of file +} +// an "uncertain" own block is replaced in place by the network's version only when testContentEqual() holds; +// rewards and fees are credited through the trusted mask, so a different mask must not count as equal content +TEST(BlockChain, ContentEqualRequiresSameTrustedMask) { + csdb::Pool own; + own.set_sequence(10); + own.add_number_trusted(3); + own.add_real_trusted(0); + + csdb::Pool canonical = own.clone(); + ASSERT_TRUE(BlockChain::testContentEqual(own, canonical)); + + canonical.add_real_trusted(cs::Utils::maskToBits(cs::Bytes{ 0, 255, 1 })); + ASSERT_FALSE(BlockChain::testContentEqual(own, canonical)); +}