Skip to content

Commit

Permalink
Merge pull request #1059 from AntelopeIO/GH-1052-fetch_block_no_block…
Browse files Browse the repository at this point in the history
…_log-1.0

[1.0.4] Fix fetch block with no block log
  • Loading branch information
heifner authored Dec 10, 2024
2 parents 037cd18 + ee1c477 commit fbe8949
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
4 changes: 2 additions & 2 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,14 +1149,14 @@ struct controller_impl {

signed_block_ptr fork_db_fetch_block_by_id( const block_id_type& id ) const {
return fork_db.apply<signed_block_ptr>([&](const auto& forkdb) {
auto bsp = forkdb.get_block(id);
auto bsp = forkdb.get_block(id, include_root_t::yes);
return bsp ? bsp->block : signed_block_ptr{};
});
}

signed_block_ptr fork_db_fetch_block_on_best_branch_by_num(uint32_t block_num) const {
return fork_db.apply<signed_block_ptr>([&](const auto& forkdb) {
auto bsp = forkdb.search_on_head_branch(block_num);
auto bsp = forkdb.search_on_head_branch(block_num, include_root_t::yes);
if (bsp) return bsp->block;
return signed_block_ptr{};
});
Expand Down
6 changes: 3 additions & 3 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ namespace eosio::chain {
BSP fork_database_impl<BSP>::search_on_branch_impl( const block_id_type& h, uint32_t block_num, include_root_t include_root ) const {
if (!root)
return {};
if( include_root == include_root_t::yes && root->id() == h && root->block_num() == block_num ) {
return root;
if( include_root == include_root_t::yes && root->block_num() == block_num ) {
return root; // root is root of every branch, no need to check h
}
if (block_num <= root->block_num())
return {};
Expand Down Expand Up @@ -582,7 +582,7 @@ namespace eosio::chain {
template<class BSP>
BSP fork_database_impl<BSP>::get_block_impl(const block_id_type& id,
include_root_t include_root /* = include_root_t::no */) const {
if( include_root == include_root_t::yes && root->id() == id ) {
if( include_root == include_root_t::yes && root && root->id() == id ) {
return root;
}
auto itr = index.find( id );
Expand Down
51 changes: 51 additions & 0 deletions unittests/database_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,57 @@ BOOST_AUTO_TEST_SUITE(database_tests)
// Check the latest head block match
BOOST_TEST(test.fetch_block_by_number(test.head().block_num())->calculate_id() ==
test.head().id());

// Verify LIB can be found
const auto lib_num = test.last_irreversible_block_num();
auto lib = test.fetch_block_by_number(lib_num);
BOOST_REQUIRE(lib);
auto lib_id = lib->calculate_id();
BOOST_TEST(lib_num == lib->block_num());
lib = test.fetch_block_by_id(lib_id);
BOOST_REQUIRE(lib);
BOOST_TEST(lib->calculate_id() == lib_id);

} FC_LOG_AND_RETHROW()
}

// Test the block fetching methods on database, fetch_bock_by_id, and fetch_block_by_number
BOOST_AUTO_TEST_CASE_TEMPLATE( get_blocks_no_block_log, T, validating_testers ) {
try {
fc::temp_directory tempdir;

constexpr bool use_genesis = true;
T test(
tempdir,
[&](controller::config& cfg) {
cfg.blog = eosio::chain::empty_blocklog_config{};
},
use_genesis
);

// Ensure that future block doesn't exist
const auto nonexisting_future_block_num = test.head().block_num() + 1;
BOOST_TEST(test.fetch_block_by_number(nonexisting_future_block_num) == nullptr);
BOOST_TEST(test.fetch_block_by_id(sha256::hash("xx")) == nullptr);

test.produce_block();

// Previous nonexisting future block should exist now
BOOST_CHECK_NO_THROW(test.fetch_block_by_number(nonexisting_future_block_num));
// Check the latest head block match
BOOST_TEST(test.fetch_block_by_number(test.head().block_num())->calculate_id() == test.head().id());
BOOST_TEST(test.fetch_block_by_id(test.head().id())->calculate_id() == test.head().id());

// Verify LIB can be found
const auto lib_num = test.last_irreversible_block_num();
auto lib = test.fetch_block_by_number(lib_num);
BOOST_REQUIRE(lib);
auto lib_id = lib->calculate_id();
BOOST_TEST(lib_num == lib->block_num());
lib = test.fetch_block_by_id(lib_id);
BOOST_REQUIRE(lib);
BOOST_TEST(lib->calculate_id() == lib_id);

} FC_LOG_AND_RETHROW()
}

Expand Down

0 comments on commit fbe8949

Please sign in to comment.