Skip to content

Commit

Permalink
Merge pull request #1009 from AntelopeIO/GH-1003-lib
Browse files Browse the repository at this point in the history
[1.0.4] Advance fork db root when pending lib greater than head
  • Loading branch information
heifner authored Nov 5, 2024
2 parents 64cb7a3 + 4ed5b76 commit da6b82f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 7 additions & 3 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1496,10 +1496,14 @@ struct controller_impl {
auto mark_branch_irreversible = [&, this](auto& forkdb) {
assert(!irreversible_mode() || forkdb.head());
const auto& head_id = irreversible_mode() ? forkdb.head()->id() : chain_head.id();
const auto head_num = block_header::num_from_id(head_id);
// verifies lib is on head branch, otherwise returns an empty branch
// The new lib needs to be on the head branch because the forkdb.advance_root() below could purge blocks that
// would be needed to be re-applied on a fork switch from the exiting chain_head.
auto branch = forkdb.fetch_branch(head_id, new_lib_id);
// The new lib needs to be on the head branch because the forkdb.advance_root() below could purge blocks that
// would be needed to be re-applied on a fork switch from the exiting chain_head.
// Pending LIB can be greater than chain head, for example when syncing, in that case fetch branch from the
// pending LIB. If the pending LIB not found on the head branch then fetch_branch returns an empty branch.
// Otherwise fetch_branch will return from chain_head to root iff chain_head on pending LIB branch.
auto branch = new_lib_num <= head_num ? forkdb.fetch_branch(head_id, new_lib_id) : forkdb.fetch_branch(new_lib_id, head_id);
try {
auto should_process = [&](auto& bsp) {
// Only make irreversible blocks that have been validated. Blocks in the fork database may not be on our current best head
Expand Down
8 changes: 8 additions & 0 deletions unittests/fork_db_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ BOOST_FIXTURE_TEST_CASE(add_remove_test, generate_forkdb_state) try {
BOOST_REQUIRE(branch.size() == 2);
BOOST_TEST(branch[0] == bsp12a);
BOOST_TEST(branch[1] == bsp11a);

// test fetch branch when lib is greater than head
branch = forkdb.fetch_branch(bsp13b->id(), bsp12a->id());
BOOST_TEST(branch.empty());
branch = forkdb.fetch_branch(bsp13b->id(), bsp12b->id());
BOOST_REQUIRE(branch.size() == 2);
BOOST_TEST(branch[0] == bsp12b);
BOOST_TEST(branch[1] == bsp11b);
} FC_LOG_AND_RETHROW();


Expand Down

0 comments on commit da6b82f

Please sign in to comment.