From d8ca68426060d8538fcce0590024029837adbbdb Mon Sep 17 00:00:00 2001 From: Gary Yu Date: Mon, 1 Oct 2018 21:58:50 +0800 Subject: [PATCH] fix: in case of all known, update header_head and sync_head (#1619) * fix: in case of all known for Headers received, update sync_head to the last header * refactor: push this logic down into pipe --- chain/src/pipe.rs | 19 +++++++++++++++++++ servers/src/common/adapters.rs | 7 ------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/chain/src/pipe.rs b/chain/src/pipe.rs index 6c3f9d784..f3ffefa4e 100644 --- a/chain/src/pipe.rs +++ b/chain/src/pipe.rs @@ -205,6 +205,24 @@ pub fn sync_block_headers( ctx: &mut BlockContext, batch: &mut store::Batch, ) -> Result<(), Error> { + let bhs_last = headers.last().unwrap().clone(); + let last_h = bhs_last.hash(); + if let Ok(_) = batch.get_block_header(&last_h) { + info!( + LOGGER, + "All known, ignoring. Update sync_head to {} at {}", last_h, bhs_last.height, + ); + + let res = update_sync_head(&bhs_last, batch); + if let &Err(ref e) = &res { + error!( + LOGGER, + "Block header {} update_sync_head fail: {:?}", last_h, e + ); + } + return Ok(()); + } + if let Some(header) = headers.first() { debug!( LOGGER, @@ -664,6 +682,7 @@ fn update_sync_head(bh: &BlockHeader, batch: &mut store::Batch) -> Result<(), Er Ok(()) } +/// Update the header head so we can keep syncing from where we left off. fn update_header_head( bh: &BlockHeader, ctx: &mut BlockContext, diff --git a/servers/src/common/adapters.rs b/servers/src/common/adapters.rs index e0a429dba..ccbb15765 100644 --- a/servers/src/common/adapters.rs +++ b/servers/src/common/adapters.rs @@ -263,13 +263,6 @@ impl p2p::ChainAdapter for NetToChainAdapter { return false; } - // headers will just set us backward if even the last is unknown - let last_h = bhs.last().unwrap().hash(); - if let Ok(_) = w(&self.chain).get_block_header(&last_h) { - info!(LOGGER, "All known, ignoring"); - return true; - } - // try to add headers to our header chain let res = w(&self.chain).sync_block_headers(&bhs, self.chain_opts()); if let &Err(ref e) = &res {