Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: check state root on syncing #2457

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libraries/core_libs/consensus/include/pbft/pbft_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,13 @@ class PbftManager : public std::enable_shared_from_this<PbftManager> {
*/
bool validatePbftBlock(const std::shared_ptr<PbftBlock> &pbft_block) const;

/**
* @brief Validates pbft block state root. It checks if:
* @param pbft_block PBFT block
* @return true if pbft block is valid, otherwise false
*/
bool validatePbftBlockStateRoot(const std::shared_ptr<PbftBlock> &pbft_block) const;

/**
* @brief If there are enough certify votes, push the vote PBFT block in PBFT chain
* @param pbft_block PBFT block
Expand Down
38 changes: 25 additions & 13 deletions libraries/core_libs/consensus/src/pbft/pbft_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,20 +1315,9 @@ std::shared_ptr<PbftBlock> PbftManager::identifyLeaderBlock_(PbftRound round, Pb
return empty_leader_block;
}

bool PbftManager::validatePbftBlock(const std::shared_ptr<PbftBlock> &pbft_block) const {
if (!pbft_block) {
LOG(log_er_) << "Unable to validate pbft block - no block provided";
return false;
}

// Validates pbft_block's previous block hash against pbft chain
if (!pbft_chain_->checkPbftBlockValidation(pbft_block)) {
return false;
}

auto const &pbft_block_hash = pbft_block->getBlockHash();

bool PbftManager::validatePbftBlockStateRoot(const std::shared_ptr<PbftBlock> &pbft_block) const {
auto period = pbft_block->getPeriod();
auto const &pbft_block_hash = pbft_block->getBlockHash();
{
h256 prev_state_root_hash;
if (period > final_chain_->delegation_delay()) {
Expand All @@ -1345,6 +1334,25 @@ bool PbftManager::validatePbftBlock(const std::shared_ptr<PbftBlock> &pbft_block
return false;
}
}
return true;
}

bool PbftManager::validatePbftBlock(const std::shared_ptr<PbftBlock> &pbft_block) const {
if (!pbft_block) {
LOG(log_er_) << "Unable to validate pbft block - no block provided";
return false;
}

// Validates pbft_block's previous block hash against pbft chain
if (!pbft_chain_->checkPbftBlockValidation(pbft_block)) {
return false;
}

auto const &pbft_block_hash = pbft_block->getBlockHash();

if (!validatePbftBlockStateRoot(pbft_block)) {
return false;
}

// Validates reward votes
if (!vote_mgr_->checkRewardVotes(pbft_block, false).first) {
Expand Down Expand Up @@ -1649,6 +1657,10 @@ std::optional<std::pair<PeriodData, std::vector<std::shared_ptr<Vote>>>> PbftMan
return std::nullopt;
}

if (!validatePbftBlockStateRoot(period_data.pbft_blk)) {
return std::nullopt;
}

// Check reward votes
auto reward_votes = vote_mgr_->checkRewardVotes(period_data.pbft_blk, true);
if (!reward_votes.first) {
Expand Down