Skip to content

Commit

Permalink
Merge pull request #2590 from Taraxa-project/rew_stats_fix
Browse files Browse the repository at this point in the history
fix: reward stats saving
  • Loading branch information
mfrankovi authored Oct 18, 2023
2 parents c28549c + 82d73e3 commit baeee05
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@
"hardforks": {
"fix_redelegate_block_num": 0,
"rewards_distribution_frequency": {
"10000": 100
"1000": 100
},
"magnolia_hf": {
"block_num": 10000,
"block_num": 1000,
"jail_time": 1000
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ class Stats {
const HardforksConfig kHardforks;
std::shared_ptr<DB> db_;
const std::function<uint64_t(EthBlockNumber)> dpos_eligible_total_vote_count_;
std::vector<BlockStats> blocks_stats_;
std::unordered_map<PbftPeriod, BlockStats> blocks_stats_;
};
} // namespace taraxa::rewards
14 changes: 9 additions & 5 deletions libraries/core_libs/consensus/src/rewards/rewards_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ Stats::Stats(uint32_t committee_size, const HardforksConfig& hardforks, std::sha
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<BlockStats>(dev::RLP(i->value().ToString())));
PbftPeriod period;
memcpy(&period, i->key().data(), sizeof(PbftPeriod));
blocks_stats_[period] = util::rlp_dec<BlockStats>(dev::RLP(i->value().ToString()));
}
}

void Stats::saveBlockStats(uint64_t period, const BlockStats& stats, DbStorage::Batch& write_batch) {
dev::RLPStream encoding;
stats.rlp(encoding);

db_->insert(write_batch, DB::Columns::block_rewards_stats, period, encoding.out());
}

Expand Down Expand Up @@ -57,6 +58,7 @@ BlockStats Stats::getBlockStats(const PeriodData& blk, const std::vector<gas_t>&

std::vector<BlockStats> Stats::processStats(const PeriodData& current_blk, const std::vector<gas_t>& trxs_gas_used,
DbStorage::Batch& write_batch) {
std::vector<BlockStats> res;
const auto current_period = current_blk.pbft_blk->getPeriod();
const auto frequency = getCurrentDistributionFrequency(current_period);
auto block_stats = getBlockStats(current_blk, trxs_gas_used);
Expand All @@ -65,15 +67,17 @@ std::vector<BlockStats> Stats::processStats(const PeriodData& current_blk, const
return {block_stats};
}

blocks_stats_.push_back(std::move(block_stats));
blocks_stats_.emplace(current_period, 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
saveBlockStats(current_period, *blocks_stats_.rbegin(), write_batch);
saveBlockStats(current_period, blocks_stats_[current_period], write_batch);
return {};
}

std::vector<BlockStats> res(std::move(blocks_stats_));
res.reserve(blocks_stats_.size());
std::transform(blocks_stats_.begin(), blocks_stats_.end(), std::back_inserter(res),
[](auto& t) { return std::move(t.second); });
clear();
return res;
}
Expand Down
16 changes: 10 additions & 6 deletions tests/rewards_stats_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TestableRewardsStats : public rewards::Stats {
public:
TestableRewardsStats(const HardforksConfig::RewardsDistributionMap& rdm, std::shared_ptr<DB> db)
: rewards::Stats(100, HardforksConfig{0, {}, rdm, MagnoliaHardfork{0, 0}}, db, [](auto) { return 100; }) {}
std::vector<rewards::BlockStats> getStats() { return blocks_stats_; }
auto getStats() { return blocks_stats_; }
};

class TestableBlockStats : public rewards::BlockStats {
Expand Down Expand Up @@ -75,9 +75,9 @@ TEST_F(RewardsStatsTest, statsSaving) {
auto stats = rewards_stats.getStats();
ASSERT_EQ(rewards_stats.getStats().size(), block_authors.size());

for (size_t i = 0; i < stats.size(); ++i) {
for (size_t i = 1; i <= stats.size(); ++i) {
auto stats_with_get = reinterpret_cast<TestableBlockStats*>(&stats[i]);
ASSERT_EQ(stats_with_get->getAuthor(), block_authors[i]);
ASSERT_EQ(stats_with_get->getAuthor(), block_authors[i - 1]);
}
}
}
Expand Down Expand Up @@ -142,9 +142,13 @@ TEST_F(RewardsStatsTest, statsProcessing) {
auto stats = rewards_stats.processStats(block, {}, batch);
ASSERT_EQ(stats.size(), block_authors.size());

for (size_t i = 0; i < stats.size(); ++i) {
auto stats_with_get = reinterpret_cast<TestableBlockStats*>(&stats[i]);
ASSERT_EQ(stats_with_get->getAuthor(), block_authors[i]);
for (auto& block_author : block_authors) {
bool found = false;
for (size_t i = 0; i < stats.size(); ++i) {
auto stats_with_get = reinterpret_cast<TestableBlockStats*>(&stats[i]);
if (stats_with_get->getAuthor() == block_author) found = true;
}
assert(found);
}
ASSERT_TRUE(rewards_stats.getStats().empty());
}
Expand Down

0 comments on commit baeee05

Please sign in to comment.