Skip to content

Commit

Permalink
refector: move process rewards stats functionality to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
kstdl committed May 5, 2023
1 parent 2db3404 commit cedd967
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 40 deletions.
6 changes: 6 additions & 0 deletions libraries/core_libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +32,7 @@ set(SOURCES
${STORAGE_SOURCES}
${NODE_SOURCES}
${GRAPHQL_GENERATED_SOURCES}
${REWARDS_SOURCES}
)

add_library(core_libs ${SOURCES} ${HEADERS})
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <functional>

#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 {
Expand Down Expand Up @@ -43,7 +43,7 @@ class StateAPI {
StateDescriptor get_last_committed_state_descriptor() const;
const StateTransitionResult& transition_state(const EVMBlock& block,
const util::RangeView<EVMTransaction>& transactions,
const std::vector<RewardsStats>& rewards_stats = {});
const std::vector<rewards::BlockStats>& rewards_stats = {});
void transition_state_commit();
void create_snapshot(PbftPeriod period);
void prune(const std::vector<dev::h256>& state_root_to_keep, EthBlockNumber blk_num);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,29 @@
#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
*
* @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);
/**
Expand Down Expand Up @@ -78,7 +75,8 @@ class RewardsStats {
// Transactions validators: tx hash -> validator that included it as first in his block
std::unordered_map<trx_hash_t, addr_t> 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<addr_t> txs_validators_;

// Txs stats: validator -> ValidatorStats
Expand All @@ -94,4 +92,4 @@ class RewardsStats {
uint64_t max_votes_weight_{0};
};

} // namespace taraxa
} // namespace taraxa::rewards
17 changes: 17 additions & 0 deletions libraries/core_libs/consensus/include/rewards/rewards_stats.hpp
Original file line number Diff line number Diff line change
@@ -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<uint64_t(EthBlockNumber)>&& dpos_eligible_total_vote_count);

std::vector<BlockStats> getStats(const PeriodData& current_blk);

private:
BlockStats getBlockStats(const PeriodData& current_blk);

const uint32_t kCommitteeSize;
const std::function<uint64_t(EthBlockNumber)> dpos_eligible_total_vote_count_;
};
} // namespace taraxa::rewards
21 changes: 5 additions & 16 deletions libraries/core_libs/consensus/src/final_chain/final_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#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 {
Expand All @@ -20,6 +20,8 @@ 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
boost::asio::thread_pool executor_thread_{1};
Expand Down Expand Up @@ -59,6 +61,7 @@ class FinalChainImpl final : public FinalChain {
kLightNode(config.is_light_node),
kLightNodeHistory(config.light_node_history),
kMaxLevelsPerPeriod(config.max_levels_per_period),
rewards_(kCommitteeSize, [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); }),
Expand Down Expand Up @@ -144,26 +147,12 @@ class FinalChainImpl final : public FinalChain {

EthBlockNumber delegation_delay() const override { return delegation_delay_; }

std::vector<RewardsStats> prepare_rewards_stats_(const PeriodData& blk) {
std::vector<RewardsStats> 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<const FinalizationResult> finalize_(PeriodData&& new_blk,
std::vector<h256>&& finalized_dag_blk_hashes,
std::shared_ptr<DagBlock>&& 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ StateDescriptor StateAPI::get_last_committed_state_descriptor() const {

const StateTransitionResult& StateAPI::transition_state(const EVMBlock& block,
const util::RangeView<EVMTransaction>& transactions,
const std::vector<RewardsStats>& rewards_stats) {
const std::vector<rewards::BlockStats>& rewards_stats) {
result_buf_transition_state_.execution_results.clear();
rlp_enc_transition_state_.clear();
c_method_args_rlp<StateTransitionResult, from_rlp, taraxa_evm_state_api_transition_state>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include "final_chain/rewards_stats.hpp"
#include "rewards/block_stats.hpp"

#include <cstdint>

#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<uint64_t>(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
Expand All @@ -26,7 +26,7 @@ bool RewardsStats::addTransaction(const trx_hash_t& tx_hash, const addr_t& valid
return true;
}

std::optional<addr_t> RewardsStats::getTransactionValidator(const trx_hash_t& tx_hash) {
std::optional<addr_t> 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 {};
Expand All @@ -35,7 +35,7 @@ std::optional<addr_t> RewardsStats::getTransactionValidator(const trx_hash_t& tx
return {found_tx->second};
}

bool RewardsStats::addVote(const std::shared_ptr<Vote>& vote) {
bool BlockStats::addVote(const std::shared_ptr<Vote>& vote) {
// Set valid cert vote to validator
auto& validator_stats = validators_stats_[vote->getVoterAddr()];
assert(validator_stats.vote_weight_ == 0);
Expand All @@ -60,7 +60,7 @@ std::set<trx_hash_t> 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);
Expand Down Expand Up @@ -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
} // namespace taraxa::rewards
41 changes: 41 additions & 0 deletions libraries/core_libs/consensus/src/rewards/rewards_stats.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "rewards/rewards_stats.hpp"

#include "storage/storage.hpp"

namespace taraxa::rewards {
Stats::Stats(uint32_t committee_size, std::function<uint64_t(EthBlockNumber)>&& dpos_eligible_total_vote_count)
: kCommitteeSize(committee_size), dpos_eligible_total_vote_count_(dpos_eligible_total_vote_count) {}

// std::vector<RewardsStats> 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<RewardsStats> 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<BlockStats> Stats::getStats(const PeriodData& current_blk) { return {getBlockStats(current_blk)}; }
} // namespace taraxa::rewards

0 comments on commit cedd967

Please sign in to comment.