Skip to content

Commit

Permalink
Merge branch 'develop' into master2dev
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusKysel authored Feb 26, 2024
2 parents a81b0b3 + 7de6729 commit 61fc875
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 35 deletions.
12 changes: 6 additions & 6 deletions libraries/core_libs/consensus/include/pbft/pbft_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class PbftManager {
public:
using time_point = std::chrono::system_clock::time_point;

PbftManager(const PbftConfig &conf, const blk_hash_t &dag_genesis_block_hash, addr_t node_addr,
std::shared_ptr<DbStorage> db, std::shared_ptr<PbftChain> pbft_chain,
std::shared_ptr<VoteManager> vote_mgr, std::shared_ptr<DagManager> dag_mgr,
std::shared_ptr<TransactionManager> trx_mgr, std::shared_ptr<FinalChain> final_chain, secret_t node_sk);
PbftManager(const GenesisConfig &conf, addr_t node_addr, std::shared_ptr<DbStorage> db,
std::shared_ptr<PbftChain> pbft_chain, std::shared_ptr<VoteManager> vote_mgr,
std::shared_ptr<DagManager> dag_mgr, std::shared_ptr<TransactionManager> trx_mgr,
std::shared_ptr<FinalChain> final_chain, secret_t node_sk);
~PbftManager();
PbftManager(const PbftManager &) = delete;
PbftManager(PbftManager &&) = delete;
Expand Down Expand Up @@ -252,7 +252,7 @@ class PbftManager {
* @brief Get PBFT committee size
* @return PBFT committee size
*/
size_t getPbftCommitteeSize() const { return config_.committee_size; }
size_t getPbftCommitteeSize() const { return config_.pbft.committee_size; }

/**
* @brief Test/enforce broadcastVotes() to actually send votes
Expand Down Expand Up @@ -567,7 +567,7 @@ class PbftManager {

const blk_hash_t dag_genesis_block_hash_;

const PbftConfig &config_;
const GenesisConfig &config_;

std::condition_variable stop_cv_;
std::mutex stop_mtx_;
Expand Down
8 changes: 5 additions & 3 deletions libraries/core_libs/consensus/src/final_chain/final_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ class FinalChainImpl final : public FinalChain {
auto rewards_stats = rewards_.processStats(new_blk, transactions_gas_used, batch);
const auto& [state_root, total_reward] = state_api_.distribute_rewards(rewards_stats);

auto blk_header = append_block(batch, new_blk.pbft_blk->getBeneficiary(), new_blk.pbft_blk->getTimestamp(),
kBlockGasLimit, state_root, total_reward, new_blk.transactions, receipts);
auto blk_header =
append_block(batch, new_blk.pbft_blk->getBeneficiary(), new_blk.pbft_blk->getTimestamp(), kBlockGasLimit,
state_root, total_reward, new_blk.transactions, receipts, new_blk.pbft_blk->getExtraDataRlp());
// Update number of executed DAG blocks and transactions
auto num_executed_dag_blk = num_executed_dag_blk_ + finalized_dag_blk_hashes.size();
auto num_executed_trx = num_executed_trx_ + new_blk.transactions.size();
Expand Down Expand Up @@ -274,7 +275,7 @@ class FinalChainImpl final : public FinalChain {
std::shared_ptr<BlockHeader> append_block(DB::Batch& batch, const addr_t& author, uint64_t timestamp,
uint64_t gas_limit, const h256& state_root, u256 total_reward,
const SharedTransactions& transactions = {},
const TransactionReceipts& receipts = {}) {
const TransactionReceipts& receipts = {}, const bytes& extra_data = {}) {
auto blk_header_ptr = std::make_shared<BlockHeader>();
auto& blk_header = *blk_header_ptr;
auto last_block = block_header();
Expand All @@ -286,6 +287,7 @@ class FinalChainImpl final : public FinalChain {
blk_header.gas_used = receipts.empty() ? 0 : receipts.back().cumulative_gas_used;
blk_header.gas_limit = gas_limit;
blk_header.total_reward = total_reward;
blk_header.extra_data = extra_data;
dev::BytesMap trxs_trie, receipts_trie;
dev::RLPStream rlp_strm;
for (size_t i(0); i < transactions.size(); ++i) {
Expand Down
39 changes: 24 additions & 15 deletions libraries/core_libs/consensus/src/pbft/pbft_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <cstdint>
#include <string>

#include "config/version.hpp"
#include "dag/dag.hpp"
#include "final_chain/final_chain.hpp"
#include "network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp"
Expand All @@ -28,11 +29,10 @@ using vrf_output_t = vrf_wrapper::vrf_output_t;
constexpr std::chrono::milliseconds kPollingIntervalMs{100};
constexpr PbftStep kMaxSteps{13}; // Need to be a odd number

PbftManager::PbftManager(const PbftConfig &conf, const blk_hash_t &dag_genesis_block_hash, addr_t node_addr,
std::shared_ptr<DbStorage> db, std::shared_ptr<PbftChain> pbft_chain,
std::shared_ptr<VoteManager> vote_mgr, std::shared_ptr<DagManager> dag_mgr,
std::shared_ptr<TransactionManager> trx_mgr, std::shared_ptr<FinalChain> final_chain,
secret_t node_sk)
PbftManager::PbftManager(const GenesisConfig &conf, addr_t node_addr, std::shared_ptr<DbStorage> db,
std::shared_ptr<PbftChain> pbft_chain, std::shared_ptr<VoteManager> vote_mgr,
std::shared_ptr<DagManager> dag_mgr, std::shared_ptr<TransactionManager> trx_mgr,
std::shared_ptr<FinalChain> final_chain, secret_t node_sk)
: db_(std::move(db)),
pbft_chain_(std::move(pbft_chain)),
vote_mgr_(std::move(vote_mgr)),
Expand All @@ -41,8 +41,8 @@ PbftManager::PbftManager(const PbftConfig &conf, const blk_hash_t &dag_genesis_b
final_chain_(std::move(final_chain)),
node_addr_(std::move(node_addr)),
node_sk_(std::move(node_sk)),
kMinLambda(conf.lambda_ms),
dag_genesis_block_hash_(dag_genesis_block_hash),
kMinLambda(conf.pbft.lambda_ms),
dag_genesis_block_hash_(conf.dag_genesis_block.getHash()),
config_(conf),
proposed_blocks_(db_) {
LOG_OBJECTS_CREATE("PBFT_MGR");
Expand Down Expand Up @@ -1085,8 +1085,16 @@ std::optional<std::pair<std::shared_ptr<PbftBlock>, std::vector<std::shared_ptr<
}
}
try {
auto block = std::make_shared<PbftBlock>(prev_blk_hash, anchor_hash, order_hash, last_state_root, propose_period,
node_addr_, node_sk_, std::move(reward_votes_hashes));
std::shared_ptr<PbftBlock> block;
// TODO: Activate in next hardfork
/*if (HARDFORK) {
block = std::make_shared<PbftBlock>(prev_blk_hash, anchor_hash, order_hash, last_state_root, propose_period,
node_addr_, node_sk_, std::move(reward_votes_hashes), TARAXA_MAJOR_VERSION,
TARAXA_MINOR_VERSION, TARAXA_PATCH_VERSION, "T");
} else */

block = std::make_shared<PbftBlock>(prev_blk_hash, anchor_hash, order_hash, last_state_root, propose_period,
node_addr_, node_sk_, std::move(reward_votes_hashes));

return {std::make_pair(std::move(block), std::move(reward_votes))};
} catch (const std::exception &e) {
Expand Down Expand Up @@ -1162,10 +1170,11 @@ PbftManager::proposePbftBlock() {
}

blk_hash_t dag_block_hash;
if (ghost.size() <= config_.dag_blocks_size) {
if (ghost.size() <= config_.pbft.dag_blocks_size) {
// Move back config_.ghost_path_move_back DAG blocks for DAG sycning
auto ghost_index =
(ghost.size() < config_.ghost_path_move_back + 1) ? 0 : (ghost.size() - 1 - config_.ghost_path_move_back);
auto ghost_index = (ghost.size() < config_.pbft.ghost_path_move_back + 1)
? 0
: (ghost.size() - 1 - config_.pbft.ghost_path_move_back);
while (ghost_index < ghost.size() - 1) {
if (ghost[ghost_index] != last_period_dag_anchor_block_hash) {
break;
Expand All @@ -1174,7 +1183,7 @@ PbftManager::proposePbftBlock() {
}
dag_block_hash = ghost[ghost_index];
} else {
dag_block_hash = ghost[config_.dag_blocks_size - 1];
dag_block_hash = ghost[config_.pbft.dag_blocks_size - 1];
}

if (dag_block_hash == dag_genesis_block_hash_) {
Expand Down Expand Up @@ -1211,7 +1220,7 @@ PbftManager::proposePbftBlock() {
}
const auto &dag_block_weight = dag_blk->getGasEstimation();

if (total_weight + dag_block_weight > config_.gas_limit) {
if (total_weight + dag_block_weight > config_.pbft.gas_limit) {
break;
}
total_weight += dag_block_weight;
Expand Down Expand Up @@ -1845,7 +1854,7 @@ bool PbftManager::checkBlockWeight(const std::vector<DagBlock> &dag_blocks) cons
const u256 total_weight =
std::accumulate(dag_blocks.begin(), dag_blocks.end(), u256(0),
[](u256 value, const auto &dag_block) { return value + dag_block.getGasEstimation(); });
if (total_weight > config_.gas_limit) {
if (total_weight > config_.pbft.gas_limit) {
return false;
}
return true;
Expand Down
5 changes: 2 additions & 3 deletions libraries/core_libs/node/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,8 @@ void FullNode::init() {
auto slashing_manager = std::make_shared<SlashingManager>(final_chain_, trx_mgr_, gas_pricer_, conf_, kp_.secret());
vote_mgr_ = std::make_shared<VoteManager>(node_addr, conf_.genesis.pbft, kp_.secret(), conf_.vrf_secret, db_,
pbft_chain_, final_chain_, key_manager_, slashing_manager);
pbft_mgr_ =
std::make_shared<PbftManager>(conf_.genesis.pbft, conf_.genesis.dag_genesis_block.getHash(), node_addr, db_,
pbft_chain_, vote_mgr_, dag_mgr_, trx_mgr_, final_chain_, kp_.secret());
pbft_mgr_ = std::make_shared<PbftManager>(conf_.genesis, node_addr, db_, pbft_chain_, vote_mgr_, dag_mgr_, trx_mgr_,
final_chain_, kp_.secret());
dag_block_proposer_ = std::make_shared<DagBlockProposer>(
conf_.genesis.dag.block_proposer, dag_mgr_, trx_mgr_, final_chain_, db_, key_manager_, node_addr, getSecretKey(),
getVrfSecretKey(), conf_.genesis.pbft.gas_limit, conf_.genesis.dag.gas_limit, conf_.genesis.state);
Expand Down
2 changes: 2 additions & 0 deletions libraries/types/pbft_block/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
set(HEADERS
include/pbft/pbft_block.hpp
include/pbft/pbft_block_extra_data.hpp
include/pbft/period_data.hpp
)
set(SOURCES
src/pbft_block.cpp
src/pbft_block_extra_data.cpp
src/period_data.cpp
)

Expand Down
16 changes: 15 additions & 1 deletion libraries/types/pbft_block/include/pbft/pbft_block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "common/types.hpp"
#include "dag/dag_block.hpp"
#include "pbft_block_extra_data.hpp"
#include "vote/vote.hpp"

namespace taraxa {
Expand All @@ -30,11 +31,12 @@ class PbftBlock {
addr_t beneficiary_;
sig_t signature_;
std::vector<vote_hash_t> reward_votes_; // Cert votes in previous period
std::optional<PbftBlockExtraData> extra_data_;

public:
PbftBlock(const blk_hash_t& prev_blk_hash, const blk_hash_t& dag_blk_hash_as_pivot, const blk_hash_t& order_hash,
const blk_hash_t& prev_state_root, PbftPeriod period, const addr_t& beneficiary, const secret_t& sk,
std::vector<vote_hash_t>&& reward_votes);
std::vector<vote_hash_t>&& reward_votes, const PbftBlockExtraData& extra_data = {});
explicit PbftBlock(const dev::RLP& rlp);
explicit PbftBlock(const bytes& RLP);

Expand Down Expand Up @@ -117,6 +119,18 @@ class PbftBlock {
*/
auto getTimestamp() const { return timestamp_; }

/**
* @brief Get extra data
* @return extra data
*/
auto getExtraData() const { return extra_data_; }

/**
* @brief Get extra data rlp
* @return extra data rlp
*/
auto getExtraDataRlp() const { return extra_data_.has_value() ? extra_data_->rlp() : bytes(); }

/**
* @brief Get PBFT block proposer address
* @return PBFT block proposer address
Expand Down
48 changes: 48 additions & 0 deletions libraries/types/pbft_block/include/pbft/pbft_block_extra_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <libdevcore/Common.h>
#include <libdevcore/RLP.h>
#include <libdevcore/SHA3.h>
#include <libdevcrypto/Common.h>

#include "common/types.hpp"
#include "dag/dag_block.hpp"
#include "vote/vote.hpp"

namespace taraxa {

/** @addtogroup PBFT
* @{
*/

class PbftBlockExtraData {
public:
PbftBlockExtraData() {}
PbftBlockExtraData(const uint16_t major_version, const uint16_t minor_version, const uint16_t patch_version,
const uint16_t net_version, const std::string node_implementation);
PbftBlockExtraData(const bytes& data);

/**
* @brief Get rlp
* @return rlp
*/
bytes rlp() const;

/**
* @brief Get JSON
* @return Json
*/
Json::Value getJson() const;

protected:
uint16_t major_version_;
uint16_t minor_version_;
uint16_t patch_version_;
uint16_t net_version_;
std::string node_implementation_;
static constexpr uint32_t kExtraDataMaxSize = 1024;
};

/** @}*/

} // namespace taraxa
27 changes: 22 additions & 5 deletions libraries/types/pbft_block/src/pbft_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,32 @@ namespace taraxa {
PbftBlock::PbftBlock(bytes const& b) : PbftBlock(dev::RLP(b)) {}

PbftBlock::PbftBlock(dev::RLP const& rlp) {
util::rlp_tuple(util::RLPDecoderRef(rlp, true), prev_block_hash_, dag_block_hash_as_pivot_, order_hash_,
prev_state_root_hash_, period_, timestamp_, reward_votes_, signature_);
if (rlp.itemCount() == 9) {
dev::bytes extra_data_bytes;
util::rlp_tuple(util::RLPDecoderRef(rlp, true), prev_block_hash_, dag_block_hash_as_pivot_, order_hash_,
prev_state_root_hash_, period_, timestamp_, reward_votes_, extra_data_bytes, signature_);
extra_data_ = PbftBlockExtraData(extra_data_bytes);
} else {
util::rlp_tuple(util::RLPDecoderRef(rlp, true), prev_block_hash_, dag_block_hash_as_pivot_, order_hash_,
prev_state_root_hash_, period_, timestamp_, reward_votes_, signature_);
}

calculateHash_();
checkUniqueRewardVotes();
}

PbftBlock::PbftBlock(const blk_hash_t& prev_blk_hash, const blk_hash_t& dag_blk_hash_as_pivot,
const blk_hash_t& order_hash, const blk_hash_t& prev_state_root, PbftPeriod period,
const addr_t& beneficiary, const secret_t& sk, std::vector<vote_hash_t>&& reward_votes)
const addr_t& beneficiary, const secret_t& sk, std::vector<vote_hash_t>&& reward_votes,
const PbftBlockExtraData& extra_data)
: prev_block_hash_(prev_blk_hash),
dag_block_hash_as_pivot_(dag_blk_hash_as_pivot),
order_hash_(order_hash),
prev_state_root_hash_(prev_state_root),
period_(period),
beneficiary_(beneficiary),
reward_votes_(reward_votes) {
reward_votes_(reward_votes),
extra_data_(extra_data) {
timestamp_ = dev::utcTime();
signature_ = dev::sign(sk, sha3(false));
calculateHash_();
Expand Down Expand Up @@ -86,20 +96,27 @@ Json::Value PbftBlock::getJson() const {
for (const auto& v : reward_votes_) {
json["reward_votes"].append(v.toString());
}
json["extra_data"] = extra_data_->getJson();

return json;
}

// Using to setup PBFT block hash
void PbftBlock::streamRLP(dev::RLPStream& strm, bool include_sig) const {
strm.appendList(include_sig ? 8 : 7);
uint32_t rlp_count = (include_sig ? 8 : 7);
if (extra_data_.has_value()) rlp_count++;
strm.appendList(rlp_count);
strm << prev_block_hash_;
strm << dag_block_hash_as_pivot_;
strm << order_hash_;
strm << prev_state_root_hash_;
strm << period_;
strm << timestamp_;
strm.appendVector(reward_votes_);

if (extra_data_.has_value()) {
strm << extra_data_->rlp();
}
if (include_sig) {
strm << signature_;
}
Expand Down
49 changes: 49 additions & 0 deletions libraries/types/pbft_block/src/pbft_block_extra_data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "pbft/pbft_block_extra_data.hpp"

#include <iostream>

#include "common/jsoncpp.hpp"

namespace taraxa {

PbftBlockExtraData::PbftBlockExtraData(const uint16_t major_version, const uint16_t minor_version,
const uint16_t patch_version, const uint16_t net_version,
const std::string node_implementation)
: major_version_(major_version),
minor_version_(minor_version),
patch_version_(patch_version),
net_version_(net_version),
node_implementation_(node_implementation) {}

PbftBlockExtraData::PbftBlockExtraData(const bytes& data) {
if (data.size() > kExtraDataMaxSize) {
throw std::runtime_error("Pbft block invalid, extra data size over the limit");
}
dev::RLP rlp(data);
util::rlp_tuple(util::RLPDecoderRef(rlp, true), major_version_, minor_version_, patch_version_, net_version_,
node_implementation_);
}

bytes PbftBlockExtraData::rlp() const {
dev::RLPStream s;
s.appendList(5);
s << major_version_;
s << minor_version_;
s << patch_version_;
s << net_version_;
s << node_implementation_;
return s.invalidate();
}

Json::Value PbftBlockExtraData::getJson() const {
Json::Value json;
json["major_version"] = major_version_;
json["minor_version"] = minor_version_;
json["patch_version"] = patch_version_;
json["net_version"] = net_version_;
json["node_implementation"] = node_implementation_;

return json;
}

} // namespace taraxa
2 changes: 1 addition & 1 deletion tests/final_chain_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ struct FinalChainTest : WithDataDir {
trxs.size(), [&](auto i) { return dev::rlp(i); },
[&](auto i) { return util::rlp_enc(receipts[i]); }));
EXPECT_EQ(blk_h.gas_limit, cfg.genesis.pbft.gas_limit);
EXPECT_EQ(blk_h.extra_data, bytes());
EXPECT_EQ(blk_h.extra_data, pbft_block->getExtraData()->rlp());
EXPECT_EQ(blk_h.nonce(), Nonce());
EXPECT_EQ(blk_h.difficulty(), 0);
EXPECT_EQ(blk_h.mix_hash(), h256());
Expand Down
Loading

0 comments on commit 61fc875

Please sign in to comment.