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
9 changes: 9 additions & 0 deletions csnode/include/csnode/blockchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t, std::string>;
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);
Expand Down Expand Up @@ -579,5 +587,6 @@ public slots:
uint32_t TimeMinStage1_ = 500;
PendingConsensusSettings pendingConsensusSettings_;
cs::Sequence startingDPOS_ = 0;
SpecialOrders specialOrders_;
};
#endif // BLOCKCHAIN_HPP
1 change: 1 addition & 0 deletions csnode/include/csnode/blockchain_serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class BlockChain_Serializer {
csdb::Amount *pendingBlockReward_;
csdb::Amount *pendingMiningCoefficient_;
cs::Sequence *startingDPOS_;
std::map<uint16_t, std::string> *specialOrders_;
};
} // namespace cs
#endif // BLOCKCHAIN_SERIALIZER_HPP
3 changes: 3 additions & 0 deletions csnode/include/csnode/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<api_diag::NodeRewardSet>& request, std::string& msg, const cs::PublicKey& pKey, bool oneNode);

Expand Down Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions csnode/src/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ void BlockChain::onReadFromDB(csdb::Pool block, bool* shouldStop) {
}
updateNonEmptyBlocks(block);
walletsCacheUpdater_->loadNextBlock(block, block.confidants(), *this);
recordSpecialOrders(block);
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<uint16_t> 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<std::string>();
if (payload.size() < sizeof(uint16_t)) {
continue;
}
// Node::processSpecialInfo reads the order as a little-endian u16
const auto order = static_cast<uint16_t>(static_cast<uint8_t>(payload[0]) | (static_cast<uint8_t>(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;
}
12 changes: 10 additions & 2 deletions csnode/src/blockchain_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/string.hpp>

#include <csnode/blockchain.hpp>
#include <csnode/blockchain_serializer.hpp>
Expand All @@ -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 {
Expand All @@ -41,6 +43,7 @@ void BlockChain_Serializer::bind(BlockChain& bchain, std::set<cs::PublicKey>& in
pendingBlockReward_ = &bchain.pendingConsensusSettings_.blockReward;
pendingMiningCoefficient_ = &bchain.pendingConsensusSettings_.miningCoefficient;
startingDPOS_ = &bchain.startingDPOS_;
specialOrders_ = &bchain.specialOrders_;
csdebug() << "Blockchain bindings made";
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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() {
Expand Down
38 changes: 27 additions & 11 deletions csnode/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -4107,7 +4121,18 @@ void Node::processSpecialInfo(const csdb::Pool& pool) {
continue;
}
else {
auto stringBytes = it.user_field(cs::trx_uf::sp::managing).value<std::string>();
processSpecialOrder(it.user_field(cs::trx_uf::sp::managing).value<std::string>());
}
}
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<cs::Byte> msg(stringBytes.begin(), stringBytes.end());
cs::IDataStream stream(msg.data(), msg.size());
uint16_t order;
Expand All @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down
63 changes: 63 additions & 0 deletions tests/csnode/blockchain_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
#include <csnode/blockchain.hpp>
#include <csnode/blockchain_serializer.hpp>

#include <csdb/amount_commission.hpp>
#include <csdb/currency.hpp>
#include <csdb/pool.hpp>
#include <csnode/nodecore.hpp>

TEST(BlockChain, block_service_info) {
csdb::Pool block{};
Expand Down Expand Up @@ -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<const char*>(&order), sizeof(order));
payload.append(reinterpret_cast<const char*>(&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<char*>(&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<cs::PublicKey> 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);
}
Loading