Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0.4 -> main] Fix fetch block with no block log #1060

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1199,14 +1199,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& fork_db) {
auto bsp = fork_db.get_block(id);
auto bsp = fork_db.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& fork_db) {
auto bsp = fork_db.search_on_head_branch(block_num);
auto bsp = fork_db.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 @@ -459,8 +459,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 @@ -593,7 +593,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
8 changes: 4 additions & 4 deletions plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1609,14 +1609,14 @@ void producer_plugin_impl::plugin_startup() {
const auto fork_db_root = chain.fetch_block_by_number(fork_db_root_num);
if (fork_db_root) {
on_irreversible_block(fork_db_root);

if (!_is_savanna_active && irreversible_mode() && chain_plug->accept_transactions()) {
wlog("Legacy consensus active. Accepting speculative transaction execution not recommended in read-mode=irreversible");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is OK this is not in rel 1.0.

} else {
_irreversible_block_time = fc::time_point::maximum();
}

if (!_is_savanna_active && irreversible_mode() && chain_plug->accept_transactions()) {
wlog("Legacy consensus active. Accepting speculative transaction execution not recommended in read-mode=irreversible");
}

if (is_configured_producer()) {
ilog("Launching block production for ${n} producers at ${time}.", ("n", _producers.size())("time", fc::time_point::now()));

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
Loading