Skip to content

Commit

Permalink
Merge pull request #2235 from Taraxa-project/save_state_root_hash
Browse files Browse the repository at this point in the history
feat: save delayed state_root_hash in pbft block
  • Loading branch information
kstdl authored Dec 12, 2022
2 parents 740b809 + 2f09e09 commit ba743ad
Show file tree
Hide file tree
Showing 32 changed files with 195 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"number_of_proposers": "0x14",
"dag_blocks_size": "0x32",
"ghost_path_move_back": "0x0",
"state_root_recording_delay": "0x3",
"lambda_ms": "0x5DC",
"gas_limit": "0x3938700"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
"number_of_proposers": "0x14",
"dag_blocks_size": "0x32",
"ghost_path_move_back": "0x0",
"state_root_recording_delay": "0x3",
"lambda_ms": "0x5DC",
"gas_limit": "0x7d2b7500"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@
"number_of_proposers": "0x14",
"dag_blocks_size": "0x32",
"ghost_path_move_back": "0x0",
"state_root_recording_delay": "0x3",
"lambda_ms": "0x5DC",
"gas_limit": "0x7d2b7500"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"number_of_proposers": "0x14",
"dag_blocks_size": "0x32",
"ghost_path_move_back": "0x0",
"state_root_recording_delay": "0x3",
"lambda_ms": "0x5DC",
"gas_limit": "0x7d2b7500"
},
Expand Down
2 changes: 1 addition & 1 deletion libraries/common/include/common/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GLOBAL_CONST(h256, EmptyRLPListSHA3);
GLOBAL_CONST(h64, EmptyNonce);
GLOBAL_CONST(u256, ZeroU256);

static const blk_hash_t kNullBlockHash = blk_hash_t(0);
static const blk_hash_t kNullBlockHash;

constexpr uint16_t kOnePercent = 100;
constexpr uint16_t kMaxLevelsPerPeriod = 100;
Expand Down
1 change: 1 addition & 0 deletions libraries/config/include/config/pbft_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct PbftConfig {
uint32_t number_of_proposers = 20;
uint32_t dag_blocks_size = 0;
uint32_t ghost_path_move_back = 0;
uint32_t state_root_recording_delay = 3;
uint64_t gas_limit = 0;

bytes rlp() const;
Expand Down
3 changes: 3 additions & 0 deletions libraries/config/src/pbft_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Json::Value enc_json(PbftConfig const& obj) {
ret["number_of_proposers"] = dev::toJS(obj.number_of_proposers);
ret["dag_blocks_size"] = dev::toJS(obj.dag_blocks_size);
ret["ghost_path_move_back"] = dev::toJS(obj.ghost_path_move_back);
ret["state_root_recording_delay"] = dev::toJS(obj.state_root_recording_delay);
ret["gas_limit"] = dev::toJS(obj.gas_limit);
return ret;
}
Expand All @@ -22,6 +23,7 @@ void dec_json(Json::Value const& json, PbftConfig& obj) {
obj.number_of_proposers = dev::jsToInt(json["number_of_proposers"].asString());
obj.dag_blocks_size = dev::jsToInt(json["dag_blocks_size"].asString());
obj.ghost_path_move_back = dev::jsToInt(json["ghost_path_move_back"].asString());
obj.state_root_recording_delay = dev::jsToInt(json["state_root_recording_delay"].asString());
obj.gas_limit = dev::getUInt(json["gas_limit"]);
}

Expand All @@ -34,6 +36,7 @@ bytes PbftConfig::rlp() const {
s << number_of_proposers;
s << dag_blocks_size;
s << ghost_path_move_back;
s << state_root_recording_delay;
s << gas_limit;

return s.out();
Expand Down
6 changes: 2 additions & 4 deletions libraries/core_libs/consensus/include/pbft/pbft_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#include "pbft/proposed_blocks.hpp"
#include "pbft/soft_voted_block_data.hpp"

#define NULL_BLOCK_HASH blk_hash_t(0)

namespace taraxa {

/** @addtogroup PBFT
Expand Down Expand Up @@ -314,7 +312,7 @@ class PbftManager : public std::enable_shared_from_this<PbftManager> {
void resetStep();

/**
* @brief If node receives 2t+1 next votes for some block(including NULL_BLOCK_HASH), advance round to + 1.
* @brief If node receives 2t+1 next votes for some block(including kNullBlockHash), advance round to + 1.
* @return true if PBFT round advanced, otherwise false
*/
bool advanceRound();
Expand Down Expand Up @@ -473,7 +471,7 @@ class PbftManager : public std::enable_shared_from_this<PbftManager> {
* @brief Identify a leader block from all received proposed PBFT blocks for the current round by using minimum
* Verifiable Random Function (VRF) output. In filter state, don’t need check vote value correction.
* @param round current pbft round
* @param period new pbft period (perriod == chain_size + 1)
* @param period new pbft period (period == chain_size + 1)
* @return shared_ptr to leader identified leader block
*/
// TODO: exchange round <-> period
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ class VoteManager {
mutable boost::shared_mutex verified_votes_access_;

// <PBFT period, <PBFT round, <PBFT step, <voter address, pair<vote 1, vote 2>>><>
// For next votes we enable 2 votes per round & step, one of which must be vote for NULL_BLOCK_HASH
// For next votes we enable 2 votes per round & step, one of which must be vote for kNullBlockHash
std::map<PbftPeriod,
std::map<PbftRound,
std::unordered_map<
Expand Down
2 changes: 0 additions & 2 deletions libraries/core_libs/consensus/src/dag/dag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp"
#include "transaction/transaction_manager.hpp"

#define NULL_BLOCK_HASH blk_hash_t(0)

namespace taraxa {

Dag::Dag(blk_hash_t const &dag_genesis_block_hash, addr_t node_addr) {
Expand Down
14 changes: 6 additions & 8 deletions libraries/core_libs/consensus/src/dag/dag_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp"
#include "transaction/transaction_manager.hpp"

#define NULL_BLOCK_HASH blk_hash_t(0)

namespace taraxa {
DagManager::DagManager(blk_hash_t const &dag_genesis_block_hash, addr_t node_addr,
const SortitionConfig &sortition_config, const DagConfig &dag_config,
Expand Down Expand Up @@ -238,8 +236,8 @@ void DagManager::updateFrontier() {
}

std::vector<blk_hash_t> DagManager::getGhostPath(const blk_hash_t &source) const {
// No need to check ghost path for NULL_BLOCK_HASH
if (source == NULL_BLOCK_HASH) {
// No need to check ghost path for kNullBlockHash
if (source == kNullBlockHash) {
return {};
}

Expand Down Expand Up @@ -309,9 +307,9 @@ uint DagManager::setDagBlockOrder(blk_hash_t const &new_anchor, PbftPeriod perio
return 0;
}

if (new_anchor == NULL_BLOCK_HASH) {
if (new_anchor == kNullBlockHash) {
period_ = period;
LOG(log_nf_) << "Set new period " << period << " with NULL_BLOCK_HASH anchor";
LOG(log_nf_) << "Set new period " << period << " with kNullBlockHash anchor";
return 0;
}

Expand Down Expand Up @@ -347,7 +345,7 @@ uint DagManager::setDagBlockOrder(blk_hash_t const &new_anchor, PbftPeriod perio

std::unordered_set<blk_hash_t> dag_order_set(dag_order.begin(), dag_order.end());
assert(dag_order_set.count(new_anchor));
addToDag(new_anchor, blk_hash_t(), vec_blk_t(), 0, true);
addToDag(new_anchor, kNullBlockHash, vec_blk_t(), 0, true);

const auto anchor_block_level = getDagBlock(new_anchor)->getLevel();
if (anchor_block_level > dag_expiry_limit_) {
Expand Down Expand Up @@ -458,7 +456,7 @@ void DagManager::recoverDag() {
if (anchor) {
anchor_ = anchor;
LOG(log_nf_) << "Recover anchor " << anchor_;
addToDag(anchor_, blk_hash_t(), vec_blk_t(), 0, true);
addToDag(anchor_, kNullBlockHash, vec_blk_t(), 0, true);
break;
}
}
Expand Down
20 changes: 10 additions & 10 deletions libraries/core_libs/consensus/src/final_chain/final_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FinalChainImpl final : public FinalChain {
StateAPI state_api_;

// It is not prepared to use more then 1 thread. Examine it if you want to change threads count
util::ThreadPool executor_thread_{1};
boost::asio::thread_pool executor_thread_{1};

std::atomic<uint64_t> num_executed_dag_blk_ = 0;
std::atomic<uint64_t> num_executed_trx_ = 0;
Expand Down Expand Up @@ -60,7 +60,6 @@ class FinalChainImpl final : public FinalChain {
[this](uint64_t blk) { return get_transaction_hashes(blk); }),
accounts_cache_(config.final_chain_cache_in_blocks,
[this](uint64_t blk, const addr_t& addr) { return state_api_.get_account(blk, addr); }),

total_vote_count_cache_(config.final_chain_cache_in_blocks,
[this](uint64_t blk) { return state_api_.dpos_eligible_total_vote_count(blk); }),
dpos_vote_count_cache_(
Expand Down Expand Up @@ -115,24 +114,25 @@ class FinalChainImpl final : public FinalChain {
delegation_delay_ = config.genesis.state.dpos.delegation_delay;
}

void stop() override { executor_thread_.stop(); }
void stop() override { executor_thread_.join(); }

std::future<std::shared_ptr<FinalizationResult const>> finalize(PeriodData&& new_blk,
std::future<std::shared_ptr<const FinalizationResult>> finalize(PeriodData&& new_blk,
std::vector<h256>&& finalized_dag_blk_hashes,
finalize_precommit_ext precommit_ext = {}) override {
auto p = std::make_shared<std::promise<std::shared_ptr<FinalizationResult const>>>();
executor_thread_.post([this, new_blk = std::move(new_blk),
finalized_dag_blk_hashes = std::move(finalized_dag_blk_hashes),
precommit_ext = std::move(precommit_ext), p]() mutable {
auto p = std::make_shared<std::promise<std::shared_ptr<const FinalizationResult>>>();
boost::asio::post(executor_thread_, [this, new_blk = std::move(new_blk),
finalized_dag_blk_hashes = std::move(finalized_dag_blk_hashes),
precommit_ext = std::move(precommit_ext), p]() mutable {
p->set_value(finalize_(std::move(new_blk), std::move(finalized_dag_blk_hashes), precommit_ext));
});
return p->get_future();
}

EthBlockNumber delegation_delay() const override { return delegation_delay_; }

std::shared_ptr<FinalizationResult> finalize_(PeriodData&& new_blk, std::vector<h256>&& finalized_dag_blk_hashes,
finalize_precommit_ext const& precommit_ext) {
std::shared_ptr<const FinalizationResult> finalize_(PeriodData&& new_blk,
std::vector<h256>&& finalized_dag_blk_hashes,
finalize_precommit_ext const& precommit_ext) {
auto batch = db_->createWriteBatch();

RewardsStats rewards_stats;
Expand Down
8 changes: 2 additions & 6 deletions libraries/core_libs/consensus/src/pbft/pbft_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ using namespace std;
namespace taraxa {

PbftChain::PbftChain(addr_t node_addr, std::shared_ptr<DbStorage> db)
: head_hash_(blk_hash_t(0)),
size_(0),
non_empty_size_(0),
last_pbft_block_hash_(blk_hash_t(0)),
db_(std::move(db)) {
: size_(0), non_empty_size_(0), db_(std::move(db)) {
LOG_OBJECTS_CREATE("PBFT_CHAIN");
// Get PBFT head from DB
auto pbft_head_str = db_->getPbftHead(head_hash_);
Expand Down Expand Up @@ -88,7 +84,7 @@ PbftBlock PbftChain::getPbftBlockInChain(const taraxa::blk_hash_t& pbft_block_ha
void PbftChain::updatePbftChain(blk_hash_t const& pbft_block_hash, blk_hash_t const& anchor_hash) {
UniqueLock lock(chain_head_access_);
size_++;
if (anchor_hash != NULL_BLOCK_HASH) {
if (anchor_hash != kNullBlockHash) {
non_empty_size_++;
last_non_null_pbft_dag_anchor_hash_ = anchor_hash;
}
Expand Down
Loading

0 comments on commit ba743ad

Please sign in to comment.