From e78e5193062809067a21d79eb7c33fae3be78075 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Thu, 20 Apr 2023 16:03:59 +0200 Subject: [PATCH] chore: check state root on syncing --- .../consensus/include/pbft/pbft_manager.hpp | 7 ++++ .../consensus/src/pbft/pbft_manager.cpp | 38 ++++++++++++------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp index 0685c1ce6c..c73c155118 100644 --- a/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp +++ b/libraries/core_libs/consensus/include/pbft/pbft_manager.hpp @@ -454,6 +454,13 @@ class PbftManager : public std::enable_shared_from_this { */ bool validatePbftBlock(const std::shared_ptr &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 &pbft_block) const; + /** * @brief If there are enough certify votes, push the vote PBFT block in PBFT chain * @param pbft_block PBFT block diff --git a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp index b40c09abe6..6bd9270dc3 100644 --- a/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp +++ b/libraries/core_libs/consensus/src/pbft/pbft_manager.cpp @@ -1315,20 +1315,9 @@ std::shared_ptr PbftManager::identifyLeaderBlock_(PbftRound round, Pb return empty_leader_block; } -bool PbftManager::validatePbftBlock(const std::shared_ptr &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 &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()) { @@ -1345,6 +1334,25 @@ bool PbftManager::validatePbftBlock(const std::shared_ptr &pbft_block return false; } } + return true; +} + +bool PbftManager::validatePbftBlock(const std::shared_ptr &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) { @@ -1649,6 +1657,10 @@ std::optional>>> 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) {