From d8d3cf97300480b9d3942c667978dd4860b76feb Mon Sep 17 00:00:00 2001 From: j-berman Date: Thu, 11 Dec 2025 21:32:23 -0800 Subject: p2p: fix race causing dropped connections during sync Without this commit: 1) read height from DB 2) add block to chain in separate thread 3) read chain for block id's and request them from peer 4) ERR in handle_response_chain_entry, peer's first block is the one that was added to the chain, which has block idx=height from step 1. This commit reads the chain for height and highest block id's in one go while holding the m_blockchain_lock to avoid the race. --- src/cryptonote_protocol/cryptonote_protocol_handler.inl | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'src/cryptonote_protocol') diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index 1686394c5..fb195b390 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -274,8 +274,7 @@ namespace cryptonote { NOTIFY_REQUEST_CHAIN::request r = {}; context.m_needed_objects.clear(); - context.m_expect_height = m_core.get_current_blockchain_height(); - m_core.get_short_chain_history(r.block_ids); + m_core.get_short_chain_history(r.block_ids, context.m_expect_height); handler_request_blocks_history( r.block_ids ); // change the limit(?), sleep(?) r.prune = m_sync_pruned_blocks; context.m_last_request_time = boost::posix_time::microsec_clock::universal_time(); @@ -730,8 +729,7 @@ namespace cryptonote context.m_needed_objects.clear(); context.m_state = cryptonote_connection_context::state_synchronizing; NOTIFY_REQUEST_CHAIN::request r = {}; - context.m_expect_height = m_core.get_current_blockchain_height(); - m_core.get_short_chain_history(r.block_ids); + m_core.get_short_chain_history(r.block_ids, context.m_expect_height); handler_request_blocks_history( r.block_ids ); // change the limit(?), sleep(?) r.prune = m_sync_pruned_blocks; context.m_last_request_time = boost::posix_time::microsec_clock::universal_time(); @@ -2340,8 +2338,7 @@ skip: {//we have to fetch more objects ids, request blockchain entry NOTIFY_REQUEST_CHAIN::request r = {}; - context.m_expect_height = m_core.get_current_blockchain_height(); - m_core.get_short_chain_history(r.block_ids); + m_core.get_short_chain_history(r.block_ids, context.m_expect_height); CHECK_AND_ASSERT_MES(!r.block_ids.empty(), false, "Short chain history is empty"); // we'll want to start off from where we are on that peer, which may not be added yet @@ -2477,7 +2474,7 @@ skip: int t_cryptonote_protocol_handler::handle_response_chain_entry(int command, NOTIFY_RESPONSE_CHAIN_ENTRY::request& arg, cryptonote_connection_context& context) { MLOG_P2P_MESSAGE("Received NOTIFY_RESPONSE_CHAIN_ENTRY: m_block_ids.size()=" << arg.m_block_ids.size() - << ", m_start_height=" << arg.start_height << ", m_total_height=" << arg.total_height); + << ", m_start_height=" << arg.start_height << ", m_total_height=" << arg.total_height << ", expect height=" << context.m_expect_height); MLOG_PEER_STATE("received chain"); if (context.m_expect_response != NOTIFY_RESPONSE_CHAIN_ENTRY::ID) -- cgit v1.2.3