Skip to content

Commit

Permalink
GH-2125 Potentially switch forks before beginning to produce a block
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Feb 27, 2024
1 parent 9e0a226 commit 17274e2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
22 changes: 21 additions & 1 deletion libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2782,7 +2782,6 @@ struct controller_impl {
if( s == controller::block_status::incomplete ) {
forkdb.add( bsp, true, false );
emit( accepted_block_header, std::tie(bsp->block, bsp->id()) );
EOS_ASSERT( bsp == forkdb.head(), fork_database_exception, "committed block did not become the new head in fork database");
} else if (s != controller::block_status::irreversible) {
forkdb.mark_valid( bsp );
}
Expand Down Expand Up @@ -3512,6 +3511,21 @@ struct controller_impl {
} FC_LOG_AND_RETHROW( )
}

void maybe_switch_forks(const forked_callback_t& cb, const trx_meta_cache_lookup& trx_lookup) {
auto maybe_switch = [&](auto& forkdb) {
if (read_mode != db_read_mode::IRREVERSIBLE) {
auto fork_head = forkdb.head();
if (forkdb.chain_head->id() != fork_head->id()) {
controller::block_report br;
maybe_switch_forks(br, fork_head, fork_head->is_valid() ? controller::block_status::validated : controller::block_status::complete,
cb, trx_lookup);
}
}
};

fork_db.apply<void>(maybe_switch);
}

template<class BSP>
void maybe_switch_forks( controller::block_report& br, const BSP& new_head, controller::block_status s,
const forked_callback_t& forked_cb, const trx_meta_cache_lookup& trx_lookup )
Expand Down Expand Up @@ -4277,6 +4291,12 @@ void controller::commit_block() {
my->commit_block(block_status::incomplete);
}

void controller::maybe_switch_forks(const forked_callback_t& cb, const trx_meta_cache_lookup& trx_lookup) {
validate_db_available_size();
my->maybe_switch_forks(cb, trx_lookup);
}


deque<transaction_metadata_ptr> controller::abort_block() {
return my->abort_block();
}
Expand Down
1 change: 1 addition & 0 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ namespace eosio::chain {
void assemble_and_complete_block( block_report& br, const signer_callback_type& signer_callback );
void sign_block( const signer_callback_type& signer_callback );
void commit_block();
void maybe_switch_forks(const forked_callback_t& cb, const trx_meta_cache_lookup& trx_lookup);

// thread-safe
std::future<block_handle> create_block_handle_future( const block_id_type& id, const signed_block_ptr& b );
Expand Down
4 changes: 4 additions & 0 deletions plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,10 @@ producer_plugin_impl::start_block_result producer_plugin_impl::start_block() {
if (!chain_plug->accept_transactions())
return start_block_result::waiting_for_block;

chain.maybe_switch_forks([this](const transaction_metadata_ptr& trx) { _unapplied_transactions.add_forked(trx); },
[this](const transaction_id_type& id) { return _unapplied_transactions.get_trx(id); });


uint32_t head_block_num = chain.head_block_num();

if (chain.get_terminate_at_block() > 0 && chain.get_terminate_at_block() <= head_block_num) {
Expand Down

0 comments on commit 17274e2

Please sign in to comment.