From cd72be893e56d9facd6acf796bcf7f24372220ac Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Mon, 19 Mar 2018 22:17:59 +0000 Subject: [PATCH] Do not request orphan predecessor if we have its header. Shows we're already on it (sync). --- grin/src/adapters.rs | 49 +++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/grin/src/adapters.rs b/grin/src/adapters.rs index 7c76732c5..9cf3b2664 100644 --- a/grin/src/adapters.rs +++ b/grin/src/adapters.rs @@ -388,9 +388,11 @@ impl NetToChainAdapter { Ok(_) => true, Err(chain::Error::Orphan) => { // make sure we did not miss the parent block - if !self.currently_syncing.load(Ordering::Relaxed) && !chain.is_orphan(&prev_hash) { - debug!(LOGGER, "adapter: process_block: received an orphan block, checking the parent: {:}", prev_hash); - self.request_block_by_hash(prev_hash, &addr) + if !chain.is_orphan(&prev_hash) { + if let Err(_) = chain.get_block_header(&prev_hash) { + debug!(LOGGER, "adapter: process_block: received an orphan block, checking the parent: {:}", prev_hash); + self.request_block_by_hash(prev_hash, &addr) + } } true } @@ -415,8 +417,6 @@ impl NetToChainAdapter { // After receiving a compact block if we cannot successfully hydrate // it into a full block then fallback to requesting the full block // from the same peer that gave us the compact block - // - // TODO - currently only request block from a single peer // consider additional peers for redundancy? fn request_block(&self, bh: &BlockHeader, addr: &SocketAddr) { self.request_block_by_hash(bh.hash(), addr) @@ -429,9 +429,6 @@ impl NetToChainAdapter { // After we have received a block header in "header first" propagation // we need to go request the block (compact representation) from the // same peer that gave us the header (unless we have already accepted the block) - // - // TODO - currently only request block from a single peer - // consider additional peers for redundancy? fn request_compact_block(&self, bh: &BlockHeader, addr: &SocketAddr) { self.send_block_request_to_peer(bh.hash(), addr, |peer, h| { peer.send_compact_block_request(h) @@ -443,24 +440,24 @@ impl NetToChainAdapter { F: Fn(&p2p::Peer, Hash) -> Result<(), p2p::Error>, { match w(&self.chain).block_exists(h) { - Ok(false) => { - match wo(&self.peers).get_connected_peer(addr) { - None => debug!(LOGGER, "send_block_request_to_peer: can't send request to peer {:?}, not connected", addr), - Some(peer) => { - match peer.read() { - Err(e) => debug!(LOGGER, "send_block_request_to_peer: can't send request to peer {:?}, read fails: {:?}", addr, e), - Ok(p) => { - if let Err(e) = f(&p, h) { - error!(LOGGER, "send_block_request_to_peer: failed: {:?}", e) - } - } - } - } - } - } - Ok(true) => debug!(LOGGER, "send_block_request_to_peer: block {} already known", h), - Err(e) => error!(LOGGER, "send_block_request_to_peer: failed to check block exists: {:?}", e) - } + Ok(false) => { + match wo(&self.peers).get_connected_peer(addr) { + None => debug!(LOGGER, "send_block_request_to_peer: can't send request to peer {:?}, not connected", addr), + Some(peer) => { + match peer.read() { + Err(e) => debug!(LOGGER, "send_block_request_to_peer: can't send request to peer {:?}, read fails: {:?}", addr, e), + Ok(p) => { + if let Err(e) = f(&p, h) { + error!(LOGGER, "send_block_request_to_peer: failed: {:?}", e) + } + } + } + } + } + } + Ok(true) => debug!(LOGGER, "send_block_request_to_peer: block {} already known", h), + Err(e) => error!(LOGGER, "send_block_request_to_peer: failed to check block exists: {:?}", e) + } } /// Prepare options for the chain pipeline