Skip to content

Commit

Permalink
Merge pull request #995 from AntelopeIO/clean_code_1_0
Browse files Browse the repository at this point in the history
[1.0.3] Improve validation
  • Loading branch information
linh2931 authored Oct 31, 2024
2 parents 5e7249a + fa6d4ad commit d075bab
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
1 change: 0 additions & 1 deletion libraries/chain/block_header_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ const producer_authority& block_header_state::get_scheduled_producer(block_times

// returns producer using the proposer policy calculated by time `t`
const producer_authority& block_header_state::get_producer_for_block_at(block_timestamp_type next_block_timestamp) const {
assert(next_block_timestamp > timestamp()); // next block timestamp must be greater than current timestamp
return detail::get_scheduled_producer(get_active_proposer_policy_for_block_at(next_block_timestamp)->proposer_schedule.producers, next_block_timestamp);
}

Expand Down
10 changes: 6 additions & 4 deletions libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,13 @@ digest_type block_state::get_validation_mroot(block_num_type target_block_num) c
}

assert(valid->validation_mroots.size() > 0);
assert(core.last_final_block_num() <= target_block_num &&
target_block_num < core.last_final_block_num() + valid->validation_mroots.size());
assert(target_block_num - core.last_final_block_num() < valid->validation_mroots.size());
auto low = core.last_final_block_num();
auto high = low + valid->validation_mroots.size();
EOS_ASSERT(low <= target_block_num && target_block_num < high, block_validate_exception,
"target_block_num ${b} is outside of range of ${low} and ${high}",
("b", target_block_num)("low", low)("high", high));

return valid->validation_mroots[target_block_num - core.last_final_block_num()];
return valid->validation_mroots[target_block_num - low];
}

digest_type block_state::get_finality_mroot_claim(const qc_claim_t& qc_claim) const {
Expand Down
8 changes: 7 additions & 1 deletion libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3942,11 +3942,17 @@ struct controller_impl {

// validate QC claim against previous block QC info

// new claimed QC block number cannot be smaller than previous block's
// new claimed QC block number cannot be less than previous block's
// claimed QC block number
EOS_ASSERT( new_qc_claim.block_num >= prev_qc_claim.block_num, invalid_qc_claim,
"Block #${b} claims a block_num (${n1}) less than the previous block's (${n2})",
("n1", new_qc_claim.block_num)("n2", prev_qc_claim.block_num)("b", block_num) );

// new claimed QC block number cannot be greater than previous block number
EOS_ASSERT( new_qc_claim.block_num <= prev.block_num(), invalid_qc_claim,
"Block #${b} claims a block_num (${n1}) that is greater than the previous block number (${n2})",
("n1", new_qc_claim.block_num)("n2", prev.block_num())("b", block_num) );

if( new_qc_claim.block_num == prev_qc_claim.block_num ) {
if( new_qc_claim.is_strong_qc == prev_qc_claim.is_strong_qc ) {
// QC block extension is redundant
Expand Down
4 changes: 2 additions & 2 deletions unittests/replay_block_invariants_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ BOOST_FIXTURE_TEST_CASE(irrelevant_qc, test_fixture) try {
eosio::testing::tester replay_chain(config, *genesis); // start replay
// An exception should have thrown
BOOST_FAIL("replay should have failed with block_validate_exception exception");
} catch (block_validate_exception& e) {
BOOST_REQUIRE(e.to_detail_string().find("Mismatch between qc.block_num") != std::string::npos);
} catch (invalid_qc_claim& e) {
BOOST_REQUIRE(e.to_detail_string().find("that is greater than the previous block number") != std::string::npos);
} catch (...) {
BOOST_FAIL("replay failed with non block_validate_exception exception");
}
Expand Down

0 comments on commit d075bab

Please sign in to comment.