Skip to content

Commit 1bf5078

Browse files
authored
Merge pull request #1223 from AntelopeIO/p2p-opt-block-processing
P2P: Avoid thread hop on incoming blocks
2 parents 7ff06d2 + 6de18cd commit 1bf5078

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

libraries/chain/controller.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -4238,9 +4238,12 @@ struct controller_impl {
42384238
EOS_ASSERT( b, block_validate_exception, "null block" );
42394239

42404240
auto f = [&](auto& fork_db) -> controller::accepted_block_result {
4241+
if (auto bsp = fork_db.get_block(id, include_root_t::yes))
4242+
return controller::accepted_block_result{.add_result = fork_db_add_t::duplicate, .block{std::optional<block_handle>{std::move(bsp)}}};
42414243
// previous not found, means it is unlinkable
42424244
auto prev = fork_db.get_block( b->previous, include_root_t::yes );
4243-
if( !prev ) return {};
4245+
if( !prev )
4246+
return controller::accepted_block_result{.add_result = fork_db_add_t::failure, .block{}};
42444247

42454248
return create_block_state_i( fork_db, id, b, *prev );
42464249
};

plugins/net_plugin/net_plugin.cpp

+5-21
Original file line numberDiff line numberDiff line change
@@ -3949,25 +3949,11 @@ namespace eosio {
39493949
void connection::handle_message( const block_id_type& id, signed_block_ptr ptr ) {
39503950
// post to dispatcher strand so that we don't have multiple threads validating the block header
39513951
peer_dlog(this, "posting block ${n} to dispatcher strand", ("n", ptr->block_num()));
3952-
my_impl->dispatcher.strand.post([id, c{shared_from_this()}, ptr{std::move(ptr)}, cid=connection_id]() mutable {
3952+
my_impl->dispatcher.strand.dispatch([id, c{shared_from_this()}, ptr{std::move(ptr)}, cid=connection_id]() mutable {
39533953
if (app().is_quiting()) // large sync span can have many of these queued up, exit quickly
39543954
return;
39553955
controller& cc = my_impl->chain_plug->chain();
39563956

3957-
auto fork_db_root_num = my_impl->get_fork_db_root_num();
3958-
3959-
// may have come in on a different connection and posted into dispatcher strand before this one
3960-
if( block_header::num_from_id(id) <= fork_db_root_num || my_impl->dispatcher.have_block( id ) || cc.block_exists( id ) ) { // thread-safe
3961-
boost::asio::post(c->strand, [c, id, ptr{std::move(ptr)}]() {
3962-
if (my_impl->dispatcher.add_peer_block( id, c->connection_id )) {
3963-
c->send_block_nack(id);
3964-
}
3965-
const fc::microseconds age(fc::time_point::now() - ptr->timestamp);
3966-
my_impl->sync_master->sync_recv_block( c, id, block_header::num_from_id(id), age );
3967-
});
3968-
return;
3969-
}
3970-
39713957
// proper_svnn_block_seen is for integration tests that verify low number of `unlinkable_blocks` logs.
39723958
// Because we now process blocks immediately into the fork database, during savanna transition the first proper
39733959
// savanna block will be reported as unlinkable when lib syncing. We will request that block again and by then
@@ -3989,7 +3975,7 @@ namespace eosio {
39893975
controller::accepted_block_result abh = cc.accept_block( id, ptr );
39903976
fork_db_add_result = abh.add_result;
39913977
obh = std::move(abh.block);
3992-
unlinkable = !obh;
3978+
unlinkable = fork_db_add_result == fork_db_add_t::failure;
39933979
close_mode = sync_manager::closing_mode::handshake;
39943980
} catch( const invalid_qc_claim& ex) {
39953981
exception = true;
@@ -4028,11 +4014,9 @@ namespace eosio {
40284014
c->block_status_monitor_.accepted();
40294015

40304016
if (my_impl->chain_plug->chain().get_read_mode() == db_read_mode::IRREVERSIBLE) {
4031-
// non-irreversible notifies sync_manager when block is applied
4032-
my_impl->dispatcher.strand.post([sync_master = my_impl->sync_master.get(), bh=*obh]() {
4033-
const fc::microseconds age(fc::time_point::now() - bh.timestamp());
4034-
sync_master->sync_recv_block(connection_ptr{}, bh.id(), bh.block_num(), age);
4035-
});
4017+
// non-irreversible notifies sync_manager when block is applied, call on dispatcher strand
4018+
const fc::microseconds age(fc::time_point::now() - obh->timestamp());
4019+
my_impl->sync_master->sync_recv_block(connection_ptr{}, obh->id(), obh->block_num(), age);
40364020
}
40374021

40384022
if (fork_db_add_result == fork_db_add_t::appended_to_head || fork_db_add_result == fork_db_add_t::fork_switch) {

0 commit comments

Comments
 (0)