From d2a78494e687876036d2ab6c92402f4c4bbeeca8 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Fri, 9 Oct 2026 14:46:24 +0200 Subject: [PATCH] csnode: keep pending consensus-settings changes across a quick start Special order 37 announces new reward settings (staking/mining flags, block reward, mining coefficient) that take effect at a given round. Until that round the change lives only in Node's memory (consensusSettingsChangingRound_ and friends). Order 9 sets Consensus::StartingDPOS, also in memory only. A node restarted from quick-start caches between the order and its activation therefore lost both: it kept blockReward 0, dropped every trusted reward (WalletsCache caps rewards at the local blockReward), and its balances drifted from the network. This was reproduced on the private test network. A node restarted after the order but before activation logged "Changing consensus settings after qs to: blockReward = 0.00", then "confidant reward ... dropped (local blockReward 0.00)", and its STATE DIGEST at block 1000 differed from the six other nodes. It is the mechanism that leaves a node unable to validate a validator's reward withdrawal (cf. block 176'776'210). - BlockChain keeps the pending change and the order-9 StartingDPOS. Node updates them when it processes orders 37 and 9, and clears the pending change when it activates. - BlockChain_Serializer writes them to an optional consensus_pending.dat next to blockchain.dat. It is not part of the checkpoint hash, and a checkpoint without it loads exactly as before (nothing pending, StartingDPOS unchanged), so existing checkpoints stay valid and no node is forced into a slow start. - After a quick start (and on the --set-bc-top path) Node restores them. A checkpoint is written before the special-info handlers of its own block run, so if the activation round is already behind the change is applied immediately. Round 0, which never activates live, is treated as nothing pending. Test: BlockChain.PendingConsensusSettingsSurviveCheckpoint (round trip, plus a checkpoint without the companion file). nodetests: 133/133 pass. Co-Authored-By: Claude Opus 5.5 --- csnode/include/csnode/blockchain.hpp | 18 ++++++ .../include/csnode/blockchain_serializer.hpp | 12 ++++ csnode/include/csnode/node.hpp | 2 + csnode/src/blockchain.cpp | 16 +++++ csnode/src/blockchain_serializer.cpp | 58 +++++++++++++++++++ csnode/src/node.cpp | 33 +++++++++++ tests/csnode/blockchain_tests.cpp | 48 +++++++++++++++ 7 files changed, 187 insertions(+) diff --git a/csnode/include/csnode/blockchain.hpp b/csnode/include/csnode/blockchain.hpp index 348aea307..0fe933dcf 100644 --- a/csnode/include/csnode/blockchain.hpp +++ b/csnode/include/csnode/blockchain.hpp @@ -2,6 +2,7 @@ #define BLOCKCHAIN_HPP #include +#include #include #include #include @@ -408,6 +409,21 @@ public slots: void setStakingOn(bool stOn); void setTimeMinStage1(uint32_t timeStage1); + // Consensus-settings change announced by special order 37 but not active yet, and StartingDPOS + // set by order 9. Node owns this state; it is kept here so that quick-start checkpoints carry it + // (BlockChain_Serializer), otherwise a node restarted before the activation round never applies it. + struct PendingConsensusSettings { + cs::Sequence round = std::numeric_limits::max(); // max: nothing pending + bool stakingOn = false; + bool miningOn = false; + csdb::Amount blockReward{0}; + csdb::Amount miningCoefficient{0}; + }; + void setPendingConsensusSettings(const PendingConsensusSettings& pending); + PendingConsensusSettings getPendingConsensusSettings() const; + void setStartingDPOS(cs::Sequence sequence); + cs::Sequence getStartingDPOS() const; // 0: never set by order 9 + // 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); @@ -561,5 +577,7 @@ public slots: bool stakingOn_ = false; bool miningOn_ = false; uint32_t TimeMinStage1_ = 500; + PendingConsensusSettings pendingConsensusSettings_; + cs::Sequence startingDPOS_ = 0; }; #endif // BLOCKCHAIN_HPP diff --git a/csnode/include/csnode/blockchain_serializer.hpp b/csnode/include/csnode/blockchain_serializer.hpp index cecd1c7ab..7ffe3350c 100644 --- a/csnode/include/csnode/blockchain_serializer.hpp +++ b/csnode/include/csnode/blockchain_serializer.hpp @@ -54,6 +54,18 @@ class BlockChain_Serializer { bool *stakingOn_; bool *miningOn_; uint32_t *TimeMinStage1_; + + // optional companion file: checkpoints written before it existed simply load without it + void savePending(const std::filesystem::path& rootDir); + void loadPending(const std::filesystem::path& rootDir); + void resetPending(); + + cs::Sequence *pendingRound_; + bool *pendingStakingOn_; + bool *pendingMiningOn_; + csdb::Amount *pendingBlockReward_; + csdb::Amount *pendingMiningCoefficient_; + cs::Sequence *startingDPOS_; }; } // namespace cs #endif // BLOCKCHAIN_SERIALIZER_HPP diff --git a/csnode/include/csnode/node.hpp b/csnode/include/csnode/node.hpp index d26053670..8fc230ee8 100644 --- a/csnode/include/csnode/node.hpp +++ b/csnode/include/csnode/node.hpp @@ -244,6 +244,8 @@ class Node { uint8_t requestKBAnswer(std::vector choice); void onSuccessQS(csdb::Amount blockReward, csdb::Amount miningCoeff, bool miningOn, bool stakingOn, uint32_t stageOneHashesTime); void saveConsensusSettingsToChain(); + // puts back the order-37 change still pending and the order-9 StartingDPOS saved in the caches + void restorePendingConsensusSettings(); void getNodeRewardEvaluation(std::vector& request, std::string& msg, const cs::PublicKey& pKey, bool oneNode); diff --git a/csnode/src/blockchain.cpp b/csnode/src/blockchain.cpp index 8ac52558b..71edef637 100644 --- a/csnode/src/blockchain.cpp +++ b/csnode/src/blockchain.cpp @@ -2371,6 +2371,22 @@ void BlockChain::setMiningOn(bool mOn) { void BlockChain::setStakingOn(bool stOn) { stakingOn_ = stOn; } +void BlockChain::setPendingConsensusSettings(const PendingConsensusSettings& pending) { + pendingConsensusSettings_ = pending; +} + +BlockChain::PendingConsensusSettings BlockChain::getPendingConsensusSettings() const { + return pendingConsensusSettings_; +} + +void BlockChain::setStartingDPOS(cs::Sequence sequence) { + startingDPOS_ = sequence; +} + +cs::Sequence BlockChain::getStartingDPOS() const { + return startingDPOS_; +} + void BlockChain::setTimeMinStage1(uint32_t timeStage1) { TimeMinStage1_ = timeStage1; } diff --git a/csnode/src/blockchain_serializer.cpp b/csnode/src/blockchain_serializer.cpp index d2c275f65..977891787 100644 --- a/csnode/src/blockchain_serializer.cpp +++ b/csnode/src/blockchain_serializer.cpp @@ -1,4 +1,5 @@ #include +#include #include //#include @@ -14,6 +15,8 @@ namespace { const std::string kDataFileName = "blockchain.dat"; +const std::string kPendingFileName = "consensus_pending.dat"; +constexpr uint8_t kPendingFormat = 1; } // namespace namespace cs { @@ -32,6 +35,12 @@ void BlockChain_Serializer::bind(BlockChain& bchain, std::set& in stakingOn_ = &bchain.stakingOn_; miningOn_ = &bchain.miningOn_; TimeMinStage1_ = &bchain.TimeMinStage1_; + pendingRound_ = &bchain.pendingConsensusSettings_.round; + pendingStakingOn_ = &bchain.pendingConsensusSettings_.stakingOn; + pendingMiningOn_ = &bchain.pendingConsensusSettings_.miningOn; + pendingBlockReward_ = &bchain.pendingConsensusSettings_.blockReward; + pendingMiningCoefficient_ = &bchain.pendingConsensusSettings_.miningCoefficient; + startingDPOS_ = &bchain.startingDPOS_; csdebug() << "Blockchain bindings made"; } @@ -52,6 +61,7 @@ void BlockChain_Serializer::clear(const std::filesystem::path& rootDir) { *stakingOn_ = false; *miningOn_ = false; *TimeMinStage1_ = 500; + resetPending(); } void BlockChain_Serializer::save(const std::filesystem::path& rootDir) { @@ -70,12 +80,59 @@ void BlockChain_Serializer::save(const std::filesystem::path& rootDir) { uint8_t miningOn = *miningOn_ ? 0U : 1U; oa << miningOn; oa << *TimeMinStage1_; + savePending(rootDir); +} + +void BlockChain_Serializer::savePending(const std::filesystem::path& rootDir) { + std::ofstream ofs(rootDir / kPendingFileName, std::ios::binary); + boost::archive::binary_oarchive oa(ofs); + oa << kPendingFormat; + oa << *pendingRound_; + oa << *pendingStakingOn_ << *pendingMiningOn_; + oa << pendingBlockReward_->integral() << pendingBlockReward_->fraction(); + oa << pendingMiningCoefficient_->integral() << pendingMiningCoefficient_->fraction(); + oa << *startingDPOS_; +} + +void BlockChain_Serializer::loadPending(const std::filesystem::path& rootDir) { + resetPending(); + const auto path = rootDir / kPendingFileName; + if (!std::filesystem::exists(path)) { + return; + } + std::ifstream ifs(path, std::ios::binary); + boost::archive::binary_iarchive ia(ifs); + uint8_t format = 0; + ia >> format; + if (format != kPendingFormat) { + cswarning() << "BlockChain_Serializer: unknown " << kPendingFileName << " format " << int(format) << ", ignored"; + return; + } + int32_t rewardIntegral = 0, coefficientIntegral = 0; + uint64_t rewardFraction = 0, coefficientFraction = 0; + ia >> *pendingRound_; + ia >> *pendingStakingOn_ >> *pendingMiningOn_; + ia >> rewardIntegral >> rewardFraction; + ia >> coefficientIntegral >> coefficientFraction; + ia >> *startingDPOS_; + *pendingBlockReward_ = csdb::Amount(rewardIntegral, rewardFraction); + *pendingMiningCoefficient_ = csdb::Amount(coefficientIntegral, coefficientFraction); +} + +void BlockChain_Serializer::resetPending() { + *pendingRound_ = std::numeric_limits::max(); + *pendingStakingOn_ = false; + *pendingMiningOn_ = false; + *pendingBlockReward_ = csdb::Amount{0}; + *pendingMiningCoefficient_ = csdb::Amount{0}; + *startingDPOS_ = 0; } ::cscrypto::Hash BlockChain_Serializer::hash() { save("."); auto result = SerializersHelper::getHashFromFile(kDataFileName); std::filesystem::remove(kDataFileName); + std::filesystem::remove(kPendingFileName); // written by save(), not part of the checkpoint hash return result; } @@ -108,5 +165,6 @@ void BlockChain_Serializer::load(const std::filesystem::path& rootDir) { *miningOn_ = miningOn == 0U ? true : false; ia >> *TimeMinStage1_; + loadPending(rootDir); } } // namespace cs diff --git a/csnode/src/node.cpp b/csnode/src/node.cpp index 6f1f5d7da..3b43ccd4c 100644 --- a/csnode/src/node.cpp +++ b/csnode/src/node.cpp @@ -197,6 +197,7 @@ bool Node::init() { Consensus::miningOn = blockChain_.getMiningOn(); Consensus::blockReward = blockChain_.getBlockReward(); Consensus::miningCoefficient = blockChain_.getMiningCoefficient(); + restorePendingConsensusSettings(); std::string miningStr = Consensus::miningOn ? "true" : "false"; std::string stakingStr = Consensus::stakingOn ? "true" : "false"; @@ -396,6 +397,35 @@ void Node::onSuccessQS(csdb::Amount blockReward, csdb::Amount miningCoeff, bool csinfo() << curMsg; csinfo() << "Number of contracts: " << solver_->smart_contracts().contracts_count(); //csinfo() << "Test value = " << solver_->smart_contracts().getTestValue(); + restorePendingConsensusSettings(); +} + +void Node::restorePendingConsensusSettings() { + if (const auto startingDPOS = blockChain_.getStartingDPOS(); startingDPOS != 0) { + Consensus::StartingDPOS = startingDPOS; + csinfo() << "StartingDPOS restored from caches: " << Consensus::StartingDPOS; + } + + const auto pending = blockChain_.getPendingConsensusSettings(); + // round 0 never matches a block sequence, so such an order is never applied live either + if (pending.round == std::numeric_limits::max() || pending.round == 0) { + return; + } + consensusSettingsChangingRound_ = pending.round; + stakingOn_ = pending.stakingOn; + miningOn_ = pending.miningOn; + blockReward_ = pending.blockReward; + miningCoefficient_ = pending.miningCoefficient; + csinfo() << "Pending consensus settings restored from caches, active from round " << pending.round + << ": blockReward " << blockReward_.to_string(); + + // a checkpoint is written before the special-info handlers of its own block run, so the + // activation round may already be behind: apply the change now instead of waiting forever + if (pending.round <= blockChain_.getLastSeq()) { + std::string msg; + checkConsensusSettings(pending.round, msg); + csinfo() << msg; + } } void Node::getUtilityMessage(const uint8_t* data, const size_t size) { @@ -4115,6 +4145,7 @@ void Node::processSpecialInfo(const csdb::Pool& pool) { cs::Sequence startDPOSSequence; stream >> startDPOSSequence; Consensus::StartingDPOS = startDPOSSequence; + blockChain_.setStartingDPOS(startDPOSSequence); cslog() << "StartingDPOS sequrnce changed to: " << Consensus::StartingDPOS; } @@ -4258,6 +4289,7 @@ void Node::processSpecialInfo(const csdb::Pool& pool) { miningOn_ = (sign == 3 || sign == 1) ? true : false; blockReward_ = csdb::Amount(rewInt, rewFrac); miningCoefficient_ = csdb::Amount(coeffInt, coefFrac); + blockChain_.setPendingConsensusSettings({ consensusSettingsChangingRound_, stakingOn_, miningOn_, blockReward_, miningCoefficient_ }); cslog() << "Mining settings will be changed in round " << consensusSettingsChangingRound_ << ": \n staking " << (stakingOn_ ? "ON" : "OFF") << "\n mining " << (miningOn_ ? "ON" : "OFF") @@ -4288,6 +4320,7 @@ void Node::checkConsensusSettings(cs::Sequence seq, std::string& msg){ return; } consensusSettingsChangingRound_ = ULLONG_MAX; + blockChain_.setPendingConsensusSettings({}); Consensus::stakingOn = stakingOn_; Consensus::miningOn = miningOn_; Consensus::blockReward = blockReward_; diff --git a/tests/csnode/blockchain_tests.cpp b/tests/csnode/blockchain_tests.cpp index 66c9a1833..a001dbd27 100644 --- a/tests/csnode/blockchain_tests.cpp +++ b/tests/csnode/blockchain_tests.cpp @@ -3,7 +3,12 @@ #include "clientconfigmock.hpp" #include "gtest/gtest.h" +#include +#include +#include + #include +#include #include @@ -75,3 +80,46 @@ TEST(BlockChain, ConsensusSettingsHaveDefinedDefaults) { ASSERT_FALSE(blockChain.getStakingOn()); ASSERT_EQ(blockChain.getTimeMinStage1(), 500u); } + +// a pending order-37 change and the order-9 StartingDPOS must survive a quick-start checkpoint, +// and a checkpoint written before this was saved must still load +TEST(BlockChain, PendingConsensusSettingsSurviveCheckpoint) { + const csdb::Address genesis = csdb::Address::from_string("0000000000000000000000000000000000000000000000000000000000000001"); + const csdb::Address start = csdb::Address::from_string("0000000000000000000000000000000000000000000000000000000000000002"); + const auto dir = std::filesystem::temp_directory_path() / "cs_pending_settings_test"; + std::filesystem::remove_all(dir); + std::filesystem::create_directories(dir); + + std::set confidants; + { + BlockChain saved(genesis, start); + BlockChain::PendingConsensusSettings pending; + pending.round = 400; + pending.stakingOn = true; + pending.miningOn = true; + pending.blockReward = csdb::Amount{ 1 }; + saved.setPendingConsensusSettings(pending); + saved.setStartingDPOS(450); + + cs::BlockChain_Serializer serializer; + serializer.bind(saved, confidants); + serializer.save(dir); + } + + BlockChain loaded(genesis, start); + cs::BlockChain_Serializer serializer; + serializer.bind(loaded, confidants); + serializer.load(dir); + ASSERT_EQ(loaded.getPendingConsensusSettings().round, 400u); + ASSERT_TRUE(loaded.getPendingConsensusSettings().miningOn); + ASSERT_TRUE(loaded.getPendingConsensusSettings().blockReward == csdb::Amount{ 1 }); + ASSERT_EQ(loaded.getStartingDPOS(), 450u); + + // older checkpoints have no companion file: nothing pending, StartingDPOS untouched + std::filesystem::remove(dir / "consensus_pending.dat"); + serializer.load(dir); + ASSERT_EQ(loaded.getPendingConsensusSettings().round, std::numeric_limits::max()); + ASSERT_EQ(loaded.getStartingDPOS(), 0u); + + std::filesystem::remove_all(dir); +}