diff --git a/csnode/include/csnode/blockchain.hpp b/csnode/include/csnode/blockchain.hpp index 0fe933dcf..768a8df4a 100644 --- a/csnode/include/csnode/blockchain.hpp +++ b/csnode/include/csnode/blockchain.hpp @@ -424,6 +424,14 @@ public slots: void setStartingDPOS(cs::Sequence sequence); cs::Sequence getStartingDPOS() const; // 0: never set by order 9 + // Payload of the last special order of each kind whose effect is held only in memory (version + // limits, consensus limits, MinStakeValue, syncroChangeRound), recorded when a block + // is applied to the caches, so before a checkpoint of that block is saved. A quick start replays + // them (Node::restoreSpecialOrders), as a slow start does when it reads the blocks. + using SpecialOrders = std::map; + void recordSpecialOrders(const csdb::Pool& pool); + const SpecialOrders& getSpecialOrders() const; + // 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); @@ -579,5 +587,6 @@ public slots: uint32_t TimeMinStage1_ = 500; PendingConsensusSettings pendingConsensusSettings_; cs::Sequence startingDPOS_ = 0; + SpecialOrders specialOrders_; }; #endif // BLOCKCHAIN_HPP diff --git a/csnode/include/csnode/blockchain_serializer.hpp b/csnode/include/csnode/blockchain_serializer.hpp index 7ffe3350c..26b6d975f 100644 --- a/csnode/include/csnode/blockchain_serializer.hpp +++ b/csnode/include/csnode/blockchain_serializer.hpp @@ -66,6 +66,7 @@ class BlockChain_Serializer { csdb::Amount *pendingBlockReward_; csdb::Amount *pendingMiningCoefficient_; cs::Sequence *startingDPOS_; + std::map *specialOrders_; }; } // namespace cs #endif // BLOCKCHAIN_SERIALIZER_HPP diff --git a/csnode/include/csnode/node.hpp b/csnode/include/csnode/node.hpp index 8fc230ee8..ae2055f0f 100644 --- a/csnode/include/csnode/node.hpp +++ b/csnode/include/csnode/node.hpp @@ -246,6 +246,8 @@ class Node { void saveConsensusSettingsToChain(); // puts back the order-37 change still pending and the order-9 StartingDPOS saved in the caches void restorePendingConsensusSettings(); + // replays the last special orders of each kind kept in the caches (BlockChain::getSpecialOrders) + void restoreSpecialOrders(); void getNodeRewardEvaluation(std::vector& request, std::string& msg, const cs::PublicKey& pKey, bool oneNode); @@ -387,6 +389,7 @@ public slots: // request current trusted nodes for block with specific sequence void sendBlockRequestToConfidants(cs::Sequence sequence); void processSpecialInfo(const csdb::Pool& pool); + void processSpecialOrder(const std::string& payload); // one special order, as carried by a special transaction // logs the wallet state digest every kStateDigestInterval blocks, comparable between nodes void logStateDigest(const csdb::Pool& pool); void logStateDigest(cs::Sequence sequence); diff --git a/csnode/src/blockchain.cpp b/csnode/src/blockchain.cpp index 71edef637..e2cccfb44 100644 --- a/csnode/src/blockchain.cpp +++ b/csnode/src/blockchain.cpp @@ -271,6 +271,7 @@ void BlockChain::onReadFromDB(csdb::Pool block, bool* shouldStop) { } updateNonEmptyBlocks(block); walletsCacheUpdater_->loadNextBlock(block, block.confidants(), *this); + recordSpecialOrders(block); } } @@ -841,6 +842,7 @@ bool BlockChain::applyBlockToCaches(const csdb::Pool& pool) { // update non-empty block storage updateNonEmptyBlocks(pool); + recordSpecialOrders(pool); } catch (std::exception & e) { cserror() << "apply block to caches, exception: " << e.what(); @@ -2387,6 +2389,30 @@ cs::Sequence BlockChain::getStartingDPOS() const { return startingDPOS_; } +void BlockChain::recordSpecialOrders(const csdb::Pool& pool) { + // 2 is kept as the initial confidants, 9 and 37 by setStartingDPOS / setPendingConsensusSettings, + // 23 by setTimeMinStage1, 32 and 33 by the smart-contract caches; 10 does nothing + static const std::set kRecorded = { 5, 11, 21, 22, 24, 25, 26, 27, 28, 29, 30, 31, 35 }; + for (const auto& t : pool.transactions()) { + if (!isSpecial(t)) { + continue; + } + const auto payload = t.user_field(cs::trx_uf::sp::managing).value(); + if (payload.size() < sizeof(uint16_t)) { + continue; + } + // Node::processSpecialInfo reads the order as a little-endian u16 + const auto order = static_cast(static_cast(payload[0]) | (static_cast(payload[1]) << 8)); + if (kRecorded.count(order) != 0) { + specialOrders_[order] = payload; + } + } +} + +const BlockChain::SpecialOrders& BlockChain::getSpecialOrders() const { + return specialOrders_; +} + void BlockChain::setTimeMinStage1(uint32_t timeStage1) { TimeMinStage1_ = timeStage1; } diff --git a/csnode/src/blockchain_serializer.cpp b/csnode/src/blockchain_serializer.cpp index 977891787..7bd2da180 100644 --- a/csnode/src/blockchain_serializer.cpp +++ b/csnode/src/blockchain_serializer.cpp @@ -7,6 +7,8 @@ #include #include +#include +#include #include #include @@ -16,7 +18,7 @@ namespace { const std::string kDataFileName = "blockchain.dat"; const std::string kPendingFileName = "consensus_pending.dat"; -constexpr uint8_t kPendingFormat = 1; +constexpr uint8_t kPendingFormat = 2; // 1: no special orders } // namespace namespace cs { @@ -41,6 +43,7 @@ void BlockChain_Serializer::bind(BlockChain& bchain, std::set& in pendingBlockReward_ = &bchain.pendingConsensusSettings_.blockReward; pendingMiningCoefficient_ = &bchain.pendingConsensusSettings_.miningCoefficient; startingDPOS_ = &bchain.startingDPOS_; + specialOrders_ = &bchain.specialOrders_; csdebug() << "Blockchain bindings made"; } @@ -92,6 +95,7 @@ void BlockChain_Serializer::savePending(const std::filesystem::path& rootDir) { oa << pendingBlockReward_->integral() << pendingBlockReward_->fraction(); oa << pendingMiningCoefficient_->integral() << pendingMiningCoefficient_->fraction(); oa << *startingDPOS_; + oa << *specialOrders_; } void BlockChain_Serializer::loadPending(const std::filesystem::path& rootDir) { @@ -104,7 +108,7 @@ void BlockChain_Serializer::loadPending(const std::filesystem::path& rootDir) { boost::archive::binary_iarchive ia(ifs); uint8_t format = 0; ia >> format; - if (format != kPendingFormat) { + if (format != 1 && format != kPendingFormat) { cswarning() << "BlockChain_Serializer: unknown " << kPendingFileName << " format " << int(format) << ", ignored"; return; } @@ -115,6 +119,9 @@ void BlockChain_Serializer::loadPending(const std::filesystem::path& rootDir) { ia >> rewardIntegral >> rewardFraction; ia >> coefficientIntegral >> coefficientFraction; ia >> *startingDPOS_; + if (format >= 2) { + ia >> *specialOrders_; + } *pendingBlockReward_ = csdb::Amount(rewardIntegral, rewardFraction); *pendingMiningCoefficient_ = csdb::Amount(coefficientIntegral, coefficientFraction); } @@ -126,6 +133,7 @@ void BlockChain_Serializer::resetPending() { *pendingBlockReward_ = csdb::Amount{0}; *pendingMiningCoefficient_ = csdb::Amount{0}; *startingDPOS_ = 0; + specialOrders_->clear(); } ::cscrypto::Hash BlockChain_Serializer::hash() { diff --git a/csnode/src/node.cpp b/csnode/src/node.cpp index 3b43ccd4c..01f5cfc38 100644 --- a/csnode/src/node.cpp +++ b/csnode/src/node.cpp @@ -401,6 +401,8 @@ void Node::onSuccessQS(csdb::Amount blockReward, csdb::Amount miningCoeff, bool } void Node::restorePendingConsensusSettings() { + restoreSpecialOrders(); + if (const auto startingDPOS = blockChain_.getStartingDPOS(); startingDPOS != 0) { Consensus::StartingDPOS = startingDPOS; csinfo() << "StartingDPOS restored from caches: " << Consensus::StartingDPOS; @@ -428,6 +430,18 @@ void Node::restorePendingConsensusSettings() { } } +void Node::restoreSpecialOrders() { + // the effect of these orders is held only in memory; a slow start rebuilds it by reading the blocks + const auto& orders = blockChain_.getSpecialOrders(); + if (orders.empty()) { + return; + } + csinfo() << "Restoring " << orders.size() << " special order(s) from caches"; + for (const auto& entry : orders) { + processSpecialOrder(entry.second); + } +} + void Node::getUtilityMessage(const uint8_t* data, const size_t size) { cswarning() << "NODE> Utility message get"; @@ -4107,7 +4121,18 @@ void Node::processSpecialInfo(const csdb::Pool& pool) { continue; } else { - auto stringBytes = it.user_field(cs::trx_uf::sp::managing).value(); + processSpecialOrder(it.user_field(cs::trx_uf::sp::managing).value()); + } + } + std::string msg; + checkNodeVersion(pool.sequence(), msg); + checkConsensusSettings(pool.sequence(), msg); + if (!msg.empty()) { + cslog() << msg; + } +} + +void Node::processSpecialOrder(const std::string& stringBytes) { std::vector msg(stringBytes.begin(), stringBytes.end()); cs::IDataStream stream(msg.data(), msg.size()); uint16_t order; @@ -4117,7 +4142,7 @@ void Node::processSpecialInfo(const csdb::Pool& pool) { uint8_t cnt; stream >> cnt; if (size_t(cnt) < Consensus::MinTrustedNodes) { - continue; + return; } cslog() << "New bootstrap nodes: "; initialConfidants_.clear(); @@ -4296,15 +4321,6 @@ void Node::processSpecialInfo(const csdb::Pool& pool) { << "\n blockReward " << blockReward_.to_string() << "\n miningCoefficient " << miningCoefficient_.to_string(); } - - } - } - std::string msg; - checkNodeVersion(pool.sequence(), msg); - checkConsensusSettings(pool.sequence(), msg); - if (!msg.empty()) { - cslog() << msg; - } } void Node::saveConsensusSettingsToChain() { diff --git a/tests/csnode/blockchain_tests.cpp b/tests/csnode/blockchain_tests.cpp index a001dbd27..189697340 100644 --- a/tests/csnode/blockchain_tests.cpp +++ b/tests/csnode/blockchain_tests.cpp @@ -10,7 +10,10 @@ #include #include +#include +#include #include +#include TEST(BlockChain, block_service_info) { csdb::Pool block{}; @@ -123,3 +126,63 @@ TEST(BlockChain, PendingConsensusSettingsSurviveCheckpoint) { std::filesystem::remove_all(dir); } + +namespace { +// a transaction carrying a special order: u16 order number (little endian) then its fields +csdb::Transaction specialOrderTransaction(int64_t id, uint16_t order, uint64_t value) { + csdb::Transaction transaction{id, csdb::Address::from_public_key(cs::PublicKey{}), csdb::Address::from_public_key(cs::PublicKey{}), + csdb::Currency{1}, csdb::Amount{0}, csdb::AmountCommission{0.}, csdb::AmountCommission{0.}, cs::Signature{}}; + std::string payload(reinterpret_cast(&order), sizeof(order)); + payload.append(reinterpret_cast(&value), sizeof(value)); + transaction.add_user_field(cs::trx_uf::sp::managing, payload); + return transaction; +} + +uint64_t orderValue(const std::string& payload) { + uint64_t value = 0; + std::copy(payload.begin() + sizeof(uint16_t), payload.end(), reinterpret_cast(&value)); + return value; +} +} // namespace + +TEST(BlockChain, SpecialOrdersSurviveCheckpoint) { + 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_special_orders_test"; + std::filesystem::remove_all(dir); + std::filesystem::create_directories(dir); + + std::set confidants; + { + BlockChain saved(genesis, start); + csdb::Pool first; + first.add_transaction(specialOrderTransaction(1, 26, 1000)); // MaxTransactionSize + first.add_transaction(specialOrderTransaction(2, 35, 777)); // syncroChangeRound + first.add_transaction(specialOrderTransaction(3, 37, 1)); // kept by the pending settings instead + saved.recordSpecialOrders(first); + csdb::Pool second; + second.add_transaction(specialOrderTransaction(4, 26, 2000)); // a later order of the same kind wins + saved.recordSpecialOrders(second); + ASSERT_EQ(saved.getSpecialOrders().size(), 2u); + + 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); + const auto& orders = loaded.getSpecialOrders(); + ASSERT_EQ(orders.size(), 2u); + ASSERT_EQ(orderValue(orders.at(26)), 2000u); + ASSERT_EQ(orderValue(orders.at(35)), 777u); + + // older checkpoints have no companion file: nothing to replay + std::filesystem::remove(dir / "consensus_pending.dat"); + serializer.load(dir); + ASSERT_TRUE(loaded.getSpecialOrders().empty()); + + std::filesystem::remove_all(dir); +}