From 432da13b980ed9a20225e674ed7faaa150f50d81 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Thu, 18 Apr 2024 13:18:57 -0500 Subject: [PATCH] GH-46 Fix irreversible mode and simplify fetch_head_branch. --- libraries/chain/controller.cpp | 2 +- libraries/chain/fork_database.cpp | 14 ++++++-------- .../chain/include/eosio/chain/fork_database.hpp | 6 ++++-- unittests/fork_db_tests.cpp | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/libraries/chain/controller.cpp b/libraries/chain/controller.cpp index 801269bad4..fc5f36c00d 100644 --- a/libraries/chain/controller.cpp +++ b/libraries/chain/controller.cpp @@ -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) { diff --git a/libraries/chain/fork_database.cpp b/libraries/chain/fork_database.cpp index 259c05b893..689e3301d2 100644 --- a/libraries/chain/fork_database.cpp +++ b/libraries/chain/fork_database.cpp @@ -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; @@ -441,23 +441,21 @@ namespace eosio::chain { template fork_database_t::branch_t - fork_database_t::fetch_head_branch(const block_id_type& b, uint32_t trim_after_block_num) const { + fork_database_t::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 fork_database_t::branch_t - fork_database_impl::fetch_head_branch_impl(const block_id_type& b, uint32_t trim_after_block_num) const { + fork_database_impl::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; diff --git a/libraries/chain/include/eosio/chain/fork_database.hpp b/libraries/chain/include/eosio/chain/fork_database.hpp index d6273e6e9b..dba160ff8e 100644 --- a/libraries/chain/include/eosio/chain/fork_database.hpp +++ b/libraries/chain/include/eosio/chain/fork_database.hpp @@ -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::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::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. diff --git a/unittests/fork_db_tests.cpp b/unittests/fork_db_tests.cpp index 38d0709881..99069821db 100644 --- a/unittests/fork_db_tests.cpp +++ b/unittests/fork_db_tests.cpp @@ -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);