From 413ebf3f19617808674eb1701642951b7a0c1b3e Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Tue, 15 May 2018 10:26:55 +0200 Subject: [PATCH] check if block is known before re-importing (#161) --- substrate/substrate/client/src/client.rs | 1 - substrate/substrate/network/src/sync.rs | 12 ++++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/substrate/substrate/client/src/client.rs b/substrate/substrate/client/src/client.rs index c0b16bf109..967a24198d 100644 --- a/substrate/substrate/client/src/client.rs +++ b/substrate/substrate/client/src/client.rs @@ -289,7 +289,6 @@ impl Client where body: Option, ) -> error::Result { // TODO: import lock - // TODO: validate block // TODO: import justification. let (header, justification) = header.into_inner(); match self.backend.blockchain().status(BlockId::Hash(header.parent_hash))? { diff --git a/substrate/substrate/network/src/sync.rs b/substrate/substrate/network/src/sync.rs index 395681efff..9cef0cd021 100644 --- a/substrate/substrate/network/src/sync.rs +++ b/substrate/substrate/network/src/sync.rs @@ -230,6 +230,18 @@ impl ChainSync { let hash = header_hash(&header); let parent = header.parent_hash; let is_best = best_seen.as_ref().map_or(false, |n| number >= *n); + + // check whether the block is known before importing. + match protocol.chain().block_status(&BlockId::Hash(hash)) { + Ok(BlockStatus::InChain) => continue, + Ok(_) => {}, + Err(e) => { + debug!(target: "sync", "Error importing block {}: {:?}: {:?}", number, hash, e); + self.restart(io, protocol); + return; + } + } + let result = protocol.chain().import( is_best, header,