From 069e954a3afd56a145f9a0019e643a949a182843 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Sun, 28 Jan 2024 10:49:46 +0100 Subject: [PATCH] Always verify blocks before justifications (#1618) * Always verify blocks before justifications * PR link --- lib/src/sync/all_forks.rs | 35 +++++++++++++++++++---------------- wasm-node/CHANGELOG.md | 4 ++++ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/src/sync/all_forks.rs b/lib/src/sync/all_forks.rs index f3020666b1..ed4d342d57 100644 --- a/lib/src/sync/all_forks.rs +++ b/lib/src/sync/all_forks.rs @@ -1003,13 +1003,30 @@ impl AllForksSync { /// This method takes ownership of the [`AllForksSync`] and starts a verification /// process. The [`AllForksSync`] is yielded back at the end of this process. pub fn process_one(mut self) -> ProcessOne { + // Try to find a block to verify. + // All blocks are always verified before verifying justifications, in order to guarantee + // that the block that a justification targets has already been verified. + // TODO: revisit that ^ as advancing finality should have priority over advancing the chain + let block_to_verify = self.inner.blocks.unverified_leaves().find(|block| { + block.parent_block_hash == self.chain.finalized_block_hash() + || self + .chain + .contains_non_finalized_block(&block.parent_block_hash) + }); + if let Some(block) = block_to_verify { + return ProcessOne::BlockVerify(BlockVerify { + parent: self, + block_to_verify: block, + }); + } + + // Try to find a justification to verify. // TODO: O(n) let source_id_with_finality_proof = self .inner .blocks .sources() .find(|id| !self.inner.blocks[*id].unverified_finality_proofs.is_none()); - if let Some(source_id_with_finality_proof) = source_id_with_finality_proof { let finality_proof_to_verify = self.inner.blocks[source_id_with_finality_proof] .unverified_finality_proofs @@ -1022,21 +1039,7 @@ impl AllForksSync { }); } - let block = self.inner.blocks.unverified_leaves().find(|block| { - block.parent_block_hash == self.chain.finalized_block_hash() - || self - .chain - .contains_non_finalized_block(&block.parent_block_hash) - }); - - if let Some(block) = block { - ProcessOne::BlockVerify(BlockVerify { - parent: self, - block_to_verify: block, - }) - } else { - ProcessOne::AllSync { sync: self } - } + ProcessOne::AllSync { sync: self } } } diff --git a/wasm-node/CHANGELOG.md b/wasm-node/CHANGELOG.md index 4a52e71855..b1b5cafa87 100644 --- a/wasm-node/CHANGELOG.md +++ b/wasm-node/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Fixed + +- Fix "Justification targets block not in the chain" errors, leading to peers being erroneously banned. ([#1618](https://github.com/smol-dot/smoldot/pull/1618)) + ## 2.0.19 - 2024-01-26 ### Fixed