Skip to content

Commit

Permalink
GH-529 Move sync_recv_block call to on_accepted_block so that chain_h…
Browse files Browse the repository at this point in the history
…ead has been updated. Fix off by one error in call to is_sync_request_ahead_allowed. Work-around accepted_block not called until block is irreversible in irreversible mode.
  • Loading branch information
heifner committed Sep 20, 2024
1 parent bf1789d commit a9eb27a
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2078,7 +2078,7 @@ namespace eosio {

bool sync_manager::is_sync_required( uint32_t fork_head_block_num ) const REQUIRES(sync_mtx) {
fc_dlog( logger, "last req = ${req}, last recv = ${recv} known = ${known} our fhead = ${h}",
("req", sync_last_requested_num)( "recv", sync_next_expected_num )( "known", sync_known_lib_num )
("req", sync_last_requested_num)( "recv", sync_next_expected_num-1 )( "known", sync_known_lib_num )
("h", fork_head_block_num ) );

return( sync_last_requested_num < sync_known_lib_num ||
Expand All @@ -2087,6 +2087,12 @@ namespace eosio {

// called from c's connection strand
bool sync_manager::is_sync_request_ahead_allowed(block_num_type blk_num) const REQUIRES(sync_mtx) {
controller& cc = my_impl->chain_plug->chain();
if (cc.get_read_mode() == db_read_mode::IRREVERSIBLE) {
// When in irreversible mode, we do not get an accepted_block signal until the block is irreversible.
// Use last received number instead so when end of range is reached we check the IRREVERSIBLE conditions below.
blk_num = sync_next_expected_num-1;
}
if (blk_num >= sync_last_requested_num) {
// do not allow to get too far ahead (sync_fetch_span) of chain head
// use chain head instead of fork head so we do not get too far ahead of applied blocks
Expand All @@ -2098,7 +2104,6 @@ namespace eosio {
return true;
}

controller& cc = my_impl->chain_plug->chain();
if (cc.get_read_mode() == db_read_mode::IRREVERSIBLE) {
auto forkdb_head = cc.fork_db_head();
auto calculated_lib = forkdb_head.irreversible_blocknum();
Expand Down Expand Up @@ -2144,7 +2149,7 @@ namespace eosio {
sync_last_requested_num = 0;
sync_next_expected_num = chain_info.lib_num + 1;
request_next_chunk( c );
} else if (sync_last_requested_num > 0 && is_sync_request_ahead_allowed(sync_next_expected_num)) {
} else if (sync_last_requested_num > 0 && is_sync_request_ahead_allowed(sync_next_expected_num-1)) {
request_next_chunk();
} else {
peer_dlog(c, "already syncing, start sync ignored");
Expand Down Expand Up @@ -2520,15 +2525,13 @@ namespace eosio {
}
}
} else { // blk_applied
if (blk_num >= sync_last_requested_num) {
if (is_sync_request_ahead_allowed(blk_num)) {
// Did not request blocks ahead, likely because too far ahead of head, or in irreversible mode
fc_dlog(logger, "Requesting blocks, head: ${h} fhead ${fh} blk_num: ${bn} sync_next_expected_num ${nen} "
"sync_last_requested_num: ${lrn}",
("h", my_impl->get_chain_head_num())("fh", my_impl->get_fork_head_num())
("bn", blk_num)("nen", sync_next_expected_num)("lrn", sync_last_requested_num));
request_next_chunk();
}
if (is_sync_request_ahead_allowed(blk_num)) {
// Did not request blocks ahead, likely because too far ahead of head, or in irreversible mode
fc_dlog(logger, "Requesting blocks, head: ${h} fhead ${fh} blk_num: ${bn} sync_next_expected_num ${nen} "
"sync_last_requested_num: ${lrn}",
("h", my_impl->get_chain_head_num())("fh", my_impl->get_fork_head_num())
("bn", blk_num)("nen", sync_next_expected_num)("lrn", sync_last_requested_num));
request_next_chunk();
}
}
}
Expand Down Expand Up @@ -3843,22 +3846,24 @@ namespace eosio {

// called from application thread
void net_plugin_impl::on_accepted_block_header(const signed_block_ptr& block, const block_id_type& id) {
fc_dlog(logger, "on_accpeted_block_header ${bn} ${id}", ("bn", block->block_num())("id", id));
update_chain_info();

my_impl->dispatcher.strand.post([sync_master = my_impl->sync_master.get(), block, id]() {
const fc::microseconds age(fc::time_point::now() - block->timestamp);
sync_master->sync_recv_block(connection_ptr{}, id, block->block_num(), age);
});

dispatcher.strand.post([block, id]() {
fc_dlog(logger, "signaled accepted_block_header, blk num = ${num}, id = ${id}", ("num", block->block_num())("id", id));
my_impl->dispatcher.bcast_block(block, id);
});
}

void net_plugin_impl::on_accepted_block( const signed_block_ptr& block, const block_id_type&) {
void net_plugin_impl::on_accepted_block( const signed_block_ptr& block, const block_id_type& id) {
fc_dlog(logger, "on_accpeted_block ${bn} ${id}", ("bn", block->block_num())("id", id));
update_chain_info();

my_impl->dispatcher.strand.post([sync_master = my_impl->sync_master.get(), block, id]() {
const fc::microseconds age(fc::time_point::now() - block->timestamp);
sync_master->sync_recv_block(connection_ptr{}, id, block->block_num(), age);
});

sync_master->send_handshakes_if_synced(fc::time_point::now() - block->timestamp);
if (const auto* pending_producers = chain_plug->chain().pending_producers()) {
on_pending_schedule(*pending_producers);
Expand Down

0 comments on commit a9eb27a

Please sign in to comment.