From 724b19e6481fe4f09898cbf8b346207efc4942c3 Mon Sep 17 00:00:00 2001 From: Gary Yu Date: Tue, 24 Jul 2018 12:29:31 +0800 Subject: [PATCH] process_block check_known() check orphans also, to avoid double-processing (#1287) --- chain/src/chain.rs | 7 ++++--- chain/src/pipe.rs | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/chain/src/chain.rs b/chain/src/chain.rs index be42ace7..c68fb4c3 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -52,7 +52,7 @@ struct Orphan { added: Instant, } -struct OrphanBlockPool { +pub struct OrphanBlockPool { // blocks indexed by their hash orphans: RwLock>, // additional index of height -> hash @@ -117,7 +117,7 @@ impl OrphanBlockPool { .map(|hs| hs.iter().filter_map(|h| orphans.remove(h)).collect()) } - fn contains(&self, hash: &Hash) -> bool { + pub fn contains(&self, hash: &Hash) -> bool { let orphans = self.orphans.read().unwrap(); orphans.contains_key(hash) } @@ -336,6 +336,7 @@ impl Chain { pow_verifier: self.pow_verifier, block_hashes_cache: self.block_hashes_cache.clone(), txhashset: self.txhashset.clone(), + orphans: self.orphans.clone(), }) } @@ -378,7 +379,7 @@ impl Chain { trace!( LOGGER, "chain: done check_orphans at {}. # remaining orphans {}", - height, + height-1, self.orphans.len(), ); } diff --git a/chain/src/pipe.rs b/chain/src/pipe.rs index 251a6793..221d22ab 100644 --- a/chain/src/pipe.rs +++ b/chain/src/pipe.rs @@ -28,6 +28,7 @@ use error::{Error, ErrorKind}; use grin_store; use store; use txhashset; +use chain::{OrphanBlockPool}; use types::{Options, Tip}; use util::LOGGER; @@ -48,6 +49,8 @@ pub struct BlockContext { pub txhashset: Arc>, /// Recently processed blocks to avoid double-processing pub block_hashes_cache: Arc>>, + /// Recent orphan blocks to avoid double-processing + pub orphans: Arc, } /// Runs the block processing pipeline, including validation and finding a @@ -194,6 +197,9 @@ fn check_known(bh: Hash, ctx: &mut BlockContext) -> Result<(), Error> { if cache.contains(&bh) { return Err(ErrorKind::Unfit("already known in cache".to_string()).into()); } + if ctx.orphans.contains(&bh) { + return Err(ErrorKind::Unfit("already known in orphans".to_string()).into()); + } Ok(()) }