From 4fbb0e3011c1b870ec6f6fa173c6481f58513d4c Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Thu, 31 Aug 2023 22:23:15 +0800 Subject: [PATCH] fix new_block_received got duplicate block --- sync/src/types/mod.rs | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/sync/src/types/mod.rs b/sync/src/types/mod.rs index 80a6774667..f420bdef58 100644 --- a/sync/src/types/mod.rs +++ b/sync/src/types/mod.rs @@ -1307,26 +1307,32 @@ impl SyncShared { // Return true when the block is that we have requested and received first time. pub fn new_block_received(&self, block: &core::BlockView) -> bool { - if self + if !self .state() .write_inflight_blocks() .remove_by_block((block.number(), block.hash()).into()) { - { - let status = self.shared().get_block_status(&block.hash()); - debug!( - "new_block_received {}-{}, status: {:?}", - block.number(), - block.hash(), - status - ); - } - self.shared() - .insert_block_status(block.hash(), BlockStatus::BLOCK_RECEIVED); - true - } else { - false + return false; + } + + let status = self.shared().get_block_status(&block.hash()); + debug!( + "new_block_received {}-{}, status: {:?}", + block.number(), + block.hash(), + status + ); + if BlockStatus::HEADER_VALID.eq(&status) { + return false; + } + + if let dashmap::mapref::entry::Entry::Vacant(status) = + self.shared().block_status_map().entry(block.hash()) + { + status.insert(BlockStatus::BLOCK_RECEIVED); + return true; } + false } }