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
18 changes: 18 additions & 0 deletions csnode/include/csnode/blockchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define BLOCKCHAIN_HPP

#include <chrono>
#include <limits>
#include <list>
#include <map>
#include <memory>
Expand Down Expand Up @@ -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<cs::Sequence>::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);
Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions csnode/include/csnode/blockchain_serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions csnode/include/csnode/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ class Node {
uint8_t requestKBAnswer(std::vector<std::string> 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<api_diag::NodeRewardSet>& request, std::string& msg, const cs::PublicKey& pKey, bool oneNode);

Expand Down
16 changes: 16 additions & 0 deletions csnode/src/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
58 changes: 58 additions & 0 deletions csnode/src/blockchain_serializer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <fstream>
#include <limits>
#include <sstream>

//#include <boost/archive/text_oarchive.hpp>
Expand All @@ -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 {
Expand All @@ -32,6 +35,12 @@ void BlockChain_Serializer::bind(BlockChain& bchain, std::set<cs::PublicKey>& 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";
}

Expand All @@ -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) {
Expand All @@ -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<cs::Sequence>::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;
}

Expand Down Expand Up @@ -108,5 +165,6 @@ void BlockChain_Serializer::load(const std::filesystem::path& rootDir) {
*miningOn_ = miningOn == 0U ? true : false;

ia >> *TimeMinStage1_;
loadPending(rootDir);
}
} // namespace cs
33 changes: 33 additions & 0 deletions csnode/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<cs::Sequence>::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) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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_;
Expand Down
48 changes: 48 additions & 0 deletions tests/csnode/blockchain_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
#include "clientconfigmock.hpp"
#include "gtest/gtest.h"

#include <filesystem>
#include <limits>
#include <set>

#include <csnode/blockchain.hpp>
#include <csnode/blockchain_serializer.hpp>

#include <csdb/pool.hpp>

Expand Down Expand Up @@ -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<cs::PublicKey> 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<cs::Sequence>::max());
ASSERT_EQ(loaded.getStartingDPOS(), 0u);

std::filesystem::remove_all(dir);
}
Loading