From e3bb5ba822c294d76458e54880de3c62e36c9498 Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Wed, 20 Nov 2024 16:35:18 +0200 Subject: [PATCH] check for empty blocks --- src/server/p2p/messages.rs | 2 +- src/server/p2p/network.rs | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/server/p2p/messages.rs b/src/server/p2p/messages.rs index 215f0e2..646b025 100644 --- a/src/server/p2p/messages.rs +++ b/src/server/p2p/messages.rs @@ -280,7 +280,7 @@ pub struct ShareChainSyncResponse { version: u64, peer_id: PeerId, algo: u64, - blocks: Vec, + pub blocks: Vec, } impl ShareChainSyncResponse { diff --git a/src/server/p2p/network.rs b/src/server/p2p/network.rs index 0396585..d6c8ce0 100644 --- a/src/server/p2p/network.rs +++ b/src/server/p2p/network.rs @@ -910,11 +910,12 @@ where S: ShareChain let local_peer_id = *self.swarm.local_peer_id(); let squad = self.config.squad.clone(); tokio::spawn(async move { - let response = ShareChainSyncResponse::new( - local_peer_id, - request.algo(), - &share_chain.get_blocks(request.missing_blocks()).await, - ); + let blocks = share_chain.get_blocks(request.missing_blocks()).await; + if blocks.is_empty() { + warn!(target: LOG_TARGET, squad; "No blocks found for sync request"); + return; + } + let response = ShareChainSyncResponse::new(local_peer_id, request.algo(), &blocks); if tx.send(InnerRequest::SyncChainRequest((channel, response))).is_err() { error!(target: LOG_TARGET, squad; "Failed to send block sync response"); @@ -932,6 +933,11 @@ where S: ShareChain trace!(target: LOG_TARGET, squad = &self.config.squad; "Peer {} has an outdated version, skipping", peer); return; } + + if response.blocks.is_empty() { + trace!(target: LOG_TARGET, squad = &self.config.squad; "Peer {} sent 0 blocks", peer); + return; + } let timer = Instant::now(); // if !self.sync_in_progress.load(Ordering::SeqCst) { // return;