Skip to content

Commit d075bab

Browse files
authored
Merge pull request #995 from AntelopeIO/clean_code_1_0
[1.0.3] Improve validation
2 parents 5e7249a + fa6d4ad commit d075bab

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

libraries/chain/block_header_state.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ const producer_authority& block_header_state::get_scheduled_producer(block_times
107107

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

libraries/chain/block_state.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,13 @@ digest_type block_state::get_validation_mroot(block_num_type target_block_num) c
325325
}
326326

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

332-
return valid->validation_mroots[target_block_num - core.last_final_block_num()];
334+
return valid->validation_mroots[target_block_num - low];
333335
}
334336

335337
digest_type block_state::get_finality_mroot_claim(const qc_claim_t& qc_claim) const {

libraries/chain/controller.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3942,11 +3942,17 @@ struct controller_impl {
39423942

39433943
// validate QC claim against previous block QC info
39443944

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

3951+
// new claimed QC block number cannot be greater than previous block number
3952+
EOS_ASSERT( new_qc_claim.block_num <= prev.block_num(), invalid_qc_claim,
3953+
"Block #${b} claims a block_num (${n1}) that is greater than the previous block number (${n2})",
3954+
("n1", new_qc_claim.block_num)("n2", prev.block_num())("b", block_num) );
3955+
39503956
if( new_qc_claim.block_num == prev_qc_claim.block_num ) {
39513957
if( new_qc_claim.is_strong_qc == prev_qc_claim.is_strong_qc ) {
39523958
// QC block extension is redundant

unittests/replay_block_invariants_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ BOOST_FIXTURE_TEST_CASE(irrelevant_qc, test_fixture) try {
189189
eosio::testing::tester replay_chain(config, *genesis); // start replay
190190
// An exception should have thrown
191191
BOOST_FAIL("replay should have failed with block_validate_exception exception");
192-
} catch (block_validate_exception& e) {
193-
BOOST_REQUIRE(e.to_detail_string().find("Mismatch between qc.block_num") != std::string::npos);
192+
} catch (invalid_qc_claim& e) {
193+
BOOST_REQUIRE(e.to_detail_string().find("that is greater than the previous block number") != std::string::npos);
194194
} catch (...) {
195195
BOOST_FAIL("replay failed with non block_validate_exception exception");
196196
}

0 commit comments

Comments
 (0)