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); +}