From db9c16cc66859014209ae460f413fcb9691df717 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Wed, 3 May 2023 08:49:11 +0200 Subject: [PATCH 001/134] chore: improve prune --- .../include/final_chain/state_api.hpp | 3 +-- .../consensus/src/dag/dag_manager.cpp | 3 +++ .../consensus/src/final_chain/final_chain.cpp | 20 ++++++++++------ .../consensus/src/final_chain/state_api.cpp | 5 ++-- submodules/taraxa-evm | 2 +- tests/full_node_test.cpp | 24 +++---------------- 6 files changed, 23 insertions(+), 34 deletions(-) diff --git a/libraries/core_libs/consensus/include/final_chain/state_api.hpp b/libraries/core_libs/consensus/include/final_chain/state_api.hpp index c5349be356..9d09067dbb 100644 --- a/libraries/core_libs/consensus/include/final_chain/state_api.hpp +++ b/libraries/core_libs/consensus/include/final_chain/state_api.hpp @@ -48,8 +48,7 @@ class StateAPI { const RewardsStats& rewards_stats = {}); void transition_state_commit(); void create_snapshot(PbftPeriod period); - void prune(const dev::h256& state_root_to_keep, const std::vector& state_root_to_prune, - EthBlockNumber blk_num); + void prune(const std::vector& state_root_to_keep, EthBlockNumber blk_num); // DPOS uint64_t dpos_eligible_total_vote_count(EthBlockNumber blk_num) const; diff --git a/libraries/core_libs/consensus/src/dag/dag_manager.cpp b/libraries/core_libs/consensus/src/dag/dag_manager.cpp index bfe041cdd4..82c91cdda0 100644 --- a/libraries/core_libs/consensus/src/dag/dag_manager.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_manager.cpp @@ -287,6 +287,8 @@ void DagManager::clearLightNodeHistory() { // Actual history size will be between 100% and 110% of light_node_history_ to avoid deleting on every period if (((period_ % (std::max(light_node_history_ / 10, (uint64_t)1)) == 0)) && period_ > light_node_history_ && dag_expiry_level_ > max_levels_per_period_ + 1) { + // This will happen at most once a day so log a silent log + LOG(log_si_) << "Clear light node history"; const auto proposal_period = db_->getProposalPeriodForDagLevel(dag_expiry_level_ - max_levels_per_period_ - 1); assert(proposal_period); @@ -299,6 +301,7 @@ void DagManager::clearLightNodeHistory() { << " *proposal_period " << *proposal_period; LOG(log_tr_) << "Delete period history from: " << start << " to " << end; db_->clearPeriodDataHistory(end); + LOG(log_si_) << "Clear light node history completed"; } } diff --git a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp index cd7d42cd97..ceb25d4d7b 100644 --- a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp +++ b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp @@ -119,9 +119,11 @@ class FinalChainImpl final : public FinalChain { } delegation_delay_ = config.genesis.state.dpos.delegation_delay; - if (config.db_config.prune_state_db && last_blk_num.has_value() && *last_blk_num > kLightNodeHistory) { + const auto kPruneblocksToKeep = kDagExpiryLevelLimit + kMaxLevelsPerPeriod + 1; + if ((config.db_config.prune_state_db || kLightNode) && last_blk_num.has_value() && + *last_blk_num > kPruneblocksToKeep) { LOG(log_si_) << "Pruning state db, this might take several minutes"; - prune(*last_blk_num - kLightNodeHistory); + prune(*last_blk_num - kPruneblocksToKeep); LOG(log_si_) << "Pruning state db complete"; } } @@ -246,13 +248,17 @@ class FinalChainImpl final : public FinalChain { } void prune(EthBlockNumber blk_n) override { - const auto last_block_to_keep = get_block_header(blk_n); + LOG(log_nf_) << "Pruning data older than " << blk_n; + auto last_block_to_keep = get_block_header(blk_n); if (last_block_to_keep) { - std::vector state_root_to_prune; - LOG(log_nf_) << "Pruning data older than " << blk_n; + auto block_to_keep = last_block_to_keep; + std::vector state_root_to_keep; + while (block_to_keep) { + state_root_to_keep.push_back(block_to_keep->state_root); + block_to_keep = get_block_header(block_to_keep->number + 1); + } auto block_to_prune = get_block_header(last_block_to_keep->number - 1); while (block_to_prune && block_to_prune->number > 0) { - state_root_to_prune.push_back(block_to_prune->state_root); db_->remove(DB::Columns::final_chain_blk_by_number, block_to_prune->number); db_->remove(DB::Columns::final_chain_blk_hash_by_number, block_to_prune->number); db_->remove(DB::Columns::final_chain_blk_number_by_hash, block_to_prune->hash); @@ -263,7 +269,7 @@ class FinalChainImpl final : public FinalChain { db_->compactColumn(DB::Columns::final_chain_blk_hash_by_number); db_->compactColumn(DB::Columns::final_chain_blk_number_by_hash); - state_api_.prune(last_block_to_keep->state_root, state_root_to_prune, last_block_to_keep->number); + state_api_.prune(state_root_to_keep, last_block_to_keep->number); } } diff --git a/libraries/core_libs/consensus/src/final_chain/state_api.cpp b/libraries/core_libs/consensus/src/final_chain/state_api.cpp index 7cf3e17657..b8d37375e7 100644 --- a/libraries/core_libs/consensus/src/final_chain/state_api.cpp +++ b/libraries/core_libs/consensus/src/final_chain/state_api.cpp @@ -204,9 +204,8 @@ void StateAPI::create_snapshot(PbftPeriod period) { err_h.check(); } -void StateAPI::prune(const dev::h256& state_root_to_keep, const std::vector& state_root_to_prune, - EthBlockNumber blk_num) { - return c_method_args_rlp(this_c_, state_root_to_keep, state_root_to_prune, blk_num); +void StateAPI::prune(const std::vector& state_root_to_keep, EthBlockNumber blk_num) { + return c_method_args_rlp(this_c_, state_root_to_keep, blk_num); } uint64_t StateAPI::dpos_eligible_total_vote_count(EthBlockNumber blk_num) const { diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index 88ab801e01..4bb83fc4c6 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit 88ab801e01aa6ceb15a5dc21a289a23111594195 +Subproject commit 4bb83fc4c6f74cdf4951baeea80d400102e0147f diff --git a/tests/full_node_test.cpp b/tests/full_node_test.cpp index 0b0ce29a37..132bb33ae0 100644 --- a/tests/full_node_test.cpp +++ b/tests/full_node_test.cpp @@ -685,31 +685,13 @@ TEST_F(FullNodeTest, sync_five_nodes) { // Prune state_db of one node auto prune_node = nodes[nodes.size() - 1]; - const uint32_t min_blocks_to_prune = 50; + const uint32_t min_blocks_to_prune = 30; // This ensures that we never prune blocks that are over proposal period - ASSERT_HAPPENS({20s, 100ms}, [&](auto &ctx) { - const auto max_level = prune_node->getDagManager()->getMaxLevel(); - const auto proposal_period = prune_node->getDB()->getProposalPeriodForDagLevel(max_level); - ASSERT_TRUE(proposal_period.has_value()); - context.dummy_transaction(); - WAIT_EXPECT_TRUE(ctx, ((*proposal_period) > min_blocks_to_prune)) + ASSERT_HAPPENS({40s, 100ms}, [&](auto &ctx) { + WAIT_EXPECT_TRUE(ctx, (prune_node->getPbftChain()->getPbftChainSize() > min_blocks_to_prune + kMaxLevelsPerPeriod)) }); prune_node->getFinalChain()->prune(min_blocks_to_prune); context.assert_balances_synced(); - - // transfer some coins to pruned node ... - context.coin_transfer(0, prune_node->getAddress(), init_bal, false); - context.wait_all_transactions_known(); - - std::cout << "Waiting until transaction is executed" << std::endl; - auto trx_cnt = context.getIssuedTrxCount(); - ASSERT_HAPPENS({20s, 500ms}, [&](auto &ctx) { - for (size_t i = 0; i < nodes.size(); ++i) - WAIT_EXPECT_EQ(ctx, nodes[i]->getDB()->getNumTransactionExecuted(), trx_cnt) - }); - - // Check balances after prune"; - context.assert_balances_synced(); } TEST_F(FullNodeTest, insert_anchor_and_compute_order) { From 6728c0cf6051378199e9fb8f652ef6314090b782 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Fri, 5 May 2023 14:34:55 +0200 Subject: [PATCH 002/134] feat: parallel execution of state pruing --- submodules/taraxa-evm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index 4bb83fc4c6..b147444796 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit 4bb83fc4c6f74cdf4951baeea80d400102e0147f +Subproject commit b14744479634cb8f29b4f13f3a8ab77742c166d0 From c114b7d537f8fa69565a105496aff20e0841ff61 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Tue, 9 May 2023 13:56:19 +0200 Subject: [PATCH 003/134] chore: disable snapshots during db rebuild --- libraries/core_libs/node/src/node.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libraries/core_libs/node/src/node.cpp b/libraries/core_libs/node/src/node.cpp index 2e6002d89e..33d89035a6 100644 --- a/libraries/core_libs/node/src/node.cpp +++ b/libraries/core_libs/node/src/node.cpp @@ -63,7 +63,9 @@ void FullNode::init() { conf_.db_config.db_max_open_files, conf_.db_config.db_max_snapshots, conf_.db_config.db_revert_to_period, node_addr, true); } - db_ = std::make_shared(conf_.db_path, conf_.db_config.db_snapshot_each_n_pbft_block, + db_ = std::make_shared(conf_.db_path, + // Snapshots should be disabled while rebuilding + conf_.db_config.rebuild_db ? 0 : conf_.db_config.db_snapshot_each_n_pbft_block, conf_.db_config.db_max_open_files, conf_.db_config.db_max_snapshots, conf_.db_config.db_revert_to_period, node_addr, false); @@ -74,7 +76,8 @@ void FullNode::init() { old_db_ = std::make_shared(conf_.db_path, conf_.db_config.db_snapshot_each_n_pbft_block, conf_.db_config.db_max_open_files, conf_.db_config.db_max_snapshots, conf_.db_config.db_revert_to_period, node_addr, true); - db_ = std::make_shared(conf_.db_path, conf_.db_config.db_snapshot_each_n_pbft_block, + db_ = std::make_shared(conf_.db_path, + 0, // Snapshots should be disabled while rebuilding conf_.db_config.db_max_open_files, conf_.db_config.db_max_snapshots, conf_.db_config.db_revert_to_period, node_addr); } else if (db_->hasMinorVersionChanged()) { From 2959dcb34f489d83918101302f644a3f3f9cc918 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 11 May 2023 08:05:44 +0200 Subject: [PATCH 004/134] chore: update evm repo --- submodules/taraxa-evm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index b147444796..c620c6fe72 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit b14744479634cb8f29b4f13f3a8ab77742c166d0 +Subproject commit c620c6fe72301f6eef8d0fe24c5bdf8db8e9f39d From 985f7dc604d57d40ae75858a5ccbb0fe67adbe6b Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Fri, 28 Apr 2023 11:09:48 +0200 Subject: [PATCH 005/134] refactor: rewards stats passing --- .../common/include/common/range_view.hpp | 7 +-- .../include/final_chain/rewards_stats.hpp | 34 +++++++------ .../include/final_chain/state_api.hpp | 4 +- .../consensus/src/final_chain/final_chain.cpp | 25 ++++++---- .../src/final_chain/rewards_stats.cpp | 50 +++++++++---------- .../consensus/src/final_chain/state_api.cpp | 7 +-- .../pbft_block/include/pbft/pbft_block.hpp | 23 +++++---- tests/final_chain_test.cpp | 31 ++++++++++-- tests/state_api_test.cpp | 6 +-- 9 files changed, 104 insertions(+), 83 deletions(-) diff --git a/libraries/common/include/common/range_view.hpp b/libraries/common/include/common/range_view.hpp index 5c93b77870..8f6cbf29d4 100644 --- a/libraries/common/include/common/range_view.hpp +++ b/libraries/common/include/common/range_view.hpp @@ -3,7 +3,7 @@ #include #include -namespace taraxa::util::range_view { +namespace taraxa::util { template struct RangeView { @@ -72,9 +72,4 @@ auto make_range_view(Seq const &seq) { return RangeView(seq); } -} // namespace taraxa::util::range_view - -namespace taraxa::util { -using range_view::make_range_view; -using range_view::RangeView; } // namespace taraxa::util diff --git a/libraries/core_libs/consensus/include/final_chain/rewards_stats.hpp b/libraries/core_libs/consensus/include/final_chain/rewards_stats.hpp index ee925e16db..c5ce0a0b1c 100644 --- a/libraries/core_libs/consensus/include/final_chain/rewards_stats.hpp +++ b/libraries/core_libs/consensus/include/final_chain/rewards_stats.hpp @@ -16,20 +16,25 @@ namespace taraxa { class RewardsStats { public: /** - * @brief Process PeriodData and returns vector of validators, who included provided block.transactions as first in - * dag block, e.g. returned validator on position 2 included transaction block.transactions[2] as first in his dag - * block + * @brief setting block_author_, max_votes_weight_ and calls processStats function * - * @param block * @param dpos_vote_count - votes count for previous block * @param committee_size - * @return vector of validators */ - std::vector processStats(const PeriodData& block, uint64_t dpos_vote_count, uint32_t committee_size); + RewardsStats(const PeriodData& block, uint64_t dpos_vote_count, uint32_t committee_size); HAS_RLP_FIELDS private: + /** + * @brief Process PeriodData and returns vector of validators, who included provided block.transactions as first in + * dag block, e.g. returned validator on position 2 included transaction block.transactions[2] as first in his dag + * block + * + * @param block + * @return vector of validators + */ + void processStats(const PeriodData& block); /** * @brief In case unique tx_hash is provided, it is mapped to it's validator's address + validator's unique txs count * is incremented. If provided tx_hash was already processed, nothing happens @@ -56,15 +61,6 @@ class RewardsStats { */ bool addVote(const std::shared_ptr& vote); - /** - * @brief Prepares reward statistics bases on period data data - * - * @param sync_blk - * @param dpos_vote_count - votes count for previous block - * @param committee_size - */ - void initStats(const PeriodData& sync_blk, uint64_t dpos_vote_count, uint32_t committee_size); - private: struct ValidatorStats { // count of rewardable(with 1 or more unique transactions) DAG blocks produced by this validator @@ -76,8 +72,14 @@ class RewardsStats { HAS_RLP_FIELDS }; + // Pbft block author + addr_t block_author_; + // Transactions validators: tx hash -> validator that included it as first in his block - std::unordered_map txs_validators_; + std::unordered_map validator_by_tx_hash_; + + // Vector with all transactions validators + std::vector txs_validators_; // Txs stats: validator -> ValidatorStats std::unordered_map validators_stats_; diff --git a/libraries/core_libs/consensus/include/final_chain/state_api.hpp b/libraries/core_libs/consensus/include/final_chain/state_api.hpp index 9d09067dbb..691494456f 100644 --- a/libraries/core_libs/consensus/include/final_chain/state_api.hpp +++ b/libraries/core_libs/consensus/include/final_chain/state_api.hpp @@ -43,9 +43,7 @@ class StateAPI { StateDescriptor get_last_committed_state_descriptor() const; const StateTransitionResult& transition_state(const EVMBlock& block, const util::RangeView& transactions, - const util::RangeView& transactions_validators = {}, - const util::RangeView& uncles = {}, - const RewardsStats& rewards_stats = {}); + const std::vector& rewards_stats = {}); void transition_state_commit(); void create_snapshot(PbftPeriod period); void prune(const std::vector& state_root_to_keep, EthBlockNumber blk_num); diff --git a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp index ceb25d4d7b..53256f9afe 100644 --- a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp +++ b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp @@ -144,19 +144,26 @@ class FinalChainImpl final : public FinalChain { EthBlockNumber delegation_delay() const override { return delegation_delay_; } + std::vector prepare_rewards_stats_(const PeriodData& blk) { + std::vector rewards_stats; + uint64_t dpos_vote_count = kCommitteeSize; + + // Block zero + if (!blk.previous_block_cert_votes.empty()) [[likely]] { + dpos_vote_count = dpos_eligible_total_vote_count(blk.previous_block_cert_votes[0]->getPeriod() - 1); + } + + rewards_stats.emplace_back(blk, dpos_vote_count, kCommitteeSize); + + return rewards_stats; + } + std::shared_ptr finalize_(PeriodData&& new_blk, std::vector&& finalized_dag_blk_hashes, std::shared_ptr&& anchor) { auto batch = db_->createWriteBatch(); - RewardsStats rewards_stats; - uint64_t dpos_vote_count = kCommitteeSize; - // Block zero - if (!new_blk.previous_block_cert_votes.empty()) [[unlikely]] { - dpos_vote_count = dpos_eligible_total_vote_count(new_blk.previous_block_cert_votes[0]->getPeriod() - 1); - } - // returns list of validators for new_blk.transactions - const std::vector txs_validators = rewards_stats.processStats(new_blk, dpos_vote_count, kCommitteeSize); + auto rewards_stats = prepare_rewards_stats_(new_blk); block_applying_emitter_.emit(block_header()->number + 1); @@ -180,7 +187,7 @@ class FinalChainImpl final : public FinalChain { auto const& [exec_results, state_root, total_reward] = state_api_.transition_state({new_blk.pbft_blk->getBeneficiary(), kBlockGasLimit, new_blk.pbft_blk->getTimestamp(), BlockHeader::difficulty()}, - to_state_api_transactions(new_blk.transactions), txs_validators, {}, rewards_stats); + to_state_api_transactions(new_blk.transactions), rewards_stats); TransactionReceipts receipts; receipts.reserve(exec_results.size()); diff --git a/libraries/core_libs/consensus/src/final_chain/rewards_stats.cpp b/libraries/core_libs/consensus/src/final_chain/rewards_stats.cpp index 809a48d227..45f20a82ea 100644 --- a/libraries/core_libs/consensus/src/final_chain/rewards_stats.cpp +++ b/libraries/core_libs/consensus/src/final_chain/rewards_stats.cpp @@ -2,25 +2,33 @@ #include +#include "pbft/pbft_block.hpp" + namespace taraxa { +RewardsStats::RewardsStats(const PeriodData& block, uint64_t dpos_vote_count, uint32_t committee_size) + : block_author_(block.pbft_blk->getBeneficiary()), + max_votes_weight_(std::min(committee_size, dpos_vote_count)) { + processStats(block); +} + bool RewardsStats::addTransaction(const trx_hash_t& tx_hash, const addr_t& validator) { - auto found_tx = txs_validators_.find(tx_hash); + auto found_tx = validator_by_tx_hash_.find(tx_hash); // Already processed tx - if (found_tx != txs_validators_.end()) { + if (found_tx != validator_by_tx_hash_.end()) { return false; } // New tx - txs_validators_[tx_hash] = validator; + validator_by_tx_hash_[tx_hash] = validator; return true; } std::optional RewardsStats::getTransactionValidator(const trx_hash_t& tx_hash) { - auto found_tx = txs_validators_.find(tx_hash); - if (found_tx == txs_validators_.end()) { + auto found_tx = validator_by_tx_hash_.find(tx_hash); + if (found_tx == validator_by_tx_hash_.end()) { return {}; } @@ -52,12 +60,12 @@ std::set toTrxHashesSet(const SharedTransactions& transactions) { return block_transactions_hashes_; } -void RewardsStats::initStats(const PeriodData& sync_blk, uint64_t dpos_vote_count, uint32_t committee_size) { - txs_validators_.reserve(sync_blk.transactions.size()); - validators_stats_.reserve(std::max(sync_blk.dag_blocks.size(), sync_blk.previous_block_cert_votes.size())); - auto block_transactions_hashes_ = toTrxHashesSet(sync_blk.transactions); +void RewardsStats::processStats(const PeriodData& block) { + validator_by_tx_hash_.reserve(block.transactions.size()); + validators_stats_.reserve(std::max(block.dag_blocks.size(), block.previous_block_cert_votes.size())); + auto block_transactions_hashes_ = toTrxHashesSet(block.transactions); - for (const auto& dag_block : sync_blk.dag_blocks) { + for (const auto& dag_block : block.dag_blocks) { const addr_t& dag_block_author = dag_block.getSender(); bool has_unique_transactions = false; for (const auto& tx_hash : dag_block.getTrxs()) { @@ -78,34 +86,24 @@ void RewardsStats::initStats(const PeriodData& sync_blk, uint64_t dpos_vote_coun } } // total_unique_txs_count_ should be always equal to transactions count in block - assert(txs_validators_.size() == sync_blk.transactions.size()); + assert(validator_by_tx_hash_.size() == block.transactions.size()); - max_votes_weight_ = std::min(committee_size, dpos_vote_count); - for (const auto& vote : sync_blk.previous_block_cert_votes) { + for (const auto& vote : block.previous_block_cert_votes) { addVote(vote); } -} - -std::vector RewardsStats::processStats(const PeriodData& block, uint64_t dpos_vote_count, - uint32_t committee_size) { - initStats(block, dpos_vote_count, committee_size); - - // Dag blocks validators that included transactions to be executed as first in their blocks - std::vector txs_validators; - txs_validators.reserve(block.transactions.size()); + txs_validators_.reserve(block.transactions.size()); for (const auto& tx : block.transactions) { // Non-executed trxs auto tx_validator = getTransactionValidator(tx->getHash()); assert(tx_validator.has_value()); - txs_validators.push_back(*tx_validator); + txs_validators_.push_back(*tx_validator); } - - return txs_validators; } RLP_FIELDS_DEFINE(RewardsStats::ValidatorStats, dag_blocks_count_, vote_weight_) -RLP_FIELDS_DEFINE(RewardsStats, validators_stats_, total_dag_blocks_count_, total_votes_weight_, max_votes_weight_) +RLP_FIELDS_DEFINE(RewardsStats, block_author_, validators_stats_, txs_validators_, total_dag_blocks_count_, + total_votes_weight_, max_votes_weight_) } // namespace taraxa \ No newline at end of file diff --git a/libraries/core_libs/consensus/src/final_chain/state_api.cpp b/libraries/core_libs/consensus/src/final_chain/state_api.cpp index b8d37375e7..c73ceb2f4b 100644 --- a/libraries/core_libs/consensus/src/final_chain/state_api.cpp +++ b/libraries/core_libs/consensus/src/final_chain/state_api.cpp @@ -177,14 +177,11 @@ StateDescriptor StateAPI::get_last_committed_state_descriptor() const { const StateTransitionResult& StateAPI::transition_state(const EVMBlock& block, const util::RangeView& transactions, - const util::RangeView& transactions_validators, - const util::RangeView& uncles, - const RewardsStats& rewards_stats) { + const std::vector& rewards_stats) { result_buf_transition_state_.execution_results.clear(); rlp_enc_transition_state_.clear(); c_method_args_rlp( - this_c_, rlp_enc_transition_state_, result_buf_transition_state_, block, transactions, transactions_validators, - uncles, rewards_stats); + this_c_, rlp_enc_transition_state_, result_buf_transition_state_, block, transactions, rewards_stats); return result_buf_transition_state_; } diff --git a/libraries/types/pbft_block/include/pbft/pbft_block.hpp b/libraries/types/pbft_block/include/pbft/pbft_block.hpp index a830a3afa5..9aa3a35cbf 100644 --- a/libraries/types/pbft_block/include/pbft/pbft_block.hpp +++ b/libraries/types/pbft_block/include/pbft/pbft_block.hpp @@ -16,7 +16,7 @@ namespace taraxa { */ /** - * @brief The PbftBlockk class is a PBFT block class that includes PBFT block hash, previous PBFT block hash, DAG anchor + * @brief The PbftBlock class is a PBFT block class that includes PBFT block hash, previous PBFT block hash, DAG anchor * hash, DAG blocks ordering hash, period number, timestamp, proposer address, and proposer signature. */ class PbftBlock { @@ -35,8 +35,8 @@ class 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&& reward_votes); - explicit PbftBlock(dev::RLP const& rlp); - explicit PbftBlock(bytes const& RLP); + explicit PbftBlock(const dev::RLP& rlp); + explicit PbftBlock(const bytes& RLP); /** * @brief Secure Hash Algorithm 3 @@ -77,33 +77,33 @@ class PbftBlock { * @param dag_blks DAG blocks hashes * @return PBFT block with DAG blocks in JSON */ - static Json::Value toJson(PbftBlock const& b, std::vector const& dag_blks); + static Json::Value toJson(const PbftBlock& b, const std::vector& dag_blks); /** * @brief Get PBFT block hash * @return PBFT block hash */ - auto const& getBlockHash() const { return block_hash_; } + const auto& getBlockHash() const { return block_hash_; } /** * @brief Get previous PBFT block hash * @return previous PBFT block hash */ - auto const& getPrevBlockHash() const { return prev_block_hash_; } + const auto& getPrevBlockHash() const { return prev_block_hash_; } /** * @brief Get DAG anchor hash for the finalized PBFT block * @return DAG anchor hash */ - auto const& getPivotDagBlockHash() const { return dag_block_hash_as_pivot_; } + const auto& getPivotDagBlockHash() const { return dag_block_hash_as_pivot_; } /** * @brief Get DAG blocks ordering hash * @return DAG blocks ordering hash */ - auto const& getOrderHash() const { return order_hash_; } + const auto& getOrderHash() const { return order_hash_; } - auto const& getPrevStateRoot() const { return prev_state_root_hash_; } + const auto& getPrevStateRoot() const { return prev_state_root_hash_; } /** * @brief Get period number @@ -121,7 +121,8 @@ class PbftBlock { * @brief Get PBFT block proposer address * @return PBFT block proposer address */ - auto const& getBeneficiary() const { return beneficiary_; } + const auto& getBeneficiary() const { return beneficiary_; } + const auto& getRewardVotes() const { return reward_votes_; } private: @@ -136,7 +137,7 @@ class PbftBlock { */ void checkUniqueRewardVotes(); }; -std::ostream& operator<<(std::ostream& strm, PbftBlock const& pbft_blk); +std::ostream& operator<<(std::ostream& strm, const PbftBlock& pbft_blk); /** @}*/ diff --git a/tests/final_chain_test.cpp b/tests/final_chain_test.cpp index a15be2ae36..781223a14b 100644 --- a/tests/final_chain_test.cpp +++ b/tests/final_chain_test.cpp @@ -49,12 +49,15 @@ struct FinalChainTest : WithDataDir { for (const auto& trx : trxs) { trx_hashes.emplace_back(trx->getHash()); } - DagBlock dag_blk({}, {}, {}, trx_hashes, {}, {}, secret_t::random()); + + auto proposer_keys = dev::KeyPair::create(); + DagBlock dag_blk({}, {}, {}, trx_hashes, {}, {}, proposer_keys.secret()); db->saveDagBlock(dag_blk); std::vector reward_votes_hashes; auto pbft_block = std::make_shared(kNullBlockHash, kNullBlockHash, kNullBlockHash, kNullBlockHash, expected_blk_num, - addr_t::random(), dev::KeyPair::create().secret(), std::move(reward_votes_hashes)); + addr_t::random(), proposer_keys.secret(), std::move(reward_votes_hashes)); + std::vector> votes; PeriodData period_data(pbft_block, votes); period_data.dag_blocks.push_back(dag_blk); @@ -62,7 +65,6 @@ struct FinalChainTest : WithDataDir { auto batch = db->createWriteBatch(); db->savePeriodData(period_data, batch); - db->commitWriteBatch(batch); auto result = SUT->finalize(std::move(period_data), {dag_blk.getHash()}).get(); @@ -447,6 +449,9 @@ TEST_F(FinalChainTest, failed_transaction_fee) { auto trx2_1 = std::make_shared(2, 101, 1, gas, dev::bytes(), sk, receiver); advance({trx1}); + auto blk = SUT->block_header(expected_blk_num); + auto proposer_balance = SUT->getBalance(blk->author); + EXPECT_EQ(proposer_balance.first, 21000); advance({trx2}); advance({trx3}); @@ -600,6 +605,26 @@ TEST_F(FinalChainTest, incorrect_estimation_regress) { } } +TEST_F(FinalChainTest, fee_rewards_distribution) { + auto sender_keys = dev::KeyPair::create(); + auto gas = 30000; + + const auto& receiver = dev::KeyPair::create().address(); + const auto& addr = sender_keys.address(); + const auto& sk = sender_keys.secret(); + cfg.genesis.state.initial_balances = {}; + cfg.genesis.state.initial_balances[addr] = 100000; + init(); + const auto gas_price = 1; + auto trx1 = std::make_shared(1, 100, gas_price, gas, dev::bytes(), sk, receiver); + + auto res = advance({trx1}); + auto gas_used = res->trx_receipts.front().gas_used; + auto blk = SUT->block_header(expected_blk_num); + auto proposer_balance = SUT->getBalance(blk->author); + EXPECT_EQ(proposer_balance.first, gas_used * gas_price); +} + // This test should be last as state_api isn't destructed correctly because of exception TEST_F(FinalChainTest, initial_validator_exceed_maximum_stake) { const dev::KeyPair key = dev::KeyPair::create(); diff --git a/tests/state_api_test.cpp b/tests/state_api_test.cpp index 49ce8fbe72..9d27f86220 100644 --- a/tests/state_api_test.cpp +++ b/tests/state_api_test.cpp @@ -25,9 +25,8 @@ struct TestBlock { h256 state_root; EVMBlock evm_block; vector transactions; - vector uncle_blocks; - RLP_FIELDS_DEFINE_INPLACE(hash, state_root, evm_block, transactions, uncle_blocks) + RLP_FIELDS_DEFINE_INPLACE(hash, state_root, evm_block, transactions) }; template @@ -202,8 +201,7 @@ TEST_F(StateAPITest, DISABLED_eth_mainnet_smoke) { progress_pct_log_threshold += 10; } auto const& test_block = test_blocks[blk_num]; - auto const& result = - SUT.transition_state(test_block.evm_block, test_block.transactions, {}, test_block.uncle_blocks); + auto const& result = SUT.transition_state(test_block.evm_block, test_block.transactions); ASSERT_EQ(result.state_root, test_block.state_root); SUT.transition_state_commit(); } From 363dc8ef406aeed45237acf2edf3c9121e490b10 Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Fri, 5 May 2023 13:18:34 +0200 Subject: [PATCH 006/134] refector: move process rewards stats functionality to separate class --- libraries/core_libs/CMakeLists.txt | 6 +++ .../final_chain/contract_interface.hpp | 1 - .../include/final_chain/state_api.hpp | 4 +- .../block_stats.hpp} | 16 ++++---- .../include/rewards/rewards_stats.hpp | 17 ++++++++ .../consensus/src/final_chain/final_chain.cpp | 24 +++-------- .../consensus/src/final_chain/state_api.cpp | 2 +- .../block_stats.cpp} | 20 ++++----- .../consensus/src/rewards/rewards_stats.cpp | 41 +++++++++++++++++++ submodules/taraxa-evm | 2 +- 10 files changed, 91 insertions(+), 42 deletions(-) rename libraries/core_libs/consensus/include/{final_chain/rewards_stats.hpp => rewards/block_stats.hpp} (83%) create mode 100644 libraries/core_libs/consensus/include/rewards/rewards_stats.hpp rename libraries/core_libs/consensus/src/{final_chain/rewards_stats.cpp => rewards/block_stats.cpp} (81%) create mode 100644 libraries/core_libs/consensus/src/rewards/rewards_stats.cpp diff --git a/libraries/core_libs/CMakeLists.txt b/libraries/core_libs/CMakeLists.txt index b7bb353dcd..b5d3266a2c 100644 --- a/libraries/core_libs/CMakeLists.txt +++ b/libraries/core_libs/CMakeLists.txt @@ -15,11 +15,15 @@ file(GLOB_RECURSE STORAGE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/storage/*.cpp) file(GLOB_RECURSE NODE_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/node/*.hpp) file(GLOB_RECURSE NODE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/node/*.cpp) +file(GLOB_RECURSE REWARDS_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/rewards/*.hpp) +file(GLOB_RECURSE REWARDS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/rewards/*.cpp) + set(HEADERS ${CONSENSUS_HEADERS} ${NETWORK_HEADERS} ${STORAGE_HEADERS} ${NODE_HEADERS} + ${REWARDS_HEADERS} ) set(SOURCES @@ -28,6 +32,7 @@ set(SOURCES ${STORAGE_SOURCES} ${NODE_SOURCES} ${GRAPHQL_GENERATED_SOURCES} + ${REWARDS_SOURCES} ) add_library(core_libs ${SOURCES} ${HEADERS}) @@ -36,6 +41,7 @@ target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/consensu target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/network/include) target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/node/include) target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/storage/include) +target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/rewards/include) target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) # GraphQL target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/network/graphql/gen) diff --git a/libraries/core_libs/consensus/include/final_chain/contract_interface.hpp b/libraries/core_libs/consensus/include/final_chain/contract_interface.hpp index 9c4820edec..a4b7ddabb3 100644 --- a/libraries/core_libs/consensus/include/final_chain/contract_interface.hpp +++ b/libraries/core_libs/consensus/include/final_chain/contract_interface.hpp @@ -6,7 +6,6 @@ #include "common/types.hpp" #include "final_chain/final_chain.hpp" -#include "final_chain/rewards_stats.hpp" namespace taraxa::final_chain { class ContractInterface { diff --git a/libraries/core_libs/consensus/include/final_chain/state_api.hpp b/libraries/core_libs/consensus/include/final_chain/state_api.hpp index 691494456f..c30763f864 100644 --- a/libraries/core_libs/consensus/include/final_chain/state_api.hpp +++ b/libraries/core_libs/consensus/include/final_chain/state_api.hpp @@ -5,8 +5,8 @@ #include #include "common/range_view.hpp" -#include "final_chain/rewards_stats.hpp" #include "final_chain/state_api_data.hpp" +#include "rewards/block_stats.hpp" #include "storage/storage.hpp" namespace taraxa::state_api { @@ -43,7 +43,7 @@ class StateAPI { StateDescriptor get_last_committed_state_descriptor() const; const StateTransitionResult& transition_state(const EVMBlock& block, const util::RangeView& transactions, - const std::vector& rewards_stats = {}); + const std::vector& rewards_stats = {}); void transition_state_commit(); void create_snapshot(PbftPeriod period); void prune(const std::vector& state_root_to_keep, EthBlockNumber blk_num); diff --git a/libraries/core_libs/consensus/include/final_chain/rewards_stats.hpp b/libraries/core_libs/consensus/include/rewards/block_stats.hpp similarity index 83% rename from libraries/core_libs/consensus/include/final_chain/rewards_stats.hpp rename to libraries/core_libs/consensus/include/rewards/block_stats.hpp index c5ce0a0b1c..a3f0f057ca 100644 --- a/libraries/core_libs/consensus/include/final_chain/rewards_stats.hpp +++ b/libraries/core_libs/consensus/include/rewards/block_stats.hpp @@ -7,13 +7,13 @@ #include "pbft/period_data.hpp" #include "vote/vote.hpp" -namespace taraxa { +namespace taraxa::rewards { /** * @class RewardsStats * @brief RewardsStats contains rewards statistics for single pbft block */ -class RewardsStats { +class BlockStats { public: /** * @brief setting block_author_, max_votes_weight_ and calls processStats function @@ -21,18 +21,15 @@ class RewardsStats { * @param dpos_vote_count - votes count for previous block * @param committee_size */ - RewardsStats(const PeriodData& block, uint64_t dpos_vote_count, uint32_t committee_size); + BlockStats(const PeriodData& block, uint64_t dpos_vote_count, uint32_t committee_size); HAS_RLP_FIELDS private: /** - * @brief Process PeriodData and returns vector of validators, who included provided block.transactions as first in - * dag block, e.g. returned validator on position 2 included transaction block.transactions[2] as first in his dag - * block + * @brief Process PeriodData and save stats in class for future serialization. returns * * @param block - * @return vector of validators */ void processStats(const PeriodData& block); /** @@ -78,7 +75,8 @@ class RewardsStats { // Transactions validators: tx hash -> validator that included it as first in his block std::unordered_map validator_by_tx_hash_; - // Vector with all transactions validators + // Vector with all transactions validators, who included provided block.transactions as first in dag block, + // e.g. returned validator on position 2 included transaction block.transactions[2] as first in his dag block std::vector txs_validators_; // Txs stats: validator -> ValidatorStats @@ -94,4 +92,4 @@ class RewardsStats { uint64_t max_votes_weight_{0}; }; -} // namespace taraxa \ No newline at end of file +} // namespace taraxa::rewards \ No newline at end of file diff --git a/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp b/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp new file mode 100644 index 0000000000..062d6a7b8d --- /dev/null +++ b/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp @@ -0,0 +1,17 @@ +#include "config/hardfork.hpp" +#include "rewards/block_stats.hpp" + +namespace taraxa::rewards { +class Stats { + public: + Stats(uint32_t committee_size, std::function&& dpos_eligible_total_vote_count); + + std::vector getStats(const PeriodData& current_blk); + + private: + BlockStats getBlockStats(const PeriodData& current_blk); + + const uint32_t kCommitteeSize; + const std::function dpos_eligible_total_vote_count_; +}; +} // namespace taraxa::rewards \ No newline at end of file diff --git a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp index 53256f9afe..785ba45e5c 100644 --- a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp +++ b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp @@ -6,20 +6,21 @@ #include "common/constants.hpp" #include "common/thread_pool.hpp" #include "final_chain/cache.hpp" -#include "final_chain/rewards_stats.hpp" #include "final_chain/trie_common.hpp" +#include "rewards/rewards_stats.hpp" #include "vote/vote.hpp" namespace taraxa::final_chain { class FinalChainImpl final : public FinalChain { std::shared_ptr db_; - const uint32_t kCommitteeSize; const uint64_t kBlockGasLimit; StateAPI state_api_; const bool kLightNode = false; const uint64_t kLightNodeHistory = 0; const uint32_t kMaxLevelsPerPeriod; + const uint32_t kRewardsDistributionInterval = 100; + rewards::Stats rewards_; // It is not prepared to use more then 1 thread. Examine it if you want to change threads count boost::asio::thread_pool executor_thread_{1}; @@ -49,7 +50,6 @@ class FinalChainImpl final : public FinalChain { public: FinalChainImpl(const std::shared_ptr& db, const taraxa::FullNodeConfig& config, const addr_t& node_addr) : db_(db), - kCommitteeSize(config.genesis.pbft.committee_size), kBlockGasLimit(config.genesis.pbft.gas_limit), state_api_([this](auto n) { return block_hash(n).value_or(ZeroHash()); }, // config.genesis.state, config.opts_final_chain, @@ -59,6 +59,8 @@ class FinalChainImpl final : public FinalChain { kLightNode(config.is_light_node), kLightNodeHistory(config.light_node_history), kMaxLevelsPerPeriod(config.max_levels_per_period), + rewards_(config.genesis.pbft.committee_size, + [this](EthBlockNumber n) { return dpos_eligible_total_vote_count(n); }), block_headers_cache_(config.final_chain_cache_in_blocks, [this](uint64_t blk) { return get_block_header(blk); }), block_hashes_cache_(config.final_chain_cache_in_blocks, [this](uint64_t blk) { return get_block_hash(blk); }), @@ -144,26 +146,12 @@ class FinalChainImpl final : public FinalChain { EthBlockNumber delegation_delay() const override { return delegation_delay_; } - std::vector prepare_rewards_stats_(const PeriodData& blk) { - std::vector rewards_stats; - uint64_t dpos_vote_count = kCommitteeSize; - - // Block zero - if (!blk.previous_block_cert_votes.empty()) [[likely]] { - dpos_vote_count = dpos_eligible_total_vote_count(blk.previous_block_cert_votes[0]->getPeriod() - 1); - } - - rewards_stats.emplace_back(blk, dpos_vote_count, kCommitteeSize); - - return rewards_stats; - } - std::shared_ptr finalize_(PeriodData&& new_blk, std::vector&& finalized_dag_blk_hashes, std::shared_ptr&& anchor) { auto batch = db_->createWriteBatch(); - auto rewards_stats = prepare_rewards_stats_(new_blk); + auto rewards_stats = rewards_.getStats(new_blk); block_applying_emitter_.emit(block_header()->number + 1); diff --git a/libraries/core_libs/consensus/src/final_chain/state_api.cpp b/libraries/core_libs/consensus/src/final_chain/state_api.cpp index c73ceb2f4b..3e29cf8b97 100644 --- a/libraries/core_libs/consensus/src/final_chain/state_api.cpp +++ b/libraries/core_libs/consensus/src/final_chain/state_api.cpp @@ -177,7 +177,7 @@ StateDescriptor StateAPI::get_last_committed_state_descriptor() const { const StateTransitionResult& StateAPI::transition_state(const EVMBlock& block, const util::RangeView& transactions, - const std::vector& rewards_stats) { + const std::vector& rewards_stats) { result_buf_transition_state_.execution_results.clear(); rlp_enc_transition_state_.clear(); c_method_args_rlp( diff --git a/libraries/core_libs/consensus/src/final_chain/rewards_stats.cpp b/libraries/core_libs/consensus/src/rewards/block_stats.cpp similarity index 81% rename from libraries/core_libs/consensus/src/final_chain/rewards_stats.cpp rename to libraries/core_libs/consensus/src/rewards/block_stats.cpp index 45f20a82ea..2e6d1bfbeb 100644 --- a/libraries/core_libs/consensus/src/final_chain/rewards_stats.cpp +++ b/libraries/core_libs/consensus/src/rewards/block_stats.cpp @@ -1,18 +1,18 @@ -#include "final_chain/rewards_stats.hpp" +#include "rewards/block_stats.hpp" #include #include "pbft/pbft_block.hpp" -namespace taraxa { +namespace taraxa::rewards { -RewardsStats::RewardsStats(const PeriodData& block, uint64_t dpos_vote_count, uint32_t committee_size) +BlockStats::BlockStats(const PeriodData& block, uint64_t dpos_vote_count, uint32_t committee_size) : block_author_(block.pbft_blk->getBeneficiary()), max_votes_weight_(std::min(committee_size, dpos_vote_count)) { processStats(block); } -bool RewardsStats::addTransaction(const trx_hash_t& tx_hash, const addr_t& validator) { +bool BlockStats::addTransaction(const trx_hash_t& tx_hash, const addr_t& validator) { auto found_tx = validator_by_tx_hash_.find(tx_hash); // Already processed tx @@ -26,7 +26,7 @@ bool RewardsStats::addTransaction(const trx_hash_t& tx_hash, const addr_t& valid return true; } -std::optional RewardsStats::getTransactionValidator(const trx_hash_t& tx_hash) { +std::optional BlockStats::getTransactionValidator(const trx_hash_t& tx_hash) { auto found_tx = validator_by_tx_hash_.find(tx_hash); if (found_tx == validator_by_tx_hash_.end()) { return {}; @@ -35,7 +35,7 @@ std::optional RewardsStats::getTransactionValidator(const trx_hash_t& tx return {found_tx->second}; } -bool RewardsStats::addVote(const std::shared_ptr& vote) { +bool BlockStats::addVote(const std::shared_ptr& vote) { // Set valid cert vote to validator auto& validator_stats = validators_stats_[vote->getVoterAddr()]; assert(validator_stats.vote_weight_ == 0); @@ -60,7 +60,7 @@ std::set toTrxHashesSet(const SharedTransactions& transactions) { return block_transactions_hashes_; } -void RewardsStats::processStats(const PeriodData& block) { +void BlockStats::processStats(const PeriodData& block) { validator_by_tx_hash_.reserve(block.transactions.size()); validators_stats_.reserve(std::max(block.dag_blocks.size(), block.previous_block_cert_votes.size())); auto block_transactions_hashes_ = toTrxHashesSet(block.transactions); @@ -102,8 +102,8 @@ void RewardsStats::processStats(const PeriodData& block) { } } -RLP_FIELDS_DEFINE(RewardsStats::ValidatorStats, dag_blocks_count_, vote_weight_) -RLP_FIELDS_DEFINE(RewardsStats, block_author_, validators_stats_, txs_validators_, total_dag_blocks_count_, +RLP_FIELDS_DEFINE(BlockStats::ValidatorStats, dag_blocks_count_, vote_weight_) +RLP_FIELDS_DEFINE(BlockStats, block_author_, validators_stats_, txs_validators_, total_dag_blocks_count_, total_votes_weight_, max_votes_weight_) -} // namespace taraxa \ No newline at end of file +} // namespace taraxa::rewards \ No newline at end of file diff --git a/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp b/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp new file mode 100644 index 0000000000..fa201db1ce --- /dev/null +++ b/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp @@ -0,0 +1,41 @@ +#include "rewards/rewards_stats.hpp" + +#include "storage/storage.hpp" + +namespace taraxa::rewards { +Stats::Stats(uint32_t committee_size, std::function&& dpos_eligible_total_vote_count) + : kCommitteeSize(committee_size), dpos_eligible_total_vote_count_(dpos_eligible_total_vote_count) {} + +// std::vector processBlockHardfork(const PeriodData& current_blk, uint32_t interval) { +// const auto current = current_blk.pbft_blk->getPeriod(); +// // skip for intermediate blocks +// if (current % interval != 0) { +// return {}; +// } + +// std::vector rewards_stats; +// rewards_stats.reserve(interval); +// // add rewards stats for (last_distribution_block, current_block) +// for (auto p = current - interval + 1; p < current; ++p) { +// auto blk = PeriodData(db_->getPeriodDataRaw(p)); +// rewards_stats.emplace_back(get_block_rewards_stats(blk)); +// } +// // add current block rewards stats +// rewards_stats.emplace_back(get_block_rewards_stats(current_blk)); + +// return rewards_stats; +// } + +BlockStats Stats::getBlockStats(const PeriodData& blk) { + uint64_t dpos_vote_count = kCommitteeSize; + + // Block zero + if (!blk.previous_block_cert_votes.empty()) [[likely]] { + dpos_vote_count = dpos_eligible_total_vote_count_(blk.previous_block_cert_votes[0]->getPeriod() - 1); + } + + return BlockStats{blk, dpos_vote_count, kCommitteeSize}; +} + +std::vector Stats::getStats(const PeriodData& current_blk) { return {getBlockStats(current_blk)}; } +} // namespace taraxa::rewards \ No newline at end of file diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index c620c6fe72..ffd207a4cb 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit c620c6fe72301f6eef8d0fe24c5bdf8db8e9f39d +Subproject commit ffd207a4cb80919c1cdbf26f919b3a158ce497f3 From 184a7a25c0d350039afcaee180d66845342ad4f1 Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Tue, 9 May 2023 12:39:19 +0200 Subject: [PATCH 007/134] feat: add option to change rewards distribution frequency with hardfork --- .../config_jsons/devnet/devnet_genesis.json | 4 + .../config_jsons/mainnet/mainnet_genesis.json | 4 + .../config_jsons/testnet/testnet_genesis.json | 4 + libraries/config/CMakeLists.txt | 4 +- libraries/config/include/config/hardfork.hpp | 13 +- .../config/include/config/state_config.hpp | 4 +- libraries/config/src/hardfork.cpp | 17 +- libraries/config/src/state_config.cpp | 4 +- .../consensus/include/rewards/block_stats.hpp | 4 +- .../include/rewards/rewards_stats.hpp | 43 +++- .../consensus/src/final_chain/final_chain.cpp | 4 +- .../consensus/src/rewards/rewards_stats.cpp | 80 +++++--- .../storage/include/storage/storage.hpp | 3 + libraries/core_libs/storage/src/storage.cpp | 10 + tests/CMakeLists.txt | 3 + tests/rewards_stats_test.cpp | 185 ++++++++++++++++++ .../test_util/include/test_util/test_util.hpp | 2 +- tests/test_util/src/test_util.cpp | 6 +- 18 files changed, 350 insertions(+), 44 deletions(-) create mode 100644 tests/rewards_stats_test.cpp diff --git a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json index f86fc92377..ff7bf49bb2 100644 --- a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json @@ -226,6 +226,10 @@ "7e4aa664f71de4e9d0b4a6473d796372639bdcde": "0x1027e72f1f12813088000000", "ee1326fbf7d9322e5ea02c6fe5eb63535fceccd1": "0x52b7d2dcc80cd2e4000000" }, + "hardforks": { + "rewards_distribution_frequency": { + } + }, "gas_price": { "blocks": 200, "percentile": 60, diff --git a/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json b/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json index 5c9777aae6..a488289bce 100644 --- a/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json @@ -243,6 +243,10 @@ } ] }, + "hardforks": { + "rewards_distribution_frequency": { + } + }, "initial_balances": { "723304d1357a2334fcf902aa3d232f5139080a1b": "0xd53323b7ca3737afbb45000", "b0800c7af0a6aec0ff8dbe01708bd8e300c6305b": "0x208b1d135e4a8000", diff --git a/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json b/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json index f07f92f352..7a7f30890f 100644 --- a/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json @@ -1698,6 +1698,10 @@ "a903715b57d3bf62e098a6a643c6924d9bdacec4": "0x170a0f5040e50400000", "5bd47fef8e8dcb6677c2957ecd78b8232354f145": "0x191cf61eb2bec223400" }, + "hardforks": { + "rewards_distribution_frequency": { + } + }, "gas_price": { "blocks": 200, "percentile": 60, diff --git a/libraries/config/CMakeLists.txt b/libraries/config/CMakeLists.txt index 75616c110c..5df88e6af7 100644 --- a/libraries/config/CMakeLists.txt +++ b/libraries/config/CMakeLists.txt @@ -7,7 +7,7 @@ set(HEADERS include/config/dag_config.hpp include/config/pbft_config.hpp include/config/state_config.hpp - # include/config/hardfork.hpp + include/config/hardfork.hpp ) set(SOURCES @@ -18,7 +18,7 @@ set(SOURCES src/dag_config.cpp src/pbft_config.cpp src/state_config.cpp - # src/hardfork.cpp + src/hardfork.cpp ) # Configure file with version diff --git a/libraries/config/include/config/hardfork.hpp b/libraries/config/include/config/hardfork.hpp index e0ca9262a5..f0364b5dbc 100644 --- a/libraries/config/include/config/hardfork.hpp +++ b/libraries/config/include/config/hardfork.hpp @@ -5,7 +5,18 @@ #include "common/encoding_rlp.hpp" struct Hardforks { - uint64_t fix_genesis_fork_block = 0; + /* + * @brief key is block number at which change is applied and value is new distribution interval. + * Default distribution frequency is every block + * To change rewards distribution frequency we should add a new element in map below. + * For example {{101, 20}, {201, 10}} means: + * 1. for blocks [1,100] we are distributing rewards every block + * 2. for blocks [101, 200] rewards are distributed every 20 block. On blocks 120, 140, etc. + * 3. for blocks after 201 rewards are distributed every 10 block. On blocks 210, 220, etc. + */ + using RewardsDistributionMap = std::map; + RewardsDistributionMap rewards_distribution_frequency; + HAS_RLP_FIELDS }; diff --git a/libraries/config/include/config/state_config.hpp b/libraries/config/include/config/state_config.hpp index 1cbda1b401..15a0a6da18 100644 --- a/libraries/config/include/config/state_config.hpp +++ b/libraries/config/include/config/state_config.hpp @@ -6,7 +6,7 @@ #include "common/encoding_rlp.hpp" #include "common/types.hpp" #include "common/vrf_wrapper.hpp" -// #include "config/hardfork.hpp" +#include "config/hardfork.hpp" namespace taraxa::state_api { @@ -61,7 +61,7 @@ struct Config { EVMChainConfig evm_chain_config; BalanceMap initial_balances; DPOSConfig dpos; - // Hardforks hardforks; + Hardforks hardforks; HAS_RLP_FIELDS }; diff --git a/libraries/config/src/hardfork.cpp b/libraries/config/src/hardfork.cpp index 950558fc19..fc4f804c0e 100644 --- a/libraries/config/src/hardfork.cpp +++ b/libraries/config/src/hardfork.cpp @@ -2,14 +2,23 @@ Json::Value enc_json(const Hardforks& obj) { Json::Value json(Json::objectValue); - json["fix_genesis_fork_block"] = dev::toJS(obj.fix_genesis_fork_block); + + auto& rewards = json["rewards_distribution_frequency"]; + rewards = Json::objectValue; + for (auto i = obj.rewards_distribution_frequency.begin(); i != obj.rewards_distribution_frequency.end(); ++i) { + rewards[std::to_string(i->first)] = i->second; + } return json; } void dec_json(const Json::Value& json, Hardforks& obj) { - if (auto const& e = json["fix_genesis_fork_block"]) { - obj.fix_genesis_fork_block = dev::getUInt(e); + if (const auto& e = json["rewards_distribution_frequency"]) { + assert(e.isObject()); + + for (auto itr = e.begin(); itr != e.end(); ++itr) { + obj.rewards_distribution_frequency[itr.key().asUInt64()] = itr->asUInt64(); + } } } -RLP_FIELDS_DEFINE(Hardforks, fix_genesis_fork_block) +RLP_FIELDS_DEFINE(Hardforks, rewards_distribution_frequency) diff --git a/libraries/config/src/state_config.cpp b/libraries/config/src/state_config.cpp index 29278f78e8..a284a65e3e 100644 --- a/libraries/config/src/state_config.cpp +++ b/libraries/config/src/state_config.cpp @@ -22,14 +22,14 @@ void dec_json(const Json::Value& /*json*/, uint64_t chain_id, EVMChainConfig& ob void append_json(Json::Value& json, const Config& obj) { json["evm_chain_config"] = enc_json(obj.evm_chain_config); json["initial_balances"] = enc_json(obj.initial_balances); - // json["hardforks"] = enc_json(obj.hardforks); + json["hardforks"] = enc_json(obj.hardforks); json["dpos"] = enc_json(obj.dpos); } void dec_json(const Json::Value& json, Config& obj) { dec_json(json["evm_chain_config"], json["chain_id"].asUInt(), obj.evm_chain_config); dec_json(json["initial_balances"], obj.initial_balances); - // dec_json(json["hardforks"], obj.hardforks); + dec_json(json["hardforks"], obj.hardforks); dec_json(json["dpos"], obj.dpos); } diff --git a/libraries/core_libs/consensus/include/rewards/block_stats.hpp b/libraries/core_libs/consensus/include/rewards/block_stats.hpp index a3f0f057ca..2e80eff84b 100644 --- a/libraries/core_libs/consensus/include/rewards/block_stats.hpp +++ b/libraries/core_libs/consensus/include/rewards/block_stats.hpp @@ -15,6 +15,8 @@ namespace taraxa::rewards { */ class BlockStats { public: + // Needed for RLP + BlockStats() = default; /** * @brief setting block_author_, max_votes_weight_ and calls processStats function * @@ -58,7 +60,7 @@ class BlockStats { */ bool addVote(const std::shared_ptr& vote); - private: + protected: struct ValidatorStats { // count of rewardable(with 1 or more unique transactions) DAG blocks produced by this validator uint32_t dag_blocks_count_ = 0; diff --git a/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp b/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp index 062d6a7b8d..22fca73188 100644 --- a/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp +++ b/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp @@ -1,17 +1,54 @@ #include "config/hardfork.hpp" #include "rewards/block_stats.hpp" +#include "storage/storage.hpp" namespace taraxa::rewards { +/* + * @brief class that is managing rewards stats processing and hardforks(intervals changes) + * So intermediate blocks stats are stored in the vector in data(to restore on the node restart) + * and full list of interval stats is returned in the end of interval + */ class Stats { public: - Stats(uint32_t committee_size, std::function&& dpos_eligible_total_vote_count); + Stats(uint32_t committee_size, const Hardforks::RewardsDistributionMap& rdm, std::shared_ptr db, + std::function&& dpos_eligible_total_vote_count); - std::vector getStats(const PeriodData& current_blk); + /* + * @brief processing passed block and returns stats that should be processed at this block + * @param current_blk block to process + * @return vector that should be processed at current block + */ + std::vector processStats(const PeriodData& current_blk); - private: + protected: + /* + * @brief load current interval stats from database + */ + void loadFromDb(); + /* + * @brief returns rewards distribution frequency for specified period + */ + uint32_t getCurrentDistributionFrequency(uint64_t current_period) const; + /* + * @brief gets all needed data and makes(processes) BlocksStats + * @param current_blk block to process + * @return block statistics needed for rewards distribution + */ BlockStats getBlockStats(const PeriodData& current_blk); + /* + * @brief saves stats to database to not lose this data in case of node restart + */ + void saveBlockStats(uint64_t number, const BlockStats& stats); + /* + * @brief called on start of new rewards interval. clears blocks_stats_ collection + * and removes all data saved in db column + */ + void clear(); const uint32_t kCommitteeSize; + const Hardforks::RewardsDistributionMap kRewardsDistributionFrequency; + std::shared_ptr db_; const std::function dpos_eligible_total_vote_count_; + std::vector blocks_stats_; }; } // namespace taraxa::rewards \ No newline at end of file diff --git a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp index 785ba45e5c..752e256d27 100644 --- a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp +++ b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp @@ -59,7 +59,7 @@ class FinalChainImpl final : public FinalChain { kLightNode(config.is_light_node), kLightNodeHistory(config.light_node_history), kMaxLevelsPerPeriod(config.max_levels_per_period), - rewards_(config.genesis.pbft.committee_size, + rewards_(config.genesis.pbft.committee_size, config.genesis.state.hardforks.rewards_distribution_frequency, db_, [this](EthBlockNumber n) { return dpos_eligible_total_vote_count(n); }), block_headers_cache_(config.final_chain_cache_in_blocks, [this](uint64_t blk) { return get_block_header(blk); }), @@ -151,7 +151,7 @@ class FinalChainImpl final : public FinalChain { std::shared_ptr&& anchor) { auto batch = db_->createWriteBatch(); - auto rewards_stats = rewards_.getStats(new_blk); + auto rewards_stats = rewards_.processStats(new_blk); block_applying_emitter_.emit(block_header()->number + 1); diff --git a/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp b/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp index fa201db1ce..1d4a03f5e5 100644 --- a/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp +++ b/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp @@ -3,28 +3,41 @@ #include "storage/storage.hpp" namespace taraxa::rewards { -Stats::Stats(uint32_t committee_size, std::function&& dpos_eligible_total_vote_count) - : kCommitteeSize(committee_size), dpos_eligible_total_vote_count_(dpos_eligible_total_vote_count) {} - -// std::vector processBlockHardfork(const PeriodData& current_blk, uint32_t interval) { -// const auto current = current_blk.pbft_blk->getPeriod(); -// // skip for intermediate blocks -// if (current % interval != 0) { -// return {}; -// } - -// std::vector rewards_stats; -// rewards_stats.reserve(interval); -// // add rewards stats for (last_distribution_block, current_block) -// for (auto p = current - interval + 1; p < current; ++p) { -// auto blk = PeriodData(db_->getPeriodDataRaw(p)); -// rewards_stats.emplace_back(get_block_rewards_stats(blk)); -// } -// // add current block rewards stats -// rewards_stats.emplace_back(get_block_rewards_stats(current_blk)); - -// return rewards_stats; -// } +Stats::Stats(uint32_t committee_size, const Hardforks::RewardsDistributionMap& rdm, std::shared_ptr db, + std::function&& dpos_eligible_total_vote_count) + : kCommitteeSize(committee_size), + kRewardsDistributionFrequency(rdm), + db_(std::move(db)), + dpos_eligible_total_vote_count_(dpos_eligible_total_vote_count) { + loadFromDb(); +} + +void Stats::loadFromDb() { + auto i = db_->getColumnIterator(DB::Columns::block_rewards_stats); + for (i->SeekToFirst(); i->Valid(); i->Next()) { + blocks_stats_.push_back(util::rlp_dec(dev::RLP(i->value().ToString()))); + } +} + +void Stats::saveBlockStats(uint64_t period, const BlockStats& stats) { + dev::RLPStream encoding; + stats.rlp(encoding); + + db_->insert(DB::Columns::block_rewards_stats, period, encoding.out()); +} + +uint32_t Stats::getCurrentDistributionFrequency(uint64_t current_block) const { + auto itr = kRewardsDistributionFrequency.upper_bound(current_block); + if (kRewardsDistributionFrequency.empty() || itr == kRewardsDistributionFrequency.begin()) { + return 1; + } + return (--itr)->second; +} + +void Stats::clear() { + blocks_stats_.clear(); + db_->deleteColumnData(DB::Columns::block_rewards_stats); +} BlockStats Stats::getBlockStats(const PeriodData& blk) { uint64_t dpos_vote_count = kCommitteeSize; @@ -37,5 +50,26 @@ BlockStats Stats::getBlockStats(const PeriodData& blk) { return BlockStats{blk, dpos_vote_count, kCommitteeSize}; } -std::vector Stats::getStats(const PeriodData& current_blk) { return {getBlockStats(current_blk)}; } +std::vector Stats::processStats(const PeriodData& current_blk) { + const auto current_period = current_blk.pbft_blk->getPeriod(); + const auto frequency = getCurrentDistributionFrequency(current_period); + + // Distribute rewards every block + if (frequency == 1) { + return {getBlockStats(current_blk)}; + } + + blocks_stats_.push_back(getBlockStats(current_blk)); + // Blocks between distribution. Process and save for future processing + if (current_period % frequency != 0) { + // Save to db, so in case of restart data could be just loaded for the period + saveBlockStats(current_period, *blocks_stats_.rbegin()); + return {}; + } + + std::vector res(std::move(blocks_stats_)); + clear(); + return res; +} + } // namespace taraxa::rewards \ No newline at end of file diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index 8d6f00182d..1dedb67234 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -121,6 +121,8 @@ class DbStorage : public std::enable_shared_from_this { COLUMN(final_chain_log_blooms_index); COLUMN_W_COMP(sortition_params_change, getIntComparator()); + COLUMN_W_COMP(block_rewards_stats, getIntComparator()); + #undef COLUMN #undef COLUMN_W_COMP }; @@ -177,6 +179,7 @@ class DbStorage : public std::enable_shared_from_this { void disableSnapshots(); void enableSnapshots(); void updateDbVersions(); + void deleteColumnData(const Column& c); uint32_t getMajorVersion() const; std::unique_ptr getColumnIterator(const Column& c); diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index aeb076e2b8..feb9d05f4b 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -91,6 +91,16 @@ void DbStorage::updateDbVersions() { saveStatusField(StatusDbField::DbMinorVersion, TARAXA_DB_MINOR_VERSION); } +void DbStorage::deleteColumnData(const Column& c) { + checkStatus(db_->DropColumnFamily(handle(c))); + + auto options = rocksdb::ColumnFamilyOptions(); + if (c.comparator_) { + options.comparator = c.comparator_; + } + checkStatus(db_->CreateColumnFamily(options, c.name(), &handles_[c.ordinal_])); +} + void DbStorage::rebuildColumns(const rocksdb::Options& options) { std::unique_ptr db; std::vector column_families; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b5b9bc2bf3..f9613497b3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -69,6 +69,9 @@ add_executable(vote_test vote_test.cpp) target_link_libraries(vote_test test_util) add_test(vote_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/vote_test) +add_executable(rewards_stats_test rewards_stats_test.cpp) +target_link_libraries(rewards_stats_test test_util) +add_test(rewards_stats_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rewards_stats_test) add_executable(tarcap_threadpool_test tarcap_threadpool_test.cpp) target_link_libraries(tarcap_threadpool_test test_util) diff --git a/tests/rewards_stats_test.cpp b/tests/rewards_stats_test.cpp new file mode 100644 index 0000000000..cce156c735 --- /dev/null +++ b/tests/rewards_stats_test.cpp @@ -0,0 +1,185 @@ +#include "rewards/rewards_stats.hpp" + +#include +#include +#include +#include + +#include "test_util/gtest.hpp" +#include "test_util/samples.hpp" + +namespace taraxa::core_tests { + +auto g_secret = dev::Secret("3800b2875669d9b2053c1aff9224ecfdc411423aac5b5a73d7a45ced1c3b9dcd", + dev::Secret::ConstructFromStringType::FromHex); +auto g_key_pair = Lazy([] { return dev::KeyPair(g_secret); }); + +struct RewardsStatsTest : NodesTest {}; + +class TestableRewardsStats : public rewards::Stats { + public: + TestableRewardsStats(const Hardforks::RewardsDistributionMap& rdm, std::shared_ptr db) + : rewards::Stats(100, rdm, db, [](auto) { return 100; }) {} + std::vector getStats() { return blocks_stats_; } +}; + +class TestableBlockStats : public rewards::BlockStats { + public: + const addr_t& getAuthor() const { return block_author_; } +}; + +TEST_F(RewardsStatsTest, defaultDistribution) { + auto db = std::make_shared(data_dir / "db"); + + std::vector> empty_votes; + auto rewards_stats = TestableRewardsStats({}, db); + + for (auto i = 1; i < 5; ++i) { + PeriodData block(make_simple_pbft_block(blk_hash_t(i), i), empty_votes); + auto stats = rewards_stats.processStats(block); + ASSERT_EQ(stats.size(), 1); + ASSERT_TRUE(rewards_stats.getStats().empty()); + } +} + +TEST_F(RewardsStatsTest, statsSaving) { + auto db = std::make_shared(data_dir / "db"); + + // distribute every 5 blocks + Hardforks::RewardsDistributionMap distribution{{0, 5}}; + + std::vector> empty_votes; + std::vector block_authors; + { + auto rewards_stats = TestableRewardsStats(distribution, db); + + for (auto i = 1; i < 5; ++i) { + auto kp = dev::KeyPair::create(); + block_authors.push_back(kp.address()); + + PeriodData block(make_simple_pbft_block(blk_hash_t(i), i, kp.secret()), empty_votes); + auto stats = rewards_stats.processStats(block); + ASSERT_EQ(rewards_stats.getStats().size(), block_authors.size()); + ASSERT_TRUE(stats.empty()); + } + } + { + // Load from db + auto rewards_stats = TestableRewardsStats(distribution, db); + auto stats = rewards_stats.getStats(); + ASSERT_EQ(rewards_stats.getStats().size(), block_authors.size()); + + for (size_t i = 0; i < stats.size(); ++i) { + auto stats_with_get = reinterpret_cast(&stats[i]); + ASSERT_EQ(stats_with_get->getAuthor(), block_authors[i]); + } + } +} + +TEST_F(RewardsStatsTest, statsCleaning) { + auto db = std::make_shared(data_dir / "db"); + + // distribute every 5 blocks + Hardforks::RewardsDistributionMap distribution{{0, 5}}; + + std::vector> empty_votes; + std::vector block_authors; + { + auto rewards_stats = TestableRewardsStats(distribution, db); + + for (auto i = 1; i < 5; ++i) { + auto kp = dev::KeyPair::create(); + block_authors.push_back(kp.address()); + + PeriodData block(make_simple_pbft_block(blk_hash_t(i), i, kp.secret()), empty_votes); + auto stats = rewards_stats.processStats(block); + ASSERT_EQ(rewards_stats.getStats().size(), block_authors.size()); + ASSERT_TRUE(stats.empty()); + } + + // Process block 5 after which we should have no stats elements in db + PeriodData block(make_simple_pbft_block(blk_hash_t(5), 5), empty_votes); + rewards_stats.processStats(block); + } + + // Load from db + auto rewards_stats = TestableRewardsStats(distribution, db); + ASSERT_TRUE(rewards_stats.getStats().empty()); +} + +TEST_F(RewardsStatsTest, statsProcessing) { + auto db = std::make_shared(data_dir / "db"); + // distribute every 10 blocks + auto rewards_stats = TestableRewardsStats({{0, 10}}, db); + + std::vector> empty_votes; + std::vector block_authors; + + // make blocks [1,9] and process them. output of processStats should be empty + for (auto i = 1; i < 10; ++i) { + auto kp = dev::KeyPair::create(); + block_authors.push_back(kp.address()); + + PeriodData block(make_simple_pbft_block(blk_hash_t(i), i, kp.secret()), empty_votes); + auto stats = rewards_stats.processStats(block); + ASSERT_TRUE(stats.empty()); + ASSERT_EQ(rewards_stats.getStats().size(), block_authors.size()); + } + + auto kp = dev::KeyPair::create(); + block_authors.push_back(kp.address()); + + PeriodData block(make_simple_pbft_block(blk_hash_t(10), 10, kp.secret()), empty_votes); + auto stats = rewards_stats.processStats(block); + ASSERT_EQ(stats.size(), block_authors.size()); + + for (size_t i = 0; i < stats.size(); ++i) { + auto stats_with_get = reinterpret_cast(&stats[i]); + ASSERT_EQ(stats_with_get->getAuthor(), block_authors[i]); + } + ASSERT_TRUE(rewards_stats.getStats().empty()); +} + +TEST_F(RewardsStatsTest, distributionChange) { + auto db = std::make_shared(data_dir / "db"); + + Hardforks::RewardsDistributionMap distribution{{6, 5}, {11, 2}}; + + auto rewards_stats = TestableRewardsStats(distribution, db); + + std::vector> empty_votes; + uint64_t period = 1; + for (; period <= 5; ++period) { + PeriodData block(make_simple_pbft_block(blk_hash_t(period), period), empty_votes); + auto stats = rewards_stats.processStats(block); + ASSERT_FALSE(stats.empty()); + } + { + // make blocks [1,9] and process them. output of processStats should be empty + for (; period < 10; ++period) { + PeriodData block(make_simple_pbft_block(blk_hash_t(period), period), empty_votes); + auto stats = rewards_stats.processStats(block); + ASSERT_TRUE(stats.empty()); + } + PeriodData block(make_simple_pbft_block(blk_hash_t(period), period), empty_votes); + auto stats = rewards_stats.processStats(block); + } + + PeriodData block(make_simple_pbft_block(blk_hash_t(period), period), empty_votes); + auto stats = rewards_stats.processStats(block); +} + +} // namespace taraxa::core_tests + +using namespace taraxa; +int main(int argc, char** argv) { + taraxa::static_init(); + + auto logging = logger::createDefaultLoggingConfig(); + logging.verbosity = logger::Verbosity::Error; + addr_t node_addr; + logging.InitLogging(node_addr); + + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/tests/test_util/include/test_util/test_util.hpp b/tests/test_util/include/test_util/test_util.hpp index cdbd8659dc..951a22dec3 100644 --- a/tests/test_util/include/test_util/test_util.hpp +++ b/tests/test_util/include/test_util/test_util.hpp @@ -169,7 +169,7 @@ state_api::BalanceMap effective_initial_balances(const state_api::Config& cfg); u256 own_effective_genesis_bal(const FullNodeConfig& cfg); std::shared_ptr make_simple_pbft_block(const h256& hash, uint64_t period, - const h256& anchor_hash = kNullBlockHash); + const secret_t& pk = secret_t::random()); std::vector getOrderedDagBlocks(const std::shared_ptr& db); diff --git a/tests/test_util/src/test_util.cpp b/tests/test_util/src/test_util.cpp index 6599c4ea84..7c045c3df4 100644 --- a/tests/test_util/src/test_util.cpp +++ b/tests/test_util/src/test_util.cpp @@ -96,10 +96,10 @@ u256 own_effective_genesis_bal(const FullNodeConfig& cfg) { return effective_initial_balances(cfg.genesis.state)[dev::toAddress(dev::Secret(cfg.node_secret))]; } -std::shared_ptr make_simple_pbft_block(const h256& hash, uint64_t period, const h256& anchor_hash) { +std::shared_ptr make_simple_pbft_block(const h256& hash, uint64_t period, const secret_t& pk) { std::vector reward_votes_hashes; - return std::make_shared(hash, anchor_hash, kNullBlockHash, kNullBlockHash, period, addr_t(0), - secret_t::random(), std::move(reward_votes_hashes)); + return std::make_shared(hash, kNullBlockHash, kNullBlockHash, kNullBlockHash, period, addr_t(0), pk, + std::move(reward_votes_hashes)); } std::vector getOrderedDagBlocks(const std::shared_ptr& db) { From a2bfdee93b77f2e70c8bfbed1f1c1f98a82179e9 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Fri, 12 May 2023 10:12:53 +0200 Subject: [PATCH 008/134] feat: clean LOG.old* files on db start --- .../storage/include/storage/storage.hpp | 4 +++ libraries/core_libs/storage/src/storage.cpp | 30 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index 8d6f00182d..60986942e4 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -178,6 +178,10 @@ class DbStorage : public std::enable_shared_from_this { void enableSnapshots(); void updateDbVersions(); + // For removal of LOG.old.* files in the database + void removeOldLogFiles() const; + void removeFilesWithPattern(const std::string& directory, const std::regex& pattern) const; + uint32_t getMajorVersion() const; std::unique_ptr getColumnIterator(const Column& c); diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index aeb076e2b8..160529236a 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "config/version.hpp" #include "dag/sortition_params_manager.hpp" @@ -42,6 +43,8 @@ DbStorage::DbStorage(fs::path const& path, uint32_t db_snapshot_each_n_pbft_bloc } fs::create_directories(db_path_); + removeOldLogFiles(); + rocksdb::Options options; options.create_missing_column_families = true; options.create_if_missing = true; @@ -86,6 +89,26 @@ DbStorage::DbStorage(fs::path const& path, uint32_t db_snapshot_each_n_pbft_bloc } } +void DbStorage::removeOldLogFiles() const { + const std::regex filePattern("LOG\\.old\\.\\d+"); + removeFilesWithPattern(db_path_, filePattern); + removeFilesWithPattern(state_db_path_, filePattern); +} + +void DbStorage::removeFilesWithPattern(const std::string& directory, const std::regex& pattern) const { + try { + for (const auto& entry : std::filesystem::directory_iterator(directory)) { + const std::string& filename = entry.path().filename().string(); + if (std::regex_match(filename, pattern)) { + std::filesystem::remove(entry.path()); + LOG(log_dg_) << "Removed file: " << filename << std::endl; + } + } + } catch (const std::filesystem::filesystem_error& e) { + LOG(log_dg_) << "Error accessing directory: " << e.what() << std::endl; + } +} + void DbStorage::updateDbVersions() { saveStatusField(StatusDbField::DbMajorVersion, TARAXA_DB_MAJOR_VERSION); saveStatusField(StatusDbField::DbMinorVersion, TARAXA_DB_MINOR_VERSION); @@ -454,7 +477,8 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period) { auto start_slice = toSlice(start_period); auto end_slice = toSlice(end_period); for (auto period = start_period; period < end_period; period++) { - // Find transactions included in the old blocks and delete data related to these transactions to free disk space + // Find transactions included in the old blocks and delete data related to these transactions to free disk + // space auto trx_hashes_raw = lookup(period, DB::Columns::final_chain_transaction_hashes_by_blk_number); auto hashes_count = trx_hashes_raw.size() / trx_hash_t::size; for (uint32_t i = 0; i < hashes_count; i++) { @@ -471,8 +495,8 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period) { commitWriteBatch(write_batch); db_->DeleteRange(write_options_, handle(Columns::period_data), start_slice, end_slice); - // Deletion alone does not guarantee that the disk space is freed, these CompactRange methods actually compact the - // data in the database and free disk space + // Deletion alone does not guarantee that the disk space is freed, these CompactRange methods actually compact + // the data in the database and free disk space db_->CompactRange({}, handle(Columns::period_data), &start_slice, &end_slice); db_->CompactRange({}, handle(Columns::final_chain_receipt_by_trx_hash), nullptr, nullptr); db_->CompactRange({}, handle(Columns::final_chain_transaction_hashes_by_blk_number), nullptr, nullptr); From d964bafb279d4d9644404ff152cd5b34a736fd9b Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Fri, 12 May 2023 17:01:27 +0200 Subject: [PATCH 009/134] chore: increase timers for more stable test runs --- tests/network_test.cpp | 8 ++++---- tests/p2p_test.cpp | 2 +- tests/test_util/src/test_util.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/network_test.cpp b/tests/network_test.cpp index c8097b8440..d40c6d94ab 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -52,7 +52,7 @@ TEST_F(NetworkTest, transfer_block) { SharedTransactions transactions({g_signed_trx_samples[0], g_signed_trx_samples[1]}); nw2->getSpecificHandler()->onNewTransactions(std::move(transactions)); - EXPECT_HAPPENS({10s, 200ms}, [&](auto& ctx) { + EXPECT_HAPPENS({60s, 100ms}, [&](auto& ctx) { nw1->setPendingPeersToReady(); nw2->setPendingPeersToReady(); WAIT_EXPECT_EQ(ctx, nw1->getPeerCount(), 1) @@ -318,7 +318,7 @@ TEST_F(NetworkTest, transfer_transaction) { nw1->start(); nw2->start(); - EXPECT_HAPPENS({20s, 100ms}, [&](auto& ctx) { + EXPECT_HAPPENS({60s, 100ms}, [&](auto& ctx) { nw1->setPendingPeersToReady(); nw2->setPendingPeersToReady(); WAIT_EXPECT_EQ(ctx, nw1->getPeerCount(), 1) @@ -365,7 +365,7 @@ TEST_F(NetworkTest, save_network) { nw2->start(); nw3->start(); - EXPECT_HAPPENS({120s, 500ms}, [&](auto& ctx) { + EXPECT_HAPPENS({120s, 100ms}, [&](auto& ctx) { nw1->setPendingPeersToReady(); nw2->setPendingPeersToReady(); nw3->setPendingPeersToReady(); @@ -382,7 +382,7 @@ TEST_F(NetworkTest, save_network) { nw2->start(); nw3->start(); - EXPECT_HAPPENS({120s, 500ms}, [&](auto& ctx) { + EXPECT_HAPPENS({120s, 100ms}, [&](auto& ctx) { nw2->setPendingPeersToReady(); nw3->setPendingPeersToReady(); WAIT_EXPECT_EQ(ctx, nw2->getPeerCount(), 1) diff --git a/tests/p2p_test.cpp b/tests/p2p_test.cpp index 1e9dcf31be..9a215160a7 100644 --- a/tests/p2p_test.cpp +++ b/tests/p2p_test.cpp @@ -311,7 +311,7 @@ TEST_F(P2PTest, multiple_capabilities) { std::filesystem::remove_all("/tmp/nw3"); }; auto wait_for_connection = [](std::shared_ptr nw1, std::shared_ptr nw2) { - EXPECT_HAPPENS({15s, 500ms}, [&](auto &ctx) { + EXPECT_HAPPENS({60s, 100ms}, [&](auto &ctx) { nw1->setPendingPeersToReady(); nw2->setPendingPeersToReady(); WAIT_EXPECT_EQ(ctx, nw1->getPeerCount(), 1) diff --git a/tests/test_util/src/test_util.cpp b/tests/test_util/src/test_util.cpp index 7c045c3df4..7c13b18e86 100644 --- a/tests/test_util/src/test_util.cpp +++ b/tests/test_util/src/test_util.cpp @@ -302,7 +302,7 @@ std::vector NodesTest::make_node_cfgs(size_t total_count bool NodesTest::wait_connect(const std::vector>& nodes) { auto num_peers_connected = nodes.size() - 1; - return wait({30s, 1s}, [&](auto& ctx) { + return wait({60s, 100ms}, [&](auto& ctx) { for (const auto& node : nodes) { if (ctx.fail_if(node->getNetwork()->getPeerCount() < num_peers_connected)) { return; From 4bf032bb1cd72e5f91d74ee3b73721677174ee5a Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Mon, 15 May 2023 10:22:03 +0200 Subject: [PATCH 010/134] chore: rework how Host is destroyed --- libraries/aleth/libp2p/Host.cpp | 58 ++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/libraries/aleth/libp2p/Host.cpp b/libraries/aleth/libp2p/Host.cpp index 4d626d95ec..1e773acf73 100644 --- a/libraries/aleth/libp2p/Host.cpp +++ b/libraries/aleth/libp2p/Host.cpp @@ -55,7 +55,7 @@ Host::Host(std::string _clientVersion, KeyPair const& kp, NetworkConfig _n, Tara handshake_ctx.port = m_listenPort; handshake_ctx.client_version = m_clientVersion; handshake_ctx.on_success = [this](auto const& id, auto const& rlp, auto frame_coder, auto socket) { - ba::post(strand_, [=, this, _ = shared_from_this(), rlp = rlp.data().cropped(0, rlp.actualSize()).toBytes(), + ba::post(strand_, [=, this, rlp = rlp.data().cropped(0, rlp.actualSize()).toBytes(), frame_coder = std::move(frame_coder)]() mutable { startPeerSession(id, RLP(rlp), std::move(frame_coder), socket); }); @@ -94,11 +94,15 @@ Host::Host(std::string _clientVersion, KeyPair const& kp, NetworkConfig _n, Tara } } LOG(m_logger) << "devp2p started. Node id: " << id(); - runAcceptor(); //!!! this needs to be post to session_ioc_ as main_loop_body handles peer/session related stuff // and it should not be execute for bootnodes, but it needs to bind with strand_ // as it touching same structures as discovery part !!! - ba::post(session_ioc_, [this] { ba::post(strand_, [this] { main_loop_body(); }); }); + ba::post(session_ioc_, [this] { + ba::post(strand_, [this] { + runAcceptor(); + main_loop_body(); + }); + }); } std::shared_ptr Host::make(std::string _clientVersion, CapabilitiesFactory const& cap_factory, KeyPair const& kp, @@ -116,10 +120,6 @@ std::shared_ptr Host::make(std::string _clientVersion, CapabilitiesFactory } Host::~Host() { - // reset io_context (allows manually polling network, below) - ioc_.stop(); - session_ioc_.restart(); - // shutdown acceptor from same executor ba::post(m_tcp4Acceptor.get_executor(), [this] { m_tcp4Acceptor.cancel(); @@ -136,9 +136,13 @@ Host::~Host() { s->disconnect(ClientQuit); } } - while (0 < session_ioc_.poll()) + // We need to poll both as strand_ is ioc_ + while (0 < session_ioc_.poll() + ioc_.poll()) ; save_state(); + + ioc_.restart(); + session_ioc_.restart(); } ba::io_context::count_type Host::do_work() { @@ -423,7 +427,7 @@ void Host::runAcceptor() { } else { // incoming connection; we don't yet know nodeid auto handshake = make_shared(handshake_ctx_, socket); - ba::post(strand_, [=, this, this_shared = shared_from_this()] { + ba::post(strand_, [=, this] { m_connecting.push_back(handshake); handshake->start(); }); @@ -462,24 +466,24 @@ void Host::connect(shared_ptr const& _p) { bi::tcp::endpoint ep(_p->get_endpoint()); cnetdetails << "Attempting connection to " << _p->id << "@" << ep << " from " << id(); auto socket = make_shared(bi::tcp::socket(make_strand(session_ioc_))); - socket->ref().async_connect( - ep, ba::bind_executor(strand_, [=, this, this_shared = shared_from_this()](boost::system::error_code const& ec) { - _p->m_lastAttempted = chrono::system_clock::now(); - _p->m_failedAttempts++; - - if (ec) { - cnetdetails << "Connection refused to node " << _p->id << "@" << ep << " (" << ec.message() << ")"; - // Manually set error (session not present) - _p->m_lastDisconnect = TCPError; - } else { - cnetdetails << "Starting RLPX handshake with " << _p->id << "@" << ep; - auto handshake = make_shared(handshake_ctx_, socket, _p->id); - m_connecting.push_back(handshake); - - handshake->start(); - } - m_pendingPeerConns.erase(nptr); - })); + socket->ref().async_connect(ep, ba::bind_executor(strand_, [=, this](boost::system::error_code const& ec) { + _p->m_lastAttempted = chrono::system_clock::now(); + _p->m_failedAttempts++; + + if (ec) { + cnetdetails << "Connection refused to node " << _p->id << "@" << ep << " (" + << ec.message() << ")"; + // Manually set error (session not present) + _p->m_lastDisconnect = TCPError; + } else { + cnetdetails << "Starting RLPX handshake with " << _p->id << "@" << ep; + auto handshake = make_shared(handshake_ctx_, socket, _p->id); + m_connecting.push_back(handshake); + + handshake->start(); + } + m_pendingPeerConns.erase(nptr); + })); } PeerSessionInfos Host::peerSessionInfos() const { From 02765ac4eff25622c2d6122275053285fb79930a Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Wed, 10 May 2023 15:05:58 +0200 Subject: [PATCH 011/134] chore: transaction pool overflow improvements --- .../consensus/include/transaction/transaction_queue.hpp | 2 +- .../core_libs/consensus/src/transaction/transaction_manager.cpp | 2 +- .../core_libs/network/include/network/tarcap/taraxa_peer.hpp | 2 +- tests/network_test.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/core_libs/consensus/include/transaction/transaction_queue.hpp b/libraries/core_libs/consensus/include/transaction/transaction_queue.hpp index 2b1d167989..eff42ef513 100644 --- a/libraries/core_libs/consensus/include/transaction/transaction_queue.hpp +++ b/libraries/core_libs/consensus/include/transaction/transaction_queue.hpp @@ -144,7 +144,7 @@ class TransactionQueue { // If transactions are dropped within last kTransactionOverflowTimeLimit seconds, dag blocks with missing transactions // will not be treated as malicious - const std::chrono::seconds kTransactionOverflowTimeLimit{300}; + const std::chrono::seconds kTransactionOverflowTimeLimit{600}; // Limit when non proposable transactions expire const size_t kNonProposableTransactionsPeriodExpiryLimit = 10; diff --git a/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp b/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp index 725978795b..0e1c9ce7ac 100644 --- a/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp +++ b/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp @@ -380,7 +380,7 @@ std::optional TransactionManager::getBlockTransactions(DagBl transactions.emplace_back(std::move(trx)); } } else { - LOG(log_er_) << "Block " << blk.getHash() << " has missing transaction " << finalizedTransactions.second; + LOG(log_nf_) << "Block " << blk.getHash() << " has missing transaction " << finalizedTransactions.second; return std::nullopt; } diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_peer.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_peer.hpp index 8defcb5886..34df483adc 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_peer.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_peer.hpp @@ -127,7 +127,7 @@ class TaraxaPeer : public boost::noncopyable { const uint64_t kMaxSuspiciousPacketPerMinute = 1000; // Performance extensive dag syncing is only allowed to be requested once each kDagSyncingLimit seconds - const uint64_t kDagSyncingLimit = 300; + const uint64_t kDagSyncingLimit = 60; // Packets stats for packets sent by *this TaraxaPeer PacketsStats sent_packets_stats_; diff --git a/tests/network_test.cpp b/tests/network_test.cpp index c8097b8440..c2b9091c80 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -1396,7 +1396,7 @@ TEST_F(NetworkTest, suspicious_packets) { TEST_F(NetworkTest, dag_syncing_limit) { network::tarcap::TaraxaPeer peer1, peer2; - const uint64_t dag_sync_limit = 300; + const uint64_t dag_sync_limit = 60; EXPECT_TRUE(peer1.dagSyncingAllowed()); peer1.peer_dag_synced_ = true; From 09a5da17c6d96b6ac2131e73f81b48570835bd5c Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Wed, 12 Apr 2023 15:32:12 +0200 Subject: [PATCH 012/134] chore: improve sync-gossip transition --- .../consensus/src/pbft/pbft_manager.cpp | 19 ++++++++++++++----- .../get_pbft_sync_packet_handler.hpp | 2 +- .../get_pbft_sync_packet_handler.cpp | 8 ++++++-- .../packets_handlers/vote_packet_handler.cpp | 10 +++++----- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index 6bd9270dc3..e02c55c8fd 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -301,6 +301,11 @@ bool PbftManager::tryPushCertVotesBlock() { auto pbft_block = getValidPbftProposedBlock(current_pbft_period, certified_block_hash); if (!pbft_block) { LOG(log_er_) << "Invalid certified block " << certified_block_hash; + auto net = network_.lock(); + // If block/reward votes are missing but block is cert voted other nodes probably advanced, sync + if (net) { + net->restartSyncingPbft(); + } return false; } @@ -664,12 +669,16 @@ bool PbftManager::stateOperations_() { // Process synced blocks pushSyncedPbftBlocksIntoChain(); - // (Re)broadcast votes if needed - broadcastVotes(); + auto net = network_.lock(); + // Only broadcast votes and try to push cert voted block if node is not syncing + if (net && !net->pbft_syncing()) { + // (Re)broadcast votes if needed + broadcastVotes(); - // Check if there is 2t+1 cert votes for some valid block, if so - push it into the chain - if (tryPushCertVotesBlock()) { - return true; + // Check if there is 2t+1 cert votes for some valid block, if so - push it into the chain + if (tryPushCertVotesBlock()) { + return true; + } } // Check if there is 2t+1 next votes for some valid block, if so - advance round diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp index 856bc52eca..bccf58638f 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp @@ -20,7 +20,7 @@ class GetPbftSyncPacketHandler final : public PacketHandler { std::shared_ptr vote_mgr, std::shared_ptr db, const addr_t& node_addr); - void sendPbftBlocks(dev::p2p::NodeID const& peer_id, PbftPeriod from_period, size_t blocks_to_transfer, + void sendPbftBlocks(const std::shared_ptr& peer, PbftPeriod from_period, size_t blocks_to_transfer, bool pbft_chain_synced); // Packet type that is processed by this handler diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp index c5b0113f89..7d569f08ca 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp @@ -59,12 +59,13 @@ void GetPbftSyncPacketHandler::process(const PacketData &packet_data, } LOG(log_tr_) << "Will send " << blocks_to_transfer << " PBFT blocks to " << packet_data.from_node_id_; - sendPbftBlocks(packet_data.from_node_id_, height_to_sync, blocks_to_transfer, pbft_chain_synced); + sendPbftBlocks(peer, height_to_sync, blocks_to_transfer, pbft_chain_synced); } // api for pbft syncing -void GetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, PbftPeriod from_period, +void GetPbftSyncPacketHandler::sendPbftBlocks(const std::shared_ptr &peer, PbftPeriod from_period, size_t blocks_to_transfer, bool pbft_chain_synced) { + const auto &peer_id = peer->getId(); LOG(log_tr_) << "sendPbftBlocks: peer want to sync from pbft chain height " << from_period << ", will send at most " << blocks_to_transfer << " pbft blocks to " << peer_id; @@ -95,6 +96,9 @@ void GetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, P } LOG(log_dg_) << "Sending PbftSyncPacket period " << block_period << " to " << peer_id; sealAndSend(peer_id, SubprotocolPacketType::PbftSyncPacket, std::move(s)); + if (pbft_chain_synced && last_block) { + peer->syncing_ = false; + } } } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/vote_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/vote_packet_handler.cpp index 3051d87122..5810b0518a 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/vote_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/vote_packet_handler.cpp @@ -40,6 +40,11 @@ void VotePacketHandler::process(const PacketData &packet_data, const std::shared LOG(log_dg_) << "Received PBFT vote " << vote->getHash(); } + // Update peer's max chain size + if (peer_chain_size.has_value() && *peer_chain_size > peer->pbft_chain_size_) { + peer->pbft_chain_size_ = *peer_chain_size; + } + const auto vote_hash = vote->getHash(); if (!isPbftRelevantVote(vote)) { @@ -74,11 +79,6 @@ void VotePacketHandler::process(const PacketData &packet_data, const std::shared // Do not mark it before, as peers have small caches of known votes. Only mark gossiping votes peer->markVoteAsKnown(vote_hash); onNewPbftVote(vote, pbft_block); - - // Update peer's max chain size - if (peer_chain_size.has_value() && vote->getVoter() == peer->getId() && *peer_chain_size > peer->pbft_chain_size_) { - peer->pbft_chain_size_ = *peer_chain_size; - } } void VotePacketHandler::onNewPbftVote(const std::shared_ptr &vote, const std::shared_ptr &block, From 57adfabbac3e678a9ed791810cf1cad630688cd4 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Tue, 16 May 2023 14:15:49 +0200 Subject: [PATCH 013/134] chore: remove check for existing trxs --- .../src/transaction/transaction_manager.cpp | 34 +++++++++---------- libraries/core_libs/storage/src/storage.cpp | 1 + 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp b/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp index 0e1c9ce7ac..ff4e052d49 100644 --- a/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp +++ b/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp @@ -103,11 +103,12 @@ std::pair TransactionManager::insertTransaction(const std::sh return {false, reason}; } - auto transaction = trx; - if (insertValidatedTransaction(std::move(transaction), status)) { + const auto trx_hash = trx->getHash(); + auto trx_copy = trx; + if (insertValidatedTransaction(std::move(trx_copy), status)) { return {true, ""}; } else { - const auto period = db_->getTransactionPeriod(trx->getHash()); + const auto period = db_->getTransactionPeriod(trx_hash); if (period != std::nullopt) { return {false, "Transaction already finalized in period" + std::to_string(period->first)}; } else { @@ -119,18 +120,14 @@ std::pair TransactionManager::insertTransaction(const std::sh bool TransactionManager::insertValidatedTransaction(std::shared_ptr &&tx, const TransactionStatus status) { const auto trx_hash = tx->getHash(); - // This lock synchronizes inserting and removing transactions from transactions memory pool with database insertion. - // It is very important to lock checking the db state of transaction together with transaction pool checking to be - // protected from new DAG block and Period data transactions insertions which are inserted directly in the database. + // This lock synchronizes inserting and removing transactions from transactions memory pool. + // It is very important to lock transaction pool checking to be + // protected from new DAG block and Period data transactions insertions. std::unique_lock transactions_lock(transactions_mutex_); - const auto last_block_number = final_chain_->last_block_number(); - - // Check the db with if transaction is really new - if (db_->transactionInDb(trx_hash)) { - transactions_pool_.markTransactionKnown(trx_hash); + if (nonfinalized_transactions_in_dag_.contains(trx_hash)) { return false; } - + const auto last_block_number = final_chain_->last_block_number(); LOG(log_dg_) << "Transaction " << trx_hash << " inserted in trx pool"; return transactions_pool_.insert(std::move(tx), status, last_block_number); } @@ -314,14 +311,17 @@ void TransactionManager::updateFinalizedTransactionsStatus(PeriodData const &per recently_finalized_transactions_.clear(); } for (auto const &trx : period_data.transactions) { - recently_finalized_transactions_[trx->getHash()] = trx; - if (!nonfinalized_transactions_in_dag_.erase(trx->getHash())) { + const auto hash = trx->getHash(); + recently_finalized_transactions_[hash] = trx; + // we should mark trx as know just in case it was only synchronized from the period data + transactions_pool_.markTransactionKnown(hash); + if (!nonfinalized_transactions_in_dag_.erase(hash)) { trx_count_++; } else { - LOG(log_dg_) << "Transaction " << trx->getHash() << " removed from nonfinalized transactions"; + LOG(log_dg_) << "Transaction " << hash << " removed from nonfinalized transactions"; } - if (transactions_pool_.erase(trx->getHash())) { - LOG(log_dg_) << "Transaction " << trx->getHash() << " removed from transactions_pool_"; + if (transactions_pool_.erase(hash)) { + LOG(log_dg_) << "Transaction " << hash << " removed from transactions_pool_"; } } db_->saveStatusField(StatusDbField::TrxCount, trx_count_); diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index feb9d05f4b..af9a3a41e9 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -471,6 +471,7 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period) { auto hash = trx_hash_t(reinterpret_cast(trx_hashes_raw.data() + i * trx_hash_t::size), trx_hash_t::ConstructFromPointer); remove(write_batch, Columns::final_chain_receipt_by_trx_hash, hash); + remove(write_batch, Columns::period_data, hash); } remove(write_batch, Columns::final_chain_transaction_hashes_by_blk_number, EthBlockNumber(period)); if ((period - start_period + 1) % max_batch_delete == 0) { From 73e281b69e0061ff2b63692be11e8d2b9513eece Mon Sep 17 00:00:00 2001 From: rjonczy Date: Tue, 16 May 2023 14:48:55 +0200 Subject: [PATCH 014/134] improvement to build process --- .circleci/config.yml | 140 +++++++++++++++++++++++++++++++++++-------- Dockerfile | 84 +------------------------- builder.Dockerfile | 81 +++++++++++++++++++++++++ 3 files changed, 196 insertions(+), 109 deletions(-) create mode 100644 builder.Dockerfile diff --git a/.circleci/config.yml b/.circleci/config.yml index 7e64d59226..7cd6c5d5a2 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -26,7 +26,6 @@ orbs: github-release: izumin5210/github-release@0.1.1 gcp-gcr: circleci/gcp-gcr@0.13.0 gcp-cli: circleci/gcp-cli@2.4.1 - docker-cache: cci-x/docker-registry-image-cache@0.2.12 helm-release: taraxa/helm-release@0.1.1 commands: @@ -328,39 +327,46 @@ commands: echo "export HELM_TEST_NAME=pr-${PR}" >> $BASH_ENV echo "export DOCKER_BRANCH_TAG=$(./scripts/docker_tag_from_branch.sh $CIRCLE_BRANCH)" >> $BASH_ENV echo "export GCP_IMAGE=gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE}" >> $BASH_ENV - - run: - name: Clean old Images if disk available is lower than 15G - command: | - for time in 120 96 72 48 24 12 6 3 1 0 - do - if [ $(df /var/lib/docker --block-size=1073741824 --output=avail|grep -v Avail) -lt 15 ]; then - df /var/lib/docker --block-size=1073741824 --output=avail - echo "Pruning images older than ${time}h" - docker image prune -a --force --filter "until=${time}h" - fi - done build: - description: Builds docker images + description: Builds docker image (node) steps: - - run: - name: Build builder image - command: | - docker build -t ${IMAGE}:${VERSION} --target builder . - run: name: Checkout Submodules command: | git submodule sync git submodule update --init --recursive --jobs 8 - run: - name: Build ctest image + name: Compile and build binaries command: | docker build -t ${IMAGE}-ctest:${VERSION} --target build . - run: - name: Build taraxad image + name: Build final Docker image (${IMAGE}) command: | docker build -t ${IMAGE}:${VERSION} . + build_builder: + description: Builds docker image (builder) + steps: + - run: + name: Build builder image + command: | + docker build -t ${IMAGE}:${VERSION} -f builder.Dockerfile . + + tag_builder: + description: Tags docker images (builder) + steps: + - run: + name: Tag images + command: | + + if [[ ${CIRCLE_TAG} != "" ]];then + docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:${CIRCLE_TAG} + docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:latest + else + docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:${VERSION} + fi + tag: description: Tags docker images steps: @@ -395,9 +401,9 @@ commands: # tag images for 'feature' branches (based on PRs) if [[ ${PR} != "false" ]]; then - docker tag ${IMAGE}:${VERSION} ${GCP_IMAGE}:pr-${PR}-${CIRCLE_BUILD_NUM} - docker tag ${IMAGE}:${VERSION} ${GCP_IMAGE}:pr-${PR}-${CIRCLE_SHA1} - docker tag ${IMAGE}:${VERSION} ${GCP_IMAGE}:pr-${PR}-${SHORT_GIT_HASH} + echo docker tag ${IMAGE}:${VERSION} ${GCP_IMAGE}:pr-${PR}-${CIRCLE_BUILD_NUM} + echo docker tag ${IMAGE}:${VERSION} ${GCP_IMAGE}:pr-${PR}-${CIRCLE_SHA1} + echo docker tag ${IMAGE}:${VERSION} ${GCP_IMAGE}:pr-${PR}-${SHORT_GIT_HASH} fi # tag images for release (based on tag) @@ -462,8 +468,15 @@ commands: - run: name: Push Images command: | - docker push taraxa/${IMAGE}:${CIRCLE_TAG} - docker push taraxa/${IMAGE}:latest + + if [[ ${CIRCLE_TAG} != "" ]];then + docker push taraxa/${IMAGE}:${CIRCLE_TAG} + docker push taraxa/${IMAGE}:latest + else + docker push taraxa/${IMAGE}:${VERSION} + fi + + test: description: Run tests @@ -691,6 +704,34 @@ jobs: - store_artifacts: path: tmp_docker + build-builder-docker-image: + environment: + - IMAGE: taraxa-builder + - CONAN_REVISIONS_ENABLED: 1 + machine: + image: ubuntu-2204:2022.04.2 + docker_layer_caching: true + resource_class: large + steps: + - checkout + - prepare + - run: + name: List images restored from DLC + command: | + docker images + - build_builder + - run: + name: List images to be saved in DLC + command: | + docker images + - run: + name: Show layers of taraxa-builder image + command: | + docker history taraxa-builder:${VERSION} + - tag_builder + - push_dockerhub + + release-docker-image: environment: - IMAGE: taraxa-node @@ -995,6 +1036,47 @@ workflows: - build-linux - build-mac + + # ### workflows for builder image ### # + # run this workflow for specific branches 'builder/*' + build-builder-docker-image: + when: + and: + - not: << pipeline.parameters.deploy_prnet >> + - not: << pipeline.parameters.redeploy_prnet >> + - not: << pipeline.parameters.cleanup_prnet >> + jobs: + - build-builder-docker-image: + filters: + branches: + only: + - /^builder\/.*/ + - /^chore\/builder-.*/ + - /^fix\/builder-.*/ + - /^feature\/builder-.*/ + context: + - DOCKERHUB + + # run this workflow for specific tags 'builder/*' + release-builder-docker-image: + when: + and: + - not: << pipeline.parameters.deploy_prnet >> + - not: << pipeline.parameters.redeploy_prnet >> + - not: << pipeline.parameters.cleanup_prnet >> + jobs: + - build-builder-docker-image: + filters: + branches: + ignore: /.*/ + tags: + only: /^builder-v\d+.\d+.\d+/ + context: + - DOCKERHUB + # ### workflows for builder image ### # + + + # ### workflows for taraxa-node image ### # # run this workflow for all branches apart those reserved for chart build-docker-image: when: @@ -1011,6 +1093,10 @@ workflows: - /^chore\/chart-.*/ - /^fix\/chart-.*/ - /^feature\/chart-.*/ + - /^builder\/.*/ + - /^chore\/builder-.*/ + - /^fix\/builder-.*/ + - /^feature\/builder-.*/ context: - TARAXA - GCP @@ -1035,7 +1121,9 @@ workflows: - K8S - GCR - DOCKERHUB + # ### workflows for taraxa-node image ### # + # ### workflows for taraxa-node helm chart ### # # run this workflow for branches specified below build-helm-chart: when: @@ -1061,7 +1149,6 @@ workflows: - /^fix\/chart-.*/ - /^feature\/chart-.*/ - # run this workflow for tags, like chart-vX.Y.Z release-helm-chart: jobs: @@ -1077,4 +1164,5 @@ workflows: branches: ignore: /.*/ tags: - only: /^chart-v\d+.\d+.\d+/ \ No newline at end of file + only: /^chart-v\d+.\d+.\d+/ + # ### workflows for taraxa-node helm chart ### # diff --git a/Dockerfile b/Dockerfile index 23997bd2bf..d347ffc8f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,91 +1,9 @@ -# Default output dir containing build artifacts ARG BUILD_OUTPUT_DIR=cmake-docker-build-debug -############################################# -# builder image - contains all dependencies # -############################################# -FROM ubuntu:22.04 as builder - - -# deps versions -ARG LLVM_VERSION=14 - -# Install standard packages -RUN apt-get update && DEBIAN_FRONTEND=noninteractive \ - apt-get install -y --no-install-recommends \ - tzdata \ - && apt-get install -y \ - tar \ - git \ - curl \ - wget \ - python3-pip \ - lsb-release \ - libgmp-dev \ - libmpfr-dev \ - libmicrohttpd-dev \ - software-properties-common \ - && rm -rf /var/lib/apt/lists/* - -# install solc for py_test if arch is not arm64 because it is not availiable -RUN \ -if [ `arch` != "aarch64" ]; \ -then \ - add-apt-repository ppa:ethereum/ethereum \ - && apt-get update \ - && apt install solc; \ -fi - -# install standart tools -RUN add-apt-repository ppa:ethereum/ethereum \ - && apt-get update \ - && apt-get install -y \ - clang-format-$LLVM_VERSION \ - clang-tidy-$LLVM_VERSION \ - llvm-$LLVM_VERSION \ - golang-go \ - ca-certificates \ - libtool \ - autoconf \ - binutils \ - cmake \ - ccache \ - # this libs are required for arm build by go part - libzstd-dev \ - libsnappy-dev \ - # replace this with conan dependency - rapidjson-dev \ - && rm -rf /var/lib/apt/lists/* - -ENV CXX="clang++-${LLVM_VERSION}" -ENV CC="clang-${LLVM_VERSION}" - -# HACK remove this when update to conan 2.0 -RUN ln -s /usr/bin/clang-${LLVM_VERSION} /usr/bin/clang -RUN ln -s /usr/bin/clang++-${LLVM_VERSION} /usr/bin/clang++ - -# Install conan -RUN pip3 install conan==1.59.0 - -ENV CONAN_REVISIONS_ENABLED=1 - -# Install conan deps -WORKDIR /opt/taraxa/ -COPY conanfile.py . - -RUN conan profile new clang --detect \ - && conan profile update settings.compiler=clang clang \ - && conan profile update settings.compiler.version=$LLVM_VERSION clang \ - && conan profile update settings.compiler.libcxx=libstdc++11 clang \ - && conan profile update settings.build_type=RelWithDebInfo clang \ - && conan profile update env.CC=clang-$LLVM_VERSION clang \ - && conan profile update env.CXX=clang++-$LLVM_VERSION clang \ - && conan install --build missing -pr=clang . - ################################################################### # Build stage - use builder image for actual build of taraxa node # ################################################################### -FROM builder as build +FROM taraxa/taraxa-builder:a0bf43231 as build # Default output dir containing build artifacts ARG BUILD_OUTPUT_DIR diff --git a/builder.Dockerfile b/builder.Dockerfile new file mode 100644 index 0000000000..1ecf5c8458 --- /dev/null +++ b/builder.Dockerfile @@ -0,0 +1,81 @@ +############################################# +# builder image - contains all dependencies # +############################################# +FROM ubuntu:22.04 + +# deps versions +ARG LLVM_VERSION=14 + +# Install standard packages +RUN apt-get update && DEBIAN_FRONTEND=noninteractive \ + apt-get install -y --no-install-recommends \ + tzdata \ + && apt-get install -y \ + tar \ + git \ + curl \ + wget \ + python3-pip \ + lsb-release \ + libgmp-dev \ + libmpfr-dev \ + libmicrohttpd-dev \ + software-properties-common \ + && rm -rf /var/lib/apt/lists/* + +# install solc for py_test if arch is not arm64 because it is not availiable +RUN \ +if [ `arch` != "aarch64" ]; \ +then \ + add-apt-repository ppa:ethereum/ethereum \ + && apt-get update \ + && apt install solc; \ +fi + +# install standart tools +RUN add-apt-repository ppa:ethereum/ethereum \ + && apt-get update \ + && apt-get install -y \ + clang-format-$LLVM_VERSION \ + clang-tidy-$LLVM_VERSION \ + llvm-$LLVM_VERSION \ + golang-go \ + ca-certificates \ + libtool \ + autoconf \ + binutils \ + cmake \ + ccache \ + # this libs are required for arm build by go part + libzstd-dev \ + libsnappy-dev \ + # replace this with conan dependency + rapidjson-dev \ + && rm -rf /var/lib/apt/lists/* + +ENV CXX="clang++-${LLVM_VERSION}" +ENV CC="clang-${LLVM_VERSION}" + +# HACK remove this when update to conan 2.0 +RUN ln -s /usr/bin/clang-${LLVM_VERSION} /usr/bin/clang +RUN ln -s /usr/bin/clang++-${LLVM_VERSION} /usr/bin/clang++ + +# Install conan +RUN pip3 install conan==1.59.0 + +ENV CONAN_REVISIONS_ENABLED=1 + +# Install conan deps +WORKDIR /opt/taraxa/ +COPY conanfile.py . + +RUN conan profile new clang --detect \ + && conan profile update settings.compiler=clang clang \ + && conan profile update settings.compiler.version=$LLVM_VERSION clang \ + && conan profile update settings.compiler.libcxx=libstdc++11 clang \ + && conan profile update settings.build_type=RelWithDebInfo clang \ + && conan profile update env.CC=clang-$LLVM_VERSION clang \ + && conan profile update env.CXX=clang++-$LLVM_VERSION clang \ + && conan install --build missing -pr=clang . + + From 2247c918e427ce01dfdb15ea6ac7b0f92aacc22c Mon Sep 17 00:00:00 2001 From: rjonczy Date: Tue, 16 May 2023 15:06:45 +0200 Subject: [PATCH 015/134] fix to docker tags --- .circleci/config.yml | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7cd6c5d5a2..9535867667 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -329,7 +329,7 @@ commands: echo "export GCP_IMAGE=gcr.io/${GOOGLE_PROJECT_ID}/${IMAGE}" >> $BASH_ENV build: - description: Builds docker image (node) + description: Builds docker image (${IMAGE}) steps: - run: name: Checkout Submodules @@ -346,7 +346,7 @@ commands: docker build -t ${IMAGE}:${VERSION} . build_builder: - description: Builds docker image (builder) + description: Builds docker image (${IMAGE}) steps: - run: name: Build builder image @@ -361,6 +361,7 @@ commands: command: | if [[ ${CIRCLE_TAG} != "" ]];then + TAG=$() docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:${CIRCLE_TAG} docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:latest else @@ -401,14 +402,15 @@ commands: # tag images for 'feature' branches (based on PRs) if [[ ${PR} != "false" ]]; then - echo docker tag ${IMAGE}:${VERSION} ${GCP_IMAGE}:pr-${PR}-${CIRCLE_BUILD_NUM} - echo docker tag ${IMAGE}:${VERSION} ${GCP_IMAGE}:pr-${PR}-${CIRCLE_SHA1} - echo docker tag ${IMAGE}:${VERSION} ${GCP_IMAGE}:pr-${PR}-${SHORT_GIT_HASH} + docker tag ${IMAGE}:${VERSION} ${GCP_IMAGE}:pr-${PR}-${CIRCLE_BUILD_NUM} + docker tag ${IMAGE}:${VERSION} ${GCP_IMAGE}:pr-${PR}-${CIRCLE_SHA1} + docker tag ${IMAGE}:${VERSION} ${GCP_IMAGE}:pr-${PR}-${SHORT_GIT_HASH} fi # tag images for release (based on tag) if [[ ${CIRCLE_TAG} != "" ]];then - docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:${CIRCLE_TAG} + TAG=$(echo ${CIRCLE_TAG} | sed 's/^builder-//g') + docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:${TAG} docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:latest fi @@ -470,7 +472,8 @@ commands: command: | if [[ ${CIRCLE_TAG} != "" ]];then - docker push taraxa/${IMAGE}:${CIRCLE_TAG} + TAG=$(echo ${CIRCLE_TAG} | sed 's/^builder-//g') + docker push taraxa/${IMAGE}:${TAG} docker push taraxa/${IMAGE}:latest else docker push taraxa/${IMAGE}:${VERSION} From dfbb266ca29584f7d3e52fa33f66cc48fc3cabd5 Mon Sep 17 00:00:00 2001 From: rjonczy Date: Tue, 16 May 2023 15:24:53 +0200 Subject: [PATCH 016/134] couple fixes --- .circleci/config.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9535867667..f9dd72f287 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -361,8 +361,8 @@ commands: command: | if [[ ${CIRCLE_TAG} != "" ]];then - TAG=$() - docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:${CIRCLE_TAG} + TAG=$(echo ${CIRCLE_TAG} | sed 's/^builder-//g') + docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:${TAG} docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:latest else docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:${VERSION} @@ -409,8 +409,7 @@ commands: # tag images for release (based on tag) if [[ ${CIRCLE_TAG} != "" ]];then - TAG=$(echo ${CIRCLE_TAG} | sed 's/^builder-//g') - docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:${TAG} + docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:${CIRCLE_TAG} docker tag ${IMAGE}:${VERSION} taraxa/${IMAGE}:latest fi From e94057c3fcefe50a95c0b3ed71411243ddbdf20d Mon Sep 17 00:00:00 2001 From: rjonczy Date: Tue, 16 May 2023 15:31:06 +0200 Subject: [PATCH 017/134] changed based image of builder --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d347ffc8f0..f7dcc07e97 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ ARG BUILD_OUTPUT_DIR=cmake-docker-build-debug ################################################################### # Build stage - use builder image for actual build of taraxa node # ################################################################### -FROM taraxa/taraxa-builder:a0bf43231 as build +FROM taraxa/taraxa-builder:v0.1.0 as build # Default output dir containing build artifacts ARG BUILD_OUTPUT_DIR From 905b5045a7f32089812a634bf4575ae75b5c8ced Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Tue, 16 May 2023 11:55:03 +0200 Subject: [PATCH 018/134] feat: pass hardforks config to the go part --- libraries/config/include/config/hardfork.hpp | 2 ++ libraries/config/include/config/state_config.hpp | 1 + libraries/config/src/genesis.cpp | 2 +- libraries/config/src/hardfork.cpp | 8 +++++++- libraries/config/src/state_config.cpp | 5 ++++- submodules/taraxa-evm | 2 +- 6 files changed, 16 insertions(+), 4 deletions(-) diff --git a/libraries/config/include/config/hardfork.hpp b/libraries/config/include/config/hardfork.hpp index f0364b5dbc..d5bfebc33a 100644 --- a/libraries/config/include/config/hardfork.hpp +++ b/libraries/config/include/config/hardfork.hpp @@ -16,6 +16,8 @@ struct Hardforks { */ using RewardsDistributionMap = std::map; RewardsDistributionMap rewards_distribution_frequency; + // disable it by default (set to max uint64) + uint64_t fee_rewards_block_num = -1; HAS_RLP_FIELDS }; diff --git a/libraries/config/include/config/state_config.hpp b/libraries/config/include/config/state_config.hpp index 15a0a6da18..b6aa692b64 100644 --- a/libraries/config/include/config/state_config.hpp +++ b/libraries/config/include/config/state_config.hpp @@ -63,6 +63,7 @@ struct Config { DPOSConfig dpos; Hardforks hardforks; + void rlp_without_hardforks(util::RLPEncoderRef encoding) const; HAS_RLP_FIELDS }; void append_json(Json::Value& json, const Config& obj); diff --git a/libraries/config/src/genesis.cpp b/libraries/config/src/genesis.cpp index 56625120a2..4308a64111 100644 --- a/libraries/config/src/genesis.cpp +++ b/libraries/config/src/genesis.cpp @@ -121,7 +121,7 @@ bytes Genesis::rlp() const { s << gas_price.rlp(); s << sortition.rlp(); s << pbft.rlp(); - state.rlp(s); + state.rlp_without_hardforks(s); s << dag.rlp(); return s.out(); diff --git a/libraries/config/src/hardfork.cpp b/libraries/config/src/hardfork.cpp index fc4f804c0e..5b127c4a9c 100644 --- a/libraries/config/src/hardfork.cpp +++ b/libraries/config/src/hardfork.cpp @@ -8,6 +8,9 @@ Json::Value enc_json(const Hardforks& obj) { for (auto i = obj.rewards_distribution_frequency.begin(); i != obj.rewards_distribution_frequency.end(); ++i) { rewards[std::to_string(i->first)] = i->second; } + + json["fee_rewards_block_num"] = obj.fee_rewards_block_num; + return json; } @@ -19,6 +22,9 @@ void dec_json(const Json::Value& json, Hardforks& obj) { obj.rewards_distribution_frequency[itr.key().asUInt64()] = itr->asUInt64(); } } + if (const auto& e = json["fee_rewards_block_num"]) { + obj.fee_rewards_block_num = dev::getUInt(e); + } } -RLP_FIELDS_DEFINE(Hardforks, rewards_distribution_frequency) +RLP_FIELDS_DEFINE(Hardforks, rewards_distribution_frequency, fee_rewards_block_num) diff --git a/libraries/config/src/state_config.cpp b/libraries/config/src/state_config.cpp index a284a65e3e..44dfad95d1 100644 --- a/libraries/config/src/state_config.cpp +++ b/libraries/config/src/state_config.cpp @@ -18,6 +18,9 @@ void dec_json(const Json::Value& /*json*/, uint64_t chain_id, EVMChainConfig& ob obj.chain_id = chain_id; // obj.homestead_block = dev::jsToInt(json["homestead_block"].asString()); } +void Config::rlp_without_hardforks(util::RLPEncoderRef encoding) const { + util::rlp_tuple(encoding, evm_chain_config, initial_balances, dpos); +} void append_json(Json::Value& json, const Config& obj) { json["evm_chain_config"] = enc_json(obj.evm_chain_config); @@ -121,7 +124,7 @@ RLP_FIELDS_DEFINE(DPOSConfig, eligibility_balance_threshold, vote_eligibility_ba minimum_deposit, max_block_author_reward, dag_proposers_reward, commission_change_delta, commission_change_frequency, delegation_delay, delegation_locking_period, blocks_per_year, yield_percentage, initial_validators) -RLP_FIELDS_DEFINE(Config, evm_chain_config, initial_balances, dpos) +RLP_FIELDS_DEFINE(Config, evm_chain_config, initial_balances, dpos, hardforks) RLP_FIELDS_DEFINE(Opts, expected_max_trx_per_block, max_trie_full_node_levels_to_cache) RLP_FIELDS_DEFINE(OptsDB, db_path, disable_most_recent_trie_value_views) diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index ffd207a4cb..e2d36e25c2 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit ffd207a4cb80919c1cdbf26f919b3a158ce497f3 +Subproject commit e2d36e25c28d7655f09f3c356ec8d1b60bd7cf29 From 8bb7f53db8bf618ba9b74222c08bc2739ec94046 Mon Sep 17 00:00:00 2001 From: rjonczy Date: Wed, 17 May 2023 13:33:56 +0200 Subject: [PATCH 019/134] rename task name --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index f9dd72f287..510b0b17a9 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -341,7 +341,7 @@ commands: command: | docker build -t ${IMAGE}-ctest:${VERSION} --target build . - run: - name: Build final Docker image (${IMAGE}) + name: Build final Docker image command: | docker build -t ${IMAGE}:${VERSION} . From dca485bb82479ca11950a6420c37f6dcf9a38d95 Mon Sep 17 00:00:00 2001 From: Leonard Mocanu Date: Wed, 5 Apr 2023 18:05:44 +0300 Subject: [PATCH 020/134] chore: removes ERC20 mirroring from genesis --- .../config_jsons/testnet/testnet_genesis.json | 1606 +---------------- 1 file changed, 1 insertion(+), 1605 deletions(-) diff --git a/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json b/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json index 7a7f30890f..4e7ae9e56d 100644 --- a/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json @@ -92,1611 +92,7 @@ "initial_balances": { "76870407332398322576505f3c5423d0a71af296": "0x33b2e3c9fd0803ce8000000", "f4a52b8f6dc8ab046fec6ad02e77023c044342e4": "0x9b18ab5df7180b6b8000000", - "ee1326fbf7d9322e5ea02c6fe5eb63535fceccd1": "0x52b7d2dcc80cd2e4000000", - "723304d1357a2334fcf902aa3d232f5139080a1b": "0x78b1ced5335fe2885ebdd00", - "b0800c7af0a6aec0ff8dbe01708bd8e300c6305b": "0x208b1d135e4a8000", - "aa7f12dee6165d7f1a8670fc7bde93ae4d238abe": "0x174d019ab1e18000", - "10f4fd4d9856efd5700f0cb70b90bf519a3cd238": "0x14384dc7cdbe8000", - "68ecf99daab887c221f4fe5ff325de9558153b99": "0x15c2a7b13fd00000", - "99a2d5feaecb1a729d4f9af4197cc03bb9a37bc3": "0xaf4c499e81e000", - "d1c8da18cf8ba1648e1e44324e5fb15f05b18b62": "0xf14f1554c5904a72a51000", - "19279330d2d16f4ad65356d804aa67528eac2ca9": "0x5af3107a4000", - "5fad6f0a6e2f907dee97483582b92a80c313ee70": "0x2bdbb64bc09000", - "3f5bf62075e26c9c064625e600b67a1a43bc9054": "0xd07313624f754f3809000", - "ba06347c5308aebdc2f71aac898548b4823812f7": "0x482961cb004adaff00000", - "8d7ed986993fa2f308672c454e958864b0b6cb4f": "0x3ff7a431a3fdb24a90000", - "694c6f3c223619c762822a3983d7ff1a0633e30a": "0x4035bbae94e38df1a00000", - "e09e96dc013461192f1a97cb8bece2b2cf0a5238": "0xfe390302b8c9bd50000", - "7baf2483a3610feb6d8c5f057085357be1034d26": "0x26e000", - "4cb305c6499a7c99ceb7da57da27f74bcec692f6": "0x152d02e542d062cae000", - "457e1dad8beba88b1b9168cf2fc505d97b2697c3": "0x17fcfd428f46940dac000", - "5a62d45089d5849220605c6c05d036a1255efb04": "0xd02f0a8f95df8e4e000", - "2e1292ee4ac05732831a0c0e9ae27513baa9512d": "0x54069233bf7f780000", - "01a4391500efaa6edd152711f1bb163c5d77f98c": "0x20d4278f65b3e2d8bb000", - "5308021885369eb88ba6d4679a4a1f657aa2b521": "0x5aa550f5b683e0ad0000", - "7af6a7d2e8e269a54b4434990c45834d70a26f16": "0xc42c78e3c0ff24000", - "16dc29e84a34d8038014f544c4848250abc3173c": "0x261dd1ce01a70042e000", - "6007f391319add076dc1722fe8c0fca31b9c1558": "0xfe1c25ab9e1ca2ca000", - "466be276def7997aee62ee917bd564a9f354ef6a": "0x9dcafa0f96ec703b4000", - "5e16d680b61a2498dba15e6f2703e1c21d42e41e": "0xd573caff9ac5c7a0000", - "ac7d2ebf53606d908f718c0440a4cebe76440bcd": "0x4c8f4df39ce7a60535000", - "2987fd4f30a57f16238ccbfaf6a090fe8d17a77f": "0x34bfc185c8e354910000", - "8df10f866ee9ab8d7d2bb394f32e4e14bc23844a": "0x2081e063b1e000", - "68349009458626e35da0eea9cb583b3c828bb815": "0x2315429b2830000", - "6fbb7bff3a244efef7c72c1fe496ed116fa2a320": "0x13da32fedd104cce0000", - "c4cf3cd0f841c6f5affa1b642166d157abd3d5d6": "0x94e47c3bef0473cb50000", - "38d31a5c839fdcf1202fbb3a1347b4fea35c694d": "0x5b7ac4553de7ae00000", - "bc0da196a231eb890f83c45f1a677e11e0579510": "0x17d2a337485db1d86000", - "70861b72b0118b7c19f24e2c0de7db14464c6b37": "0x4559cfd71281c04c0000", - "affc6d223610836090dc0383bb25adc87997f571": "0x189138583e28e1a6478000", - "5abf14ddcb863e2709201aaf932f826a181cd2e7": "0x1c965d785fd0e3252000", - "bb89c198b2e05d5c368fcccfd3e4b20a22641fbc": "0x3f8708907bcb2fe8000", - "a53f1d8fbe04a17f33107baba9df41c1a05cd7ee": "0x17d24bb2c4d5505a0000", - "1c941935c08e2e2140ab82cd19b7f42f61c77578": "0x2d79883d2000", - "fc863549fc58de846a4ac4bfcf1c86deaa1460e2": "0xd573caff9ac5c7a0000", - "39fa351eeaeef89092fd48a78ca9f7d1d60ff8cf": "0x1550f7dca70000", - "71ef15aae2713b550e5df522fe887925f7211eee": "0x1d61856c4ae000", - "e4262298a99241806b510349e4e03841c1a16552": "0x4f68cac9ebb5d8890000", - "3cbc38c77a0aee60fc03fdcd8dd4386176436987": "0x143910d9b4d5f8fae000", - "ab1348b382eaec3d4a05cb85c999fc205b45292c": "0x7c13be5fa297f40c30000", - "99da1ce45ca25825c1f1f6dfb0aa2f7c83e83df3": "0x990abb854341dee90000", - "b932d7ea640b6c4a706b3cbb9c14470dd761fe18": "0x22256148b4e27a480000", - "200998f1c6bb56b100752c9334dd445c7d0740c3": "0x4d56b96be8cfb3cc8000", - "01f2178f137a03e6aa84b4d0a19cebafdf2404ff": "0x20a3fb89dfb8000", - "204c4f3788320960abd329dab51390ab37ddc4eb": "0x4c3ba40e0e15a9d0000", - "cf99aeb1821af5aadb7ee847203a2dbced1aaa28": "0x9877481c1c2b53a0000", - "378235420895854f618b6525c0c94a4e5e7e448a": "0x30be446e518000", - "970f09a4016debd648fed7ca9dc523e5ffae937e": "0x8d40339f0660ab1a2cc00", - "b09753213524b17cf62a1306559ab50176b2683e": "0x261dd1f8d11041ce0000", - "5665785813011a5c37c10972f8d3d463441637c3": "0x16a1b4bd0deb0a5a0000", - "bcb28dccd46a31f4dee5764e005628084f436f03": "0x4a29b3afafb0000", - "bad5e1f44feea1f510942e32c5e2b882a809db3e": "0x2b21440392c000", - "9144dd892d3d0c721d03675aa7b9310eca44ae93": "0x3faa25226000", - "97ad7ce74df6aaf3888a4bd04178b74dea1c57c6": "0x130ee901fc703d332000", - "988b7e55e95df4d4cadda18b6313c76da790048b": "0x130ea2f56a4440480000", - "fe4a73857d6adae8af377a253176a049f3d6a141": "0x1dc74be914d16aa400000", - "421b07132428ee50f13a9ff7484b1886c98e9072": "0x16deb1154f79eb800000", - "e9ff4c0cacea602dafe55e10fb2ec1cf9734089f": "0x261d45ead48880900000", - "cc03475c13b7a9c0488b989f735d402f38ee7f99": "0x152d02e542d062cae000", - "fc5f398e9056cc0cbb8e0b435e295f4fd18c2a4b": "0x349fe3e8bc48000", - "a66c49fc508306de126229174f8fc624c3313d68": "0x130ee8e7bb45c84f4000", - "fac189f30aa49b528d894c9acdb8dfe2c39bf920": "0x27b3d37f480e30960000", - "b04040be058fe179800fd91cf7641bee292eb1d9": "0xee3a5f48a68b55200000", - "6fa54473743bc51ed33c54348111745ccfd60ef9": "0x1a7843935bc9b99cc000", - "765fdcbaa945c2f73dae083770dd0aedfa386d5a": "0x72cf99aaf2ce8000", - "9099c634f209a807e153bd77e05c7cbe2b48d911": "0x2d79883d2000", - "6b2bfe999315f330d6e7983f34d4a3984f34df99": "0x130ee8e804083be44000", - "cdbc202397354d00f03683a10022b5e2aa4c3ed6": "0x17b25464a6e000", - "391feddfad5b8cb950ac50f0e073853c45e4997c": "0x4c3ba40e0e15a9d0000", - "f3f8e1acef002ca661a7b73d85f804107440c85c": "0x10a01a2a195f8abed0000", - "5c9f284b6b6cd58385ebed86c1dfad002086d339": "0x46f7d77179716c2a6000", - "0cfabce21351af046f3d6abaec56e307dd84117a": "0x79c2cffcfd9447586000", - "9d1718ad55f0a625c908d88d4e989fb36b7a7742": "0x261d45ead48880900000", - "7171341c9761a254b758dc86d2d2f7025c243a3e": "0x987747e344410738000", - "bc42fc9b1a2d34f1ce75aa497b86fe64a13512ca": "0x4be4e7662590026000", - "fcf1067e39dc0deb241079d290d8d0fc972c0aff": "0x98774738bc822200000", - "8fb0bd26b6d78e4642b238d94fa9a157f5524fbe": "0x3599ef09f245bff400000", - "0bd5f1a9cfd8bc9e059490234611fb5ea760518e": "0x69e10de7e5cb1ac4c000", - "3d9c859b26735d43bbb644a025c209ea7a85e9a7": "0x3dbaca338fb8689a0000", - "8e44e933475b3cb9349500dc43709ced73f7158c": "0x2d567ea3824a9c488000", - "6f7b3ec82a000c5a310636d7f7549df425666666": "0x4f68cacbdfeeb3296000", - "e0b2add5c2d094008d014369ab6bc5c42cf3acad": "0x2a5a05919b85dc488000", - "dfc17b721be2586f235b14dbb1ff6ff8290da6e9": "0xf8277889058b8ec3a0000", - "737e5f0fbbafa9b98eb0251e9293c51221bb97c7": "0xe4b2ead51ac33300000", - "956d079b656a3955ab4f2f596d1bbfd6f3ae60dc": "0x2820fe066ccb331f28000", - "20eec2d7f6a66a50556e0c5dcd1af6a0e859ef54": "0x86fef2568e02358e0000", - "d3474bd2f558b12e83810ec7d8af14f2d1d08137": "0x4c3ba39c5e411100000", - "eafda74329f5990febb46f8ad86986273df0ef05": "0x152cb510aef680500000", - "168b761193ad0989ac5fe22b6765a516796aff58": "0x1a78437abcfd5d51a000", - "1d41caea91d750ce2370bb98f5a7a9d517046dbc": "0x29ed99fbcc6cfa236000", - "0e4fb07abda5b8c56649ae65a15bc8d0666f042a": "0x392cbb02cbdd71bfc000", - "bfbc9c75c8970bf68426c1bf67184ce6226a2261": "0x3b8e981854280b9860000", - "89eb2cf671754dff668376041f944b1e71512a88": "0x3469007b0178716b4000", - "5e965711ac04526e9041273a655ac743cc9e8a00": "0x3f87089eb1c5c3020000", - "1702bf1d68805518f972b1ba06ce50c000b06b7b": "0x11fd5457c2f22abf0000", - "56a96d4db4f416fae99ee8c97e341ef151727d0d": "0x152d02e613ff6f174000", - "ab4c45165eaffa3fe503c93b185ccb4cb263e183": "0x2fa54641968770dda000", - "73938712fcb9c5d9b2e246a0517cf5532e464bbc": "0x152d02e542d062cae000", - "d739ec618244650af7efff8bd8d0edcb190175b5": "0x50299c6f8cfc000", - "1c06efe1b692083d3d823687ffdc3b0e4332d12b": "0x19cedb639a935c6c0000", - "72bb5da373e4ad5b0f057db53e669c3c3369c861": "0x5e3b80212cbdbb240000", - "e1d59d7edce98aa7444b7383f8c27d4ab3e19eb5": "0x2615c87ffe000", - "24926a489189fc1475541149d45c1c8872246fef": "0x886c98710830", - "0748e5ee2724d681c251ffbe6fa3447a2c185e61": "0x2fa5468081ae23ce6000", - "6b3de22ec227d168dfaee06f1fc629b49eb73e60": "0xecdc03032ce000", - "8394eb66bb2b61751eef53918b538a3444bb3015": "0x574376af4ba8000", - "68bd992d5feeb3a3030c128a7a5b0764ba152a33": "0x285688345dfaacf4da01", - "55bd9c62aa3fada7c7484cf1f6ed2d213036d05d": "0x53db346a7de8f850000", - "ead598b108e88c14ae28d5b4096a67c639be0658": "0x9407cd53468e000", - "ab0f8bbc7d4c2f02b84a9804d0b3b7fe008aa9b4": "0xfe1c2241ef2cbfa0000", - "449b824730eeb34ef524ed68b304977b7029bbb9": "0x5f4a977c97474526c000", - "dcbe18cb497d00b9341ac806ac4f5a1290066d45": "0xe4b2ec2a2a40fd70000", - "6e08e6e2d0deeb294fd53e9708f53b0fbedc06d5": "0x19de0868ec3cb45820000", - "a656299ccb3f883e33440142b2427af08caafcc9": "0x3d36892ab84c980fe000", - "50af468958a1b80bc109bb7a00dcdd08de181650": "0x1c1fc7b188452278ec000", - "3082f27581a2449268a14af56d810cacd9984ec1": "0x7a1fe1602770000", - "f68d2c2ea6156e7faf7982335b7d0be57502b14a": "0xe35fa931a0000", - "1a69b7a6347e95804af8a8c138a0d26d65a3150b": "0x4e59bde0fcc8d9704000", - "be80c2ad6ef7a36803ec09526428d85c45e08e5c": "0xbe951906eba2aa80000", - "1f1ad7b21ddb1afaf388aceae5848ff1fe7b1664": "0x4b4e720cb92e000", - "1d85ddeb7f73c33802d4e2545ee65a579781e0b6": "0x152d02e542d062cae000", - "d084d4ac8f520ba2914ce42c15a4637d439f9284": "0x130ea2f56a4440480000", - "cc987f85c2150670a391e9bf6ddccb4b875b9826": "0x152cb510aef680500000", - "0a4aeff1352d8c04277bed7e76fbbe18d6da33a0": "0x2d79883d2000", - "c13456a34305e9265e907f70f76b1ba6e2055c8b": "0x1c965d5aa35866600000", - "deea7c5e1bea5a9e2e4223a1b77a820f58889cec": "0x8eeeeb5bb093123c8000", - "0e63ee1a039bcc422b1baaf65c7ae30c497d3fc8": "0x422ca8fe9014811298000", - "ab410a77ef1020587ad215a01cf1d48b4606d079": "0x11458326be529a810000", - "1faf9912ac3ff3c69c0d24872eed296988c03c92": "0x130ee8e7bb45c84f4000", - "460d2bf0a5991762d9c7c36b5e87eeefb0005236": "0xc634a9635b75f900000", - "2dd063d7e83945537d0f689213ed91ece30d96b2": "0x69e10e652a93998c8000", - "0cf2beb6615e62bae60c2a9223b8678f78b8d736": "0x1f71c014ef5703aa0000", - "758d4c3a4d8d6ce42fc00afcdff685ba94a9f802": "0x766d2f3d24caf7f0000", - "f46f65984cbb0e3dd99eff73aa7d66409a70be13": "0xfe1c2241ef2cbfa0000", - "78eef4e819759169dfe61b7761abd0cee37f6a94": "0x22bdd8b8c8cf6354c0000", - "642bf2b93dcab8e3fdbf9bfe2382a968f4a1785c": "0x90ebfe8318de000", - "be55369bfbfd4bccf13bd197ee7b1165c0b7a4f8": "0x38d7ea4c68000", - "c1bb5e58a50b1d5b3d97797614bd387736d0caa1": "0x9407cd53468e000", - "fcfd8a643ba6a986ff33e15d76450847b99b06a6": "0x553625d0596d2380000", - "f7930a4d0f0507d522bf416b03df5d414da17045": "0x2b172ccdd701a3b6e000", - "e350adfc306e5616f1d42690cc7b83845435b085": "0x9ed1973e2ac8ee1e0000", - "9d4385ec8f90b68b779f9daa768307ea82368644": "0x50fe349303a211320000", - "27854fde675aa29b625a5eb48efdbc1f0a6ef258": "0x2a5a058f82ebc7dda000", - "7c9b7c21b05f5b14febb85fdd3cf59f1e2bde86a": "0x152d02c8cdc2ee244000", - "5c1731fa4844780b986c7db8cb1028f5ad9c29a9": "0x2615c87ffe000", - "f3ba959ef4badcf801ce3c7099597be9f4351dfd": "0x928ca80cfc20000", - "0ed67daaacf97acf041cc65f04a632a8811347ff": "0x5f4a8ba731256d2d0000", - "c4fcc11b6161c2254df23234f02ce43321146782": "0x988b6dc30b18000", - "a2355463bb4b0261fb0190076b9f11fabf8f88db": "0x9a7fa69dab78000", - "73f94018c7bdc6287973397c11aba9435dfbefc7": "0x19a0000", - "de827ab61f94b5568811ce223f2049ae084ac5a5": "0x922c05cb7a3c000", - "bd67ea50825eae3099f046ce4347b2b2c1c45508": "0x31a289c6d9d7188168000", - "38863935a98d8f1bd3a6cabcf6f890d376c746c1": "0x13d883e5967dc05e68000", - "0103a4966daba5a947df52b0b892d8b3fdef5a4f": "0x4f39f17de4cb312830000", - "e9e93e42e89dbd754b22447045ece22d1304c705": "0x97e0f4917031000", - "0f679f38ecf062d0ec78e0c3ba241002db67e04a": "0xc53f2555ee000", - "602e295a4e43a27be3829778e239c971e1509112": "0x39473b761fecabcc8000", - "5f050784f5fe19b89292f8d4e8fdb52ad81fa946": "0x206fafc6cca000", - "4d73f6905f4a8031b5f1c62141a64330054bef45": "0x112fae3d19d8000", - "e343626808df5ba9351eccc3109d54c1898bcbba": "0x2184418a7b283f96b0000", - "da320bd782c2473581e7c4aca63ed3399bf32100": "0x112fae3d19d8000", - "d8bef99f3e1bc4d679012143c46783d5b19b15e7": "0x17cd9d4ffec000", - "8ab680817449682f9d540bbf1c0076402c7f983a": "0xc74295560aa7df640000", - "a2e3bdae9569c89b13cd4407a87fd063253b84f8": "0x368832c2ed306f479a0000", - "ec15db470db85cc75b0e3fa5b6a0c607a5e8c64a": "0xe043da617250000", - "448175d7787ac533fee89fcc96f1b45f37750721": "0x8403f5ff512cee84d7", - "ac58f7f36b8b4a36c4e5c226238c3c5e20dfa68c": "0x36380b2a14636053", - "f05934bdc3adfe2c990a52f994bd1f10dfa3be2e": "0xa9665a2833e2c780000", - "eb4e794269d2e0932a3ae354a4a8ac4eec60b1b2": "0x18d0bfc30532adf9a0000", - "d8d3d8ab22e30c5402ab2a2e216a4a53f4e09e9e": "0x1fc3842bd1f071c00000", - "f1e240416fd4bee599711f161a8909bcffdc6129": "0x100000", - "bb1c2b63804abd238678235802d498e61063f0ed": "0x1a055690d9db80000", - "ad7e79a9a2a99371a7f5aaa7cc16e68dbf4a3ad5": "0x1bc16d674ec80000", - "15ffdd55a831106bae0a57fa9313a445f155a919": "0x13f306a2409fc000000", - "04ed4df37ab2741da592f87e9123c7c4b0824e7f": "0x13da329b633647180000", - "78b8b6e4c064911a8aa367fb9ca78e875c4952f5": "0x1e1d1c72d5b97e00000", - "050f500c38d4e1eb97df85f5369ef1a73cc8d1db": "0x21e19e0c9bab2400000", - "e29a3b6f7eced4f9c421446ab4eb1400ed9ec5a6": "0x29e8d4bd9948a560000", - "f4193fe06fc68af9bce3570f9e149272d47a5261": "0x6f4428d363914200000", - "c5ccb1d2cfea20cb1d2b38cb66dd3dcdf3978b79": "0x11ad4c17c3e613340000", - "856bbaa83483be77a97196e6523d36abf4e682ef": "0x36aae1b351b2fbc0000", - "384158caae0ec2f338b48500d09240bcb00e3747": "0x1", - "f123f854f32fb3a03089c2a343c094b716629c59": "0x13172d43ed957b520000", - "c8ce2a1d4d8e76c678257fbb6ef174c40ffcf387": "0x1a54449ff7a388c0000", - "9780c0a84c19cef9d0a255e967cb6245cc7aa342": "0x14cf1e73c9d275f00000", - "74466c73d8a64ff4bec01b8b9cbc12f22bd8cc0f": "0x142c4c84410c10320000", - "928746499cb06d3393f594662f680bfba2fb0c25": "0x110f6218d1a25a500000", - "fdba24f1165639dc89a8683432012937119e66fa": "0x32eb945e0f201b400000", - "69b2eead000fa35f625edc774b6e6fb575731c6e": "0xde8851a6c7228400000", - "7f9c00a1d7e86f108ac57eee098db4ea3621757a": "0xcedda365f72aa00000", - "7c88565a71a1292019466c5c130e0675786a85e9": "0x6ddb564524911a00000", - "c0113b17a290b7e6275c1140dc2c76c785a662f1": "0x1f59ce7c257b66080000", - "09df8e09c6cb68c7832e215e9769f3140da85673": "0x7ad1dcedb44c6280000", - "049ceeeb76b20b41ac44419c6b33cd71fdee68cd": "0x6a460c38fbddf93c0000", - "c6e7f4cc6dc941d935cb47b0323d443b7ab002cd": "0x1193706357272de00000", - "08341ef0c20cbedf2874c688c4f7dffe006604c4": "0x18c3df48bc580d980000", - "7baf4164b03bf9bbad7fd9ef6da200ab5f67909e": "0x1f05c7e9f1bbe6900000", - "f59807c1c62bfcf46914c7b79584aced5cc5d3d2": "0x7695a92c20d6fe00000", - "5912cb37dbd803d0671c1b63bf1c5b08574362c1": "0xd60c71f5f34de200000", - "626fa85e38285b0bb68f8530e61ef03ac10d681c": "0xd60c71f5f34de200000", - "62c8cf41259231de46e45e9e3061dc552c046139": "0x3a71fd6f595404800000", - "2d69bab9738b05048be16de3e5e0a945b8eeef3a": "0x1065e531808a878600000", - "87ee2d6728b4a9f7e08ccd43dec5acf6f16dc3a3": "0x2e29cdbdfa7f94400000", - "89369a2e1833af45900ea7b028e7f166ff675fe5": "0x21e19e0c9bab2400000", - "901fea6caba962763501f9d6dad92b74c6669bf3": "0x17b2b3e3de9390000", - "b638776c550e73cad74d39b5f5114bc07f93962f": "0x10f0cf064dd59200000", - "169efe5ffa1011fd90ee8438785e49066ed8ed31": "0x18d0bf423c03d8de00000", - "2699fa60c9afbd5d7e83cb4376e33ed35dff3205": "0xf3f20b8dfa69d00000", - "a3cfd837ec6271480a1790ca11417cb44c0d66e1": "0x193dd84e83554280000", - "3952aab505ac060ae4bbb2185a6d8a1afd25395e": "0x6f4428d363914200000", - "a8d3c162c2d40e06deaa7c4fb34bcde041a49284": "0x152d02c7e14af680000", - "e7c6861fdb0ab603b7bd0973aaf53ca1015fc67b": "0xb8a736f7e9fe5100000", - "643483e7a136912c01e7301f6dcdc551e9fe8f7e": "0x1b15418ca9f34f60000", - "d6e50b15019fb25be660fcbb63d54fdb9b9cf0bb": "0x1d1118aa6828c2c0000", - "52bcd99796c30371792169412bf30c8b468a69b7": "0x152d02c7e14af6800000", - "7e74b304ddacadba05e999f4e4ae2c13fd6dee1f": "0x941a8f7512f8000", - "25f8d05ff475dfb6b32db868b73ecc2c7c482c7c": "0x10846372f249d4c0000", - "bbb87bddde17debd4fb941297d327f5913479339": "0x21e19e0c9bab2400000", - "c057ff6c03b7159cb2eb112343eddd532ee9aa72": "0x12b5924ef30ab000000", - "d7c303e6051eefde08d3ad0619d9aea45be355ce": "0x14542ba12a337c00000", - "3a91c9793767ae133c96029d6cadeafaa32fce62": "0x10f0cf064dd59200000", - "6be60a200b1cee55a3bf8e4a6408fe807cbf0be6": "0x10f0cf064dd59200000", - "ee1957a15433a15d1dcc800bf8324c688a101444": "0x1da56a4b0835bf80000", - "e29ea94d67b1d5be6fba3000572c001de97526cc": "0x17b7883c06916600000", - "238e553abacb776258115fbca0cf43f12757c35c": "0x17b7883c06916600000", - "c13a8fbd40ca06a150da4f1371008a74d99b5b53": "0x21dd47d38386d4c0000", - "78f3a2bee176756e6d482428e4427a9c1ca74915": "0x10f0cf064dd59200000", - "56a1e9afe92fd20c8b3c63c09a5f359135ce80c4": "0x21e19e0c9bab2400000", - "c28df4a6e92fcae5c00e9e93680059df97d3fe91": "0x21e19e0c9bab2400000", - "57a845be856e1439baaedce02e92edba4b775401": "0x21e19e0c9bab2400000", - "4ce4fe1b35f11a428dd36a78c56cb8cc755f8847": "0x74b5247614531780000", - "bb5590561f48604d8963420ad288119786e3b239": "0x218ae196b8d4f300000", - "033c2c3e6bc3000d0a650b211e97c385096c1b0d": "0x21e19e0c9bab2400000", - "59027ba6e82c704e6a07b8930883907248cf8dcc": "0x218ae196b8d4f300000", - "fc764deee664574446b698e9dfe15f3e40ae97fd": "0x21e19e0c9bab2400000", - "58554ed430a5ec2008280a6da5c807c5a476371c": "0x10f0cf064dd59200000", - "6f327ac86ab3da7a545f42f2b5d5979ba018435d": "0x10f0cf064dd59200000", - "6eb60a59dca14a7de8ffe4d4666623ec1784dfe1": "0x218ae196b8d4f300000", - "cd01583b9477634f808440cc9d2e73bbb2ba79d2": "0x21dd47d38386d4c0000", - "a4c355156e801efb6fb1379fa515c692e5546419": "0x21e19e0c9bab2400000", - "f7fd444657993d83e8a068e5c967c8e6960281f7": "0x21dd47d38386d4c0000", - "ec54a99924bc086b5000c6f8d655b09df1fe81e1": "0x21e19e0c9bab2400000", - "87f11efd452863e24dbaa5b2f737e8db515775a9": "0x21e19e0c9bab2400000", - "592d23401ea3201bdb33ef06c26e37eab41a156c": "0x1da56a4b0835bf80000", - "f09fa64d17727d7d71f7ce007effd327bfa7d1eb": "0x1da56a4b0835bf80000", - "de49601c8597f06b62bcd7e24144f6ebce3476f4": "0x10f0cf064dd59200000", - "669f2b375718437dcd652736bd73e4b2823b6e73": "0x21e19e0c9bab2400000", - "1621aa6a9df7142da7adef4c40116eefd0a8833f": "0x17b7883c06916600000", - "8f99ba99ce568107150f00574e5c72cf1cf18e90": "0x10f0cf064dd59200000", - "f75358983f7bd1f5eb2eb1bccd3f9789d4d39e09": "0x21e19e0c9bab2400000", - "8cb0661dd2a4efd1d992ae7d778e7a1971da978e": "0x21daf04e4868fbe0000", - "14a65e7c93656f62da2db96ef47376dd80ffa9fa": "0x218ae196b8d4f300000", - "2503f30fe332b378384c19d38f2bcc9b53fd489a": "0x218ae196b8d4f300000", - "7586b55489c13f791e1a547532458fe8426e3b96": "0x6651728988000", - "b92906954bf8288ec443f39e4a2ccba55a5c0679": "0x6c6b935b8bbd400000", - "5ad1b152c20de8e4cd1808ecc0ca3bd4251bd22e": "0x10f0cf064dd59200000", - "7611a71064fe59ad4ec1aa24d2a3475d0ce6e842": "0x218ae196b8d4f300000", - "a1d9ef1510669ac0b3255ba292a61407a2f7699b": "0x1da56a4b0835bf80000", - "ddec430ff303323a39b3e65dcfeff3e6e56e7031": "0x202fefbf2d7c2f00000", - "b1d78770e95f0b8e464417a2f09946368cd2da3f": "0xa66ccefe7161380000", - "01571b0634f99336d6b85d030340a063ab11971b": "0x1da56a4b0835bf80000", - "bb38cff0ad10668e50619466773893f5974faa7d": "0x3c6c877bf6b1c680000", - "53314c68935c8db81b4e96cc80e5a134c831095d": "0x21e19e0c9bab2400000", - "5c2d4527424aa4c56c000a367e3d553127f209ea": "0x6c6b935b8bbd400000", - "d51502d6c130de11081a0552864d35a5751a0a67": "0xde0b6b3a7640000", - "903b38f7cb381d494bbf06da39f86cc2b54c6271": "0x1da56a4b0835bf80000", - "32f2e68a0780bf30cb936e7641f7e2c3c5898c8f": "0x21753279401f66c0000", - "34fbf5c6ea3913183c7e13fdd39f6984e61afcb9": "0xd8d726b7177a800000", - "0c7c9ee33dfe38ca7494657e09fa9168e40547b1": "0xa0a100292750378d1c00", - "a335483b1874b91f84b9dc9cdad531cceccfa0b2": "0x1da56a4b0835bf80000", - "75e89d5979e4f6fba9f97c104c2f0afb3f1dcb88": "0xf19b17bf8ffa85d9a6ee9", - "ff3f072d6e08758adf7099882a9de8a53cf134f2": "0x685c682846f0000", - "3a5a0a01065c2f87474548023591d1353ccbfd5f": "0x82501090e0c7a70000", - "5c157aecb4e3ac515c216378a4c7ca737eccfbe7": "0x1bb1021963821db0000", - "198a25f97d476608e8947ee715a4e9a1333418ef": "0x1da56a4b0835bf80000", - "3c54ab6741514c7b11d13c22555c7d7a5c94ec3c": "0xd2f13f7789f0000", - "8269afe0813f5a6914177ec60a26199792644e71": "0xb5d9a33f0e69ca50000", - "064915a4dd54fabca5613be602d8b904cac25610": "0x429d069189e0000", - "115c0b25f6eef689c707b74726df8460d9400190": "0x6c6b935b8bbd400000", - "cb9beb480abfc17c4edb2bcc71101a5918283a2e": "0x1fc1c814fb7b8540000", - "cc11586bc31dd038d3c9f88d259ae87773a6e7b5": "0x19aa43e1dee0ffc0000", - "4e49651d8ebb848197a05862aa7d15fe8cc762c5": "0xcd574190012ee30000", - "e6df9699727ea17840577d385dcdc77751cd0b19": "0x1b1ae4d6e2ef5000000", - "d5727afc11e5ac0022f51bcd9c7078a7008280be": "0x19aaea6a6f4cd870000", - "0752d71a1c7f57c095b07e561e1c48349f014b01": "0x21e19e0c9bab2400000", - "cedc689cfe1bc5527648acc1df9c393874a646e1": "0xe1d6dd2f9605580000", - "ea69a9a07bab3ab7b0e961559c6e7578345ad89e": "0xcd574190012ee30000", - "e623025491482b45ff0dd85df4e52017b719aaaf": "0x19aaea6a6f4cd870000", - "b5c643e4fd49c5919362bb77340a7ffa5120cafc": "0x5f4a8c8375d15540000", - "630a16232e2fd47ed65264a9fd9ba22b667979bb": "0x19aaeca2de73d480000", - "896e45e95a5a987a3083c9bfc1a689ddd41e62f2": "0x1da56a4b0835bf80000", - "dc6067efe6f2ff02cf50fa1489bd002cd8a41dbf": "0x10f0cf064dd59200000", - "e737c5125baa11bb129f9d521c7bacf42f414c8a": "0x6c6b935b8bbd400000", - "735b2df1dee89bd8a14206ec61b2ebba36ec9aa4": "0x19aaea6a6f4cd870000", - "67a60b40a839fb003dbb45bf65e85c40cb6c887e": "0xcd574190012ee30000", - "f9a4a9e70ec5395b71753b219783001b22aff7a4": "0x19aaea6a6f4cd870000", - "7302466506cd1493508d7150cc428759182869c8": "0x341f3725dc52940000", - "cf0571c9c08f0906c5a3949e6df380e1af0f33f7": "0x19aaea6a6f4cd870000", - "9ce32837f21bdc1557d8b772d2c7303bea5e6af1": "0x1da56a4b0835bf80000", - "28973dea5d89942e00cb15d571b56ca765eab0e1": "0x19aa43e1dee0ffc0000", - "76fcb1dbcd1903193d43bb50ceef23f2abdb5f44": "0x6c6b935b8bbd400000", - "3d203879f0d4c48163ebee7be41f1ddbd7db06de": "0x10f0cf064dd59200000", - "c1da1866c8c7fdb374ad3cb84625f66f2563afab": "0x15532cbe4f574980000", - "ff431fd9993be3da1e53304dce2476048bd1a4cb": "0x1da56a4b0835bf80000", - "f61e4404d4a0d708b07fd0932d4aa3f54c83388a": "0x1091661e3ab6c280000", - "7567ad8d776a5414e322a3bc2471610cd88d13c1": "0x152d02c7e14af680000", - "b8b5dcc79fd333c03e6f034a9ca0754aed4d60d1": "0x6c6b935b8bbd400000", - "3b3072b739cb08c081778431aabae2c183db29bd": "0xb4aeaab10258f40000", - "373a23707885e3b164857bd896c012a936b2b313": "0xa2a15d09519be00000", - "f3bed9d5f0c965f852a3792a9af255b25c9640b0": "0x93623c5071f54300000", - "19b32992a9487a2abdf7293d1434d7a3f2ff22fe": "0x3635c9adc5dea00000", - "219f0a671e64b17bc8be7f7206cab7b5aeec58a0": "0x40f0741bd862fbd0000", - "b9994031d7fe460f01ee061190d10940bdf1a767": "0x13f4c2b91714ac80000", - "4ea5ae4858301282308abca736d8a73be7e1a19a": "0x4a953be3620a1d40000", - "b79f061ddf5bba8a0b17ab16b65bf8aa80e41560": "0x4a953be3620a1d40000", - "eb6dd3bc0c27c8563d568a88cfaddbf45589ede6": "0x667504da59d0480000", - "4506cbdb650fcec9f5689dc9b78b3fa4bb173810": "0x21823524888c5480000", - "cae883ba5bd0e493b28d4d3a9ada9a8ca56196d1": "0x21e19e0c9bab2400000", - "098f58d3353eb4606a887a39207aaccf7e558a73": "0xfec99a4a552ff00000", - "bd836f18e18b7096be04b7032ddbc1c843e29550": "0x1da56a4b0835bf80000", - "2a4e46967b0d4821addf51dc77cf85e521468013": "0x11f50467f6582500000", - "2fce80f93569433d2360a40e6e33a0113a7f6a3d": "0x3635c9adc5dea00000", - "8f71b205e9debb5be105506588bf65754069c58d": "0x11067e23c68b1e40000", - "0a1d533afb68c7b3883f199dd608bf651bf50b1f": "0x11067e23c68b1e40000", - "6a07f5cd2349a13d213a81abd3c5d44d7080d660": "0x49e7c2f79c5dbb40000", - "eccfcb1a536de32c584651279db9774335a6a6ac": "0x3568391b80bf2740000", - "7e64532baa7ee2b3182b28f364d84014dba025a8": "0x49e7c2f79c5dbb40000", - "5c88f818b321c38097be09aae9d8a2f3a12ef6e4": "0x286b5abf70e85d8c0000", - "55949bd69386352eb021108dfd7503ef9008d0a1": "0x49e7c2f79c5dbb40000", - "6e87466eff020c876a64081a1cab51283903f12a": "0x12f07196012acab80000", - "8091e66362e515c9d9c70dfaf4ebf23d0ac49f2e": "0x12f07196012acab80000", - "729f1a33d8fa1b77e4f08af1c39dfaedfd3a0347": "0x5a768ef395651b00000", - "61b1be9ec7f4a65c7c54e6cbbb43ed02b0a60f6e": "0x49e7c2f79c5dbb40000", - "176041e6fda8cd45c031f6970b06c533797f4b12": "0x92ca728224fe6d40000", - "43ae40ca5e1821b48e75adabf47db6eacb5e2b57": "0x12407a49673654c0000", - "ece031185cf8781343e9184417c195c313cdfde1": "0x49e7c2f79c5dbb40000", - "8b95a7b8a314f6460b670c96f54532bf818645b5": "0xade5575907edbd40000", - "22dffcff25239a38d6200de60a2a205cc5a6ad5b": "0xe3aeb5737240a00000", - "8d54d4772eb3cf050c6db226a3e381ca677a9e3f": "0x69e10e94c5ce398a0000", - "094f1bb71ec9f0414f3856e9c52364915f1f3f8f": "0x1c34c95604276f40000", - "41b889b89b8a5f1b35f1d583f8c353c9d2f2ab5b": "0x3161cc1de57265c0000", - "379b70eccaa65bb2dc950f85d48a4af37eade2b1": "0x92ca728224fe6d40000", - "e5f1c6930357a6e6efa00f6931453b2c485197cb": "0x12407a49673654c0000", - "7fd50b05c39696370ac08fae37d3e82fe61097f1": "0x13869b0ee51402c0000", - "0dd9e6d03fa33d1cae77bad37134eae51b263f7f": "0x49e7c2f79c5dbb40000", - "33baffbe099f43935527ff7643004e36982125e4": "0x92ca728224fe6d40000", - "3969a0d3a14c875b10bc15bcce6785ac2106e714": "0x3568391b80bf26c0000", - "e6cee8ae60b3347e36ea7146de0c5964b746a6f6": "0xbc76ff2621e7f840000", - "6194cd3802eefcabfef2f7a70bb328c087dc0f95": "0x15cda2aa82a560ec0000", - "468f5d4ded9be4ad4cc8f1ea4416cf3d173bcf6e": "0x77bad43fb4071e80000", - "0355679fc37a51712da750c2825615f821013634": "0x92ca728224fe6d40000", - "46e68cd46c379e83fb31c21b2ce22163f2fa8c99": "0x7799de8dc9599100000", - "4ea52a33c31838c18aa7b86ea606030d34b76586": "0x3568391b80bf26c0000", - "b536b76c9e22f0c639933e38089a33decc12e067": "0xcd05cb221aef5800000", - "1ea21091286c89861e761d1f6f3505e3b71af6b4": "0x18aad3bf041ff7200000", - "51e77af3ab58fcb4e9eda65827b617a70d8107d0": "0x12407a49673654c0000", - "7835811fd10283e7bc36ca286a526c69d3232d11": "0x49e7c2f79c5dbb40000", - "df79e79dc5e48f10cccf66375fd04afd15a7ae38": "0x49e7c2f79c5dbb40000", - "b1274d845cbb380951ee0bc743af8849d1682d71": "0x11067e23c68b1e40000", - "9540bdf7e43d7d1c64004bf1b74dfc53528d815f": "0x11067e23c68b1e40000", - "5e483c37a61af54fb80dc688a84ac16c51cf2a7c": "0x11067e23c68b1e40000", - "1a478e2699f185ef2b2043dd5822335885964933": "0x11067e23c68b1e40000", - "dbd7204e09e501f9e213dad34f844c9280697188": "0x92ca728224fe6d40000", - "8dec71a81f696c5ca3649b49cc210979af75ea1d": "0x3161cc1de57265c0000", - "5d9f345b7a3533e162616fc91102804116056093": "0x3568391b80bf26c0000", - "8e107b460e0a3c76ce7d747d1feaebe0b101752e": "0x3161cc1de57265c0000", - "819d76e593766ae7d67974912c413794fe36c3cd": "0xbc76ff2621e7f840000", - "0362a015f95dbacea478098c03c151b7297b6f91": "0x49e7c2f79c5dbb40000", - "0c76094b18e56c8a321ee53a5ed9295df66f7758": "0x49e7c2f79c5dbb40000", - "6fb11f8d123efb95ad8ed1dc542f4f6e98761235": "0x4c1c6a014a087000000", - "253d7c1188c53126f62a20eda4a3c392cbcf64e6": "0x49e7c2f79c5dbb40000", - "4cd1a752edf6b31b395f87c42375d2b2340feb1d": "0x24ee3e319532dd00000", - "d3dca77d2ee6557832d9bd9fc585267ba9a1820c": "0x49e7c2f79c5dbb40000", - "66efdf21ae88a1555f0676143df0c5ce0c359390": "0x19d01a9404b766680000", - "12a6c8b6e4f7769273427e18769967cb4f12b83e": "0x4b77fd0260310330000", - "cebd7f73c47d5500e8e5456a2484543168b3772f": "0x12407a49673654c0000", - "43aacde07b492f6e74bbd2c6831bb55009b7098c": "0x49e7c2f79c5dbb40000", - "7f4ca89e524134cc0f553485379aec4f5fd765c9": "0x3568391b80bf26c0000", - "d7e56ba9ee541a6790aaf0f31b58f35956baf967": "0x1c34c95604276f40000", - "0c1bf6d507e2038d9764c059075c2b77cc21763a": "0xbc76ff2621e7f840000", - "c83ca2d92c2326c9158af6805f48b813bf59dd05": "0x49e7c2f79c5dbb40000", - "a844462fb6d8ea09a955036dfe40e6c6575660bc": "0x1c34c95604276f40000", - "689ef3849653562aa36792558855c18f0d09ad6c": "0x3568391b80bf26c0000", - "cc88e035aad186eb14cbb0c2acad4aa4cccd020d": "0x49e7c2f79c5dbb40000", - "923cd28974d4cc94a186f90e1c026cb5eaca6e24": "0x1a196558e9836e880000", - "d152471492163c4433856fafd671384ec79fb17f": "0x1c9c380", - "2894d8569fcecbb5a8bf216775920e94d3a26a13": "0x12407a49673654c0000", - "6039ebd0895d9cd3223a0591b113b0b998ff9f9f": "0x12407a49673654c0000", - "7529897524ae7f03751e7ca2dba84b079f085d34": "0x3e3c17392c7ebed0000", - "1080a4e8c000725c74f4f4a29a80fb6e1f61230b": "0x3697ccb81aacd240000", - "140819425d1d83efaa136f66147fd9780acf397b": "0x49e7c2f79c5dbb40000", - "a869a35811ac1847978d2c37a3828e2ff72d9932": "0xb238164985d1230e000", - "eb916f547e9b2aac1cd7fdc1d65e380e7beb4b0a": "0x1c34c95604276f40000", - "24fc938bbb2ccc491083c9507682c5653a781b53": "0x1c9c380", - "7b6f003c1ecdfc42d1890c1fffc6a8957d23fb29": "0x49e7c2f79c5dbb40000", - "8f05cd07d96fc3c50fda9a9d5f9432aed65a886c": "0x69695bbf98f31200000", - "ca361a195104cab28f3bf9636065b854f0a99700": "0x19336fc57c97b780000", - "1c93ac00ab5e2165b522f9143fa7a39188f561fe": "0x28a857425466f800000", - "a563021332521ec588c3dfdab32cd029087a1fc0": "0x3161cc1de57265c0000", - "e5d34451767d1a2abc6dfc717a0cbb654acfca2a": "0x5a768ef395651b00000", - "61ffe616848c55e40b831671516a5a9748aac508": "0x49e7c2f79c5dbb40000", - "32a2fe2fd271be629b805b15ef4522754329d73d": "0x261b1bea8009d6f80000", - "68e41f435a7b5e64deb5eb2992dad631d2778516": "0x80bfbefcb5f0bc00000", - "286f9df5f2db4b07b4856e4095ef475f5dca709b": "0x147f89dc1b9e9480000", - "777963dda1110d0a5b220c41c6dc2969700218e7": "0x1c34c95604276f40000", - "c2e2463ad3e12f2d14e6d8b1e76d7ccfe50f6a78": "0x1c34c95604276f40000", - "e0ef19f9a3360ee0a7deaa5b4c58a8eba1660e54": "0x50823fe6f8740080000", - "82730f00ef06d3bf4928c71bd2fe50b43e7cf3d5": "0xbc76ff2621e7f840000", - "65947132d41f186f5a9203ce86a7320314f5e763": "0x5a768ef395651b00000", - "bf8063ed4fa187e50c0425c2a1dc7e656d58dbe5": "0x12407a49673654c0000", - "afc0abec34eae2bb7f364e2421fcdb417b68f1d5": "0x5ac2e2e0717dc100000", - "3553291c7a3ef00f78dec9812d41711b8a75d55e": "0x1c34c95604276f40000", - "617e14f4fe646d9dd05ec54e29f08ef6f0515e38": "0x49e7c2f79c5dbb40000", - "3407a903f15ed037ced0a558508af5f7df2beeae": "0x3568391b80bf26c0000", - "a17cbeb8e7581bd17cf2c48a66701d31d5ab48d7": "0xb8bce61562b3dd40000", - "971912e15da506d3f73c5f577efda9ef0d6b15cf": "0x3161cc1de57265c0000", - "236d76fdac44d167954b7d1b93bc90ae8ccd42ff": "0x1c34c95604276f40000", - "2d7557fe7c0f3d31a8f626ca7fc8565b16108650": "0x49e7c2f79c5dbb40000", - "5743f5b5ee36c75b0c9dc3caf6e5a0ca0aff3e4b": "0x49e7c2f79c5dbb40000", - "09052b16a81fab0ffadcd9395d9a3dc26054da29": "0x3161cc1de57265c0000", - "b363f7c6600c1f8f23feb41747c07eb5f7ee6baa": "0x12407a49673654c0000", - "0af80707d6716771bb8d728ac26817d154c48b27": "0x12407a49673654c0000", - "a51831911b161779a86713181973c98ce1556b8b": "0x3568391b80bf26c0000", - "96eeb3e10f61b1ef3fa74f1956b1f049e57b847a": "0x3568391b80bf26c0000", - "f5d89402278904321b86ac792b34c665c75f10f5": "0x3161cc1de57265c0000", - "83f651502b386068600ef65167e631a9d0696c25": "0x12407a49673654c0000", - "1071f959b50fb7472e39ba32551e031945f4d645": "0xbc76ff2621e7f840000", - "4d449ab784f44c2675cac0cf300ab2e27cdde2fb": "0x2085514338c7073c0000", - "5832785c466961e860e884c27f6c65bd5196ee29": "0xbc76ff2621e7f840000", - "14c263f01e6654d7b6acc6e442d88dfb7c4d0a44": "0x187ab04544f35440000", - "fc7b7572f23a5fe49ac2f1da13a20a6fec76eb5c": "0x49e7c2f79c5dbb40000", - "1cfe341046d5cd15bf5996d9b6afbb863757db94": "0x242e508e8c5d50396000", - "9555bfcb3fc76df603710c33268c91ebec63f970": "0x5ee6cd624a8e3680000", - "8d0e106713d43eec0678a8fe373fadbc56c902fd": "0x49e7c2f79c5dbb40000", - "e5b80d88f2fa7e0203d5be2802ef50d63f8a0abe": "0x49e7c2f79c5dbb40000", - "1206c35880306a3ab23be1d195fd4d9b0ef18210": "0x12407a49673654c0000", - "d50b21ec3ed532c478e3fb9e9d665f295be70c8b": "0x80000", - "e38ceb07251c8d8bd88f63a69afd71e29bcf6a3c": "0x1c34c95604276f40000", - "69bcbeae946c237b1060261e141be5c29be0a8cc": "0x92ca728224fe6d40000", - "f0f954d35f6e3b6d6536fa2122b70e2ea48ff5c4": "0x12407a49673654c0000", - "4e2cf0179517c2088d5bad7bc829a5695b92d42d": "0x49e7c2f79c5dbb40000", - "75699b7921c333a86f107c4915aab81debd10150": "0x49e7c2f79c5dbb40000", - "06ec981fe43e8e8e7c3c30eff4b8919e78004122": "0x3568391b80bf26c0000", - "da5a36d491fecd1829656ec43d73902d3f2b8c1f": "0x3568391b80bf26c0000", - "ffed9112d8e8fe17bea4f1a790970d365e008200": "0x3161cc1de57265c0000", - "96ff737f3dceeeb289bbeae80a0be1e22a9c15c5": "0x12407a49673654c0000", - "1864b6a97458d8c3b0753b6a2e868f6882a51cc0": "0x12407a49673654c0000", - "777c94f5aa8efd3dcde73553adce1661e61ec98b": "0x3568391b80bf26c0000", - "1ee4a447450da42d11bad44c64ec79f602dcd4b5": "0x92ca728224fe6dc0000", - "0acbb03e59ccf1386675f11712f95745c8c78dd5": "0x98b8549126a2c6c0000", - "4ed03f73ee55d69cd50d00ec4c3ad588322c3c11": "0x12407a49673654c0000", - "64c743d4c766ad9870c1a29887ca92618037dad4": "0x92ca728224fe6d40000", - "de76b10742ec3b2864f6b7a48aec5a6bf3fe65fe": "0x3568391b80bf26c0000", - "42748d030cab89fae8de4c5e0e5f50bcad47b9e6": "0x49e7c2f79c5dbb40000", - "b9087369f52cc247c0f45d677569a246b26d93da": "0x49e7c2f79c5dbb40000", - "aa61f2613eeda3b10d7ea38299e9eb4a3deba64d": "0x1c34c95604276f40000", - "88785437bbc09b1c748a8e98789b5e65c0a0f3dc": "0x49e7c2f79c5dbb40000", - "a3e4d037c741dffadd59ef45f31c3c53a77c1cce": "0x3161cc1de57265c0000", - "c28643c39248e4efd89e243af68d3c99a7240dc5": "0x49e7c2f79c5dbb40000", - "5568415cff13bb39581e76e9870e8bf8fd761dea": "0x49e7c2f79c5dbb40000", - "f08c2fbb7442e2cbe3ce90538e5d0e4ccfa5c9ff": "0x49e7c2f79c5dbb40000", - "1074b3837ec0a74500b3f3d30a077cc1dfa9b998": "0x10f0cf064dd59200000", - "aa1107902c3f18d70dcd1dafd8444190956c79b3": "0x5c283d410394100000", - "a0e2477dda7a7477a1e881356297f2219c0aa871": "0x32d26d12e980b600000", - "deb5b9874dee1cde41b25e2669d7125a8c3adeb0": "0xde42ee1544dd900000", - "b712c75f358015eb50f7095dfe6356b105505749": "0x147f89dc1b9e9480000", - "32507d7718b876e16e5ae7f67106f98a2f12672f": "0x32d26d12e980b600000", - "8ec0c4129f1505ad80e30c01dea92c712addb0da": "0x124bc0ddd92e5600000", - "9abbf7218c65c4d22c8483b5d6be93075a3c159c": "0x12407a49673654c0000", - "0d3540d8566627c15f677f265c2969f75f1cd26a": "0x147f89dc1b9e9480000", - "2eead2c541463af6ebc8f30fa70fce9407224e38": "0x80000", - "dac45bf8f04ad3d297707197eec816e7b0fb865b": "0x152d02c7e14af680000", - "edfcc5830777f8343fa0f81a932d69664d82da57": "0x3635c9adc5dea00000", - "dd1da3ba29b9215e2e90d9bdb30c3c79adb34b4c": "0x69e10de76676d080000", - "49d5dcd94af8e2521d3af55823860878968638cf": "0x3d3b84a005a209430000", - "8849159d368b293cb574d5ea16a3d07f772dd7f4": "0x17b7883c06916600000", - "b6fbe24a437061edf95ef349755eb4fe2d1ae211": "0x21e19e0c9bab2400000", - "1853c5191dd2db73a75d32cef79930869fe7285a": "0x1b351f77df70cd0000", - "fba6fc542ba4de15639ed1da8ef4fb910c26f488": "0x147f89dc1b9e9480000", - "0847d83adf6daf361ac3a6e4daf8ae3ff476779f": "0x152c24bc76108040000", - "35c9fa4191e5068fba724a243c8d7ca768b35c27": "0x147f89dc1b9e9480000", - "2c3b858c28439c5a974bf473a38e331cc8fd4a5c": "0x378f372935f0eed7df4a", - "804b394fa37da23ac5800609e7e70e8787261dd0": "0x531c8b0802f45957000", - "232971835d9cc43e70dc1b4f8f5dd5ab75f3ecb4": "0x15586102d2b60f00000", - "bc2e7d827c788005ba971599d7f7dc911673a6f5": "0x1a34803dc0665dd80000", - "8c401f9f6de96e7f1f4d88cf9a1ae0417f7faf3e": "0x1a0fbf19e0990b00000", - "038881858ca7771014edbea144472c71a7013d21": "0x189f1e24dc8abac0000", - "fb238bf62e2bf882f3fdff3b50ee5c4e4b0f6954": "0x9dede625a63ff8200000", - "444aef5c15c1cf565cf5d91fb5d9701139786029": "0x21e19e0c9bab2400000", - "4722c5ed35e540751703ae85df06ae204d6ca012": "0x10caa50fdfc93f00000", - "0093e5f2a850268c0ca3093c7ea53731296487eb": "0x4cedcd8acda61746000", - "f38921fdbb1589880186651f776248ba106bb359": "0x147f89dc1b9e9400000", - "843b2e108f79cce5c82d6c24977a595725d8ee23": "0x21e19e0c9bab2400000", - "24a1c079e1b4ff863719fa1c9dc98e29d1704fad": "0x21e19e0c9bab2400000", - "9f5275fa7474919d4d0e0ee6ff22ba987eb01850": "0x147f89dc1b9e9480000", - "09383868e60d6e3f750e86b1e7f9611773f44b30": "0x147f89dc1b9e9480000", - "9012a5da0a2fc06649e372fc2e40b674bf2d30de": "0x147f89dc1b9e9480000", - "b66267b7f67ca2607f51b8f7f5f8864ea73bea65": "0x1f4791b8f94f9101375f00", - "0bad224d8ac553eee997b4f85f63d1ca25fca4a8": "0x21e19e0c9bab2400000", - "fef73c419c6966b4876f7fc5b157ead6bb8c5b0a": "0x21e19e0c9bab2400000", - "9c992556631ae4fd441a3bc794a8b280d2e293f2": "0x147f89dc1b9e9480000", - "7157e8f505f42094eb325d1912f359025d23ca0a": "0xcf6d113fd11e08840000", - "47e7bb26a10d047cf3dfb8eb3e6dc38fa231fc33": "0x147f89dc1b9e9480000", - "b0b47f82e835bc01e82dc5d171ce43ed01e6fc86": "0x12c0d8e36502b1c0000", - "e4bd96abe97fb64e35075432900ea3ca5c1abca2": "0x60577b59f9e4f3adc00000", - "f3c0d88dd3ae722454863993f4b59c95501044cd": "0x217988b25843b600000", - "d5593438c59e95d7b61ddb5cfa72c29b1cb0a2ce": "0x217988b25843b600000", - "c33bf2ba77990800a369bc48056a3f82aed76930": "0xf20b365bd7091c0000", - "1953f6282b219622f839b7daeea0b54e77abe451": "0xed32ad4872d0f10000", - "5b4672cffee5e64a2a7d17c157abdb4edffbe3df": "0x15cf12dc88b0961fb0000", - "8eb85b594ee2ca4e77f85221cfc2e687365c26eb": "0x394b280868e6d80000", - "2ae86cba88dd6bce5283d654bcb4ff281fafda9f": "0x50a440aee08836e8c00", - "55c92445d2a9dea4a7c8dfdddfc6502901ce254b": "0x6b33de13e48b7fe800000", - "37dcea6a5ea623a4b4b0444cc6692fe5d1eef18a": "0x376badc676a91900000", - "e373a27a98e124aa2b13b2c60e8bbf02d5ad3964": "0x1a2bb03bde0c889598000", - "35742ed27da01ad8d602d660489324b734fc5c3f": "0x3f870857a3e0e380000", - "17c7c3062ee53e869ef8f607b93b02df9e45f0b2": "0x5e9f91f8846d07380000", - "ad6ee7c40d7d80a6c404e82e77cde5c5088e98a5": "0x24af0d3f52abbdaa4c000", - "ac01158a83a9557eca559d877b817e463403bcef": "0x3685af2755b9774f38000", - "a8ae6549c66c59aa55d50377948dfbe362d56b03": "0x2543447a824bfb3e8000", - "09388d4b84d02e4da17f4704bb615b4afb2224da": "0x1a78359922ea0cbc0000", - "372a8f0d9cf7cb052e2d63673cfd5b431581db1d": "0x240c6a51bc996bc000", - "8f45efe6061bfb07853c87fe75b434cb09b331f8": "0x12d231c7c593eec0000", - "5a6fe20632e044a70aa8d556a00b4e8dfa583a2d": "0x565c81ba826018676d", - "795611d951b7c91deb4ed4fa8d7edda67b44f67d": "0xf3f20b8dfa69d00000", - "08e3ccb20ccfc4f3377c02aeac537a0b4db25e1c": "0x6579f0cf9f059cd89fd", - "949b82dfc04558bc4d3ca033a1b194915a3a3bee": "0x269996879a1cc63880000", - "17f9f0c0ba3d2ee0aeef9d76008c59ac33c7b599": "0x469baedf4669278600000", - "3176f330533f88d1cf300f2ebeadde28495656cf": "0x25271f0763531261e00000", - "eac3173b53d34070f2813fb046275bbb481e49f8": "0x20b69c4ac759b027e4000", - "9266276bb82d252948f6f82cd69b66ee14193dad": "0x11c651864a72f480000", - "1f378a8cbb178b89158992de6e7c782f640fea43": "0x217988b25843b600000", - "09ea8cca0b654ff39ee3ddcacdafe12f69f9334b": "0x3bf5ee5a9ce405a900000", - "8e3eb20f61cb352d490fa1586c9ecbbd87b4c81f": "0x1f3d257cbdc859f4000", - "d1be66007f5a22bbb1d538677bf98ba46538c088": "0x337fe5feaf2d180000", - "21572787436b49e3c5a90af540cb328b1da042a4": "0xb701cb221a69f08000", - "1322ca4118cedfbd83ef0c2fa5ddfad82ccd53bb": "0x91d53b8d50691c9389", - "d6e38329b8dd538d28d140e013210ef961c2bd43": "0x7b3476f58f75890000", - "7c759b5a84a8203631d4c4d39e5bbf7ae9f34e9c": "0x39bb49f599a0000", - "30c44cebae2848305ed0d71418ac997f3e537716": "0xa203fc62b817800", - "e173cc94d4755b72eb9196cf50dbcd2cba54e348": "0x2b9e25b911612c3f60000", - "245801943f9f23b5e64421587acb980d526701c9": "0x68aec0ef4ab89a194000", - "23f3213e8873a3e34d90855006b79e05cdc8835c": "0x6d0b5e4eab72ee9e70000", - "60e0556c2ad6b345fc0227391e998c2f9d0130ea": "0x2984686a9024b9c0000", - "911717e69fa4e4fd115c6a52ffade2c915e777c2": "0x5fc263d71426336c000", - "152c2a7e56321cae1e93463c6e5c736ac5508aef": "0xc72f1b148b18950000", - "80d869232ccc8b20ac29be980fdc112f399b34b4": "0xf7767c317261b2e124", - "0bae81559a7246e0fd4ad434cd330a60b8ecbf70": "0x48431755769bb40000", - "998e3c8ae44ccd73ada8bdb7c94e7f647513ca06": "0x6000", - "74c70ef6852b82a52bb30cbd2dd169b2975f4f02": "0x2540b8400", - "5dd565caad88e6c040c73aeb7321100ff073ad80": "0x6abfa535fb851870000", - "228c10b2a32433dad677da969e01acfb6fe7367d": "0x1df2bdf824442a38000", - "880c1c89b4621fb4648dcecc6230b46004fa48fb": "0x12407a49673654c0000", - "9f70ba77524a2d660ab509101a66527b7ec6d54e": "0x839e5fd42104260e0000", - "0d0707963952f2fba59dd06f2b425ace40b492fe": "0xc505668d99856dda47acd2", - "721d1528ead675c723c10f2b0c6f677eefeb5e67": "0xd2ee59b2fe20b35f00000", - "f001937650bb4f62b57521824b2c20f5b91bea05": "0x154137b9e760f15b00000", - "e4f1305cd471e29f2c10b33a51fd9ef54a248fce": "0x34a8893fef471180000", - "05f3203481277933c6bf646c2fca2d8765563cb3": "0x37d384f491ccf540000", - "6abcba40446cd63a7287ff26d04bec99c9b6902d": "0xf0000", - "89e5c205d283bfd8533ada96ed4fbede363f29a9": "0xad78ebc5ac6200000", - "91d220b44323dc5b2a5e4fd6aac2b93bf2724e7c": "0x14235af58d260c40000", - "1c4b70a3968436b9a0a9cf5205c787eb81bb558c": "0xf798b25a2e61fc0a50800", - "6a1f8df55f6b80eb46ed8db5d5e5324645ae6fcb": "0x3cb71f51fc5580000", - "7263198c709a456cfdb9d968c9ec56dfb111e5b2": "0x4c54c203de6a0d6ee8000", - "869a76f801bd9ef14b76c07a83fafb1ad8e32f27": "0x21e19e0c9bab2400000", - "c93d1d9c02c8181b26c69561818262a4dc5cebb6": "0x29aeac5ade7598d5e0000", - "ab365523310906a391cc612fd9ad992381fc3726": "0x1649add1f33e29c0000", - "4d300b561ea06abe10d38ad05319e5d2ea641802": "0xd15d81de957134328000", - "e7e87b0f611eb5af1a74fb9974e1e5ae258cb4fd": "0x1a2bb0736696883acc000", - "67278acac1823ed1939bc5e5a0f4ee55cc995fc6": "0xb843315dcd328ff8dd", - "1931c45270c421a100274547962d9f13c20bcf67": "0x8690b1a34390650000", - "66fb96591c514e18673238a8c0bf6d791700606c": "0x274acf74137803c6400", - "218950092b5abf9a20b37de5910ebfa2151ebd9f": "0x21e19e0c9bab2400000", - "0c9fa515ce34dae220213813ec8743e24287fade": "0x51d7e402be603f0000", - "caad539351c7c6c2081c464c4ce0e6ed01ee3485": "0x387879d45bf83ab41e400", - "5b509ae28a5bace02b6b3dbde38c7e109594a809": "0x23f4d30c0e2127c0000", - "20b99860d324741aabc4c208f5464060a08db84a": "0xbcbce7f1b150000", - "eabfa403d1ef41a774b841babbc760ccd2c045e9": "0x19ba7103a1e35050000", - "5cbba25357cb42c6e31d7eb3182372bced8f1e3f": "0x3635c9adc5dea00000", - "66228c9eff8718e4a94920109362ad5b9369a86c": "0x26ba5cdc1b5f8950000", - "d9cf5370745e8acb1000e0753aadb45e75a09f3e": "0x10f0cf064dd59200000", - "e71ab067bb46216d3f9c125ecf0261140ea2e3d1": "0x94fe6ece6171afc0000", - "1d29fde3cace6befe448f9dc4230b9b3a6e3831a": "0x13ba9a52bac0b5000", - "7bcaf39fef1018e79ceabde60ce4db995687ccaf": "0x131554abd1027e2b0000", - "eaa749471cc81d743bbaebd8190d439505e66526": "0x9a8e393b519a84b0000", - "a0d929c559e59aa91bcfe86024c5ad91c39137a8": "0x356ad23f19da000", - "878699d76323d5157e890f8c07e81219471e5b7e": "0xecd8d1764128498000", - "68b4670e742dab0513b240e1bf3cbf78abb54ac4": "0x9d2cc5b20180708000", - "387e7be437ffaaf968bffd4e29b874c9f91ca39b": "0x1b01f0f64dad7b7826000", - "61f79667d08684b1ea132be6060b9765853f9177": "0xad427a08457f8bb0000", - "e1c32436936fea38c0aee3796abac975cfc714a1": "0x2fbd207bbfb05e800000", - "2f6566e6f368e2423a49ec1c5550ea5028deb45e": "0x56bc75e2d631000000", - "684baf99404fd1e87dd2621d839c481005804d24": "0x9b954042169b180000", - "3ee24d753c6ff3b7fbd3064208213f46f5d56bc7": "0x5d3dcb870ca7e00000", - "82b6d7f8aae4abd4b2889fa98e2746a06e806c39": "0x8a3c5be1855e180000", - "057367dfdc835cee000d6ac1c25c99add663372a": "0x68155a43676e000000", - "dd39c8deaeacdcdde35bd48a469349b9346fe7fa": "0x68155a43676e000000", - "8bf01355e0d85728e9de799102ac13bf8f4795e6": "0x7dc477bc1cfa400000", - "62cf0f013114853ece4da51aca172371791f1c2e": "0x5c283d410394100000", - "456d9fd81b1b5ee7cced435366e8032c93c1e04e": "0x655f769450bc780000", - "c202b24f3660e25c281ed14ea04d6d0f7c5a3a93": "0x92b14e0e97e2da0000", - "f7bfb7bf28123abe4d618f67df986a12c23f9788": "0x62a992e53a0af00000", - "c77a1222f675b4142ded8998e7cb938fea1db44d": "0x52663ccab1e1c00000", - "9c49cadc89a3b8cb6239a37a692ba370c32bdfd3": "0x1faafaeaad95347cc000", - "66a4382368e16a5988cd5f661efedaf45ce1a8bf": "0xde0b6b3a7640000", - "0542e8f95f765b81cd6a08db37d914f664db5d3e": "0x71d75ab9b920500000", - "7b6798549243cf31a24895a75a2827859414b0b8": "0x4cfa756c847eb00000", - "6468e6e5dda11568be78dc499f3dd5d5a050ea22": "0x57d20428df44d00000", - "a8308b4185508c25ff496ecc4a3b55378de108ef": "0xd1cb09efdc79b80000", - "26ef7c5bcb35ee3ad3a826451ffb7bd61df551ef": "0x52663ccab1e1c00000", - "eeb5a54551f79b546e8babccade66ba3c9440efc": "0x70370550ab82980000", - "528cd3a3db61d0e325bcd486baeee8e7317206af": "0xde0b6b3a7640000", - "f37d4c98df785a0f14595e180b4d7e577b7ad061": "0x3519193e68612c14000", - "2ecc1901090e39edc94bc4cefc93299daa2c97ad": "0x5d3dcb870ca7e00000", - "c790fd5896132c9fa0c18c43c37f601089d5e132": "0x1547448de36178bdec00", - "614d228afef1019d4a3279edbc43eed6b2183247": "0x57d20428df44d00000", - "f817e647e14ad9b24a8f72e7e48c1a9adb6580f9": "0x56bc75e2d631000000", - "4495d4b4b7433907c8570fd9e249ba1e1c0a39e7": "0x66ffcbfd5e5a300000", - "0eec5fb25160819bc9fbc8025cbc87097c6aba47": "0xc393accd303d228000", - "29e2ae27ad654243d55773c9cbe9756ac24091cb": "0x12309ce54000", - "444a5e0d2515f322e7278f6ee95cb34d8de98f09": "0x12a05f2000", - "99f48381512caf3b8d274287103e41bf1bfa9870": "0x5ff3af362359680000", - "570ff3b922ae2b91659e4fcd719496a68b1a0456": "0x56bc75e2d631000000", - "99f084c111d3ba6324540b77c891245319e7845c": "0x1ba233cc3a8f5f00000", - "ce205274cdcf6fc369c7573bfe4338eca6d29b8c": "0x152d02c7e14af680000", - "fbfe7dccf2ebc2b1f109d03499f81d0a46733152": "0x66ffcbfd5e5a300000", - "dbf0f59a16450bb71bace12e28c2cc8946a43d08": "0x6b9ec2a16eeae595", - "7cd1286d59b8bd2665817232c812fa6dccc0351b": "0x151451af3d400a79c00", - "80d707a6bee1e1b6deb2e028ccf4bb18f758144e": "0x5d9463b120dcf36c00", - "f06f4dde3b6a43d4e3b3760e120f9e1f8b20599a": "0x70370550ab82980000", - "e8da221058830965280f76aa1241d68f963c0ba5": "0x2be44146802ed5c0000", - "2b9834dc620502d3f3964f4e70304a46fb7ac8a7": "0x1dd0c885f9a0d800000", - "32a94cae783baf41410a69add54fa4fcfc05df9f": "0x56bc75e2d631000000", - "bb94aee4a5fdc940e5e0dfc74d6695ebe01f25f5": "0x71d75ab9b920500000", - "a80fd8985165e331bf933d27318b2b938b839f26": "0x57d20428df44d00000", - "f99fcabbd11f5465061bc233b9586a55e4379cf5": "0x5a87e7d7f5f6580000", - "172b47e864172426baa1b942b1fd69928ba59b2e": "0x25f273933db5700000", - "7907d34a4697b7a1155b289dbd9aed25793caf01": "0x13d9cd38aeb8000", - "3949fce477c43845e4ed72258abb35a529e4d643": "0x72ece8ffc234200000", - "b01cd0d330c9c8af5c9746c83671aa30250e9029": "0x344a6701658e94f4000", - "fbd5863a6e7cfd45b69a134f7d5987956e2d87bc": "0x6c6b935b8bbd400000", - "3be4518e0254cc80c2cafe7cbd33c60a9e1d862e": "0x195b55d2c118f700000", - "b1b152f814a848946ee29dea1159c1f9eb669102": "0x889c067877c0600000", - "d7583ad46f37a1d1f0d4e4a50a12256056b7c4b4": "0xf75197384e26d7c34000", - "8fb20fcb0251ec210de8d2c2973338b06e4a052c": "0xaa61c53feab49f0000", - "44250e31407e99e635ce7e93682def938428dbc3": "0x2b5e3af16b18800000", - "5a12fe0c4e532de9d23b51474e623e65d8b0382e": "0x8b67423a6862750000", - "dd3a85753faa07e28f338e30bc27a91dea3c632f": "0x92657e4aefbbd90000", - "e429f13aad2c0ecf0b283192d351c4fbf867dc88": "0x3527b2c3e313f90000", - "f6de338f57c649f80dd905f513e343fd9a075350": "0x2937d718ab246c5f000", - "1c8a634e4e3d267db23f0887079b3111306650c5": "0x114002590a43cfe4b7800", - "5a43ab8b043390a1a11f3803b6913537babecbf5": "0x2233263dfb228f80000", - "562680a4dc50ed2f14d75bf31f494cfe0b8d10a1": "0x2a20b9ff750c5003dc400", - "1c653928905576f55fb3d44b72789ff045680f73": "0xe5ad8f1c71043d0", - "adc3546ea680e21afa7d5d45dcd03f2f8957241a": "0x4d5d568244875880000", - "a8331759966bf7dbac90071e2b983d78f4d6dfc7": "0xc07ea3008686920000", - "3ec5c26a98bd89c9af152a5f6789e8071099724e": "0x3635c9adc5dea00000", - "91eb6b8a65a5ae2f604a408c9d1244380dc800b3": "0x340aad21b3b700000", - "f5ca0a539314bc9bcaf8e7388f5c3893c1c4029d": "0x38b5454dd52eb70000", - "16562e41112bf3f574a82cf17ad04ef3afdcd1de": "0xde0b6b3a7640000", - "9fdc68276541bddf5faa60aa8bc71d8b6e0c1b37": "0x19bff2ff57968c00000", - "d6216fc19db775df9774a6e33526131da7d19a2c": "0x18741a56118a30e79000000", - "0d796cc7d6bed7ee429d768acb52fbc3ce5fc61d": "0x147f89dc1b9e9480000", - "23ce144aa082eb38778aff2379fdbedb9ae057ea": "0x3ffa90af084c040000", - "5f855b107afaadf722cf9611b82167a272b4acb0": "0x70370550ab82980000", - "98ae411104e6303d37b644f4d3d9de72f88fb836": "0x1e0a6a6a77ec95090000", - "8f819bd1e3d23befb594b1d4b060f912ef607be9": "0x925e06eec972b00000", - "07bd26e5127808f3fcc74daf893640a84c173b13": "0x71df5c327aaf1db0000", - "44151be0ee49a5a34b3a4c81cb974eaae0752d5a": "0x5c1a325a1007f6d0000", - "f71bd6be27940ef76e6e37ee68ad3dfd7d32068b": "0x6a6644bcb228f830c00", - "21f11fd582a0fd3e3366ce0558e80661cbcc295f": "0x3991269e3f14473101800", - "782de982146560f6d9a2bc676482be66a0b650bc": "0x11c4c52cc9c433580000", - "371ea1e8fcb52083a2a6ee8bb44bb5540939fb82": "0x655f769450bc780000", - "e24fa6a9bc0cef55c8f7ffbd83cd3c4277e7166f": "0x7cc6483b27d0320000", - "73bb3f6a46b214f9184f3ab4b8ce1bb7632b06ac": "0xce9e6986f4d2c6c5d000", - "0abff5fcad98576b9eab92f1ce915703c8b664f8": "0x76adc0a26ccd3464000", - "706f134f4a3ca72238ddf9248df4abd1670b4730": "0x1ad5f92cd2e38760000", - "6d83c0f837a03db320bf61572e4481773ababb6b": "0x224943ef79a0be10000", - "4b0fb6e1635a7becde2ff7ea1f08323646d30c2b": "0xdc9bc12bf776d51fdc00", - "932ffb351f44a15c317a89f8097d94d06bd9fd1b": "0x597c7874378d3c5000", - "23ddd3e3692d1861ed57ede224608875809e127f": "0x170a0f5040e50400000", - "005748a596c34379bf729721ab4dfdc1a03a8fab": "0x2db2f9a198364c00000", - "01cb73260063fc6e208f1c410c83e2ec4dbb56c5": "0x56bc75e2d631000000", - "ea47e8fd9eb5f0a9b0b9ff393b1e9546a821d85d": "0xcc6c533c604000", - "c5ece30df1a8c1c22a4220f485eb342a50deb6fb": "0x21e19e0c9bab2400000", - "1f88de3d8276a21fd247fbb5e59155b4d0409727": "0x5b0a0288ca37a180000", - "f499122e3453d264ff2360531308bfe5580759ed": "0x68155a43676e000000", - "54edcfbcdf116d160e88cb88c8a565ad3d98b985": "0x35ace333b439cc9c000", - "58f4a90d88c0d671886d430868e4ae694ea2e1f5": "0x69b5afac750bb80000", - "7c01008ecaccb29225907ed26bcd9688eb53ea39": "0x1933079db2cf650f01800", - "0025cd22ff1bc43c3a9eb778cf9a88614e23e27f": "0x2230e82869efadf4000", - "795cd51b29df9368bf9ee13bf4810385385a3ab1": "0x655f769450bc780000", - "71bf76d4cb6b95f917422167fbc2a70b28e3fc89": "0x3f969408c965aaa45000", - "9bb6acee72ef687e9e0bb1b78c908526d967b745": "0x8fa8233fb2c1280000", - "852abd878da9bc7b0b4934b0ba736aa0211c54f4": "0x878678326eac900000", - "e7089505c1831cd10829ff91daa612c50040a46b": "0x3c6568f12e8000", - "1283804910443611e15e245d4869935f7e0b1947": "0x1dc74b7648c53e55f8000", - "6219a0233a4f0c366e98ce8d91f1187edec1313a": "0x16345785d8a0000", - "23118dff36dba8641394c29a83452f1fd12d7268": "0x38a57744afaaf9530000", - "2f52e1cafec3fa7674ba07e2dde63ffca9ef0e1d": "0x7f0e10af47c1c700000", - "d99e28ef233b2b61020927e85bf89d4bba7e07df": "0x1069488e1b66e5700000", - "0d180ad9f40bb8d373bc31086892b543b86ac66a": "0x5a768ef395651b00000", - "497488df19125b29ffcd71cb3382f723c7c44ee7": "0x1153b01c0dde3a80000", - "f71a73fd6c4d5bd346a97881ef2df47d55b8c183": "0x1fc1c814fb7b8540000", - "136a283646a69e1105f5dd0ff203bb7a750a41ba": "0x4e2e35a01275b4940000", - "b32e9d3d7f7877df698d2f036b6f7bbc80463b85": "0x1d1c71dc9ab36380000", - "ead3abe6255b7228dab0fc2c8e55e60e53c8f55d": "0x44232a3b5772ea81c00", - "eb85f09dbf688a37616f57b52a5f775a4eeeeddc": "0x21e19e0c9bab2400000", - "e88eb4eff480fec69152ebd2623640244b2a02bf": "0x1cb47386bad44c2c0000", - "a3763dfde8c5f5f46cb53c179a394341588425e6": "0x59ddb84bfb5f05848000", - "dab558f089dc6cbd96eae358bb12cb4ad6043647": "0x16deb1154f79eb80000", - "faac6db89e9d168f3ea7549a8e42219366215b57": "0x12e019b5c782983c4c00", - "0000000000d41c96294ccdac8612bdfe29c641af": "0x2", - "11ededebf63bef0ea2d2d071bdf88f71543ec6fb": "0x6dfb05ed4cb3be43", - "c9a21fabd31408a5b548023a5ffd9877c8ae1b00": "0x138d04c46582b8000", - "05df8c04c658b745be1f032c9f5bd89f49d1f401": "0x711db6ad56229ba3000", - "8b6379652d0cbcb40c5f61c7fb426714d3e49aae": "0x35831c7d7cd37880000", - "fa0e876f03b701e1068ad9890d841299bbe5c5f1": "0x1fc3842bd1f071c0000", - "789261feda4e3040181a5aa9ef85e848281375e3": "0x261cf3c2c3e61240000", - "2ac0e0f58061cba2055737e6fd25479347cb65e5": "0x1a6371c7b9f1232000", - "a4b423909c829b1a040e7e1a012fccefa90116a7": "0x297d97a82d65c018800", - "ba0c6e6aa812f27eb96884e08bf04f30d46fd0f8": "0x8dd5ffdc0f31840000", - "9f0a8724441d0e5283e8de46ac7f2d6596904910": "0x37088e848e5cdec0000", - "aa4eb3cfddee63453de08e6790ececd3dca158d5": "0xf8b0a10e470000", - "60e7002b75c1804bfc36b66ff5139f1d50eb4b7e": "0x399e436c421b8c70a000", - "21fbc74b6efcbccc84ea178644a5a561a7b64cad": "0x6c3d42b9f6c964c6000", - "d6668ae08f288426900857603eb664f4931ce771": "0x55c2a90a346bf800000", - "0eb031d021618d04579f624a4e356babb3cb27d2": "0x181995a7f92400", - "1d6e8bac6ea3730825bde4b005ed7b2b39a2932d": "0x3", - "2e3381202988d535e8185e7089f633f7c9998e83": "0x3c9cf7141e64329d000", - "66f049111958809841bbe4b81c034da2d953aa0c": "0x2", - "b984d5756e05fbedce915159117ffc3bcb1fd2ba": "0x3779dcb5fd5754ebe000", - "fdbc6d8cf0ac7a259185ea8022d3d228dcc86e17": "0xbd17e8d53229c00", - "ac503143bc1964f94992357a13ae8a514537bb5d": "0x7a763a66d8bc69538000", - "35879f971ba26fb20d2537a95c4f86a6876b577b": "0x5cf4bc7d090e21280000", - "46d319191c01f6a1f199b7e6740e0137cc0e6a8f": "0xf120afa2248510eec000", - "05baa3b635a92c1c5006e18a056a7ac3caf2331c": "0x10f0cf064dd59200000", - "ba1cd56c98733eee5f79b0bd6aceab44a64682f7": "0x57b4a2550155535c000", - "08b6bfc8b0d8abb6219c9259778575cc7a0f2240": "0x1a784379d99db42000000", - "8578ebedb49c18231ccaa41610ac5eda0a5d00ee": "0x3a2034d8fc00", - "c740d49eedce4db3db0bd17363e58ccbc2fa3f18": "0x5a87e7d7f5f6580000", - "ede2819b04f5e885aae418c780c1c04af7149d54": "0x154531afc0405f42400", - "1748a2215c7779bdbc0a12e4de2f7d337d55dd7e": "0x1e03866cce568241800", - "865d19fbd85d4b048f831eb974bb69079aba9724": "0x1637fb71acd6a508000", - "c8a65fadf0e0ddaf421f28feab69bf6e2e589963": "0x410d586a20a4c00000", - "7ceafd11836738eca8da42a8153830a8d36e0473": "0x22c6f7c42e065738dc00", - "b0f3f05926968e87ad138f1d47de25aabb203566": "0x376654a8b865ea82c00", - "26c351893af2885113383143fdc03ebd98eb1ff3": "0x88c78dfb925efa8b558", - "468c57b643c37771a93143ee928e4cbf787cc96c": "0x69e1534af7f915740000", - "86b5c380008b693c101d2b648c3035cce2507639": "0xbc2a90e2f9fca112a6c", - "01b37140a297c28c4676e273299c4152a99e1a79": "0x3253ca8fc2294580000", - "348d5af02ee224b875de90d9950eef28ccc57766": "0x32a2633b070ae7458000", - "6ecfb621d03f29d263f8d89312c1a84cad1322b6": "0x704cd06dd3abb3", - "bc95ed2db8759257d905a8860efa3699f60a947f": "0x98017ba7925008c4c00", - "2bdc5beec47afa25fe5827edecfe75386108b0f3": "0x4f4ce0075d8e893f400", - "7b116ff3d61e7e3f681cd8e1058cb44cd48acdc9": "0x2f7b2f35242af113c00", - "6b8f62b7cc7fc90d634d6132dbe0b1ef7c26ad46": "0x136cbf5a78551d6c0000", - "3aef315b67c2e23aae9e9c1174d05371e2c9b144": "0xc097da0f8e212818000", - "2fbbb8678be97651130646e7f2dfc5cd8dfaff42": "0x19b0bd3f8b3be0988000", - "d2fa5a3ffa93a4caa1422004b8ccbd44f0be1aca": "0x7c13bcdcd55ba02080000", - "6bae64048e2d66017b54b5d670b27bdd5846e5ae": "0x2fa4976589aaa0b40000", - "d0fe2664eef8e5d6e3f83e0c760f9f0a56872055": "0x5df5779fa4bd290e1000", - "66a2c7b0d4da7e699f6dd27f89b49ab15b0a51e8": "0x392cbab546b0ccc0000", - "edfce8dd9050caeac0ae9c568921010f8e069041": "0xfd64d565b431d2948800", - "35ac5a38a2134f1ff66a1d861a7273627f5c08db": "0x2f0e43f877e3bad5400", - "d56dc0e94776fbb167f4ac7de7db60ac61ff0764": "0x2f449f942f662a3bbc000", - "c65a16f1ca598d29d64bbae62e3b35272cffe8d8": "0x1206930872659c50000", - "bfeeda27a6c5ecbe6a8d7f49a49acf54453cd002": "0x76e5c5c2769333383d", - "30c5312d9cf0d873994f000e72f1cbf561d0209c": "0xb7931fdc84e2c90000", - "346cbd8eeb1c2ac8a2c371760a22418dbe399d3c": "0x25f273933db5700000", - "99996a6a85834de3e3b8e663deefdcd784b561f0": "0x10f0cf064dd592000000", - "73284c5ed68321de23ae6867bf2520a5dfea48bb": "0x1ca134e95fb32c80000", - "9f19a120f6d2d74a5e877c742f122ff70ff6c321": "0x5665b96cf35acf00000", - "754dc055d33be1c0591921de776e72856cbc8838": "0xbd15cac07e53b3d000", - "04174909a6bf1ab99c34ff671d87b331466f210f": "0x70390da79004e2e21b", - "7d121b87361bf7e950eb8883cbf4c91863b326a5": "0x4b09e954168f360000", - "e4cf1d5ab39b5a0d8562f4e4dae0a7c017ec3f12": "0x22d04291b5eb551a0000", - "00000000003b3cc22af3ae1eac0440bcee416b40": "0x1d3dfd8742c27398", - "4ae26e87e97374f44fbf25eab31461256840520f": "0x14c9ed2aff0000", - "ce8605e88b31f579af1b87f663a5a44327b7f403": "0x1240ff837459688000", - "b3a7bdaadf1c13fea5f702bd10372f45d2026d93": "0x13d210f055f232800000", - "3ea8addb3ff0d028e704d13b0bd84c8d3187a31e": "0x11200c7644d50000", - "7b506f2b0e2199cfe0c1e5e4c2d6531b90812fba": "0x2503c16327e646cfe800", - "cc6b8274d126a573d0675ad66bb6aa19aba72132": "0x16f5b8f594ac0e209400", - "61bf0ef78f4f5f9994ce48234ade08e29db0fbbc": "0x1034ae24bbba153120c00", - "1daaa59a8d4a5c08080cadb65204d2d275838a99": "0x3a54e2805c38728a554000", - "81b7c1dc3dbee3c36829c4c25d77c7fc41bfcf11": "0x3469e7afb68801b20000", - "519c187ac6ee6cab5c6fad22344895c84487b85e": "0xec1e552513e784785000", - "363dc6cc6fe5ecbe96c449f623e10fd18df5dee8": "0x5d48608c04a176a6000", - "fd2e0b510178ffe173dc0a54f5349103c341c674": "0x91f7038cdb056be5000", - "142495c37fa6bd7066c79e677d875198eae981ff": "0x114f4df3a048606a0000", - "c7aa0753df0bd95fdba3de283b38b94aa5cd05a5": "0x7d262c982bccf3600000", - "db211c9abefa93188ab8044708dbc5617661bcf1": "0x21bbd9317945281bc000", - "b2781265b0905123bed40d4d0fb08a4bff28702a": "0x48441197910f83e3a09000", - "d79cd4e8d302fdfb19d743ab85eda69227799adc": "0x70dd55f564c9b0308400", - "72a7ad78f2faa749850dcf741c76db9a69597898": "0xd5a0a455955163e1e000", - "3a454148af9667c8ffeefc0d9ff475d1bfddd69b": "0x36601bcbea2973131000", - "672c2aa7477334eeb257b0fc66f53008f081d03c": "0xaa6979fb965c46b0000", - "6f4577574b83f546b05975ffd525b3b0db3746e8": "0x83390f7e03b64a5e48", - "f19f198e7bf6a5b1864812b77c78aa9cfb7917fb": "0x45b118af076c4a231b", - "28cae4a8b0a660b299f4f55b03de433549ddbec9": "0x25964b55fcb1dbf00000", - "8c45f9bc4d2418a7c3365c567dda07784f392b5a": "0x99429bcf4e5c1998ab", - "1bfb3f8e86681f2d77602d24cce07b65191dbc0a": "0x6d67fa566f31aac0000", - "e7ce1f79386cff4cc1fd553f56c486070f5997f8": "0x18588d449203a294000", - "73fd5ab0fef85196be66c04ad303648aa3a44dcb": "0x3540b8a9c8a67ac000", - "45757fedd2d664f68ad2de5a7c4a4f614332d1db": "0xf844ccfa80976af000", - "e272de87b09d0cda6135ef3e72fde2778df52b22": "0x315d0b85fc22b9d000", - "2ee143b965742b44a15829e32eaaa9d2b444f9bb": "0x110f05deea8df1d7000", - "a67e4f7eb18edddd28b1903fbdfa2b2694200c14": "0x12407a49673654c0000", - "9ceb30213560e1b4e92791f1dcdf4af9af9e1396": "0x6adf8239b65d94714d", - "a3cd66f12e6a7bcd897591953e5c6fcf608a4c00": "0x97ee3bd0c82340a7c", - "d84e0ef6eb114f5d1ef0d87dbe594e29deba0d05": "0x12407a49673654c0000", - "2fde77bb815cb1cf9160dfcd4eadc88be49842bf": "0x12e99cfc14be6780000", - "8faaf449b674235d8f119b3a7d5e1187008d0fe1": "0x39f6514ff161ac5ebc", - "e28c3c392b28b786419553a574ec5afe5554fd5c": "0x25573b016d0968e0000", - "4f5fff35ec6fcf5ddb70aa2079a78d19e94d57c3": "0x5cb29e400f1cad200000", - "dd1cd70b97d1fece11ae8d90dd823cfed313fc43": "0x2660cdc44e8a2f3448800", - "bd452114ce7cc6ce88618164ec32a4ec9d8680b1": "0x41f9160a62c675eac4", - "8306d7b380ada3f71b1d0b53b39f9d8319c08832": "0xd8d726b7177a800000", - "47720de690891877405ebc329d498f17238fc5a4": "0x49df2f94a7470cbfd8", - "2a6106c382400c8c3bf2702ae6fb95908393a17c": "0x4cb5ab8c0fed0fb5cb", - "aaacb0322dd57dded2ec12929a908b5440388a54": "0x475f63878d2542a81c00", - "fd9921e9dded04f9d37f99e245a9f63d81e722e9": "0x708d0fa64f89096000", - "00000000500e2fece27a7600435d0c48d64e0c00": "0x92e1f666eab7", - "efa0ecfa6d37a736129f1f7e9f132818dfd77e97": "0x5878a1cfdf0c58fcc6", - "3cc096f12c793da0aaf6146112567c70721898ea": "0x7cccd6e097fcf800efe0", - "a0c9ecb50fdd859bc378c9f14b685207825a90c5": "0x8a1442c2445c3310000", - "b45494fdee22303767b83bc2abff533849191dcb": "0x81dfb7379204000", - "7b5db0708b7fccbbc284ba4dbdaac6316be40132": "0x1f8e4f140fb1a88e4c00", - "274ff784f8cf2604ed731e4e40c822d79e17b528": "0x4f873d27df1995393", - "a102107df24691d126e312a3866f7cb72bb23ad4": "0xa42ed0087ec2c15d43", - "dcda0214cc6713cb29b0ecf6f5b3308552dffe16": "0x6fa391bb76b01180000", - "ed04f34542f4d369d5b696ecf088cb38723c38e9": "0xa57195393a41e35a66400", - "e99a8b3b2dd13350dc9b1633724683a04ddd7570": "0x39aa3bc683dcb101d8", - "1df152692e7f56f6d40f20b104d0b167492f9069": "0xe82b39bd667284c0400", - "3eb522c6d74703559f28f5b7c76e78c7d527ee45": "0x1b92e99769582c7564", - "02f59f7ab73ed8530a186a1f1be9904862a9856c": "0x9a14e70b7559a8952e000", - "9bdd54fea5b0740b152e8d4862bd518b8f36cfca": "0x364b49d4defb58f6400", - "179d481ca5e48d408339322aefca5317210645dc": "0x85e11d01a9363fa1000", - "f135cef689b090e82f0cb3a9255cc9f3ae81d037": "0x7369cb40f00e0a0a400", - "c79b79973751b3a33095c7d8090ac0f9d2c640d9": "0x37d6a2746ec2f69854", - "5c06978c740ae2993df73b7f29fa799af96df0c5": "0x39582c7847887a9389", - "a260a28fecc69d99b81244d8a2c398702fbe0124": "0x801cc2343834cb2dc00", - "1d643ca6c41ea2c27db39d27731bbc0d82d38514": "0x2f854a5ca8dce2280000", - "001d85b3da84afa37e41046f09b44378174e1e75": "0x28ce7a486719774e00000", - "d84cfd4e1a9d536fbc09be7a058c3abad6564f9d": "0x6c039097259bf3f8e7000", - "eb8ec4759ef5b987bd60b2342b0a04c92e83124a": "0xeb3fa92c499a95dadc00", - "9c6db396cc56cd6b53f3eaf8b027ed041809b39e": "0xdb8980c6230102a2800", - "1c52562c969dbd9a67e8f1cd429dc8e86ca19af7": "0xda0a3aa8354bddd73400", - "52613fa58ccd82d5790761c0149e6b74a3e33b8f": "0x43c33c1937564800000", - "ffe54f84fa4a3b608da61772ffbf9e2fb2b34118": "0x1200630784fa960fe000", - "b2b414bd509f972808bdbefef78f5c78dfe294f3": "0x207057a795704b2074", - "0d9d8562e3ce71ef510916eb6240d298934c028b": "0x2135ee55d0be5ee56e000", - "b50541f3cbcf291bc90ea96083b8f4e4f3cf06c6": "0x2c758a0d51d15daf0000", - "265cfca59e1d4b2fccc5324c9419980bbdb7d05c": "0x9e252c4018dbdf60000", - "0dd3fdc1c3f2f4d4ff37d194a116f1ca11e806ef": "0x21e19e0c9bab2400000", - "065159f177e7604800a1e4080d82ad97bb479b46": "0x415a6a0c955025970000", - "9dc4c00bc304dc3dd3a8db0187d8ce370b1bc7f3": "0x4e6dd319c2db3f101400", - "f1b36cfd9becaf7ea0beef334311c7b650824f16": "0x1b3a546e058bc1450000", - "2e36ed140e8c6fd6fd8e4121e06fa2dafd107b31": "0x4092f9c77000", - "b5b6442dcdb19df575779228dfbb3d5bc4e62126": "0x64ddeb756004f9594000", - "bec68060e97c371cee781491e19138552665c8e7": "0x75c6dd4c3417b3e0000", - "5d733bbacc17c46692cd78b3185bafcb208fd966": "0x2210457983affe6a4000", - "f9c60ae9cbdb333b0856bc413df9702e8688abc9": "0x972f7d52edce482884", - "1252eb0912559a206dd3600f283f2a48dca24196": "0x37ff07dbc46936de7e", - "bd888a06e0f34c5317a48033b877b8d9927b29bc": "0xc5cb0f77667a9dd380", - "2159a76f51005782fae73591dca367902be3b278": "0x9b1302b6a392f92d4c", - "2543e9dd96091fb9f608d7471a4214569e17d8fb": "0x5665b96cf35acf00000", - "aec74e9e25fa5cbbcdc6c8786beeaf9397ca8410": "0x2a5a058fc295ed000000", - "626188789c84af5a30847382b50ac8323b907675": "0x15a90679e170b145dc00", - "001c90675bb5e65e23932ac88c3e084c43f6b9d8": "0x29a7f107b79b694724c", - "f7e75ce0dd5afc1772029ce9f28ab9d75f9278b1": "0x20dd68aaf3289100000", - "427e10bb0c32c1efe38d783e1b96b35de2b3c608": "0x21f2ec04ffe7d2c48000", - "4887f3e46d6e21ea5e85e7056bd41ac135762336": "0x2aa7bba79680a118400", - "565b64b09f6137b0eb13770a83cd3fd094384119": "0x41e6dba5e548c6dc410", - "40f8ea59603c737f257573eee758355f2693d285": "0x188e6508b41c6906c400", - "91f8d56e8e286b5d0b5da65ea653ecdab8622413": "0xd99402ff2662057b", - "9b766faa149414fc42b1572899ce331f8083417c": "0xa968163f0a57b400000", - "fe3f463bfc637b2b3900932d27162877a4542b78": "0x3a00dfabda86027c000", - "be51832ce6f53f66c24a3fc40e7ea7ed1e0e0e47": "0x364f1dd6d7dcf4168000", - "dfe14fa0c322e34039d33400638771876398ad88": "0x64f4ab565e3e587800000", - "65809e18dfe1eb24a04015893a68400064192d92": "0xcf0ce59b28d21a5c400", - "e83e28a906f5908c0f25f811c4025a8636d8f90e": "0xb02ecf74c3138800000", - "8220fcd064eb865304c74c3e7884510b311369d7": "0xca4776299695400", - "609d9a8976941ac594dde275f1387072cad729cc": "0x5b7ac4553de7ae00000", - "9018c9903cbf6b897e1f2c39197b0ab7dba8a30f": "0x245ba8a7cb9accc0000", - "cfc8a39fc55275a05f743868035098da2f00de80": "0x4d6d233a9df21178000", - "1d71f13a86e8bbaf163e9f743462cb585d6b1833": "0x100dc3c0b5c09ed39bc00", - "273817473c777b999f379657026b0e71800d0a8c": "0x4e142f002b732e664000", - "d4d42b040a2bb9a403e6b98b2c961b96ed607191": "0x6a5aa88eb97b2cc0000", - "47c633985e2a39b5bef016b98d708e4bde3ef746": "0x1605d9ee986271000000", - "9937cc8a3442cb893c5a0eaf8224a740abfe7dc9": "0x1e8c3710570549673e400", - "becc06630c323e32a081a612d6324342980f2c74": "0x1b6a477b73d014bf2c00", - "ba452e243483916394cb244d276f653a1adc1e56": "0xda00a803c2ceefc0000", - "47b4080e3fbb324de0154faf051c223b5f88507d": "0x6c6b935b8bbd400000", - "b243f905d97d7e3236b95396eb37e636c05dfd61": "0xde0b6b3a7640000", - "58fecbdb4a5c271c395c4f3b3170ad73306dc45d": "0xaccb72d9e6b59e00000", - "3cefb79af1f4126dc679d52f5f7458a25ce74b42": "0x146c6bff27fc7ac0000", - "7d2774da71f64835793992445f18573f515558c4": "0x3161cc1de57265c0000", - "4508ba79566e41d5241831e3385d424803355900": "0x2127ec00e9ff9e75a400", - "242d9db56ce22b3a2eecb12caaf95d2b8b663cbf": "0x6c6b935b8bbd400000", - "a1846694eb1b4f691ae6d2f0b32f5105e9ee8bb7": "0x2c07dab4567a5e443800", - "8664af24e2ff632d6949def32fd575a82c0a9825": "0x631a07ec3361fa6d000", - "7ab9da1d97e569015d9a9a97acf9fdc7892a9506": "0x24f4d68f19714985e5000", - "a496900de437124e09938223f02b84df526b5b6d": "0x2017a67f731740000", - "92f418e0417d3376bfd97ee3f37da9db32562452": "0x21e19e0c9bab2400000", - "5c0c20e468482b5d9910569a65d394d769a8f4ea": "0x4e9a5cf4073fcc852800", - "0253ce03e9446ca39b45d6b19cfa6a3698b6bb51": "0x23f77a6d04230be0170000", - "1557b81c50b836ca195f336d295a5484b843ff7a": "0x1d5ee18a493cfbc8a000", - "f9723a79ae52fcf158256ddf9111c6f9bf8c1f9e": "0x8a2b36330ef50d5749", - "828102f08dca7400c116d7e34f256ada97604ba4": "0xa768b57a0b7dea74000", - "b3bf133950d87f84508fc64735ed40a4f2797aa6": "0x5fbea8404238dde72c00", - "062072fd9d68fb33b876114e2894990d3d35ee4f": "0x315bb9cdf6d92a00000", - "3e008863cfa520fdeed8a626fc5b58c2c63d6814": "0x4819d5a7296927d3c00", - "a41b7d32b6e1de05e62026e215f09f720449bb23": "0x4376f88f0ba2984dc000", - "aa57e551364f7e97585bac5d453bb8fb8dd246df": "0x11377568451f97a62800", - "348e5788d45c2ff95057fad91496c1b567e5429b": "0x58dd03e5b7fbb48ec4", - "0000000000007f150bd6f54c40a34d7c3d5e9f56": "0x20d5", - "a6bd717706bd1684ec801697926c18ca4408ac83": "0xc187fc222c212819400", - "72adda1fc46e560257526d2107141ca338fc6f05": "0x3fd2fdc82785fbc00400", - "476265f652a6773ecf35f9ca45ee404c62f064bd": "0xa2a18aa3ca3ef820c00", - "aec17b01d8b7f69ffe70fd5503656a6fed65dee4": "0x32e9ec6fd76cde4e24", - "5748131fdb60cd47fac064397ddd634f7f708920": "0x6c6b935b8bbd400000", - "052a6e2e7ac259fea1ca6c2a8ce048d0a1d55621": "0x29d22c5c9b8d073de000", - "94900841ed46c0c942d7fce7b41406002a36c437": "0x1e689b02da7a593c000", - "6cdf53155935b209f0ba5680b4f22d2744b7176f": "0xf1a8031e065e0530000", - "35b949fc5828e03bf53a2bf4a06f55f2652be0a8": "0x100e6efadfaaf7b2c00", - "ba53429fd7bbbdad471f1139d0e1fbcb3d9b442f": "0xf05a8f62c73653c7400", - "efab64aa8d6b93756924ef07d45ae6c807488652": "0x209d36b3beacf633cc00", - "afbccb8bcaa43b4ea17030918747b803dca82668": "0x8b1d0e06071ba6fc000", - "da79eeb86d08bd884e910fd0a9a19912dca903e5": "0x5ccba204424c5160000", - "f5abbe0d6a1c633b3e09e068d6f0896393e5eab6": "0x397deb8c72b4ce08000", - "728c5dd19d8d5368e52c07851df1cb26fca8d8d8": "0x187251832c01e2630000", - "8c6fbb934bbf8e304c5a83911211ee3e3987aa9a": "0x137fceba637f3daa0000", - "448f3cd44768e3e9efad46e41ef20e502d0f5f6a": "0x3635c9adc5dea000000", - "a9b5da22440dedd72535ec0525582ca0b63be5e1": "0x17e54d55803e38f50000", - "0dc0d841f962759da25547c686fa440cf6c28c61": "0x7c78f87c68cd26d4b400", - "14c49fb42d4aca62f905e3fd4eecba1932f58c90": "0x1c113db4a9399356000", - "efe09fe6d424494d4b8e652af3726b24087c1f07": "0xd8d726b7177a800000", - "2192495ce9b43205e57e1f96d38c5750ef8ace9a": "0x3d3979f5b82cbe80000", - "ba1cad8e212e79cd1b4298abe09eb6b44384ffa2": "0x14e5bfbd159fba140000", - "bc3d939dab46e05406af32cb8068a2675f68a151": "0x14542ba12a337c00000", - "4d20fbaeda9bb39f5cab2ec4f2f0609e4f40f20a": "0x90fea510f168ee58000", - "f8b62952c3e36a862a1104adfcf036f8e916b8b1": "0xd8d726b7177a800000", - "8d4920e70de023f39829dabbb775c605f681387e": "0x35fb2f447a6f8be94c00", - "c39a218fbbac7fa3e1f746bbccf0dbbf8ed9e60c": "0xd8d726b7177a800000", - "627f4b39e1a763c3c8b2291527807550842c2c08": "0x36d5d5596bced140000", - "a27f6a138a98d250451c368cf977359609bab8ab": "0x1aae7943876392c00000", - "e095d1a7ed1a1173d69d7bd724b2b7c6c81b0e4b": "0x11107053427794b80000", - "c5759e25748b74eef9427b8a34cda4b587723c44": "0xc4c854919727b658f000", - "5aca2c941e43d2e48d36ec88f306e2bdd7e4a34f": "0x2b5f68174e1fc9fa0000", - "490fe8b9aebfc2b4a8d2f38c7205c44fba0e299e": "0x7090a7679335a5e0000", - "b8c08d5f5ce8df0a6379002fb45c8c578686f578": "0x1911a5f45765ea278000", - "db8c0fff55d4f2e3e645a8d99cf87289fd6fe9c7": "0x3e6e8cab10e7c4680000", - "7763b5b3aed592da4bbf0e30b19043def5498a52": "0x21e19e0c9bab2400000", - "2e37d03839cd75fd5e80b22329caebd9c6a32a71": "0x67c98b9098786dc0000", - "6ad8849f763071abbdaaf26cc13f35ce73b271dd": "0x8c6f3cb327feae829400", - "1162c74704bb7de3f2096698d355d4a7e75dde7d": "0x3538118d9da25f389400", - "e4f146dde9a96c2ec438c0c47e190f4aa92f5897": "0x12407a49673654c0000", - "127c0522d6d5f35f9142919a25ef9bba600fa1a7": "0xeda72136b54abc88000", - "af2705a63c230c6236988a1b8fbeeaa412ad4573": "0x30758a5664c7726d4000", - "c780ca472cd312de98b4730520c9a60bc0886fb5": "0xab596b4625351f66dc00", - "67ced95188d06e08394951405754d0163c556202": "0x5170c62b28410740000", - "4240781a9ebdb2eb14a183466e8820978b7da4e2": "0x6e49017861a6530ff0000", - "c5769501088ebc07aaf4a2cb1b0304f58913e911": "0x6c6b935b8bbd400000", - "389e36b3d70e2ce86be0710ca0f7ef2017aa9b54": "0x35831c7d7cd37880000", - "8af6509811812e425ba9d4774779444fea8d5984": "0x1e3fe5598002c1c34c00", - "05656fc8cc2ef5ad7abe729b4fa313883205aefb": "0xd02ab486cedc0000", - "fe86b8b52935f378e64fb302a3ae08aad698671f": "0x43a65b3efd8d474048800", - "f16e9b0d03470827a95cdfd0cb8a8a3b46969b91": "0x116758060c78142d2058692", - "330cd818fca7420650aa6cf0f98f2b8d228ddf7f": "0x361a192eae1919fa0000", - "484d910de6023a3d21599be252bbf99681ce34ed": "0x616ff47ee3fbbc38000", - "217bf21a49837de2b4478a79507aec9812b6d65a": "0xd357bed712490b334000", - "738cf6903e6c4e699d1c2dd9ab8b67fcdb3121ea": "0x8d6a936a4b2d022cdf8080", - "27854a16ab3154fb8d8c52fdba67063ec86f5072": "0x6c6b935b8bbd400000", - "fc6f15139c29eb85d9121c1d12aa890ad7a4e121": "0x6c6b935b8bbd400000", - "00139eab4e4d35074ad2dde0e4e8b29b5d75bdba": "0x9275031d62ffeda0000", - "6c9298484429ed534e865a83b29289ff2d0daeff": "0x25966e2cb831b014000", - "6ce8342ee77e72a4404ce8d935cc12e59af33208": "0x1433a4f504c7d983e000", - "7c4f22b5b6c3b540c4a323cb4ba2939d375967a1": "0x18371efef0a92560000", - "2bcdfc56c8334ea904143ac71c407ec71b6fff63": "0x2f6f10780d22cc00000", - "ef98a26bc101f99cb09bfcc4863852a1e49cd0e2": "0x377044409f45864f0000", - "bcc75e74dd40d20cbf5f2deb6ec10fe0ac007973": "0x10269ae04838a2f80000", - "27c71ea9a79b0d036b473efa25a6295beead6ce0": "0xa2a15d09519be000000", - "90e9ba911a6e5ac38a8f8d81c7ce5803c629a7ce": "0x23bf79155c6ec350000", - "a395a37cc73ce936cd1c84d7ad31c699bfd59475": "0x30b7dac7a19b1b2f4800", - "34cc54f3c65e6e6cfd1f05e2ad0540624a5eff24": "0x387f8ee3271d08d7c00", - "6f874cddd919542b187a2ecee17d54eb568a5f72": "0x3d299d35467a7900000", - "65b3082d9ab3b608833fa9d330f6e77d1dd615b8": "0x5deef689f8a212b48000", - "db5e906fb74205672f7866801475f451f2880335": "0x39bbc42c2dc322e850000", - "493a0745b549a231a4eac6f9e8ac77c61a4fc9a0": "0x14542ba12a337c00000", - "2b27ac10e751176f29b87e706d8a832853042146": "0x1b1ae4d6e2ef5000000", - "f968f6504240a04431ef12664c9cb78c0df92413": "0x6ce6d174169f67a8000", - "5ce4a2141a0c14e06e4485f4530fb3e6fc486b3f": "0x1b0ce877a0571ea58000", - "5939f4159587b67c2f1344ac0aab80ecf1fbd02d": "0x5a95116fe52097c4000", - "dd5ba3555a67352738d15cc092d7ac4e7b0e9c34": "0x1a7c70c270633bf90000", - "c0cd39ad70b135a44e563068006335c3472a8237": "0xe5277813839304d80000", - "4d4572ef1d9275efa69383be61930523723113d6": "0x3f1ac50326f7bee20000", - "d39cc5c0ff2fca1e42cb0bfeab1b7a7ea351a664": "0x2e9377ef8fa7d95930000", - "286c6fd84615c90559eec3ef5b2c2bb0c7b24329": "0x31cbcaf779f8c954000", - "e628fb0c7cd6e21fe3153d55a517cabb5db2c607": "0x11bd4569c5dada35400", - "38c7526dda06196772b54b5f5f0d60f3caca1e33": "0x3702ba206ed2d459400", - "d47c41a290415b85e2c850812481e2bd7cd24abc": "0x14542ba12a337c00000", - "eb5601f0d33be2cdecc3dc17c6d5b24d7aa8dabe": "0x2acc6ef466f459b4000", - "9ff19bd6a90c58b7ba4e27785ce910f7c5733a17": "0x40f5fd7df82a36324000", - "7be59190342f1279e37612e96f25002ee15ddd8d": "0x53617cadb21d133fe000", - "de6caaadd171be96ed91ad08fdd89cbc62eba085": "0x379b50770700a040000", - "7f03eba5225cc6bf98f14e6e6cdd65e956cd5fc4": "0x4f5df2ded07e56400000", - "8352242f0a72fb1435b4f92f8468c469cc285e57": "0xca90d4bfdc2731e54000", - "8e5e56ee4a567d7a3c13f96e53c71df5647713e6": "0x5b68eb68ac89528e3e", - "e5df0e6c86f46fcf6082ff57af193a65817f95ed": "0xa7aa9f2acae34b814000", - "6d0d3c7d5e29eb331efdaf3a4f58a1cf7e54defc": "0x62372fa4a91454684000", - "1f79bc0932e21ea7038cb71cb412566c809a72ab": "0xe027e466e8e1ce5e2800", - "c9f57610c600e3954af53f3c822592280c2ae3e1": "0x4628367cef51062742", - "284db61323627bb89cbe80cb047a0e9d1b3e2a2d": "0xde42ee1544dd900000", - "c4bd35fcaa21d4d5670aa747c4d4df19011b230a": "0x3cfc82e37e9a7400000", - "92e8cddd9594440c3071d587dc40170880b6154d": "0xbf849917f41176480000", - "94ce3191ff314f575f2a7d469d3d6997c8529b57": "0x1675793ddbf608167d400", - "7cf09d7a9a74f746edcb06949b9d64bcd9d1604f": "0x1", - "9333156d91d0bb47dd4f2051dc03c39c04076ec4": "0x8d4d2feebfac43f6cc", - "66d624ee5d11ba7b3767194a335f9dd805039868": "0x4886fbacd9117204400", - "533fc711beaafd047d5509125dde5a8bccb1f8de": "0xe4b5aca32176b5811400", - "64a407673fdbc4dcbe6ca5ec93ccb2bc5bfe274f": "0xfa8bfea7dc0a30ae800", - "40d2935a0ea3cae6a782b62d09efc84ef838a3e1": "0xf1e25210db981f580000", - "8e16523baca1587acf774932be569e9478339b04": "0x5b72718c6cfd9458000", - "7e8b52e6bc3f237aa9cbdd3760ed25a71a11587e": "0x287c963c8e4efdb7000", - "ca7c9b00eb8a6c8f172453687249e23e89f756fa": "0x2b73af62202e8cc59000", - "a8a9aed2b30b6809aae3f3cbedb675a76203b756": "0x6b0638faf9a6f100000", - "f7f005ceab0fb3339bfd7fb8c5b7a16a4518edee": "0x10672a4f4179ac6ca400", - "b2d123294af27e586d89875038657cdb9f43df05": "0x634373d42ab510d0000", - "912e7c0e889b1b8f42807011d587d075e3f7c507": "0x3ae0db6c74cdf0344400", - "ee6b88b799884c7a013a7a6bb6451a98cfe82bfc": "0x335a9131efbf4b00000", - "fba76d6375f94ae772e8961f940a3e3b7b4b2002": "0x9e4f13f9e376439e000", - "bcb73c9afa33671eba9b71db9d834c03804c87ac": "0x2a715502a45916f80000", - "7946c49897f462d0954c7c77cf584506dc7b76cb": "0xa968163f0a57b400000", - "eb9777c0b726e3d07a20c77765f29045783da18f": "0x395d90def7eb0f2000", - "47725286d203001bf60527a83091399b14a9c87d": "0x12d21209a658b3ffc000", - "b24884ce5cf4ade9664d5bae1d1ff6dbcc00b8d9": "0x84a47dfc5cec3650000", - "24963c275f9bd6ad4ce0cd3a8a60be75b8e47b07": "0x18baf3ef176195874000", - "7a977a6cc7da0f29e477993de8608f966fd745d3": "0x62e62aee77e57ca0000", - "4a18d31025ac9ad63119d263f8c60d078ff4799a": "0x14542ba12a337c00000", - "4aab4babb932086100a20e16d96937bd6898af7f": "0xc05653a81bd1caa8000", - "fc43217e71ec0a1cc480f3d210cd07cbde7374ec": "0x57c97464dc6ef8a13400", - "2ad0e32d5526bdb1b09ccc1c7570276885e6e7eb": "0x1599f9225fdb3da80000", - "4d0063371e5361f99524dba2dfdab1a3f3344643": "0x73d48591e5a268f0000", - "1aa5a55f6d79ad3bd84bb5e3a1a7d9ecec8edba3": "0xdcd55fcbf5ad27a20000", - "a7476ec14cd663c742d527113638c77a1631cc89": "0x6c6b935b8bbd400000", - "1e2ee5f5be535743da46f462e2cdd070c80cf501": "0x1b58466187dbb74c000", - "49a876748dcbe990e7112ccc7641885e9554f1e9": "0x19545a9285ee00518000", - "ab2deb72a272c1da05c057354d45198eceeb2bb1": "0xfdf991d8bee6217c000", - "850d0120c6475092c0ec94236a22c998a35b47ef": "0x6c6b935b8bbd400000", - "80801f9b891f1914723dec435085d9be683c0fa7": "0x21e19e0c9bab2400000", - "e9eb1368117e8f6222a1973dce0409ca4834e41d": "0x47b840f68845000", - "b80589e1a2ce9504b2da8198016bd2382ef4391a": "0x1ced7240312b468d4000", - "3a9d15c57e57edc1d4220b57280e13fe67a0618d": "0xc18e8122af3e2f4f5400", - "e601708a9b8d166b6fc069d1830f0717737d5904": "0x11ab1759a28139750000", - "c541a1d82efd2f9fb9a2b04842082fe45c049dec": "0x107a539d3895ef841000", - "96cd654473a11b9785d30a74bf83feeae7deb0a3": "0x159f3877f0ebf12d84c00", - "547e71fb9bb20108da3e40579fa2c6c895cb59ef": "0x15b6f710be14839a4000", - "53ccde24d0417c6bc6863de72b01915ff29a294b": "0xdf67cb71a5b93a50000", - "97977d82a821e0133c147726812b3516080daecc": "0x48096fbb6cd0a09ec00", - "6ebcb3ba98c7d5cc561e056ac87141f45b1f0010": "0x1edeaecb9167ea7838800", - "e19459b55c58438df3477b4c7a6449f4ff5bcc7d": "0x1874f0eae79c85435a5400", - "82b734b313c6f737039ec9214ede789af796ab3c": "0x5eea052b600d81b0000", - "764389e3f29431512bae7a51b192e66fb974c803": "0x131a136e4645ec75400", - "7c9d0c46d034a11e4ed4ba800ecc2b2ea39547cd": "0xee46a547df7c593ac00", - "b7a1f91e7c58023f9a815d83949392646873b93a": "0xc202578981805c10000", - "9482f977c538a993bbc9fd3d8811a40c7c58b207": "0x39ceeaccc52a686a000", - "7bcd299eaca62673f838dee3b81f7294a28d352d": "0x6c6b935b8bbd400000", - "a5f7ebd6a9966b68192378b83487c5dda3375bfb": "0x82b99712bf0c884d0000", - "e6ad06ec79479908049a7321d5493e3d19ec1618": "0xeb6375455cd2c3d78000", - "d9611598ae2f44f6270ede42401f6b3d0f07f4dc": "0x16e853f9967829bad4000", - "01edfa6aa3b9c8e11b7d4e810b787a2212a95c42": "0x69e111831235bcc4c000", - "931d0b36e648906a27a67bc6057579984765e198": "0x27daa3eb0e869569f5800", - "813685d77a5cbf000251287e2ef9f634ddfe08a1": "0x9178c41a50493c0000", - "f2f90e7bf787db264e4dfbf29f44f258a122ba54": "0x1665c60124adbf1ac000", - "4a67f872ee2f592c705dfa98d43cafbc4714143c": "0xa11f8a90c8dc7504bb", - "382ffce2287252f930e1c8dc9328dac5bf282ba1": "0x546791902b04476931", - "10d8264d883e91340dc1cfb8325d3be625a2c6a3": "0x126d55aba09d615a8000", - "f89f3e63c122629b5f864738e07a9687c85e3a85": "0xd8d726b7177a800000", - "4fe9fc548c3f45833c5102924e605db1fa7de885": "0x2ed1821103e730b0000", - "08c1531ce1d736f15f5e3655b3619941843a6f19": "0x35831c7d7cd37880000", - "465169037da9f80f3ba4bf6d3e6da74e5c1be182": "0x2ebfac62a2fa3760000", - "776c4a6ff9004d83b4f4e8415660519cecf6e22d": "0x4cdbe492768a85ef800", - "0a1cfb09c4e37783fed3aefaa1f126fe16cef9d7": "0x6c6b935b8bbd400000", - "7141c4b643635081a4d96c3581140a27f33c47b2": "0x22219702f47436748c00", - "de8dd1ed8d58917fa1aa8c7cb444b0b43e3c8189": "0x39992648a23c8a00000", - "e6a98b7480250fa759f624069bcaf6cbd8d72e41": "0xbbc67901eead58e000", - "d7f6bad6fc716b889f84a94fee046188f08988ce": "0x36665c2d3aa87e00000", - "bde0ec4b8373e6b123e8fb76624c909ad81b5dc2": "0x2f6f10780d22cc00000", - "eed85ddd6828b8c714141d34a12d0c07f7553182": "0x27dc0a2236fc826d7000", - "c415211ef4b5d4a07b6bdba062c224b9943e4430": "0x53497c7a6fb6c580000", - "70ac21b740a4167ddf1e5233e3869a358d5a2a7f": "0x1b1ae4d6e2ef5000000", - "8ea1250b44435be3cd5841d2256ff3356c517fd2": "0x1c965d7e2a4ad6210000", - "6e929c63cf12c1da628fa751169fb4d93debafd9": "0x59dd89d7a0ce7460000", - "4115f223cfa33a1008e14a5538e1336fab853c35": "0xc59aebfb5d3b9930000", - "79298c00fa1f10992146ea04ea8c0b0603dc3a7b": "0x7631da0f8eec36805000", - "5c89754ae6b92fdd9ec4de9230920594d7a88f1a": "0xd4961fbf4f463ae5f000", - "55f30a569785c39691b7cde7e13d6db25a8b38de": "0x7228b948f47cc5540000", - "165564f31299f5760116ddf29aa94522266cd78b": "0x1b1ae4d6e2ef5000000", - "4a179a005dcbe770c6970ee390a43d2284f67527": "0x416e98da8aaa3ffa9f000", - "ee3de5b4649e4696555809be33bd0c9f99612c47": "0x14542ba12a337c00000", - "24ceb0029bfcdf0b1dd13ff419e677210b8b0d17": "0xc6c80bafe18f6cbb63400", - "d907cda4f769179017422f1d3957cba6b0fdaa29": "0x4f782b0e85ee8ac8000", - "7a9902dbeb2555b5bdecff61850ef96b4afb5841": "0x1072101454b0dfdad000", - "4c1e092647ef3b287ac55eea989419aa420a9148": "0x8e438e8d0c0c3f18c000", - "47d3407aa81bfa1e22fb0cdb4c46f7a5d79c170c": "0xd8d726b7177a800000", - "361d0e5b837fb69eb8c427dc63466da7e943783c": "0x10ae9c9b1fcdd5523000", - "d07491c9a3046f4f64c3ea37112b0a22bd4bf00b": "0x54b40b1f852bda00000", - "ef31a73046524787912b20619a0aa3630e08abe6": "0x32d26d12e980b600000", - "a41c91a61bb64b5e6b79f738f22ca95a5a81f7b3": "0x4a89f54ef0121c00000", - "75e326a47bee44613c1aea0f13daed8ad234f2e5": "0x43c33c1937564800000", - "402b9edf697d15483ff68fb5f524441399100336": "0x9335690c8ef4c00000", - "43dffea15100f119f39c0de87205c1da3498b231": "0x2529952acb207af680000", - "341ba02ca3e1c30b9e73b387b63c3a0121e081a0": "0x14542ba12a337c00000", - "8c1d889f469ec1257b3824df403c12fbaf4be217": "0xc17c48c582ac7d34000", - "0fe3a1f88ef9d9eb90176eaa28dc5557a7a39a4a": "0xb3922c0f9f717200000", - "0caf013a85c58e463b88bb49732a4220af829de7": "0x55aae2de0d9ba489bc00", - "d62e608f7ae3e80e86d1c4ebd9a637706b337186": "0x14542ba12a337c00000", - "e6bd5a6096f829536fbaa451376a7488f42b6b04": "0x51c69158b5ed4c90000", - "a430b58ce1f5b959670cf3ef4ea08e5eb8d5eb8c": "0x7b366c8d99df92414800", - "106e6a004ac885ee72fb9e176a44aef80cccface": "0x4ded51e9cc700600000", - "d6ea84836feeead81dc2db36ea49c58ae4de882a": "0x3635c9adc5dea000000", - "0042674c36d208f403f3170f87fd66d35b0ef059": "0x79f905c6fd34e800000", - "c1dca614fdd9ccb67be7b5c283375e4a87f654a3": "0x62417d8af6a38200000", - "68abef9e0d00954fa536830cfe721e28e7290a3c": "0xe1f9fdfe0cc50d200000", - "85ead50bd908df369e223465dde67a7fe143870b": "0x54b40b1f852bda00000", - "9dc86332347eac5197229e49f1887158e54136f5": "0xa75ab2b0bb10c74a800", - "e911b5e508d1c2899b0a08e3bbe5c4656b5e939e": "0x74827e2c0e5caa710000", - "4eca693366258da0e32e13c8c2e25f4b68779e9f": "0x839036514ff46844000", - "9ab365ab006eca2f7806c23acb4592b8a4a307d0": "0x21e19e0c9bab2400000", - "31fedabad19723e44850d7a0bb00ce9730d8f789": "0x84162882953a1756000", - "77c6d58a4efb2cc5a55af14ff1d1218695c1ef2b": "0xd8d726b7177a800000", - "7cc5ec7b321f6008bb443ee256ecfa9ac5980a89": "0x21e19e0c9bab2400000", - "d58fa5dc999405e872be0b1769f4dd4b07c2fee5": "0x6677a3172f85e427b000", - "801df2c0580709c31813b32992c2dbc296f84abc": "0x6c6b935b8bbd4000000", - "5558f9bdd2f962fc88a8c8686f62bfb5c2f1b011": "0x16966a1b377f16b0000", - "08742b4f6d2a968d8486ed6d5faeb6b2929fe031": "0xb6f588aa7bcf5c00000", - "a2083a919913999dddaa75f4d09f02562b8944d2": "0x3635c9adc5dea000000", - "9b56634a6b03c8c99ff57c5ea8b43a4ce2ed3f4e": "0xf5a9b5f6ffac2820000", - "23e07909fc7a40aa51451d1fe394451283630cb0": "0x592deaf009800", - "5b73a6309bfecf001c1000d824dcd8868fc91b9e": "0xde0b6b3a7640000", - "972f913eb96e981894634de674f93bfcf9b7bcd2": "0xa8d84479a689fcac0000", - "95fa5cc5f4e8099f695ac06186491db991a27b56": "0x6c6b935b8bbd400000", - "fe9d7943c2ed8650ae5839fa2752bd672b00bf14": "0x27b46536c66c8e3000000", - "f1c3604196ce8d756999599997767486bf756bf5": "0x5150ae84a8cdf000000", - "7d0016d80130e899b2a1fb6140d9f382b46b3367": "0x5a93320350bc5467a0000", - "c84a3e59d3db4ecdd868cdb7e52d72d962e507dd": "0x3cfc82e37e9a7400000", - "5ca4aef31d705627e2fad6c4a5592ab17ae92fea": "0x14c0973485bf39400000", - "e75edd66d97624ee3645a7df4df334469c4e1f5c": "0x6c6b935b8bbd4000000", - "ed985e0ffa147a817409022fb701f57d6c2711ed": "0x5f27b0ed9734ad6f4a", - "46046bbb919064bb0d5e8ee99896df39ab1ee7f5": "0x6ae543490ef963800000", - "e1012aba386f06a337872697f55aaf536a1edb9a": "0xe572805c17e5e44a49000", - "7b306b9698e244f6393af2d526a8e75aab577afe": "0x94773feab83857e000", - "08e7b0f2e945fba16756fba229dc9158166e77b1": "0x807945dceda2b4300000", - "d01e3752e5dc1e309969c0e7e33aecac668d0a65": "0xcd5fc842cfe0c4700000", - "38c94d5b87f25be130e50797b71e880e99018393": "0xdc42ff3433128ca4400", - "5dbc4543bbe0f4b66bc997236572e59b0bcb9e5f": "0xfbd4b66aa35ac1b45800", - "0062e588f4115f3e7c0cb470b150e71b3d03083f": "0x7259756a8d619980000", - "3bb7a1501c9c7ded64c4c1a070ab57e3644c36df": "0x2580118553f6671fc00", - "b454590583e861138a478d7b745ff931f492d289": "0x5990dba3e1fbdec50000", - "5b699ed276d9725dce06cba8c3fce78ad3dab154": "0x4d29d27828f54262716400", - "fc3b9e3c1d591e5a45bd2ec15ff28f23bb8716bf": "0xbb8b0c9f9d190fd31400", - "938d432d27a317e3a785be6209e91d047d31052c": "0xe3a2bac5a8ca9a1e4000", - "1ba6ab45be862805fec25e490b6fb4cf00a1332d": "0x17fcebcd88d65a00000", - "7e20e121ded9ed7c67b4971eed536e8f82873df3": "0x42a2d21aa17e99420000", - "03c011fa2c599b32082d5e74ac7856b3d7394080": "0xe8bb2c464b8dd7f6000", - "2a2278294e5199a1a205fe179cc7e2c9eaaa85c7": "0xcd4d8ea8834f88e800", - "16d6b3955e850c7f2edd20dc1c4fd08660261446": "0x893be63e7042dc17b0000", - "3cc936b795a188f0e246cbb2d74c5bd190aecf18": "0x1ef6292bb041808f1cdc00", - "76572d4d991c9c58944bb98d27c2d6bd63246936": "0x25e8f69b1900000", - "94f567bf71a4a7a88114ab679336522120ee3788": "0x1bbc61da74ddb57f0000", - "60cd287e5911d4f33be07d712beb9938a81f8674": "0x176940b51e422575c000", - "2fc5006a5acbff0224ba0920cc947130e239f994": "0x6c6b935b8bbd400000", - "fd1796e98b9e1acf86358f158982bcd85da53672": "0xcde2e768f8b22678000", - "b7767355aa657b3ed66f0de24da5018a5e2862bd": "0x2d26fec3804d2a473c00", - "e09cdf0091bce57a7dfa736b9829c14cffaea501": "0x35db8ef19819d7e6db400", - "74ca91b349c9bca8b8d3a7dc74e76654efe5c4e3": "0xd458a075baedc84d8000", - "8982f762c182a9035082e5fdb915cc490acf0001": "0x5e3d54441cb1aea75c00", - "af6b011e71fa58e04fa0e5473ad8031f1ac2a43e": "0x1f9a12b93458bcbcbdc00", - "82432ff8c8f73bc3a109c7190a1ed82961535196": "0xb1ca3685a2e275784000", - "a32d1e2855dd7a5f34b6fba74374114edb33ecc7": "0xa754e72d5004a84c000", - "bc07e1f7352249aed8bb2387394060e053ca6f78": "0x1570dd7d5b66e65fbc00", - "1df6cc9eb097fa089740664e201fe2f4936396e8": "0xa2a15d09519be00000", - "75801f900f7c29fa57a192c4854f38ff9749b0b4": "0x54b40b1f852bda00000", - "4387170a459617a2678c6522a615eb5e2c087547": "0x7da7d69e4ef50e72000", - "bd257831fc17f396b3366acf6d5545e081f0fb9e": "0x299133d34b9db05f4000", - "e90f0a66d2934610eea07c4358ea89141cdac12c": "0xc67e20cad9e2d980e000", - "ee341a316e8c64d4b7aff8426f65ce930819a560": "0x2a01a775bf8fdb69a000", - "fa9f0487efd76c41f776601d249d32d392cb62aa": "0x25d758ae66d280b00000", - "1c4eb529072c60bdaf945ff7222507e56e7fb98a": "0xbeb6a79662a52508800", - "a86ff07d9dda5be461df3ca69207f9530627406c": "0xf34acb66b5283a5b8000", - "ce66f1065fc05433f47a7fc55543ed5039580251": "0xf57942b25d6b5d5b7400", - "88f97da887f9d1505bdfd1db7412f5cd10fb0fe3": "0x769c6126d57ce42b800", - "1db01313ae792617ea1fc8f4f7f106e9ebbca108": "0x380ffbf65a439c720000", - "7b54ade47767d909a514fffdb6b707920ee1fe71": "0x19615e3152885c643c000", - "6eb449dc7b4c43a0a42cbc868155fda9f7321613": "0xc160c384ac64974c000", - "d47705dd4c225ef298ee3dfca4309ebfc97d8f32": "0xd8d726b7177a800000", - "b1a63f663013bd0c1700bd9001a3d11569342c76": "0x32d26d12e980b600000", - "9aaf106ee832e8498ea26bd53d60829edf6aacff": "0x332cd9ad6ad08b487a800", - "2a131423c06d4e279c948cbdecbd324f4462b8d4": "0x28a857425466f800000", - "78c3968c80c8bdd7f790417c8c8103614b00733a": "0x5af02fb6cd5c400", - "1287f041c25b9473511bf75c12192bdb7863b834": "0x4c0786dcc2eb1bb8f800", - "0f55ce13b10dc56b2e7e6c7d5ca159c1c394fb34": "0x28a857425466f800000", - "774ca22280f1c68ed8266959aaaf776973a8ae33": "0x43c33c1937564800000", - "229980ee451e5eecc2fbd18b0c966063af4fe5c7": "0xeb4a7b223e9cc23e000", - "af5a5cd6da86838efac0ea91a712857d6aca53ac": "0x1f6a51e5986c54d2f4000", - "c8212bf2a583e4453072a22b6492095e2f405ffc": "0xc6765d125053000", - "e692256d270946a407f8ba9885d62e883479f0b8": "0x4080f3d670b26df34000", - "f33e1b5abda6fa7e0e197773865ea07f01e6600d": "0x65a4da25d3016c00000", - "ea39894b1575839abe3db248edeaa033aac22e4d": "0x1bbea7073ea2053ca000", - "3c0b0fed46059785d85d4dfe83ded03383ec348c": "0x43c33c1937564800000", - "11af105888e2700011730c344c0d96ae683e3a44": "0x1b88c33ac0e97bb0000", - "fc48658194dd12fdd3999f3c4f9b89c0674d31dd": "0x995aeea1ce37969d000", - "bf49090c06576bd057b8383f70278b41a2cd22fb": "0x80bfbefcb5f0bc00000", - "a5890dc344f8299d0a44bf58b11cd8720da98605": "0xf1c49f7b6e914f5e88", - "5e70556780f7f3b8e692e9518fc770824568da93": "0x37e273559593d0bcf6", - "dc134ae6414eb7c0baa2a0b0d08edc0de9af22ea": "0xa2a15d09519be000000", - "e007ec721a4e99b690186f0fdb678a6f6ef8250b": "0x79f905c6fd34e800000", - "76209497967db6546e84962c5bcc63ba9f64b6b2": "0x1a6f9e30dc9935ba0000", - "9ad832dc71141a8a9de1f7b396930c6dcda5b09e": "0x10846372f249d4c00000", - "50ff1d7126a6f67ef34104e9eaef5d88e0b7e684": "0x3b4ad496106b7f00000", - "d782e53a49d564f5fce4ba99555dd25d16d02a75": "0x22e93c3c55012d04394c00", - "4e00ca2cccf98319eb48f923b3124fee53ad4a29": "0x11c9a62d04ed0c800000", - "ab0c00c76841277567c9bf0d56effa42493554cd": "0xeecdee027d95f4362800", - "b66bda4e7c45ad4274864ffd01d3992fb219f938": "0x137c0c585a69c6a68000", - "2627246d635447d99f51446060b5b51ea143e0d9": "0x110376945f22858b8000", - "93fba1fb3c19dee4e64a6d0df9660c26486b0b98": "0x581767ba6189c400000", - "87ad1c51a4b86508434eafc5723fb4d3754d7fbd": "0x174b1ca8ab05a8c00000", - "c125f610614d6ceaf688a9a0ab2bbec0a83b0be7": "0xc1cd1766d6957c00000", - "0000000000d9455cc7eb92d06e00582a982f68fe": "0x1", - "d43f183a9afd3b643bd20528c08b684d75d7b25d": "0x92ef8f327af3c04c63", - "03d6968f256d052e9e7777361c3ae4bd266088af": "0x9215930de35c50694000", - "5d1b3f4a308f8f384fb442a436d3d51da39d2b29": "0x27bf41150408b21afb2", - "c37a59d8134e625141af7542ed1f1b89fff022a2": "0x853e88017799f1258000", - "c52ecb379493bb7bf3357a934755e4ec84a228af": "0x27aef96f683f2b200000", - "4ef7df3d7080d60455eafbee44d59743f27fbf89": "0x2540be400", - "0022d0c4cb8c58c991b87b18a3086e06f7e966fb": "0x3cfc82e37e9a7400000", - "ee678c6dfc65e30cc4bd53ef2293f42a8efe1819": "0x17b7883c069166000000", - "ae5fea9cb70f1b93e034393d6fbec75dffa98119": "0x57fe7cd37abe52c91800", - "5dbcf858bf48b487eb61c83bf7fab66215d1e07f": "0xd2106d815ebeac00000", - "7e22a2cd3402c18d41385cbc2640044428c3ae4a": "0x4f0f8c633c09bbf219400", - "34c5822a1ce15d5dff92bfd41fd88f6f75cc8b7c": "0x33db4cff4b04052bad", - "7aff1b4e704ed6f03294bf86035936e48c8f24b4": "0x48bb0636d2fe1f4ac00", - "33010c8a700132428908b0f29705829897ec5505": "0x4d95a75bfc897997f000", - "b919c508b1995d73e3e09402372002fdd8d327a9": "0x226041556ece028c26400", - "ea539841ba592bef2ffe2995e2bfda503bc94eae": "0xb1662c00e0941d766c00", - "01705c640bbfda414c6fea463a56f9c768170f5d": "0xd8d726b7177a800000", - "5fc4abf6a0f552576d23d2b42262f642f0a8196b": "0x1bf3bbfd9a06ca800000", - "6c8e10c9fc83035e2cb75b559bac33c80f753473": "0x5d7330cf159c9d3c1c00", - "0567c478706ef12051b24952d5c6e718341d2a1d": "0x294d077e2412e5948000", - "eeb27799aecec7be079cd93aa30cd6ab59664681": "0x791bca5791f0f7d30000", - "809ae086c8fde3f9d12d92ccf7ad87544718c8bd": "0x27ac02a37049c4f1a8400", - "a8f5b0d363631274f5fc565da86d3ecb1ce2b0c9": "0x143a5d545353b5b21a400", - "3f16ef82cf49de521b520a0fe8b67c33700353ff": "0x1d0ad86afe1908750000", - "46ac83468a4cef8b71a419826c939db92bd27f22": "0x9c9187dd0070568c000", - "afde7d98fdd74cf7dd20498741f5b0fd16571fee": "0x1c34c95604276f40000", - "60b9a7908dfd57249188ca7e8e4bb8922cbcd6c0": "0x6c6b935b8bbd400000", - "7d97c1515189dcefbb994c702ba937a9dbc17367": "0xd79f1f783933ab41600000", - "5b7dc97171fd90bab12d592a133b64cd03532429": "0x1da224e8864a9c128c400", - "197305c1f2266dfcfffe52dea4b2b6ed2ec7d3b7": "0x10d1713925f41ef9b4000", - "73b4c79c2e8ebe42d235574c80e753ff6a37ac44": "0x14c117931fbd059d0000", - "9dec4b65396abb89b8683f791af5e42bf3c70cbc": "0x14eafa2a081852870000", - "813d86ef3a8ceed6c6b8474c29977781d29ca1f2": "0xb949d854f353fff4cf000", - "a541d70fa461f97abd107f1de0ac0dd26a378d32": "0x65a4da25d3016c00000", - "670758723fe2491159bf3a00c944eeddb5524326": "0xab1fbfc4aff0da80000", - "66890f616b03987c49316237de50d9a648486ee4": "0x13c941cc5ce8718600000", - "f69ead7e4a9a33e94a6a2b775bdc48b627409873": "0xe89aad8055c9db79c000", - "6e7c2c4a4b43574d88b613c75a379170227f225f": "0xcd3b1e7c43abe8695c0000", - "33ba0e5e7072d173e5ec42adbe3257924a3a65fd": "0x379f03290277cb53a000", - "62d63efdfdc7804666725f4cc8cb45c1a41a0809": "0x505e5e31c95981720000", - "94fbdf35a742bbf5696b990b0d17572dc5fdd443": "0x9e57290b0d4808334000", - "2fc50dc9954d5a1db8992130ce24a7fff0561d96": "0x4f2a3ccb61716682400", - "03fa141eb1b320ad66d478621da2661bd299c505": "0x29fbbb416df407c91400", - "9ed6b58dee50a1190b20a7b063875e5206fb318b": "0x154a9781fe05e20e9c00", - "4b9caa0ef57d9dc0c6d82ded7157d444b7a0dbf2": "0xafd51528757452a4ebc00", - "cfe9b1bde08cd2bb57cd68042fa42885f53ebabb": "0x3cf7c7d08304ba0bf400", - "acdb2d226b9e3bcfdc3c72c20a95281ad1e2e8e9": "0xbafe21aefeed29f8a800", - "962803a6c21d8537f772cab98d7240f772877224": "0xd680757e124a98b0000", - "00d0e16a49290c17936e07a2f7eb834d8af0e8e6": "0x61d13d86dc9a73c1c000", - "51669ba0f2c88f282a1577756bcab738fdeee445": "0xfe1c215e8f838e000000", - "cb7f5fa6700dc7723c5b8e2c71444b7f32c4517e": "0x6e8c0d22b17c6e818000", - "b524f622df1eca8f947910c83167246e723d11f1": "0x2176f86ec73617820000", - "226e47462a47b4a1f2feb21a2cba49a4faaf0eea": "0x25688f26108463bc8000", - "b13939bf58ee023e4632585ff969d346dae50632": "0x34ae806d7d69df41cc000", - "91ff0ab3f03affad3d1812a80395942af808babe": "0x9ccbac9af96e609c95e2", - "32d73d73d7c5a71cfe809339482c5a3483cce934": "0x921cf9965bb10cb7a400", - "adf204aa4d49a51f6da8ca991cc6fb7c9091f9ee": "0x20e5848c9bdf026010000", - "8df2fc374cd4c42b7431d1daa050719b7b17f29e": "0x32e09792ae9647cc000", - "116cf261f103e75abd2124a3e9260cecc0a27417": "0x73d009f45ef7d6e0000", - "8ac83693dc50b15d073063c485d5c5fecae5ef3a": "0x258afb20c2d249a2a800", - "03f28a062a5a10f0fcf9e5a6839b84a535441546": "0x9269fa81f092395c000", - "aa9de7e5872af0399711ef7ed6db246d826abc9f": "0x17c477b31039a3908000", - "66f826927abad3cf950abc1b4a98fb2895ea4630": "0x4877c308eb2a4c204c00", - "34d7f4444a4d07b6518064142352b4d8df894c72": "0x207cf2de3498727c8000", - "f6d61953d5f65e6074ce4fd8c090a3c4c7a4ba4a": "0x9993fd574b0739ec000", - "cc92fe410cc05cb2b5013e662dfd6cdfc7464435": "0x5218cac5e7556735800", - "76bb175951b20f73970620d4ab4db0b0489ed154": "0x1d38feb7acaa0240000", - "cdc34dd95b1b2f9d0ea28c459f31545059e38fff": "0x1543f059d8790c01", - "1eb288a62d0a8575392bc5e348610e7016ae5b96": "0x4a725297c61905b00000", - "89f29e8b677c7c6a5d2f9591eb200b27933b55f3": "0x3635c9adc5dea000000", - "d26f1e487588a9ca36c6017afa3e0aedc5d89a38": "0xfab8c4c3b325a400000", - "17854ac0194684e1e3035dc42d660257afbf6dc7": "0x14542ba12a337c00000", - "b395800db40a50dca559636d6ad16fa72b48ca83": "0x8ec2ca07ccd62bb31400", - "42aba1f20845aebed7a7573451edb4dfb14e8cc3": "0x2f87347b2358adac000", - "afafdcd32b24c73a3eaf163ad5d51d9365ecd5a4": "0x48e3e360d41a39b70000", - "26dcac9d355d3e07f903d79901ddd93d88ce1450": "0xed668f05ef6425606000", - "39faa73c5aeec0df2dbc784f8e105a2bf70d1060": "0x20e404bc89f6f7300000", - "fb1b6f568a9c5db31a46fd9f9dec0d96a5f4f5ea": "0x10216e3a35713b288000", - "cb6b4d5bb7193463571bcd059602b469c4b6d671": "0x60d5ae1cae4e740c18000", - "3011756f89e3331135088db3a4ce1662467700e0": "0x69e10e6529d005a5b000", - "921ec9a90629296b9080524135a207d4b493c3b1": "0x33788529e8f3586ed400", - "19c77f64dbfd75ddd278471791d7ce5c1023e307": "0x67b45ee6a1f53a2c0000", - "db4abeca8baa459e02a9f9183990913c28d274f6": "0xea51c5bdb033c9d8000", - "d4960fb3a1cfdc3f8cc1f7d1313314aa52d17781": "0x5f2e596a16b3b8faea000", - "c81f16561a9dbf29f4ce95fe4a4cf76469b8e051": "0x6c6b935b8bbd4000000", - "d175bb697772c0d056e2062d8dcfa2cdf95600d2": "0x1bb3db3cf723b0b60000", - "1f1317a556fa5f38adc7c00deecfc20c5fe04bc5": "0x1b91adcb750000", - "cd793814531b98c3f9d60b3c18839510224d1069": "0x21e19e0c9bab2400000", - "34424fff5e91d7ce07e82d2e9c8a1a02fa4d0cf2": "0x8655cfe59a210b2640000", - "0c57387e2638fd94b2789002c7a9744c7d282095": "0x84a4b0470ac5bfd9c000", - "f9a78bc0c53a3778e421c774ea4e18df5ed9cf99": "0x2413fe384fdc62580000", - "37e4f8789a5334d753e59232c9387f0788073bb7": "0x294af89f5db893e00000", - "0cde26ec39e316008a89c1535cff6b3cd91037c8": "0x3cfbc9eced37d598000", - "45bdec2e54d476fd6dfd5ab9954856aee921204e": "0x15cd3e136032fb754000", - "434e99c77b81b389a215b7f196cf7c969de034e6": "0x14542ba12a337c00000", - "216515b80f9c507b8f2eedecf858df841449f715": "0xd0b4d6611b8c421fc00", - "2bc2104889a3eff2e19476ccca2f401f0639401f": "0xf440b910153189fb1c00", - "2b5a61876e84e8f4debed56d2edac5b8762dee34": "0x49e7c2f79c5dbb40000", - "691e2433658f28a6d949ed122d6c8f5cdad646fc": "0x2257303208dfa730000", - "c3465b3200ffe73e5ae9261cbb6455b842e5bf79": "0x3635c9adc5dea000000", - "9fec5f1c8b0735287129d68293a10bd9d7ff41a8": "0x3ed3003bdede62faef", - "824f36f593069c8e399882c7175e5be425bb204c": "0x6de70bdf4c2656600000", - "320352a81923c4c38950d77a2cd268b47082c477": "0x5656bb299cb79890000", - "cac88c05aca7456faca8a4f0a472ed00a2136d9e": "0xea2bd1a12ba07f627800", - "48217cc841c61962c7f91e14d2ba94c0ec216453": "0x114c9eb6ef8fac626400", - "a94d5575cb9ff0374b6439bc2f8095386e2110e6": "0xd1be5fc573c19842800", - "577a89f8c102d90b743f8513e2e1f2ab159b3efb": "0x48a46f1c7732b21c000", - "e16297648628864d0ef33849c0f546cb378600f2": "0x30cb984c1a494530000", - "483b000eb3b5831b9f400cbd0d478040fe555614": "0x1032112545c87e5f0000", - "d47ca344382a84ec0c57edc2b0324c195512506a": "0xed2b525841adfc00000", - "df226a4fb8c0ccbc34598680fd45511b97975c0b": "0x14fef655e75b1d428000", - "3e8b51930726084c1b3aa7457b2aacb69316a35b": "0x40c2d9d4c8f16e205f000", - "0b468c986b21896d4c383be8101a639ecc99f8de": "0x14c0973485bf39400000", - "11a7b0949eb7462a5fa5479b66c8a0de3e3366e2": "0x12f20ace0f985d0a3c0", - "4456e7e7fe6917a8c35e1c41a7895f0299bfee3f": "0x3b8ae8c77dee295bc00", - "fa619fb3ade03f66a3b6741ac1f8435c98b6f48b": "0x475f8d336cfc0212c00", - "d346b3ae2c609715dc734e782ab79e5e822a05c9": "0xd3c21bcecceda1000000", - "51011827d125832615b00d655c3f2d4c190ef571": "0x6816fd20ad926ee1f000", - "51a9925f424492018e5303b764bc8499a616344b": "0x291613c605cffaf58000", - "b5f9b60cc10f6d62c070d138a86a8ec66e8bd9ce": "0xd426d9030d30c39a0000", - "62abb8d76317c9e7971c7b3c4fe8dc09fe43ebc8": "0x1c34c95604276f40000", - "60da395f0ab481f168898cfcb59d4de84fb91a0c": "0xd2a94da1282105b8e0c00", - "f6405e76db13f76527effd3915fc006a738ddcac": "0xd06fd14ee237f2d26000", - "dcc8f181295cfcd9ce83f8be87cb16902c2e1aef": "0x2733e6ed9f88a8718d", - "f5d4fe68bf108c8ab47e7dcbd476028dc8dfd873": "0x107d18873fb1f1490b3c00", - "f06843706023a64631cac100810cb2aff5d4dfdd": "0xd4cae3884393e2b84000", - "996573a79cee4ef002b0d7a22fe96ed07d07d2fb": "0x5150ae84a8cdf000000", - "d17e3acbe2ac9ab9a84034853da85a1d2fe34839": "0x1602e04e1da812c98000", - "9dff45e2f24bbcb5a7ab6dfadd05b779f328ed76": "0x11b2d5791246602c8000", - "8fb2243f63bb4c4b8f30de45c97c62f3e0dd7231": "0x130a3f69b73608a80000", - "f4febbdca48bc920b885e47731cce7d43eecade8": "0x160d9f74c2bb6d85ac000", - "992de632b2610b9b2244aede09009b9edf107412": "0x3635c9adc5dea000000", - "c29165bb0d04eb40474c9f286bc33dbda6337877": "0x80bfbefcb5f0bc00000", - "9be3811744fb4631a0f5a2f22c40612520cf6f25": "0xa2a15d09519be000000", - "7c62fd31b807172eb37a61ed7197f3ea9e11870b": "0x46889d7fc40257a0000", - "17052860afe02908972179042ce66fb3f8f95467": "0xd8d726b7177a800000", - "a2c0a760410bce4c17b8308c4f6c32435009c4f5": "0x12407a49673654c0000", - "584fd98052f1f655c5b63576c280131f83e1f69b": "0x3e7e3c584bcf7370000", - "90d89a5be19d16802c5038994f1a8f6e9f13aeae": "0x1a7843b5e813bec2a3800", - "6734cdad6f2aa3b7e0ca4dca3176743d23a0f6b9": "0xace02d95ccfc1d14ee", - "17d0fd4337307e74f369bde8af43ccd9e0149e1b": "0xb02ecf74c3138800000", - "fa4eb038fe706657af4af9e399414a29e7d840f4": "0x21e19e0c9bab2400000", - "c619f9fbc4132b0e82c6d9b386b56ad68336a90a": "0x37c8feb0359c73740000", - "40f7e5d7011429f2f6d1780926cd09f05985defc": "0x48ead53a5b1cb0b20000", - "cb43678810de7aec74b9d563a6c19b172be00e02": "0x3635c9adc5dea000000", - "421ed062b0a81f228c9b1ea627cb17d7e4b01f62": "0x6c6b935b8bbd400000", - "12c652b35b780aca9c9d1046a24e95a55c91735e": "0x5796d946a2021574000", - "d3ba13820610b51bd779f837f137e02c16ffadfb": "0x4f5760c57eae56b4000", - "f692abd7bfcc544c3bf6729edbd5999910d85086": "0x69e10de76676d0800000", - "8e4fd0a080818377adef093d99392813c3526298": "0x1147a73d9e1311c80000", - "bb7af1c0fa6cb35de397bd4a1edf089faf9086fc": "0x130a07b51f7a683d0000", - "447d93fb7675df5bfc55c802ba8b14058dda8c9c": "0xec0b7cd35a1b07716000", - "983873529f95132bd1812a3b52c98fb271d2f679": "0x47473f5bfd4db38ae4400", - "ef5e3025d2a30a9ac5b865a1cdbc3fdd486e3e6e": "0x1714e6defd3fca200000", - "78858b707966a044f4c800e364be9e4c8aadfac9": "0x4c3ba39c5e411100000", - "dc968d39e4df44a677f14d91762e58b630bbceac": "0x101d6a05a6fcea778000", - "2d055ed92a7a64ba94b5f582a5cc1c590469aa62": "0x182fd4901ae7798a84000", - "14cc5c0f5c2f3902c7a77b5577fcf47c4088026b": "0x3ca5c66d9bc44300000", - "9ef25376918c42fae7b406182d71a41bc94de486": "0x1849f2b3725a596aa40", - "e4459d8ca5f71ec713ca5ec170c909b5ef14b4f4": "0x65a4da25d3016c00000", - "37683d1f8266cc9a8f49bf1ba8d5e726be058a0f": "0x4962119122da8ee40000", - "e7950722d7b64df1ffeedafe7540526617903443": "0x9fffed6a65a022ecb1000", - "7d3c1d9851ee5e25fa9858d6798bb33aafc29059": "0x152d02c7e14af6800000", - "7e926a05710f400b3d87c11e3ceca3e27eb6d744": "0xb02ecf74c3138800000", - "12afc6451f21feddeb1f732d6baa02684c258d9c": "0x5ede20f01a459800000", - "26a9bd296785e7b2a5a2c7d2d4c9dbf99e27f340": "0xb6f588aa7bcf5c00000", - "882f7ba8c279f933f700cf27ec126b4e33219a25": "0xed2b525841adfc00000", - "af745283ef17b9fe19bd1a24f97ac4b695e91b59": "0x47185e9de62bac7c000", - "b78c8080acee6337fff833fc0f1a3290cf4f496c": "0x4977e392fa5245167000", - "c3afb546c0c78c51b6f2e42c28d59aba37bd53bd": "0x48d6c3de44f2c4d1c00", - "cb2b4955529d1963d775516f65055965a9c98bbf": "0x62144ac6b8e5a0ac000", - "a6705c4a0db23834b10275a27bb6ca382c42c301": "0x7695a92c20d6fe00000", - "1957bf1b9b3c3922403b6864c2548624f53e7cbb": "0x15f2217537cacffaf800", - "6c33a7bb4903ddfab6db46763170679cd60bea76": "0x3820b6ecd9201180000", - "d2192242d3502e6ea222faabdfe1377538b91ae3": "0x1f740364151c35c790000", - "d803a2b59f609e39402da27d444e4c5aba2e6ee9": "0x136039959c3912300000", - "0269c4f59bf6f9c17a3f9efaa167c48e5228ee02": "0x3a71fd6f59540480000", - "5c0b6ddd18ac93f55ad9a2a722a50d7451c48f29": "0x9f44e21556688f3000", - "90e2036d2dd6fcf0db6394e327ea826bb221844a": "0x3bbd5ff5ed7f526268", - "5487b9034998ceaddaf182027e3eb6bf19aef0a9": "0x1064826c0f06e62ea400", - "077229b22d3dfef1d5c0355190daf32122372284": "0x3b4ad496106b7f00000", - "7bef23c242786ec2bdd37b106aeeb2d4661b75ce": "0x3f5f33f05bc92cdd4000", - "68b1b7423429f0b21afa88eb6119b589bd785a11": "0x16c3aff24ec19b50000", - "ce18fe0c066c8d7071f945a91b720520b998e5b9": "0xdf9ddfecd0365400000", - "f7a2562f05de4726d6364fcb59d83870462c16e3": "0x21e19e0c9bab2400000", - "e2a1239653ecee091527959b36cf6c33dcaba253": "0x39992648a23c8a00000", - "52aee6ea09e6fe05bc37ef54e74d8e3345deb925": "0xd961b88fb05cc68000", - "d1c56591397bcd47675f8f652f893daaacba62cf": "0x11c9a62d04ed0c800000", - "874509d195a850a9a0ae74fd1c6596f465ea9e9d": "0xb6f588aa7bcf5c00000", - "31c2ab99605e535e96097188bc79752e1b3e5817": "0x2f94201ce3ff6a9df93", - "c8bd8a024d829cb2cb29d28efd463fe010d58c17": "0xed2b525841adfc00000", - "3b094bceb34d12060bcfcbc35c353b8c0be3ba4c": "0x5cab153e0e85217098000", - "1996fe410d9e008397831b8258a78a3cfe5e4702": "0x24ec407b07254b1c4000", - "f0b3585c7e1e2f71884836efaaa9adf6aed4a88a": "0x1364ace3b4585954d800", - "bf45df6d145fcee9b1d3890fd873a7e88233a2ce": "0x10850c5a50679d1300000", - "715137e40f5d088a34258dc5436b27ede9ca2cd6": "0x5cf93946ad8cd7f78400", - "42ef9c695e46f0f4a090881b1c3f04b0d2508012": "0x3c9d7b206a65383c0000", - "0ad6ebf9bc5a3ebd5eed3f10d5e8d330fbcb60d6": "0x112339593a3a655a21c00", - "911171cf4544ca2587f6a08d6d2d17f0a7e9b828": "0x13303fa2fb4f50295c000", - "314519d1dba1c9d4925a49d0c9496426f00a1141": "0x32fe235f866809beb800", - "68fb35511c653c5b4eb86ea273ac767f7c005cea": "0x61325f6ae3890f08000", - "383ec4dc4ba683d37f618ac387b36dfbfdacb0a7": "0x1bfa932b4cfec56875000", - "5f9c6e923578220bc064ca32ff0ad7471b41516e": "0x20f9e8a266d296288000", - "979f0d3d7c8ae6e759604d68e721f2fe5d83d28a": "0x43aae4f8c63db19c0000", - "591d6ff9368a9ea39cf3c3b8b8fe9ee44cd89234": "0x69beef3a7ced51efc00", - "1f5fcf194cdd30d569166e683e5c34ac69deef7f": "0x1a8ba1567f0414110000", - "aa044e756076725a5e2ecd64cc0c7473318c4697": "0x4a18056ddb4f849e0000", - "0a97528fcca299a5c0c01886b9b8eb980d23415b": "0x37c888f2a19ca08fc000", - "c63c7e6210a755223198d007cfd7b7f41186b4c3": "0x43f68a1a7eab649c000", - "ed889826c46b30ffb66e15ff6a0b796acf85316b": "0x116cfcbc003d0d8d0000", - "401ca370aa563e344bc6a34fcbfb5747985aa116": "0x257cae946ae7d7c5d000", - "f7a558692dfb5f456e291791da7fae8dd046574e": "0xee72d5e629b28be8000", - "b70191e1450c4bc9c52ba20f6202155623505d97": "0x167d618be8d721048000", - "331b219a977ad1dbf0eefab517303a9a30f71f65": "0x2eb91bbdc12bdfb235800", - "23d9e240b8a7360d86e64a1b5255c87bc21cc864": "0x8ae9f7e28da5a230000", - "6510dc3628bd1df6c40c39db9437cb69d007fe39": "0x8b56c40c840c7b151800", - "ab37c03d5521c0909d08b42c867bed90c4a4002c": "0x4c55953e14a0ef720000", - "1f0f23b691e7bbeefe3114abf050ae2887e2ae7a": "0x4777e962985cfff00000", - "179bb2f6f0dfbe84249240ce27b261ab5c60e8c0": "0x9ed194db19b238c000000", - "5d5c3c9b4a89df454092546e67cf9574e56bcd2c": "0x13705dda137dc0782000", - "070275bb30ea2403e0a4f3b9ec95b90ab49fd4b8": "0xa293c9a138c2cc0000", - "54d2ed408af3973cdb046159fbf17e451183e0e5": "0xb02ecf74c3138800000", - "8b8c46af7021ac0f56f186de054464890155c3c7": "0x159296ee5294e335bd9c00", - "32bc13944dee622604c12866ea33cdff8464ad32": "0x35efbf87eb95eab9400", - "73504a496734c54abff9a7b99b1b8096acefc75a": "0x7bf75581a407826d2400", - "2ab19a66581e81f99c0f21dc60a8dfe569a08ee6": "0x1c4553c6122819a4dc000", - "16995a6ab915f04956cb336bb5315e0a3473b79e": "0x889a1b9b93c585fce000", - "ccae13dce96acc8206f139ce019c8f65ff830005": "0xdb5b62054b22079a2000", - "fb3f64b78ead2ba6822a9fed13e24e563beabffa": "0x5186e44e5693cea00000", - "31313dfce7521022302845f823f0101518c7b94c": "0x2ff0aafbf8f4e9eeb8000", - "166726ae0b0d93a011e50c1cbdbbaa39dc2b2ea2": "0x3833a7ebe0633c698000", - "a2fbd19ccd08dfb407116cea46ff6610ab035725": "0x1ec382081c2334e2ba000", - "0999e9fe953df5132495057ebd7361e5f9c1acb2": "0x294af89f5db893e00000", - "7557d513a0d558e78287de88af332b00900dda60": "0x6439ba6a3337f64a0000", - "03d5b2244b0a003cd748030f80c27be44cc587ab": "0x826cf7378db81d32ab000", - "1661cd461bf10c491e95a77c196c6e52f95e81a1": "0x4b9dac856598febd3400", - "a8aa652721ee8fb7447dd93244cb158869bdfa0b": "0x61ea9cc5d088d1969400", - "233e7734ef5b8a93da3ec829558e5ac72063333a": "0x923e24c6a4a59393a000", - "7b0314825c26c575ea08b40476b6bc9ed76829aa": "0x294af89f5db893e00000", - "cb53181d804041432c30679ddebc411be804566c": "0xa5bda5e2a3e7e8e0000", - "99057c2fab2ade11d37f36e8b4ddccadcacce320": "0x3eeaa9b266ad93f0000", - "5cebf372b1b6bb77bee6b3d67191712ad2fe28d6": "0x33512d89169f7426b0000", - "e83931a3b678cd672f96c489b22c8283dea14cb9": "0x11fa8665f6ab24915000", - "067eca2af60cdf8f049321cfc98ffa068fb37962": "0xb63e7ea8dabe07fc000", - "467a755ba59267678e14e414ed214c3cea836498": "0x2d79883d20000", - "c34147ee35d4e5d5036556bb111f528599c49e82": "0x1017f7df96be17800000", - "a359503345c598e4b6e59fc9ef051c492913dc13": "0x340e1eebb54482690000", - "a0778be959b504d7f1cd106a73a61dc1db816dda": "0x32d26d12e980b600000", - "9e9eb8f432153a9d9b1f498008ca60a4e23d17a0": "0x3bb77e335910fca9000", - "009e45f328653db0cdb971fba9a85c8e297743c9": "0x66de2f9569b88cb0000", - "0cac8b08bc31f7e479432983a58cb1bbc8ca5308": "0xf6f2dca68ae01878000", - "244a60c58c09a2d96f06c34aeab1745414029bd9": "0x1b1ae4d6e2ef5000000", - "f9023e4fa0e11ad57d68c5dca90cbb67d15a47fc": "0x50a528b79aed50b1400", - "2ff5bff45ccef3055e3c9d7e5aba7bafd52484fe": "0x14542ba12a337c00000", - "bce8c967672f44ae80fce9defdd694a5498bcd47": "0x24d03f0628093816fb400", - "8a10efe3da4dda222dd0b5421be40f833b783464": "0x2c97c95779c391c18000", - "150f1652822e7b5074551a6e6178a47167519fd5": "0x6687b343700a36b82000", - "90bb2d3eb97628335d491c30ff95939b01970891": "0xad239043f44bdb828000", - "a6aa09e3378cbb25785b2c5bdd674e552d37eb96": "0x187c0b371c7ead0400000", - "2f9a0bb9a50c85b1163b70b6cdf437881892f4cf": "0x1236738260586a90e1400", - "cbf3faa980052404713c67ce564f9f5a2f0a1384": "0x80bfbefcb5f0bc00000", - "ac14f495904e1236a584be5f9e38c5b2d4483ebb": "0xdfddd7b6f44de4f00000", - "c0f73b9d9c1f5e5d2f0e43e5af8a8e9a6a154129": "0x43c33c1937564800000", - "d82b39b48026ecf5f051134c5fd11904429bb541": "0x6cd4788d8ad2402c800", - "8d8135f83354228e6b3b024b3603dc36f6a6d957": "0xd4c6bae47b59a6afe400", - "e062783ea5bb4a15f32e0dfa42a29bf468bf4026": "0x2f8164d399b30b14000", - "465a8228594a8437014b092f65e925bc0473e6fd": "0x3247ef8461df751f0000", - "4426a1fd43ae277baaf93e152e182e66b10b0bc3": "0x898a33bb7326b689000", - "a26173a92e338e68021e4a3efeb96b2a32992c4d": "0x6f40fb93fd2c569e3800", - "6a040bf61a26c02839a68670ac14f59ab8e00533": "0x1d5fa5ef1f24a189c000", - "a41e809e3c1be12a9787d2778fdf1d42a940a373": "0x18d4fa9b6766cca69000", - "9fa56eca8cb24c411e1b628099d091210c5a72f3": "0x8f8457d2c0581c50000", - "adf2e9e7d75d21562e9306bb88fcb0ca0717b7d5": "0x15326e8f3f7859900000", - "f7f870565f56b9f509794690ed2a4526d1c0cbc9": "0x757ba83f9eac68d70000", - "97bf0f43bdf40c734dca5f1d73e509209b855a26": "0x581767ba6189c400000", - "de438689015d32998cecd13332e30875edf5ae6f": "0x604a18a6451500b4000", - "69bc20be4b5102d5be43d8e4bbdb7af4c8e695d1": "0x44a54cb0c5b93604000", - "c7ad87cb5ad1f2a81a8b8f07665ae3bf294215f4": "0x2c64eb42d53386ac0000", - "de31c2e50a758758c80f0b98a9f77b8669e7b9ce": "0xf2820cf7b65e3fd20000", - "3aada1db9ccf3cc10fc0a7335d61c04de1f5c91d": "0x5b7ac4553de7ae00000", - "81875981cc9cb6d463fbacfb86a937b103f931db": "0x312e15ba86c3fa900000", - "bdae10e7ab4a244261f0950480aab6ddcb056d36": "0x21e19e0c9bab2400000", - "265b81d155673c607cf86c335ee5779eaee89ff4": "0x4350d1a2886bdd1c2800", - "137772feb56a8ec109f5df84d4c83813089dd4c7": "0x294af89f5db893e00000", - "25b273c51eed15b9220b9a8534f4d488ac9bc07b": "0x3fc3a04418eeefc8800", - "3e148a15ba1d00024e7d8afe1a3d6c76ee915102": "0x27658d7e1b508be8000", - "98bd4133ea1802570dfc2c94026458b13540b03c": "0x9cc143443cf215d0000", - "5028fd26863b8ce047ad68b1a319d79aed22542b": "0x5f4a8c8375d155400000", - "92a646cb3b9adf6213f4400a0363c53e134a2b19": "0x77f2633c31df89469c00", - "eed4eea1ba8c6af58c951503cc80e8784e954438": "0x4a89f54ef0121c00000", - "715e334aea5c5d2fe9ca29fe5bd504665bd1ad00": "0xbc2ca0ec28c8cd90800", - "4a89018c1f7616423102a06a9dc9563f1a0f9a3a": "0xa645f4fbc7885f334000", - "5b85be3181c4939eb88e64172718bb05f3bfa769": "0x7e4b5050cecbf0eb0000", - "9292fda6afce4aced543a36c183339861020eef4": "0x12a27d53bc0487000000", - "aadc41b226fb42bc023f58e8c1fd078218bbfa7c": "0x3635c9adc5dea000000", - "f46fe94d8e3416fe7c676f1b5a97bd250268cc31": "0x79f905c6fd34e800000", - "65e2bf99a4316ab8dd7e6cdb46a197d6bd1be9c4": "0x8eeaa1f9184a725", - "fd8fc69afeddfe22d0e9ec1264210a6f175f763e": "0x1ee0ae25820ad0e9e000", - "5b1e7e7bb9ceb53aa818dcc7231c8b38747dc363": "0x21e19e0c9bab2400000", - "f571cef1bacef1ab9c777127d127aba23347d835": "0x109c1afb2e50662600000", - "766fa3bc173c2fc97ff2eac96d41495844c8663b": "0x109cb2802727a475d0000", - "8639ff8def135ef6865d24897ab0dfbde9ea7bcb": "0x5511df47ae660abd400", - "4cfa7f816c7ac842dc7b88058be1e0a772d3b99f": "0xdf3cbaede6a2980000", - "28cc390acf5554945d03009c3e663f0e58723b47": "0x1bc6be42693cb2ce0000", - "253ef13cde4452013a4b42a687f8682b94e8d370": "0x3f8f2d0e833b404ac000", - "870cdf31020a7d10f604a41ba90b87a76f0a2903": "0x1fc3842bd1f071c00000", - "6e783c867979c0834cbf4db11bafe36af54970e2": "0x1d9dba997615c3a6c41", - "aefb910a6248edda84d366675b2a11b3522eba67": "0x3b037c245e52da144000", - "5fa82bb627a4839d71acde22e0ae30db275a4936": "0x3f8708907bcb2fe8000", - "7ae13fd868300f621417b7d1fc7c45dfb2716b5a": "0x7785f6a152dbdd90000", - "1751c9168db438ddee2e04993984d026ae9b0654": "0x38223b4007976fbc000", - "d039e17395ebddb25bad1c6021b9249fd7d0f7c5": "0x6621c09223e3f000000", - "3b064c8e235775ef026470b8b01a4a9bb509fc89": "0x17a4e6a7c1af9abd0000", - "ac2ba5b0a7c8d27f31984d7c890b48bda5b97da3": "0x666c8b7a26084ef0000", - "109323445bee0766f9be59d40b6e3f10c7154b63": "0x24451c7465c0443ac000", - "d2323f0d69f55bca4f5ba269dfdaccc903af5b0e": "0x335a6701afda95d0000", - "aa679c07b06e1f999c21aa30bf4fe1499e5e0b19": "0xc4b1040f03a3d270000", - "183db4d2229afd7d5bd44bd7a66779298e860125": "0x59b846ce5a9887e0000", - "597bf6623f8f3464a69e41f8ec153313db90564b": "0x17cc594e140c7c0000", - "e0585d813ff737c56a424857f26b166aa4ea1fe6": "0x18ab0b63442ea1938000", - "569be21572ce5a03ce3f34eb72456f585bdad3cf": "0x3fe99aa00425afe64000", - "e63f5df79667bfaa7fe002382299d1a4f0a94aee": "0x21e19e0c9bab2400000", - "7dd7cdd711093bd200ecbdf231d2e3ca1d2938a6": "0x3e73af618d44a83efc000", - "89b7ba8234089409d90baa3cf73b451ceca83c01": "0x1475ff027c31d4114000", - "676683ab532172a2f39db86cf1766ebaedae4f79": "0x228166d42d24ebde0000", - "d089bba6160a8158f59d5998aa2ded4e9dc0372a": "0x3f13682caebe704050000", - "094e53615c1d207b82778f51520d74da000233b3": "0x3da11f3f39111ee60000", - "d1d6a8ee705e5916c5c40ba545027b5a5bb373ca": "0x2ebf2541dd10f5658000", - "fb8964564eabcc2f8324fed20d84b2f3af12c81c": "0x3cfc82e37e9a7400000", - "2a998f05a3b93ac713d2e4f850938f5864f0da34": "0x2b7ca63b037c0e390000", - "49512f1d4a98a539aec2982e85e0ba0e59c62faf": "0x268ad525a9c834f1c000", - "40c41ef385336925e9b2cdcfca5200094d527976": "0x6985b5d4d9fa0b4c0000", - "d44a9da8c915806c532bcfda390bd1792ead6cf2": "0x2e5e662d03bb066ec000", - "24720d73a831ba602dbfb0f87e2a2319ea6ba6c9": "0x2641a0f1620cc04b400", - "2ed6c687ddcbad8b315826ac26b0cce110053e8d": "0x4612f8810b199e808000", - "8eda4525363cb2abcd23a8050888b9e3b85a2af8": "0x404ce9da96c7d53e0000", - "ce05466ffefd61b1b0935e7c0c320d6b74e8cec0": "0x69ef602fbc028be8c000", - "9901bcc483879d2a144f91e46bfc99cbbee3945a": "0x17c85d810b95f7490000", - "ed53c60377128d5c0d62dbd895e5941649d98504": "0x4138b6a5120fd8800000", - "e7a0e2fd3d4529575bc04a25f1a2a01bfaa6a30c": "0xf02d89f434782a00000", - "ab9f500bab73f62d68a9d63d9ce7a6afeabdda1f": "0x4e062335d83de3964000", - "86d70d96c11780501aa076e84bf41f82e480af1a": "0x425d738af0e9e038000", - "57cacc57d04e2751dfdf295e68866f08c92f7f98": "0xe4b2ead51ac33300000", - "ac957e26975e078c7154d75e3c52223e7d680a09": "0x69e10de76676d0800000", - "56dbf49a90fd51c7914a900e39fe537f5f962d16": "0x33292988cc56e700000", - "c98118cee2bb94f7041817366e858be47a10055b": "0xbebb0b7a7ee05ff00000", - "e8a87397b2ee50ad4bfeb86a827f098a3f964d09": "0x15e809261268d02800000", - "919499fba7011cbc0a6c229ea29d0ceb7d6f5d4a": "0x4074ea12da7ec138000", - "2b5410b12ddaf243872d3b81fd6e457fd1713c4a": "0x51896320dab07b68000", - "0855262f03a8b82cd8d1d5c2a26cb1d5bbcff8f2": "0x862b865ae353cc000000", - "452cb7c4fc01ad1f3b3cc410211ab7b66ee28baa": "0x4094a86f3108939b0000", - "b405d675ea7b88c882189c115ef45eca2f54b98a": "0xff50831bb3ceb420000", - "8654c09d4797dbef1fe1b3510ca98473b50eb813": "0x7db369a03e827825c000", - "5cbe4184e37b8f4c1a0a141d30a1d78ef6909fb7": "0xf663ba5c81ad4100000", - "251c938adb3255ffdfa0a517e9fc261467f136d2": "0x21e19e0c9bab2400000", - "0facffa8e48242d0e1a924705be45643eeab6f5d": "0xc4116431809ea2c6c00", - "cf91dbcde54685cbf68cac29670ab517f980cf69": "0x44942a20a22b2988a8000", - "e8a8b458bcd1ececc6b6b58f80929b29ccecff40": "0xb0adb0b723be136bd80", - "f60a39143f8ecb46a0b81b172b920f6b095a4034": "0x89833df171f49fef3c280", - "36f0756acc28cd97af7c51746a56f263a45113b8": "0xff98e6f370c455c0000", - "f0ed220aab0494a83206c3b63e2aedb66b100038": "0xf0a380052b86fc0000", - "42181e74e62a007a4377fec9e604a0e95d5e6b32": "0x6b6ccb9b2fffed1980000", - "1d9950c5092c2d88041be7e5c4fa12efc5b795cc": "0x20318447bf294959aac", - "d3efe493e12a79591ead7678202eeba0c7d90e67": "0x65e125bc3b65466e4000", - "a903715b57d3bf62e098a6a643c6924d9bdacec4": "0x170a0f5040e50400000", - "5bd47fef8e8dcb6677c2957ecd78b8232354f145": "0x191cf61eb2bec223400" + "ee1326fbf7d9322e5ea02c6fe5eb63535fceccd1": "0x52b7d2dcc80cd2e4000000" }, "hardforks": { "rewards_distribution_frequency": { From d086e395974322e014d10c6ff1f192028ae7611b Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Fri, 19 May 2023 10:08:02 +0200 Subject: [PATCH 021/134] chore: update evm --- submodules/taraxa-evm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index e2d36e25c2..1aa1ced310 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit e2d36e25c28d7655f09f3c356ec8d1b60bd7cf29 +Subproject commit 1aa1ced3109110d000d3210b362bb5c298c6624c From b92ba1f8984b0dca7c9303e0db3cc676a61d5945 Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Tue, 23 May 2023 12:31:23 +0200 Subject: [PATCH 022/134] feat: accept decimal and hex as int value inputs --- libraries/aleth/libdevcore/CommonJS.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/libraries/aleth/libdevcore/CommonJS.h b/libraries/aleth/libdevcore/CommonJS.h index aff4792886..d0c17b8595 100644 --- a/libraries/aleth/libdevcore/CommonJS.h +++ b/libraries/aleth/libdevcore/CommonJS.h @@ -90,9 +90,15 @@ jsToInt(std::string const& _s) { inline u256 jsToU256(std::string const& _s) { return jsToInt<32>(_s); } -inline uint64_t jsToInt(std::string const& _s, int base = 16) { return std::stoull(_s, 0, base); } - -inline std::string jsToDecimal(std::string const& _s) { return toString(jsToU256(_s)); } +inline uint64_t jsToInt(std::string const& _s) { + if (_s.substr(0, 2) == "0x") { + return std::stoull(_s, 0, 16); // Hex + } else if (_s.find_first_not_of("0123456789") == std::string::npos) { + return std::stoull(_s, 0, 10); // Decimal + } + // Still try to parse it as hex. Will throw the std::invalid_argument if it isn't possible + return std::stoull(_s, 0, 16); +} inline uint64_t getUInt(const Json::Value& v) { if (v.isString()) { From 484b6530c9d53a632b599031582032d2b1291063 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Tue, 16 May 2023 12:48:59 +0200 Subject: [PATCH 023/134] chore: syncing optimizations --- .../consensus/src/pbft/pbft_manager.cpp | 2 +- .../network/include/network/network.hpp | 2 +- .../common/ext_syncing_packet_handler.hpp | 8 +--- .../shared_states/pbft_syncing_state.hpp | 26 ++++++------ libraries/core_libs/network/src/network.cpp | 7 ++-- .../common/ext_syncing_packet_handler.cpp | 41 ++++++++----------- .../dag_sync_packet_handler.cpp | 2 +- .../pbft_sync_packet_handler.cpp | 27 ++++++------ .../status_packet_handler.cpp | 4 +- .../shared_states/pbft_syncing_state.cpp | 31 ++++++++------ .../network/src/tarcap/taraxa_capability.cpp | 4 +- 11 files changed, 72 insertions(+), 82 deletions(-) diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index e02c55c8fd..16c8366109 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -304,7 +304,7 @@ bool PbftManager::tryPushCertVotesBlock() { auto net = network_.lock(); // If block/reward votes are missing but block is cert voted other nodes probably advanced, sync if (net) { - net->restartSyncingPbft(); + net->startSyncingPbft(); } return false; } diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index 860cb48622..c0779078c5 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -50,7 +50,7 @@ class Network { // returns count of all discovered nodes unsigned getNodeCount(); Json::Value getStatus(); - void restartSyncingPbft(bool force = false); + void startSyncingPbft(); bool pbft_syncing(); uint64_t syncTimeSeconds() const; void setSyncStatePeriod(PbftPeriod period); diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp index 88360167dd..3b7a30411e 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp @@ -28,18 +28,14 @@ class ExtSyncingPacketHandler : public PacketHandler { std::shared_ptr db, const addr_t &node_addr, const std::string &log_channel_name); virtual ~ExtSyncingPacketHandler() = default; - ExtSyncingPacketHandler(const ExtSyncingPacketHandler &) = default; - ExtSyncingPacketHandler(ExtSyncingPacketHandler &&) = default; ExtSyncingPacketHandler &operator=(const ExtSyncingPacketHandler &) = delete; ExtSyncingPacketHandler &operator=(ExtSyncingPacketHandler &&) = delete; /** - * @brief Restart syncing + * @brief Start syncing pbft if needed * - * @param shared_state - * @param force */ - void restartSyncingPbft(bool force = false); + void startSyncingPbft(); /** * @brief Send sync request to the current syncing peer with specified request_period diff --git a/libraries/core_libs/network/include/network/tarcap/shared_states/pbft_syncing_state.hpp b/libraries/core_libs/network/include/network/tarcap/shared_states/pbft_syncing_state.hpp index d5534911f4..0c6145a77d 100644 --- a/libraries/core_libs/network/include/network/tarcap/shared_states/pbft_syncing_state.hpp +++ b/libraries/core_libs/network/include/network/tarcap/shared_states/pbft_syncing_state.hpp @@ -24,25 +24,13 @@ class PbftSyncingState { * @param current_period * @param peer used in case syncing flag == true to set which peer is the node syncing with */ - void setPbftSyncing(bool syncing, PbftPeriod current_period = 0, std::shared_ptr peer = nullptr); - - /** - * @brief Set current time as last received sync packet time - */ - void setLastSyncPacketTime(); + bool setPbftSyncing(bool syncing, PbftPeriod current_period = 0, std::shared_ptr peer = nullptr); /** * @brief Set current pbft period */ void setSyncStatePeriod(PbftPeriod period); - /** - * @brief Check if syncing is active - * - * @return true if last syncing packet was received within kSyncingInactivityThreshold - */ - bool isActivelySyncing() const; - /** * @brief Check if PBFT is in deep syncing * @@ -64,6 +52,18 @@ class PbftSyncingState { */ std::shared_ptr syncingPeer() const; + /** + * @brief Set current time as last received sync packet time + */ + void setLastSyncPacketTime(); + + /** + * @brief Check if syncing is active + * + * @return true if last syncing packet was received within kSyncingInactivityThreshold + */ + bool isActivelySyncing() const; + private: std::atomic deep_pbft_syncing_{false}; std::atomic pbft_syncing_{false}; diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index 0cf9842d95..ba34082384 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -83,10 +83,9 @@ unsigned Network::getNodeCount() { return host_->getNodeCount(); } Json::Value Network::getStatus() { return taraxa_capability_->getNodeStats()->getStatus(); } -void Network::restartSyncingPbft(bool force) { - tp_.post([this, force] { - taraxa_capability_->getSpecificHandler()->restartSyncingPbft(force); - }); +void Network::startSyncingPbft() { + tp_.post( + [this] { taraxa_capability_->getSpecificHandler()->startSyncingPbft(); }); } bool Network::pbft_syncing() { return taraxa_capability_->pbft_syncing(); } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_syncing_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_syncing_packet_handler.cpp index 23f3b11158..8032dc8d9d 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_syncing_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_syncing_packet_handler.cpp @@ -21,53 +21,44 @@ ExtSyncingPacketHandler::ExtSyncingPacketHandler(const FullNodeConfig &conf, std dag_mgr_(std::move(dag_mgr)), db_(std::move(db)) {} -void ExtSyncingPacketHandler::restartSyncingPbft(bool force) { - if (pbft_syncing_state_->isPbftSyncing() && !force) { - LOG(log_dg_) << "restartSyncingPbft called but syncing_ already true"; +void ExtSyncingPacketHandler::startSyncingPbft() { + if (pbft_syncing_state_->isPbftSyncing()) { + LOG(log_dg_) << "startSyncingPbft called but syncing_ already true"; return; } std::shared_ptr peer = getMaxChainPeer(); - if (!peer) { - pbft_syncing_state_->setPbftSyncing(false); LOG(log_nf_) << "Restarting syncing PBFT not possible since no connected peers"; return; } auto pbft_sync_period = pbft_mgr_->pbftSyncingPeriod(); if (peer->pbft_chain_size_ > pbft_sync_period) { - LOG(log_si_) << "Restarting syncing PBFT from peer " << peer->getId() << ", peer PBFT chain size " - << peer->pbft_chain_size_ << ", own PBFT chain synced at period " << pbft_sync_period; - - const auto node_id = peer->getId(); - - pbft_syncing_state_->setPbftSyncing(true, pbft_sync_period, std::move(peer)); - - // Handle case where syncing peer just disconnected - if (!syncPeerPbft(pbft_sync_period + 1)) { - // Only restart syncing if peer is actually disconnected and removed from peers_state, otherwise there is a risk - // of endless recursion - if (peers_state_->getPeer(node_id) == nullptr) { - return restartSyncingPbft(true); - } - pbft_syncing_state_->setPbftSyncing(false); + auto peer_id = peer->getId().abridged(); + auto peer_pbft_chain_size = peer->pbft_chain_size_.load(); + if (!pbft_syncing_state_->setPbftSyncing(true, pbft_sync_period, std::move(peer))) { + LOG(log_dg_) << "startSyncingPbft called but syncing_ already true"; return; } + LOG(log_si_) << "Restarting syncing PBFT from peer " << peer_id << ", peer PBFT chain size " << peer_pbft_chain_size + << ", own PBFT chain synced at period " << pbft_sync_period; - // Disable snapshots only if are syncing from scratch - if (pbft_syncing_state_->isDeepPbftSyncing()) { - db_->disableSnapshots(); + if (syncPeerPbft(pbft_sync_period + 1)) { + // Disable snapshots only if are syncing from scratch + if (pbft_syncing_state_->isDeepPbftSyncing()) { + db_->disableSnapshots(); + } + } else { + pbft_syncing_state_->setPbftSyncing(false); } } else { LOG(log_nf_) << "Restarting syncing PBFT not needed since our pbft chain size: " << pbft_sync_period << "(" << pbft_chain_->getPbftChainSize() << ")" << " is greater or equal than max node pbft chain size:" << peer->pbft_chain_size_; - pbft_syncing_state_->setPbftSyncing(false); db_->enableSnapshots(); } } - bool ExtSyncingPacketHandler::syncPeerPbft(PbftPeriod request_period, bool ignore_chain_size_check) { const auto syncing_peer = pbft_syncing_state_->syncingPeer(); if (!syncing_peer) { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/dag_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/dag_sync_packet_handler.cpp index 7d1b04acc7..fdef88832d 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/dag_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/dag_sync_packet_handler.cpp @@ -40,7 +40,7 @@ void DagSyncPacketHandler::process(const PacketData& packet_data, const std::sha } peer->peer_dag_syncing_ = false; // We might be behind, restart pbft sync if needed - restartSyncingPbft(); + startSyncingPbft(); return; } else if (response_period < request_period) { // This should not be possible for honest node diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp index ed574aa4fe..551347bd34 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp @@ -99,9 +99,8 @@ void PbftSyncPacketHandler::process(const PacketData &packet_data, const std::sh << packet_data.from_node_id_ << " already present in chain"; } else { if (pbft_block_period != pbft_mgr_->pbftSyncingPeriod() + 1) { - LOG(log_wr_) << "Block " << pbft_blk_hash << " period unexpected: " << pbft_block_period + LOG(log_er_) << "Block " << pbft_blk_hash << " period unexpected: " << pbft_block_period << ". Expected period: " << pbft_mgr_->pbftSyncingPeriod() + 1; - restartSyncingPbft(true); return; } @@ -171,10 +170,10 @@ void PbftSyncPacketHandler::process(const PacketData &packet_data, const std::sh if (auto votes = vote_mgr_->checkRewardVotes(period_data.pbft_blk, true); votes.first) { period_data.previous_block_cert_votes = std::move(votes.second); } else { - // checkRewardVotes could fail because we just cert voted this block and moved to next period, in that case we - // might even be fully synced so call restartSyncingPbft to verify + // checkRewardVotes could fail because we just cert voted this block and moved to next period, + // in that case we are probably fully synced if (pbft_block_period <= vote_mgr_->getRewardVotesPbftBlockPeriod()) { - restartSyncingPbft(true); + pbft_syncing_state_->setPbftSyncing(false); return; } @@ -203,10 +202,10 @@ void PbftSyncPacketHandler::process(const PacketData &packet_data, const std::sh } if (last_block) { - // If current sync period is actually bigger than the block we just received we are probably synced but verify with - // calling restartSyncingPbft + // If current sync period is actually bigger than the block we just received we are probably synced if (pbft_sync_period > pbft_block_period) { - return restartSyncingPbft(true); + pbft_syncing_state_->setPbftSyncing(false); + return; } if (pbft_syncing_state_->isPbftSyncing()) { if (pbft_sync_period > pbft_chain_->getPbftChainSize() + (10 * kConf.network.sync_level_size)) { @@ -216,7 +215,8 @@ void PbftSyncPacketHandler::process(const PacketData &packet_data, const std::sh periodic_events_tp->post(1000, [this] { delayedPbftSync(1); }); } else { if (!syncPeerPbft(pbft_sync_period + 1, true)) { - return restartSyncingPbft(true); + pbft_syncing_state_->setPbftSyncing(false); + return; } } } @@ -232,10 +232,11 @@ void PbftSyncPacketHandler::pbftSyncComplete() { } else { LOG(log_dg_) << "Syncing PBFT is completed"; // We are pbft synced with the node we are connected to but - // calling restartSyncingPbft will check if some nodes have + // calling startSyncingPbft will check if some nodes have // greater pbft chain size and we should continue syncing with // them, Or sync pending DAG blocks - restartSyncingPbft(true); + pbft_syncing_state_->setPbftSyncing(false); + startSyncingPbft(); if (!pbft_syncing_state_->isPbftSyncing()) { requestPendingDagBlocks(); } @@ -260,7 +261,7 @@ void PbftSyncPacketHandler::delayedPbftSync(int counter) { periodic_events_tp->post(1000, [this, counter] { delayedPbftSync(counter + 1); }); } else { if (!syncPeerPbft(pbft_sync_period + 1)) { - return restartSyncingPbft(true); + pbft_syncing_state_->setPbftSyncing(false); } } } @@ -275,8 +276,6 @@ void PbftSyncPacketHandler::handleMaliciousSyncPeer(dev::p2p::NodeID const &id) } else { LOG(log_er_) << "Unable to handleMaliciousSyncPeer, host == nullptr"; } - - restartSyncingPbft(true); } } // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/status_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/status_packet_handler.cpp index 3d95837a6f..c31c20e6f8 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/status_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/status_packet_handler.cpp @@ -127,12 +127,12 @@ void StatusPacketHandler::process(const PacketData& packet_data, const std::shar LOG(log_nf_) << "Restart PBFT chain syncing. Own synced PBFT at period " << pbft_synced_period << ", peer PBFT chain size " << selected_peer->pbft_chain_size_; if (pbft_synced_period + 1 < selected_peer->pbft_chain_size_) { - restartSyncingPbft(); + startSyncingPbft(); } else { // If we are behind by only one block wait for two status messages before syncing because nodes are not always // in perfect sync if (selected_peer->last_status_pbft_chain_size_ == selected_peer->pbft_chain_size_) { - restartSyncingPbft(); + startSyncingPbft(); } } } else if (pbft_synced_period == selected_peer->pbft_chain_size_ && !selected_peer->peer_dag_synced_) { diff --git a/libraries/core_libs/network/src/tarcap/shared_states/pbft_syncing_state.cpp b/libraries/core_libs/network/src/tarcap/shared_states/pbft_syncing_state.cpp index bc8d28b9df..88b185fcf7 100644 --- a/libraries/core_libs/network/src/tarcap/shared_states/pbft_syncing_state.cpp +++ b/libraries/core_libs/network/src/tarcap/shared_states/pbft_syncing_state.cpp @@ -21,20 +21,27 @@ void PbftSyncingState::setSyncStatePeriod(PbftPeriod period) { } } -void PbftSyncingState::setPbftSyncing(bool syncing, PbftPeriod current_period, +bool PbftSyncingState::setPbftSyncing(bool syncing, PbftPeriod current_period, std::shared_ptr peer /*=nullptr*/) { assert((syncing && peer) || !syncing); - pbft_syncing_ = syncing; - std::unique_lock lock(peer_mutex_); - peer_ = std::move(peer); + // Flag was changed meanwhile we should not be updating it again + if (pbft_syncing_ && syncing) { + return false; + } + { + std::unique_lock lock(peer_mutex_); + pbft_syncing_ = syncing; + peer_ = std::move(peer); - if (syncing) { - deep_pbft_syncing_ = (peer_->pbft_chain_size_ - current_period >= kDeepSyncingThreshold); - // Reset last sync packet time when syncing is restarted/fresh syncing flag is set - setLastSyncPacketTime(); - } else { - deep_pbft_syncing_ = false; + if (syncing) { + deep_pbft_syncing_ = (peer_->pbft_chain_size_ - current_period >= kDeepSyncingThreshold); + // Reset last sync packet time when syncing is restarted/fresh syncing flag is set + setLastSyncPacketTime(); + } else { + deep_pbft_syncing_ = false; + } + return true; } } @@ -44,9 +51,8 @@ void PbftSyncingState::setLastSyncPacketTime() { } bool PbftSyncingState::isActivelySyncing() const { - auto now = std::chrono::steady_clock::now(); - std::shared_lock lock(time_mutex_); + auto now = std::chrono::steady_clock::now(); return std::chrono::duration_cast(now - last_received_sync_packet_time_) <= kSyncingInactivityThreshold; } @@ -57,7 +63,6 @@ bool PbftSyncingState::isPbftSyncing() { if (!isActivelySyncing()) { setPbftSyncing(false); } - return pbft_syncing_; } diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index e7d69fb153..15c361a31e 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -260,12 +260,12 @@ void TaraxaCapability::onDisconnect(dev::p2p::NodeID const &_nodeID) { const auto syncing_peer = pbft_syncing_state_->syncingPeer(); if (pbft_syncing_state_->isPbftSyncing() && syncing_peer && syncing_peer->getId() == _nodeID) { + pbft_syncing_state_->setPbftSyncing(false); if (peers_state_->getPeersCount() > 0) { LOG(log_dg_) << "Restart PBFT/DAG syncing due to syncing peer disconnect."; - packets_handlers_->getSpecificHandler()->restartSyncingPbft(true); + packets_handlers_->getSpecificHandler()->startSyncingPbft(); } else { LOG(log_dg_) << "Stop PBFT/DAG syncing due to syncing peer disconnect and no other peers available."; - pbft_syncing_state_->setPbftSyncing(false); } } } From cda0e16e8c0d57e3e29aac26320589732d378a7f Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Fri, 14 Apr 2023 11:05:38 -0700 Subject: [PATCH 024/134] chore: add v1 and v2 taraxa capabilities --- CMakeLists.txt | 2 +- .../include/vote_manager/vote_manager.hpp | 9 - .../consensus/src/pbft/pbft_manager.cpp | 7 +- .../src/vote_manager/vote_manager.cpp | 14 - .../include/network/tarcap/packet_types.hpp | 6 +- .../common/ext_votes_packet_handler.hpp | 5 +- .../votes_sync_packet_handler.hpp | 2 +- .../network/tarcap/taraxa_capability.hpp | 7 +- .../common/ext_votes_packet_handler.hpp | 29 ++ .../get_next_votes_sync_packet_handler.hpp | 27 ++ .../get_pbft_sync_packet_handler.hpp | 41 +++ .../pbft_sync_packet_handler.hpp | 38 +++ .../votes_sync_packet_handler.hpp | 32 ++ .../network/v1_tarcap/taraxa_capability.hpp | 56 ++++ libraries/core_libs/network/src/network.cpp | 21 +- .../common/ext_votes_packet_handler.cpp | 39 ++- .../get_next_votes_sync_packet_handler.cpp | 34 +-- .../pbft_sync_packet_handler.cpp | 1 - .../votes_sync_packet_handler.cpp | 84 +++--- .../network/src/tarcap/taraxa_capability.cpp | 8 +- .../common/ext_votes_packet_handler.cpp | 47 +++ .../get_next_votes_sync_packet_handler.cpp | 80 +++++ .../get_pbft_sync_packet_handler.cpp | 104 +++++++ .../pbft_sync_packet_handler.cpp | 280 ++++++++++++++++++ .../votes_sync_packet_handler.cpp | 161 ++++++++++ .../src/v1_tarcap/taraxa_capability.cpp | 66 +++++ libraries/types/vote/CMakeLists.txt | 4 +- libraries/types/vote/include/vote/vote.hpp | 14 - .../types/vote/include/vote/votes_bundle.hpp | 42 +++ libraries/types/vote/src/votes_bundle.cpp | 52 ++++ tests/network_test.cpp | 2 +- tests/tarcap_threadpool_test.cpp | 6 +- 32 files changed, 1185 insertions(+), 135 deletions(-) create mode 100644 libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp create mode 100644 libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp create mode 100644 libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp create mode 100644 libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp create mode 100644 libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_sync_packet_handler.hpp create mode 100644 libraries/core_libs/network/include/network/v1_tarcap/taraxa_capability.hpp create mode 100644 libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp create mode 100644 libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_next_votes_sync_packet_handler.cpp create mode 100644 libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp create mode 100644 libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp create mode 100644 libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_sync_packet_handler.cpp create mode 100644 libraries/core_libs/network/src/v1_tarcap/taraxa_capability.cpp create mode 100644 libraries/types/vote/include/vote/votes_bundle.hpp create mode 100644 libraries/types/vote/src/votes_bundle.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ff575d0dd..385620d726 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ set(TARAXA_PATCH_VERSION 1) set(TARAXA_VERSION ${TARAXA_MAJOR_VERSION}.${TARAXA_MINOR_VERSION}.${TARAXA_PATCH_VERSION}) # Any time a change in the network protocol is introduced this version should be increased -set(TARAXA_NET_VERSION 1) +set(TARAXA_NET_VERSION 2) # Major version is modified when DAG blocks, pbft blocks and any basic building blocks of our blockchain is modified # in the db set(TARAXA_DB_MAJOR_VERSION 1) diff --git a/libraries/core_libs/consensus/include/vote_manager/vote_manager.hpp b/libraries/core_libs/consensus/include/vote_manager/vote_manager.hpp index b387810031..566452f511 100644 --- a/libraries/core_libs/consensus/include/vote_manager/vote_manager.hpp +++ b/libraries/core_libs/consensus/include/vote_manager/vote_manager.hpp @@ -224,15 +224,6 @@ class VoteManager { std::vector> getTwoTPlusOneVotedBlockVotes(PbftPeriod period, PbftRound round, TwoTPlusOneVotedBlockType type) const; - /** - * Get all 2t+1 voted block next votes(both for null block as well as specific block) for specific period and round - * - * @param period - * @param round - * @return vector of next votes if 2t+1 voted block votes found, otherwise empty vector - */ - std::vector> getAllTwoTPlusOneNextVotes(PbftPeriod period, PbftRound round) const; - /** * @brief Sets current pbft period & round. It also checks if we dont alredy have 2t+1 vote bundles(pf any type) for * the provided period & round and if so, it saves these bundles into db diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index 16c8366109..0402387eb6 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -590,7 +590,12 @@ void PbftManager::broadcastVotes() { // Broadcast previous round 2t+1 next votes if (round > 1) { - gossipVotes(vote_mgr_->getAllTwoTPlusOneNextVotes(period, round - 1), "2t+1 next votes", rebroadcast); + gossipVotes( + vote_mgr_->getTwoTPlusOneVotedBlockVotes(period, round - 1, TwoTPlusOneVotedBlockType::NextVotedBlock), + "2t+1 next votes", rebroadcast); + gossipVotes( + vote_mgr_->getTwoTPlusOneVotedBlockVotes(period, round - 1, TwoTPlusOneVotedBlockType::NextVotedNullBlock), + "2t+1 next null votes", rebroadcast); } // Broadcast own votes diff --git a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp index 8c37742755..7084be773a 100644 --- a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp +++ b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp @@ -1047,18 +1047,4 @@ std::vector> VoteManager::getTwoTPlusOneVotedBlockVotes(Pb return votes; } -std::vector> VoteManager::getAllTwoTPlusOneNextVotes(PbftPeriod period, PbftRound round) const { - auto next_votes = getTwoTPlusOneVotedBlockVotes(period, round, TwoTPlusOneVotedBlockType::NextVotedBlock); - - auto null_block_next_vote = - getTwoTPlusOneVotedBlockVotes(period, round, TwoTPlusOneVotedBlockType::NextVotedNullBlock); - if (!null_block_next_vote.empty()) { - next_votes.reserve(next_votes.size() + null_block_next_vote.size()); - next_votes.insert(next_votes.end(), std::make_move_iterator(null_block_next_vote.begin()), - std::make_move_iterator(null_block_next_vote.end())); - } - - return next_votes; -} - } // namespace taraxa diff --git a/libraries/core_libs/network/include/network/tarcap/packet_types.hpp b/libraries/core_libs/network/include/network/tarcap/packet_types.hpp index 8f5ee8f4c0..2ec1ee2523 100644 --- a/libraries/core_libs/network/include/network/tarcap/packet_types.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packet_types.hpp @@ -14,7 +14,7 @@ enum SubprotocolPacketType : uint32_t { HighPriorityPackets = 0, VotePacket, // Vote packer can contain (optional) also pbft block GetNextVotesSyncPacket, - VotesSyncPacket, + VotesBundlePacket, // Standard packets with mid processing priority MidPriorityPackets, @@ -53,8 +53,8 @@ inline std::string convertPacketTypeToString(SubprotocolPacketType packet_type) return "VotePacket"; case GetNextVotesSyncPacket: return "GetNextVotesSyncPacket"; - case VotesSyncPacket: - return "VotesSyncPacket"; + case VotesBundlePacket: + return "VotesBundlePacket"; case GetPbftSyncPacket: return "GetPbftSyncPacket"; case PbftSyncPacket: diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp index 0ba977ef2c..07436afc75 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp @@ -48,7 +48,7 @@ class ExtVotesPacketHandler : public PacketHandler { */ bool isPbftRelevantVote(const std::shared_ptr& vote) const; - void sendPbftVotesBundle(const std::shared_ptr& peer, std::vector>&& votes); + virtual void sendPbftVotesBundle(const std::shared_ptr& peer, std::vector>&& votes); private: /** @@ -73,7 +73,8 @@ class ExtVotesPacketHandler : public PacketHandler { bool validateVoteAndBlock(const std::shared_ptr& vote, const std::shared_ptr& pbft_block) const; protected: - constexpr static size_t kMaxVotesInPacket{1000}; + constexpr static size_t kVotesBundlePacketSize{5}; + constexpr static size_t kMaxVotesInBundle{1000}; constexpr static std::chrono::seconds kSyncRequestInterval = std::chrono::seconds(10); mutable std::chrono::system_clock::time_point last_votes_sync_request_time_; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_sync_packet_handler.hpp index 2afc07cf7d..6cb267199f 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_sync_packet_handler.hpp @@ -22,7 +22,7 @@ class VotesSyncPacketHandler final : public ExtVotesPacketHandler { const std::optional& exclude_node = {}); // Packet type that is processed by this handler - static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::VotesSyncPacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::VotesBundlePacket; private: void validatePacketRlpFormat(const PacketData& packet_data) const override; diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp index b651d85b5c..3df6275cc8 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp @@ -35,7 +35,7 @@ class TaraxaPeer; class TaraxaCapability : public dev::p2p::CapabilityFace { public: TaraxaCapability(std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, - unsigned version); + unsigned version, const std::string &log_channel); virtual ~TaraxaCapability() = default; TaraxaCapability(const TaraxaCapability &ro) = delete; @@ -46,7 +46,8 @@ class TaraxaCapability : public dev::p2p::CapabilityFace { std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, const h256 &genesis_hash, unsigned version, std::shared_ptr db = {}, std::shared_ptr pbft_mgr = {}, std::shared_ptr pbft_chain = {}, std::shared_ptr vote_mgr = {}, - std::shared_ptr dag_mgr = {}, std::shared_ptr trx_mgr = {}); + std::shared_ptr dag_mgr = {}, std::shared_ptr trx_mgr = {}, + const std::string &log_channel = "TARCAP"); // Init capability. Register packet handlers and periodic events virtual void init(const h256 &genesis_hash, std::shared_ptr db, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, @@ -110,7 +111,7 @@ class TaraxaCapability : public dev::p2p::CapabilityFace { // Test state std::shared_ptr test_state_; - private: + protected: // Capability version unsigned version_; diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp new file mode 100644 index 0000000000..2b9329f076 --- /dev/null +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp" + +namespace taraxa::network::v1_tarcap { + +/** + * @brief ExtVotesPacketHandler is extended abstract PacketHandler with added functions that are used in packet + * handlers that process pbft votes + */ +class ExtVotesPacketHandler : public tarcap::ExtVotesPacketHandler { + public: + ExtVotesPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, const addr_t& node_addr, + const std::string& log_channel_name); + + virtual ~ExtVotesPacketHandler() = default; + ExtVotesPacketHandler(const ExtVotesPacketHandler&) = delete; + ExtVotesPacketHandler(ExtVotesPacketHandler&&) = delete; + ExtVotesPacketHandler& operator=(const ExtVotesPacketHandler&) = delete; + ExtVotesPacketHandler& operator=(ExtVotesPacketHandler&&) = delete; + + void sendPbftVotesBundle(const std::shared_ptr& peer, + std::vector>&& votes) override; +}; + +} // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp new file mode 100644 index 0000000000..47f0d569fc --- /dev/null +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp" + +namespace taraxa { +class PbftManager; +class VoteManager; +} // namespace taraxa + +namespace taraxa::network::v1_tarcap { + +class GetNextVotesSyncPacketHandler final : public ExtVotesPacketHandler { + public: + GetNextVotesSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, const addr_t& node_addr); + + // Packet type that is processed by this handler + static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::GetNextVotesSyncPacket; + + private: + void validatePacketRlpFormat(const tarcap::PacketData& packet_data) const override; + void process(const tarcap::PacketData& packet_data, const std::shared_ptr& peer) override; +}; + +} // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp new file mode 100644 index 0000000000..09d86ea250 --- /dev/null +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include "network/tarcap/packets_handlers/common/packet_handler.hpp" + +namespace taraxa { +class PbftChain; +class DbStorage; +class VoteManager; +} // namespace taraxa + +namespace taraxa::network::tarcap { +class PbftSyncingState; +} + +namespace taraxa::network::v1_tarcap { + +class OmgGetPbftSyncPacketHandler final : public tarcap::PacketHandler { + public: + OmgGetPbftSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_syncing_state, + std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, + std::shared_ptr db, const addr_t& node_addr); + + void sendPbftBlocks(dev::p2p::NodeID const& peer_id, PbftPeriod from_period, size_t blocks_to_transfer, + bool pbft_chain_synced); + + // Packet type that is processed by this handler + static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::GetPbftSyncPacket; + + private: + void validatePacketRlpFormat(const tarcap::PacketData& packet_data) const override; + void process(const tarcap::PacketData& packet_data, const std::shared_ptr& peer) override; + + std::shared_ptr pbft_syncing_state_; + std::shared_ptr pbft_chain_; + std::shared_ptr vote_mgr_; + std::shared_ptr db_; +}; + +} // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp new file mode 100644 index 0000000000..1115c151a2 --- /dev/null +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include "network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp" +#include "vote_manager/vote_manager.hpp" + +namespace taraxa::network::v1_tarcap { + +class PbftSyncPacketHandler final : public tarcap::ExtSyncingPacketHandler { + public: + PbftSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_syncing_state, + std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, + std::shared_ptr dag_mgr, std::shared_ptr vote_mgr, + std::shared_ptr periodic_events_tp, std::shared_ptr db, + const addr_t& node_addr); + + void handleMaliciousSyncPeer(dev::p2p::NodeID const& id); + + // Packet type that is processed by this handler + static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::PbftSyncPacket; + + private: + void validatePacketRlpFormat(const tarcap::PacketData& packet_data) const override; + void process(const tarcap::PacketData& packet_data, const std::shared_ptr& peer) override; + + void pbftSyncComplete(); + void delayedPbftSync(int counter); + + std::shared_ptr vote_mgr_; + + std::weak_ptr periodic_events_tp_; + + static constexpr size_t kStandardPacketSize = 2; + static constexpr size_t kChainSyncedPacketSize = 3; +}; + +} // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_sync_packet_handler.hpp new file mode 100644 index 0000000000..a8f233b309 --- /dev/null +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_sync_packet_handler.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include "network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp" + +namespace taraxa::network::v1_tarcap { + +class VotesSyncPacketHandler final : public ExtVotesPacketHandler { + public: + VotesSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, const addr_t& node_addr); + + /** + * @brief Sends pbft votes bundle to connected peers + * + * @param votes Votes to send + * @param rebroadcast if rebroadcast is true, all votes are resent to all peers + * @param exclude_node do not send votes to excluded node + */ + void onNewPbftVotesBundle(std::vector>&& votes, bool rebroadcast = false, + const std::optional& exclude_node = {}); + + // Packet type that is processed by this handler + static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::VotesBundlePacket; + + private: + void validatePacketRlpFormat(const tarcap::PacketData& packet_data) const override; + void process(const tarcap::PacketData& packet_data, const std::shared_ptr& peer) override; +}; + +} // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/include/network/v1_tarcap/taraxa_capability.hpp b/libraries/core_libs/network/include/network/v1_tarcap/taraxa_capability.hpp new file mode 100644 index 0000000000..a7dcb098e7 --- /dev/null +++ b/libraries/core_libs/network/include/network/v1_tarcap/taraxa_capability.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include "network/tarcap/taraxa_capability.hpp" + +// namespace taraxa { +// class DbStorage; +// class PbftManager; +// class PbftChain; +// class VoteManager; +// class DagManager; +// class TransactionManager; +// enum class TransactionStatus; +// } // namespace taraxa + +namespace taraxa::network::v1_tarcap { + +// class PacketsHandler; +// class PbftSyncingState; +// class TaraxaPeer; + +class TaraxaCapability : public taraxa::network::tarcap::TaraxaCapability { + public: + TaraxaCapability(std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, + unsigned version, const std::string &log_channel); + + virtual ~TaraxaCapability() = default; + TaraxaCapability(const TaraxaCapability &ro) = delete; + TaraxaCapability &operator=(const TaraxaCapability &ro) = delete; + TaraxaCapability(TaraxaCapability &&ro) = delete; + TaraxaCapability &operator=(TaraxaCapability &&ro) = delete; + // TODO: do we need this ??? + // static std::shared_ptr make( + // std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, const h256 + // &genesis_hash, unsigned version, std::shared_ptr db = {}, std::shared_ptr pbft_mgr = + // {}, std::shared_ptr pbft_chain = {}, std::shared_ptr vote_mgr = {}, + // std::shared_ptr dag_mgr = {}, std::shared_ptr trx_mgr = {}); + // + // // Init capability. Register packet handlers and periodic events + // virtual void init(const h256 &genesis_hash, std::shared_ptr db, std::shared_ptr pbft_mgr, + // std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, + // std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, + // const dev::Address &node_addr); + + protected: + virtual void registerPacketHandlers( + const h256 &genesis_hash, const std::shared_ptr &packets_stats, + const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, + const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, + const std::shared_ptr &dag_mgr, const std::shared_ptr &trx_mgr, + const addr_t &node_addr) override; + + private: + LOG_OBJECTS_DEFINE +}; + +} // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index ba34082384..8833ae1ebe 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -8,6 +8,7 @@ #include "config/version.hpp" #include "network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/v1_tarcap/taraxa_capability.hpp" namespace taraxa { @@ -45,11 +46,24 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, construct_capabilities = [&](std::weak_ptr host) { assert(!host.expired()); - auto taraxa_capability = network::tarcap::TaraxaCapability::make( - host, key, config, genesis_hash, TARAXA_NET_VERSION, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr); - return dev::p2p::Host::CapabilityList{taraxa_capability}; + const size_t kOldNetworkVersion = 1; + assert(kOldNetworkVersion < TARAXA_NET_VERSION); + + dev::p2p::Host::CapabilityList capabilities; + + // Register old version of taraxa capability + capabilities.emplace_back(network::v1_tarcap::TaraxaCapability::make(host, key, config, genesis_hash, + kOldNetworkVersion, db, pbft_mgr, pbft_chain, + vote_mgr, dag_mgr, trx_mgr, "V1_TARCAP")); + // Register new version of taraxa capability + capabilities.emplace_back(network::tarcap::TaraxaCapability::make(host, key, config, genesis_hash, + TARAXA_NET_VERSION, db, pbft_mgr, pbft_chain, + vote_mgr, dag_mgr, trx_mgr, "TARCAP")); + + return capabilities; }; } + host_ = dev::p2p::Host::make(net_version, construct_capabilities, key, net_conf, taraxa_net_conf, network_file_path); taraxa_capability_ = std::static_pointer_cast(host_->latestCapability()); for (uint i = 0; i < tp_.capacity(); ++i) { @@ -58,6 +72,7 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, ; }); } + LOG(log_nf_) << "Configured host. Listening on address: " << config.network.listen_ip << ":" << config.network.listen_port; } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp index d8862d06df..bd691f3ffb 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp @@ -176,24 +176,47 @@ void ExtVotesPacketHandler::sendPbftVotesBundle(const std::shared_ptrgetBlockHash(); + const auto reference_period = votes.back()->getPeriod(); + const auto reference_round = votes.back()->getRound(); + const auto reference_step = votes.back()->getStep(); + size_t index = 0; while (index < votes.size()) { - const size_t count = std::min(static_cast(kMaxVotesInPacket), votes.size() - index); - dev::RLPStream s(count); - for (auto i = index; i < index + count; i++) { + const size_t votes_count = std::min(static_cast(kMaxVotesInBundle), votes.size() - index); + + dev::RLPStream packet_rlp(kVotesBundlePacketSize); + packet_rlp.append(reference_block_hash); + packet_rlp.append(reference_period); + packet_rlp.append(reference_round); + packet_rlp.append(reference_step); + packet_rlp.appendList(votes_count); + + for (auto i = index; i < index + votes_count; i++) { const auto &vote = votes[i]; - s.appendRaw(vote->rlp(true, false)); + if (vote->getBlockHash() != reference_block_hash || vote->getPeriod() != reference_period || + vote->getRound() != reference_round || vote->getStep() != reference_step) { + LOG(log_er_) << "Invalid vote " << vote->getHash() << " (voted_block, period, round, step)->(" + << vote->getBlockHash() << ", " << vote->getPeriod() << ", " << vote->getRound() << ", " + << vote->getStep() << ") != reference vote (voted_block, period, round, step)->" + << reference_block_hash << ", " << reference_period << ", " << reference_round << ", " + << reference_step << ")"; + assert(false); + return; + } + + packet_rlp.appendRaw(vote->rlp(true, false)); LOG(log_dg_) << "Send vote " << vote->getHash() << " to peer " << peer->getId(); } - if (sealAndSend(peer->getId(), SubprotocolPacketType::VotesSyncPacket, std::move(s))) { - LOG(log_dg_) << count << " PBFT votes to were sent to " << peer->getId(); - for (auto i = index; i < index + count; i++) { + if (sealAndSend(peer->getId(), SubprotocolPacketType::VotesBundlePacket, std::move(packet_rlp))) { + LOG(log_dg_) << votes_count << " PBFT votes to were sent to " << peer->getId(); + for (auto i = index; i < index + votes_count; i++) { peer->markVoteAsKnown(votes[i]->getHash()); } } - index += count; + index += votes_count; } } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp index 3b9ddb71c3..1fb0e0c2bb 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp @@ -33,10 +33,14 @@ void GetNextVotesSyncPacketHandler::process(const PacketData &packet_data, const return; } - std::vector> next_votes = vote_mgr_->getAllTwoTPlusOneNextVotes(pbft_period, pbft_round - 1); + auto next_votes = + vote_mgr_->getTwoTPlusOneVotedBlockVotes(pbft_period, pbft_round - 1, TwoTPlusOneVotedBlockType::NextVotedBlock); + auto next_null_votes = vote_mgr_->getTwoTPlusOneVotedBlockVotes(pbft_period, pbft_round - 1, + TwoTPlusOneVotedBlockType::NextVotedNullBlock); + // In edge case this could theoretically happen due to race condition when we moved to the next period or round // right before calling getAllTwoTPlusOneNextVotes with specific period & round - if (next_votes.empty()) { + if (next_votes.empty() && next_null_votes.empty()) { // Try to get period & round values again const auto [tmp_pbft_round, tmp_pbft_period] = pbft_mgr_->getPbftRoundAndPeriod(); // No changes in period & round or new round == 1 @@ -51,29 +55,25 @@ void GetNextVotesSyncPacketHandler::process(const PacketData &packet_data, const return; } - next_votes = vote_mgr_->getAllTwoTPlusOneNextVotes(tmp_pbft_period, tmp_pbft_round - 1); - if (next_votes.empty()) { + next_votes = vote_mgr_->getTwoTPlusOneVotedBlockVotes(pbft_period, pbft_round - 1, + TwoTPlusOneVotedBlockType::NextVotedBlock); + next_null_votes = vote_mgr_->getTwoTPlusOneVotedBlockVotes(pbft_period, pbft_round - 1, + TwoTPlusOneVotedBlockType::NextVotedNullBlock); + if (next_votes.empty() && next_null_votes.empty()) { LOG(log_er_) << "No next votes returned for period " << tmp_pbft_period << ", round " << tmp_pbft_round - 1; return; } } - std::vector> next_votes_to_send; - next_votes_to_send.reserve(next_votes.size()); - for (const auto &vote : next_votes) { - if (!peer->isVoteKnown(vote->getHash())) { - next_votes_to_send.emplace_back(vote); - } + if (!next_votes.empty()) { + LOG(log_nf_) << "Send next votes bundle with " << next_votes.size() << " votes to " << peer->getId(); + sendPbftVotesBundle(peer, std::move(next_votes)); } - if (next_votes_to_send.empty()) { - LOG(log_dg_) << "Votes already gossiped, no need to send votes sync packet for" << pbft_period << ", round " - << pbft_round - 1; - return; + if (!next_null_votes.empty()) { + LOG(log_nf_) << "Send next null votes bundle with " << next_null_votes.size() << " votes to " << peer->getId(); + sendPbftVotesBundle(peer, std::move(next_null_votes)); } - - LOG(log_nf_) << "Next votes sync packet with " << next_votes.size() << " votes sent to " << peer->getId(); - sendPbftVotesBundle(peer, std::move(next_votes)); } } // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp index 551347bd34..32032d2b63 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp @@ -1,6 +1,5 @@ #include "network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp" -#include "network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "pbft/pbft_chain.hpp" #include "pbft/pbft_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp index 3f38145c36..00f3489515 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp @@ -15,19 +15,26 @@ VotesSyncPacketHandler::VotesSyncPacketHandler(const FullNodeConfig &conf, std:: void VotesSyncPacketHandler::validatePacketRlpFormat([[maybe_unused]] const PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); - if (items == 0 || items > kMaxVotesInPacket) { - throw InvalidRlpItemsCountException(packet_data.type_str_, items, kMaxVotesInPacket); + if (items != kVotesBundlePacketSize) { + throw InvalidRlpItemsCountException(packet_data.type_str_, items, kVotesBundlePacketSize); + } + + auto votes_count = packet_data.rlp_[kVotesBundlePacketSize - 1].itemCount(); + if (votes_count == 0 || votes_count > kMaxVotesInBundle) { + throw InvalidRlpItemsCountException(packet_data.type_str_, items, kMaxVotesInBundle); } } void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::shared_ptr &peer) { - const auto reference_vote = std::make_shared(packet_data.rlp_[0]); - const auto [current_pbft_round, current_pbft_period] = pbft_mgr_->getPbftRoundAndPeriod(); - const auto votes_bundle_pbft_period = reference_vote->getPeriod(); - const auto votes_bundle_pbft_round = reference_vote->getRound(); + + const auto votes_bundle_voted_block = packet_data.rlp_[0].toHash(); + const auto votes_bundle_pbft_period = packet_data.rlp_[1].toInt(); + const auto votes_bundle_pbft_round = packet_data.rlp_[2].toInt(); + const auto votes_bundle_votes_step = packet_data.rlp_[3].toInt(); + // TODO: special ctor for vote from optimized rlp + const auto &reference_vote = std::make_shared(packet_data.rlp_[4][0]); const auto votes_bundle_votes_type = reference_vote->getType(); - const auto votes_bundle_voted_block = reference_vote->getBlockHash(); // Votes sync bundles are allowed to cotain only votes bundles of the same type, period, round and step so if first // vote is irrelevant, all of them are @@ -39,20 +46,18 @@ void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::s return; } - // VotesSyncPacket does not support propose votes - if (votes_bundle_votes_type == PbftVoteTypes::propose_vote) { - LOG(log_er_) << "Dropping votes sync packet due to received \"propose_votes\" votes from " - << packet_data.from_node_id_ << ". The peer may be a malicious player, will be disconnected"; + // VotesBundlePacket does not support propose votes + if (reference_vote->getType() == PbftVoteTypes::propose_vote) { + LOG(log_er_) << "Dropping votes bundle packet due to received \"propose\" votes from " << packet_data.from_node_id_ + << ". The peer may be a malicious player, will be disconnected"; disconnect(packet_data.from_node_id_, dev::p2p::UserReason); return; } std::vector> votes; - blk_hash_t next_votes_bundle_voted_block = kNullBlockHash; - - const auto next_votes_count = packet_data.rlp_.itemCount(); - for (size_t i = 0; i < next_votes_count; i++) { - auto vote = std::make_shared(packet_data.rlp_[i]); + for (const auto vote_rlp : packet_data.rlp_[4]) { + // TODO: special ctor for vote from optimized rlp + auto vote = std::make_shared(vote_rlp); peer->markVoteAsKnown(vote->getHash()); // Do not process vote that has already been validated @@ -61,35 +66,11 @@ void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::s continue; } - // Next votes bundle can contain votes for kNullBlockHash as well as some specific block hash - if (vote->getType() == PbftVoteTypes::next_vote) { - if (next_votes_bundle_voted_block == kNullBlockHash && vote->getBlockHash() != kNullBlockHash) { - // initialize voted value with first block hash not equal to kNullBlockHash - next_votes_bundle_voted_block = vote->getBlockHash(); - } - - if (vote->getBlockHash() != kNullBlockHash && vote->getBlockHash() != next_votes_bundle_voted_block) { - // we see different voted value, so bundle is invalid - LOG(log_er_) << "Received next votes bundle with unmatched voted values(" << next_votes_bundle_voted_block - << ", " << vote->getBlockHash() << ") from " << packet_data.from_node_id_ - << ". The peer may be a malicious player, will be disconnected"; - disconnect(packet_data.from_node_id_, dev::p2p::UserReason); - return; - } - } else { - // Other votes bundles can contain votes only for 1 specific block hash - if (vote->getBlockHash() != votes_bundle_voted_block) { - // we see different voted value, so bundle is invalid - LOG(log_er_) << "Received votes bundle with unmatched voted values(" << votes_bundle_voted_block << ", " - << vote->getBlockHash() << ") from " << packet_data.from_node_id_ - << ". The peer may be a malicious player, will be disconnected"; - disconnect(packet_data.from_node_id_, dev::p2p::UserReason); - return; - } - } - - if (vote->getType() != votes_bundle_votes_type) { - LOG(log_er_) << "Received votes bundle with unmatched types from " << packet_data.from_node_id_ + // Votes bundles can contain votes only for 1 specific block hash + if (vote->getBlockHash() != votes_bundle_voted_block) { + // we see different voted value, so bundle is invalid + LOG(log_er_) << "Received votes bundle with unmatched voted values(" << votes_bundle_voted_block << ", " + << vote->getBlockHash() << ") from " << packet_data.from_node_id_ << ". The peer may be a malicious player, will be disconnected"; disconnect(packet_data.from_node_id_, dev::p2p::UserReason); return; @@ -109,6 +90,13 @@ void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::s return; } + if (vote->getStep() != votes_bundle_votes_step) { + LOG(log_er_) << "Received votes bundle with unmatched step from " << packet_data.from_node_id_ + << ". The peer may be a malicious player, will be disconnected"; + disconnect(packet_data.from_node_id_, dev::p2p::UserReason); + return; + } + LOG(log_dg_) << "Received sync vote " << vote->getHash().abridged(); // Process processStandardVote is called with false in case of next votes bundle -> does not check max boundaries @@ -125,9 +113,9 @@ void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::s votes.push_back(std::move(vote)); } - LOG(log_nf_) << "Received " << next_votes_count << " (processed " << votes.size() << " ) sync votes from peer " - << packet_data.from_node_id_ << " node current round " << current_pbft_round << ", peer pbft round " - << votes_bundle_pbft_round; + LOG(log_nf_) << "Received " << packet_data.rlp_[4].itemCount() << " (processed " << votes.size() + << " ) sync votes from peer " << packet_data.from_node_id_ << " node current round " + << current_pbft_round << ", peer pbft round " << votes_bundle_pbft_round; onNewPbftVotesBundle(std::move(votes), false, packet_data.from_node_id_); } diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index 15c361a31e..2ab501e083 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -27,7 +27,7 @@ namespace taraxa::network::tarcap { TaraxaCapability::TaraxaCapability(std::weak_ptr host, const dev::KeyPair &key, - const FullNodeConfig &conf, unsigned version) + const FullNodeConfig &conf, unsigned version, const std::string &log_channel) : test_state_(std::make_shared()), version_(version), kConf(conf), @@ -39,7 +39,7 @@ TaraxaCapability::TaraxaCapability(std::weak_ptr host, const dev periodic_events_tp_(std::make_shared(kPeriodicEventsThreadCount, false)), pub_key_(key.pub()) { const auto &node_addr = key.address(); - LOG_OBJECTS_CREATE("TARCAP"); + LOG_OBJECTS_CREATE(log_channel); peers_state_ = std::make_shared(host, kConf); all_packets_stats_ = @@ -53,8 +53,8 @@ std::shared_ptr TaraxaCapability::make( std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, const h256 &genesis_hash, unsigned version, std::shared_ptr db, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, - std::shared_ptr trx_mgr) { - auto instance = std::make_shared(host, key, conf, version); + std::shared_ptr trx_mgr, const std::string &log_channel) { + auto instance = std::make_shared(host, key, conf, version, log_channel); instance->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); return instance; } diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp new file mode 100644 index 0000000000..c18604ebeb --- /dev/null +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp @@ -0,0 +1,47 @@ +#include "network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp" + +#include "pbft/pbft_manager.hpp" +#include "vote/vote.hpp" +#include "vote_manager/vote_manager.hpp" + +namespace taraxa::network::v1_tarcap { + +ExtVotesPacketHandler::ExtVotesPacketHandler(const FullNodeConfig &conf, + std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_mgr, + std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, const addr_t &node_addr, + const std::string &log_channel_name) + : tarcap::ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), + std::move(pbft_chain), std::move(vote_mgr), node_addr, log_channel_name) {} + +void ExtVotesPacketHandler::sendPbftVotesBundle(const std::shared_ptr &peer, + std::vector> &&votes) { + if (votes.empty()) { + return; + } + + size_t index = 0; + while (index < votes.size()) { + const size_t count = std::min(static_cast(kMaxVotesInBundle), votes.size() - index); + dev::RLPStream s(count); + for (auto i = index; i < index + count; i++) { + const auto &vote = votes[i]; + // TODO: special ctor for optimized votes rlp + s.appendRaw(vote->rlp(true, false)); + LOG(log_dg_) << "Send vote " << vote->getHash() << " to peer " << peer->getId(); + } + + if (sealAndSend(peer->getId(), tarcap::SubprotocolPacketType::VotesBundlePacket, std::move(s))) { + LOG(log_dg_) << count << " PBFT votes to were sent to " << peer->getId(); + for (auto i = index; i < index + count; i++) { + peer->markVoteAsKnown(votes[i]->getHash()); + } + } + + index += count; + } +} + +} // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_next_votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_next_votes_sync_packet_handler.cpp new file mode 100644 index 0000000000..d6da4a4a58 --- /dev/null +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_next_votes_sync_packet_handler.cpp @@ -0,0 +1,80 @@ +#include "network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp" + +#include "pbft/pbft_manager.hpp" +#include "vote_manager/vote_manager.hpp" + +namespace taraxa::network::v1_tarcap { + +GetNextVotesSyncPacketHandler::GetNextVotesSyncPacketHandler( + const FullNodeConfig &conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, + std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, const addr_t &node_addr) + : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), + std::move(pbft_chain), std::move(vote_mgr), node_addr, "V1_GET_NEXT_VOTES_SYNC_PH") {} + +void GetNextVotesSyncPacketHandler::validatePacketRlpFormat(const tarcap::PacketData &packet_data) const { + if (constexpr size_t required_size = 2; packet_data.rlp_.itemCount() != required_size) { + throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); + } +} + +void GetNextVotesSyncPacketHandler::process(const tarcap::PacketData &packet_data, + const std::shared_ptr &peer) { + LOG(log_dg_) << "Received GetNextVotesSyncPacket request"; + + const PbftPeriod peer_pbft_period = packet_data.rlp_[0].toInt(); + const PbftRound peer_pbft_round = packet_data.rlp_[1].toInt(); + const auto [pbft_round, pbft_period] = pbft_mgr_->getPbftRoundAndPeriod(); + + // Send votes only for current_period == peer_period && current_period >= peer_round + if (pbft_period != peer_pbft_period || pbft_round == 1 || pbft_round < peer_pbft_round) { + LOG(log_nf_) << "No previous round next votes sync packet will be sent. pbft_period " << pbft_period + << ", peer_pbft_period " << peer_pbft_period << ", pbft_round " << pbft_round << ", peer_pbft_round " + << peer_pbft_round; + return; + } + + auto next_votes = + vote_mgr_->getTwoTPlusOneVotedBlockVotes(pbft_period, pbft_round - 1, TwoTPlusOneVotedBlockType::NextVotedBlock); + auto next_null_votes = vote_mgr_->getTwoTPlusOneVotedBlockVotes(pbft_period, pbft_round - 1, + TwoTPlusOneVotedBlockType::NextVotedNullBlock); + + // In edge case this could theoretically happen due to race condition when we moved to the next period or round + // right before calling getAllTwoTPlusOneNextVotes with specific period & round + if (next_votes.empty() && next_null_votes.empty()) { + // Try to get period & round values again + const auto [tmp_pbft_round, tmp_pbft_period] = pbft_mgr_->getPbftRoundAndPeriod(); + // No changes in period & round or new round == 1 + if (pbft_period == tmp_pbft_period && pbft_round == tmp_pbft_round) { + LOG(log_er_) << "No next votes returned for period " << tmp_pbft_period << ", round " << tmp_pbft_round - 1; + return; + } + + if (tmp_pbft_round == 1) { + LOG(log_wr_) << "No next votes returned for period " << tmp_pbft_period << ", round " << tmp_pbft_round - 1 + << " due to race condition - pbft already moved to the next period & round == 1"; + return; + } + + next_votes = vote_mgr_->getTwoTPlusOneVotedBlockVotes(pbft_period, pbft_round - 1, + TwoTPlusOneVotedBlockType::NextVotedBlock); + next_null_votes = vote_mgr_->getTwoTPlusOneVotedBlockVotes(pbft_period, pbft_round - 1, + TwoTPlusOneVotedBlockType::NextVotedNullBlock); + if (next_votes.empty() && next_null_votes.empty()) { + LOG(log_er_) << "No next votes returned for period " << tmp_pbft_period << ", round " << tmp_pbft_round - 1; + return; + } + } + + if (!next_votes.empty()) { + LOG(log_nf_) << "Send next votes bundle with " << next_votes.size() << " votes to " << peer->getId(); + sendPbftVotesBundle(peer, std::move(next_votes)); + } + + if (!next_null_votes.empty()) { + LOG(log_nf_) << "Send next null votes bundle with " << next_null_votes.size() << " votes to " << peer->getId(); + sendPbftVotesBundle(peer, std::move(next_null_votes)); + } +} + +} // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp new file mode 100644 index 0000000000..b3b3b89212 --- /dev/null +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp @@ -0,0 +1,104 @@ +#include "network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp" + +#include "network/tarcap/shared_states/pbft_syncing_state.hpp" +#include "pbft/pbft_chain.hpp" +#include "storage/storage.hpp" +#include "vote/vote.hpp" +#include "vote_manager/vote_manager.hpp" + +namespace taraxa::network::v1_tarcap { + +OmgGetPbftSyncPacketHandler::OmgGetPbftSyncPacketHandler(const FullNodeConfig &conf, + std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_syncing_state, + std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, + std::shared_ptr db, const addr_t &node_addr) + : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, "V1_GET_PBFT_SYNC_PH"), + pbft_syncing_state_(std::move(pbft_syncing_state)), + pbft_chain_(std::move(pbft_chain)), + vote_mgr_(std::move(vote_mgr)), + db_(std::move(db)) {} + +void OmgGetPbftSyncPacketHandler::validatePacketRlpFormat(const tarcap::PacketData &packet_data) const { + if (constexpr size_t required_size = 1; packet_data.rlp_.itemCount() != required_size) { + throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); + } +} + +void OmgGetPbftSyncPacketHandler::process(const tarcap::PacketData &packet_data, + [[maybe_unused]] const std::shared_ptr &peer) { + LOG(log_tr_) << "Received GetPbftSyncPacket Block"; + + const size_t height_to_sync = packet_data.rlp_[0].toInt(); + // Here need PBFT chain size, not synced period since synced blocks has not verified yet. + const size_t my_chain_size = pbft_chain_->getPbftChainSize(); + if (height_to_sync > my_chain_size) { + // Node update peers PBFT chain size in status packet. Should not request syncing period bigger than pbft chain size + std::ostringstream err_msg; + err_msg << "Peer " << packet_data.from_node_id_ << " request syncing period start at " << height_to_sync + << ". That's bigger than own PBFT chain size " << my_chain_size; + throw MaliciousPeerException(err_msg.str()); + } + + if (kConf.is_light_node && height_to_sync + kConf.light_node_history <= my_chain_size) { + std::ostringstream err_msg; + err_msg << "Peer " << packet_data.from_node_id_ << " request syncing period start at " << height_to_sync + << ". Light node does not have the data " << my_chain_size; + throw MaliciousPeerException(err_msg.str()); + } + + size_t blocks_to_transfer = 0; + auto pbft_chain_synced = false; + const auto total_period_datas_size = my_chain_size - height_to_sync + 1; + if (total_period_datas_size <= kConf.network.sync_level_size) { + blocks_to_transfer = total_period_datas_size; + pbft_chain_synced = true; + } else { + blocks_to_transfer = kConf.network.sync_level_size; + } + LOG(log_tr_) << "Will send " << blocks_to_transfer << " PBFT blocks to " << packet_data.from_node_id_; + + sendPbftBlocks(packet_data.from_node_id_, height_to_sync, blocks_to_transfer, pbft_chain_synced); +} + +// api for pbft syncing +void OmgGetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, PbftPeriod from_period, + size_t blocks_to_transfer, bool pbft_chain_synced) { + LOG(log_tr_) << "sendPbftBlocks: peer want to sync from pbft chain height " << from_period << ", will send at most " + << blocks_to_transfer << " pbft blocks to " << peer_id; + + for (auto block_period = from_period; block_period < from_period + blocks_to_transfer; block_period++) { + bool last_block = (block_period == from_period + blocks_to_transfer - 1); + auto data = db_->getPeriodDataRaw(block_period); + + if (data.size() == 0) { + LOG(log_er_) << "DB corrupted. Cannot find period " << block_period << " PBFT block in db"; + assert(false); + } + + dev::RLPStream s; + if (pbft_chain_synced && last_block) { + s.appendList(3); + s << last_block; + // TODO: once PeriodData db migration is done, parse PeriodData and create old rlp structure + s.appendRaw(data); + // Latest finalized block cert votes are saved in db as reward votes for new blocks + const auto votes = vote_mgr_->getRewardVotes(); + s.appendList(votes.size()); + for (const auto &vote : votes) { + s.appendRaw(vote->rlp(true)); + } + } else { + s.appendList(2); + s << last_block; + // TODO: once PeriodData db migration is done, parse PeriodData and create old rlp structure + s.appendRaw(data); + } + LOG(log_dg_) << "Sending PbftSyncPacket period " << block_period << " to " << peer_id; + sealAndSend(peer_id, tarcap::SubprotocolPacketType::PbftSyncPacket, std::move(s)); + } +} + +} // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp new file mode 100644 index 0000000000..10c55cd184 --- /dev/null +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp @@ -0,0 +1,280 @@ +#include "network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp" + +#include "network/tarcap/shared_states/pbft_syncing_state.hpp" +#include "pbft/pbft_chain.hpp" +#include "pbft/pbft_manager.hpp" +#include "transaction/transaction_manager.hpp" +#include "vote/vote.hpp" + +namespace taraxa::network::v1_tarcap { + +PbftSyncPacketHandler::PbftSyncPacketHandler( + const FullNodeConfig &conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, + std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr vote_mgr, + std::shared_ptr periodic_events_tp, std::shared_ptr db, const addr_t &node_addr) + : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), + std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, + "V1_PBFT_SYNC_PH"), + vote_mgr_(std::move(vote_mgr)), + periodic_events_tp_(periodic_events_tp) {} + +void PbftSyncPacketHandler::validatePacketRlpFormat(const tarcap::PacketData &packet_data) const { + if (packet_data.rlp_.itemCount() != kStandardPacketSize && packet_data.rlp_.itemCount() != kChainSyncedPacketSize) { + throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), kStandardPacketSize); + } + + // PeriodData rlp parsing cannot be done through util::rlp_tuple, which automatically checks the rlp size so it is + // checked here manually + if (packet_data.rlp_[1].itemCount() != PeriodData::kRlpItemCount) { + throw InvalidRlpItemsCountException(packet_data.type_str_ + ":PeriodData", packet_data.rlp_[1].itemCount(), + PeriodData::kRlpItemCount); + } +} + +void PbftSyncPacketHandler::process(const tarcap::PacketData &packet_data, + const std::shared_ptr &peer) { + // Note: no need to consider possible race conditions due to concurrent processing as it is + // disabled on priority_queue blocking dependencies level + const auto syncing_peer = pbft_syncing_state_->syncingPeer(); + if (!syncing_peer) { + LOG(log_wr_) << "PbftSyncPacket received from unexpected peer " << packet_data.from_node_id_.abridged() + << " but there is no current syncing peer set"; + return; + } + + if (syncing_peer->getId() != packet_data.from_node_id_) { + LOG(log_wr_) << "PbftSyncPacket received from unexpected peer " << packet_data.from_node_id_.abridged() + << " current syncing peer " << syncing_peer->getId().abridged(); + return; + } + + // Process received pbft blocks + // pbft_chain_synced is the flag to indicate own PBFT chain has synced with the peer's PBFT chain + const bool pbft_chain_synced = packet_data.rlp_.itemCount() == kChainSyncedPacketSize; + // last_block is the flag to indicate this is the last block in each syncing round, doesn't mean PBFT chain has synced + const bool last_block = packet_data.rlp_[0].toInt(); + PeriodData period_data; + try { + period_data = PeriodData(packet_data.rlp_[1]); + } catch (const Transaction::InvalidTransaction &e) { + throw MaliciousPeerException("Unable to parse PeriodData: " + std::string(e.what())); + } + + std::vector> current_block_cert_votes; + if (pbft_chain_synced) { + const auto cert_votes_count = packet_data.rlp_[2].itemCount(); + current_block_cert_votes.reserve(cert_votes_count); + for (size_t i = 0; i < cert_votes_count; i++) { + current_block_cert_votes.emplace_back(std::make_shared(packet_data.rlp_[2][i].data().toBytes())); + } + } + const auto pbft_blk_hash = period_data.pbft_blk->getBlockHash(); + + std::string received_dag_blocks_str; // This is just log related stuff + for (auto const &block : period_data.dag_blocks) { + received_dag_blocks_str += block.getHash().toString() + " "; + if (peer->dag_level_ < block.getLevel()) { + peer->dag_level_ = block.getLevel(); + } + } + const auto pbft_block_period = period_data.pbft_blk->getPeriod(); + + LOG(log_dg_) << "PbftSyncPacket received. Period: " << pbft_block_period + << ", dag Blocks: " << received_dag_blocks_str << " from " << packet_data.from_node_id_; + + peer->markPbftBlockAsKnown(pbft_blk_hash); + // Update peer's pbft period if outdated + if (peer->pbft_chain_size_ < pbft_block_period) { + peer->pbft_chain_size_ = pbft_block_period; + } + + LOG(log_tr_) << "Processing pbft block: " << pbft_blk_hash; + + if (pbft_chain_->findPbftBlockInChain(pbft_blk_hash)) { + LOG(log_wr_) << "PBFT block " << pbft_blk_hash << ", period: " << period_data.pbft_blk->getPeriod() << " from " + << packet_data.from_node_id_ << " already present in chain"; + } else { + if (pbft_block_period != pbft_mgr_->pbftSyncingPeriod() + 1) { + LOG(log_wr_) << "Block " << pbft_blk_hash << " period unexpected: " << pbft_block_period + << ". Expected period: " << pbft_mgr_->pbftSyncingPeriod() + 1; + restartSyncingPbft(true); + return; + } + + // Check cert vote matches if final synced block + if (pbft_chain_synced) { + for (auto const &vote : current_block_cert_votes) { + if (vote->getBlockHash() != pbft_blk_hash) { + LOG(log_er_) << "Invalid cert votes block hash " << vote->getBlockHash() << " instead of " << pbft_blk_hash + << " from peer " << packet_data.from_node_id_.abridged() << " received, stop syncing."; + handleMaliciousSyncPeer(packet_data.from_node_id_); + return; + } + } + } + + // Check votes match the hash of previous block in the queue + auto last_pbft_block_hash = pbft_mgr_->lastPbftBlockHashFromQueueOrChain(); + // Check cert vote matches + for (auto const &vote : period_data.previous_block_cert_votes) { + if (vote->getBlockHash() != last_pbft_block_hash) { + LOG(log_er_) << "Invalid cert votes block hash " << vote->getBlockHash() << " instead of " + << last_pbft_block_hash << " from peer " << packet_data.from_node_id_.abridged() + << " received, stop syncing."; + handleMaliciousSyncPeer(packet_data.from_node_id_); + return; + } + } + + auto order_hash = PbftManager::calculateOrderHash(period_data.dag_blocks); + if (order_hash != period_data.pbft_blk->getOrderHash()) { + { // This is just log related stuff + std::vector trx_order; + trx_order.reserve(period_data.transactions.size()); + std::vector blk_order; + blk_order.reserve(period_data.dag_blocks.size()); + for (auto t : period_data.transactions) { + trx_order.push_back(t->getHash()); + } + for (auto b : period_data.dag_blocks) { + blk_order.push_back(b.getHash()); + } + LOG(log_er_) << "Order hash incorrect in period data " << pbft_blk_hash << " expected: " << order_hash + << " received " << period_data.pbft_blk->getOrderHash() << "; Dag order: " << blk_order + << "; Trx order: " << trx_order << "; from " << packet_data.from_node_id_.abridged() + << ", stop syncing."; + } + handleMaliciousSyncPeer(packet_data.from_node_id_); + return; + } + + // This is special case when queue is empty and we can not say for sure that all votes that are part of this block + // have been verified before + if (pbft_mgr_->periodDataQueueEmpty()) { + for (const auto &v : period_data.previous_block_cert_votes) { + if (auto vote_is_valid = vote_mgr_->validateVote(v); vote_is_valid.first == false) { + LOG(log_er_) << "Invalid reward votes in block " << period_data.pbft_blk->getBlockHash() << " from peer " + << packet_data.from_node_id_.abridged() + << " received, stop syncing. Validation failed. Err: " << vote_is_valid.second; + handleMaliciousSyncPeer(packet_data.from_node_id_); + return; + } + + vote_mgr_->addVerifiedVote(v); + } + + // And now we need to replace it with verified votes + if (auto votes = vote_mgr_->checkRewardVotes(period_data.pbft_blk, true); votes.first) { + period_data.previous_block_cert_votes = std::move(votes.second); + } else { + // checkRewardVotes could fail because we just cert voted this block and moved to next period, in that case we + // might even be fully synced so call restartSyncingPbft to verify + if (pbft_block_period <= vote_mgr_->getRewardVotesPbftBlockPeriod()) { + restartSyncingPbft(true); + return; + } + + LOG(log_er_) << "Invalid reward votes in block " << period_data.pbft_blk->getBlockHash() << " from peer " + << packet_data.from_node_id_.abridged() << " received, stop syncing."; + handleMaliciousSyncPeer(packet_data.from_node_id_); + return; + } + } + + LOG(log_tr_) << "Synced PBFT block hash " << pbft_blk_hash << " with " + << period_data.previous_block_cert_votes.size() << " cert votes"; + LOG(log_tr_) << "Synced PBFT block " << period_data; + pbft_mgr_->periodDataQueuePush(std::move(period_data), packet_data.from_node_id_, + std::move(current_block_cert_votes)); + } + + auto pbft_sync_period = pbft_mgr_->pbftSyncingPeriod(); + + // Reset last sync packet received time + pbft_syncing_state_->setLastSyncPacketTime(); + + if (pbft_chain_synced) { + pbftSyncComplete(); + return; + } + + if (last_block) { + // If current sync period is actually bigger than the block we just received we are probably synced but verify with + // calling restartSyncingPbft + if (pbft_sync_period > pbft_block_period) { + return restartSyncingPbft(true); + } + if (pbft_syncing_state_->isPbftSyncing()) { + if (pbft_sync_period > pbft_chain_->getPbftChainSize() + (10 * kConf.network.sync_level_size)) { + LOG(log_tr_) << "Syncing pbft blocks too fast than processing. Has synced period " << pbft_sync_period + << ", PBFT chain size " << pbft_chain_->getPbftChainSize(); + if (auto periodic_events_tp = periodic_events_tp_.lock()) + periodic_events_tp->post(1000, [this] { delayedPbftSync(1); }); + } else { + if (!syncPeerPbft(pbft_sync_period + 1, true)) { + return restartSyncingPbft(true); + } + } + } + } +} + +void PbftSyncPacketHandler::pbftSyncComplete() { + if (pbft_mgr_->periodDataQueueSize()) { + LOG(log_tr_) << "Syncing pbft blocks faster than processing. Remaining sync size " + << pbft_mgr_->periodDataQueueSize(); + if (auto periodic_events_tp = periodic_events_tp_.lock()) + periodic_events_tp->post(1000, [this] { pbftSyncComplete(); }); + } else { + LOG(log_dg_) << "Syncing PBFT is completed"; + // We are pbft synced with the node we are connected to but + // calling restartSyncingPbft will check if some nodes have + // greater pbft chain size and we should continue syncing with + // them, Or sync pending DAG blocks + restartSyncingPbft(true); + if (!pbft_syncing_state_->isPbftSyncing()) { + requestPendingDagBlocks(); + } + } +} + +void PbftSyncPacketHandler::delayedPbftSync(int counter) { + auto pbft_sync_period = pbft_mgr_->pbftSyncingPeriod(); + if (counter > 60) { + LOG(log_er_) << "Pbft blocks stuck in queue, no new block processed in 60 seconds " << pbft_sync_period << " " + << pbft_chain_->getPbftChainSize(); + pbft_syncing_state_->setPbftSyncing(false); + LOG(log_tr_) << "Syncing PBFT is stopping"; + return; + } + + if (pbft_syncing_state_->isPbftSyncing()) { + if (pbft_sync_period > pbft_chain_->getPbftChainSize() + (10 * kConf.network.sync_level_size)) { + LOG(log_tr_) << "Syncing pbft blocks faster than processing " << pbft_sync_period << " " + << pbft_chain_->getPbftChainSize(); + if (auto periodic_events_tp = periodic_events_tp_.lock()) + periodic_events_tp->post(1000, [this, counter] { delayedPbftSync(counter + 1); }); + } else { + if (!syncPeerPbft(pbft_sync_period + 1)) { + return restartSyncingPbft(true); + } + } + } +} + +void PbftSyncPacketHandler::handleMaliciousSyncPeer(dev::p2p::NodeID const &id) { + peers_state_->set_peer_malicious(id); + + if (auto host = peers_state_->host_.lock(); host) { + LOG(log_nf_) << "Disconnect peer " << id; + host->disconnect(id, dev::p2p::UserReason); + } else { + LOG(log_er_) << "Unable to handleMaliciousSyncPeer, host == nullptr"; + } + + restartSyncingPbft(true); +} + +} // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_sync_packet_handler.cpp new file mode 100644 index 0000000000..fe518124c8 --- /dev/null +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_sync_packet_handler.cpp @@ -0,0 +1,161 @@ +#include "network/v1_tarcap/packets_handlers/votes_sync_packet_handler.hpp" + +#include "pbft/pbft_manager.hpp" +#include "vote_manager/vote_manager.hpp" + +namespace taraxa::network::v1_tarcap { + +VotesSyncPacketHandler::VotesSyncPacketHandler(const FullNodeConfig &conf, + std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_mgr, + std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, const addr_t &node_addr) + : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), + std::move(pbft_chain), std::move(vote_mgr), node_addr, "V1_VOTES_SYNC_PH") {} + +void VotesSyncPacketHandler::validatePacketRlpFormat([[maybe_unused]] const tarcap::PacketData &packet_data) const { + auto items = packet_data.rlp_.itemCount(); + if (items == 0 || items > kMaxVotesInBundle) { + throw InvalidRlpItemsCountException(packet_data.type_str_, items, kMaxVotesInBundle); + } +} + +void VotesSyncPacketHandler::process(const tarcap::PacketData &packet_data, + const std::shared_ptr &peer) { + const auto reference_vote = std::make_shared(packet_data.rlp_[0]); + + const auto [current_pbft_round, current_pbft_period] = pbft_mgr_->getPbftRoundAndPeriod(); + const auto votes_bundle_pbft_period = reference_vote->getPeriod(); + const auto votes_bundle_pbft_round = reference_vote->getRound(); + const auto votes_bundle_votes_type = reference_vote->getType(); + const auto votes_bundle_voted_block = reference_vote->getBlockHash(); + + // Votes sync bundles are allowed to cotain only votes bundles of the same type, period, round and step so if first + // vote is irrelevant, all of them are + if (!isPbftRelevantVote(reference_vote)) { + LOG(log_wr_) << "Drop votes sync bundle as it is irrelevant for current pbft state. Votes (period, round, step) = (" + << votes_bundle_pbft_period << ", " << votes_bundle_pbft_round << ", " << reference_vote->getStep() + << "). Current PBFT (period, round, step) = (" << current_pbft_period << ", " << current_pbft_round + << ", " << pbft_mgr_->getPbftStep() << ")"; + return; + } + + // VotesBundlePacket does not support propose votes + if (votes_bundle_votes_type == PbftVoteTypes::propose_vote) { + LOG(log_er_) << "Dropping votes sync packet due to received \"propose_votes\" votes from " + << packet_data.from_node_id_ << ". The peer may be a malicious player, will be disconnected"; + disconnect(packet_data.from_node_id_, dev::p2p::UserReason); + return; + } + + std::vector> votes; + blk_hash_t next_votes_bundle_voted_block = kNullBlockHash; + + const auto next_votes_count = packet_data.rlp_.itemCount(); + for (size_t i = 0; i < next_votes_count; i++) { + auto vote = std::make_shared(packet_data.rlp_[i]); + peer->markVoteAsKnown(vote->getHash()); + + // Do not process vote that has already been validated + if (vote_mgr_->voteAlreadyValidated(vote->getHash())) { + LOG(log_dg_) << "Received vote " << vote->getHash() << " has already been validated"; + continue; + } + + // Next votes bundle can contain votes for kNullBlockHash as well as some specific block hash + if (vote->getType() == PbftVoteTypes::next_vote) { + if (next_votes_bundle_voted_block == kNullBlockHash && vote->getBlockHash() != kNullBlockHash) { + // initialize voted value with first block hash not equal to kNullBlockHash + next_votes_bundle_voted_block = vote->getBlockHash(); + } + + if (vote->getBlockHash() != kNullBlockHash && vote->getBlockHash() != next_votes_bundle_voted_block) { + // we see different voted value, so bundle is invalid + LOG(log_er_) << "Received next votes bundle with unmatched voted values(" << next_votes_bundle_voted_block + << ", " << vote->getBlockHash() << ") from " << packet_data.from_node_id_ + << ". The peer may be a malicious player, will be disconnected"; + disconnect(packet_data.from_node_id_, dev::p2p::UserReason); + return; + } + } else { + // Other votes bundles can contain votes only for 1 specific block hash + if (vote->getBlockHash() != votes_bundle_voted_block) { + // we see different voted value, so bundle is invalid + LOG(log_er_) << "Received votes bundle with unmatched voted values(" << votes_bundle_voted_block << ", " + << vote->getBlockHash() << ") from " << packet_data.from_node_id_ + << ". The peer may be a malicious player, will be disconnected"; + disconnect(packet_data.from_node_id_, dev::p2p::UserReason); + return; + } + } + + if (vote->getType() != votes_bundle_votes_type) { + LOG(log_er_) << "Received votes bundle with unmatched types from " << packet_data.from_node_id_ + << ". The peer may be a malicious player, will be disconnected"; + disconnect(packet_data.from_node_id_, dev::p2p::UserReason); + return; + } + + if (vote->getPeriod() != votes_bundle_pbft_period) { + LOG(log_er_) << "Received votes bundle with unmatched periods from " << packet_data.from_node_id_ + << ". The peer may be a malicious player, will be disconnected"; + disconnect(packet_data.from_node_id_, dev::p2p::UserReason); + return; + } + + if (vote->getRound() != votes_bundle_pbft_round) { + LOG(log_er_) << "Received votes bundle with unmatched rounds from " << packet_data.from_node_id_ + << ". The peer may be a malicious player, will be disconnected"; + disconnect(packet_data.from_node_id_, dev::p2p::UserReason); + return; + } + + LOG(log_dg_) << "Received sync vote " << vote->getHash().abridged(); + + // Process processStandardVote is called with false in case of next votes bundle -> does not check max boundaries + // for round and step to actually being able to sync the current round in case network is stalled + bool check_max_round_step = true; + if (votes_bundle_votes_type == PbftVoteTypes::cert_vote || votes_bundle_votes_type == PbftVoteTypes::next_vote) { + check_max_round_step = false; + } + + if (!processVote(vote, nullptr, peer, check_max_round_step)) { + continue; + } + + votes.push_back(std::move(vote)); + } + + LOG(log_nf_) << "Received " << next_votes_count << " (processed " << votes.size() << " ) sync votes from peer " + << packet_data.from_node_id_ << " node current round " << current_pbft_round << ", peer pbft round " + << votes_bundle_pbft_round; + + onNewPbftVotesBundle(std::move(votes), false, packet_data.from_node_id_); +} + +void VotesSyncPacketHandler::onNewPbftVotesBundle(std::vector> &&votes, bool rebroadcast, + const std::optional &exclude_node) { + for (const auto &peer : peers_state_->getAllPeers()) { + if (peer.second->syncing_) { + continue; + } + + if (exclude_node.has_value() && *exclude_node == peer.first) { + continue; + } + + std::vector> peer_votes; + for (const auto &vote : votes) { + if (!rebroadcast && peer.second->isVoteKnown(vote->getHash())) { + continue; + } + + peer_votes.push_back(vote); + } + + sendPbftVotesBundle(peer.second, std::move(peer_votes)); + } +} + +} // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/src/v1_tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/v1_tarcap/taraxa_capability.cpp new file mode 100644 index 0000000000..cb867216e1 --- /dev/null +++ b/libraries/core_libs/network/src/v1_tarcap/taraxa_capability.cpp @@ -0,0 +1,66 @@ +#include "network/v1_tarcap/taraxa_capability.hpp" + +#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp" +#include "network/tarcap/packets_handlers/dag_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/get_dag_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/status_packet_handler.hpp" +#include "network/tarcap/packets_handlers/transaction_packet_handler.hpp" +#include "network/tarcap/packets_handlers/vote_packet_handler.hpp" +#include "network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp" +#include "network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp" +#include "network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/v1_tarcap/packets_handlers/votes_sync_packet_handler.hpp" + +namespace taraxa::network::v1_tarcap { + +TaraxaCapability::TaraxaCapability(std::weak_ptr host, const dev::KeyPair &key, + const FullNodeConfig &conf, unsigned version, const std::string &log_channel) + : tarcap::TaraxaCapability(host, key, conf, version, log_channel) {} + +void TaraxaCapability::registerPacketHandlers( + const h256 &genesis_hash, const std::shared_ptr &packets_stats, + const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, + const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, + const std::shared_ptr &dag_mgr, const std::shared_ptr &trx_mgr, + const addr_t &node_addr) { + node_stats_ = std::make_shared(peers_state_, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, + vote_mgr, trx_mgr, packets_stats, thread_pool_, node_addr); + + // Register all packet handlers + + // Consensus packets with high processing priority + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, + pbft_chain, vote_mgr, node_addr); + packets_handlers_->registerHandler( + kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, + pbft_chain, vote_mgr, node_addr); + + // Standard packets with mid processing priority + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, + pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, + trx_mgr, db, test_state_, node_addr); + + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, trx_mgr, + test_state_, node_addr); + + // Non critical packets with low processing priority + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, + pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, + db, genesis_hash, node_addr); + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, trx_mgr, + dag_mgr, db, node_addr); + + packets_handlers_->registerHandler( + kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, node_addr); + + packets_handlers_->registerHandler( + kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, vote_mgr, db, node_addr); + + packets_handlers_->registerHandler( + kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, + periodic_events_tp_, db, node_addr); + + thread_pool_->setPacketsHandlers(packets_handlers_); +} +} // namespace taraxa::network::v1_tarcap diff --git a/libraries/types/vote/CMakeLists.txt b/libraries/types/vote/CMakeLists.txt index 7289a0751b..63bf47915a 100644 --- a/libraries/types/vote/CMakeLists.txt +++ b/libraries/types/vote/CMakeLists.txt @@ -1,5 +1,5 @@ -set(HEADERS include/vote/vote.hpp include/vote/vrf_sortition.hpp) -set(SOURCES src/vote.cpp src/vrf_sortition.cpp) +set(HEADERS include/vote/vote.hpp include/vote/vrf_sortition.hpp include/vote/votes_bundle.hpp) +set(SOURCES src/vote.cpp src/vrf_sortition.cpp src/votes_bundle.cpp) add_library(vote ${SOURCES} ${HEADERS}) target_include_directories(vote PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/libraries/types/vote/include/vote/vote.hpp b/libraries/types/vote/include/vote/vote.hpp index 976b093ee3..0621c4d4b9 100644 --- a/libraries/types/vote/include/vote/vote.hpp +++ b/libraries/types/vote/include/vote/vote.hpp @@ -172,20 +172,6 @@ class Vote { mutable std::optional weight_; }; -/** - * @brief VotesBundle struct stores a bunch of votes that vote on the same voting value in the specific PBFT round and - * step, the total votes weights must be greater or equal to PBFT 2t+1. - */ -struct VotesBundle { - blk_hash_t voted_block_hash; - PbftPeriod votes_period{0}; - std::vector> votes; // Greater than 2t+1 votes - - VotesBundle() {} - VotesBundle(blk_hash_t const& voted_block_hash, std::vector> const& votes) - : voted_block_hash(voted_block_hash), votes(votes) {} -}; - /** @}*/ } // namespace taraxa \ No newline at end of file diff --git a/libraries/types/vote/include/vote/votes_bundle.hpp b/libraries/types/vote/include/vote/votes_bundle.hpp new file mode 100644 index 0000000000..751ae969c8 --- /dev/null +++ b/libraries/types/vote/include/vote/votes_bundle.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include "common/types.hpp" +#include "vote.hpp" + +namespace taraxa { + +/** @addtogroup Vote + * @{ + */ + +/** + * @brief VotesBundle stores >= 2t+1 votes with same voted hash, period, round and step. It is used for iptimized + * rlp storage so we dont save these duplicate data for each vote. + */ +class VotesBundle { + public: + VotesBundle(const std::vector>& votes, bool validate_common_data = true); + // Ctor for VotesBundle syncing packets rlp + VotesBundle(const dev::RLP& rlp); + // Ctor for PeriodData rlp - it does not contain block_hash, period and step(certify) as those are already stored in + // Pbft block rlp + VotesBundle(const blk_hash_t& block_hash, PbftPeriod period, PbftRound round, PbftStep step, const dev::RLP& rlp); + + bytes rlp() const; + + private: + // Common votes block hash + blk_hash_t block_hash_; + // Common votes period + PbftPeriod period_{0}; + // Common votes round + PbftRound round_{0}; + // Common votes step + PbftStep step_{0}; + // >= 2t+1 votes + std::vector> votes_; +}; + +/** @}*/ + +} // namespace taraxa diff --git a/libraries/types/vote/src/votes_bundle.cpp b/libraries/types/vote/src/votes_bundle.cpp new file mode 100644 index 0000000000..44573fcb8d --- /dev/null +++ b/libraries/types/vote/src/votes_bundle.cpp @@ -0,0 +1,52 @@ +#include "vote/votes_bundle.hpp" + +namespace taraxa { + +// TODO: maybe && votes ??? +VotesBundle::VotesBundle(const std::vector>& votes, bool validate_common_data) : votes_(votes) { + if (votes_.empty()) { + throw std::runtime_error("Cannot initialize VotesBundle without any votes"); + } + const auto& reference_vote = votes_.back(); + block_hash_ = reference_vote->getBlockHash(); + period_ = reference_vote->getPeriod(); + round_ = reference_vote->getRound(); + step_ = reference_vote->getStep(); + + if (validate_common_data) { + for (const auto& vote : votes_) { + if (vote->getBlockHash() != block_hash_) { + throw std::runtime_error("VotesBundle: Wrong block hash"); + } + if (vote->getPeriod() != period_) { + throw std::runtime_error("VotesBundle: Wrong period"); + } + if (vote->getRound() != round_) { + throw std::runtime_error("VotesBundle: Wrong round"); + } + if (vote->getStep() != step_) { + throw std::runtime_error("VotesBundle: step"); + } + } + } +} + +// bytes VotesBundle::rlp() const { +// // Pseudo code +// dev::RLPStream s(5); +// +// s << block_hash_; +// s << period_; +// s << round_; +// s << step_; +// +// s.appendList(votes_.size()); +// for (const auto& vote : votes_) { +// // Get optimizes vote rlp with only signature & and proof +// // s << optimized_vote_rlp +// } +// +// return s.invalidate(); +// } + +} // namespace taraxa \ No newline at end of file diff --git a/tests/network_test.cpp b/tests/network_test.cpp index 5993b155a7..970ed0ac33 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -141,7 +141,7 @@ TEST_F(NetworkTest, transfer_lot_of_blocks) { wait({120s, 200ms}, [&](auto& ctx) { WAIT_EXPECT_NE(ctx, dag_mgr2->getDagBlock(block_hash), nullptr) }); } -TEST_F(NetworkTest, update_peer_chainsize) { +TEST_F(NetworkTest, DISABLED_update_peer_chainsize) { auto node_cfgs = make_node_cfgs(2, 1, 5); auto nodes = launch_nodes(node_cfgs); diff --git a/tests/tarcap_threadpool_test.cpp b/tests/tarcap_threadpool_test.cpp index 0e95b0cc7e..a00127b961 100644 --- a/tests/tarcap_threadpool_test.cpp +++ b/tests/tarcap_threadpool_test.cpp @@ -177,7 +177,7 @@ class DummyVotesSyncPacketHandler : public DummyPacketHandler { : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::VotesSyncPacket; + static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::VotesBundlePacket; }; class DummyGetDagSyncPacketHandler : public DummyPacketHandler { @@ -366,11 +366,11 @@ TEST_F(TarcapTpTest, block_free_packets) { tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::GetNextVotesSyncPacket, {})).value(); const auto packet16_pbft_next_votes_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::VotesSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::VotesBundlePacket, {})).value(); size_t packets_count = 0; const auto packet17_pbft_next_votes_id = packets_count = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::VotesSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::VotesBundlePacket, {})).value(); tp.startProcessing(); From 52a688f24e415a38286a351367d68e50bfe03f30 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Fri, 14 Apr 2023 16:23:51 -0700 Subject: [PATCH 025/134] chore: use optimized rlp format for votes syncing --- .../common/ext_votes_packet_handler.cpp | 2 +- .../votes_sync_packet_handler.cpp | 16 ++++++------ libraries/types/vote/include/vote/vote.hpp | 12 +++++++++ libraries/types/vote/src/vote.cpp | 25 +++++++++++++++++++ 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp index bd691f3ffb..ce1330a9be 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp @@ -205,7 +205,7 @@ void ExtVotesPacketHandler::sendPbftVotesBundle(const std::shared_ptrrlp(true, false)); + packet_rlp.appendRaw(vote->optimizedRlp()); LOG(log_dg_) << "Send vote " << vote->getHash() << " to peer " << peer->getId(); } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp index 00f3489515..dc25866706 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp @@ -28,12 +28,14 @@ void VotesSyncPacketHandler::validatePacketRlpFormat([[maybe_unused]] const Pack void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::shared_ptr &peer) { const auto [current_pbft_round, current_pbft_period] = pbft_mgr_->getPbftRoundAndPeriod(); - const auto votes_bundle_voted_block = packet_data.rlp_[0].toHash(); + const auto votes_bundle_block_hash = packet_data.rlp_[0].toHash(); const auto votes_bundle_pbft_period = packet_data.rlp_[1].toInt(); const auto votes_bundle_pbft_round = packet_data.rlp_[2].toInt(); const auto votes_bundle_votes_step = packet_data.rlp_[3].toInt(); - // TODO: special ctor for vote from optimized rlp - const auto &reference_vote = std::make_shared(packet_data.rlp_[4][0]); + + const auto &reference_vote = + std::make_shared(votes_bundle_block_hash, votes_bundle_pbft_period, votes_bundle_pbft_round, + votes_bundle_votes_step, packet_data.rlp_[4][0]); const auto votes_bundle_votes_type = reference_vote->getType(); // Votes sync bundles are allowed to cotain only votes bundles of the same type, period, round and step so if first @@ -56,8 +58,8 @@ void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::s std::vector> votes; for (const auto vote_rlp : packet_data.rlp_[4]) { - // TODO: special ctor for vote from optimized rlp - auto vote = std::make_shared(vote_rlp); + auto vote = std::make_shared(votes_bundle_block_hash, votes_bundle_pbft_period, votes_bundle_pbft_round, + votes_bundle_votes_step, vote_rlp); peer->markVoteAsKnown(vote->getHash()); // Do not process vote that has already been validated @@ -67,9 +69,9 @@ void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::s } // Votes bundles can contain votes only for 1 specific block hash - if (vote->getBlockHash() != votes_bundle_voted_block) { + if (vote->getBlockHash() != votes_bundle_block_hash) { // we see different voted value, so bundle is invalid - LOG(log_er_) << "Received votes bundle with unmatched voted values(" << votes_bundle_voted_block << ", " + LOG(log_er_) << "Received votes bundle with unmatched voted values(" << votes_bundle_block_hash << ", " << vote->getBlockHash() << ") from " << packet_data.from_node_id_ << ". The peer may be a malicious player, will be disconnected"; disconnect(packet_data.from_node_id_, dev::p2p::UserReason); diff --git a/libraries/types/vote/include/vote/vote.hpp b/libraries/types/vote/include/vote/vote.hpp index 0621c4d4b9..60279f6f6d 100644 --- a/libraries/types/vote/include/vote/vote.hpp +++ b/libraries/types/vote/include/vote/vote.hpp @@ -20,6 +20,10 @@ class Vote { Vote() = default; Vote(secret_t const& node_sk, VrfPbftSortition vrf_sortition, blk_hash_t const& blockhash); + // Ctor for optimized rlp vote objects - only signature and vrf proof are in the rlp + explicit Vote(const blk_hash_t& block_hash, PbftPeriod period, PbftRound round, PbftStep step, dev::RLP const& rlp); + + // Ctors for full rlp vote objects - all data are encoded in the rlp explicit Vote(dev::RLP const& rlp); explicit Vote(bytes const& rlp); bool operator==(Vote const& other) const { return rlp() == other.rlp(); } @@ -116,6 +120,14 @@ class Vote { */ bytes rlp(bool inc_sig = true, bool inc_weight = false) const; + /** + * @brief Optimed Recursive Length Prefix + * @note Encode only vote's signature and vrf proof into the rlp + * + * @return bytes of RLP stream + */ + bytes optimizedRlp() const; + /** * @brief Verify vote * @return true if passed diff --git a/libraries/types/vote/src/vote.cpp b/libraries/types/vote/src/vote.cpp index 65d264136b..ca0b5ff595 100644 --- a/libraries/types/vote/src/vote.cpp +++ b/libraries/types/vote/src/vote.cpp @@ -4,6 +4,23 @@ namespace taraxa { +Vote::Vote(const blk_hash_t& block_hash, PbftPeriod period, PbftRound round, PbftStep step, dev::RLP const& rlp) + : blockhash_(block_hash) { + vrf_wrapper::vrf_proof_t vrf_proof; + sig_t vote_signature; + util::rlp_tuple(util::RLPDecoderRef(rlp, true), vrf_proof, vote_signature); + + VrfPbftSortition vrf_sortition; + vrf_sortition.pbft_msg_.period_ = period; + vrf_sortition.pbft_msg_.round_ = round; + vrf_sortition.pbft_msg_.step_ = step; + vrf_sortition.proof_ = vrf_proof; + + vrf_sortition_ = std::move(vrf_sortition); + vote_signature_ = std::move(vote_signature); + vote_hash_ = sha3(true); +} + Vote::Vote(dev::RLP const& rlp) { bytes vrf_bytes; if (rlp.itemCount() == 3) { @@ -49,4 +66,12 @@ bytes Vote::rlp(bool inc_sig, bool inc_weight) const { return s.invalidate(); } +bytes Vote::optimizedRlp() const { + dev::RLPStream s(2); + s << vrf_sortition_.proof_; + s << vote_signature_; + + return s.invalidate(); +} + } // namespace taraxa \ No newline at end of file From 7b4ba5e77049fb35b3068bfbd9b85a9a481aff75 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Fri, 14 Apr 2023 18:39:48 -0700 Subject: [PATCH 026/134] chore: use optimized votes rlp for PeriodData db storage as well as network --- .../common/ext_votes_packet_handler.hpp | 3 +- .../common/ext_votes_packet_handler.cpp | 69 +++++++++--------- .../get_pbft_sync_packet_handler.cpp | 9 ++- .../pbft_sync_packet_handler.cpp | 7 +- .../votes_sync_packet_handler.cpp | 42 ++--------- .../common/ext_votes_packet_handler.cpp | 3 +- .../votes_sync_packet_handler.cpp | 4 +- .../types/pbft_block/src/period_data.cpp | 13 ++-- libraries/types/vote/CMakeLists.txt | 4 +- .../types/vote/include/vote/votes_bundle.hpp | 42 ----------- .../vote/include/vote/votes_bundle_rlp.hpp | 37 ++++++++++ libraries/types/vote/src/votes_bundle.cpp | 52 -------------- libraries/types/vote/src/votes_bundle_rlp.cpp | 70 +++++++++++++++++++ 13 files changed, 164 insertions(+), 191 deletions(-) delete mode 100644 libraries/types/vote/include/vote/votes_bundle.hpp create mode 100644 libraries/types/vote/include/vote/votes_bundle_rlp.hpp delete mode 100644 libraries/types/vote/src/votes_bundle.cpp create mode 100644 libraries/types/vote/src/votes_bundle_rlp.cpp diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp index 07436afc75..78eb84cf3b 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp @@ -73,8 +73,7 @@ class ExtVotesPacketHandler : public PacketHandler { bool validateVoteAndBlock(const std::shared_ptr& vote, const std::shared_ptr& pbft_block) const; protected: - constexpr static size_t kVotesBundlePacketSize{5}; - constexpr static size_t kMaxVotesInBundle{1000}; + constexpr static size_t kMaxVotesInBundleRlp{1000}; constexpr static std::chrono::seconds kSyncRequestInterval = std::chrono::seconds(10); mutable std::chrono::system_clock::time_point last_votes_sync_request_time_; diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp index ce1330a9be..c02f17c8c2 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp @@ -2,6 +2,7 @@ #include "pbft/pbft_manager.hpp" #include "vote/vote.hpp" +#include "vote/votes_bundle_rlp.hpp" #include "vote_manager/vote_manager.hpp" namespace taraxa::network::tarcap { @@ -176,47 +177,43 @@ void ExtVotesPacketHandler::sendPbftVotesBundle(const std::shared_ptrgetBlockHash(); - const auto reference_period = votes.back()->getPeriod(); - const auto reference_round = votes.back()->getRound(); - const auto reference_step = votes.back()->getStep(); - - size_t index = 0; - while (index < votes.size()) { - const size_t votes_count = std::min(static_cast(kMaxVotesInBundle), votes.size() - index); - - dev::RLPStream packet_rlp(kVotesBundlePacketSize); - packet_rlp.append(reference_block_hash); - packet_rlp.append(reference_period); - packet_rlp.append(reference_round); - packet_rlp.append(reference_step); - packet_rlp.appendList(votes_count); - - for (auto i = index; i < index + votes_count; i++) { - const auto &vote = votes[i]; - if (vote->getBlockHash() != reference_block_hash || vote->getPeriod() != reference_period || - vote->getRound() != reference_round || vote->getStep() != reference_step) { - LOG(log_er_) << "Invalid vote " << vote->getHash() << " (voted_block, period, round, step)->(" - << vote->getBlockHash() << ", " << vote->getPeriod() << ", " << vote->getRound() << ", " - << vote->getStep() << ") != reference vote (voted_block, period, round, step)->" - << reference_block_hash << ", " << reference_period << ", " << reference_round << ", " - << reference_step << ")"; - assert(false); - return; - } - - packet_rlp.appendRaw(vote->optimizedRlp()); - LOG(log_dg_) << "Send vote " << vote->getHash() << " to peer " << peer->getId(); + auto sendVotes = [this, &peer](std::vector> &&votes) { + auto votes_bytes = encodeVotesBundleRlp(std::move(votes), false); + if (votes_bytes.empty()) { + LOG(log_er_) << "Unable to send VotesBundle rlp"; + return; } - if (sealAndSend(peer->getId(), SubprotocolPacketType::VotesBundlePacket, std::move(packet_rlp))) { - LOG(log_dg_) << votes_count << " PBFT votes to were sent to " << peer->getId(); - for (auto i = index; i < index + votes_count; i++) { - peer->markVoteAsKnown(votes[i]->getHash()); + dev::RLPStream votes_rlp_stream; + votes_rlp_stream.appendRaw(votes_bytes); + + if (sealAndSend(peer->getId(), SubprotocolPacketType::VotesBundlePacket, std::move(votes_rlp_stream))) { + LOG(log_dg_) << " Votes bundle with " << votes.size() << " sent to " << peer->getId(); + for (const auto &vote : votes) { + peer->markVoteAsKnown(vote->getHash()); } } + }; + + if (votes.size() <= kMaxVotesInBundleRlp) { + sendVotes(std::move(votes)); + return; + } else { + // Need to split votes into multiple packets + size_t index = 0; + while (index < votes.size()) { + const size_t votes_count = std::min(kMaxVotesInBundleRlp, votes.size() - index); + + const auto begin_it = std::next(votes.begin(), index); + const auto end_it = std::next(begin_it, votes_count); + + std::vector> votes_sub_vector; + std::move(begin_it, end_it, std::back_inserter(votes_sub_vector)); - index += votes_count; + sendVotes(std::move(votes_sub_vector)); + + index += votes_count; + } } } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp index 7d569f08ca..82a590f4f5 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp @@ -4,6 +4,7 @@ #include "pbft/pbft_chain.hpp" #include "storage/storage.hpp" #include "vote/vote.hpp" +#include "vote/votes_bundle_rlp.hpp" #include "vote_manager/vote_manager.hpp" namespace taraxa::network::tarcap { @@ -84,11 +85,9 @@ void GetPbftSyncPacketHandler::sendPbftBlocks(const std::shared_ptr s << last_block; s.appendRaw(data); // Latest finalized block cert votes are saved in db as reward votes for new blocks - const auto votes = vote_mgr_->getRewardVotes(); - s.appendList(votes.size()); - for (const auto &vote : votes) { - s.appendRaw(vote->rlp(true)); - } + const auto reward_votes = vote_mgr_->getRewardVotes(); + assert(!reward_votes.empty()); + s.appendRaw(encodeVotesBundleRlp(reward_votes, false)); } else { s.appendList(2); s << last_block; diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp index 32032d2b63..2ee67c97bb 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp @@ -5,6 +5,7 @@ #include "pbft/pbft_manager.hpp" #include "transaction/transaction_manager.hpp" #include "vote/vote.hpp" +#include "vote/votes_bundle_rlp.hpp" namespace taraxa::network::tarcap { @@ -65,11 +66,7 @@ void PbftSyncPacketHandler::process(const PacketData &packet_data, const std::sh std::vector> current_block_cert_votes; if (pbft_chain_synced) { - const auto cert_votes_count = packet_data.rlp_[2].itemCount(); - current_block_cert_votes.reserve(cert_votes_count); - for (size_t i = 0; i < cert_votes_count; i++) { - current_block_cert_votes.emplace_back(std::make_shared(packet_data.rlp_[2][i].data().toBytes())); - } + current_block_cert_votes = decodeVotesBundleRlp(packet_data.rlp_[2]); } const auto pbft_blk_hash = period_data.pbft_blk->getBlockHash(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp index dc25866706..8f1f8c57c9 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp @@ -1,6 +1,7 @@ #include "network/tarcap/packets_handlers/votes_sync_packet_handler.hpp" #include "pbft/pbft_manager.hpp" +#include "vote/votes_bundle_rlp.hpp" #include "vote_manager/vote_manager.hpp" namespace taraxa::network::tarcap { @@ -15,13 +16,13 @@ VotesSyncPacketHandler::VotesSyncPacketHandler(const FullNodeConfig &conf, std:: void VotesSyncPacketHandler::validatePacketRlpFormat([[maybe_unused]] const PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); - if (items != kVotesBundlePacketSize) { - throw InvalidRlpItemsCountException(packet_data.type_str_, items, kVotesBundlePacketSize); + if (items != kVotesBundleRlpSize) { + throw InvalidRlpItemsCountException(packet_data.type_str_, items, kVotesBundleRlpSize); } - auto votes_count = packet_data.rlp_[kVotesBundlePacketSize - 1].itemCount(); - if (votes_count == 0 || votes_count > kMaxVotesInBundle) { - throw InvalidRlpItemsCountException(packet_data.type_str_, items, kMaxVotesInBundle); + auto votes_count = packet_data.rlp_[kVotesBundleRlpSize - 1].itemCount(); + if (votes_count == 0 || votes_count > kMaxVotesInBundleRlp) { + throw InvalidRlpItemsCountException(packet_data.type_str_, items, kMaxVotesInBundleRlp); } } @@ -68,37 +69,6 @@ void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::s continue; } - // Votes bundles can contain votes only for 1 specific block hash - if (vote->getBlockHash() != votes_bundle_block_hash) { - // we see different voted value, so bundle is invalid - LOG(log_er_) << "Received votes bundle with unmatched voted values(" << votes_bundle_block_hash << ", " - << vote->getBlockHash() << ") from " << packet_data.from_node_id_ - << ". The peer may be a malicious player, will be disconnected"; - disconnect(packet_data.from_node_id_, dev::p2p::UserReason); - return; - } - - if (vote->getPeriod() != votes_bundle_pbft_period) { - LOG(log_er_) << "Received votes bundle with unmatched periods from " << packet_data.from_node_id_ - << ". The peer may be a malicious player, will be disconnected"; - disconnect(packet_data.from_node_id_, dev::p2p::UserReason); - return; - } - - if (vote->getRound() != votes_bundle_pbft_round) { - LOG(log_er_) << "Received votes bundle with unmatched rounds from " << packet_data.from_node_id_ - << ". The peer may be a malicious player, will be disconnected"; - disconnect(packet_data.from_node_id_, dev::p2p::UserReason); - return; - } - - if (vote->getStep() != votes_bundle_votes_step) { - LOG(log_er_) << "Received votes bundle with unmatched step from " << packet_data.from_node_id_ - << ". The peer may be a malicious player, will be disconnected"; - disconnect(packet_data.from_node_id_, dev::p2p::UserReason); - return; - } - LOG(log_dg_) << "Received sync vote " << vote->getHash().abridged(); // Process processStandardVote is called with false in case of next votes bundle -> does not check max boundaries diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp index c18604ebeb..b1f4ed8fea 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp @@ -24,11 +24,10 @@ void ExtVotesPacketHandler::sendPbftVotesBundle(const std::shared_ptr(kMaxVotesInBundle), votes.size() - index); + const size_t count = std::min(static_cast(kMaxVotesInBundleRlp), votes.size() - index); dev::RLPStream s(count); for (auto i = index; i < index + count; i++) { const auto &vote = votes[i]; - // TODO: special ctor for optimized votes rlp s.appendRaw(vote->rlp(true, false)); LOG(log_dg_) << "Send vote " << vote->getHash() << " to peer " << peer->getId(); } diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_sync_packet_handler.cpp index fe518124c8..d047e81e91 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_sync_packet_handler.cpp @@ -16,8 +16,8 @@ VotesSyncPacketHandler::VotesSyncPacketHandler(const FullNodeConfig &conf, void VotesSyncPacketHandler::validatePacketRlpFormat([[maybe_unused]] const tarcap::PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); - if (items == 0 || items > kMaxVotesInBundle) { - throw InvalidRlpItemsCountException(packet_data.type_str_, items, kMaxVotesInBundle); + if (items == 0 || items > kMaxVotesInBundleRlp) { + throw InvalidRlpItemsCountException(packet_data.type_str_, items, kMaxVotesInBundleRlp); } } diff --git a/libraries/types/pbft_block/src/period_data.cpp b/libraries/types/pbft_block/src/period_data.cpp index 427691eb99..493e26266e 100644 --- a/libraries/types/pbft_block/src/period_data.cpp +++ b/libraries/types/pbft_block/src/period_data.cpp @@ -6,6 +6,7 @@ #include "pbft/pbft_block.hpp" #include "transaction/transaction.hpp" #include "vote/vote.hpp" +#include "vote/votes_bundle_rlp.hpp" namespace taraxa { @@ -18,9 +19,7 @@ PeriodData::PeriodData(std::shared_ptr pbft_blk, PeriodData::PeriodData(dev::RLP&& rlp) { auto it = rlp.begin(); pbft_blk = std::make_shared(*it++); - for (auto const vote_rlp : *it++) { - previous_block_cert_votes.emplace_back(std::make_shared(vote_rlp)); - } + previous_block_cert_votes = decodeVotesBundleRlp(*it++); for (auto const dag_block_rlp : *it++) { dag_blocks.emplace_back(dag_block_rlp); @@ -36,18 +35,18 @@ PeriodData::PeriodData(bytes const& all_rlp) : PeriodData(dev::RLP(all_rlp)) {} bytes PeriodData::rlp() const { dev::RLPStream s(kRlpItemCount); s.appendRaw(pbft_blk->rlp(true)); - s.appendList(previous_block_cert_votes.size()); - for (auto const& v : previous_block_cert_votes) { - s.appendRaw(v->rlp(true)); - } + s.appendRaw(encodeVotesBundleRlp(previous_block_cert_votes, false)); + s.appendList(dag_blocks.size()); for (auto const& b : dag_blocks) { s.appendRaw(b.rlp(true)); } + s.appendList(transactions.size()); for (auto const& t : transactions) { s.appendRaw(t->rlp()); } + return s.invalidate(); } diff --git a/libraries/types/vote/CMakeLists.txt b/libraries/types/vote/CMakeLists.txt index 63bf47915a..3fdc49c18e 100644 --- a/libraries/types/vote/CMakeLists.txt +++ b/libraries/types/vote/CMakeLists.txt @@ -1,5 +1,5 @@ -set(HEADERS include/vote/vote.hpp include/vote/vrf_sortition.hpp include/vote/votes_bundle.hpp) -set(SOURCES src/vote.cpp src/vrf_sortition.cpp src/votes_bundle.cpp) +set(HEADERS include/vote/vote.hpp include/vote/vrf_sortition.hpp include/vote/votes_bundle_rlp.hpp) +set(SOURCES src/vote.cpp src/vrf_sortition.cpp src/votes_bundle_rlp.cpp) add_library(vote ${SOURCES} ${HEADERS}) target_include_directories(vote PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/libraries/types/vote/include/vote/votes_bundle.hpp b/libraries/types/vote/include/vote/votes_bundle.hpp deleted file mode 100644 index 751ae969c8..0000000000 --- a/libraries/types/vote/include/vote/votes_bundle.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -#include "common/types.hpp" -#include "vote.hpp" - -namespace taraxa { - -/** @addtogroup Vote - * @{ - */ - -/** - * @brief VotesBundle stores >= 2t+1 votes with same voted hash, period, round and step. It is used for iptimized - * rlp storage so we dont save these duplicate data for each vote. - */ -class VotesBundle { - public: - VotesBundle(const std::vector>& votes, bool validate_common_data = true); - // Ctor for VotesBundle syncing packets rlp - VotesBundle(const dev::RLP& rlp); - // Ctor for PeriodData rlp - it does not contain block_hash, period and step(certify) as those are already stored in - // Pbft block rlp - VotesBundle(const blk_hash_t& block_hash, PbftPeriod period, PbftRound round, PbftStep step, const dev::RLP& rlp); - - bytes rlp() const; - - private: - // Common votes block hash - blk_hash_t block_hash_; - // Common votes period - PbftPeriod period_{0}; - // Common votes round - PbftRound round_{0}; - // Common votes step - PbftStep step_{0}; - // >= 2t+1 votes - std::vector> votes_; -}; - -/** @}*/ - -} // namespace taraxa diff --git a/libraries/types/vote/include/vote/votes_bundle_rlp.hpp b/libraries/types/vote/include/vote/votes_bundle_rlp.hpp new file mode 100644 index 0000000000..152cf2d3f8 --- /dev/null +++ b/libraries/types/vote/include/vote/votes_bundle_rlp.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include +#include + +#include + +namespace taraxa { + +class Vote; + +/** @addtogroup Vote + * @{ + */ + +constexpr static size_t kVotesBundleRlpSize{5}; + +/** + * @brief Encodes votes into optimized votes bundle rlp + * + * @param votes + * @param validate_common_data validate if all votes have the same block_hash, period, round and step + * @return votes bundle rlp bytes + */ +dev::bytes encodeVotesBundleRlp(const std::vector>& votes, bool validate_common_data); + +/** + * @brief Decodes votes from optimized votes bundle rlp + * + * @param votes_bundle_rlp + * @return votes + */ +std::vector> decodeVotesBundleRlp(const dev::RLP& votes_bundle_rlp); + +/** @}*/ + +} // namespace taraxa diff --git a/libraries/types/vote/src/votes_bundle.cpp b/libraries/types/vote/src/votes_bundle.cpp deleted file mode 100644 index 44573fcb8d..0000000000 --- a/libraries/types/vote/src/votes_bundle.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "vote/votes_bundle.hpp" - -namespace taraxa { - -// TODO: maybe && votes ??? -VotesBundle::VotesBundle(const std::vector>& votes, bool validate_common_data) : votes_(votes) { - if (votes_.empty()) { - throw std::runtime_error("Cannot initialize VotesBundle without any votes"); - } - const auto& reference_vote = votes_.back(); - block_hash_ = reference_vote->getBlockHash(); - period_ = reference_vote->getPeriod(); - round_ = reference_vote->getRound(); - step_ = reference_vote->getStep(); - - if (validate_common_data) { - for (const auto& vote : votes_) { - if (vote->getBlockHash() != block_hash_) { - throw std::runtime_error("VotesBundle: Wrong block hash"); - } - if (vote->getPeriod() != period_) { - throw std::runtime_error("VotesBundle: Wrong period"); - } - if (vote->getRound() != round_) { - throw std::runtime_error("VotesBundle: Wrong round"); - } - if (vote->getStep() != step_) { - throw std::runtime_error("VotesBundle: step"); - } - } - } -} - -// bytes VotesBundle::rlp() const { -// // Pseudo code -// dev::RLPStream s(5); -// -// s << block_hash_; -// s << period_; -// s << round_; -// s << step_; -// -// s.appendList(votes_.size()); -// for (const auto& vote : votes_) { -// // Get optimizes vote rlp with only signature & and proof -// // s << optimized_vote_rlp -// } -// -// return s.invalidate(); -// } - -} // namespace taraxa \ No newline at end of file diff --git a/libraries/types/vote/src/votes_bundle_rlp.cpp b/libraries/types/vote/src/votes_bundle_rlp.cpp new file mode 100644 index 0000000000..0026cd6d41 --- /dev/null +++ b/libraries/types/vote/src/votes_bundle_rlp.cpp @@ -0,0 +1,70 @@ +#include "vote/votes_bundle_rlp.hpp" + +#include "vote/vote.hpp" + +namespace taraxa { + +dev::bytes encodeVotesBundleRlp(const std::vector>& votes, bool validate_common_data) { + if (votes.empty()) { + // TODO: either remove logs or solve it in different way + // LOG(log_er_) << "Cannot create VotesBundle rlp: no votes provided"; + assert(false); + return {}; + } + + // const auto& reference_vote = votes.back(); + // blk_hash_t block_hash = reference_vote->getBlockHash(); + // PbftPeriod period = reference_vote->getPeriod(); + // PbftRound round = reference_vote->getRound(); + // PbftStep step = reference_vote->getStep(); + + const auto& reference_block_hash = votes.back()->getBlockHash(); + const auto reference_period = votes.back()->getPeriod(); + const auto reference_round = votes.back()->getRound(); + const auto reference_step = votes.back()->getStep(); + + dev::RLPStream votes_bundle_rlp(kVotesBundleRlpSize); + votes_bundle_rlp.append(reference_block_hash); + votes_bundle_rlp.append(reference_period); + votes_bundle_rlp.append(reference_round); + votes_bundle_rlp.append(reference_step); + votes_bundle_rlp.appendList(votes.size()); + + for (const auto& vote : votes) { + if (validate_common_data && + (vote->getBlockHash() != reference_block_hash || vote->getPeriod() != reference_period || + vote->getRound() != reference_round || vote->getStep() != reference_step)) { + // LOG(log_er_) << "Cannot create VotesBundle rlp: invalid vote " << vote->getHash() << " (voted_block, + // period, round, step)->(" + // << vote->getBlockHash() << ", " << vote->getPeriod() << ", " << vote->getRound() << ", " + // << vote->getStep() << ") != reference vote (voted_block, period, round, step)->" + // << reference_block_hash << ", " << reference_period << ", " << reference_round << ", " + // << reference_step << ")"; + assert(false); + return {}; + } + + votes_bundle_rlp.appendRaw(vote->optimizedRlp()); + } + + return votes_bundle_rlp.invalidate(); +} + +std::vector> decodeVotesBundleRlp(const dev::RLP& votes_bundle_rlp) { + const blk_hash_t votes_bundle_block_hash = votes_bundle_rlp[0].toHash(); + const PbftPeriod votes_bundle_pbft_period = votes_bundle_rlp[1].toInt(); + const PbftRound votes_bundle_pbft_round = votes_bundle_rlp[2].toInt(); + const PbftStep votes_bundle_votes_step = votes_bundle_rlp[3].toInt(); + + std::vector> votes; + for (const auto vote_rlp : votes_bundle_rlp[4]) { + auto vote = std::make_shared(votes_bundle_block_hash, votes_bundle_pbft_period, votes_bundle_pbft_round, + votes_bundle_votes_step, vote_rlp); + // TODO: verify vote signature / hash ??? + votes.push_back(std::move(vote)); + } + + return votes; +} + +} // namespace taraxa \ No newline at end of file From 381b04ffaf841f000c16caaf2e6eb21724ae5fd6 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Mon, 17 Apr 2023 20:59:37 -0700 Subject: [PATCH 027/134] fix tests --- .../get_pbft_sync_packet_handler.cpp | 3 +- .../votes_sync_packet_handler.cpp | 14 ++++---- libraries/core_libs/storage/src/storage.cpp | 14 +++----- .../pbft_block/include/pbft/period_data.hpp | 2 +- .../types/pbft_block/src/period_data.cpp | 15 +++++++-- libraries/types/vote/src/votes_bundle_rlp.cpp | 8 ++--- tests/final_chain_test.cpp | 4 +++ tests/full_node_test.cpp | 32 +++++-------------- tests/network_test.cpp | 26 ++++++++------- tests/sortition_test.cpp | 6 ++++ .../test_util/include/test_util/test_util.hpp | 3 ++ tests/test_util/src/test_util.cpp | 15 +++++++++ 12 files changed, 79 insertions(+), 63 deletions(-) diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp index 82a590f4f5..299eee79ae 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp @@ -80,10 +80,11 @@ void GetPbftSyncPacketHandler::sendPbftBlocks(const std::shared_ptr } dev::RLPStream s; - if (pbft_chain_synced && last_block) { + if (pbft_chain_synced && last_block && block_period > 1) { s.appendList(3); s << last_block; s.appendRaw(data); + // Latest finalized block cert votes are saved in db as reward votes for new blocks const auto reward_votes = vote_mgr_->getRewardVotes(); assert(!reward_votes.empty()); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp index 8f1f8c57c9..96cb281a3c 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp @@ -57,6 +57,13 @@ void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::s return; } + // Process processStandardVote is called with false in case of next votes bundle -> does not check max boundaries + // for round and step to actually being able to sync the current round in case network is stalled + bool check_max_round_step = true; + if (votes_bundle_votes_type == PbftVoteTypes::cert_vote || votes_bundle_votes_type == PbftVoteTypes::next_vote) { + check_max_round_step = false; + } + std::vector> votes; for (const auto vote_rlp : packet_data.rlp_[4]) { auto vote = std::make_shared(votes_bundle_block_hash, votes_bundle_pbft_period, votes_bundle_pbft_round, @@ -71,13 +78,6 @@ void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::s LOG(log_dg_) << "Received sync vote " << vote->getHash().abridged(); - // Process processStandardVote is called with false in case of next votes bundle -> does not check max boundaries - // for round and step to actually being able to sync the current round in case network is stalled - bool check_max_round_step = true; - if (votes_bundle_votes_type == PbftVoteTypes::cert_vote || votes_bundle_votes_type == PbftVoteTypes::next_vote) { - check_max_round_step = false; - } - if (!processVote(vote, nullptr, peer, check_max_round_step)) { continue; } diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 6f3beff3bd..a102dbafb0 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -11,6 +11,7 @@ #include "rocksdb/utilities/checkpoint.h" #include "storage/uint_comparator.hpp" #include "vote/vote.hpp" +#include "vote/votes_bundle_rlp.hpp" namespace taraxa { namespace fs = std::filesystem; @@ -689,18 +690,13 @@ std::pair, trx_hash_t> DbStorage::getFinalized } std::vector> DbStorage::getPeriodCertVotes(PbftPeriod period) const { - std::vector> cert_votes; auto period_data = getPeriodDataRaw(period); - if (period_data.size() > 0) { - auto period_data_rlp = dev::RLP(period_data); - auto cert_votes_data = period_data_rlp[CERT_VOTES_POS_IN_PERIOD_DATA]; - cert_votes.reserve(cert_votes_data.size()); - for (auto const vote : cert_votes_data) { - cert_votes.emplace_back(std::make_shared(vote)); - } + if (period_data.empty()) { + return {}; } - return cert_votes; + auto period_data_rlp = dev::RLP(period_data); + return decodeVotesBundleRlp(period_data_rlp[CERT_VOTES_POS_IN_PERIOD_DATA]); } std::optional DbStorage::getPeriodTransactions(PbftPeriod period) const { diff --git a/libraries/types/pbft_block/include/pbft/period_data.hpp b/libraries/types/pbft_block/include/pbft/period_data.hpp index 31a95dd125..6ce782765f 100644 --- a/libraries/types/pbft_block/include/pbft/period_data.hpp +++ b/libraries/types/pbft_block/include/pbft/period_data.hpp @@ -24,7 +24,7 @@ class PbftBlock; class PeriodData { public: PeriodData() = default; - PeriodData(std::shared_ptr pbft_blk, std::vector> const& cert_votes); + PeriodData(std::shared_ptr pbft_blk, const std::vector>& previous_block_cert_votes); explicit PeriodData(dev::RLP&& all_rlp); explicit PeriodData(bytes const& all_rlp); diff --git a/libraries/types/pbft_block/src/period_data.cpp b/libraries/types/pbft_block/src/period_data.cpp index 493e26266e..3850f8b7e2 100644 --- a/libraries/types/pbft_block/src/period_data.cpp +++ b/libraries/types/pbft_block/src/period_data.cpp @@ -13,13 +13,17 @@ namespace taraxa { using namespace std; PeriodData::PeriodData(std::shared_ptr pbft_blk, - std::vector> const& previous_block_cert_votes) + const std::vector>& previous_block_cert_votes) : pbft_blk(std::move(pbft_blk)), previous_block_cert_votes(previous_block_cert_votes) {} PeriodData::PeriodData(dev::RLP&& rlp) { auto it = rlp.begin(); pbft_blk = std::make_shared(*it++); - previous_block_cert_votes = decodeVotesBundleRlp(*it++); + + const auto votes_bundle_rlp = *it++; + if (pbft_blk->getPeriod() > 1) [[likely]] { + previous_block_cert_votes = decodeVotesBundleRlp(votes_bundle_rlp); + } for (auto const dag_block_rlp : *it++) { dag_blocks.emplace_back(dag_block_rlp); @@ -35,7 +39,12 @@ PeriodData::PeriodData(bytes const& all_rlp) : PeriodData(dev::RLP(all_rlp)) {} bytes PeriodData::rlp() const { dev::RLPStream s(kRlpItemCount); s.appendRaw(pbft_blk->rlp(true)); - s.appendRaw(encodeVotesBundleRlp(previous_block_cert_votes, false)); + + if (pbft_blk->getPeriod() > 1) [[likely]] { + s.appendRaw(encodeVotesBundleRlp(previous_block_cert_votes, false)); + } else { + s.append(""); + } s.appendList(dag_blocks.size()); for (auto const& b : dag_blocks) { diff --git a/libraries/types/vote/src/votes_bundle_rlp.cpp b/libraries/types/vote/src/votes_bundle_rlp.cpp index 0026cd6d41..2745e9de14 100644 --- a/libraries/types/vote/src/votes_bundle_rlp.cpp +++ b/libraries/types/vote/src/votes_bundle_rlp.cpp @@ -12,12 +12,6 @@ dev::bytes encodeVotesBundleRlp(const std::vector>& votes, return {}; } - // const auto& reference_vote = votes.back(); - // blk_hash_t block_hash = reference_vote->getBlockHash(); - // PbftPeriod period = reference_vote->getPeriod(); - // PbftRound round = reference_vote->getRound(); - // PbftStep step = reference_vote->getStep(); - const auto& reference_block_hash = votes.back()->getBlockHash(); const auto reference_period = votes.back()->getPeriod(); const auto reference_round = votes.back()->getRound(); @@ -57,6 +51,8 @@ std::vector> decodeVotesBundleRlp(const dev::RLP& votes_bu const PbftStep votes_bundle_votes_step = votes_bundle_rlp[3].toInt(); std::vector> votes; + votes.reserve(votes_bundle_rlp[4].itemCount()); + for (const auto vote_rlp : votes_bundle_rlp[4]) { auto vote = std::make_shared(votes_bundle_block_hash, votes_bundle_pbft_period, votes_bundle_pbft_round, votes_bundle_votes_step, vote_rlp); diff --git a/tests/final_chain_test.cpp b/tests/final_chain_test.cpp index 781223a14b..f9b89005f7 100644 --- a/tests/final_chain_test.cpp +++ b/tests/final_chain_test.cpp @@ -62,6 +62,10 @@ struct FinalChainTest : WithDataDir { PeriodData period_data(pbft_block, votes); period_data.dag_blocks.push_back(dag_blk); period_data.transactions = trxs; + if (pbft_block->getPeriod() > 1) { + period_data.previous_block_cert_votes = { + genDummyVote(PbftVoteTypes::cert_vote, pbft_block->getPeriod() - 1, 1, 3, pbft_block->getBlockHash())}; + } auto batch = db->createWriteBatch(); db->savePeriodData(period_data, batch); diff --git a/tests/full_node_test.cpp b/tests/full_node_test.cpp index 132bb33ae0..730e79bcb3 100644 --- a/tests/full_node_test.cpp +++ b/tests/full_node_test.cpp @@ -155,23 +155,16 @@ TEST_F(FullNodeTest, db_test) { // Certified votes std::vector> cert_votes; for (auto i = 0; i < 3; i++) { - VrfPbftMsg msg(PbftVoteTypes::cert_vote, 2, 2, 3); - vrf_wrapper::vrf_sk_t vrf_sk( - "0b6627a6680e01cea3d9f36fa797f7f34e8869c3a526d9ed63ed8170e35542aad05dc12c" - "1df1edc9f3367fba550b7971fc2de6c5998d8784051c5be69abc9644"); - VrfPbftSortition vrf_sortition(vrf_sk, msg); - Vote vote(g_secret, vrf_sortition, blk_hash_t(10)); - cert_votes.emplace_back(std::make_shared(vote)); + cert_votes.emplace_back(genDummyVote(PbftVoteTypes::cert_vote, 2, 2, 3, blk_hash_t(1))); } - batch = db.createWriteBatch(); - std::vector> votes; - + std::vector> votes{genDummyVote(PbftVoteTypes::cert_vote, 1, 1, 3, blk_hash_t(1))}; PeriodData period_data1(pbft_block1, cert_votes); PeriodData period_data2(pbft_block2, votes); PeriodData period_data3(pbft_block3, votes); PeriodData period_data4(pbft_block4, votes); + batch = db.createWriteBatch(); db.savePeriodData(period_data1, batch); db.savePeriodData(period_data2, batch); db.savePeriodData(period_data3, batch); @@ -218,20 +211,11 @@ TEST_F(FullNodeTest, db_test) { EXPECT_EQ(db.getStatusField(StatusDbField::ExecutedBlkCount), 10); EXPECT_EQ(db.getStatusField(StatusDbField::ExecutedTrxCount), 20); - auto genVote = [](PbftVoteTypes type, PbftPeriod period, PbftRound round, PbftStep step) { - VrfPbftMsg msg(type, period, round, step); - vrf_wrapper::vrf_sk_t vrf_sk( - "0b6627a6680e01cea3d9f36fa797f7f34e8869c3a526d9ed63ed8170e35542aad05dc12c" - "1df1edc9f3367fba550b7971fc2de6c5998d8784051c5be69abc9644"); - VrfPbftSortition vrf_sortition(vrf_sk, msg); - return std::make_shared(g_secret, vrf_sortition, blk_hash_t(1)); - }; - // Own verified votes EXPECT_TRUE(db.getOwnVerifiedVotes().empty()); std::vector> verified_votes; for (auto i = 0; i < 3; i++) { - const auto vote = genVote(PbftVoteTypes::soft_vote, i, i, 2); + const auto vote = genDummyVote(PbftVoteTypes::soft_vote, i, i, 2); verified_votes.push_back(vote); db.saveOwnVerifiedVote(vote); } @@ -256,15 +240,15 @@ TEST_F(FullNodeTest, db_test) { } // Save new votes for different TwoTPlusOneVotedBlockType - const auto cert_vote = genVote(PbftVoteTypes::cert_vote, 1, 1, 3); + const auto cert_vote = genDummyVote(PbftVoteTypes::cert_vote, 1, 1, 3); verified_votes.push_back(cert_vote); db.replaceTwoTPlusOneVotes(TwoTPlusOneVotedBlockType::CertVotedBlock, {cert_vote}); - const auto next_vote = genVote(PbftVoteTypes::next_vote, 1, 1, 4); + const auto next_vote = genDummyVote(PbftVoteTypes::next_vote, 1, 1, 4); verified_votes.push_back(next_vote); db.replaceTwoTPlusOneVotes(TwoTPlusOneVotedBlockType::NextVotedBlock, {next_vote}); - const auto next_null_vote = genVote(PbftVoteTypes::next_vote, 1, 1, 5); + const auto next_null_vote = genDummyVote(PbftVoteTypes::next_vote, 1, 1, 5); verified_votes.push_back(next_null_vote); db.replaceTwoTPlusOneVotes(TwoTPlusOneVotedBlockType::NextVotedNullBlock, {next_null_vote}); EXPECT_EQ(db.getAllTwoTPlusOneVotes().size(), verified_votes.size()); @@ -293,7 +277,7 @@ TEST_F(FullNodeTest, db_test) { EXPECT_EQ(db_vote->rlp(true, true), verified_votes_map[db_vote->getHash()]->rlp(true, true)); } - const auto new_reward_vote = genVote(PbftVoteTypes::cert_vote, 10, 10, 3); + const auto new_reward_vote = genDummyVote(PbftVoteTypes::cert_vote, 10, 10, 3); verified_votes_map[new_reward_vote->getHash()] = new_reward_vote; db.saveExtraRewardVote(new_reward_vote); diff --git a/tests/network_test.cpp b/tests/network_test.cpp index 970ed0ac33..ce889ec51d 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -699,13 +699,12 @@ TEST_F(NetworkTest, node_pbft_sync_without_enough_votes) { PbftBlock pbft_block1(prev_block_hash, blk1.getHash(), dev::sha3(order_stream.out()), kNullBlockHash, period, beneficiary, node1->getSecretKey(), {}); - std::vector> votes_for_pbft_blk1; - votes_for_pbft_blk1.emplace_back( - node1->getVoteManager()->generateVote(pbft_block1.getBlockHash(), PbftVoteTypes::cert_vote, 1, 1, 3)); - std::cout << "Generate 1 vote for first PBFT block" << std::endl; - // Add cert votes in DB - // Add PBFT block in DB + const auto pbft_block1_cert_vote = node1->getVoteManager()->generateVote( + pbft_block1.getBlockHash(), PbftVoteTypes::cert_vote, pbft_block1.getPeriod(), 1, 3); + pbft_block1_cert_vote->calculateWeight(1, 1, 1); + node1->getVoteManager()->addVerifiedVote(pbft_block1_cert_vote); + // Add PBFT block in DB PeriodData period_data1(std::make_shared(pbft_block1), {}); period_data1.dag_blocks.push_back(blk1); period_data1.transactions.push_back(g_signed_trx_samples[0]); @@ -747,13 +746,12 @@ TEST_F(NetworkTest, node_pbft_sync_without_enough_votes) { PbftBlock pbft_block2(prev_block_hash, blk2.getHash(), dev::sha3(order_stream2.out()), kNullBlockHash, period, beneficiary, node1->getSecretKey(), {}); - std::cout << "Use fake votes for the second PBFT block" << std::endl; - // TODO: how can these fake votes be somehow properly handled when we save it directly to db ??? - // node1 put block2 into pbft chain and use fake votes storing into DB (malicious player) - // Add fake votes in DB - // Add PBFT block in DB + const auto pbft_block2_cert_vote = node1->getVoteManager()->generateVote( + pbft_block2.getBlockHash(), PbftVoteTypes::cert_vote, pbft_block2.getPeriod(), 1, 3); + pbft_block2_cert_vote->calculateWeight(1, 1, 1); + node1->getVoteManager()->addVerifiedVote(pbft_block2_cert_vote); - PeriodData period_data2(std::make_shared(pbft_block2), votes_for_pbft_blk1); + PeriodData period_data2(std::make_shared(pbft_block2), {pbft_block1_cert_vote}); period_data2.dag_blocks.push_back(blk2); period_data2.transactions.push_back(g_signed_trx_samples[2]); period_data2.transactions.push_back(g_signed_trx_samples[3]); @@ -765,7 +763,11 @@ TEST_F(NetworkTest, node_pbft_sync_without_enough_votes) { pbft_chain_head_hash = pbft_chain1->getHeadHash(); pbft_chain_head_str = pbft_chain1->getJsonStr(); db1->addPbftHeadToBatch(pbft_chain_head_hash, pbft_chain_head_str, batch); + + node1->getVoteManager()->resetRewardVotes(pbft_block2.getPeriod(), 1, 3, pbft_block2.getBlockHash(), batch); + db1->commitWriteBatch(batch); + expect_pbft_chain_size = 2; EXPECT_EQ(node1->getPbftChain()->getPbftChainSize(), expect_pbft_chain_size); diff --git a/tests/sortition_test.cpp b/tests/sortition_test.cpp index e85233b4d1..d4c79419de 100644 --- a/tests/sortition_test.cpp +++ b/tests/sortition_test.cpp @@ -27,6 +27,12 @@ PeriodData createBlock(PbftPeriod period, uint16_t efficiency, size_t dag_blocks std::vector reward_votes_hashes; b.pbft_blk = std::make_shared(kNullBlockHash, anchor_hash, kNullBlockHash, kNullBlockHash, period, addr_t(0), dev::KeyPair::create().secret(), std::move(reward_votes_hashes)); + + if (period > 1) { + b.previous_block_cert_votes = { + genDummyVote(PbftVoteTypes::cert_vote, b.pbft_blk->getPeriod() - 1, 1, 3, b.pbft_blk->getBlockHash())}; + } + size_t effective_transactions = kTrxCount * efficiency / (100 * kOnePercent); auto trx_hashes = generateTrxHashes(effective_transactions); auto trx_per_block = effective_transactions / dag_blocks_count; diff --git a/tests/test_util/include/test_util/test_util.hpp b/tests/test_util/include/test_util/test_util.hpp index 951a22dec3..efbb5d17e2 100644 --- a/tests/test_util/include/test_util/test_util.hpp +++ b/tests/test_util/include/test_util/test_util.hpp @@ -181,6 +181,9 @@ void wait_for_balances(const std::vector>& nodes, cons std::shared_ptr genDummyVote(PbftVoteTypes type, PbftPeriod period, PbftRound round, PbftStep step, blk_hash_t block_hash, const std::shared_ptr vote_mgr); +std::shared_ptr genDummyVote(PbftVoteTypes type, PbftPeriod period, PbftRound round, PbftStep step, + blk_hash_t block_hash = blk_hash_t(1)); + std::pair clearAllVotes(const std::vector>& nodes); struct NodesTest : virtual WithDataDir { diff --git a/tests/test_util/src/test_util.cpp b/tests/test_util/src/test_util.cpp index 7c13b18e86..7a09238bbc 100644 --- a/tests/test_util/src/test_util.cpp +++ b/tests/test_util/src/test_util.cpp @@ -5,6 +5,9 @@ namespace taraxa { +auto g_secret = dev::Secret("3800b2875669d9b2053c1aff9224ecfdc411423aac5b5a73d7a45ced1c3b9dcd", + dev::Secret::ConstructFromStringType::FromHex); + bool wait(const wait_opts& opts, const std::function& poller) { struct NullBuffer : std::streambuf { int overflow(int c) override { return c; } @@ -151,6 +154,18 @@ std::shared_ptr genDummyVote(PbftVoteTypes type, PbftPeriod period, PbftRo return vote; } +std::shared_ptr genDummyVote(PbftVoteTypes type, PbftPeriod period, PbftRound round, PbftStep step, + blk_hash_t block_hash) { + VrfPbftMsg msg(type, period, round, step); + vrf_wrapper::vrf_sk_t vrf_sk( + "0b6627a6680e01cea3d9f36fa797f7f34e8869c3a526d9ed63ed8170e35542aad05dc12c" + "1df1edc9f3367fba550b7971fc2de6c5998d8784051c5be69abc9644"); + VrfPbftSortition vrf_sortition(vrf_sk, msg); + auto vote = std::make_shared(g_secret, vrf_sortition, block_hash); + vote->calculateWeight(1, 1, 1); + return vote; +} + std::pair clearAllVotes(const std::vector>& nodes) { // Get highest round from all nodes PbftPeriod max_period = 0; From 3006b34364ce04551ff31f8293714743282ae968 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Tue, 18 Apr 2023 10:47:20 -0700 Subject: [PATCH 028/134] chore: send v1 period data rlp in v1 sync packet handler --- .../get_pbft_sync_packet_handler.cpp | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp index b3b3b89212..476742c797 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp @@ -78,12 +78,35 @@ void OmgGetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id assert(false); } + // Transform period data rlp from v2 to v1 + auto transformPeriodDataRlpToV1 = [](const dev::bytes &period_data_v2) -> dev::bytes { + // Create PeriodData old(v1) rlp format + PeriodData period_data(period_data_v2); + + dev::RLPStream period_data_rlp(PeriodData::kRlpItemCount); + period_data_rlp.appendRaw(period_data.pbft_blk->rlp(true)); + period_data_rlp.appendList(period_data.previous_block_cert_votes.size()); + for (auto const &v : period_data.previous_block_cert_votes) { + period_data_rlp.appendRaw(v->rlp(true)); + } + period_data_rlp.appendList(period_data.dag_blocks.size()); + for (auto const &b : period_data.dag_blocks) { + period_data_rlp.appendRaw(b.rlp(true)); + } + period_data_rlp.appendList(period_data.transactions.size()); + for (auto const &t : period_data.transactions) { + period_data_rlp.appendRaw(t->rlp()); + } + + return period_data_rlp.invalidate(); + }; + dev::RLPStream s; - if (pbft_chain_synced && last_block) { + if (pbft_chain_synced && last_block && block_period > 1) { s.appendList(3); s << last_block; - // TODO: once PeriodData db migration is done, parse PeriodData and create old rlp structure - s.appendRaw(data); + s.appendRaw(transformPeriodDataRlpToV1(data)); + // Latest finalized block cert votes are saved in db as reward votes for new blocks const auto votes = vote_mgr_->getRewardVotes(); s.appendList(votes.size()); @@ -93,8 +116,7 @@ void OmgGetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id } else { s.appendList(2); s << last_block; - // TODO: once PeriodData db migration is done, parse PeriodData and create old rlp structure - s.appendRaw(data); + s.appendRaw(transformPeriodDataRlpToV1(data)); } LOG(log_dg_) << "Sending PbftSyncPacket period " << block_period << " to " << peer_id; sealAndSend(peer_id, tarcap::SubprotocolPacketType::PbftSyncPacket, std::move(s)); From 27c473179bca6c485ba27a79a3ef009a7dd66eb4 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Tue, 18 Apr 2023 13:12:40 -0700 Subject: [PATCH 029/134] chore: add period data db migration --- CMakeLists.txt | 2 +- .../include/storage/migration/period_data.hpp | 15 ++++++++ .../src/migration/migration_manager.cpp | 2 + .../storage/src/migration/period_data.cpp | 37 +++++++++++++++++++ libraries/types/vote/src/votes_bundle_rlp.cpp | 11 +----- 5 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 libraries/core_libs/storage/include/storage/migration/period_data.hpp create mode 100644 libraries/core_libs/storage/src/migration/period_data.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 385620d726..9a3ed884d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ set(TARAXA_VERSION ${TARAXA_MAJOR_VERSION}.${TARAXA_MINOR_VERSION}.${TARAXA_PATC set(TARAXA_NET_VERSION 2) # Major version is modified when DAG blocks, pbft blocks and any basic building blocks of our blockchain is modified # in the db -set(TARAXA_DB_MAJOR_VERSION 1) +set(TARAXA_DB_MAJOR_VERSION 2) # Minor version should be modified when changes to the database are made in the tables that can be rebuilt from the # basic tables set(TARAXA_DB_MINOR_VERSION 1) diff --git a/libraries/core_libs/storage/include/storage/migration/period_data.hpp b/libraries/core_libs/storage/include/storage/migration/period_data.hpp new file mode 100644 index 0000000000..71052a6ea1 --- /dev/null +++ b/libraries/core_libs/storage/include/storage/migration/period_data.hpp @@ -0,0 +1,15 @@ +#pragma once +#include + +#include "final_chain/final_chain.hpp" +#include "storage/migration/migration_base.hpp" + +namespace taraxa::storage::migration { +class PeriodData : public migration::Base { + public: + PeriodData(std::shared_ptr db); + std::string id() override; + uint32_t dbVersion() override; + void migrate() override; +}; +} // namespace taraxa::storage::migration \ No newline at end of file diff --git a/libraries/core_libs/storage/src/migration/migration_manager.cpp b/libraries/core_libs/storage/src/migration/migration_manager.cpp index 7f5dabd999..26e33c6290 100644 --- a/libraries/core_libs/storage/src/migration/migration_manager.cpp +++ b/libraries/core_libs/storage/src/migration/migration_manager.cpp @@ -1,11 +1,13 @@ #include "storage/migration/migration_manager.hpp" +#include "storage/migration/period_data.hpp" #include "storage/migration/transaction_hashes.hpp" namespace taraxa::storage::migration { Manager::Manager(std::shared_ptr db, const addr_t& node_addr) : db_(db) { LOG_OBJECTS_CREATE("MIGRATIONS"); registerMigration(); + registerMigration(); } void Manager::applyAll() { diff --git a/libraries/core_libs/storage/src/migration/period_data.cpp b/libraries/core_libs/storage/src/migration/period_data.cpp new file mode 100644 index 0000000000..361e990296 --- /dev/null +++ b/libraries/core_libs/storage/src/migration/period_data.cpp @@ -0,0 +1,37 @@ +#include "storage/migration/period_data.hpp" + +namespace taraxa::storage::migration { + +PeriodData::PeriodData(std::shared_ptr db) : migration::Base(db) {} + +std::string PeriodData::id() { return "PeriodData"; } + +uint32_t PeriodData::dbVersion() { return 2; } + +void PeriodData::migrate() { + auto it = db_->getColumnIterator(DB::Columns::period_data); + + // Get and save data in new format for all blocks + for (it->SeekToFirst(); it->Valid(); it->Next()) { + const auto period_data_old_rlp = dev::RLP(it->value().ToString()); + assert(period_data_old_rlp.itemCount() == 4); + + ::taraxa::PeriodData period_data; + period_data.pbft_blk = std::make_shared(period_data_old_rlp[0]); + for (auto const vote_rlp : period_data_old_rlp[1]) { + period_data.previous_block_cert_votes.emplace_back(std::make_shared(vote_rlp)); + } + + for (auto const dag_block_rlp : period_data_old_rlp[2]) { + period_data.dag_blocks.emplace_back(dag_block_rlp); + } + + for (auto const trx_rlp : period_data_old_rlp[3]) { + period_data.transactions.emplace_back(std::make_shared(trx_rlp)); + } + + const auto period_data_new_rlp = period_data.rlp(); + db_->insert(batch_, DB::Columns::period_data, it->key(), period_data_new_rlp); + } +} +} // namespace taraxa::storage::migration \ No newline at end of file diff --git a/libraries/types/vote/src/votes_bundle_rlp.cpp b/libraries/types/vote/src/votes_bundle_rlp.cpp index 2745e9de14..1094ddd64d 100644 --- a/libraries/types/vote/src/votes_bundle_rlp.cpp +++ b/libraries/types/vote/src/votes_bundle_rlp.cpp @@ -6,8 +6,6 @@ namespace taraxa { dev::bytes encodeVotesBundleRlp(const std::vector>& votes, bool validate_common_data) { if (votes.empty()) { - // TODO: either remove logs or solve it in different way - // LOG(log_er_) << "Cannot create VotesBundle rlp: no votes provided"; assert(false); return {}; } @@ -28,12 +26,6 @@ dev::bytes encodeVotesBundleRlp(const std::vector>& votes, if (validate_common_data && (vote->getBlockHash() != reference_block_hash || vote->getPeriod() != reference_period || vote->getRound() != reference_round || vote->getStep() != reference_step)) { - // LOG(log_er_) << "Cannot create VotesBundle rlp: invalid vote " << vote->getHash() << " (voted_block, - // period, round, step)->(" - // << vote->getBlockHash() << ", " << vote->getPeriod() << ", " << vote->getRound() << ", " - // << vote->getStep() << ") != reference vote (voted_block, period, round, step)->" - // << reference_block_hash << ", " << reference_period << ", " << reference_round << ", " - // << reference_step << ")"; assert(false); return {}; } @@ -45,6 +37,8 @@ dev::bytes encodeVotesBundleRlp(const std::vector>& votes, } std::vector> decodeVotesBundleRlp(const dev::RLP& votes_bundle_rlp) { + assert(votes_bundle_rlp.itemCount() == kVotesBundleRlpSize); + const blk_hash_t votes_bundle_block_hash = votes_bundle_rlp[0].toHash(); const PbftPeriod votes_bundle_pbft_period = votes_bundle_rlp[1].toInt(); const PbftRound votes_bundle_pbft_round = votes_bundle_rlp[2].toInt(); @@ -56,7 +50,6 @@ std::vector> decodeVotesBundleRlp(const dev::RLP& votes_bu for (const auto vote_rlp : votes_bundle_rlp[4]) { auto vote = std::make_shared(votes_bundle_block_hash, votes_bundle_pbft_period, votes_bundle_pbft_round, votes_bundle_votes_step, vote_rlp); - // TODO: verify vote signature / hash ??? votes.push_back(std::move(vote)); } From d9cc20804f2b04a791cef37d331f801f4ecec797 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Tue, 18 Apr 2023 15:10:24 -0700 Subject: [PATCH 030/134] chore: minor refactor --- .../consensus/src/pbft/pbft_manager.cpp | 10 +++---- .../src/vote_manager/vote_manager.cpp | 2 +- .../get_next_votes_sync_packet_handler.hpp | 10 +++---- ...er.hpp => votes_bundle_packet_handler.hpp} | 10 +++---- .../get_next_votes_sync_packet_handler.hpp | 10 +++---- .../get_pbft_sync_packet_handler.hpp | 12 ++++----- ...er.hpp => votes_bundle_packet_handler.hpp} | 10 +++---- .../network/v1_tarcap/taraxa_capability.hpp | 26 ------------------- .../get_next_votes_sync_packet_handler.cpp | 6 ++--- ...er.cpp => votes_bundle_packet_handler.cpp} | 20 +++++++------- .../network/src/tarcap/taraxa_capability.cpp | 10 +++---- .../get_next_votes_sync_packet_handler.cpp | 8 +++--- .../get_pbft_sync_packet_handler.cpp | 24 ++++++++--------- ...er.cpp => votes_bundle_packet_handler.cpp} | 24 ++++++++--------- .../src/v1_tarcap/taraxa_capability.cpp | 10 +++---- tests/network_test.cpp | 2 +- tests/tarcap_threadpool_test.cpp | 16 ++++++------ 17 files changed, 92 insertions(+), 118 deletions(-) rename libraries/core_libs/network/include/network/tarcap/packets_handlers/{votes_sync_packet_handler.hpp => votes_bundle_packet_handler.hpp} (68%) rename libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/{votes_sync_packet_handler.hpp => votes_bundle_packet_handler.hpp} (68%) rename libraries/core_libs/network/src/tarcap/packets_handlers/{votes_sync_packet_handler.cpp => votes_bundle_packet_handler.cpp} (81%) rename libraries/core_libs/network/src/v1_tarcap/packet_handlers/{votes_sync_packet_handler.cpp => votes_bundle_packet_handler.cpp} (85%) diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index 0402387eb6..a5600c4e95 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -18,7 +18,7 @@ #include "final_chain/final_chain.hpp" #include "network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp" #include "network/tarcap/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/packets_handlers/votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp" #include "pbft/period_data.hpp" #include "vote_manager/vote_manager.hpp" @@ -565,7 +565,7 @@ void PbftManager::broadcastVotes() { return; } - const auto votes_sync_packet_handler = net->getSpecificHandler(); + const auto votes_sync_packet_handler = net->getSpecificHandler(); // Send votes to the other peers auto gossipVotes = [this, &votes_sync_packet_handler](std::vector> &&votes, @@ -600,7 +600,7 @@ void PbftManager::broadcastVotes() { // Broadcast own votes auto vote_packet_handler = net->getSpecificHandler(); - // TODO: this could be optimized to use VotesSyncPacketHandler if we drop some of the checks in process function + // TODO: this could be optimized to use VotesBundlePacketHandler if we drop some of the checks in process function // Send votes by one as votes sync packet must contain votes with the same type, period and round const auto &own_votes = vote_mgr_->getOwnVerifiedVotes(); for (const auto &vote : own_votes) { @@ -769,8 +769,8 @@ bool PbftManager::genAndPlaceProposeVote(const std::shared_ptr &propo LOG(log_dg_) << "Broadcast propose block reward votes for block " << proposed_block->getBlockHash() << ", num of reward votes: " << reward_votes.size() << ", period " << current_pbft_period << ", round " << current_pbft_round; - net->getSpecificHandler()->onNewPbftVotesBundle(std::move(reward_votes), - false); + net->getSpecificHandler()->onNewPbftVotesBundle(std::move(reward_votes), + false); } if (!placeVote(propose_vote, "propose vote", proposed_block)) { diff --git a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp index 7084be773a..efd383e23e 100644 --- a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp +++ b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp @@ -8,7 +8,7 @@ #include "network/network.hpp" #include "network/tarcap/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/packets_handlers/votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" namespace taraxa { diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp index fd517a329c..ae7aae82de 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp @@ -9,12 +9,12 @@ class VoteManager; namespace taraxa::network::tarcap { -class GetNextVotesSyncPacketHandler final : public ExtVotesPacketHandler { +class GetNextVotesBundlePacketHandler final : public ExtVotesPacketHandler { public: - GetNextVotesSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t& node_addr); + GetNextVotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, const addr_t& node_addr); // Packet type that is processed by this handler static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetNextVotesSyncPacket; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp similarity index 68% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp index 6cb267199f..9e13e7a31f 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp @@ -4,12 +4,12 @@ namespace taraxa::network::tarcap { -class VotesSyncPacketHandler final : public ExtVotesPacketHandler { +class VotesBundlePacketHandler final : public ExtVotesPacketHandler { public: - VotesSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, - std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - const addr_t& node_addr); + VotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, + std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, + const addr_t& node_addr); /** * @brief Sends pbft votes bundle to connected peers diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp index 47f0d569fc..b977283a48 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp @@ -9,12 +9,12 @@ class VoteManager; namespace taraxa::network::v1_tarcap { -class GetNextVotesSyncPacketHandler final : public ExtVotesPacketHandler { +class GetNextVotesBundlePacketHandler final : public ExtVotesPacketHandler { public: - GetNextVotesSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t& node_addr); + GetNextVotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, const addr_t& node_addr); // Packet type that is processed by this handler static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::GetNextVotesSyncPacket; diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp index 09d86ea250..40172320d8 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp @@ -14,13 +14,13 @@ class PbftSyncingState; namespace taraxa::network::v1_tarcap { -class OmgGetPbftSyncPacketHandler final : public tarcap::PacketHandler { +class GetPbftSyncPacketHandler final : public tarcap::PacketHandler { public: - OmgGetPbftSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_syncing_state, - std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - std::shared_ptr db, const addr_t& node_addr); + GetPbftSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_syncing_state, + std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, + std::shared_ptr db, const addr_t& node_addr); void sendPbftBlocks(dev::p2p::NodeID const& peer_id, PbftPeriod from_period, size_t blocks_to_transfer, bool pbft_chain_synced); diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp similarity index 68% rename from libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp index a8f233b309..158827569e 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp @@ -4,12 +4,12 @@ namespace taraxa::network::v1_tarcap { -class VotesSyncPacketHandler final : public ExtVotesPacketHandler { +class VotesBundlePacketHandler final : public ExtVotesPacketHandler { public: - VotesSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t& node_addr); + VotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, const addr_t& node_addr); /** * @brief Sends pbft votes bundle to connected peers diff --git a/libraries/core_libs/network/include/network/v1_tarcap/taraxa_capability.hpp b/libraries/core_libs/network/include/network/v1_tarcap/taraxa_capability.hpp index a7dcb098e7..b54c4db302 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/taraxa_capability.hpp +++ b/libraries/core_libs/network/include/network/v1_tarcap/taraxa_capability.hpp @@ -2,22 +2,8 @@ #include "network/tarcap/taraxa_capability.hpp" -// namespace taraxa { -// class DbStorage; -// class PbftManager; -// class PbftChain; -// class VoteManager; -// class DagManager; -// class TransactionManager; -// enum class TransactionStatus; -// } // namespace taraxa - namespace taraxa::network::v1_tarcap { -// class PacketsHandler; -// class PbftSyncingState; -// class TaraxaPeer; - class TaraxaCapability : public taraxa::network::tarcap::TaraxaCapability { public: TaraxaCapability(std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, @@ -28,18 +14,6 @@ class TaraxaCapability : public taraxa::network::tarcap::TaraxaCapability { TaraxaCapability &operator=(const TaraxaCapability &ro) = delete; TaraxaCapability(TaraxaCapability &&ro) = delete; TaraxaCapability &operator=(TaraxaCapability &&ro) = delete; - // TODO: do we need this ??? - // static std::shared_ptr make( - // std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, const h256 - // &genesis_hash, unsigned version, std::shared_ptr db = {}, std::shared_ptr pbft_mgr = - // {}, std::shared_ptr pbft_chain = {}, std::shared_ptr vote_mgr = {}, - // std::shared_ptr dag_mgr = {}, std::shared_ptr trx_mgr = {}); - // - // // Init capability. Register packet handlers and periodic events - // virtual void init(const h256 &genesis_hash, std::shared_ptr db, std::shared_ptr pbft_mgr, - // std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - // std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, - // const dev::Address &node_addr); protected: virtual void registerPacketHandlers( diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp index 1fb0e0c2bb..c714ff3fd1 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp @@ -5,20 +5,20 @@ namespace taraxa::network::tarcap { -GetNextVotesSyncPacketHandler::GetNextVotesSyncPacketHandler( +GetNextVotesBundlePacketHandler::GetNextVotesBundlePacketHandler( const FullNodeConfig &conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, const addr_t &node_addr) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), std::move(pbft_chain), std::move(vote_mgr), node_addr, "GET_NEXT_VOTES_SYNC_PH") {} -void GetNextVotesSyncPacketHandler::validatePacketRlpFormat(const PacketData &packet_data) const { +void GetNextVotesBundlePacketHandler::validatePacketRlpFormat(const PacketData &packet_data) const { if (constexpr size_t required_size = 2; packet_data.rlp_.itemCount() != required_size) { throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); } } -void GetNextVotesSyncPacketHandler::process(const PacketData &packet_data, const std::shared_ptr &peer) { +void GetNextVotesBundlePacketHandler::process(const PacketData &packet_data, const std::shared_ptr &peer) { LOG(log_dg_) << "Received GetNextVotesSyncPacket request"; const PbftPeriod peer_pbft_period = packet_data.rlp_[0].toInt(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp similarity index 81% rename from libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp index 96cb281a3c..f6eeae06ee 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote/votes_bundle_rlp.hpp" @@ -6,15 +6,15 @@ namespace taraxa::network::tarcap { -VotesSyncPacketHandler::VotesSyncPacketHandler(const FullNodeConfig &conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_mgr, - std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t &node_addr) +VotesBundlePacketHandler::VotesBundlePacketHandler(const FullNodeConfig &conf, std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_mgr, + std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, const addr_t &node_addr) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), std::move(pbft_chain), std::move(vote_mgr), node_addr, "VOTES_SYNC_PH") {} -void VotesSyncPacketHandler::validatePacketRlpFormat([[maybe_unused]] const PacketData &packet_data) const { +void VotesBundlePacketHandler::validatePacketRlpFormat([[maybe_unused]] const PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); if (items != kVotesBundleRlpSize) { throw InvalidRlpItemsCountException(packet_data.type_str_, items, kVotesBundleRlpSize); @@ -26,7 +26,7 @@ void VotesSyncPacketHandler::validatePacketRlpFormat([[maybe_unused]] const Pack } } -void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::shared_ptr &peer) { +void VotesBundlePacketHandler::process(const PacketData &packet_data, const std::shared_ptr &peer) { const auto [current_pbft_round, current_pbft_period] = pbft_mgr_->getPbftRoundAndPeriod(); const auto votes_bundle_block_hash = packet_data.rlp_[0].toHash(); @@ -92,8 +92,8 @@ void VotesSyncPacketHandler::process(const PacketData &packet_data, const std::s onNewPbftVotesBundle(std::move(votes), false, packet_data.from_node_id_); } -void VotesSyncPacketHandler::onNewPbftVotesBundle(std::vector> &&votes, bool rebroadcast, - const std::optional &exclude_node) { +void VotesBundlePacketHandler::onNewPbftVotesBundle(std::vector> &&votes, bool rebroadcast, + const std::optional &exclude_node) { for (const auto &peer : peers_state_->getAllPeers()) { if (peer.second->syncing_) { continue; diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index 2ab501e083..408894b307 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -13,7 +13,7 @@ #include "network/tarcap/packets_handlers/status_packet_handler.hpp" #include "network/tarcap/packets_handlers/transaction_packet_handler.hpp" #include "network/tarcap/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/packets_handlers/votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "network/tarcap/shared_states/test_state.hpp" #include "network/tarcap/stats/node_stats.hpp" @@ -193,10 +193,10 @@ void TaraxaCapability::registerPacketHandlers( // Consensus packets with high processing priority packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, - vote_mgr, node_addr); + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, + pbft_chain, vote_mgr, node_addr); + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, + vote_mgr, node_addr); // Standard packets with mid processing priority packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_syncing_state_, diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_next_votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_next_votes_sync_packet_handler.cpp index d6da4a4a58..3857b4bc8c 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_next_votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_next_votes_sync_packet_handler.cpp @@ -5,21 +5,21 @@ namespace taraxa::network::v1_tarcap { -GetNextVotesSyncPacketHandler::GetNextVotesSyncPacketHandler( +GetNextVotesBundlePacketHandler::GetNextVotesBundlePacketHandler( const FullNodeConfig &conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, const addr_t &node_addr) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), std::move(pbft_chain), std::move(vote_mgr), node_addr, "V1_GET_NEXT_VOTES_SYNC_PH") {} -void GetNextVotesSyncPacketHandler::validatePacketRlpFormat(const tarcap::PacketData &packet_data) const { +void GetNextVotesBundlePacketHandler::validatePacketRlpFormat(const tarcap::PacketData &packet_data) const { if (constexpr size_t required_size = 2; packet_data.rlp_.itemCount() != required_size) { throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); } } -void GetNextVotesSyncPacketHandler::process(const tarcap::PacketData &packet_data, - const std::shared_ptr &peer) { +void GetNextVotesBundlePacketHandler::process(const tarcap::PacketData &packet_data, + const std::shared_ptr &peer) { LOG(log_dg_) << "Received GetNextVotesSyncPacket request"; const PbftPeriod peer_pbft_period = packet_data.rlp_[0].toInt(); diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp index 476742c797..25ecca6314 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp @@ -8,27 +8,27 @@ namespace taraxa::network::v1_tarcap { -OmgGetPbftSyncPacketHandler::OmgGetPbftSyncPacketHandler(const FullNodeConfig &conf, - std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_syncing_state, - std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, - std::shared_ptr db, const addr_t &node_addr) +GetPbftSyncPacketHandler::GetPbftSyncPacketHandler(const FullNodeConfig &conf, + std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_syncing_state, + std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, std::shared_ptr db, + const addr_t &node_addr) : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, "V1_GET_PBFT_SYNC_PH"), pbft_syncing_state_(std::move(pbft_syncing_state)), pbft_chain_(std::move(pbft_chain)), vote_mgr_(std::move(vote_mgr)), db_(std::move(db)) {} -void OmgGetPbftSyncPacketHandler::validatePacketRlpFormat(const tarcap::PacketData &packet_data) const { +void GetPbftSyncPacketHandler::validatePacketRlpFormat(const tarcap::PacketData &packet_data) const { if (constexpr size_t required_size = 1; packet_data.rlp_.itemCount() != required_size) { throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); } } -void OmgGetPbftSyncPacketHandler::process(const tarcap::PacketData &packet_data, - [[maybe_unused]] const std::shared_ptr &peer) { +void GetPbftSyncPacketHandler::process(const tarcap::PacketData &packet_data, + [[maybe_unused]] const std::shared_ptr &peer) { LOG(log_tr_) << "Received GetPbftSyncPacket Block"; const size_t height_to_sync = packet_data.rlp_[0].toInt(); @@ -64,8 +64,8 @@ void OmgGetPbftSyncPacketHandler::process(const tarcap::PacketData &packet_data, } // api for pbft syncing -void OmgGetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, PbftPeriod from_period, - size_t blocks_to_transfer, bool pbft_chain_synced) { +void GetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, PbftPeriod from_period, + size_t blocks_to_transfer, bool pbft_chain_synced) { LOG(log_tr_) << "sendPbftBlocks: peer want to sync from pbft chain height " << from_period << ", will send at most " << blocks_to_transfer << " pbft blocks to " << peer_id; diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp similarity index 85% rename from libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_sync_packet_handler.cpp rename to libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp index d047e81e91..db73e46857 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp @@ -1,28 +1,28 @@ -#include "network/v1_tarcap/packets_handlers/votes_sync_packet_handler.hpp" +#include "network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote_manager/vote_manager.hpp" namespace taraxa::network::v1_tarcap { -VotesSyncPacketHandler::VotesSyncPacketHandler(const FullNodeConfig &conf, - std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_mgr, - std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t &node_addr) +VotesBundlePacketHandler::VotesBundlePacketHandler(const FullNodeConfig &conf, + std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_mgr, + std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, const addr_t &node_addr) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), std::move(pbft_chain), std::move(vote_mgr), node_addr, "V1_VOTES_SYNC_PH") {} -void VotesSyncPacketHandler::validatePacketRlpFormat([[maybe_unused]] const tarcap::PacketData &packet_data) const { +void VotesBundlePacketHandler::validatePacketRlpFormat([[maybe_unused]] const tarcap::PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); if (items == 0 || items > kMaxVotesInBundleRlp) { throw InvalidRlpItemsCountException(packet_data.type_str_, items, kMaxVotesInBundleRlp); } } -void VotesSyncPacketHandler::process(const tarcap::PacketData &packet_data, - const std::shared_ptr &peer) { +void VotesBundlePacketHandler::process(const tarcap::PacketData &packet_data, + const std::shared_ptr &peer) { const auto reference_vote = std::make_shared(packet_data.rlp_[0]); const auto [current_pbft_round, current_pbft_period] = pbft_mgr_->getPbftRoundAndPeriod(); @@ -134,8 +134,8 @@ void VotesSyncPacketHandler::process(const tarcap::PacketData &packet_data, onNewPbftVotesBundle(std::move(votes), false, packet_data.from_node_id_); } -void VotesSyncPacketHandler::onNewPbftVotesBundle(std::vector> &&votes, bool rebroadcast, - const std::optional &exclude_node) { +void VotesBundlePacketHandler::onNewPbftVotesBundle(std::vector> &&votes, bool rebroadcast, + const std::optional &exclude_node) { for (const auto &peer : peers_state_->getAllPeers()) { if (peer.second->syncing_) { continue; diff --git a/libraries/core_libs/network/src/v1_tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/v1_tarcap/taraxa_capability.cpp index cb867216e1..5ed922b4b8 100644 --- a/libraries/core_libs/network/src/v1_tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/taraxa_capability.cpp @@ -9,7 +9,7 @@ #include "network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp" #include "network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp" #include "network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp" -#include "network/v1_tarcap/packets_handlers/votes_sync_packet_handler.hpp" +#include "network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp" namespace taraxa::network::v1_tarcap { @@ -31,10 +31,10 @@ void TaraxaCapability::registerPacketHandlers( // Consensus packets with high processing priority packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); - packets_handlers_->registerHandler( + packets_handlers_->registerHandler( kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr); + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, + pbft_chain, vote_mgr, node_addr); // Standard packets with mid processing priority packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, @@ -54,7 +54,7 @@ void TaraxaCapability::registerPacketHandlers( packets_handlers_->registerHandler( kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, node_addr); - packets_handlers_->registerHandler( + packets_handlers_->registerHandler( kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, vote_mgr, db, node_addr); packets_handlers_->registerHandler( diff --git a/tests/network_test.cpp b/tests/network_test.cpp index ce889ec51d..b4a56c3f05 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -18,7 +18,7 @@ #include "network/tarcap/packets_handlers/status_packet_handler.hpp" #include "network/tarcap/packets_handlers/transaction_packet_handler.hpp" #include "network/tarcap/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/packets_handlers/votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "test_util/samples.hpp" #include "test_util/test_util.hpp" diff --git a/tests/tarcap_threadpool_test.cpp b/tests/tarcap_threadpool_test.cpp index a00127b961..6606806952 100644 --- a/tests/tarcap_threadpool_test.cpp +++ b/tests/tarcap_threadpool_test.cpp @@ -160,20 +160,20 @@ class DummyVotePacketHandler : public DummyPacketHandler { static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::VotePacket; }; -class DummyGetNextVotesSyncPacketHandler : public DummyPacketHandler { +class DummyGetNextVotesBundlePacketHandler : public DummyPacketHandler { public: - DummyGetNextVotesSyncPacketHandler(const HandlersInitData& init_data, const std::string& log_channel_name, - uint32_t processing_delay_ms) + DummyGetNextVotesBundlePacketHandler(const HandlersInitData& init_data, const std::string& log_channel_name, + uint32_t processing_delay_ms) : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::GetNextVotesSyncPacket; }; -class DummyVotesSyncPacketHandler : public DummyPacketHandler { +class DummyVotesBundlePacketHandler : public DummyPacketHandler { public: - DummyVotesSyncPacketHandler(const HandlersInitData& init_data, const std::string& log_channel_name, - uint32_t processing_delay_ms) + DummyVotesBundlePacketHandler(const HandlersInitData& init_data, const std::string& log_channel_name, + uint32_t processing_delay_ms) : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler @@ -322,8 +322,8 @@ TEST_F(TarcapTpTest, block_free_packets) { packets_handler->registerHandler(init_data, "DAG_BLOCK_PH", 20); packets_handler->registerHandler(init_data, "STATUS_PH", 20); packets_handler->registerHandler(init_data, "VOTE_PH", 20); - packets_handler->registerHandler(init_data, "GET_NEXT_VOTES_SYNC_PH", 20); - packets_handler->registerHandler(init_data, "VOTES_SYNC_PH", 20); + packets_handler->registerHandler(init_data, "GET_NEXT_VOTES_SYNC_PH", 20); + packets_handler->registerHandler(init_data, "VOTES_SYNC_PH", 20); // Creates threadpool // Note: make num of threads >= num of packets to check if they are processed concurrently without blocks, otherwise From 07a785e0499e9f2c1f559af02752aef234a7a401 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 19 Apr 2023 11:59:22 -0700 Subject: [PATCH 031/134] chore: increase db minor version vs major --- CMakeLists.txt | 4 ++-- libraries/core_libs/storage/src/migration/period_data.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a3ed884d1..a01d3962f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,10 +10,10 @@ set(TARAXA_VERSION ${TARAXA_MAJOR_VERSION}.${TARAXA_MINOR_VERSION}.${TARAXA_PATC set(TARAXA_NET_VERSION 2) # Major version is modified when DAG blocks, pbft blocks and any basic building blocks of our blockchain is modified # in the db -set(TARAXA_DB_MAJOR_VERSION 2) +set(TARAXA_DB_MAJOR_VERSION 1) # Minor version should be modified when changes to the database are made in the tables that can be rebuilt from the # basic tables -set(TARAXA_DB_MINOR_VERSION 1) +set(TARAXA_DB_MINOR_VERSION 2) # Defines Taraxa library target. project(taraxa-node VERSION ${TARAXA_VERSION}) diff --git a/libraries/core_libs/storage/src/migration/period_data.cpp b/libraries/core_libs/storage/src/migration/period_data.cpp index 361e990296..297fa01e3b 100644 --- a/libraries/core_libs/storage/src/migration/period_data.cpp +++ b/libraries/core_libs/storage/src/migration/period_data.cpp @@ -6,7 +6,7 @@ PeriodData::PeriodData(std::shared_ptr db) : migration::Base(db) {} std::string PeriodData::id() { return "PeriodData"; } -uint32_t PeriodData::dbVersion() { return 2; } +uint32_t PeriodData::dbVersion() { return 1; } void PeriodData::migrate() { auto it = db_->getColumnIterator(DB::Columns::period_data); From 4d634df00862e10f57a4bd05a4f346534c0b52ea Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 19 Apr 2023 12:38:44 -0700 Subject: [PATCH 032/134] chore: log msg refactor --- .../storage/include/storage/migration/migration_base.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libraries/core_libs/storage/include/storage/migration/migration_base.hpp b/libraries/core_libs/storage/include/storage/migration/migration_base.hpp index 6ea3f15dd7..5ba40a8633 100644 --- a/libraries/core_libs/storage/include/storage/migration/migration_base.hpp +++ b/libraries/core_libs/storage/include/storage/migration/migration_base.hpp @@ -14,14 +14,16 @@ class Base { bool isApplied() { return db_->lookup_int(id(), DB::Columns::migrations).has_value(); } void apply(logger::Logger& log) { if (db_->getMajorVersion() != dbVersion()) { - LOG(log) << id() - << ": skip migration as it was made for different major db version. Could be removed from the code" - << std::endl; + LOG(log) << "Skip \"" << id() << "\" migration as it was made for different major db version " << dbVersion() + << ", current db major version " << db_->getMajorVersion() << ". Migration can be removed from the code"; return; } + migrate(); setApplied(); db_->commitWriteBatch(batch_); + + LOG(log) << "\"" << id() << "\" migration was applied"; } protected: From 54c15b1e41dac71b9e3999ceec9662e7ae17e96b Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 19 Apr 2023 12:59:37 -0700 Subject: [PATCH 033/134] ... --- .../storage/migration/migration_base.hpp | 11 +---------- .../storage/src/migration/migration_manager.cpp | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/libraries/core_libs/storage/include/storage/migration/migration_base.hpp b/libraries/core_libs/storage/include/storage/migration/migration_base.hpp index 5ba40a8633..dbd4ef0f23 100644 --- a/libraries/core_libs/storage/include/storage/migration/migration_base.hpp +++ b/libraries/core_libs/storage/include/storage/migration/migration_base.hpp @@ -1,5 +1,4 @@ #pragma once -#include "logger/logger.hpp" #include "storage/storage.hpp" namespace taraxa::storage::migration { @@ -12,18 +11,10 @@ class Base { virtual uint32_t dbVersion() = 0; bool isApplied() { return db_->lookup_int(id(), DB::Columns::migrations).has_value(); } - void apply(logger::Logger& log) { - if (db_->getMajorVersion() != dbVersion()) { - LOG(log) << "Skip \"" << id() << "\" migration as it was made for different major db version " << dbVersion() - << ", current db major version " << db_->getMajorVersion() << ". Migration can be removed from the code"; - return; - } - + void apply() { migrate(); setApplied(); db_->commitWriteBatch(batch_); - - LOG(log) << "\"" << id() << "\" migration was applied"; } protected: diff --git a/libraries/core_libs/storage/src/migration/migration_manager.cpp b/libraries/core_libs/storage/src/migration/migration_manager.cpp index 26e33c6290..54f6d3adea 100644 --- a/libraries/core_libs/storage/src/migration/migration_manager.cpp +++ b/libraries/core_libs/storage/src/migration/migration_manager.cpp @@ -12,11 +12,20 @@ Manager::Manager(std::shared_ptr db, const addr_t& node_addr) : db_(d void Manager::applyAll() { for (const auto& m : migrations_) { - if (!m->isApplied()) { - LOG(log_si_) << "Applying migration " << m->id(); - m->apply(log_si_); - LOG(log_si_) << "Migration applied " << m->id(); + if (m->isApplied()) { + LOG(log_si_) << "Skip \"" << m->id() << "\" migration. It was already applied"; + continue; } + + if (db_->getMajorVersion() != m->dbVersion()) { + LOG(log_si_) << "Skip \"" << m->id() << "\" migration as it was made for different major db version " << m->dbVersion() + << ", current db major version " << db_->getMajorVersion() << ". Migration can be removed from the code"; + continue; + } + + LOG(log_si_) << "Applying migration " << m->id(); + m->apply(); + LOG(log_si_) << "Migration applied " << m->id(); } } } // namespace taraxa::storage::migration \ No newline at end of file From fa0534b93a03a150652b6c1ac1d204d6014ef36b Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Mon, 24 Apr 2023 11:10:33 -0700 Subject: [PATCH 034/134] chore: fix tarcap creation --- .../network/tarcap/taraxa_capability.hpp | 7 +------ libraries/core_libs/network/src/network.cpp | 13 ++++++------ .../network/src/tarcap/taraxa_capability.cpp | 10 --------- .../pbft_sync_packet_handler.cpp | 21 ++++++++++++++++++- .../src/migration/migration_manager.cpp | 5 +++-- 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp index 3df6275cc8..95042dc94a 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp @@ -42,12 +42,7 @@ class TaraxaCapability : public dev::p2p::CapabilityFace { TaraxaCapability &operator=(const TaraxaCapability &ro) = delete; TaraxaCapability(TaraxaCapability &&ro) = delete; TaraxaCapability &operator=(TaraxaCapability &&ro) = delete; - static std::shared_ptr make( - std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, const h256 &genesis_hash, - unsigned version, std::shared_ptr db = {}, std::shared_ptr pbft_mgr = {}, - std::shared_ptr pbft_chain = {}, std::shared_ptr vote_mgr = {}, - std::shared_ptr dag_mgr = {}, std::shared_ptr trx_mgr = {}, - const std::string &log_channel = "TARCAP"); + // Init capability. Register packet handlers and periodic events virtual void init(const h256 &genesis_hash, std::shared_ptr db, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index 8833ae1ebe..284970d525 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -52,13 +52,14 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, dev::p2p::Host::CapabilityList capabilities; // Register old version of taraxa capability - capabilities.emplace_back(network::v1_tarcap::TaraxaCapability::make(host, key, config, genesis_hash, - kOldNetworkVersion, db, pbft_mgr, pbft_chain, - vote_mgr, dag_mgr, trx_mgr, "V1_TARCAP")); + auto v1_tarcap = std::make_shared(host, key, config, kOldNetworkVersion, "V1_TARCAP"); + v1_tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); + capabilities.emplace_back(v1_tarcap); + // Register new version of taraxa capability - capabilities.emplace_back(network::tarcap::TaraxaCapability::make(host, key, config, genesis_hash, - TARAXA_NET_VERSION, db, pbft_mgr, pbft_chain, - vote_mgr, dag_mgr, trx_mgr, "TARCAP")); +// auto v2_tarcap = std::make_shared(host, key, config, TARAXA_NET_VERSION, "TARCAP"); +// v2_tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); +// capabilities.emplace_back(v2_tarcap); return capabilities; }; diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index 408894b307..aa7106e997 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -49,16 +49,6 @@ TaraxaCapability::TaraxaCapability(std::weak_ptr host, const dev addBootNodes(true); } -std::shared_ptr TaraxaCapability::make( - std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, const h256 &genesis_hash, - unsigned version, std::shared_ptr db, std::shared_ptr pbft_mgr, - std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, - std::shared_ptr trx_mgr, const std::string &log_channel) { - auto instance = std::make_shared(host, key, conf, version, log_channel); - instance->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); - return instance; -} - void TaraxaCapability::init(const h256 &genesis_hash, std::shared_ptr db, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp index 10c55cd184..18c504e55a 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp @@ -50,6 +50,25 @@ void PbftSyncPacketHandler::process(const tarcap::PacketData &packet_data, return; } + auto decodePeriodDataRlpV1 = [](const dev::RLP &period_data_v1) -> PeriodData { + PeriodData period_data; + auto it = period_data_v1.begin(); + period_data.pbft_blk = std::make_shared(*it++); + for (auto const vote_rlp : *it++) { + period_data.previous_block_cert_votes.emplace_back(std::make_shared(vote_rlp)); + } + + for (auto const dag_block_rlp : *it++) { + period_data.dag_blocks.emplace_back(dag_block_rlp); + } + + for (auto const trx_rlp : *it) { + period_data.transactions.emplace_back(std::make_shared(trx_rlp)); + } + + return period_data; + }; + // Process received pbft blocks // pbft_chain_synced is the flag to indicate own PBFT chain has synced with the peer's PBFT chain const bool pbft_chain_synced = packet_data.rlp_.itemCount() == kChainSyncedPacketSize; @@ -57,7 +76,7 @@ void PbftSyncPacketHandler::process(const tarcap::PacketData &packet_data, const bool last_block = packet_data.rlp_[0].toInt(); PeriodData period_data; try { - period_data = PeriodData(packet_data.rlp_[1]); + period_data = decodePeriodDataRlpV1(packet_data.rlp_[1]); } catch (const Transaction::InvalidTransaction &e) { throw MaliciousPeerException("Unable to parse PeriodData: " + std::string(e.what())); } diff --git a/libraries/core_libs/storage/src/migration/migration_manager.cpp b/libraries/core_libs/storage/src/migration/migration_manager.cpp index 54f6d3adea..1bc7b4bc5e 100644 --- a/libraries/core_libs/storage/src/migration/migration_manager.cpp +++ b/libraries/core_libs/storage/src/migration/migration_manager.cpp @@ -18,8 +18,9 @@ void Manager::applyAll() { } if (db_->getMajorVersion() != m->dbVersion()) { - LOG(log_si_) << "Skip \"" << m->id() << "\" migration as it was made for different major db version " << m->dbVersion() - << ", current db major version " << db_->getMajorVersion() << ". Migration can be removed from the code"; + LOG(log_si_) << "Skip \"" << m->id() << "\" migration as it was made for different major db version " + << m->dbVersion() << ", current db major version " << db_->getMajorVersion() + << ". Migration can be removed from the code"; continue; } From 0c2769f801d2e333ab52b2875736d5f812f8d6b6 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Mon, 24 Apr 2023 18:09:37 -0700 Subject: [PATCH 035/134] =?UTF-8?q?c=E2=89=88chore:=20partially=20support?= =?UTF-8?q?=20multiple=20capabilities=20throughout=20the=20codebase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libraries/aleth/libp2p/Host.h | 2 +- .../network/include/network/network.hpp | 6 +- libraries/core_libs/network/src/network.cpp | 58 +++++++++++++------ .../common/packet_handler.cpp | 1 + .../network/src/tarcap/taraxa_capability.cpp | 2 + tests/p2p_test.cpp | 58 +++++++++++-------- 6 files changed, 79 insertions(+), 48 deletions(-) diff --git a/libraries/aleth/libp2p/Host.h b/libraries/aleth/libp2p/Host.h index b24b70c9dd..c7299252a7 100644 --- a/libraries/aleth/libp2p/Host.h +++ b/libraries/aleth/libp2p/Host.h @@ -84,7 +84,7 @@ struct Host final : std::enable_shared_from_this { KeyPair const& kp, NetworkConfig _n, TaraxaNetworkConfig taraxa_conf = {}, std::filesystem::path state_file_path = {}); - std::shared_ptr latestCapability() { return m_capabilities.rbegin()->second.ref; } + Capabilities getSupportedCapabilities() { return m_capabilities; } ~Host(); ba::io_context::count_type do_work(); diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index c0779078c5..187037abee 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -24,7 +24,6 @@ namespace taraxa { class PacketHandler; -// TODO merge with TaraxaCapability, and then split the result in reasonable components class Network { public: Network(const FullNodeConfig &config, const h256 &genesis_hash = {}, @@ -69,14 +68,15 @@ class Network { private: util::ThreadPool tp_; std::shared_ptr host_; - std::shared_ptr taraxa_capability_; + std::map> tarcaps_; LOG_OBJECTS_DEFINE }; template std::shared_ptr Network::getSpecificHandler() const { - return taraxa_capability_->getSpecificHandler(); + // TODO: rework this - we have to use both tarcaps + return tarcaps_.rbegin()->second->getSpecificHandler(); } } // namespace taraxa diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index 284970d525..16be2df42f 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -52,21 +52,27 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, dev::p2p::Host::CapabilityList capabilities; // Register old version of taraxa capability - auto v1_tarcap = std::make_shared(host, key, config, kOldNetworkVersion, "V1_TARCAP"); + auto v1_tarcap = + std::make_shared(host, key, config, kOldNetworkVersion, "V1_TARCAP"); v1_tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); capabilities.emplace_back(v1_tarcap); // Register new version of taraxa capability -// auto v2_tarcap = std::make_shared(host, key, config, TARAXA_NET_VERSION, "TARCAP"); -// v2_tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); -// capabilities.emplace_back(v2_tarcap); + auto v2_tarcap = + std::make_shared(host, key, config, TARAXA_NET_VERSION, "TARCAP"); + v2_tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); + capabilities.emplace_back(v2_tarcap); return capabilities; }; } host_ = dev::p2p::Host::make(net_version, construct_capabilities, key, net_conf, taraxa_net_conf, network_file_path); - taraxa_capability_ = std::static_pointer_cast(host_->latestCapability()); + for (const auto &tarcap : host_->getSupportedCapabilities()) { + tarcaps_.emplace(tarcap.second.ref->version(), + std::static_pointer_cast(tarcap.second.ref)); + } + for (uint i = 0; i < tp_.capacity(); ++i) { tp_.post_loop({100 + i * 20}, [this] { while (0 < host_->do_work()) @@ -80,12 +86,17 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, Network::~Network() { tp_.stop(); - taraxa_capability_->stop(); + for (auto &tarcap : host_->getSupportedCapabilities()) { + std::static_pointer_cast(tarcap.second.ref)->stop(); + } } void Network::start() { tp_.start(); - taraxa_capability_->start(); + for (auto &tarcap : host_->getSupportedCapabilities()) { + std::static_pointer_cast(tarcap.second.ref)->start(); + } + LOG(log_nf_) << "Started Node id: " << host_->id() << ", listening on port " << host_->listenPort(); } @@ -97,22 +108,30 @@ size_t Network::getPeerCount() { return host_->peer_count(); } unsigned Network::getNodeCount() { return host_->getNodeCount(); } -Json::Value Network::getStatus() { return taraxa_capability_->getNodeStats()->getStatus(); } +Json::Value Network::getStatus() { + // TODO: refactor this: combine node stats from all tarcaps... + return tarcaps_.end()->second->getNodeStats()->getStatus(); +} -void Network::startSyncingPbft() { - tp_.post( - [this] { taraxa_capability_->getSpecificHandler()->startSyncingPbft(); }); +bool Network::pbft_syncing() { + return std::ranges::any_of(tarcaps_, [](const auto &tarcap) { return tarcap.second->pbft_syncing(); }); } -bool Network::pbft_syncing() { return taraxa_capability_->pbft_syncing(); } -uint64_t Network::syncTimeSeconds() const { return taraxa_capability_->getNodeStats()->syncTimeSeconds(); } +void Network::setSyncStatePeriod(PbftPeriod period) { + for (auto &tarcap : tarcaps_) { + if (tarcap.second->pbft_syncing()) { + tarcap.second->setSyncStatePeriod(period); + } + } +} -void Network::setSyncStatePeriod(PbftPeriod period) { taraxa_capability_->setSyncStatePeriod(period); } +// METHODS USED IN TESTS ONLY +// Note: for functions use in tests all data are fetched only from the tarcap with the highest version, +// other functions must use all tarcaps -// Only for test void Network::setPendingPeersToReady() { - const auto &peers_state = taraxa_capability_->getPeersState(); + const auto &peers_state = tarcaps_.end()->second->getPeersState(); auto peerIds = peers_state->getAllPendingPeersIDs(); for (const auto &peerId : peerIds) { @@ -125,12 +144,13 @@ void Network::setPendingPeersToReady() { dev::p2p::NodeID Network::getNodeId() const { return host_->id(); } -int Network::getReceivedBlocksCount() const { return taraxa_capability_->getReceivedBlocksCount(); } +int Network::getReceivedBlocksCount() const { return tarcaps_.end()->second->getReceivedBlocksCount(); } -int Network::getReceivedTransactionsCount() const { return taraxa_capability_->getReceivedTransactionsCount(); } +int Network::getReceivedTransactionsCount() const { return tarcaps_.end()->second->getReceivedTransactionsCount(); } std::shared_ptr Network::getPeer(dev::p2p::NodeID const &id) const { - return taraxa_capability_->getPeersState()->getPeer(id); + return tarcaps_.end()->second->getPeersState()->getPeer(id); } +// METHODS USED IN TESTS ONLY } // namespace taraxa diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp index e9a2c997e7..0a8260ada3 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp @@ -94,6 +94,7 @@ bool PacketHandler::sealAndSend(const dev::p2p::NodeID& node_id, SubprotocolPack const auto begin = std::chrono::steady_clock::now(); const size_t packet_size = rlp.out().size(); + LOG(log_er_) << "sealAndSend: " << convertPacketTypeToString(packet_type); host->send(node_id, TARAXA_CAPABILITY_NAME, packet_type, rlp.invalidate(), [begin, node_id, packet_size, packet_type, this]() { if (!kConf.network.ddos_protection.log_packets_stats) { diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index aa7106e997..52ac1dc5d5 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -291,6 +291,8 @@ void TaraxaCapability::interpretCapabilityPacket(std::weak_ptrisDeepPbftSyncing() && filterSyncIrrelevantPackets(packet_type)) { LOG(log_dg_) << "Ignored " << convertPacketTypeToString(packet_type) << " because we are still syncing"; return; diff --git a/tests/p2p_test.cpp b/tests/p2p_test.cpp index 9a215160a7..8e65ce16d3 100644 --- a/tests/p2p_test.cpp +++ b/tests/p2p_test.cpp @@ -48,6 +48,15 @@ void setPendingPeersToReady(std::shared_ptr makeTarcap(std::weak_ptr host, + const dev::KeyPair &key, + const FullNodeConfig &conf, + const h256 &genesis_hash, unsigned version) { + auto instance = std::make_shared(host, key, conf, version, "TARCAP"); + instance->init(genesis_hash, {}, {}, {}, {}, {}, {}, key.address()); + return instance; +} + /* Test creates one boot node and 10 nodes that uses that boot node to find each other. Test confirm that after a delay each node had found @@ -102,14 +111,14 @@ TEST_F(P2PTest, capability_send_block) { auto host1 = Host::make( "Test", [&](auto host) { - thc1 = network::tarcap::TaraxaCapability::make(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); + thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); return Host::CapabilityList{thc1}; }, KeyPair::create(), prefs1); auto host2 = Host::make( "Test", [&](auto host) { - thc2 = network::tarcap::TaraxaCapability::make(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); + thc2 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); return Host::CapabilityList{thc2}; }, KeyPair::create(), prefs2); @@ -195,7 +204,7 @@ TEST_F(P2PTest, block_propagate) { auto host1 = Host::make( "Test", [&](auto host) { - thc1 = network::tarcap::TaraxaCapability::make(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); + thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); thc1->start(); return Host::CapabilityList{thc1}; }, @@ -208,8 +217,7 @@ TEST_F(P2PTest, block_propagate) { auto host = vHosts.emplace_back(Host::make( "Test", [&](auto host) { - auto cap = vCapabilities.emplace_back( - network::tarcap::TaraxaCapability::make(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION)); + auto cap = vCapabilities.emplace_back(makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION)); cap->start(); return Host::CapabilityList{cap}; }, @@ -325,14 +333,14 @@ TEST_F(P2PTest, multiple_capabilities) { auto nw1 = std::make_shared( node_cfgs[0], genesis_hash, [kp1, &node_cfgs, &genesis_hash](auto host) { - auto cap = network::tarcap::TaraxaCapability::make(host, kp1, node_cfgs[0], genesis_hash, 3); + auto cap = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 3); return Host::CapabilityList{cap}; }, "/tmp/nw2"); auto nw2 = std::make_shared( node_cfgs[1], genesis_hash, [kp2, &node_cfgs, &genesis_hash](auto host) { - auto cap = network::tarcap::TaraxaCapability::make(host, kp2, node_cfgs[1], genesis_hash, 3); + auto cap = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 3); return Host::CapabilityList{cap}; }, "/tmp/nw3"); @@ -345,18 +353,18 @@ TEST_F(P2PTest, multiple_capabilities) { auto nw1 = std::make_shared( node_cfgs[0], genesis_hash, [kp1, &node_cfgs, &genesis_hash](auto host) { - auto cap1 = network::tarcap::TaraxaCapability::make(host, kp1, node_cfgs[0], genesis_hash, 1); - auto cap2 = network::tarcap::TaraxaCapability::make(host, kp1, node_cfgs[0], genesis_hash, 2); - auto cap3 = network::tarcap::TaraxaCapability::make(host, kp1, node_cfgs[0], genesis_hash, 3); + auto cap1 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 1); + auto cap2 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 2); + auto cap3 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 3); return Host::CapabilityList{cap1, cap2, cap3}; }, "/tmp/nw2"); auto nw2 = std::make_shared( node_cfgs[1], genesis_hash, [kp2, &node_cfgs, &genesis_hash](auto host) { - auto cap1 = network::tarcap::TaraxaCapability::make(host, kp2, node_cfgs[1], genesis_hash, 1); - auto cap2 = network::tarcap::TaraxaCapability::make(host, kp2, node_cfgs[1], genesis_hash, 2); - auto cap3 = network::tarcap::TaraxaCapability::make(host, kp2, node_cfgs[1], genesis_hash, 3); + auto cap1 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 1); + auto cap2 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 2); + auto cap3 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 3); return Host::CapabilityList{cap1, cap2, cap3}; }, "/tmp/nw3"); @@ -369,18 +377,18 @@ TEST_F(P2PTest, multiple_capabilities) { auto nw1 = std::make_shared( node_cfgs[0], genesis_hash, [kp1, &node_cfgs, &genesis_hash](auto host) { - auto cap1 = network::tarcap::TaraxaCapability::make(host, kp1, node_cfgs[0], genesis_hash, 1); - auto cap2 = network::tarcap::TaraxaCapability::make(host, kp1, node_cfgs[0], genesis_hash, 2); - auto cap3 = network::tarcap::TaraxaCapability::make(host, kp1, node_cfgs[0], genesis_hash, 3); + auto cap1 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 1); + auto cap2 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 2); + auto cap3 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 3); return Host::CapabilityList{cap1, cap2, cap3}; }, "/tmp/nw2"); auto nw2 = std::make_shared( node_cfgs[1], genesis_hash, [kp2, &node_cfgs, &genesis_hash](auto host) { - auto cap2 = network::tarcap::TaraxaCapability::make(host, kp2, node_cfgs[1], genesis_hash, 2); - auto cap3 = network::tarcap::TaraxaCapability::make(host, kp2, node_cfgs[1], genesis_hash, 3); - auto cap4 = network::tarcap::TaraxaCapability::make(host, kp2, node_cfgs[1], genesis_hash, 4); + auto cap2 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 2); + auto cap3 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 3); + auto cap4 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 4); return Host::CapabilityList{cap2, cap3, cap4}; }, "/tmp/nw3"); @@ -393,18 +401,18 @@ TEST_F(P2PTest, multiple_capabilities) { auto nw1 = std::make_shared( node_cfgs[0], genesis_hash, [kp1, &node_cfgs, &genesis_hash](auto host) { - auto cap1 = network::tarcap::TaraxaCapability::make(host, kp1, node_cfgs[0], genesis_hash, 1); - auto cap2 = network::tarcap::TaraxaCapability::make(host, kp1, node_cfgs[0], genesis_hash, 2); - auto cap3 = network::tarcap::TaraxaCapability::make(host, kp1, node_cfgs[0], genesis_hash, 3); + auto cap1 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 1); + auto cap2 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 2); + auto cap3 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 3); return Host::CapabilityList{cap1, cap2, cap3}; }, "/tmp/nw2"); auto nw2 = std::make_shared( node_cfgs[1], genesis_hash, [kp2, &node_cfgs, &genesis_hash](auto host) { - auto cap4 = network::tarcap::TaraxaCapability::make(host, kp2, node_cfgs[1], genesis_hash, 4); - auto cap5 = network::tarcap::TaraxaCapability::make(host, kp2, node_cfgs[1], genesis_hash, 5); - auto cap6 = network::tarcap::TaraxaCapability::make(host, kp2, node_cfgs[1], genesis_hash, 6); + auto cap4 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 4); + auto cap5 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 5); + auto cap6 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 6); return Host::CapabilityList{cap4, cap5, cap6}; }, "/tmp/nw3"); From 3ea20e0a010b88d3e55036da4962d6b04afd7241 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Tue, 25 Apr 2023 15:10:33 -0700 Subject: [PATCH 036/134] chore: support multiple taraxa capabilities --- .../consensus/src/dag/dag_manager.cpp | 3 +- .../consensus/src/pbft/pbft_manager.cpp | 40 +++++------- .../network/graphql/src/sync_state.cpp | 15 +++-- .../network/include/network/network.hpp | 10 ++- .../dag_block_packet_handler.hpp | 2 +- .../pbft_sync_packet_handler.hpp | 2 +- .../votes_bundle_packet_handler.hpp | 2 +- .../votes_bundle_packet_handler.hpp | 2 +- libraries/core_libs/network/src/network.cpp | 64 +++++++++++++++++-- .../common/packet_handler.cpp | 1 - .../dag_block_packet_handler.cpp | 4 +- .../pbft_sync_packet_handler.cpp | 2 +- .../votes_bundle_packet_handler.cpp | 4 +- .../network/src/tarcap/taraxa_capability.cpp | 2 - .../votes_bundle_packet_handler.cpp | 4 +- 15 files changed, 107 insertions(+), 50 deletions(-) diff --git a/libraries/core_libs/consensus/src/dag/dag_manager.cpp b/libraries/core_libs/consensus/src/dag/dag_manager.cpp index 82c91cdda0..548447e248 100644 --- a/libraries/core_libs/consensus/src/dag/dag_manager.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_manager.cpp @@ -177,8 +177,7 @@ std::pair> DagManager::addDagBlock(DagBlock &&blk, if (save) { block_verified_.emit(blk); if (auto net = network_.lock()) { - net->getSpecificHandler()->onNewBlockVerified(std::move(blk), proposed, - std::move(trxs)); + net->gossipDagBlock(blk, proposed, trxs); } } } diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index a5600c4e95..a41493da3e 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -565,15 +565,13 @@ void PbftManager::broadcastVotes() { return; } - const auto votes_sync_packet_handler = net->getSpecificHandler(); - // Send votes to the other peers - auto gossipVotes = [this, &votes_sync_packet_handler](std::vector> &&votes, - const std::string &votes_type_str, bool rebroadcast) { + auto gossipVotes = [this, &net](const std::vector> &votes, const std::string &votes_type_str, + bool rebroadcast) { if (!votes.empty()) { LOG(log_dg_) << "Broadcast " << votes_type_str << " for period " << votes.back()->getPeriod() << ", round " << votes.back()->getRound(); - votes_sync_packet_handler->onNewPbftVotesBundle(std::move(votes), rebroadcast); + net->gossipVotesBundle(votes, rebroadcast); } }; @@ -598,16 +596,12 @@ void PbftManager::broadcastVotes() { "2t+1 next null votes", rebroadcast); } - // Broadcast own votes - auto vote_packet_handler = net->getSpecificHandler(); - // TODO: this could be optimized to use VotesBundlePacketHandler if we drop some of the checks in process function - // Send votes by one as votes sync packet must contain votes with the same type, period and round - const auto &own_votes = vote_mgr_->getOwnVerifiedVotes(); - for (const auto &vote : own_votes) { - vote_packet_handler->onNewPbftVote(vote, getPbftProposedBlock(vote->getPeriod(), vote->getBlockHash()), - rebroadcast); - } - if (!own_votes.empty()) { + // Broadcast own votes - send votes by one as they have different type, period, round, step + if (const auto &own_votes = vote_mgr_->getOwnVerifiedVotes(); !own_votes.empty()) { + for (const auto &vote : own_votes) { + net->gossipVote(vote, getPbftProposedBlock(vote->getPeriod(), vote->getBlockHash()), rebroadcast); + } + LOG(log_dg_) << "Broadcast own votes for period " << period << ", round " << round; } }; @@ -769,8 +763,7 @@ bool PbftManager::genAndPlaceProposeVote(const std::shared_ptr &propo LOG(log_dg_) << "Broadcast propose block reward votes for block " << proposed_block->getBlockHash() << ", num of reward votes: " << reward_votes.size() << ", period " << current_pbft_period << ", round " << current_pbft_round; - net->getSpecificHandler()->onNewPbftVotesBundle(std::move(reward_votes), - false); + net->gossipVotesBundle(reward_votes, false); } if (!placeVote(propose_vote, "propose vote", proposed_block)) { @@ -793,7 +786,7 @@ void PbftManager::gossipNewVote(const std::shared_ptr &vote, const std::sh return; } - net->getSpecificHandler()->onNewPbftVote(vote, voted_block); + net->gossipVote(vote, voted_block); auto found_voted_block_it = current_round_broadcasted_votes_.find(vote->getBlockHash()); if (found_voted_block_it == current_round_broadcasted_votes_.end()) { @@ -1666,8 +1659,9 @@ std::optional>>> PbftMan << "; prevHash: " << period_data.pbft_blk->getPrevBlockHash() << " from peer " << node_id.abridged() << " received, stop syncing."; sync_queue_.clear(); + // Handle malicious peer on network level - net->getSpecificHandler()->handleMaliciousSyncPeer(node_id); + net->handleMaliciousSyncPeer(node_id); return std::nullopt; } @@ -1681,7 +1675,7 @@ std::optional>>> PbftMan LOG(log_er_) << "Failed verifying reward votes for block " << pbft_block_hash << ". Disconnect malicious peer " << node_id.abridged(); sync_queue_.clear(); - net->getSpecificHandler()->handleMaliciousSyncPeer(node_id); + net->handleMaliciousSyncPeer(node_id); return std::nullopt; } @@ -1697,7 +1691,7 @@ std::optional>>> PbftMan LOG(log_er_) << "Synced PBFT block " << pbft_block_hash << " doesn't have enough valid cert votes. Clear synced PBFT blocks!"; sync_queue_.clear(); - net->getSpecificHandler()->handleMaliciousSyncPeer(node_id); + net->handleMaliciousSyncPeer(node_id); return std::nullopt; } @@ -1717,7 +1711,7 @@ std::optional>>> PbftMan LOG(log_er_) << "Synced PBFT block " << pbft_block_hash << " transactions count " << period_data.transactions.size() << " incorrect, expected: " << non_finalized_transactions.size(); sync_queue_.clear(); - net->getSpecificHandler()->handleMaliciousSyncPeer(node_id); + net->handleMaliciousSyncPeer(node_id); return std::nullopt; } for (uint32_t i = 0; i < non_finalized_transactions.size(); i++) { @@ -1726,7 +1720,7 @@ std::optional>>> PbftMan << non_finalized_transactions[i] << " incorrect, expected: " << period_data.transactions[i]->getHash(); sync_queue_.clear(); - net->getSpecificHandler()->handleMaliciousSyncPeer(node_id); + net->handleMaliciousSyncPeer(node_id); return std::nullopt; } } diff --git a/libraries/core_libs/network/graphql/src/sync_state.cpp b/libraries/core_libs/network/graphql/src/sync_state.cpp index eda1d8db72..4c30a3834f 100644 --- a/libraries/core_libs/network/graphql/src/sync_state.cpp +++ b/libraries/core_libs/network/graphql/src/sync_state.cpp @@ -16,12 +16,17 @@ response::Value SyncState::getCurrentBlock() const noexcept { } response::Value SyncState::getHighestBlock() const noexcept { - if (auto net = network_.lock(); net) { - const auto peer = net->getSpecificHandler<::taraxa::network::tarcap::PbftSyncPacketHandler>()->getMaxChainPeer(); - return response::Value(static_cast(peer->pbft_chain_size_)); - } else { - return response::Value(0); + auto net = network_.lock(); + if (!net) { + return {}; } + + const auto peer = net->getMaxChainPeer(); + if (!peer) { + return {}; + } + + return response::Value(static_cast(peer->pbft_chain_size_)); } std::optional SyncState::getPulledStates() const noexcept { return std::nullopt; } diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index 187037abee..0626045db9 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -20,6 +20,7 @@ #include "tarcap/taraxa_capability.hpp" #include "transaction/transaction.hpp" +// TODO: use network::taraxa namespace namespace taraxa { class PacketHandler; @@ -54,10 +55,16 @@ class Network { uint64_t syncTimeSeconds() const; void setSyncStatePeriod(PbftPeriod period); + void gossipDagBlock(const DagBlock &block, bool proposed, const SharedTransactions &trxs); + void gossipVote(const std::shared_ptr &vote, const std::shared_ptr &block, bool rebroadcast = false); + void gossipVotesBundle(const std::vector> &votes, bool rebroadcast = false); + void handleMaliciousSyncPeer(const dev::p2p::NodeID &id); + std::shared_ptr getMaxChainPeer() const; + + // METHODS USED IN TESTS ONLY template std::shared_ptr getSpecificHandler() const; - // METHODS USED IN TESTS ONLY void setPendingPeersToReady(); dev::p2p::NodeID getNodeId() const; int getReceivedBlocksCount() const; @@ -75,7 +82,6 @@ class Network { template std::shared_ptr Network::getSpecificHandler() const { - // TODO: rework this - we have to use both tarcaps return tarcaps_.rbegin()->second->getSpecificHandler(); } diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_block_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_block_packet_handler.hpp index d2c001569d..884cdfe1bd 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_block_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_block_packet_handler.hpp @@ -21,7 +21,7 @@ class DagBlockPacketHandler final : public ExtSyncingPacketHandler { void sendBlock(dev::p2p::NodeID const &peer_id, DagBlock block, const SharedTransactions &trxs); void onNewBlockReceived(DagBlock &&block, const std::shared_ptr &peer = nullptr); - void onNewBlockVerified(DagBlock &&block, bool proposed, SharedTransactions &&trxs); + void onNewBlockVerified(const DagBlock &block, bool proposed, const SharedTransactions &trxs); // Packet type that is processed by this handler static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::DagBlockPacket; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp index 9d7de6ecd4..b69207d79f 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp @@ -14,7 +14,7 @@ class PbftSyncPacketHandler final : public ExtSyncingPacketHandler { std::shared_ptr vote_mgr, std::shared_ptr periodic_events_tp, std::shared_ptr db, const addr_t& node_addr); - void handleMaliciousSyncPeer(dev::p2p::NodeID const& id); + void handleMaliciousSyncPeer(const dev::p2p::NodeID& id); // Packet type that is processed by this handler static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::PbftSyncPacket; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp index 9e13e7a31f..332ade7ef7 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp @@ -18,7 +18,7 @@ class VotesBundlePacketHandler final : public ExtVotesPacketHandler { * @param rebroadcast if rebroadcast is true, all votes are resent to all peers * @param exclude_node do not send votes to excluded node */ - void onNewPbftVotesBundle(std::vector>&& votes, bool rebroadcast = false, + void onNewPbftVotesBundle(const std::vector>& votes, bool rebroadcast = false, const std::optional& exclude_node = {}); // Packet type that is processed by this handler diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp index 158827569e..1770ace5cb 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp @@ -18,7 +18,7 @@ class VotesBundlePacketHandler final : public ExtVotesPacketHandler { * @param rebroadcast if rebroadcast is true, all votes are resent to all peers * @param exclude_node do not send votes to excluded node */ - void onNewPbftVotesBundle(std::vector>&& votes, bool rebroadcast = false, + void onNewPbftVotesBundle(const std::vector>& votes, bool rebroadcast = false, const std::optional& exclude_node = {}); // Packet type that is processed by this handler diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index 16be2df42f..dce7f753b0 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -5,9 +5,13 @@ #include #include +#include #include "config/version.hpp" +#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp" #include "network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp" #include "network/v1_tarcap/taraxa_capability.hpp" namespace taraxa { @@ -120,18 +124,70 @@ bool Network::pbft_syncing() { void Network::setSyncStatePeriod(PbftPeriod period) { for (auto &tarcap : tarcaps_) { + // TODO: double check this ??? if (tarcap.second->pbft_syncing()) { tarcap.second->setSyncStatePeriod(period); } } } +void Network::gossipDagBlock(const DagBlock &block, bool proposed, const SharedTransactions &trxs) { + for (const auto &tarcap : tarcaps_ | std::views::reverse) { + tarcap.second->getSpecificHandler()->onNewBlockVerified(block, proposed, + trxs); + } +} + +void Network::gossipVote(const std::shared_ptr &vote, const std::shared_ptr &block, bool rebroadcast) { + for (const auto &tarcap : tarcaps_ | std::views::reverse) { + tarcap.second->getSpecificHandler()->onNewPbftVote(vote, block, rebroadcast); + } +} + +void Network::gossipVotesBundle(const std::vector> &votes, bool rebroadcast) { + for (const auto &tarcap : tarcaps_ | std::views::reverse) { + tarcap.second->getSpecificHandler()->onNewPbftVotesBundle(votes, + rebroadcast); + } +} + +void Network::handleMaliciousSyncPeer(const dev::p2p::NodeID &node_id) { + for (const auto &tarcap : tarcaps_ | std::views::reverse) { + // Peer is present only in one taraxa capability depending on his network version + if (auto peer = tarcap.second->getPeersState()->getPeer(node_id); !peer) { + continue; + } + + tarcap.second->getSpecificHandler()->handleMaliciousSyncPeer(node_id); + } +} + +std::shared_ptr Network::getMaxChainPeer() const { + std::shared_ptr max_chain_peer{nullptr}; + + for (const auto &tarcap : tarcaps_ | std::views::reverse) { + const auto peer = + tarcap.second->getSpecificHandler<::taraxa::network::tarcap::PbftSyncPacketHandler>()->getMaxChainPeer(); + if (!peer) { + continue; + } + + if (!max_chain_peer) { + max_chain_peer = peer; + } else if (peer->pbft_chain_size_ > max_chain_peer->pbft_chain_size_) { + max_chain_peer = peer; + } + } + + return max_chain_peer; +} + // METHODS USED IN TESTS ONLY // Note: for functions use in tests all data are fetched only from the tarcap with the highest version, // other functions must use all tarcaps void Network::setPendingPeersToReady() { - const auto &peers_state = tarcaps_.end()->second->getPeersState(); + const auto &peers_state = tarcaps_.rbegin()->second->getPeersState(); auto peerIds = peers_state->getAllPendingPeersIDs(); for (const auto &peerId : peerIds) { @@ -144,12 +200,12 @@ void Network::setPendingPeersToReady() { dev::p2p::NodeID Network::getNodeId() const { return host_->id(); } -int Network::getReceivedBlocksCount() const { return tarcaps_.end()->second->getReceivedBlocksCount(); } +int Network::getReceivedBlocksCount() const { return tarcaps_.rbegin()->second->getReceivedBlocksCount(); } -int Network::getReceivedTransactionsCount() const { return tarcaps_.end()->second->getReceivedTransactionsCount(); } +int Network::getReceivedTransactionsCount() const { return tarcaps_.rbegin()->second->getReceivedTransactionsCount(); } std::shared_ptr Network::getPeer(dev::p2p::NodeID const &id) const { - return tarcaps_.end()->second->getPeersState()->getPeer(id); + return tarcaps_.rbegin()->second->getPeersState()->getPeer(id); } // METHODS USED IN TESTS ONLY diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp index 0a8260ada3..e9a2c997e7 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp @@ -94,7 +94,6 @@ bool PacketHandler::sealAndSend(const dev::p2p::NodeID& node_id, SubprotocolPack const auto begin = std::chrono::steady_clock::now(); const size_t packet_size = rlp.out().size(); - LOG(log_er_) << "sealAndSend: " << convertPacketTypeToString(packet_type); host->send(node_id, TARAXA_CAPABILITY_NAME, packet_type, rlp.invalidate(), [begin, node_id, packet_size, packet_type, this]() { if (!kConf.network.ddos_protection.log_packets_stats) { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/dag_block_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/dag_block_packet_handler.cpp index d71dfbfe15..20ef15e896 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/dag_block_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/dag_block_packet_handler.cpp @@ -181,14 +181,14 @@ void DagBlockPacketHandler::onNewBlockReceived(DagBlock &&block, const std::shar } } else if (!test_state_->hasBlock(block.getHash())) { test_state_->insertBlock(block); - onNewBlockVerified(std::move(block), false, {}); + onNewBlockVerified(block, false, {}); } else { LOG(log_tr_) << "Received NewBlock " << block.getHash() << "that is already known"; return; } } -void DagBlockPacketHandler::onNewBlockVerified(DagBlock &&block, bool proposed, SharedTransactions &&trxs) { +void DagBlockPacketHandler::onNewBlockVerified(const DagBlock &block, bool proposed, const SharedTransactions &trxs) { // If node is pbft syncing and block is not proposed by us, this is an old block that has been verified - no block // gossip is needed if (!proposed && pbft_syncing_state_->isDeepPbftSyncing()) { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp index 2ee67c97bb..240d5221b9 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp @@ -263,7 +263,7 @@ void PbftSyncPacketHandler::delayedPbftSync(int counter) { } } -void PbftSyncPacketHandler::handleMaliciousSyncPeer(dev::p2p::NodeID const &id) { +void PbftSyncPacketHandler::handleMaliciousSyncPeer(const dev::p2p::NodeID &id) { peers_state_->set_peer_malicious(id); if (auto host = peers_state_->host_.lock(); host) { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp index f6eeae06ee..7516623840 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp @@ -89,10 +89,10 @@ void VotesBundlePacketHandler::process(const PacketData &packet_data, const std: << " ) sync votes from peer " << packet_data.from_node_id_ << " node current round " << current_pbft_round << ", peer pbft round " << votes_bundle_pbft_round; - onNewPbftVotesBundle(std::move(votes), false, packet_data.from_node_id_); + onNewPbftVotesBundle(votes, false, packet_data.from_node_id_); } -void VotesBundlePacketHandler::onNewPbftVotesBundle(std::vector> &&votes, bool rebroadcast, +void VotesBundlePacketHandler::onNewPbftVotesBundle(const std::vector> &votes, bool rebroadcast, const std::optional &exclude_node) { for (const auto &peer : peers_state_->getAllPeers()) { if (peer.second->syncing_) { diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index 52ac1dc5d5..aa7106e997 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -291,8 +291,6 @@ void TaraxaCapability::interpretCapabilityPacket(std::weak_ptrisDeepPbftSyncing() && filterSyncIrrelevantPackets(packet_type)) { LOG(log_dg_) << "Ignored " << convertPacketTypeToString(packet_type) << " because we are still syncing"; return; diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp index db73e46857..9acea1d9ad 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp @@ -131,10 +131,10 @@ void VotesBundlePacketHandler::process(const tarcap::PacketData &packet_data, << packet_data.from_node_id_ << " node current round " << current_pbft_round << ", peer pbft round " << votes_bundle_pbft_round; - onNewPbftVotesBundle(std::move(votes), false, packet_data.from_node_id_); + onNewPbftVotesBundle(votes, false, packet_data.from_node_id_); } -void VotesBundlePacketHandler::onNewPbftVotesBundle(std::vector> &&votes, bool rebroadcast, +void VotesBundlePacketHandler::onNewPbftVotesBundle(const std::vector> &votes, bool rebroadcast, const std::optional &exclude_node) { for (const auto &peer : peers_state_->getAllPeers()) { if (peer.second->syncing_) { From 246969bbfffeebd8f7e21a3653f490fc7cc51938 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 26 Apr 2023 14:27:07 -0700 Subject: [PATCH 037/134] ... --- for_devs/requirements.txt | 2 +- libraries/core_libs/network/include/network/network.hpp | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/for_devs/requirements.txt b/for_devs/requirements.txt index d48df00d45..171e0141dc 100644 --- a/for_devs/requirements.txt +++ b/for_devs/requirements.txt @@ -11,7 +11,7 @@ eth-abi==2.1.1 eth-account==0.5.9 eth-hash==0.3.1 eth-keyfile==0.5.1 -eth-keys==0.3.3 +eth-keys==0.3.4 eth-rlp==0.2.1 eth-typing==2.2.2 eth-utils==1.10.0 diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index 0626045db9..b4f03a4bcc 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -20,7 +20,6 @@ #include "tarcap/taraxa_capability.hpp" #include "transaction/transaction.hpp" -// TODO: use network::taraxa namespace namespace taraxa { class PacketHandler; From b5bbc5e5ed2a409c48b1af9007e5c512cef50e6c Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 26 Apr 2023 16:09:07 -0700 Subject: [PATCH 038/134] chore: move threadpool to network folder --- .../network/include/network/network.hpp | 9 + .../include/network/tarcap/packet_types.hpp | 4 +- .../common/packet_handler.hpp | 12 +- .../dag_block_packet_handler.hpp | 4 +- .../dag_sync_packet_handler.hpp | 4 +- .../get_dag_sync_packet_handler.hpp | 4 +- .../get_next_votes_sync_packet_handler.hpp | 4 +- .../get_pbft_sync_packet_handler.hpp | 4 +- .../pbft_sync_packet_handler.hpp | 4 +- .../status_packet_handler.hpp | 4 +- .../transaction_packet_handler.hpp | 4 +- .../packets_handlers/vote_packet_handler.hpp | 4 +- .../votes_bundle_packet_handler.hpp | 4 +- .../network/tarcap/stats/node_stats.hpp | 9 +- .../network/tarcap/taraxa_capability.hpp | 4 +- .../{tarcap => }/threadpool/packet_data.hpp | 4 +- .../threadpool/packets_blocking_mask.hpp | 6 +- .../{tarcap => }/threadpool/packets_queue.hpp | 6 +- .../threadpool/priority_queue.hpp | 6 +- .../threadpool/tarcap_thread_pool.hpp | 26 +-- .../get_next_votes_sync_packet_handler.hpp | 6 +- .../get_pbft_sync_packet_handler.hpp | 6 +- .../pbft_sync_packet_handler.hpp | 6 +- .../votes_bundle_packet_handler.hpp | 6 +- libraries/core_libs/network/src/network.cpp | 12 +- .../common/packet_handler.cpp | 6 +- .../dag_block_packet_handler.cpp | 5 +- .../dag_sync_packet_handler.cpp | 4 +- .../get_dag_sync_packet_handler.cpp | 4 +- .../get_next_votes_sync_packet_handler.cpp | 5 +- .../get_pbft_sync_packet_handler.cpp | 4 +- .../pbft_sync_packet_handler.cpp | 5 +- .../status_packet_handler.cpp | 4 +- .../transaction_packet_handler.cpp | 5 +- .../packets_handlers/vote_packet_handler.cpp | 4 +- .../votes_bundle_packet_handler.cpp | 6 +- .../network/src/tarcap/stats/node_stats.cpp | 4 +- .../network/src/tarcap/taraxa_capability.cpp | 5 +- .../{tarcap => }/threadpool/packet_data.cpp | 6 +- .../threadpool/packets_blocking_mask.cpp | 6 +- .../{tarcap => }/threadpool/packets_queue.cpp | 6 +- .../threadpool/priority_queue.cpp | 6 +- .../threadpool/tarcap_thread_pool.cpp | 24 +-- .../common/ext_votes_packet_handler.cpp | 2 +- .../get_next_votes_sync_packet_handler.cpp | 4 +- .../get_pbft_sync_packet_handler.cpp | 6 +- .../pbft_sync_packet_handler.cpp | 4 +- .../votes_bundle_packet_handler.cpp | 5 +- tests/tarcap_threadpool_test.cpp | 165 +++++++++--------- 49 files changed, 242 insertions(+), 215 deletions(-) rename libraries/core_libs/network/include/network/{tarcap => }/threadpool/packet_data.hpp (93%) rename libraries/core_libs/network/include/network/{tarcap => }/threadpool/packets_blocking_mask.hpp (96%) rename libraries/core_libs/network/include/network/{tarcap => }/threadpool/packets_queue.hpp (93%) rename libraries/core_libs/network/include/network/{tarcap => }/threadpool/priority_queue.hpp (95%) rename libraries/core_libs/network/include/network/{tarcap => }/threadpool/tarcap_thread_pool.hpp (74%) rename libraries/core_libs/network/src/{tarcap => }/threadpool/packet_data.cpp (93%) rename libraries/core_libs/network/src/{tarcap => }/threadpool/packets_blocking_mask.cpp (98%) rename libraries/core_libs/network/src/{tarcap => }/threadpool/packets_queue.cpp (90%) rename libraries/core_libs/network/src/{tarcap => }/threadpool/priority_queue.cpp (98%) rename libraries/core_libs/network/src/{tarcap => }/threadpool/tarcap_thread_pool.cpp (83%) diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index b4f03a4bcc..16d398dce1 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -74,8 +74,17 @@ class Network { private: util::ThreadPool tp_; std::shared_ptr host_; + + // All supported taraxa capabilities std::map> tarcaps_; + // Threadpool for packets + std::shared_ptr packets_tp_; + + // Threadpool for periodic and delayed events + // TODO: tp_ could be used for this instead + util::ThreadPool periodic_events_tp_; + LOG_OBJECTS_DEFINE }; diff --git a/libraries/core_libs/network/include/network/tarcap/packet_types.hpp b/libraries/core_libs/network/include/network/tarcap/packet_types.hpp index 2ec1ee2523..25ab9ed77d 100644 --- a/libraries/core_libs/network/include/network/tarcap/packet_types.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packet_types.hpp @@ -4,7 +4,7 @@ #include "common/types.hpp" -namespace taraxa::network::tarcap { +namespace taraxa::network { /** * @brief SubprotocolPacketType is used in networking layer to differentiate packet types @@ -66,4 +66,4 @@ inline std::string convertPacketTypeToString(SubprotocolPacketType packet_type) return "Unknown packet type: " + std::to_string(packet_type); } -} // namespace taraxa::network::tarcap +} // namespace taraxa::network diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/packet_handler.hpp index 84175306f6..f1bd96812a 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/packet_handler.hpp @@ -10,7 +10,7 @@ #include "network/tarcap/packets_handlers/common/exceptions.hpp" #include "network/tarcap/shared_states/peers_state.hpp" #include "network/tarcap/taraxa_peer.hpp" -#include "network/tarcap/threadpool/packet_data.hpp" +#include "network/threadpool/packet_data.hpp" namespace taraxa::network::tarcap { @@ -38,26 +38,26 @@ class PacketHandler { * * @param packet_data */ - void processPacket(const PacketData& packet_data); + void processPacket(const threadpool::PacketData& packet_data); void requestPbftNextVotesAtPeriodRound(const dev::p2p::NodeID& peerID, PbftPeriod pbft_period, PbftRound pbft_round); private: - void handle_caught_exception(std::string_view exception_msg, const PacketData& packet_data, + void handle_caught_exception(std::string_view exception_msg, const threadpool::PacketData& packet_data, dev::p2p::DisconnectReason disconnect_reason = dev::p2p::DisconnectReason::UserReason, bool set_peer_as_malicious = false); /** * @brief Main packet processing function */ - virtual void process(const PacketData& packet_data, const std::shared_ptr& peer) = 0; + virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) = 0; /** * @brief Validates packet rlp format - items count * * @throws InvalidRlpItemsCountException exception */ - virtual void validatePacketRlpFormat(const PacketData& packet_data) const = 0; + virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const = 0; protected: /** @@ -66,7 +66,7 @@ class PacketHandler { * @param packet_data * @throws InvalidRlpItemsCountException exception */ - void checkPacketRlpIsList(const PacketData& packet_data) const; + void checkPacketRlpIsList(const threadpool::PacketData& packet_data) const; bool sealAndSend(const dev::p2p::NodeID& nodeID, SubprotocolPacketType packet_type, dev::RLPStream&& rlp); void disconnect(const dev::p2p::NodeID& node_id, dev::p2p::DisconnectReason reason); diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_block_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_block_packet_handler.hpp index 884cdfe1bd..22197cdbd8 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_block_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_block_packet_handler.hpp @@ -27,8 +27,8 @@ class DagBlockPacketHandler final : public ExtSyncingPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::DagBlockPacket; private: - void validatePacketRlpFormat(const PacketData &packet_data) const override; - void process(const PacketData &packet_data, const std::shared_ptr &peer) override; + void validatePacketRlpFormat(const threadpool::PacketData &packet_data) const override; + void process(const threadpool::PacketData &packet_data, const std::shared_ptr &peer) override; std::shared_ptr test_state_; std::shared_ptr trx_mgr_{nullptr}; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_sync_packet_handler.hpp index f1b4367460..b94a0509e0 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_sync_packet_handler.hpp @@ -21,8 +21,8 @@ class DagSyncPacketHandler final : public ExtSyncingPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::DagSyncPacket; private: - void validatePacketRlpFormat(const PacketData& packet_data) const override; - void process(const PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; std::shared_ptr trx_mgr_{nullptr}; }; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_dag_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_dag_sync_packet_handler.hpp index 1187c29b5b..2da4ed381b 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_dag_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_dag_sync_packet_handler.hpp @@ -24,8 +24,8 @@ class GetDagSyncPacketHandler final : public PacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetDagSyncPacket; private: - void validatePacketRlpFormat(const PacketData& packet_data) const override; - void process(const PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; std::shared_ptr trx_mgr_; std::shared_ptr dag_mgr_; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp index ae7aae82de..32aaddd277 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp @@ -20,8 +20,8 @@ class GetNextVotesBundlePacketHandler final : public ExtVotesPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetNextVotesSyncPacket; private: - void validatePacketRlpFormat(const PacketData& packet_data) const override; - void process(const PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; }; } // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp index bccf58638f..957fb74e24 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp @@ -27,8 +27,8 @@ class GetPbftSyncPacketHandler final : public PacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetPbftSyncPacket; private: - void validatePacketRlpFormat(const PacketData& packet_data) const override; - void process(const PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; std::shared_ptr pbft_syncing_state_; std::shared_ptr pbft_chain_; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp index b69207d79f..c51ebef8b9 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp @@ -20,8 +20,8 @@ class PbftSyncPacketHandler final : public ExtSyncingPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::PbftSyncPacket; private: - void validatePacketRlpFormat(const PacketData& packet_data) const override; - void process(const PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; void pbftSyncComplete(); void delayedPbftSync(int counter); diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/status_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/status_packet_handler.hpp index c2285357fe..f1c8f219a1 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/status_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/status_packet_handler.hpp @@ -19,8 +19,8 @@ class StatusPacketHandler final : public ExtSyncingPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::StatusPacket; private: - void validatePacketRlpFormat(const PacketData& packet_data) const override; - void process(const PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; static constexpr uint16_t kInitialStatusPacketItemsCount = 11; static constexpr uint16_t kStandardStatusPacketItemsCount = 4; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/transaction_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/transaction_packet_handler.hpp index cd71341cf5..5b33e485a4 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/transaction_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/transaction_packet_handler.hpp @@ -47,8 +47,8 @@ class TransactionPacketHandler final : public PacketHandler { void onNewTransactions(const SharedTransactions& transactions); private: - void validatePacketRlpFormat(const PacketData& packet_data) const override; - void process(const PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; std::shared_ptr trx_mgr_; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/vote_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/vote_packet_handler.hpp index 13c30d346a..48aec1b3c2 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/vote_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/vote_packet_handler.hpp @@ -27,8 +27,8 @@ class VotePacketHandler final : public ExtVotesPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::VotePacket; private: - void validatePacketRlpFormat(const PacketData& packet_data) const override; - void process(const PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; private: const size_t kVotePacketSize{1}; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp index 332ade7ef7..536c883b67 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp @@ -25,8 +25,8 @@ class VotesBundlePacketHandler final : public ExtVotesPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::VotesBundlePacket; private: - void validatePacketRlpFormat(const PacketData& packet_data) const override; - void process(const PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; }; } // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp b/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp index 73ba947300..605297e0b0 100644 --- a/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp +++ b/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp @@ -12,12 +12,15 @@ class DagManager; class TransactionManager; } // namespace taraxa +namespace taraxa::network::threadpool { +class PacketsThreadPool; +} + namespace taraxa::network::tarcap { class PeersState; class PbftSyncingState; class TimePeriodPacketsStats; -class TarcapThreadPool; class NodeStats { public: @@ -25,7 +28,7 @@ class NodeStats { std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr vote_mgr, std::shared_ptr trx_mgr, std::shared_ptr packets_stats, - std::shared_ptr thread_pool, const addr_t &node_addr); + std::shared_ptr thread_pool, const addr_t &node_addr); void logNodeStats(); uint64_t syncTimeSeconds() const; @@ -40,7 +43,7 @@ class NodeStats { std::shared_ptr vote_mgr_; std::shared_ptr trx_mgr_; std::shared_ptr packets_stats_; - std::shared_ptr thread_pool_; + std::shared_ptr thread_pool_; level_t local_max_level_in_dag_prev_interval_{0}; uint64_t local_pbft_round_prev_interval_{0}; diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp index 95042dc94a..6fa956d1b4 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp @@ -13,8 +13,8 @@ #include "network/tarcap/shared_states/peers_state.hpp" #include "network/tarcap/shared_states/test_state.hpp" #include "network/tarcap/stats/node_stats.hpp" +#include "network/threadpool/tarcap_thread_pool.hpp" #include "pbft/pbft_chain.hpp" -#include "threadpool/tarcap_thread_pool.hpp" namespace taraxa { class DbStorage; @@ -129,7 +129,7 @@ class TaraxaCapability : public dev::p2p::CapabilityFace { std::shared_ptr packets_handlers_; // Main Threadpool for processing packets - std::shared_ptr thread_pool_; + std::shared_ptr thread_pool_; // Threadpool for periodic and delayed events std::shared_ptr periodic_events_tp_; diff --git a/libraries/core_libs/network/include/network/tarcap/threadpool/packet_data.hpp b/libraries/core_libs/network/include/network/threadpool/packet_data.hpp similarity index 93% rename from libraries/core_libs/network/include/network/tarcap/threadpool/packet_data.hpp rename to libraries/core_libs/network/include/network/threadpool/packet_data.hpp index 66545a1911..af521b17ac 100644 --- a/libraries/core_libs/network/include/network/tarcap/threadpool/packet_data.hpp +++ b/libraries/core_libs/network/include/network/threadpool/packet_data.hpp @@ -7,7 +7,7 @@ #include "json/value.h" #include "network/tarcap/packet_types.hpp" -namespace taraxa::network::tarcap { +namespace taraxa::network::threadpool { class PacketData { public: @@ -46,4 +46,4 @@ class PacketData { dev::RLP rlp_; }; -} // namespace taraxa::network::tarcap \ No newline at end of file +} // namespace taraxa::network::threadpool \ No newline at end of file diff --git a/libraries/core_libs/network/include/network/tarcap/threadpool/packets_blocking_mask.hpp b/libraries/core_libs/network/include/network/threadpool/packets_blocking_mask.hpp similarity index 96% rename from libraries/core_libs/network/include/network/tarcap/threadpool/packets_blocking_mask.hpp rename to libraries/core_libs/network/include/network/threadpool/packets_blocking_mask.hpp index 512bd7a000..c4c19cc9d4 100644 --- a/libraries/core_libs/network/include/network/tarcap/threadpool/packets_blocking_mask.hpp +++ b/libraries/core_libs/network/include/network/threadpool/packets_blocking_mask.hpp @@ -7,9 +7,9 @@ #include #include "network/tarcap/packet_types.hpp" -#include "network/tarcap/threadpool/packet_data.hpp" +#include "network/threadpool/packet_data.hpp" -namespace taraxa::network::tarcap { +namespace taraxa::network::threadpool { class PacketsBlockingMask { public: @@ -69,4 +69,4 @@ class PacketsBlockingMask { std::map processing_dag_blocks_; }; -} // namespace taraxa::network::tarcap +} // namespace taraxa::network::threadpool diff --git a/libraries/core_libs/network/include/network/tarcap/threadpool/packets_queue.hpp b/libraries/core_libs/network/include/network/threadpool/packets_queue.hpp similarity index 93% rename from libraries/core_libs/network/include/network/tarcap/threadpool/packets_queue.hpp rename to libraries/core_libs/network/include/network/threadpool/packets_queue.hpp index 6d9d1215e7..5f5c321897 100644 --- a/libraries/core_libs/network/include/network/tarcap/threadpool/packets_queue.hpp +++ b/libraries/core_libs/network/include/network/threadpool/packets_queue.hpp @@ -3,10 +3,10 @@ #include #include -#include "network/tarcap/threadpool/packets_blocking_mask.hpp" +#include "network/threadpool/packets_blocking_mask.hpp" #include "packet_data.hpp" -namespace taraxa::network::tarcap { +namespace taraxa::network::threadpool { class PacketsQueue { public: @@ -84,4 +84,4 @@ class PacketsQueue { std::atomic act_packets_count_{0}; }; -} // namespace taraxa::network::tarcap +} // namespace taraxa::network::threadpool diff --git a/libraries/core_libs/network/include/network/tarcap/threadpool/priority_queue.hpp b/libraries/core_libs/network/include/network/threadpool/priority_queue.hpp similarity index 95% rename from libraries/core_libs/network/include/network/tarcap/threadpool/priority_queue.hpp rename to libraries/core_libs/network/include/network/threadpool/priority_queue.hpp index b2c1fac9da..b76bf4d6b4 100644 --- a/libraries/core_libs/network/include/network/tarcap/threadpool/priority_queue.hpp +++ b/libraries/core_libs/network/include/network/threadpool/priority_queue.hpp @@ -7,10 +7,10 @@ #include "logger/logger.hpp" #include "network/tarcap/packet_types.hpp" -#include "network/tarcap/threadpool/packets_blocking_mask.hpp" +#include "network/threadpool/packets_blocking_mask.hpp" #include "packets_queue.hpp" -namespace taraxa::network::tarcap { +namespace taraxa::network::threadpool { class PriorityQueue { public: @@ -86,4 +86,4 @@ class PriorityQueue { std::atomic act_total_workers_count_; }; -} // namespace taraxa::network::tarcap +} // namespace taraxa::network::threadpool diff --git a/libraries/core_libs/network/include/network/tarcap/threadpool/tarcap_thread_pool.hpp b/libraries/core_libs/network/include/network/threadpool/tarcap_thread_pool.hpp similarity index 74% rename from libraries/core_libs/network/include/network/tarcap/threadpool/tarcap_thread_pool.hpp rename to libraries/core_libs/network/include/network/threadpool/tarcap_thread_pool.hpp index 287840aa78..11b98f8c15 100644 --- a/libraries/core_libs/network/include/network/tarcap/threadpool/tarcap_thread_pool.hpp +++ b/libraries/core_libs/network/include/network/threadpool/tarcap_thread_pool.hpp @@ -9,24 +9,26 @@ #include "priority_queue.hpp" namespace taraxa::network::tarcap { - class PacketsHandler; +} + +namespace taraxa::network::threadpool { /** - * @brief Taraxa ThreadPool that is supposed to process incoming packets in concurrent way + * @brief PacketsThreadPool for concurrent packets processing **/ -class TarcapThreadPool { +class PacketsThreadPool { public: /** * @param workers_num Number of workers **/ - TarcapThreadPool(size_t workers_num = 10, const addr_t& node_addr = {}); - ~TarcapThreadPool(); + PacketsThreadPool(size_t workers_num = 10, const addr_t& node_addr = {}); + ~PacketsThreadPool(); - TarcapThreadPool(const TarcapThreadPool&) = delete; - TarcapThreadPool& operator=(const TarcapThreadPool&) = delete; - TarcapThreadPool(TarcapThreadPool&&) = delete; - TarcapThreadPool& operator=(TarcapThreadPool&&) = delete; + PacketsThreadPool(const PacketsThreadPool&) = delete; + PacketsThreadPool& operator=(const PacketsThreadPool&) = delete; + PacketsThreadPool(PacketsThreadPool&&) = delete; + PacketsThreadPool& operator=(PacketsThreadPool&&) = delete; /** * @brief Push the given element value to the end of the queue. Used for r-values @@ -55,7 +57,7 @@ class TarcapThreadPool { * * @param packets_handlers */ - void setPacketsHandlers(std::shared_ptr packets_handlers); + void setPacketsHandlers(std::shared_ptr packets_handlers); /** * @brief Returns actual size of all priority queues (thread-safe) @@ -73,7 +75,7 @@ class TarcapThreadPool { const size_t workers_num_; // Common packets handler - std::shared_ptr packets_handlers_; + std::shared_ptr packets_handlers_; // If true, stop processing packets and join all workers threads std::atomic stopProcessing_{false}; @@ -94,4 +96,4 @@ class TarcapThreadPool { std::vector workers_; }; -} // namespace taraxa::network::tarcap +} // namespace taraxa::network::threadpool diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp index b977283a48..01498ca61d 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp @@ -17,11 +17,11 @@ class GetNextVotesBundlePacketHandler final : public ExtVotesPacketHandler { std::shared_ptr vote_mgr, const addr_t& node_addr); // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::GetNextVotesSyncPacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetNextVotesSyncPacket; private: - void validatePacketRlpFormat(const tarcap::PacketData& packet_data) const override; - void process(const tarcap::PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; }; } // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp index 40172320d8..f60d3a35b7 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp @@ -26,11 +26,11 @@ class GetPbftSyncPacketHandler final : public tarcap::PacketHandler { bool pbft_chain_synced); // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::GetPbftSyncPacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetPbftSyncPacket; private: - void validatePacketRlpFormat(const tarcap::PacketData& packet_data) const override; - void process(const tarcap::PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; std::shared_ptr pbft_syncing_state_; std::shared_ptr pbft_chain_; diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp index 1115c151a2..10cc817904 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp @@ -18,11 +18,11 @@ class PbftSyncPacketHandler final : public tarcap::ExtSyncingPacketHandler { void handleMaliciousSyncPeer(dev::p2p::NodeID const& id); // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::PbftSyncPacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::PbftSyncPacket; private: - void validatePacketRlpFormat(const tarcap::PacketData& packet_data) const override; - void process(const tarcap::PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; void pbftSyncComplete(); void delayedPbftSync(int counter); diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp index 1770ace5cb..628314dabf 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp @@ -22,11 +22,11 @@ class VotesBundlePacketHandler final : public ExtVotesPacketHandler { const std::optional& exclude_node = {}); // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::VotesBundlePacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::VotesBundlePacket; private: - void validatePacketRlpFormat(const tarcap::PacketData& packet_data) const override; - void process(const tarcap::PacketData& packet_data, const std::shared_ptr& peer) override; + void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; }; } // namespace taraxa::network::v1_tarcap diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index dce7f753b0..27ffa8372f 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -22,7 +22,9 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr) - : tp_(config.network.num_threads, false) { + : tp_(config.network.num_threads, false), + packets_tp_(std::make_shared(config.network.packets_processing_threads, + key.address())) { auto const &node_addr = key.address(); LOG_OBJECTS_CREATE("NETWORK"); LOG(log_nf_) << "Read Network Config: " << std::endl << config.network << std::endl; @@ -90,6 +92,10 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, Network::~Network() { tp_.stop(); + packets_tp_->stopProcessing(); + periodic_events_tp_.stop(); + + // TODO: remove once packets_tp_ and periodic_events_tp_ are moved from tarcaps to network for (auto &tarcap : host_->getSupportedCapabilities()) { std::static_pointer_cast(tarcap.second.ref)->stop(); } @@ -97,6 +103,10 @@ Network::~Network() { void Network::start() { tp_.start(); + packets_tp_->startProcessing(); + periodic_events_tp_.start(); + + // TODO: remove once packets_tp_ and periodic_events_tp_ are moved from tarcaps to network for (auto &tarcap : host_->getSupportedCapabilities()) { std::static_pointer_cast(tarcap.second.ref)->start(); } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp index e9a2c997e7..5ab82554dd 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp @@ -11,13 +11,13 @@ PacketHandler::PacketHandler(const FullNodeConfig& conf, std::shared_ptr &peer) { +void DagBlockPacketHandler::process(const threadpool::PacketData &packet_data, + const std::shared_ptr &peer) { DagBlock block(packet_data.rlp_); blk_hash_t const hash = block.getHash(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/dag_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/dag_sync_packet_handler.cpp index fdef88832d..0105dd59a0 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/dag_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/dag_sync_packet_handler.cpp @@ -20,13 +20,13 @@ DagSyncPacketHandler::DagSyncPacketHandler(const FullNodeConfig& conf, std::shar "DAG_SYNC_PH"), trx_mgr_(std::move(trx_mgr)) {} -void DagSyncPacketHandler::validatePacketRlpFormat(const PacketData& packet_data) const { +void DagSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketData& packet_data) const { if (constexpr size_t required_size = 4; packet_data.rlp_.itemCount() != required_size) { throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); } } -void DagSyncPacketHandler::process(const PacketData& packet_data, const std::shared_ptr& peer) { +void DagSyncPacketHandler::process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) { auto it = packet_data.rlp_.begin(); const auto request_period = (*it++).toInt(); const auto response_period = (*it++).toInt(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/get_dag_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/get_dag_sync_packet_handler.cpp index 4739edef17..4021dd78aa 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/get_dag_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/get_dag_sync_packet_handler.cpp @@ -15,13 +15,13 @@ GetDagSyncPacketHandler::GetDagSyncPacketHandler(const FullNodeConfig &conf, std dag_mgr_(std::move(dag_mgr)), db_(std::move(db)) {} -void GetDagSyncPacketHandler::validatePacketRlpFormat(const PacketData &packet_data) const { +void GetDagSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { if (constexpr size_t required_size = 2; packet_data.rlp_.itemCount() != required_size) { throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); } } -void GetDagSyncPacketHandler::process(const PacketData &packet_data, +void GetDagSyncPacketHandler::process(const threadpool::PacketData &packet_data, [[maybe_unused]] const std::shared_ptr &peer) { if (!peer->requestDagSyncingAllowed()) { // This should not be possible for honest node diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp index c714ff3fd1..596c20a2ea 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp @@ -12,13 +12,14 @@ GetNextVotesBundlePacketHandler::GetNextVotesBundlePacketHandler( : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), std::move(pbft_chain), std::move(vote_mgr), node_addr, "GET_NEXT_VOTES_SYNC_PH") {} -void GetNextVotesBundlePacketHandler::validatePacketRlpFormat(const PacketData &packet_data) const { +void GetNextVotesBundlePacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { if (constexpr size_t required_size = 2; packet_data.rlp_.itemCount() != required_size) { throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); } } -void GetNextVotesBundlePacketHandler::process(const PacketData &packet_data, const std::shared_ptr &peer) { +void GetNextVotesBundlePacketHandler::process(const threadpool::PacketData &packet_data, + const std::shared_ptr &peer) { LOG(log_dg_) << "Received GetNextVotesSyncPacket request"; const PbftPeriod peer_pbft_period = packet_data.rlp_[0].toInt(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp index 299eee79ae..842ea217fe 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp @@ -21,13 +21,13 @@ GetPbftSyncPacketHandler::GetPbftSyncPacketHandler(const FullNodeConfig &conf, s vote_mgr_(std::move(vote_mgr)), db_(std::move(db)) {} -void GetPbftSyncPacketHandler::validatePacketRlpFormat(const PacketData &packet_data) const { +void GetPbftSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { if (constexpr size_t required_size = 1; packet_data.rlp_.itemCount() != required_size) { throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); } } -void GetPbftSyncPacketHandler::process(const PacketData &packet_data, +void GetPbftSyncPacketHandler::process(const threadpool::PacketData &packet_data, [[maybe_unused]] const std::shared_ptr &peer) { LOG(log_tr_) << "Received GetPbftSyncPacket Block"; diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp index 240d5221b9..49aef5d151 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp @@ -23,7 +23,7 @@ PbftSyncPacketHandler::PbftSyncPacketHandler(const FullNodeConfig &conf, std::sh vote_mgr_(std::move(vote_mgr)), periodic_events_tp_(periodic_events_tp) {} -void PbftSyncPacketHandler::validatePacketRlpFormat(const PacketData &packet_data) const { +void PbftSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { if (packet_data.rlp_.itemCount() != kStandardPacketSize && packet_data.rlp_.itemCount() != kChainSyncedPacketSize) { throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), kStandardPacketSize); } @@ -36,7 +36,8 @@ void PbftSyncPacketHandler::validatePacketRlpFormat(const PacketData &packet_dat } } -void PbftSyncPacketHandler::process(const PacketData &packet_data, const std::shared_ptr &peer) { +void PbftSyncPacketHandler::process(const threadpool::PacketData &packet_data, + const std::shared_ptr &peer) { // Note: no need to consider possible race conditions due to concurrent processing as it is // disabled on priority_queue blocking dependencies level const auto syncing_peer = pbft_syncing_state_->syncingPeer(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/status_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/status_packet_handler.cpp index c31c20e6f8..3480888bd2 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/status_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/status_packet_handler.cpp @@ -21,7 +21,7 @@ StatusPacketHandler::StatusPacketHandler(const FullNodeConfig& conf, std::shared "STATUS_PH"), kGenesisHash(genesis_hash) {} -void StatusPacketHandler::validatePacketRlpFormat(const PacketData& packet_data) const { +void StatusPacketHandler::validatePacketRlpFormat(const threadpool::PacketData& packet_data) const { if (const auto items_count = packet_data.rlp_.itemCount(); items_count != kInitialStatusPacketItemsCount && items_count != kStandardStatusPacketItemsCount) { throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), @@ -29,7 +29,7 @@ void StatusPacketHandler::validatePacketRlpFormat(const PacketData& packet_data) } } -void StatusPacketHandler::process(const PacketData& packet_data, const std::shared_ptr& peer) { +void StatusPacketHandler::process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) { // Important !!! Use only "selected_peer" and not "peer" in this function as "peer" might be nullptr auto selected_peer = peer; const auto pbft_synced_period = pbft_mgr_->pbftSyncingPeriod(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/transaction_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/transaction_packet_handler.cpp index 6132d0091c..04dc574806 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/transaction_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/transaction_packet_handler.cpp @@ -16,7 +16,7 @@ TransactionPacketHandler::TransactionPacketHandler(const FullNodeConfig &conf, s trx_mgr_(std::move(trx_mgr)), test_state_(std::move(test_state)) {} -void TransactionPacketHandler::validatePacketRlpFormat(const PacketData &packet_data) const { +void TransactionPacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); if (items != kTransactionPacketItemCount) { throw InvalidRlpItemsCountException(packet_data.type_str_, items, kTransactionPacketItemCount); @@ -32,7 +32,8 @@ void TransactionPacketHandler::validatePacketRlpFormat(const PacketData &packet_ } } -inline void TransactionPacketHandler::process(const PacketData &packet_data, const std::shared_ptr &peer) { +inline void TransactionPacketHandler::process(const threadpool::PacketData &packet_data, + const std::shared_ptr &peer) { std::vector received_transactions; const auto transaction_count = packet_data.rlp_[0].itemCount(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/vote_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/vote_packet_handler.cpp index 5810b0518a..a483fd9e93 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/vote_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/vote_packet_handler.cpp @@ -12,7 +12,7 @@ VotePacketHandler::VotePacketHandler(const FullNodeConfig &conf, std::shared_ptr : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), std::move(pbft_chain), std::move(vote_mgr), node_addr, "PBFT_VOTE_PH") {} -void VotePacketHandler::validatePacketRlpFormat([[maybe_unused]] const PacketData &packet_data) const { +void VotePacketHandler::validatePacketRlpFormat([[maybe_unused]] const threadpool::PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); // Vote packet can contain either just a vote or vote + block + peer_chain_size if (items != kVotePacketSize && items != kExtendedVotePacketSize) { @@ -20,7 +20,7 @@ void VotePacketHandler::validatePacketRlpFormat([[maybe_unused]] const PacketDat } } -void VotePacketHandler::process(const PacketData &packet_data, const std::shared_ptr &peer) { +void VotePacketHandler::process(const threadpool::PacketData &packet_data, const std::shared_ptr &peer) { const auto [current_pbft_round, current_pbft_period] = pbft_mgr_->getPbftRoundAndPeriod(); // Optional packet items diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp index 7516623840..01190fd698 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp @@ -14,7 +14,8 @@ VotesBundlePacketHandler::VotesBundlePacketHandler(const FullNodeConfig &conf, s : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), std::move(pbft_chain), std::move(vote_mgr), node_addr, "VOTES_SYNC_PH") {} -void VotesBundlePacketHandler::validatePacketRlpFormat([[maybe_unused]] const PacketData &packet_data) const { +void VotesBundlePacketHandler::validatePacketRlpFormat( + [[maybe_unused]] const threadpool::PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); if (items != kVotesBundleRlpSize) { throw InvalidRlpItemsCountException(packet_data.type_str_, items, kVotesBundleRlpSize); @@ -26,7 +27,8 @@ void VotesBundlePacketHandler::validatePacketRlpFormat([[maybe_unused]] const Pa } } -void VotesBundlePacketHandler::process(const PacketData &packet_data, const std::shared_ptr &peer) { +void VotesBundlePacketHandler::process(const threadpool::PacketData &packet_data, + const std::shared_ptr &peer) { const auto [current_pbft_round, current_pbft_period] = pbft_mgr_->getPbftRoundAndPeriod(); const auto votes_bundle_block_hash = packet_data.rlp_[0].toHash(); diff --git a/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp b/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp index a3a51b1b4b..47dab60a48 100644 --- a/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp +++ b/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp @@ -6,7 +6,7 @@ #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "network/tarcap/shared_states/peers_state.hpp" #include "network/tarcap/stats/time_period_packets_stats.hpp" -#include "network/tarcap/threadpool/tarcap_thread_pool.hpp" +#include "network/threadpool/tarcap_thread_pool.hpp" #include "pbft/pbft_chain.hpp" #include "pbft/pbft_manager.hpp" #include "transaction/transaction_manager.hpp" @@ -17,7 +17,7 @@ NodeStats::NodeStats(std::shared_ptr peers_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr vote_mgr, std::shared_ptr trx_mgr, std::shared_ptr packets_stats, - std::shared_ptr thread_pool, const addr_t &node_addr) + std::shared_ptr thread_pool, const addr_t &node_addr) : peers_state_(std::move(peers_state)), pbft_syncing_state_(std::move(pbft_syncing_state)), pbft_chain_(std::move(pbft_chain)), diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index aa7106e997..321346f385 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -35,7 +35,8 @@ TaraxaCapability::TaraxaCapability(std::weak_ptr host, const dev pbft_syncing_state_(std::make_shared(conf.network.deep_syncing_threshold)), node_stats_(nullptr), packets_handlers_(std::make_shared()), - thread_pool_(std::make_shared(conf.network.packets_processing_threads, key.address())), + thread_pool_( + std::make_shared(conf.network.packets_processing_threads, key.address())), periodic_events_tp_(std::make_shared(kPeriodicEventsThreadCount, false)), pub_key_(key.pub()) { const auto &node_addr = key.address(); @@ -352,7 +353,7 @@ void TaraxaCapability::interpretCapabilityPacket(std::weak_ptrpush(PacketData(packet_type, node_id, _r.data().toBytes())); + thread_pool_->push(threadpool::PacketData(packet_type, node_id, _r.data().toBytes())); } inline bool TaraxaCapability::filterSyncIrrelevantPackets(SubprotocolPacketType packet_type) const { diff --git a/libraries/core_libs/network/src/tarcap/threadpool/packet_data.cpp b/libraries/core_libs/network/src/threadpool/packet_data.cpp similarity index 93% rename from libraries/core_libs/network/src/tarcap/threadpool/packet_data.cpp rename to libraries/core_libs/network/src/threadpool/packet_data.cpp index 00d1cefc3f..5f8c187c46 100644 --- a/libraries/core_libs/network/src/tarcap/threadpool/packet_data.cpp +++ b/libraries/core_libs/network/src/threadpool/packet_data.cpp @@ -1,6 +1,6 @@ -#include "network/tarcap/threadpool/packet_data.hpp" +#include "network/threadpool/packet_data.hpp" -namespace taraxa::network::tarcap { +namespace taraxa::network::threadpool { PacketData::PacketData(SubprotocolPacketType type, const dev::p2p::NodeID& from_node_id, std::vector&& bytes) @@ -51,4 +51,4 @@ Json::Value PacketData::getPacketDataJson() const { return ret; } -} // namespace taraxa::network::tarcap +} // namespace taraxa::network::threadpool diff --git a/libraries/core_libs/network/src/tarcap/threadpool/packets_blocking_mask.cpp b/libraries/core_libs/network/src/threadpool/packets_blocking_mask.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/threadpool/packets_blocking_mask.cpp rename to libraries/core_libs/network/src/threadpool/packets_blocking_mask.cpp index 3c0b878365..dd94bd2715 100644 --- a/libraries/core_libs/network/src/tarcap/threadpool/packets_blocking_mask.cpp +++ b/libraries/core_libs/network/src/threadpool/packets_blocking_mask.cpp @@ -1,8 +1,8 @@ -#include "network/tarcap/threadpool/packets_blocking_mask.hpp" +#include "network/threadpool/packets_blocking_mask.hpp" #include "dag/dag_block.hpp" -namespace taraxa::network::tarcap { +namespace taraxa::network::threadpool { void PacketsBlockingMask::markPacketAsHardBlocked(const PacketData& blocking_packet, SubprotocolPacketType packet_type_to_block) { @@ -209,4 +209,4 @@ bool PacketsBlockingMask::isPacketBlocked(const PacketData& packet_data) const { return false; } -} // namespace taraxa::network::tarcap \ No newline at end of file +} // namespace taraxa::network::threadpool \ No newline at end of file diff --git a/libraries/core_libs/network/src/tarcap/threadpool/packets_queue.cpp b/libraries/core_libs/network/src/threadpool/packets_queue.cpp similarity index 90% rename from libraries/core_libs/network/src/tarcap/threadpool/packets_queue.cpp rename to libraries/core_libs/network/src/threadpool/packets_queue.cpp index ee64c67b4c..3cbcb8d432 100644 --- a/libraries/core_libs/network/src/tarcap/threadpool/packets_queue.cpp +++ b/libraries/core_libs/network/src/threadpool/packets_queue.cpp @@ -1,6 +1,6 @@ -#include "network/tarcap/threadpool/packets_queue.hpp" +#include "network/threadpool/packets_queue.hpp" -namespace taraxa::network::tarcap { +namespace taraxa::network::threadpool { bool PacketsQueue::maxWorkersCountReached() const { if (act_workers_count_ >= kMaxWorkersCount_) { @@ -50,4 +50,4 @@ size_t PacketsQueue::size() const { return act_packets_count_; } size_t PacketsQueue::getActiveWorkersNum() const { return act_workers_count_; } -} // namespace taraxa::network::tarcap \ No newline at end of file +} // namespace taraxa::network::threadpool \ No newline at end of file diff --git a/libraries/core_libs/network/src/tarcap/threadpool/priority_queue.cpp b/libraries/core_libs/network/src/threadpool/priority_queue.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/threadpool/priority_queue.cpp rename to libraries/core_libs/network/src/threadpool/priority_queue.cpp index 220a0131f8..a2374b3163 100644 --- a/libraries/core_libs/network/src/tarcap/threadpool/priority_queue.cpp +++ b/libraries/core_libs/network/src/threadpool/priority_queue.cpp @@ -1,6 +1,6 @@ -#include "network/tarcap/threadpool/priority_queue.hpp" +#include "network/threadpool/priority_queue.hpp" -namespace taraxa::network::tarcap { +namespace taraxa::network::threadpool { PriorityQueue::PriorityQueue(size_t tp_workers_count, const addr_t& node_addr) : blocked_packets_mask_(), MAX_TOTAL_WORKERS_COUNT(tp_workers_count), act_total_workers_count_(0) { @@ -215,4 +215,4 @@ size_t PriorityQueue::getPrirotityQueueSize(PacketData::PacketPriority priority) return packets_queues_[priority].size(); } -} // namespace taraxa::network::tarcap +} // namespace taraxa::network::threadpool diff --git a/libraries/core_libs/network/src/tarcap/threadpool/tarcap_thread_pool.cpp b/libraries/core_libs/network/src/threadpool/tarcap_thread_pool.cpp similarity index 83% rename from libraries/core_libs/network/src/tarcap/threadpool/tarcap_thread_pool.cpp rename to libraries/core_libs/network/src/threadpool/tarcap_thread_pool.cpp index a53735d25d..445af95873 100644 --- a/libraries/core_libs/network/src/tarcap/threadpool/tarcap_thread_pool.cpp +++ b/libraries/core_libs/network/src/threadpool/tarcap_thread_pool.cpp @@ -1,10 +1,10 @@ -#include "network/tarcap/threadpool/tarcap_thread_pool.hpp" +#include "network/threadpool/tarcap_thread_pool.hpp" #include "network/tarcap/packets_handler.hpp" -namespace taraxa::network::tarcap { +namespace taraxa::network::threadpool { -TarcapThreadPool::TarcapThreadPool(size_t workers_num, const addr_t& node_addr) +PacketsThreadPool::PacketsThreadPool(size_t workers_num, const addr_t& node_addr) : workers_num_(workers_num), packets_handlers_(nullptr), stopProcessing_(false), @@ -16,7 +16,7 @@ TarcapThreadPool::TarcapThreadPool(size_t workers_num, const addr_t& node_addr) LOG_OBJECTS_CREATE("TARCAP_TP"); } -TarcapThreadPool::~TarcapThreadPool() { +PacketsThreadPool::~PacketsThreadPool() { stopProcessing(); for (auto& worker : workers_) { @@ -31,7 +31,7 @@ TarcapThreadPool::~TarcapThreadPool() { * * @return packet unique ID. In case push was not successful, empty optional is returned **/ -std::optional TarcapThreadPool::push(PacketData&& packet_data) { +std::optional PacketsThreadPool::push(PacketData&& packet_data) { if (stopProcessing_) { LOG(log_wr_) << "Trying to push packet while tp processing is stopped"; return {}; @@ -55,12 +55,12 @@ std::optional TarcapThreadPool::push(PacketData&& packet_data) { return {packet_unique_id}; } -void TarcapThreadPool::startProcessing() { +void PacketsThreadPool::startProcessing() { assert(packets_handlers_ != nullptr); try { for (size_t idx = 0; idx < workers_num_; idx++) { - workers_.emplace_back(&TarcapThreadPool::processPacket, this, idx); + workers_.emplace_back(&PacketsThreadPool::processPacket, this, idx); } } catch (...) { stopProcessing(); @@ -69,7 +69,7 @@ void TarcapThreadPool::startProcessing() { } } -void TarcapThreadPool::stopProcessing() { +void PacketsThreadPool::stopProcessing() { stopProcessing_ = true; cond_var_.notify_all(); } @@ -77,7 +77,7 @@ void TarcapThreadPool::stopProcessing() { /** * @brief Threadpool sycnchronized processing function, which calls user-defined custom processing function **/ -void TarcapThreadPool::processPacket(size_t worker_id) { +void PacketsThreadPool::processPacket(size_t worker_id) { LOG(log_dg_) << "Worker (" << worker_id << ") started"; std::unique_lock lock(queue_mutex_, std::defer_lock); @@ -125,14 +125,14 @@ void TarcapThreadPool::processPacket(size_t worker_id) { } } -void TarcapThreadPool::setPacketsHandlers(std::shared_ptr packets_handlers) { +void PacketsThreadPool::setPacketsHandlers(std::shared_ptr packets_handlers) { packets_handlers_ = std::move(packets_handlers); } -std::tuple TarcapThreadPool::getQueueSize() const { +std::tuple PacketsThreadPool::getQueueSize() const { return {queue_.getPrirotityQueueSize(PacketData::PacketPriority::High), queue_.getPrirotityQueueSize(PacketData::PacketPriority::Mid), queue_.getPrirotityQueueSize(PacketData::PacketPriority::Low)}; } -} // namespace taraxa::network::tarcap \ No newline at end of file +} // namespace taraxa::network::threadpool \ No newline at end of file diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp index b1f4ed8fea..ebd2c2b4a1 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp @@ -32,7 +32,7 @@ void ExtVotesPacketHandler::sendPbftVotesBundle(const std::shared_ptrgetHash() << " to peer " << peer->getId(); } - if (sealAndSend(peer->getId(), tarcap::SubprotocolPacketType::VotesBundlePacket, std::move(s))) { + if (sealAndSend(peer->getId(), SubprotocolPacketType::VotesBundlePacket, std::move(s))) { LOG(log_dg_) << count << " PBFT votes to were sent to " << peer->getId(); for (auto i = index; i < index + count; i++) { peer->markVoteAsKnown(votes[i]->getHash()); diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_next_votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_next_votes_sync_packet_handler.cpp index 3857b4bc8c..2abd6e795b 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_next_votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_next_votes_sync_packet_handler.cpp @@ -12,13 +12,13 @@ GetNextVotesBundlePacketHandler::GetNextVotesBundlePacketHandler( : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), std::move(pbft_chain), std::move(vote_mgr), node_addr, "V1_GET_NEXT_VOTES_SYNC_PH") {} -void GetNextVotesBundlePacketHandler::validatePacketRlpFormat(const tarcap::PacketData &packet_data) const { +void GetNextVotesBundlePacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { if (constexpr size_t required_size = 2; packet_data.rlp_.itemCount() != required_size) { throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); } } -void GetNextVotesBundlePacketHandler::process(const tarcap::PacketData &packet_data, +void GetNextVotesBundlePacketHandler::process(const threadpool::PacketData &packet_data, const std::shared_ptr &peer) { LOG(log_dg_) << "Received GetNextVotesSyncPacket request"; diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp index 25ecca6314..2335d14aad 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp @@ -21,13 +21,13 @@ GetPbftSyncPacketHandler::GetPbftSyncPacketHandler(const FullNodeConfig &conf, vote_mgr_(std::move(vote_mgr)), db_(std::move(db)) {} -void GetPbftSyncPacketHandler::validatePacketRlpFormat(const tarcap::PacketData &packet_data) const { +void GetPbftSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { if (constexpr size_t required_size = 1; packet_data.rlp_.itemCount() != required_size) { throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); } } -void GetPbftSyncPacketHandler::process(const tarcap::PacketData &packet_data, +void GetPbftSyncPacketHandler::process(const threadpool::PacketData &packet_data, [[maybe_unused]] const std::shared_ptr &peer) { LOG(log_tr_) << "Received GetPbftSyncPacket Block"; @@ -119,7 +119,7 @@ void GetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, P s.appendRaw(transformPeriodDataRlpToV1(data)); } LOG(log_dg_) << "Sending PbftSyncPacket period " << block_period << " to " << peer_id; - sealAndSend(peer_id, tarcap::SubprotocolPacketType::PbftSyncPacket, std::move(s)); + sealAndSend(peer_id, SubprotocolPacketType::PbftSyncPacket, std::move(s)); } } diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp index 18c504e55a..90b50d339c 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp @@ -20,7 +20,7 @@ PbftSyncPacketHandler::PbftSyncPacketHandler( vote_mgr_(std::move(vote_mgr)), periodic_events_tp_(periodic_events_tp) {} -void PbftSyncPacketHandler::validatePacketRlpFormat(const tarcap::PacketData &packet_data) const { +void PbftSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { if (packet_data.rlp_.itemCount() != kStandardPacketSize && packet_data.rlp_.itemCount() != kChainSyncedPacketSize) { throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), kStandardPacketSize); } @@ -33,7 +33,7 @@ void PbftSyncPacketHandler::validatePacketRlpFormat(const tarcap::PacketData &pa } } -void PbftSyncPacketHandler::process(const tarcap::PacketData &packet_data, +void PbftSyncPacketHandler::process(const threadpool::PacketData &packet_data, const std::shared_ptr &peer) { // Note: no need to consider possible race conditions due to concurrent processing as it is // disabled on priority_queue blocking dependencies level diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp index 9acea1d9ad..7e4f2e94b9 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp @@ -14,14 +14,15 @@ VotesBundlePacketHandler::VotesBundlePacketHandler(const FullNodeConfig &conf, : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), std::move(pbft_chain), std::move(vote_mgr), node_addr, "V1_VOTES_SYNC_PH") {} -void VotesBundlePacketHandler::validatePacketRlpFormat([[maybe_unused]] const tarcap::PacketData &packet_data) const { +void VotesBundlePacketHandler::validatePacketRlpFormat( + [[maybe_unused]] const threadpool::PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); if (items == 0 || items > kMaxVotesInBundleRlp) { throw InvalidRlpItemsCountException(packet_data.type_str_, items, kMaxVotesInBundleRlp); } } -void VotesBundlePacketHandler::process(const tarcap::PacketData &packet_data, +void VotesBundlePacketHandler::process(const threadpool::PacketData &packet_data, const std::shared_ptr &peer) { const auto reference_vote = std::make_shared(packet_data.rlp_[0]); diff --git a/tests/tarcap_threadpool_test.cpp b/tests/tarcap_threadpool_test.cpp index 6606806952..136f4c2b75 100644 --- a/tests/tarcap_threadpool_test.cpp +++ b/tests/tarcap_threadpool_test.cpp @@ -6,7 +6,7 @@ #include "dag/dag_block.hpp" #include "logger/logger.hpp" #include "network/tarcap/packets_handler.hpp" -#include "network/tarcap/threadpool/tarcap_thread_pool.hpp" +#include "network/threadpool/tarcap_thread_pool.hpp" #include "test_util/test_util.hpp" namespace taraxa::core_tests { @@ -39,14 +39,14 @@ class PacketsProcessingInfo { }; public: - void addPacketProcessingTimes(tarcap::PacketData::PacketId packet_id, + void addPacketProcessingTimes(threadpool::PacketData::PacketId packet_id, const PacketProcessingTimes& packet_processing_times) { std::scoped_lock lock(mutex_); bool res = packets_processing_times_.emplace(packet_id, packet_processing_times).second; assert(res); } - PacketProcessingTimes getPacketProcessingTimes(tarcap::PacketData::PacketId packet_id) const { + PacketProcessingTimes getPacketProcessingTimes(threadpool::PacketData::PacketId packet_id) const { std::shared_lock lock(mutex_); auto found_packet_info = packets_processing_times_.find(packet_id); @@ -66,7 +66,7 @@ class PacketsProcessingInfo { } private: - std::unordered_map packets_processing_times_; + std::unordered_map packets_processing_times_; mutable std::shared_mutex mutex_; }; @@ -99,9 +99,9 @@ class DummyPacketHandler : public tarcap::PacketHandler { DummyPacketHandler& operator=(DummyPacketHandler&&) = delete; private: - void validatePacketRlpFormat([[maybe_unused]] const tarcap::PacketData& packet_data) const override {} + void validatePacketRlpFormat([[maybe_unused]] const threadpool::PacketData& packet_data) const override {} - void process(const tarcap::PacketData& packet_data, + void process(const threadpool::PacketData& packet_data, [[maybe_unused]] const std::shared_ptr& peer) override { // Note do not use LOG() before saving start & finish time as it is internally synchronized and can // cause delays, which result in tests fails @@ -127,7 +127,7 @@ class DummyTransactionPacketHandler : public DummyPacketHandler { : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::TransactionPacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::TransactionPacket; }; class DummyDagBlockPacketHandler : public DummyPacketHandler { @@ -137,7 +137,7 @@ class DummyDagBlockPacketHandler : public DummyPacketHandler { : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::DagBlockPacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::DagBlockPacket; }; class DummyStatusPacketHandler : public DummyPacketHandler { @@ -147,7 +147,7 @@ class DummyStatusPacketHandler : public DummyPacketHandler { : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::StatusPacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::StatusPacket; }; class DummyVotePacketHandler : public DummyPacketHandler { @@ -157,7 +157,7 @@ class DummyVotePacketHandler : public DummyPacketHandler { : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::VotePacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::VotePacket; }; class DummyGetNextVotesBundlePacketHandler : public DummyPacketHandler { @@ -167,7 +167,7 @@ class DummyGetNextVotesBundlePacketHandler : public DummyPacketHandler { : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::GetNextVotesSyncPacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetNextVotesSyncPacket; }; class DummyVotesBundlePacketHandler : public DummyPacketHandler { @@ -177,7 +177,7 @@ class DummyVotesBundlePacketHandler : public DummyPacketHandler { : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::VotesBundlePacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::VotesBundlePacket; }; class DummyGetDagSyncPacketHandler : public DummyPacketHandler { @@ -187,7 +187,7 @@ class DummyGetDagSyncPacketHandler : public DummyPacketHandler { : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::GetDagSyncPacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetDagSyncPacket; }; class DummyGetPbftSyncPacketHandler : public DummyPacketHandler { @@ -197,7 +197,7 @@ class DummyGetPbftSyncPacketHandler : public DummyPacketHandler { : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::GetPbftSyncPacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetPbftSyncPacket; }; class DummyDagSyncPacketHandler : public DummyPacketHandler { @@ -207,7 +207,7 @@ class DummyDagSyncPacketHandler : public DummyPacketHandler { : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::DagSyncPacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::DagSyncPacket; }; class DummyPbftSyncPacketHandler : public DummyPacketHandler { @@ -217,7 +217,7 @@ class DummyPbftSyncPacketHandler : public DummyPacketHandler { : DummyPacketHandler(init_data, log_channel_name, processing_delay_ms) {} // Packet type that is processed by this handler - static constexpr tarcap::SubprotocolPacketType kPacketType_ = tarcap::SubprotocolPacketType::PbftSyncPacket; + static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::PbftSyncPacket; }; HandlersInitData createHandlersInitData() { @@ -237,8 +237,8 @@ HandlersInitData createHandlersInitData() { return ret_init_data; } -tarcap::PacketData createPacket(const dev::p2p::NodeID& sender_node_id, tarcap::SubprotocolPacketType packet_type, - std::optional> packet_rlp_bytes = {}) { +threadpool::PacketData createPacket(const dev::p2p::NodeID& sender_node_id, SubprotocolPacketType packet_type, + std::optional> packet_rlp_bytes = {}) { if (packet_rlp_bytes.has_value()) { return {packet_type, sender_node_id, std::move(packet_rlp_bytes.value())}; } @@ -296,7 +296,7 @@ void checkSerialProcessing( } } -size_t queuesSize(const tarcap::TarcapThreadPool& tp) { +size_t queuesSize(const threadpool::PacketsThreadPool& tp) { const auto [high_priority_queue_size, mid_priority_queue_size, low_priority_queue_size] = tp.getQueueSize(); return high_priority_queue_size + mid_priority_queue_size + low_priority_queue_size; @@ -328,49 +328,47 @@ TEST_F(TarcapTpTest, block_free_packets) { // Creates threadpool // Note: make num of threads >= num of packets to check if they are processed concurrently without blocks, otherwise // some blocks would be blocked for processing due to max threads limit - tarcap::TarcapThreadPool tp(18); + threadpool::PacketsThreadPool tp(18); tp.setPacketsHandlers(packets_handler); // Pushes packets to the tp const auto packet0_tx_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::TransactionPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::TransactionPacket, {})).value(); const auto packet1_tx_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::TransactionPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::TransactionPacket, {})).value(); const auto packet2_tx_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::TransactionPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::TransactionPacket, {})).value(); const auto packet3_tx_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::TransactionPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::TransactionPacket, {})).value(); const auto packet4_dag_block_id = - tp.push(createPacket(dev::p2p::NodeID(sender2), tarcap::SubprotocolPacketType::DagBlockPacket, - {createDagBlockRlp(0, 1)})) + tp.push(createPacket(dev::p2p::NodeID(sender2), SubprotocolPacketType::DagBlockPacket, {createDagBlockRlp(0, 1)})) .value(); const auto packet5_dag_block_id = - tp.push(createPacket(dev::p2p::NodeID(sender2), tarcap::SubprotocolPacketType::DagBlockPacket, - {createDagBlockRlp(0, 2)})) + tp.push(createPacket(dev::p2p::NodeID(sender2), SubprotocolPacketType::DagBlockPacket, {createDagBlockRlp(0, 2)})) .value(); const auto packet8_status_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::StatusPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::StatusPacket, {})).value(); const auto packet9_status_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::StatusPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::StatusPacket, {})).value(); const auto packet12_vote_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::VotePacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::VotePacket, {})).value(); const auto packet13_vote_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::VotePacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::VotePacket, {})).value(); const auto packet14_get_pbft_next_votes_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::GetNextVotesSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::GetNextVotesSyncPacket, {})).value(); const auto packet15_get_pbft_next_votes_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::GetNextVotesSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::GetNextVotesSyncPacket, {})).value(); const auto packet16_pbft_next_votes_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::VotesBundlePacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::VotesBundlePacket, {})).value(); size_t packets_count = 0; const auto packet17_pbft_next_votes_id = packets_count = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::VotesBundlePacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::VotesBundlePacket, {})).value(); tp.startProcessing(); @@ -468,30 +466,30 @@ TEST_F(TarcapTpTest, hard_blocking_deps) { packets_handler->registerHandler(init_data, "PBFT_SYNC_PH", 20); // Creates threadpool - tarcap::TarcapThreadPool tp(10); + threadpool::PacketsThreadPool tp(10); tp.setPacketsHandlers(packets_handler); // Pushes packets to the tp const auto packet0_dag_sync_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagSyncPacket, {})).value(); const auto packet1_dag_sync_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagSyncPacket, {})).value(); const auto packet2_get_dag_sync_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::GetDagSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::GetDagSyncPacket, {})).value(); const auto packet3_get_dag_sync_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::GetDagSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::GetDagSyncPacket, {})).value(); const auto packet4_get_pbft_sync_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::GetPbftSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::GetPbftSyncPacket, {})).value(); const auto packet5_get_pbft_sync_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::GetPbftSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::GetPbftSyncPacket, {})).value(); const auto packet6_pbft_sync_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::PbftSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::PbftSyncPacket, {})).value(); const auto packet7_pbft_sync_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::PbftSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::PbftSyncPacket, {})).value(); size_t packets_count = 0; const auto packet8_get_dag_sync_id = packets_count = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::GetDagSyncPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::GetDagSyncPacket, {})).value(); tp.startProcessing(); @@ -590,23 +588,22 @@ TEST_F(TarcapTpTest, peer_order_blocking_deps) { packets_handler->registerHandler(init_data, "SYNC_TEST_PH", 40); // Creates threadpool - tarcap::TarcapThreadPool tp(10); + threadpool::PacketsThreadPool tp(10); tp.setPacketsHandlers(packets_handler); // Pushes packets to the tp const auto packet0_tx_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::TransactionPacket)).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::TransactionPacket)).value(); const auto packet1_tx_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::TransactionPacket)).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::TransactionPacket)).value(); const auto packet2_dag_sync_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagSyncPacket)).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagSyncPacket)).value(); const auto packet3_tx_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::TransactionPacket)).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::TransactionPacket)).value(); size_t packets_count = 0; const auto packet4_dag_block_id = packets_count = - tp.push( - createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagBlockPacket, {createDagBlockRlp(1)})) + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagBlockPacket, {createDagBlockRlp(1)})) .value(); // How should packets be processed: @@ -677,24 +674,24 @@ TEST_F(TarcapTpTest, same_dag_blks_ordering) { packets_handler->registerHandler(init_data, "DAG_BLOCK_PH", 20); // Creates threadpool - tarcap::TarcapThreadPool tp(10); + threadpool::PacketsThreadPool tp(10); tp.setPacketsHandlers(packets_handler); auto dag_block = createDagBlockRlp(0); // Pushes packets to the tp const auto blk0_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagBlockPacket, {dag_block})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagBlockPacket, {dag_block})).value(); const auto blk1_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagBlockPacket, {dag_block})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagBlockPacket, {dag_block})).value(); const auto blk2_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagBlockPacket, {dag_block})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagBlockPacket, {dag_block})).value(); const auto blk3_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagBlockPacket, {dag_block})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagBlockPacket, {dag_block})).value(); size_t packets_count = 0; const auto blk4_id = packets_count = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagBlockPacket, {dag_block})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagBlockPacket, {dag_block})).value(); tp.startProcessing(); @@ -742,30 +739,29 @@ TEST_F(TarcapTpTest, dag_blks_lvls_ordering) { packets_handler->registerHandler(init_data, "DAG_BLOCK_PH", 20); // Creates threadpool - tarcap::TarcapThreadPool tp(10); + threadpool::PacketsThreadPool tp(10); tp.setPacketsHandlers(packets_handler); // Pushes packets to the tp - const auto blk0_lvl1_id = tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagBlockPacket, - {createDagBlockRlp(1, 1)})) - .value(); - const auto blk1_lvl1_id = tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagBlockPacket, - {createDagBlockRlp(1, 2)})) - .value(); - const auto blk2_lvl0_id = tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagBlockPacket, - {createDagBlockRlp(0, 3)})) - .value(); - const auto blk3_lvl1_id = tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagBlockPacket, - {createDagBlockRlp(1, 4)})) - .value(); - const auto blk4_lvl2_id = tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagBlockPacket, - {createDagBlockRlp(2, 5)})) - .value(); + const auto blk0_lvl1_id = + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagBlockPacket, {createDagBlockRlp(1, 1)})) + .value(); + const auto blk1_lvl1_id = + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagBlockPacket, {createDagBlockRlp(1, 2)})) + .value(); + const auto blk2_lvl0_id = + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagBlockPacket, {createDagBlockRlp(0, 3)})) + .value(); + const auto blk3_lvl1_id = + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagBlockPacket, {createDagBlockRlp(1, 4)})) + .value(); + const auto blk4_lvl2_id = + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagBlockPacket, {createDagBlockRlp(2, 5)})) + .value(); size_t packets_count = 0; const auto blk5_lvl3_id = packets_count = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::DagBlockPacket, - {createDagBlockRlp(3, 6)})) + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::DagBlockPacket, {createDagBlockRlp(3, 6)})) .value(); tp.startProcessing(); @@ -845,14 +841,13 @@ TEST_F(TarcapTpTest, threads_borrowing) { // Creates threadpool const size_t threads_num = 10; - tarcap::TarcapThreadPool tp(threads_num); + threadpool::PacketsThreadPool tp(threads_num); tp.setPacketsHandlers(packets_handler); // Pushes packets to the tp std::vector pushed_packets_ids; for (size_t i = 0; i < threads_num; i++) { - uint64_t packet_id = - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::VotePacket, {})).value(); + uint64_t packet_id = tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::VotePacket, {})).value(); pushed_packets_ids.push_back(packet_id); } @@ -908,7 +903,7 @@ TEST_F(TarcapTpTest, threads_borrowing) { EXPECT_EQ(packets_proc_info->getPacketProcessingTimes(pushed_packets_ids[9]).finish_time_, default_time_point); std::vector> packets_proc_info_vec; - for (size_t i = 0; i < threads_num - (tarcap::PacketData::PacketPriority::Count - 1); i++) { + for (size_t i = 0; i < threads_num - (threadpool::PacketData::PacketPriority::Count - 1); i++) { packets_proc_info_vec.emplace_back(packets_proc_info->getPacketProcessingTimes(pushed_packets_ids[i]), "packet" + std::to_string(pushed_packets_ids[i]) + "_vote"); } @@ -937,20 +932,20 @@ TEST_F(TarcapTpTest, low_priotity_queue_starvation) { // Creates threadpool size_t threads_num = 10; - tarcap::TarcapThreadPool tp(threads_num); + threadpool::PacketsThreadPool tp(threads_num); tp.setPacketsHandlers(packets_handler); // Push 10x more packets for each prioriy queue than max tp capacity to make sure that tp wont be able to process all // packets from each queue concurrently -> many packets will be waiting due to max threads num reached for specific // priority queues for (size_t i = 0; i < 2 * 10 * threads_num; i++) { - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::VotePacket, {})).value(); - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::TransactionPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::VotePacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::TransactionPacket, {})).value(); } // Push a few packets low priority packets for (size_t i = 0; i < 4; i++) { - tp.push(createPacket(init_data.copySender(), tarcap::SubprotocolPacketType::StatusPacket, {})).value(); + tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::StatusPacket, {})).value(); } tp.startProcessing(); From f64526a370690b0e858d7096c01cfb2fb28a772d Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Thu, 27 Apr 2023 12:05:39 -0700 Subject: [PATCH 039/134] chore: separate taraxa capabilities and create new base class --- .../consensus/include/pbft/pbft_manager.hpp | 2 +- libraries/core_libs/consensus/src/dag/dag.cpp | 2 +- .../consensus/src/dag/dag_manager.cpp | 2 +- .../consensus/src/pbft/pbft_manager.cpp | 6 +- .../src/vote_manager/vote_manager.cpp | 4 +- .../network/graphql/src/sync_state.cpp | 2 +- .../network/include/network/network.hpp | 4 +- .../packets_handlers/common/exceptions.hpp | 0 .../common/ext_syncing_packet_handler.hpp | 0 .../common/ext_votes_packet_handler.hpp | 0 .../common/packet_handler.hpp | 2 +- .../dag_block_packet_handler.hpp | 2 +- .../dag_sync_packet_handler.hpp | 2 +- .../get_dag_sync_packet_handler.hpp | 2 +- .../get_next_votes_sync_packet_handler.hpp | 2 +- .../get_pbft_sync_packet_handler.hpp | 2 +- .../pbft_sync_packet_handler.hpp | 4 +- .../status_packet_handler.hpp | 2 +- .../transaction_packet_handler.hpp | 2 +- .../packets_handlers/vote_packet_handler.hpp | 2 +- .../votes_bundle_packet_handler.hpp | 2 +- .../capability_latest/taraxa_capability.hpp | 31 ++++++++ .../common/ext_votes_packet_handler.hpp | 8 +- .../get_next_votes_sync_packet_handler.hpp | 6 +- .../get_pbft_sync_packet_handler.hpp | 6 +- .../pbft_sync_packet_handler.hpp | 8 +- .../votes_bundle_packet_handler.hpp | 6 +- .../capability_v1}/taraxa_capability.hpp | 11 ++- .../network/tarcap/packets_handler.hpp | 2 +- .../network/include/network/tarcap/readme.md | 19 +++++ ...ability.hpp => taraxa_capability_base.hpp} | 30 +++---- libraries/core_libs/network/src/network.cpp | 27 +++---- .../common/ext_syncing_packet_handler.cpp | 2 +- .../common/ext_votes_packet_handler.cpp | 2 +- .../common/packet_handler.cpp | 2 +- .../dag_block_packet_handler.cpp | 4 +- .../dag_sync_packet_handler.cpp | 4 +- .../get_dag_sync_packet_handler.cpp | 2 +- .../get_next_votes_sync_packet_handler.cpp | 2 +- .../get_pbft_sync_packet_handler.cpp | 2 +- .../pbft_sync_packet_handler.cpp | 2 +- .../status_packet_handler.cpp | 4 +- .../transaction_packet_handler.cpp | 2 +- .../packets_handlers/vote_packet_handler.cpp | 2 +- .../votes_bundle_packet_handler.cpp | 2 +- .../capability_latest/taraxa_capability.cpp | 62 +++++++++++++++ .../common/ext_votes_packet_handler.cpp | 6 +- .../get_next_votes_sync_packet_handler.cpp | 6 +- .../get_pbft_sync_packet_handler.cpp | 6 +- .../pbft_sync_packet_handler.cpp | 6 +- .../votes_bundle_packet_handler.cpp | 6 +- .../capability_v1}/taraxa_capability.cpp | 40 +++++----- ...ability.cpp => taraxa_capability_base.cpp} | 78 +++++++++---------- tests/network_test.cpp | 14 ++-- tests/p2p_test.cpp | 27 +++---- tests/pbft_manager_test.cpp | 2 +- tests/vote_test.cpp | 2 +- 57 files changed, 298 insertions(+), 189 deletions(-) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/common/exceptions.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/common/ext_syncing_packet_handler.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/common/ext_votes_packet_handler.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/common/packet_handler.hpp (97%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/dag_block_packet_handler.hpp (95%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/dag_sync_packet_handler.hpp (93%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/get_dag_sync_packet_handler.hpp (95%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/get_next_votes_sync_packet_handler.hpp (92%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/get_pbft_sync_packet_handler.hpp (95%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/pbft_sync_packet_handler.hpp (92%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/status_packet_handler.hpp (93%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/transaction_packet_handler.hpp (95%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/vote_packet_handler.hpp (94%) rename libraries/core_libs/network/include/network/tarcap/{ => capability_latest}/packets_handlers/votes_bundle_packet_handler.hpp (94%) create mode 100644 libraries/core_libs/network/include/network/tarcap/capability_latest/taraxa_capability.hpp rename libraries/core_libs/network/include/network/{v1_tarcap => tarcap/capability_v1}/packets_handlers/common/ext_votes_packet_handler.hpp (79%) rename libraries/core_libs/network/include/network/{v1_tarcap => tarcap/capability_v1}/packets_handlers/get_next_votes_sync_packet_handler.hpp (85%) rename libraries/core_libs/network/include/network/{v1_tarcap => tarcap/capability_v1}/packets_handlers/get_pbft_sync_packet_handler.hpp (89%) rename libraries/core_libs/network/include/network/{v1_tarcap => tarcap/capability_v1}/packets_handlers/pbft_sync_packet_handler.hpp (85%) rename libraries/core_libs/network/include/network/{v1_tarcap => tarcap/capability_v1}/packets_handlers/votes_bundle_packet_handler.hpp (88%) rename libraries/core_libs/network/include/network/{v1_tarcap => tarcap/capability_v1}/taraxa_capability.hpp (73%) create mode 100644 libraries/core_libs/network/include/network/tarcap/readme.md rename libraries/core_libs/network/include/network/tarcap/{taraxa_capability.hpp => taraxa_capability_base.hpp} (79%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/common/ext_syncing_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/common/ext_votes_packet_handler.cpp (99%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/common/packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/dag_block_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/dag_sync_packet_handler.cpp (97%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/get_dag_sync_packet_handler.cpp (97%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/get_next_votes_sync_packet_handler.cpp (97%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/get_pbft_sync_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/pbft_sync_packet_handler.cpp (99%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/status_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/transaction_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/vote_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{ => capability_latest}/packets_handlers/votes_bundle_packet_handler.cpp (98%) create mode 100644 libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp rename libraries/core_libs/network/src/{v1_tarcap => tarcap/capability_v1}/packet_handlers/common/ext_votes_packet_handler.cpp (91%) rename libraries/core_libs/network/src/{v1_tarcap => tarcap/capability_v1}/packet_handlers/get_next_votes_sync_packet_handler.cpp (95%) rename libraries/core_libs/network/src/{v1_tarcap => tarcap/capability_v1}/packet_handlers/get_pbft_sync_packet_handler.cpp (97%) rename libraries/core_libs/network/src/{v1_tarcap => tarcap/capability_v1}/packet_handlers/pbft_sync_packet_handler.cpp (98%) rename libraries/core_libs/network/src/{v1_tarcap => tarcap/capability_v1}/packet_handlers/votes_bundle_packet_handler.cpp (97%) rename libraries/core_libs/network/src/{v1_tarcap => tarcap/capability_v1}/taraxa_capability.cpp (64%) rename libraries/core_libs/network/src/tarcap/{taraxa_capability.cpp => taraxa_capability_base.cpp} (81%) diff --git a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp index c73c155118..dbb0487f49 100644 --- a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp +++ b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp @@ -8,7 +8,7 @@ #include "final_chain/final_chain.hpp" #include "logger/logger.hpp" #include "network/network.hpp" -#include "network/tarcap/taraxa_capability.hpp" +#include "network/tarcap/capability_latest/taraxa_capability.hpp" #include "pbft/period_data_queue.hpp" #include "pbft/proposed_blocks.hpp" diff --git a/libraries/core_libs/consensus/src/dag/dag.cpp b/libraries/core_libs/consensus/src/dag/dag.cpp index 0029cb3088..498238dbb8 100644 --- a/libraries/core_libs/consensus/src/dag/dag.cpp +++ b/libraries/core_libs/consensus/src/dag/dag.cpp @@ -14,7 +14,7 @@ #include "dag/dag.hpp" #include "key_manager/key_manager.hpp" #include "network/network.hpp" -#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" #include "transaction/transaction_manager.hpp" namespace taraxa { diff --git a/libraries/core_libs/consensus/src/dag/dag_manager.cpp b/libraries/core_libs/consensus/src/dag/dag_manager.cpp index 548447e248..707aaaffbd 100644 --- a/libraries/core_libs/consensus/src/dag/dag_manager.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_manager.cpp @@ -14,7 +14,7 @@ #include "dag/dag.hpp" #include "key_manager/key_manager.hpp" #include "network/network.hpp" -#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" #include "transaction/transaction_manager.hpp" namespace taraxa { diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index a41493da3e..d8ebc32d51 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -16,9 +16,9 @@ #include "dag/dag.hpp" #include "final_chain/final_chain.hpp" -#include "network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp" -#include "network/tarcap/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" #include "pbft/period_data.hpp" #include "vote_manager/vote_manager.hpp" diff --git a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp index efd383e23e..0cffea22d2 100644 --- a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp +++ b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp @@ -7,8 +7,8 @@ #include #include "network/network.hpp" -#include "network/tarcap/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" namespace taraxa { diff --git a/libraries/core_libs/network/graphql/src/sync_state.cpp b/libraries/core_libs/network/graphql/src/sync_state.cpp index 4c30a3834f..f10ab0f511 100644 --- a/libraries/core_libs/network/graphql/src/sync_state.cpp +++ b/libraries/core_libs/network/graphql/src/sync_state.cpp @@ -1,7 +1,7 @@ #include "graphql/sync_state.hpp" -#include "network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" namespace graphql::taraxa { diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index 16d398dce1..c5943addee 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -17,7 +17,7 @@ #include "common/thread_pool.hpp" #include "common/util.hpp" #include "config/config.hpp" -#include "tarcap/taraxa_capability.hpp" +#include "tarcap/taraxa_capability_base.hpp" #include "transaction/transaction.hpp" namespace taraxa { @@ -76,7 +76,7 @@ class Network { std::shared_ptr host_; // All supported taraxa capabilities - std::map> tarcaps_; + std::map> tarcaps_; // Threadpool for packets std::shared_ptr packets_tp_; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/exceptions.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/exceptions.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/common/exceptions.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/exceptions.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp similarity index 97% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/common/packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp index f1bd96812a..7e8a54f5c1 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/common/packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp @@ -6,8 +6,8 @@ #include "common/thread_pool.hpp" #include "libdevcore/RLP.h" #include "logger/logger.hpp" +#include "network/tarcap/capability_latest/packets_handlers/common/exceptions.hpp" #include "network/tarcap/packet_types.hpp" -#include "network/tarcap/packets_handlers/common/exceptions.hpp" #include "network/tarcap/shared_states/peers_state.hpp" #include "network/tarcap/taraxa_peer.hpp" #include "network/threadpool/packet_data.hpp" diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_block_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp similarity index 95% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_block_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp index 22197cdbd8..b919c563b2 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_block_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp @@ -1,6 +1,6 @@ #pragma once -#include "network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp" +#include "common/ext_syncing_packet_handler.hpp" namespace taraxa { class TransactionManager; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.hpp similarity index 93% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.hpp index b94a0509e0..62f5b5b975 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/dag_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.hpp @@ -1,6 +1,6 @@ #pragma once -#include "network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp" +#include "common/ext_syncing_packet_handler.hpp" namespace taraxa { class TransactionManager; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_dag_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp similarity index 95% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/get_dag_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp index 2da4ed381b..6c098f533a 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_dag_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp @@ -1,6 +1,6 @@ #pragma once -#include "network/tarcap/packets_handlers/common/packet_handler.hpp" +#include "common/packet_handler.hpp" namespace taraxa { class DagManager; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp similarity index 92% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp index 32aaddd277..8435b8016b 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp @@ -1,6 +1,6 @@ #pragma once -#include "network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp" +#include "common/ext_votes_packet_handler.hpp" namespace taraxa { class PbftManager; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.hpp similarity index 95% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.hpp index 957fb74e24..e616cd8109 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.hpp @@ -1,6 +1,6 @@ #pragma once -#include "network/tarcap/packets_handlers/common/packet_handler.hpp" +#include "common/packet_handler.hpp" namespace taraxa { class PbftChain; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp similarity index 92% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp index c51ebef8b9..391d689753 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp @@ -1,7 +1,7 @@ #pragma once -#include "network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp" -#include "vote_manager/vote_manager.hpp" +#include "common/ext_syncing_packet_handler.hpp" +#include "consensus/include/vote_manager/vote_manager.hpp" namespace taraxa::network::tarcap { diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/status_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp similarity index 93% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/status_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp index f1c8f219a1..2d2b7b9913 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/status_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp @@ -1,6 +1,6 @@ #pragma once -#include "network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp" +#include "common/ext_syncing_packet_handler.hpp" namespace taraxa::network::tarcap { diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/transaction_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp similarity index 95% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/transaction_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp index 5b33e485a4..e3c4362414 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/transaction_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp @@ -1,7 +1,7 @@ #pragma once #include "dag/dag_block.hpp" -#include "network/tarcap/packets_handlers/common/packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp" #include "transaction/transaction.hpp" namespace taraxa { diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/vote_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp similarity index 94% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/vote_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp index 48aec1b3c2..9b3511e65c 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/vote_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp @@ -1,6 +1,6 @@ #pragma once -#include "network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp" +#include "common/ext_votes_packet_handler.hpp" namespace taraxa::network::tarcap { diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp similarity index 94% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp index 536c883b67..92991b3ebd 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp @@ -1,6 +1,6 @@ #pragma once -#include "network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp" +#include "common/ext_votes_packet_handler.hpp" namespace taraxa::network::tarcap { diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/taraxa_capability.hpp new file mode 100644 index 0000000000..2a6d2bcc9f --- /dev/null +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/taraxa_capability.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include "network/tarcap/taraxa_capability_base.hpp" + +// NoteL latest taraxa capability does not use any special namespace, other version use for example +// taraxa::network::tarcap::v1 +namespace taraxa::network::tarcap { + +class TaraxaCapability : public taraxa::network::tarcap::TaraxaCapabilityBase { + public: + using TaraxaCapabilityBase::TaraxaCapabilityBase; + + virtual ~TaraxaCapability() = default; + TaraxaCapability(const TaraxaCapability &ro) = delete; + TaraxaCapability &operator=(const TaraxaCapability &ro) = delete; + TaraxaCapability(TaraxaCapability &&ro) = delete; + TaraxaCapability &operator=(TaraxaCapability &&ro) = delete; + + protected: + void registerPacketHandlers(const h256 &genesis_hash, + const std::shared_ptr &packets_stats, + const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, + const std::shared_ptr &pbft_chain, + const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, + const std::shared_ptr &trx_mgr, const addr_t &node_addr) override; + + private: + LOG_OBJECTS_DEFINE +}; + +} // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/common/ext_votes_packet_handler.hpp similarity index 79% rename from libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/common/ext_votes_packet_handler.hpp index 2b9329f076..d39c8cd9a2 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/common/ext_votes_packet_handler.hpp @@ -1,8 +1,8 @@ #pragma once -#include "network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.hpp" -namespace taraxa::network::v1_tarcap { +namespace taraxa::network::tarcap::v1 { /** * @brief ExtVotesPacketHandler is extended abstract PacketHandler with added functions that are used in packet @@ -10,6 +10,8 @@ namespace taraxa::network::v1_tarcap { */ class ExtVotesPacketHandler : public tarcap::ExtVotesPacketHandler { public: + // TODO: can be used this instead of ctor copy/paste ? + // using tarcap::ExtVotesPacketHandler::TaraxaCapabilityBase; ExtVotesPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, @@ -26,4 +28,4 @@ class ExtVotesPacketHandler : public tarcap::ExtVotesPacketHandler { std::vector>&& votes) override; }; -} // namespace taraxa::network::v1_tarcap +} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/get_next_votes_sync_packet_handler.hpp similarity index 85% rename from libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/get_next_votes_sync_packet_handler.hpp index 01498ca61d..8376e890d5 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/get_next_votes_sync_packet_handler.hpp @@ -1,13 +1,13 @@ #pragma once -#include "network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp" +#include "common/ext_votes_packet_handler.hpp" namespace taraxa { class PbftManager; class VoteManager; } // namespace taraxa -namespace taraxa::network::v1_tarcap { +namespace taraxa::network::tarcap::v1 { class GetNextVotesBundlePacketHandler final : public ExtVotesPacketHandler { public: @@ -24,4 +24,4 @@ class GetNextVotesBundlePacketHandler final : public ExtVotesPacketHandler { void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; }; -} // namespace taraxa::network::v1_tarcap +} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/get_pbft_sync_packet_handler.hpp similarity index 89% rename from libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/get_pbft_sync_packet_handler.hpp index f60d3a35b7..551c50f55b 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/get_pbft_sync_packet_handler.hpp @@ -1,6 +1,6 @@ #pragma once -#include "network/tarcap/packets_handlers/common/packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp" namespace taraxa { class PbftChain; @@ -12,7 +12,7 @@ namespace taraxa::network::tarcap { class PbftSyncingState; } -namespace taraxa::network::v1_tarcap { +namespace taraxa::network::tarcap::v1 { class GetPbftSyncPacketHandler final : public tarcap::PacketHandler { public: @@ -38,4 +38,4 @@ class GetPbftSyncPacketHandler final : public tarcap::PacketHandler { std::shared_ptr db_; }; -} // namespace taraxa::network::v1_tarcap +} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp similarity index 85% rename from libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp index 10cc817904..6e77e9a4b9 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp @@ -1,9 +1,9 @@ #pragma once -#include "network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp" -#include "vote_manager/vote_manager.hpp" +#include "consensus/include/vote_manager/vote_manager.hpp" +#include "network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp" -namespace taraxa::network::v1_tarcap { +namespace taraxa::network::tarcap::v1 { class PbftSyncPacketHandler final : public tarcap::ExtSyncingPacketHandler { public: @@ -35,4 +35,4 @@ class PbftSyncPacketHandler final : public tarcap::ExtSyncingPacketHandler { static constexpr size_t kChainSyncedPacketSize = 3; }; -} // namespace taraxa::network::v1_tarcap +} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/votes_bundle_packet_handler.hpp similarity index 88% rename from libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/votes_bundle_packet_handler.hpp index 628314dabf..46e87a7783 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/votes_bundle_packet_handler.hpp @@ -1,8 +1,8 @@ #pragma once -#include "network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp" +#include "common/ext_votes_packet_handler.hpp" -namespace taraxa::network::v1_tarcap { +namespace taraxa::network::tarcap::v1 { class VotesBundlePacketHandler final : public ExtVotesPacketHandler { public: @@ -29,4 +29,4 @@ class VotesBundlePacketHandler final : public ExtVotesPacketHandler { void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; }; -} // namespace taraxa::network::v1_tarcap +} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/include/network/v1_tarcap/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/capability_v1/taraxa_capability.hpp similarity index 73% rename from libraries/core_libs/network/include/network/v1_tarcap/taraxa_capability.hpp rename to libraries/core_libs/network/include/network/tarcap/capability_v1/taraxa_capability.hpp index b54c4db302..33b9c4bcee 100644 --- a/libraries/core_libs/network/include/network/v1_tarcap/taraxa_capability.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_v1/taraxa_capability.hpp @@ -1,13 +1,12 @@ #pragma once -#include "network/tarcap/taraxa_capability.hpp" +#include "network/tarcap/taraxa_capability_base.hpp" -namespace taraxa::network::v1_tarcap { +namespace taraxa::network::tarcap::v1 { -class TaraxaCapability : public taraxa::network::tarcap::TaraxaCapability { +class TaraxaCapability : public taraxa::network::tarcap::TaraxaCapabilityBase { public: - TaraxaCapability(std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, - unsigned version, const std::string &log_channel); + using TaraxaCapabilityBase::TaraxaCapabilityBase; virtual ~TaraxaCapability() = default; TaraxaCapability(const TaraxaCapability &ro) = delete; @@ -27,4 +26,4 @@ class TaraxaCapability : public taraxa::network::tarcap::TaraxaCapability { LOG_OBJECTS_DEFINE }; -} // namespace taraxa::network::v1_tarcap +} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handler.hpp index 3b03ec2ba8..269d0620d3 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handler.hpp @@ -3,7 +3,7 @@ #include #include -#include "network/tarcap/packets_handlers/common/packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp" namespace taraxa::network::tarcap { diff --git a/libraries/core_libs/network/include/network/tarcap/readme.md b/libraries/core_libs/network/include/network/tarcap/readme.md new file mode 100644 index 0000000000..12f6a01aca --- /dev/null +++ b/libraries/core_libs/network/include/network/tarcap/readme.md @@ -0,0 +1,19 @@ +### Multiple taraxa capabilities support +- Create new folder, e.g. `capability_v1` +- Create new `TaraxaCapability` class derived from `TaraxaCapabiliyyBase` in new namespace, e.g.: + + + # Create clang profile + namespace taraxa::network::tarcap::v1 { + + class TaraxaCapability : public taraxa::network::tarcap::TaraxaCapabilityBase { + ... + } + + } +- Derive new packet handlers with different logic than the original one. +- + `!!! Important:` These handlers must be + directly on indirectly derived from the latest packets handlers, which are inside + `network/tarcap/capability_latest` folder, otherwise network class would not work properly + and project would not compile \ No newline at end of file diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp similarity index 79% rename from libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp rename to libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp index 6fa956d1b4..e6f28d5620 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp @@ -32,30 +32,32 @@ class PacketsHandler; class PbftSyncingState; class TaraxaPeer; -class TaraxaCapability : public dev::p2p::CapabilityFace { +class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { public: - TaraxaCapability(std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, - unsigned version, const std::string &log_channel); + TaraxaCapabilityBase(std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, + unsigned version, const std::string &log_channel); - virtual ~TaraxaCapability() = default; - TaraxaCapability(const TaraxaCapability &ro) = delete; - TaraxaCapability &operator=(const TaraxaCapability &ro) = delete; - TaraxaCapability(TaraxaCapability &&ro) = delete; - TaraxaCapability &operator=(TaraxaCapability &&ro) = delete; + virtual ~TaraxaCapabilityBase() = default; + TaraxaCapabilityBase(const TaraxaCapabilityBase &ro) = delete; + TaraxaCapabilityBase &operator=(const TaraxaCapabilityBase &ro) = delete; + TaraxaCapabilityBase(TaraxaCapabilityBase &&ro) = delete; + TaraxaCapabilityBase &operator=(TaraxaCapabilityBase &&ro) = delete; // Init capability. Register packet handlers and periodic events virtual void init(const h256 &genesis_hash, std::shared_ptr db, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, const dev::Address &node_addr); + // CapabilityFace implemented interface std::string name() const override; unsigned version() const override; unsigned messageCount() const override; - void onConnect(std::weak_ptr session, u256 const &) override; - void onDisconnect(dev::p2p::NodeID const &_nodeID) override; - void interpretCapabilityPacket(std::weak_ptr session, unsigned _id, dev::RLP const &_r) override; - std::string packetTypeToString(unsigned _packetType) const override; + virtual void onConnect(std::weak_ptr session, u256 const &) override; + virtual void onDisconnect(dev::p2p::NodeID const &_nodeID) override; + virtual void interpretCapabilityPacket(std::weak_ptr session, unsigned _id, + dev::RLP const &_r) override; + virtual std::string packetTypeToString(unsigned _packetType) const override; template std::shared_ptr getSpecificHandler() const; @@ -94,7 +96,7 @@ class TaraxaCapability : public dev::p2p::CapabilityFace { const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, - const std::shared_ptr &trx_mgr, const addr_t &node_addr); + const std::shared_ptr &trx_mgr, const addr_t &node_addr) = 0; private: void addBootNodes(bool initial = false); @@ -141,7 +143,7 @@ class TaraxaCapability : public dev::p2p::CapabilityFace { }; template -std::shared_ptr TaraxaCapability::getSpecificHandler() const { +std::shared_ptr TaraxaCapabilityBase::getSpecificHandler() const { return packets_handlers_->getSpecificHandler(); } diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index 27ffa8372f..d5023ceb6c 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -8,11 +8,12 @@ #include #include "config/version.hpp" -#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp" -#include "network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp" -#include "network/tarcap/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp" -#include "network/v1_tarcap/taraxa_capability.hpp" +#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" +#include "network/tarcap/capability_latest/taraxa_capability.hpp" +#include "network/tarcap/capability_v1/taraxa_capability.hpp" namespace taraxa { @@ -59,7 +60,7 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, // Register old version of taraxa capability auto v1_tarcap = - std::make_shared(host, key, config, kOldNetworkVersion, "V1_TARCAP"); + std::make_shared(host, key, config, kOldNetworkVersion, "V1_TARCAP"); v1_tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); capabilities.emplace_back(v1_tarcap); @@ -76,7 +77,7 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, host_ = dev::p2p::Host::make(net_version, construct_capabilities, key, net_conf, taraxa_net_conf, network_file_path); for (const auto &tarcap : host_->getSupportedCapabilities()) { tarcaps_.emplace(tarcap.second.ref->version(), - std::static_pointer_cast(tarcap.second.ref)); + std::static_pointer_cast(tarcap.second.ref)); } for (uint i = 0; i < tp_.capacity(); ++i) { @@ -92,23 +93,23 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, Network::~Network() { tp_.stop(); - packets_tp_->stopProcessing(); - periodic_events_tp_.stop(); + // packets_tp_->stopProcessing(); + // periodic_events_tp_.stop(); // TODO: remove once packets_tp_ and periodic_events_tp_ are moved from tarcaps to network for (auto &tarcap : host_->getSupportedCapabilities()) { - std::static_pointer_cast(tarcap.second.ref)->stop(); + std::static_pointer_cast(tarcap.second.ref)->stop(); } } void Network::start() { tp_.start(); - packets_tp_->startProcessing(); - periodic_events_tp_.start(); + // packets_tp_->startProcessing(); + // periodic_events_tp_.start(); // TODO: remove once packets_tp_ and periodic_events_tp_ are moved from tarcaps to network for (auto &tarcap : host_->getSupportedCapabilities()) { - std::static_pointer_cast(tarcap.second.ref)->start(); + std::static_pointer_cast(tarcap.second.ref)->start(); } LOG(log_nf_) << "Started Node id: " << host_->id() << ", listening on port " << host_->listenPort(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_syncing_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_syncing_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.cpp index 8032dc8d9d..e788787da3 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_syncing_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp" #include "dag/dag.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.cpp similarity index 99% rename from libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.cpp index c02f17c8c2..66124585a8 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/common/ext_votes_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote/vote.hpp" diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/packet_handler.cpp index 5ab82554dd..3786bb0e51 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/common/packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/common/packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp" #include "network/tarcap/stats/time_period_packets_stats.hpp" diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/dag_block_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/packets_handlers/dag_block_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.cpp index 2ab1a7d439..a18faa9f5a 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/dag_block_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.cpp @@ -1,7 +1,7 @@ -#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" #include "dag/dag_manager.hpp" -#include "network/tarcap/packets_handlers/transaction_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "network/tarcap/shared_states/test_state.hpp" #include "transaction/transaction_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/dag_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.cpp similarity index 97% rename from libraries/core_libs/network/src/tarcap/packets_handlers/dag_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.cpp index 0105dd59a0..6cd2aa377b 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/dag_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.cpp @@ -1,7 +1,7 @@ -#include "network/tarcap/packets_handlers/dag_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.hpp" #include "dag/dag.hpp" -#include "network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "transaction/transaction.hpp" #include "transaction/transaction_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/get_dag_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.cpp similarity index 97% rename from libraries/core_libs/network/src/tarcap/packets_handlers/get_dag_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.cpp index 4021dd78aa..a16b8b0b8f 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/get_dag_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/get_dag_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp" #include "dag/dag_manager.hpp" #include "transaction/transaction_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.cpp similarity index 97% rename from libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.cpp index 596c20a2ea..0a2dc0428e 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/get_next_votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote_manager/vote_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.cpp index 842ea217fe..ae92c8cb74 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "pbft/pbft_chain.hpp" diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.cpp similarity index 99% rename from libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.cpp index 49aef5d151..3a61670b80 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "pbft/pbft_chain.hpp" diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/status_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/status_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/packets_handlers/status_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/status_packet_handler.cpp index 3480888bd2..d2420fa2d0 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/status_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/status_packet_handler.cpp @@ -1,8 +1,8 @@ -#include "network/tarcap/packets_handlers/status_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp" #include "config/version.hpp" #include "dag/dag.hpp" -#include "network/tarcap/packets_handlers/common/ext_syncing_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "pbft/pbft_chain.hpp" #include "pbft/pbft_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/transaction_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/transaction_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/packets_handlers/transaction_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/transaction_packet_handler.cpp index 04dc574806..76433f5c02 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/transaction_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/transaction_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/transaction_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" #include diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/vote_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/vote_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/packets_handlers/vote_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/vote_packet_handler.cpp index a483fd9e93..3c516126fb 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/vote_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/vote_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote_manager/vote_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.cpp index 01190fd698..685fd0d84f 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote/votes_bundle_rlp.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp new file mode 100644 index 0000000000..db39e3d583 --- /dev/null +++ b/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp @@ -0,0 +1,62 @@ +#include "network/tarcap/capability_latest/taraxa_capability.hpp" + +#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" + +namespace taraxa::network::tarcap { + +void TaraxaCapability::registerPacketHandlers( + const h256 &genesis_hash, const std::shared_ptr &packets_stats, + const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, + const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, + const std::shared_ptr &dag_mgr, const std::shared_ptr &trx_mgr, + const addr_t &node_addr) { + node_stats_ = std::make_shared(peers_state_, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, + vote_mgr, trx_mgr, packets_stats, thread_pool_, node_addr); + + // Register all packet handlers + + // Consensus packets with high processing priority + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, + pbft_chain, vote_mgr, node_addr); + packets_handlers_->registerHandler( + kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, + pbft_chain, vote_mgr, node_addr); + + // Standard packets with mid processing priority + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, + pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, + trx_mgr, db, test_state_, node_addr); + + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, trx_mgr, + test_state_, node_addr); + + // Non critical packets with low processing priority + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, + pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, + db, genesis_hash, node_addr); + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, trx_mgr, + dag_mgr, db, node_addr); + + packets_handlers_->registerHandler( + kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, node_addr); + + packets_handlers_->registerHandler( + kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, vote_mgr, db, node_addr); + + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, + pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, + vote_mgr, periodic_events_tp_, db, node_addr); + + thread_pool_->setPacketsHandlers(packets_handlers_); +} +} // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/common/ext_votes_packet_handler.cpp similarity index 91% rename from libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/common/ext_votes_packet_handler.cpp index ebd2c2b4a1..a9e0d4dcb1 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/common/ext_votes_packet_handler.cpp @@ -1,10 +1,10 @@ -#include "network/v1_tarcap/packets_handlers/common/ext_votes_packet_handler.hpp" +#include "network/tarcap/capability_v1/packets_handlers/common/ext_votes_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote/vote.hpp" #include "vote_manager/vote_manager.hpp" -namespace taraxa::network::v1_tarcap { +namespace taraxa::network::tarcap::v1 { ExtVotesPacketHandler::ExtVotesPacketHandler(const FullNodeConfig &conf, std::shared_ptr peers_state, @@ -43,4 +43,4 @@ void ExtVotesPacketHandler::sendPbftVotesBundle(const std::shared_ptr peers_state, @@ -77,4 +77,4 @@ void GetNextVotesBundlePacketHandler::process(const threadpool::PacketData &pack } } -} // namespace taraxa::network::v1_tarcap +} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/get_pbft_sync_packet_handler.cpp similarity index 97% rename from libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/get_pbft_sync_packet_handler.cpp index 2335d14aad..251e586769 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/get_pbft_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/v1_tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp" +#include "network/tarcap/capability_v1/packets_handlers/get_pbft_sync_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "pbft/pbft_chain.hpp" @@ -6,7 +6,7 @@ #include "vote/vote.hpp" #include "vote_manager/vote_manager.hpp" -namespace taraxa::network::v1_tarcap { +namespace taraxa::network::tarcap::v1 { GetPbftSyncPacketHandler::GetPbftSyncPacketHandler(const FullNodeConfig &conf, std::shared_ptr peers_state, @@ -123,4 +123,4 @@ void GetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, P } } -} // namespace taraxa::network::v1_tarcap +} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/pbft_sync_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/pbft_sync_packet_handler.cpp index 90b50d339c..2490b35844 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/pbft_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/v1_tarcap/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "pbft/pbft_chain.hpp" @@ -6,7 +6,7 @@ #include "transaction/transaction_manager.hpp" #include "vote/vote.hpp" -namespace taraxa::network::v1_tarcap { +namespace taraxa::network::tarcap::v1 { PbftSyncPacketHandler::PbftSyncPacketHandler( const FullNodeConfig &conf, std::shared_ptr peers_state, @@ -296,4 +296,4 @@ void PbftSyncPacketHandler::handleMaliciousSyncPeer(dev::p2p::NodeID const &id) restartSyncingPbft(true); } -} // namespace taraxa::network::v1_tarcap +} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/votes_bundle_packet_handler.cpp similarity index 97% rename from libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/votes_bundle_packet_handler.cpp index 7e4f2e94b9..e7c1164bcc 100644 --- a/libraries/core_libs/network/src/v1_tarcap/packet_handlers/votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/votes_bundle_packet_handler.cpp @@ -1,9 +1,9 @@ -#include "network/v1_tarcap/packets_handlers/votes_bundle_packet_handler.hpp" +#include "network/tarcap/capability_v1/packets_handlers/votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote_manager/vote_manager.hpp" -namespace taraxa::network::v1_tarcap { +namespace taraxa::network::tarcap::v1 { VotesBundlePacketHandler::VotesBundlePacketHandler(const FullNodeConfig &conf, std::shared_ptr peers_state, @@ -159,4 +159,4 @@ void VotesBundlePacketHandler::onNewPbftVotesBundle(const std::vector host, const dev::KeyPair &key, - const FullNodeConfig &conf, unsigned version, const std::string &log_channel) - : tarcap::TaraxaCapability(host, key, conf, version, log_channel) {} +namespace taraxa::network::tarcap::v1 { void TaraxaCapability::registerPacketHandlers( const h256 &genesis_hash, const std::shared_ptr &packets_stats, @@ -31,10 +27,10 @@ void TaraxaCapability::registerPacketHandlers( // Consensus packets with high processing priority packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); - packets_handlers_->registerHandler( + packets_handlers_->registerHandler( kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr); + packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, + pbft_chain, vote_mgr, node_addr); // Standard packets with mid processing priority packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, @@ -54,13 +50,13 @@ void TaraxaCapability::registerPacketHandlers( packets_handlers_->registerHandler( kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, node_addr); - packets_handlers_->registerHandler( + packets_handlers_->registerHandler( kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, vote_mgr, db, node_addr); - packets_handlers_->registerHandler( + packets_handlers_->registerHandler( kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, periodic_events_tp_, db, node_addr); thread_pool_->setPacketsHandlers(packets_handlers_); } -} // namespace taraxa::network::v1_tarcap +} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp similarity index 81% rename from libraries/core_libs/network/src/tarcap/taraxa_capability.cpp rename to libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp index 321346f385..af7cbb2fc8 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp @@ -1,19 +1,19 @@ -#include "network/tarcap/taraxa_capability.hpp" +#include "network/tarcap/taraxa_capability_base.hpp" #include #include "dag/dag.hpp" +#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" #include "network/tarcap/packets_handler.hpp" -#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp" -#include "network/tarcap/packets_handlers/dag_sync_packet_handler.hpp" -#include "network/tarcap/packets_handlers/get_dag_sync_packet_handler.hpp" -#include "network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp" -#include "network/tarcap/packets_handlers/get_pbft_sync_packet_handler.hpp" -#include "network/tarcap/packets_handlers/pbft_sync_packet_handler.hpp" -#include "network/tarcap/packets_handlers/status_packet_handler.hpp" -#include "network/tarcap/packets_handlers/transaction_packet_handler.hpp" -#include "network/tarcap/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "network/tarcap/shared_states/test_state.hpp" #include "network/tarcap/stats/node_stats.hpp" @@ -26,8 +26,8 @@ namespace taraxa::network::tarcap { -TaraxaCapability::TaraxaCapability(std::weak_ptr host, const dev::KeyPair &key, - const FullNodeConfig &conf, unsigned version, const std::string &log_channel) +TaraxaCapabilityBase::TaraxaCapabilityBase(std::weak_ptr host, const dev::KeyPair &key, + const FullNodeConfig &conf, unsigned version, const std::string &log_channel) : test_state_(std::make_shared()), version_(version), kConf(conf), @@ -50,10 +50,10 @@ TaraxaCapability::TaraxaCapability(std::weak_ptr host, const dev addBootNodes(true); } -void TaraxaCapability::init(const h256 &genesis_hash, std::shared_ptr db, - std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, - std::shared_ptr trx_mgr, const dev::Address &node_addr) { +void TaraxaCapabilityBase::init(const h256 &genesis_hash, std::shared_ptr db, + std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, + std::shared_ptr trx_mgr, const dev::Address &node_addr) { // Creates and registers all packets handlers registerPacketHandlers(genesis_hash, all_packets_stats_, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, node_addr); @@ -62,7 +62,7 @@ void TaraxaCapability::init(const h256 &genesis_hash, std::shared_ptr initPeriodicEvents(pbft_mgr, trx_mgr, all_packets_stats_); } -void TaraxaCapability::addBootNodes(bool initial) { +void TaraxaCapabilityBase::addBootNodes(bool initial) { auto resolveHost = [](const std::string &addr, uint16_t port) { static boost::asio::io_context s_resolverIoService; boost::system::error_code ec; @@ -112,9 +112,9 @@ void TaraxaCapability::addBootNodes(bool initial) { } } -void TaraxaCapability::initPeriodicEvents(const std::shared_ptr &pbft_mgr, - std::shared_ptr trx_mgr, - std::shared_ptr packets_stats) { +void TaraxaCapabilityBase::initPeriodicEvents(const std::shared_ptr &pbft_mgr, + std::shared_ptr trx_mgr, + std::shared_ptr packets_stats) { // TODO: refactor this: // 1. Most of time is this single threaded thread pool doing nothing... // 2. These periodic events are sending packets - that might be processed by main thread_pool ??? @@ -170,7 +170,7 @@ void TaraxaCapability::initPeriodicEvents(const std::shared_ptr &pb }); } -void TaraxaCapability::registerPacketHandlers( +void TaraxaCapabilityBase::registerPacketHandlers( const h256 &genesis_hash, const std::shared_ptr &packets_stats, const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, @@ -217,13 +217,13 @@ void TaraxaCapability::registerPacketHandlers( thread_pool_->setPacketsHandlers(packets_handlers_); } -std::string TaraxaCapability::name() const { return TARAXA_CAPABILITY_NAME; } +std::string TaraxaCapabilityBase::name() const { return TARAXA_CAPABILITY_NAME; } -unsigned TaraxaCapability::version() const { return version_; } +unsigned TaraxaCapabilityBase::version() const { return version_; } -unsigned TaraxaCapability::messageCount() const { return SubprotocolPacketType::PacketCount; } +unsigned TaraxaCapabilityBase::messageCount() const { return SubprotocolPacketType::PacketCount; } -void TaraxaCapability::onConnect(std::weak_ptr session, u256 const &) { +void TaraxaCapabilityBase::onConnect(std::weak_ptr session, u256 const &) { const auto session_p = session.lock(); if (!session_p) { LOG(log_er_) << "Unable to obtain session ptr !"; @@ -245,7 +245,7 @@ void TaraxaCapability::onConnect(std::weak_ptr session, u256 status_packet_handler->sendStatus(node_id, true); } -void TaraxaCapability::onDisconnect(dev::p2p::NodeID const &_nodeID) { +void TaraxaCapabilityBase::onDisconnect(dev::p2p::NodeID const &_nodeID) { LOG(log_nf_) << "Node " << _nodeID << " disconnected"; peers_state_->erasePeer(_nodeID); @@ -261,12 +261,12 @@ void TaraxaCapability::onDisconnect(dev::p2p::NodeID const &_nodeID) { } } -std::string TaraxaCapability::packetTypeToString(unsigned _packetType) const { +std::string TaraxaCapabilityBase::packetTypeToString(unsigned _packetType) const { return convertPacketTypeToString(static_cast(_packetType)); } -void TaraxaCapability::interpretCapabilityPacket(std::weak_ptr session, unsigned _id, - dev::RLP const &_r) { +void TaraxaCapabilityBase::interpretCapabilityPacket(std::weak_ptr session, unsigned _id, + dev::RLP const &_r) { const auto session_p = session.lock(); if (!session_p) { LOG(log_er_) << "Unable to obtain session ptr !"; @@ -356,7 +356,7 @@ void TaraxaCapability::interpretCapabilityPacket(std::weak_ptrpush(threadpool::PacketData(packet_type, node_id, _r.data().toBytes())); } -inline bool TaraxaCapability::filterSyncIrrelevantPackets(SubprotocolPacketType packet_type) const { +inline bool TaraxaCapabilityBase::filterSyncIrrelevantPackets(SubprotocolPacketType packet_type) const { switch (packet_type) { case StatusPacket: case GetPbftSyncPacket: @@ -367,28 +367,28 @@ inline bool TaraxaCapability::filterSyncIrrelevantPackets(SubprotocolPacketType } } -void TaraxaCapability::start() { +void TaraxaCapabilityBase::start() { thread_pool_->startProcessing(); periodic_events_tp_->start(); } -void TaraxaCapability::stop() { +void TaraxaCapabilityBase::stop() { thread_pool_->stopProcessing(); periodic_events_tp_->stop(); } -const std::shared_ptr &TaraxaCapability::getPeersState() { return peers_state_; } +const std::shared_ptr &TaraxaCapabilityBase::getPeersState() { return peers_state_; } -const std::shared_ptr &TaraxaCapability::getNodeStats() { return node_stats_; } +const std::shared_ptr &TaraxaCapabilityBase::getNodeStats() { return node_stats_; } -bool TaraxaCapability::pbft_syncing() const { return pbft_syncing_state_->isPbftSyncing(); } +bool TaraxaCapabilityBase::pbft_syncing() const { return pbft_syncing_state_->isPbftSyncing(); } -void TaraxaCapability::setSyncStatePeriod(PbftPeriod period) { pbft_syncing_state_->setSyncStatePeriod(period); } +void TaraxaCapabilityBase::setSyncStatePeriod(PbftPeriod period) { pbft_syncing_state_->setSyncStatePeriod(period); } // METHODS USED IN TESTS ONLY -size_t TaraxaCapability::getReceivedBlocksCount() const { return test_state_->getBlocksSize(); } +size_t TaraxaCapabilityBase::getReceivedBlocksCount() const { return test_state_->getBlocksSize(); } -size_t TaraxaCapability::getReceivedTransactionsCount() const { return test_state_->getTransactionsSize(); } +size_t TaraxaCapabilityBase::getReceivedTransactionsCount() const { return test_state_->getTransactionsSize(); } // END METHODS USED IN TESTS ONLY } // namespace taraxa::network::tarcap diff --git a/tests/network_test.cpp b/tests/network_test.cpp index b4a56c3f05..11b4d6b01c 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -12,13 +12,13 @@ #include "dag/dag.hpp" #include "dag/dag_block_proposer.hpp" #include "logger/logger.hpp" -#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp" -#include "network/tarcap/packets_handlers/get_dag_sync_packet_handler.hpp" -#include "network/tarcap/packets_handlers/get_next_votes_sync_packet_handler.hpp" -#include "network/tarcap/packets_handlers/status_packet_handler.hpp" -#include "network/tarcap/packets_handlers/transaction_packet_handler.hpp" -#include "network/tarcap/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/packets_handlers/votes_bundle_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "test_util/samples.hpp" #include "test_util/test_util.hpp" diff --git a/tests/p2p_test.cpp b/tests/p2p_test.cpp index 8e65ce16d3..b3be8102b7 100644 --- a/tests/p2p_test.cpp +++ b/tests/p2p_test.cpp @@ -7,18 +7,15 @@ #include #include -#include -#include #include -#include "common/lazy.hpp" #include "common/static_init.hpp" #include "config/config.hpp" #include "logger/logger.hpp" #include "network/network.hpp" -#include "network/tarcap/packets_handlers/dag_block_packet_handler.hpp" -#include "network/tarcap/packets_handlers/transaction_packet_handler.hpp" -#include "network/tarcap/taraxa_capability.hpp" +#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" +#include "network/tarcap/capability_latest/taraxa_capability.hpp" #include "test_util/samples.hpp" #include "test_util/test_util.hpp" @@ -36,7 +33,7 @@ auto g_signed_trx_samples = Lazy([] { return samples::createSignedTrxSamples(0, struct P2PTest : NodesTest {}; // TODO this needs to be removed and called from tracap->setPendingPeersToReady() directly -void setPendingPeersToReady(std::shared_ptr taraxa_capability) { +void setPendingPeersToReady(std::shared_ptr taraxa_capability) { const auto &peers_state = taraxa_capability->getPeersState(); auto peerIds = peers_state->getAllPendingPeersIDs(); @@ -48,10 +45,10 @@ void setPendingPeersToReady(std::shared_ptr makeTarcap(std::weak_ptr host, - const dev::KeyPair &key, - const FullNodeConfig &conf, - const h256 &genesis_hash, unsigned version) { +std::shared_ptr makeTarcap(std::weak_ptr host, + const dev::KeyPair &key, + const FullNodeConfig &conf, + const h256 &genesis_hash, unsigned version) { auto instance = std::make_shared(host, key, conf, version, "TARCAP"); instance->init(genesis_hash, {}, {}, {}, {}, {}, {}, key.address()); return instance; @@ -94,7 +91,7 @@ TEST_F(P2PTest, p2p_discovery) { /* Test creates two host/network/capability and verifies that host connect to each other and that a block packet message can be sent from one host -to the other using TaraxaCapability +to the other using TaraxaCapabilityBase */ TEST_F(P2PTest, capability_send_block) { int const step = 10; @@ -107,7 +104,7 @@ TEST_F(P2PTest, capability_send_block) { FullNodeConfig conf; conf.network.transaction_interval_ms = 1000; h256 genesis; - std::shared_ptr thc1, thc2; + std::shared_ptr thc1, thc2; auto host1 = Host::make( "Test", [&](auto host) { @@ -200,7 +197,7 @@ TEST_F(P2PTest, block_propagate) { FullNodeConfig conf; conf.network.transaction_interval_ms = 1000; h256 genesis; - std::shared_ptr thc1; + std::shared_ptr thc1; auto host1 = Host::make( "Test", [&](auto host) { @@ -212,7 +209,7 @@ TEST_F(P2PTest, block_propagate) { util::ThreadPool tp; tp.post_loop({}, [=] { host1->do_work(); }); std::vector> vHosts; - std::vector> vCapabilities; + std::vector> vCapabilities; for (int i = 0; i < nodeCount; i++) { auto host = vHosts.emplace_back(Host::make( "Test", diff --git a/tests/pbft_manager_test.cpp b/tests/pbft_manager_test.cpp index adcaa4b20f..52b0020eaf 100644 --- a/tests/pbft_manager_test.cpp +++ b/tests/pbft_manager_test.cpp @@ -4,7 +4,7 @@ #include "common/static_init.hpp" #include "logger/logger.hpp" #include "network/network.hpp" -#include "network/tarcap/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" #include "test_util/node_dag_creation_fixture.hpp" #include "vdf/sortition.hpp" diff --git a/tests/vote_test.cpp b/tests/vote_test.cpp index b6c8dab36e..435fe0b2fd 100644 --- a/tests/vote_test.cpp +++ b/tests/vote_test.cpp @@ -4,7 +4,7 @@ #include "common/static_init.hpp" #include "logger/logger.hpp" #include "network/network.hpp" -#include "network/tarcap/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" #include "node/node.hpp" #include "pbft/pbft_manager.hpp" #include "test_util/test_util.hpp" From 312bfce286f0d6eecec94d93cd214169eef6d368 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Thu, 27 Apr 2023 17:02:07 -0700 Subject: [PATCH 040/134] chore: use the same threadpool for all taraxa capabilities .. --- .../network/include/network/network.hpp | 5 +- .../network/tarcap/taraxa_capability_base.hpp | 17 +++++- .../include/network/tarcap/tarcap_version.hpp | 5 ++ .../network/threadpool/packets_queue.hpp | 7 +-- .../network/threadpool/priority_queue.hpp | 5 +- .../network/threadpool/tarcap_thread_pool.hpp | 10 ++-- libraries/core_libs/network/src/network.cpp | 24 +++++---- .../capability_latest/taraxa_capability.cpp | 2 - .../capability_v1/taraxa_capability.cpp | 2 - .../network/src/tarcap/stats/node_stats.cpp | 11 ++-- .../src/tarcap/taraxa_capability_base.cpp | 25 +++++---- .../network/src/threadpool/packets_queue.cpp | 9 ++-- .../network/src/threadpool/priority_queue.cpp | 7 ++- .../src/threadpool/tarcap_thread_pool.cpp | 54 ++++++++++++------- tests/p2p_test.cpp | 42 +++++++++++---- tests/tarcap_threadpool_test.cpp | 31 +++++++---- 16 files changed, 164 insertions(+), 92 deletions(-) create mode 100644 libraries/core_libs/network/include/network/tarcap/tarcap_version.hpp diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index c5943addee..d555225695 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -17,7 +17,8 @@ #include "common/thread_pool.hpp" #include "common/util.hpp" #include "config/config.hpp" -#include "tarcap/taraxa_capability_base.hpp" +#include "network/tarcap/taraxa_capability_base.hpp" +#include "network/tarcap/tarcap_version.hpp" #include "transaction/transaction.hpp" namespace taraxa { @@ -76,7 +77,7 @@ class Network { std::shared_ptr host_; // All supported taraxa capabilities - std::map> tarcaps_; + std::map> tarcaps_; // Threadpool for packets std::shared_ptr packets_tp_; diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp index e6f28d5620..3812a85ad9 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp @@ -13,6 +13,7 @@ #include "network/tarcap/shared_states/peers_state.hpp" #include "network/tarcap/shared_states/test_state.hpp" #include "network/tarcap/stats/node_stats.hpp" +#include "network/tarcap/tarcap_version.hpp" #include "network/threadpool/tarcap_thread_pool.hpp" #include "pbft/pbft_chain.hpp" @@ -35,7 +36,7 @@ class TaraxaPeer; class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { public: TaraxaCapabilityBase(std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, - unsigned version, const std::string &log_channel); + TarcapVersion version, const std::string &log_channel); virtual ~TaraxaCapabilityBase() = default; TaraxaCapabilityBase(const TaraxaCapabilityBase &ro) = delete; @@ -51,7 +52,7 @@ class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { // CapabilityFace implemented interface std::string name() const override; - unsigned version() const override; + TarcapVersion version() const override; unsigned messageCount() const override; virtual void onConnect(std::weak_ptr session, u256 const &) override; virtual void onDisconnect(dev::p2p::NodeID const &_nodeID) override; @@ -62,6 +63,18 @@ class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { template std::shared_ptr getSpecificHandler() const; + /** + * @brief Sets threadpool for packets processing + * + * @param threadpool + */ + void setThreadPool(std::shared_ptr threadpool); + + /** + * @return packets handler + */ + std::shared_ptr getPacketsHandler() const; + /** * @brief Start processing packets */ diff --git a/libraries/core_libs/network/include/network/tarcap/tarcap_version.hpp b/libraries/core_libs/network/include/network/tarcap/tarcap_version.hpp new file mode 100644 index 0000000000..e5580fb61b --- /dev/null +++ b/libraries/core_libs/network/include/network/tarcap/tarcap_version.hpp @@ -0,0 +1,5 @@ +#pragma once + +namespace taraxa::network::tarcap { +using TarcapVersion = unsigned; +} diff --git a/libraries/core_libs/network/include/network/threadpool/packets_queue.hpp b/libraries/core_libs/network/include/network/threadpool/packets_queue.hpp index 5f5c321897..1f2bfe1b21 100644 --- a/libraries/core_libs/network/include/network/threadpool/packets_queue.hpp +++ b/libraries/core_libs/network/include/network/threadpool/packets_queue.hpp @@ -3,6 +3,7 @@ #include #include +#include "network/tarcap/tarcap_version.hpp" #include "network/threadpool/packets_blocking_mask.hpp" #include "packet_data.hpp" @@ -17,7 +18,7 @@ class PacketsQueue { * * @param packet */ - void pushBack(PacketData&& packet); + void pushBack(std::pair&& packet); /** * @brief Return Task from queue. In some rare situations when all packets are blocked for processing due to @@ -28,7 +29,7 @@ class PacketsQueue { * * @return std::optional */ - std::optional pop(const PacketsBlockingMask& packets_blocking_mask); + std::optional> pop(const PacketsBlockingMask& packets_blocking_mask); /** * @return false in case there is already kMaxWorkersCount_ workers processing packets from @@ -72,7 +73,7 @@ class PacketsQueue { size_t getActiveWorkersNum() const; private: - std::list packets_; + std::list> packets_; // How many workers can process packets from this queue at the same time size_t kMaxWorkersCount_{0}; diff --git a/libraries/core_libs/network/include/network/threadpool/priority_queue.hpp b/libraries/core_libs/network/include/network/threadpool/priority_queue.hpp index b76bf4d6b4..196b8407a7 100644 --- a/libraries/core_libs/network/include/network/threadpool/priority_queue.hpp +++ b/libraries/core_libs/network/include/network/threadpool/priority_queue.hpp @@ -7,6 +7,7 @@ #include "logger/logger.hpp" #include "network/tarcap/packet_types.hpp" +#include "network/tarcap/tarcap_version.hpp" #include "network/threadpool/packets_blocking_mask.hpp" #include "packets_queue.hpp" @@ -20,12 +21,12 @@ class PriorityQueue { * @brief Pushes new packet into the priority queue * @param packet */ - void pushBack(PacketData&& packet); + void pushBack(std::pair&& packet); /** * @return std::optional packet with the highest priority & oldest "receive" time */ - std::optional pop(); + std::optional> pop(); /** * @return true of all priority packets_queues_ are empty, otheriwse false diff --git a/libraries/core_libs/network/include/network/threadpool/tarcap_thread_pool.hpp b/libraries/core_libs/network/include/network/threadpool/tarcap_thread_pool.hpp index 11b98f8c15..2e3380a840 100644 --- a/libraries/core_libs/network/include/network/threadpool/tarcap_thread_pool.hpp +++ b/libraries/core_libs/network/include/network/threadpool/tarcap_thread_pool.hpp @@ -6,6 +6,7 @@ #include #include "logger/logger.hpp" +#include "network/tarcap/tarcap_version.hpp" #include "priority_queue.hpp" namespace taraxa::network::tarcap { @@ -35,7 +36,7 @@ class PacketsThreadPool { * * @return packet unique ID. In case push was not successful, empty optional is returned **/ - std::optional push(PacketData&& packet_data); + std::optional push(std::pair&& packet_data); /** * @brief Start all processing threads @@ -57,7 +58,8 @@ class PacketsThreadPool { * * @param packets_handlers */ - void setPacketsHandlers(std::shared_ptr packets_handlers); + void setPacketsHandlers(tarcap::TarcapVersion tarcap_version, + std::shared_ptr packets_handlers); /** * @brief Returns actual size of all priority queues (thread-safe) @@ -74,8 +76,8 @@ class PacketsThreadPool { // Number of workers(threads) const size_t workers_num_; - // Common packets handler - std::shared_ptr packets_handlers_; + // Common packets handler - each taraxa capability haits own packets handler + std::unordered_map> packets_handlers_; // If true, stop processing packets and join all workers threads std::atomic stopProcessing_{false}; diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index d5023ceb6c..7dd88894da 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -75,9 +75,13 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, } host_ = dev::p2p::Host::make(net_version, construct_capabilities, key, net_conf, taraxa_net_conf, network_file_path); - for (const auto &tarcap : host_->getSupportedCapabilities()) { - tarcaps_.emplace(tarcap.second.ref->version(), - std::static_pointer_cast(tarcap.second.ref)); + for (const auto &cap : host_->getSupportedCapabilities()) { + const auto tarcap_version = cap.second.ref->version(); + auto tarcap = std::static_pointer_cast(cap.second.ref); + packets_tp_->setPacketsHandlers(tarcap_version, tarcap->getPacketsHandler()); + + tarcap->setThreadPool(packets_tp_); + tarcaps_[tarcap_version] = std::move(tarcap); } for (uint i = 0; i < tp_.capacity(); ++i) { @@ -93,25 +97,25 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, Network::~Network() { tp_.stop(); - // packets_tp_->stopProcessing(); + packets_tp_->stopProcessing(); // periodic_events_tp_.stop(); // TODO: remove once packets_tp_ and periodic_events_tp_ are moved from tarcaps to network - for (auto &tarcap : host_->getSupportedCapabilities()) { - std::static_pointer_cast(tarcap.second.ref)->stop(); + for (auto &tarcap : tarcaps_) { + tarcap.second->stop(); } } void Network::start() { - tp_.start(); - // packets_tp_->startProcessing(); + packets_tp_->startProcessing(); // periodic_events_tp_.start(); // TODO: remove once packets_tp_ and periodic_events_tp_ are moved from tarcaps to network - for (auto &tarcap : host_->getSupportedCapabilities()) { - std::static_pointer_cast(tarcap.second.ref)->start(); + for (auto &tarcap : tarcaps_) { + tarcap.second->start(); } + tp_.start(); LOG(log_nf_) << "Started Node id: " << host_->id() << ", listening on port " << host_->listenPort(); } diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp index db39e3d583..b217a74298 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp @@ -56,7 +56,5 @@ void TaraxaCapability::registerPacketHandlers( packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, periodic_events_tp_, db, node_addr); - - thread_pool_->setPacketsHandlers(packets_handlers_); } } // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/src/tarcap/capability_v1/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/capability_v1/taraxa_capability.cpp index cbb10b2f57..b7d12b1ca5 100644 --- a/libraries/core_libs/network/src/tarcap/capability_v1/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_v1/taraxa_capability.cpp @@ -56,7 +56,5 @@ void TaraxaCapability::registerPacketHandlers( packets_handlers_->registerHandler( kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, periodic_events_tp_, db, node_addr); - - thread_pool_->setPacketsHandlers(packets_handlers_); } } // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp b/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp index 47dab60a48..3a56cedbd0 100644 --- a/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp +++ b/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp @@ -176,11 +176,12 @@ void NodeStats::logNodeStats() { LOG(log_dg_) << "Non finalized dag blocks levels: " << non_finalized_blocks_levels; LOG(log_dg_) << "Non finalized dag blocks size: " << non_finalized_blocks_size; - const auto [high_priority_queue_size, mid_priority_queue_size, low_priority_queue_size] = - thread_pool_->getQueueSize(); - LOG(log_dg_) << "High priority queue size: " << high_priority_queue_size; - LOG(log_dg_) << "Mid priority queue size: " << mid_priority_queue_size; - LOG(log_dg_) << "Low priority queue size: " << low_priority_queue_size; + // TODO: enable once periodic events moved to network class + // const auto [high_priority_queue_size, mid_priority_queue_size, low_priority_queue_size] = + // thread_pool_->getQueueSize(); + // LOG(log_dg_) << "High priority queue size: " << high_priority_queue_size; + // LOG(log_dg_) << "Mid priority queue size: " << mid_priority_queue_size; + // LOG(log_dg_) << "Low priority queue size: " << low_priority_queue_size; LOG(log_nf_) << "------------- tl;dr -------------"; diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp index af7cbb2fc8..56d0e073e5 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp @@ -27,7 +27,8 @@ namespace taraxa::network::tarcap { TaraxaCapabilityBase::TaraxaCapabilityBase(std::weak_ptr host, const dev::KeyPair &key, - const FullNodeConfig &conf, unsigned version, const std::string &log_channel) + const FullNodeConfig &conf, TarcapVersion version, + const std::string &log_channel) : test_state_(std::make_shared()), version_(version), kConf(conf), @@ -35,8 +36,7 @@ TaraxaCapabilityBase::TaraxaCapabilityBase(std::weak_ptr host, c pbft_syncing_state_(std::make_shared(conf.network.deep_syncing_threshold)), node_stats_(nullptr), packets_handlers_(std::make_shared()), - thread_pool_( - std::make_shared(conf.network.packets_processing_threads, key.address())), + thread_pool_(nullptr), periodic_events_tp_(std::make_shared(kPeriodicEventsThreadCount, false)), pub_key_(key.pub()) { const auto &node_addr = key.address(); @@ -214,12 +214,12 @@ void TaraxaCapabilityBase::registerPacketHandlers( pbft_chain, pbft_mgr, dag_mgr, vote_mgr, periodic_events_tp_, db, node_addr); - thread_pool_->setPacketsHandlers(packets_handlers_); + thread_pool_->setPacketsHandlers(version(), packets_handlers_); } std::string TaraxaCapabilityBase::name() const { return TARAXA_CAPABILITY_NAME; } -unsigned TaraxaCapabilityBase::version() const { return version_; } +TarcapVersion TaraxaCapabilityBase::version() const { return version_; } unsigned TaraxaCapabilityBase::messageCount() const { return SubprotocolPacketType::PacketCount; } @@ -353,7 +353,7 @@ void TaraxaCapabilityBase::interpretCapabilityPacket(std::weak_ptrpush(threadpool::PacketData(packet_type, node_id, _r.data().toBytes())); + thread_pool_->push({version(), threadpool::PacketData(packet_type, node_id, _r.data().toBytes())}); } inline bool TaraxaCapabilityBase::filterSyncIrrelevantPackets(SubprotocolPacketType packet_type) const { @@ -367,15 +367,14 @@ inline bool TaraxaCapabilityBase::filterSyncIrrelevantPackets(SubprotocolPacketT } } -void TaraxaCapabilityBase::start() { - thread_pool_->startProcessing(); - periodic_events_tp_->start(); +void TaraxaCapabilityBase::setThreadPool(std::shared_ptr threadpool) { + thread_pool_ = std::move(threadpool); } +std::shared_ptr TaraxaCapabilityBase::getPacketsHandler() const { return packets_handlers_; } -void TaraxaCapabilityBase::stop() { - thread_pool_->stopProcessing(); - periodic_events_tp_->stop(); -} +void TaraxaCapabilityBase::start() { periodic_events_tp_->start(); } + +void TaraxaCapabilityBase::stop() { periodic_events_tp_->stop(); } const std::shared_ptr &TaraxaCapabilityBase::getPeersState() { return peers_state_; } diff --git a/libraries/core_libs/network/src/threadpool/packets_queue.cpp b/libraries/core_libs/network/src/threadpool/packets_queue.cpp index 3cbcb8d432..9813bc3988 100644 --- a/libraries/core_libs/network/src/threadpool/packets_queue.cpp +++ b/libraries/core_libs/network/src/threadpool/packets_queue.cpp @@ -10,19 +10,20 @@ bool PacketsQueue::maxWorkersCountReached() const { return false; } -void PacketsQueue::pushBack(PacketData&& packet) { +void PacketsQueue::pushBack(std::pair&& packet) { packets_.push_back(std::move(packet)); act_packets_count_++; } -std::optional PacketsQueue::pop(const PacketsBlockingMask& packets_blocking_mask) { +std::optional> PacketsQueue::pop( + const PacketsBlockingMask& packets_blocking_mask) { for (auto packet_it = packets_.begin(); packet_it != packets_.end(); ++packet_it) { // Packet type is currently blocked for processing - if (packets_blocking_mask.isPacketBlocked(*packet_it)) { + if (packets_blocking_mask.isPacketBlocked(packet_it->second)) { continue; } - std::optional ret = std::move(*packet_it); + std::optional> ret = std::move(*packet_it); packets_.erase(packet_it); assert(act_packets_count_); diff --git a/libraries/core_libs/network/src/threadpool/priority_queue.cpp b/libraries/core_libs/network/src/threadpool/priority_queue.cpp index a2374b3163..35a16922b5 100644 --- a/libraries/core_libs/network/src/threadpool/priority_queue.cpp +++ b/libraries/core_libs/network/src/threadpool/priority_queue.cpp @@ -31,7 +31,10 @@ PriorityQueue::PriorityQueue(size_t tp_workers_count, const addr_t& node_addr) << ", Low priority packets max num of workers = " << low_priority_queue_workers; } -void PriorityQueue::pushBack(PacketData&& packet) { packets_queues_[packet.priority_].pushBack(std::move(packet)); } +void PriorityQueue::pushBack(std::pair&& packet) { + const auto priority = packet.second.priority_; + packets_queues_[priority].pushBack(std::move(packet)); +} bool PriorityQueue::canBorrowThread() { size_t reserved_threads_num = 0; @@ -48,7 +51,7 @@ bool PriorityQueue::canBorrowThread() { return act_total_workers_count_ < (MAX_TOTAL_WORKERS_COUNT - reserved_threads_num); } -std::optional PriorityQueue::pop() { +std::optional> PriorityQueue::pop() { if (act_total_workers_count_ >= MAX_TOTAL_WORKERS_COUNT) { LOG(log_dg_) << "MAX_TOTAL_WORKERS_COUNT(" << MAX_TOTAL_WORKERS_COUNT << ") reached, unable to pop data."; return {}; diff --git a/libraries/core_libs/network/src/threadpool/tarcap_thread_pool.cpp b/libraries/core_libs/network/src/threadpool/tarcap_thread_pool.cpp index 445af95873..bc49a42c98 100644 --- a/libraries/core_libs/network/src/threadpool/tarcap_thread_pool.cpp +++ b/libraries/core_libs/network/src/threadpool/tarcap_thread_pool.cpp @@ -6,7 +6,7 @@ namespace taraxa::network::threadpool { PacketsThreadPool::PacketsThreadPool(size_t workers_num, const addr_t& node_addr) : workers_num_(workers_num), - packets_handlers_(nullptr), + packets_handlers_(), stopProcessing_(false), packets_count_(0), queue_(workers_num, node_addr), @@ -31,13 +31,13 @@ PacketsThreadPool::~PacketsThreadPool() { * * @return packet unique ID. In case push was not successful, empty optional is returned **/ -std::optional PacketsThreadPool::push(PacketData&& packet_data) { +std::optional PacketsThreadPool::push(std::pair&& packet_data) { if (stopProcessing_) { LOG(log_wr_) << "Trying to push packet while tp processing is stopped"; return {}; } - std::string packet_type_str = packet_data.type_str_; + std::string packet_type_str = packet_data.second.type_str_; uint64_t packet_unique_id; { // Put packet into the priority queue @@ -45,7 +45,7 @@ std::optional PacketsThreadPool::push(PacketData&& packet_data) { // Create packet unique id packet_unique_id = packets_count_++; - packet_data.id_ = packet_unique_id; + packet_data.second.id_ = packet_unique_id; queue_.pushBack(std::move(packet_data)); cond_var_.notify_one(); @@ -56,7 +56,7 @@ std::optional PacketsThreadPool::push(PacketData&& packet_data) { } void PacketsThreadPool::startProcessing() { - assert(packets_handlers_ != nullptr); + assert(!packets_handlers_.empty()); try { for (size_t idx = 0; idx < workers_num_; idx++) { @@ -82,7 +82,7 @@ void PacketsThreadPool::processPacket(size_t worker_id) { std::unique_lock lock(queue_mutex_, std::defer_lock); // Packet to be processed - std::optional packet; + std::optional> packet; while (stopProcessing_ == false) { lock.lock(); @@ -100,33 +100,49 @@ void PacketsThreadPool::processPacket(size_t worker_id) { cond_var_.wait(lock); } - LOG(log_dg_) << "Worker (" << worker_id << ") process packet: " << packet->type_str_ << ", id(" << packet->id_ - << ")"; + LOG(log_dg_) << "Worker (" << worker_id << ") process packet: " << packet->second.type_str_ + << ", id: " << packet->second.id_ << ", tarcap version: " << packet->first; - queue_.updateDependenciesStart(*packet); + queue_.updateDependenciesStart(packet->second); lock.unlock(); try { - // Get specific packet handler according to packet type - auto& handler = packets_handlers_->getSpecificHandler(packet->type_); + // Get packets handler based on tarcap version + const auto packets_handler = packets_handlers_.find(packet->first); + if (packets_handler == packets_handlers_.end()) { + LOG(log_er_) << "Worker (" << worker_id << ") process packet: " << packet->second.type_str_ + << ", id: " << packet->second.id_ << ", tarcap version: " << packet->first + << " error: Unsupported tarcap version !"; + assert(false); + throw std::runtime_error("Unsupported tarcap version " + std::to_string(packet->first)); + } + + // Get specific packet handler based on packet type + auto& handler = packets_handler->second->getSpecificHandler(packet->second.type_); // Process packet by specific packet type handler - handler->processPacket(*packet); + handler->processPacket(packet->second); } catch (const std::exception& e) { - LOG(log_er_) << "Worker (" << worker_id << ") packet: " << packet->type_str_ << ", id(" << packet->id_ - << ") processing exception caught: " << e.what(); + LOG(log_er_) << "Worker (" << worker_id << ") process packet: " << packet->second.type_str_ + << ", id: " << packet->second.id_ << ", tarcap version: " << packet->first + << " processing exception caught: " << e.what(); } catch (...) { - LOG(log_er_) << "Worker (" << worker_id << ") packet: " << packet->type_str_ << ", id(" << packet->id_ - << ") processing unknown exception caught"; + LOG(log_er_) << "Worker (" << worker_id << ") process packet: " << packet->second.type_str_ + << ", id: " << packet->second.id_ << ", tarcap version: " << packet->first + << " processing unknown exception caught"; } // Once packet handler is done with processing, update priority queue dependencies - queue_.updateDependenciesFinish(*packet, queue_mutex_, cond_var_); + queue_.updateDependenciesFinish(packet->second, queue_mutex_, cond_var_); } } -void PacketsThreadPool::setPacketsHandlers(std::shared_ptr packets_handlers) { - packets_handlers_ = std::move(packets_handlers); +void PacketsThreadPool::setPacketsHandlers(tarcap::TarcapVersion tarcap_version, + std::shared_ptr packets_handlers) { + if (!packets_handlers_.emplace(tarcap_version, std::move(packets_handlers)).second) { + LOG(log_er_) << "Packets handler for capability version " << tarcap_version << " already set"; + assert(false); + } } std::tuple PacketsThreadPool::getQueueSize() const { diff --git a/tests/p2p_test.cpp b/tests/p2p_test.cpp index b3be8102b7..857e51c093 100644 --- a/tests/p2p_test.cpp +++ b/tests/p2p_test.cpp @@ -45,13 +45,18 @@ void setPendingPeersToReady(std::shared_ptr makeTarcap(std::weak_ptr host, - const dev::KeyPair &key, - const FullNodeConfig &conf, - const h256 &genesis_hash, unsigned version) { - auto instance = std::make_shared(host, key, conf, version, "TARCAP"); - instance->init(genesis_hash, {}, {}, {}, {}, {}, {}, key.address()); - return instance; +std::shared_ptr makeTarcap( + std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, const h256 &genesis_hash, + unsigned version, std::shared_ptr thread_pool = nullptr) { + auto tarcap = std::make_shared(host, key, conf, version, "TARCAP"); + tarcap->init(genesis_hash, {}, {}, {}, {}, {}, {}, key.address()); + + if (thread_pool) { + thread_pool->setPacketsHandlers(version, tarcap->getPacketsHandler()); + tarcap->setThreadPool(thread_pool); + } + + return tarcap; } /* @@ -104,21 +109,29 @@ TEST_F(P2PTest, capability_send_block) { FullNodeConfig conf; conf.network.transaction_interval_ms = 1000; h256 genesis; + + auto packets_tp1 = std::make_shared(conf.network.packets_processing_threads); + auto packets_tp2 = std::make_shared(conf.network.packets_processing_threads); + std::shared_ptr thc1, thc2; auto host1 = Host::make( "Test", [&](auto host) { - thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); + thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION, packets_tp1); return Host::CapabilityList{thc1}; }, KeyPair::create(), prefs1); auto host2 = Host::make( "Test", [&](auto host) { - thc2 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); + thc2 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION, packets_tp2); return Host::CapabilityList{thc2}; }, KeyPair::create(), prefs2); + + packets_tp1->startProcessing(); + packets_tp2->startProcessing(); + util::ThreadPool tp; tp.post_loop({}, [=] { host1->do_work(); }); tp.post_loop({}, [=] { host2->do_work(); }); @@ -198,29 +211,36 @@ TEST_F(P2PTest, block_propagate) { conf.network.transaction_interval_ms = 1000; h256 genesis; std::shared_ptr thc1; + auto packets_tp1 = std::make_shared(conf.network.packets_processing_threads); auto host1 = Host::make( "Test", [&](auto host) { - thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); + thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION, packets_tp1); thc1->start(); return Host::CapabilityList{thc1}; }, KeyPair::create(), prefs1, taraxa_net_conf_1); + packets_tp1->startProcessing(); + util::ThreadPool tp; tp.post_loop({}, [=] { host1->do_work(); }); std::vector> vHosts; std::vector> vCapabilities; for (int i = 0; i < nodeCount; i++) { + auto packets_tp = std::make_shared(conf.network.packets_processing_threads); auto host = vHosts.emplace_back(Host::make( "Test", [&](auto host) { - auto cap = vCapabilities.emplace_back(makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION)); + auto cap = vCapabilities.emplace_back( + makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION, packets_tp)); cap->start(); return Host::CapabilityList{cap}; }, KeyPair::create(), vPrefs[i])); + packets_tp->startProcessing(); tp.post_loop({}, [=] { host->do_work(); }); } + printf("Starting %d hosts\n", nodeCount); auto port1 = host1->listenPort(); EXPECT_NE(port1, 0); diff --git a/tests/tarcap_threadpool_test.cpp b/tests/tarcap_threadpool_test.cpp index 136f4c2b75..df24fd7c2a 100644 --- a/tests/tarcap_threadpool_test.cpp +++ b/tests/tarcap_threadpool_test.cpp @@ -237,14 +237,17 @@ HandlersInitData createHandlersInitData() { return ret_init_data; } -threadpool::PacketData createPacket(const dev::p2p::NodeID& sender_node_id, SubprotocolPacketType packet_type, - std::optional> packet_rlp_bytes = {}) { +std::pair createPacket( + const dev::p2p::NodeID& sender_node_id, SubprotocolPacketType packet_type, + std::optional> packet_rlp_bytes = {}) { if (packet_rlp_bytes.has_value()) { - return {packet_type, sender_node_id, std::move(packet_rlp_bytes.value())}; + threadpool::PacketData packet_data(packet_type, sender_node_id, std::move(packet_rlp_bytes.value())); + return {TARAXA_NET_VERSION, std::move(packet_data)}; } dev::RLPStream s(0); - return {packet_type, sender_node_id, s.invalidate()}; + threadpool::PacketData packet_data(packet_type, sender_node_id, s.invalidate()); + return {TARAXA_NET_VERSION, std::move(packet_data)}; } bytes createDagBlockRlp(level_t level, uint32_t sig = 777) { @@ -329,9 +332,15 @@ TEST_F(TarcapTpTest, block_free_packets) { // Note: make num of threads >= num of packets to check if they are processed concurrently without blocks, otherwise // some blocks would be blocked for processing due to max threads limit threadpool::PacketsThreadPool tp(18); - tp.setPacketsHandlers(packets_handler); + tp.setPacketsHandlers(TARAXA_NET_VERSION, packets_handler); // Pushes packets to the tp + auto packet = createPacket(init_data.copySender(), SubprotocolPacketType::TransactionPacket, {}); + if (packet.second.rlp_.isList()) { + std::cout << "is list"; + } else { + std::cout << "not list"; + } const auto packet0_tx_id = tp.push(createPacket(init_data.copySender(), SubprotocolPacketType::TransactionPacket, {})).value(); const auto packet1_tx_id = @@ -467,7 +476,7 @@ TEST_F(TarcapTpTest, hard_blocking_deps) { // Creates threadpool threadpool::PacketsThreadPool tp(10); - tp.setPacketsHandlers(packets_handler); + tp.setPacketsHandlers(TARAXA_NET_VERSION, packets_handler); // Pushes packets to the tp const auto packet0_dag_sync_id = @@ -589,7 +598,7 @@ TEST_F(TarcapTpTest, peer_order_blocking_deps) { // Creates threadpool threadpool::PacketsThreadPool tp(10); - tp.setPacketsHandlers(packets_handler); + tp.setPacketsHandlers(TARAXA_NET_VERSION, packets_handler); // Pushes packets to the tp const auto packet0_tx_id = @@ -675,7 +684,7 @@ TEST_F(TarcapTpTest, same_dag_blks_ordering) { // Creates threadpool threadpool::PacketsThreadPool tp(10); - tp.setPacketsHandlers(packets_handler); + tp.setPacketsHandlers(TARAXA_NET_VERSION, packets_handler); auto dag_block = createDagBlockRlp(0); @@ -740,7 +749,7 @@ TEST_F(TarcapTpTest, dag_blks_lvls_ordering) { // Creates threadpool threadpool::PacketsThreadPool tp(10); - tp.setPacketsHandlers(packets_handler); + tp.setPacketsHandlers(TARAXA_NET_VERSION, packets_handler); // Pushes packets to the tp const auto blk0_lvl1_id = @@ -842,7 +851,7 @@ TEST_F(TarcapTpTest, threads_borrowing) { // Creates threadpool const size_t threads_num = 10; threadpool::PacketsThreadPool tp(threads_num); - tp.setPacketsHandlers(packets_handler); + tp.setPacketsHandlers(TARAXA_NET_VERSION, packets_handler); // Pushes packets to the tp std::vector pushed_packets_ids; @@ -933,7 +942,7 @@ TEST_F(TarcapTpTest, low_priotity_queue_starvation) { // Creates threadpool size_t threads_num = 10; threadpool::PacketsThreadPool tp(threads_num); - tp.setPacketsHandlers(packets_handler); + tp.setPacketsHandlers(TARAXA_NET_VERSION, packets_handler); // Push 10x more packets for each prioriy queue than max tp capacity to make sure that tp wont be able to process all // packets from each queue concurrently -> many packets will be waiting due to max threads num reached for specific From 8def6d9e6169b61e162a23fc901362ab2265bf5d Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Tue, 2 May 2023 11:53:03 -0700 Subject: [PATCH 041/134] chore: move periodic events from tarcap to network class --- .../network/include/network/network.hpp | 41 +++- .../pbft_sync_packet_handler.hpp | 5 +- .../pbft_sync_packet_handler.hpp | 6 +- .../network/tarcap/stats/node_stats.hpp | 15 +- .../stats/time_period_packets_stats.hpp | 3 +- .../network/tarcap/taraxa_capability_base.hpp | 33 +--- libraries/core_libs/network/src/network.cpp | 180 +++++++++++++++--- .../pbft_sync_packet_handler.cpp | 16 +- .../capability_latest/taraxa_capability.cpp | 8 +- .../pbft_sync_packet_handler.cpp | 25 ++- .../capability_v1/taraxa_capability.cpp | 6 +- .../network/src/tarcap/stats/node_stats.cpp | 109 ++++++----- .../stats/time_period_packets_stats.cpp | 8 +- .../src/tarcap/taraxa_capability_base.cpp | 143 +------------- tests/p2p_test.cpp | 7 +- 15 files changed, 292 insertions(+), 313 deletions(-) diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index d555225695..6491f34c67 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -23,6 +23,11 @@ namespace taraxa { +namespace network::tarcap { +class TimePeriodPacketsStats; +class NodeStats; +} // namespace network::tarcap + class PacketHandler; class Network { @@ -40,13 +45,12 @@ class Network { Network &operator=(const Network &) = delete; Network &operator=(Network &&) = delete; - static std::pair resolveHost(string const &addr, uint16_t port); - // METHODS USED IN REAL CODE void start(); bool isStarted(); std::list getAllNodes() const; size_t getPeerCount(); + // returns count of all discovered nodes unsigned getNodeCount(); Json::Value getStatus(); @@ -73,6 +77,39 @@ class Network { // END METHODS USED IN TESTS ONLY private: + static std::pair resolveHost(string const &addr, uint16_t port); + + /** + * @brief Register period events, e.g. sending status packet, transaction packet etc... + * + * @param config + * @param pbft_mgr + * @param trx_mgr + */ + void registerPeriodicEvents(const std::shared_ptr &pbft_mgr, + std::shared_ptr trx_mgr); + + void addBootNodes(bool initial = false); + + private: + // Node config + const FullNodeConfig &kConf; + + // Node public key + const dev::Public pub_key_; + + // Packets stats per time period + // TODO: maybe remove tarcap namespace ??? + std::shared_ptr all_packets_stats_; + + // Node stats + // TODO: maybe remove tarcap namespace ??? + std::shared_ptr node_stats_; + + // Syncing state + // TODO: maybe remove tarcap namespace ??? + std::shared_ptr pbft_syncing_state_; + util::ThreadPool tp_; std::shared_ptr host_; diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp index 391d689753..d535061e14 100644 --- a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp @@ -11,8 +11,7 @@ class PbftSyncPacketHandler final : public ExtSyncingPacketHandler { std::shared_ptr packets_stats, std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, - std::shared_ptr vote_mgr, std::shared_ptr periodic_events_tp, - std::shared_ptr db, const addr_t& node_addr); + std::shared_ptr vote_mgr, std::shared_ptr db, const addr_t& node_addr); void handleMaliciousSyncPeer(const dev::p2p::NodeID& id); @@ -28,7 +27,7 @@ class PbftSyncPacketHandler final : public ExtSyncingPacketHandler { std::shared_ptr vote_mgr_; - std::weak_ptr periodic_events_tp_; + util::ThreadPool periodic_events_tp_; static constexpr size_t kStandardPacketSize = 2; static constexpr size_t kChainSyncedPacketSize = 3; diff --git a/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp index 6e77e9a4b9..b56789305d 100644 --- a/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp @@ -12,8 +12,7 @@ class PbftSyncPacketHandler final : public tarcap::ExtSyncingPacketHandler { std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr vote_mgr, - std::shared_ptr periodic_events_tp, std::shared_ptr db, - const addr_t& node_addr); + std::shared_ptr db, const addr_t& node_addr); void handleMaliciousSyncPeer(dev::p2p::NodeID const& id); @@ -28,8 +27,7 @@ class PbftSyncPacketHandler final : public tarcap::ExtSyncingPacketHandler { void delayedPbftSync(int counter); std::shared_ptr vote_mgr_; - - std::weak_ptr periodic_events_tp_; + util::ThreadPool periodic_events_tp_; static constexpr size_t kStandardPacketSize = 2; static constexpr size_t kChainSyncedPacketSize = 3; diff --git a/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp b/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp index 605297e0b0..d017ce5cbe 100644 --- a/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp +++ b/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp @@ -18,24 +18,23 @@ class PacketsThreadPool; namespace taraxa::network::tarcap { -class PeersState; +class TaraxaPeer; class PbftSyncingState; class TimePeriodPacketsStats; class NodeStats { public: - NodeStats(std::shared_ptr peers_state, std::shared_ptr pbft_syncing_state, - std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, - std::shared_ptr dag_mgr, std::shared_ptr vote_mgr, - std::shared_ptr trx_mgr, std::shared_ptr packets_stats, - std::shared_ptr thread_pool, const addr_t &node_addr); + NodeStats(std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, + std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, + std::shared_ptr vote_mgr, std::shared_ptr trx_mgr, + std::shared_ptr packets_stats, + std::shared_ptr thread_pool, const addr_t& node_addr); - void logNodeStats(); + void logNodeStats(const std::vector>& all_peers, size_t nodes_count); uint64_t syncTimeSeconds() const; Json::Value getStatus() const; private: - std::shared_ptr peers_state_; std::shared_ptr pbft_syncing_state_; std::shared_ptr pbft_chain_; std::shared_ptr pbft_mgr_; diff --git a/libraries/core_libs/network/include/network/tarcap/stats/time_period_packets_stats.hpp b/libraries/core_libs/network/include/network/tarcap/stats/time_period_packets_stats.hpp index cf30aa3a06..4d8c7e99fb 100644 --- a/libraries/core_libs/network/include/network/tarcap/stats/time_period_packets_stats.hpp +++ b/libraries/core_libs/network/include/network/tarcap/stats/time_period_packets_stats.hpp @@ -8,6 +8,7 @@ namespace taraxa::network::tarcap { class PeersState; +class TaraxaPeer; /** * @brief Stats for all received and sent packets of all types @@ -22,7 +23,7 @@ class TimePeriodPacketsStats { /** * @brief Logs both received as well as sent packets stats + updates max count/size and reset stats */ - void processStats(const std::shared_ptr& peers_state); + void processStats(const std::vector>& all_peers); private: /** diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp index 3812a85ad9..9152abcf69 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp @@ -12,7 +12,6 @@ #include "network/tarcap/packets_handler.hpp" #include "network/tarcap/shared_states/peers_state.hpp" #include "network/tarcap/shared_states/test_state.hpp" -#include "network/tarcap/stats/node_stats.hpp" #include "network/tarcap/tarcap_version.hpp" #include "network/threadpool/tarcap_thread_pool.hpp" #include "pbft/pbft_chain.hpp" @@ -36,7 +35,8 @@ class TaraxaPeer; class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { public: TaraxaCapabilityBase(std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, - TarcapVersion version, const std::string &log_channel); + TarcapVersion version, std::shared_ptr packets_stats, + std::shared_ptr syncing_state, const std::string &log_channel); virtual ~TaraxaCapabilityBase() = default; TaraxaCapabilityBase(const TaraxaCapabilityBase &ro) = delete; @@ -64,6 +64,7 @@ class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { std::shared_ptr getSpecificHandler() const; /** + * @brief Sets threadpool for packets processing * @brief Sets threadpool for packets processing * * @param threadpool @@ -75,23 +76,9 @@ class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { */ std::shared_ptr getPacketsHandler() const; - /** - * @brief Start processing packets - */ - void start(); - - /** - * @brief Stop processing packets - */ - void stop(); - // Interface required in network class to access packets handlers functionality // METHODS USED IN REAL CODE const std::shared_ptr &getPeersState(); - const std::shared_ptr &getNodeStats(); - - bool pbft_syncing() const; - void setSyncStatePeriod(PbftPeriod period); // METHODS USED IN TESTS ONLY size_t getReceivedBlocksCount() const; @@ -99,9 +86,6 @@ class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { // END METHODS USED IN TESTS ONLY protected: - virtual void initPeriodicEvents(const std::shared_ptr &pbft_mgr, - std::shared_ptr trx_mgr, - std::shared_ptr packets_stats); virtual void registerPacketHandlers(const h256 &genesis_hash, const std::shared_ptr &packets_stats, const std::shared_ptr &db, @@ -112,8 +96,6 @@ class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { const std::shared_ptr &trx_mgr, const addr_t &node_addr) = 0; private: - void addBootNodes(bool initial = false); - bool filterSyncIrrelevantPackets(SubprotocolPacketType packet_type) const; public: @@ -137,21 +119,12 @@ class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { // Syncing state + syncing handler std::shared_ptr pbft_syncing_state_; - // Node stats - std::shared_ptr node_stats_; - // Packets handlers std::shared_ptr packets_handlers_; // Main Threadpool for processing packets std::shared_ptr thread_pool_; - // Threadpool for periodic and delayed events - std::shared_ptr periodic_events_tp_; - - // Node public key - const dev::Public pub_key_; - LOG_OBJECTS_DEFINE }; diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index 7dd88894da..f31c198302 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -10,10 +10,16 @@ #include "config/version.hpp" #include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" #include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp" +#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" #include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" #include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" #include "network/tarcap/capability_latest/taraxa_capability.hpp" #include "network/tarcap/capability_v1/taraxa_capability.hpp" +#include "network/tarcap/shared_states/pbft_syncing_state.hpp" +#include "network/tarcap/stats/node_stats.hpp" +#include "network/tarcap/stats/time_period_packets_stats.hpp" +#include "pbft/pbft_manager.hpp" namespace taraxa { @@ -23,13 +29,26 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr) - : tp_(config.network.num_threads, false), + : kConf(config), + pub_key_(key.pub()), + all_packets_stats_(nullptr), + node_stats_(nullptr), + pbft_syncing_state_(std::make_shared(config.network.deep_syncing_threshold)), + tp_(config.network.num_threads, false), packets_tp_(std::make_shared(config.network.packets_processing_threads, - key.address())) { + key.address())), + periodic_events_tp_(kPeriodicEventsThreadCount, false) { auto const &node_addr = key.address(); LOG_OBJECTS_CREATE("NETWORK"); LOG(log_nf_) << "Read Network Config: " << std::endl << config.network << std::endl; + all_packets_stats_ = std::make_shared( + kConf.network.ddos_protection.packets_stats_time_period_ms, node_addr); + + node_stats_ = + std::make_shared(pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, + trx_mgr, all_packets_stats_, packets_tp_, node_addr); + // TODO make all these properties configurable dev::p2p::NetworkConfig net_conf; net_conf.listenIPAddress = config.network.listen_ip; @@ -59,14 +78,14 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, dev::p2p::Host::CapabilityList capabilities; // Register old version of taraxa capability - auto v1_tarcap = - std::make_shared(host, key, config, kOldNetworkVersion, "V1_TARCAP"); + auto v1_tarcap = std::make_shared( + host, key, config, kOldNetworkVersion, all_packets_stats_, pbft_syncing_state_, "V1_TARCAP"); v1_tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); capabilities.emplace_back(v1_tarcap); // Register new version of taraxa capability - auto v2_tarcap = - std::make_shared(host, key, config, TARAXA_NET_VERSION, "TARCAP"); + auto v2_tarcap = std::make_shared( + host, key, config, TARAXA_NET_VERSION, all_packets_stats_, pbft_syncing_state_, "TARCAP"); v2_tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); capabilities.emplace_back(v2_tarcap); @@ -84,6 +103,11 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, tarcaps_[tarcap_version] = std::move(tarcap); } + addBootNodes(true); + + // Register periodic events. Must be called after full init of tarcaps_ + registerPeriodicEvents(pbft_mgr, trx_mgr); + for (uint i = 0; i < tp_.capacity(); ++i) { tp_.post_loop({100 + i * 20}, [this] { while (0 < host_->do_work()) @@ -97,25 +121,15 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, Network::~Network() { tp_.stop(); + periodic_events_tp_.stop(); packets_tp_->stopProcessing(); - // periodic_events_tp_.stop(); - - // TODO: remove once packets_tp_ and periodic_events_tp_ are moved from tarcaps to network - for (auto &tarcap : tarcaps_) { - tarcap.second->stop(); - } } void Network::start() { packets_tp_->startProcessing(); - // periodic_events_tp_.start(); - - // TODO: remove once packets_tp_ and periodic_events_tp_ are moved from tarcaps to network - for (auto &tarcap : tarcaps_) { - tarcap.second->start(); - } - + periodic_events_tp_.start(); tp_.start(); + LOG(log_nf_) << "Started Node id: " << host_->id() << ", listening on port " << host_->listenPort(); } @@ -127,21 +141,129 @@ size_t Network::getPeerCount() { return host_->peer_count(); } unsigned Network::getNodeCount() { return host_->getNodeCount(); } -Json::Value Network::getStatus() { - // TODO: refactor this: combine node stats from all tarcaps... - return tarcaps_.end()->second->getNodeStats()->getStatus(); +Json::Value Network::getStatus() { return node_stats_->getStatus(); } + +bool Network::pbft_syncing() { return pbft_syncing_state_->isPbftSyncing(); } + +uint64_t Network::syncTimeSeconds() const { + // TODO: this should be probably part of syncing_state, not node_stats + return node_stats_->syncTimeSeconds(); } +==== BASE ==== + +void Network::setSyncStatePeriod(PbftPeriod period) { pbft_syncing_state_->setSyncStatePeriod(period); } + +void Network::registerPeriodicEvents(const std::shared_ptr &pbft_mgr, + std::shared_ptr trx_mgr) { + auto getAllPeers = [this]() { + std::vector> all_peers; + for (auto &tarcap : tarcaps_) { + for (const auto &peer : tarcap.second->getPeersState()->getAllPeers()) { + all_peers.push_back(std::move(peer.second)); + } + } + + return all_peers; + }; + + uint64_t lambda_ms = pbft_mgr ? pbft_mgr->getPbftInitialLambda().count() : 2000; + + // Send new transactions + if (trx_mgr) { // because of tests + auto sendTxs = [this, trx_mgr = trx_mgr]() { + for (auto &tarcap : tarcaps_) { + auto tx_packet_handler = tarcap.second->getSpecificHandler(); + tx_packet_handler->periodicSendTransactions(trx_mgr->getAllPoolTrxs()); + } + }; + periodic_events_tp_.post_loop({kConf.network.transaction_interval_ms}, sendTxs); + } + + // Send status packet + auto sendStatus = [this]() { + for (auto &tarcap : tarcaps_) { + auto status_packet_handler = tarcap.second->getSpecificHandler(); + status_packet_handler->sendStatusToPeers(); + } + }; + const auto send_status_interval = 6 * lambda_ms; + periodic_events_tp_.post_loop({send_status_interval}, sendStatus); + + // Check nodes connections and refresh boot nodes + auto checkNodesConnections = [this]() { + // If node count drops to zero add boot nodes again and retry + if (host_->peer_count() == 0) { + addBootNodes(); + } + }; + periodic_events_tp_.post_loop({30000}, checkNodesConnections); + + // Ddos protection stats + auto ddosStats = [getAllPeers, ddos_protection = kConf.network.ddos_protection, + all_packets_stats = all_packets_stats_] { + const auto all_peers = getAllPeers(); + + // Log interval + max packets stats only if enabled in config + if (ddos_protection.log_packets_stats) { + all_packets_stats->processStats(all_peers); + } -bool Network::pbft_syncing() { - return std::ranges::any_of(tarcaps_, [](const auto &tarcap) { return tarcap.second->pbft_syncing(); }); + // Per peer packets stats are used for ddos protection + for (const auto &peer : all_peers) { + peer->resetPacketsStats(); + } + }; + periodic_events_tp_.post_loop( + {static_cast(kConf.network.ddos_protection.packets_stats_time_period_ms.count())}, ddosStats); + + // SUMMARY log + const auto node_stats_log_interval = 5 * 6 * lambda_ms; + auto summaryLog = [getAllPeers, node_stats = node_stats_, host = host_]() { + node_stats->logNodeStats(getAllPeers(), host->getNodeCount()); + }; + periodic_events_tp_.post_loop({node_stats_log_interval}, summaryLog); } +void Network::addBootNodes(bool initial) { + auto resolveHost = [](const std::string &addr, uint16_t port) { + static boost::asio::io_context s_resolverIoService; + boost::system::error_code ec; + bi::address address = bi::address::from_string(addr, ec); + bi::tcp::endpoint ep(bi::address(), port); + if (!ec) { + ep.address(address); + } else { + // resolve returns an iterator (host can resolve to multiple addresses) + bi::tcp::resolver r(s_resolverIoService); + auto it = r.resolve({bi::tcp::v4(), addr, toString(port)}, ec); + if (ec) { + return std::make_pair(false, bi::tcp::endpoint()); + } else { + ep = *it; + } + } + return std::make_pair(true, ep); + }; + + for (auto const &node : kConf.network.boot_nodes) { + dev::Public pub(node.id); + if (pub == pub_key_) { + LOG(log_wr_) << "not adding self to the boot node list"; + continue; + } + + if (host_->nodeTableHasNode(pub)) { + LOG(log_dg_) << "skipping node " << node.id << " already in table"; + continue; + } -void Network::setSyncStatePeriod(PbftPeriod period) { - for (auto &tarcap : tarcaps_) { - // TODO: double check this ??? - if (tarcap.second->pbft_syncing()) { - tarcap.second->setSyncStatePeriod(period); + auto ip = resolveHost(node.ip, node.port); + LOG(log_nf_) << "Adding boot node:" << node.ip << ":" << node.port << " " << ip.second.address().to_string(); + dev::p2p::Node boot_node(pub, dev::p2p::NodeIPEndpoint(ip.second.address(), node.port, node.port), + dev::p2p::PeerType::Required); + host_->addNode(boot_node); + if (!initial) { + host_->invalidateNode(boot_node.id); } } } diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.cpp index 3a61670b80..c7ac903dc1 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.cpp @@ -14,14 +14,13 @@ PbftSyncPacketHandler::PbftSyncPacketHandler(const FullNodeConfig &conf, std::sh std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, - std::shared_ptr vote_mgr, - std::shared_ptr periodic_events_tp, - std::shared_ptr db, const addr_t &node_addr) + std::shared_ptr vote_mgr, std::shared_ptr db, + const addr_t &node_addr) : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, "PBFT_SYNC_PH"), vote_mgr_(std::move(vote_mgr)), - periodic_events_tp_(periodic_events_tp) {} + periodic_events_tp_(1, true) {} void PbftSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { if (packet_data.rlp_.itemCount() != kStandardPacketSize && packet_data.rlp_.itemCount() != kChainSyncedPacketSize) { @@ -208,8 +207,7 @@ void PbftSyncPacketHandler::process(const threadpool::PacketData &packet_data, if (pbft_sync_period > pbft_chain_->getPbftChainSize() + (10 * kConf.network.sync_level_size)) { LOG(log_tr_) << "Syncing pbft blocks too fast than processing. Has synced period " << pbft_sync_period << ", PBFT chain size " << pbft_chain_->getPbftChainSize(); - if (auto periodic_events_tp = periodic_events_tp_.lock()) - periodic_events_tp->post(1000, [this] { delayedPbftSync(1); }); + periodic_events_tp_.post(1000, [this] { delayedPbftSync(1); }); } else { if (!syncPeerPbft(pbft_sync_period + 1, true)) { pbft_syncing_state_->setPbftSyncing(false); @@ -224,8 +222,7 @@ void PbftSyncPacketHandler::pbftSyncComplete() { if (pbft_mgr_->periodDataQueueSize()) { LOG(log_tr_) << "Syncing pbft blocks faster than processing. Remaining sync size " << pbft_mgr_->periodDataQueueSize(); - if (auto periodic_events_tp = periodic_events_tp_.lock()) - periodic_events_tp->post(1000, [this] { pbftSyncComplete(); }); + periodic_events_tp_.post(1000, [this] { pbftSyncComplete(); }); } else { LOG(log_dg_) << "Syncing PBFT is completed"; // We are pbft synced with the node we are connected to but @@ -254,8 +251,7 @@ void PbftSyncPacketHandler::delayedPbftSync(int counter) { if (pbft_sync_period > pbft_chain_->getPbftChainSize() + (10 * kConf.network.sync_level_size)) { LOG(log_tr_) << "Syncing pbft blocks faster than processing " << pbft_sync_period << " " << pbft_chain_->getPbftChainSize(); - if (auto periodic_events_tp = periodic_events_tp_.lock()) - periodic_events_tp->post(1000, [this, counter] { delayedPbftSync(counter + 1); }); + periodic_events_tp_.post(1000, [this, counter] { delayedPbftSync(counter + 1); }); } else { if (!syncPeerPbft(pbft_sync_period + 1)) { pbft_syncing_state_->setPbftSyncing(false); diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp index b217a74298..402de85d5a 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp @@ -19,9 +19,6 @@ void TaraxaCapability::registerPacketHandlers( const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, const std::shared_ptr &trx_mgr, const addr_t &node_addr) { - node_stats_ = std::make_shared(peers_state_, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, - vote_mgr, trx_mgr, packets_stats, thread_pool_, node_addr); - // Register all packet handlers // Consensus packets with high processing priority @@ -53,8 +50,7 @@ void TaraxaCapability::registerPacketHandlers( packets_handlers_->registerHandler( kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, vote_mgr, db, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, - pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, - vote_mgr, periodic_events_tp_, db, node_addr); + packets_handlers_->registerHandler( + kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, db, node_addr); } } // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/pbft_sync_packet_handler.cpp index 2490b35844..f5e1430489 100644 --- a/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/pbft_sync_packet_handler.cpp @@ -8,17 +8,19 @@ namespace taraxa::network::tarcap::v1 { -PbftSyncPacketHandler::PbftSyncPacketHandler( - const FullNodeConfig &conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, - std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr vote_mgr, - std::shared_ptr periodic_events_tp, std::shared_ptr db, const addr_t &node_addr) +PbftSyncPacketHandler::PbftSyncPacketHandler(const FullNodeConfig &conf, + std::shared_ptr peers_state, + std::shared_ptr packets_stats, + std::shared_ptr pbft_syncing_state, + std::shared_ptr pbft_chain, + std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, + std::shared_ptr vote_mgr, std::shared_ptr db, + const addr_t &node_addr) : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, "V1_PBFT_SYNC_PH"), vote_mgr_(std::move(vote_mgr)), - periodic_events_tp_(periodic_events_tp) {} + periodic_events_tp_(1, true) {} void PbftSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { if (packet_data.rlp_.itemCount() != kStandardPacketSize && packet_data.rlp_.itemCount() != kChainSyncedPacketSize) { @@ -229,8 +231,7 @@ void PbftSyncPacketHandler::process(const threadpool::PacketData &packet_data, if (pbft_sync_period > pbft_chain_->getPbftChainSize() + (10 * kConf.network.sync_level_size)) { LOG(log_tr_) << "Syncing pbft blocks too fast than processing. Has synced period " << pbft_sync_period << ", PBFT chain size " << pbft_chain_->getPbftChainSize(); - if (auto periodic_events_tp = periodic_events_tp_.lock()) - periodic_events_tp->post(1000, [this] { delayedPbftSync(1); }); + periodic_events_tp_.post(1000, [this] { delayedPbftSync(1); }); } else { if (!syncPeerPbft(pbft_sync_period + 1, true)) { return restartSyncingPbft(true); @@ -244,8 +245,7 @@ void PbftSyncPacketHandler::pbftSyncComplete() { if (pbft_mgr_->periodDataQueueSize()) { LOG(log_tr_) << "Syncing pbft blocks faster than processing. Remaining sync size " << pbft_mgr_->periodDataQueueSize(); - if (auto periodic_events_tp = periodic_events_tp_.lock()) - periodic_events_tp->post(1000, [this] { pbftSyncComplete(); }); + periodic_events_tp_.post(1000, [this] { pbftSyncComplete(); }); } else { LOG(log_dg_) << "Syncing PBFT is completed"; // We are pbft synced with the node we are connected to but @@ -273,8 +273,7 @@ void PbftSyncPacketHandler::delayedPbftSync(int counter) { if (pbft_sync_period > pbft_chain_->getPbftChainSize() + (10 * kConf.network.sync_level_size)) { LOG(log_tr_) << "Syncing pbft blocks faster than processing " << pbft_sync_period << " " << pbft_chain_->getPbftChainSize(); - if (auto periodic_events_tp = periodic_events_tp_.lock()) - periodic_events_tp->post(1000, [this, counter] { delayedPbftSync(counter + 1); }); + periodic_events_tp_.post(1000, [this, counter] { delayedPbftSync(counter + 1); }); } else { if (!syncPeerPbft(pbft_sync_period + 1)) { return restartSyncingPbft(true); diff --git a/libraries/core_libs/network/src/tarcap/capability_v1/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/capability_v1/taraxa_capability.cpp index b7d12b1ca5..e186658f8b 100644 --- a/libraries/core_libs/network/src/tarcap/capability_v1/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/capability_v1/taraxa_capability.cpp @@ -19,9 +19,6 @@ void TaraxaCapability::registerPacketHandlers( const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, const std::shared_ptr &trx_mgr, const addr_t &node_addr) { - node_stats_ = std::make_shared(peers_state_, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, - vote_mgr, trx_mgr, packets_stats, thread_pool_, node_addr); - // Register all packet handlers // Consensus packets with high processing priority @@ -54,7 +51,6 @@ void TaraxaCapability::registerPacketHandlers( kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, vote_mgr, db, node_addr); packets_handlers_->registerHandler( - kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, - periodic_events_tp_, db, node_addr); + kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, db, node_addr); } } // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp b/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp index 3a56cedbd0..ee07ec8198 100644 --- a/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp +++ b/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp @@ -4,8 +4,8 @@ #include "dag/dag_manager.hpp" #include "libp2p/Common.h" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" -#include "network/tarcap/shared_states/peers_state.hpp" #include "network/tarcap/stats/time_period_packets_stats.hpp" +#include "network/tarcap/taraxa_peer.hpp" #include "network/threadpool/tarcap_thread_pool.hpp" #include "pbft/pbft_chain.hpp" #include "pbft/pbft_manager.hpp" @@ -13,13 +13,12 @@ #include "vote_manager/vote_manager.hpp" namespace taraxa::network::tarcap { -NodeStats::NodeStats(std::shared_ptr peers_state, std::shared_ptr pbft_syncing_state, - std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, - std::shared_ptr dag_mgr, std::shared_ptr vote_mgr, - std::shared_ptr trx_mgr, std::shared_ptr packets_stats, +NodeStats::NodeStats(std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, + std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, + std::shared_ptr vote_mgr, std::shared_ptr trx_mgr, + std::shared_ptr packets_stats, std::shared_ptr thread_pool, const addr_t &node_addr) - : peers_state_(std::move(peers_state)), - pbft_syncing_state_(std::move(pbft_syncing_state)), + : pbft_syncing_state_(std::move(pbft_syncing_state)), pbft_chain_(std::move(pbft_chain)), pbft_mgr_(std::move(pbft_mgr)), dag_mgr_(std::move(dag_mgr)), @@ -32,7 +31,8 @@ NodeStats::NodeStats(std::shared_ptr peers_state, std::shared_ptr> &all_peers, + size_t nodes_count) { bool is_pbft_syncing = pbft_syncing_state_->isPbftSyncing(); dev::p2p::NodeID max_pbft_round_node_id; @@ -42,34 +42,30 @@ void NodeStats::logNodeStats() { uint64_t peer_max_pbft_chain_size = 1; uint64_t peer_max_node_dag_level = 1; - const size_t peers_size = peers_state_->getPeersCount(); + const size_t peers_size = all_peers.size(); std::string connected_peers_str{""}; - size_t number_of_discov_peers = 0; - if (const auto host = peers_state_->host_.lock()) { - number_of_discov_peers = host->getNodeCount(); - } - - for (auto const &peer : peers_state_->getAllPeers()) { + size_t number_of_discov_peers = nodes_count; + for (auto const &peer : all_peers) { // Find max pbft chain size - if (peer.second->pbft_chain_size_ > peer_max_pbft_chain_size) { - peer_max_pbft_chain_size = peer.second->pbft_chain_size_; - max_pbft_chain_node_id = peer.first; + if (peer->pbft_chain_size_ > peer_max_pbft_chain_size) { + peer_max_pbft_chain_size = peer->pbft_chain_size_; + max_pbft_chain_node_id = peer->getId(); } // Find max dag level - if (peer.second->dag_level_ > peer_max_node_dag_level) { - peer_max_node_dag_level = peer.second->dag_level_; - max_node_dag_level_node_id = peer.first; + if (peer->dag_level_ > peer_max_node_dag_level) { + peer_max_node_dag_level = peer->dag_level_; + max_node_dag_level_node_id = peer->getId(); } // Find max peer PBFT round - if (peer.second->pbft_round_ > peer_max_pbft_round) { - peer_max_pbft_round = peer.second->pbft_round_; - max_pbft_round_node_id = peer.first; + if (peer->pbft_round_ > peer_max_pbft_round) { + peer_max_pbft_round = peer->pbft_round_; + max_pbft_round_node_id = peer->getId(); } - connected_peers_str += peer.first.abridged() + " "; + connected_peers_str += peer->getId().abridged() + " "; } // Local dag info... @@ -176,12 +172,11 @@ void NodeStats::logNodeStats() { LOG(log_dg_) << "Non finalized dag blocks levels: " << non_finalized_blocks_levels; LOG(log_dg_) << "Non finalized dag blocks size: " << non_finalized_blocks_size; - // TODO: enable once periodic events moved to network class - // const auto [high_priority_queue_size, mid_priority_queue_size, low_priority_queue_size] = - // thread_pool_->getQueueSize(); - // LOG(log_dg_) << "High priority queue size: " << high_priority_queue_size; - // LOG(log_dg_) << "Mid priority queue size: " << mid_priority_queue_size; - // LOG(log_dg_) << "Low priority queue size: " << low_priority_queue_size; + const auto [high_priority_queue_size, mid_priority_queue_size, low_priority_queue_size] = + thread_pool_->getQueueSize(); + LOG(log_dg_) << "High priority queue size: " << high_priority_queue_size; + LOG(log_dg_) << "Mid priority queue size: " << mid_priority_queue_size; + LOG(log_dg_) << "Low priority queue size: " << low_priority_queue_size; LOG(log_nf_) << "------------- tl;dr -------------"; @@ -245,31 +240,33 @@ Json::Value NodeStats::getStatus() const { res["peers"] = Json::Value(Json::arrayValue); - for (auto const &peer : peers_state_->getAllPeers()) { - Json::Value peer_status; - peer_status["node_id"] = peer.first.toString(); - peer_status["dag_level"] = Json::UInt64(peer.second->dag_level_); - peer_status["pbft_size"] = Json::UInt64(peer.second->pbft_chain_size_); - peer_status["dag_synced"] = !peer.second->syncing_; - res["peers"].append(peer_status); - // Find max pbft chain size - if (peer.second->pbft_chain_size_ > peer_max_pbft_chain_size) { - peer_max_pbft_chain_size = peer.second->pbft_chain_size_; - max_pbft_chain_nodeID = peer.first; - } - - // Find max dag level - if (peer.second->dag_level_ > peer_max_node_dag_level) { - peer_max_node_dag_level = peer.second->dag_level_; - max_node_dag_level_nodeID = peer.first; - } - - // Find max peer PBFT round - if (peer.second->pbft_round_ > peer_max_pbft_round) { - peer_max_pbft_round = peer.second->pbft_round_; - max_pbft_round_nodeID = peer.first; - } - } + // TODO: uncomment this + // for (auto const &peer : peers_state_->getAllPeers()) { + // Json::Value peer_status; + // peer_status["node_id"] = peer.first.toString(); + // // TODO: add peer network version ! + // peer_status["dag_level"] = Json::UInt64(peer.second->dag_level_); + // peer_status["pbft_size"] = Json::UInt64(peer.second->pbft_chain_size_); + // peer_status["dag_synced"] = !peer.second->syncing_; + // res["peers"].append(peer_status); + // // Find max pbft chain size + // if (peer.second->pbft_chain_size_ > peer_max_pbft_chain_size) { + // peer_max_pbft_chain_size = peer.second->pbft_chain_size_; + // max_pbft_chain_nodeID = peer.first; + // } + // + // // Find max dag level + // if (peer.second->dag_level_ > peer_max_node_dag_level) { + // peer_max_node_dag_level = peer.second->dag_level_; + // max_node_dag_level_nodeID = peer.first; + // } + // + // // Find max peer PBFT round + // if (peer.second->pbft_round_ > peer_max_pbft_round) { + // peer_max_pbft_round = peer.second->pbft_round_; + // max_pbft_round_nodeID = peer.first; + // } + // } if (const auto syncing_peer = pbft_syncing_state_->syncingPeer(); syncing_peer && pbft_syncing_state_->isPbftSyncing()) { diff --git a/libraries/core_libs/network/src/tarcap/stats/time_period_packets_stats.cpp b/libraries/core_libs/network/src/tarcap/stats/time_period_packets_stats.cpp index a68e3699b1..d59a767285 100644 --- a/libraries/core_libs/network/src/tarcap/stats/time_period_packets_stats.cpp +++ b/libraries/core_libs/network/src/tarcap/stats/time_period_packets_stats.cpp @@ -38,7 +38,7 @@ std::pair TimePeriodPacketsStats::validMaxStats return {false, reset_time_period}; } -void TimePeriodPacketsStats::processStats(const std::shared_ptr& peers_state) { +void TimePeriodPacketsStats::processStats(const std::vector>& all_peers) { LOG(log_nf_) << "Received packets stats: " << jsonToUnstyledString(received_packets_stats_.getStatsJson()); LOG(log_nf_) << "Sent packets stats: " << jsonToUnstyledString(sent_packets_stats_.getStatsJson()); @@ -48,12 +48,12 @@ void TimePeriodPacketsStats::processStats(const std::shared_ptr& pee MaxStats peer_max_stats_per_time_window; - for (const auto& peer : peers_state->getAllPeers()) { - const auto [start_time, peer_packets_stats] = peer.second->getAllPacketsStatsCopy(); + for (const auto& peer : all_peers) { + const auto [start_time, peer_packets_stats] = peer->getAllPacketsStatsCopy(); // Check if processStats was called on expected time period so we can use current stats for max stats if (const auto valid_reset_period = validMaxStatsTimePeriod(start_time); !valid_reset_period.first) { - LOG(log_wr_) << "Cannot process stats from peer " << peer.first << " for \"max\" stats. Current reset period " + LOG(log_wr_) << "Cannot process stats from peer " << peer->getId() << " for \"max\" stats. Current reset period " << valid_reset_period.second.count() << ", expected reset period " << kResetTimePeriod.count(); continue; } diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp index 56d0e073e5..71b5660a6a 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp @@ -16,7 +16,6 @@ #include "network/tarcap/packets_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "network/tarcap/shared_states/test_state.hpp" -#include "network/tarcap/stats/node_stats.hpp" #include "network/tarcap/taraxa_peer.hpp" #include "node/node.hpp" #include "pbft/pbft_chain.hpp" @@ -28,26 +27,21 @@ namespace taraxa::network::tarcap { TaraxaCapabilityBase::TaraxaCapabilityBase(std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, TarcapVersion version, + std::shared_ptr packets_stats, + std::shared_ptr syncing_state, const std::string &log_channel) : test_state_(std::make_shared()), version_(version), + all_packets_stats_(std::move(packets_stats)), kConf(conf), peers_state_(nullptr), - pbft_syncing_state_(std::make_shared(conf.network.deep_syncing_threshold)), - node_stats_(nullptr), + pbft_syncing_state_(std::move(syncing_state)), packets_handlers_(std::make_shared()), - thread_pool_(nullptr), - periodic_events_tp_(std::make_shared(kPeriodicEventsThreadCount, false)), - pub_key_(key.pub()) { + thread_pool_(nullptr) { const auto &node_addr = key.address(); LOG_OBJECTS_CREATE(log_channel); peers_state_ = std::make_shared(host, kConf); - all_packets_stats_ = - std::make_shared(kConf.network.ddos_protection.packets_stats_time_period_ms, node_addr); - - // Inits boot nodes (based on config) - addBootNodes(true); } void TaraxaCapabilityBase::init(const h256 &genesis_hash, std::shared_ptr db, @@ -57,117 +51,6 @@ void TaraxaCapabilityBase::init(const h256 &genesis_hash, std::shared_ptrhost_.lock(); - if (!host) { - LOG(log_er_) << "Unable to obtain host in addBootNodes"; - return; - } - - for (auto const &node : kConf.network.boot_nodes) { - dev::Public pub(node.id); - if (pub == pub_key_) { - LOG(log_wr_) << "not adding self to the boot node list"; - continue; - } - - if (host->nodeTableHasNode(pub)) { - LOG(log_dg_) << "skipping node " << node.id << " already in table"; - continue; - } - - auto ip = resolveHost(node.ip, node.port); - LOG(log_nf_) << "Adding boot node:" << node.ip << ":" << node.port << " " << ip.second.address().to_string(); - dev::p2p::Node boot_node(pub, dev::p2p::NodeIPEndpoint(ip.second.address(), node.port, node.port), - dev::p2p::PeerType::Required); - host->addNode(boot_node); - if (!initial) { - host->invalidateNode(boot_node.id); - } - } -} - -void TaraxaCapabilityBase::initPeriodicEvents(const std::shared_ptr &pbft_mgr, - std::shared_ptr trx_mgr, - std::shared_ptr packets_stats) { - // TODO: refactor this: - // 1. Most of time is this single threaded thread pool doing nothing... - // 2. These periodic events are sending packets - that might be processed by main thread_pool ??? - // Creates periodic events - uint64_t lambda_ms = pbft_mgr ? pbft_mgr->getPbftInitialLambda().count() : 2000; - - // Send new txs periodic event - auto tx_packet_handler = packets_handlers_->getSpecificHandler(); - if (trx_mgr /* just because of tests */ && kConf.network.transaction_interval_ms > 0) { - periodic_events_tp_->post_loop({kConf.network.transaction_interval_ms}, - [tx_packet_handler = std::move(tx_packet_handler), trx_mgr = std::move(trx_mgr)] { - tx_packet_handler->periodicSendTransactions(trx_mgr->getAllPoolTrxs()); - }); - } - - // Send status periodic event - auto status_packet_handler = packets_handlers_->getSpecificHandler(); - const auto send_status_interval = 6 * lambda_ms; - periodic_events_tp_->post_loop({send_status_interval}, [status_packet_handler = std::move(status_packet_handler)] { - status_packet_handler->sendStatusToPeers(); - }); - - periodic_events_tp_->post_loop( - {static_cast(kConf.network.ddos_protection.packets_stats_time_period_ms.count())}, - [ddos_protection = kConf.network.ddos_protection, stats = packets_stats, peers_state = peers_state_] { - // Log interval + max packets stats only if enabled in config - if (ddos_protection.log_packets_stats) { - stats->processStats(peers_state); - } - - // Per peer packets stats are used for ddos protection - for (const auto &peer : peers_state->getAllPeers()) { - peer.second->resetPacketsStats(); - } - }); - - // SUMMARY log periodic event - const auto node_stats_log_interval = 5 * 6 * lambda_ms; - periodic_events_tp_->post_loop({node_stats_log_interval}, - [node_stats = node_stats_]() mutable { node_stats->logNodeStats(); }); - - // Every 30 seconds check if connected to another node and refresh boot nodes - periodic_events_tp_->post_loop({30000}, [this] { - auto host = peers_state_->host_.lock(); - if (!host) { - LOG(log_er_) << "Unable to obtain host in initPeriodicEvents"; - return; - } - // If node count drops to zero add boot nodes again and retry - if (host->peer_count() == 0) { - addBootNodes(); - } - }); } void TaraxaCapabilityBase::registerPacketHandlers( @@ -176,9 +59,6 @@ void TaraxaCapabilityBase::registerPacketHandlers( const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, const std::shared_ptr &trx_mgr, const addr_t &node_addr) { - node_stats_ = std::make_shared(peers_state_, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, - trx_mgr, packets_stats, thread_pool_, node_addr); - // Register all packet handlers // Consensus packets with high processing priority @@ -211,8 +91,7 @@ void TaraxaCapabilityBase::registerPacketHandlers( pbft_chain, vote_mgr, db, node_addr); packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_syncing_state_, - pbft_chain, pbft_mgr, dag_mgr, vote_mgr, - periodic_events_tp_, db, node_addr); + pbft_chain, pbft_mgr, dag_mgr, vote_mgr, db, node_addr); thread_pool_->setPacketsHandlers(version(), packets_handlers_); } @@ -372,18 +251,8 @@ void TaraxaCapabilityBase::setThreadPool(std::shared_ptr TaraxaCapabilityBase::getPacketsHandler() const { return packets_handlers_; } -void TaraxaCapabilityBase::start() { periodic_events_tp_->start(); } - -void TaraxaCapabilityBase::stop() { periodic_events_tp_->stop(); } - const std::shared_ptr &TaraxaCapabilityBase::getPeersState() { return peers_state_; } -const std::shared_ptr &TaraxaCapabilityBase::getNodeStats() { return node_stats_; } - -bool TaraxaCapabilityBase::pbft_syncing() const { return pbft_syncing_state_->isPbftSyncing(); } - -void TaraxaCapabilityBase::setSyncStatePeriod(PbftPeriod period) { pbft_syncing_state_->setSyncStatePeriod(period); } - // METHODS USED IN TESTS ONLY size_t TaraxaCapabilityBase::getReceivedBlocksCount() const { return test_state_->getBlocksSize(); } diff --git a/tests/p2p_test.cpp b/tests/p2p_test.cpp index 857e51c093..f57e7c8670 100644 --- a/tests/p2p_test.cpp +++ b/tests/p2p_test.cpp @@ -48,7 +48,8 @@ void setPendingPeersToReady(std::shared_ptr makeTarcap( std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, const h256 &genesis_hash, unsigned version, std::shared_ptr thread_pool = nullptr) { - auto tarcap = std::make_shared(host, key, conf, version, "TARCAP"); + auto tarcap = + std::make_shared(host, key, conf, version, nullptr, nullptr, "TARCAP"); tarcap->init(genesis_hash, {}, {}, {}, {}, {}, {}, key.address()); if (thread_pool) { @@ -135,8 +136,6 @@ TEST_F(P2PTest, capability_send_block) { util::ThreadPool tp; tp.post_loop({}, [=] { host1->do_work(); }); tp.post_loop({}, [=] { host2->do_work(); }); - thc1->start(); - thc2->start(); auto port1 = host1->listenPort(); auto port2 = host2->listenPort(); EXPECT_NE(port1, 0); @@ -216,7 +215,6 @@ TEST_F(P2PTest, block_propagate) { "Test", [&](auto host) { thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION, packets_tp1); - thc1->start(); return Host::CapabilityList{thc1}; }, KeyPair::create(), prefs1, taraxa_net_conf_1); @@ -233,7 +231,6 @@ TEST_F(P2PTest, block_propagate) { [&](auto host) { auto cap = vCapabilities.emplace_back( makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION, packets_tp)); - cap->start(); return Host::CapabilityList{cap}; }, KeyPair::create(), vPrefs[i])); From 6bfcc074c7fd3fa82b924b830ebdf21df8ae9147 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 3 May 2023 15:20:04 -0700 Subject: [PATCH 042/134] chore: fix tests after moving threadpool & periodic events from tarcap to network --- .../network/include/network/network.hpp | 7 +- .../network/tarcap/stats/node_stats.hpp | 4 +- .../network/tarcap/taraxa_capability_base.hpp | 16 +-- libraries/core_libs/network/src/network.cpp | 61 +++++--- .../network/src/tarcap/stats/node_stats.cpp | 56 ++++---- .../src/tarcap/taraxa_capability_base.cpp | 11 +- libraries/core_libs/node/src/node.cpp | 5 +- tests/network_test.cpp | 17 +-- tests/p2p_test.cpp | 135 ++++++------------ 9 files changed, 134 insertions(+), 178 deletions(-) diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index 6491f34c67..d99f97fdb0 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -33,11 +33,11 @@ class PacketHandler; class Network { public: Network(const FullNodeConfig &config, const h256 &genesis_hash = {}, - dev::p2p::Host::CapabilitiesFactory construct_capabilities = {}, - std::filesystem::path const &network_file_path = {}, dev::KeyPair const &key = dev::KeyPair::create(), + const std::filesystem::path &network_file_path = {}, const dev::KeyPair &key = dev::KeyPair::create(), std::shared_ptr db = {}, std::shared_ptr pbft_mgr = {}, std::shared_ptr pbft_chain = {}, std::shared_ptr vote_mgr = {}, - std::shared_ptr dag_mgr = {}, std::shared_ptr trx_mgr = {}); + std::shared_ptr dag_mgr = {}, std::shared_ptr trx_mgr = {}, + const std::vector &create_test_tarcaps = {}); ~Network(); Network(const Network &) = delete; @@ -65,7 +65,6 @@ class Network { void handleMaliciousSyncPeer(const dev::p2p::NodeID &id); std::shared_ptr getMaxChainPeer() const; - // METHODS USED IN TESTS ONLY template std::shared_ptr getSpecificHandler() const; diff --git a/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp b/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp index d017ce5cbe..1c8108cc10 100644 --- a/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp +++ b/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp @@ -3,6 +3,7 @@ #include "common/types.hpp" #include "json/value.h" #include "logger/logger.hpp" +#include "network/tarcap/tarcap_version.hpp" namespace taraxa { class PbftChain; @@ -32,7 +33,8 @@ class NodeStats { void logNodeStats(const std::vector>& all_peers, size_t nodes_count); uint64_t syncTimeSeconds() const; - Json::Value getStatus() const; + Json::Value getStatus( + std::map> peers) const; private: std::shared_ptr pbft_syncing_state_; diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp index 9152abcf69..ed049c3727 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp @@ -35,7 +35,8 @@ class TaraxaPeer; class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { public: TaraxaCapabilityBase(std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, - TarcapVersion version, std::shared_ptr packets_stats, + TarcapVersion version, std::shared_ptr threadpool, + std::shared_ptr packets_stats, std::shared_ptr syncing_state, const std::string &log_channel); virtual ~TaraxaCapabilityBase() = default; @@ -63,19 +64,6 @@ class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { template std::shared_ptr getSpecificHandler() const; - /** - * @brief Sets threadpool for packets processing - * @brief Sets threadpool for packets processing - * - * @param threadpool - */ - void setThreadPool(std::shared_ptr threadpool); - - /** - * @return packets handler - */ - std::shared_ptr getPacketsHandler() const; - // Interface required in network class to access packets handlers functionality // METHODS USED IN REAL CODE const std::shared_ptr &getPeersState(); diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index f31c198302..bcf7c7cd5c 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -23,12 +23,11 @@ namespace taraxa { -Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, - dev::p2p::Host::CapabilitiesFactory construct_capabilities, - std::filesystem::path const &network_file_path, dev::KeyPair const &key, std::shared_ptr db, - std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, - std::shared_ptr trx_mgr) +Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::filesystem::path const &network_file_path, + dev::KeyPair const &key, std::shared_ptr db, std::shared_ptr pbft_mgr, + std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, + std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, + const std::vector &create_test_tarcaps) : kConf(config), pub_key_(key.pub()), all_packets_stats_(nullptr), @@ -68,8 +67,11 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, taraxa_net_conf.expected_parallelism = tp_.capacity(); string net_version = "TaraxaNode"; // TODO maybe give a proper name? - if (!construct_capabilities) { - construct_capabilities = [&](std::weak_ptr host) { + dev::p2p::Host::CapabilitiesFactory constructCapabilities; + + // Create real taraxa capabilities + if (create_test_tarcaps.empty()) { + constructCapabilities = [&](std::weak_ptr host) { assert(!host.expired()); const size_t kOldNetworkVersion = 1; @@ -79,27 +81,39 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, // Register old version of taraxa capability auto v1_tarcap = std::make_shared( - host, key, config, kOldNetworkVersion, all_packets_stats_, pbft_syncing_state_, "V1_TARCAP"); + host, key, config, kOldNetworkVersion, packets_tp_, all_packets_stats_, pbft_syncing_state_, "V1_TARCAP"); v1_tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); capabilities.emplace_back(v1_tarcap); // Register new version of taraxa capability auto v2_tarcap = std::make_shared( - host, key, config, TARAXA_NET_VERSION, all_packets_stats_, pbft_syncing_state_, "TARCAP"); + host, key, config, TARAXA_NET_VERSION, packets_tp_, all_packets_stats_, pbft_syncing_state_, "TARCAP"); v2_tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); capabilities.emplace_back(v2_tarcap); + return capabilities; + }; + } else { // Create test taraxa capabilities + constructCapabilities = [&](std::weak_ptr host) { + assert(!host.expired()); + + dev::p2p::Host::CapabilityList capabilities; + for (const auto test_tarcap_version : create_test_tarcaps) { + auto tarcap = std::make_shared( + host, key, config, test_tarcap_version, packets_tp_, all_packets_stats_, pbft_syncing_state_, + "V" + std::to_string(test_tarcap_version) + "_TARCAP"); + tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); + capabilities.emplace_back(tarcap); + } + return capabilities; }; } - host_ = dev::p2p::Host::make(net_version, construct_capabilities, key, net_conf, taraxa_net_conf, network_file_path); + host_ = dev::p2p::Host::make(net_version, constructCapabilities, key, net_conf, taraxa_net_conf, network_file_path); for (const auto &cap : host_->getSupportedCapabilities()) { const auto tarcap_version = cap.second.ref->version(); auto tarcap = std::static_pointer_cast(cap.second.ref); - packets_tp_->setPacketsHandlers(tarcap_version, tarcap->getPacketsHandler()); - - tarcap->setThreadPool(packets_tp_); tarcaps_[tarcap_version] = std::move(tarcap); } @@ -141,7 +155,16 @@ size_t Network::getPeerCount() { return host_->peer_count(); } unsigned Network::getNodeCount() { return host_->getNodeCount(); } -Json::Value Network::getStatus() { return node_stats_->getStatus(); } +Json::Value Network::getStatus() { + std::map> peers; + for (auto &tarcap : tarcaps_) { + for (const auto &peer : tarcap.second->getPeersState()->getAllPeers()) { + peers.emplace(tarcap.second->version(), std::move(peer.second)); + } + } + + return node_stats_->getStatus(peers); +} bool Network::pbft_syncing() { return pbft_syncing_state_->isPbftSyncing(); } @@ -276,20 +299,20 @@ void Network::gossipDagBlock(const DagBlock &block, bool proposed, const SharedT } void Network::gossipVote(const std::shared_ptr &vote, const std::shared_ptr &block, bool rebroadcast) { - for (const auto &tarcap : tarcaps_ | std::views::reverse) { + for (const auto &tarcap : tarcaps_) { tarcap.second->getSpecificHandler()->onNewPbftVote(vote, block, rebroadcast); } } void Network::gossipVotesBundle(const std::vector> &votes, bool rebroadcast) { - for (const auto &tarcap : tarcaps_ | std::views::reverse) { + for (const auto &tarcap : tarcaps_) { tarcap.second->getSpecificHandler()->onNewPbftVotesBundle(votes, rebroadcast); } } void Network::handleMaliciousSyncPeer(const dev::p2p::NodeID &node_id) { - for (const auto &tarcap : tarcaps_ | std::views::reverse) { + for (const auto &tarcap : tarcaps_) { // Peer is present only in one taraxa capability depending on his network version if (auto peer = tarcap.second->getPeersState()->getPeer(node_id); !peer) { continue; @@ -302,7 +325,7 @@ void Network::handleMaliciousSyncPeer(const dev::p2p::NodeID &node_id) { std::shared_ptr Network::getMaxChainPeer() const { std::shared_ptr max_chain_peer{nullptr}; - for (const auto &tarcap : tarcaps_ | std::views::reverse) { + for (const auto &tarcap : tarcaps_) { const auto peer = tarcap.second->getSpecificHandler<::taraxa::network::tarcap::PbftSyncPacketHandler>()->getMaxChainPeer(); if (!peer) { diff --git a/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp b/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp index ee07ec8198..e7781c52c1 100644 --- a/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp +++ b/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp @@ -229,7 +229,8 @@ void NodeStats::logNodeStats(const std::vector> peers) const { Json::Value res; dev::p2p::NodeID max_pbft_round_nodeID; dev::p2p::NodeID max_pbft_chain_nodeID; @@ -240,33 +241,32 @@ Json::Value NodeStats::getStatus() const { res["peers"] = Json::Value(Json::arrayValue); - // TODO: uncomment this - // for (auto const &peer : peers_state_->getAllPeers()) { - // Json::Value peer_status; - // peer_status["node_id"] = peer.first.toString(); - // // TODO: add peer network version ! - // peer_status["dag_level"] = Json::UInt64(peer.second->dag_level_); - // peer_status["pbft_size"] = Json::UInt64(peer.second->pbft_chain_size_); - // peer_status["dag_synced"] = !peer.second->syncing_; - // res["peers"].append(peer_status); - // // Find max pbft chain size - // if (peer.second->pbft_chain_size_ > peer_max_pbft_chain_size) { - // peer_max_pbft_chain_size = peer.second->pbft_chain_size_; - // max_pbft_chain_nodeID = peer.first; - // } - // - // // Find max dag level - // if (peer.second->dag_level_ > peer_max_node_dag_level) { - // peer_max_node_dag_level = peer.second->dag_level_; - // max_node_dag_level_nodeID = peer.first; - // } - // - // // Find max peer PBFT round - // if (peer.second->pbft_round_ > peer_max_pbft_round) { - // peer_max_pbft_round = peer.second->pbft_round_; - // max_pbft_round_nodeID = peer.first; - // } - // } + for (auto const &peer : peers) { + Json::Value peer_status; + peer_status["node_id"] = peer.second->getId().toString(); + peer_status["net_version"] = Json::UInt64(peer.first); + peer_status["dag_level"] = Json::UInt64(peer.second->dag_level_); + peer_status["pbft_size"] = Json::UInt64(peer.second->pbft_chain_size_); + peer_status["dag_synced"] = !peer.second->syncing_; + res["peers"].append(peer_status); + // Find max pbft chain size + if (peer.second->pbft_chain_size_ > peer_max_pbft_chain_size) { + peer_max_pbft_chain_size = peer.second->pbft_chain_size_; + max_pbft_chain_nodeID = peer.second->getId(); + } + + // Find max dag level + if (peer.second->dag_level_ > peer_max_node_dag_level) { + peer_max_node_dag_level = peer.second->dag_level_; + max_node_dag_level_nodeID = peer.second->getId(); + } + + // Find max peer PBFT round + if (peer.second->pbft_round_ > peer_max_pbft_round) { + peer_max_pbft_round = peer.second->pbft_round_; + max_pbft_round_nodeID = peer.second->getId(); + } + } if (const auto syncing_peer = pbft_syncing_state_->syncingPeer(); syncing_peer && pbft_syncing_state_->isPbftSyncing()) { diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp index 71b5660a6a..f7d1beadee 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp @@ -27,6 +27,7 @@ namespace taraxa::network::tarcap { TaraxaCapabilityBase::TaraxaCapabilityBase(std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, TarcapVersion version, + std::shared_ptr threadpool, std::shared_ptr packets_stats, std::shared_ptr syncing_state, const std::string &log_channel) @@ -37,7 +38,7 @@ TaraxaCapabilityBase::TaraxaCapabilityBase(std::weak_ptr host, c peers_state_(nullptr), pbft_syncing_state_(std::move(syncing_state)), packets_handlers_(std::make_shared()), - thread_pool_(nullptr) { + thread_pool_(std::move(threadpool)) { const auto &node_addr = key.address(); LOG_OBJECTS_CREATE(log_channel); @@ -51,6 +52,9 @@ void TaraxaCapabilityBase::init(const h256 &genesis_hash, std::shared_ptrsetPacketsHandlers(version(), packets_handlers_); } void TaraxaCapabilityBase::registerPacketHandlers( @@ -246,11 +250,6 @@ inline bool TaraxaCapabilityBase::filterSyncIrrelevantPackets(SubprotocolPacketT } } -void TaraxaCapabilityBase::setThreadPool(std::shared_ptr threadpool) { - thread_pool_ = std::move(threadpool); -} -std::shared_ptr TaraxaCapabilityBase::getPacketsHandler() const { return packets_handlers_; } - const std::shared_ptr &TaraxaCapabilityBase::getPeersState() { return peers_state_; } // METHODS USED IN TESTS ONLY diff --git a/libraries/core_libs/node/src/node.cpp b/libraries/core_libs/node/src/node.cpp index 33d89035a6..6ceaeac46f 100644 --- a/libraries/core_libs/node/src/node.cpp +++ b/libraries/core_libs/node/src/node.cpp @@ -131,9 +131,8 @@ void FullNode::init() { dag_block_proposer_ = std::make_shared( 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); - network_ = std::make_shared(conf_, genesis_hash, dev::p2p::Host::CapabilitiesFactory(), - conf_.net_file_path().string(), kp_, db_, pbft_mgr_, pbft_chain_, vote_mgr_, - dag_mgr_, trx_mgr_); + network_ = std::make_shared(conf_, genesis_hash, conf_.net_file_path().string(), kp_, db_, pbft_mgr_, + pbft_chain_, vote_mgr_, dag_mgr_, trx_mgr_); } void FullNode::setupMetricsUpdaters() { diff --git a/tests/network_test.cpp b/tests/network_test.cpp index 11b4d6b01c..2f90f1327c 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -345,7 +345,7 @@ TEST_F(NetworkTest, transfer_transaction) { } // Test verifies saving network to a file and restoring it from a file -// is successfull. Once restored from the file it is able to reestablish +// is successful. Once restored from the file it is able to reestablish // connections even with boot nodes down TEST_F(NetworkTest, save_network) { std::filesystem::remove_all("/tmp/nw2"); @@ -354,12 +354,9 @@ TEST_F(NetworkTest, save_network) { auto key3 = dev::KeyPair::create(); h256 genesis_hash; { - std::shared_ptr nw1 = - std::make_shared(node_cfgs[0], genesis_hash, Host::CapabilitiesFactory()); - std::shared_ptr nw2 = - std::make_shared(node_cfgs[1], genesis_hash, Host::CapabilitiesFactory(), "/tmp/nw2", key2); - std::shared_ptr nw3 = - std::make_shared(node_cfgs[2], genesis_hash, Host::CapabilitiesFactory(), "/tmp/nw3", key3); + std::shared_ptr nw1 = std::make_shared(node_cfgs[0], genesis_hash); + std::shared_ptr nw2 = std::make_shared(node_cfgs[1], genesis_hash, "/tmp/nw2", key2); + std::shared_ptr nw3 = std::make_shared(node_cfgs[2], genesis_hash, "/tmp/nw3", key3); nw1->start(); nw2->start(); @@ -375,10 +372,8 @@ TEST_F(NetworkTest, save_network) { }); } - std::shared_ptr nw2 = - std::make_shared(node_cfgs[1], genesis_hash, Host::CapabilitiesFactory(), "/tmp/nw2", key2); - std::shared_ptr nw3 = - std::make_shared(node_cfgs[2], genesis_hash, Host::CapabilitiesFactory(), "/tmp/nw3", key3); + std::shared_ptr nw2 = std::make_shared(node_cfgs[1], genesis_hash, "/tmp/nw2", key2); + std::shared_ptr nw3 = std::make_shared(node_cfgs[2], genesis_hash, "/tmp/nw3", key3); nw2->start(); nw3->start(); diff --git a/tests/p2p_test.cpp b/tests/p2p_test.cpp index f57e7c8670..3ef061eb96 100644 --- a/tests/p2p_test.cpp +++ b/tests/p2p_test.cpp @@ -16,6 +16,7 @@ #include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" #include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" #include "network/tarcap/capability_latest/taraxa_capability.hpp" +#include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "test_util/samples.hpp" #include "test_util/test_util.hpp" @@ -45,17 +46,20 @@ void setPendingPeersToReady(std::shared_ptr makeTarcap( - std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, const h256 &genesis_hash, - unsigned version, std::shared_ptr thread_pool = nullptr) { - auto tarcap = - std::make_shared(host, key, conf, version, nullptr, nullptr, "TARCAP"); +std::shared_ptr makeTarcap(std::weak_ptr host, + const dev::KeyPair &key, + const FullNodeConfig &conf, + const h256 &genesis_hash, unsigned version) { + auto thread_pool = std::make_shared(conf.network.packets_processing_threads); + auto packets_stats = std::make_shared( + conf.network.ddos_protection.packets_stats_time_period_ms, Address{}); + auto syncing_state = std::make_shared(conf.network.deep_syncing_threshold); + + auto tarcap = std::make_shared(host, key, conf, version, thread_pool, + packets_stats, syncing_state, "TARCAP"); tarcap->init(genesis_hash, {}, {}, {}, {}, {}, {}, key.address()); - if (thread_pool) { - thread_pool->setPacketsHandlers(version, tarcap->getPacketsHandler()); - tarcap->setThreadPool(thread_pool); - } + thread_pool->startProcessing(); return tarcap; } @@ -111,28 +115,22 @@ TEST_F(P2PTest, capability_send_block) { conf.network.transaction_interval_ms = 1000; h256 genesis; - auto packets_tp1 = std::make_shared(conf.network.packets_processing_threads); - auto packets_tp2 = std::make_shared(conf.network.packets_processing_threads); - std::shared_ptr thc1, thc2; auto host1 = Host::make( "Test", [&](auto host) { - thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION, packets_tp1); + thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); return Host::CapabilityList{thc1}; }, KeyPair::create(), prefs1); auto host2 = Host::make( "Test", [&](auto host) { - thc2 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION, packets_tp2); + thc2 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); return Host::CapabilityList{thc2}; }, KeyPair::create(), prefs2); - packets_tp1->startProcessing(); - packets_tp2->startProcessing(); - util::ThreadPool tp; tp.post_loop({}, [=] { host1->do_work(); }); tp.post_loop({}, [=] { host2->do_work(); }); @@ -210,31 +208,26 @@ TEST_F(P2PTest, block_propagate) { conf.network.transaction_interval_ms = 1000; h256 genesis; std::shared_ptr thc1; - auto packets_tp1 = std::make_shared(conf.network.packets_processing_threads); auto host1 = Host::make( "Test", [&](auto host) { - thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION, packets_tp1); + thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); return Host::CapabilityList{thc1}; }, KeyPair::create(), prefs1, taraxa_net_conf_1); - packets_tp1->startProcessing(); util::ThreadPool tp; tp.post_loop({}, [=] { host1->do_work(); }); std::vector> vHosts; std::vector> vCapabilities; for (int i = 0; i < nodeCount; i++) { - auto packets_tp = std::make_shared(conf.network.packets_processing_threads); auto host = vHosts.emplace_back(Host::make( "Test", [&](auto host) { - auto cap = vCapabilities.emplace_back( - makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION, packets_tp)); + auto cap = vCapabilities.emplace_back(makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION)); return Host::CapabilityList{cap}; }, KeyPair::create(), vPrefs[i])); - packets_tp->startProcessing(); tp.post_loop({}, [=] { host->do_work(); }); } @@ -344,92 +337,50 @@ TEST_F(P2PTest, multiple_capabilities) { const auto kp2 = KeyPair::create(); cleanup(); { - auto nw1 = std::make_shared( - node_cfgs[0], genesis_hash, - [kp1, &node_cfgs, &genesis_hash](auto host) { - auto cap = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 3); - return Host::CapabilityList{cap}; - }, - "/tmp/nw2"); - auto nw2 = std::make_shared( - node_cfgs[1], genesis_hash, - [kp2, &node_cfgs, &genesis_hash](auto host) { - auto cap = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 3); - return Host::CapabilityList{cap}; - }, - "/tmp/nw3"); + auto nw1 = + std::make_shared(node_cfgs[0], genesis_hash, "/tmp/nw2", kp1, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, std::vector{3}); + auto nw2 = + std::make_shared(node_cfgs[1], genesis_hash, "/tmp/nw3", kp2, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, std::vector{3}); + nw1->start(); nw2->start(); wait_for_connection(nw1, nw2); } cleanup(); { - auto nw1 = std::make_shared( - node_cfgs[0], genesis_hash, - [kp1, &node_cfgs, &genesis_hash](auto host) { - auto cap1 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 1); - auto cap2 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 2); - auto cap3 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 3); - return Host::CapabilityList{cap1, cap2, cap3}; - }, - "/tmp/nw2"); - auto nw2 = std::make_shared( - node_cfgs[1], genesis_hash, - [kp2, &node_cfgs, &genesis_hash](auto host) { - auto cap1 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 1); - auto cap2 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 2); - auto cap3 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 3); - return Host::CapabilityList{cap1, cap2, cap3}; - }, - "/tmp/nw3"); + auto nw1 = std::make_shared(node_cfgs[0], genesis_hash, "/tmp/nw2", kp1, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, + std::vector{1, 2, 3}); + auto nw2 = std::make_shared(node_cfgs[1], genesis_hash, "/tmp/nw3", kp2, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, + std::vector{1, 2, 3}); nw1->start(); nw2->start(); wait_for_connection(nw1, nw2); } cleanup(); { - auto nw1 = std::make_shared( - node_cfgs[0], genesis_hash, - [kp1, &node_cfgs, &genesis_hash](auto host) { - auto cap1 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 1); - auto cap2 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 2); - auto cap3 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 3); - return Host::CapabilityList{cap1, cap2, cap3}; - }, - "/tmp/nw2"); - auto nw2 = std::make_shared( - node_cfgs[1], genesis_hash, - [kp2, &node_cfgs, &genesis_hash](auto host) { - auto cap2 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 2); - auto cap3 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 3); - auto cap4 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 4); - return Host::CapabilityList{cap2, cap3, cap4}; - }, - "/tmp/nw3"); + auto nw1 = std::make_shared(node_cfgs[0], genesis_hash, "/tmp/nw2", kp1, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, + std::vector{1, 2, 3}); + auto nw2 = std::make_shared(node_cfgs[1], genesis_hash, "/tmp/nw3", kp2, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, + std::vector{2, 3, 4}); nw1->start(); nw2->start(); wait_for_connection(nw1, nw2); } cleanup(); { - auto nw1 = std::make_shared( - node_cfgs[0], genesis_hash, - [kp1, &node_cfgs, &genesis_hash](auto host) { - auto cap1 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 1); - auto cap2 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 2); - auto cap3 = makeTarcap(host, kp1, node_cfgs[0], genesis_hash, 3); - return Host::CapabilityList{cap1, cap2, cap3}; - }, - "/tmp/nw2"); - auto nw2 = std::make_shared( - node_cfgs[1], genesis_hash, - [kp2, &node_cfgs, &genesis_hash](auto host) { - auto cap4 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 4); - auto cap5 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 5); - auto cap6 = makeTarcap(host, kp2, node_cfgs[1], genesis_hash, 6); - return Host::CapabilityList{cap4, cap5, cap6}; - }, - "/tmp/nw3"); + auto nw1 = std::make_shared(node_cfgs[0], genesis_hash, "/tmp/nw2", kp1, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, + std::vector{1, 2, 3}); + auto nw2 = std::make_shared(node_cfgs[1], genesis_hash, "/tmp/nw3", kp2, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, + std::vector{4, 5, 6}); + nw1->start(); nw2->start(); From 3db954c88cdd201856347b70bdc683f6c48f9997 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Thu, 4 May 2023 11:08:23 -0700 Subject: [PATCH 043/134] save tarcaps in descending order --- .../core_libs/network/include/network/network.hpp | 8 +++++--- libraries/core_libs/network/src/network.cpp | 10 +++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index d99f97fdb0..358f08aedb 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -112,8 +112,10 @@ class Network { util::ThreadPool tp_; std::shared_ptr host_; - // All supported taraxa capabilities - std::map> tarcaps_; + // All supported taraxa capabilities - in descending order + std::map, + std::greater> + tarcaps_; // Threadpool for packets std::shared_ptr packets_tp_; @@ -127,7 +129,7 @@ class Network { template std::shared_ptr Network::getSpecificHandler() const { - return tarcaps_.rbegin()->second->getSpecificHandler(); + return tarcaps_.begin()->second->getSpecificHandler(); } } // namespace taraxa diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index bcf7c7cd5c..45026d47ad 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -292,7 +292,7 @@ void Network::addBootNodes(bool initial) { } void Network::gossipDagBlock(const DagBlock &block, bool proposed, const SharedTransactions &trxs) { - for (const auto &tarcap : tarcaps_ | std::views::reverse) { + for (const auto &tarcap : tarcaps_) { tarcap.second->getSpecificHandler()->onNewBlockVerified(block, proposed, trxs); } @@ -347,7 +347,7 @@ std::shared_ptr Network::getMaxChainPeer() const { // other functions must use all tarcaps void Network::setPendingPeersToReady() { - const auto &peers_state = tarcaps_.rbegin()->second->getPeersState(); + const auto &peers_state = tarcaps_.begin()->second->getPeersState(); auto peerIds = peers_state->getAllPendingPeersIDs(); for (const auto &peerId : peerIds) { @@ -360,12 +360,12 @@ void Network::setPendingPeersToReady() { dev::p2p::NodeID Network::getNodeId() const { return host_->id(); } -int Network::getReceivedBlocksCount() const { return tarcaps_.rbegin()->second->getReceivedBlocksCount(); } +int Network::getReceivedBlocksCount() const { return tarcaps_.begin()->second->getReceivedBlocksCount(); } -int Network::getReceivedTransactionsCount() const { return tarcaps_.rbegin()->second->getReceivedTransactionsCount(); } +int Network::getReceivedTransactionsCount() const { return tarcaps_.begin()->second->getReceivedTransactionsCount(); } std::shared_ptr Network::getPeer(dev::p2p::NodeID const &id) const { - return tarcaps_.rbegin()->second->getPeersState()->getPeer(id); + return tarcaps_.begin()->second->getPeersState()->getPeer(id); } // METHODS USED IN TESTS ONLY From 5095ad47a89d9ffe821484237b1e68bc796c4fac Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Fri, 5 May 2023 13:09:36 -0700 Subject: [PATCH 044/134] use lambda function for registering packets handlers instead of class inheritance --- .../consensus/include/pbft/pbft_manager.hpp | 1 - libraries/core_libs/consensus/src/dag/dag.cpp | 2 +- .../consensus/src/dag/dag_manager.cpp | 2 +- .../consensus/src/pbft/pbft_manager.cpp | 6 +- .../src/vote_manager/vote_manager.cpp | 4 +- .../network/graphql/src/sync_state.cpp | 2 +- .../network/include/network/network.hpp | 9 +- .../capability_latest/taraxa_capability.hpp | 31 ---- .../capability_v1/taraxa_capability.hpp | 29 --- .../network/tarcap/packets_handler.hpp | 2 +- .../latest}/common/exceptions.hpp | 7 +- .../common/ext_syncing_packet_handler.hpp | 4 +- .../common/ext_votes_packet_handler.hpp | 0 .../latest}/common/packet_handler.hpp | 21 ++- .../latest}/dag_block_packet_handler.hpp | 0 .../latest}/dag_sync_packet_handler.hpp | 0 .../latest}/get_dag_sync_packet_handler.hpp | 0 .../get_next_votes_sync_packet_handler.hpp | 0 .../latest}/get_pbft_sync_packet_handler.hpp | 0 .../latest}/pbft_sync_packet_handler.hpp | 2 +- .../latest}/status_packet_handler.hpp | 0 .../latest}/transaction_packet_handler.hpp | 6 +- .../latest}/vote_packet_handler.hpp | 0 .../latest}/votes_bundle_packet_handler.hpp | 0 .../v1}/common/ext_votes_packet_handler.hpp | 4 +- .../get_next_votes_sync_packet_handler.hpp | 0 .../v1}/get_pbft_sync_packet_handler.hpp | 2 +- .../v1/init_packets_handlers.hpp | 67 +++++++ .../v1}/pbft_sync_packet_handler.hpp | 4 +- .../v1}/votes_bundle_packet_handler.hpp | 0 .../network/include/network/tarcap/readme.md | 19 -- .../network/tarcap/taraxa_capability.hpp | 128 ++++++++++++++ .../network/tarcap/taraxa_capability_base.hpp | 124 ------------- libraries/core_libs/network/src/network.cpp | 40 ++--- .../capability_latest/taraxa_capability.cpp | 56 ------ .../capability_v1/taraxa_capability.cpp | 56 ------ .../common/ext_syncing_packet_handler.cpp | 3 +- .../common/ext_votes_packet_handler.cpp | 2 +- .../latest}/common/packet_handler.cpp | 2 +- .../latest}/dag_block_packet_handler.cpp | 4 +- .../latest}/dag_sync_packet_handler.cpp | 4 +- .../latest}/get_dag_sync_packet_handler.cpp | 2 +- .../get_next_votes_sync_packet_handler.cpp | 2 +- .../latest}/get_pbft_sync_packet_handler.cpp | 2 +- .../latest}/pbft_sync_packet_handler.cpp | 2 +- .../latest}/status_packet_handler.cpp | 4 +- .../latest}/transaction_packet_handler.cpp | 2 +- .../latest}/vote_packet_handler.cpp | 2 +- .../latest}/votes_bundle_packet_handler.cpp | 2 +- .../v1}/common/ext_votes_packet_handler.cpp | 2 +- .../get_next_votes_sync_packet_handler.cpp | 2 +- .../v1}/get_pbft_sync_packet_handler.cpp | 2 +- .../v1}/pbft_sync_packet_handler.cpp | 2 +- .../v1}/votes_bundle_packet_handler.cpp | 2 +- ...ability_base.cpp => taraxa_capability.cpp} | 167 +++++++++--------- tests/network_test.cpp | 14 +- tests/p2p_test.cpp | 31 ++-- tests/pbft_manager_test.cpp | 2 +- tests/vote_test.cpp | 2 +- 59 files changed, 376 insertions(+), 511 deletions(-) delete mode 100644 libraries/core_libs/network/include/network/tarcap/capability_latest/taraxa_capability.hpp delete mode 100644 libraries/core_libs/network/include/network/tarcap/capability_v1/taraxa_capability.hpp rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/common/exceptions.hpp (81%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/common/ext_syncing_packet_handler.hpp (94%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/common/ext_votes_packet_handler.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/common/packet_handler.hpp (79%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/dag_block_packet_handler.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/dag_sync_packet_handler.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/get_dag_sync_packet_handler.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/get_next_votes_sync_packet_handler.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/get_pbft_sync_packet_handler.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/pbft_sync_packet_handler.hpp (94%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/status_packet_handler.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/transaction_packet_handler.hpp (90%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/vote_packet_handler.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/votes_bundle_packet_handler.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{capability_v1/packets_handlers => packets_handlers/v1}/common/ext_votes_packet_handler.hpp (89%) rename libraries/core_libs/network/include/network/tarcap/{capability_v1/packets_handlers => packets_handlers/v1}/get_next_votes_sync_packet_handler.hpp (100%) rename libraries/core_libs/network/include/network/tarcap/{capability_v1/packets_handlers => packets_handlers/v1}/get_pbft_sync_packet_handler.hpp (94%) create mode 100644 libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp rename libraries/core_libs/network/include/network/tarcap/{capability_v1/packets_handlers => packets_handlers/v1}/pbft_sync_packet_handler.hpp (89%) rename libraries/core_libs/network/include/network/tarcap/{capability_v1/packets_handlers => packets_handlers/v1}/votes_bundle_packet_handler.hpp (100%) delete mode 100644 libraries/core_libs/network/include/network/tarcap/readme.md create mode 100644 libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp delete mode 100644 libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp delete mode 100644 libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp delete mode 100644 libraries/core_libs/network/src/tarcap/capability_v1/taraxa_capability.cpp rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/common/ext_syncing_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/common/ext_votes_packet_handler.cpp (99%) rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/common/packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/dag_block_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/dag_sync_packet_handler.cpp (97%) rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/get_dag_sync_packet_handler.cpp (97%) rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/get_next_votes_sync_packet_handler.cpp (97%) rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/get_pbft_sync_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/pbft_sync_packet_handler.cpp (99%) rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/status_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/transaction_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/vote_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{capability_latest/packets_handlers => packets_handlers/latest}/votes_bundle_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{capability_v1/packet_handlers => packets_handlers/v1}/common/ext_votes_packet_handler.cpp (95%) rename libraries/core_libs/network/src/tarcap/{capability_v1/packet_handlers => packets_handlers/v1}/get_next_votes_sync_packet_handler.cpp (97%) rename libraries/core_libs/network/src/tarcap/{capability_v1/packet_handlers => packets_handlers/v1}/get_pbft_sync_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{capability_v1/packet_handlers => packets_handlers/v1}/pbft_sync_packet_handler.cpp (99%) rename libraries/core_libs/network/src/tarcap/{capability_v1/packet_handlers => packets_handlers/v1}/votes_bundle_packet_handler.cpp (98%) rename libraries/core_libs/network/src/tarcap/{taraxa_capability_base.cpp => taraxa_capability.cpp} (53%) diff --git a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp index dbb0487f49..8924a25e67 100644 --- a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp +++ b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp @@ -8,7 +8,6 @@ #include "final_chain/final_chain.hpp" #include "logger/logger.hpp" #include "network/network.hpp" -#include "network/tarcap/capability_latest/taraxa_capability.hpp" #include "pbft/period_data_queue.hpp" #include "pbft/proposed_blocks.hpp" diff --git a/libraries/core_libs/consensus/src/dag/dag.cpp b/libraries/core_libs/consensus/src/dag/dag.cpp index 498238dbb8..227aeda47f 100644 --- a/libraries/core_libs/consensus/src/dag/dag.cpp +++ b/libraries/core_libs/consensus/src/dag/dag.cpp @@ -14,7 +14,7 @@ #include "dag/dag.hpp" #include "key_manager/key_manager.hpp" #include "network/network.hpp" -#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp" #include "transaction/transaction_manager.hpp" namespace taraxa { diff --git a/libraries/core_libs/consensus/src/dag/dag_manager.cpp b/libraries/core_libs/consensus/src/dag/dag_manager.cpp index 707aaaffbd..0ef42f1c73 100644 --- a/libraries/core_libs/consensus/src/dag/dag_manager.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_manager.cpp @@ -14,7 +14,7 @@ #include "dag/dag.hpp" #include "key_manager/key_manager.hpp" #include "network/network.hpp" -#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp" #include "transaction/transaction_manager.hpp" namespace taraxa { diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index d8ebc32d51..c9c55f069d 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -16,9 +16,9 @@ #include "dag/dag.hpp" #include "final_chain/final_chain.hpp" -#include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp" #include "pbft/period_data.hpp" #include "vote_manager/vote_manager.hpp" diff --git a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp index 0cffea22d2..349c435749 100644 --- a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp +++ b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp @@ -7,8 +7,8 @@ #include #include "network/network.hpp" -#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" namespace taraxa { diff --git a/libraries/core_libs/network/graphql/src/sync_state.cpp b/libraries/core_libs/network/graphql/src/sync_state.cpp index f10ab0f511..4a41e83dad 100644 --- a/libraries/core_libs/network/graphql/src/sync_state.cpp +++ b/libraries/core_libs/network/graphql/src/sync_state.cpp @@ -1,7 +1,7 @@ #include "graphql/sync_state.hpp" -#include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp" namespace graphql::taraxa { diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index 358f08aedb..f24d24c06b 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -17,7 +17,7 @@ #include "common/thread_pool.hpp" #include "common/util.hpp" #include "config/config.hpp" -#include "network/tarcap/taraxa_capability_base.hpp" +#include "network/tarcap/taraxa_capability.hpp" #include "network/tarcap/tarcap_version.hpp" #include "transaction/transaction.hpp" @@ -65,6 +65,7 @@ class Network { void handleMaliciousSyncPeer(const dev::p2p::NodeID &id); std::shared_ptr getMaxChainPeer() const; + // METHODS USED IN TESTS ONLY template std::shared_ptr getSpecificHandler() const; @@ -98,22 +99,19 @@ class Network { const dev::Public pub_key_; // Packets stats per time period - // TODO: maybe remove tarcap namespace ??? std::shared_ptr all_packets_stats_; // Node stats - // TODO: maybe remove tarcap namespace ??? std::shared_ptr node_stats_; // Syncing state - // TODO: maybe remove tarcap namespace ??? std::shared_ptr pbft_syncing_state_; util::ThreadPool tp_; std::shared_ptr host_; // All supported taraxa capabilities - in descending order - std::map, + std::map, std::greater> tarcaps_; @@ -121,7 +119,6 @@ class Network { std::shared_ptr packets_tp_; // Threadpool for periodic and delayed events - // TODO: tp_ could be used for this instead util::ThreadPool periodic_events_tp_; LOG_OBJECTS_DEFINE diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/capability_latest/taraxa_capability.hpp deleted file mode 100644 index 2a6d2bcc9f..0000000000 --- a/libraries/core_libs/network/include/network/tarcap/capability_latest/taraxa_capability.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include "network/tarcap/taraxa_capability_base.hpp" - -// NoteL latest taraxa capability does not use any special namespace, other version use for example -// taraxa::network::tarcap::v1 -namespace taraxa::network::tarcap { - -class TaraxaCapability : public taraxa::network::tarcap::TaraxaCapabilityBase { - public: - using TaraxaCapabilityBase::TaraxaCapabilityBase; - - virtual ~TaraxaCapability() = default; - TaraxaCapability(const TaraxaCapability &ro) = delete; - TaraxaCapability &operator=(const TaraxaCapability &ro) = delete; - TaraxaCapability(TaraxaCapability &&ro) = delete; - TaraxaCapability &operator=(TaraxaCapability &&ro) = delete; - - protected: - void registerPacketHandlers(const h256 &genesis_hash, - const std::shared_ptr &packets_stats, - const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, - const std::shared_ptr &pbft_chain, - const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, - const std::shared_ptr &trx_mgr, const addr_t &node_addr) override; - - private: - LOG_OBJECTS_DEFINE -}; - -} // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/include/network/tarcap/capability_v1/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/capability_v1/taraxa_capability.hpp deleted file mode 100644 index 33b9c4bcee..0000000000 --- a/libraries/core_libs/network/include/network/tarcap/capability_v1/taraxa_capability.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include "network/tarcap/taraxa_capability_base.hpp" - -namespace taraxa::network::tarcap::v1 { - -class TaraxaCapability : public taraxa::network::tarcap::TaraxaCapabilityBase { - public: - using TaraxaCapabilityBase::TaraxaCapabilityBase; - - virtual ~TaraxaCapability() = default; - TaraxaCapability(const TaraxaCapability &ro) = delete; - TaraxaCapability &operator=(const TaraxaCapability &ro) = delete; - TaraxaCapability(TaraxaCapability &&ro) = delete; - TaraxaCapability &operator=(TaraxaCapability &&ro) = delete; - - protected: - virtual void registerPacketHandlers( - const h256 &genesis_hash, const std::shared_ptr &packets_stats, - const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, - const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, - const std::shared_ptr &dag_mgr, const std::shared_ptr &trx_mgr, - const addr_t &node_addr) override; - - private: - LOG_OBJECTS_DEFINE -}; - -} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handler.hpp index 269d0620d3..2e1f01cf67 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handler.hpp @@ -3,7 +3,7 @@ #include #include -#include "network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/common/packet_handler.hpp" namespace taraxa::network::tarcap { diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/exceptions.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/exceptions.hpp similarity index 81% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/exceptions.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/exceptions.hpp index 9c166974d1..ae825b36dd 100644 --- a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/exceptions.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/exceptions.hpp @@ -1,9 +1,8 @@ #pragma once -#include -#include - -#include "libp2p/Common.h" +#include "../../../../../../../../../../../../../../Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/stdexcept" +#include "../../../../../../../../../../../../../../Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/string" +#include "../../../../../../../../aleth/libp2p/Common.h" namespace taraxa { diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp similarity index 94% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp index 3b7a30411e..d18ad9de2f 100644 --- a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp @@ -1,7 +1,7 @@ #pragma once -#include "dag/dag_block.hpp" -#include "dag/dag_manager.hpp" +#include "../../../../../../../../types/dag_block/include/dag/dag_block.hpp" +#include "../../../../../../../consensus/include/dag/dag_manager.hpp" #include "packet_handler.hpp" namespace taraxa { diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/packet_handler.hpp similarity index 79% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/packet_handler.hpp index 7e8a54f5c1..a9cd329b68 100644 --- a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/packet_handler.hpp @@ -1,16 +1,15 @@ #pragma once -#include -#include - -#include "common/thread_pool.hpp" -#include "libdevcore/RLP.h" -#include "logger/logger.hpp" -#include "network/tarcap/capability_latest/packets_handlers/common/exceptions.hpp" -#include "network/tarcap/packet_types.hpp" -#include "network/tarcap/shared_states/peers_state.hpp" -#include "network/tarcap/taraxa_peer.hpp" -#include "network/threadpool/packet_data.hpp" +#include "../../../../../../../../../../../../../../Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/memory" +#include "../../../../../../../../../../../../../../Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/string_view" +#include "../../../../../../../../aleth/libdevcore/RLP.h" +#include "../../../../../../../../common/include/common/thread_pool.hpp" +#include "../../../../../../../../logger/include/logger/logger.hpp" +#include "../../../../threadpool/packet_data.hpp" +#include "../../../packet_types.hpp" +#include "../../../shared_states/peers_state.hpp" +#include "../../../taraxa_peer.hpp" +#include "exceptions.hpp" namespace taraxa::network::tarcap { diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp similarity index 94% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp index d535061e14..f54eb27e58 100644 --- a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp @@ -1,7 +1,7 @@ #pragma once +#include "../../../../../../consensus/include/vote_manager/vote_manager.hpp" #include "common/ext_syncing_packet_handler.hpp" -#include "consensus/include/vote_manager/vote_manager.hpp" namespace taraxa::network::tarcap { diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp similarity index 90% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp index e3c4362414..1c48dd3ea8 100644 --- a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp @@ -1,8 +1,8 @@ #pragma once -#include "dag/dag_block.hpp" -#include "network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp" -#include "transaction/transaction.hpp" +#include "../../../../../../../types/dag_block/include/dag/dag_block.hpp" +#include "../../../../../../../types/transaction/include/transaction/transaction.hpp" +#include "common/packet_handler.hpp" namespace taraxa { class TransactionManager; diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/common/ext_votes_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.hpp similarity index 89% rename from libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/common/ext_votes_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.hpp index d39c8cd9a2..d43a0e70aa 100644 --- a/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/common/ext_votes_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.hpp @@ -1,6 +1,6 @@ #pragma once -#include "network/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.hpp" namespace taraxa::network::tarcap::v1 { @@ -11,7 +11,7 @@ namespace taraxa::network::tarcap::v1 { class ExtVotesPacketHandler : public tarcap::ExtVotesPacketHandler { public: // TODO: can be used this instead of ctor copy/paste ? - // using tarcap::ExtVotesPacketHandler::TaraxaCapabilityBase; + // using tarcap::ExtVotesPacketHandler::TaraxaCapability; ExtVotesPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, diff --git a/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/get_next_votes_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp similarity index 94% rename from libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/get_pbft_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp index 551c50f55b..01a0f786a8 100644 --- a/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp @@ -1,6 +1,6 @@ #pragma once -#include "network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/common/packet_handler.hpp" namespace taraxa { class PbftChain; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp new file mode 100644 index 0000000000..eefd115b84 --- /dev/null +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include "../../taraxa_capability.hpp" +#include "../latest/dag_block_packet_handler.hpp" +#include "../latest/dag_sync_packet_handler.hpp" +#include "../latest/get_dag_sync_packet_handler.hpp" +#include "../latest/status_packet_handler.hpp" +#include "../latest/transaction_packet_handler.hpp" +#include "../latest/vote_packet_handler.hpp" +#include "get_next_votes_sync_packet_handler.hpp" +#include "get_pbft_sync_packet_handler.hpp" +#include "pbft_sync_packet_handler.hpp" +#include "votes_bundle_packet_handler.hpp" + +namespace taraxa::network::tarcap::v1 { + +/** + * @brief Taraxa capability V1 InitPacketsHandlers function definition + */ +static const TaraxaCapability::InitPacketsHandlers kInitV1Handlers = + [](const TarcapVersion version [[maybe_unused]], const FullNodeConfig &config, const h256 &genesis_hash, + const std::shared_ptr &peers_state, const std::shared_ptr &pbft_syncing_state, + const std::shared_ptr &test_state, + const std::shared_ptr &packets_stats, const std::shared_ptr &db, + const std::shared_ptr &pbft_mgr, const std::shared_ptr &pbft_chain, + const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, + const std::shared_ptr &trx_mgr, const addr_t &node_addr) { + auto packets_handlers = std::make_shared(); + + // Consensus packets with high processing priority + packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, + pbft_chain, vote_mgr, node_addr); + packets_handlers->registerHandler( + config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); + packets_handlers->registerHandler( + config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); + + // Standard packets with mid processing priority + packets_handlers->registerHandler(config, peers_state, packets_stats, + pbft_syncing_state, pbft_chain, pbft_mgr, + dag_mgr, trx_mgr, db, test_state, node_addr); + + packets_handlers->registerHandler(config, peers_state, packets_stats, trx_mgr, + test_state, node_addr); + + // Non critical packets with low processing priority + packets_handlers->registerHandler(config, peers_state, packets_stats, + pbft_syncing_state, pbft_chain, pbft_mgr, dag_mgr, + db, genesis_hash, node_addr); + packets_handlers->registerHandler(config, peers_state, packets_stats, trx_mgr, + dag_mgr, db, node_addr); + + packets_handlers->registerHandler(config, peers_state, packets_stats, + pbft_syncing_state, pbft_chain, pbft_mgr, dag_mgr, + trx_mgr, db, node_addr); + + packets_handlers->registerHandler( + config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, vote_mgr, db, node_addr); + + packets_handlers->registerHandler(config, peers_state, packets_stats, + pbft_syncing_state, pbft_chain, pbft_mgr, + dag_mgr, vote_mgr, db, node_addr); + + return packets_handlers; + }; + +} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp similarity index 89% rename from libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp index b56789305d..1ca6098f73 100644 --- a/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp @@ -1,7 +1,7 @@ #pragma once -#include "consensus/include/vote_manager/vote_manager.hpp" -#include "network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp" +#include "../../../../../../consensus/include/vote_manager/vote_manager.hpp" +#include "network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp" namespace taraxa::network::tarcap::v1 { diff --git a/libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/votes_bundle_packet_handler.hpp similarity index 100% rename from libraries/core_libs/network/include/network/tarcap/capability_v1/packets_handlers/votes_bundle_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/votes_bundle_packet_handler.hpp diff --git a/libraries/core_libs/network/include/network/tarcap/readme.md b/libraries/core_libs/network/include/network/tarcap/readme.md deleted file mode 100644 index 12f6a01aca..0000000000 --- a/libraries/core_libs/network/include/network/tarcap/readme.md +++ /dev/null @@ -1,19 +0,0 @@ -### Multiple taraxa capabilities support -- Create new folder, e.g. `capability_v1` -- Create new `TaraxaCapability` class derived from `TaraxaCapabiliyyBase` in new namespace, e.g.: - - - # Create clang profile - namespace taraxa::network::tarcap::v1 { - - class TaraxaCapability : public taraxa::network::tarcap::TaraxaCapabilityBase { - ... - } - - } -- Derive new packet handlers with different logic than the original one. -- - `!!! Important:` These handlers must be - directly on indirectly derived from the latest packets handlers, which are inside - `network/tarcap/capability_latest` folder, otherwise network class would not work properly - and project would not compile \ No newline at end of file diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp new file mode 100644 index 0000000000..fb2b6bf982 --- /dev/null +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp @@ -0,0 +1,128 @@ +#pragma once + +#include +#include +#include +#include + +#include + +#include "common/thread_pool.hpp" +#include "config/config.hpp" +#include "network/tarcap/packets_handler.hpp" +#include "network/tarcap/shared_states/peers_state.hpp" +#include "network/tarcap/shared_states/test_state.hpp" +#include "network/tarcap/tarcap_version.hpp" +#include "network/threadpool/tarcap_thread_pool.hpp" +#include "pbft/pbft_chain.hpp" + +namespace taraxa { +class DbStorage; +class PbftManager; +class PbftChain; +class VoteManager; +class DagManager; +class TransactionManager; +enum class TransactionStatus; +} // namespace taraxa + +namespace taraxa::network::tarcap { + +class PacketsHandler; +class PbftSyncingState; +class TaraxaPeer; + +class TaraxaCapability final : public dev::p2p::CapabilityFace { + public: + /** + * @brief Function signature for creating taraxa capability packets handlers + */ + using InitPacketsHandlers = std::function( + const TarcapVersion version, const FullNodeConfig &config, const h256 &genesis_hash, + const std::shared_ptr &peers_state, const std::shared_ptr &pbft_syncing_state, + const std::shared_ptr &test_state, + const std::shared_ptr &packets_stats, const std::shared_ptr &db, + const std::shared_ptr &pbft_mgr, const std::shared_ptr &pbft_chain, + const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, + const std::shared_ptr &trx_mgr, const addr_t &node_addr)>; + + /** + * @brief Default InitPacketsHandlers function definition with the latest version of packets handlers + */ + static const InitPacketsHandlers kInitLatestVersionHandlers; + + public: + TaraxaCapability(TarcapVersion version, const FullNodeConfig &conf, const h256 &genesis_hash, + std::weak_ptr host, const dev::KeyPair &key, + std::shared_ptr threadpool, + std::shared_ptr packets_stats, + std::shared_ptr syncing_state, std::shared_ptr db, + std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, + std::shared_ptr trx_mgr, const std::string &log_channel, + InitPacketsHandlers init_packets_handlers = kInitLatestVersionHandlers); + + virtual ~TaraxaCapability() = default; + TaraxaCapability(const TaraxaCapability &ro) = delete; + TaraxaCapability &operator=(const TaraxaCapability &ro) = delete; + TaraxaCapability(TaraxaCapability &&ro) = delete; + TaraxaCapability &operator=(TaraxaCapability &&ro) = delete; + + // CapabilityFace implemented interface + std::string name() const override; + TarcapVersion version() const override; + unsigned messageCount() const override; + void onConnect(std::weak_ptr session, u256 const &) override; + void onDisconnect(dev::p2p::NodeID const &_nodeID) override; + void interpretCapabilityPacket(std::weak_ptr session, unsigned _id, dev::RLP const &_r) override; + std::string packetTypeToString(unsigned _packetType) const override; + + const std::shared_ptr &getPeersState(); + + template + std::shared_ptr getSpecificHandler() const; + + // METHODS USED IN TESTS ONLY + size_t getReceivedBlocksCount() const; + size_t getReceivedTransactionsCount() const; + // END METHODS USED IN TESTS ONLY + + private: + bool filterSyncIrrelevantPackets(SubprotocolPacketType packet_type) const; + + public: + // TODO: Remove in future when tests are refactored + // Test state + std::shared_ptr test_state_; + + private: + // Capability version + unsigned version_; + + // Packets stats per time period + std::shared_ptr all_packets_stats_; + + // Node config + const FullNodeConfig &kConf; + + // Peers state + std::shared_ptr peers_state_; + + // Syncing state + syncing handler + std::shared_ptr pbft_syncing_state_; + + // Packets handlers + std::shared_ptr packets_handlers_; + + // Main Threadpool for processing packets + std::shared_ptr thread_pool_; + + LOG_OBJECTS_DEFINE +}; + +template +std::shared_ptr TaraxaCapability::getSpecificHandler() const { + return packets_handlers_->getSpecificHandler(); +} + +} // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp deleted file mode 100644 index ed049c3727..0000000000 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_capability_base.hpp +++ /dev/null @@ -1,124 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include - -#include "common/thread_pool.hpp" -#include "config/config.hpp" -#include "network/tarcap/packets_handler.hpp" -#include "network/tarcap/shared_states/peers_state.hpp" -#include "network/tarcap/shared_states/test_state.hpp" -#include "network/tarcap/tarcap_version.hpp" -#include "network/threadpool/tarcap_thread_pool.hpp" -#include "pbft/pbft_chain.hpp" - -namespace taraxa { -class DbStorage; -class PbftManager; -class PbftChain; -class VoteManager; -class DagManager; -class TransactionManager; -enum class TransactionStatus; -} // namespace taraxa - -namespace taraxa::network::tarcap { - -class PacketsHandler; -class PbftSyncingState; -class TaraxaPeer; - -class TaraxaCapabilityBase : public dev::p2p::CapabilityFace { - public: - TaraxaCapabilityBase(std::weak_ptr host, const dev::KeyPair &key, const FullNodeConfig &conf, - TarcapVersion version, std::shared_ptr threadpool, - std::shared_ptr packets_stats, - std::shared_ptr syncing_state, const std::string &log_channel); - - virtual ~TaraxaCapabilityBase() = default; - TaraxaCapabilityBase(const TaraxaCapabilityBase &ro) = delete; - TaraxaCapabilityBase &operator=(const TaraxaCapabilityBase &ro) = delete; - TaraxaCapabilityBase(TaraxaCapabilityBase &&ro) = delete; - TaraxaCapabilityBase &operator=(TaraxaCapabilityBase &&ro) = delete; - - // Init capability. Register packet handlers and periodic events - virtual void init(const h256 &genesis_hash, std::shared_ptr db, std::shared_ptr pbft_mgr, - std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, - const dev::Address &node_addr); - - // CapabilityFace implemented interface - std::string name() const override; - TarcapVersion version() const override; - unsigned messageCount() const override; - virtual void onConnect(std::weak_ptr session, u256 const &) override; - virtual void onDisconnect(dev::p2p::NodeID const &_nodeID) override; - virtual void interpretCapabilityPacket(std::weak_ptr session, unsigned _id, - dev::RLP const &_r) override; - virtual std::string packetTypeToString(unsigned _packetType) const override; - - template - std::shared_ptr getSpecificHandler() const; - - // Interface required in network class to access packets handlers functionality - // METHODS USED IN REAL CODE - const std::shared_ptr &getPeersState(); - - // METHODS USED IN TESTS ONLY - size_t getReceivedBlocksCount() const; - size_t getReceivedTransactionsCount() const; - // END METHODS USED IN TESTS ONLY - - protected: - virtual void registerPacketHandlers(const h256 &genesis_hash, - const std::shared_ptr &packets_stats, - const std::shared_ptr &db, - const std::shared_ptr &pbft_mgr, - const std::shared_ptr &pbft_chain, - const std::shared_ptr &vote_mgr, - const std::shared_ptr &dag_mgr, - const std::shared_ptr &trx_mgr, const addr_t &node_addr) = 0; - - private: - bool filterSyncIrrelevantPackets(SubprotocolPacketType packet_type) const; - - public: - // TODO: Remove in future when tests are refactored - // Test state - std::shared_ptr test_state_; - - protected: - // Capability version - unsigned version_; - - // Packets stats per time period - std::shared_ptr all_packets_stats_; - - // Node config - const FullNodeConfig &kConf; - - // Peers state - std::shared_ptr peers_state_; - - // Syncing state + syncing handler - std::shared_ptr pbft_syncing_state_; - - // Packets handlers - std::shared_ptr packets_handlers_; - - // Main Threadpool for processing packets - std::shared_ptr thread_pool_; - - LOG_OBJECTS_DEFINE -}; - -template -std::shared_ptr TaraxaCapabilityBase::getSpecificHandler() const { - return packets_handlers_->getSpecificHandler(); -} - -} // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index 45026d47ad..dc8c479db9 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -8,17 +8,13 @@ #include #include "config/version.hpp" -#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" -#include "network/tarcap/capability_latest/taraxa_capability.hpp" -#include "network/tarcap/capability_v1/taraxa_capability.hpp" +#include "network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp" +#include "network/tarcap/packets_handlers/v1/init_packets_handlers.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "network/tarcap/stats/node_stats.hpp" #include "network/tarcap/stats/time_period_packets_stats.hpp" +#include "network/tarcap/taraxa_capability.hpp" #include "pbft/pbft_manager.hpp" namespace taraxa { @@ -74,22 +70,22 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::fi constructCapabilities = [&](std::weak_ptr host) { assert(!host.expired()); - const size_t kOldNetworkVersion = 1; - assert(kOldNetworkVersion < TARAXA_NET_VERSION); + const size_t kV1NetworkVersion = 1; + assert(kV1NetworkVersion < TARAXA_NET_VERSION); dev::p2p::Host::CapabilityList capabilities; - // Register old version of taraxa capability - auto v1_tarcap = std::make_shared( - host, key, config, kOldNetworkVersion, packets_tp_, all_packets_stats_, pbft_syncing_state_, "V1_TARCAP"); - v1_tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); + // Register old version (V1) of taraxa capability + auto v1_tarcap = std::make_shared( + kV1NetworkVersion, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, db, + pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, "V1_TARCAP", network::tarcap::v1::kInitV1Handlers); capabilities.emplace_back(v1_tarcap); - // Register new version of taraxa capability - auto v2_tarcap = std::make_shared( - host, key, config, TARAXA_NET_VERSION, packets_tp_, all_packets_stats_, pbft_syncing_state_, "TARCAP"); - v2_tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); - capabilities.emplace_back(v2_tarcap); + // Register latest version of taraxa capability + auto latest_tarcap = std::make_shared( + TARAXA_NET_VERSION, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, db, + pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, "TARCAP"); + capabilities.emplace_back(latest_tarcap); return capabilities; }; @@ -100,9 +96,9 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::fi dev::p2p::Host::CapabilityList capabilities; for (const auto test_tarcap_version : create_test_tarcaps) { auto tarcap = std::make_shared( - host, key, config, test_tarcap_version, packets_tp_, all_packets_stats_, pbft_syncing_state_, + test_tarcap_version, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, + db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, "V" + std::to_string(test_tarcap_version) + "_TARCAP"); - tarcap->init(genesis_hash, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, key.address()); capabilities.emplace_back(tarcap); } @@ -113,7 +109,7 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::fi host_ = dev::p2p::Host::make(net_version, constructCapabilities, key, net_conf, taraxa_net_conf, network_file_path); for (const auto &cap : host_->getSupportedCapabilities()) { const auto tarcap_version = cap.second.ref->version(); - auto tarcap = std::static_pointer_cast(cap.second.ref); + auto tarcap = std::static_pointer_cast(cap.second.ref); tarcaps_[tarcap_version] = std::move(tarcap); } diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp deleted file mode 100644 index 402de85d5a..0000000000 --- a/libraries/core_libs/network/src/tarcap/capability_latest/taraxa_capability.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "network/tarcap/capability_latest/taraxa_capability.hpp" - -#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" - -namespace taraxa::network::tarcap { - -void TaraxaCapability::registerPacketHandlers( - const h256 &genesis_hash, const std::shared_ptr &packets_stats, - const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, - const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, - const std::shared_ptr &dag_mgr, const std::shared_ptr &trx_mgr, - const addr_t &node_addr) { - // Register all packet handlers - - // Consensus packets with high processing priority - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr); - packets_handlers_->registerHandler( - kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr); - - // Standard packets with mid processing priority - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, - pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, - trx_mgr, db, test_state_, node_addr); - - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, trx_mgr, - test_state_, node_addr); - - // Non critical packets with low processing priority - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, - pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, - db, genesis_hash, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, trx_mgr, - dag_mgr, db, node_addr); - - packets_handlers_->registerHandler( - kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, node_addr); - - packets_handlers_->registerHandler( - kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, vote_mgr, db, node_addr); - - packets_handlers_->registerHandler( - kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, db, node_addr); -} -} // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/src/tarcap/capability_v1/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/capability_v1/taraxa_capability.cpp deleted file mode 100644 index e186658f8b..0000000000 --- a/libraries/core_libs/network/src/tarcap/capability_v1/taraxa_capability.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "network/tarcap/capability_v1/taraxa_capability.hpp" - -#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/capability_v1/packets_handlers/get_next_votes_sync_packet_handler.hpp" -#include "network/tarcap/capability_v1/packets_handlers/get_pbft_sync_packet_handler.hpp" -#include "network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp" -#include "network/tarcap/capability_v1/packets_handlers/votes_bundle_packet_handler.hpp" - -namespace taraxa::network::tarcap::v1 { - -void TaraxaCapability::registerPacketHandlers( - const h256 &genesis_hash, const std::shared_ptr &packets_stats, - const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, - const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, - const std::shared_ptr &dag_mgr, const std::shared_ptr &trx_mgr, - const addr_t &node_addr) { - // Register all packet handlers - - // Consensus packets with high processing priority - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr); - packets_handlers_->registerHandler( - kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr); - - // Standard packets with mid processing priority - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, - pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, - trx_mgr, db, test_state_, node_addr); - - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, trx_mgr, - test_state_, node_addr); - - // Non critical packets with low processing priority - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, - pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, - db, genesis_hash, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, trx_mgr, - dag_mgr, db, node_addr); - - packets_handlers_->registerHandler( - kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, node_addr); - - packets_handlers_->registerHandler( - kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, vote_mgr, db, node_addr); - - packets_handlers_->registerHandler( - kConf, peers_state_, packets_stats, pbft_syncing_state_, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, db, node_addr); -} -} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.cpp index e788787da3..061d414b18 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.cpp @@ -1,6 +1,5 @@ -#include "network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp" -#include "dag/dag.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "pbft/pbft_chain.hpp" #include "pbft/pbft_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp similarity index 99% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp index 66124585a8..34c0b1359f 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_latest/packets_handlers/common/ext_votes_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote/vote.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/packet_handler.cpp index 3786bb0e51..323c699baf 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/common/packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_latest/packets_handlers/common/packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/common/packet_handler.hpp" #include "network/tarcap/stats/time_period_packets_stats.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp index a18faa9f5a..eeec23f0b1 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp @@ -1,7 +1,7 @@ -#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp" #include "dag/dag_manager.hpp" -#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "network/tarcap/shared_states/test_state.hpp" #include "transaction/transaction_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_sync_packet_handler.cpp similarity index 97% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_sync_packet_handler.cpp index 6cd2aa377b..f351dacec8 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_sync_packet_handler.cpp @@ -1,7 +1,7 @@ -#include "network/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp" #include "dag/dag.hpp" -#include "network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "transaction/transaction.hpp" #include "transaction/transaction_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.cpp similarity index 97% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.cpp index a16b8b0b8f..49af08a380 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp" #include "dag/dag_manager.hpp" #include "transaction/transaction_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.cpp similarity index 97% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.cpp index 0a2dc0428e..c99c093542 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote_manager/vote_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp index ae92c8cb74..b20fa00b37 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "pbft/pbft_chain.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp similarity index 99% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp index c7ac903dc1..e68bd655b8 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "pbft/pbft_chain.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/status_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/status_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp index d2420fa2d0..9877afa226 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/status_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp @@ -1,8 +1,8 @@ -#include "network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/status_packet_handler.hpp" #include "config/version.hpp" #include "dag/dag.hpp" -#include "network/tarcap/capability_latest/packets_handlers/common/ext_syncing_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "pbft/pbft_chain.hpp" #include "pbft/pbft_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/transaction_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/transaction_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp index 76433f5c02..aa51cfa953 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/transaction_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp" #include diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/vote_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/vote_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp index 3c516126fb..4eb75ad7ef 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/vote_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote_manager/vote_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp index 685fd0d84f..7b0c7eb34c 100644 --- a/libraries/core_libs/network/src/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote/votes_bundle_rlp.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.cpp similarity index 95% rename from libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/common/ext_votes_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.cpp index a9e0d4dcb1..4b59f5c508 100644 --- a/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_v1/packets_handlers/common/ext_votes_packet_handler.hpp" +#include "network/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote/vote.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/get_next_votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.cpp similarity index 97% rename from libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/get_next_votes_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.cpp index 9cb9a68a1f..21cb1155d5 100644 --- a/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/get_next_votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_v1/packets_handlers/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote_manager/vote_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/get_pbft_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp index 251e586769..388ad35b11 100644 --- a/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_v1/packets_handlers/get_pbft_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "pbft/pbft_chain.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/pbft_sync_packet_handler.cpp similarity index 99% rename from libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/pbft_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/v1/pbft_sync_packet_handler.cpp index f5e1430489..4639286ca5 100644 --- a/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/pbft_sync_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_v1/packets_handlers/pbft_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "pbft/pbft_chain.hpp" diff --git a/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/votes_bundle_packet_handler.cpp similarity index 98% rename from libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/votes_bundle_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/v1/votes_bundle_packet_handler.cpp index e7c1164bcc..a9cc3fd996 100644 --- a/libraries/core_libs/network/src/tarcap/capability_v1/packet_handlers/votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/votes_bundle_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/capability_v1/packets_handlers/votes_bundle_packet_handler.hpp" +#include "network/tarcap/packets_handlers/v1/votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote_manager/vote_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp similarity index 53% rename from libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp rename to libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index f7d1beadee..366c9d256a 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability_base.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -1,22 +1,20 @@ -#include "network/tarcap/taraxa_capability_base.hpp" +#include "network/tarcap/taraxa_capability.hpp" #include -#include "dag/dag.hpp" -#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/dag_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/get_pbft_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/pbft_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" #include "network/tarcap/packets_handler.hpp" +#include "network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/status_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" #include "network/tarcap/shared_states/test_state.hpp" -#include "network/tarcap/taraxa_peer.hpp" #include "node/node.hpp" #include "pbft/pbft_chain.hpp" #include "pbft/pbft_manager.hpp" @@ -25,12 +23,15 @@ namespace taraxa::network::tarcap { -TaraxaCapabilityBase::TaraxaCapabilityBase(std::weak_ptr host, const dev::KeyPair &key, - const FullNodeConfig &conf, TarcapVersion version, - std::shared_ptr threadpool, - std::shared_ptr packets_stats, - std::shared_ptr syncing_state, - const std::string &log_channel) +TaraxaCapability::TaraxaCapability(TarcapVersion version, const FullNodeConfig &conf, const h256 &genesis_hash, + std::weak_ptr host, const dev::KeyPair &key, + std::shared_ptr threadpool, + std::shared_ptr packets_stats, + std::shared_ptr syncing_state, std::shared_ptr db, + std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, + std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, + std::shared_ptr trx_mgr, const std::string &log_channel, + InitPacketsHandlers init_packets_handlers) : test_state_(std::make_shared()), version_(version), all_packets_stats_(std::move(packets_stats)), @@ -43,70 +44,21 @@ TaraxaCapabilityBase::TaraxaCapabilityBase(std::weak_ptr host, c LOG_OBJECTS_CREATE(log_channel); peers_state_ = std::make_shared(host, kConf); -} - -void TaraxaCapabilityBase::init(const h256 &genesis_hash, std::shared_ptr db, - std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, - std::shared_ptr trx_mgr, const dev::Address &node_addr) { - // Creates and registers all packets handlers - registerPacketHandlers(genesis_hash, all_packets_stats_, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, - node_addr); - - // Must be called after registerPacketHandlers - thread_pool_->setPacketsHandlers(version(), packets_handlers_); -} - -void TaraxaCapabilityBase::registerPacketHandlers( - const h256 &genesis_hash, const std::shared_ptr &packets_stats, - const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, - const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, - const std::shared_ptr &dag_mgr, const std::shared_ptr &trx_mgr, - const addr_t &node_addr) { - // Register all packet handlers - - // Consensus packets with high processing priority - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, - vote_mgr, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_mgr, pbft_chain, - vote_mgr, node_addr); - - // Standard packets with mid processing priority - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_syncing_state_, - pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, test_state_, - node_addr); - - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, trx_mgr, test_state_, - node_addr); - - // Non critical packets with low processing priority - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_syncing_state_, - pbft_chain, pbft_mgr, dag_mgr, db, genesis_hash, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, trx_mgr, dag_mgr, db, - node_addr); - - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_syncing_state_, - pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, node_addr); - - // TODO there is additional logic, that should be moved outside process function - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_syncing_state_, - pbft_chain, vote_mgr, db, node_addr); + packets_handlers_ = + init_packets_handlers(version, conf, genesis_hash, peers_state_, pbft_syncing_state_, test_state_, + all_packets_stats_, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, node_addr); - packets_handlers_->registerHandler(kConf, peers_state_, packets_stats, pbft_syncing_state_, - pbft_chain, pbft_mgr, dag_mgr, vote_mgr, db, node_addr); - - thread_pool_->setPacketsHandlers(version(), packets_handlers_); + // Must be called after init_packets_handlers + thread_pool_->setPacketsHandlers(version, packets_handlers_); } -std::string TaraxaCapabilityBase::name() const { return TARAXA_CAPABILITY_NAME; } +std::string TaraxaCapability::name() const { return TARAXA_CAPABILITY_NAME; } -TarcapVersion TaraxaCapabilityBase::version() const { return version_; } +TarcapVersion TaraxaCapability::version() const { return version_; } -unsigned TaraxaCapabilityBase::messageCount() const { return SubprotocolPacketType::PacketCount; } +unsigned TaraxaCapability::messageCount() const { return SubprotocolPacketType::PacketCount; } -void TaraxaCapabilityBase::onConnect(std::weak_ptr session, u256 const &) { +void TaraxaCapability::onConnect(std::weak_ptr session, u256 const &) { const auto session_p = session.lock(); if (!session_p) { LOG(log_er_) << "Unable to obtain session ptr !"; @@ -128,7 +80,7 @@ void TaraxaCapabilityBase::onConnect(std::weak_ptr session, u status_packet_handler->sendStatus(node_id, true); } -void TaraxaCapabilityBase::onDisconnect(dev::p2p::NodeID const &_nodeID) { +void TaraxaCapability::onDisconnect(dev::p2p::NodeID const &_nodeID) { LOG(log_nf_) << "Node " << _nodeID << " disconnected"; peers_state_->erasePeer(_nodeID); @@ -144,12 +96,12 @@ void TaraxaCapabilityBase::onDisconnect(dev::p2p::NodeID const &_nodeID) { } } -std::string TaraxaCapabilityBase::packetTypeToString(unsigned _packetType) const { +std::string TaraxaCapability::packetTypeToString(unsigned _packetType) const { return convertPacketTypeToString(static_cast(_packetType)); } -void TaraxaCapabilityBase::interpretCapabilityPacket(std::weak_ptr session, unsigned _id, - dev::RLP const &_r) { +void TaraxaCapability::interpretCapabilityPacket(std::weak_ptr session, unsigned _id, + dev::RLP const &_r) { const auto session_p = session.lock(); if (!session_p) { LOG(log_er_) << "Unable to obtain session ptr !"; @@ -239,7 +191,7 @@ void TaraxaCapabilityBase::interpretCapabilityPacket(std::weak_ptrpush({version(), threadpool::PacketData(packet_type, node_id, _r.data().toBytes())}); } -inline bool TaraxaCapabilityBase::filterSyncIrrelevantPackets(SubprotocolPacketType packet_type) const { +inline bool TaraxaCapability::filterSyncIrrelevantPackets(SubprotocolPacketType packet_type) const { switch (packet_type) { case StatusPacket: case GetPbftSyncPacket: @@ -250,12 +202,57 @@ inline bool TaraxaCapabilityBase::filterSyncIrrelevantPackets(SubprotocolPacketT } } -const std::shared_ptr &TaraxaCapabilityBase::getPeersState() { return peers_state_; } +const std::shared_ptr &TaraxaCapability::getPeersState() { return peers_state_; } // METHODS USED IN TESTS ONLY -size_t TaraxaCapabilityBase::getReceivedBlocksCount() const { return test_state_->getBlocksSize(); } +size_t TaraxaCapability::getReceivedBlocksCount() const { return test_state_->getBlocksSize(); } -size_t TaraxaCapabilityBase::getReceivedTransactionsCount() const { return test_state_->getTransactionsSize(); } +size_t TaraxaCapability::getReceivedTransactionsCount() const { return test_state_->getTransactionsSize(); } // END METHODS USED IN TESTS ONLY +const TaraxaCapability::InitPacketsHandlers TaraxaCapability::kInitLatestVersionHandlers = + [](const TarcapVersion version [[maybe_unused]], const FullNodeConfig &config, const h256 &genesis_hash, + const std::shared_ptr &peers_state, const std::shared_ptr &pbft_syncing_state, + const std::shared_ptr &test_state, + const std::shared_ptr &packets_stats, const std::shared_ptr &db, + const std::shared_ptr &pbft_mgr, const std::shared_ptr &pbft_chain, + const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, + const std::shared_ptr &trx_mgr, const addr_t &node_addr) { + auto packets_handlers = std::make_shared(); + + // Consensus packets with high processing priority + packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, pbft_chain, + vote_mgr, node_addr); + packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, + pbft_chain, vote_mgr, node_addr); + packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, + pbft_chain, vote_mgr, node_addr); + + // Standard packets with mid processing priority + packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, + pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, test_state, + node_addr); + + packets_handlers->registerHandler(config, peers_state, packets_stats, trx_mgr, + test_state, node_addr); + + // Non critical packets with low processing priority + packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, + pbft_chain, pbft_mgr, dag_mgr, db, genesis_hash, + node_addr); + packets_handlers->registerHandler(config, peers_state, packets_stats, trx_mgr, dag_mgr, + db, node_addr); + + packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, + pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, node_addr); + + packets_handlers->registerHandler( + config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, vote_mgr, db, node_addr); + + packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, + pbft_chain, pbft_mgr, dag_mgr, vote_mgr, db, node_addr); + + return packets_handlers; + }; + } // namespace taraxa::network::tarcap diff --git a/tests/network_test.cpp b/tests/network_test.cpp index 2f90f1327c..c1d899489c 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -12,13 +12,13 @@ #include "dag/dag.hpp" #include "dag/dag_block_proposer.hpp" #include "logger/logger.hpp" -#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/get_dag_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/get_next_votes_sync_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/status_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/votes_bundle_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/status_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "test_util/samples.hpp" #include "test_util/test_util.hpp" diff --git a/tests/p2p_test.cpp b/tests/p2p_test.cpp index 3ef061eb96..648abde954 100644 --- a/tests/p2p_test.cpp +++ b/tests/p2p_test.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -13,10 +12,10 @@ #include "config/config.hpp" #include "logger/logger.hpp" #include "network/network.hpp" -#include "network/tarcap/capability_latest/packets_handlers/dag_block_packet_handler.hpp" -#include "network/tarcap/capability_latest/packets_handlers/transaction_packet_handler.hpp" -#include "network/tarcap/capability_latest/taraxa_capability.hpp" +#include "network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" +#include "network/tarcap/taraxa_capability.hpp" #include "test_util/samples.hpp" #include "test_util/test_util.hpp" @@ -34,7 +33,7 @@ auto g_signed_trx_samples = Lazy([] { return samples::createSignedTrxSamples(0, struct P2PTest : NodesTest {}; // TODO this needs to be removed and called from tracap->setPendingPeersToReady() directly -void setPendingPeersToReady(std::shared_ptr taraxa_capability) { +void setPendingPeersToReady(std::shared_ptr taraxa_capability) { const auto &peers_state = taraxa_capability->getPeersState(); auto peerIds = peers_state->getAllPendingPeersIDs(); @@ -46,18 +45,18 @@ void setPendingPeersToReady(std::shared_ptr makeTarcap(std::weak_ptr host, - const dev::KeyPair &key, - const FullNodeConfig &conf, - const h256 &genesis_hash, unsigned version) { +std::shared_ptr makeTarcap(std::weak_ptr host, + const dev::KeyPair &key, + const FullNodeConfig &conf, + const h256 &genesis_hash, unsigned version) { auto thread_pool = std::make_shared(conf.network.packets_processing_threads); auto packets_stats = std::make_shared( conf.network.ddos_protection.packets_stats_time_period_ms, Address{}); auto syncing_state = std::make_shared(conf.network.deep_syncing_threshold); - auto tarcap = std::make_shared(host, key, conf, version, thread_pool, - packets_stats, syncing_state, "TARCAP"); - tarcap->init(genesis_hash, {}, {}, {}, {}, {}, {}, key.address()); + auto tarcap = std::make_shared(version, conf, genesis_hash, host, key, thread_pool, + packets_stats, syncing_state, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, "TARCAP"); thread_pool->startProcessing(); @@ -101,7 +100,7 @@ TEST_F(P2PTest, p2p_discovery) { /* Test creates two host/network/capability and verifies that host connect to each other and that a block packet message can be sent from one host -to the other using TaraxaCapabilityBase +to the other using TaraxaCapability */ TEST_F(P2PTest, capability_send_block) { int const step = 10; @@ -115,7 +114,7 @@ TEST_F(P2PTest, capability_send_block) { conf.network.transaction_interval_ms = 1000; h256 genesis; - std::shared_ptr thc1, thc2; + std::shared_ptr thc1, thc2; auto host1 = Host::make( "Test", [&](auto host) { @@ -207,7 +206,7 @@ TEST_F(P2PTest, block_propagate) { FullNodeConfig conf; conf.network.transaction_interval_ms = 1000; h256 genesis; - std::shared_ptr thc1; + std::shared_ptr thc1; auto host1 = Host::make( "Test", [&](auto host) { @@ -219,7 +218,7 @@ TEST_F(P2PTest, block_propagate) { util::ThreadPool tp; tp.post_loop({}, [=] { host1->do_work(); }); std::vector> vHosts; - std::vector> vCapabilities; + std::vector> vCapabilities; for (int i = 0; i < nodeCount; i++) { auto host = vHosts.emplace_back(Host::make( "Test", diff --git a/tests/pbft_manager_test.cpp b/tests/pbft_manager_test.cpp index 52b0020eaf..f0cb186b26 100644 --- a/tests/pbft_manager_test.cpp +++ b/tests/pbft_manager_test.cpp @@ -4,7 +4,7 @@ #include "common/static_init.hpp" #include "logger/logger.hpp" #include "network/network.hpp" -#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp" #include "test_util/node_dag_creation_fixture.hpp" #include "vdf/sortition.hpp" diff --git a/tests/vote_test.cpp b/tests/vote_test.cpp index 435fe0b2fd..bb2616d985 100644 --- a/tests/vote_test.cpp +++ b/tests/vote_test.cpp @@ -4,7 +4,7 @@ #include "common/static_init.hpp" #include "logger/logger.hpp" #include "network/network.hpp" -#include "network/tarcap/capability_latest/packets_handlers/vote_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp" #include "node/node.hpp" #include "pbft/pbft_manager.hpp" #include "test_util/test_util.hpp" From b34045b674b1b818e09ece4d05ad17088fc1b96f Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Mon, 8 May 2023 17:12:15 -0700 Subject: [PATCH 045/134] chore: fix rlp in period data migration --- libraries/core_libs/storage/src/migration/period_data.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/core_libs/storage/src/migration/period_data.cpp b/libraries/core_libs/storage/src/migration/period_data.cpp index 297fa01e3b..1d1c100121 100644 --- a/libraries/core_libs/storage/src/migration/period_data.cpp +++ b/libraries/core_libs/storage/src/migration/period_data.cpp @@ -13,7 +13,9 @@ void PeriodData::migrate() { // Get and save data in new format for all blocks for (it->SeekToFirst(); it->Valid(); it->Next()) { - const auto period_data_old_rlp = dev::RLP(it->value().ToString()); + // Use separate variable for rlp bytes, dev::RLP(it->value().ToString()); does not work + const auto bytes = it->value().ToString(); + const auto period_data_old_rlp = dev::RLP(bytes); assert(period_data_old_rlp.itemCount() == 4); ::taraxa::PeriodData period_data; From 9cceaedb8735d293beec423dde1f40a0f484febe Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Tue, 9 May 2023 11:15:33 -0700 Subject: [PATCH 046/134] chore: fix includes --- .../latest/common/exceptions.hpp | 7 ++++--- .../common/ext_syncing_packet_handler.hpp | 4 ++-- .../latest/common/packet_handler.hpp | 20 ++++++++++--------- .../latest/pbft_sync_packet_handler.hpp | 2 +- .../latest/transaction_packet_handler.hpp | 4 ++-- .../v1/init_packets_handlers.hpp | 14 ++++++------- .../v1/pbft_sync_packet_handler.hpp | 2 +- .../test_util/node_dag_creation_fixture.hpp | 3 ++- 8 files changed, 30 insertions(+), 26 deletions(-) diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/exceptions.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/exceptions.hpp index ae825b36dd..9c166974d1 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/exceptions.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/exceptions.hpp @@ -1,8 +1,9 @@ #pragma once -#include "../../../../../../../../../../../../../../Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/stdexcept" -#include "../../../../../../../../../../../../../../Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/string" -#include "../../../../../../../../aleth/libp2p/Common.h" +#include +#include + +#include "libp2p/Common.h" namespace taraxa { diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp index d18ad9de2f..3b7a30411e 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../../../../../../../../types/dag_block/include/dag/dag_block.hpp" -#include "../../../../../../../consensus/include/dag/dag_manager.hpp" +#include "dag/dag_block.hpp" +#include "dag/dag_manager.hpp" #include "packet_handler.hpp" namespace taraxa { diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/packet_handler.hpp index a9cd329b68..b23e30db9c 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/packet_handler.hpp @@ -1,15 +1,17 @@ #pragma once -#include "../../../../../../../../../../../../../../Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/memory" -#include "../../../../../../../../../../../../../../Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/string_view" -#include "../../../../../../../../aleth/libdevcore/RLP.h" -#include "../../../../../../../../common/include/common/thread_pool.hpp" -#include "../../../../../../../../logger/include/logger/logger.hpp" -#include "../../../../threadpool/packet_data.hpp" -#include "../../../packet_types.hpp" -#include "../../../shared_states/peers_state.hpp" -#include "../../../taraxa_peer.hpp" +#include + +#include +#include + +#include "common/thread_pool.hpp" #include "exceptions.hpp" +#include "logger/logger.hpp" +#include "network/tarcap/packet_types.hpp" +#include "network/tarcap/shared_states/peers_state.hpp" +#include "network/tarcap/taraxa_peer.hpp" +#include "network/threadpool/packet_data.hpp" namespace taraxa::network::tarcap { diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp index f54eb27e58..80abff0088 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../../../../../../consensus/include/vote_manager/vote_manager.hpp" #include "common/ext_syncing_packet_handler.hpp" +#include "vote_manager/vote_manager.hpp" namespace taraxa::network::tarcap { diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp index 1c48dd3ea8..2c94e365e3 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp @@ -1,8 +1,8 @@ #pragma once -#include "../../../../../../../types/dag_block/include/dag/dag_block.hpp" -#include "../../../../../../../types/transaction/include/transaction/transaction.hpp" #include "common/packet_handler.hpp" +#include "dag/dag_block.hpp" +#include "transaction/transaction.hpp" namespace taraxa { class TransactionManager; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp index eefd115b84..0b1a264748 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp @@ -1,14 +1,14 @@ #pragma once -#include "../../taraxa_capability.hpp" -#include "../latest/dag_block_packet_handler.hpp" -#include "../latest/dag_sync_packet_handler.hpp" -#include "../latest/get_dag_sync_packet_handler.hpp" -#include "../latest/status_packet_handler.hpp" -#include "../latest/transaction_packet_handler.hpp" -#include "../latest/vote_packet_handler.hpp" #include "get_next_votes_sync_packet_handler.hpp" #include "get_pbft_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/status_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp" +#include "network/tarcap/taraxa_capability.hpp" #include "pbft_sync_packet_handler.hpp" #include "votes_bundle_packet_handler.hpp" diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp index 1ca6098f73..906369f57d 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../../../../../../consensus/include/vote_manager/vote_manager.hpp" #include "network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp" +#include "vote_manager/vote_manager.hpp" namespace taraxa::network::tarcap::v1 { diff --git a/tests/test_util/include/test_util/node_dag_creation_fixture.hpp b/tests/test_util/include/test_util/node_dag_creation_fixture.hpp index e88b03e332..d8b4d34b7d 100644 --- a/tests/test_util/include/test_util/node_dag_creation_fixture.hpp +++ b/tests/test_util/include/test_util/node_dag_creation_fixture.hpp @@ -1,4 +1,5 @@ -#include "../../gtest.hpp" +#include + #include "dag/dag.hpp" #include "dag/dag_block_proposer.hpp" #include "dag/dag_manager.hpp" From 48edb9f5521f8025c6549ea8dd774a63ef12f917 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Tue, 9 May 2023 18:55:56 -0700 Subject: [PATCH 047/134] fix packets handlers inheritance issues --- .../latest/dag_block_packet_handler.hpp | 7 +- .../latest/dag_sync_packet_handler.hpp | 7 +- .../latest/get_dag_sync_packet_handler.hpp | 7 +- .../get_next_votes_sync_packet_handler.hpp | 6 +- .../latest/get_pbft_sync_packet_handler.hpp | 13 +- .../latest/pbft_sync_packet_handler.hpp | 11 +- .../latest/status_packet_handler.hpp | 7 +- .../latest/transaction_packet_handler.hpp | 7 +- .../latest/vote_packet_handler.hpp | 8 +- .../latest/votes_bundle_packet_handler.hpp | 6 +- .../v1/common/ext_votes_packet_handler.hpp | 31 -- .../v1/get_next_votes_sync_packet_handler.hpp | 23 +- .../v1/get_pbft_sync_packet_handler.hpp | 36 +-- .../v1/pbft_sync_packet_handler.hpp | 33 +- .../v1/votes_bundle_packet_handler.hpp | 24 +- .../common/ext_votes_packet_handler.cpp | 2 +- .../latest/pbft_sync_packet_handler.cpp | 12 +- .../v1/common/ext_votes_packet_handler.cpp | 46 --- .../v1/get_next_votes_sync_packet_handler.cpp | 79 +---- .../v1/get_pbft_sync_packet_handler.cpp | 102 ++---- .../v1/pbft_sync_packet_handler.cpp | 291 +----------------- .../src/tarcap/packets_handlers/v1/readme.md | 6 + .../v1/votes_bundle_packet_handler.cpp | 42 ++- .../pbft_block/include/pbft/period_data.hpp | 2 +- .../types/pbft_block/src/period_data.cpp | 2 +- 25 files changed, 163 insertions(+), 647 deletions(-) delete mode 100644 libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.hpp delete mode 100644 libraries/core_libs/network/src/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.cpp create mode 100644 libraries/core_libs/network/src/tarcap/packets_handlers/v1/readme.md diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp index b919c563b2..c296155a4b 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp @@ -10,7 +10,7 @@ namespace taraxa::network::tarcap { class TestState; -class DagBlockPacketHandler final : public ExtSyncingPacketHandler { +class DagBlockPacketHandler : public ExtSyncingPacketHandler { public: DagBlockPacketHandler(const FullNodeConfig &conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, @@ -27,9 +27,10 @@ class DagBlockPacketHandler final : public ExtSyncingPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::DagBlockPacket; private: - void validatePacketRlpFormat(const threadpool::PacketData &packet_data) const override; - void process(const threadpool::PacketData &packet_data, const std::shared_ptr &peer) override; + virtual void validatePacketRlpFormat(const threadpool::PacketData &packet_data) const override; + virtual void process(const threadpool::PacketData &packet_data, const std::shared_ptr &peer) override; + protected: std::shared_ptr test_state_; std::shared_ptr trx_mgr_{nullptr}; }; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp index 62f5b5b975..4bc665a4d6 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp @@ -8,7 +8,7 @@ class TransactionManager; namespace taraxa::network::tarcap { -class DagSyncPacketHandler final : public ExtSyncingPacketHandler { +class DagSyncPacketHandler : public ExtSyncingPacketHandler { public: DagSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, @@ -21,9 +21,10 @@ class DagSyncPacketHandler final : public ExtSyncingPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::DagSyncPacket; private: - void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; - void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + protected: std::shared_ptr trx_mgr_{nullptr}; }; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp index 6c098f533a..4cfa9be4b0 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp @@ -10,7 +10,7 @@ class TransactionManager; namespace taraxa::network::tarcap { -class GetDagSyncPacketHandler final : public PacketHandler { +class GetDagSyncPacketHandler : public PacketHandler { public: GetDagSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, @@ -24,9 +24,10 @@ class GetDagSyncPacketHandler final : public PacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetDagSyncPacket; private: - void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; - void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + protected: std::shared_ptr trx_mgr_; std::shared_ptr dag_mgr_; std::shared_ptr db_; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp index 8435b8016b..076eded03f 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp @@ -9,7 +9,7 @@ class VoteManager; namespace taraxa::network::tarcap { -class GetNextVotesBundlePacketHandler final : public ExtVotesPacketHandler { +class GetNextVotesBundlePacketHandler : public ExtVotesPacketHandler { public: GetNextVotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, @@ -20,8 +20,8 @@ class GetNextVotesBundlePacketHandler final : public ExtVotesPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetNextVotesSyncPacket; private: - void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; - void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; }; } // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp index e616cd8109..14af2ecc00 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp @@ -12,7 +12,7 @@ namespace taraxa::network::tarcap { class PbftSyncingState; -class GetPbftSyncPacketHandler final : public PacketHandler { +class GetPbftSyncPacketHandler : public PacketHandler { public: GetPbftSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, @@ -20,16 +20,17 @@ class GetPbftSyncPacketHandler final : public PacketHandler { std::shared_ptr vote_mgr, std::shared_ptr db, const addr_t& node_addr); - void sendPbftBlocks(const std::shared_ptr& peer, PbftPeriod from_period, size_t blocks_to_transfer, - bool pbft_chain_synced); - // Packet type that is processed by this handler static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetPbftSyncPacket; private: - void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; - void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + + virtual void sendPbftBlocks(dev::p2p::NodeID const& peer_id, PbftPeriod from_period, size_t blocks_to_transfer, + bool pbft_chain_synced); + protected: std::shared_ptr pbft_syncing_state_; std::shared_ptr pbft_chain_; std::shared_ptr vote_mgr_; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp index 80abff0088..dcbce7a7bd 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp @@ -5,7 +5,7 @@ namespace taraxa::network::tarcap { -class PbftSyncPacketHandler final : public ExtSyncingPacketHandler { +class PbftSyncPacketHandler : public ExtSyncingPacketHandler { public: PbftSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, @@ -19,14 +19,17 @@ class PbftSyncPacketHandler final : public ExtSyncingPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::PbftSyncPacket; private: - void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; - void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + + protected: + virtual PeriodData decodePeriodData(const dev::RLP& period_data_rlp) const; + virtual std::vector> decodeVotesBundle(const dev::RLP& votes_bundle_rlp) const; void pbftSyncComplete(); void delayedPbftSync(int counter); std::shared_ptr vote_mgr_; - util::ThreadPool periodic_events_tp_; static constexpr size_t kStandardPacketSize = 2; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp index 2d2b7b9913..e2ce7e5ee6 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp @@ -4,7 +4,7 @@ namespace taraxa::network::tarcap { -class StatusPacketHandler final : public ExtSyncingPacketHandler { +class StatusPacketHandler : public ExtSyncingPacketHandler { public: StatusPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, @@ -19,9 +19,10 @@ class StatusPacketHandler final : public ExtSyncingPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::StatusPacket; private: - void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; - void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + protected: static constexpr uint16_t kInitialStatusPacketItemsCount = 11; static constexpr uint16_t kStandardStatusPacketItemsCount = 4; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp index 2c94e365e3..3d49d13394 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp @@ -13,7 +13,7 @@ namespace taraxa::network::tarcap { class TestState; -class TransactionPacketHandler final : public PacketHandler { +class TransactionPacketHandler : public PacketHandler { public: TransactionPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, @@ -47,9 +47,10 @@ class TransactionPacketHandler final : public PacketHandler { void onNewTransactions(const SharedTransactions& transactions); private: - void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; - void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + protected: std::shared_ptr trx_mgr_; // FOR TESTING ONLY diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp index 9b3511e65c..2f875799c7 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp @@ -4,7 +4,7 @@ namespace taraxa::network::tarcap { -class VotePacketHandler final : public ExtVotesPacketHandler { +class VotePacketHandler : public ExtVotesPacketHandler { public: VotePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, @@ -27,10 +27,10 @@ class VotePacketHandler final : public ExtVotesPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::VotePacket; private: - void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; - void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; - private: + protected: const size_t kVotePacketSize{1}; const size_t kExtendedVotePacketSize{3}; }; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp index 92991b3ebd..03f7f8dbb4 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp @@ -4,7 +4,7 @@ namespace taraxa::network::tarcap { -class VotesBundlePacketHandler final : public ExtVotesPacketHandler { +class VotesBundlePacketHandler : public ExtVotesPacketHandler { public: VotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, @@ -25,8 +25,8 @@ class VotesBundlePacketHandler final : public ExtVotesPacketHandler { static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::VotesBundlePacket; private: - void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; - void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; + virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; }; } // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.hpp deleted file mode 100644 index d43a0e70aa..0000000000 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include "network/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.hpp" - -namespace taraxa::network::tarcap::v1 { - -/** - * @brief ExtVotesPacketHandler is extended abstract PacketHandler with added functions that are used in packet - * handlers that process pbft votes - */ -class ExtVotesPacketHandler : public tarcap::ExtVotesPacketHandler { - public: - // TODO: can be used this instead of ctor copy/paste ? - // using tarcap::ExtVotesPacketHandler::TaraxaCapability; - ExtVotesPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t& node_addr, - const std::string& log_channel_name); - - virtual ~ExtVotesPacketHandler() = default; - ExtVotesPacketHandler(const ExtVotesPacketHandler&) = delete; - ExtVotesPacketHandler(ExtVotesPacketHandler&&) = delete; - ExtVotesPacketHandler& operator=(const ExtVotesPacketHandler&) = delete; - ExtVotesPacketHandler& operator=(ExtVotesPacketHandler&&) = delete; - - void sendPbftVotesBundle(const std::shared_ptr& peer, - std::vector>&& votes) override; -}; - -} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.hpp index 8376e890d5..1d55226777 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.hpp @@ -1,27 +1,16 @@ #pragma once -#include "common/ext_votes_packet_handler.hpp" - -namespace taraxa { -class PbftManager; -class VoteManager; -} // namespace taraxa +#include "network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp" namespace taraxa::network::tarcap::v1 { -class GetNextVotesBundlePacketHandler final : public ExtVotesPacketHandler { +// V1 packets handlers must be derived from latest packets handlers otherwise netowrk class might not work properly ! +class GetNextVotesBundlePacketHandler final : public tarcap::GetNextVotesBundlePacketHandler { public: - GetNextVotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t& node_addr); - - // Packet type that is processed by this handler - static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetNextVotesSyncPacket; + using tarcap::GetNextVotesBundlePacketHandler::GetNextVotesBundlePacketHandler; - private: - void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; - void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; + void sendPbftVotesBundle(const std::shared_ptr& peer, + std::vector>&& votes) override; }; } // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp index 01a0f786a8..885b62db65 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp @@ -1,41 +1,17 @@ #pragma once -#include "network/tarcap/packets_handlers/latest/common/packet_handler.hpp" - -namespace taraxa { -class PbftChain; -class DbStorage; -class VoteManager; -} // namespace taraxa - -namespace taraxa::network::tarcap { -class PbftSyncingState; -} +#include "network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp" namespace taraxa::network::tarcap::v1 { -class GetPbftSyncPacketHandler final : public tarcap::PacketHandler { +// V1 packets handlers must be derived from latest packets handlers otherwise netowrk class might not work properly ! +class GetPbftSyncPacketHandler final : public tarcap::GetPbftSyncPacketHandler { public: - GetPbftSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_syncing_state, - std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - std::shared_ptr db, const addr_t& node_addr); - - void sendPbftBlocks(dev::p2p::NodeID const& peer_id, PbftPeriod from_period, size_t blocks_to_transfer, - bool pbft_chain_synced); - - // Packet type that is processed by this handler - static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetPbftSyncPacket; + using tarcap::GetPbftSyncPacketHandler::GetPbftSyncPacketHandler; private: - void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; - void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; - - std::shared_ptr pbft_syncing_state_; - std::shared_ptr pbft_chain_; - std::shared_ptr vote_mgr_; - std::shared_ptr db_; + void sendPbftBlocks(dev::p2p::NodeID const& peer_id, PbftPeriod from_period, size_t blocks_to_transfer, + bool pbft_chain_synced) override; }; } // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp index 906369f57d..92464e4c30 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp @@ -1,36 +1,17 @@ #pragma once -#include "network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp" -#include "vote_manager/vote_manager.hpp" +#include "network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp" namespace taraxa::network::tarcap::v1 { -class PbftSyncPacketHandler final : public tarcap::ExtSyncingPacketHandler { +// V1 packets handlers must be derived from latest packets handlers otherwise netowrk class might not work properly ! +class PbftSyncPacketHandler final : public tarcap::PbftSyncPacketHandler { public: - PbftSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_syncing_state, - std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, - std::shared_ptr dag_mgr, std::shared_ptr vote_mgr, - std::shared_ptr db, const addr_t& node_addr); + using tarcap::PbftSyncPacketHandler::PbftSyncPacketHandler; - void handleMaliciousSyncPeer(dev::p2p::NodeID const& id); - - // Packet type that is processed by this handler - static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::PbftSyncPacket; - - private: - void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; - void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; - - void pbftSyncComplete(); - void delayedPbftSync(int counter); - - std::shared_ptr vote_mgr_; - util::ThreadPool periodic_events_tp_; - - static constexpr size_t kStandardPacketSize = 2; - static constexpr size_t kChainSyncedPacketSize = 3; + protected: + PeriodData decodePeriodData(const dev::RLP& period_data_rlp) const override; + std::vector> decodeVotesBundle(const dev::RLP& votes_bundle_rlp) const override; }; } // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/votes_bundle_packet_handler.hpp index 46e87a7783..d528775867 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/votes_bundle_packet_handler.hpp @@ -1,28 +1,16 @@ #pragma once -#include "common/ext_votes_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp" namespace taraxa::network::tarcap::v1 { -class VotesBundlePacketHandler final : public ExtVotesPacketHandler { +// V1 packets handlers must be derived from latest packets handlers otherwise netowrk class might not work properly ! +class VotesBundlePacketHandler final : public tarcap::VotesBundlePacketHandler { public: - VotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t& node_addr); + using tarcap::VotesBundlePacketHandler::VotesBundlePacketHandler; - /** - * @brief Sends pbft votes bundle to connected peers - * - * @param votes Votes to send - * @param rebroadcast if rebroadcast is true, all votes are resent to all peers - * @param exclude_node do not send votes to excluded node - */ - void onNewPbftVotesBundle(const std::vector>& votes, bool rebroadcast = false, - const std::optional& exclude_node = {}); - - // Packet type that is processed by this handler - static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::VotesBundlePacket; + void sendPbftVotesBundle(const std::shared_ptr& peer, + std::vector>&& votes) override; private: void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp index 34c0b1359f..b8dd0c2f64 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp @@ -188,7 +188,7 @@ void ExtVotesPacketHandler::sendPbftVotesBundle(const std::shared_ptrgetId(), SubprotocolPacketType::VotesBundlePacket, std::move(votes_rlp_stream))) { - LOG(log_dg_) << " Votes bundle with " << votes.size() << " sent to " << peer->getId(); + LOG(log_dg_) << " Votes bundle with " << votes.size() << " votes sent to " << peer->getId(); for (const auto &vote : votes) { peer->markVoteAsKnown(vote->getHash()); } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp index e68bd655b8..9b9ebb7a01 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp @@ -59,14 +59,14 @@ void PbftSyncPacketHandler::process(const threadpool::PacketData &packet_data, const bool last_block = packet_data.rlp_[0].toInt(); PeriodData period_data; try { - period_data = PeriodData(packet_data.rlp_[1]); + period_data = decodePeriodData(packet_data.rlp_[1]); } catch (const std::runtime_error &e) { throw MaliciousPeerException("Unable to parse PeriodData: " + std::string(e.what())); } std::vector> current_block_cert_votes; if (pbft_chain_synced) { - current_block_cert_votes = decodeVotesBundleRlp(packet_data.rlp_[2]); + current_block_cert_votes = decodeVotesBundle(packet_data.rlp_[2]); } const auto pbft_blk_hash = period_data.pbft_blk->getBlockHash(); @@ -218,6 +218,14 @@ void PbftSyncPacketHandler::process(const threadpool::PacketData &packet_data, } } +PeriodData PbftSyncPacketHandler::decodePeriodData(const dev::RLP &period_data_rlp) const { + return PeriodData(period_data_rlp); +} + +std::vector> PbftSyncPacketHandler::decodeVotesBundle(const dev::RLP &votes_bundle_rlp) const { + return decodeVotesBundleRlp(votes_bundle_rlp); +} + void PbftSyncPacketHandler::pbftSyncComplete() { if (pbft_mgr_->periodDataQueueSize()) { LOG(log_tr_) << "Syncing pbft blocks faster than processing. Remaining sync size " diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.cpp deleted file mode 100644 index 4b59f5c508..0000000000 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "network/tarcap/packets_handlers/v1/common/ext_votes_packet_handler.hpp" - -#include "pbft/pbft_manager.hpp" -#include "vote/vote.hpp" -#include "vote_manager/vote_manager.hpp" - -namespace taraxa::network::tarcap::v1 { - -ExtVotesPacketHandler::ExtVotesPacketHandler(const FullNodeConfig &conf, - std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_mgr, - std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t &node_addr, - const std::string &log_channel_name) - : tarcap::ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), - std::move(pbft_chain), std::move(vote_mgr), node_addr, log_channel_name) {} - -void ExtVotesPacketHandler::sendPbftVotesBundle(const std::shared_ptr &peer, - std::vector> &&votes) { - if (votes.empty()) { - return; - } - - size_t index = 0; - while (index < votes.size()) { - const size_t count = std::min(static_cast(kMaxVotesInBundleRlp), votes.size() - index); - dev::RLPStream s(count); - for (auto i = index; i < index + count; i++) { - const auto &vote = votes[i]; - s.appendRaw(vote->rlp(true, false)); - LOG(log_dg_) << "Send vote " << vote->getHash() << " to peer " << peer->getId(); - } - - if (sealAndSend(peer->getId(), SubprotocolPacketType::VotesBundlePacket, std::move(s))) { - LOG(log_dg_) << count << " PBFT votes to were sent to " << peer->getId(); - for (auto i = index; i < index + count; i++) { - peer->markVoteAsKnown(votes[i]->getHash()); - } - } - - index += count; - } -} - -} // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.cpp index 21cb1155d5..19c9c9a539 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.cpp @@ -5,75 +5,30 @@ namespace taraxa::network::tarcap::v1 { -GetNextVotesBundlePacketHandler::GetNextVotesBundlePacketHandler( - const FullNodeConfig &conf, std::shared_ptr peers_state, - std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, - std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, const addr_t &node_addr) - : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), - std::move(pbft_chain), std::move(vote_mgr), node_addr, "V1_GET_NEXT_VOTES_SYNC_PH") {} - -void GetNextVotesBundlePacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { - if (constexpr size_t required_size = 2; packet_data.rlp_.itemCount() != required_size) { - throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); - } -} - -void GetNextVotesBundlePacketHandler::process(const threadpool::PacketData &packet_data, - const std::shared_ptr &peer) { - LOG(log_dg_) << "Received GetNextVotesSyncPacket request"; - - const PbftPeriod peer_pbft_period = packet_data.rlp_[0].toInt(); - const PbftRound peer_pbft_round = packet_data.rlp_[1].toInt(); - const auto [pbft_round, pbft_period] = pbft_mgr_->getPbftRoundAndPeriod(); - - // Send votes only for current_period == peer_period && current_period >= peer_round - if (pbft_period != peer_pbft_period || pbft_round == 1 || pbft_round < peer_pbft_round) { - LOG(log_nf_) << "No previous round next votes sync packet will be sent. pbft_period " << pbft_period - << ", peer_pbft_period " << peer_pbft_period << ", pbft_round " << pbft_round << ", peer_pbft_round " - << peer_pbft_round; +void GetNextVotesBundlePacketHandler::sendPbftVotesBundle(const std::shared_ptr &peer, + std::vector> &&votes) { + if (votes.empty()) { return; } - auto next_votes = - vote_mgr_->getTwoTPlusOneVotedBlockVotes(pbft_period, pbft_round - 1, TwoTPlusOneVotedBlockType::NextVotedBlock); - auto next_null_votes = vote_mgr_->getTwoTPlusOneVotedBlockVotes(pbft_period, pbft_round - 1, - TwoTPlusOneVotedBlockType::NextVotedNullBlock); - - // In edge case this could theoretically happen due to race condition when we moved to the next period or round - // right before calling getAllTwoTPlusOneNextVotes with specific period & round - if (next_votes.empty() && next_null_votes.empty()) { - // Try to get period & round values again - const auto [tmp_pbft_round, tmp_pbft_period] = pbft_mgr_->getPbftRoundAndPeriod(); - // No changes in period & round or new round == 1 - if (pbft_period == tmp_pbft_period && pbft_round == tmp_pbft_round) { - LOG(log_er_) << "No next votes returned for period " << tmp_pbft_period << ", round " << tmp_pbft_round - 1; - return; - } - - if (tmp_pbft_round == 1) { - LOG(log_wr_) << "No next votes returned for period " << tmp_pbft_period << ", round " << tmp_pbft_round - 1 - << " due to race condition - pbft already moved to the next period & round == 1"; - return; + size_t index = 0; + while (index < votes.size()) { + const size_t count = std::min(static_cast(kMaxVotesInBundleRlp), votes.size() - index); + dev::RLPStream s(count); + for (auto i = index; i < index + count; i++) { + const auto &vote = votes[i]; + s.appendRaw(vote->rlp(true, false)); + LOG(log_dg_) << "Send vote " << vote->getHash() << " to peer " << peer->getId(); } - next_votes = vote_mgr_->getTwoTPlusOneVotedBlockVotes(pbft_period, pbft_round - 1, - TwoTPlusOneVotedBlockType::NextVotedBlock); - next_null_votes = vote_mgr_->getTwoTPlusOneVotedBlockVotes(pbft_period, pbft_round - 1, - TwoTPlusOneVotedBlockType::NextVotedNullBlock); - if (next_votes.empty() && next_null_votes.empty()) { - LOG(log_er_) << "No next votes returned for period " << tmp_pbft_period << ", round " << tmp_pbft_round - 1; - return; + if (sealAndSend(peer->getId(), SubprotocolPacketType::VotesBundlePacket, std::move(s))) { + LOG(log_dg_) << count << " PBFT votes to were sent to " << peer->getId(); + for (auto i = index; i < index + count; i++) { + peer->markVoteAsKnown(votes[i]->getHash()); + } } - } - - if (!next_votes.empty()) { - LOG(log_nf_) << "Send next votes bundle with " << next_votes.size() << " votes to " << peer->getId(); - sendPbftVotesBundle(peer, std::move(next_votes)); - } - if (!next_null_votes.empty()) { - LOG(log_nf_) << "Send next null votes bundle with " << next_null_votes.size() << " votes to " << peer->getId(); - sendPbftVotesBundle(peer, std::move(next_null_votes)); + index += count; } } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp index 388ad35b11..829849927d 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp @@ -8,67 +8,34 @@ namespace taraxa::network::tarcap::v1 { -GetPbftSyncPacketHandler::GetPbftSyncPacketHandler(const FullNodeConfig &conf, - std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_syncing_state, - std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, std::shared_ptr db, - const addr_t &node_addr) - : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, "V1_GET_PBFT_SYNC_PH"), - pbft_syncing_state_(std::move(pbft_syncing_state)), - pbft_chain_(std::move(pbft_chain)), - vote_mgr_(std::move(vote_mgr)), - db_(std::move(db)) {} - -void GetPbftSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { - if (constexpr size_t required_size = 1; packet_data.rlp_.itemCount() != required_size) { - throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), required_size); - } -} - -void GetPbftSyncPacketHandler::process(const threadpool::PacketData &packet_data, - [[maybe_unused]] const std::shared_ptr &peer) { - LOG(log_tr_) << "Received GetPbftSyncPacket Block"; - - const size_t height_to_sync = packet_data.rlp_[0].toInt(); - // Here need PBFT chain size, not synced period since synced blocks has not verified yet. - const size_t my_chain_size = pbft_chain_->getPbftChainSize(); - if (height_to_sync > my_chain_size) { - // Node update peers PBFT chain size in status packet. Should not request syncing period bigger than pbft chain size - std::ostringstream err_msg; - err_msg << "Peer " << packet_data.from_node_id_ << " request syncing period start at " << height_to_sync - << ". That's bigger than own PBFT chain size " << my_chain_size; - throw MaliciousPeerException(err_msg.str()); - } - - if (kConf.is_light_node && height_to_sync + kConf.light_node_history <= my_chain_size) { - std::ostringstream err_msg; - err_msg << "Peer " << packet_data.from_node_id_ << " request syncing period start at " << height_to_sync - << ". Light node does not have the data " << my_chain_size; - throw MaliciousPeerException(err_msg.str()); - } - - size_t blocks_to_transfer = 0; - auto pbft_chain_synced = false; - const auto total_period_datas_size = my_chain_size - height_to_sync + 1; - if (total_period_datas_size <= kConf.network.sync_level_size) { - blocks_to_transfer = total_period_datas_size; - pbft_chain_synced = true; - } else { - blocks_to_transfer = kConf.network.sync_level_size; - } - LOG(log_tr_) << "Will send " << blocks_to_transfer << " PBFT blocks to " << packet_data.from_node_id_; - - sendPbftBlocks(packet_data.from_node_id_, height_to_sync, blocks_to_transfer, pbft_chain_synced); -} - -// api for pbft syncing void GetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, PbftPeriod from_period, size_t blocks_to_transfer, bool pbft_chain_synced) { LOG(log_tr_) << "sendPbftBlocks: peer want to sync from pbft chain height " << from_period << ", will send at most " << blocks_to_transfer << " pbft blocks to " << peer_id; + // Transform period data rlp from v2 to v1 + auto transformPeriodDataRlpToV1 = [](const dev::bytes &period_data_v2) -> dev::bytes { + // Create PeriodData old(v1) rlp format + PeriodData period_data(period_data_v2); + + dev::RLPStream period_data_rlp(PeriodData::kRlpItemCount); + period_data_rlp.appendRaw(period_data.pbft_blk->rlp(true)); + period_data_rlp.appendList(period_data.previous_block_cert_votes.size()); + for (auto const &v : period_data.previous_block_cert_votes) { + period_data_rlp.appendRaw(v->rlp(true)); + } + period_data_rlp.appendList(period_data.dag_blocks.size()); + for (auto const &b : period_data.dag_blocks) { + period_data_rlp.appendRaw(b.rlp(true)); + } + period_data_rlp.appendList(period_data.transactions.size()); + for (auto const &t : period_data.transactions) { + period_data_rlp.appendRaw(t->rlp()); + } + + return period_data_rlp.invalidate(); + }; + for (auto block_period = from_period; block_period < from_period + blocks_to_transfer; block_period++) { bool last_block = (block_period == from_period + blocks_to_transfer - 1); auto data = db_->getPeriodDataRaw(block_period); @@ -78,29 +45,6 @@ void GetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, P assert(false); } - // Transform period data rlp from v2 to v1 - auto transformPeriodDataRlpToV1 = [](const dev::bytes &period_data_v2) -> dev::bytes { - // Create PeriodData old(v1) rlp format - PeriodData period_data(period_data_v2); - - dev::RLPStream period_data_rlp(PeriodData::kRlpItemCount); - period_data_rlp.appendRaw(period_data.pbft_blk->rlp(true)); - period_data_rlp.appendList(period_data.previous_block_cert_votes.size()); - for (auto const &v : period_data.previous_block_cert_votes) { - period_data_rlp.appendRaw(v->rlp(true)); - } - period_data_rlp.appendList(period_data.dag_blocks.size()); - for (auto const &b : period_data.dag_blocks) { - period_data_rlp.appendRaw(b.rlp(true)); - } - period_data_rlp.appendList(period_data.transactions.size()); - for (auto const &t : period_data.transactions) { - period_data_rlp.appendRaw(t->rlp()); - } - - return period_data_rlp.invalidate(); - }; - dev::RLPStream s; if (pbft_chain_synced && last_block && block_period > 1) { s.appendList(3); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/pbft_sync_packet_handler.cpp index 4639286ca5..80bac3e977 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/pbft_sync_packet_handler.cpp @@ -8,291 +8,34 @@ namespace taraxa::network::tarcap::v1 { -PbftSyncPacketHandler::PbftSyncPacketHandler(const FullNodeConfig &conf, - std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_syncing_state, - std::shared_ptr pbft_chain, - std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, - std::shared_ptr vote_mgr, std::shared_ptr db, - const addr_t &node_addr) - : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), - std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, - "V1_PBFT_SYNC_PH"), - vote_mgr_(std::move(vote_mgr)), - periodic_events_tp_(1, true) {} +PeriodData PbftSyncPacketHandler::decodePeriodData(const dev::RLP& period_data_rlp) const { + auto it = period_data_rlp.begin(); -void PbftSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { - if (packet_data.rlp_.itemCount() != kStandardPacketSize && packet_data.rlp_.itemCount() != kChainSyncedPacketSize) { - throw InvalidRlpItemsCountException(packet_data.type_str_, packet_data.rlp_.itemCount(), kStandardPacketSize); - } - - // PeriodData rlp parsing cannot be done through util::rlp_tuple, which automatically checks the rlp size so it is - // checked here manually - if (packet_data.rlp_[1].itemCount() != PeriodData::kRlpItemCount) { - throw InvalidRlpItemsCountException(packet_data.type_str_ + ":PeriodData", packet_data.rlp_[1].itemCount(), - PeriodData::kRlpItemCount); - } -} - -void PbftSyncPacketHandler::process(const threadpool::PacketData &packet_data, - const std::shared_ptr &peer) { - // Note: no need to consider possible race conditions due to concurrent processing as it is - // disabled on priority_queue blocking dependencies level - const auto syncing_peer = pbft_syncing_state_->syncingPeer(); - if (!syncing_peer) { - LOG(log_wr_) << "PbftSyncPacket received from unexpected peer " << packet_data.from_node_id_.abridged() - << " but there is no current syncing peer set"; - return; - } - - if (syncing_peer->getId() != packet_data.from_node_id_) { - LOG(log_wr_) << "PbftSyncPacket received from unexpected peer " << packet_data.from_node_id_.abridged() - << " current syncing peer " << syncing_peer->getId().abridged(); - return; - } - - auto decodePeriodDataRlpV1 = [](const dev::RLP &period_data_v1) -> PeriodData { - PeriodData period_data; - auto it = period_data_v1.begin(); - period_data.pbft_blk = std::make_shared(*it++); - for (auto const vote_rlp : *it++) { - period_data.previous_block_cert_votes.emplace_back(std::make_shared(vote_rlp)); - } - - for (auto const dag_block_rlp : *it++) { - period_data.dag_blocks.emplace_back(dag_block_rlp); - } - - for (auto const trx_rlp : *it) { - period_data.transactions.emplace_back(std::make_shared(trx_rlp)); - } - - return period_data; - }; - - // Process received pbft blocks - // pbft_chain_synced is the flag to indicate own PBFT chain has synced with the peer's PBFT chain - const bool pbft_chain_synced = packet_data.rlp_.itemCount() == kChainSyncedPacketSize; - // last_block is the flag to indicate this is the last block in each syncing round, doesn't mean PBFT chain has synced - const bool last_block = packet_data.rlp_[0].toInt(); PeriodData period_data; - try { - period_data = decodePeriodDataRlpV1(packet_data.rlp_[1]); - } catch (const Transaction::InvalidTransaction &e) { - throw MaliciousPeerException("Unable to parse PeriodData: " + std::string(e.what())); - } - - std::vector> current_block_cert_votes; - if (pbft_chain_synced) { - const auto cert_votes_count = packet_data.rlp_[2].itemCount(); - current_block_cert_votes.reserve(cert_votes_count); - for (size_t i = 0; i < cert_votes_count; i++) { - current_block_cert_votes.emplace_back(std::make_shared(packet_data.rlp_[2][i].data().toBytes())); - } - } - const auto pbft_blk_hash = period_data.pbft_blk->getBlockHash(); - - std::string received_dag_blocks_str; // This is just log related stuff - for (auto const &block : period_data.dag_blocks) { - received_dag_blocks_str += block.getHash().toString() + " "; - if (peer->dag_level_ < block.getLevel()) { - peer->dag_level_ = block.getLevel(); - } - } - const auto pbft_block_period = period_data.pbft_blk->getPeriod(); - - LOG(log_dg_) << "PbftSyncPacket received. Period: " << pbft_block_period - << ", dag Blocks: " << received_dag_blocks_str << " from " << packet_data.from_node_id_; - - peer->markPbftBlockAsKnown(pbft_blk_hash); - // Update peer's pbft period if outdated - if (peer->pbft_chain_size_ < pbft_block_period) { - peer->pbft_chain_size_ = pbft_block_period; - } - - LOG(log_tr_) << "Processing pbft block: " << pbft_blk_hash; - - if (pbft_chain_->findPbftBlockInChain(pbft_blk_hash)) { - LOG(log_wr_) << "PBFT block " << pbft_blk_hash << ", period: " << period_data.pbft_blk->getPeriod() << " from " - << packet_data.from_node_id_ << " already present in chain"; - } else { - if (pbft_block_period != pbft_mgr_->pbftSyncingPeriod() + 1) { - LOG(log_wr_) << "Block " << pbft_blk_hash << " period unexpected: " << pbft_block_period - << ". Expected period: " << pbft_mgr_->pbftSyncingPeriod() + 1; - restartSyncingPbft(true); - return; - } - - // Check cert vote matches if final synced block - if (pbft_chain_synced) { - for (auto const &vote : current_block_cert_votes) { - if (vote->getBlockHash() != pbft_blk_hash) { - LOG(log_er_) << "Invalid cert votes block hash " << vote->getBlockHash() << " instead of " << pbft_blk_hash - << " from peer " << packet_data.from_node_id_.abridged() << " received, stop syncing."; - handleMaliciousSyncPeer(packet_data.from_node_id_); - return; - } - } - } - - // Check votes match the hash of previous block in the queue - auto last_pbft_block_hash = pbft_mgr_->lastPbftBlockHashFromQueueOrChain(); - // Check cert vote matches - for (auto const &vote : period_data.previous_block_cert_votes) { - if (vote->getBlockHash() != last_pbft_block_hash) { - LOG(log_er_) << "Invalid cert votes block hash " << vote->getBlockHash() << " instead of " - << last_pbft_block_hash << " from peer " << packet_data.from_node_id_.abridged() - << " received, stop syncing."; - handleMaliciousSyncPeer(packet_data.from_node_id_); - return; - } - } - - auto order_hash = PbftManager::calculateOrderHash(period_data.dag_blocks); - if (order_hash != period_data.pbft_blk->getOrderHash()) { - { // This is just log related stuff - std::vector trx_order; - trx_order.reserve(period_data.transactions.size()); - std::vector blk_order; - blk_order.reserve(period_data.dag_blocks.size()); - for (auto t : period_data.transactions) { - trx_order.push_back(t->getHash()); - } - for (auto b : period_data.dag_blocks) { - blk_order.push_back(b.getHash()); - } - LOG(log_er_) << "Order hash incorrect in period data " << pbft_blk_hash << " expected: " << order_hash - << " received " << period_data.pbft_blk->getOrderHash() << "; Dag order: " << blk_order - << "; Trx order: " << trx_order << "; from " << packet_data.from_node_id_.abridged() - << ", stop syncing."; - } - handleMaliciousSyncPeer(packet_data.from_node_id_); - return; - } - - // This is special case when queue is empty and we can not say for sure that all votes that are part of this block - // have been verified before - if (pbft_mgr_->periodDataQueueEmpty()) { - for (const auto &v : period_data.previous_block_cert_votes) { - if (auto vote_is_valid = vote_mgr_->validateVote(v); vote_is_valid.first == false) { - LOG(log_er_) << "Invalid reward votes in block " << period_data.pbft_blk->getBlockHash() << " from peer " - << packet_data.from_node_id_.abridged() - << " received, stop syncing. Validation failed. Err: " << vote_is_valid.second; - handleMaliciousSyncPeer(packet_data.from_node_id_); - return; - } - - vote_mgr_->addVerifiedVote(v); - } - - // And now we need to replace it with verified votes - if (auto votes = vote_mgr_->checkRewardVotes(period_data.pbft_blk, true); votes.first) { - period_data.previous_block_cert_votes = std::move(votes.second); - } else { - // checkRewardVotes could fail because we just cert voted this block and moved to next period, in that case we - // might even be fully synced so call restartSyncingPbft to verify - if (pbft_block_period <= vote_mgr_->getRewardVotesPbftBlockPeriod()) { - restartSyncingPbft(true); - return; - } - - LOG(log_er_) << "Invalid reward votes in block " << period_data.pbft_blk->getBlockHash() << " from peer " - << packet_data.from_node_id_.abridged() << " received, stop syncing."; - handleMaliciousSyncPeer(packet_data.from_node_id_); - return; - } - } - - LOG(log_tr_) << "Synced PBFT block hash " << pbft_blk_hash << " with " - << period_data.previous_block_cert_votes.size() << " cert votes"; - LOG(log_tr_) << "Synced PBFT block " << period_data; - pbft_mgr_->periodDataQueuePush(std::move(period_data), packet_data.from_node_id_, - std::move(current_block_cert_votes)); + period_data.pbft_blk = std::make_shared(*it++); + for (auto const vote_rlp : *it++) { + period_data.previous_block_cert_votes.emplace_back(std::make_shared(vote_rlp)); } - - auto pbft_sync_period = pbft_mgr_->pbftSyncingPeriod(); - - // Reset last sync packet received time - pbft_syncing_state_->setLastSyncPacketTime(); - - if (pbft_chain_synced) { - pbftSyncComplete(); - return; - } - - if (last_block) { - // If current sync period is actually bigger than the block we just received we are probably synced but verify with - // calling restartSyncingPbft - if (pbft_sync_period > pbft_block_period) { - return restartSyncingPbft(true); - } - if (pbft_syncing_state_->isPbftSyncing()) { - if (pbft_sync_period > pbft_chain_->getPbftChainSize() + (10 * kConf.network.sync_level_size)) { - LOG(log_tr_) << "Syncing pbft blocks too fast than processing. Has synced period " << pbft_sync_period - << ", PBFT chain size " << pbft_chain_->getPbftChainSize(); - periodic_events_tp_.post(1000, [this] { delayedPbftSync(1); }); - } else { - if (!syncPeerPbft(pbft_sync_period + 1, true)) { - return restartSyncingPbft(true); - } - } - } - } -} - -void PbftSyncPacketHandler::pbftSyncComplete() { - if (pbft_mgr_->periodDataQueueSize()) { - LOG(log_tr_) << "Syncing pbft blocks faster than processing. Remaining sync size " - << pbft_mgr_->periodDataQueueSize(); - periodic_events_tp_.post(1000, [this] { pbftSyncComplete(); }); - } else { - LOG(log_dg_) << "Syncing PBFT is completed"; - // We are pbft synced with the node we are connected to but - // calling restartSyncingPbft will check if some nodes have - // greater pbft chain size and we should continue syncing with - // them, Or sync pending DAG blocks - restartSyncingPbft(true); - if (!pbft_syncing_state_->isPbftSyncing()) { - requestPendingDagBlocks(); - } + for (auto const dag_block_rlp : *it++) { + period_data.dag_blocks.emplace_back(dag_block_rlp); } -} - -void PbftSyncPacketHandler::delayedPbftSync(int counter) { - auto pbft_sync_period = pbft_mgr_->pbftSyncingPeriod(); - if (counter > 60) { - LOG(log_er_) << "Pbft blocks stuck in queue, no new block processed in 60 seconds " << pbft_sync_period << " " - << pbft_chain_->getPbftChainSize(); - pbft_syncing_state_->setPbftSyncing(false); - LOG(log_tr_) << "Syncing PBFT is stopping"; - return; + for (auto const trx_rlp : *it) { + period_data.transactions.emplace_back(std::make_shared(trx_rlp)); } - if (pbft_syncing_state_->isPbftSyncing()) { - if (pbft_sync_period > pbft_chain_->getPbftChainSize() + (10 * kConf.network.sync_level_size)) { - LOG(log_tr_) << "Syncing pbft blocks faster than processing " << pbft_sync_period << " " - << pbft_chain_->getPbftChainSize(); - periodic_events_tp_.post(1000, [this, counter] { delayedPbftSync(counter + 1); }); - } else { - if (!syncPeerPbft(pbft_sync_period + 1)) { - return restartSyncingPbft(true); - } - } - } + return period_data; } -void PbftSyncPacketHandler::handleMaliciousSyncPeer(dev::p2p::NodeID const &id) { - peers_state_->set_peer_malicious(id); +std::vector> PbftSyncPacketHandler::decodeVotesBundle(const dev::RLP& votes_bundle_rlp) const { + std::vector> votes; + const auto cert_votes_count = votes_bundle_rlp.itemCount(); + votes.reserve(cert_votes_count); - if (auto host = peers_state_->host_.lock(); host) { - LOG(log_nf_) << "Disconnect peer " << id; - host->disconnect(id, dev::p2p::UserReason); - } else { - LOG(log_er_) << "Unable to handleMaliciousSyncPeer, host == nullptr"; + for (size_t i = 0; i < cert_votes_count; i++) { + votes.emplace_back(std::make_shared(votes_bundle_rlp[i].data().toBytes())); } - restartSyncingPbft(true); + return votes; } } // namespace taraxa::network::tarcap::v1 diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/readme.md b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/readme.md new file mode 100644 index 0000000000..3872e21f26 --- /dev/null +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/readme.md @@ -0,0 +1,6 @@ +### Multiple taraxa capabilities support +- Derive new packet handlers with different logic than the original ones. +- +`!!! Important:` These handlers must be +directly on indirectly derived from the latest packets handlers, which are inside +`network/tarcap/packets_handlers/latest/` folder, otherwise network class would not work properly diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/votes_bundle_packet_handler.cpp index a9cc3fd996..99a5f8dc70 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/votes_bundle_packet_handler.cpp @@ -5,15 +5,6 @@ namespace taraxa::network::tarcap::v1 { -VotesBundlePacketHandler::VotesBundlePacketHandler(const FullNodeConfig &conf, - std::shared_ptr peers_state, - std::shared_ptr packets_stats, - std::shared_ptr pbft_mgr, - std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t &node_addr) - : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), - std::move(pbft_chain), std::move(vote_mgr), node_addr, "V1_VOTES_SYNC_PH") {} - void VotesBundlePacketHandler::validatePacketRlpFormat( [[maybe_unused]] const threadpool::PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); @@ -135,27 +126,30 @@ void VotesBundlePacketHandler::process(const threadpool::PacketData &packet_data onNewPbftVotesBundle(votes, false, packet_data.from_node_id_); } -void VotesBundlePacketHandler::onNewPbftVotesBundle(const std::vector> &votes, bool rebroadcast, - const std::optional &exclude_node) { - for (const auto &peer : peers_state_->getAllPeers()) { - if (peer.second->syncing_) { - continue; - } +void VotesBundlePacketHandler::sendPbftVotesBundle(const std::shared_ptr &peer, + std::vector> &&votes) { + if (votes.empty()) { + return; + } - if (exclude_node.has_value() && *exclude_node == peer.first) { - continue; + size_t index = 0; + while (index < votes.size()) { + const size_t count = std::min(static_cast(kMaxVotesInBundleRlp), votes.size() - index); + dev::RLPStream s(count); + for (auto i = index; i < index + count; i++) { + const auto &vote = votes[i]; + s.appendRaw(vote->rlp(true, false)); + LOG(log_dg_) << "Send vote " << vote->getHash() << " to peer " << peer->getId(); } - std::vector> peer_votes; - for (const auto &vote : votes) { - if (!rebroadcast && peer.second->isVoteKnown(vote->getHash())) { - continue; + if (sealAndSend(peer->getId(), SubprotocolPacketType::VotesBundlePacket, std::move(s))) { + LOG(log_dg_) << count << " PBFT votes to were sent to " << peer->getId(); + for (auto i = index; i < index + count; i++) { + peer->markVoteAsKnown(votes[i]->getHash()); } - - peer_votes.push_back(vote); } - sendPbftVotesBundle(peer.second, std::move(peer_votes)); + index += count; } } diff --git a/libraries/types/pbft_block/include/pbft/period_data.hpp b/libraries/types/pbft_block/include/pbft/period_data.hpp index 6ce782765f..d4e95d733a 100644 --- a/libraries/types/pbft_block/include/pbft/period_data.hpp +++ b/libraries/types/pbft_block/include/pbft/period_data.hpp @@ -25,7 +25,7 @@ class PeriodData { public: PeriodData() = default; PeriodData(std::shared_ptr pbft_blk, const std::vector>& previous_block_cert_votes); - explicit PeriodData(dev::RLP&& all_rlp); + explicit PeriodData(const dev::RLP& all_rlp); explicit PeriodData(bytes const& all_rlp); std::shared_ptr pbft_blk; diff --git a/libraries/types/pbft_block/src/period_data.cpp b/libraries/types/pbft_block/src/period_data.cpp index 3850f8b7e2..b8fcdf6b66 100644 --- a/libraries/types/pbft_block/src/period_data.cpp +++ b/libraries/types/pbft_block/src/period_data.cpp @@ -16,7 +16,7 @@ PeriodData::PeriodData(std::shared_ptr pbft_blk, const std::vector>& previous_block_cert_votes) : pbft_blk(std::move(pbft_blk)), previous_block_cert_votes(previous_block_cert_votes) {} -PeriodData::PeriodData(dev::RLP&& rlp) { +PeriodData::PeriodData(const dev::RLP& rlp) { auto it = rlp.begin(); pbft_blk = std::make_shared(*it++); From a608cf18359e56829903fe8c562622b1d0e3b31e Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Thu, 11 May 2023 11:33:21 -0700 Subject: [PATCH 048/134] different versions of handlers have different log channels names --- .../latest/dag_block_packet_handler.hpp | 3 ++- .../latest/dag_sync_packet_handler.hpp | 2 +- .../latest/get_dag_sync_packet_handler.hpp | 3 ++- ...=> get_next_votes_bundle_packet_handler.hpp} | 3 ++- .../latest/get_pbft_sync_packet_handler.hpp | 2 +- .../latest/pbft_sync_packet_handler.hpp | 3 ++- .../latest/status_packet_handler.hpp | 3 ++- .../latest/transaction_packet_handler.hpp | 2 +- .../latest/vote_packet_handler.hpp | 2 +- .../latest/votes_bundle_packet_handler.hpp | 2 +- ...=> get_next_votes_bundle_packet_handler.hpp} | 4 ++-- .../v1/get_pbft_sync_packet_handler.hpp | 2 +- .../v1/init_packets_handlers.hpp | 17 +++++++++-------- .../v1/pbft_sync_packet_handler.hpp | 2 +- .../v1/votes_bundle_packet_handler.hpp | 2 +- .../latest/dag_block_packet_handler.cpp | 5 +++-- .../latest/dag_sync_packet_handler.cpp | 4 ++-- .../latest/get_dag_sync_packet_handler.cpp | 4 ++-- ...=> get_next_votes_bundle_packet_handler.cpp} | 7 ++++--- .../latest/get_pbft_sync_packet_handler.cpp | 4 ++-- .../latest/pbft_sync_packet_handler.cpp | 4 ++-- .../latest/status_packet_handler.cpp | 5 +++-- .../latest/transaction_packet_handler.cpp | 5 +++-- .../latest/vote_packet_handler.cpp | 5 +++-- .../latest/votes_bundle_packet_handler.cpp | 5 +++-- ...=> get_next_votes_bundle_packet_handler.cpp} | 2 +- .../network/src/tarcap/taraxa_capability.cpp | 2 +- tests/network_test.cpp | 2 +- 28 files changed, 59 insertions(+), 47 deletions(-) rename libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/{get_next_votes_sync_packet_handler.hpp => get_next_votes_bundle_packet_handler.hpp} (88%) rename libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/{get_next_votes_sync_packet_handler.hpp => get_next_votes_bundle_packet_handler.hpp} (87%) rename libraries/core_libs/network/src/tarcap/packets_handlers/latest/{get_next_votes_sync_packet_handler.cpp => get_next_votes_bundle_packet_handler.cpp} (96%) rename libraries/core_libs/network/src/tarcap/packets_handlers/v1/{get_next_votes_sync_packet_handler.cpp => get_next_votes_bundle_packet_handler.cpp} (92%) diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp index c296155a4b..46e3cc1948 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp @@ -17,7 +17,8 @@ class DagBlockPacketHandler : public ExtSyncingPacketHandler { std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, std::shared_ptr db, - std::shared_ptr test_state, const addr_t &node_addr); + std::shared_ptr test_state, const addr_t &node_addr, + const std::string &log_channel_name = "DAG_BLOCK_PH"); void sendBlock(dev::p2p::NodeID const &peer_id, DagBlock block, const SharedTransactions &trxs); void onNewBlockReceived(DagBlock &&block, const std::shared_ptr &peer = nullptr); diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp index 4bc665a4d6..d5ccb69de1 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp @@ -15,7 +15,7 @@ class DagSyncPacketHandler : public ExtSyncingPacketHandler { std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, std::shared_ptr db, - const addr_t& node_addr); + const addr_t& node_addr, const std::string& log_channel_name = "DAG_SYNC_PH"); // Packet type that is processed by this handler static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::DagSyncPacket; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp index 4cfa9be4b0..516f2778f7 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp @@ -15,7 +15,8 @@ class GetDagSyncPacketHandler : public PacketHandler { GetDagSyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr trx_mgr, std::shared_ptr dag_mgr, - std::shared_ptr db, const addr_t& node_addr); + std::shared_ptr db, const addr_t& node_addr, + const std::string& log_channel_name = "GET_DAG_SYNC_PH"); void sendBlocks(const dev::p2p::NodeID& peer_id, std::vector>&& blocks, SharedTransactions&& transactions, PbftPeriod request_period, PbftPeriod period); diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp similarity index 88% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp index 076eded03f..7189c94e48 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp @@ -14,7 +14,8 @@ class GetNextVotesBundlePacketHandler : public ExtVotesPacketHandler { GetNextVotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t& node_addr); + std::shared_ptr vote_mgr, const addr_t& node_addr, + const std::string& log_channel_name = "GET_NEXT_VOTES_BUNDLE_PH"); // Packet type that is processed by this handler static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetNextVotesSyncPacket; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp index 14af2ecc00..c97b5a7da6 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp @@ -18,7 +18,7 @@ class GetPbftSyncPacketHandler : public PacketHandler { std::shared_ptr packets_stats, std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr db, - const addr_t& node_addr); + const addr_t& node_addr, const std::string& log_channel_name = "GET_PBFT_SYNC_PH"); // Packet type that is processed by this handler static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetPbftSyncPacket; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp index dcbce7a7bd..29b33fa05f 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp @@ -11,7 +11,8 @@ class PbftSyncPacketHandler : public ExtSyncingPacketHandler { std::shared_ptr packets_stats, std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, - std::shared_ptr vote_mgr, std::shared_ptr db, const addr_t& node_addr); + std::shared_ptr vote_mgr, std::shared_ptr db, const addr_t& node_addr, + const std::string& log_channel_name = "PBFT_SYNC_PH"); void handleMaliciousSyncPeer(const dev::p2p::NodeID& id); diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp index e2ce7e5ee6..699ee7973a 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp @@ -10,7 +10,8 @@ class StatusPacketHandler : public ExtSyncingPacketHandler { std::shared_ptr packets_stats, std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, - std::shared_ptr db, h256 genesis_hash, const addr_t& node_addr); + std::shared_ptr db, h256 genesis_hash, const addr_t& node_addr, + const std::string& log_channel_name = "STATUS_PH"); bool sendStatus(const dev::p2p::NodeID& node_id, bool initial); void sendStatusToPeers(); diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp index 3d49d13394..1e7b49ccc3 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp @@ -18,7 +18,7 @@ class TransactionPacketHandler : public PacketHandler { TransactionPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr trx_mgr, std::shared_ptr test_state, - const addr_t& node_addr); + const addr_t& node_addr, const std::string& log_channel_name = "TRANSACTION_PH"); /** * @brief Send transactions diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp index 2f875799c7..1ff99effbe 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp @@ -9,7 +9,7 @@ class VotePacketHandler : public ExtVotesPacketHandler { VotePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - const addr_t& node_addr); + const addr_t& node_addr, const std::string& log_channel_name = "PBFT_VOTE_PH"); /** * @brief Sends pbft vote to connected peers diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp index 03f7f8dbb4..ddef864438 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp @@ -9,7 +9,7 @@ class VotesBundlePacketHandler : public ExtVotesPacketHandler { VotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - const addr_t& node_addr); + const addr_t& node_addr, const std::string& log_channel_name = "VOTES_BUNDLE_PH"); /** * @brief Sends pbft votes bundle to connected peers diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_next_votes_bundle_packet_handler.hpp similarity index 87% rename from libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.hpp rename to libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_next_votes_bundle_packet_handler.hpp index 1d55226777..7208ab6ec3 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_next_votes_bundle_packet_handler.hpp @@ -1,10 +1,10 @@ #pragma once -#include "network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp" namespace taraxa::network::tarcap::v1 { -// V1 packets handlers must be derived from latest packets handlers otherwise netowrk class might not work properly ! +// V1 packets handlers must be derived from latest packets handlers otherwise network class might not work properly ! class GetNextVotesBundlePacketHandler final : public tarcap::GetNextVotesBundlePacketHandler { public: using tarcap::GetNextVotesBundlePacketHandler::GetNextVotesBundlePacketHandler; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp index 885b62db65..2fcc72ed3a 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp @@ -4,7 +4,7 @@ namespace taraxa::network::tarcap::v1 { -// V1 packets handlers must be derived from latest packets handlers otherwise netowrk class might not work properly ! +// V1 packets handlers must be derived from latest packets handlers otherwise network class might not work properly ! class GetPbftSyncPacketHandler final : public tarcap::GetPbftSyncPacketHandler { public: using tarcap::GetPbftSyncPacketHandler::GetPbftSyncPacketHandler; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp index 0b1a264748..aceef1dfe2 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp @@ -1,6 +1,6 @@ #pragma once -#include "get_next_votes_sync_packet_handler.hpp" +#include "get_next_votes_bundle_packet_handler.hpp" #include "get_pbft_sync_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp" @@ -31,9 +31,9 @@ static const TaraxaCapability::InitPacketsHandlers kInitV1Handlers = packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); packets_handlers->registerHandler( - config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); + config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr, "V1_GET_NEXT_VOTES_BUNDLE_PH"); packets_handlers->registerHandler( - config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr); + config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr, "V1_VOTES_BUNDLE_PH"); // Standard packets with mid processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, @@ -54,12 +54,13 @@ static const TaraxaCapability::InitPacketsHandlers kInitV1Handlers = pbft_syncing_state, pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, node_addr); - packets_handlers->registerHandler( - config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, vote_mgr, db, node_addr); + packets_handlers->registerHandler(config, peers_state, packets_stats, + pbft_syncing_state, pbft_chain, vote_mgr, + db, node_addr, "V1_GET_PBFT_SYNC_PH"); - packets_handlers->registerHandler(config, peers_state, packets_stats, - pbft_syncing_state, pbft_chain, pbft_mgr, - dag_mgr, vote_mgr, db, node_addr); + packets_handlers->registerHandler( + config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, db, + node_addr, "V1_PBFT_SYNC_PH"); return packets_handlers; }; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp index 92464e4c30..f698d9b7ab 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/pbft_sync_packet_handler.hpp @@ -4,7 +4,7 @@ namespace taraxa::network::tarcap::v1 { -// V1 packets handlers must be derived from latest packets handlers otherwise netowrk class might not work properly ! +// V1 packets handlers must be derived from latest packets handlers otherwise network class might not work properly ! class PbftSyncPacketHandler final : public tarcap::PbftSyncPacketHandler { public: using tarcap::PbftSyncPacketHandler::PbftSyncPacketHandler; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/votes_bundle_packet_handler.hpp index d528775867..d4614fff02 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/votes_bundle_packet_handler.hpp @@ -4,7 +4,7 @@ namespace taraxa::network::tarcap::v1 { -// V1 packets handlers must be derived from latest packets handlers otherwise netowrk class might not work properly ! +// V1 packets handlers must be derived from latest packets handlers otherwise network class might not work properly ! class VotesBundlePacketHandler final : public tarcap::VotesBundlePacketHandler { public: using tarcap::VotesBundlePacketHandler::VotesBundlePacketHandler; diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp index eeec23f0b1..9db054cf57 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp @@ -14,10 +14,11 @@ DagBlockPacketHandler::DagBlockPacketHandler(const FullNodeConfig &conf, std::sh std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, std::shared_ptr db, - std::shared_ptr test_state, const addr_t &node_addr) + std::shared_ptr test_state, const addr_t &node_addr, + const std::string &log_channel_name) : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, - "DAG_BLOCK_PH"), + log_channel_name), test_state_(std::move(test_state)), trx_mgr_(std::move(trx_mgr)) {} diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_sync_packet_handler.cpp index f351dacec8..5f6e36ef8e 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_sync_packet_handler.cpp @@ -14,10 +14,10 @@ DagSyncPacketHandler::DagSyncPacketHandler(const FullNodeConfig& conf, std::shar std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, std::shared_ptr db, - const addr_t& node_addr) + const addr_t& node_addr, const std::string& log_channel_name) : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, - "DAG_SYNC_PH"), + log_channel_name), trx_mgr_(std::move(trx_mgr)) {} void DagSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketData& packet_data) const { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.cpp index 49af08a380..9c136ff067 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.cpp @@ -9,8 +9,8 @@ GetDagSyncPacketHandler::GetDagSyncPacketHandler(const FullNodeConfig &conf, std std::shared_ptr packets_stats, std::shared_ptr trx_mgr, std::shared_ptr dag_mgr, std::shared_ptr db, - const addr_t &node_addr) - : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, "GET_DAG_SYNC_PH"), + const addr_t &node_addr, const std::string &log_channel_name) + : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, log_channel_name), trx_mgr_(std::move(trx_mgr)), dag_mgr_(std::move(dag_mgr)), db_(std::move(db)) {} diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.cpp similarity index 96% rename from libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.cpp index c99c093542..8dad868768 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote_manager/vote_manager.hpp" @@ -8,9 +8,10 @@ namespace taraxa::network::tarcap { GetNextVotesBundlePacketHandler::GetNextVotesBundlePacketHandler( const FullNodeConfig &conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, - std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, const addr_t &node_addr) + std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, const addr_t &node_addr, + const std::string &log_channel_name) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), - std::move(pbft_chain), std::move(vote_mgr), node_addr, "GET_NEXT_VOTES_SYNC_PH") {} + std::move(pbft_chain), std::move(vote_mgr), node_addr, log_channel_name) {} void GetNextVotesBundlePacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { if (constexpr size_t required_size = 2; packet_data.rlp_.itemCount() != required_size) { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp index b20fa00b37..1b016a0f82 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp @@ -14,8 +14,8 @@ GetPbftSyncPacketHandler::GetPbftSyncPacketHandler(const FullNodeConfig &conf, s std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr db, - const addr_t &node_addr) - : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, "GET_PBFT_SYNC_PH"), + const addr_t &node_addr, const std::string &log_channel_name) + : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, log_channel_name), pbft_syncing_state_(std::move(pbft_syncing_state)), pbft_chain_(std::move(pbft_chain)), vote_mgr_(std::move(vote_mgr)), diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp index 9b9ebb7a01..fc0ea1bf11 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp @@ -15,10 +15,10 @@ PbftSyncPacketHandler::PbftSyncPacketHandler(const FullNodeConfig &conf, std::sh std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr vote_mgr, std::shared_ptr db, - const addr_t &node_addr) + const addr_t &node_addr, const std::string &log_channel_name) : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, - "PBFT_SYNC_PH"), + log_channel_name), vote_mgr_(std::move(vote_mgr)), periodic_events_tp_(1, true) {} diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp index 9877afa226..e7bdbf304b 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp @@ -15,10 +15,11 @@ StatusPacketHandler::StatusPacketHandler(const FullNodeConfig& conf, std::shared std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr db, - h256 genesis_hash, const addr_t& node_addr) + h256 genesis_hash, const addr_t& node_addr, + const std::string& log_channel_name) : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, - "STATUS_PH"), + log_channel_name), kGenesisHash(genesis_hash) {} void StatusPacketHandler::validatePacketRlpFormat(const threadpool::PacketData& packet_data) const { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp index aa51cfa953..4d5424c1e5 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp @@ -11,8 +11,9 @@ namespace taraxa::network::tarcap { TransactionPacketHandler::TransactionPacketHandler(const FullNodeConfig &conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr trx_mgr, - std::shared_ptr test_state, const addr_t &node_addr) - : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, "TRANSACTION_PH"), + std::shared_ptr test_state, const addr_t &node_addr, + const std::string &log_channel_name) + : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, log_channel_name), trx_mgr_(std::move(trx_mgr)), test_state_(std::move(test_state)) {} diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp index 4eb75ad7ef..3ab4cab04f 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp @@ -8,9 +8,10 @@ namespace taraxa::network::tarcap { VotePacketHandler::VotePacketHandler(const FullNodeConfig &conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t &node_addr) + std::shared_ptr vote_mgr, const addr_t &node_addr, + const std::string &log_channel_name) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), - std::move(pbft_chain), std::move(vote_mgr), node_addr, "PBFT_VOTE_PH") {} + std::move(pbft_chain), std::move(vote_mgr), node_addr, log_channel_name) {} void VotePacketHandler::validatePacketRlpFormat([[maybe_unused]] const threadpool::PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp index 7b0c7eb34c..912aae6e43 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp @@ -10,9 +10,10 @@ VotesBundlePacketHandler::VotesBundlePacketHandler(const FullNodeConfig &conf, s std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t &node_addr) + std::shared_ptr vote_mgr, const addr_t &node_addr, + const std::string &log_channel_name) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), - std::move(pbft_chain), std::move(vote_mgr), node_addr, "VOTES_SYNC_PH") {} + std::move(pbft_chain), std::move(vote_mgr), node_addr, log_channel_name) {} void VotesBundlePacketHandler::validatePacketRlpFormat( [[maybe_unused]] const threadpool::PacketData &packet_data) const { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_bundle_packet_handler.cpp similarity index 92% rename from libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.cpp rename to libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_bundle_packet_handler.cpp index 19c9c9a539..dc6be20eed 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_next_votes_bundle_packet_handler.cpp @@ -1,4 +1,4 @@ -#include "network/tarcap/packets_handlers/v1/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/v1/get_next_votes_bundle_packet_handler.hpp" #include "pbft/pbft_manager.hpp" #include "vote_manager/vote_manager.hpp" diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index 366c9d256a..a48934cf89 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -6,7 +6,7 @@ #include "network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp" -#include "network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/status_packet_handler.hpp" diff --git a/tests/network_test.cpp b/tests/network_test.cpp index c1d899489c..8223d854e2 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -14,7 +14,7 @@ #include "logger/logger.hpp" #include "network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp" -#include "network/tarcap/packets_handlers/latest/get_next_votes_sync_packet_handler.hpp" +#include "network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/status_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp" From 5bf26ae9a4cde14dc97cec15618b3b5096661f02 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 24 May 2023 11:40:23 -0700 Subject: [PATCH 049/134] resolved conflicts --- libraries/core_libs/consensus/src/dag/dag_manager.cpp | 3 --- libraries/core_libs/consensus/src/pbft/pbft_manager.cpp | 5 ----- libraries/core_libs/network/include/network/network.hpp | 1 - .../latest/get_pbft_sync_packet_handler.hpp | 4 ++-- .../packets_handlers/v1/get_pbft_sync_packet_handler.hpp | 2 +- libraries/core_libs/network/src/network.cpp | 1 - .../latest/get_pbft_sync_packet_handler.cpp | 2 +- .../packets_handlers/v1/get_pbft_sync_packet_handler.cpp | 7 ++++++- 8 files changed, 10 insertions(+), 15 deletions(-) diff --git a/libraries/core_libs/consensus/src/dag/dag_manager.cpp b/libraries/core_libs/consensus/src/dag/dag_manager.cpp index 0ef42f1c73..3f7e7d8771 100644 --- a/libraries/core_libs/consensus/src/dag/dag_manager.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_manager.cpp @@ -4,8 +4,6 @@ #include #include -#include -#include #include #include #include @@ -14,7 +12,6 @@ #include "dag/dag.hpp" #include "key_manager/key_manager.hpp" #include "network/network.hpp" -#include "network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp" #include "transaction/transaction_manager.hpp" namespace taraxa { diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index c9c55f069d..19c5234fdc 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -301,11 +301,6 @@ bool PbftManager::tryPushCertVotesBlock() { auto pbft_block = getValidPbftProposedBlock(current_pbft_period, certified_block_hash); if (!pbft_block) { LOG(log_er_) << "Invalid certified block " << certified_block_hash; - auto net = network_.lock(); - // If block/reward votes are missing but block is cert voted other nodes probably advanced, sync - if (net) { - net->startSyncingPbft(); - } return false; } diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index f24d24c06b..00cdbc7823 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -54,7 +54,6 @@ class Network { // returns count of all discovered nodes unsigned getNodeCount(); Json::Value getStatus(); - void startSyncingPbft(); bool pbft_syncing(); uint64_t syncTimeSeconds() const; void setSyncStatePeriod(PbftPeriod period); diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp index c97b5a7da6..d325663a46 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp @@ -27,8 +27,8 @@ class GetPbftSyncPacketHandler : public PacketHandler { virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; - virtual void sendPbftBlocks(dev::p2p::NodeID const& peer_id, PbftPeriod from_period, size_t blocks_to_transfer, - bool pbft_chain_synced); + virtual void sendPbftBlocks(const std::shared_ptr& peer, PbftPeriod from_period, + size_t blocks_to_transfer, bool pbft_chain_synced); protected: std::shared_ptr pbft_syncing_state_; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp index 2fcc72ed3a..a6d895439e 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.hpp @@ -10,7 +10,7 @@ class GetPbftSyncPacketHandler final : public tarcap::GetPbftSyncPacketHandler { using tarcap::GetPbftSyncPacketHandler::GetPbftSyncPacketHandler; private: - void sendPbftBlocks(dev::p2p::NodeID const& peer_id, PbftPeriod from_period, size_t blocks_to_transfer, + void sendPbftBlocks(const std::shared_ptr& peer, PbftPeriod from_period, size_t blocks_to_transfer, bool pbft_chain_synced) override; }; diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index dc8c479db9..7cfe577985 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -168,7 +168,6 @@ uint64_t Network::syncTimeSeconds() const { // TODO: this should be probably part of syncing_state, not node_stats return node_stats_->syncTimeSeconds(); } -==== BASE ==== void Network::setSyncStatePeriod(PbftPeriod period) { pbft_syncing_state_->setSyncStatePeriod(period); } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp index 1b016a0f82..795928c169 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp @@ -28,7 +28,7 @@ void GetPbftSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketD } void GetPbftSyncPacketHandler::process(const threadpool::PacketData &packet_data, - [[maybe_unused]] const std::shared_ptr &peer) { + const std::shared_ptr &peer) { LOG(log_tr_) << "Received GetPbftSyncPacket Block"; const size_t height_to_sync = packet_data.rlp_[0].toInt(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp index 829849927d..80e404c124 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp @@ -8,8 +8,9 @@ namespace taraxa::network::tarcap::v1 { -void GetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, PbftPeriod from_period, +void GetPbftSyncPacketHandler::sendPbftBlocks(const std::shared_ptr &peer, PbftPeriod from_period, size_t blocks_to_transfer, bool pbft_chain_synced) { + const auto &peer_id = peer->getId(); LOG(log_tr_) << "sendPbftBlocks: peer want to sync from pbft chain height " << from_period << ", will send at most " << blocks_to_transfer << " pbft blocks to " << peer_id; @@ -62,8 +63,12 @@ void GetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, P s << last_block; s.appendRaw(transformPeriodDataRlpToV1(data)); } + LOG(log_dg_) << "Sending PbftSyncPacket period " << block_period << " to " << peer_id; sealAndSend(peer_id, SubprotocolPacketType::PbftSyncPacket, std::move(s)); + if (pbft_chain_synced && last_block) { + peer->syncing_ = false; + } } } From 3029689fde9791ebd1a521e9f230de82d2d08cbb Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 25 May 2023 11:14:23 +0200 Subject: [PATCH 050/134] chore: fix period data trxs order --- CMakeLists.txt | 14 +++--- .../consensus/src/pbft/pbft_manager.cpp | 5 +- .../storage/migration/period_data_reorder.hpp | 16 ++++++ .../src/migration/migration_manager.cpp | 2 + .../src/migration/period_data_reorder.cpp | 49 +++++++++++++++++++ 5 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 libraries/core_libs/storage/include/storage/migration/period_data_reorder.hpp create mode 100644 libraries/core_libs/storage/src/migration/period_data_reorder.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ff575d0dd..dc35b329bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ set(TARAXA_NET_VERSION 1) set(TARAXA_DB_MAJOR_VERSION 1) # Minor version should be modified when changes to the database are made in the tables that can be rebuilt from the # basic tables -set(TARAXA_DB_MINOR_VERSION 1) +set(TARAXA_DB_MINOR_VERSION 2) # Defines Taraxa library target. project(taraxa-node VERSION ${TARAXA_VERSION}) @@ -162,12 +162,12 @@ else() include(${CMAKE_BINARY_DIR}/conan.cmake) - conan_cmake_run(CONANFILE conanfile.py - BUILD_TYPE ${CMAKE_BUILD_TYPE} + conan_cmake_run(CONANFILE conanfile.py + BUILD_TYPE ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} - BASIC_SETUP - CMAKE_TARGETS - KEEP_RPATHS + BASIC_SETUP + CMAKE_TARGETS + KEEP_RPATHS PROFILE ${CONAN_PROFILE} BUILD missing ) @@ -212,7 +212,7 @@ find_package(GMP) find_package(MPFR) include(ExternalProject) -# use JSONCPP library from conan for JSONRPCCPP build +# use JSONCPP library from conan for JSONRPCCPP build set(JSONCPP_INCLUDE_DIR ${CONAN_INCLUDE_DIRS_JSONCPP}) include(ProjectJSONRPCCPP) diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index 16c8366109..0827721fd2 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -1536,8 +1536,6 @@ void PbftManager::finalize_(PeriodData &&period_data, std::vector &&finali bool synchronous_processing) { std::shared_ptr anchor_block = nullptr; - reorderTransactions(period_data.transactions); - if (const auto anchor = period_data.pbft_blk->getPivotDagBlockHash()) { anchor_block = dag_mgr_->getDagBlock(anchor); if (!anchor_block) { @@ -1583,6 +1581,9 @@ bool PbftManager::pushPbftBlock_(PeriodData &&period_data, std::vectorsavePeriodData(period_data, batch); // Replace current reward votes diff --git a/libraries/core_libs/storage/include/storage/migration/period_data_reorder.hpp b/libraries/core_libs/storage/include/storage/migration/period_data_reorder.hpp new file mode 100644 index 0000000000..ec85d6ea10 --- /dev/null +++ b/libraries/core_libs/storage/include/storage/migration/period_data_reorder.hpp @@ -0,0 +1,16 @@ +#pragma once +#include + +#include "common/thread_pool.hpp" +#include "pbft/period_data.hpp" +#include "storage/migration/migration_base.hpp" + +namespace taraxa::storage::migration { +class PeriodDataReorder : public migration::Base { + public: + PeriodDataReorder(std::shared_ptr db); + std::string id() override; + uint32_t dbVersion() override; + void migrate() override; +}; +} // namespace taraxa::storage::migration \ No newline at end of file diff --git a/libraries/core_libs/storage/src/migration/migration_manager.cpp b/libraries/core_libs/storage/src/migration/migration_manager.cpp index 7f5dabd999..a0edca9ea5 100644 --- a/libraries/core_libs/storage/src/migration/migration_manager.cpp +++ b/libraries/core_libs/storage/src/migration/migration_manager.cpp @@ -1,11 +1,13 @@ #include "storage/migration/migration_manager.hpp" +#include "storage/migration/period_data_reorder.hpp" #include "storage/migration/transaction_hashes.hpp" namespace taraxa::storage::migration { Manager::Manager(std::shared_ptr db, const addr_t& node_addr) : db_(db) { LOG_OBJECTS_CREATE("MIGRATIONS"); registerMigration(); + registerMigration(); } void Manager::applyAll() { diff --git a/libraries/core_libs/storage/src/migration/period_data_reorder.cpp b/libraries/core_libs/storage/src/migration/period_data_reorder.cpp new file mode 100644 index 0000000000..65c418f1b8 --- /dev/null +++ b/libraries/core_libs/storage/src/migration/period_data_reorder.cpp @@ -0,0 +1,49 @@ +#include "storage/migration/period_data_reorder.hpp" + +#include + +#include "pbft/pbft_manager.hpp" + +namespace taraxa::storage::migration { + +PeriodDataReorder::PeriodDataReorder(std::shared_ptr db) : migration::Base(db) {} + +std::string PeriodDataReorder::id() { return "PeriodDataReorder"; } + +uint32_t PeriodDataReorder::dbVersion() { return 1; } + +void PeriodDataReorder::migrate() { + auto it = db_->getColumnIterator(DB::Columns::period_data); + it->SeekToFirst(); + if (!it->Valid()) { + return; + } + + uint64_t start_period, end_period; + memcpy(&start_period, it->key().data(), sizeof(uint64_t)); + + it->SeekToLast(); + if (!it->Valid()) { + it->Prev(); + } + memcpy(&end_period, it->key().data(), sizeof(uint64_t)); + util::ThreadPool executor{5}; + + // Get and save data in new format for all blocks + for (uint64_t i = start_period; i <= end_period; ++i) { + executor.post([this, i]() { + PeriodData period_data(db_->getPeriodDataRaw(i)); + PbftManager::reorderTransactions(period_data.transactions); + db_->insert(DB::Columns::period_data, i, period_data.rlp()); + }); + // This should slow down main loop so we are not using so much memory + while (executor.num_pending_tasks() > (executor.capacity() * 3)) { + taraxa::thisThreadSleepForMilliSeconds(50); + } + } + // I know it's not perfect to check with sleep, but it's just migration that should be run once + do { + taraxa::thisThreadSleepForMilliSeconds(100); + } while (executor.num_pending_tasks()); +} +} // namespace taraxa::storage::migration \ No newline at end of file From e1b589c3fb442e80c6eb89ee879333ec08aecddb Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 25 May 2023 12:20:09 +0200 Subject: [PATCH 051/134] chore: change verification on syncing --- .../include/transaction/transaction_manager.hpp | 2 +- libraries/core_libs/consensus/src/pbft/pbft_manager.cpp | 9 ++++----- .../consensus/src/transaction/transaction_manager.cpp | 7 ++++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/libraries/core_libs/consensus/include/transaction/transaction_manager.hpp b/libraries/core_libs/consensus/include/transaction/transaction_manager.hpp index 5fa50c1120..814e80a924 100644 --- a/libraries/core_libs/consensus/include/transaction/transaction_manager.hpp +++ b/libraries/core_libs/consensus/include/transaction/transaction_manager.hpp @@ -147,7 +147,7 @@ class TransactionManager : public std::enable_shared_from_this excludeFinalizedTransactions(const std::vector &hashes); + std::unordered_set excludeFinalizedTransactions(const std::vector &hashes); /** * @brief Get the block transactions diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index 0827721fd2..ba4c5d14ec 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -1716,11 +1716,10 @@ std::optional>>> PbftMan net->getSpecificHandler()->handleMaliciousSyncPeer(node_id); return std::nullopt; } - for (uint32_t i = 0; i < non_finalized_transactions.size(); i++) { - if (non_finalized_transactions[i] != period_data.transactions[i]->getHash()) { - LOG(log_er_) << "Synced PBFT block " << pbft_block_hash << " transaction mismatch " - << non_finalized_transactions[i] - << " incorrect, expected: " << period_data.transactions[i]->getHash(); + for (uint32_t i = 0; i < period_data.transactions.size(); i++) { + if (!non_finalized_transactions.contains(period_data.transactions[i]->getHash())) { + LOG(log_er_) << "Synced PBFT block " << pbft_block_hash << " has incorrect transaction " + << period_data.transactions[i]->getHash(); sync_queue_.clear(); net->getSpecificHandler()->handleMaliciousSyncPeer(node_id); return std::nullopt; diff --git a/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp b/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp index 0e1c9ce7ac..d63a24d7c6 100644 --- a/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp +++ b/libraries/core_libs/consensus/src/transaction/transaction_manager.cpp @@ -1,6 +1,7 @@ #include "transaction/transaction_manager.hpp" #include +#include #include #include "dag/dag.hpp" @@ -259,14 +260,14 @@ std::vector> TransactionManager::getNonfinalizedTrx return ret; } -std::vector TransactionManager::excludeFinalizedTransactions(const std::vector &hashes) { - std::vector ret; +std::unordered_set TransactionManager::excludeFinalizedTransactions(const std::vector &hashes) { + std::unordered_set ret; ret.reserve(hashes.size()); std::shared_lock transactions_lock(transactions_mutex_); for (const auto &hash : hashes) { if (!recently_finalized_transactions_.contains(hash)) { if (!db_->transactionFinalized(hash)) { - ret.push_back(hash); + ret.insert(hash); } } } From 24e31c4db20457e72f38d674ce4811c59ab77971 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 25 May 2023 13:00:28 +0200 Subject: [PATCH 052/134] chore: implement progress --- .../include/storage/migration/migration_base.hpp | 7 +++---- .../include/storage/migration/period_data_reorder.hpp | 2 +- .../include/storage/migration/transaction_hashes.hpp | 2 +- .../storage/src/migration/period_data_reorder.cpp | 10 +++++++++- .../storage/src/migration/transaction_hashes.cpp | 2 +- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/libraries/core_libs/storage/include/storage/migration/migration_base.hpp b/libraries/core_libs/storage/include/storage/migration/migration_base.hpp index 6ea3f15dd7..54f355d364 100644 --- a/libraries/core_libs/storage/include/storage/migration/migration_base.hpp +++ b/libraries/core_libs/storage/include/storage/migration/migration_base.hpp @@ -15,18 +15,17 @@ class Base { void apply(logger::Logger& log) { if (db_->getMajorVersion() != dbVersion()) { LOG(log) << id() - << ": skip migration as it was made for different major db version. Could be removed from the code" - << std::endl; + << ": skip migration as it was made for different major db version. Could be removed from the code"; return; } - migrate(); + migrate(log); setApplied(); db_->commitWriteBatch(batch_); } protected: // Method with custom logic. All db changes should be made using `batch_` - virtual void migrate() = 0; + virtual void migrate(logger::Logger& log) = 0; void setApplied() { db_->insert(batch_, DB::Columns::migrations, id(), true); } diff --git a/libraries/core_libs/storage/include/storage/migration/period_data_reorder.hpp b/libraries/core_libs/storage/include/storage/migration/period_data_reorder.hpp index ec85d6ea10..1c89782cc3 100644 --- a/libraries/core_libs/storage/include/storage/migration/period_data_reorder.hpp +++ b/libraries/core_libs/storage/include/storage/migration/period_data_reorder.hpp @@ -11,6 +11,6 @@ class PeriodDataReorder : public migration::Base { PeriodDataReorder(std::shared_ptr db); std::string id() override; uint32_t dbVersion() override; - void migrate() override; + void migrate(logger::Logger& log) override; }; } // namespace taraxa::storage::migration \ No newline at end of file diff --git a/libraries/core_libs/storage/include/storage/migration/transaction_hashes.hpp b/libraries/core_libs/storage/include/storage/migration/transaction_hashes.hpp index ab0876a987..f766588c61 100644 --- a/libraries/core_libs/storage/include/storage/migration/transaction_hashes.hpp +++ b/libraries/core_libs/storage/include/storage/migration/transaction_hashes.hpp @@ -11,6 +11,6 @@ class TransactionHashes : public migration::Base { TransactionHashes(std::shared_ptr db); std::string id() override; uint32_t dbVersion() override; - void migrate() override; + void migrate(logger::Logger& log) override; }; } // namespace taraxa::storage::migration \ No newline at end of file diff --git a/libraries/core_libs/storage/src/migration/period_data_reorder.cpp b/libraries/core_libs/storage/src/migration/period_data_reorder.cpp index 65c418f1b8..afffbfb2b9 100644 --- a/libraries/core_libs/storage/src/migration/period_data_reorder.cpp +++ b/libraries/core_libs/storage/src/migration/period_data_reorder.cpp @@ -12,7 +12,7 @@ std::string PeriodDataReorder::id() { return "PeriodDataReorder"; } uint32_t PeriodDataReorder::dbVersion() { return 1; } -void PeriodDataReorder::migrate() { +void PeriodDataReorder::migrate(logger::Logger& log) { auto it = db_->getColumnIterator(DB::Columns::period_data); it->SeekToFirst(); if (!it->Valid()) { @@ -29,6 +29,9 @@ void PeriodDataReorder::migrate() { memcpy(&end_period, it->key().data(), sizeof(uint64_t)); util::ThreadPool executor{5}; + const auto diff = end_period - start_period; + uint64_t curr_progress = 0; + // Get and save data in new format for all blocks for (uint64_t i = start_period; i <= end_period; ++i) { executor.post([this, i]() { @@ -40,6 +43,11 @@ void PeriodDataReorder::migrate() { while (executor.num_pending_tasks() > (executor.capacity() * 3)) { taraxa::thisThreadSleepForMilliSeconds(50); } + auto percentage = (i - start_period) / diff * 100; + if (percentage > curr_progress) { + curr_progress = percentage; + LOG(log) << "Migration " << id() << " progress " << curr_progress << "%"; + } } // I know it's not perfect to check with sleep, but it's just migration that should be run once do { diff --git a/libraries/core_libs/storage/src/migration/transaction_hashes.cpp b/libraries/core_libs/storage/src/migration/transaction_hashes.cpp index 63f5ab0b0e..3b711af079 100644 --- a/libraries/core_libs/storage/src/migration/transaction_hashes.cpp +++ b/libraries/core_libs/storage/src/migration/transaction_hashes.cpp @@ -20,7 +20,7 @@ std::string TransactionHashes::id() { return "TransactionHashes"; } uint32_t TransactionHashes::dbVersion() { return 1; } -void TransactionHashes::migrate() { +void TransactionHashes::migrate(logger::Logger& /*log*/) { auto it = db_->getColumnIterator(DB::Columns::final_chain_transaction_hashes_by_blk_number); // Get and save data in new format for all blocks From 3d63d95fcafdcf5f44eee288ec934f33df3a7c2f Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 25 May 2023 13:36:55 +0200 Subject: [PATCH 053/134] chore: fix and cleanup batch after applying migration --- libraries/core_libs/storage/src/storage.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 6f3beff3bd..472e8ec35c 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -311,6 +311,7 @@ DbStorage::Batch DbStorage::createWriteBatch() { return DbStorage::Batch(); } void DbStorage::commitWriteBatch(Batch& write_batch, rocksdb::WriteOptions const& opts) { auto status = db_->Write(opts, write_batch.GetWriteBatch()); checkStatus(status); + write_batch.Clear(); } std::shared_ptr DbStorage::getDagBlock(blk_hash_t const& hash) { From 25247f9d028616dce283dab04a732f9f8db469d0 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 25 May 2023 13:45:40 +0200 Subject: [PATCH 054/134] chore: fix printing progress --- .../core_libs/storage/src/migration/period_data_reorder.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/core_libs/storage/src/migration/period_data_reorder.cpp b/libraries/core_libs/storage/src/migration/period_data_reorder.cpp index afffbfb2b9..bdc895c91b 100644 --- a/libraries/core_libs/storage/src/migration/period_data_reorder.cpp +++ b/libraries/core_libs/storage/src/migration/period_data_reorder.cpp @@ -27,7 +27,7 @@ void PeriodDataReorder::migrate(logger::Logger& log) { it->Prev(); } memcpy(&end_period, it->key().data(), sizeof(uint64_t)); - util::ThreadPool executor{5}; + util::ThreadPool executor{std::thread::hardware_concurrency()}; const auto diff = end_period - start_period; uint64_t curr_progress = 0; @@ -43,7 +43,7 @@ void PeriodDataReorder::migrate(logger::Logger& log) { while (executor.num_pending_tasks() > (executor.capacity() * 3)) { taraxa::thisThreadSleepForMilliSeconds(50); } - auto percentage = (i - start_period) / diff * 100; + auto percentage = (i - start_period) * 100 / diff ; if (percentage > curr_progress) { curr_progress = percentage; LOG(log) << "Migration " << id() << " progress " << curr_progress << "%"; From f3f8e580b451bd4482c1f9125c8f66c6cb638113 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Thu, 25 May 2023 10:48:54 -0700 Subject: [PATCH 055/134] refactor init of packets handlers loggers --- .../latest/dag_block_packet_handler.hpp | 2 +- .../latest/dag_sync_packet_handler.hpp | 2 +- .../latest/get_dag_sync_packet_handler.hpp | 2 +- .../get_next_votes_bundle_packet_handler.hpp | 2 +- .../latest/get_pbft_sync_packet_handler.hpp | 2 +- .../latest/pbft_sync_packet_handler.hpp | 2 +- .../latest/status_packet_handler.hpp | 2 +- .../latest/transaction_packet_handler.hpp | 2 +- .../latest/vote_packet_handler.hpp | 2 +- .../latest/votes_bundle_packet_handler.hpp | 2 +- .../v1/init_packets_handlers.hpp | 29 ++++++++------- .../network/tarcap/taraxa_capability.hpp | 4 +-- libraries/core_libs/network/src/network.cpp | 7 ++-- .../latest/dag_block_packet_handler.cpp | 4 +-- .../latest/dag_sync_packet_handler.cpp | 4 +-- .../latest/get_dag_sync_packet_handler.cpp | 4 +-- .../get_next_votes_bundle_packet_handler.cpp | 5 +-- .../latest/get_pbft_sync_packet_handler.cpp | 5 +-- .../latest/pbft_sync_packet_handler.cpp | 4 +-- .../latest/status_packet_handler.cpp | 5 ++- .../latest/transaction_packet_handler.cpp | 4 +-- .../latest/vote_packet_handler.cpp | 4 +-- .../latest/votes_bundle_packet_handler.cpp | 4 +-- .../network/src/tarcap/taraxa_capability.cpp | 35 ++++++++++--------- tests/p2p_test.cpp | 2 +- 25 files changed, 71 insertions(+), 69 deletions(-) diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp index 46e3cc1948..951d687536 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp @@ -18,7 +18,7 @@ class DagBlockPacketHandler : public ExtSyncingPacketHandler { std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, std::shared_ptr db, std::shared_ptr test_state, const addr_t &node_addr, - const std::string &log_channel_name = "DAG_BLOCK_PH"); + const std::string &logs_prefix = ""); void sendBlock(dev::p2p::NodeID const &peer_id, DagBlock block, const SharedTransactions &trxs); void onNewBlockReceived(DagBlock &&block, const std::shared_ptr &peer = nullptr); diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp index d5ccb69de1..631e94fb8c 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_sync_packet_handler.hpp @@ -15,7 +15,7 @@ class DagSyncPacketHandler : public ExtSyncingPacketHandler { std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, std::shared_ptr db, - const addr_t& node_addr, const std::string& log_channel_name = "DAG_SYNC_PH"); + const addr_t& node_addr, const std::string& logs_prefix = ""); // Packet type that is processed by this handler static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::DagSyncPacket; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp index 516f2778f7..4c48aa779b 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.hpp @@ -16,7 +16,7 @@ class GetDagSyncPacketHandler : public PacketHandler { std::shared_ptr packets_stats, std::shared_ptr trx_mgr, std::shared_ptr dag_mgr, std::shared_ptr db, const addr_t& node_addr, - const std::string& log_channel_name = "GET_DAG_SYNC_PH"); + const std::string& logs_prefix = "GET_DAG_SYNC_PH"); void sendBlocks(const dev::p2p::NodeID& peer_id, std::vector>&& blocks, SharedTransactions&& transactions, PbftPeriod request_period, PbftPeriod period); diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp index 7189c94e48..6f8889c824 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp @@ -15,7 +15,7 @@ class GetNextVotesBundlePacketHandler : public ExtVotesPacketHandler { std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, const addr_t& node_addr, - const std::string& log_channel_name = "GET_NEXT_VOTES_BUNDLE_PH"); + const std::string& logs_prefix = "GET_NEXT_VOTES_BUNDLE_PH"); // Packet type that is processed by this handler static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetNextVotesSyncPacket; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp index d325663a46..763bebb372 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.hpp @@ -18,7 +18,7 @@ class GetPbftSyncPacketHandler : public PacketHandler { std::shared_ptr packets_stats, std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr db, - const addr_t& node_addr, const std::string& log_channel_name = "GET_PBFT_SYNC_PH"); + const addr_t& node_addr, const std::string& logs_prefix = "GET_PBFT_SYNC_PH"); // Packet type that is processed by this handler static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::GetPbftSyncPacket; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp index 29b33fa05f..1f37e31eaf 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/pbft_sync_packet_handler.hpp @@ -12,7 +12,7 @@ class PbftSyncPacketHandler : public ExtSyncingPacketHandler { std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr vote_mgr, std::shared_ptr db, const addr_t& node_addr, - const std::string& log_channel_name = "PBFT_SYNC_PH"); + const std::string& logs_prefix = "PBFT_SYNC_PH"); void handleMaliciousSyncPeer(const dev::p2p::NodeID& id); diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp index 699ee7973a..755db336be 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/status_packet_handler.hpp @@ -11,7 +11,7 @@ class StatusPacketHandler : public ExtSyncingPacketHandler { std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr db, h256 genesis_hash, const addr_t& node_addr, - const std::string& log_channel_name = "STATUS_PH"); + const std::string& logs_prefix = "STATUS_PH"); bool sendStatus(const dev::p2p::NodeID& node_id, bool initial); void sendStatusToPeers(); diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp index 1e7b49ccc3..0695d0dd85 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp @@ -18,7 +18,7 @@ class TransactionPacketHandler : public PacketHandler { TransactionPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr trx_mgr, std::shared_ptr test_state, - const addr_t& node_addr, const std::string& log_channel_name = "TRANSACTION_PH"); + const addr_t& node_addr, const std::string& logs_prefix = "TRANSACTION_PH"); /** * @brief Send transactions diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp index 1ff99effbe..740bed8fe0 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp @@ -9,7 +9,7 @@ class VotePacketHandler : public ExtVotesPacketHandler { VotePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - const addr_t& node_addr, const std::string& log_channel_name = "PBFT_VOTE_PH"); + const addr_t& node_addr, const std::string& logs_prefix = ""); /** * @brief Sends pbft vote to connected peers diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp index ddef864438..30d140045d 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp @@ -9,7 +9,7 @@ class VotesBundlePacketHandler : public ExtVotesPacketHandler { VotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - const addr_t& node_addr, const std::string& log_channel_name = "VOTES_BUNDLE_PH"); + const addr_t& node_addr, const std::string& logs_prefix = ""); /** * @brief Sends pbft votes bundle to connected peers diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp index aceef1dfe2..c000527fbc 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp @@ -18,7 +18,7 @@ namespace taraxa::network::tarcap::v1 { * @brief Taraxa capability V1 InitPacketsHandlers function definition */ static const TaraxaCapability::InitPacketsHandlers kInitV1Handlers = - [](const TarcapVersion version [[maybe_unused]], const FullNodeConfig &config, const h256 &genesis_hash, + [](const std::string &logs_prefix, const FullNodeConfig &config, const h256 &genesis_hash, const std::shared_ptr &peers_state, const std::shared_ptr &pbft_syncing_state, const std::shared_ptr &test_state, const std::shared_ptr &packets_stats, const std::shared_ptr &db, @@ -29,38 +29,37 @@ static const TaraxaCapability::InitPacketsHandlers kInitV1Handlers = // Consensus packets with high processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr); + pbft_chain, vote_mgr, node_addr, logs_prefix); packets_handlers->registerHandler( - config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr, "V1_GET_NEXT_VOTES_BUNDLE_PH"); + config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr, logs_prefix); packets_handlers->registerHandler( - config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr, "V1_VOTES_BUNDLE_PH"); + config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr, logs_prefix); // Standard packets with mid processing priority - packets_handlers->registerHandler(config, peers_state, packets_stats, - pbft_syncing_state, pbft_chain, pbft_mgr, - dag_mgr, trx_mgr, db, test_state, node_addr); + packets_handlers->registerHandler( + config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, + test_state, node_addr, logs_prefix); packets_handlers->registerHandler(config, peers_state, packets_stats, trx_mgr, - test_state, node_addr); + test_state, node_addr, logs_prefix); // Non critical packets with low processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, pbft_mgr, dag_mgr, - db, genesis_hash, node_addr); + db, genesis_hash, node_addr, logs_prefix); packets_handlers->registerHandler(config, peers_state, packets_stats, trx_mgr, - dag_mgr, db, node_addr); + dag_mgr, db, node_addr, logs_prefix); packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, pbft_mgr, dag_mgr, - trx_mgr, db, node_addr); + trx_mgr, db, node_addr, logs_prefix); - packets_handlers->registerHandler(config, peers_state, packets_stats, - pbft_syncing_state, pbft_chain, vote_mgr, - db, node_addr, "V1_GET_PBFT_SYNC_PH"); + packets_handlers->registerHandler( + config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, vote_mgr, db, node_addr, logs_prefix); packets_handlers->registerHandler( config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, pbft_mgr, dag_mgr, vote_mgr, db, - node_addr, "V1_PBFT_SYNC_PH"); + node_addr, logs_prefix); return packets_handlers; }; diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp index fb2b6bf982..558a44bd6a 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp @@ -38,7 +38,7 @@ class TaraxaCapability final : public dev::p2p::CapabilityFace { * @brief Function signature for creating taraxa capability packets handlers */ using InitPacketsHandlers = std::function( - const TarcapVersion version, const FullNodeConfig &config, const h256 &genesis_hash, + const std::string &logs_prefix, const FullNodeConfig &config, const h256 &genesis_hash, const std::shared_ptr &peers_state, const std::shared_ptr &pbft_syncing_state, const std::shared_ptr &test_state, const std::shared_ptr &packets_stats, const std::shared_ptr &db, @@ -59,7 +59,7 @@ class TaraxaCapability final : public dev::p2p::CapabilityFace { std::shared_ptr syncing_state, std::shared_ptr db, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, - std::shared_ptr trx_mgr, const std::string &log_channel, + std::shared_ptr trx_mgr, InitPacketsHandlers init_packets_handlers = kInitLatestVersionHandlers); virtual ~TaraxaCapability() = default; diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index 7cfe577985..3a2dfcfcf9 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -78,13 +78,13 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::fi // Register old version (V1) of taraxa capability auto v1_tarcap = std::make_shared( kV1NetworkVersion, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, db, - pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, "V1_TARCAP", network::tarcap::v1::kInitV1Handlers); + pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, network::tarcap::v1::kInitV1Handlers); capabilities.emplace_back(v1_tarcap); // Register latest version of taraxa capability auto latest_tarcap = std::make_shared( TARAXA_NET_VERSION, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, db, - pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, "TARCAP"); + pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr); capabilities.emplace_back(latest_tarcap); return capabilities; @@ -97,8 +97,7 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::fi for (const auto test_tarcap_version : create_test_tarcaps) { auto tarcap = std::make_shared( test_tarcap_version, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, - db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, - "V" + std::to_string(test_tarcap_version) + "_TARCAP"); + db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr); capabilities.emplace_back(tarcap); } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp index 9db054cf57..be3b5cd89f 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp @@ -15,10 +15,10 @@ DagBlockPacketHandler::DagBlockPacketHandler(const FullNodeConfig &conf, std::sh std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, std::shared_ptr db, std::shared_ptr test_state, const addr_t &node_addr, - const std::string &log_channel_name) + const std::string &logs_prefix) : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, - log_channel_name), + logs_prefix + "DAG_BLOCK_PH"), test_state_(std::move(test_state)), trx_mgr_(std::move(trx_mgr)) {} diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_sync_packet_handler.cpp index 5f6e36ef8e..e28ca3c9ca 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_sync_packet_handler.cpp @@ -14,10 +14,10 @@ DagSyncPacketHandler::DagSyncPacketHandler(const FullNodeConfig& conf, std::shar std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, std::shared_ptr db, - const addr_t& node_addr, const std::string& log_channel_name) + const addr_t& node_addr, const std::string& logs_prefix) : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, - log_channel_name), + logs_prefix + "DAG_SYNC_PH"), trx_mgr_(std::move(trx_mgr)) {} void DagSyncPacketHandler::validatePacketRlpFormat(const threadpool::PacketData& packet_data) const { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.cpp index 9c136ff067..a841fbac42 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_dag_sync_packet_handler.cpp @@ -9,8 +9,8 @@ GetDagSyncPacketHandler::GetDagSyncPacketHandler(const FullNodeConfig &conf, std std::shared_ptr packets_stats, std::shared_ptr trx_mgr, std::shared_ptr dag_mgr, std::shared_ptr db, - const addr_t &node_addr, const std::string &log_channel_name) - : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, log_channel_name), + const addr_t &node_addr, const std::string &logs_prefix) + : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, logs_prefix + "GET_DAG_SYNC_PH"), trx_mgr_(std::move(trx_mgr)), dag_mgr_(std::move(dag_mgr)), db_(std::move(db)) {} diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.cpp index 8dad868768..519a646e22 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.cpp @@ -9,9 +9,10 @@ GetNextVotesBundlePacketHandler::GetNextVotesBundlePacketHandler( const FullNodeConfig &conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, const addr_t &node_addr, - const std::string &log_channel_name) + const std::string &logs_prefix) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), - std::move(pbft_chain), std::move(vote_mgr), node_addr, log_channel_name) {} + std::move(pbft_chain), std::move(vote_mgr), node_addr, + logs_prefix + "GET_NEXT_VOTES_BUNDLE_PH") {} void GetNextVotesBundlePacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { if (constexpr size_t required_size = 2; packet_data.rlp_.itemCount() != required_size) { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp index 795928c169..6f9d78900c 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp @@ -14,8 +14,9 @@ GetPbftSyncPacketHandler::GetPbftSyncPacketHandler(const FullNodeConfig &conf, s std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr db, - const addr_t &node_addr, const std::string &log_channel_name) - : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, log_channel_name), + const addr_t &node_addr, const std::string &logs_prefix) + : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, + logs_prefix + "GET_PBFT_SYNC_PH"), pbft_syncing_state_(std::move(pbft_syncing_state)), pbft_chain_(std::move(pbft_chain)), vote_mgr_(std::move(vote_mgr)), diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp index fc0ea1bf11..fb6a1a381b 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp @@ -15,10 +15,10 @@ PbftSyncPacketHandler::PbftSyncPacketHandler(const FullNodeConfig &conf, std::sh std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr vote_mgr, std::shared_ptr db, - const addr_t &node_addr, const std::string &log_channel_name) + const addr_t &node_addr, const std::string &logs_prefix) : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, - log_channel_name), + logs_prefix + "PBFT_SYNC_PH"), vote_mgr_(std::move(vote_mgr)), periodic_events_tp_(1, true) {} diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp index e7bdbf304b..95fd704ecf 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp @@ -15,11 +15,10 @@ StatusPacketHandler::StatusPacketHandler(const FullNodeConfig& conf, std::shared std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr db, - h256 genesis_hash, const addr_t& node_addr, - const std::string& log_channel_name) + h256 genesis_hash, const addr_t& node_addr, const std::string& logs_prefix) : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, - log_channel_name), + logs_prefix + "STATUS_PH"), kGenesisHash(genesis_hash) {} void StatusPacketHandler::validatePacketRlpFormat(const threadpool::PacketData& packet_data) const { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp index 4d5424c1e5..2206278cb1 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp @@ -12,8 +12,8 @@ TransactionPacketHandler::TransactionPacketHandler(const FullNodeConfig &conf, s std::shared_ptr packets_stats, std::shared_ptr trx_mgr, std::shared_ptr test_state, const addr_t &node_addr, - const std::string &log_channel_name) - : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, log_channel_name), + const std::string &logs_prefix) + : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, logs_prefix + "TRANSACTION_PH"), trx_mgr_(std::move(trx_mgr)), test_state_(std::move(test_state)) {} diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp index 3ab4cab04f..f071ed1fb3 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp @@ -9,9 +9,9 @@ VotePacketHandler::VotePacketHandler(const FullNodeConfig &conf, std::shared_ptr std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, const addr_t &node_addr, - const std::string &log_channel_name) + const std::string &logs_prefix) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), - std::move(pbft_chain), std::move(vote_mgr), node_addr, log_channel_name) {} + std::move(pbft_chain), std::move(vote_mgr), node_addr, logs_prefix + "PBFT_VOTE_PH") {} void VotePacketHandler::validatePacketRlpFormat([[maybe_unused]] const threadpool::PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp index 912aae6e43..7074d4ebce 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp @@ -11,9 +11,9 @@ VotesBundlePacketHandler::VotesBundlePacketHandler(const FullNodeConfig &conf, s std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, const addr_t &node_addr, - const std::string &log_channel_name) + const std::string &logs_prefix) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), - std::move(pbft_chain), std::move(vote_mgr), node_addr, log_channel_name) {} + std::move(pbft_chain), std::move(vote_mgr), node_addr, logs_prefix + "VOTES_BUNDLE_PH") {} void VotesBundlePacketHandler::validatePacketRlpFormat( [[maybe_unused]] const threadpool::PacketData &packet_data) const { diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index a48934cf89..dc605e7d09 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -30,7 +30,7 @@ TaraxaCapability::TaraxaCapability(TarcapVersion version, const FullNodeConfig & std::shared_ptr syncing_state, std::shared_ptr db, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, - std::shared_ptr trx_mgr, const std::string &log_channel, + std::shared_ptr trx_mgr, InitPacketsHandlers init_packets_handlers) : test_state_(std::make_shared()), version_(version), @@ -40,12 +40,14 @@ TaraxaCapability::TaraxaCapability(TarcapVersion version, const FullNodeConfig & pbft_syncing_state_(std::move(syncing_state)), packets_handlers_(std::make_shared()), thread_pool_(std::move(threadpool)) { + const std::string logs_prefix = "V" + std::to_string(version) + "_"; const auto &node_addr = key.address(); - LOG_OBJECTS_CREATE(log_channel); + + LOG_OBJECTS_CREATE(logs_prefix + "TARCAP"); peers_state_ = std::make_shared(host, kConf); packets_handlers_ = - init_packets_handlers(version, conf, genesis_hash, peers_state_, pbft_syncing_state_, test_state_, + init_packets_handlers(logs_prefix, conf, genesis_hash, peers_state_, pbft_syncing_state_, test_state_, all_packets_stats_, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, node_addr); // Must be called after init_packets_handlers @@ -211,7 +213,7 @@ size_t TaraxaCapability::getReceivedTransactionsCount() const { return test_stat // END METHODS USED IN TESTS ONLY const TaraxaCapability::InitPacketsHandlers TaraxaCapability::kInitLatestVersionHandlers = - [](const TarcapVersion version [[maybe_unused]], const FullNodeConfig &config, const h256 &genesis_hash, + [](const std::string &logs_prefix, const FullNodeConfig &config, const h256 &genesis_hash, const std::shared_ptr &peers_state, const std::shared_ptr &pbft_syncing_state, const std::shared_ptr &test_state, const std::shared_ptr &packets_stats, const std::shared_ptr &db, @@ -219,38 +221,39 @@ const TaraxaCapability::InitPacketsHandlers TaraxaCapability::kInitLatestVersion const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, const std::shared_ptr &trx_mgr, const addr_t &node_addr) { auto packets_handlers = std::make_shared(); - // Consensus packets with high processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, pbft_chain, - vote_mgr, node_addr); + vote_mgr, node_addr, logs_prefix); packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr); + pbft_chain, vote_mgr, node_addr, logs_prefix); packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr); + pbft_chain, vote_mgr, node_addr, logs_prefix); // Standard packets with mid processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, test_state, - node_addr); + node_addr, logs_prefix); packets_handlers->registerHandler(config, peers_state, packets_stats, trx_mgr, - test_state, node_addr); + test_state, node_addr, logs_prefix); // Non critical packets with low processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, - pbft_chain, pbft_mgr, dag_mgr, db, genesis_hash, - node_addr); + pbft_chain, pbft_mgr, dag_mgr, db, genesis_hash, node_addr, + logs_prefix); packets_handlers->registerHandler(config, peers_state, packets_stats, trx_mgr, dag_mgr, - db, node_addr); + db, node_addr, logs_prefix); packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, - pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, node_addr); + pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, node_addr, + logs_prefix); packets_handlers->registerHandler( - config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, vote_mgr, db, node_addr); + config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, vote_mgr, db, node_addr, logs_prefix); packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, - pbft_chain, pbft_mgr, dag_mgr, vote_mgr, db, node_addr); + pbft_chain, pbft_mgr, dag_mgr, vote_mgr, db, node_addr, + logs_prefix); return packets_handlers; }; diff --git a/tests/p2p_test.cpp b/tests/p2p_test.cpp index 648abde954..8d621187a8 100644 --- a/tests/p2p_test.cpp +++ b/tests/p2p_test.cpp @@ -56,7 +56,7 @@ std::shared_ptr makeTarcap(std::weak_ auto tarcap = std::make_shared(version, conf, genesis_hash, host, key, thread_pool, packets_stats, syncing_state, nullptr, nullptr, - nullptr, nullptr, nullptr, nullptr, "TARCAP"); + nullptr, nullptr, nullptr, nullptr); thread_pool->startProcessing(); From 9fff869fddba5600b13f9369f0db131935fc63cb Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Fri, 26 May 2023 13:37:53 +0200 Subject: [PATCH 056/134] chore: add check for saving only when re-order happend --- .../core_libs/consensus/include/pbft/pbft_manager.hpp | 3 ++- .../core_libs/consensus/src/pbft/pbft_manager.cpp | 11 +++++++++-- .../storage/src/migration/period_data_reorder.cpp | 8 +++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp index c73c155118..493102d9df 100644 --- a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp +++ b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp @@ -225,8 +225,9 @@ class PbftManager : public std::enable_shared_from_this { * @brief Reorder transactions data if DAG reordering caused transactions with same sender to have nonce in incorrect * order. Reordering is deterministic so that same order is produced on any node on any platform * @param transactions transactions to reorder + * @return true if there was reordering, false otherwise */ - static void reorderTransactions(SharedTransactions &transactions); + static bool reorderTransactions(SharedTransactions &transactions); /** * @brief Check a block weight of gas estimation diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index ba4c5d14ec..5033011129 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -1479,7 +1479,7 @@ void PbftManager::pushSyncedPbftBlocksIntoChain() { } } -void PbftManager::reorderTransactions(SharedTransactions &transactions) { +bool PbftManager::reorderTransactions(SharedTransactions &transactions) { // DAG reordering can cause transactions from same sender to be reordered by nonce. If this is the case only // transactions from these accounts are sorted and reordered, all other transactions keep the order SharedTransactions ordered_transactions; @@ -1528,8 +1528,15 @@ void PbftManager::reorderTransactions(SharedTransactions &transactions) { ordered_transactions.push_back(t); } } - transactions = ordered_transactions; + // just check if there is different order + for (uint32_t i = 0; i < transactions.size(); i++) { + if (transactions[i]->getHash() != ordered_transactions[i]->getHash()) { + transactions = ordered_transactions; + return true; + } + } } + return false; } void PbftManager::finalize_(PeriodData &&period_data, std::vector &&finalized_dag_blk_hashes, diff --git a/libraries/core_libs/storage/src/migration/period_data_reorder.cpp b/libraries/core_libs/storage/src/migration/period_data_reorder.cpp index bdc895c91b..9d60a78e13 100644 --- a/libraries/core_libs/storage/src/migration/period_data_reorder.cpp +++ b/libraries/core_libs/storage/src/migration/period_data_reorder.cpp @@ -36,14 +36,16 @@ void PeriodDataReorder::migrate(logger::Logger& log) { for (uint64_t i = start_period; i <= end_period; ++i) { executor.post([this, i]() { PeriodData period_data(db_->getPeriodDataRaw(i)); - PbftManager::reorderTransactions(period_data.transactions); - db_->insert(DB::Columns::period_data, i, period_data.rlp()); + const auto trx_cpy = period_data.transactions; + if (PbftManager::reorderTransactions(period_data.transactions)) { + db_->insert(DB::Columns::period_data, i, period_data.rlp()); + } }); // This should slow down main loop so we are not using so much memory while (executor.num_pending_tasks() > (executor.capacity() * 3)) { taraxa::thisThreadSleepForMilliSeconds(50); } - auto percentage = (i - start_period) * 100 / diff ; + auto percentage = (i - start_period) * 100 / diff; if (percentage > curr_progress) { curr_progress = percentage; LOG(log) << "Migration " << id() << " progress " << curr_progress << "%"; From 3f4c98ac33dca0d6cce03fcd94ff385d4840063d Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Mon, 29 May 2023 10:03:06 +0200 Subject: [PATCH 057/134] chore: improve API call --- .../consensus/src/final_chain/final_chain.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp index 752e256d27..f6fc4a375c 100644 --- a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp +++ b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp @@ -445,15 +445,10 @@ class FinalChainImpl final : public FinalChain { } const SharedTransactions get_transactions(std::optional n = {}) const { - SharedTransactions ret; - auto hashes = transaction_hashes(n); - ret.reserve(hashes->size()); - for (size_t i = 0; i < ret.capacity(); ++i) { - auto trx = db_->getTransaction(hashes->at(i)); - assert(trx); - ret.emplace_back(trx); + if (auto trxs = db_->getPeriodTransactions(last_if_absent(n))) { + return *trxs; } - return ret; + return {}; } std::shared_ptr get_block_header(EthBlockNumber n) const { From 086a0219dca9dde11200496949ac92a2f3c05aa5 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Mon, 29 May 2023 10:36:58 +0200 Subject: [PATCH 058/134] chore: add migrate flag --- libraries/cli/include/cli/config.hpp | 1 + libraries/cli/src/config.cpp | 4 ++++ libraries/config/include/config/config.hpp | 1 + .../core_libs/consensus/include/pbft/pbft_manager.hpp | 3 +-- .../core_libs/consensus/src/pbft/pbft_manager.cpp | 11 ++--------- libraries/core_libs/node/src/node.cpp | 9 +++++++-- .../core_libs/storage/src/migration/period_data.cpp | 2 +- libraries/core_libs/storage/src/storage.cpp | 1 + 8 files changed, 18 insertions(+), 14 deletions(-) diff --git a/libraries/cli/include/cli/config.hpp b/libraries/cli/include/cli/config.hpp index 99ed1cc28c..a6882c129b 100644 --- a/libraries/cli/include/cli/config.hpp +++ b/libraries/cli/include/cli/config.hpp @@ -60,6 +60,7 @@ class Config { static constexpr const char* OVERWRITE_CONFIG = "overwrite-config"; static constexpr const char* ENABLE_TEST_RPC = "enable-test-rpc"; static constexpr const char* ENABLE_DEBUG = "debug"; + static constexpr const char* MIGRATE_ONLY = "migrate-only"; std::string dirNameFromFile(const std::string& file); }; diff --git a/libraries/cli/src/config.cpp b/libraries/cli/src/config.cpp index 2a4c8fdf20..13d4f02b22 100644 --- a/libraries/cli/src/config.cpp +++ b/libraries/cli/src/config.cpp @@ -47,6 +47,7 @@ Config::Config(int argc, const char* argv[]) { bool enable_test_rpc = false; bool enable_debug = false; + bool migrate_only = false; // Set node as default command command.push_back(NODE_COMMAND); @@ -132,6 +133,8 @@ Config::Config(int argc, const char* argv[]) { node_command_options.add_options()(ENABLE_DEBUG, bpo::bool_switch(&enable_debug), "Enables Debug RPC interface. Disabled by default"); node_command_options.add_options()(PRUNE_STATE_DB, bpo::bool_switch(&prune_state_db), "Prune state_db"); + node_command_options.add_options()(MIGRATE_ONLY, bpo::bool_switch(&migrate_only), + "Only migrate DB, it will NOT run a node"); allowed_options.add(main_options); @@ -266,6 +269,7 @@ Config::Config(int argc, const char* argv[]) { node_config_.db_config.rebuild_db = rebuild_db; node_config_.db_config.prune_state_db = prune_state_db; node_config_.db_config.rebuild_db_period = rebuild_db_period; + node_config_.db_config.migrate_only = migrate_only; node_config_.enable_test_rpc = enable_test_rpc; node_config_.enable_debug = enable_debug; diff --git a/libraries/config/include/config/config.hpp b/libraries/config/include/config/config.hpp index 9a4388d0cb..6a537a3b93 100644 --- a/libraries/config/include/config/config.hpp +++ b/libraries/config/include/config/config.hpp @@ -16,6 +16,7 @@ struct DBConfig { PbftPeriod db_revert_to_period = 0; bool rebuild_db = false; bool prune_state_db = false; + bool migrate_only = false; PbftPeriod rebuild_db_period = 0; }; diff --git a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp index 22aa4fbaef..8924a25e67 100644 --- a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp +++ b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp @@ -224,9 +224,8 @@ class PbftManager : public std::enable_shared_from_this { * @brief Reorder transactions data if DAG reordering caused transactions with same sender to have nonce in incorrect * order. Reordering is deterministic so that same order is produced on any node on any platform * @param transactions transactions to reorder - * @return true if there was reordering, false otherwise */ - static bool reorderTransactions(SharedTransactions &transactions); + static void reorderTransactions(SharedTransactions &transactions); /** * @brief Check a block weight of gas estimation diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index 6379d12597..f343726283 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -1472,7 +1472,7 @@ void PbftManager::pushSyncedPbftBlocksIntoChain() { } } -bool PbftManager::reorderTransactions(SharedTransactions &transactions) { +void PbftManager::reorderTransactions(SharedTransactions &transactions) { // DAG reordering can cause transactions from same sender to be reordered by nonce. If this is the case only // transactions from these accounts are sorted and reordered, all other transactions keep the order SharedTransactions ordered_transactions; @@ -1521,15 +1521,8 @@ bool PbftManager::reorderTransactions(SharedTransactions &transactions) { ordered_transactions.push_back(t); } } - // just check if there is different order - for (uint32_t i = 0; i < transactions.size(); i++) { - if (transactions[i]->getHash() != ordered_transactions[i]->getHash()) { - transactions = ordered_transactions; - return true; - } - } + transactions = ordered_transactions; } - return false; } void PbftManager::finalize_(PeriodData &&period_data, std::vector &&finalized_dag_blk_hashes, diff --git a/libraries/core_libs/node/src/node.cpp b/libraries/core_libs/node/src/node.cpp index 6ceaeac46f..70648cec9c 100644 --- a/libraries/core_libs/node/src/node.cpp +++ b/libraries/core_libs/node/src/node.cpp @@ -80,11 +80,12 @@ void FullNode::init() { 0, // Snapshots should be disabled while rebuilding conf_.db_config.db_max_open_files, conf_.db_config.db_max_snapshots, conf_.db_config.db_revert_to_period, node_addr); - } else if (db_->hasMinorVersionChanged()) { - storage::migration::Manager(db_).applyAll(); } + db_->updateDbVersions(); + storage::migration::Manager(db_).applyAll(); + if (db_->getDagBlocksCount() == 0) { db_->setGenesisHash(conf_.genesis.genesisHash()); } @@ -316,6 +317,10 @@ void FullNode::start() { LOG(log_si_) << "Rebuild db completed successfully. Restart node without db_rebuild option"; started_ = false; return; + } else if (conf_.db_config.migrate_only) { + LOG(log_si_) << "DB migrated successfully, please restart the node without the flag"; + started_ = false; + return; } else { network_->start(); dag_block_proposer_->setNetwork(network_); diff --git a/libraries/core_libs/storage/src/migration/period_data.cpp b/libraries/core_libs/storage/src/migration/period_data.cpp index 238ff6c6c2..eea05e4bd8 100644 --- a/libraries/core_libs/storage/src/migration/period_data.cpp +++ b/libraries/core_libs/storage/src/migration/period_data.cpp @@ -29,7 +29,7 @@ void PeriodData::migrate(logger::Logger& log) { memcpy(&end_period, it->key().data(), sizeof(uint64_t)); util::ThreadPool executor{std::thread::hardware_concurrency()}; - const auto diff = end_period - start_period; + const auto diff = (end_period - start_period) ? (end_period - start_period) : 1; uint64_t curr_progress = 0; // Get and save data in new format for all blocks diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index fd1aae7691..b2e7dad8ca 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -113,6 +113,7 @@ void DbStorage::removeFilesWithPattern(const std::string& directory, const std:: void DbStorage::updateDbVersions() { saveStatusField(StatusDbField::DbMajorVersion, TARAXA_DB_MAJOR_VERSION); saveStatusField(StatusDbField::DbMinorVersion, TARAXA_DB_MINOR_VERSION); + kMajorVersion_ = TARAXA_DB_MAJOR_VERSION; } void DbStorage::deleteColumnData(const Column& c) { From a95c2292255323e639072ef00e46670daadf49c9 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Wed, 31 May 2023 08:03:37 +0200 Subject: [PATCH 059/134] chore: init storage loger as first object --- libraries/core_libs/storage/src/storage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 7ff25d8ad6..5216621212 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -42,6 +42,7 @@ DbStorage::DbStorage(fs::path const& path, uint32_t db_snapshot_each_n_pbft_bloc db_path_ = backup_db_path; state_db_path_ = backup_state_db_path; } + LOG_OBJECTS_CREATE("DBS"); fs::create_directories(db_path_); removeOldLogFiles(); @@ -62,7 +63,6 @@ DbStorage::DbStorage(fs::path const& path, uint32_t db_snapshot_each_n_pbft_bloc if (col.comparator_) options.comparator = col.comparator_; return rocksdb::ColumnFamilyDescriptor(col.name(), options); }); - LOG_OBJECTS_CREATE("DBS"); rebuildColumns(options); From b07b45e8a4bfcd93ac459b9eb081513b7157eb34 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Wed, 31 May 2023 09:37:39 +0200 Subject: [PATCH 060/134] chore: disable artifacts for circleci --- .circleci/config.yml | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 510b0b17a9..a4d5f6c0fc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -183,10 +183,10 @@ commands: - run: name: Cleanup PRNet IPs command: | - if [[ $(gcloud compute addresses list --format json --project << parameters.project >> --filter="name~'prnet-boot-<< parameters.pr-name >>.*'" ) != "[]" ]];then + if [[ $(gcloud compute addresses list --format json --project << parameters.project >> --filter="name~'prnet-boot-<< parameters.pr-name >>.*'" ) != "[]" ]];then gcloud compute addresses delete prnet-boot-<< parameters.pr-name >>-{1..3} --region << parameters.region >> --project << parameters.project >> --quiet fi - if [[ $(gcloud compute addresses list --format json --project << parameters.project >> --filter="name~'prnet-rpc-<< parameters.pr-name >>.*'" ) != "[]" ]];then + if [[ $(gcloud compute addresses list --format json --project << parameters.project >> --filter="name~'prnet-rpc-<< parameters.pr-name >>.*'" ) != "[]" ]];then gcloud compute addresses delete prnet-rpc-<< parameters.pr-name >>-{1..3} --region << parameters.region >> --project << parameters.project >> --quiet fi when: << parameters.when >> @@ -215,7 +215,7 @@ commands: echo -n "<< parameters.kubeconfig >>" | base64 --decode > $HOME/.kube/config curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" - sudo cp kubectl /usr/local/bin + sudo cp kubectl /usr/local/bin sudo chmod +x /usr/local/bin/kubectl when: << parameters.when >> @@ -305,7 +305,7 @@ commands: echo "waiting.. for all resources to be deleted" echo ${GETALL} sleep 10 - fi + fi done if [ $i -eq 50 ];then echo "Exiting after 500 seconds not all resources may have cleaned up" @@ -319,7 +319,7 @@ commands: steps: - run: name: Prepare env variables - command: | + command: | echo "export PR=$( [[ -z ${CIRCLE_PULL_REQUEST+x} ]] && echo false || echo $(basename ${CIRCLE_PULL_REQUEST}) )" >> $BASH_ENV export PR=$( [[ -z ${CIRCLE_PULL_REQUEST+x} ]] && echo false || echo $(basename ${CIRCLE_PULL_REQUEST}) ) echo "export SHORT_GIT_HASH=$(echo $CIRCLE_SHA1 | cut -c -7)" >> $BASH_ENV @@ -506,24 +506,24 @@ commands: if [ ! -z "$(docker network list --format '{{.Name}}' | grep -o smoketest-${VERSION})" ]; then docker network rm smoketest-${VERSION} >/dev/null; fi - + docker network create --driver=bridge \ smoketest-${VERSION} - + docker run -d\ --name smoketest-${VERSION} \ --net smoketest-${VERSION} \ ${IMAGE}:${VERSION} \ single - + mkdir -p $PWD/test_build-d/ - + sleep 30 echo "--- Logs from container ---" docker logs smoketest-${VERSION} echo "--- Logs from container ---" - + http_code=$(docker run --rm -v $PWD/test_build-d:/data \ --net smoketest-${VERSION} \ byrnedo/alpine-curl \ @@ -542,7 +542,7 @@ commands: "secret": "3800b2875669d9b2053c1aff9224ecfdc411423aac5b5a73d7a45ced1c3b9dcd" }] }') - + cat $PWD/test_build-d/http.out || true if [[ $http_code -eq 200 ]] ; then exit 0 @@ -703,8 +703,6 @@ jobs: - test - tag - push_gcr - - store_artifacts: - path: tmp_docker build-builder-docker-image: environment: @@ -729,7 +727,7 @@ jobs: - run: name: Show layers of taraxa-builder image command: | - docker history taraxa-builder:${VERSION} + docker history taraxa-builder:${VERSION} - tag_builder - push_dockerhub @@ -749,8 +747,6 @@ jobs: - test - tag - push_dockerhub - - store_artifacts: - path: tmp_docker create-and-push-builder: docker: @@ -1112,7 +1108,7 @@ workflows: - release-docker-image: filters: tags: - only: + only: - /^v?\d+\.\d+\.\d+$/ - /^v?\d+\.\d+\.\d+-beta$/ branches: @@ -1145,7 +1141,7 @@ workflows: - CHARTSREPO filters: branches: - only: + only: - /^chart\/.*/ - /^chore\/chart-.*/ - /^fix\/chart-.*/ From 62919a0c17f7325860eeb8b54de9c63afacff656 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 1 Jun 2023 06:48:31 +0200 Subject: [PATCH 061/134] chore: fix light node test --- tests/full_node_test.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/full_node_test.cpp b/tests/full_node_test.cpp index 730e79bcb3..74c74f7e03 100644 --- a/tests/full_node_test.cpp +++ b/tests/full_node_test.cpp @@ -1434,7 +1434,9 @@ TEST_F(FullNodeTest, light_node) { } // Verify light node keeps at least light_node_history and it deletes old blocks EXPECT_GE(non_empty_counter, node_cfgs[1].light_node_history); - EXPECT_LE(non_empty_counter, node_cfgs[1].light_node_history + 5); + // Actual history size will be between 100% and 110% of light_node_history_ to + // avoid deleting on every period + EXPECT_LE(non_empty_counter, node_cfgs[1].light_node_history * 1.1 + node_cfgs[1].dag_expiry_limit); } TEST_F(FullNodeTest, clear_period_data) { From d1760ebd35fe81d11b8adc43fc50923091bcf8df Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Thu, 1 Jun 2023 13:45:34 +0200 Subject: [PATCH 062/134] chore: reduce prune memory usage --- submodules/taraxa-evm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index 1aa1ced310..ab9aafbec8 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit 1aa1ced3109110d000d3210b362bb5c298c6624c +Subproject commit ab9aafbec8b663e569d1eee6fddda86869cbd5ec From f2ca8fffdb3d527dbea081a55774c58b11ff83ab Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Tue, 30 May 2023 19:11:15 -0700 Subject: [PATCH 063/134] copy column before aplying migration --- .../storage/include/storage/storage.hpp | 13 +++++- .../storage/src/migration/period_data.cpp | 19 ++++++-- libraries/core_libs/storage/src/storage.cpp | 46 +++++++++++++++++++ 3 files changed, 72 insertions(+), 6 deletions(-) diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index f8e8bdd47a..87c9672bdf 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -127,6 +127,8 @@ class DbStorage : public std::enable_shared_from_this { #undef COLUMN_W_COMP }; + auto handle(Column const& col) const { return handles_[col.ordinal_]; } + private: fs::path path_; fs::path db_path_; @@ -149,8 +151,6 @@ class DbStorage : public std::enable_shared_from_this { bool major_version_changed_ = false; bool minor_version_changed_ = false; - auto handle(Column const& col) const { return handles_[col.ordinal_]; } - LOG_OBJECTS_DEFINE public: @@ -181,12 +181,16 @@ class DbStorage : public std::enable_shared_from_this { void updateDbVersions(); void deleteColumnData(const Column& c); + void replaceColumn(const Column& to_be_replaced_col, rocksdb::ColumnFamilyHandle* replacing_col); + rocksdb::ColumnFamilyHandle* copyColumn(rocksdb::ColumnFamilyHandle* orig_column, const std::string& new_col_name, bool move_data = false); + // For removal of LOG.old.* files in the database void removeOldLogFiles() const; void removeFilesWithPattern(const std::string& directory, const std::regex& pattern) const; uint32_t getMajorVersion() const; std::unique_ptr getColumnIterator(const Column& c); + std::unique_ptr getColumnIterator(rocksdb::ColumnFamilyHandle* c); // Genesis void setGenesisHash(const h256& genesis_hash); @@ -399,6 +403,11 @@ class DbStorage : public std::enable_shared_from_this { static void checkStatus(rocksdb::Status const& status); + template + void insert(rocksdb::ColumnFamilyHandle* col, const K& k, const V& v) { + checkStatus(db_->Put(write_options_, col, toSlice(k), toSlice(v))); + } + template void insert(Column const& col, K const& k, V const& v) { checkStatus(db_->Put(write_options_, handle(col), toSlice(k), toSlice(v))); diff --git a/libraries/core_libs/storage/src/migration/period_data.cpp b/libraries/core_libs/storage/src/migration/period_data.cpp index eea05e4bd8..7dbfe718f4 100644 --- a/libraries/core_libs/storage/src/migration/period_data.cpp +++ b/libraries/core_libs/storage/src/migration/period_data.cpp @@ -13,7 +13,15 @@ std::string PeriodData::id() { return "PeriodData"; } uint32_t PeriodData::dbVersion() { return 1; } void PeriodData::migrate(logger::Logger& log) { - auto it = db_->getColumnIterator(DB::Columns::period_data); + auto orig_col = DB::Columns::period_data; + auto copied_col = db_->copyColumn(db_->handle(orig_col), orig_col.name() + "-copy"); + + if (copied_col == nullptr) { + LOG(log) << "Migration " << id() << " failed: Unable to copy " << orig_col.name() << " column"; + return; + } + + auto it = db_->getColumnIterator(copied_col); it->SeekToFirst(); if (!it->Valid()) { return; @@ -34,7 +42,7 @@ void PeriodData::migrate(logger::Logger& log) { // Get and save data in new format for all blocks for (uint64_t i = start_period; i <= end_period; ++i) { - executor.post([this, i]() { + executor.post([this, i, copied_col]() { const auto bytes = db_->getPeriodDataRaw(i); const auto period_data_old_rlp = dev::RLP(bytes); assert(period_data_old_rlp.itemCount() == 4); @@ -55,7 +63,7 @@ void PeriodData::migrate(logger::Logger& log) { // Reorder transactions PbftManager::reorderTransactions(period_data.transactions); - db_->insert(DB::Columns::period_data, i, period_data.rlp()); + db_->insert(copied_col, i, period_data.rlp()); }); // This should slow down main loop so we are not using so much memory while (executor.num_pending_tasks() > (executor.capacity() * 3)) { @@ -67,9 +75,12 @@ void PeriodData::migrate(logger::Logger& log) { LOG(log) << "Migration " << id() << " progress " << curr_progress << "%"; } } - // I know it's not perfect to check with sleep, but it's just migration that should be run once + + // It's not perfect to check with sleep, but it's just migration that should be run once do { taraxa::thisThreadSleepForMilliSeconds(100); } while (executor.num_pending_tasks()); + + db_->replaceColumn(orig_col, copied_col); } } // namespace taraxa::storage::migration \ No newline at end of file diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 5216621212..33b3166f4a 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -116,6 +116,48 @@ void DbStorage::updateDbVersions() { kMajorVersion_ = TARAXA_DB_MAJOR_VERSION; } +rocksdb::ColumnFamilyHandle* DbStorage::copyColumn(rocksdb::ColumnFamilyHandle* orig_column, const std::string& new_col_name, bool move_data) { + // Create rocskd checkpoint/snapshot + rocksdb::Checkpoint* checkpoint; + auto status = rocksdb::Checkpoint::Create(db_.get(), &checkpoint); + checkStatus(status); + + const fs::path export_dir = path() / "migrations" / new_col_name; + fs::create_directory(export_dir.parent_path()); + + // Export dir should not exist before exporting the column family + fs::remove_all(export_dir); + + rocksdb::ExportImportFilesMetaData* metadata; + status = checkpoint->ExportColumnFamily(orig_column, export_dir, &metadata); + checkStatus(status); + + const rocksdb::Comparator* comparator = orig_column->GetComparator(); + auto options = rocksdb::ColumnFamilyOptions(); + if (comparator != nullptr) { + options.comparator = comparator; + } + + rocksdb::ImportColumnFamilyOptions import_options; + import_options.move_files = move_data; + + rocksdb::ColumnFamilyHandle* copied_column; + status = db_->CreateColumnFamilyWithImport(options, new_col_name, import_options, *metadata, &copied_column); + checkStatus(status); + + // Remove export dir after successful import + fs::remove_all(export_dir); + + return copied_column; +} + +void DbStorage::replaceColumn(const Column& to_be_replaced_col, rocksdb::ColumnFamilyHandle* replacing_col) { + checkStatus(db_->DropColumnFamily(handle(to_be_replaced_col))); + + rocksdb::ColumnFamilyHandle* replaced_col = copyColumn(replacing_col, to_be_replaced_col.name(), true); + handles_[to_be_replaced_col.ordinal_] = replaced_col; +} + void DbStorage::deleteColumnData(const Column& c) { checkStatus(db_->DropColumnFamily(handle(c))); @@ -302,6 +344,10 @@ std::unique_ptr DbStorage::getColumnIterator(const Column& c) return std::unique_ptr(db_->NewIterator(read_options_, handle(c))); } +std::unique_ptr DbStorage::getColumnIterator(rocksdb::ColumnFamilyHandle* c) { + return std::unique_ptr(db_->NewIterator(read_options_, c)); +} + void DbStorage::checkStatus(rocksdb::Status const& status) { if (status.ok()) return; throw DbException(string("Db error. Status code: ") + std::to_string(status.code()) + From 9fb5a5437a93608e37afa107f5cce12049f0c66f Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Thu, 1 Jun 2023 19:16:54 -0700 Subject: [PATCH 064/134] fix potential memory leaks realted to column copying --- .../storage/include/storage/storage.hpp | 5 +-- .../storage/src/migration/period_data.cpp | 8 ++--- libraries/core_libs/storage/src/storage.cpp | 34 +++++++++++++------ 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index 87c9672bdf..a43a06624c 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -181,8 +181,9 @@ class DbStorage : public std::enable_shared_from_this { void updateDbVersions(); void deleteColumnData(const Column& c); - void replaceColumn(const Column& to_be_replaced_col, rocksdb::ColumnFamilyHandle* replacing_col); - rocksdb::ColumnFamilyHandle* copyColumn(rocksdb::ColumnFamilyHandle* orig_column, const std::string& new_col_name, bool move_data = false); + void replaceColumn(const Column& to_be_replaced_col, std::unique_ptr&& replacing_col); + std::unique_ptr copyColumn(rocksdb::ColumnFamilyHandle* orig_column, + const std::string& new_col_name, bool move_data = false); // For removal of LOG.old.* files in the database void removeOldLogFiles() const; diff --git a/libraries/core_libs/storage/src/migration/period_data.cpp b/libraries/core_libs/storage/src/migration/period_data.cpp index 7dbfe718f4..bff1da7e67 100644 --- a/libraries/core_libs/storage/src/migration/period_data.cpp +++ b/libraries/core_libs/storage/src/migration/period_data.cpp @@ -21,7 +21,7 @@ void PeriodData::migrate(logger::Logger& log) { return; } - auto it = db_->getColumnIterator(copied_col); + auto it = db_->getColumnIterator(copied_col.get()); it->SeekToFirst(); if (!it->Valid()) { return; @@ -42,7 +42,7 @@ void PeriodData::migrate(logger::Logger& log) { // Get and save data in new format for all blocks for (uint64_t i = start_period; i <= end_period; ++i) { - executor.post([this, i, copied_col]() { + executor.post([this, i, &copied_col]() { const auto bytes = db_->getPeriodDataRaw(i); const auto period_data_old_rlp = dev::RLP(bytes); assert(period_data_old_rlp.itemCount() == 4); @@ -63,7 +63,7 @@ void PeriodData::migrate(logger::Logger& log) { // Reorder transactions PbftManager::reorderTransactions(period_data.transactions); - db_->insert(copied_col, i, period_data.rlp()); + db_->insert(copied_col.get(), i, period_data.rlp()); }); // This should slow down main loop so we are not using so much memory while (executor.num_pending_tasks() > (executor.capacity() * 3)) { @@ -81,6 +81,6 @@ void PeriodData::migrate(logger::Logger& log) { taraxa::thisThreadSleepForMilliSeconds(100); } while (executor.num_pending_tasks()); - db_->replaceColumn(orig_col, copied_col); + db_->replaceColumn(orig_col, std::move(copied_col)); } } // namespace taraxa::storage::migration \ No newline at end of file diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 33b3166f4a..3b05febe30 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -116,10 +116,17 @@ void DbStorage::updateDbVersions() { kMajorVersion_ = TARAXA_DB_MAJOR_VERSION; } -rocksdb::ColumnFamilyHandle* DbStorage::copyColumn(rocksdb::ColumnFamilyHandle* orig_column, const std::string& new_col_name, bool move_data) { - // Create rocskd checkpoint/snapshot - rocksdb::Checkpoint* checkpoint; - auto status = rocksdb::Checkpoint::Create(db_.get(), &checkpoint); +std::unique_ptr DbStorage::copyColumn(rocksdb::ColumnFamilyHandle* orig_column, + const std::string& new_col_name, bool move_data) { + auto it = getColumnIterator(orig_column); + // No data to be copied + if (it->SeekToFirst(); !it->Valid()) { + return nullptr; + } + + rocksdb::Checkpoint* checkpoint_raw; + auto status = rocksdb::Checkpoint::Create(db_.get(), &checkpoint_raw); + std::unique_ptr checkpoint(checkpoint_raw); checkStatus(status); const fs::path export_dir = path() / "migrations" / new_col_name; @@ -128,8 +135,9 @@ rocksdb::ColumnFamilyHandle* DbStorage::copyColumn(rocksdb::ColumnFamilyHandle* // Export dir should not exist before exporting the column family fs::remove_all(export_dir); - rocksdb::ExportImportFilesMetaData* metadata; - status = checkpoint->ExportColumnFamily(orig_column, export_dir, &metadata); + rocksdb::ExportImportFilesMetaData* metadata_raw; + status = checkpoint->ExportColumnFamily(orig_column, export_dir, &metadata_raw); + std::unique_ptr metadata(metadata_raw); checkStatus(status); const rocksdb::Comparator* comparator = orig_column->GetComparator(); @@ -141,8 +149,9 @@ rocksdb::ColumnFamilyHandle* DbStorage::copyColumn(rocksdb::ColumnFamilyHandle* rocksdb::ImportColumnFamilyOptions import_options; import_options.move_files = move_data; - rocksdb::ColumnFamilyHandle* copied_column; - status = db_->CreateColumnFamilyWithImport(options, new_col_name, import_options, *metadata, &copied_column); + rocksdb::ColumnFamilyHandle* copied_column_raw; + status = db_->CreateColumnFamilyWithImport(options, new_col_name, import_options, *metadata, &copied_column_raw); + std::unique_ptr copied_column(copied_column_raw); checkStatus(status); // Remove export dir after successful import @@ -151,11 +160,14 @@ rocksdb::ColumnFamilyHandle* DbStorage::copyColumn(rocksdb::ColumnFamilyHandle* return copied_column; } -void DbStorage::replaceColumn(const Column& to_be_replaced_col, rocksdb::ColumnFamilyHandle* replacing_col) { +void DbStorage::replaceColumn(const Column& to_be_replaced_col, + std::unique_ptr&& replacing_col) { checkStatus(db_->DropColumnFamily(handle(to_be_replaced_col))); + db_->DestroyColumnFamilyHandle(handle(to_be_replaced_col)); - rocksdb::ColumnFamilyHandle* replaced_col = copyColumn(replacing_col, to_be_replaced_col.name(), true); - handles_[to_be_replaced_col.ordinal_] = replaced_col; + std::unique_ptr replaced_col = + copyColumn(replacing_col.get(), to_be_replaced_col.name(), true); + handles_[to_be_replaced_col.ordinal_] = replaced_col.release(); } void DbStorage::deleteColumnData(const Column& c) { From 09f32743cca84225a6d15f89f6258e307b4fc2c2 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Thu, 1 Jun 2023 19:20:35 -0700 Subject: [PATCH 065/134] add DestroyColumnFamilyHandle in deleteColumnData --- libraries/core_libs/storage/src/storage.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 3b05febe30..6f6e02c48f 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -172,6 +172,7 @@ void DbStorage::replaceColumn(const Column& to_be_replaced_col, void DbStorage::deleteColumnData(const Column& c) { checkStatus(db_->DropColumnFamily(handle(c))); + db_->DestroyColumnFamilyHandle(handle(c)); auto options = rocksdb::ColumnFamilyOptions(); if (c.comparator_) { From e773d27fb41612e2162d2e47238ab5d6aa1fb260 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Fri, 2 Jun 2023 10:51:12 -0700 Subject: [PATCH 066/134] added check in replaceColumn --- libraries/core_libs/storage/src/storage.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 6f6e02c48f..35ba70dfb7 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -167,6 +167,13 @@ void DbStorage::replaceColumn(const Column& to_be_replaced_col, std::unique_ptr replaced_col = copyColumn(replacing_col.get(), to_be_replaced_col.name(), true); + + if (!replaced_col) { + LOG(log_er_) << "Unable to replace column " << to_be_replaced_col.name() + << " by " << replacing_col->GetName() << " due to failed copy"; + return; + } + handles_[to_be_replaced_col.ordinal_] = replaced_col.release(); } From 688704970baeaa9472db596fcfe8912947fef965 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Fri, 2 Jun 2023 12:24:25 -0700 Subject: [PATCH 067/134] remove tests tha duplicate testing functionality from transfer_lot_of_blocks --- tests/network_test.cpp | 35 ----- tests/p2p_test.cpp | 333 +++++++++++++++-------------------------- 2 files changed, 122 insertions(+), 246 deletions(-) diff --git a/tests/network_test.cpp b/tests/network_test.cpp index 8223d854e2..75e667778a 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -38,41 +38,6 @@ auto g_signed_trx_samples = Lazy([] { return samples::createSignedTrxSamples(0, struct NetworkTest : NodesTest {}; -// Test creates two Network setup and verifies sending block between is successful -TEST_F(NetworkTest, transfer_block) { - auto nw1 = std::make_unique(node_cfgs[0]); - auto nw2 = std::make_unique(node_cfgs[1]); - - nw1->start(); - nw2->start(); - DagBlock blk(blk_hash_t(1111), 0, {blk_hash_t(222), blk_hash_t(333), blk_hash_t(444)}, - {g_signed_trx_samples[0]->getHash(), g_signed_trx_samples[1]->getHash()}, sig_t(7777), blk_hash_t(888), - addr_t(999)); - - SharedTransactions transactions({g_signed_trx_samples[0], g_signed_trx_samples[1]}); - nw2->getSpecificHandler()->onNewTransactions(std::move(transactions)); - - EXPECT_HAPPENS({60s, 100ms}, [&](auto& ctx) { - nw1->setPendingPeersToReady(); - nw2->setPendingPeersToReady(); - WAIT_EXPECT_EQ(ctx, nw1->getPeerCount(), 1) - WAIT_EXPECT_EQ(ctx, nw2->getPeerCount(), 1) - }); - - nw2->getSpecificHandler()->sendBlock(nw1->getNodeId(), blk, {}); - - std::cout << "Waiting packages for 10 seconds ..." << std::endl; - - for (int i = 0; i < 100; i++) { - if (nw1->getReceivedBlocksCount()) break; - taraxa::thisThreadSleepForMilliSeconds(100); - } - nw2 = nullptr; - unsigned long long num_received = nw1->getReceivedBlocksCount(); - nw1 = nullptr; - ASSERT_EQ(1, num_received); -} - // Test creates two Network setup and verifies sending blocks between is successful TEST_F(NetworkTest, transfer_lot_of_blocks) { auto node_cfgs = make_node_cfgs(2, 1, 20); diff --git a/tests/p2p_test.cpp b/tests/p2p_test.cpp index 8d621187a8..3f09e5f8c0 100644 --- a/tests/p2p_test.cpp +++ b/tests/p2p_test.cpp @@ -97,223 +97,134 @@ TEST_F(P2PTest, p2p_discovery) { }); } -/* -Test creates two host/network/capability and verifies that host connect -to each other and that a block packet message can be sent from one host -to the other using TaraxaCapability -*/ -TEST_F(P2PTest, capability_send_block) { - int const step = 10; - const char *const localhost = "127.0.0.1"; - dev::p2p::NetworkConfig prefs1(localhost, 10007, false, true); - prefs1.discovery = false; - dev::p2p::NetworkConfig prefs2(localhost, 10003, false, true); - prefs2.discovery = false; - - FullNodeConfig conf; - conf.network.transaction_interval_ms = 1000; - h256 genesis; - - std::shared_ptr thc1, thc2; - auto host1 = Host::make( - "Test", - [&](auto host) { - thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); - return Host::CapabilityList{thc1}; - }, - KeyPair::create(), prefs1); - auto host2 = Host::make( - "Test", - [&](auto host) { - thc2 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); - return Host::CapabilityList{thc2}; - }, - KeyPair::create(), prefs2); - - util::ThreadPool tp; - tp.post_loop({}, [=] { host1->do_work(); }); - tp.post_loop({}, [=] { host2->do_work(); }); - auto port1 = host1->listenPort(); - auto port2 = host2->listenPort(); - EXPECT_NE(port1, 0); - EXPECT_NE(port2, 0); - EXPECT_NE(port1, port2); - - host1->addNode( - Node(host2->id(), NodeIPEndpoint(bi::address::from_string(localhost), port2, port2), PeerType::Required)); - - // Wait for up to 12 seconds, to give the hosts time to connect to each - // other. - for (unsigned i = 0; i < 12000; i += step) { - std::this_thread::sleep_for(std::chrono::milliseconds(step)); - setPendingPeersToReady(thc1); - setPendingPeersToReady(thc2); - - if ((host1->peer_count() > 0) && (host2->peer_count() > 0)) break; - } - - EXPECT_GT(host1->peer_count(), 0); - EXPECT_GT(host2->peer_count(), 0); - - DagBlock blk(blk_hash_t(1111), 0, {blk_hash_t(222), blk_hash_t(333), blk_hash_t(444)}, - {g_signed_trx_samples[0]->getHash(), g_signed_trx_samples[1]->getHash()}, sig_t(7777), blk_hash_t(888), - addr_t(999)); - - SharedTransactions transactions{g_signed_trx_samples[0], g_signed_trx_samples[1]}; - thc2->getSpecificHandler()->onNewTransactions( - SharedTransactions(transactions)); - SharedTransactions transactions_to_send; - transactions_to_send.push_back(g_signed_trx_samples[0]); - transactions_to_send.push_back(g_signed_trx_samples[1]); - std::vector transactions_hashes; - transactions_hashes.push_back(g_signed_trx_samples[0]->getHash()); - transactions_hashes.push_back(g_signed_trx_samples[1]->getHash()); - thc2->getSpecificHandler()->sendTransactions( - thc2->getPeersState()->getPeer(host1->id()), std::move(transactions_to_send)); - thc2->getSpecificHandler()->sendBlock(host1->id(), blk, {}); - - std::this_thread::sleep_for(std::chrono::seconds(1)); - auto blocks = thc1->test_state_->getBlocks(); - auto rtransactions = thc1->test_state_->getTransactions(); - EXPECT_EQ(blocks.size(), 1); - if (blocks.size()) { - EXPECT_EQ(blk, blocks.begin()->second); - } - EXPECT_EQ(rtransactions.size(), 2); - if (rtransactions.size() == 2) { - EXPECT_EQ(*transactions[0], *rtransactions[g_signed_trx_samples[0]->getHash()]); - EXPECT_EQ(*transactions[1], *rtransactions[g_signed_trx_samples[1]->getHash()]); - } -} - /* Test creates 50 host/network/capability which connect to each other using node discovery. Block is created on one host and automatically propagated to all other hosts. Test verifies that each node has received the block */ -TEST_F(P2PTest, block_propagate) { - int const nodeCount = 10; - const char *const localhost = "127.0.0.1"; - dev::p2p::NetworkConfig prefs1(localhost, 10007, false, true); - std::vector vPrefs; - for (int i = 0; i < nodeCount; i++) { - vPrefs.push_back(dev::p2p::NetworkConfig(localhost, 10007 + i + 1, false, true)); - } - TaraxaNetworkConfig taraxa_net_conf_1; - taraxa_net_conf_1.is_boot_node = true; - - FullNodeConfig conf; - conf.network.transaction_interval_ms = 1000; - h256 genesis; - std::shared_ptr thc1; - auto host1 = Host::make( - "Test", - [&](auto host) { - thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); - return Host::CapabilityList{thc1}; - }, - KeyPair::create(), prefs1, taraxa_net_conf_1); - - util::ThreadPool tp; - tp.post_loop({}, [=] { host1->do_work(); }); - std::vector> vHosts; - std::vector> vCapabilities; - for (int i = 0; i < nodeCount; i++) { - auto host = vHosts.emplace_back(Host::make( - "Test", - [&](auto host) { - auto cap = vCapabilities.emplace_back(makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION)); - return Host::CapabilityList{cap}; - }, - KeyPair::create(), vPrefs[i])); - tp.post_loop({}, [=] { host->do_work(); }); - } - - printf("Starting %d hosts\n", nodeCount); - auto port1 = host1->listenPort(); - EXPECT_NE(port1, 0); - for (int i = 0; i < nodeCount; i++) { - EXPECT_NE(vHosts[i]->listenPort(), 0); - EXPECT_NE(port1, vHosts[i]->listenPort()); - for (int j = 0; j < i; j++) EXPECT_NE(vHosts[j]->listenPort(), vHosts[i]->listenPort()); - } - - for (int i = 0; i < nodeCount; i++) { - if (i < 10) - vHosts[i]->addNode(Node(host1->id(), NodeIPEndpoint(bi::address::from_string(localhost), port1, port1))); - else - vHosts[i]->addNode( - Node(vHosts[i % 10]->id(), NodeIPEndpoint(bi::address::from_string(localhost), vHosts[i % 10]->listenPort(), - vHosts[i % 10]->listenPort()))); - std::this_thread::sleep_for(std::chrono::milliseconds(20)); - } - - printf("Addnode %d hosts\n", nodeCount); - - // Wait for to give the hosts time to connect to each - // other. - bool connected = false; - for (unsigned i = 0; i < 500; i++) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - connected = true; - setPendingPeersToReady(thc1); - for (int j = 0; j < nodeCount; j++) { - setPendingPeersToReady(vCapabilities[j]); - if (vHosts[j]->peer_count() < 1) connected = false; - } - - if ((host1->peer_count() > 0) && connected) break; - } - EXPECT_TRUE(connected); - EXPECT_GT(host1->peer_count(), 0); - - DagBlock blk(blk_hash_t(1111), 0, {blk_hash_t(222), blk_hash_t(333), blk_hash_t(444)}, - {g_signed_trx_samples[0]->getHash(), g_signed_trx_samples[1]->getHash()}, sig_t(7777), kNullBlockHash, - addr_t(999)); - - SharedTransactions transactions{g_signed_trx_samples[0], g_signed_trx_samples[1]}; - thc1->getSpecificHandler()->onNewTransactions( - SharedTransactions(transactions)); - SharedTransactions transactions2; - thc1->getSpecificHandler()->onNewTransactions(std::move(transactions2)); - thc1->getSpecificHandler()->onNewBlockReceived(DagBlock(blk)); - - SharedTransactions transactions_to_send; - transactions_to_send.push_back(g_signed_trx_samples[0]); - transactions_to_send.push_back(g_signed_trx_samples[1]); - for (int i = 0; i < nodeCount; i++) { - thc1->getSpecificHandler()->sendTransactions( - thc1->getPeersState()->getPeer(vHosts[i]->id()), std::move(transactions_to_send)); - } - for (int i = 0; i < 50; i++) { - std::this_thread::sleep_for(std::chrono::seconds(1)); - bool synced = true; - for (int j = 0; j < nodeCount; j++) - if (vCapabilities[j]->test_state_->getBlocks().size() == 0) { - synced = false; - } - if (synced) break; - } - auto blocks1 = thc1->test_state_->getBlocks(); - for (int i = 0; i < nodeCount; i++) { - EXPECT_EQ(vCapabilities[i]->test_state_->getBlocks().size(), 1); - if (vCapabilities[i]->test_state_->getBlocks().size() == 1) { - EXPECT_EQ(vCapabilities[i]->test_state_->getBlocks().begin()->second, blk); - EXPECT_EQ(vCapabilities[i]->test_state_->getBlocks().begin()->second.getHash(), blk.getHash()); - } - auto rtransactions = vCapabilities[i]->test_state_->getTransactions(); - EXPECT_EQ(rtransactions.size(), 2); - if (rtransactions.size() == 2) { - EXPECT_EQ(*transactions[0], *rtransactions[g_signed_trx_samples[0]->getHash()]); - EXPECT_EQ(*transactions[1], *rtransactions[g_signed_trx_samples[1]->getHash()]); - } - } - EXPECT_EQ(blocks1.size(), 1); - if (blocks1.size()) { - EXPECT_EQ(blk, blocks1.begin()->second); - } -} +//TEST_F(P2PTest, block_propagate) { +// int const nodeCount = 10; +// const char *const localhost = "127.0.0.1"; +// dev::p2p::NetworkConfig prefs1(localhost, 10007, false, true); +// std::vector vPrefs; +// for (int i = 0; i < nodeCount; i++) { +// vPrefs.push_back(dev::p2p::NetworkConfig(localhost, 10007 + i + 1, false, true)); +// } +// TaraxaNetworkConfig taraxa_net_conf_1; +// taraxa_net_conf_1.is_boot_node = true; +// +// FullNodeConfig conf; +// conf.network.transaction_interval_ms = 1000; +// h256 genesis; +// std::shared_ptr thc1; +// auto host1 = Host::make( +// "Test", +// [&](auto host) { +// thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); +// return Host::CapabilityList{thc1}; +// }, +// KeyPair::create(), prefs1, taraxa_net_conf_1); +// +// util::ThreadPool tp; +// tp.post_loop({}, [=] { host1->do_work(); }); +// std::vector> vHosts; +// std::vector> vCapabilities; +// for (int i = 0; i < nodeCount; i++) { +// auto host = vHosts.emplace_back(Host::make( +// "Test", +// [&](auto host) { +// auto cap = vCapabilities.emplace_back(makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION)); +// return Host::CapabilityList{cap}; +// }, +// KeyPair::create(), vPrefs[i])); +// tp.post_loop({}, [=] { host->do_work(); }); +// } +// +// printf("Starting %d hosts\n", nodeCount); +// auto port1 = host1->listenPort(); +// EXPECT_NE(port1, 0); +// for (int i = 0; i < nodeCount; i++) { +// EXPECT_NE(vHosts[i]->listenPort(), 0); +// EXPECT_NE(port1, vHosts[i]->listenPort()); +// for (int j = 0; j < i; j++) EXPECT_NE(vHosts[j]->listenPort(), vHosts[i]->listenPort()); +// } +// +// for (int i = 0; i < nodeCount; i++) { +// if (i < 10) +// vHosts[i]->addNode(Node(host1->id(), NodeIPEndpoint(bi::address::from_string(localhost), port1, port1))); +// else +// vHosts[i]->addNode( +// Node(vHosts[i % 10]->id(), NodeIPEndpoint(bi::address::from_string(localhost), vHosts[i % 10]->listenPort(), +// vHosts[i % 10]->listenPort()))); +// std::this_thread::sleep_for(std::chrono::milliseconds(20)); +// } +// +// printf("Addnode %d hosts\n", nodeCount); +// +// // Wait for to give the hosts time to connect to each +// // other. +// bool connected = false; +// for (unsigned i = 0; i < 500; i++) { +// std::this_thread::sleep_for(std::chrono::milliseconds(100)); +// connected = true; +// setPendingPeersToReady(thc1); +// for (int j = 0; j < nodeCount; j++) { +// setPendingPeersToReady(vCapabilities[j]); +// if (vHosts[j]->peer_count() < 1) connected = false; +// } +// +// if ((host1->peer_count() > 0) && connected) break; +// } +// EXPECT_TRUE(connected); +// EXPECT_GT(host1->peer_count(), 0); +// +// DagBlock blk(blk_hash_t(1111), 0, {blk_hash_t(222), blk_hash_t(333), blk_hash_t(444)}, +// {g_signed_trx_samples[0]->getHash(), g_signed_trx_samples[1]->getHash()}, sig_t(7777), kNullBlockHash, +// addr_t(999)); +// +// SharedTransactions transactions{g_signed_trx_samples[0], g_signed_trx_samples[1]}; +// thc1->getSpecificHandler()->onNewTransactions( +// SharedTransactions(transactions)); +// SharedTransactions transactions2; +// thc1->getSpecificHandler()->onNewTransactions(std::move(transactions2)); +// thc1->getSpecificHandler()->onNewBlockReceived(DagBlock(blk)); +// +// SharedTransactions transactions_to_send; +// transactions_to_send.push_back(g_signed_trx_samples[0]); +// transactions_to_send.push_back(g_signed_trx_samples[1]); +// for (int i = 0; i < nodeCount; i++) { +// thc1->getSpecificHandler()->sendTransactions( +// thc1->getPeersState()->getPeer(vHosts[i]->id()), std::move(transactions_to_send)); +// } +// for (int i = 0; i < 50; i++) { +// std::this_thread::sleep_for(std::chrono::seconds(1)); +// bool synced = true; +// for (int j = 0; j < nodeCount; j++) +// if (vCapabilities[j]->test_state_->getBlocks().size() == 0) { +// synced = false; +// } +// if (synced) break; +// } +// auto blocks1 = thc1->test_state_->getBlocks(); +// for (int i = 0; i < nodeCount; i++) { +// EXPECT_EQ(vCapabilities[i]->test_state_->getBlocks().size(), 1); +// if (vCapabilities[i]->test_state_->getBlocks().size() == 1) { +// EXPECT_EQ(vCapabilities[i]->test_state_->getBlocks().begin()->second, blk); +// EXPECT_EQ(vCapabilities[i]->test_state_->getBlocks().begin()->second.getHash(), blk.getHash()); +// } +// auto rtransactions = vCapabilities[i]->test_state_->getTransactions(); +// EXPECT_EQ(rtransactions.size(), 2); +// if (rtransactions.size() == 2) { +// EXPECT_EQ(*transactions[0], *rtransactions[g_signed_trx_samples[0]->getHash()]); +// EXPECT_EQ(*transactions[1], *rtransactions[g_signed_trx_samples[1]->getHash()]); +// } +// } +// EXPECT_EQ(blocks1.size(), 1); +// if (blocks1.size()) { +// EXPECT_EQ(blk, blocks1.begin()->second); +// } +//} TEST_F(P2PTest, multiple_capabilities) { auto node_cfgs = make_node_cfgs(3); From 2da03107da7ca376cf1b8132dead582c4b51ff73 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Fri, 2 Jun 2023 14:21:36 -0700 Subject: [PATCH 068/134] remove test_state --- .../network/include/network/network.hpp | 2 - .../latest/dag_block_packet_handler.hpp | 4 +- .../latest/transaction_packet_handler.hpp | 10 +- .../v1/init_packets_handlers.hpp | 9 +- .../tarcap/shared_states/test_state.hpp | 34 ---- .../network/tarcap/taraxa_capability.hpp | 13 +- libraries/core_libs/network/src/network.cpp | 4 - .../latest/dag_block_packet_handler.cpp | 169 ++++++++---------- .../latest/transaction_packet_handler.cpp | 99 ++++------ .../src/tarcap/shared_states/test_state.cpp | 59 ------ .../network/src/tarcap/taraxa_capability.cpp | 21 +-- 11 files changed, 131 insertions(+), 293 deletions(-) delete mode 100644 libraries/core_libs/network/include/network/tarcap/shared_states/test_state.hpp delete mode 100644 libraries/core_libs/network/src/tarcap/shared_states/test_state.cpp diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index 00cdbc7823..07a5961e27 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -70,8 +70,6 @@ class Network { void setPendingPeersToReady(); dev::p2p::NodeID getNodeId() const; - int getReceivedBlocksCount() const; - int getReceivedTransactionsCount() const; std::shared_ptr getPeer(dev::p2p::NodeID const &id) const; // END METHODS USED IN TESTS ONLY diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp index 951d687536..8c22096c03 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp @@ -17,8 +17,7 @@ class DagBlockPacketHandler : public ExtSyncingPacketHandler { std::shared_ptr pbft_syncing_state, std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, std::shared_ptr db, - std::shared_ptr test_state, const addr_t &node_addr, - const std::string &logs_prefix = ""); + const addr_t &node_addr, const std::string &logs_prefix = ""); void sendBlock(dev::p2p::NodeID const &peer_id, DagBlock block, const SharedTransactions &trxs); void onNewBlockReceived(DagBlock &&block, const std::shared_ptr &peer = nullptr); @@ -32,7 +31,6 @@ class DagBlockPacketHandler : public ExtSyncingPacketHandler { virtual void process(const threadpool::PacketData &packet_data, const std::shared_ptr &peer) override; protected: - std::shared_ptr test_state_; std::shared_ptr trx_mgr_{nullptr}; }; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp index 0695d0dd85..0fc9d4400f 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp @@ -17,8 +17,8 @@ class TransactionPacketHandler : public PacketHandler { public: TransactionPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, - std::shared_ptr trx_mgr, std::shared_ptr test_state, - const addr_t& node_addr, const std::string& logs_prefix = "TRANSACTION_PH"); + std::shared_ptr trx_mgr, const addr_t& node_addr, + const std::string& logs_prefix = "TRANSACTION_PH"); /** * @brief Send transactions @@ -43,9 +43,6 @@ class TransactionPacketHandler : public PacketHandler { // 2 items: hashes and transactions static constexpr uint32_t kTransactionPacketItemCount = 2; - // Used only for unit tests - void onNewTransactions(const SharedTransactions& transactions); - private: virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override; virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr& peer) override; @@ -53,9 +50,6 @@ class TransactionPacketHandler : public PacketHandler { protected: std::shared_ptr trx_mgr_; - // FOR TESTING ONLY - std::shared_ptr test_state_; - std::atomic received_trx_count_{0}; std::atomic unique_received_trx_count_{0}; }; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp index c000527fbc..178711a526 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp @@ -20,7 +20,6 @@ namespace taraxa::network::tarcap::v1 { static const TaraxaCapability::InitPacketsHandlers kInitV1Handlers = [](const std::string &logs_prefix, const FullNodeConfig &config, const h256 &genesis_hash, const std::shared_ptr &peers_state, const std::shared_ptr &pbft_syncing_state, - const std::shared_ptr &test_state, const std::shared_ptr &packets_stats, const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, @@ -36,12 +35,12 @@ static const TaraxaCapability::InitPacketsHandlers kInitV1Handlers = config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr, logs_prefix); // Standard packets with mid processing priority - packets_handlers->registerHandler( - config, peers_state, packets_stats, pbft_syncing_state, pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, - test_state, node_addr, logs_prefix); + packets_handlers->registerHandler(config, peers_state, packets_stats, + pbft_syncing_state, pbft_chain, pbft_mgr, + dag_mgr, trx_mgr, db, node_addr, logs_prefix); packets_handlers->registerHandler(config, peers_state, packets_stats, trx_mgr, - test_state, node_addr, logs_prefix); + node_addr, logs_prefix); // Non critical packets with low processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, diff --git a/libraries/core_libs/network/include/network/tarcap/shared_states/test_state.hpp b/libraries/core_libs/network/include/network/tarcap/shared_states/test_state.hpp deleted file mode 100644 index 6d1c601bd8..0000000000 --- a/libraries/core_libs/network/include/network/tarcap/shared_states/test_state.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include - -#include "dag/dag_block.hpp" -#include "transaction/transaction.hpp" - -namespace taraxa::network::tarcap { - -// TODO: THIS NEEDS TO BE REMOVED !!!!!!! -class TestState { - public: - bool hasTransaction(const trx_hash_t& tx_hash) const; - void insertTransaction(const std::shared_ptr& tx); - const std::shared_ptr getTransaction(const trx_hash_t& tx_hash) const; - size_t getTransactionsSize() const; - - bool hasBlock(const blk_hash_t& block_hash) const; - void insertBlock(const DagBlock& tx); - const DagBlock& getBlock(const blk_hash_t& block_hash) const; - size_t getBlocksSize() const; - - std::unordered_map> getTransactions(); - std::unordered_map getBlocks(); - - private: - std::unordered_map> test_transactions_; - mutable std::shared_mutex transactions_mutex_; - - std::unordered_map test_blocks_; - mutable std::shared_mutex blocks_mutex_; -}; - -} // namespace taraxa::network::tarcap \ No newline at end of file diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp index 558a44bd6a..f653b00e33 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp @@ -11,7 +11,6 @@ #include "config/config.hpp" #include "network/tarcap/packets_handler.hpp" #include "network/tarcap/shared_states/peers_state.hpp" -#include "network/tarcap/shared_states/test_state.hpp" #include "network/tarcap/tarcap_version.hpp" #include "network/threadpool/tarcap_thread_pool.hpp" #include "pbft/pbft_chain.hpp" @@ -40,7 +39,7 @@ class TaraxaCapability final : public dev::p2p::CapabilityFace { using InitPacketsHandlers = std::function( const std::string &logs_prefix, const FullNodeConfig &config, const h256 &genesis_hash, const std::shared_ptr &peers_state, const std::shared_ptr &pbft_syncing_state, - const std::shared_ptr &test_state, + const std::shared_ptr &packets_stats, const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, @@ -82,19 +81,9 @@ class TaraxaCapability final : public dev::p2p::CapabilityFace { template std::shared_ptr getSpecificHandler() const; - // METHODS USED IN TESTS ONLY - size_t getReceivedBlocksCount() const; - size_t getReceivedTransactionsCount() const; - // END METHODS USED IN TESTS ONLY - private: bool filterSyncIrrelevantPackets(SubprotocolPacketType packet_type) const; - public: - // TODO: Remove in future when tests are refactored - // Test state - std::shared_ptr test_state_; - private: // Capability version unsigned version_; diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index 3a2dfcfcf9..e35a67490b 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -354,10 +354,6 @@ void Network::setPendingPeersToReady() { dev::p2p::NodeID Network::getNodeId() const { return host_->id(); } -int Network::getReceivedBlocksCount() const { return tarcaps_.begin()->second->getReceivedBlocksCount(); } - -int Network::getReceivedTransactionsCount() const { return tarcaps_.begin()->second->getReceivedTransactionsCount(); } - std::shared_ptr Network::getPeer(dev::p2p::NodeID const &id) const { return tarcaps_.begin()->second->getPeersState()->getPeer(id); } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp index be3b5cd89f..6438c98d57 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp @@ -3,7 +3,6 @@ #include "dag/dag_manager.hpp" #include "network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" -#include "network/tarcap/shared_states/test_state.hpp" #include "transaction/transaction_manager.hpp" namespace taraxa::network::tarcap { @@ -14,12 +13,10 @@ DagBlockPacketHandler::DagBlockPacketHandler(const FullNodeConfig &conf, std::sh std::shared_ptr pbft_chain, std::shared_ptr pbft_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, std::shared_ptr db, - std::shared_ptr test_state, const addr_t &node_addr, - const std::string &logs_prefix) + const addr_t &node_addr, const std::string &logs_prefix) : ExtSyncingPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_syncing_state), std::move(pbft_chain), std::move(pbft_mgr), std::move(dag_mgr), std::move(db), node_addr, logs_prefix + "DAG_BLOCK_PH"), - test_state_(std::move(test_state)), trx_mgr_(std::move(trx_mgr)) {} void DagBlockPacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { @@ -40,12 +37,10 @@ void DagBlockPacketHandler::process(const threadpool::PacketData &packet_data, peer->dag_level_ = block.getLevel(); } - if (dag_mgr_) [[likely]] { - // Do not process this block in case we already have it - if (dag_mgr_->isDagBlockKnown(block.getHash())) { - LOG(log_tr_) << "Received known DagBlockPacket " << hash << "from: " << peer->getId(); - return; - } + // Do not process this block in case we already have it + if (dag_mgr_->isDagBlockKnown(block.getHash())) { + LOG(log_tr_) << "Received known DagBlockPacket " << hash << "from: " << peer->getId(); + return; } onNewBlockReceived(std::move(block), peer); @@ -97,96 +92,88 @@ void DagBlockPacketHandler::sendBlock(dev::p2p::NodeID const &peer_id, taraxa::D } void DagBlockPacketHandler::onNewBlockReceived(DagBlock &&block, const std::shared_ptr &peer) { - if (dag_mgr_) [[likely]] { - const auto block_hash = block.getHash(); - const auto verified = dag_mgr_->verifyBlock(block); - switch (verified) { - case DagManager::VerifyBlockReturnType::IncorrectTransactionsEstimation: - case DagManager::VerifyBlockReturnType::BlockTooBig: - case DagManager::VerifyBlockReturnType::FailedVdfVerification: - case DagManager::VerifyBlockReturnType::NotEligible: - case DagManager::VerifyBlockReturnType::FailedTipsVerification: { + const auto block_hash = block.getHash(); + const auto verified = dag_mgr_->verifyBlock(block); + switch (verified) { + case DagManager::VerifyBlockReturnType::IncorrectTransactionsEstimation: + case DagManager::VerifyBlockReturnType::BlockTooBig: + case DagManager::VerifyBlockReturnType::FailedVdfVerification: + case DagManager::VerifyBlockReturnType::NotEligible: + case DagManager::VerifyBlockReturnType::FailedTipsVerification: { + std::ostringstream err_msg; + err_msg << "DagBlock " << block_hash << " failed verification with error code " + << static_cast(verified); + throw MaliciousPeerException(err_msg.str()); + } + case DagManager::VerifyBlockReturnType::MissingTransaction: + if (peer->dagSyncingAllowed()) { + if (trx_mgr_->transactionsDropped()) [[unlikely]] { + LOG(log_nf_) << "NewBlock " << block_hash.toString() << " from peer " << peer->getId() + << " is missing transaction, our pool recently dropped transactions, requesting dag sync"; + } else { + LOG(log_wr_) << "NewBlock " << block_hash.toString() << " from peer " << peer->getId() + << " is missing transaction, requesting dag sync"; + } + peer->peer_dag_synced_ = false; + requestPendingDagBlocks(peer); + } else { + if (trx_mgr_->transactionsDropped()) [[unlikely]] { + // Disconnecting since anything after will also contain missing pivot/tips ... + LOG(log_nf_) << "NewBlock " << block_hash.toString() << " from peer " << peer->getId() + << " is missing transaction, but our pool recently dropped transactions, disconnecting"; + disconnect(peer->getId(), dev::p2p::UserReason); + } else { + std::ostringstream err_msg; + err_msg << "DagBlock" << block_hash << " is missing a transaction while in a dag synced state"; + throw MaliciousPeerException(err_msg.str()); + } + } + break; + case DagManager::VerifyBlockReturnType::MissingTip: + if (peer->peer_dag_synced_) { std::ostringstream err_msg; - err_msg << "DagBlock " << block_hash << " failed verification with error code " - << static_cast(verified); + err_msg << "DagBlock has missing tip"; throw MaliciousPeerException(err_msg.str()); + } else { + // peer_dag_synced_ flag ensures that this can only be performed once for a peer + requestPendingDagBlocks(peer); } - case DagManager::VerifyBlockReturnType::MissingTransaction: - if (peer->dagSyncingAllowed()) { - if (trx_mgr_->transactionsDropped()) [[unlikely]] { - LOG(log_nf_) << "NewBlock " << block_hash.toString() << " from peer " << peer->getId() - << " is missing transaction, our pool recently dropped transactions, requesting dag sync"; - } else { - LOG(log_wr_) << "NewBlock " << block_hash.toString() << " from peer " << peer->getId() - << " is missing transaction, requesting dag sync"; - } - peer->peer_dag_synced_ = false; - requestPendingDagBlocks(peer); + break; + case DagManager::VerifyBlockReturnType::AheadBlock: + case DagManager::VerifyBlockReturnType::FutureBlock: + if (peer->peer_dag_synced_) { + LOG(log_er_) << "DagBlock" << block_hash << " is an ahead/future block. Peer " << peer->getId() + << " will be disconnected"; + disconnect(peer->getId(), dev::p2p::UserReason); + } + break; + case DagManager::VerifyBlockReturnType::Verified: { + auto transactions = trx_mgr_->getPoolTransactions(block.getTrxs()).first; + auto status = dag_mgr_->addDagBlock(std::move(block), std::move(transactions)); + if (!status.first) { + LOG(log_dg_) << "Received DagBlockPacket " << block_hash << "from: " << peer->getId(); + // Ignore new block packets when pbft syncing + if (pbft_syncing_state_->isPbftSyncing()) { + LOG(log_dg_) << "Ignore new dag block " << block_hash << ", pbft syncing is on"; + } else if (peer->peer_dag_syncing_) { + LOG(log_dg_) << "Ignore new dag block " << block_hash << ", dag syncing is on"; } else { - if (trx_mgr_->transactionsDropped()) [[unlikely]] { - // Disconnecting since anything after will also contain missing pivot/tips ... - LOG(log_nf_) << "NewBlock " << block_hash.toString() << " from peer " << peer->getId() - << " is missing transaction, but our pool recently dropped transactions, disconnecting"; - disconnect(peer->getId(), dev::p2p::UserReason); - } else { + if (peer->peer_dag_synced_) { std::ostringstream err_msg; - err_msg << "DagBlock" << block_hash << " is missing a transaction while in a dag synced state"; + if (status.second.size() > 0) + err_msg << "DagBlock" << block.getHash() << " has missing pivot or/and tips " << status.second; + else + err_msg << "DagBlock" << block.getHash() << " could not be added to DAG"; throw MaliciousPeerException(err_msg.str()); - } - } - break; - case DagManager::VerifyBlockReturnType::MissingTip: - if (peer->peer_dag_synced_) { - std::ostringstream err_msg; - err_msg << "DagBlock has missing tip"; - throw MaliciousPeerException(err_msg.str()); - } else { - // peer_dag_synced_ flag ensures that this can only be performed once for a peer - requestPendingDagBlocks(peer); - } - break; - case DagManager::VerifyBlockReturnType::AheadBlock: - case DagManager::VerifyBlockReturnType::FutureBlock: - if (peer->peer_dag_synced_) { - LOG(log_er_) << "DagBlock" << block_hash << " is an ahead/future block. Peer " << peer->getId() - << " will be disconnected"; - disconnect(peer->getId(), dev::p2p::UserReason); - } - break; - case DagManager::VerifyBlockReturnType::Verified: { - auto transactions = trx_mgr_->getPoolTransactions(block.getTrxs()).first; - auto status = dag_mgr_->addDagBlock(std::move(block), std::move(transactions)); - if (!status.first) { - LOG(log_dg_) << "Received DagBlockPacket " << block_hash << "from: " << peer->getId(); - // Ignore new block packets when pbft syncing - if (pbft_syncing_state_->isPbftSyncing()) { - LOG(log_dg_) << "Ignore new dag block " << block_hash << ", pbft syncing is on"; - } else if (peer->peer_dag_syncing_) { - LOG(log_dg_) << "Ignore new dag block " << block_hash << ", dag syncing is on"; } else { - if (peer->peer_dag_synced_) { - std::ostringstream err_msg; - if (status.second.size() > 0) - err_msg << "DagBlock" << block.getHash() << " has missing pivot or/and tips " << status.second; - else - err_msg << "DagBlock" << block.getHash() << " could not be added to DAG"; - throw MaliciousPeerException(err_msg.str()); - } else { - // peer_dag_synced_ flag ensures that this can only be performed once for a peer - requestPendingDagBlocks(peer); - } + // peer_dag_synced_ flag ensures that this can only be performed once for a peer + requestPendingDagBlocks(peer); } } - } break; - case DagManager::VerifyBlockReturnType::ExpiredBlock: - break; - } - } else if (!test_state_->hasBlock(block.getHash())) { - test_state_->insertBlock(block); - onNewBlockVerified(block, false, {}); - } else { - LOG(log_tr_) << "Received NewBlock " << block.getHash() << "that is already known"; - return; + } + } break; + case DagManager::VerifyBlockReturnType::ExpiredBlock: + break; } } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp index 2206278cb1..ec3f5693ae 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/transaction_packet_handler.cpp @@ -2,7 +2,6 @@ #include -#include "network/tarcap/shared_states/test_state.hpp" #include "transaction/transaction.hpp" #include "transaction/transaction_manager.hpp" @@ -10,12 +9,10 @@ namespace taraxa::network::tarcap { TransactionPacketHandler::TransactionPacketHandler(const FullNodeConfig &conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, - std::shared_ptr trx_mgr, - std::shared_ptr test_state, const addr_t &node_addr, + std::shared_ptr trx_mgr, const addr_t &node_addr, const std::string &logs_prefix) : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, logs_prefix + "TRANSACTION_PH"), - trx_mgr_(std::move(trx_mgr)), - test_state_(std::move(test_state)) {} + trx_mgr_(std::move(trx_mgr)) {} void TransactionPacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); @@ -53,55 +50,50 @@ inline void TransactionPacketHandler::process(const threadpool::PacketData &pack for (size_t tx_idx = 0; tx_idx < transaction_count; tx_idx++) { const auto &trx_hash = trx_hashes[tx_idx]; - if (trx_mgr_) [[likely]] { // ONLY FOR TESTING - // Skip any transactions that are already known to the trx mgr - if (trx_mgr_->isTransactionKnown(trx_hash)) { - continue; - } + // Skip any transactions that are already known to the trx mgr + if (trx_mgr_->isTransactionKnown(trx_hash)) { + continue; + } - std::shared_ptr transaction; - // Deserialization is expensive, do it only for the transactions we are about to process - try { - transaction = std::make_shared(packet_data.rlp_[1][tx_idx].data().toBytes()); - received_transactions.emplace_back(trx_hash); - } catch (const Transaction::InvalidTransaction &e) { - throw MaliciousPeerException("Unable to parse transaction: " + std::string(e.what())); - } + std::shared_ptr transaction; + // Deserialization is expensive, do it only for the transactions we are about to process + try { + transaction = std::make_shared(packet_data.rlp_[1][tx_idx].data().toBytes()); + received_transactions.emplace_back(trx_hash); + } catch (const Transaction::InvalidTransaction &e) { + throw MaliciousPeerException("Unable to parse transaction: " + std::string(e.what())); + } - TransactionStatus status; - std::string reason; - std::tie(status, reason) = trx_mgr_->verifyTransaction(transaction); - switch (status) { - case TransactionStatus::Invalid: { + TransactionStatus status; + std::string reason; + std::tie(status, reason) = trx_mgr_->verifyTransaction(transaction); + switch (status) { + case TransactionStatus::Invalid: { + std::ostringstream err_msg; + err_msg << "DagBlock transaction " << transaction->getHash() << " validation failed: " << reason; + throw MaliciousPeerException(err_msg.str()); + } + case TransactionStatus::InsufficentBalance: + case TransactionStatus::LowNonce: { + // Raise exception in trx pool is over the limit and this peer already has too many suspicious packets + if (peer->reportSuspiciousPacket() && trx_mgr_->nonProposableTransactionsOverTheLimit()) { std::ostringstream err_msg; - err_msg << "DagBlock transaction " << transaction->getHash() << " validation failed: " << reason; + err_msg << "Suspicious packets over the limit on DagBlock transaction " << transaction->getHash() + << " validation: " << reason; throw MaliciousPeerException(err_msg.str()); } - case TransactionStatus::InsufficentBalance: - case TransactionStatus::LowNonce: { - // Raise exception in trx pool is over the limit and this peer already has too many suspicious packets - if (peer->reportSuspiciousPacket() && trx_mgr_->nonProposableTransactionsOverTheLimit()) { - std::ostringstream err_msg; - err_msg << "Suspicious packets over the limit on DagBlock transaction " << transaction->getHash() - << " validation: " << reason; - throw MaliciousPeerException(err_msg.str()); - } - - break; - } - case TransactionStatus::Verified: - break; - default: - assert(false); - } - received_trx_count_++; - if (trx_mgr_->insertValidatedTransaction(std::move(transaction), std::move(status))) { - unique_received_trx_count_++; + break; } - } else { - // Only for unit tests - onNewTransactions({std::make_shared(packet_data.rlp_[1][tx_idx].data().toBytes())}); + case TransactionStatus::Verified: + break; + default: + assert(false); + } + + received_trx_count_++; + if (trx_mgr_->insertValidatedTransaction(std::move(transaction), std::move(status))) { + unique_received_trx_count_++; } } @@ -112,19 +104,6 @@ inline void TransactionPacketHandler::process(const threadpool::PacketData &pack } } -void TransactionPacketHandler::onNewTransactions(const SharedTransactions &transactions) { - // Only for testing - for (auto const &trx : transactions) { - auto trx_hash = trx->getHash(); - if (!test_state_->hasTransaction(trx_hash)) { - test_state_->insertTransaction(trx); - LOG(log_tr_) << "Received New Transaction " << trx_hash; - } else { - LOG(log_tr_) << "Received New Transaction" << trx_hash << "that is already known"; - } - } -} - void TransactionPacketHandler::periodicSendTransactions(SharedTransactions &&transactions) { std::vector> peers_with_transactions_to_send; diff --git a/libraries/core_libs/network/src/tarcap/shared_states/test_state.cpp b/libraries/core_libs/network/src/tarcap/shared_states/test_state.cpp deleted file mode 100644 index 273fed3c70..0000000000 --- a/libraries/core_libs/network/src/tarcap/shared_states/test_state.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "network/tarcap/shared_states/test_state.hpp" - -namespace taraxa::network::tarcap { - -bool TestState::hasTransaction(const trx_hash_t& tx_hash) const { - std::shared_lock lock(transactions_mutex_); - return test_transactions_.count(tx_hash); -} - -void TestState::insertTransaction(const std::shared_ptr& tx) { - std::unique_lock lock(transactions_mutex_); - test_transactions_.emplace(tx->getHash(), tx); -} - -const std::shared_ptr TestState::getTransaction(const trx_hash_t& tx_hash) const { - std::shared_lock lock(transactions_mutex_); - assert(test_transactions_.count(tx_hash)); - - return test_transactions_.at(tx_hash); -} - -size_t TestState::getTransactionsSize() const { - std::shared_lock lock(transactions_mutex_); - return test_transactions_.size(); -} - -bool TestState::hasBlock(const blk_hash_t& block_hash) const { - std::shared_lock lock(blocks_mutex_); - return test_blocks_.count(block_hash); -} - -void TestState::insertBlock(const DagBlock& block) { - std::unique_lock lock(blocks_mutex_); - test_blocks_.emplace(block.getHash(), block); -} - -const DagBlock& TestState::getBlock(const blk_hash_t& block_hash) const { - std::shared_lock lock(blocks_mutex_); - assert(test_blocks_.count(block_hash)); - - return test_blocks_.at(block_hash); -} - -size_t TestState::getBlocksSize() const { - std::shared_lock lock(blocks_mutex_); - return test_blocks_.size(); -} - -std::unordered_map> TestState::getTransactions() { - std::shared_lock lock(transactions_mutex_); - return test_transactions_; -} - -std::unordered_map TestState::getBlocks() { - std::shared_lock lock(blocks_mutex_); - return test_blocks_; -} - -} // namespace taraxa::network::tarcap \ No newline at end of file diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index dc605e7d09..0461816e7d 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -14,7 +14,6 @@ #include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" -#include "network/tarcap/shared_states/test_state.hpp" #include "node/node.hpp" #include "pbft/pbft_chain.hpp" #include "pbft/pbft_manager.hpp" @@ -32,8 +31,7 @@ TaraxaCapability::TaraxaCapability(TarcapVersion version, const FullNodeConfig & std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, InitPacketsHandlers init_packets_handlers) - : test_state_(std::make_shared()), - version_(version), + : version_(version), all_packets_stats_(std::move(packets_stats)), kConf(conf), peers_state_(nullptr), @@ -47,8 +45,8 @@ TaraxaCapability::TaraxaCapability(TarcapVersion version, const FullNodeConfig & peers_state_ = std::make_shared(host, kConf); packets_handlers_ = - init_packets_handlers(logs_prefix, conf, genesis_hash, peers_state_, pbft_syncing_state_, test_state_, - all_packets_stats_, db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, node_addr); + init_packets_handlers(logs_prefix, conf, genesis_hash, peers_state_, pbft_syncing_state_, all_packets_stats_, db, + pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, node_addr); // Must be called after init_packets_handlers thread_pool_->setPacketsHandlers(version, packets_handlers_); @@ -206,16 +204,9 @@ inline bool TaraxaCapability::filterSyncIrrelevantPackets(SubprotocolPacketType const std::shared_ptr &TaraxaCapability::getPeersState() { return peers_state_; } -// METHODS USED IN TESTS ONLY -size_t TaraxaCapability::getReceivedBlocksCount() const { return test_state_->getBlocksSize(); } - -size_t TaraxaCapability::getReceivedTransactionsCount() const { return test_state_->getTransactionsSize(); } -// END METHODS USED IN TESTS ONLY - const TaraxaCapability::InitPacketsHandlers TaraxaCapability::kInitLatestVersionHandlers = [](const std::string &logs_prefix, const FullNodeConfig &config, const h256 &genesis_hash, const std::shared_ptr &peers_state, const std::shared_ptr &pbft_syncing_state, - const std::shared_ptr &test_state, const std::shared_ptr &packets_stats, const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, @@ -231,11 +222,11 @@ const TaraxaCapability::InitPacketsHandlers TaraxaCapability::kInitLatestVersion // Standard packets with mid processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, - pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, test_state, - node_addr, logs_prefix); + pbft_chain, pbft_mgr, dag_mgr, trx_mgr, db, node_addr, + logs_prefix); packets_handlers->registerHandler(config, peers_state, packets_stats, trx_mgr, - test_state, node_addr, logs_prefix); + node_addr, logs_prefix); // Non critical packets with low processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, From a4e385e4aeab08f3a10105f24b9d425dc8a132e7 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Fri, 2 Jun 2023 14:22:06 -0700 Subject: [PATCH 069/134] replace dummy propagate_block test with real one --- tests/network_test.cpp | 68 +++++++++++++++++++--- tests/p2p_test.cpp | 129 ----------------------------------------- 2 files changed, 59 insertions(+), 138 deletions(-) diff --git a/tests/network_test.cpp b/tests/network_test.cpp index 75e667778a..ce44f1dfec 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -33,7 +33,6 @@ auto g_secret = Lazy([] { return dev::Secret("3800b2875669d9b2053c1aff9224ecfdc411423aac5b5a73d7a45ced1c3b9dcd", dev::Secret::ConstructFromStringType::FromHex); }); -auto node_key = dev::KeyPair(g_secret); auto g_signed_trx_samples = Lazy([] { return samples::createSignedTrxSamples(0, NUM_TRX, g_secret); }); struct NetworkTest : NodesTest {}; @@ -106,6 +105,51 @@ TEST_F(NetworkTest, transfer_lot_of_blocks) { wait({120s, 200ms}, [&](auto& ctx) { WAIT_EXPECT_NE(ctx, dag_mgr2->getDagBlock(block_hash), nullptr) }); } +// TODO: debug why the test take so long... + TEST_F(NetworkTest, propagate_block) { + auto node_cfgs = make_node_cfgs(5, 1); + auto nodes = launch_nodes(node_cfgs); + const auto& node1 = nodes[0]; + + // Stop PBFT manager + for (auto& node : nodes) { + node->getPbftManager()->stop(); + } + + const auto db1 = node1->getDB(); + const auto dag_mgr1 = node1->getDagManager(); + + auto trxs = samples::createSignedTrxSamples(0, 1, g_secret); + const auto estimation = node1->getTransactionManager()->estimateTransactionGas(trxs[0], {}); + + // node1 add one valid block + const auto proposal_level = 1; + const auto proposal_period = *db1->getProposalPeriodForDagLevel(proposal_level); + const auto period_block_hash = db1->getPeriodBlockHash(proposal_period); + const auto sortition_params = dag_mgr1->sortitionParamsManager().getSortitionParams(proposal_period); + vdf_sortition::VdfSortition vdf(sortition_params, node1->getVrfSecretKey(), + VrfSortitionBase::makeVrfInput(proposal_level, period_block_hash), 1, 1); + const auto dag_genesis = node1->getConfig().genesis.dag_genesis_block.getHash(); + dev::bytes vdf_msg = DagManager::getVdfMessage(dag_genesis, {trxs[0]}); + vdf.computeVdfSolution(sortition_params, vdf_msg, false); + DagBlock blk(dag_genesis, proposal_level, {}, {trxs[0]->getHash()}, estimation, vdf, node1->getSecretKey()); + + const auto block_hash = blk.getHash(); + + // Add block gossip it to connected peers + dag_mgr1->addDagBlock(std::move(blk), {trxs[0]}); + + wait({1s, 200ms}, [&](auto& ctx) { WAIT_EXPECT_NE(ctx, dag_mgr1->getDagBlock(block_hash), nullptr) }); + + std::cout << "Waiting Sync ..." << std::endl; + wait({20s, 200ms}, [&](auto& ctx) { + for (const auto& node : nodes) { + const auto dag_mgr = node->getDagManager(); + WAIT_EXPECT_NE(ctx, dag_mgr->getDagBlock(block_hash), nullptr) + } + }); + } + TEST_F(NetworkTest, DISABLED_update_peer_chainsize) { auto node_cfgs = make_node_cfgs(2, 1, 5); auto nodes = launch_nodes(node_cfgs); @@ -278,10 +322,17 @@ TEST_F(NetworkTest, sync_large_pbft_block) { // Test creates two Network setup and verifies sending transaction // between is successfull TEST_F(NetworkTest, transfer_transaction) { - auto nw1 = std::make_unique(node_cfgs[0]); - auto nw2 = std::make_unique(node_cfgs[1]); - nw1->start(); - nw2->start(); + auto node_cfgs = make_node_cfgs(2, 0, 20); + auto nodes = launch_nodes(node_cfgs); + const auto& node1 = nodes[0]; + const auto& node2 = nodes[1]; + + // Stop PBFT manager + node1->getPbftManager()->stop(); + node2->getPbftManager()->stop(); + + const auto nw1 = node1->getNetwork(); + const auto nw2 = node2->getNetwork(); EXPECT_HAPPENS({60s, 100ms}, [&](auto& ctx) { nw1->setPendingPeersToReady(); @@ -300,13 +351,12 @@ TEST_F(NetworkTest, transfer_transaction) { SharedTransactions transactions; transactions.push_back(g_signed_trx_samples[0]); - transactions.push_back(g_signed_trx_samples[1]); - transactions.push_back(g_signed_trx_samples[2]); nw2->getSpecificHandler()->sendTransactions(peer1, std::move(transactions)); - - EXPECT_HAPPENS({2s, 200ms}, [&](auto& ctx) { WAIT_EXPECT_EQ(ctx, nw1->getReceivedTransactionsCount(), 3) }); + const auto tx_mgr1 = node1->getTransactionManager(); + EXPECT_HAPPENS({2s, 200ms}, + [&](auto& ctx) { WAIT_EXPECT_TRUE(ctx, tx_mgr1->getTransaction(g_signed_trx_samples[0]->getHash())) }); } // Test verifies saving network to a file and restoring it from a file diff --git a/tests/p2p_test.cpp b/tests/p2p_test.cpp index 3f09e5f8c0..bb50322b61 100644 --- a/tests/p2p_test.cpp +++ b/tests/p2p_test.cpp @@ -97,135 +97,6 @@ TEST_F(P2PTest, p2p_discovery) { }); } -/* -Test creates 50 host/network/capability which connect to each other -using node discovery. Block is created on one host and automatically -propagated to all other hosts. Test verifies that each node has received -the block -*/ -//TEST_F(P2PTest, block_propagate) { -// int const nodeCount = 10; -// const char *const localhost = "127.0.0.1"; -// dev::p2p::NetworkConfig prefs1(localhost, 10007, false, true); -// std::vector vPrefs; -// for (int i = 0; i < nodeCount; i++) { -// vPrefs.push_back(dev::p2p::NetworkConfig(localhost, 10007 + i + 1, false, true)); -// } -// TaraxaNetworkConfig taraxa_net_conf_1; -// taraxa_net_conf_1.is_boot_node = true; -// -// FullNodeConfig conf; -// conf.network.transaction_interval_ms = 1000; -// h256 genesis; -// std::shared_ptr thc1; -// auto host1 = Host::make( -// "Test", -// [&](auto host) { -// thc1 = makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION); -// return Host::CapabilityList{thc1}; -// }, -// KeyPair::create(), prefs1, taraxa_net_conf_1); -// -// util::ThreadPool tp; -// tp.post_loop({}, [=] { host1->do_work(); }); -// std::vector> vHosts; -// std::vector> vCapabilities; -// for (int i = 0; i < nodeCount; i++) { -// auto host = vHosts.emplace_back(Host::make( -// "Test", -// [&](auto host) { -// auto cap = vCapabilities.emplace_back(makeTarcap(host, KeyPair::create(), conf, genesis, TARAXA_NET_VERSION)); -// return Host::CapabilityList{cap}; -// }, -// KeyPair::create(), vPrefs[i])); -// tp.post_loop({}, [=] { host->do_work(); }); -// } -// -// printf("Starting %d hosts\n", nodeCount); -// auto port1 = host1->listenPort(); -// EXPECT_NE(port1, 0); -// for (int i = 0; i < nodeCount; i++) { -// EXPECT_NE(vHosts[i]->listenPort(), 0); -// EXPECT_NE(port1, vHosts[i]->listenPort()); -// for (int j = 0; j < i; j++) EXPECT_NE(vHosts[j]->listenPort(), vHosts[i]->listenPort()); -// } -// -// for (int i = 0; i < nodeCount; i++) { -// if (i < 10) -// vHosts[i]->addNode(Node(host1->id(), NodeIPEndpoint(bi::address::from_string(localhost), port1, port1))); -// else -// vHosts[i]->addNode( -// Node(vHosts[i % 10]->id(), NodeIPEndpoint(bi::address::from_string(localhost), vHosts[i % 10]->listenPort(), -// vHosts[i % 10]->listenPort()))); -// std::this_thread::sleep_for(std::chrono::milliseconds(20)); -// } -// -// printf("Addnode %d hosts\n", nodeCount); -// -// // Wait for to give the hosts time to connect to each -// // other. -// bool connected = false; -// for (unsigned i = 0; i < 500; i++) { -// std::this_thread::sleep_for(std::chrono::milliseconds(100)); -// connected = true; -// setPendingPeersToReady(thc1); -// for (int j = 0; j < nodeCount; j++) { -// setPendingPeersToReady(vCapabilities[j]); -// if (vHosts[j]->peer_count() < 1) connected = false; -// } -// -// if ((host1->peer_count() > 0) && connected) break; -// } -// EXPECT_TRUE(connected); -// EXPECT_GT(host1->peer_count(), 0); -// -// DagBlock blk(blk_hash_t(1111), 0, {blk_hash_t(222), blk_hash_t(333), blk_hash_t(444)}, -// {g_signed_trx_samples[0]->getHash(), g_signed_trx_samples[1]->getHash()}, sig_t(7777), kNullBlockHash, -// addr_t(999)); -// -// SharedTransactions transactions{g_signed_trx_samples[0], g_signed_trx_samples[1]}; -// thc1->getSpecificHandler()->onNewTransactions( -// SharedTransactions(transactions)); -// SharedTransactions transactions2; -// thc1->getSpecificHandler()->onNewTransactions(std::move(transactions2)); -// thc1->getSpecificHandler()->onNewBlockReceived(DagBlock(blk)); -// -// SharedTransactions transactions_to_send; -// transactions_to_send.push_back(g_signed_trx_samples[0]); -// transactions_to_send.push_back(g_signed_trx_samples[1]); -// for (int i = 0; i < nodeCount; i++) { -// thc1->getSpecificHandler()->sendTransactions( -// thc1->getPeersState()->getPeer(vHosts[i]->id()), std::move(transactions_to_send)); -// } -// for (int i = 0; i < 50; i++) { -// std::this_thread::sleep_for(std::chrono::seconds(1)); -// bool synced = true; -// for (int j = 0; j < nodeCount; j++) -// if (vCapabilities[j]->test_state_->getBlocks().size() == 0) { -// synced = false; -// } -// if (synced) break; -// } -// auto blocks1 = thc1->test_state_->getBlocks(); -// for (int i = 0; i < nodeCount; i++) { -// EXPECT_EQ(vCapabilities[i]->test_state_->getBlocks().size(), 1); -// if (vCapabilities[i]->test_state_->getBlocks().size() == 1) { -// EXPECT_EQ(vCapabilities[i]->test_state_->getBlocks().begin()->second, blk); -// EXPECT_EQ(vCapabilities[i]->test_state_->getBlocks().begin()->second.getHash(), blk.getHash()); -// } -// auto rtransactions = vCapabilities[i]->test_state_->getTransactions(); -// EXPECT_EQ(rtransactions.size(), 2); -// if (rtransactions.size() == 2) { -// EXPECT_EQ(*transactions[0], *rtransactions[g_signed_trx_samples[0]->getHash()]); -// EXPECT_EQ(*transactions[1], *rtransactions[g_signed_trx_samples[1]->getHash()]); -// } -// } -// EXPECT_EQ(blocks1.size(), 1); -// if (blocks1.size()) { -// EXPECT_EQ(blk, blocks1.begin()->second); -// } -//} - TEST_F(P2PTest, multiple_capabilities) { auto node_cfgs = make_node_cfgs(3); h256 genesis_hash; From 52694fe37e46d7f82c5e11f1ec7c1d3face37cb6 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Tue, 6 Jun 2023 08:31:31 +0200 Subject: [PATCH 070/134] bugfix: do not ignor pbft size check on syncing --- .../common/ext_syncing_packet_handler.hpp | 6 +- .../common/ext_syncing_packet_handler.cpp | 6 +- .../latest/get_pbft_sync_packet_handler.cpp | 6 +- .../latest/pbft_sync_packet_handler.cpp | 2 +- .../storage/src/migration/period_data.cpp | 2 +- libraries/core_libs/storage/src/storage.cpp | 4 +- submodules/taraxa-evm | 2 +- tests/network_test.cpp | 90 +++++++++---------- 8 files changed, 54 insertions(+), 64 deletions(-) diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp index 3b7a30411e..d230ecc6be 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp @@ -41,14 +41,10 @@ class ExtSyncingPacketHandler : public PacketHandler { * @brief Send sync request to the current syncing peer with specified request_period * * @param request_period - * @param ignore_chain_size_check ignore peer's chain size check - it is used when processing sync packet from - * current syncing peer as he specifies in packet if he already send his last block - * or not. This info is more up to date then peer->chain_size that might have been - * saved in the past and it is not valid anymore * * @return true if sync request was sent, otherwise false */ - bool syncPeerPbft(PbftPeriod request_period, bool ignore_chain_size_check = false); + bool syncPeerPbft(PbftPeriod request_period); void requestDagBlocks(const dev::p2p::NodeID &_nodeID, const std::unordered_set &blocks, PbftPeriod period); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.cpp index 061d414b18..3554417c6d 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.cpp @@ -58,15 +58,15 @@ void ExtSyncingPacketHandler::startSyncingPbft() { db_->enableSnapshots(); } } -bool ExtSyncingPacketHandler::syncPeerPbft(PbftPeriod request_period, bool ignore_chain_size_check) { +bool ExtSyncingPacketHandler::syncPeerPbft(PbftPeriod request_period) { const auto syncing_peer = pbft_syncing_state_->syncingPeer(); if (!syncing_peer) { LOG(log_er_) << "Unable to send GetPbftSyncPacket. No syncing peer set."; return false; } - if (!ignore_chain_size_check && request_period > syncing_peer->pbft_chain_size_) { - LOG(log_er_) << "Invalid syncPeerPbft argument. Node " << syncing_peer->getId() << " chain size " + if (request_period > syncing_peer->pbft_chain_size_) { + LOG(log_wr_) << "Invalid syncPeerPbft argument. Node " << syncing_peer->getId() << " chain size " << syncing_peer->pbft_chain_size_ << ", requested period " << request_period; return false; } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp index 6f9d78900c..9b822f9a83 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp @@ -52,9 +52,9 @@ void GetPbftSyncPacketHandler::process(const threadpool::PacketData &packet_data size_t blocks_to_transfer = 0; auto pbft_chain_synced = false; - const auto total_period_datas_size = my_chain_size - height_to_sync + 1; - if (total_period_datas_size <= kConf.network.sync_level_size) { - blocks_to_transfer = total_period_datas_size; + const auto total_period_data_size = my_chain_size - height_to_sync + 1; + if (total_period_data_size <= kConf.network.sync_level_size) { + blocks_to_transfer = total_period_data_size; pbft_chain_synced = true; } else { blocks_to_transfer = kConf.network.sync_level_size; diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp index fb6a1a381b..0c2a9e9838 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/pbft_sync_packet_handler.cpp @@ -209,7 +209,7 @@ void PbftSyncPacketHandler::process(const threadpool::PacketData &packet_data, << ", PBFT chain size " << pbft_chain_->getPbftChainSize(); periodic_events_tp_.post(1000, [this] { delayedPbftSync(1); }); } else { - if (!syncPeerPbft(pbft_sync_period + 1, true)) { + if (!syncPeerPbft(pbft_sync_period + 1)) { pbft_syncing_state_->setPbftSyncing(false); return; } diff --git a/libraries/core_libs/storage/src/migration/period_data.cpp b/libraries/core_libs/storage/src/migration/period_data.cpp index bff1da7e67..2cc027e239 100644 --- a/libraries/core_libs/storage/src/migration/period_data.cpp +++ b/libraries/core_libs/storage/src/migration/period_data.cpp @@ -17,7 +17,7 @@ void PeriodData::migrate(logger::Logger& log) { auto copied_col = db_->copyColumn(db_->handle(orig_col), orig_col.name() + "-copy"); if (copied_col == nullptr) { - LOG(log) << "Migration " << id() << " failed: Unable to copy " << orig_col.name() << " column"; + LOG(log) << "Migration " << id() << " skipped: Unable to copy " << orig_col.name() << " column"; return; } diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 35ba70dfb7..12bb29f2c3 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -169,8 +169,8 @@ void DbStorage::replaceColumn(const Column& to_be_replaced_col, copyColumn(replacing_col.get(), to_be_replaced_col.name(), true); if (!replaced_col) { - LOG(log_er_) << "Unable to replace column " << to_be_replaced_col.name() - << " by " << replacing_col->GetName() << " due to failed copy"; + LOG(log_er_) << "Unable to replace column " << to_be_replaced_col.name() << " by " << replacing_col->GetName() + << " due to failed copy"; return; } diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index ab9aafbec8..af5164e1dc 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit ab9aafbec8b663e569d1eee6fddda86869cbd5ec +Subproject commit af5164e1dc56318c40367b910b1e890a3b9cf180 diff --git a/tests/network_test.cpp b/tests/network_test.cpp index ce44f1dfec..7a78c3fad6 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -106,49 +106,49 @@ TEST_F(NetworkTest, transfer_lot_of_blocks) { } // TODO: debug why the test take so long... - TEST_F(NetworkTest, propagate_block) { - auto node_cfgs = make_node_cfgs(5, 1); - auto nodes = launch_nodes(node_cfgs); - const auto& node1 = nodes[0]; +TEST_F(NetworkTest, propagate_block) { + auto node_cfgs = make_node_cfgs(5, 1); + auto nodes = launch_nodes(node_cfgs); + const auto& node1 = nodes[0]; - // Stop PBFT manager - for (auto& node : nodes) { + // Stop PBFT manager + for (auto& node : nodes) { node->getPbftManager()->stop(); - } - - const auto db1 = node1->getDB(); - const auto dag_mgr1 = node1->getDagManager(); - - auto trxs = samples::createSignedTrxSamples(0, 1, g_secret); - const auto estimation = node1->getTransactionManager()->estimateTransactionGas(trxs[0], {}); - - // node1 add one valid block - const auto proposal_level = 1; - const auto proposal_period = *db1->getProposalPeriodForDagLevel(proposal_level); - const auto period_block_hash = db1->getPeriodBlockHash(proposal_period); - const auto sortition_params = dag_mgr1->sortitionParamsManager().getSortitionParams(proposal_period); - vdf_sortition::VdfSortition vdf(sortition_params, node1->getVrfSecretKey(), - VrfSortitionBase::makeVrfInput(proposal_level, period_block_hash), 1, 1); - const auto dag_genesis = node1->getConfig().genesis.dag_genesis_block.getHash(); - dev::bytes vdf_msg = DagManager::getVdfMessage(dag_genesis, {trxs[0]}); - vdf.computeVdfSolution(sortition_params, vdf_msg, false); - DagBlock blk(dag_genesis, proposal_level, {}, {trxs[0]->getHash()}, estimation, vdf, node1->getSecretKey()); - - const auto block_hash = blk.getHash(); - - // Add block gossip it to connected peers - dag_mgr1->addDagBlock(std::move(blk), {trxs[0]}); - - wait({1s, 200ms}, [&](auto& ctx) { WAIT_EXPECT_NE(ctx, dag_mgr1->getDagBlock(block_hash), nullptr) }); - - std::cout << "Waiting Sync ..." << std::endl; - wait({20s, 200ms}, [&](auto& ctx) { - for (const auto& node : nodes) { - const auto dag_mgr = node->getDagManager(); - WAIT_EXPECT_NE(ctx, dag_mgr->getDagBlock(block_hash), nullptr) - } - }); - } + } + + const auto db1 = node1->getDB(); + const auto dag_mgr1 = node1->getDagManager(); + + auto trxs = samples::createSignedTrxSamples(0, 1, g_secret); + const auto estimation = node1->getTransactionManager()->estimateTransactionGas(trxs[0], {}); + + // node1 add one valid block + const auto proposal_level = 1; + const auto proposal_period = *db1->getProposalPeriodForDagLevel(proposal_level); + const auto period_block_hash = db1->getPeriodBlockHash(proposal_period); + const auto sortition_params = dag_mgr1->sortitionParamsManager().getSortitionParams(proposal_period); + vdf_sortition::VdfSortition vdf(sortition_params, node1->getVrfSecretKey(), + VrfSortitionBase::makeVrfInput(proposal_level, period_block_hash), 1, 1); + const auto dag_genesis = node1->getConfig().genesis.dag_genesis_block.getHash(); + dev::bytes vdf_msg = DagManager::getVdfMessage(dag_genesis, {trxs[0]}); + vdf.computeVdfSolution(sortition_params, vdf_msg, false); + DagBlock blk(dag_genesis, proposal_level, {}, {trxs[0]->getHash()}, estimation, vdf, node1->getSecretKey()); + + const auto block_hash = blk.getHash(); + + // Add block gossip it to connected peers + dag_mgr1->addDagBlock(std::move(blk), {trxs[0]}); + + wait({1s, 200ms}, [&](auto& ctx) { WAIT_EXPECT_NE(ctx, dag_mgr1->getDagBlock(block_hash), nullptr) }); + + std::cout << "Waiting Sync ..." << std::endl; + wait({20s, 200ms}, [&](auto& ctx) { + for (const auto& node : nodes) { + const auto dag_mgr = node->getDagManager(); + WAIT_EXPECT_NE(ctx, dag_mgr->getDagBlock(block_hash), nullptr) + } + }); +} TEST_F(NetworkTest, DISABLED_update_peer_chainsize) { auto node_cfgs = make_node_cfgs(2, 1, 5); @@ -297,11 +297,10 @@ TEST_F(NetworkTest, sync_large_pbft_block) { // Launch node2, node2 own 0 balance, could not vote auto nodes2 = launch_nodes({node_cfgs[1]}); - nodes[0]->getPbftManager()->stop(); const auto node2_pbft_chain = nodes2[0]->getPbftChain(); // verify that the large pbft block is synced - EXPECT_HAPPENS({30s, 100ms}, [&](auto& ctx) { + EXPECT_HAPPENS({100s, 100ms}, [&](auto& ctx) { WAIT_EXPECT_EQ(ctx, node2_pbft_chain->getPbftChainSize(), node1_pbft_chain->getPbftChainSize()) }); @@ -312,11 +311,6 @@ TEST_F(NetworkTest, sync_large_pbft_block) { std::cout << "PBFT block2 " << *pbft_blocks2 << std::endl; } EXPECT_EQ(pbft_blocks1->rlp(true), pbft_blocks2->rlp(true)); - - // this sleep is needed to process all remaining packets and destruct all network stuff - // on removal will cause next tests in the suite to fail because p2p port left binded - // see https://github.com/Taraxa-project/taraxa-node/issues/977 for more info - std::this_thread::sleep_for(1s); } // Test creates two Network setup and verifies sending transaction From 349a1e03381bcb97d1d85442d2f3f771e0f06f8a Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 8 Jun 2023 09:39:46 +0200 Subject: [PATCH 071/134] chore: add node addr to summary logs --- .../network/include/network/tarcap/stats/node_stats.hpp | 2 ++ libraries/core_libs/network/src/tarcap/stats/node_stats.cpp | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp b/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp index 1c8108cc10..002e75073c 100644 --- a/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp +++ b/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp @@ -55,6 +55,8 @@ class NodeStats { uint64_t syncing_duration_seconds{0}; uint64_t stalled_syncing_duration_seconds{0}; + const std::string kNodeAddress; + LOG_OBJECTS_DEFINE }; diff --git a/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp b/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp index e7781c52c1..072554a4e6 100644 --- a/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp +++ b/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp @@ -25,7 +25,8 @@ NodeStats::NodeStats(std::shared_ptr pbft_syncing_state, std:: vote_mgr_(std::move(vote_mgr)), trx_mgr_(std::move(trx_mgr)), packets_stats_(std::move(packets_stats)), - thread_pool_(std::move(thread_pool)) { + thread_pool_(std::move(thread_pool)), + kNodeAddress(node_addr.toString()) { LOG_OBJECTS_CREATE("SUMMARY"); } @@ -129,6 +130,7 @@ void NodeStats::logNodeStats(const std::vector Date: Mon, 5 Jun 2023 12:16:40 +0200 Subject: [PATCH 072/134] feat: add RPC methods to speed up indexing --- doc/RPC.md | 132 ++++++++++++++++++ libraries/core_libs/network/rpc/Taraxa.cpp | 85 ++++++++++- libraries/core_libs/network/rpc/Taraxa.h | 2 + .../core_libs/network/rpc/Taraxa.jsonrpc.json | 12 ++ .../core_libs/network/rpc/TaraxaClient.h | 18 +++ libraries/core_libs/network/rpc/TaraxaFace.h | 14 ++ libraries/core_libs/network/rpc/eth/Eth.cpp | 4 + libraries/core_libs/network/rpc/eth/Eth.h | 29 ---- libraries/core_libs/network/rpc/eth/data.hpp | 29 ++++ 9 files changed, 292 insertions(+), 33 deletions(-) diff --git a/doc/RPC.md b/doc/RPC.md index 5713106b93..a386a92e9e 100644 --- a/doc/RPC.md +++ b/doc/RPC.md @@ -511,6 +511,138 @@ curl -X POST --data '{"jsonrpc":"2.0","method":"taraxa_getChainStats","params":[ } ``` +### taraxa_getPeriodTransactionsWithReceipts + +Returns all transactions from block at specified period + +#### Parameters + +`QUANTITY` - period + +#### Returns + +`ARRAY` of `OBJECT` - Object that is result of transaction and receipt objects merging +* `blockHash`: `DATA`, 32 Bytes - hash of the block where this transaction was in. null when its pending. +* `blockNumber`: `QUANTITY` - block number where this transaction was in. null when its pending. +* `from`: `DATA`, 20 Bytes - address of the sender. +* `gas`: `QUANTITY` - gas provided by the sender. +* `gasPrice`: `QUANTITY` - gas price provided by the sender in Wei. +* `hash`: `DATA`, 32 Bytes - hash of the transaction. +* `input`: `DATA` - the data send along with the transaction. +* `nonce`: `QUANTITY` - the number of transactions made by the sender prior to this one. +* `to`: `DATA`, 20 Bytes - address of the receiver. null when its a contract creation transaction. +* `transactionIndex`: `QUANTITY` - integer of the transactions index position in the block. null when its pending. +* `value`: `QUANTITY` - value transferred in Wei. +* `v`: `QUANTITY` - ECDSA recovery id +* `r`: `QUANTITY` - ECDSA signature r +* `s`: `QUANTITY` - ECDSA signature s +* `cumulativeGasUsed`: `QUANTITY` - The total amount of gas used when this transaction was executed in the block. +* `gasUsed`: `QUANTITY` - The amount of gas used by this specific transaction alone. +* `contractAddress`: `DATA`, 20 Bytes - The contract address created, if the transaction was a contract creation, otherwise null. +* `logs`: `Array` - Array of log objects, which this transaction generated. +* `logsBloom`: `DATA`, 256 Bytes - Bloom filter for light clients to quickly retrieve related logs. +* `status`: `QUANTITY` either 1 (success) or 0 (failure) + + +#### Example + +```json +// Request +curl -X POST --data '{"jsonrpc":"2.0","method":"taraxa_getPeriodTransactionsWithReceipts","params":["0x100"],"id":1}' + +// Result +{ + "id": 1, + "jsonrpc": "2.0", + "result": [ + { + "blockHash": "0x9f27c225f7b645299a023d59d5407537e05cbc1aee09b8b338e2893e3a1c3698", + "blockNumber": "0x100", + "contractAddress": null, + "cumulativeGasUsed": "0x5208", + "from": "0x111f91441efc8c6c0edf6534970cc887e2fabaa8", + "gas": "0x5208", + "gasPrice": "0x1", + "gasUsed": "0x5208", + "hash": "0xca4f7b0c2bb4d0fd0ba525aee8c5148dbb10a4f937f5f39270c95e7735b48552", + "input": "0x", + "logs": [], + "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0x2761", + "r": "0x9069748f0ea83d278e70b585c97f6033698d39b17aa60d74bb47b67c3f275429", + "s": "0x235abed51caca24778c4ce13ebd92811a61fef53d6a318dfa0c2340000e20dc3", + "status": "0x1", + "to": "0x8dcc94cc57c72eaa6609ee77cb50c84141fa39bd", + "transactionHash": "0xca4f7b0c2bb4d0fd0ba525aee8c5148dbb10a4f937f5f39270c95e7735b48552", + "transactionIndex": "0x0", + "v": "0x0", + "value": "0x1" + }, ... + ] +} +``` + +### taraxa_getPeriodDagBlocks + +Returns all dag blocks for specified period + +#### Parameters + +`QUANTITY` - period + +#### Returns + +`ARRAY` of `OBJECT` - A DAG block object: +* `pivot`: `DATA`, 32 Bytes - +* `level`: `QUANTITY` - Level at which block was produced +* `period`: `QUANTITY` - Finalization period of this block +* `tips`: `ARRAY` - List of tips +* `transactions`: `ARRAY` - Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter. +* `trx_estimations`: `ARRAY` - Array of `QUANTITIES` that means transaction gas estimation for corresponding transaction. For example `trx_estimations[0]` is for `transactions[0]` +* `sig`: `DATA`, 65 Bytes - Signature of block creator +* `hash`: `DATA`, 32 Bytes - Hash of this block +* `sender`: `DATA`, 20 Bytes - Address of block creator +* `timestamp`: `QUANTITY` - Time of block creation +* `vdf`: `OBJECT` - vdf proof for this block + * `pk`: `DATA`, 32 Bytes - Public key + * `proof`: `DATA`, 80 Bytes - Bytes of proof + * `sol1`: `DATA` - First part of solution + * `sol1`: `DATA` - Second part of solution + * `difficulty`: `QUANTITY` - Difficulty with which block was produced + +#### Example + +```json +// Request +curl -X POST --data '{"jsonrpc":"2.0","method":"taraxa_getPeriodDagBlocks","params":["0x100"],"id":1}' + +// Result +{ + "id": 1, + "jsonrpc": "2.0", + "result": [ + { + "hash": "0xc142fafaebd46803a8c5bd8d7411a202e163fad0513e3fa2431d379b5c267a77", + "level": "0x1f9", + "period": "0x100", + "pivot": "0xa1e8e5485167951b81172ff6fe04b0d9fa7ac02280824da97e8af42cab783daa", + "sender": "0xe143ededbf9285dc39daf64c120037a4b9a0f095", + "sig": "0x8b5ba54ef14ee89255a4db84007f119589e73cb0b98befb3d04fd9fa0d92dc923dcb6ec2f6ffe60cd31a5d28fd2c290c293fa12fabff7e25d0051b701118748501", + "timestamp": "0x642ab512", + "tips": [], + "transactions": [...], + "trx_estimations": "0x668a0", + "vdf": { + "difficulty": "0x10", + "proof": "0x7ef44c562d64fb9169e6db68d720d182e4d9648c80349699a82c5b358cd9bc4f9b3ee50c97651bbc5579f98f412b642aa4b3c4af5a038816e54457152459e1e90cee85bcdc5fb386748777586186a10b", + "sol1": "0x041536b789da11f68c22b2ce1c23ed969a28e6269d3e0e65f81bfa3049e021875cff724803fe6f5ec3fe5362054eee9965978a004372b7249f4ce8485c7e92a64696aada82150329764612f9f5fabeabf0fb08e1ec33d7b52db165259033ca52d6f75284bb0163c993e0e6a4725e9a3417948ddf3dad8581aa596c2aeb53f321d6675d53ead1bafdee2d31928d77ad61452ad0e3c2c04cb6d27bfff2aaf94e2a47098f0270c62e7c5d1eb8e8c98398d2a75146a70b7bcbfc03be12c9deaf427562a9c37fbb93d466e6055ecd52abf6f9e14b49d4df19dc24187e22f62d0ed061c91b4c1b1712be47987e785094e91ea973af806c6b14299783ea0481070853d8", + "sol2": "0x2bb0760e2b5b4cbe2dfbe5c740206ad0f8606d397a7e16b157ebc4dcf899e67175d8ea83ac7c1b13cd7e84f1032f625b3d216c6595c5285898257a2d3b8359079cc6d6a7d5381a77756fe021747d3dcbae7cdaabc6def80eb9ee2e0b7cd9c7a85d06acaee456198d45b6652872b47f5dd5895548fc459d0ac854334c3ec3749b7011df27ee1b22ab7f8ad6ed731fbc2d8dc96eaf9e4d2fc87e2e8a1c52bff11e087e5da1f5c1e1ee316d1cd279d628c4a38cfb727b0aff896a82d9fe68a3c64490809f002615954ccd6422818210b4cafacd21dcf542c4c81263b71ccd149869d3077c3af5b674ae08b01059414247410f6f76f5215cfe40e10d0d95d5936197" + } + }, ... + ] +} +``` + ## Test API ### get_sortition_change diff --git a/libraries/core_libs/network/rpc/Taraxa.cpp b/libraries/core_libs/network/rpc/Taraxa.cpp index 7fe42bc23d..f25c0aa8be 100644 --- a/libraries/core_libs/network/rpc/Taraxa.cpp +++ b/libraries/core_libs/network/rpc/Taraxa.cpp @@ -5,8 +5,11 @@ #include #include +#include + #include "dag/dag_manager.hpp" #include "json/reader.h" +#include "network/rpc/eth/data.hpp" #include "pbft/pbft_manager.hpp" #include "transaction/transaction_manager.hpp" @@ -85,7 +88,7 @@ std::string Taraxa::taraxa_pbftBlockHashByPeriod(const std::string& _period) { try { auto node = tryGetNode(); auto db = node->getDB(); - auto blk = db->getPbftBlock(std::stoull(_period, 0, 16)); + auto blk = db->getPbftBlock(dev::jsToInt(_period)); if (!blk.has_value()) { return {}; } @@ -97,13 +100,14 @@ std::string Taraxa::taraxa_pbftBlockHashByPeriod(const std::string& _period) { Json::Value Taraxa::taraxa_getScheduleBlockByPeriod(const std::string& _period) { try { + auto period = dev::jsToInt(_period); auto node = tryGetNode(); auto db = node->getDB(); - auto blk = db->getPbftBlock(std::stoull(_period, 0, 16)); + auto blk = db->getPbftBlock(period); if (!blk.has_value()) { return Json::Value(); } - return PbftBlock::toJson(*blk, db->getFinalizedDagBlockHashesByPeriod(std::stoull(_period, 0, 16))); + return PbftBlock::toJson(*blk, db->getFinalizedDagBlockHashesByPeriod(period)); } catch (...) { BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS)); } @@ -112,7 +116,7 @@ Json::Value Taraxa::taraxa_getScheduleBlockByPeriod(const std::string& _period) Json::Value Taraxa::taraxa_getDagBlockByLevel(const string& _blockLevel, bool _includeTransactions) { try { auto node = tryGetNode(); - auto blocks = node->getDB()->getDagBlocksAtLevel(std::stoull(_blockLevel, 0, 16), 1); + auto blocks = node->getDB()->getDagBlocksAtLevel(dev::jsToInt(_blockLevel), 1); auto res = Json::Value(Json::arrayValue); for (auto const& b : blocks) { auto block_json = b->getJson(); @@ -151,4 +155,77 @@ Json::Value Taraxa::taraxa_getChainStats() { } return res; } + +Json::Value mergeJsons(Json::Value&& o1, Json::Value&& o2) { + for (auto itr = o2.begin(); itr != o2.end(); ++itr) { + o1[itr.key().asString()] = *itr; + } + return o1; +} + +template +Json::Value transformToJsonParallel(const S& source, FN op) { + Json::Value out(Json::arrayValue); + out.resize(source.size()); + + util::ThreadPool executor{std::thread::hardware_concurrency() / 2}; + std::atomic_uint processed = 0; + for (unsigned i = 0; i < source.size(); ++i) { + executor.post([&, i]() { + out[i] = op(source[i]); + ++processed; + }); + } + while (true) { + if (processed == source.size()) { + break; + } + } + return out; +} + +Json::Value Taraxa::taraxa_getPeriodTransactionsWithReceipts(const std::string& _period) { + try { + auto node = tryGetNode(); + auto final_chain = node->getFinalChain(); + auto period = dev::jsToInt(_period); + auto block_hash = final_chain->block_hash(period); + auto trxs = node->getDB()->getPeriodTransactions(period); + Json::Value result(Json::arrayValue); + if (!trxs.has_value()) { + return result; + } + return transformToJsonParallel(*trxs, [&final_chain, &block_hash](const auto& trx) { + auto hash = trx->getHash(); + auto r = final_chain->transaction_receipt(hash); + auto location = + rpc::eth::ExtendedTransactionLocation{{*final_chain->transaction_location(hash), *block_hash}, hash}; + auto transaction = rpc::eth::LocalisedTransaction{trx, location}; + auto receipt = rpc::eth::LocalisedTransactionReceipt{*r, location, trx->getSender(), trx->getReceiver()}; + auto receipt_json = rpc::eth::toJson(receipt); + receipt_json.removeMember("transactionHash"); + + return mergeJsons(rpc::eth::toJson(transaction), std::move(receipt_json)); + }); + } catch (...) { + BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS)); + } +} + +Json::Value Taraxa::taraxa_getPeriodDagBlocks(const std::string& _period) { + try { + auto period = dev::jsToInt(_period); + auto node = tryGetNode(); + auto dags = node->getDB()->getFinalizedDagBlockByPeriod(period); + Json::Value result(Json::arrayValue); + return transformToJsonParallel(dags, [&period](const auto& dag) { + auto block_json = dag->getJson(); + block_json["period"] = toJS(period); + return block_json; + }); + } catch (...) { + BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS)); + } +} + } // namespace taraxa::net \ No newline at end of file diff --git a/libraries/core_libs/network/rpc/Taraxa.h b/libraries/core_libs/network/rpc/Taraxa.h index 39b32cca34..d8b6f50251 100644 --- a/libraries/core_libs/network/rpc/Taraxa.h +++ b/libraries/core_libs/network/rpc/Taraxa.h @@ -29,6 +29,8 @@ class Taraxa : public TaraxaFace { virtual std::string taraxa_pbftBlockHashByPeriod(const std::string& _period) override; virtual Json::Value taraxa_getConfig() override; virtual Json::Value taraxa_getChainStats() override; + virtual Json::Value taraxa_getPeriodTransactionsWithReceipts(const std::string& _period) override; + virtual Json::Value taraxa_getPeriodDagBlocks(const std::string& _period) override; protected: std::weak_ptr full_node_; diff --git a/libraries/core_libs/network/rpc/Taraxa.jsonrpc.json b/libraries/core_libs/network/rpc/Taraxa.jsonrpc.json index 15e5b2135c..5811ad6d9d 100644 --- a/libraries/core_libs/network/rpc/Taraxa.jsonrpc.json +++ b/libraries/core_libs/network/rpc/Taraxa.jsonrpc.json @@ -64,6 +64,18 @@ "params": [""], "order": [], "returns": "" + }, + { + "name": "taraxa_getPeriodTransactionsWithReceipts", + "params": [""], + "order": [], + "returns": [] + }, + { + "name": "taraxa_getPeriodDagBlocks", + "params": [""], + "order": [], + "returns": [] } ] diff --git a/libraries/core_libs/network/rpc/TaraxaClient.h b/libraries/core_libs/network/rpc/TaraxaClient.h index a8a3c105a1..2e87335bd7 100644 --- a/libraries/core_libs/network/rpc/TaraxaClient.h +++ b/libraries/core_libs/network/rpc/TaraxaClient.h @@ -106,6 +106,24 @@ class TaraxaClient : public jsonrpc::Client { else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } + Json::Value taraxa_getPeriodTransactionsWithReceipts(const std::string& param1) throw(jsonrpc::JsonRpcException) { + Json::Value p; + p.append(param1); + Json::Value result = this->CallMethod("taraxa_getPeriodTransactionsWithReceipts", p); + if (result.isObject()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + } + Json::Value taraxa_getPeriodDagBlocks(const std::string& param1) throw(jsonrpc::JsonRpcException) { + Json::Value p; + p.append(param1); + Json::Value result = this->CallMethod("taraxa_getPeriodDagBlocks", p); + if (result.isObject()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + } }; } // namespace net diff --git a/libraries/core_libs/network/rpc/TaraxaFace.h b/libraries/core_libs/network/rpc/TaraxaFace.h index acf4d29c0d..8c13ba5213 100644 --- a/libraries/core_libs/network/rpc/TaraxaFace.h +++ b/libraries/core_libs/network/rpc/TaraxaFace.h @@ -44,6 +44,12 @@ class TaraxaFace : public ServerInterface { this->bindAndAddMethod(jsonrpc::Procedure("taraxa_pbftBlockHashByPeriod", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1", jsonrpc::JSON_STRING, NULL), &taraxa::net::TaraxaFace::taraxa_pbftBlockHashByPeriodI); + this->bindAndAddMethod(jsonrpc::Procedure("taraxa_getPeriodTransactionsWithReceipts", jsonrpc::PARAMS_BY_POSITION, + jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_STRING, NULL), + &taraxa::net::TaraxaFace::taraxa_getPeriodTransactionsWithReceiptsI); + this->bindAndAddMethod(jsonrpc::Procedure("taraxa_getPeriodDagBlocks", jsonrpc::PARAMS_BY_POSITION, + jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_STRING, NULL), + &taraxa::net::TaraxaFace::taraxa_getPeriodDagBlocksI); } inline virtual void taraxa_protocolVersionI(const Json::Value &request, Json::Value &response) { @@ -82,6 +88,12 @@ class TaraxaFace : public ServerInterface { inline virtual void taraxa_pbftBlockHashByPeriodI(const Json::Value &request, Json::Value &response) { response = this->taraxa_pbftBlockHashByPeriod(request[0u].asString()); } + inline virtual void taraxa_getPeriodTransactionsWithReceiptsI(const Json::Value &request, Json::Value &response) { + response = this->taraxa_getPeriodTransactionsWithReceipts(request[0u].asString()); + } + inline virtual void taraxa_getPeriodDagBlocksI(const Json::Value &request, Json::Value &response) { + response = this->taraxa_getPeriodDagBlocks(request[0u].asString()); + } virtual std::string taraxa_protocolVersion() = 0; virtual Json::Value taraxa_getVersion() = 0; virtual Json::Value taraxa_getDagBlockByHash(const std::string ¶m1, bool param2) = 0; @@ -92,6 +104,8 @@ class TaraxaFace : public ServerInterface { virtual Json::Value taraxa_getConfig() = 0; virtual Json::Value taraxa_getChainStats() = 0; virtual std::string taraxa_pbftBlockHashByPeriod(const std::string ¶m1) = 0; + virtual Json::Value taraxa_getPeriodTransactionsWithReceipts(const std::string ¶m1) = 0; + virtual Json::Value taraxa_getPeriodDagBlocks(const std::string ¶m1) = 0; }; } // namespace net diff --git a/libraries/core_libs/network/rpc/eth/Eth.cpp b/libraries/core_libs/network/rpc/eth/Eth.cpp index 735217b279..f45d37824a 100644 --- a/libraries/core_libs/network/rpc/eth/Eth.cpp +++ b/libraries/core_libs/network/rpc/eth/Eth.cpp @@ -510,6 +510,10 @@ class EthImpl : public Eth, EthParams { } } if (const auto& topics_json = json["topics"]; !topics_json.empty()) { + if (topics_json.size() > 4) { + BOOST_THROW_EXCEPTION(jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS, + "only up to four topic slots may be specified")); + } for (uint32_t i = 0; i < topics_json.size(); i++) { const auto& topic_json = topics_json[i]; if (topic_json.isArray()) { diff --git a/libraries/core_libs/network/rpc/eth/Eth.h b/libraries/core_libs/network/rpc/eth/Eth.h index acf361e0b7..c36285bf82 100644 --- a/libraries/core_libs/network/rpc/eth/Eth.h +++ b/libraries/core_libs/network/rpc/eth/Eth.h @@ -7,35 +7,6 @@ namespace taraxa::net::rpc::eth { -void add(Json::Value& obj, const std::optional& info); -void add(Json::Value& obj, const ExtendedTransactionLocation& info); -Json::Value toJson(const final_chain::BlockHeader& obj); -Json::Value toJson(const Transaction& trx, const std::optional& loc); -Json::Value toJson(const LocalisedTransaction& lt); -Json::Value toJson(const final_chain::BlockHeader& obj); -Json::Value toJson(const LocalisedLogEntry& lle); -Json::Value toJson(const LocalisedTransactionReceipt& ltr); -Json::Value toJson(const SyncStatus& obj); - -template -Json::Value toJson(const T& t) { - return toJS(t); -} - -template -Json::Value toJsonArray(const std::vector& _es) { - Json::Value res(Json::arrayValue); - for (const auto& e : _es) { - res.append(toJson(e)); - } - return res; -} - -template -Json::Value toJson(const std::optional& t) { - return t ? toJson(*t) : Json::Value(); -} - struct EthParams { Address address; uint64_t chain_id = 0; diff --git a/libraries/core_libs/network/rpc/eth/data.hpp b/libraries/core_libs/network/rpc/eth/data.hpp index b691068f23..2228cfb922 100644 --- a/libraries/core_libs/network/rpc/eth/data.hpp +++ b/libraries/core_libs/network/rpc/eth/data.hpp @@ -50,4 +50,33 @@ struct SyncStatus { EthBlockNumber starting_block = 0, current_block = 0, highest_block = 0; }; +void add(Json::Value& obj, const std::optional& info); +void add(Json::Value& obj, const ExtendedTransactionLocation& info); +Json::Value toJson(const final_chain::BlockHeader& obj); +Json::Value toJson(const Transaction& trx, const std::optional& loc); +Json::Value toJson(const LocalisedTransaction& lt); +Json::Value toJson(const final_chain::BlockHeader& obj); +Json::Value toJson(const LocalisedLogEntry& lle); +Json::Value toJson(const LocalisedTransactionReceipt& ltr); +Json::Value toJson(const SyncStatus& obj); + +template +Json::Value toJson(const T& t) { + return toJS(t); +} + +template +Json::Value toJsonArray(const std::vector& _es) { + Json::Value res(Json::arrayValue); + for (const auto& e : _es) { + res.append(toJson(e)); + } + return res; +} + +template +Json::Value toJson(const std::optional& t) { + return t ? toJson(*t) : Json::Value(); +} + } // namespace taraxa::net::rpc::eth \ No newline at end of file From 5ff72c13233e28b592153e558fe9bf48914869b8 Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Mon, 5 Jun 2023 13:59:28 +0200 Subject: [PATCH 073/134] fix: reuse threadpool in transformToJsonParallel --- libraries/core_libs/network/rpc/Taraxa.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/libraries/core_libs/network/rpc/Taraxa.cpp b/libraries/core_libs/network/rpc/Taraxa.cpp index f25c0aa8be..fcc2e0748b 100644 --- a/libraries/core_libs/network/rpc/Taraxa.cpp +++ b/libraries/core_libs/network/rpc/Taraxa.cpp @@ -165,10 +165,14 @@ Json::Value mergeJsons(Json::Value&& o1, Json::Value&& o2) { template Json::Value transformToJsonParallel(const S& source, FN op) { + if (source.empty()) { + return Json::Value(Json::arrayValue); + } + + static util::ThreadPool executor{std::thread::hardware_concurrency() / 2}; + Json::Value out(Json::arrayValue); out.resize(source.size()); - - util::ThreadPool executor{std::thread::hardware_concurrency() / 2}; std::atomic_uint processed = 0; for (unsigned i = 0; i < source.size(); ++i) { executor.post([&, i]() { @@ -176,6 +180,7 @@ Json::Value transformToJsonParallel(const S& source, FN op) { ++processed; }); } + while (true) { if (processed == source.size()) { break; @@ -191,10 +196,10 @@ Json::Value Taraxa::taraxa_getPeriodTransactionsWithReceipts(const std::string& auto period = dev::jsToInt(_period); auto block_hash = final_chain->block_hash(period); auto trxs = node->getDB()->getPeriodTransactions(period); - Json::Value result(Json::arrayValue); if (!trxs.has_value()) { - return result; + return Json::Value(Json::arrayValue); } + return transformToJsonParallel(*trxs, [&final_chain, &block_hash](const auto& trx) { auto hash = trx->getHash(); auto r = final_chain->transaction_receipt(hash); @@ -217,7 +222,7 @@ Json::Value Taraxa::taraxa_getPeriodDagBlocks(const std::string& _period) { auto period = dev::jsToInt(_period); auto node = tryGetNode(); auto dags = node->getDB()->getFinalizedDagBlockByPeriod(period); - Json::Value result(Json::arrayValue); + return transformToJsonParallel(dags, [&period](const auto& dag) { auto block_json = dag->getJson(); block_json["period"] = toJS(period); From a8b4c576257c879c49772b07775c92d55664db0a Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Tue, 13 Jun 2023 14:11:19 +0200 Subject: [PATCH 074/134] chore: improve performance of vote weight calculation --- libraries/types/vote/src/vrf_sortition.cpp | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/libraries/types/vote/src/vrf_sortition.cpp b/libraries/types/vote/src/vrf_sortition.cpp index f3f42a786c..c38f0947c9 100644 --- a/libraries/types/vote/src/vrf_sortition.cpp +++ b/libraries/types/vote/src/vrf_sortition.cpp @@ -73,21 +73,12 @@ uint64_t VrfPbftSortition::getBinominalDistribution(uint64_t stake, double dpos_ const double ratio = division.convert_to(); boost::math::binomial_distribution dist(static_cast(stake), threshold / dpos_total_votes_count); - // Binary search for find the lowest stake <= cdf - uint64_t start = 0; - uint64_t end = stake - 1; - while (start + 1 < end) { - const auto mid = start + (end - start) / 2; - const auto target = cdf(dist, mid); - if (ratio <= target) { - end = mid; - } else { - start = mid; + for (uint64_t j = 0; j < stake; j++) { + // Found the correct boundary, break + if (ratio <= cdf(dist, j)) { + return j; } } - // Found the correct boundary - if (ratio <= cdf(dist, start)) return start; - if (ratio <= cdf(dist, end)) return end; return stake; } From ce14f8b9e2b89f34a486bb515787bf3b528cceda Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Tue, 20 Jun 2023 10:39:35 +0200 Subject: [PATCH 075/134] chore:fix sync large pbft block test --- .../packets_handlers/latest/get_pbft_sync_packet_handler.cpp | 2 +- .../tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp index 9b822f9a83..f71ddf33aa 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_pbft_sync_packet_handler.cpp @@ -81,7 +81,7 @@ void GetPbftSyncPacketHandler::sendPbftBlocks(const std::shared_ptr } dev::RLPStream s; - if (pbft_chain_synced && last_block && block_period > 1) { + if (pbft_chain_synced && last_block) { s.appendList(3); s << last_block; s.appendRaw(data); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp index 80e404c124..3d46f3dd52 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/v1/get_pbft_sync_packet_handler.cpp @@ -47,7 +47,7 @@ void GetPbftSyncPacketHandler::sendPbftBlocks(const std::shared_ptr } dev::RLPStream s; - if (pbft_chain_synced && last_block && block_period > 1) { + if (pbft_chain_synced && last_block) { s.appendList(3); s << last_block; s.appendRaw(transformPeriodDataRlpToV1(data)); From b6d0455bd9e76ae460db3603295e07d118b4269c Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Wed, 14 Jun 2023 15:40:46 +0200 Subject: [PATCH 076/134] chore: optimize peer cache --- libraries/common/include/common/util.hpp | 103 +++++++++++++++--- .../include/network/tarcap/taraxa_peer.hpp | 8 +- .../network/src/tarcap/taraxa_peer.cpp | 30 +++-- tests/network_test.cpp | 26 +++++ 4 files changed, 133 insertions(+), 34 deletions(-) diff --git a/libraries/common/include/common/util.hpp b/libraries/common/include/common/util.hpp index ac30a83d1b..737d84d317 100644 --- a/libraries/common/include/common/util.hpp +++ b/libraries/common/include/common/util.hpp @@ -207,7 +207,7 @@ std::string fmt(const std::string &pattern, const TS &...args) { template class ExpirationCache { public: - ExpirationCache(uint32_t max_size, uint32_t delete_step) : max_size_(max_size), delete_step_(delete_step) {} + ExpirationCache(uint32_t max_size, uint32_t delete_step) : kMaxSize(max_size), kDeleteStep(delete_step) {} /** * @brief Inserts key into the cache map. In case provided key is already in cache, only shared lock @@ -229,8 +229,8 @@ class ExpirationCache { } expiration_.push_back(key); - if (cache_.size() > max_size_) { - for (uint32_t i = 0; i < delete_step_; i++) { + if (cache_.size() > kMaxSize) { + for (uint32_t i = 0; i < kDeleteStep; i++) { cache_.erase(expiration_.front()); expiration_.pop_front(); } @@ -254,18 +254,85 @@ class ExpirationCache { return cache_.count(key); } + std::size_t size() const { + std::shared_lock lck(mtx_); + return cache_.size(); + } + void clear() { std::unique_lock lck(mtx_); cache_.clear(); expiration_.clear(); } - private: + protected: std::unordered_set cache_; - std::deque expiration_; - const uint32_t max_size_; - const uint32_t delete_step_; + const uint32_t kMaxSize; + const uint32_t kDeleteStep; mutable std::shared_mutex mtx_; + + private: + std::deque expiration_; +}; + +template +class ExpirationBlockNumberCache : public ExpirationCache { + public: + ExpirationBlockNumberCache(uint32_t max_size, uint32_t delete_step, uint32_t blocks_to_keep) + : ExpirationCache(max_size, delete_step), kBlocksToKeep(blocks_to_keep) {} + + /** + * @brief Inserts key into the cache map. In case provided key is already in cache, only shared lock + * is acquired and function returns false. This means insert does not need to be used together with count() + * to save the performance by not acquiring unique lock + * + * @param key + * @param block_number + * @return true if actual insertion took place, otherwise false + */ + bool insert(Key const &key, uint64_t block_number) { + if (this->contains(key)) { + return false; + } + // There must be double check if key is not already in cache due to possible race condition + std::unique_lock lock(this->mtx_); + if (!this->cache_.insert(key).second) { + return false; + } + + if (block_number > last_block_number_) { + last_block_number_ = block_number; + while (block_expiration_.size() > 0) { + const auto &exp = block_expiration_.front(); + if (block_number > kBlocksToKeep && exp.second < block_number - kBlocksToKeep) { + this->cache_.erase(exp.first); + block_expiration_.pop_front(); + } else { + break; + } + } + } + block_expiration_.push_back({key, block_number}); + if (this->cache_.size() > this->kMaxSize) { + for (uint32_t i = 0; i < this->kDeleteStep; i++) { + this->cache_.erase(block_expiration_.front().first); + block_expiration_.pop_front(); + } + } + + return true; + } + + void clear() { + std::shared_lock lck(this->mtx_); + this->cache_.clear(); + block_expiration_.clear(); + } + + private: + std::deque> block_expiration_; + const uint32_t kBlocksToKeep; + uint64_t last_block_number_ = 0; }; template @@ -284,7 +351,7 @@ auto getRlpBytes(T const &t) { template class ExpirationCacheMap { public: - ExpirationCacheMap(uint32_t max_size, uint32_t delete_step) : max_size_(max_size), delete_step_(delete_step) {} + ExpirationCacheMap(uint32_t max_size, uint32_t delete_step) : kMaxSize(max_size), kDeleteStep(delete_step) {} /** * @brief Inserts pair into the cache map. In case provided key is already in cache, only shared lock @@ -311,7 +378,7 @@ class ExpirationCacheMap { } expiration_.push_back(key); - if (cache_.size() > max_size_) { + if (cache_.size() > kMaxSize) { eraseOldest(); } return true; @@ -350,8 +417,8 @@ class ExpirationCacheMap { cache_[key] = value; expiration_.push_back(key); - if (cache_.size() > max_size_) { - for (uint32_t i = 0; i < delete_step_; i++) { + if (cache_.size() > kMaxSize) { + for (uint32_t i = 0; i < kDeleteStep; i++) { cache_.erase(expiration_.front()); expiration_.pop_front(); } @@ -375,8 +442,8 @@ class ExpirationCacheMap { } cache_[key] = value; expiration_.push_back(key); - if (cache_.size() > max_size_) { - for (uint32_t i = 0; i < delete_step_; i++) { + if (cache_.size() > kMaxSize) { + for (uint32_t i = 0; i < kDeleteStep; i++) { cache_.erase(expiration_.front()); expiration_.pop_front(); } @@ -395,8 +462,8 @@ class ExpirationCacheMap { if (it != cache_.end() && it->second == expected_value) { it->second = value; expiration_.push_back(key); - if (cache_.size() > max_size_) { - for (auto i = 0; i < delete_step_; i++) { + if (cache_.size() > kMaxSize) { + for (auto i = 0; i < kDeleteStep; i++) { cache_.erase(expiration_.front()); expiration_.pop_front(); } @@ -408,7 +475,7 @@ class ExpirationCacheMap { protected: virtual void eraseOldest() { - for (uint32_t i = 0; i < delete_step_; i++) { + for (uint32_t i = 0; i < kDeleteStep; i++) { cache_.erase(expiration_.front()); expiration_.pop_front(); } @@ -416,8 +483,8 @@ class ExpirationCacheMap { std::unordered_map cache_; std::deque expiration_; - const uint32_t max_size_; - const uint32_t delete_step_; + const uint32_t kMaxSize; + const uint32_t kDeleteStep; mutable std::shared_mutex mtx_; }; diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_peer.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_peer.hpp index 34df483adc..d048065938 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_peer.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_peer.hpp @@ -116,11 +116,11 @@ class TaraxaPeer : public boost::noncopyable { private: dev::p2p::NodeID id_; - ExpirationCache known_dag_blocks_; - ExpirationCache known_transactions_; + ExpirationBlockNumberCache known_dag_blocks_; + ExpirationBlockNumberCache known_transactions_; // PBFT - ExpirationCache known_pbft_blocks_; - ExpirationCache known_votes_; // for peers + ExpirationBlockNumberCache known_pbft_blocks_; + ExpirationBlockNumberCache known_votes_; // for peers std::atomic timestamp_suspicious_packet_ = 0; std::atomic suspicious_packet_count_ = 0; diff --git a/libraries/core_libs/network/src/tarcap/taraxa_peer.cpp b/libraries/core_libs/network/src/tarcap/taraxa_peer.cpp index 0bdd6d927b..bd173d0330 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_peer.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_peer.cpp @@ -3,31 +3,37 @@ namespace taraxa::network::tarcap { TaraxaPeer::TaraxaPeer() - : known_dag_blocks_(10000, 1000), - known_transactions_(100000, 10000), - known_pbft_blocks_(10000, 1000), - known_votes_(10000, 1000) {} + : known_dag_blocks_(10000, 1000, 10), + known_transactions_(100000, 10000, 10), + known_pbft_blocks_(10000, 1000, 10), + known_votes_(10000, 1000, 10) {} TaraxaPeer::TaraxaPeer(const dev::p2p::NodeID& id, size_t transaction_pool_size) : id_(id), - known_dag_blocks_(10000, 1000), - known_transactions_(transaction_pool_size * 1.2, transaction_pool_size / 10), - known_pbft_blocks_(10000, 1000), - known_votes_(100000, 1000) {} + known_dag_blocks_(10000, 1000, 10), + known_transactions_(transaction_pool_size * 1.2, transaction_pool_size / 10, 10), + known_pbft_blocks_(10000, 1000, 10), + known_votes_(100000, 1000, 10) {} -bool TaraxaPeer::markDagBlockAsKnown(const blk_hash_t& hash) { return known_dag_blocks_.insert(hash); } +bool TaraxaPeer::markDagBlockAsKnown(const blk_hash_t& hash) { + return known_dag_blocks_.insert(hash, pbft_chain_size_); +} bool TaraxaPeer::isDagBlockKnown(const blk_hash_t& hash) const { return known_dag_blocks_.contains(hash); } -bool TaraxaPeer::markTransactionAsKnown(const trx_hash_t& hash) { return known_transactions_.insert(hash); } +bool TaraxaPeer::markTransactionAsKnown(const trx_hash_t& hash) { + return known_transactions_.insert(hash, pbft_chain_size_); +} bool TaraxaPeer::isTransactionKnown(const trx_hash_t& hash) const { return known_transactions_.contains(hash); } -bool TaraxaPeer::markVoteAsKnown(const vote_hash_t& hash) { return known_votes_.insert(hash); } +bool TaraxaPeer::markVoteAsKnown(const vote_hash_t& hash) { return known_votes_.insert(hash, pbft_chain_size_); } bool TaraxaPeer::isVoteKnown(const vote_hash_t& hash) const { return known_votes_.contains(hash); } -bool TaraxaPeer::markPbftBlockAsKnown(const blk_hash_t& hash) { return known_pbft_blocks_.insert(hash); } +bool TaraxaPeer::markPbftBlockAsKnown(const blk_hash_t& hash) { + return known_pbft_blocks_.insert(hash, pbft_chain_size_); +} bool TaraxaPeer::isPbftBlockKnown(const blk_hash_t& hash) const { return known_pbft_blocks_.contains(hash); } diff --git a/tests/network_test.cpp b/tests/network_test.cpp index 7a78c3fad6..f3bd226565 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -1437,6 +1437,32 @@ TEST_F(NetworkTest, dag_syncing_limit) { EXPECT_TRUE(peer2.requestDagSyncingAllowed()); } +TEST_F(NetworkTest, peer_cache_test) { + const uint64_t max_cache_size = 200000; + const uint64_t delete_step = 1000; + const uint64_t max_block_to_keep_in_cache = 10; + ExpirationBlockNumberCache known_transactions_with_block_number(max_cache_size, delete_step, + max_block_to_keep_in_cache); + ExpirationCache known_transactions(max_cache_size, delete_step); + for (uint64_t block_number = 1; block_number < 1000; block_number++) { + // Each block insert 2000 transactions + const uint64_t transactions_in_block = 2000; + for (uint64_t i = 0; i < transactions_in_block; i++) { + known_transactions_with_block_number.insert(trx_hash_t(block_number * transactions_in_block + i), block_number); + known_transactions.insert(trx_hash_t(block_number * transactions_in_block + i)); + } + uint64_t number_of_insertion = block_number * transactions_in_block; + uint64_t expected_known_transactions = std::min(number_of_insertion, max_cache_size); + EXPECT_EQ(known_transactions.size(), expected_known_transactions); + uint64_t expected_known_transactions_with_block_number = + std::min(number_of_insertion, (max_block_to_keep_in_cache + 1) * transactions_in_block); + EXPECT_EQ(known_transactions_with_block_number.size(), expected_known_transactions_with_block_number); + + std::cout << block_number << " " << known_transactions_with_block_number.size() << " " << known_transactions.size() + << std::endl; + } +} + } // namespace taraxa::core_tests using namespace taraxa; From 46e2a76fc6faaea5d5f1cdd164f018daa4c8538b Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Wed, 28 Jun 2023 10:05:45 +0200 Subject: [PATCH 077/134] fix: period data migration --- libraries/core_libs/storage/src/storage.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 12bb29f2c3..e26bac7d43 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -175,6 +175,7 @@ void DbStorage::replaceColumn(const Column& to_be_replaced_col, } handles_[to_be_replaced_col.ordinal_] = replaced_col.release(); + checkStatus(db_->DropColumnFamily(replacing_col.get())); } void DbStorage::deleteColumnData(const Column& c) { From b65346f6cbe0b715a7e92ff13ebe5dfde9f661f8 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Tue, 27 Jun 2023 15:57:16 +0200 Subject: [PATCH 078/134] chore: light node optimizations --- .../consensus/include/dag/dag_manager.hpp | 2 +- .../consensus/src/dag/dag_manager.cpp | 18 ++++- .../storage/include/storage/storage.hpp | 4 +- libraries/core_libs/storage/src/storage.cpp | 69 ++++++++++++++++--- tests/full_node_test.cpp | 5 +- 5 files changed, 79 insertions(+), 19 deletions(-) diff --git a/libraries/core_libs/consensus/include/dag/dag_manager.hpp b/libraries/core_libs/consensus/include/dag/dag_manager.hpp index 2de214cb1d..91a0b795a1 100644 --- a/libraries/core_libs/consensus/include/dag/dag_manager.hpp +++ b/libraries/core_libs/consensus/include/dag/dag_manager.hpp @@ -239,7 +239,7 @@ class DagManager : public std::enable_shared_from_this { bool validateBlockNotExpired(const std::shared_ptr &dag_block, std::unordered_map> &expired_dag_blocks_to_remove); void handleExpiredDagBlocksTransactions(const std::vector &transactions_from_expired_dag_blocks) const; - void clearLightNodeHistory(); + void clearLightNodeHistory(bool force = false); std::pair> getFrontier() const; // return pivot and tips void updateFrontier(); diff --git a/libraries/core_libs/consensus/src/dag/dag_manager.cpp b/libraries/core_libs/consensus/src/dag/dag_manager.cpp index 3f7e7d8771..55344bf952 100644 --- a/libraries/core_libs/consensus/src/dag/dag_manager.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_manager.cpp @@ -53,6 +53,9 @@ DagManager::DagManager(const DagBlock &dag_genesis_block, addr_t node_addr, cons db_->saveProposalPeriodDagLevelsMap(max_levels_per_period, 0); } recoverDag(); + if (is_light_node_) { + clearLightNodeHistory(true); + } } catch (std::exception &e) { std::cerr << e.what() << std::endl; } @@ -279,9 +282,9 @@ std::vector DagManager::getDagBlockOrder(blk_hash_t const &anchor, P return blk_orders; } -void DagManager::clearLightNodeHistory() { +void DagManager::clearLightNodeHistory(bool force) { // Actual history size will be between 100% and 110% of light_node_history_ to avoid deleting on every period - if (((period_ % (std::max(light_node_history_ / 10, (uint64_t)1)) == 0)) && period_ > light_node_history_ && + if (((period_ % (std::max(light_node_history_ / 10, (uint64_t)1)) == 0) || force) && period_ > light_node_history_ && dag_expiry_level_ > max_levels_per_period_ + 1) { // This will happen at most once a day so log a silent log LOG(log_si_) << "Clear light node history"; @@ -296,7 +299,11 @@ void DagManager::clearLightNodeHistory() { LOG(log_tr_) << "dag_expiry_level - max_levels_per_period_ - 1: " << dag_expiry_level_ - max_levels_per_period_ - 1 << " *proposal_period " << *proposal_period; LOG(log_tr_) << "Delete period history from: " << start << " to " << end; - db_->clearPeriodDataHistory(end); + uint64_t dag_level_to_keep = 1; + if (dag_expiry_level_ > max_levels_per_period_) { + dag_level_to_keep = dag_expiry_level_ - max_levels_per_period_; + } + db_->clearPeriodDataHistory(end, dag_level_to_keep); LOG(log_si_) << "Clear light node history completed"; } } @@ -459,6 +466,11 @@ void DagManager::recoverDag() { anchor_ = anchor; LOG(log_nf_) << "Recover anchor " << anchor_; addToDag(anchor_, kNullBlockHash, vec_blk_t(), 0, true); + + const auto anchor_block_level = getDagBlock(anchor_)->getLevel(); + if (anchor_block_level > dag_expiry_limit_) { + dag_expiry_level_ = anchor_block_level - dag_expiry_limit_; + } break; } } diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index a43a06624c..bf5bc15385 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -96,7 +96,7 @@ class DbStorage : public std::enable_shared_from_this { COLUMN_W_COMP(period_data, getIntComparator()); COLUMN(genesis); COLUMN(dag_blocks); - COLUMN(dag_blocks_index); + COLUMN_W_COMP(dag_blocks_level, getIntComparator()); COLUMN(transactions); COLUMN(trx_period); COLUMN(status); @@ -199,7 +199,7 @@ class DbStorage : public std::enable_shared_from_this { // Period data void savePeriodData(const PeriodData& period_data, Batch& write_batch); - void clearPeriodDataHistory(PbftPeriod period); + void clearPeriodDataHistory(PbftPeriod period, uint64_t dag_level_to_keep); dev::bytes getPeriodDataRaw(PbftPeriod period) const; std::optional getPbftBlock(PbftPeriod period) const; std::vector> getPeriodCertVotes(PbftPeriod period) const; diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index e26bac7d43..7958cde8e9 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -217,6 +217,23 @@ void DbStorage::rebuildColumns(const rocksdb::Options& options) { const auto it = std::find_if(Columns::all.begin(), Columns::all.end(), [&handles, i](const Column& col) { return col.name() == handles[i]->GetName(); }); if (it == Columns::all.end()) { + // All the data from dag_blocks_index is moved to dag_blocks_level + if (handles[i]->GetName() == "dag_blocks_index") { + rocksdb::ColumnFamilyHandle* handle_dag_blocks_level; + + auto options = rocksdb::ColumnFamilyOptions(); + options.comparator = getIntComparator(); + checkStatus(db->CreateColumnFamily(options, Columns::dag_blocks_level.name(), &handle_dag_blocks_level)); + + auto it_dag_level = std::unique_ptr(db->NewIterator(read_options_, handles[i])); + it_dag_level->SeekToFirst(); + + while (it_dag_level->Valid()) { + checkStatus(db->Put(write_options_, handle_dag_blocks_level, toSlice(it_dag_level->key()), + toSlice(it_dag_level->value()))); + it_dag_level->Next(); + } + } LOG(log_si_) << "Removing column: " << handles[i]->GetName(); checkStatus(db->DropColumnFamily(handles[i])); } @@ -409,14 +426,14 @@ bool DbStorage::dagBlockInDb(blk_hash_t const& hash) { } std::set DbStorage::getBlocksByLevel(level_t level) { - auto data = asBytes(lookup(toSlice(level), Columns::dag_blocks_index)); + auto data = asBytes(lookup(toSlice(level), Columns::dag_blocks_level)); dev::RLP rlp(data); return rlp.toSet(); } level_t DbStorage::getLastBlocksLevel() const { level_t level = 0; - auto it = std::unique_ptr(db_->NewIterator(read_options_, handle(Columns::dag_blocks_index))); + auto it = std::unique_ptr(db_->NewIterator(read_options_, handle(Columns::dag_blocks_level))); it->SeekToLast(); if (it->Valid()) { memcpy(&level, it->key().data(), sizeof(level_t)); @@ -476,7 +493,7 @@ void DbStorage::updateDagBlockCounters(std::vector blks) { for (auto const& hash : block_hashes) { blocks_stream << hash; } - insert(write_batch, Columns::dag_blocks_index, toSlice(level), toSlice(blocks_stream.out())); + insert(write_batch, Columns::dag_blocks_level, toSlice(level), toSlice(blocks_stream.out())); dag_blocks_count_.fetch_add(1); dag_edge_count_.fetch_add(blk.getTips().size() + 1); } @@ -501,7 +518,7 @@ void DbStorage::saveDagBlock(DagBlock const& blk, Batch* write_batch_p) { for (auto const& hash : block_hashes) { blocks_stream << hash; } - insert(write_batch, Columns::dag_blocks_index, toSlice(level), toSlice(blocks_stream.out())); + insert(write_batch, Columns::dag_blocks_level, toSlice(level), toSlice(blocks_stream.out())); dag_blocks_count_.fetch_add(1); dag_edge_count_.fetch_add(blk.getTips().size() + 1); @@ -542,7 +559,7 @@ std::optional DbStorage::getParamsChangeForPeriod(PbftPer return SortitionParamsChange::from_rlp(dev::RLP(it->value().ToString())); } -void DbStorage::clearPeriodDataHistory(PbftPeriod end_period) { +void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level_to_keep) { // This is expensive operation but it should not be run more than once a day auto it = std::unique_ptr(db_->NewIterator(read_options_, handle(Columns::period_data))); // Find the first non-deleted period @@ -560,12 +577,11 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period) { // Find transactions included in the old blocks and delete data related to these transactions to free disk // space auto trx_hashes_raw = lookup(period, DB::Columns::final_chain_transaction_hashes_by_blk_number); - auto hashes_count = trx_hashes_raw.size() / trx_hash_t::size; - for (uint32_t i = 0; i < hashes_count; i++) { - auto hash = trx_hash_t(reinterpret_cast(trx_hashes_raw.data() + i * trx_hash_t::size), - trx_hash_t::ConstructFromPointer); + TransactionHashes trx_hashes = (util::rlp_dec(dev::RLP(trx_hashes_raw))); + + for (auto hash : trx_hashes) { remove(write_batch, Columns::final_chain_receipt_by_trx_hash, hash); - remove(write_batch, Columns::period_data, hash); + remove(write_batch, Columns::trx_period, hash); } remove(write_batch, Columns::final_chain_transaction_hashes_by_blk_number, EthBlockNumber(period)); if ((period - start_period + 1) % max_batch_delete == 0) { @@ -573,14 +589,47 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period) { write_batch = createWriteBatch(); } } + commitWriteBatch(write_batch); db_->DeleteRange(write_options_, handle(Columns::period_data), start_slice, end_slice); + // Deletion alone does not guarantee that the disk space is freed, these CompactRange methods actually compact // the data in the database and free disk space db_->CompactRange({}, handle(Columns::period_data), &start_slice, &end_slice); db_->CompactRange({}, handle(Columns::final_chain_receipt_by_trx_hash), nullptr, nullptr); db_->CompactRange({}, handle(Columns::final_chain_transaction_hashes_by_blk_number), nullptr, nullptr); + db_->CompactRange({}, handle(Columns::trx_period), nullptr, nullptr); + } + } + + it = std::unique_ptr(db_->NewIterator(read_options_, handle(Columns::dag_blocks_level))); + it->SeekToFirst(); + if (it->Valid()) { + uint64_t start_level; + memcpy(&start_level, it->key().data(), sizeof(uint64_t)); + if (start_level < dag_level_to_keep) { + auto write_batch = createWriteBatch(); + // Delete up to max 10000 period at once + const uint32_t max_batch_delete = 10000; + auto start_slice = toSlice(start_level); + auto end_slice = toSlice(dag_level_to_keep - 1); + for (auto level = start_level; level < dag_level_to_keep; level++) { + // Find old dag blocks and delete data related to these blocks to free disk space + auto dag_block_hashes = getBlocksByLevel(start_level); + for (auto dag_block_hash : dag_block_hashes) { + remove(write_batch, Columns::dag_block_period, dag_block_hash); + } + if ((dag_level_to_keep - start_level + 1) % max_batch_delete == 0) { + commitWriteBatch(write_batch); + write_batch = createWriteBatch(); + } + } + commitWriteBatch(write_batch); + db_->DeleteRange(write_options_, handle(Columns::dag_blocks_level), start_slice, end_slice); + + db_->CompactRange({}, handle(Columns::dag_block_period), nullptr, nullptr); + db_->CompactRange({}, handle(Columns::dag_blocks_level), nullptr, nullptr); } } } diff --git a/tests/full_node_test.cpp b/tests/full_node_test.cpp index 74c74f7e03..7dbbbe98a9 100644 --- a/tests/full_node_test.cpp +++ b/tests/full_node_test.cpp @@ -1477,9 +1477,8 @@ TEST_F(FullNodeTest, clear_period_data) { for (uint64_t i = 0; i < nodes[1]->getPbftChain()->getPbftChainSize(); i++) { const auto pbft_block = nodes[1]->getDB()->getPbftBlock(i); if (pbft_block && pbft_block->getPivotDagBlockHash() != kNullBlockHash) { - if (nodes[1]->getDB()->getDagBlock(pbft_block->getPivotDagBlockHash())->getLevel() + - node_cfgs[0].dag_expiry_limit >= - last_anchor_level) { + auto dag_block = nodes[1]->getDB()->getDagBlock(pbft_block->getPivotDagBlockHash()); + if (dag_block && (dag_block->getLevel() + node_cfgs[0].dag_expiry_limit >= last_anchor_level)) { first_over_limit = i; break; } From 49229f7f52c4b0e2a908f480d97e3f31036a9e40 Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Wed, 28 Jun 2023 10:26:19 +0200 Subject: [PATCH 079/134] feat: add method to get cert votes --- doc/RPC.md | 35 +++++++++++++++++++ .../src/vote_manager/vote_manager.cpp | 2 +- libraries/core_libs/network/rpc/Taraxa.cpp | 27 ++++++++++++++ libraries/core_libs/network/rpc/Taraxa.h | 1 + .../core_libs/network/rpc/Taraxa.jsonrpc.json | 6 ++++ .../core_libs/network/rpc/TaraxaClient.h | 9 +++++ libraries/core_libs/network/rpc/TaraxaFace.h | 8 +++++ libraries/core_libs/storage/src/storage.cpp | 6 +++- libraries/types/vote/include/vote/vote.hpp | 8 ++++- libraries/types/vote/src/vote.cpp | 13 +++++++ 10 files changed, 112 insertions(+), 3 deletions(-) diff --git a/doc/RPC.md b/doc/RPC.md index a386a92e9e..adfd2cb485 100644 --- a/doc/RPC.md +++ b/doc/RPC.md @@ -643,6 +643,41 @@ curl -X POST --data '{"jsonrpc":"2.0","method":"taraxa_getPeriodDagBlocks","para } ``` +### taraxa_getPreviousBlockCertVotes + +Returns all previous block cert votes for specified period + +#### Parameters + +`QUANTITY` - period + +#### Returns + +`ARRAY` of `OBJECT` - A Vote block object: +* `hash`: `DATA`, 32 Bytes - Hash of the vote +* `voter`: `DATA`, 20 Bytes - Address of vote creator +* `sig`: `DATA`, 65 Bytes - Vote signature +* `block_hash`: `DATA`, 32 Bytes - Hash of the block vote +* `weight`: `QUANTITY` - weight of the vote + +#### Example + +```json +// Request +curl -X POST --data '{"jsonrpc":"2.0","method":"taraxa_getPreviousBlockCertVotes","params":["0x100"],"id":1}' + +// Result +{ + "id": 1, + "jsonrpc": "2.0", + "result": [ + { + + }, ... + ] +} +``` + ## Test API ### get_sortition_change diff --git a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp index 349c435749..c23bbe3af4 100644 --- a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp +++ b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp @@ -868,7 +868,6 @@ std::pair VoteManager::validateVote(const std::shared_ptrdpos_eligible_vote_count(vote_period - 1, vote->getVoterAddr()); - const uint64_t total_dpos_votes_count = final_chain_->dpos_eligible_total_vote_count(vote_period - 1); // Mark vote as validated only after getting dpos_eligible_vote_count and other values from dpos contract. It is // possible that we are behind in processing pbft blocks, in which case we wont be able to get values from dpos @@ -896,6 +895,7 @@ std::pair VoteManager::validateVote(const std::shared_ptrdpos_eligible_total_vote_count(vote_period - 1); const uint64_t pbft_sortition_threshold = getPbftSortitionThreshold(total_dpos_votes_count, vote->getType()); if (!vote->calculateWeight(voter_dpos_votes_count, total_dpos_votes_count, pbft_sortition_threshold)) { err_msg << "Invalid vote " << vote->getHash() << ": zero weight"; diff --git a/libraries/core_libs/network/rpc/Taraxa.cpp b/libraries/core_libs/network/rpc/Taraxa.cpp index fcc2e0748b..b062df6d07 100644 --- a/libraries/core_libs/network/rpc/Taraxa.cpp +++ b/libraries/core_libs/network/rpc/Taraxa.cpp @@ -233,4 +233,31 @@ Json::Value Taraxa::taraxa_getPeriodDagBlocks(const std::string& _period) { } } +Json::Value Taraxa::taraxa_getPreviousBlockCertVotes(const std::string& _period) { + try { + auto node = tryGetNode(); + auto final_chain = node->getFinalChain(); + auto vote_manager = node->getVoteManager(); + + Json::Value res(Json::objectValue); + + auto period = dev::jsToInt(_period); + auto votes = node->getDB()->getPeriodCertVotes(period); + if (votes.empty()) { + return res; + } + + const auto votes_period = votes.front()->getPeriod(); + const uint64_t total_dpos_votes_count = final_chain->dpos_eligible_total_vote_count(votes_period - 1); + res["total_votes_count"] = total_dpos_votes_count; + res["votes"] = transformToJsonParallel(votes, [&](const auto& vote) { + vote_manager->validateVote(vote); + return vote->toJSON(); + }); + return res; + } catch (...) { + BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS)); + } +} + } // namespace taraxa::net \ No newline at end of file diff --git a/libraries/core_libs/network/rpc/Taraxa.h b/libraries/core_libs/network/rpc/Taraxa.h index d8b6f50251..e984f6d057 100644 --- a/libraries/core_libs/network/rpc/Taraxa.h +++ b/libraries/core_libs/network/rpc/Taraxa.h @@ -31,6 +31,7 @@ class Taraxa : public TaraxaFace { virtual Json::Value taraxa_getChainStats() override; virtual Json::Value taraxa_getPeriodTransactionsWithReceipts(const std::string& _period) override; virtual Json::Value taraxa_getPeriodDagBlocks(const std::string& _period) override; + virtual Json::Value taraxa_getPreviousBlockCertVotes(const std::string& _period) override; protected: std::weak_ptr full_node_; diff --git a/libraries/core_libs/network/rpc/Taraxa.jsonrpc.json b/libraries/core_libs/network/rpc/Taraxa.jsonrpc.json index 5811ad6d9d..23202759c5 100644 --- a/libraries/core_libs/network/rpc/Taraxa.jsonrpc.json +++ b/libraries/core_libs/network/rpc/Taraxa.jsonrpc.json @@ -76,6 +76,12 @@ "params": [""], "order": [], "returns": [] + }, + { + "name": "taraxa_getPreviousBlockCertVotes", + "params": [""], + "order": [], + "returns": {} } ] diff --git a/libraries/core_libs/network/rpc/TaraxaClient.h b/libraries/core_libs/network/rpc/TaraxaClient.h index 2e87335bd7..82c6a1363c 100644 --- a/libraries/core_libs/network/rpc/TaraxaClient.h +++ b/libraries/core_libs/network/rpc/TaraxaClient.h @@ -124,6 +124,15 @@ class TaraxaClient : public jsonrpc::Client { else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } + Json::Value taraxa_getPreviousBlockCertVotes(const std::string& param1) throw(jsonrpc::JsonRpcException) { + Json::Value p; + p.append(param1); + Json::Value result = this->CallMethod("taraxa_getPreviousBlockCertVotes", p); + if (result.isObject()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + } }; } // namespace net diff --git a/libraries/core_libs/network/rpc/TaraxaFace.h b/libraries/core_libs/network/rpc/TaraxaFace.h index 8c13ba5213..e24b3630d5 100644 --- a/libraries/core_libs/network/rpc/TaraxaFace.h +++ b/libraries/core_libs/network/rpc/TaraxaFace.h @@ -50,6 +50,9 @@ class TaraxaFace : public ServerInterface { this->bindAndAddMethod(jsonrpc::Procedure("taraxa_getPeriodDagBlocks", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_STRING, NULL), &taraxa::net::TaraxaFace::taraxa_getPeriodDagBlocksI); + this->bindAndAddMethod(jsonrpc::Procedure("taraxa_getPreviousBlockCertVotes", jsonrpc::PARAMS_BY_POSITION, + jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_STRING, NULL), + &taraxa::net::TaraxaFace::taraxa_getPeriodPreviousBlockCertVotesI); } inline virtual void taraxa_protocolVersionI(const Json::Value &request, Json::Value &response) { @@ -94,6 +97,10 @@ class TaraxaFace : public ServerInterface { inline virtual void taraxa_getPeriodDagBlocksI(const Json::Value &request, Json::Value &response) { response = this->taraxa_getPeriodDagBlocks(request[0u].asString()); } + inline virtual void taraxa_getPeriodPreviousBlockCertVotesI(const Json::Value &request, Json::Value &response) { + response = this->taraxa_getPreviousBlockCertVotes(request[0u].asString()); + } + virtual std::string taraxa_protocolVersion() = 0; virtual Json::Value taraxa_getVersion() = 0; virtual Json::Value taraxa_getDagBlockByHash(const std::string ¶m1, bool param2) = 0; @@ -106,6 +113,7 @@ class TaraxaFace : public ServerInterface { virtual std::string taraxa_pbftBlockHashByPeriod(const std::string ¶m1) = 0; virtual Json::Value taraxa_getPeriodTransactionsWithReceipts(const std::string ¶m1) = 0; virtual Json::Value taraxa_getPeriodDagBlocks(const std::string ¶m1) = 0; + virtual Json::Value taraxa_getPreviousBlockCertVotes(const std::string ¶m1) = 0; }; } // namespace net diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index e26bac7d43..b124595444 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -766,7 +766,11 @@ std::vector> DbStorage::getPeriodCertVotes(PbftPeriod peri } auto period_data_rlp = dev::RLP(period_data); - return decodeVotesBundleRlp(period_data_rlp[CERT_VOTES_POS_IN_PERIOD_DATA]); + auto votes_rlp = period_data_rlp[CERT_VOTES_POS_IN_PERIOD_DATA]; + if (votes_rlp.itemCount() == 0) { + return {}; + } + return decodeVotesBundleRlp(votes_rlp); } std::optional DbStorage::getPeriodTransactions(PbftPeriod period) const { diff --git a/libraries/types/vote/include/vote/vote.hpp b/libraries/types/vote/include/vote/vote.hpp index 60279f6f6d..982586dd4d 100644 --- a/libraries/types/vote/include/vote/vote.hpp +++ b/libraries/types/vote/include/vote/vote.hpp @@ -160,13 +160,19 @@ class Vote { strm << "[Vote] " << std::endl; strm << " vote_hash: " << vote.vote_hash_ << std::endl; strm << " voter: " << vote.getVoter() << std::endl; - strm << " vote_signatue: " << vote.vote_signature_ << std::endl; + strm << " vote_signature: " << vote.vote_signature_ << std::endl; strm << " blockhash: " << vote.blockhash_ << std::endl; if (vote.weight_) strm << " weight: " << vote.weight_.value() << std::endl; strm << " vrf_sorition: " << vote.vrf_sortition_ << std::endl; return strm; } + /** + * @brief Get vote in JSON representation + * @return vote JSON + */ + Json::Value toJSON() const; + private: /** * @brief Secure Hash Algorithm 3 diff --git a/libraries/types/vote/src/vote.cpp b/libraries/types/vote/src/vote.cpp index ca0b5ff595..83bc0745b6 100644 --- a/libraries/types/vote/src/vote.cpp +++ b/libraries/types/vote/src/vote.cpp @@ -1,5 +1,7 @@ #include "vote/vote.hpp" +#include + #include "common/encoding_rlp.hpp" namespace taraxa { @@ -74,4 +76,15 @@ bytes Vote::optimizedRlp() const { return s.invalidate(); } +Json::Value Vote::toJSON() const { + Json::Value json(Json::objectValue); + json["hash"] = dev::toJS(getHash()); + json["voter"] = dev::toJS(getVoterAddr()); + json["signature"] = dev::toJS(getVoteSignature()); + json["block_hash"] = dev::toJS(getBlockHash()); + json["weight"] = dev::toJS(*weight_); + + return json; +} + } // namespace taraxa \ No newline at end of file From 4ecf42507a2c0ff13843ba510e39d8047c787638 Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Fri, 30 Jun 2023 11:13:29 +0200 Subject: [PATCH 080/134] fix: move indexer methods to debug API --- doc/RPC.md | 336 +++++++++--------- libraries/core_libs/network/rpc/Debug.cpp | 115 ++++++ libraries/core_libs/network/rpc/Debug.h | 3 + .../core_libs/network/rpc/Debug.jsonrpc.json | 18 + libraries/core_libs/network/rpc/DebugClient.h | 27 ++ libraries/core_libs/network/rpc/DebugFace.h | 21 ++ libraries/core_libs/network/rpc/Taraxa.cpp | 104 ------ libraries/core_libs/network/rpc/Taraxa.h | 3 - .../core_libs/network/rpc/Taraxa.jsonrpc.json | 18 - .../core_libs/network/rpc/TaraxaClient.h | 27 -- libraries/core_libs/network/rpc/TaraxaFace.h | 21 -- libraries/types/vote/src/vote.cpp | 4 +- submodules/taraxa-evm | 2 +- 13 files changed, 357 insertions(+), 342 deletions(-) diff --git a/doc/RPC.md b/doc/RPC.md index adfd2cb485..72f3d5f671 100644 --- a/doc/RPC.md +++ b/doc/RPC.md @@ -511,173 +511,6 @@ curl -X POST --data '{"jsonrpc":"2.0","method":"taraxa_getChainStats","params":[ } ``` -### taraxa_getPeriodTransactionsWithReceipts - -Returns all transactions from block at specified period - -#### Parameters - -`QUANTITY` - period - -#### Returns - -`ARRAY` of `OBJECT` - Object that is result of transaction and receipt objects merging -* `blockHash`: `DATA`, 32 Bytes - hash of the block where this transaction was in. null when its pending. -* `blockNumber`: `QUANTITY` - block number where this transaction was in. null when its pending. -* `from`: `DATA`, 20 Bytes - address of the sender. -* `gas`: `QUANTITY` - gas provided by the sender. -* `gasPrice`: `QUANTITY` - gas price provided by the sender in Wei. -* `hash`: `DATA`, 32 Bytes - hash of the transaction. -* `input`: `DATA` - the data send along with the transaction. -* `nonce`: `QUANTITY` - the number of transactions made by the sender prior to this one. -* `to`: `DATA`, 20 Bytes - address of the receiver. null when its a contract creation transaction. -* `transactionIndex`: `QUANTITY` - integer of the transactions index position in the block. null when its pending. -* `value`: `QUANTITY` - value transferred in Wei. -* `v`: `QUANTITY` - ECDSA recovery id -* `r`: `QUANTITY` - ECDSA signature r -* `s`: `QUANTITY` - ECDSA signature s -* `cumulativeGasUsed`: `QUANTITY` - The total amount of gas used when this transaction was executed in the block. -* `gasUsed`: `QUANTITY` - The amount of gas used by this specific transaction alone. -* `contractAddress`: `DATA`, 20 Bytes - The contract address created, if the transaction was a contract creation, otherwise null. -* `logs`: `Array` - Array of log objects, which this transaction generated. -* `logsBloom`: `DATA`, 256 Bytes - Bloom filter for light clients to quickly retrieve related logs. -* `status`: `QUANTITY` either 1 (success) or 0 (failure) - - -#### Example - -```json -// Request -curl -X POST --data '{"jsonrpc":"2.0","method":"taraxa_getPeriodTransactionsWithReceipts","params":["0x100"],"id":1}' - -// Result -{ - "id": 1, - "jsonrpc": "2.0", - "result": [ - { - "blockHash": "0x9f27c225f7b645299a023d59d5407537e05cbc1aee09b8b338e2893e3a1c3698", - "blockNumber": "0x100", - "contractAddress": null, - "cumulativeGasUsed": "0x5208", - "from": "0x111f91441efc8c6c0edf6534970cc887e2fabaa8", - "gas": "0x5208", - "gasPrice": "0x1", - "gasUsed": "0x5208", - "hash": "0xca4f7b0c2bb4d0fd0ba525aee8c5148dbb10a4f937f5f39270c95e7735b48552", - "input": "0x", - "logs": [], - "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - "nonce": "0x2761", - "r": "0x9069748f0ea83d278e70b585c97f6033698d39b17aa60d74bb47b67c3f275429", - "s": "0x235abed51caca24778c4ce13ebd92811a61fef53d6a318dfa0c2340000e20dc3", - "status": "0x1", - "to": "0x8dcc94cc57c72eaa6609ee77cb50c84141fa39bd", - "transactionHash": "0xca4f7b0c2bb4d0fd0ba525aee8c5148dbb10a4f937f5f39270c95e7735b48552", - "transactionIndex": "0x0", - "v": "0x0", - "value": "0x1" - }, ... - ] -} -``` - -### taraxa_getPeriodDagBlocks - -Returns all dag blocks for specified period - -#### Parameters - -`QUANTITY` - period - -#### Returns - -`ARRAY` of `OBJECT` - A DAG block object: -* `pivot`: `DATA`, 32 Bytes - -* `level`: `QUANTITY` - Level at which block was produced -* `period`: `QUANTITY` - Finalization period of this block -* `tips`: `ARRAY` - List of tips -* `transactions`: `ARRAY` - Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter. -* `trx_estimations`: `ARRAY` - Array of `QUANTITIES` that means transaction gas estimation for corresponding transaction. For example `trx_estimations[0]` is for `transactions[0]` -* `sig`: `DATA`, 65 Bytes - Signature of block creator -* `hash`: `DATA`, 32 Bytes - Hash of this block -* `sender`: `DATA`, 20 Bytes - Address of block creator -* `timestamp`: `QUANTITY` - Time of block creation -* `vdf`: `OBJECT` - vdf proof for this block - * `pk`: `DATA`, 32 Bytes - Public key - * `proof`: `DATA`, 80 Bytes - Bytes of proof - * `sol1`: `DATA` - First part of solution - * `sol1`: `DATA` - Second part of solution - * `difficulty`: `QUANTITY` - Difficulty with which block was produced - -#### Example - -```json -// Request -curl -X POST --data '{"jsonrpc":"2.0","method":"taraxa_getPeriodDagBlocks","params":["0x100"],"id":1}' - -// Result -{ - "id": 1, - "jsonrpc": "2.0", - "result": [ - { - "hash": "0xc142fafaebd46803a8c5bd8d7411a202e163fad0513e3fa2431d379b5c267a77", - "level": "0x1f9", - "period": "0x100", - "pivot": "0xa1e8e5485167951b81172ff6fe04b0d9fa7ac02280824da97e8af42cab783daa", - "sender": "0xe143ededbf9285dc39daf64c120037a4b9a0f095", - "sig": "0x8b5ba54ef14ee89255a4db84007f119589e73cb0b98befb3d04fd9fa0d92dc923dcb6ec2f6ffe60cd31a5d28fd2c290c293fa12fabff7e25d0051b701118748501", - "timestamp": "0x642ab512", - "tips": [], - "transactions": [...], - "trx_estimations": "0x668a0", - "vdf": { - "difficulty": "0x10", - "proof": "0x7ef44c562d64fb9169e6db68d720d182e4d9648c80349699a82c5b358cd9bc4f9b3ee50c97651bbc5579f98f412b642aa4b3c4af5a038816e54457152459e1e90cee85bcdc5fb386748777586186a10b", - "sol1": "0x041536b789da11f68c22b2ce1c23ed969a28e6269d3e0e65f81bfa3049e021875cff724803fe6f5ec3fe5362054eee9965978a004372b7249f4ce8485c7e92a64696aada82150329764612f9f5fabeabf0fb08e1ec33d7b52db165259033ca52d6f75284bb0163c993e0e6a4725e9a3417948ddf3dad8581aa596c2aeb53f321d6675d53ead1bafdee2d31928d77ad61452ad0e3c2c04cb6d27bfff2aaf94e2a47098f0270c62e7c5d1eb8e8c98398d2a75146a70b7bcbfc03be12c9deaf427562a9c37fbb93d466e6055ecd52abf6f9e14b49d4df19dc24187e22f62d0ed061c91b4c1b1712be47987e785094e91ea973af806c6b14299783ea0481070853d8", - "sol2": "0x2bb0760e2b5b4cbe2dfbe5c740206ad0f8606d397a7e16b157ebc4dcf899e67175d8ea83ac7c1b13cd7e84f1032f625b3d216c6595c5285898257a2d3b8359079cc6d6a7d5381a77756fe021747d3dcbae7cdaabc6def80eb9ee2e0b7cd9c7a85d06acaee456198d45b6652872b47f5dd5895548fc459d0ac854334c3ec3749b7011df27ee1b22ab7f8ad6ed731fbc2d8dc96eaf9e4d2fc87e2e8a1c52bff11e087e5da1f5c1e1ee316d1cd279d628c4a38cfb727b0aff896a82d9fe68a3c64490809f002615954ccd6422818210b4cafacd21dcf542c4c81263b71ccd149869d3077c3af5b674ae08b01059414247410f6f76f5215cfe40e10d0d95d5936197" - } - }, ... - ] -} -``` - -### taraxa_getPreviousBlockCertVotes - -Returns all previous block cert votes for specified period - -#### Parameters - -`QUANTITY` - period - -#### Returns - -`ARRAY` of `OBJECT` - A Vote block object: -* `hash`: `DATA`, 32 Bytes - Hash of the vote -* `voter`: `DATA`, 20 Bytes - Address of vote creator -* `sig`: `DATA`, 65 Bytes - Vote signature -* `block_hash`: `DATA`, 32 Bytes - Hash of the block vote -* `weight`: `QUANTITY` - weight of the vote - -#### Example - -```json -// Request -curl -X POST --data '{"jsonrpc":"2.0","method":"taraxa_getPreviousBlockCertVotes","params":["0x100"],"id":1}' - -// Result -{ - "id": 1, - "jsonrpc": "2.0", - "result": [ - { - - }, ... - ] -} -``` - ## Test API ### get_sortition_change @@ -911,3 +744,172 @@ curl -X POST --data '{"jsonrpc":"2.0","method":"get_all_nodes","params":[],"id": "result": "f8ff62f2df819566023ec3cda97f73697e789089b5c97c22a24c175edec3256242796a7a843fe1b949558160dae0fd5510e31c93440798212ef99a55c64e7b0f 66.94.114.186:10002\n45022ea1765d3a569003e2088a0893ffe00656c25f32ee414b39c6e45c8aa05cc41453158d832c67b5bba710edb648223f654fae9e2a1f513d7e42447f25a2d6 38.242.218.203:10002" } ``` + +## Debug API + +### debug_getPeriodTransactionsWithReceipts + +Returns all transactions from block at specified period + +#### Parameters + +`QUANTITY` - period + +#### Returns + +`ARRAY` of `OBJECT` - Object that is result of transaction and receipt objects merging +* `blockHash`: `DATA`, 32 Bytes - hash of the block where this transaction was in. null when its pending. +* `blockNumber`: `QUANTITY` - block number where this transaction was in. null when its pending. +* `from`: `DATA`, 20 Bytes - address of the sender. +* `gas`: `QUANTITY` - gas provided by the sender. +* `gasPrice`: `QUANTITY` - gas price provided by the sender in Wei. +* `hash`: `DATA`, 32 Bytes - hash of the transaction. +* `input`: `DATA` - the data send along with the transaction. +* `nonce`: `QUANTITY` - the number of transactions made by the sender prior to this one. +* `to`: `DATA`, 20 Bytes - address of the receiver. null when its a contract creation transaction. +* `transactionIndex`: `QUANTITY` - integer of the transactions index position in the block. null when its pending. +* `value`: `QUANTITY` - value transferred in Wei. +* `v`: `QUANTITY` - ECDSA recovery id +* `r`: `QUANTITY` - ECDSA signature r +* `s`: `QUANTITY` - ECDSA signature s +* `cumulativeGasUsed`: `QUANTITY` - The total amount of gas used when this transaction was executed in the block. +* `gasUsed`: `QUANTITY` - The amount of gas used by this specific transaction alone. +* `contractAddress`: `DATA`, 20 Bytes - The contract address created, if the transaction was a contract creation, otherwise null. +* `logs`: `Array` - Array of log objects, which this transaction generated. +* `logsBloom`: `DATA`, 256 Bytes - Bloom filter for light clients to quickly retrieve related logs. +* `status`: `QUANTITY` either 1 (success) or 0 (failure) + + +#### Example + +```json +// Request +curl -X POST --data '{"jsonrpc":"2.0","method":"debug_getPeriodTransactionsWithReceipts","params":["0x100"],"id":1}' + +// Result +{ + "id": 1, + "jsonrpc": "2.0", + "result": [ + { + "blockHash": "0x9f27c225f7b645299a023d59d5407537e05cbc1aee09b8b338e2893e3a1c3698", + "blockNumber": "0x100", + "contractAddress": null, + "cumulativeGasUsed": "0x5208", + "from": "0x111f91441efc8c6c0edf6534970cc887e2fabaa8", + "gas": "0x5208", + "gasPrice": "0x1", + "gasUsed": "0x5208", + "hash": "0xca4f7b0c2bb4d0fd0ba525aee8c5148dbb10a4f937f5f39270c95e7735b48552", + "input": "0x", + "logs": [], + "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "nonce": "0x2761", + "r": "0x9069748f0ea83d278e70b585c97f6033698d39b17aa60d74bb47b67c3f275429", + "s": "0x235abed51caca24778c4ce13ebd92811a61fef53d6a318dfa0c2340000e20dc3", + "status": "0x1", + "to": "0x8dcc94cc57c72eaa6609ee77cb50c84141fa39bd", + "transactionHash": "0xca4f7b0c2bb4d0fd0ba525aee8c5148dbb10a4f937f5f39270c95e7735b48552", + "transactionIndex": "0x0", + "v": "0x0", + "value": "0x1" + }, ... + ] +} +``` + +### debug_getPeriodDagBlocks + +Returns all dag blocks for specified period + +#### Parameters + +`QUANTITY` - period + +#### Returns + +`ARRAY` of `OBJECT` - A DAG block object: +* `pivot`: `DATA`, 32 Bytes - +* `level`: `QUANTITY` - Level at which block was produced +* `period`: `QUANTITY` - Finalization period of this block +* `tips`: `ARRAY` - List of tips +* `transactions`: `ARRAY` - Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter. +* `trx_estimations`: `ARRAY` - Array of `QUANTITIES` that means transaction gas estimation for corresponding transaction. For example `trx_estimations[0]` is for `transactions[0]` +* `sig`: `DATA`, 65 Bytes - Signature of block creator +* `hash`: `DATA`, 32 Bytes - Hash of this block +* `sender`: `DATA`, 20 Bytes - Address of block creator +* `timestamp`: `QUANTITY` - Time of block creation +* `vdf`: `OBJECT` - vdf proof for this block + * `pk`: `DATA`, 32 Bytes - Public key + * `proof`: `DATA`, 80 Bytes - Bytes of proof + * `sol1`: `DATA` - First part of solution + * `sol1`: `DATA` - Second part of solution + * `difficulty`: `QUANTITY` - Difficulty with which block was produced + +#### Example + +```json +// Request +curl -X POST --data '{"jsonrpc":"2.0","method":"debug_getPeriodDagBlocks","params":["0x100"],"id":1}' + +// Result +{ + "id": 1, + "jsonrpc": "2.0", + "result": [ + { + "hash": "0xc142fafaebd46803a8c5bd8d7411a202e163fad0513e3fa2431d379b5c267a77", + "level": "0x1f9", + "period": "0x100", + "pivot": "0xa1e8e5485167951b81172ff6fe04b0d9fa7ac02280824da97e8af42cab783daa", + "sender": "0xe143ededbf9285dc39daf64c120037a4b9a0f095", + "sig": "0x8b5ba54ef14ee89255a4db84007f119589e73cb0b98befb3d04fd9fa0d92dc923dcb6ec2f6ffe60cd31a5d28fd2c290c293fa12fabff7e25d0051b701118748501", + "timestamp": "0x642ab512", + "tips": [], + "transactions": [...], + "trx_estimations": "0x668a0", + "vdf": { + "difficulty": "0x10", + "proof": "0x7ef44c562d64fb9169e6db68d720d182e4d9648c80349699a82c5b358cd9bc4f9b3ee50c97651bbc5579f98f412b642aa4b3c4af5a038816e54457152459e1e90cee85bcdc5fb386748777586186a10b", + "sol1": "0x041536b789da11f68c22b2ce1c23ed969a28e6269d3e0e65f81bfa3049e021875cff724803fe6f5ec3fe5362054eee9965978a004372b7249f4ce8485c7e92a64696aada82150329764612f9f5fabeabf0fb08e1ec33d7b52db165259033ca52d6f75284bb0163c993e0e6a4725e9a3417948ddf3dad8581aa596c2aeb53f321d6675d53ead1bafdee2d31928d77ad61452ad0e3c2c04cb6d27bfff2aaf94e2a47098f0270c62e7c5d1eb8e8c98398d2a75146a70b7bcbfc03be12c9deaf427562a9c37fbb93d466e6055ecd52abf6f9e14b49d4df19dc24187e22f62d0ed061c91b4c1b1712be47987e785094e91ea973af806c6b14299783ea0481070853d8", + "sol2": "0x2bb0760e2b5b4cbe2dfbe5c740206ad0f8606d397a7e16b157ebc4dcf899e67175d8ea83ac7c1b13cd7e84f1032f625b3d216c6595c5285898257a2d3b8359079cc6d6a7d5381a77756fe021747d3dcbae7cdaabc6def80eb9ee2e0b7cd9c7a85d06acaee456198d45b6652872b47f5dd5895548fc459d0ac854334c3ec3749b7011df27ee1b22ab7f8ad6ed731fbc2d8dc96eaf9e4d2fc87e2e8a1c52bff11e087e5da1f5c1e1ee316d1cd279d628c4a38cfb727b0aff896a82d9fe68a3c64490809f002615954ccd6422818210b4cafacd21dcf542c4c81263b71ccd149869d3077c3af5b674ae08b01059414247410f6f76f5215cfe40e10d0d95d5936197" + } + }, ... + ] +} +``` + +### debug_getPreviousBlockCertVotes + +Returns all previous block cert votes for specified period + +#### Parameters + +`QUANTITY` - period + +#### Returns + +`ARRAY` of `OBJECT` - A Vote block object: +* `hash`: `DATA`, 32 Bytes - Hash of the vote +* `voter`: `DATA`, 20 Bytes - Address of vote creator +* `sig`: `DATA`, 65 Bytes - Vote signature +* `block_hash`: `DATA`, 32 Bytes - Hash of the block vote +* `weight`: `QUANTITY` - weight of the vote + +#### Example + +```json +// Request +curl -X POST --data '{"jsonrpc":"2.0","method":"debug_getPreviousBlockCertVotes","params":["0x100"],"id":1}' + +// Result +{ + "id": 1, + "jsonrpc": "2.0", + "result": [ + { + + }, ... + ] +} +``` \ No newline at end of file diff --git a/libraries/core_libs/network/rpc/Debug.cpp b/libraries/core_libs/network/rpc/Debug.cpp index 9cfb85bf55..66316be57f 100644 --- a/libraries/core_libs/network/rpc/Debug.cpp +++ b/libraries/core_libs/network/rpc/Debug.cpp @@ -5,6 +5,7 @@ #include "common/jsoncpp.hpp" #include "final_chain/state_api_data.hpp" +#include "network/rpc/eth/data.hpp" #include "pbft/pbft_manager.hpp" using namespace std; @@ -111,6 +112,120 @@ Json::Value Debug::trace_replayBlockTransactions(const std::string& block_num, c return res; } +template +Json::Value transformToJsonParallel(const S& source, FN op) { + if (source.empty()) { + return Json::Value(Json::arrayValue); + } + static util::ThreadPool executor{std::thread::hardware_concurrency() / 2}; + + Json::Value out(Json::arrayValue); + out.resize(source.size()); + std::atomic_uint processed = 0; + for (unsigned i = 0; i < source.size(); ++i) { + executor.post([&, i]() { + out[i] = op(source[i]); + ++processed; + }); + } + + while (true) { + if (processed == source.size()) { + break; + } + } + return out; +} + +Json::Value mergeJsons(Json::Value&& o1, Json::Value&& o2) { + for (auto itr = o2.begin(); itr != o2.end(); ++itr) { + o1[itr.key().asString()] = *itr; + } + return o1; +} + +Json::Value Debug::debug_getPeriodTransactionsWithReceipts(const std::string& _period) { + try { + auto node = full_node_.lock(); + if (!node) { + BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INTERNAL_ERROR)); + } + auto final_chain = node->getFinalChain(); + auto period = dev::jsToInt(_period); + auto block_hash = final_chain->block_hash(period); + auto trxs = node->getDB()->getPeriodTransactions(period); + if (!trxs.has_value()) { + return Json::Value(Json::arrayValue); + } + + return transformToJsonParallel(*trxs, [&final_chain, &block_hash](const auto& trx) { + auto hash = trx->getHash(); + auto r = final_chain->transaction_receipt(hash); + auto location = + rpc::eth::ExtendedTransactionLocation{{*final_chain->transaction_location(hash), *block_hash}, hash}; + auto transaction = rpc::eth::LocalisedTransaction{trx, location}; + auto receipt = rpc::eth::LocalisedTransactionReceipt{*r, location, trx->getSender(), trx->getReceiver()}; + auto receipt_json = rpc::eth::toJson(receipt); + receipt_json.removeMember("transactionHash"); + + return mergeJsons(rpc::eth::toJson(transaction), std::move(receipt_json)); + }); + } catch (...) { + BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS)); + } +} + +Json::Value Debug::debug_getPeriodDagBlocks(const std::string& _period) { + try { + auto node = full_node_.lock(); + if (!node) { + BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INTERNAL_ERROR)); + } + + auto period = dev::jsToInt(_period); + auto dags = node->getDB()->getFinalizedDagBlockByPeriod(period); + + return transformToJsonParallel(dags, [&period](const auto& dag) { + auto block_json = dag->getJson(); + block_json["period"] = toJS(period); + return block_json; + }); + } catch (...) { + BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS)); + } +} + +Json::Value Debug::debug_getPreviousBlockCertVotes(const std::string& _period) { + try { + auto node = full_node_.lock(); + if (!node) { + BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INTERNAL_ERROR)); + } + + auto final_chain = node->getFinalChain(); + auto vote_manager = node->getVoteManager(); + + Json::Value res(Json::objectValue); + + auto period = dev::jsToInt(_period); + auto votes = node->getDB()->getPeriodCertVotes(period); + if (votes.empty()) { + return res; + } + + const auto votes_period = votes.front()->getPeriod(); + const uint64_t total_dpos_votes_count = final_chain->dpos_eligible_total_vote_count(votes_period - 1); + res["total_votes_count"] = total_dpos_votes_count; + res["votes"] = transformToJsonParallel(votes, [&](const auto& vote) { + vote_manager->validateVote(vote); + return vote->toJSON(); + }); + return res; + } catch (...) { + BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS)); + } +} + state_api::Tracing Debug::parse_tracking_parms(const Json::Value& json) const { state_api::Tracing ret; if (!json.isArray() || json.empty()) { diff --git a/libraries/core_libs/network/rpc/Debug.h b/libraries/core_libs/network/rpc/Debug.h index d318f9deee..0ed159b412 100644 --- a/libraries/core_libs/network/rpc/Debug.h +++ b/libraries/core_libs/network/rpc/Debug.h @@ -34,6 +34,9 @@ class Debug : public DebugFace { virtual Json::Value debug_traceTransaction(const std::string& param1) override; virtual Json::Value debug_traceCall(const Json::Value& param1, const std::string& param2) override; + virtual Json::Value debug_getPeriodTransactionsWithReceipts(const std::string& _period) override; + virtual Json::Value debug_getPeriodDagBlocks(const std::string& _period) override; + virtual Json::Value debug_getPreviousBlockCertVotes(const std::string& _period) override; virtual Json::Value trace_call(const Json::Value& param1, const Json::Value& param2, const std::string& param3) override; virtual Json::Value trace_replayTransaction(const std::string& param1, const Json::Value& param2) override; diff --git a/libraries/core_libs/network/rpc/Debug.jsonrpc.json b/libraries/core_libs/network/rpc/Debug.jsonrpc.json index 88c5411af1..2d673470b8 100644 --- a/libraries/core_libs/network/rpc/Debug.jsonrpc.json +++ b/libraries/core_libs/network/rpc/Debug.jsonrpc.json @@ -16,6 +16,24 @@ "order": [], "returns": {} }, + { + "name": "debug_getPeriodTransactionsWithReceipts", + "params": [""], + "order": [], + "returns": [] + }, + { + "name": "debug_getPeriodDagBlocks", + "params": [""], + "order": [], + "returns": [] + }, + { + "name": "debug_getPreviousBlockCertVotes", + "params": [""], + "order": [], + "returns": {} + }, { "name": "trace_call", "params": [ diff --git a/libraries/core_libs/network/rpc/DebugClient.h b/libraries/core_libs/network/rpc/DebugClient.h index f0faaf58ec..8849aca181 100644 --- a/libraries/core_libs/network/rpc/DebugClient.h +++ b/libraries/core_libs/network/rpc/DebugClient.h @@ -33,6 +33,33 @@ class DebugClient : public jsonrpc::Client { else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } + Json::Value debug_getPreviousBlockCertVotes(const std::string& param1) throw(jsonrpc::JsonRpcException) { + Json::Value p; + p.append(param1); + Json::Value result = this->CallMethod("debug_getPreviousBlockCertVotes", p); + if (result.isObject()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + } + Json::Value debug_getPeriodTransactionsWithReceipts(const std::string& param1) throw(jsonrpc::JsonRpcException) { + Json::Value p; + p.append(param1); + Json::Value result = this->CallMethod("debug_getPeriodTransactionsWithReceipts", p); + if (result.isObject()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + } + Json::Value debug_getPeriodDagBlocks(const std::string& param1) throw(jsonrpc::JsonRpcException) { + Json::Value p; + p.append(param1); + Json::Value result = this->CallMethod("debug_getPeriodDagBlocks", p); + if (result.isObject()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + } Json::Value trace_call(const Json::Value& param1, const Json::Value& param2, const std::string& param3) throw(jsonrpc::JsonRpcException) { Json::Value p; diff --git a/libraries/core_libs/network/rpc/DebugFace.h b/libraries/core_libs/network/rpc/DebugFace.h index fbfd16f363..23c0e7ba89 100644 --- a/libraries/core_libs/network/rpc/DebugFace.h +++ b/libraries/core_libs/network/rpc/DebugFace.h @@ -18,6 +18,15 @@ class DebugFace : public ServerInterface { this->bindAndAddMethod(jsonrpc::Procedure("debug_traceCall", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_OBJECT, "param2", jsonrpc::JSON_STRING, NULL), &taraxa::net::DebugFace::debug_traceCallI); + this->bindAndAddMethod(jsonrpc::Procedure("debug_getPreviousBlockCertVotes", jsonrpc::PARAMS_BY_POSITION, + jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_STRING, NULL), + &taraxa::net::DebugFace::debug_getPreviousBlockCertVotesI); + this->bindAndAddMethod(jsonrpc::Procedure("debug_getPeriodTransactionsWithReceipts", jsonrpc::PARAMS_BY_POSITION, + jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_STRING, NULL), + &taraxa::net::DebugFace::debug_getPeriodTransactionsWithReceiptsI); + this->bindAndAddMethod(jsonrpc::Procedure("debug_getPeriodDagBlocks", jsonrpc::PARAMS_BY_POSITION, + jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_STRING, NULL), + &taraxa::net::DebugFace::debug_getPeriodDagBlocksI); this->bindAndAddMethod( jsonrpc::Procedure("trace_call", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_OBJECT, "param2", jsonrpc::JSON_ARRAY, "param3", jsonrpc::JSON_STRING, NULL), @@ -38,6 +47,15 @@ class DebugFace : public ServerInterface { inline virtual void debug_traceCallI(const Json::Value& request, Json::Value& response) { response = this->debug_traceCall(request[0u], request[1u].asString()); } + inline virtual void debug_getPreviousBlockCertVotesI(const Json::Value& request, Json::Value& response) { + response = this->debug_getPreviousBlockCertVotes(request[0u].asString()); + } + inline virtual void debug_getPeriodTransactionsWithReceiptsI(const Json::Value& request, Json::Value& response) { + response = this->debug_getPeriodTransactionsWithReceipts(request[0u].asString()); + } + inline virtual void debug_getPeriodDagBlocksI(const Json::Value& request, Json::Value& response) { + response = this->debug_getPeriodDagBlocks(request[0u].asString()); + } inline virtual void trace_callI(const Json::Value& request, Json::Value& response) { response = this->trace_call(request[0u], request[1u], request[2u].asString()); } @@ -49,6 +67,9 @@ class DebugFace : public ServerInterface { } virtual Json::Value debug_traceTransaction(const std::string& param1) = 0; virtual Json::Value debug_traceCall(const Json::Value& param1, const std::string& param2) = 0; + virtual Json::Value debug_getPreviousBlockCertVotes(const std::string& param1) = 0; + virtual Json::Value debug_getPeriodTransactionsWithReceipts(const std::string& param1) = 0; + virtual Json::Value debug_getPeriodDagBlocks(const std::string& param1) = 0; virtual Json::Value trace_call(const Json::Value& param1, const Json::Value& param2, const std::string& param3) = 0; virtual Json::Value trace_replayTransaction(const std::string& param1, const Json::Value& param2) = 0; virtual Json::Value trace_replayBlockTransactions(const std::string& param1, const Json::Value& param2) = 0; diff --git a/libraries/core_libs/network/rpc/Taraxa.cpp b/libraries/core_libs/network/rpc/Taraxa.cpp index b062df6d07..5853befab2 100644 --- a/libraries/core_libs/network/rpc/Taraxa.cpp +++ b/libraries/core_libs/network/rpc/Taraxa.cpp @@ -156,108 +156,4 @@ Json::Value Taraxa::taraxa_getChainStats() { return res; } -Json::Value mergeJsons(Json::Value&& o1, Json::Value&& o2) { - for (auto itr = o2.begin(); itr != o2.end(); ++itr) { - o1[itr.key().asString()] = *itr; - } - return o1; -} - -template -Json::Value transformToJsonParallel(const S& source, FN op) { - if (source.empty()) { - return Json::Value(Json::arrayValue); - } - - static util::ThreadPool executor{std::thread::hardware_concurrency() / 2}; - - Json::Value out(Json::arrayValue); - out.resize(source.size()); - std::atomic_uint processed = 0; - for (unsigned i = 0; i < source.size(); ++i) { - executor.post([&, i]() { - out[i] = op(source[i]); - ++processed; - }); - } - - while (true) { - if (processed == source.size()) { - break; - } - } - return out; -} - -Json::Value Taraxa::taraxa_getPeriodTransactionsWithReceipts(const std::string& _period) { - try { - auto node = tryGetNode(); - auto final_chain = node->getFinalChain(); - auto period = dev::jsToInt(_period); - auto block_hash = final_chain->block_hash(period); - auto trxs = node->getDB()->getPeriodTransactions(period); - if (!trxs.has_value()) { - return Json::Value(Json::arrayValue); - } - - return transformToJsonParallel(*trxs, [&final_chain, &block_hash](const auto& trx) { - auto hash = trx->getHash(); - auto r = final_chain->transaction_receipt(hash); - auto location = - rpc::eth::ExtendedTransactionLocation{{*final_chain->transaction_location(hash), *block_hash}, hash}; - auto transaction = rpc::eth::LocalisedTransaction{trx, location}; - auto receipt = rpc::eth::LocalisedTransactionReceipt{*r, location, trx->getSender(), trx->getReceiver()}; - auto receipt_json = rpc::eth::toJson(receipt); - receipt_json.removeMember("transactionHash"); - - return mergeJsons(rpc::eth::toJson(transaction), std::move(receipt_json)); - }); - } catch (...) { - BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS)); - } -} - -Json::Value Taraxa::taraxa_getPeriodDagBlocks(const std::string& _period) { - try { - auto period = dev::jsToInt(_period); - auto node = tryGetNode(); - auto dags = node->getDB()->getFinalizedDagBlockByPeriod(period); - - return transformToJsonParallel(dags, [&period](const auto& dag) { - auto block_json = dag->getJson(); - block_json["period"] = toJS(period); - return block_json; - }); - } catch (...) { - BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS)); - } -} - -Json::Value Taraxa::taraxa_getPreviousBlockCertVotes(const std::string& _period) { - try { - auto node = tryGetNode(); - auto final_chain = node->getFinalChain(); - auto vote_manager = node->getVoteManager(); - - Json::Value res(Json::objectValue); - - auto period = dev::jsToInt(_period); - auto votes = node->getDB()->getPeriodCertVotes(period); - if (votes.empty()) { - return res; - } - - const auto votes_period = votes.front()->getPeriod(); - const uint64_t total_dpos_votes_count = final_chain->dpos_eligible_total_vote_count(votes_period - 1); - res["total_votes_count"] = total_dpos_votes_count; - res["votes"] = transformToJsonParallel(votes, [&](const auto& vote) { - vote_manager->validateVote(vote); - return vote->toJSON(); - }); - return res; - } catch (...) { - BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS)); - } -} - } // namespace taraxa::net \ No newline at end of file diff --git a/libraries/core_libs/network/rpc/Taraxa.h b/libraries/core_libs/network/rpc/Taraxa.h index e984f6d057..39b32cca34 100644 --- a/libraries/core_libs/network/rpc/Taraxa.h +++ b/libraries/core_libs/network/rpc/Taraxa.h @@ -29,9 +29,6 @@ class Taraxa : public TaraxaFace { virtual std::string taraxa_pbftBlockHashByPeriod(const std::string& _period) override; virtual Json::Value taraxa_getConfig() override; virtual Json::Value taraxa_getChainStats() override; - virtual Json::Value taraxa_getPeriodTransactionsWithReceipts(const std::string& _period) override; - virtual Json::Value taraxa_getPeriodDagBlocks(const std::string& _period) override; - virtual Json::Value taraxa_getPreviousBlockCertVotes(const std::string& _period) override; protected: std::weak_ptr full_node_; diff --git a/libraries/core_libs/network/rpc/Taraxa.jsonrpc.json b/libraries/core_libs/network/rpc/Taraxa.jsonrpc.json index 23202759c5..15e5b2135c 100644 --- a/libraries/core_libs/network/rpc/Taraxa.jsonrpc.json +++ b/libraries/core_libs/network/rpc/Taraxa.jsonrpc.json @@ -64,24 +64,6 @@ "params": [""], "order": [], "returns": "" - }, - { - "name": "taraxa_getPeriodTransactionsWithReceipts", - "params": [""], - "order": [], - "returns": [] - }, - { - "name": "taraxa_getPeriodDagBlocks", - "params": [""], - "order": [], - "returns": [] - }, - { - "name": "taraxa_getPreviousBlockCertVotes", - "params": [""], - "order": [], - "returns": {} } ] diff --git a/libraries/core_libs/network/rpc/TaraxaClient.h b/libraries/core_libs/network/rpc/TaraxaClient.h index 82c6a1363c..a8a3c105a1 100644 --- a/libraries/core_libs/network/rpc/TaraxaClient.h +++ b/libraries/core_libs/network/rpc/TaraxaClient.h @@ -106,33 +106,6 @@ class TaraxaClient : public jsonrpc::Client { else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value taraxa_getPeriodTransactionsWithReceipts(const std::string& param1) throw(jsonrpc::JsonRpcException) { - Json::Value p; - p.append(param1); - Json::Value result = this->CallMethod("taraxa_getPeriodTransactionsWithReceipts", p); - if (result.isObject()) - return result; - else - throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); - } - Json::Value taraxa_getPeriodDagBlocks(const std::string& param1) throw(jsonrpc::JsonRpcException) { - Json::Value p; - p.append(param1); - Json::Value result = this->CallMethod("taraxa_getPeriodDagBlocks", p); - if (result.isObject()) - return result; - else - throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); - } - Json::Value taraxa_getPreviousBlockCertVotes(const std::string& param1) throw(jsonrpc::JsonRpcException) { - Json::Value p; - p.append(param1); - Json::Value result = this->CallMethod("taraxa_getPreviousBlockCertVotes", p); - if (result.isObject()) - return result; - else - throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); - } }; } // namespace net diff --git a/libraries/core_libs/network/rpc/TaraxaFace.h b/libraries/core_libs/network/rpc/TaraxaFace.h index e24b3630d5..3f8a19c448 100644 --- a/libraries/core_libs/network/rpc/TaraxaFace.h +++ b/libraries/core_libs/network/rpc/TaraxaFace.h @@ -44,15 +44,6 @@ class TaraxaFace : public ServerInterface { this->bindAndAddMethod(jsonrpc::Procedure("taraxa_pbftBlockHashByPeriod", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1", jsonrpc::JSON_STRING, NULL), &taraxa::net::TaraxaFace::taraxa_pbftBlockHashByPeriodI); - this->bindAndAddMethod(jsonrpc::Procedure("taraxa_getPeriodTransactionsWithReceipts", jsonrpc::PARAMS_BY_POSITION, - jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_STRING, NULL), - &taraxa::net::TaraxaFace::taraxa_getPeriodTransactionsWithReceiptsI); - this->bindAndAddMethod(jsonrpc::Procedure("taraxa_getPeriodDagBlocks", jsonrpc::PARAMS_BY_POSITION, - jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_STRING, NULL), - &taraxa::net::TaraxaFace::taraxa_getPeriodDagBlocksI); - this->bindAndAddMethod(jsonrpc::Procedure("taraxa_getPreviousBlockCertVotes", jsonrpc::PARAMS_BY_POSITION, - jsonrpc::JSON_OBJECT, "param1", jsonrpc::JSON_STRING, NULL), - &taraxa::net::TaraxaFace::taraxa_getPeriodPreviousBlockCertVotesI); } inline virtual void taraxa_protocolVersionI(const Json::Value &request, Json::Value &response) { @@ -91,15 +82,6 @@ class TaraxaFace : public ServerInterface { inline virtual void taraxa_pbftBlockHashByPeriodI(const Json::Value &request, Json::Value &response) { response = this->taraxa_pbftBlockHashByPeriod(request[0u].asString()); } - inline virtual void taraxa_getPeriodTransactionsWithReceiptsI(const Json::Value &request, Json::Value &response) { - response = this->taraxa_getPeriodTransactionsWithReceipts(request[0u].asString()); - } - inline virtual void taraxa_getPeriodDagBlocksI(const Json::Value &request, Json::Value &response) { - response = this->taraxa_getPeriodDagBlocks(request[0u].asString()); - } - inline virtual void taraxa_getPeriodPreviousBlockCertVotesI(const Json::Value &request, Json::Value &response) { - response = this->taraxa_getPreviousBlockCertVotes(request[0u].asString()); - } virtual std::string taraxa_protocolVersion() = 0; virtual Json::Value taraxa_getVersion() = 0; @@ -111,9 +93,6 @@ class TaraxaFace : public ServerInterface { virtual Json::Value taraxa_getConfig() = 0; virtual Json::Value taraxa_getChainStats() = 0; virtual std::string taraxa_pbftBlockHashByPeriod(const std::string ¶m1) = 0; - virtual Json::Value taraxa_getPeriodTransactionsWithReceipts(const std::string ¶m1) = 0; - virtual Json::Value taraxa_getPeriodDagBlocks(const std::string ¶m1) = 0; - virtual Json::Value taraxa_getPreviousBlockCertVotes(const std::string ¶m1) = 0; }; } // namespace net diff --git a/libraries/types/vote/src/vote.cpp b/libraries/types/vote/src/vote.cpp index 83bc0745b6..3b11699e10 100644 --- a/libraries/types/vote/src/vote.cpp +++ b/libraries/types/vote/src/vote.cpp @@ -82,7 +82,9 @@ Json::Value Vote::toJSON() const { json["voter"] = dev::toJS(getVoterAddr()); json["signature"] = dev::toJS(getVoteSignature()); json["block_hash"] = dev::toJS(getBlockHash()); - json["weight"] = dev::toJS(*weight_); + if (weight_.has_value()) { + json["weight"] = dev::toJS(*weight_); + } return json; } diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index 3072170de2..af5164e1dc 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit 3072170de250ca420767abadce8cb5ee508c262d +Subproject commit af5164e1dc56318c40367b910b1e890a3b9cf180 From 356fbf31e59868f6ec88a1cc40db0737f2dbf0a1 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Fri, 30 Jun 2023 14:49:47 -0700 Subject: [PATCH 081/134] implement getTotalDelegation function --- submodules/taraxa-evm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index af5164e1dc..6c59f9bfc1 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit af5164e1dc56318c40367b910b1e890a3b9cf180 +Subproject commit 6c59f9bfc1c7e1fd0f1d05c57a19a5509c536d91 From 359adbd39d13e37530ed2356378f1725d4d3d2dd Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Fri, 9 Jun 2023 11:01:55 -0700 Subject: [PATCH 082/134] clean up code from test related code --- .../consensus/include/pbft/pbft_manager.hpp | 8 +- .../consensus/src/final_chain/final_chain.cpp | 1 - .../consensus/src/pbft/pbft_manager.cpp | 20 -- .../network/include/network/network.hpp | 16 +- .../network/tarcap/taraxa_capability.hpp | 2 +- libraries/core_libs/network/src/network.cpp | 99 ++++------ .../latest/dag_block_packet_handler.cpp | 11 +- .../latest/status_packet_handler.cpp | 47 +++-- .../core_libs/node/include/node/node.hpp | 80 ++++---- libraries/core_libs/node/src/node.cpp | 2 +- libraries/core_libs/storage/src/storage.cpp | 4 + tests/network_test.cpp | 88 ++++----- tests/p2p_test.cpp | 172 ++++++++---------- 13 files changed, 230 insertions(+), 320 deletions(-) diff --git a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp index 8924a25e67..017f032b2f 100644 --- a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp +++ b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp @@ -50,7 +50,7 @@ enum PbftStates { value_proposal_state = 1, filter_state, certify_state, finish_ * - Finish polling state: Start after first finish state. If node receives enough next voting votes within 2 lambda * duration, PBFT will go to next round. Otherwise that will go back to Finish state. */ -class PbftManager : public std::enable_shared_from_this { +class PbftManager { public: using time_point = std::chrono::system_clock::time_point; @@ -246,12 +246,6 @@ class PbftManager : public std::enable_shared_from_this { const std::shared_ptr &propose_vote); // **** Notice: functions used only in tests **** - // TODO: Add a check for some kind of guards to ensure these are only called from within a test - /** - * @brief Resume PBFT daemon. Only to be used for unit tests - */ - void resume(); - /** * @brief Get a proposed PBFT block based on specified period and block hash * @param period diff --git a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp index f6fc4a375c..d9c17abbf3 100644 --- a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp +++ b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp @@ -1,6 +1,5 @@ #include "final_chain/final_chain.hpp" -#include #include #include "common/constants.hpp" diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index f343726283..fa8fac2969 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -104,26 +104,6 @@ void PbftManager::stop() { LOG(log_dg_) << "PBFT daemon terminated ..."; } -// Only to be used for tests... -void PbftManager::resume() { - // Will only appear in testing... - LOG(log_si_) << "Resuming PBFT daemon..."; - - if (step_ == 1) { - state_ = value_proposal_state; - } else if (step_ == 2) { - state_ = filter_state; - } else if (step_ == 3) { - state_ = certify_state; - } else if (step_ % 2 == 0) { - state_ = finish_state; - } else { - state_ = finish_polling_state; - } - - daemon_ = std::make_unique([this]() { run(); }); -} - /* When a node starts up it has to sync to the current phase (type of block * being generated) and step (within the block generation round) * Five step loop for block generation over three phases of blocks diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index 07a5961e27..db2ef24491 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -32,12 +32,10 @@ class PacketHandler; class Network { public: - Network(const FullNodeConfig &config, const h256 &genesis_hash = {}, - const std::filesystem::path &network_file_path = {}, const dev::KeyPair &key = dev::KeyPair::create(), - std::shared_ptr db = {}, std::shared_ptr pbft_mgr = {}, - std::shared_ptr pbft_chain = {}, std::shared_ptr vote_mgr = {}, - std::shared_ptr dag_mgr = {}, std::shared_ptr trx_mgr = {}, - const std::vector &create_test_tarcaps = {}); + Network(const FullNodeConfig &config, const h256 &genesis_hash, const std::filesystem::path &network_file_path, + const dev::KeyPair &key, std::shared_ptr db, std::shared_ptr pbft_mgr, + std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, + std::shared_ptr dag_mgr, std::shared_ptr trx_mgr); ~Network(); Network(const Network &) = delete; @@ -45,8 +43,11 @@ class Network { Network &operator=(const Network &) = delete; Network &operator=(Network &&) = delete; - // METHODS USED IN REAL CODE + /** + * @brief Starts threadpools for packets communication in general, specific packets processing and periodic events + */ void start(); + bool isStarted(); std::list getAllNodes() const; size_t getPeerCount(); @@ -68,7 +69,6 @@ class Network { template std::shared_ptr getSpecificHandler() const; - void setPendingPeersToReady(); dev::p2p::NodeID getNodeId() const; std::shared_ptr getPeer(dev::p2p::NodeID const &id) const; // END METHODS USED IN TESTS ONLY diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp index f653b00e33..0cda972aa5 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp @@ -86,7 +86,7 @@ class TaraxaCapability final : public dev::p2p::CapabilityFace { private: // Capability version - unsigned version_; + TarcapVersion version_; // Packets stats per time period std::shared_ptr all_packets_stats_; diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index ea8e69261e..29cbecd9ba 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -22,8 +22,7 @@ namespace taraxa { Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::filesystem::path const &network_file_path, dev::KeyPair const &key, std::shared_ptr db, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, - const std::vector &create_test_tarcaps) + std::shared_ptr dag_mgr, std::shared_ptr trx_mgr) : kConf(config), pub_key_(key.pub()), all_packets_stats_(nullptr), @@ -63,47 +62,30 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::fi taraxa_net_conf.expected_parallelism = tp_.capacity(); string net_version = "TaraxaNode"; // TODO maybe give a proper name? - dev::p2p::Host::CapabilitiesFactory constructCapabilities; - - // Create real taraxa capabilities - if (create_test_tarcaps.empty()) { - constructCapabilities = [&](std::weak_ptr host) { - assert(!host.expired()); - - const size_t kV1NetworkVersion = 1; - assert(kV1NetworkVersion < TARAXA_NET_VERSION); - - dev::p2p::Host::CapabilityList capabilities; - - // Register old version (V1) of taraxa capability - auto v1_tarcap = std::make_shared( - kV1NetworkVersion, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, db, - pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, network::tarcap::v1::kInitV1Handlers); - capabilities.emplace_back(v1_tarcap); - - // Register latest version of taraxa capability - auto latest_tarcap = std::make_shared( - TARAXA_NET_VERSION, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, db, - pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr); - capabilities.emplace_back(latest_tarcap); - - return capabilities; - }; - } else { // Create test taraxa capabilities - constructCapabilities = [&](std::weak_ptr host) { - assert(!host.expired()); - - dev::p2p::Host::CapabilityList capabilities; - for (const auto test_tarcap_version : create_test_tarcaps) { - auto tarcap = std::make_shared( - test_tarcap_version, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, - db, pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr); - capabilities.emplace_back(tarcap); - } - return capabilities; - }; - } + // Create taraxa capabilities + dev::p2p::Host::CapabilitiesFactory constructCapabilities = [&](std::weak_ptr host) { + assert(!host.expired()); + + const size_t kV1NetworkVersion = 1; + assert(kV1NetworkVersion < TARAXA_NET_VERSION); + + dev::p2p::Host::CapabilityList capabilities; + + // Register old version (V1) of taraxa capability + auto v1_tarcap = std::make_shared( + kV1NetworkVersion, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, db, + pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, network::tarcap::v1::kInitV1Handlers); + capabilities.emplace_back(v1_tarcap); + + // Register latest version of taraxa capability + auto latest_tarcap = std::make_shared( + TARAXA_NET_VERSION, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, db, + pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr); + capabilities.emplace_back(latest_tarcap); + + return capabilities; + }; host_ = dev::p2p::Host::make(net_version, constructCapabilities, key, net_conf, taraxa_net_conf, network_file_path); for (const auto &cap : host_->getSupportedCapabilities()) { @@ -130,14 +112,14 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::fi Network::~Network() { tp_.stop(); - periodic_events_tp_.stop(); packets_tp_->stopProcessing(); + periodic_events_tp_.stop(); } void Network::start() { packets_tp_->startProcessing(); - periodic_events_tp_.start(); tp_.start(); + periodic_events_tp_.start(); LOG(log_nf_) << "Started Node id: " << host_->id() << ", listening on port " << host_->listenPort(); } @@ -186,15 +168,13 @@ void Network::registerPeriodicEvents(const std::shared_ptr &pbft_mg uint64_t lambda_ms = pbft_mgr ? pbft_mgr->getPbftInitialLambda().count() : 2000; // Send new transactions - if (trx_mgr) { // because of tests - auto sendTxs = [this, trx_mgr = trx_mgr]() { - for (auto &tarcap : tarcaps_) { - auto tx_packet_handler = tarcap.second->getSpecificHandler(); - tx_packet_handler->periodicSendTransactions(trx_mgr->getAllPoolTrxs()); - } - }; - periodic_events_tp_.post_loop({kConf.network.transaction_interval_ms}, sendTxs); - } + auto sendTxs = [this, trx_mgr = trx_mgr]() { + for (auto &tarcap : tarcaps_) { + auto tx_packet_handler = tarcap.second->getSpecificHandler(); + tx_packet_handler->periodicSendTransactions(trx_mgr->getAllPoolTrxs()); + } + }; + periodic_events_tp_.post_loop({kConf.network.transaction_interval_ms}, sendTxs); // Send status packet auto sendStatus = [this]() { @@ -339,19 +319,6 @@ std::shared_ptr Network::getMaxChainPeer() const { // METHODS USED IN TESTS ONLY // Note: for functions use in tests all data are fetched only from the tarcap with the highest version, // other functions must use all tarcaps - -void Network::setPendingPeersToReady() { - const auto &peers_state = tarcaps_.begin()->second->getPeersState(); - - auto peerIds = peers_state->getAllPendingPeersIDs(); - for (const auto &peerId : peerIds) { - auto peer = peers_state->getPendingPeer(peerId); - if (peer) { - peers_state->setPeerAsReadyToSendMessages(peerId, peer); - } - } -} - dev::p2p::NodeID Network::getNodeId() const { return host_->id(); } std::shared_ptr Network::getPeer(dev::p2p::NodeID const &id) const { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp index 6438c98d57..197bc59bb7 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp @@ -231,13 +231,12 @@ void DagBlockPacketHandler::onNewBlockVerified(const DagBlock &block, bool propo if (peer->isTransactionKnown(trx_hash)) { continue; } + // Db can be nullptr in some unit tests - if (db_ != nullptr) { - auto trx = db_->getTransaction(trx_hash); - assert(trx != nullptr); - transactions_to_send.push_back(trx); - peer_and_transactions_to_log += trx_hash.abridged(); - } + auto trx = db_->getTransaction(trx_hash); + assert(trx != nullptr); + transactions_to_send.push_back(trx); + peer_and_transactions_to_log += trx_hash.abridged(); } sendBlock(peer_id, block, transactions_to_send); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp index 95fd704ecf..855df89d43 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/status_packet_handler.cpp @@ -156,31 +156,28 @@ void StatusPacketHandler::process(const threadpool::PacketData& packet_data, con bool StatusPacketHandler::sendStatus(const dev::p2p::NodeID& node_id, bool initial) { bool success = false; - - if (dag_mgr_) { - std::string status_packet_type = initial ? "initial" : "standard"; - - LOG(log_dg_) << "Sending " << status_packet_type << " status message to " << node_id << ", protocol version " - << TARAXA_NET_VERSION << ", network id " << kConf.genesis.chain_id << ", genesis " << kGenesisHash - << ", node version " << TARAXA_VERSION; - - auto dag_max_level = dag_mgr_->getMaxLevel(); - auto pbft_chain_size = pbft_chain_->getPbftChainSize(); - const auto pbft_round = pbft_mgr_->getPbftRound(); - - if (initial) { - success = sealAndSend(node_id, StatusPacket, - std::move(dev::RLPStream(kInitialStatusPacketItemsCount) - << kConf.genesis.chain_id << dag_max_level << kGenesisHash << pbft_chain_size - << pbft_syncing_state_->isPbftSyncing() << pbft_round << TARAXA_MAJOR_VERSION - << TARAXA_MINOR_VERSION << TARAXA_PATCH_VERSION << dag_mgr_->isLightNode() - << dag_mgr_->getLightNodeHistory())); - } else { - success = sealAndSend( - node_id, StatusPacket, - std::move(dev::RLPStream(kStandardStatusPacketItemsCount) - << dag_max_level << pbft_chain_size << pbft_syncing_state_->isDeepPbftSyncing() << pbft_round)); - } + std::string status_packet_type = initial ? "initial" : "standard"; + + LOG(log_dg_) << "Sending " << status_packet_type << " status message to " << node_id << ", protocol version " + << TARAXA_NET_VERSION << ", network id " << kConf.genesis.chain_id << ", genesis " << kGenesisHash + << ", node version " << TARAXA_VERSION; + + auto dag_max_level = dag_mgr_->getMaxLevel(); + auto pbft_chain_size = pbft_chain_->getPbftChainSize(); + const auto pbft_round = pbft_mgr_->getPbftRound(); + + if (initial) { + success = sealAndSend( + node_id, StatusPacket, + std::move(dev::RLPStream(kInitialStatusPacketItemsCount) + << kConf.genesis.chain_id << dag_max_level << kGenesisHash << pbft_chain_size + << pbft_syncing_state_->isPbftSyncing() << pbft_round << TARAXA_MAJOR_VERSION << TARAXA_MINOR_VERSION + << TARAXA_PATCH_VERSION << dag_mgr_->isLightNode() << dag_mgr_->getLightNodeHistory())); + } else { + success = sealAndSend( + node_id, StatusPacket, + std::move(dev::RLPStream(kStandardStatusPacketItemsCount) + << dag_max_level << pbft_chain_size << pbft_syncing_state_->isDeepPbftSyncing() << pbft_round)); } return success; diff --git a/libraries/core_libs/node/include/node/node.hpp b/libraries/core_libs/node/include/node/node.hpp index 665c2a6b63..d90b52f9fe 100644 --- a/libraries/core_libs/node/include/node/node.hpp +++ b/libraries/core_libs/node/include/node/node.hpp @@ -33,6 +33,7 @@ namespace taraxa { namespace metrics { class MetricsService; } + class Network; class DagBlockProposer; class DagManager; @@ -45,15 +46,46 @@ struct NetworkConfig; class KeyManager; class FullNode : public std::enable_shared_from_this { - using shared_ptr_t = std::shared_ptr; - using vrf_pk_t = vrf_wrapper::vrf_pk_t; - using vrf_sk_t = vrf_wrapper::vrf_sk_t; - using vrf_proof_t = vrf_wrapper::vrf_proof_t; - using jsonrpc_server_t = ModularServer; + public: + explicit FullNode(FullNodeConfig const &conf); + ~FullNode(); + + FullNode(const FullNode &) = delete; + FullNode(FullNode &&) = delete; + FullNode &operator=(const FullNode &) = delete; + FullNode &operator=(FullNode &&) = delete; + + void start(); + bool isStarted() const { return started_; } + auto const &getConfig() const { return conf_; } + auto const &getNetwork() const { return network_; } + auto const &getTransactionManager() const { return trx_mgr_; } + auto const &getDagManager() const { return dag_mgr_; } + auto const &getDB() const { return db_; } + auto const &getPbftManager() const { return pbft_mgr_; } + auto const &getVoteManager() const { return vote_mgr_; } + auto const &getPbftChain() const { return pbft_chain_; } + auto const &getFinalChain() const { return final_chain_; } + // used only in tests + auto &getDagBlockProposer() { return dag_block_proposer_; } + auto const &getGasPricer() const { return gas_pricer_; } + + auto const &getAddress() const { return kp_.address(); } + auto const &getSecretKey() const { return kp_.secret(); } + auto const &getVrfSecretKey() const { return conf_.vrf_secret; } + + // For Debug + uint64_t getProposedBlocksCount() const; + + void rebuildDb(); + + private: + using JsonRpcServer = ModularServer; // should be destroyed after all components, since they may depend on it through unsafe pointers std::unique_ptr rpc_thread_pool_; std::unique_ptr graphql_thread_pool_; + // In cae we will you config for this TP, it needs to be unique_ptr !!! util::ThreadPool subscription_pool_; @@ -62,6 +94,7 @@ class FullNode : public std::enable_shared_from_this { FullNodeConfig conf_; // Ethereum key pair dev::KeyPair kp_; + // components std::shared_ptr db_; std::shared_ptr old_db_; @@ -79,7 +112,7 @@ class FullNode : public std::enable_shared_from_this { std::shared_ptr graphql_http_; std::shared_ptr jsonrpc_ws_; std::shared_ptr graphql_ws_; - std::unique_ptr jsonrpc_api_; + std::unique_ptr jsonrpc_api_; std::unique_ptr metrics_; // logging @@ -95,41 +128,6 @@ class FullNode : public std::enable_shared_from_this { * So we don't need to pass metrics classes instances in other classes. */ void setupMetricsUpdaters(); - - public: - explicit FullNode(FullNodeConfig const &conf); - ~FullNode(); - - FullNode(const FullNode &) = delete; - FullNode(FullNode &&) = delete; - FullNode &operator=(const FullNode &) = delete; - FullNode &operator=(FullNode &&) = delete; - - void start(); - bool isStarted() const { return started_; } - shared_ptr_t getShared() { return shared_from_this(); } - auto const &getConfig() const { return conf_; } - auto const &getNetwork() const { return network_; } - auto const &getTransactionManager() const { return trx_mgr_; } - auto const &getDagManager() const { return dag_mgr_; } - auto const &getDB() const { return db_; } - auto const &getPbftManager() const { return pbft_mgr_; } - auto const &getVoteManager() const { return vote_mgr_; } - auto const &getPbftChain() const { return pbft_chain_; } - auto const &getFinalChain() const { return final_chain_; } - // used only in tests - auto &getDagBlockProposer() { return dag_block_proposer_; } - auto const &getGasPricer() const { return gas_pricer_; } - - auto const &getAddress() const { return kp_.address(); } - auto const &getPublicKey() const { return kp_.pub(); } - auto const &getSecretKey() const { return kp_.secret(); } - auto const &getVrfSecretKey() const { return conf_.vrf_secret; } - - // For Debug - uint64_t getProposedBlocksCount() const; - - void rebuildDb(); }; } // namespace taraxa diff --git a/libraries/core_libs/node/src/node.cpp b/libraries/core_libs/node/src/node.cpp index 70648cec9c..a17affff53 100644 --- a/libraries/core_libs/node/src/node.cpp +++ b/libraries/core_libs/node/src/node.cpp @@ -211,7 +211,7 @@ void FullNode::start() { debug_json_rpc = std::make_shared(shared_from_this(), conf_.genesis.dag.gas_limit); } - jsonrpc_api_ = std::make_unique( + jsonrpc_api_ = std::make_unique( std::make_shared(shared_from_this()), // TODO Because this object refers to FullNode, the // lifecycle/dependency management is more complicated std::make_shared(shared_from_this()), // TODO Because this object refers to FullNode, the diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index d15c0653e7..b5deb5c8f0 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -97,6 +97,10 @@ void DbStorage::removeOldLogFiles() const { } void DbStorage::removeFilesWithPattern(const std::string& directory, const std::regex& pattern) const { + if (!std::filesystem::exists(directory)) { + return; + } + try { for (const auto& entry : std::filesystem::directory_iterator(directory)) { const std::string& filename = entry.path().filename().string(); diff --git a/tests/network_test.cpp b/tests/network_test.cpp index f3bd226565..fcb4524025 100644 --- a/tests/network_test.cpp +++ b/tests/network_test.cpp @@ -35,7 +35,43 @@ auto g_secret = Lazy([] { }); auto g_signed_trx_samples = Lazy([] { return samples::createSignedTrxSamples(0, NUM_TRX, g_secret); }); -struct NetworkTest : NodesTest {}; +struct NetworkTest : public NodesTest {}; + +// Test verifies saving network to a file and restoring it from a file +// is successful. Once restored from the file it is able to reestablish +// connections even with boot nodes down +TEST_F(NetworkTest, save_network) { + auto key2 = dev::KeyPair::create(); + auto key3 = dev::KeyPair::create(); + h256 genesis_hash; + + auto node_cfgs = make_node_cfgs(3, 0, 20); + std::filesystem::remove_all(node_cfgs[0].net_file_path()); + std::filesystem::remove_all(node_cfgs[1].net_file_path()); + std::filesystem::remove_all(node_cfgs[2].net_file_path()); + + { + auto nodes = launch_nodes(node_cfgs); + const auto& node1 = nodes[0]; + const auto& node2 = nodes[1]; + const auto& node3 = nodes[2]; + + EXPECT_HAPPENS({120s, 100ms}, [&](auto& ctx) { + WAIT_EXPECT_EQ(ctx, node1->getNetwork()->getPeerCount(), 2) + WAIT_EXPECT_EQ(ctx, node2->getNetwork()->getPeerCount(), 2) + WAIT_EXPECT_EQ(ctx, node3->getNetwork()->getPeerCount(), 2) + }); + } + + auto nodes = launch_nodes({node_cfgs[0], node_cfgs[1]}); + const auto& node1 = nodes[0]; + const auto& node2 = nodes[1]; + + EXPECT_HAPPENS({120s, 100ms}, [&](auto& ctx) { + WAIT_EXPECT_EQ(ctx, node1->getNetwork()->getPeerCount(), 1) + WAIT_EXPECT_EQ(ctx, node2->getNetwork()->getPeerCount(), 1) + }); +} // Test creates two Network setup and verifies sending blocks between is successful TEST_F(NetworkTest, transfer_lot_of_blocks) { @@ -314,7 +350,7 @@ TEST_F(NetworkTest, sync_large_pbft_block) { } // Test creates two Network setup and verifies sending transaction -// between is successfull +// between is successful TEST_F(NetworkTest, transfer_transaction) { auto node_cfgs = make_node_cfgs(2, 0, 20); auto nodes = launch_nodes(node_cfgs); @@ -328,13 +364,6 @@ TEST_F(NetworkTest, transfer_transaction) { const auto nw1 = node1->getNetwork(); const auto nw2 = node2->getNetwork(); - EXPECT_HAPPENS({60s, 100ms}, [&](auto& ctx) { - nw1->setPendingPeersToReady(); - nw2->setPendingPeersToReady(); - WAIT_EXPECT_EQ(ctx, nw1->getPeerCount(), 1) - WAIT_EXPECT_EQ(ctx, nw2->getPeerCount(), 1) - }); - auto nw1_nodeid = nw1->getNodeId(); auto nw2_nodeid = nw2->getNodeId(); @@ -353,47 +382,6 @@ TEST_F(NetworkTest, transfer_transaction) { [&](auto& ctx) { WAIT_EXPECT_TRUE(ctx, tx_mgr1->getTransaction(g_signed_trx_samples[0]->getHash())) }); } -// Test verifies saving network to a file and restoring it from a file -// is successful. Once restored from the file it is able to reestablish -// connections even with boot nodes down -TEST_F(NetworkTest, save_network) { - std::filesystem::remove_all("/tmp/nw2"); - std::filesystem::remove_all("/tmp/nw3"); - auto key2 = dev::KeyPair::create(); - auto key3 = dev::KeyPair::create(); - h256 genesis_hash; - { - std::shared_ptr nw1 = std::make_shared(node_cfgs[0], genesis_hash); - std::shared_ptr nw2 = std::make_shared(node_cfgs[1], genesis_hash, "/tmp/nw2", key2); - std::shared_ptr nw3 = std::make_shared(node_cfgs[2], genesis_hash, "/tmp/nw3", key3); - - nw1->start(); - nw2->start(); - nw3->start(); - - EXPECT_HAPPENS({120s, 100ms}, [&](auto& ctx) { - nw1->setPendingPeersToReady(); - nw2->setPendingPeersToReady(); - nw3->setPendingPeersToReady(); - WAIT_EXPECT_EQ(ctx, nw1->getPeerCount(), 2) - WAIT_EXPECT_EQ(ctx, nw2->getPeerCount(), 2) - WAIT_EXPECT_EQ(ctx, nw3->getPeerCount(), 2) - }); - } - - std::shared_ptr nw2 = std::make_shared(node_cfgs[1], genesis_hash, "/tmp/nw2", key2); - std::shared_ptr nw3 = std::make_shared(node_cfgs[2], genesis_hash, "/tmp/nw3", key3); - nw2->start(); - nw3->start(); - - EXPECT_HAPPENS({120s, 100ms}, [&](auto& ctx) { - nw2->setPendingPeersToReady(); - nw3->setPendingPeersToReady(); - WAIT_EXPECT_EQ(ctx, nw2->getPeerCount(), 1) - WAIT_EXPECT_EQ(ctx, nw3->getPeerCount(), 1) - }); -} - // Test creates one node with testnet network ID and one node with main ID and verifies that connection fails TEST_F(NetworkTest, node_chain_id) { auto node_cfgs = make_node_cfgs(2); diff --git a/tests/p2p_test.cpp b/tests/p2p_test.cpp index bb50322b61..1f84dbdbfb 100644 --- a/tests/p2p_test.cpp +++ b/tests/p2p_test.cpp @@ -9,13 +9,11 @@ #include #include "common/static_init.hpp" -#include "config/config.hpp" #include "logger/logger.hpp" -#include "network/network.hpp" #include "network/tarcap/packets_handlers/latest/dag_block_packet_handler.hpp" #include "network/tarcap/packets_handlers/latest/transaction_packet_handler.hpp" #include "network/tarcap/shared_states/pbft_syncing_state.hpp" -#include "network/tarcap/taraxa_capability.hpp" +#include "network/tarcap/tarcap_version.hpp" #include "test_util/samples.hpp" #include "test_util/test_util.hpp" @@ -32,35 +30,37 @@ auto g_signed_trx_samples = Lazy([] { return samples::createSignedTrxSamples(0, struct P2PTest : NodesTest {}; -// TODO this needs to be removed and called from tracap->setPendingPeersToReady() directly -void setPendingPeersToReady(std::shared_ptr taraxa_capability) { - const auto &peers_state = taraxa_capability->getPeersState(); - - auto peerIds = peers_state->getAllPendingPeersIDs(); - for (const auto &peerId : peerIds) { - auto peer = peers_state->getPendingPeer(peerId); - if (peer) { - peers_state->setPeerAsReadyToSendMessages(peerId, peer); +class TestTaraxaCapability final : public dev::p2p::CapabilityFace { + public: + TestTaraxaCapability(taraxa::network::tarcap::TarcapVersion version) : version_(version) {} + + std::string name() const override { return ""; } + taraxa::network::tarcap::TarcapVersion version() const override { return version_; } + unsigned messageCount() const override { return 0; } + void onConnect(std::weak_ptr, u256 const &) override {} + void onDisconnect(dev::p2p::NodeID const &) override {} + void interpretCapabilityPacket(std::weak_ptr, unsigned, dev::RLP const &) override {} + std::string packetTypeToString(unsigned) const override { return ""; } + + private: + taraxa::network::tarcap::TarcapVersion version_{1}; +}; + +std::shared_ptr makeTestNode(unsigned short listenPort, + std::vector tarcap_versions, + std::filesystem::path state_file_path) { + auto makeTestTarcaps = [tarcap_versions](std::weak_ptr) { + Host::CapabilityList tarcaps; + for (const auto &version : tarcap_versions) { + tarcaps.emplace_back(std::make_shared(version)); } - } -} - -std::shared_ptr makeTarcap(std::weak_ptr host, - const dev::KeyPair &key, - const FullNodeConfig &conf, - const h256 &genesis_hash, unsigned version) { - auto thread_pool = std::make_shared(conf.network.packets_processing_threads); - auto packets_stats = std::make_shared( - conf.network.ddos_protection.packets_stats_time_period_ms, Address{}); - auto syncing_state = std::make_shared(conf.network.deep_syncing_threshold); - auto tarcap = std::make_shared(version, conf, genesis_hash, host, key, thread_pool, - packets_stats, syncing_state, nullptr, nullptr, - nullptr, nullptr, nullptr, nullptr); - - thread_pool->startProcessing(); + return tarcaps; + }; - return tarcap; + return Host::make("TaraxaNode", makeTestTarcaps, dev::KeyPair::create(), + dev::p2p::NetworkConfig("127.0.0.1", listenPort, false, true), TaraxaNetworkConfig{}, + state_file_path); } /* @@ -98,77 +98,61 @@ TEST_F(P2PTest, p2p_discovery) { } TEST_F(P2PTest, multiple_capabilities) { - auto node_cfgs = make_node_cfgs(3); - h256 genesis_hash; - NetworkConfig network_conf; - network_conf.transaction_interval_ms = 1000; - auto cleanup = []() { + // Create boot node + auto secret = dev::Secret("3800b2875669d9b2053c1aff9224ecfdc411423aac5b5a73d7a45ced1c3b9dcd", + dev::Secret::ConstructFromStringType::FromHex); + auto key = dev::KeyPair(secret); + dev::p2p::NetworkConfig net_conf("127.0.0.1", 20001, false, true); + TaraxaNetworkConfig taraxa_net_conf; + taraxa_net_conf.is_boot_node = true; + auto boot_node = Host::make( + "TaraxaNode", [](auto /*host*/) { return Host::CapabilityList{}; }, key, net_conf, taraxa_net_conf); + const auto &boot_node_key = boot_node->id(); + + util::ThreadPool boot_node_tp; + boot_node_tp.post_loop({}, [=] { boot_node->do_work(); }); + + // Create 2 nodes(hosts) with specified tarcap versionS and wait for their connection being established + auto test_tarcaps = [boot_node_key](std::vector node1_tarcap_versions, + std::vector node2_tarcap_versions, + bool wait_for_connection = true) -> std::vector> { + std::filesystem::remove_all("/tmp/nw1"); std::filesystem::remove_all("/tmp/nw2"); - std::filesystem::remove_all("/tmp/nw3"); - }; - auto wait_for_connection = [](std::shared_ptr nw1, std::shared_ptr nw2) { - EXPECT_HAPPENS({60s, 100ms}, [&](auto &ctx) { - nw1->setPendingPeersToReady(); - nw2->setPendingPeersToReady(); - WAIT_EXPECT_EQ(ctx, nw1->getPeerCount(), 1) - WAIT_EXPECT_EQ(ctx, nw2->getPeerCount(), 1) - }); + + util::ThreadPool tp; + + const auto node1 = makeTestNode(20002, node1_tarcap_versions, "/tmp/nw1"); + std::cout << "node1->peer_count(): " << node1->peer_count() << std::endl; + node1->addNode(Node(boot_node_key, dev::p2p::NodeIPEndpoint(bi::address::from_string("127.0.0.1"), 20001, 20001))); + tp.post_loop({}, [=] { node1->do_work(); }); + + const auto node2 = makeTestNode(20003, node2_tarcap_versions, "/tmp/nw2"); + node2->addNode(Node(boot_node_key, dev::p2p::NodeIPEndpoint(bi::address::from_string("127.0.0.1"), 20001, 20001))); + tp.post_loop({}, [=] { node2->do_work(); }); + + if (wait_for_connection) { + wait({60s, 500ms}, [&](auto &ctx) { + WAIT_EXPECT_EQ(ctx, node1->peer_count(), 1 /* boot node + node2 */); + WAIT_EXPECT_EQ(ctx, node2->peer_count(), 1 /* boot node + node1 */); + }); + + return {nullptr, nullptr}; + } + + return {node1, node2}; }; - const auto kp1 = KeyPair::create(); - const auto kp2 = KeyPair::create(); - cleanup(); - { - auto nw1 = - std::make_shared(node_cfgs[0], genesis_hash, "/tmp/nw2", kp1, nullptr, nullptr, nullptr, - nullptr, nullptr, nullptr, std::vector{3}); - auto nw2 = - std::make_shared(node_cfgs[1], genesis_hash, "/tmp/nw3", kp2, nullptr, nullptr, nullptr, - nullptr, nullptr, nullptr, std::vector{3}); - - nw1->start(); - nw2->start(); - wait_for_connection(nw1, nw2); - } - cleanup(); - { - auto nw1 = std::make_shared(node_cfgs[0], genesis_hash, "/tmp/nw2", kp1, nullptr, nullptr, nullptr, - nullptr, nullptr, nullptr, - std::vector{1, 2, 3}); - auto nw2 = std::make_shared(node_cfgs[1], genesis_hash, "/tmp/nw3", kp2, nullptr, nullptr, nullptr, - nullptr, nullptr, nullptr, - std::vector{1, 2, 3}); - nw1->start(); - nw2->start(); - wait_for_connection(nw1, nw2); - } - cleanup(); - { - auto nw1 = std::make_shared(node_cfgs[0], genesis_hash, "/tmp/nw2", kp1, nullptr, nullptr, nullptr, - nullptr, nullptr, nullptr, - std::vector{1, 2, 3}); - auto nw2 = std::make_shared(node_cfgs[1], genesis_hash, "/tmp/nw3", kp2, nullptr, nullptr, nullptr, - nullptr, nullptr, nullptr, - std::vector{2, 3, 4}); - nw1->start(); - nw2->start(); - wait_for_connection(nw1, nw2); - } - cleanup(); - { - auto nw1 = std::make_shared(node_cfgs[0], genesis_hash, "/tmp/nw2", kp1, nullptr, nullptr, nullptr, - nullptr, nullptr, nullptr, - std::vector{1, 2, 3}); - auto nw2 = std::make_shared(node_cfgs[1], genesis_hash, "/tmp/nw3", kp2, nullptr, nullptr, nullptr, - nullptr, nullptr, nullptr, - std::vector{4, 5, 6}); - nw1->start(); - nw2->start(); + // At least 1 common tarcap version - connection should be established + { test_tarcaps({1}, {1}); } + { test_tarcaps({1, 2, 3}, {3, 4, 5}); } + // No common tarcapm version, connection should not be established + { + auto nodes = test_tarcaps({1, 2, 3}, {4, 5, 6}, false); // check that connection wasn't established std::this_thread::sleep_for(5s); - EXPECT_EQ(nw1->getPeerCount(), 0); - EXPECT_EQ(nw2->getPeerCount(), 0); + EXPECT_EQ(nodes[0]->peer_count(), 0); + EXPECT_EQ(nodes[1]->peer_count(), 0); } } From 7ccbc0a850b555296580c58d0e5c57c8aefd3335 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Wed, 12 Jul 2023 10:39:44 +0200 Subject: [PATCH 083/134] chore: correct error handling in debug API --- libraries/core_libs/network/rpc/Debug.cpp | 101 +++++++++------------- 1 file changed, 40 insertions(+), 61 deletions(-) diff --git a/libraries/core_libs/network/rpc/Debug.cpp b/libraries/core_libs/network/rpc/Debug.cpp index 66316be57f..d2a7f7fcc4 100644 --- a/libraries/core_libs/network/rpc/Debug.cpp +++ b/libraries/core_libs/network/rpc/Debug.cpp @@ -3,6 +3,8 @@ #include #include +#include + #include "common/jsoncpp.hpp" #include "final_chain/state_api_data.hpp" #include "network/rpc/eth/data.hpp" @@ -21,32 +23,23 @@ inline EthBlockNumber get_ctx_block_num(EthBlockNumber block_number) { Json::Value Debug::debug_traceTransaction(const std::string& transaction_hash) { Json::Value res; - try { - auto [trx, loc] = get_transaction_with_location(transaction_hash); - if (!trx || !loc) { - res["status"] = "Transaction not found"; - return res; - } - if (auto node = full_node_.lock()) { - return util::readJsonFromString( - node->getFinalChain()->trace({to_eth_trx(std::move(trx))}, get_ctx_block_num(loc->blk_n))); - } - } catch (std::exception& e) { - res["status"] = e.what(); + auto [trx, loc] = get_transaction_with_location(transaction_hash); + if (!trx || !loc) { + throw std::runtime_error("Transaction not found"); + } + if (auto node = full_node_.lock()) { + return util::readJsonFromString( + node->getFinalChain()->trace({to_eth_trx(std::move(trx))}, get_ctx_block_num(loc->blk_n))); } return res; } Json::Value Debug::debug_traceCall(const Json::Value& call_params, const std::string& blk_num) { Json::Value res; - try { - const auto block = parse_blk_num(blk_num); - auto trx = to_eth_trx(call_params, block); - if (auto node = full_node_.lock()) { - return util::readJsonFromString(node->getFinalChain()->trace({std::move(trx)}, block)); - } - } catch (std::exception& e) { - res["status"] = e.what(); + const auto block = parse_blk_num(blk_num); + auto trx = to_eth_trx(call_params, block); + if (auto node = full_node_.lock()) { + return util::readJsonFromString(node->getFinalChain()->trace({std::move(trx)}, block)); } return res; } @@ -54,60 +47,46 @@ Json::Value Debug::debug_traceCall(const Json::Value& call_params, const std::st Json::Value Debug::trace_call(const Json::Value& call_params, const Json::Value& trace_params, const std::string& blk_num) { Json::Value res; - try { - const auto block = parse_blk_num(blk_num); - auto params = parse_tracking_parms(trace_params); - if (auto node = full_node_.lock()) { - return util::readJsonFromString( - node->getFinalChain()->trace({to_eth_trx(call_params, block)}, block, std::move(params))); - } - } catch (std::exception& e) { - res["status"] = e.what(); + const auto block = parse_blk_num(blk_num); + auto params = parse_tracking_parms(trace_params); + if (auto node = full_node_.lock()) { + return util::readJsonFromString( + node->getFinalChain()->trace({to_eth_trx(call_params, block)}, block, std::move(params))); } return res; } Json::Value Debug::trace_replayTransaction(const std::string& transaction_hash, const Json::Value& trace_params) { Json::Value res; - try { - auto params = parse_tracking_parms(trace_params); - auto [trx, loc] = get_transaction_with_location(transaction_hash); - if (!trx || !loc) { - res["status"] = "Transaction not found"; - return res; - } - if (auto node = full_node_.lock()) { - return util::readJsonFromString( - node->getFinalChain()->trace({to_eth_trx(std::move(trx))}, get_ctx_block_num(loc->blk_n), std::move(params))); - } - } catch (std::exception& e) { - res["status"] = e.what(); + auto params = parse_tracking_parms(trace_params); + auto [trx, loc] = get_transaction_with_location(transaction_hash); + if (!trx || !loc) { + throw std::runtime_error("Transaction not found"); + } + if (auto node = full_node_.lock()) { + return util::readJsonFromString( + node->getFinalChain()->trace({to_eth_trx(std::move(trx))}, get_ctx_block_num(loc->blk_n), std::move(params))); } return res; } Json::Value Debug::trace_replayBlockTransactions(const std::string& block_num, const Json::Value& trace_params) { Json::Value res; - try { - const auto block = parse_blk_num(block_num); - auto params = parse_tracking_parms(trace_params); - if (auto node = full_node_.lock()) { - auto transactions = node->getDB()->getPeriodTransactions(block); - if (!transactions.has_value() || transactions->empty()) { - res["status"] = "Block has no transactions"; - return res; - } - // TODO[2495]: remove after a proper fox of transactions ordering in PeriodData - PbftManager::reorderTransactions(*transactions); - std::vector trxs; - trxs.reserve(transactions->size()); - std::transform(transactions->begin(), transactions->end(), std::back_inserter(trxs), - [this](auto t) { return to_eth_trx(std::move(t)); }); - return util::readJsonFromString( - node->getFinalChain()->trace(std::move(trxs), get_ctx_block_num(block), std::move(params))); + const auto block = parse_blk_num(block_num); + auto params = parse_tracking_parms(trace_params); + if (auto node = full_node_.lock()) { + auto transactions = node->getDB()->getPeriodTransactions(block); + if (!transactions.has_value() || transactions->empty()) { + return Json::Value(Json::arrayValue); } - } catch (std::exception& e) { - res["status"] = e.what(); + // TODO[2495]: remove after a proper fox of transactions ordering in PeriodData + PbftManager::reorderTransactions(*transactions); + std::vector trxs; + trxs.reserve(transactions->size()); + std::transform(transactions->begin(), transactions->end(), std::back_inserter(trxs), + [this](auto t) { return to_eth_trx(std::move(t)); }); + return util::readJsonFromString( + node->getFinalChain()->trace(std::move(trxs), get_ctx_block_num(block), std::move(params))); } return res; } From 5d009709457269f103416833cb950b834f2e3d6c Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Wed, 12 Jul 2023 10:40:05 +0200 Subject: [PATCH 084/134] chore: add vrf keys to default genesis --- .../cli/config_jsons/default/default_genesis.json | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/cli/include/cli/config_jsons/default/default_genesis.json b/libraries/cli/include/cli/config_jsons/default/default_genesis.json index 200d987afa..fbb17199c6 100644 --- a/libraries/cli/include/cli/config_jsons/default/default_genesis.json +++ b/libraries/cli/include/cli/config_jsons/default/default_genesis.json @@ -29,7 +29,8 @@ "description": "Taraxa validator 1", "delegations": { "0x7e4aa664f71de4e9d0b4a6473d796372639bdcde": "0x84595161401484a000000" - } + }, + "vrf_key": "05fe580fd2d461ee5f762a33bbe669403bb04a851f2e9ed8d2579a9c9b77c3ec" }, { "address": "0x56e0de6933d9d0453d0363caf42b136eb5854e4e", @@ -39,7 +40,8 @@ "description": "Taraxa validator 2", "delegations": { "0x7e4aa664f71de4e9d0b4a6473d796372639bdcde": "0x84595161401484a000000" - } + }, + "vrf_key": "70d34c86787e5f7bd0f266cad291cb521e23176fa37c6efc034858a1620ac69e" }, { "address": "0x71bdcbec7e3642782447b0fbf31eed068dfbdbb1", @@ -49,7 +51,8 @@ "description": "Taraxa validator 3", "delegations": { "0x7e4aa664f71de4e9d0b4a6473d796372639bdcde": "0x84595161401484a000000" - } + }, + "vrf_key": "f8d5c00ce9fa3058341e051b36a1e6ccf69df81fb865568b2bf1507d085691e2" } ] }, From 72d87847ea79ef2a0afa46f46513964eb5b8dd0b Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Wed, 12 Jul 2023 10:47:18 +0200 Subject: [PATCH 085/134] chore: modified errors for all APIs --- libraries/core_libs/network/rpc/Taraxa.cpp | 13 +- libraries/core_libs/network/rpc/Test.cpp | 203 +++++++++------------ 2 files changed, 92 insertions(+), 124 deletions(-) diff --git a/libraries/core_libs/network/rpc/Taraxa.cpp b/libraries/core_libs/network/rpc/Taraxa.cpp index 5853befab2..59bb124b5e 100644 --- a/libraries/core_libs/network/rpc/Taraxa.cpp +++ b/libraries/core_libs/network/rpc/Taraxa.cpp @@ -144,15 +144,12 @@ Json::Value Taraxa::taraxa_getConfig() { return enc_json(tryGetNode()->getConfig Json::Value Taraxa::taraxa_getChainStats() { Json::Value res; - try { - if (auto node = full_node_.lock()) { - res["pbft_period"] = Json::UInt64(node->getPbftChain()->getPbftChainSize()); - res["dag_blocks_executed"] = Json::UInt64(node->getDB()->getNumBlockExecuted()); - res["transactions_executed"] = Json::UInt64(node->getDB()->getNumTransactionExecuted()); - } - } catch (std::exception& e) { - res["status"] = e.what(); + if (auto node = full_node_.lock()) { + res["pbft_period"] = Json::UInt64(node->getPbftChain()->getPbftChainSize()); + res["dag_blocks_executed"] = Json::UInt64(node->getDB()->getNumBlockExecuted()); + res["transactions_executed"] = Json::UInt64(node->getDB()->getNumTransactionExecuted()); } + return res; } diff --git a/libraries/core_libs/network/rpc/Test.cpp b/libraries/core_libs/network/rpc/Test.cpp index 0448abd06a..6f69dc1445 100644 --- a/libraries/core_libs/network/rpc/Test.cpp +++ b/libraries/core_libs/network/rpc/Test.cpp @@ -21,141 +21,117 @@ namespace taraxa::net { Json::Value Test::get_sortition_change(const Json::Value ¶m1) { Json::Value res; - try { - if (auto node = full_node_.lock()) { - uint64_t period = param1["period"].asUInt64(); - auto params_change = node->getDB()->getParamsChangeForPeriod(period); - res["interval_efficiency"] = params_change->interval_efficiency; - res["period"] = params_change->period; - res["threshold_upper"] = params_change->vrf_params.threshold_upper; - res["kThresholdUpperMinValue"] = params_change->vrf_params.kThresholdUpperMinValue; - } - } catch (std::exception &e) { - res["status"] = e.what(); + if (auto node = full_node_.lock()) { + uint64_t period = param1["period"].asUInt64(); + auto params_change = node->getDB()->getParamsChangeForPeriod(period); + res["interval_efficiency"] = params_change->interval_efficiency; + res["period"] = params_change->period; + res["threshold_upper"] = params_change->vrf_params.threshold_upper; + res["kThresholdUpperMinValue"] = params_change->vrf_params.kThresholdUpperMinValue; } return res; } Json::Value Test::send_coin_transaction(const Json::Value ¶m1) { Json::Value res; - try { - if (auto node = full_node_.lock()) { - secret_t sk = secret_t(param1["secret"].asString()); - uint64_t nonce = 0; - if (!param1["nonce"]) { - auto acc = node->getFinalChain()->get_account(toAddress(sk)); - nonce = acc->nonce.convert_to() + 1; - } else { - nonce = dev::jsToInt(param1["nonce"].asString()); - } - val_t value = val_t(param1["value"].asString()); - val_t gas_price = val_t(param1["gas_price"].asString()); - auto gas = dev::jsToInt(param1["gas"].asString()); - addr_t receiver = addr_t(param1["receiver"].asString()); - auto trx = std::make_shared(nonce, value, gas_price, gas, bytes(), sk, receiver, kChainId); - if (auto [ok, err_msg] = node->getTransactionManager()->insertTransaction(trx); !ok) { - res["status"] = err_msg; - } else { - res = toHex(trx->rlp()); - } + if (auto node = full_node_.lock()) { + secret_t sk = secret_t(param1["secret"].asString()); + uint64_t nonce = 0; + if (!param1["nonce"]) { + auto acc = node->getFinalChain()->get_account(toAddress(sk)); + nonce = acc->nonce.convert_to() + 1; + } else { + nonce = dev::jsToInt(param1["nonce"].asString()); + } + val_t value = val_t(param1["value"].asString()); + val_t gas_price = val_t(param1["gas_price"].asString()); + auto gas = dev::jsToInt(param1["gas"].asString()); + addr_t receiver = addr_t(param1["receiver"].asString()); + auto trx = std::make_shared(nonce, value, gas_price, gas, bytes(), sk, receiver, kChainId); + if (auto [ok, err_msg] = node->getTransactionManager()->insertTransaction(trx); !ok) { + res["status"] = err_msg; + } else { + res = toHex(trx->rlp()); } - } catch (std::exception &e) { - res["status"] = e.what(); } return res; } Json::Value Test::send_coin_transactions(const Json::Value ¶m1) { Json::Value res; - try { - uint32_t inserted = 0; - if (auto node = full_node_.lock()) { - secret_t sk = secret_t(param1["secret"].asString()); - auto nonce = param1["nonce"].asUInt64(); - val_t value = val_t(param1["value"].asString()); - val_t gas_price = val_t(param1["gas_price"].asString()); - auto gas = dev::jsToInt(param1["gas"].asString()); - auto transactions_count = param1["transaction_count"].asUInt64(); - std::vector receivers; - std::transform(param1["receiver"].begin(), param1["receiver"].end(), std::back_inserter(receivers), - [](const auto rec) { return addr_t(rec.asString()); }); - for (uint32_t i = 0; i < transactions_count; i++) { - auto trx = std::make_shared(nonce, value, gas_price, gas, bytes(), sk, - receivers[i % receivers.size()], kChainId); - nonce++; - if (auto [ok, err_msg] = node->getTransactionManager()->insertTransaction(trx); !ok) { - res["err"] = err_msg; - break; - } else { - inserted++; - } + uint32_t inserted = 0; + if (auto node = full_node_.lock()) { + secret_t sk = secret_t(param1["secret"].asString()); + auto nonce = param1["nonce"].asUInt64(); + val_t value = val_t(param1["value"].asString()); + val_t gas_price = val_t(param1["gas_price"].asString()); + auto gas = dev::jsToInt(param1["gas"].asString()); + auto transactions_count = param1["transaction_count"].asUInt64(); + std::vector receivers; + std::transform(param1["receiver"].begin(), param1["receiver"].end(), std::back_inserter(receivers), + [](const auto rec) { return addr_t(rec.asString()); }); + for (uint32_t i = 0; i < transactions_count; i++) { + auto trx = std::make_shared(nonce, value, gas_price, gas, bytes(), sk, + receivers[i % receivers.size()], kChainId); + nonce++; + if (auto [ok, err_msg] = node->getTransactionManager()->insertTransaction(trx); !ok) { + res["err"] = err_msg; + break; + } else { + inserted++; } } - res["status"] = Json::UInt64(inserted); - } catch (std::exception &e) { - res["status"] = e.what(); } + res["status"] = Json::UInt64(inserted); return res; } Json::Value Test::get_account_address() { Json::Value res; - try { - if (auto node = full_node_.lock()) { - addr_t addr = node->getAddress(); - res["value"] = addr.toString(); - } - } catch (std::exception &e) { - res["status"] = e.what(); + if (auto node = full_node_.lock()) { + addr_t addr = node->getAddress(); + res["value"] = addr.toString(); } return res; } Json::Value Test::get_peer_count() { Json::Value res; - try { - if (auto node = full_node_.lock()) { - auto peer = node->getNetwork()->getPeerCount(); - res["value"] = Json::UInt64(peer); - } - } catch (std::exception &e) { - res["status"] = e.what(); + if (auto node = full_node_.lock()) { + auto peer = node->getNetwork()->getPeerCount(); + res["value"] = Json::UInt64(peer); } return res; } Json::Value Test::get_node_status() { Json::Value res; - try { - if (auto node = full_node_.lock()) { - const auto chain_size = node->getPbftChain()->getPbftChainSize(); - const auto dpos_total_votes_opt = node->getPbftManager()->getCurrentDposTotalVotesCount(); - const auto dpos_node_votes_opt = node->getPbftManager()->getCurrentNodeVotesCount(); - const auto two_t_plus_one_opt = node->getVoteManager()->getPbftTwoTPlusOne(chain_size, PbftVoteTypes::cert_vote); - - res["synced"] = !node->getNetwork()->pbft_syncing(); - res["syncing_seconds"] = Json::UInt64(node->getNetwork()->syncTimeSeconds()); - res["peer_count"] = Json::UInt64(node->getNetwork()->getPeerCount()); - res["node_count"] = Json::UInt64(node->getNetwork()->getNodeCount()); - res["blk_executed"] = Json::UInt64(node->getDB()->getNumBlockExecuted()); - res["blk_count"] = Json::UInt64(node->getDB()->getDagBlocksCount()); - res["trx_executed"] = Json::UInt64(node->getDB()->getNumTransactionExecuted()); - res["trx_count"] = Json::UInt64(node->getTransactionManager()->getTransactionCount()); - res["dag_level"] = Json::UInt64(node->getDagManager()->getMaxLevel()); - res["pbft_size"] = Json::UInt64(chain_size); - res["pbft_sync_period"] = Json::UInt64(node->getPbftManager()->pbftSyncingPeriod()); - res["pbft_round"] = Json::UInt64(node->getPbftManager()->getPbftRound()); - res["dpos_total_votes"] = Json::UInt64(dpos_total_votes_opt.has_value() ? *dpos_total_votes_opt : 0); - res["dpos_node_votes"] = Json::UInt64(dpos_node_votes_opt ? *dpos_node_votes_opt : 0); - res["dpos_quorum"] = Json::UInt64(two_t_plus_one_opt ? *two_t_plus_one_opt : 0); - res["pbft_sync_queue_size"] = Json::UInt64(node->getPbftManager()->periodDataQueueSize()); - res["trx_pool_size"] = Json::UInt64(node->getTransactionManager()->getTransactionPoolSize()); - res["trx_nonfinalized_size"] = Json::UInt64(node->getTransactionManager()->getNonfinalizedTrxSize()); - res["network"] = node->getNetwork()->getStatus(); - } - } catch (std::exception &e) { - res["status"] = e.what(); + if (auto node = full_node_.lock()) { + const auto chain_size = node->getPbftChain()->getPbftChainSize(); + const auto dpos_total_votes_opt = node->getPbftManager()->getCurrentDposTotalVotesCount(); + const auto dpos_node_votes_opt = node->getPbftManager()->getCurrentNodeVotesCount(); + const auto two_t_plus_one_opt = node->getVoteManager()->getPbftTwoTPlusOne(chain_size, PbftVoteTypes::cert_vote); + + res["synced"] = !node->getNetwork()->pbft_syncing(); + res["syncing_seconds"] = Json::UInt64(node->getNetwork()->syncTimeSeconds()); + res["peer_count"] = Json::UInt64(node->getNetwork()->getPeerCount()); + res["node_count"] = Json::UInt64(node->getNetwork()->getNodeCount()); + res["blk_executed"] = Json::UInt64(node->getDB()->getNumBlockExecuted()); + res["blk_count"] = Json::UInt64(node->getDB()->getDagBlocksCount()); + res["trx_executed"] = Json::UInt64(node->getDB()->getNumTransactionExecuted()); + res["trx_count"] = Json::UInt64(node->getTransactionManager()->getTransactionCount()); + res["dag_level"] = Json::UInt64(node->getDagManager()->getMaxLevel()); + res["pbft_size"] = Json::UInt64(chain_size); + res["pbft_sync_period"] = Json::UInt64(node->getPbftManager()->pbftSyncingPeriod()); + res["pbft_round"] = Json::UInt64(node->getPbftManager()->getPbftRound()); + res["dpos_total_votes"] = Json::UInt64(dpos_total_votes_opt.has_value() ? *dpos_total_votes_opt : 0); + res["dpos_node_votes"] = Json::UInt64(dpos_node_votes_opt ? *dpos_node_votes_opt : 0); + res["dpos_quorum"] = Json::UInt64(two_t_plus_one_opt ? *two_t_plus_one_opt : 0); + res["pbft_sync_queue_size"] = Json::UInt64(node->getPbftManager()->periodDataQueueSize()); + res["trx_pool_size"] = Json::UInt64(node->getTransactionManager()->getTransactionPoolSize()); + res["trx_nonfinalized_size"] = Json::UInt64(node->getTransactionManager()->getNonfinalizedTrxSize()); + res["network"] = node->getNetwork()->getStatus(); } return res; } @@ -163,23 +139,18 @@ Json::Value Test::get_node_status() { Json::Value Test::get_all_nodes() { Json::Value res; - try { - if (auto full_node = full_node_.lock()) { - auto nodes = full_node->getNetwork()->getAllNodes(); - res["nodes_count"] = Json::UInt64(nodes.size()); - res["nodes"] = Json::Value(Json::arrayValue); - for (auto const &n : nodes) { - Json::Value node; - node["node_id"] = n.id().toString(); - node["address"] = n.endpoint().address().to_string(); - node["listen_port"] = Json::UInt64(n.endpoint().tcpPort()); - res["nodes"].append(node); - } + if (auto full_node = full_node_.lock()) { + auto nodes = full_node->getNetwork()->getAllNodes(); + res["nodes_count"] = Json::UInt64(nodes.size()); + res["nodes"] = Json::Value(Json::arrayValue); + for (auto const &n : nodes) { + Json::Value node; + node["node_id"] = n.id().toString(); + node["address"] = n.endpoint().address().to_string(); + node["listen_port"] = Json::UInt64(n.endpoint().tcpPort()); + res["nodes"].append(node); } - } catch (std::exception &e) { - res["status"] = e.what(); } - return res; } From 25af4cb52cbefef3b59fcd0ab25485402d914d86 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Fri, 7 Jul 2023 14:34:56 +0200 Subject: [PATCH 086/134] chore:light node optimization --- .../consensus/include/dag/dag_manager.hpp | 2 +- .../consensus/src/dag/dag_manager.cpp | 13 ++++++---- .../storage/include/storage/storage.hpp | 2 +- libraries/core_libs/storage/src/storage.cpp | 26 +++++++++---------- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/libraries/core_libs/consensus/include/dag/dag_manager.hpp b/libraries/core_libs/consensus/include/dag/dag_manager.hpp index 91a0b795a1..90b73acaf3 100644 --- a/libraries/core_libs/consensus/include/dag/dag_manager.hpp +++ b/libraries/core_libs/consensus/include/dag/dag_manager.hpp @@ -239,7 +239,7 @@ class DagManager : public std::enable_shared_from_this { bool validateBlockNotExpired(const std::shared_ptr &dag_block, std::unordered_map> &expired_dag_blocks_to_remove); void handleExpiredDagBlocksTransactions(const std::vector &transactions_from_expired_dag_blocks) const; - void clearLightNodeHistory(bool force = false); + void clearLightNodeHistory(bool initial = false); std::pair> getFrontier() const; // return pivot and tips void updateFrontier(); diff --git a/libraries/core_libs/consensus/src/dag/dag_manager.cpp b/libraries/core_libs/consensus/src/dag/dag_manager.cpp index 55344bf952..ed57baa4c2 100644 --- a/libraries/core_libs/consensus/src/dag/dag_manager.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_manager.cpp @@ -282,10 +282,13 @@ std::vector DagManager::getDagBlockOrder(blk_hash_t const &anchor, P return blk_orders; } -void DagManager::clearLightNodeHistory(bool force) { - // Actual history size will be between 100% and 110% of light_node_history_ to avoid deleting on every period - if (((period_ % (std::max(light_node_history_ / 10, (uint64_t)1)) == 0) || force) && period_ > light_node_history_ && - dag_expiry_level_ > max_levels_per_period_ + 1) { +void DagManager::clearLightNodeHistory(bool initial) { + // Delete once size 1% over the light_node_history_, by default light node history is a week of data so this condition + // will pass once an hour + uint64_t clear_interval = std::max(light_node_history_ / 100, (uint64_t)1); + bool dag_expiry_level_condition = dag_expiry_level_ > max_levels_per_period_ + 1; + bool period_over_history_condition = period_ > light_node_history_; + if (((period_ % clear_interval == 0) || initial) && period_over_history_condition && dag_expiry_level_condition) { // This will happen at most once a day so log a silent log LOG(log_si_) << "Clear light node history"; const auto proposal_period = db_->getProposalPeriodForDagLevel(dag_expiry_level_ - max_levels_per_period_ - 1); @@ -303,7 +306,7 @@ void DagManager::clearLightNodeHistory(bool force) { if (dag_expiry_level_ > max_levels_per_period_) { dag_level_to_keep = dag_expiry_level_ - max_levels_per_period_; } - db_->clearPeriodDataHistory(end, dag_level_to_keep); + db_->clearPeriodDataHistory(end, dag_level_to_keep, initial); LOG(log_si_) << "Clear light node history completed"; } } diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index bf5bc15385..dd8e656488 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -199,7 +199,7 @@ class DbStorage : public std::enable_shared_from_this { // Period data void savePeriodData(const PeriodData& period_data, Batch& write_batch); - void clearPeriodDataHistory(PbftPeriod period, uint64_t dag_level_to_keep); + void clearPeriodDataHistory(PbftPeriod period, uint64_t dag_level_to_keep, bool initial); dev::bytes getPeriodDataRaw(PbftPeriod period) const; std::optional getPbftBlock(PbftPeriod period) const; std::vector> getPeriodCertVotes(PbftPeriod period) const; diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index b5deb5c8f0..76d66dca30 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -563,8 +563,7 @@ std::optional DbStorage::getParamsChangeForPeriod(PbftPer return SortitionParamsChange::from_rlp(dev::RLP(it->value().ToString())); } -void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level_to_keep) { - // This is expensive operation but it should not be run more than once a day +void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level_to_keep, bool initial) { auto it = std::unique_ptr(db_->NewIterator(read_options_, handle(Columns::period_data))); // Find the first non-deleted period it->SeekToFirst(); @@ -577,6 +576,7 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level const uint32_t max_batch_delete = 10000; auto start_slice = toSlice(start_period); auto end_slice = toSlice(end_period); + for (auto period = start_period; period < end_period; period++) { // Find transactions included in the old blocks and delete data related to these transactions to free disk // space @@ -585,7 +585,6 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level for (auto hash : trx_hashes) { remove(write_batch, Columns::final_chain_receipt_by_trx_hash, hash); - remove(write_batch, Columns::trx_period, hash); } remove(write_batch, Columns::final_chain_transaction_hashes_by_blk_number, EthBlockNumber(period)); if ((period - start_period + 1) % max_batch_delete == 0) { @@ -593,17 +592,16 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level write_batch = createWriteBatch(); } } - commitWriteBatch(write_batch); - db_->DeleteRange(write_options_, handle(Columns::period_data), start_slice, end_slice); - // Deletion alone does not guarantee that the disk space is freed, these CompactRange methods actually compact - // the data in the database and free disk space - db_->CompactRange({}, handle(Columns::period_data), &start_slice, &end_slice); - db_->CompactRange({}, handle(Columns::final_chain_receipt_by_trx_hash), nullptr, nullptr); - db_->CompactRange({}, handle(Columns::final_chain_transaction_hashes_by_blk_number), nullptr, nullptr); - db_->CompactRange({}, handle(Columns::trx_period), nullptr, nullptr); + if (initial) { + // Deletion alone does not guarantee that the disk space is freed, CompactRange actually compacts + // the data in the database and free disk space + db_->CompactRange({}, handle(Columns::period_data), &start_slice, &end_slice); + db_->CompactRange({}, handle(Columns::final_chain_receipt_by_trx_hash), nullptr, nullptr); + db_->CompactRange({}, handle(Columns::final_chain_transaction_hashes_by_blk_number), nullptr, nullptr); + } } } @@ -632,8 +630,10 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level commitWriteBatch(write_batch); db_->DeleteRange(write_options_, handle(Columns::dag_blocks_level), start_slice, end_slice); - db_->CompactRange({}, handle(Columns::dag_block_period), nullptr, nullptr); - db_->CompactRange({}, handle(Columns::dag_blocks_level), nullptr, nullptr); + if (initial) { + db_->CompactRange({}, handle(Columns::dag_block_period), nullptr, nullptr); + db_->CompactRange({}, handle(Columns::dag_blocks_level), nullptr, nullptr); + } } } } From a59cd590e9906aadc5ac89496b603dfcd9bdde4d Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Wed, 12 Jul 2023 13:02:58 +0200 Subject: [PATCH 087/134] perf: remove final_chain_transaction_hashes_by_blk_number column and get transactions hashes from period data --- .../consensus/src/final_chain/final_chain.cpp | 14 ++++--- libraries/core_libs/network/rpc/eth/Eth.cpp | 6 +-- .../storage/migration/transaction_hashes.hpp | 16 -------- .../storage/include/storage/storage.hpp | 1 - .../src/migration/migration_manager.cpp | 2 - .../src/migration/transaction_hashes.cpp | 37 ------------------- libraries/core_libs/storage/src/storage.cpp | 12 +++--- 7 files changed, 17 insertions(+), 71 deletions(-) delete mode 100644 libraries/core_libs/storage/include/storage/migration/transaction_hashes.hpp delete mode 100644 libraries/core_libs/storage/src/migration/transaction_hashes.cpp diff --git a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp index d9c17abbf3..6cb07ce631 100644 --- a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp +++ b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp @@ -308,8 +308,6 @@ class FinalChainImpl final : public FinalChain { chunk_to_alter[index % c_bloomIndexSize] |= log_bloom_for_index; db_->insert(batch, DB::Columns::final_chain_log_blooms_index, chunk_id, util::rlp_enc(rlp_strm, chunk_to_alter)); } - db_->insert(batch, DB::Columns::final_chain_transaction_hashes_by_blk_number, blk_header.number, - dev::rlp(hashes_from_transactions(transactions))); db_->insert(batch, DB::Columns::final_chain_blk_hash_by_number, blk_header.number, blk_header.hash); db_->insert(batch, DB::Columns::final_chain_blk_number_by_hash, blk_header.hash, blk_header.number); db_->insert(batch, DB::Columns::final_chain_meta, DBMetaKeys::LAST_NUMBER, blk_header.number); @@ -438,9 +436,15 @@ class FinalChainImpl final : public FinalChain { private: std::shared_ptr get_transaction_hashes(std::optional n = {}) const { - auto res = db_->lookup(last_if_absent(n), DB::Columns::final_chain_transaction_hashes_by_blk_number); - - return std::make_shared(util::rlp_dec(dev::RLP(res))); + const auto& trxs = db_->getPeriodTransactions(last_if_absent(n)); + auto ret = std::make_shared(); + if (!trxs) { + return ret; + } + ret->reserve(trxs->size()); + std::transform(trxs->cbegin(), trxs->cend(), std::back_inserter(*ret), + [](auto const& trx) { return trx->getHash(); }); + return ret; } const SharedTransactions get_transactions(std::optional n = {}) const { diff --git a/libraries/core_libs/network/rpc/eth/Eth.cpp b/libraries/core_libs/network/rpc/eth/Eth.cpp index f45d37824a..4a47c45774 100644 --- a/libraries/core_libs/network/rpc/eth/Eth.cpp +++ b/libraries/core_libs/network/rpc/eth/Eth.cpp @@ -343,12 +343,12 @@ class EthImpl : public Eth, EthParams { } optional get_transaction(uint64_t trx_pos, EthBlockNumber blk_n) const { - auto hashes = final_chain->transaction_hashes(blk_n); - if (hashes->size() <= trx_pos) { + const auto& trxs = final_chain->transactions(blk_n); + if (trxs.size() <= trx_pos) { return {}; } return LocalisedTransaction{ - get_trx(hashes->at(trx_pos)), + trxs[trx_pos], TransactionLocationWithBlockHash{ {blk_n, trx_pos}, *final_chain->block_hash(blk_n), diff --git a/libraries/core_libs/storage/include/storage/migration/transaction_hashes.hpp b/libraries/core_libs/storage/include/storage/migration/transaction_hashes.hpp deleted file mode 100644 index f766588c61..0000000000 --- a/libraries/core_libs/storage/include/storage/migration/transaction_hashes.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include - -#include "final_chain/final_chain.hpp" -#include "storage/migration/migration_base.hpp" -#include "transaction/transaction.hpp" - -namespace taraxa::storage::migration { -class TransactionHashes : public migration::Base { - public: - TransactionHashes(std::shared_ptr db); - std::string id() override; - uint32_t dbVersion() override; - void migrate(logger::Logger& log) override; -}; -} // namespace taraxa::storage::migration \ No newline at end of file diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index dd8e656488..ce76655636 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -113,7 +113,6 @@ class DbStorage : public std::enable_shared_from_this { COLUMN(dag_block_period); COLUMN_W_COMP(proposal_period_levels_map, getIntComparator()); COLUMN(final_chain_meta); - COLUMN(final_chain_transaction_hashes_by_blk_number); COLUMN(final_chain_blk_by_number); COLUMN(final_chain_blk_hash_by_number); COLUMN(final_chain_blk_number_by_hash); diff --git a/libraries/core_libs/storage/src/migration/migration_manager.cpp b/libraries/core_libs/storage/src/migration/migration_manager.cpp index 954395f1b9..da9a73bb04 100644 --- a/libraries/core_libs/storage/src/migration/migration_manager.cpp +++ b/libraries/core_libs/storage/src/migration/migration_manager.cpp @@ -1,12 +1,10 @@ #include "storage/migration/migration_manager.hpp" #include "storage/migration/period_data.hpp" -#include "storage/migration/transaction_hashes.hpp" namespace taraxa::storage::migration { Manager::Manager(std::shared_ptr db, const addr_t& node_addr) : db_(db) { LOG_OBJECTS_CREATE("MIGRATIONS"); - registerMigration(); registerMigration(); } diff --git a/libraries/core_libs/storage/src/migration/transaction_hashes.cpp b/libraries/core_libs/storage/src/migration/transaction_hashes.cpp deleted file mode 100644 index 3b711af079..0000000000 --- a/libraries/core_libs/storage/src/migration/transaction_hashes.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "storage/migration/transaction_hashes.hpp" - -namespace taraxa::storage::migration { -struct OldTransactionsHashes { - std::string serialized_; - size_t count_; - - explicit OldTransactionsHashes(std::string serialized) - : serialized_(std::move(serialized)), count_(serialized_.size() / dev::h256::size) {} - dev::h256 get(size_t i) const { - return dev::h256(reinterpret_cast(serialized_.data() + i * dev::h256::size), - dev::h256::ConstructFromPointer); - } - size_t count() const { return count_; } -}; - -TransactionHashes::TransactionHashes(std::shared_ptr db) : migration::Base(db) {} - -std::string TransactionHashes::id() { return "TransactionHashes"; } - -uint32_t TransactionHashes::dbVersion() { return 1; } - -void TransactionHashes::migrate(logger::Logger& /*log*/) { - auto it = db_->getColumnIterator(DB::Columns::final_chain_transaction_hashes_by_blk_number); - - // Get and save data in new format for all blocks - for (it->SeekToFirst(); it->Valid(); it->Next()) { - ::taraxa::TransactionHashes new_data; - auto old_data = std::make_unique(it->value().ToString()); - new_data.reserve(old_data->count()); - for (size_t i = 0; i < new_data.capacity(); ++i) { - new_data.emplace_back(old_data->get(i)); - } - db_->insert(batch_, DB::Columns::final_chain_transaction_hashes_by_blk_number, it->key(), dev::rlp(new_data)); - } -} -} // namespace taraxa::storage::migration \ No newline at end of file diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 76d66dca30..540b9bf8ec 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -580,13 +580,12 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level for (auto period = start_period; period < end_period; period++) { // Find transactions included in the old blocks and delete data related to these transactions to free disk // space - auto trx_hashes_raw = lookup(period, DB::Columns::final_chain_transaction_hashes_by_blk_number); - TransactionHashes trx_hashes = (util::rlp_dec(dev::RLP(trx_hashes_raw))); - - for (auto hash : trx_hashes) { - remove(write_batch, Columns::final_chain_receipt_by_trx_hash, hash); + const auto& transactions = getPeriodTransactions(period); + if (transactions.has_value()) { + for (const auto& trx : *transactions) { + remove(write_batch, Columns::final_chain_receipt_by_trx_hash, trx->getHash()); + } } - remove(write_batch, Columns::final_chain_transaction_hashes_by_blk_number, EthBlockNumber(period)); if ((period - start_period + 1) % max_batch_delete == 0) { commitWriteBatch(write_batch); write_batch = createWriteBatch(); @@ -600,7 +599,6 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level // the data in the database and free disk space db_->CompactRange({}, handle(Columns::period_data), &start_slice, &end_slice); db_->CompactRange({}, handle(Columns::final_chain_receipt_by_trx_hash), nullptr, nullptr); - db_->CompactRange({}, handle(Columns::final_chain_transaction_hashes_by_blk_number), nullptr, nullptr); } } } From afb7d8c205ff5ac53da96fb20e404bb08773c99b Mon Sep 17 00:00:00 2001 From: Leonard Mocanu Date: Fri, 14 Jul 2023 12:05:19 +0300 Subject: [PATCH 088/134] chore: separates indexer ingress from node ingress --- .../taraxa-node-indexer-ingress.yaml | 64 +++++++++++++++++++ .../templates/taraxa-node-ingress.yaml | 48 -------------- charts/taraxa-node/values.yaml | 25 +++++--- 3 files changed, 79 insertions(+), 58 deletions(-) create mode 100644 charts/taraxa-node/templates/taraxa-node-indexer-ingress.yaml diff --git a/charts/taraxa-node/templates/taraxa-node-indexer-ingress.yaml b/charts/taraxa-node/templates/taraxa-node-indexer-ingress.yaml new file mode 100644 index 0000000000..e0c1f74926 --- /dev/null +++ b/charts/taraxa-node/templates/taraxa-node-indexer-ingress.yaml @@ -0,0 +1,64 @@ +{{ if .Values.node.enabled }} +{{ if .Values.node.indexer.enabled }} +{{- if .Values.node.indexer.ingress.enabled -}} + +{{- $fullName := include "taraxa-node.fullname" . -}} +{{- $apiIsStable := eq (include "taraxa-node.ingress.isStable" .) "true" -}} +{{- $ingressSupportsPathType := eq (include "taraxa-node.ingress.supportsPathType" .) "true" -}} +{{- $serviceName := printf "%s-%s" (include "taraxa-node.fullname" .) "head" -}} + +{{- $servicePortHttp := 8080 -}} + +{{- range .Values.node.service.ports }} + {{ if eq .name "http-indexer"}} {{ $servicePortHttp = .port }} {{ end }} +{{- end }} + +{{- $pathType := .Values.node.ingress.pathType | default "ImplementationSpecific" -}} +--- +apiVersion: {{ include "taraxa-node.ingress.apiVersion" . }} +kind: Ingress +metadata: + name: {{ $fullName }}-indexer + labels: + app: taraxa-node + app.kubernetes.io/name: {{ include "taraxa-node.name" . }} + helm.sh/chart: {{ include "taraxa-node.chart" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + {{- with .Values.node.indexer.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: +{{- if $apiIsStable }} +{{- if .Values.node.indexer.ingress.ingressClassName }} + ingressClassName: {{ .Values.node.indexer.ingress.ingressClassName }} +{{- end }} +{{- end }} + {{- if .Values.wildCertDomainSecret }} + tls: + - hosts: + - {{ include "taraxa-node.indexerName" . | quote }} + secretName: {{ .Values.wildCertDomainSecret }} + {{- end }} + rules: + - host: {{ include "taraxa-node.indexerName" . | quote }} + http: + paths: + - path: / + {{- if and $pathType $ingressSupportsPathType }} + pathType: {{ $pathType }} + {{- end }} + backend: + {{- if $apiIsStable }} + service: + name: {{ $serviceName }} + port: + number: {{ $servicePortHttp }} + {{- else }} + serviceName: {{ $serviceName }} + servicePort: {{ $servicePortHttp }} + {{- end }} +{{- end }} +{{- end }} +{{- end }} diff --git a/charts/taraxa-node/templates/taraxa-node-ingress.yaml b/charts/taraxa-node/templates/taraxa-node-ingress.yaml index e42f1f868b..7e10368c55 100644 --- a/charts/taraxa-node/templates/taraxa-node-ingress.yaml +++ b/charts/taraxa-node/templates/taraxa-node-ingress.yaml @@ -10,14 +10,12 @@ {{- $servicePortRpcWs := 8777 -}} {{- $servicePortGraphQl := 9777 -}} {{- $servicePortGraphQlWs := 6777 -}} -{{- $servicePortHttp := 8080 -}} {{- range .Values.node.service.ports }} {{ if eq .name "rest"}} {{ $servicePortRpc = .port }} {{ end }} {{ if eq .name "ws"}} {{ $servicePortRpcWs = .port }} {{ end }} {{ if eq .name "graphql"}} {{ $servicePortGraphQl = .port }} {{ end }} {{ if eq .name "graphql-ws"}} {{ $servicePortGraphQlWs = .port }} {{ end }} - {{ if eq .name "http-indexer"}} {{ $servicePortHttp = .port }} {{ end }} {{- end }} {{- $pathType := .Values.node.ingress.pathType | default "ImplementationSpecific" -}} @@ -201,51 +199,5 @@ spec: serviceName: {{ $serviceName }} servicePort: {{ $servicePortGraphQlWs }} {{- end }} - ---- -apiVersion: {{ include "taraxa-node.ingress.apiVersion" . }} -kind: Ingress -metadata: - name: {{ $fullName }}-indexer - labels: - app: taraxa-node - app.kubernetes.io/name: {{ include "taraxa-node.name" . }} - helm.sh/chart: {{ include "taraxa-node.chart" . }} - app.kubernetes.io/instance: {{ .Release.Name }} - app.kubernetes.io/managed-by: {{ .Release.Service }} - {{- with .Values.node.ingress.annotationsIndexer }} - annotations: - {{- toYaml . | nindent 4 }} - {{- end }} -spec: -{{- if $apiIsStable }} -{{- if .Values.node.ingress.ingressClassName }} - ingressClassName: {{ .Values.node.ingress.ingressClassName }} -{{- end }} -{{- end }} - {{- if .Values.wildCertDomainSecret }} - tls: - - hosts: - - {{ include "taraxa-node.indexerName" . | quote }} - secretName: {{ .Values.wildCertDomainSecret }} - {{- end }} - rules: - - host: {{ include "taraxa-node.indexerName" . | quote }} - http: - paths: - - path: / - {{- if and $pathType $ingressSupportsPathType }} - pathType: {{ $pathType }} - {{- end }} - backend: - {{- if $apiIsStable }} - service: - name: {{ $serviceName }} - port: - number: {{ $servicePortHttp }} - {{- else }} - serviceName: {{ $serviceName }} - servicePort: {{ $servicePortHttp }} - {{- end }} {{- end }} {{- end }} diff --git a/charts/taraxa-node/values.yaml b/charts/taraxa-node/values.yaml index aa49074ef8..6d1fcd19ed 100644 --- a/charts/taraxa-node/values.yaml +++ b/charts/taraxa-node/values.yaml @@ -104,16 +104,6 @@ node: annotationsRpcWS: {} annotationsGraphQl: {} annotationsGraphQlWS: {} - annotationsIndexer: - nginx.ingress.kubernetes.io/affinity: "cookie" - nginx.ingress.kubernetes.io/session-cookie-name: "stickounet" - nginx.ingress.kubernetes.io/session-cookie-expires: "172800" - nginx.ingress.kubernetes.io/session-cookie-max-age: "172800" - nginx.ingress.kubernetes.io/affinity-mode: persistent - nginx.ingress.kubernetes.io/enable-cors: "true" - nginx.ingress.kubernetes.io/cors-allow-origin: "*" - nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS, DELETE" - nginx.ingress.kubernetes.io/cors-allow-headers: "Authorization,Range,Content-Range,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Access-Control-Allow-Origin" hosts: [] tls: [] ports: @@ -171,6 +161,21 @@ node: annotations: {} indexer: enabled: false + ingress: + # For Kubernetes >= 1.18 you should specify the ingress-controller via the field ingressClassName + # See https://kubernetes.io/blog/2020/04/02/improvements-to-the-ingress-api-in-kubernetes-1.18/#specifying-the-class-of-an-ingress + # ingressClassName: nginx + enabled: false + annotations: + nginx.ingress.kubernetes.io/affinity: "cookie" + nginx.ingress.kubernetes.io/session-cookie-name: "stickounet" + nginx.ingress.kubernetes.io/session-cookie-expires: "172800" + nginx.ingress.kubernetes.io/session-cookie-max-age: "172800" + nginx.ingress.kubernetes.io/affinity-mode: persistent + nginx.ingress.kubernetes.io/enable-cors: "true" + nginx.ingress.kubernetes.io/cors-allow-origin: "*" + nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS, DELETE" + nginx.ingress.kubernetes.io/cors-allow-headers: "Authorization,Range,Content-Range,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Access-Control-Allow-Origin" image: repository: gcr.io/jovial-meridian-249123/taraxa-indexer tag: latest From 8dd562edf74a200a3c998baa709cd9a6a53dadb5 Mon Sep 17 00:00:00 2001 From: Leonard Mocanu Date: Fri, 14 Jul 2023 12:19:17 +0300 Subject: [PATCH 089/134] chore: bumps chart version and changelog --- charts/taraxa-node/CHANGELOG.md | 6 ++++++ charts/taraxa-node/Chart.yaml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/charts/taraxa-node/CHANGELOG.md b/charts/taraxa-node/CHANGELOG.md index f923c637a9..9e2b98a6bb 100644 --- a/charts/taraxa-node/CHANGELOG.md +++ b/charts/taraxa-node/CHANGELOG.md @@ -3,6 +3,12 @@ This file documents all notable changes to `taraxa-node` Helm Chart. The release numbering uses [semantic versioning](http://semver.org). +## v0.3.11 + +### Minor changes + +* Separates ingress for indexer + ## v0.3.9 ### Major changes diff --git a/charts/taraxa-node/Chart.yaml b/charts/taraxa-node/Chart.yaml index bd384e984d..a9b46b465c 100644 --- a/charts/taraxa-node/Chart.yaml +++ b/charts/taraxa-node/Chart.yaml @@ -2,7 +2,7 @@ apiVersion: v2 appVersion: "1.0" description: Kubernetes helm chart for Taraxa blockchain full node implementation. name: taraxa-node -version: 0.3.10 +version: 0.3.11 keywords: - blockchain - taraxa From 0e845c7ea7e04e5cfa585fb1bc5c1699181fb3d6 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Fri, 14 Jul 2023 13:48:27 +0200 Subject: [PATCH 090/134] chore: remove entry only on same endpoint --- libraries/aleth/libp2p/NodeTable.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/aleth/libp2p/NodeTable.cpp b/libraries/aleth/libp2p/NodeTable.cpp index cab8040814..41fea1769c 100644 --- a/libraries/aleth/libp2p/NodeTable.cpp +++ b/libraries/aleth/libp2p/NodeTable.cpp @@ -695,10 +695,13 @@ void NodeTable::doHandleTimeouts() { for (auto it = m_sentPings.begin(); it != m_sentPings.end();) { if (chrono::steady_clock::now() > it->second.pingSentTime + m_requestTimeToLive) { if (auto node = nodeEntry(it->second.nodeID)) { - dropNode(std::move(node)); + if (node->endpoint() == it->first) { + dropNode(std::move(node)); - // save the replacement node that should be activated - if (it->second.replacementNodeEntry) nodesToActivate.emplace_back(std::move(it->second.replacementNodeEntry)); + // save the replacement node that should be activated + if (it->second.replacementNodeEntry) + nodesToActivate.emplace_back(std::move(it->second.replacementNodeEntry)); + } } it = m_sentPings.erase(it); From 56400b437f536921f9e5ab951b5e0540acfdd603 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Fri, 14 Jul 2023 14:35:09 +0200 Subject: [PATCH 091/134] chore: readd bootnodes on 0 found nodes --- libraries/core_libs/network/src/network.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index 29cbecd9ba..f4a0ac8ab9 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -189,7 +189,7 @@ void Network::registerPeriodicEvents(const std::shared_ptr &pbft_mg // Check nodes connections and refresh boot nodes auto checkNodesConnections = [this]() { // If node count drops to zero add boot nodes again and retry - if (host_->peer_count() == 0) { + if (host_->peer_count() == 0 || host_->getNodeCount() == 0) { addBootNodes(); } }; From 8bbbfbda3cbd75b4ca0dbc83351ec0e6cca6133d Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Thu, 13 Jul 2023 14:32:44 +0200 Subject: [PATCH 092/134] fix: state root validation on syncing --- .../consensus/include/pbft/pbft_manager.hpp | 7 ++-- .../consensus/src/pbft/pbft_manager.cpp | 33 +++++++++++++++---- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp index 017f032b2f..331c0f5816 100644 --- a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp +++ b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp @@ -20,6 +20,7 @@ namespace taraxa { class FullNode; enum PbftStates { value_proposal_state = 1, filter_state, certify_state, finish_state, finish_polling_state }; +enum class PbftStateRootValidation { Valid = 0, Missing, Invalid }; /** * @brief PbftManager class is a daemon that is used to finalize a bench of directed acyclic graph (DAG) blocks by using @@ -448,11 +449,11 @@ class PbftManager { bool validatePbftBlock(const std::shared_ptr &pbft_block) const; /** - * @brief Validates pbft block state root. It checks if: + * @brief Validates pbft block state root. * @param pbft_block PBFT block - * @return true if pbft block is valid, otherwise false + * @return validation result */ - bool validatePbftBlockStateRoot(const std::shared_ptr &pbft_block) const; + PbftStateRootValidation validatePbftBlockStateRoot(const std::shared_ptr &pbft_block) const; /** * @brief If there are enough certify votes, push the vote PBFT block in PBFT chain diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index fa8fac2969..59c04938ea 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -1297,7 +1297,7 @@ std::shared_ptr PbftManager::identifyLeaderBlock_(PbftRound round, Pb return empty_leader_block; } -bool PbftManager::validatePbftBlockStateRoot(const std::shared_ptr &pbft_block) const { +PbftStateRootValidation PbftManager::validatePbftBlockStateRoot(const std::shared_ptr &pbft_block) const { auto period = pbft_block->getPeriod(); auto const &pbft_block_hash = pbft_block->getBlockHash(); { @@ -1307,16 +1307,16 @@ bool PbftManager::validatePbftBlockStateRoot(const std::shared_ptr &p prev_state_root_hash = header->state_root; } else { LOG(log_wr_) << "Block " << pbft_block_hash << " could not be validated as we are behind"; - return false; + return PbftStateRootValidation::Missing; } } if (pbft_block->getPrevStateRoot() != prev_state_root_hash) { LOG(log_er_) << "Block " << pbft_block_hash << " state root " << pbft_block->getPrevStateRoot() << " isn't matching actual " << prev_state_root_hash; - return false; + return PbftStateRootValidation::Invalid; } } - return true; + return PbftStateRootValidation::Valid; } bool PbftManager::validatePbftBlock(const std::shared_ptr &pbft_block) const { @@ -1332,7 +1332,7 @@ bool PbftManager::validatePbftBlock(const std::shared_ptr &pbft_block auto const &pbft_block_hash = pbft_block->getBlockHash(); - if (!validatePbftBlockStateRoot(pbft_block)) { + if (validatePbftBlockStateRoot(pbft_block) != PbftStateRootValidation::Valid) { return false; } @@ -1641,8 +1641,27 @@ std::optional>>> PbftMan return std::nullopt; } - if (!validatePbftBlockStateRoot(period_data.pbft_blk)) { - return std::nullopt; + bool retry_logged = false; + while (true) { + auto validation_result = validatePbftBlockStateRoot(period_data.pbft_blk); + if (validation_result != PbftStateRootValidation::Missing) { + if (validation_result == PbftStateRootValidation::Invalid) { + LOG(log_er_) << "Failed verifying block " << pbft_block_hash + << " with invalid state root: " << period_data.pbft_blk->getPrevStateRoot() + << ". Disconnect malicious peer " << node_id.abridged(); + sync_queue_.clear(); + net->handleMaliciousSyncPeer(node_id); + return std::nullopt; + } + break; + } + // If syncing and pbft manager is faster than execution a delay might be needed to allow EVM to catch up + thisThreadSleepForMilliSeconds(10); + if (!retry_logged) { + LOG(log_wr_) << "PBFT block " << pbft_block_hash + << " validation delayed, state root missing, execution is behind"; + retry_logged = true; + } } // Check reward votes From 7c47a598cd01d96e7055668706ac769c1959e587 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Wed, 19 Jul 2023 13:40:26 +0200 Subject: [PATCH 093/134] chore: improve ping timeout handling on discovery --- libraries/aleth/libp2p/NodeTable.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/libraries/aleth/libp2p/NodeTable.cpp b/libraries/aleth/libp2p/NodeTable.cpp index 41fea1769c..99b82f85ae 100644 --- a/libraries/aleth/libp2p/NodeTable.cpp +++ b/libraries/aleth/libp2p/NodeTable.cpp @@ -461,6 +461,14 @@ shared_ptr NodeTable::handlePong(bi::udp::endpoint const& _from, Disc } m_sentPings.erase(_from); + // Remove any other sent pings which was sent to other endpoints for the same nodeID + for (auto it = m_sentPings.begin(); it != m_sentPings.end();) { + if (nodeValidation.nodeID == it->second.nodeID) { + it = m_sentPings.erase(it); + } else { + ++it; + } + } // update our external endpoint address and UDP port if (m_endpointTracker.addEndpointStatement(_from, pong.destination) >= c_minEndpointTrackStatements) { @@ -695,13 +703,10 @@ void NodeTable::doHandleTimeouts() { for (auto it = m_sentPings.begin(); it != m_sentPings.end();) { if (chrono::steady_clock::now() > it->second.pingSentTime + m_requestTimeToLive) { if (auto node = nodeEntry(it->second.nodeID)) { - if (node->endpoint() == it->first) { - dropNode(std::move(node)); + dropNode(std::move(node)); - // save the replacement node that should be activated - if (it->second.replacementNodeEntry) - nodesToActivate.emplace_back(std::move(it->second.replacementNodeEntry)); - } + // save the replacement node that should be activated + if (it->second.replacementNodeEntry) nodesToActivate.emplace_back(std::move(it->second.replacementNodeEntry)); } it = m_sentPings.erase(it); From 4b6a475db153eac9b38999c2e9222e5e0eb047bb Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Mon, 17 Jul 2023 11:10:02 +0200 Subject: [PATCH 094/134] feat: check for config change and update logging config on change --- libraries/config/include/config/config.hpp | 4 ++ libraries/config/src/config.cpp | 76 ++++++++++++++++------ libraries/core_libs/node/src/node.cpp | 2 + libraries/logger/src/logger_config.cpp | 2 +- 4 files changed, 63 insertions(+), 21 deletions(-) diff --git a/libraries/config/include/config/config.hpp b/libraries/config/include/config/config.hpp index 6a537a3b93..85d44bcb59 100644 --- a/libraries/config/include/config/config.hpp +++ b/libraries/config/include/config/config.hpp @@ -36,8 +36,12 @@ struct FullNodeConfig { const Json::Value &genesis = Json::Value::null, const std::string &config_file_path = ""); void overwriteConfigFromJson(const Json::Value &config_json); + std::vector loadLoggingConfigs(const Json::Value &logging); + void scheduleLoggingConfigUpdate(); + void InitLogging(const addr_t &node_address); std::string json_file_name; + std::filesystem::file_time_type last_json_update_time; dev::Secret node_secret; vrf_wrapper::vrf_sk_t vrf_secret; fs::path data_path; diff --git a/libraries/config/src/config.cpp b/libraries/config/src/config.cpp index 91746e521d..d0e8e26da6 100644 --- a/libraries/config/src/config.cpp +++ b/libraries/config/src/config.cpp @@ -5,6 +5,7 @@ #include #include "common/jsoncpp.hpp" +#include "common/thread_pool.hpp" #include "config/config_utils.hpp" namespace taraxa { @@ -17,29 +18,16 @@ void dec_json(Json::Value const &json, DBConfig &db_config) { db_config.db_max_open_files = getConfigDataAsUInt(json, {"db_max_open_files"}, true, db_config.db_max_open_files); } -void FullNodeConfig::overwriteConfigFromJson(const Json::Value &root) { - data_path = getConfigDataAsString(root, {"data_path"}); - db_path = data_path / "db"; - - final_chain_cache_in_blocks = - getConfigDataAsUInt(root, {"final_chain_cache_in_blocks"}, true, final_chain_cache_in_blocks); - - // config values that limits transactions and blocks memory pools - transactions_pool_size = getConfigDataAsUInt(root, {"transactions_pool_size"}, true, kDefaultTransactionPoolSize); - - dec_json(root["network"], network); - - dec_json(root["db_config"], db_config); - - // Network logging in p2p library creates performance issues even with - // channel/verbosity off Disable it completely in net channel is not present - if (!root["logging"].isNull()) { - if (auto path = getConfigData(root["logging"], {"log_path"}, true); !path.isNull()) { +std::vector FullNodeConfig::loadLoggingConfigs(const Json::Value &logging) { + last_json_update_time = std::filesystem::last_write_time(std::filesystem::path(json_file_name)); + std::vector res; + if (!logging.isNull()) { + if (auto path = getConfigData(logging, {"log_path"}, true); !path.isNull()) { log_path = path.asString(); } else { log_path = data_path / "logs"; } - for (auto &item : root["logging"]["configurations"]) { + for (auto &item : logging["configurations"]) { auto on = getConfigDataAsBoolean(item, {"on"}); if (on) { logger::Config logging; @@ -69,10 +57,28 @@ void FullNodeConfig::overwriteConfigFromJson(const Json::Value &root) { } logging.outputs.push_back(output); } - log_configs.push_back(logging); + res.push_back(logging); } } } + return res; +} + +void FullNodeConfig::overwriteConfigFromJson(const Json::Value &root) { + data_path = getConfigDataAsString(root, {"data_path"}); + db_path = data_path / "db"; + + final_chain_cache_in_blocks = + getConfigDataAsUInt(root, {"final_chain_cache_in_blocks"}, true, final_chain_cache_in_blocks); + + // config values that limits transactions and blocks memory pools + transactions_pool_size = getConfigDataAsUInt(root, {"transactions_pool_size"}, true, kDefaultTransactionPoolSize); + + dec_json(root["network"], network); + + dec_json(root["db_config"], db_config); + + log_configs = loadLoggingConfigs(root["logging"]); is_light_node = getConfigDataAsBoolean(root, {"is_light_node"}, true, is_light_node); light_node_history = getConfigDataAsUInt(root, {"light_node_history"}, true, light_node_history); @@ -145,6 +151,36 @@ FullNodeConfig::FullNodeConfig(const Json::Value &string_or_object, const Json:: opts_final_chain.max_trie_full_node_levels_to_cache = 4; } +void FullNodeConfig::scheduleLoggingConfigUpdate() { + static util::ThreadPool executor_{1}; + static auto node_address = dev::KeyPair(node_secret).address(); + executor_.post([&]() { + while (true) { + std::this_thread::sleep_for(std::chrono::minutes(1)); + auto update_time = std::filesystem::last_write_time(std::filesystem::path(json_file_name)); + if (last_json_update_time >= update_time) { + continue; + } + last_json_update_time = update_time; + try { + auto config = getJsonFromFileOrString(json_file_name); + log_configs = loadLoggingConfigs(config["logging"]); + InitLogging(node_address); + } catch (const ConfigException &e) { + std::cerr << "FullNodeConfig: Failed to update logging config: " << e.what() << std::endl; + continue; + } + std::cout << "FullNodeConfig: Updated logging config" << std::endl; + } + }); +} + +void FullNodeConfig::InitLogging(const addr_t &node_address) { + for (auto &logging : log_configs) { + logging.InitLogging(node_address); + } +} + void FullNodeConfig::validate() const { genesis.validate(); network.validate(genesis.state.dpos.delegation_delay); diff --git a/libraries/core_libs/node/src/node.cpp b/libraries/core_libs/node/src/node.cpp index a17affff53..687a74b0a7 100644 --- a/libraries/core_libs/node/src/node.cpp +++ b/libraries/core_libs/node/src/node.cpp @@ -46,6 +46,8 @@ void FullNode::init() { logging.InitLogging(node_addr); } + conf_.scheduleLoggingConfigUpdate(); + LOG_OBJECTS_CREATE("FULLND"); LOG(log_si_) << "Node public key: " << EthGreen << kp_.pub().toString() << std::endl diff --git a/libraries/logger/src/logger_config.cpp b/libraries/logger/src/logger_config.cpp index 21de503564..d613426502 100644 --- a/libraries/logger/src/logger_config.cpp +++ b/libraries/logger/src/logger_config.cpp @@ -23,7 +23,7 @@ Verbosity stringToVerbosity(std::string _verbosity) { if (_verbosity == "INFO") return Verbosity::Info; if (_verbosity == "DEBUG") return Verbosity::Debug; if (_verbosity == "TRACE") return Verbosity::Trace; - throw("Unknown verbosity string"); + throw ConfigException("Unknown verbosity string: " + _verbosity); } Config::Config(fs::path log_path) { From 9a19ff530c0bc9d973ac037f688a717214458ee8 Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Mon, 17 Jul 2023 14:26:23 +0200 Subject: [PATCH 095/134] fix: failing tests because of missing config file path --- libraries/config/src/config.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libraries/config/src/config.cpp b/libraries/config/src/config.cpp index d0e8e26da6..35acac408d 100644 --- a/libraries/config/src/config.cpp +++ b/libraries/config/src/config.cpp @@ -19,7 +19,10 @@ void dec_json(Json::Value const &json, DBConfig &db_config) { } std::vector FullNodeConfig::loadLoggingConfigs(const Json::Value &logging) { - last_json_update_time = std::filesystem::last_write_time(std::filesystem::path(json_file_name)); + // could be empty if config loaded from json e.g. tests + if (!json_file_name.empty()) { + last_json_update_time = std::filesystem::last_write_time(std::filesystem::path(json_file_name)); + } std::vector res; if (!logging.isNull()) { if (auto path = getConfigData(logging, {"log_path"}, true); !path.isNull()) { @@ -152,6 +155,10 @@ FullNodeConfig::FullNodeConfig(const Json::Value &string_or_object, const Json:: } void FullNodeConfig::scheduleLoggingConfigUpdate() { + // no file to check updates for (e.g. tests) + if (json_file_name.empty()) { + return; + } static util::ThreadPool executor_{1}; static auto node_address = dev::KeyPair(node_secret).address(); executor_.post([&]() { From 581418f3b83ba26fd16e2af1621fe6466bbbb190 Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Thu, 20 Jul 2023 12:30:39 +0200 Subject: [PATCH 096/134] feat: add pbftHash to graphql pbft block --- CMakeModules/cpp_graphql_gen.cmake | 51 +++++++------- libraries/core_libs/CMakeLists.txt | 50 +++++++------- .../network/graphql/include/graphql/block.hpp | 6 +- .../network/graphql/include/graphql/query.hpp | 2 + .../graphql/include/graphql/transaction.hpp | 2 + .../include/graphql/types/dag_block.hpp | 4 +- .../graphql/schema/schema.taraxa.graphql | 2 + .../core_libs/network/graphql/src/block.cpp | 16 +++-- .../core_libs/network/graphql/src/query.cpp | 67 ++++++++++--------- .../network/graphql/src/transaction.cpp | 5 +- .../network/graphql/src/types/dag_block.cpp | 8 ++- 11 files changed, 120 insertions(+), 93 deletions(-) diff --git a/CMakeModules/cpp_graphql_gen.cmake b/CMakeModules/cpp_graphql_gen.cmake index 8cfbcfbafe..1ee15d1f86 100644 --- a/CMakeModules/cpp_graphql_gen.cmake +++ b/CMakeModules/cpp_graphql_gen.cmake @@ -1,4 +1,4 @@ -########### cppgraphqlgen ########### +# ########## cppgraphqlgen ########### include(FetchContent) set(Boost_NO_WARN_NEW_VERSIONS 1) @@ -6,36 +6,41 @@ set(Boost_NO_WARN_NEW_VERSIONS 1) FetchContent_Declare( cppgraphqlgen GIT_REPOSITORY https://github.com/microsoft/cppgraphqlgen.git - GIT_TAG 8c1623acc42392ef2a1bc0336482621386f98c77 # v4.5.0 + GIT_TAG 8c1623acc42392ef2a1bc0336482621386f98c77 # v4.5.0 ) set(GRAPHQL_BUILD_TESTS OFF) set(GRAPHQL_UPDATE_VERSION OFF) set(GRAPHQL_UPDATE_SAMPLES OFF) -#set(GRAPHQL_BUILD_SCHEMAGEN OFF) + +# set(GRAPHQL_BUILD_SCHEMAGEN OFF) set(GRAPHQL_BUILD_CLIENTGEN OFF) FetchContent_MakeAvailable(cppgraphqlgen) -###################################### + +# ##################################### set(GRAPHQL_GEN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/network/graphql/gen) list(APPEND GRAPHQL_GENERATED_SOURCES - ${GRAPHQL_GEN_DIR}/AccountObject.cpp - ${GRAPHQL_GEN_DIR}/BlockObject.cpp - ${GRAPHQL_GEN_DIR}/CallResultObject.cpp - ${GRAPHQL_GEN_DIR}/CurrentStateObject.cpp - ${GRAPHQL_GEN_DIR}/DagBlockObject.cpp - ${GRAPHQL_GEN_DIR}/LogObject.cpp - ${GRAPHQL_GEN_DIR}/MutationObject.cpp - ${GRAPHQL_GEN_DIR}/PendingObject.cpp - ${GRAPHQL_GEN_DIR}/QueryObject.cpp - ${GRAPHQL_GEN_DIR}/SubscriptionObject.cpp - ${GRAPHQL_GEN_DIR}/SyncStateObject.cpp - ${GRAPHQL_GEN_DIR}/TaraxaSchema.cpp - ${GRAPHQL_GEN_DIR}/TransactionObject.cpp + ${GRAPHQL_GEN_DIR}/AccountObject.cpp + ${GRAPHQL_GEN_DIR}/BlockObject.cpp + ${GRAPHQL_GEN_DIR}/CallResultObject.cpp + ${GRAPHQL_GEN_DIR}/CurrentStateObject.cpp + ${GRAPHQL_GEN_DIR}/DagBlockObject.cpp + ${GRAPHQL_GEN_DIR}/LogObject.cpp + ${GRAPHQL_GEN_DIR}/MutationObject.cpp + ${GRAPHQL_GEN_DIR}/PendingObject.cpp + ${GRAPHQL_GEN_DIR}/QueryObject.cpp + ${GRAPHQL_GEN_DIR}/SubscriptionObject.cpp + ${GRAPHQL_GEN_DIR}/SyncStateObject.cpp + ${GRAPHQL_GEN_DIR}/TaraxaSchema.cpp + ${GRAPHQL_GEN_DIR}/TransactionObject.cpp ) -add_custom_command(OUTPUT ${GRAPHQL_GENERATED_SOURCES} - COMMAND ${CMAKE_COMMAND} -E remove -f ${GRAPHQL_GEN_DIR}/*.cpp - COMMAND ${CMAKE_COMMAND} -E remove -f ${GRAPHQL_GEN_DIR}/*.h - COMMAND cppgraphqlgen::schemagen --schema="${CMAKE_CURRENT_SOURCE_DIR}/network/graphql/schema/schema.taraxa.graphql" --prefix="Taraxa" --namespace="taraxa" - WORKING_DIRECTORY ${GRAPHQL_GEN_DIR} - COMMENT "Regenerating TaraxaSchema files") +add_custom_command( + OUTPUT ${GRAPHQL_GENERATED_SOURCES} + COMMAND ${CMAKE_COMMAND} -E remove -f ${GRAPHQL_GEN_DIR}/*.cpp + COMMAND ${CMAKE_COMMAND} -E remove -f ${GRAPHQL_GEN_DIR}/*.h + COMMAND cppgraphqlgen::schemagen --schema="${CMAKE_CURRENT_SOURCE_DIR}/network/graphql/schema/schema.taraxa.graphql" --prefix="Taraxa" --namespace="taraxa" + WORKING_DIRECTORY ${GRAPHQL_GEN_DIR} + COMMENT "Regenerating TaraxaSchema files" + DEPENDS ${GRAPHQL_GEN_DIR}/../schema/schema.taraxa.graphql +) diff --git a/libraries/core_libs/CMakeLists.txt b/libraries/core_libs/CMakeLists.txt index b5d3266a2c..1e0655003e 100644 --- a/libraries/core_libs/CMakeLists.txt +++ b/libraries/core_libs/CMakeLists.txt @@ -5,6 +5,7 @@ file(GLOB_RECURSE CONSENSUS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/consensus/*.cpp) file(GLOB_RECURSE NETWORK_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/network/*.hpp) file(GLOB_RECURSE NETWORK_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/network/*.cpp) + # GraphQL include(${PROJECT_SOURCE_DIR}/CMakeModules/cpp_graphql_gen.cmake) file(GLOB_RECURSE NETWORK_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/network/*.h) @@ -19,20 +20,20 @@ file(GLOB_RECURSE REWARDS_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/rewards/*.hpp) file(GLOB_RECURSE REWARDS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/rewards/*.cpp) set(HEADERS - ${CONSENSUS_HEADERS} - ${NETWORK_HEADERS} - ${STORAGE_HEADERS} - ${NODE_HEADERS} - ${REWARDS_HEADERS} + ${CONSENSUS_HEADERS} + ${NETWORK_HEADERS} + ${STORAGE_HEADERS} + ${NODE_HEADERS} + ${REWARDS_HEADERS} ) set(SOURCES - ${CONSENSUS_SOURCES} - ${NETWORK_SOURCES} - ${STORAGE_SOURCES} - ${NODE_SOURCES} - ${GRAPHQL_GENERATED_SOURCES} - ${REWARDS_SOURCES} + ${CONSENSUS_SOURCES} + ${NETWORK_SOURCES} + ${STORAGE_SOURCES} + ${NODE_SOURCES} + ${GRAPHQL_GENERATED_SOURCES} + ${REWARDS_SOURCES} ) add_library(core_libs ${SOURCES} ${HEADERS}) @@ -43,25 +44,26 @@ target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/node/inc target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/storage/include) target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/rewards/include) target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + # GraphQL target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/network/graphql/gen) target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/network/graphql/include) target_link_libraries(core_libs PUBLIC - cli - pbft_block - taraxa-evm - p2p - metrics - Jsonrpccpp - CONAN_PKG::rocksdb -# GraphQL - cppgraphqlgen::graphqlservice - cppgraphqlgen::graphqljson -) + cli + pbft_block + taraxa-evm + p2p + metrics + Jsonrpccpp + CONAN_PKG::rocksdb + # GraphQL + cppgraphqlgen::graphqlservice + cppgraphqlgen::graphqljson +) install(TARGETS core_libs - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ) \ No newline at end of file diff --git a/libraries/core_libs/network/graphql/include/graphql/block.hpp b/libraries/core_libs/network/graphql/include/graphql/block.hpp index f253382002..cbee097866 100644 --- a/libraries/core_libs/network/graphql/include/graphql/block.hpp +++ b/libraries/core_libs/network/graphql/include/graphql/block.hpp @@ -8,15 +8,17 @@ #include "transaction/transaction_manager.hpp" namespace graphql::taraxa { - class Block { public: explicit Block(std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, std::shared_ptr<::taraxa::TransactionManager> trx_manager, + std::function(::taraxa::EthBlockNumber)> get_block_by_num, + const ::taraxa::blk_hash_t& pbft_block_hash, std::shared_ptr block_header) noexcept; response::Value getNumber() const noexcept; response::Value getHash() const noexcept; + response::Value getPbftHash() const noexcept; std::shared_ptr getParent() const noexcept; response::Value getNonce() const noexcept; response::Value getTransactionsRoot() const noexcept; @@ -46,6 +48,8 @@ class Block { private: std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain_; std::shared_ptr<::taraxa::TransactionManager> trx_manager_; + std::function(::taraxa::EthBlockNumber)> get_block_by_num_; + const ::taraxa::blk_hash_t kPBftBlockHash; std::shared_ptr block_header_; mutable std::vector> transactions_; }; diff --git a/libraries/core_libs/network/graphql/include/graphql/query.hpp b/libraries/core_libs/network/graphql/include/graphql/query.hpp index 78dc542fab..237e673689 100644 --- a/libraries/core_libs/network/graphql/include/graphql/query.hpp +++ b/libraries/core_libs/network/graphql/include/graphql/query.hpp @@ -3,6 +3,7 @@ #include "QueryObject.h" #include "dag/dag_manager.hpp" #include "final_chain/final_chain.hpp" +#include "graphql/block.hpp" #include "network/network.hpp" #include "pbft/pbft_manager.hpp" #include "transaction/gas_pricer.hpp" @@ -46,6 +47,7 @@ class Query { std::shared_ptr<::taraxa::GasPricer> gas_pricer_; std::weak_ptr<::taraxa::Network> network_; const uint64_t kChainId; + std::function(::taraxa::EthBlockNumber)> get_block_by_num_; }; } // namespace graphql::taraxa \ No newline at end of file diff --git a/libraries/core_libs/network/graphql/include/graphql/transaction.hpp b/libraries/core_libs/network/graphql/include/graphql/transaction.hpp index 63e7fbd059..300d442e9e 100644 --- a/libraries/core_libs/network/graphql/include/graphql/transaction.hpp +++ b/libraries/core_libs/network/graphql/include/graphql/transaction.hpp @@ -14,6 +14,7 @@ class Transaction final : public std::enable_shared_from_this { public: explicit Transaction(std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, std::shared_ptr<::taraxa::TransactionManager> trx_manager, + std::function(::taraxa::EthBlockNumber)>, std::shared_ptr<::taraxa::Transaction> transaction) noexcept; response::Value getHash() const noexcept; @@ -38,6 +39,7 @@ class Transaction final : public std::enable_shared_from_this { private: std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain_; std::shared_ptr<::taraxa::TransactionManager> trx_manager_; + std::function(::taraxa::EthBlockNumber)> get_block_by_num_; std::shared_ptr<::taraxa::Transaction> transaction_; // Caching for performance mutable std::optional<::taraxa::final_chain::TransactionReceipt> receipt_; diff --git a/libraries/core_libs/network/graphql/include/graphql/types/dag_block.hpp b/libraries/core_libs/network/graphql/include/graphql/types/dag_block.hpp index 8267135dbe..6ef037d513 100644 --- a/libraries/core_libs/network/graphql/include/graphql/types/dag_block.hpp +++ b/libraries/core_libs/network/graphql/include/graphql/types/dag_block.hpp @@ -12,7 +12,8 @@ class DagBlock { explicit DagBlock(std::shared_ptr<::taraxa::DagBlock> dag_block, std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, std::shared_ptr<::taraxa::PbftManager> pbft_manager, - std::shared_ptr<::taraxa::TransactionManager> transaction_manager) noexcept; + std::shared_ptr<::taraxa::TransactionManager> transaction_manager, + std::function(::taraxa::EthBlockNumber)> get_block_by_num) noexcept; response::Value getHash() const noexcept; response::Value getPivot() const noexcept; @@ -31,6 +32,7 @@ class DagBlock { std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain_; std::shared_ptr<::taraxa::PbftManager> pbft_manager_; std::shared_ptr<::taraxa::TransactionManager> transaction_manager_; + std::function(::taraxa::EthBlockNumber)> get_block_by_num_; mutable std::optional period_; }; diff --git a/libraries/core_libs/network/graphql/schema/schema.taraxa.graphql b/libraries/core_libs/network/graphql/schema/schema.taraxa.graphql index 4bfad2bbb2..4c9c7267ee 100644 --- a/libraries/core_libs/network/graphql/schema/schema.taraxa.graphql +++ b/libraries/core_libs/network/graphql/schema/schema.taraxa.graphql @@ -159,6 +159,8 @@ type Block { number: Long! # Hash is the block hash of this block. hash: Bytes32! + # Pbft hash of this block. + pbftHash: Bytes32! # Parent is the parent block of this block. parent: Block # Nonce is the block nonce, an 8 byte sequence determined by the miner. diff --git a/libraries/core_libs/network/graphql/src/block.cpp b/libraries/core_libs/network/graphql/src/block.cpp index da39f2f36b..9158d37e1c 100644 --- a/libraries/core_libs/network/graphql/src/block.cpp +++ b/libraries/core_libs/network/graphql/src/block.cpp @@ -13,19 +13,23 @@ namespace graphql::taraxa { Block::Block(std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, std::shared_ptr<::taraxa::TransactionManager> trx_manager, + std::function(::taraxa::EthBlockNumber)> get_block_by_num, + const ::taraxa::blk_hash_t& pbft_block_hash, std::shared_ptr block_header) noexcept : final_chain_(std::move(final_chain)), trx_manager_(std::move(trx_manager)), + get_block_by_num_(std::move(get_block_by_num)), + kPBftBlockHash(pbft_block_hash), block_header_(std::move(block_header)) {} response::Value Block::getNumber() const noexcept { return response::Value(static_cast(block_header_->number)); } response::Value Block::getHash() const noexcept { return response::Value(block_header_->hash.toString()); } +response::Value Block::getPbftHash() const noexcept { return response::Value(kPBftBlockHash.toString()); } + std::shared_ptr Block::getParent() const noexcept { - return std::make_shared(std::make_shared( - final_chain_, trx_manager_, - final_chain_->block_header(final_chain_->block_number(dev::h256(block_header_->parent_hash))))); + return get_block_by_num_(block_header_->number - 1); } response::Value Block::getNonce() const noexcept { return response::Value(block_header_->nonce().toString()); } @@ -99,8 +103,8 @@ std::optional>> Block::getTrans } ret.reserve(transactions_.size()); for (auto& t : transactions_) { - ret.emplace_back( - std::make_shared(std::make_shared(final_chain_, trx_manager_, t))); + ret.emplace_back(std::make_shared( + std::make_shared(final_chain_, trx_manager_, get_block_by_num_, t))); } return ret; } @@ -114,7 +118,7 @@ std::shared_ptr Block::getTransactionAt(response::IntType&& return nullptr; } return std::make_shared( - std::make_shared(final_chain_, trx_manager_, transactions_[index])); + std::make_shared(final_chain_, trx_manager_, get_block_by_num_, transactions_[index])); } std::vector> Block::getLogs(BlockFilterCriteria&&) const noexcept { diff --git a/libraries/core_libs/network/graphql/src/query.cpp b/libraries/core_libs/network/graphql/src/query.cpp index 7606de867b..9afae748f1 100644 --- a/libraries/core_libs/network/graphql/src/query.cpp +++ b/libraries/core_libs/network/graphql/src/query.cpp @@ -26,40 +26,41 @@ Query::Query(std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, db_(std::move(db)), gas_pricer_(std::move(gas_pricer)), network_(std::move(network)), - kChainId(chain_id) {} + kChainId(chain_id) { + get_block_by_num_ = [&](::taraxa::EthBlockNumber num) { + return getBlock(response::Value(static_cast(num)), std::nullopt); + }; +} std::shared_ptr Query::getBlock(std::optional&& number, std::optional&& hash) const { + std::optional<::taraxa::EthBlockNumber> block_number; if (number) { - const uint64_t block_number = number->get(); + block_number = number->get(); if (const auto last_block_number = final_chain_->last_block_number(); last_block_number < block_number) { return nullptr; } - if (auto block_header = final_chain_->block_header(block_number)) { - return std::make_shared( - std::make_shared(final_chain_, transaction_manager_, std::move(block_header))); - } - return nullptr; } if (hash) { - if (auto block_number = final_chain_->block_number(dev::h256(hash->get()))) { - if (auto block_header = final_chain_->block_header(block_number)) { - return std::make_shared( - std::make_shared(final_chain_, transaction_manager_, std::move(block_header))); - } - } + block_number = final_chain_->block_number(dev::h256(hash->get())); + } + auto block_header = final_chain_->block_header(block_number); + + auto pbft_block = db_->getPbftBlock(block_header->number); + if (!pbft_block) { + // shouldn't be possible return nullptr; } - return std::make_shared( - std::make_shared(final_chain_, transaction_manager_, final_chain_->block_header())); + return std::make_shared(std::make_shared(final_chain_, transaction_manager_, get_block_by_num_, + pbft_block->getBlockHash(), block_header)); } std::vector> Query::getBlocks(response::Value&& fromArg, std::optional&& toArg) const { std::vector> blocks; - uint64_t start_block_num = fromArg.get(); - uint64_t end_block_num = toArg ? toArg->get() : (start_block_num + Query::kMaxPropagationLimit); + int start_block_num = fromArg.get(); + int end_block_num = toArg ? toArg->get() : (start_block_num + Query::kMaxPropagationLimit); // Incase of reverse order of blocks if (start_block_num > end_block_num) { @@ -68,11 +69,11 @@ std::vector> Query::getBlocks(response::Value&& f end_block_num = tmp; } - if (end_block_num - start_block_num > Query::kMaxPropagationLimit) { + if (end_block_num - start_block_num > static_cast(Query::kMaxPropagationLimit)) { end_block_num = start_block_num + Query::kMaxPropagationLimit; } - const auto last_block_number = final_chain_->last_block_number(); + const int last_block_number = final_chain_->last_block_number(); if (start_block_num > last_block_number) { return blocks; } else if (end_block_num > last_block_number) { @@ -81,9 +82,8 @@ std::vector> Query::getBlocks(response::Value&& f blocks.reserve(end_block_num - start_block_num); - for (uint64_t block_num = start_block_num; block_num <= end_block_num; block_num++) { - blocks.emplace_back(std::make_shared( - std::make_shared(final_chain_, transaction_manager_, final_chain_->block_header(block_num)))); + for (int block_num = start_block_num; block_num <= end_block_num; block_num++) { + blocks.emplace_back(getBlock(response::Value(block_num), std::nullopt)); } return blocks; @@ -92,7 +92,7 @@ std::vector> Query::getBlocks(response::Value&& f std::shared_ptr Query::getTransaction(response::Value&& hashArg) const { if (auto transaction = transaction_manager_->getTransaction(::taraxa::trx_hash_t(hashArg.get()))) { return std::make_shared( - std::make_shared(final_chain_, transaction_manager_, std::move(transaction))); + std::make_shared(final_chain_, transaction_manager_, get_block_by_num_, std::move(transaction))); } return nullptr; } @@ -130,10 +130,11 @@ std::shared_ptr Query::getDagBlock(std::optional(std::make_shared( - std::move(taraxa_dag_block), final_chain_, pbft_manager_, transaction_manager_)) - : nullptr; + if (taraxa_dag_block) { + return std::make_shared(std::make_shared( + std::move(taraxa_dag_block), final_chain_, pbft_manager_, transaction_manager_, get_block_by_num_)); + } + return nullptr; } std::vector> Query::getPeriodDagBlocks( @@ -149,8 +150,8 @@ std::vector> Query::getPeriodDagBlocks( if (dag_blocks.size()) { blocks.reserve(dag_blocks.size()); for (auto block : dag_blocks) { - blocks.emplace_back(std::make_shared( - std::make_shared(std::move(block), final_chain_, pbft_manager_, transaction_manager_))); + blocks.emplace_back(std::make_shared(std::make_shared( + std::move(block), final_chain_, pbft_manager_, transaction_manager_, get_block_by_num_))); } } return blocks; @@ -170,11 +171,11 @@ std::vector> Query::getDagBlocks(std::optional } auto addDagBlocks = [final_chain = final_chain_, pbft_manager = pbft_manager_, - transaction_manager = transaction_manager_](auto taraxa_dag_blocks, - auto& result_dag_blocks) -> size_t { + transaction_manager = transaction_manager_, get_block_by_num = get_block_by_num_]( + auto taraxa_dag_blocks, auto& result_dag_blocks) -> size_t { for (auto& dag_block : taraxa_dag_blocks) { - result_dag_blocks.emplace_back(std::make_shared( - std::make_shared(std::move(dag_block), final_chain, pbft_manager, transaction_manager))); + result_dag_blocks.emplace_back(std::make_shared(std::make_shared( + std::move(dag_block), final_chain, pbft_manager, transaction_manager, get_block_by_num))); } return taraxa_dag_blocks.size(); diff --git a/libraries/core_libs/network/graphql/src/transaction.cpp b/libraries/core_libs/network/graphql/src/transaction.cpp index afe45ce171..b6770ec7ac 100644 --- a/libraries/core_libs/network/graphql/src/transaction.cpp +++ b/libraries/core_libs/network/graphql/src/transaction.cpp @@ -13,9 +13,11 @@ namespace graphql::taraxa { Transaction::Transaction(std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, std::shared_ptr<::taraxa::TransactionManager> trx_manager, + std::function(::taraxa::EthBlockNumber)> get_block_by_num, std::shared_ptr<::taraxa::Transaction> transaction) noexcept : final_chain_(std::move(final_chain)), trx_manager_(std::move(trx_manager)), + get_block_by_num_(std::move(get_block_by_num)), transaction_(std::move(transaction)) {} response::Value Transaction::getHash() const noexcept { return response::Value(transaction_->getHash().toString()); } @@ -70,8 +72,7 @@ std::shared_ptr Transaction::getBlock() const { location_ = final_chain_->transaction_location(transaction_->getHash()); if (!location_) return nullptr; } - return std::make_shared( - std::make_shared(final_chain_, trx_manager_, final_chain_->block_header(location_->blk_n))); + return get_block_by_num_(location_->blk_n); } std::optional Transaction::getStatus() const noexcept { diff --git a/libraries/core_libs/network/graphql/src/types/dag_block.cpp b/libraries/core_libs/network/graphql/src/types/dag_block.cpp index 7124be5fd2..a9b1c78a81 100644 --- a/libraries/core_libs/network/graphql/src/types/dag_block.cpp +++ b/libraries/core_libs/network/graphql/src/types/dag_block.cpp @@ -10,11 +10,13 @@ namespace graphql::taraxa { DagBlock::DagBlock(std::shared_ptr<::taraxa::DagBlock> dag_block, std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, std::shared_ptr<::taraxa::PbftManager> pbft_manager, - std::shared_ptr<::taraxa::TransactionManager> transaction_manager) noexcept + std::shared_ptr<::taraxa::TransactionManager> transaction_manager, + std::function(::taraxa::EthBlockNumber)> get_block_by_num) noexcept : dag_block_(std::move(dag_block)), final_chain_(std::move(final_chain)), pbft_manager_(std::move(pbft_manager)), - transaction_manager_(std::move(transaction_manager)) {} + transaction_manager_(std::move(transaction_manager)), + get_block_by_num_(get_block_by_num) {} response::Value DagBlock::getHash() const noexcept { return response::Value(dag_block_->getHash().toString()); } @@ -72,7 +74,7 @@ std::optional>> DagBlock::getTr std::vector> transactions_result; for (const auto& trx_hash : dag_block_->getTrxs()) { transactions_result.push_back(std::make_shared(std::make_shared( - final_chain_, transaction_manager_, transaction_manager_->getTransaction(trx_hash)))); + final_chain_, transaction_manager_, get_block_by_num_, transaction_manager_->getTransaction(trx_hash)))); } return transactions_result; From b4d91dffad70f4a395569e8deb61d1d9a6cfe682 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 20 Jul 2023 15:14:58 +0000 Subject: [PATCH 097/134] chore(deps): bump aiohttp from 3.7.4.post0 to 3.8.5 in /for_devs Bumps [aiohttp](https://github.com/aio-libs/aiohttp) from 3.7.4.post0 to 3.8.5. - [Release notes](https://github.com/aio-libs/aiohttp/releases) - [Changelog](https://github.com/aio-libs/aiohttp/blob/v3.8.5/CHANGES.rst) - [Commits](https://github.com/aio-libs/aiohttp/compare/v3.7.4.post0...v3.8.5) --- updated-dependencies: - dependency-name: aiohttp dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- for_devs/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/for_devs/requirements.txt b/for_devs/requirements.txt index fb69bedb91..d8db5758f9 100644 --- a/for_devs/requirements.txt +++ b/for_devs/requirements.txt @@ -1,4 +1,4 @@ -aiohttp==3.7.4.post0 +aiohttp==3.8.5 async-timeout==3.0.1 attrs==21.2.0 base58==2.1.0 From 1e4501a17c741d32dc3b9c677ab2b9e5ab2d490b Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Mon, 24 Jul 2023 15:06:01 +0200 Subject: [PATCH 098/134] chore: better approach on deleting node entries --- libraries/aleth/libp2p/NodeTable.cpp | 18 +++++++----------- libraries/aleth/libp2p/NodeTable.h | 4 +--- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/libraries/aleth/libp2p/NodeTable.cpp b/libraries/aleth/libp2p/NodeTable.cpp index 99b82f85ae..7a6f5cd51e 100644 --- a/libraries/aleth/libp2p/NodeTable.cpp +++ b/libraries/aleth/libp2p/NodeTable.cpp @@ -461,14 +461,6 @@ shared_ptr NodeTable::handlePong(bi::udp::endpoint const& _from, Disc } m_sentPings.erase(_from); - // Remove any other sent pings which was sent to other endpoints for the same nodeID - for (auto it = m_sentPings.begin(); it != m_sentPings.end();) { - if (nodeValidation.nodeID == it->second.nodeID) { - it = m_sentPings.erase(it); - } else { - ++it; - } - } // update our external endpoint address and UDP port if (m_endpointTracker.addEndpointStatement(_from, pong.destination) >= c_minEndpointTrackStatements) { @@ -513,7 +505,8 @@ shared_ptr NodeTable::handleNeighbours(bi::udp::endpoint const& _from return true; }); if (!expected) { - LOG(m_logger) << "Dropping unsolicited neighbours packet from " << _packet.sourceid << "@" << _from.address(); + LOG(m_logger) << "Dropping unsolicited neighbours packet from " << _packet.sourceid << "@" << _from.address() << ":" + << _from.port(); return sourceNodeEntry; } @@ -703,8 +696,11 @@ void NodeTable::doHandleTimeouts() { for (auto it = m_sentPings.begin(); it != m_sentPings.end();) { if (chrono::steady_clock::now() > it->second.pingSentTime + m_requestTimeToLive) { if (auto node = nodeEntry(it->second.nodeID)) { - dropNode(std::move(node)); - + if (node->lastPongReceivedTime < RLPXDatagramFace::secondsSinceEpoch() - m_requestTimeToLive.count()) { + dropNode(std::move(node)); + } else { + LOG(m_logger) << "Not dropping node " << it->second.nodeID << " " << it->first << " as it was updated"; + } // save the replacement node that should be activated if (it->second.replacementNodeEntry) nodesToActivate.emplace_back(std::move(it->second.replacementNodeEntry)); } diff --git a/libraries/aleth/libp2p/NodeTable.h b/libraries/aleth/libp2p/NodeTable.h index ae062d97f3..5d6f97f480 100644 --- a/libraries/aleth/libp2p/NodeTable.h +++ b/libraries/aleth/libp2p/NodeTable.h @@ -316,8 +316,6 @@ class NodeTable : UDPSocketEvents { // Remove old records in m_endpointTracker. void doEndpointTracking(); - // Useful only for tests. - void setRequestTimeToLive(std::chrono::seconds const& _time) { m_requestTimeToLive = _time; } uint32_t nextRequestExpirationTime() const { return RLPXDatagramFace::futureFromEpoch(m_requestTimeToLive); } /// Determines if a node with the supplied endpoint is allowed to participate @@ -374,7 +372,7 @@ class NodeTable : UDPSocketEvents { std::unordered_map m_sentPings; // Expiration time of sent discovery packets. - std::chrono::seconds m_requestTimeToLive; + const std::chrono::seconds m_requestTimeToLive; mutable Logger m_logger{createLogger(VerbosityDebug, "discov")}; From 1be31560645631d7f84ec682e36fc3e2cf20817d Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Mon, 24 Jul 2023 15:06:56 +0200 Subject: [PATCH 099/134] Revert "chore: better approach on deleting node entries" This reverts commit 1e4501a17c741d32dc3b9c677ab2b9e5ab2d490b. --- libraries/aleth/libp2p/NodeTable.cpp | 18 +++++++++++------- libraries/aleth/libp2p/NodeTable.h | 4 +++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/libraries/aleth/libp2p/NodeTable.cpp b/libraries/aleth/libp2p/NodeTable.cpp index 7a6f5cd51e..99b82f85ae 100644 --- a/libraries/aleth/libp2p/NodeTable.cpp +++ b/libraries/aleth/libp2p/NodeTable.cpp @@ -461,6 +461,14 @@ shared_ptr NodeTable::handlePong(bi::udp::endpoint const& _from, Disc } m_sentPings.erase(_from); + // Remove any other sent pings which was sent to other endpoints for the same nodeID + for (auto it = m_sentPings.begin(); it != m_sentPings.end();) { + if (nodeValidation.nodeID == it->second.nodeID) { + it = m_sentPings.erase(it); + } else { + ++it; + } + } // update our external endpoint address and UDP port if (m_endpointTracker.addEndpointStatement(_from, pong.destination) >= c_minEndpointTrackStatements) { @@ -505,8 +513,7 @@ shared_ptr NodeTable::handleNeighbours(bi::udp::endpoint const& _from return true; }); if (!expected) { - LOG(m_logger) << "Dropping unsolicited neighbours packet from " << _packet.sourceid << "@" << _from.address() << ":" - << _from.port(); + LOG(m_logger) << "Dropping unsolicited neighbours packet from " << _packet.sourceid << "@" << _from.address(); return sourceNodeEntry; } @@ -696,11 +703,8 @@ void NodeTable::doHandleTimeouts() { for (auto it = m_sentPings.begin(); it != m_sentPings.end();) { if (chrono::steady_clock::now() > it->second.pingSentTime + m_requestTimeToLive) { if (auto node = nodeEntry(it->second.nodeID)) { - if (node->lastPongReceivedTime < RLPXDatagramFace::secondsSinceEpoch() - m_requestTimeToLive.count()) { - dropNode(std::move(node)); - } else { - LOG(m_logger) << "Not dropping node " << it->second.nodeID << " " << it->first << " as it was updated"; - } + dropNode(std::move(node)); + // save the replacement node that should be activated if (it->second.replacementNodeEntry) nodesToActivate.emplace_back(std::move(it->second.replacementNodeEntry)); } diff --git a/libraries/aleth/libp2p/NodeTable.h b/libraries/aleth/libp2p/NodeTable.h index 5d6f97f480..ae062d97f3 100644 --- a/libraries/aleth/libp2p/NodeTable.h +++ b/libraries/aleth/libp2p/NodeTable.h @@ -316,6 +316,8 @@ class NodeTable : UDPSocketEvents { // Remove old records in m_endpointTracker. void doEndpointTracking(); + // Useful only for tests. + void setRequestTimeToLive(std::chrono::seconds const& _time) { m_requestTimeToLive = _time; } uint32_t nextRequestExpirationTime() const { return RLPXDatagramFace::futureFromEpoch(m_requestTimeToLive); } /// Determines if a node with the supplied endpoint is allowed to participate @@ -372,7 +374,7 @@ class NodeTable : UDPSocketEvents { std::unordered_map m_sentPings; // Expiration time of sent discovery packets. - const std::chrono::seconds m_requestTimeToLive; + std::chrono::seconds m_requestTimeToLive; mutable Logger m_logger{createLogger(VerbosityDebug, "discov")}; From ba2cd791e651fbfc1fba0790db2cbcc6f31da518 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Mon, 24 Jul 2023 15:06:01 +0200 Subject: [PATCH 100/134] chore: better approach on deleting node entries --- libraries/aleth/libp2p/NodeTable.cpp | 18 +++++++----------- libraries/aleth/libp2p/NodeTable.h | 4 +--- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/libraries/aleth/libp2p/NodeTable.cpp b/libraries/aleth/libp2p/NodeTable.cpp index 99b82f85ae..7a6f5cd51e 100644 --- a/libraries/aleth/libp2p/NodeTable.cpp +++ b/libraries/aleth/libp2p/NodeTable.cpp @@ -461,14 +461,6 @@ shared_ptr NodeTable::handlePong(bi::udp::endpoint const& _from, Disc } m_sentPings.erase(_from); - // Remove any other sent pings which was sent to other endpoints for the same nodeID - for (auto it = m_sentPings.begin(); it != m_sentPings.end();) { - if (nodeValidation.nodeID == it->second.nodeID) { - it = m_sentPings.erase(it); - } else { - ++it; - } - } // update our external endpoint address and UDP port if (m_endpointTracker.addEndpointStatement(_from, pong.destination) >= c_minEndpointTrackStatements) { @@ -513,7 +505,8 @@ shared_ptr NodeTable::handleNeighbours(bi::udp::endpoint const& _from return true; }); if (!expected) { - LOG(m_logger) << "Dropping unsolicited neighbours packet from " << _packet.sourceid << "@" << _from.address(); + LOG(m_logger) << "Dropping unsolicited neighbours packet from " << _packet.sourceid << "@" << _from.address() << ":" + << _from.port(); return sourceNodeEntry; } @@ -703,8 +696,11 @@ void NodeTable::doHandleTimeouts() { for (auto it = m_sentPings.begin(); it != m_sentPings.end();) { if (chrono::steady_clock::now() > it->second.pingSentTime + m_requestTimeToLive) { if (auto node = nodeEntry(it->second.nodeID)) { - dropNode(std::move(node)); - + if (node->lastPongReceivedTime < RLPXDatagramFace::secondsSinceEpoch() - m_requestTimeToLive.count()) { + dropNode(std::move(node)); + } else { + LOG(m_logger) << "Not dropping node " << it->second.nodeID << " " << it->first << " as it was updated"; + } // save the replacement node that should be activated if (it->second.replacementNodeEntry) nodesToActivate.emplace_back(std::move(it->second.replacementNodeEntry)); } diff --git a/libraries/aleth/libp2p/NodeTable.h b/libraries/aleth/libp2p/NodeTable.h index ae062d97f3..5d6f97f480 100644 --- a/libraries/aleth/libp2p/NodeTable.h +++ b/libraries/aleth/libp2p/NodeTable.h @@ -316,8 +316,6 @@ class NodeTable : UDPSocketEvents { // Remove old records in m_endpointTracker. void doEndpointTracking(); - // Useful only for tests. - void setRequestTimeToLive(std::chrono::seconds const& _time) { m_requestTimeToLive = _time; } uint32_t nextRequestExpirationTime() const { return RLPXDatagramFace::futureFromEpoch(m_requestTimeToLive); } /// Determines if a node with the supplied endpoint is allowed to participate @@ -374,7 +372,7 @@ class NodeTable : UDPSocketEvents { std::unordered_map m_sentPings; // Expiration time of sent discovery packets. - std::chrono::seconds m_requestTimeToLive; + const std::chrono::seconds m_requestTimeToLive; mutable Logger m_logger{createLogger(VerbosityDebug, "discov")}; From ad84653100d77b279e255e5d2c0c39fd822f4012 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Tue, 25 Jul 2023 14:03:04 +0200 Subject: [PATCH 101/134] feat: handle batch requests via ws interface --- .../network/rpc/jsonrpc_ws_server.cpp | 97 +++++++++++-------- .../network/rpc/jsonrpc_ws_server.hpp | 4 + 2 files changed, 59 insertions(+), 42 deletions(-) diff --git a/libraries/core_libs/network/rpc/jsonrpc_ws_server.cpp b/libraries/core_libs/network/rpc/jsonrpc_ws_server.cpp index 269d196ba6..6cf5dd020d 100644 --- a/libraries/core_libs/network/rpc/jsonrpc_ws_server.cpp +++ b/libraries/core_libs/network/rpc/jsonrpc_ws_server.cpp @@ -25,55 +25,68 @@ std::string JsonRpcWsSession::processRequest(const std::string_view &request) { return {}; } - auto id = json.get("id", 0); - Json::Value json_response; - auto method = json.get("method", ""); - std::string response; - if (method == "eth_subscribe") { - auto params = json.get("params", Json::Value(Json::Value(Json::arrayValue))); - json_response["id"] = id; - json_response["jsonrpc"] = "2.0"; - subscription_id_++; - if (params.size() > 0) { - if (params[0].asString() == "newHeads") { - new_heads_subscription_ = subscription_id_; - } else if (params[0].asString() == "newPendingTransactions") { - new_transactions_subscription_ = subscription_id_; - } else if (params[0].asString() == "newDagBlocks") { - new_dag_blocks_subscription_ = subscription_id_; - } else if (params[0].asString() == "newDagBlocksFinalized") { - new_dag_block_finalized_subscription_ = subscription_id_; - } else if (params[0].asString() == "newPbftBlocks") { - new_pbft_block_executed_subscription_ = subscription_id_; - } + // Check for Batch requests + if (!json.isArray()) { + if (const auto method = json.get("method", ""); method == "eth_subscribe") { + return handleSubscription(json); } - json_response["result"] = dev::toJS(subscription_id_); - response = util::to_string(json_response); - LOG(log_tr_) << "WS WRITE " << response.c_str(); - } else { - auto ws_server = ws_server_.lock(); - if (ws_server) { - auto handler = ws_server->GetHandler(); - if (handler != NULL) { - try { - LOG(log_tr_) << "WS Read: " << static_cast(buffer_.data().data()); - handler->HandleRequest(static_cast(buffer_.data().data()), response); - } catch (std::exception const &e) { - LOG(log_er_) << "Exception " << e.what(); - auto &res_json_error = json_response["error"] = Json::Value(Json::objectValue); - res_json_error["code"] = jsonrpc::Errors::ERROR_RPC_INTERNAL_ERROR; - res_json_error["message"] = e.what(); - json_response["id"] = id; - json_response["jsonrpc"] = "2.0"; - response = util::to_string(json_response); - } - LOG(log_tr_) << "WS Write: " << response; + } + + return handleRequest(json); +} + +std::string JsonRpcWsSession::handleRequest(const Json::Value &req) { + std::string response; + auto ws_server = ws_server_.lock(); + if (ws_server) { + auto handler = ws_server->GetHandler(); + if (handler != NULL) { + try { + handler->HandleRequest(util::to_string(req), response); + } catch (std::exception const &e) { + LOG(log_er_) << "Exception " << e.what(); + Json::Value json_response; + auto &res_json_error = json_response["error"] = Json::Value(Json::objectValue); + res_json_error["code"] = jsonrpc::Errors::ERROR_RPC_INTERNAL_ERROR; + res_json_error["message"] = e.what(); + json_response["id"] = req.get("id", 0); + json_response["jsonrpc"] = "2.0"; + return util::to_string(json_response); } } } return response; } +std::string JsonRpcWsSession::handleSubscription(const Json::Value &req) { + Json::Value json_response; + + const auto params = req.get("params", Json::Value(Json::Value(Json::arrayValue))); + + json_response["id"] = req.get("id", 0); + ; + json_response["jsonrpc"] = "2.0"; + subscription_id_++; + + if (params.size() > 0) { + if (params[0].asString() == "newHeads") { + new_heads_subscription_ = subscription_id_; + } else if (params[0].asString() == "newPendingTransactions") { + new_transactions_subscription_ = subscription_id_; + } else if (params[0].asString() == "newDagBlocks") { + new_dag_blocks_subscription_ = subscription_id_; + } else if (params[0].asString() == "newDagBlocksFinalized") { + new_dag_block_finalized_subscription_ = subscription_id_; + } else if (params[0].asString() == "newPbftBlocks") { + new_pbft_block_executed_subscription_ = subscription_id_; + } + } + + json_response["result"] = dev::toJS(subscription_id_); + + return util::to_string(json_response); +} + std::shared_ptr JsonRpcWsServer::createSession(tcp::socket &&socket) { return std::make_shared(std::move(socket), node_addr_, shared_from_this()); } diff --git a/libraries/core_libs/network/rpc/jsonrpc_ws_server.hpp b/libraries/core_libs/network/rpc/jsonrpc_ws_server.hpp index 37b220a0cc..455d8d9221 100644 --- a/libraries/core_libs/network/rpc/jsonrpc_ws_server.hpp +++ b/libraries/core_libs/network/rpc/jsonrpc_ws_server.hpp @@ -8,6 +8,10 @@ class JsonRpcWsSession final : public WsSession { public: using WsSession::WsSession; std::string processRequest(const std::string_view& request) override; + + private: + std::string handleRequest(const Json::Value& req); + std::string handleSubscription(const Json::Value& req); }; class JsonRpcWsServer final : public WsServer { From 6f87e166db2cda0481288aa8cf75d5c1ffbac951 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Tue, 8 Aug 2023 13:25:42 +0200 Subject: [PATCH 102/134] chore: improve light node history delete --- libraries/core_libs/storage/src/storage.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 540b9bf8ec..9337b32c9e 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -580,10 +580,11 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level for (auto period = start_period; period < end_period; period++) { // Find transactions included in the old blocks and delete data related to these transactions to free disk // space - const auto& transactions = getPeriodTransactions(period); - if (transactions.has_value()) { - for (const auto& trx : *transactions) { - remove(write_batch, Columns::final_chain_receipt_by_trx_hash, trx->getHash()); + const auto& dag_blocks = getFinalizedDagBlockByPeriod(period); + + for (const auto& dag_block : dag_blocks) { + for (const auto& trx_hash : dag_block->getTrxs()) { + remove(write_batch, Columns::final_chain_receipt_by_trx_hash, trx_hash); } } if ((period - start_period + 1) % max_batch_delete == 0) { @@ -591,6 +592,7 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level write_batch = createWriteBatch(); } } + commitWriteBatch(write_batch); db_->DeleteRange(write_options_, handle(Columns::period_data), start_slice, end_slice); From 6722e4e2b71429c99baf03d3650e5891732f1f57 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Wed, 16 Aug 2023 11:59:26 +0200 Subject: [PATCH 103/134] chore: async clear light node history --- .../consensus/src/dag/dag_manager.cpp | 2 - .../storage/include/storage/storage.hpp | 1 + libraries/core_libs/storage/src/storage.cpp | 135 ++++++++++-------- 3 files changed, 74 insertions(+), 64 deletions(-) diff --git a/libraries/core_libs/consensus/src/dag/dag_manager.cpp b/libraries/core_libs/consensus/src/dag/dag_manager.cpp index ed57baa4c2..491a4177e0 100644 --- a/libraries/core_libs/consensus/src/dag/dag_manager.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_manager.cpp @@ -290,7 +290,6 @@ void DagManager::clearLightNodeHistory(bool initial) { bool period_over_history_condition = period_ > light_node_history_; if (((period_ % clear_interval == 0) || initial) && period_over_history_condition && dag_expiry_level_condition) { // This will happen at most once a day so log a silent log - LOG(log_si_) << "Clear light node history"; const auto proposal_period = db_->getProposalPeriodForDagLevel(dag_expiry_level_ - max_levels_per_period_ - 1); assert(proposal_period); @@ -307,7 +306,6 @@ void DagManager::clearLightNodeHistory(bool initial) { dag_level_to_keep = dag_expiry_level_ - max_levels_per_period_; } db_->clearPeriodDataHistory(end, dag_level_to_keep, initial); - LOG(log_si_) << "Clear light node history completed"; } } diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index ce76655636..b4833464dd 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -145,6 +145,7 @@ class DbStorage : public std::enable_shared_from_this { std::atomic snapshots_enabled_ = true; const uint32_t kDbSnapshotsMaxCount = 0; std::set snapshots_; + std::unique_ptr clear_history_thread_; uint32_t kMajorVersion_; bool major_version_changed_ = false; diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 9337b32c9e..0ab7bd0097 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -374,6 +374,7 @@ std::optional DbStorage::getGenesisHash() { } DbStorage::~DbStorage() { + if (clear_history_thread_) clear_history_thread_->join(); for (auto cf : handles_) { checkStatus(db_->DestroyColumnFamilyHandle(cf)); } @@ -564,75 +565,85 @@ std::optional DbStorage::getParamsChangeForPeriod(PbftPer } void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level_to_keep, bool initial) { + LOG(log_si_) << "Clear light node history"; auto it = std::unique_ptr(db_->NewIterator(read_options_, handle(Columns::period_data))); // Find the first non-deleted period it->SeekToFirst(); if (it->Valid()) { uint64_t start_period; memcpy(&start_period, it->key().data(), sizeof(uint64_t)); - if (start_period < end_period) { - auto write_batch = createWriteBatch(); - // Delete up to max 10000 period at once - const uint32_t max_batch_delete = 10000; - auto start_slice = toSlice(start_period); - auto end_slice = toSlice(end_period); - - for (auto period = start_period; period < end_period; period++) { - // Find transactions included in the old blocks and delete data related to these transactions to free disk - // space - const auto& dag_blocks = getFinalizedDagBlockByPeriod(period); - - for (const auto& dag_block : dag_blocks) { - for (const auto& trx_hash : dag_block->getTrxs()) { - remove(write_batch, Columns::final_chain_receipt_by_trx_hash, trx_hash); - } - } - if ((period - start_period + 1) % max_batch_delete == 0) { - commitWriteBatch(write_batch); - write_batch = createWriteBatch(); - } - } - - commitWriteBatch(write_batch); - db_->DeleteRange(write_options_, handle(Columns::period_data), start_slice, end_slice); - - if (initial) { - // Deletion alone does not guarantee that the disk space is freed, CompactRange actually compacts - // the data in the database and free disk space - db_->CompactRange({}, handle(Columns::period_data), &start_slice, &end_slice); - db_->CompactRange({}, handle(Columns::final_chain_receipt_by_trx_hash), nullptr, nullptr); - } - } - } - - it = std::unique_ptr(db_->NewIterator(read_options_, handle(Columns::dag_blocks_level))); - it->SeekToFirst(); - if (it->Valid()) { - uint64_t start_level; - memcpy(&start_level, it->key().data(), sizeof(uint64_t)); - if (start_level < dag_level_to_keep) { - auto write_batch = createWriteBatch(); - // Delete up to max 10000 period at once - const uint32_t max_batch_delete = 10000; - auto start_slice = toSlice(start_level); - auto end_slice = toSlice(dag_level_to_keep - 1); - for (auto level = start_level; level < dag_level_to_keep; level++) { - // Find old dag blocks and delete data related to these blocks to free disk space - auto dag_block_hashes = getBlocksByLevel(start_level); - for (auto dag_block_hash : dag_block_hashes) { - remove(write_batch, Columns::dag_block_period, dag_block_hash); - } - if ((dag_level_to_keep - start_level + 1) % max_batch_delete == 0) { - commitWriteBatch(write_batch); - write_batch = createWriteBatch(); - } - } - commitWriteBatch(write_batch); - db_->DeleteRange(write_options_, handle(Columns::dag_blocks_level), start_slice, end_slice); - + it = std::unique_ptr(db_->NewIterator(read_options_, handle(Columns::dag_blocks_level))); + it->SeekToFirst(); + if (it->Valid()) { + uint64_t start_level; + memcpy(&start_level, it->key().data(), sizeof(uint64_t)); + if (clear_history_thread_) clear_history_thread_->join(); + clear_history_thread_ = + std::make_unique([this, start_period, start_level, initial, dag_level_to_keep, end_period]() { + if (start_period < end_period) { + auto write_batch = createWriteBatch(); + // Delete up to max 10000 period at once + const uint32_t max_batch_delete = 10000; + auto start_slice = toSlice(start_period); + auto end_slice = toSlice(end_period); + + for (auto period = start_period; period < end_period; period++) { + // Find transactions included in the old blocks and delete data related to these transactions to free + // disk space + const auto& dag_blocks = getFinalizedDagBlockByPeriod(period); + + for (const auto& dag_block : dag_blocks) { + for (const auto& trx_hash : dag_block->getTrxs()) { + remove(write_batch, Columns::final_chain_receipt_by_trx_hash, trx_hash); + } + } + if ((period - start_period + 1) % max_batch_delete == 0) { + commitWriteBatch(write_batch); + write_batch = createWriteBatch(); + } + } + + commitWriteBatch(write_batch); + db_->DeleteRange(write_options_, handle(Columns::period_data), start_slice, end_slice); + + if (initial) { + // Deletion alone does not guarantee that the disk space is freed, CompactRange actually compacts + // the data in the database and free disk space + db_->CompactRange({}, handle(Columns::period_data), &start_slice, &end_slice); + db_->CompactRange({}, handle(Columns::final_chain_receipt_by_trx_hash), nullptr, nullptr); + } + } + + if (start_level < dag_level_to_keep) { + auto write_batch = createWriteBatch(); + // Delete up to max 10000 period at once + const uint32_t max_batch_delete = 10000; + auto start_slice = toSlice(start_level); + auto end_slice = toSlice(dag_level_to_keep - 1); + for (auto level = start_level; level < dag_level_to_keep; level++) { + // Find old dag blocks and delete data related to these blocks to free disk space + auto dag_block_hashes = getBlocksByLevel(start_level); + for (auto dag_block_hash : dag_block_hashes) { + remove(write_batch, Columns::dag_block_period, dag_block_hash); + } + if ((dag_level_to_keep - start_level + 1) % max_batch_delete == 0) { + commitWriteBatch(write_batch); + write_batch = createWriteBatch(); + } + } + commitWriteBatch(write_batch); + db_->DeleteRange(write_options_, handle(Columns::dag_blocks_level), start_slice, end_slice); + + if (initial) { + db_->CompactRange({}, handle(Columns::dag_block_period), nullptr, nullptr); + db_->CompactRange({}, handle(Columns::dag_blocks_level), nullptr, nullptr); + } + LOG(log_si_) << "Clear light node history completed"; + } + }); if (initial) { - db_->CompactRange({}, handle(Columns::dag_block_period), nullptr, nullptr); - db_->CompactRange({}, handle(Columns::dag_blocks_level), nullptr, nullptr); + clear_history_thread_->join(); + clear_history_thread_ = nullptr; } } } From 5cbb8ae0d51e0a570b873505d4585b1af4b1fcad Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Wed, 16 Aug 2023 13:06:36 +0200 Subject: [PATCH 104/134] chore: async clear light node history --- .../core_libs/storage/include/storage/storage.hpp | 2 +- libraries/core_libs/storage/src/storage.cpp | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index b4833464dd..28e2190aea 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -145,7 +145,7 @@ class DbStorage : public std::enable_shared_from_this { std::atomic snapshots_enabled_ = true; const uint32_t kDbSnapshotsMaxCount = 0; std::set snapshots_; - std::unique_ptr clear_history_thread_; + std::unique_ptr> clear_history_future_; uint32_t kMajorVersion_; bool major_version_changed_ = false; diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 0ab7bd0097..9a39caba58 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -374,7 +374,7 @@ std::optional DbStorage::getGenesisHash() { } DbStorage::~DbStorage() { - if (clear_history_thread_) clear_history_thread_->join(); + if (clear_history_future_) clear_history_future_->wait(); for (auto cf : handles_) { checkStatus(db_->DestroyColumnFamilyHandle(cf)); } @@ -577,9 +577,9 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level if (it->Valid()) { uint64_t start_level; memcpy(&start_level, it->key().data(), sizeof(uint64_t)); - if (clear_history_thread_) clear_history_thread_->join(); - clear_history_thread_ = - std::make_unique([this, start_period, start_level, initial, dag_level_to_keep, end_period]() { + if (clear_history_future_) clear_history_future_->wait(); + clear_history_future_ = std::make_unique>( + std::async(std::launch::async, [this, start_period, start_level, initial, dag_level_to_keep, end_period]() { if (start_period < end_period) { auto write_batch = createWriteBatch(); // Delete up to max 10000 period at once @@ -640,10 +640,10 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level } LOG(log_si_) << "Clear light node history completed"; } - }); + })); if (initial) { - clear_history_thread_->join(); - clear_history_thread_ = nullptr; + clear_history_future_->wait(); + clear_history_future_ = nullptr; } } } From b33eb08b5a4b0ee3de7959c57a36572197c6a96b Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Wed, 16 Aug 2023 14:59:29 +0200 Subject: [PATCH 105/134] chore: add additional network logging --- .../network/tarcap/shared_states/peers_state.hpp | 2 +- .../include/network/tarcap/stats/node_stats.hpp | 3 ++- .../network/include/network/tarcap/taraxa_peer.hpp | 3 ++- libraries/core_libs/network/src/network.cpp | 14 +++++++++++++- .../src/tarcap/shared_states/peers_state.cpp | 5 +++-- .../network/src/tarcap/stats/node_stats.cpp | 8 ++++++-- .../network/src/tarcap/taraxa_capability.cpp | 2 +- .../core_libs/network/src/tarcap/taraxa_peer.cpp | 5 +++-- programs/taraxa-bootnode/main.cpp | 10 ++++++++++ tests/tarcap_threadpool_test.cpp | 4 ++-- 10 files changed, 43 insertions(+), 13 deletions(-) diff --git a/libraries/core_libs/network/include/network/tarcap/shared_states/peers_state.hpp b/libraries/core_libs/network/include/network/tarcap/shared_states/peers_state.hpp index 9512c8ce0f..ec89429fdc 100644 --- a/libraries/core_libs/network/include/network/tarcap/shared_states/peers_state.hpp +++ b/libraries/core_libs/network/include/network/tarcap/shared_states/peers_state.hpp @@ -38,7 +38,7 @@ class PeersState { PeersMap getAllPeers() const; std::vector getAllPendingPeersIDs() const; size_t getPeersCount() const; - std::shared_ptr addPendingPeer(const dev::p2p::NodeID& node_id); + std::shared_ptr addPendingPeer(const dev::p2p::NodeID& node_id, const std::string& address); void erasePeer(const dev::p2p::NodeID& node_id); std::shared_ptr setPeerAsReadyToSendMessages(dev::p2p::NodeID const& node_id, std::shared_ptr peer); diff --git a/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp b/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp index 002e75073c..c6feffb50e 100644 --- a/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp +++ b/libraries/core_libs/network/include/network/tarcap/stats/node_stats.hpp @@ -31,7 +31,8 @@ class NodeStats { std::shared_ptr packets_stats, std::shared_ptr thread_pool, const addr_t& node_addr); - void logNodeStats(const std::vector>& all_peers, size_t nodes_count); + void logNodeStats(const std::vector>& all_peers, + const std::vector& nodes); uint64_t syncTimeSeconds() const; Json::Value getStatus( std::map> peers) const; diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_peer.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_peer.hpp index d048065938..ca8983973a 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_peer.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_peer.hpp @@ -13,7 +13,7 @@ namespace taraxa::network::tarcap { class TaraxaPeer : public boost::noncopyable { public: TaraxaPeer(); - TaraxaPeer(const dev::p2p::NodeID& id, size_t transaction_pool_size); + TaraxaPeer(const dev::p2p::NodeID& id, size_t transaction_pool_size, std::string address); /** * @brief Mark dag block as known @@ -109,6 +109,7 @@ class TaraxaPeer : public boost::noncopyable { std::atomic_uint64_t peer_requested_dag_syncing_time_ = 0; std::atomic_bool peer_light_node = false; std::atomic peer_light_node_history = 0; + std::string address_; // Mutex used to prevent race condition between dag syncing and gossiping mutable boost::shared_mutex mutex_for_sending_dag_blocks_; diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index f4a0ac8ab9..f7ac34d6df 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -216,7 +216,19 @@ void Network::registerPeriodicEvents(const std::shared_ptr &pbft_mg // SUMMARY log const auto node_stats_log_interval = 5 * 6 * lambda_ms; auto summaryLog = [getAllPeers, node_stats = node_stats_, host = host_]() { - node_stats->logNodeStats(getAllPeers(), host->getNodeCount()); + std::vector nodes_addresses; + auto nodes = host->getNodes(); + nodes_addresses.reserve(nodes.size()); + for (auto node : nodes) { + auto endpoint = node.endpoint(); + if (endpoint.tcpPort() == endpoint.udpPort()) + nodes_addresses.push_back(node.id().abridged() + ":" + endpoint.address().to_string() + ":" + + std::to_string(endpoint.tcpPort())); + else + nodes_addresses.push_back(node.id().abridged() + ":" + endpoint.address().to_string() + ":" + + std::to_string(endpoint.tcpPort()) + ":" + std::to_string(endpoint.udpPort())); + } + node_stats->logNodeStats(getAllPeers(), nodes_addresses); }; periodic_events_tp_.post_loop({node_stats_log_interval}, summaryLog); } diff --git a/libraries/core_libs/network/src/tarcap/shared_states/peers_state.cpp b/libraries/core_libs/network/src/tarcap/shared_states/peers_state.cpp index 73ce22cb71..22f3d7c08d 100644 --- a/libraries/core_libs/network/src/tarcap/shared_states/peers_state.cpp +++ b/libraries/core_libs/network/src/tarcap/shared_states/peers_state.cpp @@ -71,9 +71,10 @@ PeersState::PeersMap PeersState::getAllPeers() const { return peers_; } -std::shared_ptr PeersState::addPendingPeer(const dev::p2p::NodeID& node_id) { +std::shared_ptr PeersState::addPendingPeer(const dev::p2p::NodeID& node_id, const std::string& address) { std::unique_lock lock(peers_mutex_); - auto ret = pending_peers_.emplace(node_id, std::make_shared(node_id, kConf.transactions_pool_size)); + auto ret = + pending_peers_.emplace(node_id, std::make_shared(node_id, kConf.transactions_pool_size, address)); if (!ret.second) { // LOG(log_er_) << "Peer " << node_id.abridged() << " is already in pending peers list"; } diff --git a/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp b/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp index 072554a4e6..f9e1a175b3 100644 --- a/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp +++ b/libraries/core_libs/network/src/tarcap/stats/node_stats.cpp @@ -33,7 +33,7 @@ NodeStats::NodeStats(std::shared_ptr pbft_syncing_state, std:: uint64_t NodeStats::syncTimeSeconds() const { return syncing_duration_seconds; } void NodeStats::logNodeStats(const std::vector> &all_peers, - size_t nodes_count) { + const std::vector &nodes) { bool is_pbft_syncing = pbft_syncing_state_->isPbftSyncing(); dev::p2p::NodeID max_pbft_round_node_id; @@ -45,8 +45,9 @@ void NodeStats::logNodeStats(const std::vectorpbft_chain_size_ > peer_max_pbft_chain_size) { @@ -67,6 +68,7 @@ void NodeStats::logNodeStats(const std::vectorgetId().abridged() + " "; + connected_peers_str_with_ip += peer->getId().abridged() + ":" + peer->address_ + " "; } // Local dag info... @@ -132,7 +134,9 @@ void NodeStats::logNodeStats(const std::vector session, u256 return; } - peers_state_->addPendingPeer(node_id); + peers_state_->addPendingPeer(node_id, session_p->info().host + ":" + std::to_string(session_p->info().port)); LOG(log_nf_) << "Node " << node_id << " connected"; auto status_packet_handler = packets_handlers_->getSpecificHandler(); diff --git a/libraries/core_libs/network/src/tarcap/taraxa_peer.cpp b/libraries/core_libs/network/src/tarcap/taraxa_peer.cpp index bd173d0330..1bf9a8b9a3 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_peer.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_peer.cpp @@ -8,8 +8,9 @@ TaraxaPeer::TaraxaPeer() known_pbft_blocks_(10000, 1000, 10), known_votes_(10000, 1000, 10) {} -TaraxaPeer::TaraxaPeer(const dev::p2p::NodeID& id, size_t transaction_pool_size) - : id_(id), +TaraxaPeer::TaraxaPeer(const dev::p2p::NodeID& id, size_t transaction_pool_size, std::string address) + : address_(address), + id_(id), known_dag_blocks_(10000, 1000, 10), known_transactions_(transaction_pool_size * 1.2, transaction_pool_size / 10, 10), known_pbft_blocks_(10000, 1000, 10), diff --git a/programs/taraxa-bootnode/main.cpp b/programs/taraxa-bootnode/main.cpp index 2bd427fabd..0c391ef532 100644 --- a/programs/taraxa-bootnode/main.cpp +++ b/programs/taraxa-bootnode/main.cpp @@ -53,6 +53,16 @@ void setupLogging(dev::LoggingOptions const& options) { sink->set_formatter(boost::log::aux::acquire_formatter("%Channel% [%TimeStamp%] %SeverityStr%: %Message%")); boost::log::core::get()->add_sink(sink); + + auto file_sink = boost::log::add_file_log( + boost::log::keywords::file_name = "boot-node.log", boost::log::keywords::rotation_size = 10000000, + boost::log::keywords::max_size = 10000000l); + + file_sink->set_formatter(boost::log::aux::acquire_formatter("%Channel% [%TimeStamp%] %SeverityStr%: %Message%")); + file_sink->locked_backend()->auto_flush(true); + + boost::log::core::get()->add_sink(file_sink); + boost::log::core::get()->add_global_attribute("TimeStamp", boost::log::attributes::local_clock()); boost::log::core::get()->set_exception_handler(boost::log::make_exception_handler( diff --git a/tests/tarcap_threadpool_test.cpp b/tests/tarcap_threadpool_test.cpp index df24fd7c2a..4bca7fae80 100644 --- a/tests/tarcap_threadpool_test.cpp +++ b/tests/tarcap_threadpool_test.cpp @@ -231,7 +231,7 @@ HandlersInitData createHandlersInitData() { ret_init_data.packets_processing_info = std::make_shared(); // Enable packets from sending peer to be processed - auto peer = ret_init_data.peers_state->addPendingPeer(ret_init_data.sender_node_id); + auto peer = ret_init_data.peers_state->addPendingPeer(ret_init_data.sender_node_id, ""); ret_init_data.peers_state->setPeerAsReadyToSendMessages(ret_init_data.sender_node_id, peer); return ret_init_data; @@ -316,7 +316,7 @@ TEST_F(TarcapTpTest, block_free_packets) { // Creates sender 2 to bypass peer order block on Transaction -> DagBlock packet. In case those packets sent // 2 different senders those packets are "block-free" dev::p2p::NodeID sender2(3); - auto peer = init_data.peers_state->addPendingPeer(sender2); + auto peer = init_data.peers_state->addPendingPeer(sender2, ""); init_data.peers_state->setPeerAsReadyToSendMessages(sender2, peer); auto packets_handler = std::make_shared(); From 55a0409180335f076a5170d0e4813db7d233da49 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Mon, 21 Aug 2023 12:36:28 +0200 Subject: [PATCH 106/134] chore: allow dag resyncing after node is out of pbft sync --- .../latest/dag_block_packet_handler.cpp | 13 ++++++++++--- .../src/tarcap/shared_states/pbft_syncing_state.cpp | 2 ++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp index 197bc59bb7..5903c3d3b0 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp @@ -131,9 +131,16 @@ void DagBlockPacketHandler::onNewBlockReceived(DagBlock &&block, const std::shar break; case DagManager::VerifyBlockReturnType::MissingTip: if (peer->peer_dag_synced_) { - std::ostringstream err_msg; - err_msg << "DagBlock has missing tip"; - throw MaliciousPeerException(err_msg.str()); + if (peer->dagSyncingAllowed()) { + LOG(log_wr_) << "NewBlock " << block_hash.toString() << " from peer " << peer->getId() + << " is missing tip, requesting dag sync"; + peer->peer_dag_synced_ = false; + requestPendingDagBlocks(peer); + } else { + std::ostringstream err_msg; + err_msg << "DagBlock has missing tip"; + throw MaliciousPeerException(err_msg.str()); + } } else { // peer_dag_synced_ flag ensures that this can only be performed once for a peer requestPendingDagBlocks(peer); diff --git a/libraries/core_libs/network/src/tarcap/shared_states/pbft_syncing_state.cpp b/libraries/core_libs/network/src/tarcap/shared_states/pbft_syncing_state.cpp index 88b185fcf7..85410c233d 100644 --- a/libraries/core_libs/network/src/tarcap/shared_states/pbft_syncing_state.cpp +++ b/libraries/core_libs/network/src/tarcap/shared_states/pbft_syncing_state.cpp @@ -35,6 +35,8 @@ bool PbftSyncingState::setPbftSyncing(bool syncing, PbftPeriod current_period, peer_ = std::move(peer); if (syncing) { + // If pbft syncing, set dag synced state to false + peer_->peer_dag_synced_ = false; deep_pbft_syncing_ = (peer_->pbft_chain_size_ - current_period >= kDeepSyncingThreshold); // Reset last sync packet time when syncing is restarted/fresh syncing flag is set setLastSyncPacketTime(); From 6d2a745316169663ef4b157b705e8326ed9bcd09 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Tue, 22 Aug 2023 16:59:46 +0200 Subject: [PATCH 107/134] chore: light node clearing on restart only --- .../consensus/include/dag/dag_manager.hpp | 2 +- .../consensus/src/dag/dag_manager.cpp | 12 +- .../storage/include/storage/storage.hpp | 3 +- libraries/core_libs/storage/src/storage.cpp | 121 ++++++++---------- 4 files changed, 60 insertions(+), 78 deletions(-) diff --git a/libraries/core_libs/consensus/include/dag/dag_manager.hpp b/libraries/core_libs/consensus/include/dag/dag_manager.hpp index 90b73acaf3..2de214cb1d 100644 --- a/libraries/core_libs/consensus/include/dag/dag_manager.hpp +++ b/libraries/core_libs/consensus/include/dag/dag_manager.hpp @@ -239,7 +239,7 @@ class DagManager : public std::enable_shared_from_this { bool validateBlockNotExpired(const std::shared_ptr &dag_block, std::unordered_map> &expired_dag_blocks_to_remove); void handleExpiredDagBlocksTransactions(const std::vector &transactions_from_expired_dag_blocks) const; - void clearLightNodeHistory(bool initial = false); + void clearLightNodeHistory(); std::pair> getFrontier() const; // return pivot and tips void updateFrontier(); diff --git a/libraries/core_libs/consensus/src/dag/dag_manager.cpp b/libraries/core_libs/consensus/src/dag/dag_manager.cpp index 491a4177e0..3658467634 100644 --- a/libraries/core_libs/consensus/src/dag/dag_manager.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_manager.cpp @@ -54,7 +54,7 @@ DagManager::DagManager(const DagBlock &dag_genesis_block, addr_t node_addr, cons } recoverDag(); if (is_light_node_) { - clearLightNodeHistory(true); + clearLightNodeHistory(); } } catch (std::exception &e) { std::cerr << e.what() << std::endl; @@ -282,14 +282,10 @@ std::vector DagManager::getDagBlockOrder(blk_hash_t const &anchor, P return blk_orders; } -void DagManager::clearLightNodeHistory(bool initial) { - // Delete once size 1% over the light_node_history_, by default light node history is a week of data so this condition - // will pass once an hour - uint64_t clear_interval = std::max(light_node_history_ / 100, (uint64_t)1); +void DagManager::clearLightNodeHistory() { bool dag_expiry_level_condition = dag_expiry_level_ > max_levels_per_period_ + 1; bool period_over_history_condition = period_ > light_node_history_; - if (((period_ % clear_interval == 0) || initial) && period_over_history_condition && dag_expiry_level_condition) { - // This will happen at most once a day so log a silent log + if (period_over_history_condition && dag_expiry_level_condition) { const auto proposal_period = db_->getProposalPeriodForDagLevel(dag_expiry_level_ - max_levels_per_period_ - 1); assert(proposal_period); @@ -305,7 +301,7 @@ void DagManager::clearLightNodeHistory(bool initial) { if (dag_expiry_level_ > max_levels_per_period_) { dag_level_to_keep = dag_expiry_level_ - max_levels_per_period_; } - db_->clearPeriodDataHistory(end, dag_level_to_keep, initial); + db_->clearPeriodDataHistory(end, dag_level_to_keep); } } diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index 28e2190aea..5fa58e44c2 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -145,7 +145,6 @@ class DbStorage : public std::enable_shared_from_this { std::atomic snapshots_enabled_ = true; const uint32_t kDbSnapshotsMaxCount = 0; std::set snapshots_; - std::unique_ptr> clear_history_future_; uint32_t kMajorVersion_; bool major_version_changed_ = false; @@ -199,7 +198,7 @@ class DbStorage : public std::enable_shared_from_this { // Period data void savePeriodData(const PeriodData& period_data, Batch& write_batch); - void clearPeriodDataHistory(PbftPeriod period, uint64_t dag_level_to_keep, bool initial); + void clearPeriodDataHistory(PbftPeriod period, uint64_t dag_level_to_keep); dev::bytes getPeriodDataRaw(PbftPeriod period) const; std::optional getPbftBlock(PbftPeriod period) const; std::vector> getPeriodCertVotes(PbftPeriod period) const; diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 9a39caba58..1785911b9f 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -374,7 +374,6 @@ std::optional DbStorage::getGenesisHash() { } DbStorage::~DbStorage() { - if (clear_history_future_) clear_history_future_->wait(); for (auto cf : handles_) { checkStatus(db_->DestroyColumnFamilyHandle(cf)); } @@ -564,7 +563,7 @@ std::optional DbStorage::getParamsChangeForPeriod(PbftPer return SortitionParamsChange::from_rlp(dev::RLP(it->value().ToString())); } -void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level_to_keep, bool initial) { +void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level_to_keep) { LOG(log_si_) << "Clear light node history"; auto it = std::unique_ptr(db_->NewIterator(read_options_, handle(Columns::period_data))); // Find the first non-deleted period @@ -577,73 +576,61 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level if (it->Valid()) { uint64_t start_level; memcpy(&start_level, it->key().data(), sizeof(uint64_t)); - if (clear_history_future_) clear_history_future_->wait(); - clear_history_future_ = std::make_unique>( - std::async(std::launch::async, [this, start_period, start_level, initial, dag_level_to_keep, end_period]() { - if (start_period < end_period) { - auto write_batch = createWriteBatch(); - // Delete up to max 10000 period at once - const uint32_t max_batch_delete = 10000; - auto start_slice = toSlice(start_period); - auto end_slice = toSlice(end_period); - - for (auto period = start_period; period < end_period; period++) { - // Find transactions included in the old blocks and delete data related to these transactions to free - // disk space - const auto& dag_blocks = getFinalizedDagBlockByPeriod(period); - - for (const auto& dag_block : dag_blocks) { - for (const auto& trx_hash : dag_block->getTrxs()) { - remove(write_batch, Columns::final_chain_receipt_by_trx_hash, trx_hash); - } - } - if ((period - start_period + 1) % max_batch_delete == 0) { - commitWriteBatch(write_batch); - write_batch = createWriteBatch(); - } - } - - commitWriteBatch(write_batch); - db_->DeleteRange(write_options_, handle(Columns::period_data), start_slice, end_slice); - - if (initial) { - // Deletion alone does not guarantee that the disk space is freed, CompactRange actually compacts - // the data in the database and free disk space - db_->CompactRange({}, handle(Columns::period_data), &start_slice, &end_slice); - db_->CompactRange({}, handle(Columns::final_chain_receipt_by_trx_hash), nullptr, nullptr); - } + if (start_period < end_period) { + auto write_batch = createWriteBatch(); + // Delete up to max 10000 period at once + const uint32_t max_batch_delete = 10000; + auto start_slice = toSlice(start_period); + auto end_slice = toSlice(end_period); + + for (auto period = start_period; period < end_period; period++) { + // Find transactions included in the old blocks and delete data related to these transactions to free + // disk space + const auto& dag_blocks = getFinalizedDagBlockByPeriod(period); + + for (const auto& dag_block : dag_blocks) { + for (const auto& trx_hash : dag_block->getTrxs()) { + remove(write_batch, Columns::final_chain_receipt_by_trx_hash, trx_hash); } + } + if ((period - start_period + 1) % max_batch_delete == 0) { + commitWriteBatch(write_batch); + write_batch = createWriteBatch(); + } + } - if (start_level < dag_level_to_keep) { - auto write_batch = createWriteBatch(); - // Delete up to max 10000 period at once - const uint32_t max_batch_delete = 10000; - auto start_slice = toSlice(start_level); - auto end_slice = toSlice(dag_level_to_keep - 1); - for (auto level = start_level; level < dag_level_to_keep; level++) { - // Find old dag blocks and delete data related to these blocks to free disk space - auto dag_block_hashes = getBlocksByLevel(start_level); - for (auto dag_block_hash : dag_block_hashes) { - remove(write_batch, Columns::dag_block_period, dag_block_hash); - } - if ((dag_level_to_keep - start_level + 1) % max_batch_delete == 0) { - commitWriteBatch(write_batch); - write_batch = createWriteBatch(); - } - } - commitWriteBatch(write_batch); - db_->DeleteRange(write_options_, handle(Columns::dag_blocks_level), start_slice, end_slice); - - if (initial) { - db_->CompactRange({}, handle(Columns::dag_block_period), nullptr, nullptr); - db_->CompactRange({}, handle(Columns::dag_blocks_level), nullptr, nullptr); - } - LOG(log_si_) << "Clear light node history completed"; - } - })); - if (initial) { - clear_history_future_->wait(); - clear_history_future_ = nullptr; + commitWriteBatch(write_batch); + db_->DeleteRange(write_options_, handle(Columns::period_data), start_slice, end_slice); + + // Deletion alone does not guarantee that the disk space is freed, CompactRange actually compacts + // the data in the database and free disk space + db_->CompactRange({}, handle(Columns::period_data), &start_slice, &end_slice); + db_->CompactRange({}, handle(Columns::final_chain_receipt_by_trx_hash), nullptr, nullptr); + } + + if (start_level < dag_level_to_keep) { + auto write_batch = createWriteBatch(); + // Delete up to max 10000 period at once + const uint32_t max_batch_delete = 10000; + auto start_slice = toSlice(start_level); + auto end_slice = toSlice(dag_level_to_keep - 1); + for (auto level = start_level; level < dag_level_to_keep; level++) { + // Find old dag blocks and delete data related to these blocks to free disk space + auto dag_block_hashes = getBlocksByLevel(start_level); + for (auto dag_block_hash : dag_block_hashes) { + remove(write_batch, Columns::dag_block_period, dag_block_hash); + } + if ((dag_level_to_keep - start_level + 1) % max_batch_delete == 0) { + commitWriteBatch(write_batch); + write_batch = createWriteBatch(); + } + } + commitWriteBatch(write_batch); + db_->DeleteRange(write_options_, handle(Columns::dag_blocks_level), start_slice, end_slice); + + db_->CompactRange({}, handle(Columns::dag_block_period), nullptr, nullptr); + db_->CompactRange({}, handle(Columns::dag_blocks_level), nullptr, nullptr); + LOG(log_si_) << "Clear light node history completed"; } } } From c75c754d8c4fd854d3830d7078c793b0c78f9e7d Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Thu, 24 Aug 2023 13:09:52 +0200 Subject: [PATCH 108/134] chore: improve discovery ping handling --- libraries/aleth/libp2p/NodeTable.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libraries/aleth/libp2p/NodeTable.cpp b/libraries/aleth/libp2p/NodeTable.cpp index 7a6f5cd51e..b6e00d7d08 100644 --- a/libraries/aleth/libp2p/NodeTable.cpp +++ b/libraries/aleth/libp2p/NodeTable.cpp @@ -697,7 +697,12 @@ void NodeTable::doHandleTimeouts() { if (chrono::steady_clock::now() > it->second.pingSentTime + m_requestTimeToLive) { if (auto node = nodeEntry(it->second.nodeID)) { if (node->lastPongReceivedTime < RLPXDatagramFace::secondsSinceEpoch() - m_requestTimeToLive.count()) { - dropNode(std::move(node)); + if (it->first == node->endpoint()) { + dropNode(std::move(node)); + } else { + LOG(m_logger) << "Not dropping node " << it->second.nodeID << " on ping timeout for " << it->first << " " + << " as previous endpoint still valid " << node->endpoint(); + } } else { LOG(m_logger) << "Not dropping node " << it->second.nodeID << " " << it->first << " as it was updated"; } From aeb79ef97aa919ddd35af1fd79304ad99364837f Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Fri, 25 Aug 2023 09:28:55 +0200 Subject: [PATCH 109/134] chore: clean light node only on start --- libraries/core_libs/consensus/src/dag/dag_manager.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libraries/core_libs/consensus/src/dag/dag_manager.cpp b/libraries/core_libs/consensus/src/dag/dag_manager.cpp index 3658467634..a27985784c 100644 --- a/libraries/core_libs/consensus/src/dag/dag_manager.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_manager.cpp @@ -390,10 +390,6 @@ uint DagManager::setDagBlockOrder(blk_hash_t const &new_anchor, PbftPeriod perio period_ = period; updateFrontier(); - if (is_light_node_) { - clearLightNodeHistory(); - } - LOG(log_nf_) << "Set new period " << period << " with anchor " << new_anchor; return dag_order_set.size(); From e575a47d3de06a99af076ecae47fcba861bba0ba Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Fri, 25 Aug 2023 11:08:14 +0200 Subject: [PATCH 110/134] chore: fix light node unit test --- libraries/core_libs/consensus/include/dag/dag_manager.hpp | 7 ++++++- tests/full_node_test.cpp | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/libraries/core_libs/consensus/include/dag/dag_manager.hpp b/libraries/core_libs/consensus/include/dag/dag_manager.hpp index 2de214cb1d..b9cde3e56b 100644 --- a/libraries/core_libs/consensus/include/dag/dag_manager.hpp +++ b/libraries/core_libs/consensus/include/dag/dag_manager.hpp @@ -232,6 +232,12 @@ class DagManager : public std::enable_shared_from_this { */ static dev::bytes getVdfMessage(blk_hash_t const &hash, std::vector const &trx_hashes); + /** + * @brief Clears light node history + * + */ + void clearLightNodeHistory(); + private: void recoverDag(); void addToDag(blk_hash_t const &hash, blk_hash_t const &pivot, std::vector const &tips, uint64_t level, @@ -239,7 +245,6 @@ class DagManager : public std::enable_shared_from_this { bool validateBlockNotExpired(const std::shared_ptr &dag_block, std::unordered_map> &expired_dag_blocks_to_remove); void handleExpiredDagBlocksTransactions(const std::vector &transactions_from_expired_dag_blocks) const; - void clearLightNodeHistory(); std::pair> getFrontier() const; // return pivot and tips void updateFrontier(); diff --git a/tests/full_node_test.cpp b/tests/full_node_test.cpp index 7dbbbe98a9..f9df1ee239 100644 --- a/tests/full_node_test.cpp +++ b/tests/full_node_test.cpp @@ -1419,6 +1419,7 @@ TEST_F(FullNodeTest, light_node) { // broadcast dummy transaction nodes[1]->getTransactionManager()->insertTransaction(dummy_trx); thisThreadSleepForMilliSeconds(200); + nodes[1]->getDagManager()->clearLightNodeHistory(); } EXPECT_HAPPENS({10s, 1s}, [&](auto &ctx) { // Verify full node and light node sync without any issues From f855e0c437413e7fa8a5c338af97ce8268997305 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Thu, 31 Aug 2023 14:30:07 +0200 Subject: [PATCH 111/134] chore: light node size optimization --- .../storage/include/storage/storage.hpp | 2 + libraries/core_libs/storage/src/storage.cpp | 39 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/libraries/core_libs/storage/include/storage/storage.hpp b/libraries/core_libs/storage/include/storage/storage.hpp index 5fa58e44c2..7e66789733 100644 --- a/libraries/core_libs/storage/include/storage/storage.hpp +++ b/libraries/core_libs/storage/include/storage/storage.hpp @@ -313,6 +313,8 @@ class DbStorage : public std::enable_shared_from_this { std::vector getFinalizedDagBlockHashesByPeriod(PbftPeriod period); std::vector> getFinalizedDagBlockByPeriod(PbftPeriod period); + std::pair>> getLastPbftblockHashAndFinalizedDagBlockByPeriod( + PbftPeriod period); // DPOS level to proposal period map std::optional getProposalPeriodForDagLevel(uint64_t level); diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index 1785911b9f..f9a419ddbc 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -8,6 +8,7 @@ #include "config/version.hpp" #include "dag/sortition_params_manager.hpp" +#include "final_chain/final_chain.hpp" #include "rocksdb/utilities/checkpoint.h" #include "storage/uint_comparator.hpp" #include "vote/vote.hpp" @@ -20,6 +21,7 @@ static constexpr uint16_t PBFT_BLOCK_POS_IN_PERIOD_DATA = 0; static constexpr uint16_t CERT_VOTES_POS_IN_PERIOD_DATA = 1; static constexpr uint16_t DAG_BLOCKS_POS_IN_PERIOD_DATA = 2; static constexpr uint16_t TRANSACTIONS_POS_IN_PERIOD_DATA = 3; +static constexpr uint16_t PREV_BLOCK_HASH_POS_IN_PBFT_BLOCK = 0; DbStorage::DbStorage(fs::path const& path, uint32_t db_snapshot_each_n_pbft_block, uint32_t max_open_files, uint32_t db_max_snapshots, PbftPeriod db_revert_to_period, addr_t node_addr, bool rebuild) @@ -586,13 +588,22 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level for (auto period = start_period; period < end_period; period++) { // Find transactions included in the old blocks and delete data related to these transactions to free // disk space - const auto& dag_blocks = getFinalizedDagBlockByPeriod(period); + const auto& [pbft_block_hash, dag_blocks] = getLastPbftblockHashAndFinalizedDagBlockByPeriod(period); for (const auto& dag_block : dag_blocks) { for (const auto& trx_hash : dag_block->getTrxs()) { remove(write_batch, Columns::final_chain_receipt_by_trx_hash, trx_hash); + remove(write_batch, Columns::trx_period, toSlice(trx_hash.asBytes())); } } + remove(write_batch, Columns::pbft_block_period, toSlice(pbft_block_hash.asBytes())); + + for (uint64_t level = 0, index = period; level < final_chain::c_bloomIndexLevels; + ++level, index /= final_chain::c_bloomIndexSize) { + auto chunk_id = h256(index / final_chain::c_bloomIndexSize * 0xff + level); + remove(write_batch, Columns::final_chain_log_blooms_index, chunk_id); + } + if ((period - start_period + 1) % max_batch_delete == 0) { commitWriteBatch(write_batch); write_batch = createWriteBatch(); @@ -606,6 +617,9 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level // the data in the database and free disk space db_->CompactRange({}, handle(Columns::period_data), &start_slice, &end_slice); db_->CompactRange({}, handle(Columns::final_chain_receipt_by_trx_hash), nullptr, nullptr); + db_->CompactRange({}, handle(Columns::trx_period), nullptr, nullptr); + db_->CompactRange({}, handle(Columns::pbft_block_period), nullptr, nullptr); + db_->CompactRange({}, handle(Columns::final_chain_log_blooms_index), nullptr, nullptr); } if (start_level < dag_level_to_keep) { @@ -616,11 +630,11 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period, uint64_t dag_level auto end_slice = toSlice(dag_level_to_keep - 1); for (auto level = start_level; level < dag_level_to_keep; level++) { // Find old dag blocks and delete data related to these blocks to free disk space - auto dag_block_hashes = getBlocksByLevel(start_level); + auto dag_block_hashes = getBlocksByLevel(level); for (auto dag_block_hash : dag_block_hashes) { - remove(write_batch, Columns::dag_block_period, dag_block_hash); + remove(write_batch, Columns::dag_block_period, toSlice(dag_block_hash.asBytes())); } - if ((dag_level_to_keep - start_level + 1) % max_batch_delete == 0) { + if ((level - start_level + 1) % max_batch_delete == 0) { commitWriteBatch(write_batch); write_batch = createWriteBatch(); } @@ -1122,6 +1136,23 @@ std::vector> DbStorage::getFinalizedDagBlockByPeriod(P return ret; } +std::pair>> +DbStorage::getLastPbftblockHashAndFinalizedDagBlockByPeriod(PbftPeriod period) { + std::vector> ret; + blk_hash_t last_pbft_block_hash; + if (auto period_data = getPeriodDataRaw(period); period_data.size() > 0) { + auto const period_data_rlp = dev::RLP(period_data); + auto dag_blocks_data = period_data_rlp[DAG_BLOCKS_POS_IN_PERIOD_DATA]; + ret.reserve(dag_blocks_data.size()); + for (auto const block : dag_blocks_data) { + ret.emplace_back(std::make_shared(block)); + } + last_pbft_block_hash = + period_data_rlp[PBFT_BLOCK_POS_IN_PERIOD_DATA][PREV_BLOCK_HASH_POS_IN_PBFT_BLOCK].toHash(); + } + return {last_pbft_block_hash, ret}; +} + std::optional DbStorage::getProposalPeriodForDagLevel(uint64_t level) { auto it = std::unique_ptr(db_->NewIterator(read_options_, handle(Columns::proposal_period_levels_map))); From 3e6cf04c761c43655de750cb14a772280dd81e7f Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Mon, 18 Sep 2023 16:20:16 +0200 Subject: [PATCH 112/134] chore: update dependencies --- conanfile.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conanfile.py b/conanfile.py index 364e1d5d2a..49e27f5ef8 100644 --- a/conanfile.py +++ b/conanfile.py @@ -13,11 +13,11 @@ class TaraxaConan(ConanFile): generators = "cmake" def requirements(self): - self.requires("boost/1.81.0") - self.requires("cppcheck/2.10") - self.requires("openssl/1.1.1t") + self.requires("boost/1.83.0") + self.requires("cppcheck/2.12") + self.requires("openssl/3.0.10") self.requires("cryptopp/8.7.0") - self.requires("gtest/1.13.0") + self.requires("gtest/1.14.0") self.requires("lz4/1.9.4") self.requires("rocksdb/6.29.5") self.requires("prometheus-cpp/1.1.0") From 3033ca98ca95d619b1c5344ddc1888e2c63c4650 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Mon, 18 Sep 2023 16:20:42 +0200 Subject: [PATCH 113/134] chore: remove cppcheck warnings --- CMakeModules/cppcheck.cmake | 5 +++- libraries/aleth/libdevcore/Address.cpp | 10 ------- libraries/aleth/libdevcore/Address.h | 9 +----- libraries/aleth/libdevcore/Common.h | 1 - libraries/cli/include/cli/tools.hpp | 5 ++-- libraries/cli/src/tools.cpp | 14 +++------ .../config/include/config/config_utils.hpp | 2 +- libraries/config/src/config_utils.cpp | 2 +- .../consensus/src/final_chain/final_chain.cpp | 1 - .../core_libs/network/rpc/eth/LogFilter.cpp | 29 +++++++++---------- .../latest/dag_block_packet_handler.cpp | 2 +- libraries/core_libs/storage/src/storage.cpp | 4 ++- 12 files changed, 31 insertions(+), 53 deletions(-) delete mode 100644 libraries/aleth/libdevcore/Address.cpp diff --git a/CMakeModules/cppcheck.cmake b/CMakeModules/cppcheck.cmake index 4f7a0b22bb..2cd752ccc1 100644 --- a/CMakeModules/cppcheck.cmake +++ b/CMakeModules/cppcheck.cmake @@ -11,6 +11,7 @@ else () --error-exitcode=1 --enable=all --suppress=missingInclude + --suppress=missingIncludeSystem # find_if - useless here --suppress=useStlAlgorithm:${PROJECT_SOURCE_DIR}/*/pbft_sync_packet_handler.cpp --suppress=noExplicitConstructor @@ -36,8 +37,10 @@ else () --suppress=unmatchedSuppression:${PROJECT_SOURCE_DIR}/*/Common.h --suppress=cstyleCast:${PROJECT_SOURCE_DIR}/*/vector_ref.h --suppress=cstyleCast:${PROJECT_SOURCE_DIR}/*/Common.h - #not an issue here + # not an issue here --suppress=virtualCallInConstructor:${PROJECT_SOURCE_DIR}/*/final_chain.cpp + # just a warning + --suppress=duplInheritedMember:${PROJECT_SOURCE_DIR}/*/FixedHash.h # Only show found errors "--quiet" diff --git a/libraries/aleth/libdevcore/Address.cpp b/libraries/aleth/libdevcore/Address.cpp deleted file mode 100644 index 6bb3463e46..0000000000 --- a/libraries/aleth/libdevcore/Address.cpp +++ /dev/null @@ -1,10 +0,0 @@ -// Aleth: Ethereum C++ client, tools and libraries. -// Copyright 2014-2019 Aleth Authors. -// Licensed under the GNU General Public License, Version 3. -#include "Address.h" - -namespace dev { -Address const ZeroAddress; -Address const MaxAddress{"0xffffffffffffffffffffffffffffffffffffffff"}; -Address const SystemAddress{"0xfffffffffffffffffffffffffffffffffffffffe"}; -} // namespace dev diff --git a/libraries/aleth/libdevcore/Address.h b/libraries/aleth/libdevcore/Address.h index b9fdcf8fae..66be630322 100644 --- a/libraries/aleth/libdevcore/Address.h +++ b/libraries/aleth/libdevcore/Address.h @@ -23,12 +23,5 @@ using Addresses = h160s; using AddressSet = std::unordered_set; /// The zero address. -extern Address const ZeroAddress; - -/// The last address. -extern Address const MaxAddress; - -/// The SYSTEM address. -extern Address const SystemAddress; - +Address const ZeroAddress; } // namespace dev diff --git a/libraries/aleth/libdevcore/Common.h b/libraries/aleth/libdevcore/Common.h index 10dc3e8403..e7dfe89035 100644 --- a/libraries/aleth/libdevcore/Common.h +++ b/libraries/aleth/libdevcore/Common.h @@ -127,7 +127,6 @@ using strings = std::vector; // Null/Invalid values for convenience. extern bytes const NullBytes; -u256 constexpr Invalid256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_cppui256; /// Interprets @a _u as a two's complement signed number and returns the /// resulting s256. diff --git a/libraries/cli/include/cli/tools.hpp b/libraries/cli/include/cli/tools.hpp index d372684ca9..cf33ed2928 100644 --- a/libraries/cli/include/cli/tools.hpp +++ b/libraries/cli/include/cli/tools.hpp @@ -35,8 +35,9 @@ Json::Value getGenesis(Config::ChainIdType chain_id); void generateWallet(const std::string& wallet); // Override existing config and wallet files -Json::Value overrideConfig(Json::Value& config, std::string& data_dir, std::vector boot_nodes, - std::vector log_channels, std::vector log_configurations, +Json::Value overrideConfig(Json::Value& config, std::string& data_dir, const std::vector& boot_nodes, + const std::vector& log_channels, + const std::vector& log_configurations, const std::vector& boot_nodes_append, const std::vector& log_channels_append); Json::Value overrideWallet(Json::Value& wallet, const std::string& node_key, const std::string& vrf_key); diff --git a/libraries/cli/src/tools.cpp b/libraries/cli/src/tools.cpp index 92b2720760..cdfea7be4f 100644 --- a/libraries/cli/src/tools.cpp +++ b/libraries/cli/src/tools.cpp @@ -64,8 +64,8 @@ Json::Value getGenesis(Config::ChainIdType chain_id) { return genesis; } -Json::Value overrideConfig(Json::Value& conf, std::string& data_dir, vector boot_nodes, - vector log_channels, vector log_configurations, +Json::Value overrideConfig(Json::Value& conf, std::string& data_dir, const vector& boot_nodes, + const vector& log_channels, const vector& log_configurations, const vector& boot_nodes_append, const vector& log_channels_append) { if (data_dir.empty()) { if (conf["data_path"].asString().empty()) { @@ -88,10 +88,7 @@ Json::Value overrideConfig(Json::Value& conf, std::string& data_dir, vector 0) { - boot_nodes = boot_nodes_append; - } - if (boot_nodes.size() > 0) { - for (auto const& b : boot_nodes) { + for (auto const& b : boot_nodes_append) { vector result; boost::split(result, b, boost::is_any_of(":/")); if (result.size() != 3) throw invalid_argument("Boot node in boot_nodes not specified correctly"); @@ -130,10 +127,7 @@ Json::Value overrideConfig(Json::Value& conf, std::string& data_dir, vector 0) { - log_channels = log_channels_append; - } - if (log_channels.size() > 0) { - for (auto const& l : log_channels) { + for (auto const& l : log_channels_append) { vector result; boost::split(result, l, boost::is_any_of(":")); if (result.size() != 2) throw invalid_argument("Log channel in log_channels not specified correctly"); diff --git a/libraries/config/include/config/config_utils.hpp b/libraries/config/include/config/config_utils.hpp index 36d7f5367f..e5b168d85f 100644 --- a/libraries/config/include/config/config_utils.hpp +++ b/libraries/config/include/config/config_utils.hpp @@ -7,7 +7,7 @@ #include "common/util.hpp" namespace taraxa { -std::string getConfigErr(std::vector path); +std::string getConfigErr(const std::vector &path); Json::Value getConfigData(Json::Value root, const std::vector &path, bool optional = false); diff --git a/libraries/config/src/config_utils.cpp b/libraries/config/src/config_utils.cpp index 599125ae8b..bdef058854 100644 --- a/libraries/config/src/config_utils.cpp +++ b/libraries/config/src/config_utils.cpp @@ -2,7 +2,7 @@ namespace taraxa { -std::string getConfigErr(std::vector path) { +std::string getConfigErr(const std::vector &path) { std::string res = "Error in processing configuration file on param: "; for (size_t i = 0; i < path.size(); i++) res += path[i] + "."; res += " "; diff --git a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp index 6cb07ce631..285ae8832b 100644 --- a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp +++ b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp @@ -18,7 +18,6 @@ class FinalChainImpl final : public FinalChain { const bool kLightNode = false; const uint64_t kLightNodeHistory = 0; const uint32_t kMaxLevelsPerPeriod; - const uint32_t kRewardsDistributionInterval = 100; rewards::Stats rewards_; // It is not prepared to use more then 1 thread. Examine it if you want to change threads count diff --git a/libraries/core_libs/network/rpc/eth/LogFilter.cpp b/libraries/core_libs/network/rpc/eth/LogFilter.cpp index 2130f49fa6..efd332930f 100644 --- a/libraries/core_libs/network/rpc/eth/LogFilter.cpp +++ b/libraries/core_libs/network/rpc/eth/LogFilter.cpp @@ -48,14 +48,12 @@ std::vector LogFilter::bloomPossibilities() const { bool LogFilter::matches(LogBloom b) const { if (!addresses_.empty()) { - auto ok = false; - for (const auto& i : addresses_) { - if (b.containsBloom<3>(sha3(i))) { - ok = true; - break; - } - } - if (!ok) { + if (std::none_of(addresses_.cbegin(), addresses_.cend(), [&b](const auto& i) { + if (b.containsBloom<3>(sha3(i))) { + return true; + } + return false; + })) { return false; } } @@ -63,14 +61,13 @@ bool LogFilter::matches(LogBloom b) const { if (t.empty()) { continue; } - auto ok = false; - for (const auto& i : t) { - if (b.containsBloom<3>(sha3(i))) { - ok = true; - break; - } - } - if (!ok) { + + if (std::none_of(t.cbegin(), t.cend(), [&b](const auto& i) { + if (b.containsBloom<3>(sha3(i))) { + return true; + } + return false; + })) { return false; } } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp index 5903c3d3b0..1d6fb93bf2 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/dag_block_packet_handler.cpp @@ -67,7 +67,7 @@ void DagBlockPacketHandler::sendBlock(dev::p2p::NodeID const &peer_id, taraxa::D taraxa::bytes trx_bytes; for (uint32_t i = index; i < index + trx_count_to_send; i++) { - auto &trx_data = trxs[i]->rlp(); + auto trx_data = trxs[i]->rlp(); s << trxs[i]->getHash(); trx_bytes.insert(trx_bytes.end(), std::begin(trx_data), std::end(trx_data)); } diff --git a/libraries/core_libs/storage/src/storage.cpp b/libraries/core_libs/storage/src/storage.cpp index f9a419ddbc..64144592c2 100644 --- a/libraries/core_libs/storage/src/storage.cpp +++ b/libraries/core_libs/storage/src/storage.cpp @@ -923,7 +923,9 @@ void DbStorage::addPbftMgrFieldToBatch(PbftMgrField field, uint32_t value, Batch bool DbStorage::getPbftMgrStatus(PbftMgrStatus field) { auto status = lookup(toSlice(field), Columns::pbft_mgr_status); if (!status.empty()) { - return *(bool*)&status[0]; + bool value; + memcpy(&value, status.data(), sizeof(bool)); + return value; } return false; } From 803af1f651104a5c22e7829387ca686f98f0254c Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Thu, 18 May 2023 11:04:32 -0700 Subject: [PATCH 114/134] implement magnolia hardfork --- .../include/cli/config_jsons/default/default_genesis.json | 5 +++++ libraries/config/include/config/hardfork.hpp | 8 +++++++- libraries/config/src/hardfork.cpp | 8 +++++++- submodules/taraxa-evm | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/libraries/cli/include/cli/config_jsons/default/default_genesis.json b/libraries/cli/include/cli/config_jsons/default/default_genesis.json index eec548c299..4096d43c01 100644 --- a/libraries/cli/include/cli/config_jsons/default/default_genesis.json +++ b/libraries/cli/include/cli/config_jsons/default/default_genesis.json @@ -60,6 +60,11 @@ "2cd4da7d3b345e022ca7e997c2bb3276a4d3d2e9": "0x1027e72f1f12813088000000", "7e4aa664f71de4e9d0b4a6473d796372639bdcde": "0x1027e72f1f12813088000000" }, + "hardforks": { + "rewards_distribution_frequency": { + }, + "magnolia_hf_block_num" : 0 + }, "gas_price": { "blocks": 200, "percentile": 60, diff --git a/libraries/config/include/config/hardfork.hpp b/libraries/config/include/config/hardfork.hpp index 744dedee9a..47c3e88018 100644 --- a/libraries/config/include/config/hardfork.hpp +++ b/libraries/config/include/config/hardfork.hpp @@ -32,8 +32,14 @@ struct Hardforks { RewardsDistributionMap rewards_distribution_frequency; // disable it by default (set to max uint64) uint64_t fee_rewards_block_num = -1; + + // Magnolia hardfork is fixing premature deletion of validators in dpos contract -> validator is deleted only + // after last delegator confirms his undelegation and: + // total_stake == 0, rewards_pool == 0, undelegations_count == 0. + uint64_t magnolia_hf_block_num = 0; + HAS_RLP_FIELDS }; Json::Value enc_json(const Hardforks& obj); -void dec_json(const Json::Value& json, Hardforks& obj); \ No newline at end of file +void dec_json(const Json::Value& json, Hardforks& obj); diff --git a/libraries/config/src/hardfork.cpp b/libraries/config/src/hardfork.cpp index d25ab894bc..12c0a3efd1 100644 --- a/libraries/config/src/hardfork.cpp +++ b/libraries/config/src/hardfork.cpp @@ -31,6 +31,7 @@ Json::Value enc_json(const Hardforks& obj) { } json["fee_rewards_block_num"] = obj.fee_rewards_block_num; + json["magnolia_hf_block_num"] = obj.magnolia_hf_block_num; return json; } @@ -52,10 +53,15 @@ void dec_json(const Json::Value& json, Hardforks& obj) { obj.rewards_distribution_frequency[itr.key().asUInt64()] = itr->asUInt64(); } } + if (const auto& e = json["fee_rewards_block_num"]) { obj.fee_rewards_block_num = dev::getUInt(e); } + + if (const auto& e = json["magnolia_hf_block_num"]) { + obj.magnolia_hf_block_num = dev::getUInt(e); + } } RLP_FIELDS_DEFINE(Hardforks, fix_redelegate_block_num, redelegations, rewards_distribution_frequency, - fee_rewards_block_num) + fee_rewards_block_num, magnolia_hf_block_num) diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index e1e538b299..e3d0731365 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit e1e538b299612af24d4527bc10af0956bd580f97 +Subproject commit e3d0731365c3b898f1ce79163caa29be217ea0b7 From 37517a9364e9dd70e042368b50116470410d9818 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Tue, 23 May 2023 15:44:08 -0700 Subject: [PATCH 115/134] aadded magnolia hf into devnet config --- .../include/cli/config_jsons/devnet/devnet_genesis.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json index 7e3efa7712..4570b9bdfa 100644 --- a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json @@ -226,6 +226,11 @@ "7e4aa664f71de4e9d0b4a6473d796372639bdcde": "0x1027e72f1f12813088000000", "ee1326fbf7d9322e5ea02c6fe5eb63535fceccd1": "0x52b7d2dcc80cd2e4000000" }, + "hardforks": { + "rewards_distribution_frequency": { + }, + "magnolia_hf_block_num" : 0 + }, "gas_price": { "blocks": 200, "percentile": 60, @@ -267,4 +272,4 @@ "fix_redelegate_block_num": 0, "rewards_distribution_frequency": {} } -} \ No newline at end of file +} From b5f73b713ab4514109dcf7a53b9142176d7b19c1 Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Mon, 22 May 2023 14:36:41 +0200 Subject: [PATCH 116/134] feat: implement hardfork to change fee rewards distribution --- .gitignore | 2 +- for_devs/local-net | 1 + .../config_jsons/mainnet/mainnet_genesis.json | 5 ++ .../config_jsons/testnet/testnet_genesis.json | 7 +- libraries/config/include/config/hardfork.hpp | 14 ++-- libraries/config/src/hardfork.cpp | 8 +-- .../include/final_chain/state_api.hpp | 14 ++-- .../include/final_chain/state_api_data.hpp | 7 +- .../consensus/include/rewards/block_stats.hpp | 37 ++++++----- .../include/rewards/rewards_stats.hpp | 20 +++--- .../consensus/src/final_chain/final_chain.cpp | 20 ++++-- .../consensus/src/final_chain/state_api.cpp | 28 +++++--- .../src/final_chain/state_api_data.cpp | 3 +- .../consensus/src/rewards/block_stats.cpp | 60 +++++++++-------- .../consensus/src/rewards/rewards_stats.cpp | 25 ++++--- tests/final_chain_test.cpp | 62 +++++++++++++++--- tests/rewards_stats_test.cpp | 65 +++++++++++++++---- tests/state_api_test.cpp | 3 +- tests/test_util/src/test_util.cpp | 6 -- 19 files changed, 252 insertions(+), 135 deletions(-) diff --git a/.gitignore b/.gitignore index c3c9173fb8..62bbc4767e 100644 --- a/.gitignore +++ b/.gitignore @@ -15,7 +15,7 @@ cmake-* # put here local env stuff local -/local-net-data/ +*/local-net-data/ .DS_Store diff --git a/for_devs/local-net b/for_devs/local-net index 29c938e422..2cfd1c57f2 100755 --- a/for_devs/local-net +++ b/for_devs/local-net @@ -286,6 +286,7 @@ def node_worker(binary, worker_name, enable_test_rpc): cmd = f'{binary} --config ./{data_dir}/conf-{worker_name}.json --wallet ./{data_dir}/wallet-{worker_name}.json --genesis ./{data_dir}/genesis-{worker_name}.json' if enable_test_rpc: cmd += ' --enable-test-rpc ' + process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) logs(worker_name, process) diff --git a/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json b/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json index d2179d1c81..6750a962b0 100644 --- a/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json @@ -1588,6 +1588,11 @@ "c7cbe7d179857ea0ee5721f6b8b1b0dab193daf9": "0x381ce3ea85dc8f21bf0000", "6ba16abe80af8be7c9b6c852c6bd3e381a493c92": "0x4958bc77396e445c569000" }, + "hardforks": { + "rewards_distribution_frequency": { + }, + "magnolia_hf_block_num" : 18446744073709551615 + }, "gas_price": { "blocks": 200, "percentile": 60, diff --git a/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json b/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json index b5b457e7ca..7c1377a75b 100644 --- a/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json @@ -94,6 +94,11 @@ "f4a52b8f6dc8ab046fec6ad02e77023c044342e4": "0x19d971e4fe8401e74000000", "ee1326fbf7d9322e5ea02c6fe5eb63535fceccd1": "0x19d971e4fe8401e74000000" }, + "hardforks": { + "rewards_distribution_frequency": { + }, + "magnolia_hf_block_num" : 18446744073709551615 + }, "gas_price": { "blocks": 200, "percentile": 60, @@ -136,4 +141,4 @@ "redelegations": [], "rewards_distribution_frequency": {} } -} \ No newline at end of file +} diff --git a/libraries/config/include/config/hardfork.hpp b/libraries/config/include/config/hardfork.hpp index 47c3e88018..41b23b3b59 100644 --- a/libraries/config/include/config/hardfork.hpp +++ b/libraries/config/include/config/hardfork.hpp @@ -30,13 +30,15 @@ struct Hardforks { */ using RewardsDistributionMap = std::map; RewardsDistributionMap rewards_distribution_frequency; - // disable it by default (set to max uint64) - uint64_t fee_rewards_block_num = -1; - // Magnolia hardfork is fixing premature deletion of validators in dpos contract -> validator is deleted only - // after last delegator confirms his undelegation and: - // total_stake == 0, rewards_pool == 0, undelegations_count == 0. - uint64_t magnolia_hf_block_num = 0; + // Magnolia hardfork: + // 1.fixing premature deletion of validators in dpos contract -> validator is deleted only + // after last delegator confirms his undelegation and: + // total_stake == 0, rewards_pool == 0, undelegations_count == 0. + // 2. changing fee rewards distribution. + // Rewards will be distributed to dag blocks creator commission pool, but not directly to the balance of pbft block + // creator. + uint64_t magnolia_hf_block_num = -1; HAS_RLP_FIELDS }; diff --git a/libraries/config/src/hardfork.cpp b/libraries/config/src/hardfork.cpp index 12c0a3efd1..caa4ba281f 100644 --- a/libraries/config/src/hardfork.cpp +++ b/libraries/config/src/hardfork.cpp @@ -30,7 +30,6 @@ Json::Value enc_json(const Hardforks& obj) { rewards[std::to_string(i->first)] = i->second; } - json["fee_rewards_block_num"] = obj.fee_rewards_block_num; json["magnolia_hf_block_num"] = obj.magnolia_hf_block_num; return json; @@ -50,14 +49,9 @@ void dec_json(const Json::Value& json, Hardforks& obj) { assert(e.isObject()); for (auto itr = e.begin(); itr != e.end(); ++itr) { - obj.rewards_distribution_frequency[itr.key().asUInt64()] = itr->asUInt64(); + obj.rewards_distribution_frequency[dev::getUInt(itr.key())] = dev::getUInt(*itr); } } - - if (const auto& e = json["fee_rewards_block_num"]) { - obj.fee_rewards_block_num = dev::getUInt(e); - } - if (const auto& e = json["magnolia_hf_block_num"]) { obj.magnolia_hf_block_num = dev::getUInt(e); } diff --git a/libraries/core_libs/consensus/include/final_chain/state_api.hpp b/libraries/core_libs/consensus/include/final_chain/state_api.hpp index c30763f864..f4825789e9 100644 --- a/libraries/core_libs/consensus/include/final_chain/state_api.hpp +++ b/libraries/core_libs/consensus/include/final_chain/state_api.hpp @@ -19,8 +19,10 @@ class StateAPI { std::function get_blk_hash_; taraxa_evm_GetBlockHash get_blk_hash_c_; taraxa_evm_state_API_ptr this_c_; - dev::RLPStream rlp_enc_transition_state_; - StateTransitionResult result_buf_transition_state_; + dev::RLPStream rlp_enc_execution_result_; + TransactionsExecutionResult result_buf_execution_result_; + dev::RLPStream rlp_enc_rewards_distribution_; + RewardsDistributionResult result_buf_rewards_distribution_; string db_path_; public: @@ -41,10 +43,12 @@ class StateAPI { bytes trace(EthBlockNumber blk_num, const EVMBlock& blk, const std::vector trx, std::optional params = {}) const; StateDescriptor get_last_committed_state_descriptor() const; - const StateTransitionResult& transition_state(const EVMBlock& block, - const util::RangeView& transactions, - const std::vector& rewards_stats = {}); + + const TransactionsExecutionResult& execute_transactions(const EVMBlock& block, + const util::RangeView& transactions); + const RewardsDistributionResult& distribute_rewards(const std::vector& rewards_stats); void transition_state_commit(); + void create_snapshot(PbftPeriod period); void prune(const std::vector& state_root_to_keep, EthBlockNumber blk_num); diff --git a/libraries/core_libs/consensus/include/final_chain/state_api_data.hpp b/libraries/core_libs/consensus/include/final_chain/state_api_data.hpp index 6166e58af4..cf828cabb6 100644 --- a/libraries/core_libs/consensus/include/final_chain/state_api_data.hpp +++ b/libraries/core_libs/consensus/include/final_chain/state_api_data.hpp @@ -72,8 +72,13 @@ struct ExecutionResult { HAS_RLP_FIELDS }; -struct StateTransitionResult { +struct TransactionsExecutionResult { std::vector execution_results; + + HAS_RLP_FIELDS +}; + +struct RewardsDistributionResult { h256 state_root; u256 total_reward; diff --git a/libraries/core_libs/consensus/include/rewards/block_stats.hpp b/libraries/core_libs/consensus/include/rewards/block_stats.hpp index 2e80eff84b..1d992600af 100644 --- a/libraries/core_libs/consensus/include/rewards/block_stats.hpp +++ b/libraries/core_libs/consensus/include/rewards/block_stats.hpp @@ -23,7 +23,8 @@ class BlockStats { * @param dpos_vote_count - votes count for previous block * @param committee_size */ - BlockStats(const PeriodData& block, uint64_t dpos_vote_count, uint32_t committee_size); + BlockStats(const PeriodData& block, const std::vector& trxs_gas_used, uint64_t dpos_vote_count, + uint32_t committee_size); HAS_RLP_FIELDS @@ -34,22 +35,24 @@ class BlockStats { * @param block */ void processStats(const PeriodData& block); + /** - * @brief In case unique tx_hash is provided, it is mapped to it's validator's address + validator's unique txs count - * is incremented. If provided tx_hash was already processed, nothing happens + * @brief Prepare fee_by_trx_hash_ map with trx fee by trx hash * - * @param tx_hash - * @param validator - * @return true in case tx_hash was unique and processed, otherwise false + * @param transactions collection with transactions included in the block + * @param trxs_gas_used collection with gas used by transactions */ - bool addTransaction(const trx_hash_t& tx_hash, const addr_t& validator); + void initFeeByTrxHash(const SharedTransactions& transactions, const std::vector& trxs_gas_used); /** - * @param tx_hash - * @return dag block validator, who included tx_hash as first in his block. If no validator is found, - * empty optional is returned + * @brief In case unique trx_hash is provided, it is mapped to it's validator's address + validator's unique trxs + * count is incremented. If provided trx_hash was already processed, nothing happens + * + * @param trx_hash + * @param validator + * @return true in case trx_hash was unique and processed, otherwise false */ - std::optional getTransactionValidator(const trx_hash_t& tx_hash); + bool addTransaction(const trx_hash_t& trx_hash, const addr_t& validator); /** * @brief In case unique vote is provided, author's votes weight is updated. If provided vote was @@ -68,20 +71,18 @@ class BlockStats { // Validator cert voted block weight uint64_t vote_weight_ = 0; + u256 fees_rewards_ = 0; + HAS_RLP_FIELDS }; // Pbft block author addr_t block_author_; - // Transactions validators: tx hash -> validator that included it as first in his block - std::unordered_map validator_by_tx_hash_; - - // Vector with all transactions validators, who included provided block.transactions as first in dag block, - // e.g. returned validator on position 2 included transaction block.transactions[2] as first in his dag block - std::vector txs_validators_; + // Fee by trx : trx hash -> fee + std::unordered_map fee_by_trx_hash_; - // Txs stats: validator -> ValidatorStats + // Validator stats: validator -> ValidatorStats std::unordered_map validators_stats_; // Total rewardable(with 1 or more unique transactions) DAG blocks count diff --git a/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp b/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp index 22fca73188..648917ff14 100644 --- a/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp +++ b/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp @@ -3,29 +3,29 @@ #include "storage/storage.hpp" namespace taraxa::rewards { -/* +/** * @brief class that is managing rewards stats processing and hardforks(intervals changes) * So intermediate blocks stats are stored in the vector in data(to restore on the node restart) * and full list of interval stats is returned in the end of interval */ class Stats { public: - Stats(uint32_t committee_size, const Hardforks::RewardsDistributionMap& rdm, std::shared_ptr db, + Stats(uint32_t committee_size, const Hardforks& hardforks, std::shared_ptr db, std::function&& dpos_eligible_total_vote_count); - /* + /** * @brief processing passed block and returns stats that should be processed at this block * @param current_blk block to process * @return vector that should be processed at current block */ - std::vector processStats(const PeriodData& current_blk); + std::vector processStats(const PeriodData& current_blk, const std::vector& trxs_gas_used); protected: - /* + /** * @brief load current interval stats from database */ void loadFromDb(); - /* + /** * @brief returns rewards distribution frequency for specified period */ uint32_t getCurrentDistributionFrequency(uint64_t current_period) const; @@ -34,19 +34,19 @@ class Stats { * @param current_blk block to process * @return block statistics needed for rewards distribution */ - BlockStats getBlockStats(const PeriodData& current_blk); - /* + BlockStats getBlockStats(const PeriodData& current_blk, std::vector trxs_fees); + /** * @brief saves stats to database to not lose this data in case of node restart */ void saveBlockStats(uint64_t number, const BlockStats& stats); - /* + /** * @brief called on start of new rewards interval. clears blocks_stats_ collection * and removes all data saved in db column */ void clear(); const uint32_t kCommitteeSize; - const Hardforks::RewardsDistributionMap kRewardsDistributionFrequency; + const Hardforks kHardforks; std::shared_ptr db_; const std::function dpos_eligible_total_vote_count_; std::vector blocks_stats_; diff --git a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp index 6cb07ce631..c4b848cd4e 100644 --- a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp +++ b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp @@ -58,7 +58,7 @@ class FinalChainImpl final : public FinalChain { kLightNode(config.is_light_node), kLightNodeHistory(config.light_node_history), kMaxLevelsPerPeriod(config.max_levels_per_period), - rewards_(config.genesis.pbft.committee_size, config.genesis.state.hardforks.rewards_distribution_frequency, db_, + rewards_(config.genesis.pbft.committee_size, config.genesis.state.hardforks, db_, [this](EthBlockNumber n) { return dpos_eligible_total_vote_count(n); }), block_headers_cache_(config.final_chain_cache_in_blocks, [this](uint64_t blk) { return get_block_header(blk); }), @@ -150,8 +150,6 @@ class FinalChainImpl final : public FinalChain { std::shared_ptr&& anchor) { auto batch = db_->createWriteBatch(); - auto rewards_stats = rewards_.processStats(new_blk); - block_applying_emitter_.emit(block_header()->number + 1); /* @@ -171,13 +169,16 @@ class FinalChainImpl final : public FinalChain { } } */ - auto const& [exec_results, state_root, total_reward] = - state_api_.transition_state({new_blk.pbft_blk->getBeneficiary(), kBlockGasLimit, - new_blk.pbft_blk->getTimestamp(), BlockHeader::difficulty()}, - to_state_api_transactions(new_blk.transactions), rewards_stats); + auto const& [exec_results] = + state_api_.execute_transactions({new_blk.pbft_blk->getBeneficiary(), kBlockGasLimit, + new_blk.pbft_blk->getTimestamp(), BlockHeader::difficulty()}, + to_state_api_transactions(new_blk.transactions)); TransactionReceipts receipts; receipts.reserve(exec_results.size()); + std::vector transactions_gas_used; + transactions_gas_used.reserve(exec_results.size()); + gas_t cumulative_gas_used = 0; for (auto const& r : exec_results) { LogEntries logs; @@ -185,6 +186,7 @@ class FinalChainImpl final : public FinalChain { std::transform(r.logs.cbegin(), r.logs.cend(), std::back_inserter(logs), [](const auto& l) { return LogEntry{l.address, l.topics, l.data}; }); + transactions_gas_used.push_back(r.gas_used); receipts.emplace_back(TransactionReceipt{ r.code_err.empty() && r.consensus_err.empty(), r.gas_used, @@ -193,6 +195,10 @@ class FinalChainImpl final : public FinalChain { r.new_contract_addr ? std::optional(r.new_contract_addr) : std::nullopt, }); } + + auto rewards_stats = rewards_.processStats(new_blk, transactions_gas_used); + 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); // Update number of executed DAG blocks and transactions diff --git a/libraries/core_libs/consensus/src/final_chain/state_api.cpp b/libraries/core_libs/consensus/src/final_chain/state_api.cpp index 3e29cf8b97..fce6a39a56 100644 --- a/libraries/core_libs/consensus/src/final_chain/state_api.cpp +++ b/libraries/core_libs/consensus/src/final_chain/state_api.cpp @@ -118,8 +118,9 @@ StateAPI::StateAPI(decltype(get_blk_hash_) get_blk_hash, const Config& state_con }, }, db_path_(opts_db.db_path) { - result_buf_transition_state_.execution_results.reserve(opts.expected_max_trx_per_block); - rlp_enc_transition_state_.reserve(opts.expected_max_trx_per_block * 1024, opts.expected_max_trx_per_block * 128); + result_buf_execution_result_.execution_results.reserve(opts.expected_max_trx_per_block); + rlp_enc_execution_result_.reserve(opts.expected_max_trx_per_block * 1024, opts.expected_max_trx_per_block * 128); + rlp_enc_rewards_distribution_.reserve(opts.expected_max_trx_per_block * 1024, opts.expected_max_trx_per_block * 128); dev::RLPStream encoding; util::rlp_tuple(encoding, reinterpret_cast(&get_blk_hash_c_), state_config, opts, opts_db); ErrorHandler err_h; @@ -175,14 +176,21 @@ StateDescriptor StateAPI::get_last_committed_state_descriptor() const { return ret; } -const StateTransitionResult& StateAPI::transition_state(const EVMBlock& block, - const util::RangeView& transactions, - const std::vector& rewards_stats) { - result_buf_transition_state_.execution_results.clear(); - rlp_enc_transition_state_.clear(); - c_method_args_rlp( - this_c_, rlp_enc_transition_state_, result_buf_transition_state_, block, transactions, rewards_stats); - return result_buf_transition_state_; +const TransactionsExecutionResult& StateAPI::execute_transactions(const EVMBlock& block, + const util::RangeView& transactions) { + result_buf_execution_result_.execution_results.clear(); + rlp_enc_execution_result_.clear(); + c_method_args_rlp( + this_c_, rlp_enc_execution_result_, result_buf_execution_result_, block, transactions); + return result_buf_execution_result_; +} + +const RewardsDistributionResult& StateAPI::distribute_rewards(const std::vector& rewards_stats) { + // result_buf_rewards_distribution_; + rlp_enc_rewards_distribution_.clear(); + c_method_args_rlp( + this_c_, rlp_enc_rewards_distribution_, result_buf_rewards_distribution_, rewards_stats); + return result_buf_rewards_distribution_; } void StateAPI::transition_state_commit() { diff --git a/libraries/core_libs/consensus/src/final_chain/state_api_data.cpp b/libraries/core_libs/consensus/src/final_chain/state_api_data.cpp index e41715dd2e..d90b733080 100644 --- a/libraries/core_libs/consensus/src/final_chain/state_api_data.cpp +++ b/libraries/core_libs/consensus/src/final_chain/state_api_data.cpp @@ -16,7 +16,8 @@ RLP_FIELDS_DEFINE(EVMTransaction, from, gas_price, to, nonce, value, gas, input) RLP_FIELDS_DEFINE(UncleBlock, number, author) RLP_FIELDS_DEFINE(LogRecord, address, topics, data) RLP_FIELDS_DEFINE(ExecutionResult, code_retval, new_contract_addr, logs, gas_used, code_err, consensus_err) -RLP_FIELDS_DEFINE(StateTransitionResult, execution_results, state_root, total_reward) +RLP_FIELDS_DEFINE(TransactionsExecutionResult, execution_results) +RLP_FIELDS_DEFINE(RewardsDistributionResult, state_root, total_reward) RLP_FIELDS_DEFINE(Account, nonce, balance, storage_root_hash, code_hash, code_size) RLP_FIELDS_DEFINE(StateDescriptor, blk_num, state_root) RLP_FIELDS_DEFINE(Tracing, vmTrace, trace, stateDiff) diff --git a/libraries/core_libs/consensus/src/rewards/block_stats.cpp b/libraries/core_libs/consensus/src/rewards/block_stats.cpp index 2e6d1bfbeb..5888ee4c88 100644 --- a/libraries/core_libs/consensus/src/rewards/block_stats.cpp +++ b/libraries/core_libs/consensus/src/rewards/block_stats.cpp @@ -6,33 +6,40 @@ namespace taraxa::rewards { -BlockStats::BlockStats(const PeriodData& block, uint64_t dpos_vote_count, uint32_t committee_size) +BlockStats::BlockStats(const PeriodData& block, const std::vector& trxs_gas_used, uint64_t dpos_vote_count, + uint32_t committee_size) : block_author_(block.pbft_blk->getBeneficiary()), max_votes_weight_(std::min(committee_size, dpos_vote_count)) { + initFeeByTrxHash(block.transactions, trxs_gas_used); processStats(block); } -bool BlockStats::addTransaction(const trx_hash_t& tx_hash, const addr_t& validator) { - auto found_tx = validator_by_tx_hash_.find(tx_hash); +void BlockStats::initFeeByTrxHash(const SharedTransactions& transactions, const std::vector& trxs_gas_used) { + // assert(transactions.size() == trxs_gas_used.size()); - // Already processed tx - if (found_tx != validator_by_tx_hash_.end()) { - return false; + for (uint32_t i = 0; i < transactions.size(); ++i) { + const auto& trx = transactions[i]; + uint64_t gas_used = 0; + // if its not empty then we will add this to validator stats and distribute it + if (!trxs_gas_used.empty()) { + gas_used = trxs_gas_used[i]; + } + fee_by_trx_hash_.emplace(trx->getHash(), trx->getGasPrice() * gas_used); } - - // New tx - validator_by_tx_hash_[tx_hash] = validator; - - return true; } -std::optional BlockStats::getTransactionValidator(const trx_hash_t& tx_hash) { - auto found_tx = validator_by_tx_hash_.find(tx_hash); - if (found_tx == validator_by_tx_hash_.end()) { - return {}; +bool BlockStats::addTransaction(const trx_hash_t& trx_hash, const addr_t& validator) { + auto itr = fee_by_trx_hash_.find(trx_hash); + if (itr == fee_by_trx_hash_.end()) { + // No fee record found for transaction means that transaction was processed before + return false; } - return {found_tx->second}; + validators_stats_[validator].fees_rewards_ += itr->second; + + fee_by_trx_hash_.erase(itr); + + return true; } bool BlockStats::addVote(const std::shared_ptr& vote) { @@ -61,7 +68,9 @@ std::set toTrxHashesSet(const SharedTransactions& transactions) { } void BlockStats::processStats(const PeriodData& block) { - validator_by_tx_hash_.reserve(block.transactions.size()); + // total unique transactions count should be always equal to transactions count in block + assert(fee_by_trx_hash_.size() == block.transactions.size()); + validators_stats_.reserve(std::max(block.dag_blocks.size(), block.previous_block_cert_votes.size())); auto block_transactions_hashes_ = toTrxHashesSet(block.transactions); @@ -85,25 +94,14 @@ void BlockStats::processStats(const PeriodData& block) { total_dag_blocks_count_ += 1; } } - // total_unique_txs_count_ should be always equal to transactions count in block - assert(validator_by_tx_hash_.size() == block.transactions.size()); for (const auto& vote : block.previous_block_cert_votes) { addVote(vote); } - - txs_validators_.reserve(block.transactions.size()); - for (const auto& tx : block.transactions) { - // Non-executed trxs - auto tx_validator = getTransactionValidator(tx->getHash()); - assert(tx_validator.has_value()); - - txs_validators_.push_back(*tx_validator); - } } -RLP_FIELDS_DEFINE(BlockStats::ValidatorStats, dag_blocks_count_, vote_weight_) -RLP_FIELDS_DEFINE(BlockStats, block_author_, validators_stats_, txs_validators_, total_dag_blocks_count_, - total_votes_weight_, max_votes_weight_) +RLP_FIELDS_DEFINE(BlockStats::ValidatorStats, dag_blocks_count_, vote_weight_, fees_rewards_) +RLP_FIELDS_DEFINE(BlockStats, block_author_, validators_stats_, total_dag_blocks_count_, total_votes_weight_, + max_votes_weight_) } // namespace taraxa::rewards \ No newline at end of file diff --git a/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp b/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp index 1d4a03f5e5..9b6fc5dbf9 100644 --- a/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp +++ b/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp @@ -3,10 +3,10 @@ #include "storage/storage.hpp" namespace taraxa::rewards { -Stats::Stats(uint32_t committee_size, const Hardforks::RewardsDistributionMap& rdm, std::shared_ptr db, +Stats::Stats(uint32_t committee_size, const Hardforks& hardforks, std::shared_ptr db, std::function&& dpos_eligible_total_vote_count) : kCommitteeSize(committee_size), - kRewardsDistributionFrequency(rdm), + kHardforks(hardforks), db_(std::move(db)), dpos_eligible_total_vote_count_(dpos_eligible_total_vote_count) { loadFromDb(); @@ -27,39 +27,44 @@ void Stats::saveBlockStats(uint64_t period, const BlockStats& stats) { } uint32_t Stats::getCurrentDistributionFrequency(uint64_t current_block) const { - auto itr = kRewardsDistributionFrequency.upper_bound(current_block); - if (kRewardsDistributionFrequency.empty() || itr == kRewardsDistributionFrequency.begin()) { + auto distribution_frequencies = kHardforks.rewards_distribution_frequency; + auto itr = distribution_frequencies.upper_bound(current_block); + if (distribution_frequencies.empty() || itr == distribution_frequencies.begin()) { return 1; } return (--itr)->second; } void Stats::clear() { + // clear need to be called on vector because it was moved before blocks_stats_.clear(); db_->deleteColumnData(DB::Columns::block_rewards_stats); } -BlockStats Stats::getBlockStats(const PeriodData& blk) { +BlockStats Stats::getBlockStats(const PeriodData& blk, std::vector trxs_fees) { uint64_t dpos_vote_count = kCommitteeSize; // Block zero if (!blk.previous_block_cert_votes.empty()) [[likely]] { dpos_vote_count = dpos_eligible_total_vote_count_(blk.previous_block_cert_votes[0]->getPeriod() - 1); } + if (blk.pbft_blk->getPeriod() < kHardforks.magnolia_hf_block_num) { + trxs_fees = {}; + } - return BlockStats{blk, dpos_vote_count, kCommitteeSize}; + return BlockStats{blk, trxs_fees, dpos_vote_count, kCommitteeSize}; } -std::vector Stats::processStats(const PeriodData& current_blk) { +std::vector Stats::processStats(const PeriodData& current_blk, const std::vector& trxs_gas_used) { const auto current_period = current_blk.pbft_blk->getPeriod(); const auto frequency = getCurrentDistributionFrequency(current_period); - + auto block_stats = getBlockStats(current_blk, trxs_gas_used); // Distribute rewards every block if (frequency == 1) { - return {getBlockStats(current_blk)}; + return {block_stats}; } - blocks_stats_.push_back(getBlockStats(current_blk)); + blocks_stats_.push_back(std::move(block_stats)); // Blocks between distribution. Process and save for future processing if (current_period % frequency != 0) { // Save to db, so in case of restart data could be just loaded for the period diff --git a/tests/final_chain_test.cpp b/tests/final_chain_test.cpp index 90a27db882..c8b8c172c1 100644 --- a/tests/final_chain_test.cpp +++ b/tests/final_chain_test.cpp @@ -30,6 +30,19 @@ struct FinalChainTest : WithDataDir { bool assume_only_toplevel_transfers = true; std::unordered_map expected_balances; uint64_t expected_blk_num = 0; + dev::KeyPair dag_proposer_keys = dev::KeyPair::create(); + dev::KeyPair pbft_proposer_keys = dev::KeyPair::create(); + void create_validators() { + dev::KeyPair validator_owner_keys = dev::KeyPair::create(); + cfg.genesis.state.initial_balances[validator_owner_keys.address()] = + 10 * cfg.genesis.state.dpos.validator_maximum_stake; + for (const auto& keys : {dag_proposer_keys, pbft_proposer_keys}) { + const auto [vrf_key, _] = taraxa::vrf_wrapper::getVrfKeyPair(); + state_api::ValidatorInfo validator{keys.address(), validator_owner_keys.address(), vrf_key, 0, "", "", {}}; + validator.delegations.emplace(validator_owner_keys.address(), cfg.genesis.state.dpos.validator_maximum_stake); + cfg.genesis.state.dpos.initial_validators.emplace_back(validator); + } + } void init() { SUT = NewFinalChain(db, cfg); const auto& effective_balances = effective_initial_balances(cfg.genesis.state); @@ -50,13 +63,12 @@ struct FinalChainTest : WithDataDir { trx_hashes.emplace_back(trx->getHash()); } - auto proposer_keys = dev::KeyPair::create(); - DagBlock dag_blk({}, {}, {}, trx_hashes, {}, {}, proposer_keys.secret()); + DagBlock dag_blk({}, {}, {}, trx_hashes, {}, {}, dag_proposer_keys.secret()); db->saveDagBlock(dag_blk); std::vector reward_votes_hashes; auto pbft_block = std::make_shared(kNullBlockHash, kNullBlockHash, kNullBlockHash, kNullBlockHash, expected_blk_num, - addr_t::random(), proposer_keys.secret(), std::move(reward_votes_hashes)); + addr_t::random(), pbft_proposer_keys.secret(), std::move(reward_votes_hashes)); std::vector> votes; PeriodData period_data(pbft_block, votes); @@ -734,15 +746,47 @@ TEST_F(FinalChainTest, fee_rewards_distribution) { const auto& sk = sender_keys.secret(); cfg.genesis.state.initial_balances = {}; cfg.genesis.state.initial_balances[addr] = 100000; + cfg.genesis.state.hardforks.magnolia_hf_block_num = 2; + create_validators(); init(); const auto gas_price = 1; - auto trx1 = std::make_shared(1, 100, gas_price, gas, dev::bytes(), sk, receiver); + { + auto trx = std::make_shared(1, 100, gas_price, gas, dev::bytes(), sk, receiver); - auto res = advance({trx1}); - auto gas_used = res->trx_receipts.front().gas_used; - auto blk = SUT->block_header(expected_blk_num); - auto proposer_balance = SUT->getBalance(blk->author); - EXPECT_EQ(proposer_balance.first, gas_used * gas_price); + auto res = advance({trx}); + auto gas_used = res->trx_receipts.front().gas_used; + EXPECT_EQ(SUT->getBalance(pbft_proposer_keys.address()).first, gas_used * gas_price); + } + { + auto trx = std::make_shared(2, 100, gas_price, gas, dev::bytes(), sk, receiver); + + auto res = advance({trx}); + EXPECT_EQ(2, expected_blk_num); + EXPECT_EQ(res->trx_receipts.size(), 1); + auto gas_used = res->trx_receipts.front().gas_used; + auto dags = db->getFinalizedDagBlockByPeriod(expected_blk_num); + EXPECT_EQ(dags.size(), 1); + EXPECT_EQ(SUT->getBalance(dag_proposer_keys.address()).first, 0); + + auto get_commission_rewards = [&](addr_t a) { + const addr_t dpos_contract("0x00000000000000000000000000000000000000FE"); + auto ret = SUT->call({ + addr, + 0, + dpos_contract, + 0, + 0, + 1000000, + // getValidator() + dev::fromHex("0x1904bb2e000000000000000000000000" + a.toString()), + }); + EXPECT_GE(ret.code_retval.size(), 96); + // for some reason parsing u256 from bytes is failing check after + auto hex_commission = "0x" + dev::toHex(bytes(ret.code_retval.begin() + 64, ret.code_retval.begin() + 96)); + return u256(hex_commission); + }; + EXPECT_EQ(get_commission_rewards(dag_proposer_keys.address()), u256(gas_used * gas_price)); + } } // This test should be last as state_api isn't destructed correctly because of exception diff --git a/tests/rewards_stats_test.cpp b/tests/rewards_stats_test.cpp index cce156c735..8e2dbea278 100644 --- a/tests/rewards_stats_test.cpp +++ b/tests/rewards_stats_test.cpp @@ -19,13 +19,14 @@ struct RewardsStatsTest : NodesTest {}; class TestableRewardsStats : public rewards::Stats { public: TestableRewardsStats(const Hardforks::RewardsDistributionMap& rdm, std::shared_ptr db) - : rewards::Stats(100, rdm, db, [](auto) { return 100; }) {} + : rewards::Stats(100, Hardforks{rdm, 0}, db, [](auto) { return 100; }) {} std::vector getStats() { return blocks_stats_; } }; class TestableBlockStats : public rewards::BlockStats { public: const addr_t& getAuthor() const { return block_author_; } + const auto& getValidatorStats() const { return validators_stats_; } }; TEST_F(RewardsStatsTest, defaultDistribution) { @@ -36,7 +37,7 @@ TEST_F(RewardsStatsTest, defaultDistribution) { for (auto i = 1; i < 5; ++i) { PeriodData block(make_simple_pbft_block(blk_hash_t(i), i), empty_votes); - auto stats = rewards_stats.processStats(block); + auto stats = rewards_stats.processStats(block, {}); ASSERT_EQ(stats.size(), 1); ASSERT_TRUE(rewards_stats.getStats().empty()); } @@ -58,7 +59,7 @@ TEST_F(RewardsStatsTest, statsSaving) { block_authors.push_back(kp.address()); PeriodData block(make_simple_pbft_block(blk_hash_t(i), i, kp.secret()), empty_votes); - auto stats = rewards_stats.processStats(block); + auto stats = rewards_stats.processStats(block, {}); ASSERT_EQ(rewards_stats.getStats().size(), block_authors.size()); ASSERT_TRUE(stats.empty()); } @@ -92,14 +93,14 @@ TEST_F(RewardsStatsTest, statsCleaning) { block_authors.push_back(kp.address()); PeriodData block(make_simple_pbft_block(blk_hash_t(i), i, kp.secret()), empty_votes); - auto stats = rewards_stats.processStats(block); + auto stats = rewards_stats.processStats(block, {}); ASSERT_EQ(rewards_stats.getStats().size(), block_authors.size()); ASSERT_TRUE(stats.empty()); } // Process block 5 after which we should have no stats elements in db PeriodData block(make_simple_pbft_block(blk_hash_t(5), 5), empty_votes); - rewards_stats.processStats(block); + rewards_stats.processStats(block, {}); } // Load from db @@ -121,7 +122,7 @@ TEST_F(RewardsStatsTest, statsProcessing) { block_authors.push_back(kp.address()); PeriodData block(make_simple_pbft_block(blk_hash_t(i), i, kp.secret()), empty_votes); - auto stats = rewards_stats.processStats(block); + auto stats = rewards_stats.processStats(block, {}); ASSERT_TRUE(stats.empty()); ASSERT_EQ(rewards_stats.getStats().size(), block_authors.size()); } @@ -130,7 +131,7 @@ TEST_F(RewardsStatsTest, statsProcessing) { block_authors.push_back(kp.address()); PeriodData block(make_simple_pbft_block(blk_hash_t(10), 10, kp.secret()), empty_votes); - auto stats = rewards_stats.processStats(block); + auto stats = rewards_stats.processStats(block, {}); ASSERT_EQ(stats.size(), block_authors.size()); for (size_t i = 0; i < stats.size(); ++i) { @@ -151,22 +152,64 @@ TEST_F(RewardsStatsTest, distributionChange) { uint64_t period = 1; for (; period <= 5; ++period) { PeriodData block(make_simple_pbft_block(blk_hash_t(period), period), empty_votes); - auto stats = rewards_stats.processStats(block); + auto stats = rewards_stats.processStats(block, {}); ASSERT_FALSE(stats.empty()); } { // make blocks [1,9] and process them. output of processStats should be empty for (; period < 10; ++period) { PeriodData block(make_simple_pbft_block(blk_hash_t(period), period), empty_votes); - auto stats = rewards_stats.processStats(block); + auto stats = rewards_stats.processStats(block, {}); ASSERT_TRUE(stats.empty()); } PeriodData block(make_simple_pbft_block(blk_hash_t(period), period), empty_votes); - auto stats = rewards_stats.processStats(block); + auto stats = rewards_stats.processStats(block, {}); } PeriodData block(make_simple_pbft_block(blk_hash_t(period), period), empty_votes); - auto stats = rewards_stats.processStats(block); + auto stats = rewards_stats.processStats(block, {}); +} + +TEST_F(RewardsStatsTest, feeRewards) { + auto db = std::make_shared(data_dir / "db"); + auto pbft_proposer = dev::KeyPair::create(); + auto dag_proposer = dev::KeyPair::create(); + + Hardforks::RewardsDistributionMap distribution{}; + + auto rewards_stats = TestableRewardsStats(distribution, db); + + std::vector> empty_votes; + uint64_t period = 1; + uint64_t nonce = 1; + + const auto trx_gas_fee = 1000000; + auto trx = std::make_shared(nonce++, 0, 1, trx_gas_fee, dev::fromHex(samples::greeter_contract_code), + pbft_proposer.secret()); + + DagBlock dag_blk({}, {}, {}, {trx->getHash()}, {}, {}, dag_proposer.secret()); + db->saveDagBlock(dag_blk); + std::vector reward_votes_hashes; + auto pbft_block = + std::make_shared(kNullBlockHash, kNullBlockHash, kNullBlockHash, kNullBlockHash, period, + addr_t::random(), pbft_proposer.secret(), std::move(reward_votes_hashes)); + + PeriodData period_data(pbft_block, empty_votes); + period_data.dag_blocks.push_back(dag_blk); + period_data.transactions = {trx}; + + auto stats = rewards_stats.processStats(period_data, {trx_gas_fee}).front(); + + auto testable_stats = reinterpret_cast(&stats); + auto validators_stats = testable_stats->getValidatorStats(); + // stats should be only for dag block proposer + ASSERT_EQ(validators_stats.size(), 1); + for (const auto& vs : validators_stats) { + if (vs.first == dag_proposer.address()) { + ASSERT_EQ(vs.second.dag_blocks_count_, 1); + ASSERT_EQ(vs.second.fees_rewards_, trx_gas_fee); + } + } } } // namespace taraxa::core_tests diff --git a/tests/state_api_test.cpp b/tests/state_api_test.cpp index 9d27f86220..a6b0ac6d90 100644 --- a/tests/state_api_test.cpp +++ b/tests/state_api_test.cpp @@ -201,7 +201,8 @@ TEST_F(StateAPITest, DISABLED_eth_mainnet_smoke) { progress_pct_log_threshold += 10; } auto const& test_block = test_blocks[blk_num]; - auto const& result = SUT.transition_state(test_block.evm_block, test_block.transactions); + SUT.execute_transactions(test_block.evm_block, test_block.transactions); + const auto& result = SUT.distribute_rewards({}); ASSERT_EQ(result.state_root, test_block.state_root); SUT.transition_state_commit(); } diff --git a/tests/test_util/src/test_util.cpp b/tests/test_util/src/test_util.cpp index 7a09238bbc..b1ca6064af 100644 --- a/tests/test_util/src/test_util.cpp +++ b/tests/test_util/src/test_util.cpp @@ -122,12 +122,6 @@ std::vector getOrderedDagBlocks(const std::shared_ptr& db return res; } -addr_t make_addr(uint8_t i) { - addr_t ret; - ret[10] = i; - return ret; -} - void wait_for_balances(const shared_nodes_t& nodes, const expected_balances_map_t& balances, wait_opts to_wait) { wait(to_wait, [&](auto& ctx) { for (const auto& node : nodes) { From 65cab404df7496d8e86f3ec7bdcc7c2ed528d031 Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Thu, 3 Aug 2023 17:42:21 +0200 Subject: [PATCH 117/134] chore: resolve discussion --- .../core_libs/consensus/include/rewards/rewards_stats.hpp | 2 +- libraries/core_libs/consensus/src/rewards/rewards_stats.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp b/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp index 648917ff14..b8f0dc2c07 100644 --- a/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp +++ b/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp @@ -34,7 +34,7 @@ class Stats { * @param current_blk block to process * @return block statistics needed for rewards distribution */ - BlockStats getBlockStats(const PeriodData& current_blk, std::vector trxs_fees); + BlockStats getBlockStats(const PeriodData& current_blk, const std::vector& trxs_fees); /** * @brief saves stats to database to not lose this data in case of node restart */ diff --git a/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp b/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp index 9b6fc5dbf9..e4184cb467 100644 --- a/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp +++ b/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp @@ -41,7 +41,7 @@ void Stats::clear() { db_->deleteColumnData(DB::Columns::block_rewards_stats); } -BlockStats Stats::getBlockStats(const PeriodData& blk, std::vector trxs_fees) { +BlockStats Stats::getBlockStats(const PeriodData& blk, const std::vector& trxs_fees) { uint64_t dpos_vote_count = kCommitteeSize; // Block zero @@ -49,7 +49,7 @@ BlockStats Stats::getBlockStats(const PeriodData& blk, std::vector trxs_f dpos_vote_count = dpos_eligible_total_vote_count_(blk.previous_block_cert_votes[0]->getPeriod() - 1); } if (blk.pbft_blk->getPeriod() < kHardforks.magnolia_hf_block_num) { - trxs_fees = {}; + return BlockStats{blk, {}, dpos_vote_count, kCommitteeSize}; } return BlockStats{blk, trxs_fees, dpos_vote_count, kCommitteeSize}; From 646695763da7407e8ed8f8d74802cdd598b7bdb8 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 9 Aug 2023 16:36:43 -0700 Subject: [PATCH 118/134] add slashing config --- libraries/config/include/config/hardfork.hpp | 2 ++ .../config/include/config/state_config.hpp | 11 +++++++++++ libraries/config/src/state_config.cpp | 19 ++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/libraries/config/include/config/hardfork.hpp b/libraries/config/include/config/hardfork.hpp index 41b23b3b59..01652b1706 100644 --- a/libraries/config/include/config/hardfork.hpp +++ b/libraries/config/include/config/hardfork.hpp @@ -38,6 +38,8 @@ struct Hardforks { // 2. changing fee rewards distribution. // Rewards will be distributed to dag blocks creator commission pool, but not directly to the balance of pbft block // creator. + // 3. Introducing slashing/jailing contract - in case someone double votes - he is jailed for N blocks and unable to + // participate in consensus uint64_t magnolia_hf_block_num = -1; HAS_RLP_FIELDS diff --git a/libraries/config/include/config/state_config.hpp b/libraries/config/include/config/state_config.hpp index b6aa692b64..6cd1c11813 100644 --- a/libraries/config/include/config/state_config.hpp +++ b/libraries/config/include/config/state_config.hpp @@ -37,6 +37,14 @@ struct ValidatorInfo { Json::Value enc_json(const ValidatorInfo& obj); void dec_json(const Json::Value& json, ValidatorInfo& obj); +struct Slashing { + uint32_t jail_time = 0; // number of blocks + + HAS_RLP_FIELDS +}; +Json::Value enc_json(const Slashing& obj); +void dec_json(const Json::Value& json, Slashing& obj); + struct DPOSConfig { u256 eligibility_balance_threshold; u256 vote_eligibility_balance_step; @@ -52,6 +60,9 @@ struct DPOSConfig { uint16_t yield_percentage = 0; // [%] std::vector initial_validators; + // Slashing + Slashing slashing; + HAS_RLP_FIELDS }; Json::Value enc_json(const DPOSConfig& obj); diff --git a/libraries/config/src/state_config.cpp b/libraries/config/src/state_config.cpp index 44dfad95d1..f32a5d743d 100644 --- a/libraries/config/src/state_config.cpp +++ b/libraries/config/src/state_config.cpp @@ -74,6 +74,17 @@ void dec_json(const Json::Value& json, ValidatorInfo& obj) { dec_json(json["delegations"], obj.delegations); } +Json::Value enc_json(const Slashing& obj) { + Json::Value json(Json::objectValue); + json["jail_time"] = dev::toJS(obj.jail_time); + + return json; +} + +void dec_json(const Json::Value& json, Slashing& obj) { + obj.jail_time = static_cast(dev::getUInt(json["jail_time"].asString())); +} + Json::Value enc_json(const DPOSConfig& obj) { Json::Value json(Json::objectValue); json["eligibility_balance_threshold"] = dev::toJS(obj.eligibility_balance_threshold); @@ -93,6 +104,9 @@ Json::Value enc_json(const DPOSConfig& obj) { for (const auto& v : obj.initial_validators) { json["initial_validators"].append(enc_json(v)); } + + json["slashing"] = enc_json(obj.slashing); + return json; } @@ -116,14 +130,17 @@ void dec_json(const Json::Value& json, DPOSConfig& obj) { for (uint32_t i = 0; i < initial_validators_json.size(); ++i) { dec_json(initial_validators_json[i], obj.initial_validators[i]); } + + dec_json(json["slashing"], obj.slashing); } RLP_FIELDS_DEFINE(EVMChainConfig, chain_id) RLP_FIELDS_DEFINE(ValidatorInfo, address, owner, vrf_key, commission, endpoint, description, delegations) +RLP_FIELDS_DEFINE(Slashing, jail_time) RLP_FIELDS_DEFINE(DPOSConfig, eligibility_balance_threshold, vote_eligibility_balance_step, validator_maximum_stake, minimum_deposit, max_block_author_reward, dag_proposers_reward, commission_change_delta, commission_change_frequency, delegation_delay, delegation_locking_period, blocks_per_year, - yield_percentage, initial_validators) + yield_percentage, initial_validators, slashing) RLP_FIELDS_DEFINE(Config, evm_chain_config, initial_balances, dpos, hardforks) RLP_FIELDS_DEFINE(Opts, expected_max_trx_per_block, max_trie_full_node_levels_to_cache) RLP_FIELDS_DEFINE(OptsDB, db_path, disable_most_recent_trie_value_views) From 50c78526039e7312f75181c90728fa43a844b616 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Sat, 12 Aug 2023 21:39:52 -0700 Subject: [PATCH 119/134] added slashing config + updated evm --- .../cli/config_jsons/default/default_genesis.json | 9 +++++---- .../include/cli/config_jsons/devnet/devnet_genesis.json | 9 +++++---- .../cli/config_jsons/mainnet/mainnet_genesis.json | 9 +++++---- .../cli/config_jsons/testnet/testnet_genesis.json | 9 +++++---- libraries/config/include/config/state_config.hpp | 2 +- libraries/config/src/state_config.cpp | 4 +--- 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/libraries/cli/include/cli/config_jsons/default/default_genesis.json b/libraries/cli/include/cli/config_jsons/default/default_genesis.json index 4096d43c01..9234a41178 100644 --- a/libraries/cli/include/cli/config_jsons/default/default_genesis.json +++ b/libraries/cli/include/cli/config_jsons/default/default_genesis.json @@ -54,16 +54,17 @@ }, "vrf_key": "f8d5c00ce9fa3058341e051b36a1e6ccf69df81fb865568b2bf1507d085691e2" } - ] + ], + "slashing": { + "jail_time": "0x5" + } }, "initial_balances": { "2cd4da7d3b345e022ca7e997c2bb3276a4d3d2e9": "0x1027e72f1f12813088000000", "7e4aa664f71de4e9d0b4a6473d796372639bdcde": "0x1027e72f1f12813088000000" }, "hardforks": { - "rewards_distribution_frequency": { - }, - "magnolia_hf_block_num" : 0 + "magnolia_hf_block_num" : "0x0" }, "gas_price": { "blocks": 200, diff --git a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json index 4570b9bdfa..35ea4dcc83 100644 --- a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json @@ -219,7 +219,10 @@ }, "vrf_key": "37bf145ac98e7de7db6e5b933e72737fbf190fd4fb1d193b15cf8b00db30ba30" } - ] + ], + "slashing": { + "jail_time": "0x5" + } }, "initial_balances": { "2cd4da7d3b345e022ca7e997c2bb3276a4d3d2e9": "0x1027e72f1f12813088000000", @@ -227,9 +230,7 @@ "ee1326fbf7d9322e5ea02c6fe5eb63535fceccd1": "0x52b7d2dcc80cd2e4000000" }, "hardforks": { - "rewards_distribution_frequency": { - }, - "magnolia_hf_block_num" : 0 + "magnolia_hf_block_num" : "0x0" }, "gas_price": { "blocks": 200, diff --git a/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json b/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json index 6750a962b0..f8de61bb39 100644 --- a/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json @@ -241,7 +241,10 @@ }, "vrf_key": "a25053afb5316ff12137ae573b6272b1258020454606631308d32cbf776788c0" } - ] + ], + "slashing": { + "jail_time": "0x5" + } }, "initial_balances": { "723304d1357a2334fcf902aa3d232f5139080a1b": "0xd53323b7ca3737afbb45000", @@ -1589,9 +1592,7 @@ "6ba16abe80af8be7c9b6c852c6bd3e381a493c92": "0x4958bc77396e445c569000" }, "hardforks": { - "rewards_distribution_frequency": { - }, - "magnolia_hf_block_num" : 18446744073709551615 + "magnolia_hf_block_num" : "0x0" }, "gas_price": { "blocks": 200, diff --git a/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json b/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json index 7c1377a75b..0a650f1f8f 100644 --- a/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json @@ -87,7 +87,10 @@ }, "vrf_key": "f5f88dcc7fbfa50a576359ae05ace6ca28352fbc40b4ebbc99605e04c6f42883" } - ] + ], + "slashing": { + "jail_time": "0x5" + } }, "initial_balances": { "76870407332398322576505f3c5423d0a71af296": "0x1d14a0219e54822428000000", @@ -95,9 +98,7 @@ "ee1326fbf7d9322e5ea02c6fe5eb63535fceccd1": "0x19d971e4fe8401e74000000" }, "hardforks": { - "rewards_distribution_frequency": { - }, - "magnolia_hf_block_num" : 18446744073709551615 + "magnolia_hf_block_num" : "0x0" }, "gas_price": { "blocks": 200, diff --git a/libraries/config/include/config/state_config.hpp b/libraries/config/include/config/state_config.hpp index 6cd1c11813..bb1bd34207 100644 --- a/libraries/config/include/config/state_config.hpp +++ b/libraries/config/include/config/state_config.hpp @@ -38,7 +38,7 @@ Json::Value enc_json(const ValidatorInfo& obj); void dec_json(const Json::Value& json, ValidatorInfo& obj); struct Slashing { - uint32_t jail_time = 0; // number of blocks + uint64_t jail_time{0}; // number of blocks HAS_RLP_FIELDS }; diff --git a/libraries/config/src/state_config.cpp b/libraries/config/src/state_config.cpp index f32a5d743d..f500d9efee 100644 --- a/libraries/config/src/state_config.cpp +++ b/libraries/config/src/state_config.cpp @@ -81,9 +81,7 @@ Json::Value enc_json(const Slashing& obj) { return json; } -void dec_json(const Json::Value& json, Slashing& obj) { - obj.jail_time = static_cast(dev::getUInt(json["jail_time"].asString())); -} +void dec_json(const Json::Value& json, Slashing& obj) { obj.jail_time = dev::getUInt(json["jail_time"].asString()); } Json::Value enc_json(const DPOSConfig& obj) { Json::Value json(Json::objectValue); From d8fc74758ad806de8eb832ddde6414d7d003d261 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 16 Aug 2023 15:35:20 -0700 Subject: [PATCH 120/134] post merge fixes --- .../cli/include/cli/config_jsons/devnet/devnet_genesis.json | 6 ++---- libraries/config/src/hardfork.cpp | 2 +- tests/rewards_stats_test.cpp | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json index 35ea4dcc83..643ec62c07 100644 --- a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json @@ -229,9 +229,6 @@ "7e4aa664f71de4e9d0b4a6473d796372639bdcde": "0x1027e72f1f12813088000000", "ee1326fbf7d9322e5ea02c6fe5eb63535fceccd1": "0x52b7d2dcc80cd2e4000000" }, - "hardforks": { - "magnolia_hf_block_num" : "0x0" - }, "gas_price": { "blocks": 200, "percentile": 60, @@ -271,6 +268,7 @@ }, "hardforks": { "fix_redelegate_block_num": 0, - "rewards_distribution_frequency": {} + "rewards_distribution_frequency": {}, + "magnolia_hf_block_num" : "0x0" } } diff --git a/libraries/config/src/hardfork.cpp b/libraries/config/src/hardfork.cpp index caa4ba281f..cc476716b1 100644 --- a/libraries/config/src/hardfork.cpp +++ b/libraries/config/src/hardfork.cpp @@ -58,4 +58,4 @@ void dec_json(const Json::Value& json, Hardforks& obj) { } RLP_FIELDS_DEFINE(Hardforks, fix_redelegate_block_num, redelegations, rewards_distribution_frequency, - fee_rewards_block_num, magnolia_hf_block_num) + magnolia_hf_block_num) diff --git a/tests/rewards_stats_test.cpp b/tests/rewards_stats_test.cpp index 8e2dbea278..bc85364c76 100644 --- a/tests/rewards_stats_test.cpp +++ b/tests/rewards_stats_test.cpp @@ -19,7 +19,7 @@ struct RewardsStatsTest : NodesTest {}; class TestableRewardsStats : public rewards::Stats { public: TestableRewardsStats(const Hardforks::RewardsDistributionMap& rdm, std::shared_ptr db) - : rewards::Stats(100, Hardforks{rdm, 0}, db, [](auto) { return 100; }) {} + : rewards::Stats(100, Hardforks{0, {}, rdm, 0}, db, [](auto) { return 100; }) {} std::vector getStats() { return blocks_stats_; } }; From 5560dcccca86d1007a704a4070e17d55ac6b5402 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Thu, 17 Aug 2023 11:02:03 -0700 Subject: [PATCH 121/134] adjust configs parameters --- .../include/cli/config_jsons/default/default_genesis.json | 8 +++----- .../include/cli/config_jsons/devnet/devnet_genesis.json | 4 ++-- .../include/cli/config_jsons/mainnet/mainnet_genesis.json | 8 +++----- .../include/cli/config_jsons/testnet/testnet_genesis.json | 8 +++----- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/libraries/cli/include/cli/config_jsons/default/default_genesis.json b/libraries/cli/include/cli/config_jsons/default/default_genesis.json index 9234a41178..5316a2485e 100644 --- a/libraries/cli/include/cli/config_jsons/default/default_genesis.json +++ b/libraries/cli/include/cli/config_jsons/default/default_genesis.json @@ -56,16 +56,13 @@ } ], "slashing": { - "jail_time": "0x5" + "jail_time": 163459 } }, "initial_balances": { "2cd4da7d3b345e022ca7e997c2bb3276a4d3d2e9": "0x1027e72f1f12813088000000", "7e4aa664f71de4e9d0b4a6473d796372639bdcde": "0x1027e72f1f12813088000000" }, - "hardforks": { - "magnolia_hf_block_num" : "0x0" - }, "gas_price": { "blocks": 200, "percentile": 60, @@ -105,6 +102,7 @@ }, "hardforks": { "fix_redelegate_block_num": 0, - "rewards_distribution_frequency": {} + "rewards_distribution_frequency": {}, + "magnolia_hf_block_num" : 0 } } \ No newline at end of file diff --git a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json index 643ec62c07..be3725fc2f 100644 --- a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json @@ -221,7 +221,7 @@ } ], "slashing": { - "jail_time": "0x5" + "jail_time": 163459 } }, "initial_balances": { @@ -269,6 +269,6 @@ "hardforks": { "fix_redelegate_block_num": 0, "rewards_distribution_frequency": {}, - "magnolia_hf_block_num" : "0x0" + "magnolia_hf_block_num" : 0 } } diff --git a/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json b/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json index f8de61bb39..3cd6a86698 100644 --- a/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json @@ -243,7 +243,7 @@ } ], "slashing": { - "jail_time": "0x5" + "jail_time": 163459 } }, "initial_balances": { @@ -1591,9 +1591,6 @@ "c7cbe7d179857ea0ee5721f6b8b1b0dab193daf9": "0x381ce3ea85dc8f21bf0000", "6ba16abe80af8be7c9b6c852c6bd3e381a493c92": "0x4958bc77396e445c569000" }, - "hardforks": { - "magnolia_hf_block_num" : "0x0" - }, "gas_price": { "blocks": 200, "percentile": 60, @@ -1640,6 +1637,7 @@ "amount": "0x3e8" } ], - "rewards_distribution_frequency": {} + "rewards_distribution_frequency": {}, + "magnolia_hf_block_num" : 0 } } \ No newline at end of file diff --git a/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json b/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json index 0a650f1f8f..dec8b878c5 100644 --- a/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json @@ -89,7 +89,7 @@ } ], "slashing": { - "jail_time": "0x5" + "jail_time": 163459 } }, "initial_balances": { @@ -97,9 +97,6 @@ "f4a52b8f6dc8ab046fec6ad02e77023c044342e4": "0x19d971e4fe8401e74000000", "ee1326fbf7d9322e5ea02c6fe5eb63535fceccd1": "0x19d971e4fe8401e74000000" }, - "hardforks": { - "magnolia_hf_block_num" : "0x0" - }, "gas_price": { "blocks": 200, "percentile": 60, @@ -140,6 +137,7 @@ "hardforks": { "fix_redelegate_block_num": 0, "redelegations": [], - "rewards_distribution_frequency": {} + "rewards_distribution_frequency": {}, + "magnolia_hf_block_num" : 0 } } From 6aebf8ac5a735511eb9c9ffe1cedc288cc1304c5 Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Wed, 26 Jul 2023 12:56:06 +0200 Subject: [PATCH 122/134] feat: implement prototype of slashing manager --- .../slashing_manager/slashing_manager.hpp | 27 ++++++++++++ .../src/slashing_manager/slashing_manager.cpp | 43 +++++++++++++++++++ .../network/include/network/network.hpp | 3 +- .../v1/init_packets_handlers.hpp | 4 +- .../network/tarcap/taraxa_capability.hpp | 7 ++- libraries/core_libs/network/src/network.cpp | 7 +-- .../network/src/tarcap/taraxa_capability.cpp | 7 ++- libraries/core_libs/node/src/node.cpp | 5 ++- 8 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp create mode 100644 libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp diff --git a/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp b/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp new file mode 100644 index 0000000000..ed86a38a9d --- /dev/null +++ b/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "final_chain/final_chain.hpp" +#include "transaction/gas_pricer.hpp" +#include "transaction/transaction_manager.hpp" + +namespace taraxa { +class SlashingManager { + public: + SlashingManager(std::shared_ptr final_chain, std::shared_ptr trx_manager, + std::shared_ptr gas_pricer, uint64_t chain_id, secret_t node_sk); + SlashingManager(const SlashingManager &) = delete; + SlashingManager(SlashingManager &&) = delete; + SlashingManager &operator=(const SlashingManager &) = delete; + SlashingManager &operator=(SlashingManager &&) = delete; + + bool submit_double_voting(const std::shared_ptr &vote_a, const std::shared_ptr &vote_b) const; + + private: + std::shared_ptr final_chain_; + std::shared_ptr trx_manager_; + std::shared_ptr gas_pricer_; + const uint64_t kChainId; + const addr_t kAddress; + const secret_t kPrivateKey; +}; +} // namespace taraxa \ No newline at end of file diff --git a/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp b/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp new file mode 100644 index 0000000000..03f826b308 --- /dev/null +++ b/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp @@ -0,0 +1,43 @@ +#include "slashing_manager/slashing_manager.hpp" + +#include "common/types.hpp" +#include "final_chain/contract_interface.hpp" +#include "transaction/transaction_manager.hpp" + +namespace taraxa { + +const auto kContractAddress = addr_t("0x00000000000000000000000000000000000000FE"); + +SlashingManager::SlashingManager(std::shared_ptr final_chain, + std::shared_ptr trx_manager, std::shared_ptr gas_pricer, + uint64_t chain_id, secret_t node_sk) + : final_chain_(std::move(final_chain)), + trx_manager_(std::move(trx_manager)), + gas_pricer_(std::move(gas_pricer)), + kChainId(chain_id), + kAddress(toAddress(node_sk)), + kPrivateKey(std::move(node_sk)) {} + +bool SlashingManager::submit_double_voting(const std::shared_ptr &vote_a, + const std::shared_ptr &vote_b) const { + // First we need to check the balance + const auto account = final_chain_->get_account(kAddress).value_or(taraxa::state_api::ZeroAccount); + if (account.balance == 0) { + return false; + } + + // function commitDoubleVotingProof( + // bytes memory vote1, + // bytes memory vote2 + // ) external; + + auto input = final_chain::ContractInterface::packFunctionCall("commitDoubleVotingProof(bytes,bytes)", vote_a->rlp(), + vote_b->rlp()); + + // TODO we need to calculate gas for this transaction and hardcode it + // CommitDoubleVotingProofGas uint64 = 20000 + const auto trx = std::make_shared(account.nonce, 0, gas_pricer_->bid(), 1000000, std::move(input), + kPrivateKey, kContractAddress, kChainId); + return trx_manager_->insertTransaction(trx).first; +} +} // namespace taraxa \ No newline at end of file diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index db2ef24491..ae12b3b69b 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -35,7 +35,8 @@ class Network { Network(const FullNodeConfig &config, const h256 &genesis_hash, const std::filesystem::path &network_file_path, const dev::KeyPair &key, std::shared_ptr db, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - std::shared_ptr dag_mgr, std::shared_ptr trx_mgr); + std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, + std::shared_ptr slashing_manager); ~Network(); Network(const Network &) = delete; diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp index 178711a526..c0234ac665 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp @@ -10,6 +10,7 @@ #include "network/tarcap/packets_handlers/latest/vote_packet_handler.hpp" #include "network/tarcap/taraxa_capability.hpp" #include "pbft_sync_packet_handler.hpp" +#include "slashing_manager/slashing_manager.hpp" #include "votes_bundle_packet_handler.hpp" namespace taraxa::network::tarcap::v1 { @@ -23,7 +24,8 @@ static const TaraxaCapability::InitPacketsHandlers kInitV1Handlers = const std::shared_ptr &packets_stats, const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, - const std::shared_ptr &trx_mgr, const addr_t &node_addr) { + const std::shared_ptr &trx_mgr, + const std::shared_ptr & /*slashing_manager*/, const addr_t &node_addr) { auto packets_handlers = std::make_shared(); // Consensus packets with high processing priority diff --git a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp index 0cda972aa5..0e09c62ed4 100644 --- a/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp +++ b/libraries/core_libs/network/include/network/tarcap/taraxa_capability.hpp @@ -14,6 +14,7 @@ #include "network/tarcap/tarcap_version.hpp" #include "network/threadpool/tarcap_thread_pool.hpp" #include "pbft/pbft_chain.hpp" +#include "slashing_manager/slashing_manager.hpp" namespace taraxa { class DbStorage; @@ -22,6 +23,7 @@ class PbftChain; class VoteManager; class DagManager; class TransactionManager; +class SlashingManager; enum class TransactionStatus; } // namespace taraxa @@ -43,7 +45,8 @@ class TaraxaCapability final : public dev::p2p::CapabilityFace { const std::shared_ptr &packets_stats, const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, - const std::shared_ptr &trx_mgr, const addr_t &node_addr)>; + const std::shared_ptr &trx_mgr, const std::shared_ptr &slashing_manager, + const addr_t &node_addr)>; /** * @brief Default InitPacketsHandlers function definition with the latest version of packets handlers @@ -58,7 +61,7 @@ class TaraxaCapability final : public dev::p2p::CapabilityFace { std::shared_ptr syncing_state, std::shared_ptr db, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, - std::shared_ptr trx_mgr, + std::shared_ptr trx_mgr, std::shared_ptr slashing_manager, InitPacketsHandlers init_packets_handlers = kInitLatestVersionHandlers); virtual ~TaraxaCapability() = default; diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index f7ac34d6df..a12cede4d6 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -22,7 +22,8 @@ namespace taraxa { Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::filesystem::path const &network_file_path, dev::KeyPair const &key, std::shared_ptr db, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - std::shared_ptr dag_mgr, std::shared_ptr trx_mgr) + std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, + std::shared_ptr slashing_manager) : kConf(config), pub_key_(key.pub()), all_packets_stats_(nullptr), @@ -75,13 +76,13 @@ Network::Network(const FullNodeConfig &config, const h256 &genesis_hash, std::fi // Register old version (V1) of taraxa capability auto v1_tarcap = std::make_shared( kV1NetworkVersion, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, db, - pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, network::tarcap::v1::kInitV1Handlers); + pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, slashing_manager, network::tarcap::v1::kInitV1Handlers); capabilities.emplace_back(v1_tarcap); // Register latest version of taraxa capability auto latest_tarcap = std::make_shared( TARAXA_NET_VERSION, config, genesis_hash, host, key, packets_tp_, all_packets_stats_, pbft_syncing_state_, db, - pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr); + pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, slashing_manager); capabilities.emplace_back(latest_tarcap); return capabilities; diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index 91feba064c..7e22a841d5 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -17,6 +17,7 @@ #include "node/node.hpp" #include "pbft/pbft_chain.hpp" #include "pbft/pbft_manager.hpp" +#include "slashing_manager/slashing_manager.hpp" #include "transaction/transaction_manager.hpp" #include "vote/vote.hpp" @@ -30,6 +31,7 @@ TaraxaCapability::TaraxaCapability(TarcapVersion version, const FullNodeConfig & std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, + std::shared_ptr slashing_manager, InitPacketsHandlers init_packets_handlers) : version_(version), all_packets_stats_(std::move(packets_stats)), @@ -46,7 +48,7 @@ TaraxaCapability::TaraxaCapability(TarcapVersion version, const FullNodeConfig & peers_state_ = std::make_shared(host, kConf); packets_handlers_ = init_packets_handlers(logs_prefix, conf, genesis_hash, peers_state_, pbft_syncing_state_, all_packets_stats_, db, - pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, node_addr); + pbft_mgr, pbft_chain, vote_mgr, dag_mgr, trx_mgr, slashing_manager, node_addr); // Must be called after init_packets_handlers thread_pool_->setPacketsHandlers(version, packets_handlers_); @@ -210,7 +212,8 @@ const TaraxaCapability::InitPacketsHandlers TaraxaCapability::kInitLatestVersion const std::shared_ptr &packets_stats, const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, - const std::shared_ptr &trx_mgr, const addr_t &node_addr) { + const std::shared_ptr &trx_mgr, + const std::shared_ptr & /*slashing_manager*/, const addr_t &node_addr) { auto packets_handlers = std::make_shared(); // Consensus packets with high processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, pbft_chain, diff --git a/libraries/core_libs/node/src/node.cpp b/libraries/core_libs/node/src/node.cpp index 687a74b0a7..f47872524e 100644 --- a/libraries/core_libs/node/src/node.cpp +++ b/libraries/core_libs/node/src/node.cpp @@ -26,6 +26,7 @@ #include "network/rpc/jsonrpc_http_processor.hpp" #include "network/rpc/jsonrpc_ws_server.hpp" #include "pbft/pbft_manager.hpp" +#include "slashing_manager/slashing_manager.hpp" #include "storage/migration/migration_manager.hpp" #include "transaction/gas_pricer.hpp" #include "transaction/transaction_manager.hpp" @@ -134,8 +135,10 @@ void FullNode::init() { dag_block_proposer_ = std::make_shared( 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); + auto slashing_manager = + std::make_shared(final_chain_, trx_mgr_, gas_pricer_, conf_.genesis.chain_id, kp_.secret()); network_ = std::make_shared(conf_, genesis_hash, conf_.net_file_path().string(), kp_, db_, pbft_mgr_, - pbft_chain_, vote_mgr_, dag_mgr_, trx_mgr_); + pbft_chain_, vote_mgr_, dag_mgr_, trx_mgr_, std::move(slashing_manager)); } void FullNode::setupMetricsUpdaters() { From 0fdbbaec4747843eaa3bb7e72aa0b615b2ef641a Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Tue, 15 Aug 2023 16:30:51 -0700 Subject: [PATCH 123/134] add C++ double voting test + update latest magnolia hf evm --- .../slashing_manager/slashing_manager.hpp | 2 +- .../src/slashing_manager/slashing_manager.cpp | 4 +-- tests/state_api_test.cpp | 29 ++++++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp b/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp index ed86a38a9d..98980e038e 100644 --- a/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp +++ b/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp @@ -14,7 +14,7 @@ class SlashingManager { SlashingManager &operator=(const SlashingManager &) = delete; SlashingManager &operator=(SlashingManager &&) = delete; - bool submit_double_voting(const std::shared_ptr &vote_a, const std::shared_ptr &vote_b) const; + bool submitDoubleVotingProof(const std::shared_ptr &vote_a, const std::shared_ptr &vote_b) const; private: std::shared_ptr final_chain_; diff --git a/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp b/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp index 03f826b308..6b405e6650 100644 --- a/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp +++ b/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp @@ -6,7 +6,7 @@ namespace taraxa { -const auto kContractAddress = addr_t("0x00000000000000000000000000000000000000FE"); +const auto kContractAddress = addr_t("0x00000000000000000000000000000000000000EE"); SlashingManager::SlashingManager(std::shared_ptr final_chain, std::shared_ptr trx_manager, std::shared_ptr gas_pricer, @@ -18,7 +18,7 @@ SlashingManager::SlashingManager(std::shared_ptr final_chain, kAddress(toAddress(node_sk)), kPrivateKey(std::move(node_sk)) {} -bool SlashingManager::submit_double_voting(const std::shared_ptr &vote_a, +bool SlashingManager::submitDoubleVotingProof(const std::shared_ptr &vote_a, const std::shared_ptr &vote_b) const { // First we need to check the balance const auto account = final_chain_->get_account(kAddress).value_or(taraxa::state_api::ZeroAccount); diff --git a/tests/state_api_test.cpp b/tests/state_api_test.cpp index a6b0ac6d90..0aa5a73ad8 100644 --- a/tests/state_api_test.cpp +++ b/tests/state_api_test.cpp @@ -18,7 +18,7 @@ using boost::filesystem::temp_directory_path; using namespace std; // using namespace core_tests; -struct StateAPITest : WithDataDir {}; +struct StateAPITest : NodesTest {}; struct TestBlock { h256 hash; @@ -208,6 +208,33 @@ TEST_F(StateAPITest, DISABLED_eth_mainnet_smoke) { } } +TEST_F(StateAPITest, slashing) { + auto node_cfgs = make_node_cfgs(1, 1, 20); + for (auto &cfg : node_cfgs) { + cfg.genesis.state.dpos.slashing.jail_time = 100; + cfg.genesis.state.hardforks.magnolia_hf_block_num = 0; + } + + auto nodes = launch_nodes(node_cfgs); + auto node = nodes.begin()->get(); + auto node_cfg = node_cfgs[0]; + ASSERT_EQ(true, node->getFinalChain()->dpos_is_eligible(node->getFinalChain()->last_block_number(), node->getAddress())); + + // Generate 2 cert votes for 2 different blocks + auto vote_a = node->getVoteManager()->generateVote( blk_hash_t{1}, PbftVoteTypes::cert_vote, 1, 1, 3); + auto vote_b = node->getVoteManager()->generateVote( blk_hash_t{2}, PbftVoteTypes::cert_vote, 1, 1, 3); + + // Commit double voting proof + auto slashing_manager = + std::make_shared(node->getFinalChain(), node->getTransactionManager(), node->getGasPricer(), node_cfg.genesis.chain_id, node->getSecretKey()); + ASSERT_EQ(true, slashing_manager->submitDoubleVotingProof(vote_a, vote_b)); + + // After few blocks malicious validator should be jailed + ASSERT_HAPPENS({5s, 100ms}, [&](auto &ctx) { + WAIT_EXPECT_EQ(ctx, false, node->getFinalChain()->dpos_is_eligible(node->getFinalChain()->last_block_number(), node->getAddress())) + }); +} + } // namespace taraxa::state_api TARAXA_TEST_MAIN({}) From 60ebe15523ab831b6f8f96c7fdd46a4b9eb565f7 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 16 Aug 2023 12:26:05 -0700 Subject: [PATCH 124/134] commit double voting proof when received non-unique vote through packets --- .../slashing_manager/slashing_manager.hpp | 6 +++- .../include/vote_manager/vote_manager.hpp | 4 +-- .../src/slashing_manager/slashing_manager.cpp | 35 ++++++++++++++----- .../src/vote_manager/vote_manager.cpp | 25 +++++++------ .../common/ext_votes_packet_handler.hpp | 5 ++- .../get_next_votes_bundle_packet_handler.hpp | 3 +- .../latest/vote_packet_handler.hpp | 3 +- .../latest/votes_bundle_packet_handler.hpp | 3 +- .../v1/init_packets_handlers.hpp | 12 +++---- .../common/ext_votes_packet_handler.cpp | 9 +++-- .../get_next_votes_bundle_packet_handler.cpp | 6 ++-- .../latest/vote_packet_handler.cpp | 6 ++-- .../latest/votes_bundle_packet_handler.cpp | 8 +++-- .../network/src/tarcap/taraxa_capability.cpp | 14 ++++---- tests/state_api_test.cpp | 18 ++++++---- 15 files changed, 100 insertions(+), 57 deletions(-) diff --git a/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp b/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp index 98980e038e..b55e3d2709 100644 --- a/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp +++ b/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp @@ -14,12 +14,16 @@ class SlashingManager { SlashingManager &operator=(const SlashingManager &) = delete; SlashingManager &operator=(SlashingManager &&) = delete; - bool submitDoubleVotingProof(const std::shared_ptr &vote_a, const std::shared_ptr &vote_b) const; + bool submitDoubleVotingProof(const std::shared_ptr &vote_a, const std::shared_ptr &vote_b); private: std::shared_ptr final_chain_; std::shared_ptr trx_manager_; std::shared_ptr gas_pricer_; + + // Already processed double voting proofs + ExpirationCache double_voting_proofs_; + const uint64_t kChainId; const addr_t kAddress; const secret_t kPrivateKey; diff --git a/libraries/core_libs/consensus/include/vote_manager/vote_manager.hpp b/libraries/core_libs/consensus/include/vote_manager/vote_manager.hpp index 566452f511..6fa7011bda 100644 --- a/libraries/core_libs/consensus/include/vote_manager/vote_manager.hpp +++ b/libraries/core_libs/consensus/include/vote_manager/vote_manager.hpp @@ -57,9 +57,9 @@ class VoteManager { /** * @param vote - * @return if vote is unique per round & step & voter, otherwise + * @return if vote is unique per round & step & voter, otherwise */ - std::pair isUniqueVote(const std::shared_ptr& vote) const; + std::pair> isUniqueVote(const std::shared_ptr& vote) const; /** * @brief Get all verified votes diff --git a/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp b/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp index 6b405e6650..8f534baa73 100644 --- a/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp +++ b/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp @@ -14,23 +14,36 @@ SlashingManager::SlashingManager(std::shared_ptr final_chain, : final_chain_(std::move(final_chain)), trx_manager_(std::move(trx_manager)), gas_pricer_(std::move(gas_pricer)), + double_voting_proofs_(10000, 1000), kChainId(chain_id), kAddress(toAddress(node_sk)), kPrivateKey(std::move(node_sk)) {} bool SlashingManager::submitDoubleVotingProof(const std::shared_ptr &vote_a, - const std::shared_ptr &vote_b) const { - // First we need to check the balance + const std::shared_ptr &vote_b) { + // Create votes combination hash + dev::RLPStream hash_rlp(2); + if (vote_a->getHash() < vote_b->getHash()) { + hash_rlp << vote_a->getHash(); + hash_rlp << vote_b->getHash(); + } else { + hash_rlp << vote_b->getHash(); + hash_rlp << vote_a->getHash(); + } + const auto hash_bytes = hash_rlp.invalidate(); + const auto hash = dev::sha3(hash_bytes); + + // Check if this proof wasn't already processed + if (double_voting_proofs_.contains(hash)) { + return false; + } + + // Check the balance const auto account = final_chain_->get_account(kAddress).value_or(taraxa::state_api::ZeroAccount); if (account.balance == 0) { return false; } - // function commitDoubleVotingProof( - // bytes memory vote1, - // bytes memory vote2 - // ) external; - auto input = final_chain::ContractInterface::packFunctionCall("commitDoubleVotingProof(bytes,bytes)", vote_a->rlp(), vote_b->rlp()); @@ -38,6 +51,12 @@ bool SlashingManager::submitDoubleVotingProof(const std::shared_ptr &vote_ // CommitDoubleVotingProofGas uint64 = 20000 const auto trx = std::make_shared(account.nonce, 0, gas_pricer_->bid(), 1000000, std::move(input), kPrivateKey, kContractAddress, kChainId); - return trx_manager_->insertTransaction(trx).first; + + if (trx_manager_->insertTransaction(trx).first) { + double_voting_proofs_.insert(hash); + return true; + } + + return false; } } // namespace taraxa \ No newline at end of file diff --git a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp index c23bbe3af4..7a1dfc58b2 100644 --- a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp +++ b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp @@ -365,31 +365,32 @@ bool VoteManager::voteInVerifiedMap(const std::shared_ptr& vote) const { return found_voted_value_it->second.second.find(vote->getHash()) != found_voted_value_it->second.second.end(); } -std::pair VoteManager::isUniqueVote(const std::shared_ptr& vote) const { +std::pair> VoteManager::isUniqueVote(const std::shared_ptr& vote) const { std::shared_lock lock(verified_votes_access_); const auto found_period_it = verified_votes_.find(vote->getPeriod()); if (found_period_it == verified_votes_.end()) { - return {true, ""}; + return {true, nullptr}; } const auto found_round_it = found_period_it->second.find(vote->getRound()); if (found_round_it == found_period_it->second.end()) { - return {true, ""}; + return {true, nullptr}; } const auto found_step_it = found_round_it->second.step_votes.find(vote->getStep()); if (found_step_it == found_round_it->second.step_votes.end()) { - return {true, ""}; + return {true, nullptr}; } const auto found_voter_it = found_step_it->second.unique_voters.find(vote->getVoterAddr()); if (found_voter_it == found_step_it->second.unique_voters.end()) { - return {true, ""}; + return {true, nullptr}; } - if (found_voter_it->second.first->getHash() == vote->getHash()) { - return {true, ""}; + const auto found_vote = found_voter_it->second.first; + if (found_vote->getHash() == vote->getHash()) { + return {true, nullptr}; } // Next votes are special case, where we allow voting for both kNullBlockHash and some other specific block hash @@ -399,13 +400,13 @@ std::pair VoteManager::isUniqueVote(const std::shared_ptrsecond.second == nullptr) { // One of the next votes == kNullBlockHash -> valid scenario if (found_voter_it->second.first->getBlockHash() == kNullBlockHash && vote->getBlockHash() != kNullBlockHash) { - return {true, ""}; + return {true, nullptr}; } else if (found_voter_it->second.first->getBlockHash() != kNullBlockHash && vote->getBlockHash() == kNullBlockHash) { - return {true, ""}; + return {true, nullptr}; } } else if (found_voter_it->second.second->getHash() == vote->getHash()) { - return {true, ""}; + return {true, nullptr}; } } @@ -420,7 +421,9 @@ std::pair VoteManager::isUniqueVote(const std::shared_ptrsecond.second->getBlockHash().abridged() << ")"; } err << ", round: " << vote->getRound() << ", step: " << vote->getStep() << ", voter: " << vote->getVoterAddr(); - return {false, err.str()}; + LOG(log_er_) << err.str(); + + return {false, found_vote}; } bool VoteManager::insertUniqueVote(const std::shared_ptr& vote) { diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.hpp index 78eb84cf3b..fea5b0612c 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.hpp @@ -8,6 +8,7 @@ class PbftManager; class PbftChain; class PbftBlock; class VoteManager; +class SlashingManager; } // namespace taraxa namespace taraxa::network::tarcap { @@ -21,7 +22,8 @@ class ExtVotesPacketHandler : public PacketHandler { ExtVotesPacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - const addr_t& node_addr, const std::string& log_channel_name); + std::shared_ptr slashing_manager, const addr_t& node_addr, + const std::string& log_channel_name); virtual ~ExtVotesPacketHandler() = default; ExtVotesPacketHandler(const ExtVotesPacketHandler&) = delete; @@ -82,6 +84,7 @@ class ExtVotesPacketHandler : public PacketHandler { std::shared_ptr pbft_mgr_; std::shared_ptr pbft_chain_; std::shared_ptr vote_mgr_; + std::shared_ptr slashing_manager_; }; } // namespace taraxa::network::tarcap diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp index 6f8889c824..acabecec08 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.hpp @@ -14,7 +14,8 @@ class GetNextVotesBundlePacketHandler : public ExtVotesPacketHandler { GetNextVotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t& node_addr, + std::shared_ptr vote_mgr, + std::shared_ptr slashing_manager, const addr_t& node_addr, const std::string& logs_prefix = "GET_NEXT_VOTES_BUNDLE_PH"); // Packet type that is processed by this handler diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp index 740bed8fe0..afe2bf73e0 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/vote_packet_handler.hpp @@ -9,7 +9,8 @@ class VotePacketHandler : public ExtVotesPacketHandler { VotePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - const addr_t& node_addr, const std::string& logs_prefix = ""); + std::shared_ptr slashing_manager, const addr_t& node_addr, + const std::string& logs_prefix = ""); /** * @brief Sends pbft vote to connected peers diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp index 30d140045d..02af87ae8e 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/latest/votes_bundle_packet_handler.hpp @@ -9,7 +9,8 @@ class VotesBundlePacketHandler : public ExtVotesPacketHandler { VotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, - const addr_t& node_addr, const std::string& logs_prefix = ""); + std::shared_ptr slashing_manager, const addr_t& node_addr, + const std::string& logs_prefix = ""); /** * @brief Sends pbft votes bundle to connected peers diff --git a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp index c0234ac665..802277bf89 100644 --- a/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp +++ b/libraries/core_libs/network/include/network/tarcap/packets_handlers/v1/init_packets_handlers.hpp @@ -24,17 +24,17 @@ static const TaraxaCapability::InitPacketsHandlers kInitV1Handlers = const std::shared_ptr &packets_stats, const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, - const std::shared_ptr &trx_mgr, - const std::shared_ptr & /*slashing_manager*/, const addr_t &node_addr) { + const std::shared_ptr &trx_mgr, const std::shared_ptr &slashing_manager, + const addr_t &node_addr) { auto packets_handlers = std::make_shared(); // Consensus packets with high processing priority - packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr, logs_prefix); + packets_handlers->registerHandler( + config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, slashing_manager, node_addr, logs_prefix); packets_handlers->registerHandler( - config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr, logs_prefix); + config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, slashing_manager, node_addr, logs_prefix); packets_handlers->registerHandler( - config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, node_addr, logs_prefix); + config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, slashing_manager, node_addr, logs_prefix); // Standard packets with mid processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp index f6cf3e98e0..e3d2754c35 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp @@ -11,14 +11,16 @@ ExtVotesPacketHandler::ExtVotesPacketHandler(const FullNodeConfig &conf, std::sh std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t &node_addr, + std::shared_ptr vote_mgr, + std::shared_ptr slashing_manager, const addr_t &node_addr, const std::string &log_channel_name) : PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, log_channel_name), last_votes_sync_request_time_(std::chrono::system_clock::now()), last_pbft_block_sync_request_time_(std::chrono::system_clock::now()), pbft_mgr_(std::move(pbft_mgr)), pbft_chain_(std::move(pbft_chain)), - vote_mgr_(std::move(vote_mgr)) {} + vote_mgr_(std::move(vote_mgr)), + slashing_manager_(std::move(slashing_manager)) {} bool ExtVotesPacketHandler::processVote(const std::shared_ptr &vote, const std::shared_ptr &pbft_block, const std::shared_ptr &peer, bool validate_max_round_step) { @@ -40,7 +42,8 @@ bool ExtVotesPacketHandler::processVote(const std::shared_ptr &vote, const // Check is vote is unique per period, round & step & voter -> each address can generate just 1 vote // (for a value that isn't NBH) per period, round & step if (auto vote_valid = vote_mgr_->isUniqueVote(vote); !vote_valid.first) { - LOG(log_er_) << "Vote uniqueness " << vote->getHash() << " validation failed. Err: " << vote_valid.second; + // Create double voting proof + slashing_manager_->submitDoubleVotingProof(vote, vote_valid.second); throw MaliciousPeerException("Received double vote", vote->getVoter()); } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.cpp index 519a646e22..b7443b6e5d 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/get_next_votes_bundle_packet_handler.cpp @@ -8,10 +8,10 @@ namespace taraxa::network::tarcap { GetNextVotesBundlePacketHandler::GetNextVotesBundlePacketHandler( const FullNodeConfig &conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, - std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, const addr_t &node_addr, - const std::string &logs_prefix) + std::shared_ptr pbft_chain, std::shared_ptr vote_mgr, + std::shared_ptr slashing_manager, const addr_t &node_addr, const std::string &logs_prefix) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), - std::move(pbft_chain), std::move(vote_mgr), node_addr, + std::move(pbft_chain), std::move(vote_mgr), std::move(slashing_manager), node_addr, logs_prefix + "GET_NEXT_VOTES_BUNDLE_PH") {} void GetNextVotesBundlePacketHandler::validatePacketRlpFormat(const threadpool::PacketData &packet_data) const { diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp index 11d684de67..106dc7f6e7 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/vote_packet_handler.cpp @@ -8,10 +8,12 @@ namespace taraxa::network::tarcap { VotePacketHandler::VotePacketHandler(const FullNodeConfig &conf, std::shared_ptr peers_state, std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t &node_addr, + std::shared_ptr vote_mgr, + std::shared_ptr slashing_manager, const addr_t &node_addr, const std::string &logs_prefix) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), - std::move(pbft_chain), std::move(vote_mgr), node_addr, logs_prefix + "PBFT_VOTE_PH") {} + std::move(pbft_chain), std::move(vote_mgr), std::move(slashing_manager), node_addr, + logs_prefix + "PBFT_VOTE_PH") {} void VotePacketHandler::validatePacketRlpFormat([[maybe_unused]] const threadpool::PacketData &packet_data) const { auto items = packet_data.rlp_.itemCount(); diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp index 7074d4ebce..bcba4c6dca 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/votes_bundle_packet_handler.cpp @@ -10,10 +10,12 @@ VotesBundlePacketHandler::VotesBundlePacketHandler(const FullNodeConfig &conf, s std::shared_ptr packets_stats, std::shared_ptr pbft_mgr, std::shared_ptr pbft_chain, - std::shared_ptr vote_mgr, const addr_t &node_addr, - const std::string &logs_prefix) + std::shared_ptr vote_mgr, + std::shared_ptr slashing_manager, + const addr_t &node_addr, const std::string &logs_prefix) : ExtVotesPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pbft_mgr), - std::move(pbft_chain), std::move(vote_mgr), node_addr, logs_prefix + "VOTES_BUNDLE_PH") {} + std::move(pbft_chain), std::move(vote_mgr), std::move(slashing_manager), node_addr, + logs_prefix + "VOTES_BUNDLE_PH") {} void VotesBundlePacketHandler::validatePacketRlpFormat( [[maybe_unused]] const threadpool::PacketData &packet_data) const { diff --git a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp index 7e22a841d5..301678d7d2 100644 --- a/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp +++ b/libraries/core_libs/network/src/tarcap/taraxa_capability.cpp @@ -212,16 +212,16 @@ const TaraxaCapability::InitPacketsHandlers TaraxaCapability::kInitLatestVersion const std::shared_ptr &packets_stats, const std::shared_ptr &db, const std::shared_ptr &pbft_mgr, const std::shared_ptr &pbft_chain, const std::shared_ptr &vote_mgr, const std::shared_ptr &dag_mgr, - const std::shared_ptr &trx_mgr, - const std::shared_ptr & /*slashing_manager*/, const addr_t &node_addr) { + const std::shared_ptr &trx_mgr, const std::shared_ptr &slashing_manager, + const addr_t &node_addr) { auto packets_handlers = std::make_shared(); // Consensus packets with high processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, pbft_chain, - vote_mgr, node_addr, logs_prefix); - packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr, logs_prefix); - packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_mgr, - pbft_chain, vote_mgr, node_addr, logs_prefix); + vote_mgr, slashing_manager, node_addr, logs_prefix); + packets_handlers->registerHandler( + config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, slashing_manager, node_addr, logs_prefix); + packets_handlers->registerHandler( + config, peers_state, packets_stats, pbft_mgr, pbft_chain, vote_mgr, slashing_manager, node_addr, logs_prefix); // Standard packets with mid processing priority packets_handlers->registerHandler(config, peers_state, packets_stats, pbft_syncing_state, diff --git a/tests/state_api_test.cpp b/tests/state_api_test.cpp index 0aa5a73ad8..ffc04dc884 100644 --- a/tests/state_api_test.cpp +++ b/tests/state_api_test.cpp @@ -210,7 +210,7 @@ TEST_F(StateAPITest, DISABLED_eth_mainnet_smoke) { TEST_F(StateAPITest, slashing) { auto node_cfgs = make_node_cfgs(1, 1, 20); - for (auto &cfg : node_cfgs) { + for (auto& cfg : node_cfgs) { cfg.genesis.state.dpos.slashing.jail_time = 100; cfg.genesis.state.hardforks.magnolia_hf_block_num = 0; } @@ -218,20 +218,24 @@ TEST_F(StateAPITest, slashing) { auto nodes = launch_nodes(node_cfgs); auto node = nodes.begin()->get(); auto node_cfg = node_cfgs[0]; - ASSERT_EQ(true, node->getFinalChain()->dpos_is_eligible(node->getFinalChain()->last_block_number(), node->getAddress())); + ASSERT_EQ(true, + node->getFinalChain()->dpos_is_eligible(node->getFinalChain()->last_block_number(), node->getAddress())); // Generate 2 cert votes for 2 different blocks - auto vote_a = node->getVoteManager()->generateVote( blk_hash_t{1}, PbftVoteTypes::cert_vote, 1, 1, 3); - auto vote_b = node->getVoteManager()->generateVote( blk_hash_t{2}, PbftVoteTypes::cert_vote, 1, 1, 3); + auto vote_a = node->getVoteManager()->generateVote(blk_hash_t{1}, PbftVoteTypes::cert_vote, 1, 1, 3); + auto vote_b = node->getVoteManager()->generateVote(blk_hash_t{2}, PbftVoteTypes::cert_vote, 1, 1, 3); // Commit double voting proof auto slashing_manager = - std::make_shared(node->getFinalChain(), node->getTransactionManager(), node->getGasPricer(), node_cfg.genesis.chain_id, node->getSecretKey()); + std::make_shared(node->getFinalChain(), node->getTransactionManager(), node->getGasPricer(), + node_cfg.genesis.chain_id, node->getSecretKey()); ASSERT_EQ(true, slashing_manager->submitDoubleVotingProof(vote_a, vote_b)); // After few blocks malicious validator should be jailed - ASSERT_HAPPENS({5s, 100ms}, [&](auto &ctx) { - WAIT_EXPECT_EQ(ctx, false, node->getFinalChain()->dpos_is_eligible(node->getFinalChain()->last_block_number(), node->getAddress())) + ASSERT_HAPPENS({5s, 100ms}, [&](auto& ctx) { + WAIT_EXPECT_EQ( + ctx, false, + node->getFinalChain()->dpos_is_eligible(node->getFinalChain()->last_block_number(), node->getAddress())) }); } From 695d4f1579a96c47821010ebaeb969d43008c2f3 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 16 Aug 2023 12:50:23 -0700 Subject: [PATCH 125/134] fix isUniqueVote function to allow 2 voted blocks only for second finishing steps --- .../core_libs/consensus/src/vote_manager/vote_manager.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp index 7a1dfc58b2..7dd19cc241 100644 --- a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp +++ b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp @@ -393,9 +393,9 @@ std::pair> VoteManager::isUniqueVote(const std::shar return {true, nullptr}; } - // Next votes are special case, where we allow voting for both kNullBlockHash and some other specific block hash - // at the same time - if (vote->getType() == PbftVoteTypes::next_vote) { + // Next votes (second finishing steps) are special case, where we allow voting for both kNullBlockHash and + // some other specific block hash at the same time -> 2 unique votes per round & step & voter + if (vote->getType() == PbftVoteTypes::next_vote && vote->getStep() % 2) { // New second next vote if (found_voter_it->second.second == nullptr) { // One of the next votes == kNullBlockHash -> valid scenario @@ -450,6 +450,8 @@ bool VoteManager::insertUniqueVote(const std::shared_ptr& vote) { return true; } + const auto existing_vote = inserted_vote.first->second.first; + // There was already some vote inserted, check if it is the same vote as we are trying to insert if (inserted_vote.first->second.first->getHash() != vote->getHash()) { // Next votes (second finishing steps) are special case, where we allow voting for both kNullBlockHash and From 5a88e3056eb1197bd27777711bbc6f776793fcf9 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 16 Aug 2023 13:24:25 -0700 Subject: [PATCH 126/134] commit double voting proof when vote insertion fails due to non-unique vote --- .../include/vote_manager/vote_manager.hpp | 10 +- .../src/vote_manager/vote_manager.cpp | 130 +++++++++--------- libraries/core_libs/node/src/node.cpp | 7 +- 3 files changed, 78 insertions(+), 69 deletions(-) diff --git a/libraries/core_libs/consensus/include/vote_manager/vote_manager.hpp b/libraries/core_libs/consensus/include/vote_manager/vote_manager.hpp index 6fa7011bda..6562acfc2e 100644 --- a/libraries/core_libs/consensus/include/vote_manager/vote_manager.hpp +++ b/libraries/core_libs/consensus/include/vote_manager/vote_manager.hpp @@ -15,6 +15,7 @@ namespace taraxa { */ class Network; +class SlashingManager; namespace network::tarcap { class TaraxaPeer; @@ -27,7 +28,8 @@ class VoteManager { public: VoteManager(const addr_t& node_addr, const PbftConfig& pbft_config, const secret_t& node_sk, const vrf_wrapper::vrf_sk_t& vrf_sk, std::shared_ptr db, std::shared_ptr pbft_chain, - std::shared_ptr final_chain, std::shared_ptr key_manager); + std::shared_ptr final_chain, std::shared_ptr key_manager, + std::shared_ptr slashing_manager); ~VoteManager() = default; VoteManager(const VoteManager&) = delete; VoteManager(VoteManager&&) = delete; @@ -254,10 +256,9 @@ class VoteManager { /** * @brief Inserts unique vote * @param vote - * @return true if vote was successfully inserted(it was unique) or this specific vote was already inserted, otherwise - * false + * @return if vote is unique per round & step & voter, otherwise */ - bool insertUniqueVote(const std::shared_ptr& vote); + std::pair> insertUniqueVote(const std::shared_ptr& vote); /** * @brief Get PBFT sortition threshold for specific period @@ -279,6 +280,7 @@ class VoteManager { std::shared_ptr final_chain_; std::shared_ptr key_manager_; std::weak_ptr network_; + std::shared_ptr slashing_manager_; // Current pbft period based on pbft_manager std::atomic current_pbft_period_{0}; diff --git a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp index 7dd19cc241..3a614c7d12 100644 --- a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp +++ b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp @@ -16,7 +16,7 @@ namespace taraxa { VoteManager::VoteManager(const addr_t& node_addr, const PbftConfig& pbft_config, const secret_t& node_sk, const vrf_wrapper::vrf_sk_t& vrf_sk, std::shared_ptr db, std::shared_ptr pbft_chain, std::shared_ptr final_chain, - std::shared_ptr key_manager) + std::shared_ptr key_manager, std::shared_ptr slashing_manager) : kNodeAddr(node_addr), kPbftConfig(pbft_config), kVrfSk(vrf_sk), @@ -26,6 +26,7 @@ VoteManager::VoteManager(const addr_t& node_addr, const PbftConfig& pbft_config, pbft_chain_(std::move(pbft_chain)), final_chain_(std::move(final_chain)), key_manager_(std::move(key_manager)), + slashing_manager_(std::move(slashing_manager)), already_validated_votes_(1000000, 1000) { LOG_OBJECTS_CREATE("VOTE_MGR"); @@ -195,8 +196,10 @@ bool VoteManager::addVerifiedVote(const std::shared_ptr& vote) { { std::scoped_lock lock(verified_votes_access_); - if (!insertUniqueVote(vote)) { + if (auto vote_inserted = insertUniqueVote(vote); !vote_inserted.first) { LOG(log_wr_) << "Non unique vote " << vote->getHash().abridged() << " (race condition)"; + // Create double voting proof + slashing_manager_->submitDoubleVotingProof(vote, vote_inserted.second); return false; } @@ -388,8 +391,7 @@ std::pair> VoteManager::isUniqueVote(const std::shar return {true, nullptr}; } - const auto found_vote = found_voter_it->second.first; - if (found_vote->getHash() == vote->getHash()) { + if (found_voter_it->second.first->getHash() == vote->getHash()) { return {true, nullptr}; } @@ -423,77 +425,81 @@ std::pair> VoteManager::isUniqueVote(const std::shar err << ", round: " << vote->getRound() << ", step: " << vote->getStep() << ", voter: " << vote->getVoterAddr(); LOG(log_er_) << err.str(); - return {false, found_vote}; + // Return existing vote + if (found_voter_it->second.second && vote->getHash() != found_voter_it->second.second->getHash()) { + return {false, found_voter_it->second.second}; + } + return {false, found_voter_it->second.first}; } -bool VoteManager::insertUniqueVote(const std::shared_ptr& vote) { - auto insertVote = [this](const std::shared_ptr& vote) -> bool { - auto found_period_it = verified_votes_.find(vote->getPeriod()); - if (found_period_it == verified_votes_.end()) { - found_period_it = verified_votes_.insert({vote->getPeriod(), {}}).first; - } +std::pair> VoteManager::insertUniqueVote(const std::shared_ptr& vote) { + auto found_period_it = verified_votes_.find(vote->getPeriod()); + if (found_period_it == verified_votes_.end()) { + found_period_it = verified_votes_.insert({vote->getPeriod(), {}}).first; + } - auto found_round_it = found_period_it->second.find(vote->getRound()); - if (found_round_it == found_period_it->second.end()) { - found_round_it = found_period_it->second.insert({vote->getRound(), {}}).first; - } + auto found_round_it = found_period_it->second.find(vote->getRound()); + if (found_round_it == found_period_it->second.end()) { + found_round_it = found_period_it->second.insert({vote->getRound(), {}}).first; + } - auto found_step_it = found_round_it->second.step_votes.find(vote->getStep()); - if (found_step_it == found_round_it->second.step_votes.end()) { - found_step_it = found_round_it->second.step_votes.insert({vote->getStep(), {}}).first; - } + auto found_step_it = found_round_it->second.step_votes.find(vote->getStep()); + if (found_step_it == found_round_it->second.step_votes.end()) { + found_step_it = found_round_it->second.step_votes.insert({vote->getStep(), {}}).first; + } - auto inserted_vote = found_step_it->second.unique_voters.insert({vote->getVoterAddr(), {vote, nullptr}}); + auto inserted_vote = found_step_it->second.unique_voters.insert({vote->getVoterAddr(), {vote, nullptr}}); - // Vote was successfully inserted -> it is unique - if (inserted_vote.second) { - return true; - } + // Vote was successfully inserted -> it is unique + if (inserted_vote.second) { + return {true, nullptr}; + } - const auto existing_vote = inserted_vote.first->second.first; - - // There was already some vote inserted, check if it is the same vote as we are trying to insert - if (inserted_vote.first->second.first->getHash() != vote->getHash()) { - // Next votes (second finishing steps) are special case, where we allow voting for both kNullBlockHash and - // some other specific block hash at the same time -> 2 unique votes per round & step & voter - if (vote->getType() == PbftVoteTypes::next_vote && vote->getStep() % 2) { - // New second next vote - if (inserted_vote.first->second.second == nullptr) { - // One of the next votes == kNullBlockHash -> valid scenario - if (inserted_vote.first->second.first->getBlockHash() == kNullBlockHash && - vote->getBlockHash() != kNullBlockHash) { - inserted_vote.first->second.second = vote; - return true; - } else if (inserted_vote.first->second.first->getBlockHash() != kNullBlockHash && - vote->getBlockHash() == kNullBlockHash) { - inserted_vote.first->second.second = vote; - return true; - } - } else if (inserted_vote.first->second.second->getHash() == vote->getHash()) { - return true; + const auto existing_vote = inserted_vote.first->second.first; + + // There was already some vote inserted, check if it is the same vote as we are trying to insert + if (inserted_vote.first->second.first->getHash() != vote->getHash()) { + // Next votes (second finishing steps) are special case, where we allow voting for both kNullBlockHash and + // some other specific block hash at the same time -> 2 unique votes per round & step & voter + if (vote->getType() == PbftVoteTypes::next_vote && vote->getStep() % 2) { + // New second next vote + if (inserted_vote.first->second.second == nullptr) { + // One of the next votes == kNullBlockHash -> valid scenario + if (inserted_vote.first->second.first->getBlockHash() == kNullBlockHash && + vote->getBlockHash() != kNullBlockHash) { + inserted_vote.first->second.second = vote; + return {true, nullptr}; + } else if (inserted_vote.first->second.first->getBlockHash() != kNullBlockHash && + vote->getBlockHash() == kNullBlockHash) { + inserted_vote.first->second.second = vote; + return {true, nullptr}; } + } else if (inserted_vote.first->second.second->getHash() == vote->getHash()) { + return {true, nullptr}; } + } - std::stringstream err; - err << "Unable to insert new unique vote(race condition): " - << ", new vote hash (voted value): " << vote->getHash().abridged() << " (" << vote->getBlockHash() << ")" - << ", orig. vote hash (voted value): " << inserted_vote.first->second.first->getHash().abridged() << " (" - << inserted_vote.first->second.first->getBlockHash() << ")"; - if (inserted_vote.first->second.second != nullptr) { - err << ", orig. vote 2 hash (voted value): " << inserted_vote.first->second.second->getHash().abridged() << " (" - << inserted_vote.first->second.second->getBlockHash() << ")"; - } - err << ", period: " << vote->getPeriod() << ", round: " << vote->getRound() << ", step: " << vote->getStep() - << ", voter: " << vote->getVoterAddr(); - LOG(log_er_) << err.str(); - - return false; + std::stringstream err; + err << "Unable to insert new unique vote(race condition): " + << ", new vote hash (voted value): " << vote->getHash().abridged() << " (" << vote->getBlockHash() << ")" + << ", orig. vote hash (voted value): " << inserted_vote.first->second.first->getHash().abridged() << " (" + << inserted_vote.first->second.first->getBlockHash() << ")"; + if (inserted_vote.first->second.second != nullptr) { + err << ", orig. vote 2 hash (voted value): " << inserted_vote.first->second.second->getHash().abridged() << " (" + << inserted_vote.first->second.second->getBlockHash() << ")"; } + err << ", period: " << vote->getPeriod() << ", round: " << vote->getRound() << ", step: " << vote->getStep() + << ", voter: " << vote->getVoterAddr(); + LOG(log_er_) << err.str(); - return true; - }; + // Return existing vote + if (inserted_vote.first->second.second && vote->getHash() != inserted_vote.first->second.second->getHash()) { + return {false, inserted_vote.first->second.second}; + } + return {false, inserted_vote.first->second.first}; + } - return insertVote(vote); + return {true, nullptr}; } void VoteManager::cleanupVotesByPeriod(PbftPeriod pbft_period) { diff --git a/libraries/core_libs/node/src/node.cpp b/libraries/core_libs/node/src/node.cpp index f47872524e..e9975523fc 100644 --- a/libraries/core_libs/node/src/node.cpp +++ b/libraries/core_libs/node/src/node.cpp @@ -127,16 +127,17 @@ void FullNode::init() { conf_.genesis.dag, trx_mgr_, pbft_chain_, final_chain_, db_, key_manager_, conf_.genesis.pbft.gas_limit, conf_.is_light_node, conf_.light_node_history, conf_.max_levels_per_period, conf_.dag_expiry_limit); + auto slashing_manager = + std::make_shared(final_chain_, trx_mgr_, gas_pricer_, conf_.genesis.chain_id, kp_.secret()); vote_mgr_ = std::make_shared(node_addr, conf_.genesis.pbft, kp_.secret(), conf_.vrf_secret, db_, - pbft_chain_, final_chain_, key_manager_); + pbft_chain_, final_chain_, key_manager_, slashing_manager); pbft_mgr_ = std::make_shared(conf_.genesis.pbft, conf_.genesis.dag_genesis_block.getHash(), node_addr, db_, pbft_chain_, vote_mgr_, dag_mgr_, trx_mgr_, final_chain_, kp_.secret()); dag_block_proposer_ = std::make_shared( 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); - auto slashing_manager = - std::make_shared(final_chain_, trx_mgr_, gas_pricer_, conf_.genesis.chain_id, kp_.secret()); + network_ = std::make_shared(conf_, genesis_hash, conf_.net_file_path().string(), kp_, db_, pbft_mgr_, pbft_chain_, vote_mgr_, dag_mgr_, trx_mgr_, std::move(slashing_manager)); } From 62a597d9ad290da877b7708881ee0a685e156d62 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 16 Aug 2023 14:11:53 -0700 Subject: [PATCH 127/134] commit double voting proof only after magnolia hf block passed --- libraries/config/include/config/config.hpp | 2 +- libraries/config/include/config/genesis.hpp | 8 ++++---- libraries/config/include/config/hardfork.hpp | 6 +++--- libraries/config/include/config/state_config.hpp | 2 +- libraries/config/src/config.cpp | 2 +- libraries/config/src/genesis.cpp | 14 +++++++------- libraries/config/src/hardfork.cpp | 7 +++---- .../consensus/include/rewards/rewards_stats.hpp | 4 ++-- .../include/slashing_manager/slashing_manager.hpp | 7 ++++--- .../consensus/src/rewards/rewards_stats.cpp | 2 +- .../src/slashing_manager/slashing_manager.cpp | 12 ++++++++---- .../consensus/src/vote_manager/vote_manager.cpp | 2 +- .../latest/common/ext_votes_packet_handler.cpp | 2 +- libraries/core_libs/node/src/node.cpp | 2 +- tests/rewards_stats_test.cpp | 10 +++++----- tests/state_api_test.cpp | 4 ++-- 16 files changed, 45 insertions(+), 41 deletions(-) diff --git a/libraries/config/include/config/config.hpp b/libraries/config/include/config/config.hpp index 85d44bcb59..04a60b05bd 100644 --- a/libraries/config/include/config/config.hpp +++ b/libraries/config/include/config/config.hpp @@ -49,7 +49,7 @@ struct FullNodeConfig { fs::path log_path; NetworkConfig network; DBConfig db_config; - Genesis genesis; + GenesisConfig genesis; state_api::Opts opts_final_chain; std::vector log_configs; bool is_light_node = false; // Is light node diff --git a/libraries/config/include/config/genesis.hpp b/libraries/config/include/config/genesis.hpp index 608f4df49f..4963214807 100644 --- a/libraries/config/include/config/genesis.hpp +++ b/libraries/config/include/config/genesis.hpp @@ -27,7 +27,7 @@ struct GasPriceConfig { Json::Value enc_json(GasPriceConfig const& obj); void dec_json(Json::Value const& json, GasPriceConfig& obj); -struct Genesis { +struct GenesisConfig { uint64_t chain_id = 0; DagBlock dag_genesis_block; GasPriceConfig gas_price; @@ -36,14 +36,14 @@ struct Genesis { state_api::Config state; DagConfig dag; - Genesis(); + GenesisConfig(); void validate() const; bytes rlp() const; blk_hash_t genesisHash() const; void updateBlocksPerYear(); }; -Json::Value enc_json(Genesis const& obj); -void dec_json(Json::Value const& json, Genesis& obj); +Json::Value enc_json(GenesisConfig const& obj); +void dec_json(Json::Value const& json, GenesisConfig& obj); } // namespace taraxa diff --git a/libraries/config/include/config/hardfork.hpp b/libraries/config/include/config/hardfork.hpp index 01652b1706..f24cfab543 100644 --- a/libraries/config/include/config/hardfork.hpp +++ b/libraries/config/include/config/hardfork.hpp @@ -15,7 +15,7 @@ struct Redelegation { Json::Value enc_json(const Redelegation& obj); void dec_json(const Json::Value& json, Redelegation& obj); -struct Hardforks { +struct HardforksConfig { // disable it by default (set to max uint64) uint64_t fix_redelegate_block_num = -1; std::vector redelegations; @@ -45,5 +45,5 @@ struct Hardforks { HAS_RLP_FIELDS }; -Json::Value enc_json(const Hardforks& obj); -void dec_json(const Json::Value& json, Hardforks& obj); +Json::Value enc_json(const HardforksConfig& obj); +void dec_json(const Json::Value& json, HardforksConfig& obj); diff --git a/libraries/config/include/config/state_config.hpp b/libraries/config/include/config/state_config.hpp index bb1bd34207..0762a51588 100644 --- a/libraries/config/include/config/state_config.hpp +++ b/libraries/config/include/config/state_config.hpp @@ -72,7 +72,7 @@ struct Config { EVMChainConfig evm_chain_config; BalanceMap initial_balances; DPOSConfig dpos; - Hardforks hardforks; + HardforksConfig hardforks; void rlp_without_hardforks(util::RLPEncoderRef encoding) const; HAS_RLP_FIELDS diff --git a/libraries/config/src/config.cpp b/libraries/config/src/config.cpp index 35acac408d..8dae3b49d5 100644 --- a/libraries/config/src/config.cpp +++ b/libraries/config/src/config.cpp @@ -103,7 +103,7 @@ FullNodeConfig::FullNodeConfig(const Json::Value &string_or_object, const Json:: if (const auto &v = genesis_json; v.isObject()) { dec_json(v, genesis); } else { - genesis = Genesis(); + genesis = GenesisConfig(); } is_light_node = getConfigDataAsBoolean(root, {"is_light_node"}, true, is_light_node); diff --git a/libraries/config/src/genesis.cpp b/libraries/config/src/genesis.cpp index 4308a64111..57a1ec70c6 100644 --- a/libraries/config/src/genesis.cpp +++ b/libraries/config/src/genesis.cpp @@ -41,7 +41,7 @@ bytes GasPriceConfig::rlp() const { return s.out(); } -Json::Value enc_json(const Genesis& obj) { +Json::Value enc_json(const GenesisConfig& obj) { Json::Value json(Json::objectValue); json["chain_id"] = obj.chain_id; json["dag_genesis_block"] = obj.dag_genesis_block.getJson(false); @@ -53,7 +53,7 @@ Json::Value enc_json(const Genesis& obj) { return json; } -void dec_json(Json::Value const& json, Genesis& obj) { +void dec_json(Json::Value const& json, GenesisConfig& obj) { obj.chain_id = json["chain_id"].asUInt(); obj.dag_genesis_block = DagBlock(json["dag_genesis_block"]); dec_json(json["sortition"], obj.sortition); @@ -64,7 +64,7 @@ void dec_json(Json::Value const& json, Genesis& obj) { obj.updateBlocksPerYear(); } -Genesis::Genesis() { +GenesisConfig::GenesisConfig() { dag_genesis_block = DagBlock(string(R"({ "level": 0, "tips": [], @@ -103,7 +103,7 @@ Genesis::Genesis() { updateBlocksPerYear(); } -void Genesis::updateBlocksPerYear() { +void GenesisConfig::updateBlocksPerYear() { uint64_t year_ms = 365 * 24 * 60 * 60; year_ms *= 1000; // we have fixed 2*lambda time for proposing step and adding some expecting value for filter and certify steps @@ -111,9 +111,9 @@ void Genesis::updateBlocksPerYear() { state.dpos.blocks_per_year = year_ms / expected_block_time; } -void Genesis::validate() const { gas_price.validate(); } +void GenesisConfig::validate() const { gas_price.validate(); } -bytes Genesis::rlp() const { +bytes GenesisConfig::rlp() const { dev::RLPStream s; s.appendList(6); @@ -127,6 +127,6 @@ bytes Genesis::rlp() const { return s.out(); } -blk_hash_t Genesis::genesisHash() const { return dev::sha3(rlp()); } +blk_hash_t GenesisConfig::genesisHash() const { return dev::sha3(rlp()); } } // namespace taraxa diff --git a/libraries/config/src/hardfork.cpp b/libraries/config/src/hardfork.cpp index cc476716b1..55043635d3 100644 --- a/libraries/config/src/hardfork.cpp +++ b/libraries/config/src/hardfork.cpp @@ -16,7 +16,7 @@ void dec_json(const Json::Value& json, Redelegation& obj) { RLP_FIELDS_DEFINE(Redelegation, validator, delegator, amount) -Json::Value enc_json(const Hardforks& obj) { +Json::Value enc_json(const HardforksConfig& obj) { Json::Value json(Json::objectValue); json["fix_redelegate_block_num"] = dev::toJS(obj.fix_redelegate_block_num); json["initial_validators"] = Json::Value(Json::arrayValue); @@ -35,7 +35,7 @@ Json::Value enc_json(const Hardforks& obj) { return json; } -void dec_json(const Json::Value& json, Hardforks& obj) { +void dec_json(const Json::Value& json, HardforksConfig& obj) { obj.fix_redelegate_block_num = dev::getUInt(json["fix_redelegate_block_num"]); const auto& redelegations_json = json["redelegations"]; @@ -57,5 +57,4 @@ void dec_json(const Json::Value& json, Hardforks& obj) { } } -RLP_FIELDS_DEFINE(Hardforks, fix_redelegate_block_num, redelegations, rewards_distribution_frequency, - magnolia_hf_block_num) +RLP_FIELDS_DEFINE(HardforksConfig, rewards_distribution_frequency, magnolia_hf_block_num) diff --git a/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp b/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp index b8f0dc2c07..c84a94a4a6 100644 --- a/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp +++ b/libraries/core_libs/consensus/include/rewards/rewards_stats.hpp @@ -10,7 +10,7 @@ namespace taraxa::rewards { */ class Stats { public: - Stats(uint32_t committee_size, const Hardforks& hardforks, std::shared_ptr db, + Stats(uint32_t committee_size, const HardforksConfig& hardforks, std::shared_ptr db, std::function&& dpos_eligible_total_vote_count); /** @@ -46,7 +46,7 @@ class Stats { void clear(); const uint32_t kCommitteeSize; - const Hardforks kHardforks; + const HardforksConfig kHardforks; std::shared_ptr db_; const std::function dpos_eligible_total_vote_count_; std::vector blocks_stats_; diff --git a/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp b/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp index b55e3d2709..ba0c800a81 100644 --- a/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp +++ b/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp @@ -8,13 +8,14 @@ namespace taraxa { class SlashingManager { public: SlashingManager(std::shared_ptr final_chain, std::shared_ptr trx_manager, - std::shared_ptr gas_pricer, uint64_t chain_id, secret_t node_sk); + std::shared_ptr gas_pricer, const GenesisConfig &genesis_config, secret_t node_sk); SlashingManager(const SlashingManager &) = delete; SlashingManager(SlashingManager &&) = delete; SlashingManager &operator=(const SlashingManager &) = delete; SlashingManager &operator=(SlashingManager &&) = delete; - bool submitDoubleVotingProof(const std::shared_ptr &vote_a, const std::shared_ptr &vote_b); + bool submitDoubleVotingProof(PbftPeriod current_period, const std::shared_ptr &vote_a, + const std::shared_ptr &vote_b); private: std::shared_ptr final_chain_; @@ -24,7 +25,7 @@ class SlashingManager { // Already processed double voting proofs ExpirationCache double_voting_proofs_; - const uint64_t kChainId; + const GenesisConfig kGenesisConfig; const addr_t kAddress; const secret_t kPrivateKey; }; diff --git a/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp b/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp index e4184cb467..92ea28fe55 100644 --- a/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp +++ b/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp @@ -3,7 +3,7 @@ #include "storage/storage.hpp" namespace taraxa::rewards { -Stats::Stats(uint32_t committee_size, const Hardforks& hardforks, std::shared_ptr db, +Stats::Stats(uint32_t committee_size, const HardforksConfig& hardforks, std::shared_ptr db, std::function&& dpos_eligible_total_vote_count) : kCommitteeSize(committee_size), kHardforks(hardforks), diff --git a/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp b/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp index 8f534baa73..b24849147e 100644 --- a/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp +++ b/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp @@ -10,17 +10,21 @@ const auto kContractAddress = addr_t("0x00000000000000000000000000000000000000EE SlashingManager::SlashingManager(std::shared_ptr final_chain, std::shared_ptr trx_manager, std::shared_ptr gas_pricer, - uint64_t chain_id, secret_t node_sk) + const GenesisConfig &genesis_config, secret_t node_sk) : final_chain_(std::move(final_chain)), trx_manager_(std::move(trx_manager)), gas_pricer_(std::move(gas_pricer)), double_voting_proofs_(10000, 1000), - kChainId(chain_id), + kGenesisConfig(genesis_config), kAddress(toAddress(node_sk)), kPrivateKey(std::move(node_sk)) {} -bool SlashingManager::submitDoubleVotingProof(const std::shared_ptr &vote_a, +bool SlashingManager::submitDoubleVotingProof(PbftPeriod current_period, const std::shared_ptr &vote_a, const std::shared_ptr &vote_b) { + if (current_period < kGenesisConfig.state.hardforks.magnolia_hf_block_num) { + return false; + } + // Create votes combination hash dev::RLPStream hash_rlp(2); if (vote_a->getHash() < vote_b->getHash()) { @@ -50,7 +54,7 @@ bool SlashingManager::submitDoubleVotingProof(const std::shared_ptr &vote_ // TODO we need to calculate gas for this transaction and hardcode it // CommitDoubleVotingProofGas uint64 = 20000 const auto trx = std::make_shared(account.nonce, 0, gas_pricer_->bid(), 1000000, std::move(input), - kPrivateKey, kContractAddress, kChainId); + kPrivateKey, kContractAddress, kGenesisConfig.chain_id); if (trx_manager_->insertTransaction(trx).first) { double_voting_proofs_.insert(hash); diff --git a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp index 3a614c7d12..4a65503883 100644 --- a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp +++ b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp @@ -199,7 +199,7 @@ bool VoteManager::addVerifiedVote(const std::shared_ptr& vote) { if (auto vote_inserted = insertUniqueVote(vote); !vote_inserted.first) { LOG(log_wr_) << "Non unique vote " << vote->getHash().abridged() << " (race condition)"; // Create double voting proof - slashing_manager_->submitDoubleVotingProof(vote, vote_inserted.second); + slashing_manager_->submitDoubleVotingProof(pbft_chain_->getPbftChainSize(), vote, vote_inserted.second); return false; } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp index e3d2754c35..e9111198d0 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp @@ -43,7 +43,7 @@ bool ExtVotesPacketHandler::processVote(const std::shared_ptr &vote, const // (for a value that isn't NBH) per period, round & step if (auto vote_valid = vote_mgr_->isUniqueVote(vote); !vote_valid.first) { // Create double voting proof - slashing_manager_->submitDoubleVotingProof(vote, vote_valid.second); + slashing_manager_->submitDoubleVotingProof(pbft_chain_->getPbftChainSize(), vote, vote_valid.second); throw MaliciousPeerException("Received double vote", vote->getVoter()); } diff --git a/libraries/core_libs/node/src/node.cpp b/libraries/core_libs/node/src/node.cpp index e9975523fc..e1f8c7f58e 100644 --- a/libraries/core_libs/node/src/node.cpp +++ b/libraries/core_libs/node/src/node.cpp @@ -128,7 +128,7 @@ void FullNode::init() { conf_.genesis.pbft.gas_limit, conf_.is_light_node, conf_.light_node_history, conf_.max_levels_per_period, conf_.dag_expiry_limit); auto slashing_manager = - std::make_shared(final_chain_, trx_mgr_, gas_pricer_, conf_.genesis.chain_id, kp_.secret()); + std::make_shared(final_chain_, trx_mgr_, gas_pricer_, conf_.genesis, kp_.secret()); vote_mgr_ = std::make_shared(node_addr, conf_.genesis.pbft, kp_.secret(), conf_.vrf_secret, db_, pbft_chain_, final_chain_, key_manager_, slashing_manager); pbft_mgr_ = diff --git a/tests/rewards_stats_test.cpp b/tests/rewards_stats_test.cpp index bc85364c76..389423922b 100644 --- a/tests/rewards_stats_test.cpp +++ b/tests/rewards_stats_test.cpp @@ -47,7 +47,7 @@ TEST_F(RewardsStatsTest, statsSaving) { auto db = std::make_shared(data_dir / "db"); // distribute every 5 blocks - Hardforks::RewardsDistributionMap distribution{{0, 5}}; + HardforksConfig::RewardsDistributionMap distribution{{0, 5}}; std::vector> empty_votes; std::vector block_authors; @@ -81,7 +81,7 @@ TEST_F(RewardsStatsTest, statsCleaning) { auto db = std::make_shared(data_dir / "db"); // distribute every 5 blocks - Hardforks::RewardsDistributionMap distribution{{0, 5}}; + HardforksConfig::RewardsDistributionMap distribution{{0, 5}}; std::vector> empty_votes; std::vector block_authors; @@ -144,7 +144,7 @@ TEST_F(RewardsStatsTest, statsProcessing) { TEST_F(RewardsStatsTest, distributionChange) { auto db = std::make_shared(data_dir / "db"); - Hardforks::RewardsDistributionMap distribution{{6, 5}, {11, 2}}; + HardforksConfig::RewardsDistributionMap distribution{{6, 5}, {11, 2}}; auto rewards_stats = TestableRewardsStats(distribution, db); @@ -175,7 +175,7 @@ TEST_F(RewardsStatsTest, feeRewards) { auto pbft_proposer = dev::KeyPair::create(); auto dag_proposer = dev::KeyPair::create(); - Hardforks::RewardsDistributionMap distribution{}; + HardforksConfig::RewardsDistributionMap distribution{}; auto rewards_stats = TestableRewardsStats(distribution, db); @@ -225,4 +225,4 @@ int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); -} \ No newline at end of file +} diff --git a/tests/state_api_test.cpp b/tests/state_api_test.cpp index ffc04dc884..60209c4e89 100644 --- a/tests/state_api_test.cpp +++ b/tests/state_api_test.cpp @@ -228,8 +228,8 @@ TEST_F(StateAPITest, slashing) { // Commit double voting proof auto slashing_manager = std::make_shared(node->getFinalChain(), node->getTransactionManager(), node->getGasPricer(), - node_cfg.genesis.chain_id, node->getSecretKey()); - ASSERT_EQ(true, slashing_manager->submitDoubleVotingProof(vote_a, vote_b)); + node_cfg.genesis, node->getSecretKey()); + ASSERT_EQ(true, slashing_manager->submitDoubleVotingProof(node->getPbftChain()->getPbftChainSize(), vote_a, vote_b)); // After few blocks malicious validator should be jailed ASSERT_HAPPENS({5s, 100ms}, [&](auto& ctx) { From f052c5a554e8917dc40659b328d8f4e17658ae0b Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Thu, 17 Aug 2023 10:30:38 -0700 Subject: [PATCH 128/134] after merge fixes --- libraries/config/src/hardfork.cpp | 3 ++- tests/rewards_stats_test.cpp | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/libraries/config/src/hardfork.cpp b/libraries/config/src/hardfork.cpp index 55043635d3..26d9a19ed0 100644 --- a/libraries/config/src/hardfork.cpp +++ b/libraries/config/src/hardfork.cpp @@ -57,4 +57,5 @@ void dec_json(const Json::Value& json, HardforksConfig& obj) { } } -RLP_FIELDS_DEFINE(HardforksConfig, rewards_distribution_frequency, magnolia_hf_block_num) +RLP_FIELDS_DEFINE(HardforksConfig, fix_redelegate_block_num, redelegations, rewards_distribution_frequency, + magnolia_hf_block_num) diff --git a/tests/rewards_stats_test.cpp b/tests/rewards_stats_test.cpp index 389423922b..eb240a19db 100644 --- a/tests/rewards_stats_test.cpp +++ b/tests/rewards_stats_test.cpp @@ -18,8 +18,8 @@ struct RewardsStatsTest : NodesTest {}; class TestableRewardsStats : public rewards::Stats { public: - TestableRewardsStats(const Hardforks::RewardsDistributionMap& rdm, std::shared_ptr db) - : rewards::Stats(100, Hardforks{0, {}, rdm, 0}, db, [](auto) { return 100; }) {} + TestableRewardsStats(const HardforksConfig::RewardsDistributionMap& rdm, std::shared_ptr db) + : rewards::Stats(100, HardforksConfig{0, {}, rdm, 0}, db, [](auto) { return 100; }) {} std::vector getStats() { return blocks_stats_; } }; From 4b22ebad56170731231001e39dc1676945b15e2f Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Fri, 18 Aug 2023 13:36:50 -0700 Subject: [PATCH 129/134] make slashing config part of magnolia hf config + improvements --- .../config_jsons/default/default_genesis.json | 10 ++++----- .../config_jsons/devnet/devnet_genesis.json | 10 ++++----- .../config_jsons/mainnet/mainnet_genesis.json | 10 ++++----- .../config_jsons/testnet/testnet_genesis.json | 10 ++++----- libraries/config/include/config/hardfork.hpp | 12 ++++++++-- .../config/include/config/state_config.hpp | 11 ---------- libraries/config/src/hardfork.cpp | 22 ++++++++++++++----- libraries/config/src/state_config.cpp | 16 +------------- .../slashing_manager/slashing_manager.hpp | 3 +-- .../consensus/src/rewards/rewards_stats.cpp | 2 +- .../src/slashing_manager/slashing_manager.cpp | 11 +++++++--- .../src/vote_manager/vote_manager.cpp | 2 +- .../common/ext_votes_packet_handler.cpp | 2 +- tests/final_chain_test.cpp | 2 +- tests/rewards_stats_test.cpp | 2 +- tests/state_api_test.cpp | 6 ++--- 16 files changed, 65 insertions(+), 66 deletions(-) diff --git a/libraries/cli/include/cli/config_jsons/default/default_genesis.json b/libraries/cli/include/cli/config_jsons/default/default_genesis.json index 5316a2485e..676b014fe7 100644 --- a/libraries/cli/include/cli/config_jsons/default/default_genesis.json +++ b/libraries/cli/include/cli/config_jsons/default/default_genesis.json @@ -54,10 +54,7 @@ }, "vrf_key": "f8d5c00ce9fa3058341e051b36a1e6ccf69df81fb865568b2bf1507d085691e2" } - ], - "slashing": { - "jail_time": 163459 - } + ] }, "initial_balances": { "2cd4da7d3b345e022ca7e997c2bb3276a4d3d2e9": "0x1027e72f1f12813088000000", @@ -103,6 +100,9 @@ "hardforks": { "fix_redelegate_block_num": 0, "rewards_distribution_frequency": {}, - "magnolia_hf_block_num" : 0 + "magnolia_hf" : { + "block_num" : 0, + "jail_time": 163459 + } } } \ No newline at end of file diff --git a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json index be3725fc2f..588fce3ef6 100644 --- a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json @@ -219,10 +219,7 @@ }, "vrf_key": "37bf145ac98e7de7db6e5b933e72737fbf190fd4fb1d193b15cf8b00db30ba30" } - ], - "slashing": { - "jail_time": 163459 - } + ] }, "initial_balances": { "2cd4da7d3b345e022ca7e997c2bb3276a4d3d2e9": "0x1027e72f1f12813088000000", @@ -269,6 +266,9 @@ "hardforks": { "fix_redelegate_block_num": 0, "rewards_distribution_frequency": {}, - "magnolia_hf_block_num" : 0 + "magnolia_hf" : { + "block_num" : 0, + "jail_time": 163459 + } } } diff --git a/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json b/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json index 3cd6a86698..a906dde36c 100644 --- a/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/mainnet/mainnet_genesis.json @@ -241,10 +241,7 @@ }, "vrf_key": "a25053afb5316ff12137ae573b6272b1258020454606631308d32cbf776788c0" } - ], - "slashing": { - "jail_time": 163459 - } + ] }, "initial_balances": { "723304d1357a2334fcf902aa3d232f5139080a1b": "0xd53323b7ca3737afbb45000", @@ -1638,6 +1635,9 @@ } ], "rewards_distribution_frequency": {}, - "magnolia_hf_block_num" : 0 + "magnolia_hf" : { + "block_num" : 0, + "jail_time": 163459 + } } } \ No newline at end of file diff --git a/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json b/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json index dec8b878c5..0dff0cfbce 100644 --- a/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/testnet/testnet_genesis.json @@ -87,10 +87,7 @@ }, "vrf_key": "f5f88dcc7fbfa50a576359ae05ace6ca28352fbc40b4ebbc99605e04c6f42883" } - ], - "slashing": { - "jail_time": 163459 - } + ] }, "initial_balances": { "76870407332398322576505f3c5423d0a71af296": "0x1d14a0219e54822428000000", @@ -138,6 +135,9 @@ "fix_redelegate_block_num": 0, "redelegations": [], "rewards_distribution_frequency": {}, - "magnolia_hf_block_num" : 0 + "magnolia_hf" : { + "block_num" : 0, + "jail_time": 163459 + } } } diff --git a/libraries/config/include/config/hardfork.hpp b/libraries/config/include/config/hardfork.hpp index f24cfab543..7246e9932c 100644 --- a/libraries/config/include/config/hardfork.hpp +++ b/libraries/config/include/config/hardfork.hpp @@ -11,10 +11,18 @@ struct Redelegation { taraxa::uint256_t amount; HAS_RLP_FIELDS }; - Json::Value enc_json(const Redelegation& obj); void dec_json(const Json::Value& json, Redelegation& obj); +struct MagnoliaHardfork { + uint64_t block_num = -1; + uint64_t jail_time = 0; // number of blocks + + HAS_RLP_FIELDS +}; +Json::Value enc_json(const MagnoliaHardfork& obj); +void dec_json(const Json::Value& json, MagnoliaHardfork& obj); + struct HardforksConfig { // disable it by default (set to max uint64) uint64_t fix_redelegate_block_num = -1; @@ -40,7 +48,7 @@ struct HardforksConfig { // creator. // 3. Introducing slashing/jailing contract - in case someone double votes - he is jailed for N blocks and unable to // participate in consensus - uint64_t magnolia_hf_block_num = -1; + MagnoliaHardfork magnolia_hf; HAS_RLP_FIELDS }; diff --git a/libraries/config/include/config/state_config.hpp b/libraries/config/include/config/state_config.hpp index 0762a51588..338f28e23c 100644 --- a/libraries/config/include/config/state_config.hpp +++ b/libraries/config/include/config/state_config.hpp @@ -37,14 +37,6 @@ struct ValidatorInfo { Json::Value enc_json(const ValidatorInfo& obj); void dec_json(const Json::Value& json, ValidatorInfo& obj); -struct Slashing { - uint64_t jail_time{0}; // number of blocks - - HAS_RLP_FIELDS -}; -Json::Value enc_json(const Slashing& obj); -void dec_json(const Json::Value& json, Slashing& obj); - struct DPOSConfig { u256 eligibility_balance_threshold; u256 vote_eligibility_balance_step; @@ -60,9 +52,6 @@ struct DPOSConfig { uint16_t yield_percentage = 0; // [%] std::vector initial_validators; - // Slashing - Slashing slashing; - HAS_RLP_FIELDS }; Json::Value enc_json(const DPOSConfig& obj); diff --git a/libraries/config/src/hardfork.cpp b/libraries/config/src/hardfork.cpp index 26d9a19ed0..6b4f7d5cee 100644 --- a/libraries/config/src/hardfork.cpp +++ b/libraries/config/src/hardfork.cpp @@ -16,6 +16,19 @@ void dec_json(const Json::Value& json, Redelegation& obj) { RLP_FIELDS_DEFINE(Redelegation, validator, delegator, amount) +Json::Value enc_json(const MagnoliaHardfork& obj) { + Json::Value json(Json::objectValue); + json["block_num"] = dev::toJS(obj.block_num); + json["jail_time"] = dev::toJS(obj.jail_time); + return json; +} + +void dec_json(const Json::Value& json, MagnoliaHardfork& obj) { + obj.block_num = dev::getUInt(json["block_num"]); + obj.jail_time = dev::getUInt(json["jail_time"]); +} +RLP_FIELDS_DEFINE(MagnoliaHardfork, block_num, jail_time) + Json::Value enc_json(const HardforksConfig& obj) { Json::Value json(Json::objectValue); json["fix_redelegate_block_num"] = dev::toJS(obj.fix_redelegate_block_num); @@ -30,7 +43,7 @@ Json::Value enc_json(const HardforksConfig& obj) { rewards[std::to_string(i->first)] = i->second; } - json["magnolia_hf_block_num"] = obj.magnolia_hf_block_num; + json["magnolia_hf"] = enc_json(obj.magnolia_hf); return json; } @@ -52,10 +65,9 @@ void dec_json(const Json::Value& json, HardforksConfig& obj) { obj.rewards_distribution_frequency[dev::getUInt(itr.key())] = dev::getUInt(*itr); } } - if (const auto& e = json["magnolia_hf_block_num"]) { - obj.magnolia_hf_block_num = dev::getUInt(e); + if (const auto& e = json["magnolia_hf"]) { + dec_json(e, obj.magnolia_hf); } } -RLP_FIELDS_DEFINE(HardforksConfig, fix_redelegate_block_num, redelegations, rewards_distribution_frequency, - magnolia_hf_block_num) +RLP_FIELDS_DEFINE(HardforksConfig, fix_redelegate_block_num, redelegations, rewards_distribution_frequency, magnolia_hf) diff --git a/libraries/config/src/state_config.cpp b/libraries/config/src/state_config.cpp index f500d9efee..e938d3d000 100644 --- a/libraries/config/src/state_config.cpp +++ b/libraries/config/src/state_config.cpp @@ -74,15 +74,6 @@ void dec_json(const Json::Value& json, ValidatorInfo& obj) { dec_json(json["delegations"], obj.delegations); } -Json::Value enc_json(const Slashing& obj) { - Json::Value json(Json::objectValue); - json["jail_time"] = dev::toJS(obj.jail_time); - - return json; -} - -void dec_json(const Json::Value& json, Slashing& obj) { obj.jail_time = dev::getUInt(json["jail_time"].asString()); } - Json::Value enc_json(const DPOSConfig& obj) { Json::Value json(Json::objectValue); json["eligibility_balance_threshold"] = dev::toJS(obj.eligibility_balance_threshold); @@ -103,8 +94,6 @@ Json::Value enc_json(const DPOSConfig& obj) { json["initial_validators"].append(enc_json(v)); } - json["slashing"] = enc_json(obj.slashing); - return json; } @@ -128,17 +117,14 @@ void dec_json(const Json::Value& json, DPOSConfig& obj) { for (uint32_t i = 0; i < initial_validators_json.size(); ++i) { dec_json(initial_validators_json[i], obj.initial_validators[i]); } - - dec_json(json["slashing"], obj.slashing); } RLP_FIELDS_DEFINE(EVMChainConfig, chain_id) RLP_FIELDS_DEFINE(ValidatorInfo, address, owner, vrf_key, commission, endpoint, description, delegations) -RLP_FIELDS_DEFINE(Slashing, jail_time) RLP_FIELDS_DEFINE(DPOSConfig, eligibility_balance_threshold, vote_eligibility_balance_step, validator_maximum_stake, minimum_deposit, max_block_author_reward, dag_proposers_reward, commission_change_delta, commission_change_frequency, delegation_delay, delegation_locking_period, blocks_per_year, - yield_percentage, initial_validators, slashing) + yield_percentage, initial_validators) RLP_FIELDS_DEFINE(Config, evm_chain_config, initial_balances, dpos, hardforks) RLP_FIELDS_DEFINE(Opts, expected_max_trx_per_block, max_trie_full_node_levels_to_cache) RLP_FIELDS_DEFINE(OptsDB, db_path, disable_most_recent_trie_value_views) diff --git a/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp b/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp index ba0c800a81..9c9ec65310 100644 --- a/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp +++ b/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp @@ -14,8 +14,7 @@ class SlashingManager { SlashingManager &operator=(const SlashingManager &) = delete; SlashingManager &operator=(SlashingManager &&) = delete; - bool submitDoubleVotingProof(PbftPeriod current_period, const std::shared_ptr &vote_a, - const std::shared_ptr &vote_b); + bool submitDoubleVotingProof(const std::shared_ptr &vote_a, const std::shared_ptr &vote_b); private: std::shared_ptr final_chain_; diff --git a/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp b/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp index 92ea28fe55..af27d8c7d4 100644 --- a/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp +++ b/libraries/core_libs/consensus/src/rewards/rewards_stats.cpp @@ -48,7 +48,7 @@ BlockStats Stats::getBlockStats(const PeriodData& blk, const std::vector& if (!blk.previous_block_cert_votes.empty()) [[likely]] { dpos_vote_count = dpos_eligible_total_vote_count_(blk.previous_block_cert_votes[0]->getPeriod() - 1); } - if (blk.pbft_blk->getPeriod() < kHardforks.magnolia_hf_block_num) { + if (blk.pbft_blk->getPeriod() < kHardforks.magnolia_hf.block_num) { return BlockStats{blk, {}, dpos_vote_count, kCommitteeSize}; } diff --git a/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp b/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp index b24849147e..d9e36cb9ec 100644 --- a/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp +++ b/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp @@ -14,14 +14,19 @@ SlashingManager::SlashingManager(std::shared_ptr final_chain, : final_chain_(std::move(final_chain)), trx_manager_(std::move(trx_manager)), gas_pricer_(std::move(gas_pricer)), - double_voting_proofs_(10000, 1000), + double_voting_proofs_(1000, 100), kGenesisConfig(genesis_config), kAddress(toAddress(node_sk)), kPrivateKey(std::move(node_sk)) {} -bool SlashingManager::submitDoubleVotingProof(PbftPeriod current_period, const std::shared_ptr &vote_a, +bool SlashingManager::submitDoubleVotingProof(const std::shared_ptr &vote_a, const std::shared_ptr &vote_b) { - if (current_period < kGenesisConfig.state.hardforks.magnolia_hf_block_num) { + if (vote_a->getPeriod() != vote_b->getPeriod() || vote_a->getRound() != vote_b->getRound() || + vote_a->getStep() != vote_b->getStep()) { + return false; + } + + if (vote_a->getPeriod() < kGenesisConfig.state.hardforks.magnolia_hf.block_num) { return false; } diff --git a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp index 4a65503883..3a614c7d12 100644 --- a/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp +++ b/libraries/core_libs/consensus/src/vote_manager/vote_manager.cpp @@ -199,7 +199,7 @@ bool VoteManager::addVerifiedVote(const std::shared_ptr& vote) { if (auto vote_inserted = insertUniqueVote(vote); !vote_inserted.first) { LOG(log_wr_) << "Non unique vote " << vote->getHash().abridged() << " (race condition)"; // Create double voting proof - slashing_manager_->submitDoubleVotingProof(pbft_chain_->getPbftChainSize(), vote, vote_inserted.second); + slashing_manager_->submitDoubleVotingProof(vote, vote_inserted.second); return false; } diff --git a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp index e9111198d0..e3d2754c35 100644 --- a/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp +++ b/libraries/core_libs/network/src/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.cpp @@ -43,7 +43,7 @@ bool ExtVotesPacketHandler::processVote(const std::shared_ptr &vote, const // (for a value that isn't NBH) per period, round & step if (auto vote_valid = vote_mgr_->isUniqueVote(vote); !vote_valid.first) { // Create double voting proof - slashing_manager_->submitDoubleVotingProof(pbft_chain_->getPbftChainSize(), vote, vote_valid.second); + slashing_manager_->submitDoubleVotingProof(vote, vote_valid.second); throw MaliciousPeerException("Received double vote", vote->getVoter()); } diff --git a/tests/final_chain_test.cpp b/tests/final_chain_test.cpp index c8b8c172c1..79180a906f 100644 --- a/tests/final_chain_test.cpp +++ b/tests/final_chain_test.cpp @@ -746,7 +746,7 @@ TEST_F(FinalChainTest, fee_rewards_distribution) { const auto& sk = sender_keys.secret(); cfg.genesis.state.initial_balances = {}; cfg.genesis.state.initial_balances[addr] = 100000; - cfg.genesis.state.hardforks.magnolia_hf_block_num = 2; + cfg.genesis.state.hardforks.magnolia_hf.block_num = 2; create_validators(); init(); const auto gas_price = 1; diff --git a/tests/rewards_stats_test.cpp b/tests/rewards_stats_test.cpp index eb240a19db..0c478e4e99 100644 --- a/tests/rewards_stats_test.cpp +++ b/tests/rewards_stats_test.cpp @@ -19,7 +19,7 @@ struct RewardsStatsTest : NodesTest {}; class TestableRewardsStats : public rewards::Stats { public: TestableRewardsStats(const HardforksConfig::RewardsDistributionMap& rdm, std::shared_ptr db) - : rewards::Stats(100, HardforksConfig{0, {}, rdm, 0}, db, [](auto) { return 100; }) {} + : rewards::Stats(100, HardforksConfig{0, {}, rdm, MagnoliaHardfork{0, 0}}, db, [](auto) { return 100; }) {} std::vector getStats() { return blocks_stats_; } }; diff --git a/tests/state_api_test.cpp b/tests/state_api_test.cpp index 60209c4e89..84612b407b 100644 --- a/tests/state_api_test.cpp +++ b/tests/state_api_test.cpp @@ -211,8 +211,8 @@ TEST_F(StateAPITest, DISABLED_eth_mainnet_smoke) { TEST_F(StateAPITest, slashing) { auto node_cfgs = make_node_cfgs(1, 1, 20); for (auto& cfg : node_cfgs) { - cfg.genesis.state.dpos.slashing.jail_time = 100; - cfg.genesis.state.hardforks.magnolia_hf_block_num = 0; + cfg.genesis.state.hardforks.magnolia_hf.jail_time = 100; + cfg.genesis.state.hardforks.magnolia_hf.block_num = 0; } auto nodes = launch_nodes(node_cfgs); @@ -229,7 +229,7 @@ TEST_F(StateAPITest, slashing) { auto slashing_manager = std::make_shared(node->getFinalChain(), node->getTransactionManager(), node->getGasPricer(), node_cfg.genesis, node->getSecretKey()); - ASSERT_EQ(true, slashing_manager->submitDoubleVotingProof(node->getPbftChain()->getPbftChainSize(), vote_a, vote_b)); + ASSERT_EQ(true, slashing_manager->submitDoubleVotingProof(vote_a, vote_b)); // After few blocks malicious validator should be jailed ASSERT_HAPPENS({5s, 100ms}, [&](auto& ctx) { From 879665bf4dd9232f55640663eb50fec34fa230f8 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 23 Aug 2023 15:00:41 -0700 Subject: [PATCH 130/134] use simplified slashing contract --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 62bbc4767e..c3c9173fb8 100644 --- a/.gitignore +++ b/.gitignore @@ -15,7 +15,7 @@ cmake-* # put here local env stuff local -*/local-net-data/ +/local-net-data/ .DS_Store From 7a167973f0cbcbdd27269f62501537a2d93736d3 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Thu, 24 Aug 2023 14:51:42 -0700 Subject: [PATCH 131/134] report malicious behaviour only if enabled in config + evm update --- libraries/config/include/config/config.hpp | 3 +++ libraries/config/src/config.cpp | 2 ++ .../slashing_manager/slashing_manager.hpp | 4 ++-- .../consensus/src/pbft/pbft_manager.cpp | 2 +- .../src/slashing_manager/slashing_manager.cpp | 16 ++++++++----- libraries/core_libs/node/src/node.cpp | 3 +-- submodules/taraxa-evm | 2 +- tests/state_api_test.cpp | 23 ++++++++++++++----- 8 files changed, 37 insertions(+), 18 deletions(-) diff --git a/libraries/config/include/config/config.hpp b/libraries/config/include/config/config.hpp index 04a60b05bd..275a16dce4 100644 --- a/libraries/config/include/config/config.hpp +++ b/libraries/config/include/config/config.hpp @@ -63,6 +63,9 @@ struct FullNodeConfig { // config values that limits transactions pool uint32_t transactions_pool_size = kDefaultTransactionPoolSize; + // Report malicious behaviour like double voting, etc... to slashing/jailing contract + bool report_malicious_behaviour = false; + auto net_file_path() const { return data_path / "net"; } /** diff --git a/libraries/config/src/config.cpp b/libraries/config/src/config.cpp index 8dae3b49d5..c3f8e6225e 100644 --- a/libraries/config/src/config.cpp +++ b/libraries/config/src/config.cpp @@ -85,6 +85,8 @@ void FullNodeConfig::overwriteConfigFromJson(const Json::Value &root) { is_light_node = getConfigDataAsBoolean(root, {"is_light_node"}, true, is_light_node); light_node_history = getConfigDataAsUInt(root, {"light_node_history"}, true, light_node_history); + report_malicious_behaviour = + getConfigDataAsUInt(root, {"report_malicious_behaviour"}, true, report_malicious_behaviour); } FullNodeConfig::FullNodeConfig(const Json::Value &string_or_object, const Json::Value &wallet, diff --git a/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp b/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp index 9c9ec65310..4c8562d2a2 100644 --- a/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp +++ b/libraries/core_libs/consensus/include/slashing_manager/slashing_manager.hpp @@ -8,7 +8,7 @@ namespace taraxa { class SlashingManager { public: SlashingManager(std::shared_ptr final_chain, std::shared_ptr trx_manager, - std::shared_ptr gas_pricer, const GenesisConfig &genesis_config, secret_t node_sk); + std::shared_ptr gas_pricer, const FullNodeConfig &config, secret_t node_sk); SlashingManager(const SlashingManager &) = delete; SlashingManager(SlashingManager &&) = delete; SlashingManager &operator=(const SlashingManager &) = delete; @@ -24,7 +24,7 @@ class SlashingManager { // Already processed double voting proofs ExpirationCache double_voting_proofs_; - const GenesisConfig kGenesisConfig; + const FullNodeConfig kConfig; const addr_t kAddress; const secret_t kPrivateKey; }; diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index 59c04938ea..88f2c65d11 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -307,7 +307,7 @@ bool PbftManager::advancePeriod() { // Cleanup proposed blocks proposed_blocks_.cleanupProposedPbftBlocksByPeriod(new_period); - LOG(log_nf_) << "Period advanced to: " << new_period << ", round and step reset to 1"; + LOG(log_er_) << "Period advanced to: " << new_period << ", round and step reset to 1"; // Restart while loop... return true; diff --git a/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp b/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp index d9e36cb9ec..5327169344 100644 --- a/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp +++ b/libraries/core_libs/consensus/src/slashing_manager/slashing_manager.cpp @@ -10,23 +10,27 @@ const auto kContractAddress = addr_t("0x00000000000000000000000000000000000000EE SlashingManager::SlashingManager(std::shared_ptr final_chain, std::shared_ptr trx_manager, std::shared_ptr gas_pricer, - const GenesisConfig &genesis_config, secret_t node_sk) + const FullNodeConfig &config, secret_t node_sk) : final_chain_(std::move(final_chain)), trx_manager_(std::move(trx_manager)), gas_pricer_(std::move(gas_pricer)), double_voting_proofs_(1000, 100), - kGenesisConfig(genesis_config), + kConfig(config), kAddress(toAddress(node_sk)), kPrivateKey(std::move(node_sk)) {} bool SlashingManager::submitDoubleVotingProof(const std::shared_ptr &vote_a, const std::shared_ptr &vote_b) { - if (vote_a->getPeriod() != vote_b->getPeriod() || vote_a->getRound() != vote_b->getRound() || - vote_a->getStep() != vote_b->getStep()) { + if (!kConfig.report_malicious_behaviour) { return false; } - if (vote_a->getPeriod() < kGenesisConfig.state.hardforks.magnolia_hf.block_num) { + if (vote_a->getPeriod() < kConfig.genesis.state.hardforks.magnolia_hf.block_num) { + return false; + } + + if (vote_a->getPeriod() != vote_b->getPeriod() || vote_a->getRound() != vote_b->getRound() || + vote_a->getStep() != vote_b->getStep()) { return false; } @@ -59,7 +63,7 @@ bool SlashingManager::submitDoubleVotingProof(const std::shared_ptr &vote_ // TODO we need to calculate gas for this transaction and hardcode it // CommitDoubleVotingProofGas uint64 = 20000 const auto trx = std::make_shared(account.nonce, 0, gas_pricer_->bid(), 1000000, std::move(input), - kPrivateKey, kContractAddress, kGenesisConfig.chain_id); + kPrivateKey, kContractAddress, kConfig.genesis.chain_id); if (trx_manager_->insertTransaction(trx).first) { double_voting_proofs_.insert(hash); diff --git a/libraries/core_libs/node/src/node.cpp b/libraries/core_libs/node/src/node.cpp index e1f8c7f58e..cf3eeb43a1 100644 --- a/libraries/core_libs/node/src/node.cpp +++ b/libraries/core_libs/node/src/node.cpp @@ -127,8 +127,7 @@ void FullNode::init() { conf_.genesis.dag, trx_mgr_, pbft_chain_, final_chain_, db_, key_manager_, conf_.genesis.pbft.gas_limit, conf_.is_light_node, conf_.light_node_history, conf_.max_levels_per_period, conf_.dag_expiry_limit); - auto slashing_manager = - std::make_shared(final_chain_, trx_mgr_, gas_pricer_, conf_.genesis, kp_.secret()); + auto slashing_manager = std::make_shared(final_chain_, trx_mgr_, gas_pricer_, conf_, kp_.secret()); vote_mgr_ = std::make_shared(node_addr, conf_.genesis.pbft, kp_.secret(), conf_.vrf_secret, db_, pbft_chain_, final_chain_, key_manager_, slashing_manager); pbft_mgr_ = diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index e3d0731365..712a29ca9b 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit e3d0731365c3b898f1ce79163caa29be217ea0b7 +Subproject commit 712a29ca9bd8737241b5dc21ce3469074dc92438 diff --git a/tests/state_api_test.cpp b/tests/state_api_test.cpp index 84612b407b..3fd5dae072 100644 --- a/tests/state_api_test.cpp +++ b/tests/state_api_test.cpp @@ -209,15 +209,19 @@ TEST_F(StateAPITest, DISABLED_eth_mainnet_smoke) { } TEST_F(StateAPITest, slashing) { - auto node_cfgs = make_node_cfgs(1, 1, 20); + auto node_cfgs = make_node_cfgs(1, 1, 5); + // Option 2: more sophisticated and longer test + // auto node_cfgs = make_node_cfgs(4, 4, 5); for (auto& cfg : node_cfgs) { - cfg.genesis.state.hardforks.magnolia_hf.jail_time = 100; + cfg.genesis.state.dpos.delegation_delay = 2; + cfg.genesis.state.hardforks.magnolia_hf.jail_time = 2; cfg.genesis.state.hardforks.magnolia_hf.block_num = 0; + cfg.report_malicious_behaviour = true; } auto nodes = launch_nodes(node_cfgs); auto node = nodes.begin()->get(); - auto node_cfg = node_cfgs[0]; + auto node_cfg = node_cfgs.begin(); ASSERT_EQ(true, node->getFinalChain()->dpos_is_eligible(node->getFinalChain()->last_block_number(), node->getAddress())); @@ -226,9 +230,8 @@ TEST_F(StateAPITest, slashing) { auto vote_b = node->getVoteManager()->generateVote(blk_hash_t{2}, PbftVoteTypes::cert_vote, 1, 1, 3); // Commit double voting proof - auto slashing_manager = - std::make_shared(node->getFinalChain(), node->getTransactionManager(), node->getGasPricer(), - node_cfg.genesis, node->getSecretKey()); + auto slashing_manager = std::make_shared(node->getFinalChain(), node->getTransactionManager(), + node->getGasPricer(), *node_cfg, node->getSecretKey()); ASSERT_EQ(true, slashing_manager->submitDoubleVotingProof(vote_a, vote_b)); // After few blocks malicious validator should be jailed @@ -237,6 +240,14 @@ TEST_F(StateAPITest, slashing) { ctx, false, node->getFinalChain()->dpos_is_eligible(node->getFinalChain()->last_block_number(), node->getAddress())) }); + + // Option 2: more sophisticated and longer test + // After few blocks malicious validator should be unjailed + // ASSERT_HAPPENS({5s, 100ms}, [&](auto& ctx) { + // WAIT_EXPECT_EQ( + // ctx, true, + // node->getFinalChain()->dpos_is_eligible(node->getFinalChain()->last_block_number(), node->getAddress())) + // }); } } // namespace taraxa::state_api From b9c995ee0b0f7e4ced03e139472c20ac91c18f29 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Tue, 19 Sep 2023 12:06:55 +0200 Subject: [PATCH 132/134] chore: improve dag propose probability --- .../include/dag/dag_block_proposer.hpp | 6 +- .../consensus/include/dag/dag_manager.hpp | 6 +- .../consensus/src/dag/dag_block_proposer.cpp | 16 +++-- .../consensus/src/dag/dag_manager.cpp | 26 +++++--- .../consensus/src/pbft/pbft_manager.cpp | 2 +- libraries/core_libs/node/src/node.cpp | 10 +-- tests/crypto_test.cpp | 63 +++++++++++++++++++ tests/dag_test.cpp | 36 +++++------ tests/full_node_test.cpp | 12 +++- tests/state_api_test.cpp | 2 +- 10 files changed, 137 insertions(+), 42 deletions(-) diff --git a/libraries/core_libs/consensus/include/dag/dag_block_proposer.hpp b/libraries/core_libs/consensus/include/dag/dag_block_proposer.hpp index fe78837f05..0b79a617ed 100644 --- a/libraries/core_libs/consensus/include/dag/dag_block_proposer.hpp +++ b/libraries/core_libs/consensus/include/dag/dag_block_proposer.hpp @@ -38,7 +38,8 @@ class DagBlockProposer { DagBlockProposer(const DagBlockProposerConfig& bp_config, std::shared_ptr dag_mgr, std::shared_ptr trx_mgr, std::shared_ptr final_chain, std::shared_ptr db, std::shared_ptr key_manager, addr_t node_addr, - secret_t node_sk, vrf_wrapper::vrf_sk_t vrf_sk, uint64_t pbft_gas_limit, uint64_t dag_gas_limit); + secret_t node_sk, vrf_wrapper::vrf_sk_t vrf_sk, uint64_t pbft_gas_limit, uint64_t dag_gas_limit, + const state_api::Config& state_config); ~DagBlockProposer() { stop(); } DagBlockProposer(const DagBlockProposer&) = delete; DagBlockProposer(DagBlockProposer&&) = delete; @@ -149,6 +150,9 @@ class DagBlockProposer { const uint64_t kPbftGasLimit; const uint64_t kDagGasLimit; + const HardforksConfig kHardforks; + const uint64_t kValidatorMaxVote; + LOG_OBJECTS_DEFINE }; diff --git a/libraries/core_libs/consensus/include/dag/dag_manager.hpp b/libraries/core_libs/consensus/include/dag/dag_manager.hpp index b9cde3e56b..6e77862f1b 100644 --- a/libraries/core_libs/consensus/include/dag/dag_manager.hpp +++ b/libraries/core_libs/consensus/include/dag/dag_manager.hpp @@ -48,8 +48,8 @@ class DagManager : public std::enable_shared_from_this { const DagConfig &dag_config, std::shared_ptr trx_mgr, std::shared_ptr pbft_chain, std::shared_ptr final_chain, std::shared_ptr db, std::shared_ptr key_manager, uint64_t pbft_gas_limit, - bool is_light_node = false, uint64_t light_node_history = 0, - uint32_t max_levels_per_period = kMaxLevelsPerPeriod, + const state_api::Config &state_config, bool is_light_node = false, + uint64_t light_node_history = 0, uint32_t max_levels_per_period = kMaxLevelsPerPeriod, uint32_t dag_expiry_limit = kDagExpiryLevelLimit); DagManager(const DagManager &) = delete; @@ -282,6 +282,8 @@ class DagManager : public std::enable_shared_from_this { ExpirationCacheMap seen_blocks_; std::shared_ptr final_chain_; const uint64_t kPbftGasLimit; + const HardforksConfig kHardforks; + const uint64_t kValidatorMaxVote; LOG_OBJECTS_DEFINE }; diff --git a/libraries/core_libs/consensus/src/dag/dag_block_proposer.cpp b/libraries/core_libs/consensus/src/dag/dag_block_proposer.cpp index 416a8f6906..44d6d4f0a1 100644 --- a/libraries/core_libs/consensus/src/dag/dag_block_proposer.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_block_proposer.cpp @@ -17,7 +17,8 @@ DagBlockProposer::DagBlockProposer(const DagBlockProposerConfig& bp_config, std: std::shared_ptr trx_mgr, std::shared_ptr final_chain, std::shared_ptr db, std::shared_ptr key_manager, addr_t node_addr, secret_t node_sk, - vrf_wrapper::vrf_sk_t vrf_sk, uint64_t pbft_gas_limit, uint64_t dag_gas_limit) + vrf_wrapper::vrf_sk_t vrf_sk, uint64_t pbft_gas_limit, uint64_t dag_gas_limit, + const state_api::Config& state_config) : bp_config_(bp_config), total_trx_shards_(std::max(bp_config_.shard, uint16_t(1))), dag_mgr_(std::move(dag_mgr)), @@ -30,7 +31,9 @@ DagBlockProposer::DagBlockProposer(const DagBlockProposerConfig& bp_config, std: vrf_sk_(std::move(vrf_sk)), vrf_pk_(vrf_wrapper::getVrfPublicKey(vrf_sk_)), kPbftGasLimit(pbft_gas_limit), - kDagGasLimit(dag_gas_limit) { + kDagGasLimit(dag_gas_limit), + kHardforks(state_config.hardforks), + kValidatorMaxVote(state_config.dpos.validator_maximum_stake / state_config.dpos.vote_eligibility_balance_step) { LOG_OBJECTS_CREATE("DAG_PROPOSER"); // Add a random component in proposing stale blocks so that not all nodes propose stale blocks at the same time @@ -69,15 +72,20 @@ bool DagBlockProposer::proposeDagBlock() { return false; } - const auto total_vote_count = final_chain_->dpos_eligible_total_vote_count(*proposal_period); + uint64_t max_vote_count = 0; const auto vote_count = final_chain_->dpos_eligible_vote_count(*proposal_period, node_addr_); + if (*proposal_period < kHardforks.magnolia_hf.block_num) { + max_vote_count = final_chain_->dpos_eligible_total_vote_count(*proposal_period); + } else { + max_vote_count = kValidatorMaxVote; + } const auto period_block_hash = db_->getPeriodBlockHash(*proposal_period); // get sortition const auto sortition_params = dag_mgr_->sortitionParamsManager().getSortitionParams(*proposal_period); vdf_sortition::VdfSortition vdf(sortition_params, vrf_sk_, VrfSortitionBase::makeVrfInput(propose_level, period_block_hash), vote_count, - total_vote_count); + max_vote_count); if (vdf.isStale(sortition_params)) { if (last_propose_level_ == propose_level) { if (num_tries_ < max_num_tries_) { diff --git a/libraries/core_libs/consensus/src/dag/dag_manager.cpp b/libraries/core_libs/consensus/src/dag/dag_manager.cpp index a27985784c..d9e4229ff1 100644 --- a/libraries/core_libs/consensus/src/dag/dag_manager.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_manager.cpp @@ -19,8 +19,8 @@ DagManager::DagManager(const DagBlock &dag_genesis_block, addr_t node_addr, cons const DagConfig &dag_config, std::shared_ptr trx_mgr, std::shared_ptr pbft_chain, std::shared_ptr final_chain, std::shared_ptr db, std::shared_ptr key_manager, uint64_t pbft_gas_limit, - bool is_light_node, uint64_t light_node_history, uint32_t max_levels_per_period, - uint32_t dag_expiry_limit) try + const state_api::Config &state_config, bool is_light_node, uint64_t light_node_history, + uint32_t max_levels_per_period, uint32_t dag_expiry_limit) try : max_level_(db->getLastBlocksLevel()), pivot_tree_(std::make_shared(dag_genesis_block.getHash(), node_addr)), total_dag_(std::make_shared(dag_genesis_block.getHash(), node_addr)), @@ -39,7 +39,9 @@ DagManager::DagManager(const DagBlock &dag_genesis_block, addr_t node_addr, cons dag_expiry_limit_(dag_expiry_limit), seen_blocks_(cache_max_size_, cache_delete_step_), final_chain_(std::move(final_chain)), - kPbftGasLimit(pbft_gas_limit) { + kPbftGasLimit(pbft_gas_limit), + kHardforks(state_config.hardforks), + kValidatorMaxVote(state_config.dpos.validator_maximum_stake / state_config.dpos.vote_eligibility_balance_step) { LOG_OBJECTS_CREATE("DAGMGR"); if (auto ret = getLatestPivotAndTips(); ret) { frontier_.pivot = ret->first; @@ -502,10 +504,15 @@ void DagManager::recoverDag() { } // Verify VDF solution try { - const auto total_vote_count = final_chain_->dpos_eligible_total_vote_count(*propose_period); + uint64_t max_vote_count = 0; const auto vote_count = final_chain_->dpos_eligible_vote_count(*propose_period, blk.getSender()); + if (*propose_period < kHardforks.magnolia_hf.block_num) { + max_vote_count = final_chain_->dpos_eligible_total_vote_count(*propose_period); + } else { + max_vote_count = kValidatorMaxVote; + } blk.verifyVdf(sortition_params_manager_.getSortitionParams(*propose_period), - db_->getPeriodBlockHash(*propose_period), *pk, vote_count, total_vote_count); + db_->getPeriodBlockHash(*propose_period), *pk, vote_count, max_vote_count); } catch (vdf_sortition::VdfSortition::InvalidVdfSortition const &e) { LOG(log_er_) << "DAG block " << blk.getHash() << " with " << blk.getLevel() << " level failed on VDF verification with pivot hash " << blk.getPivot() << " reason " @@ -630,10 +637,15 @@ DagManager::VerifyBlockReturnType DagManager::verifyBlock(const DagBlock &blk) { try { const auto proposal_period_hash = db_->getPeriodBlockHash(*propose_period); - const auto total_vote_count = final_chain_->dpos_eligible_total_vote_count(*propose_period); + uint64_t max_vote_count = 0; const auto vote_count = final_chain_->dpos_eligible_vote_count(*propose_period, blk.getSender()); + if (*propose_period < kHardforks.magnolia_hf.block_num) { + max_vote_count = final_chain_->dpos_eligible_total_vote_count(*propose_period); + } else { + max_vote_count = kValidatorMaxVote; + } blk.verifyVdf(sortition_params_manager_.getSortitionParams(*propose_period), proposal_period_hash, *pk, vote_count, - total_vote_count); + max_vote_count); } catch (vdf_sortition::VdfSortition::InvalidVdfSortition const &e) { LOG(log_er_) << "DAG block " << block_hash << " with " << blk.getLevel() << " level failed on VDF verification with pivot hash " << blk.getPivot() << " reason " << e.what(); diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index 88f2c65d11..59c04938ea 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -307,7 +307,7 @@ bool PbftManager::advancePeriod() { // Cleanup proposed blocks proposed_blocks_.cleanupProposedPbftBlocksByPeriod(new_period); - LOG(log_er_) << "Period advanced to: " << new_period << ", round and step reset to 1"; + LOG(log_nf_) << "Period advanced to: " << new_period << ", round and step reset to 1"; // Restart while loop... return true; diff --git a/libraries/core_libs/node/src/node.cpp b/libraries/core_libs/node/src/node.cpp index cf3eeb43a1..a0d834ac64 100644 --- a/libraries/core_libs/node/src/node.cpp +++ b/libraries/core_libs/node/src/node.cpp @@ -123,10 +123,10 @@ void FullNode::init() { } pbft_chain_ = std::make_shared(node_addr, db_); - dag_mgr_ = std::make_shared(conf_.genesis.dag_genesis_block, node_addr, conf_.genesis.sortition, - conf_.genesis.dag, trx_mgr_, pbft_chain_, final_chain_, db_, key_manager_, - conf_.genesis.pbft.gas_limit, conf_.is_light_node, conf_.light_node_history, - conf_.max_levels_per_period, conf_.dag_expiry_limit); + dag_mgr_ = std::make_shared( + conf_.genesis.dag_genesis_block, node_addr, conf_.genesis.sortition, conf_.genesis.dag, trx_mgr_, pbft_chain_, + final_chain_, db_, key_manager_, conf_.genesis.pbft.gas_limit, conf_.genesis.state, conf_.is_light_node, + conf_.light_node_history, conf_.max_levels_per_period, conf_.dag_expiry_limit); auto slashing_manager = std::make_shared(final_chain_, trx_mgr_, gas_pricer_, conf_, kp_.secret()); vote_mgr_ = std::make_shared(node_addr, conf_.genesis.pbft, kp_.secret(), conf_.vrf_secret, db_, pbft_chain_, final_chain_, key_manager_, slashing_manager); @@ -135,7 +135,7 @@ void FullNode::init() { pbft_chain_, vote_mgr_, dag_mgr_, trx_mgr_, final_chain_, kp_.secret()); dag_block_proposer_ = std::make_shared( 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); + getVrfSecretKey(), conf_.genesis.pbft.gas_limit, conf_.genesis.dag.gas_limit, conf_.genesis.state); network_ = std::make_shared(conf_, genesis_hash, conf_.net_file_path().string(), kp_, db_, pbft_mgr_, pbft_chain_, vote_mgr_, dag_mgr_, trx_mgr_, std::move(slashing_manager)); diff --git a/tests/crypto_test.cpp b/tests/crypto_test.cpp index 5218146128..fb82494eb1 100644 --- a/tests/crypto_test.cpp +++ b/tests/crypto_test.cpp @@ -173,6 +173,69 @@ TEST_F(CryptoTest, vdf_stake_test) { << (count_dag_blocks_production[i] * 1000 / total_dags % 100) << "%" << std::endl; } } + + // Post magnolia hardfork stakes + for (uint32_t upper_threshold = 0x5ff; upper_threshold < 0xffff; upper_threshold *= 3) { + std::cout << "Upper threshold: " << upper_threshold << std::endl; + SortitionParams sortition_params(upper_threshold, 16, 21, 23, 0x64); + uint64_t total_vote_count = 800; + const uint64_t voters_count = 8; + uint64_t voters_vote_count[voters_count]; + uint64_t count_dag_blocks_production[voters_count]; + for (uint64_t i = 0; i < voters_count; i++) { + count_dag_blocks_production[i] = 0; + } + voters_vote_count[0] = 50; + voters_vote_count[1] = 100; + voters_vote_count[2] = 200; + voters_vote_count[3] = 300; + voters_vote_count[4] = 400; + voters_vote_count[5] = 500; + voters_vote_count[6] = 650; + voters_vote_count[7] = 800; + vrf_sk_t sk( + "0b6627a6680e01cea3d9f36fa797f7f34e8869c3a526d9ed63ed8170e35542aad05dc12c" + "1df1edc9f3367fba550b7971fc2de6c5998d8784051c5be69abc9644"); + for (uint32_t counter = 1; counter < 3000; counter++) { + level_t level = counter * voters_count; + uint64_t difficulties[voters_count]; + for (uint64_t i = 0; i < voters_count; i++) { + VdfSortition vdf(sortition_params, sk, getRlpBytes(level + i), voters_vote_count[i], total_vote_count); + difficulties[i] = vdf.getDifficulty(); + } + uint64_t min_diff = 24; + for (uint64_t i = 0; i < voters_count; i++) { + if (difficulties[i] < min_diff) { + min_diff = difficulties[i]; + } + } + // Stale block is produced by random node + if (min_diff == 23) { + count_dag_blocks_production[rand() % voters_count]++; + } else { + for (uint64_t i = 0; i < voters_count; i++) { + if (difficulties[i] == min_diff) { + count_dag_blocks_production[i]++; + // std::cout << "min AT " << i << std::endl; + } + } + } + } + uint64_t total_dags = 0; + for (auto count : count_dag_blocks_production) { + total_dags += count; + } + std::cout << "total_dags: " << total_dags << std::endl; + for (uint64_t i = 0; i < voters_count; i++) { + if (i > 0) { + // Verify that greater stake produce more dag blocks + EXPECT_GE(count_dag_blocks_production[i], count_dag_blocks_production[i - 1]); + } + std::cout << "Vote stake " << voters_vote_count[i] / 8 << "." << voters_vote_count[i] % 8 + << "% - Dag ratio: " << count_dag_blocks_production[i] * 100 / total_dags << "." + << (count_dag_blocks_production[i] * 1000 / total_dags % 100) << "%" << std::endl; + } + } } TEST_F(CryptoTest, vdf_sortition) { diff --git a/tests/dag_test.cpp b/tests/dag_test.cpp index 8d7cdc07ff..9a74fad8dc 100644 --- a/tests/dag_test.cpp +++ b/tests/dag_test.cpp @@ -135,9 +135,9 @@ TEST_F(DagTest, compute_epoch) { auto trx_mgr = std::make_shared(FullNodeConfig(), db_ptr, nullptr, addr_t()); auto pbft_chain = std::make_shared(addr_t(), db_ptr); const blk_hash_t GENESIS = node_cfgs[0].genesis.dag_genesis_block.getHash(); - auto mgr = - std::make_shared(node_cfgs[0].genesis.dag_genesis_block, addr_t(), node_cfgs[0].genesis.sortition, - node_cfgs[0].genesis.dag, trx_mgr, pbft_chain, nullptr, db_ptr, nullptr, 100000); + auto mgr = std::make_shared(node_cfgs[0].genesis.dag_genesis_block, addr_t(), + node_cfgs[0].genesis.sortition, node_cfgs[0].genesis.dag, trx_mgr, pbft_chain, + nullptr, db_ptr, nullptr, 100000, node_cfgs[0].genesis.state); DagBlock blkA(GENESIS, 1, {}, {trx_hash_t(2)}, sig_t(1), blk_hash_t(2), addr_t(1)); DagBlock blkB(GENESIS, 1, {}, {trx_hash_t(3), trx_hash_t(4)}, sig_t(1), blk_hash_t(3), addr_t(1)); @@ -228,9 +228,9 @@ TEST_F(DagTest, dag_expiry) { auto trx_mgr = std::make_shared(FullNodeConfig(), db_ptr, nullptr, addr_t()); auto pbft_chain = std::make_shared(addr_t(), db_ptr); const blk_hash_t GENESIS = node_cfgs[0].genesis.dag_genesis_block.getHash(); - auto mgr = std::make_shared(node_cfgs[0].genesis.dag_genesis_block, addr_t(), - node_cfgs[0].genesis.sortition, node_cfgs[0].genesis.dag, trx_mgr, pbft_chain, - nullptr, db_ptr, nullptr, 100000, false, 0, 3, EXPIRY_LIMIT); + auto mgr = std::make_shared( + node_cfgs[0].genesis.dag_genesis_block, addr_t(), node_cfgs[0].genesis.sortition, node_cfgs[0].genesis.dag, + trx_mgr, pbft_chain, nullptr, db_ptr, nullptr, 100000, node_cfgs[0].genesis.state, false, 0, 3, EXPIRY_LIMIT); DagBlock blkA(GENESIS, 1, {}, {trx_hash_t(2)}, sig_t(1), blk_hash_t(2), addr_t(1)); DagBlock blkB(GENESIS, 1, {}, {trx_hash_t(3), trx_hash_t(4)}, sig_t(1), blk_hash_t(3), addr_t(1)); @@ -304,9 +304,9 @@ TEST_F(DagTest, receive_block_in_order) { auto pbft_chain = std::make_shared(addr_t(), db_ptr); auto trx_mgr = std::make_shared(FullNodeConfig(), db_ptr, nullptr, addr_t()); const blk_hash_t GENESIS = node_cfgs[0].genesis.dag_genesis_block.getHash(); - auto mgr = - std::make_shared(node_cfgs[0].genesis.dag_genesis_block, addr_t(), node_cfgs[0].genesis.sortition, - node_cfgs[0].genesis.dag, trx_mgr, pbft_chain, nullptr, db_ptr, nullptr, 100000); + auto mgr = std::make_shared(node_cfgs[0].genesis.dag_genesis_block, addr_t(), + node_cfgs[0].genesis.sortition, node_cfgs[0].genesis.dag, trx_mgr, pbft_chain, + nullptr, db_ptr, nullptr, 100000, node_cfgs[0].genesis.state); DagBlock blk1(GENESIS, 1, {}, {}, sig_t(777), blk_hash_t(1), addr_t(15)); DagBlock blk2(blk_hash_t(1), 2, {}, {}, sig_t(777), blk_hash_t(2), addr_t(15)); @@ -336,9 +336,9 @@ TEST_F(DagTest, compute_epoch_2) { auto pbft_chain = std::make_shared(addr_t(), db_ptr); auto trx_mgr = std::make_shared(FullNodeConfig(), db_ptr, nullptr, addr_t()); const blk_hash_t GENESIS = node_cfgs[0].genesis.dag_genesis_block.getHash(); - auto mgr = - std::make_shared(node_cfgs[0].genesis.dag_genesis_block, addr_t(), node_cfgs[0].genesis.sortition, - node_cfgs[0].genesis.dag, trx_mgr, pbft_chain, nullptr, db_ptr, nullptr, 100000); + auto mgr = std::make_shared(node_cfgs[0].genesis.dag_genesis_block, addr_t(), + node_cfgs[0].genesis.sortition, node_cfgs[0].genesis.dag, trx_mgr, pbft_chain, + nullptr, db_ptr, nullptr, 100000, node_cfgs[0].genesis.state); DagBlock blkA(GENESIS, 1, {}, {trx_hash_t(2)}, sig_t(1), blk_hash_t(2), addr_t(1)); DagBlock blkB(GENESIS, 1, {}, {trx_hash_t(3), trx_hash_t(4)}, sig_t(1), blk_hash_t(3), addr_t(1)); @@ -419,9 +419,9 @@ TEST_F(DagTest, get_latest_pivot_tips) { auto trx_mgr = std::make_shared(FullNodeConfig(), db_ptr, nullptr, addr_t()); auto pbft_chain = std::make_shared(addr_t(), db_ptr); const blk_hash_t GENESIS = node_cfgs[0].genesis.dag_genesis_block.getHash(); - auto mgr = - std::make_shared(node_cfgs[0].genesis.dag_genesis_block, addr_t(), node_cfgs[0].genesis.sortition, - node_cfgs[0].genesis.dag, trx_mgr, pbft_chain, nullptr, db_ptr, nullptr, 100000); + auto mgr = std::make_shared(node_cfgs[0].genesis.dag_genesis_block, addr_t(), + node_cfgs[0].genesis.sortition, node_cfgs[0].genesis.dag, trx_mgr, pbft_chain, + nullptr, db_ptr, nullptr, 100000, node_cfgs[0].genesis.state); DagBlock blk2(GENESIS, 1, {}, {}, sig_t(1), blk_hash_t(2), addr_t(15)); DagBlock blk3(blk_hash_t(2), 2, {}, {}, sig_t(1), blk_hash_t(3), addr_t(15)); @@ -446,9 +446,9 @@ TEST_F(DagTest, initial_pivot) { auto db_ptr = std::make_shared(data_dir / "db"); auto trx_mgr = std::make_shared(FullNodeConfig(), db_ptr, nullptr, addr_t()); auto pbft_chain = std::make_shared(addr_t(), db_ptr); - auto mgr = - std::make_shared(node_cfgs[0].genesis.dag_genesis_block, addr_t(), node_cfgs[0].genesis.sortition, - node_cfgs[0].genesis.dag, trx_mgr, pbft_chain, nullptr, db_ptr, nullptr, 100000); + auto mgr = std::make_shared(node_cfgs[0].genesis.dag_genesis_block, addr_t(), + node_cfgs[0].genesis.sortition, node_cfgs[0].genesis.dag, trx_mgr, pbft_chain, + nullptr, db_ptr, nullptr, 100000, node_cfgs[0].genesis.state); auto pt = mgr->getLatestPivotAndTips(); diff --git a/tests/full_node_test.cpp b/tests/full_node_test.cpp index f9df1ee239..770939230d 100644 --- a/tests/full_node_test.cpp +++ b/tests/full_node_test.cpp @@ -1433,11 +1433,17 @@ TEST_F(FullNodeTest, light_node) { non_empty_counter++; } } + + uint32_t non_empty_counter_full_node = 0; + for (uint64_t i = 0; i < nodes[0]->getPbftChain()->getPbftChainSize(); i++) { + const auto pbft_block = nodes[0]->getDB()->getPbftBlock(i); + if (pbft_block) { + non_empty_counter_full_node++; + } + } // Verify light node keeps at least light_node_history and it deletes old blocks EXPECT_GE(non_empty_counter, node_cfgs[1].light_node_history); - // Actual history size will be between 100% and 110% of light_node_history_ to - // avoid deleting on every period - EXPECT_LE(non_empty_counter, node_cfgs[1].light_node_history * 1.1 + node_cfgs[1].dag_expiry_limit); + EXPECT_LT(non_empty_counter, non_empty_counter_full_node); } TEST_F(FullNodeTest, clear_period_data) { diff --git a/tests/state_api_test.cpp b/tests/state_api_test.cpp index 3fd5dae072..723ac88150 100644 --- a/tests/state_api_test.cpp +++ b/tests/state_api_test.cpp @@ -235,7 +235,7 @@ TEST_F(StateAPITest, slashing) { ASSERT_EQ(true, slashing_manager->submitDoubleVotingProof(vote_a, vote_b)); // After few blocks malicious validator should be jailed - ASSERT_HAPPENS({5s, 100ms}, [&](auto& ctx) { + ASSERT_HAPPENS({10s, 100ms}, [&](auto& ctx) { WAIT_EXPECT_EQ( ctx, false, node->getFinalChain()->dpos_is_eligible(node->getFinalChain()->last_block_number(), node->getAddress())) From 9585f420e370140ab039ad0622129867317afc68 Mon Sep 17 00:00:00 2001 From: Jakub Fornadel Date: Wed, 20 Sep 2023 11:18:28 -0700 Subject: [PATCH 133/134] update evm --- submodules/taraxa-evm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index 712a29ca9b..1bd817c24a 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit 712a29ca9bd8737241b5dc21ce3469074dc92438 +Subproject commit 1bd817c24ada01aa29d60454b0347a074c65f5b2 From 3b6ea8c3df08f5629380eba57c149663df35c1ee Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Fri, 22 Sep 2023 12:27:08 +0200 Subject: [PATCH 134/134] chore: enable test for magnolia hf on devnet --- .../cli/include/cli/config_jsons/devnet/devnet_genesis.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json index 588fce3ef6..f6e084169f 100644 --- a/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json +++ b/libraries/cli/include/cli/config_jsons/devnet/devnet_genesis.json @@ -265,9 +265,9 @@ }, "hardforks": { "fix_redelegate_block_num": 0, - "rewards_distribution_frequency": {}, + "rewards_distribution_frequency": {"1230000" : 100}, "magnolia_hf" : { - "block_num" : 0, + "block_num" : 1230000, "jail_time": 163459 } }