Skip to content

Commit fd8bda8

Browse files
committed
Merge branch 'main' of github.com:AntelopeIO/spring into gh_1183
2 parents 71ba5e0 + 92c53ca commit fd8bda8

File tree

2 files changed

+60
-36
lines changed

2 files changed

+60
-36
lines changed

libraries/eos-vm

plugins/net_plugin/net_plugin.cpp

+59-35
Original file line numberDiff line numberDiff line change
@@ -609,20 +609,33 @@ namespace eosio {
609609

610610
constexpr uint16_t net_version_max = proto_block_nack;
611611

612-
/**
613-
* Index by start_block_num
614-
*/
615612
struct peer_sync_state {
616-
explicit peer_sync_state(uint32_t start = 0, uint32_t end = 0, uint32_t last_acted = 0)
617-
:start_block( start ), end_block( end ), last( last_acted ),
618-
start_time(time_point::now())
613+
enum class sync_t {
614+
peer_sync, // LIB or head catchup, syncing request_message:catch_up
615+
block_nack // sync due to block nack (block_notice_message) request_message:normal
616+
};
617+
peer_sync_state(uint32_t start, uint32_t end, uint32_t last_acted, sync_t sync_type)
618+
:start_block( start ), end_block( end ), last( last_acted ), sync_type( sync_type )
619619
{}
620+
621+
bool valid() const;
622+
620623
uint32_t start_block;
621624
uint32_t end_block;
622625
uint32_t last; ///< last sent or received
623-
time_point start_time; ///< time request made or received
626+
sync_t sync_type;
624627
};
625628

629+
bool peer_sync_state::valid() const {
630+
bool valid = start_block > 0 && end_block >= start_block && last >= start_block-1 && last <= end_block;
631+
if (sync_type == sync_t::block_nack && valid) {
632+
// block nack should only be used for "current" blocks, limit size to something reasonable
633+
const auto size = end_block - start_block;
634+
valid = size < 100;
635+
}
636+
return valid;
637+
}
638+
626639
// thread safe
627640
class queued_buffer : boost::noncopyable {
628641
public:
@@ -1011,7 +1024,7 @@ namespace eosio {
10111024

10121025
void blk_send_branch( const block_id_type& msg_head_id );
10131026
void blk_send_branch_from_nack_request( const block_id_type& msg_head_id, const block_id_type& req_id );
1014-
void blk_send_branch( uint32_t msg_head_num, uint32_t fork_db_root_num, uint32_t head_num );
1027+
void blk_send_branch(uint32_t msg_head_num, uint32_t fork_db_root_num, uint32_t head_num, peer_sync_state::sync_t sync_type);
10151028

10161029
void enqueue( const net_message& msg );
10171030
size_t enqueue_block( const std::vector<char>& sb, uint32_t block_num, queued_buffer::queue_t queue );
@@ -1521,7 +1534,7 @@ namespace eosio {
15211534

15221535
auto msg_head_num = block_header::num_from_id(msg_head_id);
15231536
if (msg_head_num == 0) {
1524-
blk_send_branch( msg_head_num, fork_db_root_num, head_num );
1537+
blk_send_branch( msg_head_num, fork_db_root_num, head_num, peer_sync_state::sync_t::peer_sync );
15251538
return;
15261539
}
15271540

@@ -1534,7 +1547,7 @@ namespace eosio {
15341547
// if peer on fork, start at their last fork_db_root_num, otherwise we can start at msg_head+1
15351548
if (on_fork)
15361549
msg_head_num = 0;
1537-
blk_send_branch( msg_head_num, fork_db_root_num, head_num );
1550+
blk_send_branch( msg_head_num, fork_db_root_num, head_num, peer_sync_state::sync_t::peer_sync );
15381551
}
15391552
}
15401553

@@ -1547,28 +1560,30 @@ namespace eosio {
15471560
// a more complicated better approach would be to find where the fork branches and send from there, for now use lib
15481561
uint32_t fork_db_root_num = my_impl->get_fork_db_root_num();
15491562
// --fork_db_root_num since blk_send_branch adds one to the request, and we want to start at fork_db_root_num
1550-
blk_send_branch( --fork_db_root_num, 0, head_num);
1563+
blk_send_branch( --fork_db_root_num, 0, head_num, peer_sync_state::sync_t::block_nack);
15511564
} else {
15521565
auto msg_req_num = block_header::num_from_id(req_id);
15531566
// --msg_req_num since blk_send_branch adds one to the request, and we need to start at msg_req_num
1554-
blk_send_branch( --msg_req_num, 0, head_num );
1567+
blk_send_branch( --msg_req_num, 0, head_num, peer_sync_state::sync_t::block_nack );
15551568
}
15561569
}
15571570

15581571
// called from connection strand
1559-
void connection::blk_send_branch( uint32_t msg_head_num, uint32_t fork_db_root_num, uint32_t head_num ) {
1572+
void connection::blk_send_branch( uint32_t msg_head_num, uint32_t fork_db_root_num, uint32_t head_num, peer_sync_state::sync_t sync_type) {
15601573
if( !peer_requested ) {
15611574
auto last = msg_head_num != 0 ? msg_head_num : fork_db_root_num;
1562-
peer_requested = peer_sync_state( last+1, head_num, last );
1575+
peer_requested = peer_sync_state( last+1, head_num, last, sync_type );
15631576
} else {
15641577
auto last = msg_head_num != 0 ? msg_head_num : std::min( peer_requested->last, fork_db_root_num );
15651578
uint32_t end = std::max( peer_requested->end_block, head_num );
15661579
if (peer_requested->start_block <= last+1 && peer_requested->end_block >= end)
15671580
return; // nothing to do, send in progress
1568-
peer_requested = peer_sync_state( last+1, end, last );
1581+
peer_requested = peer_sync_state( last+1, end, last, sync_type );
15691582
}
1570-
if( peer_requested->start_block <= peer_requested->end_block ) {
1571-
peer_ilog( this, "enqueue ${s} - ${e}", ("s", peer_requested->start_block)("e", peer_requested->end_block) );
1583+
if( peer_requested->valid() ) {
1584+
peer_ilog( this, "enqueue ${t} ${s} - ${e}",
1585+
("t", sync_type == peer_sync_state::sync_t::peer_sync ? "peer" : "block")
1586+
("s", peer_requested->start_block)("e", peer_requested->end_block) );
15721587
enqueue_sync_block();
15731588
} else {
15741589
peer_ilog( this, "nothing to enqueue" );
@@ -1790,8 +1805,15 @@ namespace eosio {
17901805
block_sync_frame_bytes_sent = 0;
17911806
peer_dlog( this, "completing enqueue_sync_block ${num}", ("num", num) );
17921807
}
1808+
} else if (peer_requested->sync_type == peer_sync_state::sync_t::block_nack) {
1809+
// Do not have the block, likely because in the middle of a fork-switch. A fork-switch will send out
1810+
// block_notice_message for the new blocks. Ignore, similar to the ignore in blk_send_branch().
1811+
peer_ilog( this, "enqueue block sync, unable to fetch block ${num}, resetting peer request", ("num", num) );
1812+
peer_requested.reset(); // unable to provide requested blocks
1813+
block_sync_send_start = 0ns;
1814+
block_sync_frame_bytes_sent = 0;
17931815
} else {
1794-
peer_ilog( this, "enqueue sync, unable to fetch block ${num}, sending benign_other go away", ("num", num) );
1816+
peer_ilog( this, "enqueue peer sync, unable to fetch block ${num}, sending benign_other go away", ("num", num) );
17951817
peer_requested.reset(); // unable to provide requested blocks
17961818
block_sync_send_start = 0ns;
17971819
block_sync_frame_bytes_sent = 0;
@@ -2607,10 +2629,12 @@ namespace eosio {
26072629
g_cp_conn.unlock();
26082630
if( fork_db_head_id == null_id ) {
26092631
// continue
2610-
} else if( c && (fork_db_head_num < blk_num || fork_db_head_id == blk_id) ) {
2611-
fc::lock_guard g_conn( c->conn_mtx );
2612-
c->conn_fork_db_head = null_id;
2613-
c->conn_fork_db_head_num = 0;
2632+
} else if( fork_db_head_num < blk_num || fork_db_head_id == blk_id ) {
2633+
if (c) {
2634+
fc::lock_guard g_conn( c->conn_mtx );
2635+
c->conn_fork_db_head = null_id;
2636+
c->conn_fork_db_head_num = 0;
2637+
}
26142638
} else {
26152639
set_state_to_head_catchup = true;
26162640
}
@@ -3828,7 +3852,7 @@ namespace eosio {
38283852
peer_requested->end_block = std::max(msg.end_block, peer_requested->end_block);
38293853
}
38303854
else {
3831-
peer_requested = peer_sync_state( msg.start_block, msg.end_block, msg.start_block-1);
3855+
peer_requested = peer_sync_state(msg.start_block, msg.end_block, msg.start_block-1, peer_sync_state::sync_t::peer_sync);
38323856
}
38333857
enqueue_sync_block();
38343858
}
@@ -4082,19 +4106,19 @@ namespace eosio {
40824106
fc_dlog(logger, "on_accepted_block_header ${bn} ${id}", ("bn", block->block_num())("id", id));
40834107
update_chain_info();
40844108

4085-
boost::asio::post( my_impl->thread_pool.get_executor(), [block, id]() {
4109+
boost::asio::post( thread_pool.get_executor(), [block, id, this]() {
40864110
fc_dlog(logger, "signaled accepted_block_header, blk num = ${num}, id = ${id}", ("num", block->block_num())("id", id));
4087-
my_impl->dispatcher.bcast_block(block, id);
4111+
dispatcher.bcast_block(block, id);
40884112
});
40894113
}
40904114

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

4095-
if (my_impl->chain_plug->chain().get_read_mode() != db_read_mode::IRREVERSIBLE) {
4119+
if (chain_plug->chain().get_read_mode() != db_read_mode::IRREVERSIBLE) {
40964120
// irreversible notifies sync_manager when added to fork_db, non-irreversible notifies when applied
4097-
my_impl->dispatcher.strand.post([sync_master = my_impl->sync_master.get(), block, id]() {
4121+
dispatcher.strand.post([sync_master = sync_master.get(), block, id]() {
40984122
const fc::microseconds age(fc::time_point::now() - block->timestamp);
40994123
sync_master->sync_recv_block(connection_ptr{}, id, block->block_num(), age);
41004124
});
@@ -4112,9 +4136,9 @@ namespace eosio {
41124136
fc_dlog( logger, "on_irreversible_block, blk num = ${num}, id = ${id}", ("num", block->block_num())("id", id) );
41134137
update_chain_info(id);
41144138

4115-
if (my_impl->chain_plug->chain().get_read_mode() == db_read_mode::IRREVERSIBLE) {
4139+
if (chain_plug->chain().get_read_mode() == db_read_mode::IRREVERSIBLE) {
41164140
// irreversible notifies sync_manager when added to fork_db, non-irreversible notifies when applied
4117-
my_impl->dispatcher.strand.post([sync_master = my_impl->sync_master.get(), block, id]() {
4141+
dispatcher.strand.post([sync_master = sync_master.get(), block, id]() {
41184142
const fc::microseconds age(fc::time_point::now() - block->timestamp);
41194143
sync_master->sync_recv_block(connection_ptr{}, id, block->block_num(), age);
41204144
});
@@ -4147,7 +4171,7 @@ namespace eosio {
41474171
case vote_result_t::invalid_signature:
41484172
case vote_result_t::max_exceeded: // close peer immediately
41494173
fc_elog(vote_logger, "Invalid vote(s), closing connection - ${c}", ("c", connection_id));
4150-
my_impl->connections.any_of_connections([connection_id](const connection_ptr& c) {
4174+
connections.any_of_connections([connection_id](const connection_ptr& c) {
41514175
if (c->connection_id == connection_id) {
41524176
c->close( false );
41534177
return true;
@@ -4158,7 +4182,7 @@ namespace eosio {
41584182
case vote_result_t::unknown_block: // track the failure
41594183
fc_dlog(vote_logger, "connection - ${c} vote unknown block #${bn}:${id}..",
41604184
("c", connection_id)("bn", block_header::num_from_id(msg->block_id))("id", msg->block_id.str().substr(8,16)));
4161-
my_impl->connections.any_of_connections([connection_id](const connection_ptr& c) {
4185+
connections.any_of_connections([connection_id](const connection_ptr& c) {
41624186
if (c->connection_id == connection_id) {
41634187
boost::asio::post(c->strand, [c]() {
41644188
c->block_status_monitor_.rejected();
@@ -4176,24 +4200,24 @@ namespace eosio {
41764200
}
41774201

41784202
void net_plugin_impl::bcast_vote_message( uint32_t exclude_peer, const chain::vote_message_ptr& msg ) {
4179-
if (my_impl->sync_master->syncing_from_peer())
4203+
if (sync_master->syncing_from_peer())
41804204
return;
41814205

41824206
fc_dlog(vote_logger, "bcast ${t} vote: block #${bn} ${id}.., ${v}, key ${k}..",
41834207
("t", exclude_peer ? "received" : "our")("bn", block_header::num_from_id(msg->block_id))("id", msg->block_id.str().substr(8,16))
41844208
("v", msg->strong ? "strong" : "weak")("k", msg->finalizer_key.to_string().substr(8,16)));
41854209

4186-
boost::asio::post( my_impl->thread_pool.get_executor(), [exclude_peer, msg]() mutable {
4210+
boost::asio::post( thread_pool.get_executor(), [exclude_peer, msg, this]() mutable {
41874211
buffer_factory buff_factory;
41884212
auto send_buffer = buff_factory.get_send_buffer( *msg );
41894213

4190-
my_impl->dispatcher.bcast_vote_msg( exclude_peer, std::move(send_buffer) );
4214+
dispatcher.bcast_vote_msg( exclude_peer, std::move(send_buffer) );
41914215
});
41924216
}
41934217

41944218
// called from application thread
41954219
void net_plugin_impl::transaction_ack(const std::pair<fc::exception_ptr, packed_transaction_ptr>& results) {
4196-
boost::asio::post( my_impl->thread_pool.get_executor(), [&dispatcher = my_impl->dispatcher, results]() {
4220+
boost::asio::post( thread_pool.get_executor(), [this, results]() {
41974221
const auto& id = results.second->id();
41984222
if (results.first) {
41994223
fc_dlog( logger, "signaled NACK, trx-id = ${id} : ${why}", ("id", id)( "why", results.first->to_detail_string() ) );

0 commit comments

Comments
 (0)