Skip to content

Commit

Permalink
GH-2125 Simplify search_on_head_branch_impl and add new test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Mar 1, 2024
1 parent e3059bf commit 2a2a5da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
7 changes: 1 addition & 6 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,7 @@ namespace eosio::chain {

template<class BSP>
BSP fork_database_impl<BSP>::search_on_head_branch_impl( uint32_t block_num ) const {
for (auto i = index.find(head->id()); i != index.end(); i = index.find((*i)->previous())) {
if ((*i)->block_num() == block_num)
return *i;
}

return {};
return search_on_branch_impl(head->id(), block_num);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions unittests/fork_db_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ BOOST_AUTO_TEST_CASE(add_remove_test) try {
forkdb.add(bsp13b, mark_valid_t::no, ignore_duplicate_t::no); // will throw if already exists
forkdb.add(bsp14b, mark_valid_t::no, ignore_duplicate_t::no); // will throw if already exists

// test search
BOOST_TEST(forkdb.search_on_branch( bsp13bb->id(), 11) == bsp11b);
BOOST_TEST(forkdb.search_on_branch( bsp13bb->id(), 9) == block_state_ptr{});

// test fetch branch
auto branch = forkdb.fetch_branch( bsp13b->id(), 12);
BOOST_REQUIRE(branch.size() == 2);
BOOST_TEST(branch[0] == bsp12b);
BOOST_TEST(branch[1] == bsp11b);
branch = forkdb.fetch_branch( bsp13bbb->id(), 13);
BOOST_REQUIRE(branch.size() == 3);
BOOST_TEST(branch[0] == bsp13bbb);
BOOST_TEST(branch[1] == bsp12bb);
BOOST_TEST(branch[2] == bsp11b);

} FC_LOG_AND_RETHROW();

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 2a2a5da

Please sign in to comment.