Skip to content

Commit

Permalink
GH-46 Fix irreversible mode and simplify fetch_head_branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Apr 18, 2024
1 parent f5b58d8 commit 432da13
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ struct controller_impl {

bool savanna_transistion_required = false;
auto mark_branch_irreversible = [&, this](auto& forkdb) {
auto branch = savanna ? forkdb.fetch_head_branch( irreversible_block_id, new_lib_num)
auto branch = savanna ? forkdb.fetch_head_branch( fork_db_head_or_pending(forkdb)->id(), irreversible_block_id)
: forkdb.fetch_branch( fork_db_head_or_pending(forkdb)->id(), new_lib_num );
try {
auto should_process = [&](auto& bsp) {
Expand Down
14 changes: 6 additions & 8 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ namespace eosio::chain {
void remove_impl( const block_id_type& id );
branch_t fetch_branch_impl( const block_id_type& h, uint32_t trim_after_block_num ) const;
block_branch_t fetch_block_branch_impl( const block_id_type& h, uint32_t trim_after_block_num ) const;
branch_t fetch_head_branch_impl( const block_id_type& b, uint32_t trim_after_block_num ) const;
branch_t fetch_head_branch_impl( const block_id_type& h, const block_id_type& b ) const;
full_branch_t fetch_full_branch_impl(const block_id_type& h) const;
bsp_t search_on_branch_impl( const block_id_type& h, uint32_t block_num, include_root_t include_root ) const;
bsp_t search_on_head_branch_impl( uint32_t block_num, include_root_t include_root ) const;
Expand Down Expand Up @@ -441,23 +441,21 @@ namespace eosio::chain {

template <class BSP>
fork_database_t<BSP>::branch_t
fork_database_t<BSP>::fetch_head_branch(const block_id_type& b, uint32_t trim_after_block_num) const {
fork_database_t<BSP>::fetch_head_branch(const block_id_type& h, const block_id_type& b) const {
std::lock_guard g(my->mtx);
return my->fetch_head_branch_impl(b, trim_after_block_num);
return my->fetch_head_branch_impl(h, b);
}

template <class BSP>
fork_database_t<BSP>::branch_t
fork_database_impl<BSP>::fetch_head_branch_impl(const block_id_type& b, uint32_t trim_after_block_num) const {
fork_database_impl<BSP>::fetch_head_branch_impl(const block_id_type& h, const block_id_type& b) const {
branch_t result;
if (!head)
return result;
result.reserve(index.size());
bool found_branch = false;
for (auto i = index.find(head->id()); i != index.end(); i = index.find((*i)->previous())) {
for (auto i = index.find(h); i != index.end(); i = index.find((*i)->previous())) {
if ((*i)->id() == b)
found_branch = true;
if (found_branch && (*i)->block_num() <= trim_after_block_num)
if (found_branch)
result.push_back(*i);
}
return result;
Expand Down
6 changes: 4 additions & 2 deletions libraries/chain/include/eosio/chain/fork_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ namespace eosio::chain {
block_branch_t fetch_block_branch( const block_id_type& h, uint32_t trim_after_block_num = std::numeric_limits<uint32_t>::max() ) const;

/**
* Similar to fetch_branch but only returns up to head or empty if b not on head branch.
* Returns the sequence of block states resulting from trimming the branch from the
* root block (exclusive) to the block with an id of `h` (inclusive) by removing any
* block states that are after block `b`. Returns empty if `b` not found on `h` branch.
*/
branch_t fetch_head_branch( const block_id_type& b, uint32_t trim_after_block_num = std::numeric_limits<uint32_t>::max() ) const;
branch_t fetch_head_branch( const block_id_type& h, const block_id_type& b ) const;

/**
* Returns full branch of block_header_state pointers including the root.
Expand Down
4 changes: 2 additions & 2 deletions unittests/fork_db_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ BOOST_AUTO_TEST_CASE(add_remove_test) try {
BOOST_TEST(forkdb.head()->id() == root->id());
forkdb.mark_valid(bsp13a);
BOOST_TEST(forkdb.head()->id() == bsp13a->id());
branch = forkdb.fetch_head_branch(bsp11c->id());
branch = forkdb.fetch_head_branch(forkdb.head()->id(), bsp11c->id());
BOOST_TEST(branch.empty()); // bsp11c not on bsp13a branch
branch = forkdb.fetch_head_branch(bsp12a->id());
branch = forkdb.fetch_head_branch(forkdb.head()->id(), bsp12a->id());
BOOST_REQUIRE(branch.size() == 2);
BOOST_TEST(branch[0] == bsp12a);
BOOST_TEST(branch[1] == bsp11a);
Expand Down

0 comments on commit 432da13

Please sign in to comment.