Skip to content

Commit

Permalink
GH-2125 Optimize fetch_block_branch
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Feb 8, 2024
1 parent 3ffeb9d commit 472b1d4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1339,13 +1339,13 @@ struct controller_impl {
forkdb.reset( *head );
} else if( !except_ptr && !check_shutdown() && forkdb.head() ) {
auto head_block_num = head->block_num();
auto branch = forkdb.fetch_branch( forkdb.head()->id() );
auto branch = fork_db.fetch_branch_from_head();
int rev = 0;
for( auto i = branch.rbegin(); i != branch.rend(); ++i ) {
if( check_shutdown() ) break;
if( (*i)->block_num() <= head_block_num ) continue;
++rev;
replay_push_block<BSP>( (*i)->block, controller::block_status::validated );
replay_push_block<BSP>( *i, controller::block_status::validated );
}
ilog( "${n} reversible blocks replayed", ("n",rev) );
}
Expand Down
31 changes: 23 additions & 8 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace eosio::chain {
void advance_root_impl( const block_id_type& id );
void remove_impl( const block_id_type& id );
branch_type 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;
bsp search_on_branch_impl( const block_id_type& h, uint32_t block_num ) const;
void mark_valid_impl( const bsp& h );
branch_type_pair fetch_branch_from_impl( const block_id_type& first, const block_id_type& second ) const;
Expand Down Expand Up @@ -420,6 +421,25 @@ namespace eosio::chain {
return result;
}

template <class bsp>
block_branch_t
fork_database_t<bsp>::fetch_block_branch(const block_id_type& h, uint32_t trim_after_block_num) const {
std::lock_guard g(my->mtx);
return my->fetch_block_branch_impl(h, trim_after_block_num);
}

template <class bsp>
block_branch_t
fork_database_impl<bsp>::fetch_block_branch_impl(const block_id_type& h, uint32_t trim_after_block_num) const {
block_branch_t result;
for (auto s = get_block_impl(h); s; s = get_block_impl(s->previous())) {
if (s->block_num() <= trim_after_block_num)
result.push_back(s->block);
}

return result;
}

template<class bsp>
bsp fork_database_t<bsp>::search_on_branch( const block_id_type& h, uint32_t block_num ) const {
std::lock_guard g( my->mtx );
Expand Down Expand Up @@ -648,15 +668,10 @@ namespace eosio::chain {
});
}

std::vector<signed_block_ptr> fork_database::fetch_branch_from_head() {
std::vector<signed_block_ptr> r;
apply<void>([&](auto& forkdb) {
auto branch = forkdb.fetch_branch(forkdb.head()->id());
r.reserve(branch.size());
for (auto& b : branch)
r.push_back(b->block);
block_branch_t fork_database::fetch_branch_from_head() const {
return apply<block_branch_t>([&](auto& forkdb) {
return forkdb.fetch_block_branch(forkdb.head()->id());
});
return r;
}

// do class instantiations
Expand Down
5 changes: 4 additions & 1 deletion libraries/chain/include/eosio/chain/fork_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace eosio::chain {
template<class bsp>
struct fork_database_impl;

using block_branch_t = deque<signed_block_ptr>;

/**
* @class fork_database_t
* @brief manages light-weight state for all potential unconfirmed forks
Expand Down Expand Up @@ -85,6 +87,7 @@ namespace eosio::chain {
* A block with an id of `h` must exist in the fork database otherwise this method will throw an exception.
*/
branch_type fetch_branch( const block_id_type& h, uint32_t trim_after_block_num = std::numeric_limits<uint32_t>::max() ) const;
block_branch_t fetch_block_branch( const block_id_type& h, uint32_t trim_after_block_num = std::numeric_limits<uint32_t>::max() ) const;


/**
Expand Down Expand Up @@ -131,7 +134,7 @@ namespace eosio::chain {
void switch_from_legacy();

// see fork_database_t::fetch_branch(forkdb->head()->id())
std::vector<signed_block_ptr> fetch_branch_from_head();
block_branch_t fetch_branch_from_head() const;

template <class R, class F>
R apply(const F& f) {
Expand Down
2 changes: 1 addition & 1 deletion programs/leap-util/actions/blocklog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ int blocklog_actions::read_log() {
opt->first_block = block_logger.first_block_num();
}

std::vector<signed_block_ptr> fork_db_branch;
block_branch_t fork_db_branch;

if(std::filesystem::exists(std::filesystem::path(opt->blocks_dir) / config::reversible_blocks_dir_name / config::forkdb_filename)) {
ilog("opening fork_db");
Expand Down

0 comments on commit 472b1d4

Please sign in to comment.