Skip to content

Commit

Permalink
Add testcase with incorrect call.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg7mdp committed Sep 17, 2024
1 parent 3519806 commit 9bde458
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,7 @@ struct controller_impl {
});
}

// prerequisite: claimed_id is either id, or an ancestor of id
// returns true if block `id`, or one of its ancestors not older than claimed_id, is found in fork_db
// and `is_valid()`
// ------------------------------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,9 @@ namespace eosio::chain {
return my->validated_block_exists_impl(id, claimed_id);
}

// prerequisite: claimed_id is either id, or an ancestor of id
// returns true if block `id`, or one of its ancestors not older than claimed_id, is found in fork_db
// and `is_valid()`
// and `is_valid()`.
// ------------------------------------------------------------------------------------------------------
template<class BSP>
bool fork_database_impl<BSP>::validated_block_exists_impl(const block_id_type& id, const block_id_type& claimed_id) const {
Expand Down
5 changes: 5 additions & 0 deletions unittests/fork_db_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,17 @@ BOOST_FIXTURE_TEST_CASE(validated_block_exists, generate_forkdb_state) try {
bsp12b->set_valid(true);
bsp13b->set_valid(true);
bsp14b->set_valid(true);
bsp11a->set_valid(true);

BOOST_REQUIRE_EQUAL(true, forkdb.validated_block_exists(bsp14b->id(), bsp14b->id()));
BOOST_REQUIRE_EQUAL(true, forkdb.validated_block_exists(bsp14b->id(), bsp13b->id()));
BOOST_REQUIRE_EQUAL(true, forkdb.validated_block_exists(bsp14b->id(), bsp12b->id()));
BOOST_REQUIRE_EQUAL(true, forkdb.validated_block_exists(bsp14b->id(), bsp11b->id()));

// incorrect call as bsp13a is not an ancestor of bsp14b. As a result `claimed_id` is
// not found, so we assume it is either the root or an older block which is `valid`.
BOOST_REQUIRE_EQUAL(true, forkdb.validated_block_exists(bsp14b->id(), bsp13a->id()));

bsp14b->set_valid(false);
BOOST_REQUIRE_EQUAL(false, forkdb.validated_block_exists(bsp14b->id(), bsp14b->id()));
BOOST_REQUIRE_EQUAL(true, forkdb.validated_block_exists(bsp14b->id(), bsp13b->id()));
Expand Down

0 comments on commit 9bde458

Please sign in to comment.