Skip to content

Commit

Permalink
GH-1039 Interrupt apply block when a fork switch is received.
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Nov 22, 2024
1 parent 3ff642b commit 773542c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
12 changes: 6 additions & 6 deletions plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3713,7 +3713,7 @@ namespace eosio {

std::optional<block_handle> obh;
bool exception = false;
bool best_head = false;
fork_db_add_t fork_db_add_result = fork_db_add_t::failure;
bool unlinkable = false;
sync_manager::closing_mode close_mode = sync_manager::closing_mode::immediately;
try {
Expand All @@ -3723,7 +3723,7 @@ namespace eosio {
}
// this will return empty optional<block_handle> if block is not linkable
controller::accepted_block_result abh = cc.accept_block( id, ptr );
best_head = abh.add_result == fork_db_add_t::appended_to_head || abh.add_result == fork_db_add_t::fork_switch;
fork_db_add_result = abh.add_result;
obh = std::move(abh.block);
unlinkable = !obh;
close_mode = sync_manager::closing_mode::handshake;
Expand Down Expand Up @@ -3757,8 +3757,8 @@ namespace eosio {
uint32_t block_num = obh->block_num();
proper_svnn_block_seen = obh->header().is_proper_svnn_block();

fc_dlog( logger, "validated block header, best_head ${bt}, broadcasting immediately, connection - ${cid}, blk num = ${num}, id = ${id}",
("bt", best_head)("cid", cid)("num", block_num)("id", obh->id()) );
fc_dlog( logger, "validated block header, forkdb add ${bt}, broadcasting immediately, connection - ${cid}, blk num = ${num}, id = ${id}",
("bt", fork_db_add_result)("cid", cid)("num", block_num)("id", obh->id()) );
my_impl->dispatcher.add_peer_block( obh->id(), cid ); // no need to send back to sender
my_impl->dispatcher.bcast_block( obh->block(), obh->id() );
c->block_status_monitor_.accepted();
Expand All @@ -3771,7 +3771,7 @@ namespace eosio {
});
}

if (best_head) {
if (fork_db_add_result == fork_db_add_t::appended_to_head || fork_db_add_result == fork_db_add_t::fork_switch) {
++c->unique_blocks_rcvd_count;
fc_dlog(logger, "posting incoming_block to app thread, block ${n}", ("n", ptr->block_num()));

Expand All @@ -3792,7 +3792,7 @@ namespace eosio {
});

// ready to process immediately, so signal producer to interrupt start_block
my_impl->producer_plug->received_block(block_num);
my_impl->producer_plug->received_block(block_num, fork_db_add_result);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class producer_plugin : public appbase::plugin<producer_plugin> {
void log_failed_transaction(const transaction_id_type& trx_id, const chain::packed_transaction_ptr& packed_trx_ptr, const char* reason) const;

// thread-safe, called when a new block is received
void received_block(uint32_t block_num);
void received_block(uint32_t block_num, chain::fork_db_add_t fork_db_add_result);

const std::set<account_name>& producer_accounts() const;

Expand Down
7 changes: 6 additions & 1 deletion plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <eosio/chain/subjective_billing.hpp>
#include <eosio/chain/thread_utils.hpp>
#include <eosio/chain/unapplied_transaction_queue.hpp>
#include <eosio/chain/fork_database.hpp>
#include <eosio/resource_monitor_plugin/resource_monitor_plugin.hpp>

#include <fc/io/json.hpp>
Expand Down Expand Up @@ -2947,8 +2948,12 @@ void producer_plugin_impl::produce_block() {
_time_tracker.clear();
}

void producer_plugin::received_block(uint32_t block_num) {
void producer_plugin::received_block(uint32_t block_num, chain::fork_db_add_t fork_db_add_result) {
my->_received_block = block_num;
if (fork_db_add_result == fork_db_add_t::fork_switch) {
fc_ilog(_log, "new best fork received");
my->chain_plug->chain().interrupt_apply_block_transaction();
}
}

void producer_plugin::log_failed_transaction(const transaction_id_type& trx_id,
Expand Down

0 comments on commit 773542c

Please sign in to comment.