From b8b7a1f06616dfb05e4305cbe201e8db402f5fb6 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 7 Mar 2022 11:27:27 +0100 Subject: [PATCH] Make set_parent_hash mark entire chain as bad (#2121) * Make set_parent_hash mark entire chain as bad * Test with an actual chain * Update CHANGELOG --- bin/wasm-node/CHANGELOG.md | 1 + src/sync/all_forks/disjoint.rs | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/bin/wasm-node/CHANGELOG.md b/bin/wasm-node/CHANGELOG.md index 8a90c744b3..1c11844431 100644 --- a/bin/wasm-node/CHANGELOG.md +++ b/bin/wasm-node/CHANGELOG.md @@ -15,3 +15,4 @@ - Implement the `ext_crypto_ecdsa_verify_version_1` host function ([#2120](https://github.com/paritytech/smoldot/pull/2120)). - Implement the `ext_crypto_ecdsa_sign_prehashed_version_1` host function ([#2120](https://github.com/paritytech/smoldot/pull/2120)). - Implement the `ext_crypto_ecdsa_verify_prehashed_version_1` host function ([#2120](https://github.com/paritytech/smoldot/pull/2120)). +- Properly mark all descendants as bad when a block is determined to be bad ([#2121](https://github.com/paritytech/smoldot/pull/2121)). diff --git a/src/sync/all_forks/disjoint.rs b/src/sync/all_forks/disjoint.rs index d80376a461..ea3844b5ed 100644 --- a/src/sync/all_forks/disjoint.rs +++ b/src/sync/all_forks/disjoint.rs @@ -240,7 +240,7 @@ impl DisjointBlocks { } if parent_is_bad { - block.bad = true; + self.set_block_bad(height, hash); } } @@ -432,8 +432,8 @@ mod tests { #[test] fn set_parent_hash_updates_bad() { - // Calling `set_parent_hash` where the parent is a known a bad block marks the block as - // bad as well. + // Calling `set_parent_hash` where the parent is a known a bad block marks the block and + // its children as bad as well. let mut collection = super::DisjointBlocks::new(); @@ -443,10 +443,21 @@ mod tests { assert_eq!(collection.unknown_blocks().count(), 0); collection.insert(2, [2; 32], None, ()); + assert!(!collection.is_bad(2, &[2; 32]).unwrap()); + collection.insert(3, [3; 32], Some([2; 32]), ()); + assert!(!collection.is_bad(3, &[3; 32]).unwrap()); + collection.insert(3, [31; 32], Some([2; 32]), ()); + assert!(!collection.is_bad(3, &[31; 32]).unwrap()); + collection.insert(4, [4; 32], Some([3; 32]), ()); + assert!(!collection.is_bad(4, &[4; 32]).unwrap()); assert_eq!(collection.unknown_blocks().count(), 1); collection.set_parent_hash(2, &[2; 32], [1; 32]); assert_eq!(collection.unknown_blocks().count(), 0); + assert!(collection.is_bad(2, &[2; 32]).unwrap()); + assert!(collection.is_bad(3, &[3; 32]).unwrap()); + assert!(collection.is_bad(3, &[31; 32]).unwrap()); + assert!(collection.is_bad(4, &[4; 32]).unwrap()); } #[test]