From f94486945ea8965d3f44d600e1616d70f6c9a1e3 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Mon, 22 Jul 2024 10:37:04 +0000 Subject: [PATCH 01/16] blockchain/backend: Skip genesis leaf to unblock syncing Signed-off-by: Alexandru Vasile --- substrate/primitives/blockchain/src/backend.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/substrate/primitives/blockchain/src/backend.rs b/substrate/primitives/blockchain/src/backend.rs index 2accd4dad12c5..c17213c5d57a2 100644 --- a/substrate/primitives/blockchain/src/backend.rs +++ b/substrate/primitives/blockchain/src/backend.rs @@ -255,6 +255,7 @@ pub trait Backend: ) -> std::result::Result, Error> { let leaves = self.leaves()?; + let now = std::time::Instant::now(); debug!( target: crate::LOG_TARGET, ?leaves, @@ -297,6 +298,17 @@ pub trait Backend: let mut displaced_blocks_candidates = Vec::new(); for leaf_hash in leaves { + let info = self.info(); + if leaf_hash == info.genesis_hash { + // Genesis block is not displaced + debug!( + target: crate::LOG_TARGET, + "Skip genesis block {leaf_hash:?} reporterd as leaf (elapsed {:?})", now.elapsed(), + ); + + continue; + } + let mut current_header_metadata = MinimalBlockMetadata::from(&self.header_metadata(leaf_hash)?); let leaf_number = current_header_metadata.number; From c9a13e8012697fec7557fe61ab348b7dbdfd5195 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Mon, 22 Jul 2024 10:38:18 +0000 Subject: [PATCH 02/16] blockchain/backend: Add debug logs for easier debugging if need be Signed-off-by: Alexandru Vasile --- .../primitives/blockchain/src/backend.rs | 84 +++++++++++++++++-- 1 file changed, 78 insertions(+), 6 deletions(-) diff --git a/substrate/primitives/blockchain/src/backend.rs b/substrate/primitives/blockchain/src/backend.rs index c17213c5d57a2..f3f64b4901b7b 100644 --- a/substrate/primitives/blockchain/src/backend.rs +++ b/substrate/primitives/blockchain/src/backend.rs @@ -259,7 +259,7 @@ pub trait Backend: debug!( target: crate::LOG_TARGET, ?leaves, - %finalized_block_hash, + ?finalized_block_hash, ?finalized_block_number, "Checking for displaced leaves after finalization." ); @@ -282,7 +282,15 @@ pub trait Backend: ); return Ok(DisplacedLeavesAfterFinalization::default()); }, - Err(e) => Err(e)?, + Err(e) => { + debug!( + target: crate::LOG_TARGET, + hash = ?finalized_block_hash, + err = ?e, + "Failed to fetch block" + ); + return Err(e); + }, }; finalized_chain.push_front(MinimalBlockMetadata::from(¤t_finalized)); @@ -310,9 +318,22 @@ pub trait Backend: } let mut current_header_metadata = - MinimalBlockMetadata::from(&self.header_metadata(leaf_hash)?); + MinimalBlockMetadata::from(&self.header_metadata(leaf_hash).map_err(|err| { + debug!( + target: crate::LOG_TARGET, + ?err, + "Failed to fetch header for {leaf_hash:?} (elapsed {:?})", now.elapsed(), + ); + err + })?); let leaf_number = current_header_metadata.number; + debug!( + target: crate::LOG_TARGET, + ?leaf_number, + "Handle displaced leaf {leaf_hash:?} (elapsed {:?})", now.elapsed(), + ); + // Collect all block hashes until the height of the finalized block displaced_blocks_candidates.clear(); while current_header_metadata.number > finalized_block_number { @@ -325,7 +346,15 @@ pub trait Backend: }, None => { current_header_metadata = - MinimalBlockMetadata::from(&self.header_metadata(parent_hash)?); + MinimalBlockMetadata::from(&self.header_metadata(parent_hash).map_err(|err| { + debug!( + target: crate::LOG_TARGET, + ?err, + "Failed to fetch header for parent hash {parent_hash:?} during leaf tracking (elapsed {:?})", now.elapsed(), + ); + + err + })?); // Cache locally in case more branches above finalized block reference // the same block hash local_cache.insert(parent_hash, current_header_metadata); @@ -336,6 +365,11 @@ pub trait Backend: // If points back to the finalized header then nothing left to do, this leaf will be // checked again later if current_header_metadata.hash == finalized_block_hash { + debug!( + target: crate::LOG_TARGET, + "Leaf points to the finalized header {leaf_hash:?}, skipping for now (elapsed {:?})", now.elapsed(), + ); + continue; } @@ -364,7 +398,14 @@ pub trait Backend: ); break; }, - Err(e) => Err(e)?, + Err(e) => { + debug!( + target: crate::LOG_TARGET, + err = ?e, + "Failed to fetch header for parent hash {:?} during block collection (elapsed {:?})", to_fetch.parent, now.elapsed(), + ); + return Err(e); + }, }; let metadata = MinimalBlockMetadata::from(&metadata); let result = (metadata.number, metadata.hash); @@ -376,6 +417,13 @@ pub trait Backend: if current_header_metadata.number <= finalized_chain_block_number { // Skip more blocks until we get all blocks on finalized chain until the height // of the parent block + debug!( + target: crate::LOG_TARGET, + current_hash = ?current_header_metadata.hash, + current_num = ?current_header_metadata.number, + finalized_num = ?finalized_chain_block_number, + "Skip more blocks until we get all blocks on finalized chain until the height of the parent block" + ); continue; } @@ -384,13 +432,29 @@ pub trait Backend: // Reached finalized chain, nothing left to do result.displaced_blocks.extend(displaced_blocks_candidates.drain(..)); result.displaced_leaves.push((leaf_number, leaf_hash)); + + debug!( + target: crate::LOG_TARGET, + "Found displaced leaf {leaf_hash:?} (elapsed {:?})", now.elapsed(), + ); break; } // Store displaced block and look deeper for block on finalized chain + debug!( + target: crate::LOG_TARGET, + "Found displaced block {parent_hash:?} and look further... (elapsed {:?})", now.elapsed(), + ); displaced_blocks_candidates.push(parent_hash); current_header_metadata = - MinimalBlockMetadata::from(&self.header_metadata(parent_hash)?); + MinimalBlockMetadata::from(&self.header_metadata(parent_hash).map_err(|err| { + debug!( + target: crate::LOG_TARGET, + ?err, + "Failed to fetch header for parent {parent_hash:?} during displaced block collection (elapsed {:?})", now.elapsed(), + ); + err + })?); } } @@ -398,6 +462,14 @@ pub trait Backend: result.displaced_blocks.sort_unstable(); result.displaced_blocks.dedup(); + debug!( + target: crate::LOG_TARGET, + %finalized_block_hash, + ?finalized_block_number, + "Finished with result {result:?} (elapsed {:?})", + now.elapsed(), + ); + return Ok(result); } } From 1c74bf6aa9fddd6f8a0f3514252669f25b5a5b49 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Mon, 22 Jul 2024 11:00:31 +0000 Subject: [PATCH 03/16] blockchain/backend: Move the debug log above to avoid spams Signed-off-by: Alexandru Vasile --- substrate/primitives/blockchain/src/backend.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/substrate/primitives/blockchain/src/backend.rs b/substrate/primitives/blockchain/src/backend.rs index f3f64b4901b7b..5fe91ef3597dc 100644 --- a/substrate/primitives/blockchain/src/backend.rs +++ b/substrate/primitives/blockchain/src/backend.rs @@ -378,6 +378,14 @@ pub trait Backend: // check for this gap later. displaced_blocks_candidates.push(current_header_metadata.hash); + debug!( + target: crate::LOG_TARGET, + current_hash = ?current_header_metadata.hash, + current_num = ?current_header_metadata.number, + ?finalized_block_number, + "Looking for path from current num to finalized block num" + ); + // Collect the rest of the displaced blocks of leaf branch for distance_from_finalized in 1_u32.. { // Find block at `distance_from_finalized` from finalized block @@ -417,13 +425,6 @@ pub trait Backend: if current_header_metadata.number <= finalized_chain_block_number { // Skip more blocks until we get all blocks on finalized chain until the height // of the parent block - debug!( - target: crate::LOG_TARGET, - current_hash = ?current_header_metadata.hash, - current_num = ?current_header_metadata.number, - finalized_num = ?finalized_chain_block_number, - "Skip more blocks until we get all blocks on finalized chain until the height of the parent block" - ); continue; } From 39e29c6ade549f95dfe2f3cdfb51d69acf514bd3 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Mon, 22 Jul 2024 11:08:06 +0000 Subject: [PATCH 04/16] blockchain/backend: Use proper path for debug log Signed-off-by: Alexandru Vasile --- substrate/primitives/blockchain/src/backend.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/primitives/blockchain/src/backend.rs b/substrate/primitives/blockchain/src/backend.rs index 5fe91ef3597dc..b31e5b894c86f 100644 --- a/substrate/primitives/blockchain/src/backend.rs +++ b/substrate/primitives/blockchain/src/backend.rs @@ -383,7 +383,7 @@ pub trait Backend: current_hash = ?current_header_metadata.hash, current_num = ?current_header_metadata.number, ?finalized_block_number, - "Looking for path from current num to finalized block num" + "Looking for path from finalized block number to current leaf number" ); // Collect the rest of the displaced blocks of leaf branch From c8e4c341be88af4798f4a76d1d00a1444523b8ce Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Mon, 22 Jul 2024 17:08:19 +0000 Subject: [PATCH 05/16] blockchain/backend: Adjust stop check and debug logs Signed-off-by: Alexandru Vasile --- .../primitives/blockchain/src/backend.rs | 89 ++++++++++++------- 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/substrate/primitives/blockchain/src/backend.rs b/substrate/primitives/blockchain/src/backend.rs index b31e5b894c86f..cb6e5776c41a7 100644 --- a/substrate/primitives/blockchain/src/backend.rs +++ b/substrate/primitives/blockchain/src/backend.rs @@ -278,7 +278,8 @@ pub trait Backend: debug!( target: crate::LOG_TARGET, hash = ?finalized_block_hash, - "Tried to fetch unknown block, block ancestry has gaps." + elapsed = ?now.elapsed(), + "Tried to fetch unknown block, block ancestry has gaps.", ); return Ok(DisplacedLeavesAfterFinalization::default()); }, @@ -287,7 +288,8 @@ pub trait Backend: target: crate::LOG_TARGET, hash = ?finalized_block_hash, err = ?e, - "Failed to fetch block" + elapsed = ?now.elapsed(), + "Failed to fetch block.", ); return Err(e); }, @@ -306,23 +308,14 @@ pub trait Backend: let mut displaced_blocks_candidates = Vec::new(); for leaf_hash in leaves { - let info = self.info(); - if leaf_hash == info.genesis_hash { - // Genesis block is not displaced - debug!( - target: crate::LOG_TARGET, - "Skip genesis block {leaf_hash:?} reporterd as leaf (elapsed {:?})", now.elapsed(), - ); - - continue; - } - let mut current_header_metadata = MinimalBlockMetadata::from(&self.header_metadata(leaf_hash).map_err(|err| { debug!( target: crate::LOG_TARGET, + ?leaf_hash, ?err, - "Failed to fetch header for {leaf_hash:?} (elapsed {:?})", now.elapsed(), + elapsed = ?now.elapsed(), + "Failed to fetch leaf header.", ); err })?); @@ -331,7 +324,8 @@ pub trait Backend: debug!( target: crate::LOG_TARGET, ?leaf_number, - "Handle displaced leaf {leaf_hash:?} (elapsed {:?})", now.elapsed(), + elapsed = ?now.elapsed(), + "Handle displaced leaf.", ); // Collect all block hashes until the height of the finalized block @@ -345,16 +339,19 @@ pub trait Backend: current_header_metadata = *metadata_header; }, None => { - current_header_metadata = - MinimalBlockMetadata::from(&self.header_metadata(parent_hash).map_err(|err| { + current_header_metadata = MinimalBlockMetadata::from( + &self.header_metadata(parent_hash).map_err(|err| { debug!( target: crate::LOG_TARGET, ?err, - "Failed to fetch header for parent hash {parent_hash:?} during leaf tracking (elapsed {:?})", now.elapsed(), + ?parent_hash, + elapsed = ?now.elapsed(), + "Failed to fetch parent header during leaf tracking.", ); err - })?); + })?, + ); // Cache locally in case more branches above finalized block reference // the same block hash local_cache.insert(parent_hash, current_header_metadata); @@ -367,7 +364,9 @@ pub trait Backend: if current_header_metadata.hash == finalized_block_hash { debug!( target: crate::LOG_TARGET, - "Leaf points to the finalized header {leaf_hash:?}, skipping for now (elapsed {:?})", now.elapsed(), + ?leaf_hash, + elapsed = ?now.elapsed(), + "Leaf points to the finalized header, skipping for now.", ); continue; @@ -383,6 +382,7 @@ pub trait Backend: current_hash = ?current_header_metadata.hash, current_num = ?current_header_metadata.number, ?finalized_block_number, + elapsed = ?now.elapsed(), "Looking for path from finalized block number to current leaf number" ); @@ -402,17 +402,21 @@ pub trait Backend: distance_from_finalized, hash = ?to_fetch.parent, number = ?to_fetch.number, + elapsed = ?now.elapsed(), "Tried to fetch unknown block, block ancestry has gaps." ); break; }, - Err(e) => { + Err(err) => { debug!( target: crate::LOG_TARGET, - err = ?e, - "Failed to fetch header for parent hash {:?} during block collection (elapsed {:?})", to_fetch.parent, now.elapsed(), + hash = ?to_fetch.parent, + number = ?to_fetch.number, + ?err, + elapsed = ?now.elapsed(), + "Failed to fetch header for parent hash.", ); - return Err(e); + return Err(err); }, }; let metadata = MinimalBlockMetadata::from(&metadata); @@ -422,6 +426,19 @@ pub trait Backend: }, }; + if current_header_metadata.hash == finalized_chain_block_hash { + // Found the block on the finalized chain, nothing left to do + result.displaced_leaves.push((leaf_number, leaf_hash)); + + debug!( + target: crate::LOG_TARGET, + ?leaf_hash, + elapsed = ?now.elapsed(), + "Leaf is ancestor of finalized block." + ); + break; + } + if current_header_metadata.number <= finalized_chain_block_number { // Skip more blocks until we get all blocks on finalized chain until the height // of the parent block @@ -436,7 +453,9 @@ pub trait Backend: debug!( target: crate::LOG_TARGET, - "Found displaced leaf {leaf_hash:?} (elapsed {:?})", now.elapsed(), + ?leaf_hash, + elapsed = ?now.elapsed(), + "Found displaced leaf." ); break; } @@ -444,18 +463,23 @@ pub trait Backend: // Store displaced block and look deeper for block on finalized chain debug!( target: crate::LOG_TARGET, - "Found displaced block {parent_hash:?} and look further... (elapsed {:?})", now.elapsed(), + ?parent_hash, + elapsed = ?now.elapsed(), + "Found displaced block. Looking further.", ); displaced_blocks_candidates.push(parent_hash); - current_header_metadata = - MinimalBlockMetadata::from(&self.header_metadata(parent_hash).map_err(|err| { + current_header_metadata = MinimalBlockMetadata::from( + &self.header_metadata(parent_hash).map_err(|err| { debug!( target: crate::LOG_TARGET, ?err, - "Failed to fetch header for parent {parent_hash:?} during displaced block collection (elapsed {:?})", now.elapsed(), + ?parent_hash, + elapsed = ?now.elapsed(), + "Failed to fetch header for parent during displaced block collection", ); err - })?); + })?, + ); } } @@ -467,8 +491,9 @@ pub trait Backend: target: crate::LOG_TARGET, %finalized_block_hash, ?finalized_block_number, - "Finished with result {result:?} (elapsed {:?})", - now.elapsed(), + ?result, + elapsed = ?now.elapsed(), + "Finished checking for displaced leaves after finalization.", ); return Ok(result); From de72635dc3cb27abcbab32c5ced917fad52565c7 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Tue, 23 Jul 2024 09:19:04 +0000 Subject: [PATCH 06/16] zombienet/0001-basic-warp-sync: Extend checks for genesis leaf Signed-off-by: Alexandru Vasile --- .gitlab/pipeline/zombienet/substrate.yml | 9 +++++++++ .../zombienet/0001-basic-warp-sync/test-warp-sync.toml | 2 +- .../zombienet/0001-basic-warp-sync/test-warp-sync.zndsl | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.gitlab/pipeline/zombienet/substrate.yml b/.gitlab/pipeline/zombienet/substrate.yml index 2013ffd571cf3..6226d0c237b74 100644 --- a/.gitlab/pipeline/zombienet/substrate.yml +++ b/.gitlab/pipeline/zombienet/substrate.yml @@ -51,6 +51,15 @@ # After the issue is fixed, we should replace it with a pruned version of the DB. DB_SNAPSHOT: "https://storage.googleapis.com/zombienet-db-snaps/substrate/0001-basic-warp-sync/chains-9677807d738b951e9f6c82e5fd15518eb0ae0419.tgz" DB_BLOCK_HEIGHT: 56687 + # The genesis hash of the snapshot. This can be determied by running the node over the snapshot using the following env variable for logs `RUST_LOG=state-db=trace". + # + # The logs will contain the genesis hash of the snapshot, similar to: + # + # ``` + # INFO main sc_service::client::client: 🔨 Initializing Genesis block/state (state: 0x971d…f0c1, header-hash: 0xb3fe…356c) + # TRACE main state-db: Inserted uncanonicalized changeset 0.0 0xb3fe8367d5b079908c9f72cc43cd28f6fa50665e0137fac5fc1050ead4b8356c (248 inserted, 0 deleted) + # ``` + DB_GENESIS_HASH: "0xb3fe8367d5b079908c9f72cc43cd28f6fa50665e0137fac5fc1050ead4b8356c" zombienet-substrate-0000-block-building: extends: diff --git a/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.toml b/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.toml index f4586ba69d00c..d40da6d643704 100644 --- a/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.toml +++ b/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.toml @@ -27,4 +27,4 @@ chain_spec_path = "chain-spec.json" [[relaychain.nodes]] name = "dave" validator = false - args = ["--sync warp"] + args = ["--sync warp -ldb::blockchain"] diff --git a/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl b/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl index c5644797321e1..e7b9b49ce17dc 100644 --- a/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl +++ b/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl @@ -25,6 +25,7 @@ dave: log line matches "Warp sync is complete" within 60 seconds # State sync is logically part of warp sync dave: log line matches "State sync is complete" within 60 seconds dave: log line matches "Block history download is complete" within 10 seconds +dave: log line matches "Checking for displaced leaves after finalization. leaves=[0xb3fe8367d5b079908c9f72cc43cd28f6fa50665e0137fac5fc1050ead4b8356c]" within 10 seconds dave: count of log lines containing "error" is 0 within 10 seconds dave: count of log lines containing "verification failed" is 0 within 10 seconds From 275410d09d76db057053eac5606a7e9350deb872 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Tue, 23 Jul 2024 10:37:18 +0000 Subject: [PATCH 07/16] zombienet/0001-basic-warp-sync: Expect no genesis leaf Signed-off-by: Alexandru Vasile --- substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl b/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl index e7b9b49ce17dc..1934a29eb7da7 100644 --- a/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl +++ b/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl @@ -25,7 +25,7 @@ dave: log line matches "Warp sync is complete" within 60 seconds # State sync is logically part of warp sync dave: log line matches "State sync is complete" within 60 seconds dave: log line matches "Block history download is complete" within 10 seconds -dave: log line matches "Checking for displaced leaves after finalization. leaves=[0xb3fe8367d5b079908c9f72cc43cd28f6fa50665e0137fac5fc1050ead4b8356c]" within 10 seconds +dave: log line matches "Checking for displaced leaves after finalization. leaves=[]" within 10 seconds dave: count of log lines containing "error" is 0 within 10 seconds dave: count of log lines containing "verification failed" is 0 within 10 seconds From 1a507e7afeb42203b361d247a7dc8e81e93d552d Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Tue, 23 Jul 2024 12:15:10 +0000 Subject: [PATCH 08/16] blockchain/backend: Skip genesis leaf entirely Signed-off-by: Alexandru Vasile --- substrate/primitives/blockchain/src/backend.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/substrate/primitives/blockchain/src/backend.rs b/substrate/primitives/blockchain/src/backend.rs index cb6e5776c41a7..749acadd65cca 100644 --- a/substrate/primitives/blockchain/src/backend.rs +++ b/substrate/primitives/blockchain/src/backend.rs @@ -253,7 +253,9 @@ pub trait Backend: finalized_block_hash: Block::Hash, finalized_block_number: NumberFor, ) -> std::result::Result, Error> { - let leaves = self.leaves()?; + // The genesis block is never displaced, as it is part of the canonical chain. + let genesis_hash = self.info().genesis_hash; + let leaves = self.leaves()?.into_iter().filter(|h| *h != genesis_hash).collect::>(); let now = std::time::Instant::now(); debug!( From b09ec22f2577d1fb617bfc84412f2b1e7c1e2a0d Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Tue, 23 Jul 2024 12:23:52 +0000 Subject: [PATCH 09/16] Add prdoc Signed-off-by: Alexandru Vasile --- prdoc/pr_5103.prdoc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 prdoc/pr_5103.prdoc diff --git a/prdoc/pr_5103.prdoc b/prdoc/pr_5103.prdoc new file mode 100644 index 0000000000000..b0f72bf531f18 --- /dev/null +++ b/prdoc/pr_5103.prdoc @@ -0,0 +1,18 @@ +title: Skip genesis leaf to unblock syncing + +doc: + - audience: + - Node Operator + - Node Dev + description: | + This PR skips over the genesis block reported as leaf when calculating displaced branches. + In those cases, when the genesis block is reported as leaf, the node would compute the path + from the current finalized block to the genesis block. This operation is time consuming and + is enough to block syncing. In the current state, the genesis block is assumed to always be + part of the finalized chain. + +crates: +- name: sc-client-db + bump: none +- name: sp-blockchain + bump: patch From a6fe9325d945148359b56e4be4a95fa3544290b6 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:58:37 +0300 Subject: [PATCH 10/16] Update substrate/primitives/blockchain/src/backend.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- substrate/primitives/blockchain/src/backend.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/substrate/primitives/blockchain/src/backend.rs b/substrate/primitives/blockchain/src/backend.rs index 749acadd65cca..ee84b5f0404de 100644 --- a/substrate/primitives/blockchain/src/backend.rs +++ b/substrate/primitives/blockchain/src/backend.rs @@ -326,6 +326,7 @@ pub trait Backend: debug!( target: crate::LOG_TARGET, ?leaf_number, + ?leaf_hash, elapsed = ?now.elapsed(), "Handle displaced leaf.", ); From 606483348711e6f9a5de1d59731ca648b5095739 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:58:55 +0300 Subject: [PATCH 11/16] Update substrate/primitives/blockchain/src/backend.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- substrate/primitives/blockchain/src/backend.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/substrate/primitives/blockchain/src/backend.rs b/substrate/primitives/blockchain/src/backend.rs index ee84b5f0404de..951968f3392c4 100644 --- a/substrate/primitives/blockchain/src/backend.rs +++ b/substrate/primitives/blockchain/src/backend.rs @@ -348,6 +348,7 @@ pub trait Backend: target: crate::LOG_TARGET, ?err, ?parent_hash, + ?leaf_hash, elapsed = ?now.elapsed(), "Failed to fetch parent header during leaf tracking.", ); From 69df716b4fa755e755124a04cade2f284191054e Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Tue, 23 Jul 2024 14:18:40 +0000 Subject: [PATCH 12/16] Displace leaf if it is genesis Signed-off-by: Alexandru Vasile --- substrate/primitives/blockchain/src/backend.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/substrate/primitives/blockchain/src/backend.rs b/substrate/primitives/blockchain/src/backend.rs index 951968f3392c4..c148708c1e44f 100644 --- a/substrate/primitives/blockchain/src/backend.rs +++ b/substrate/primitives/blockchain/src/backend.rs @@ -253,9 +253,7 @@ pub trait Backend: finalized_block_hash: Block::Hash, finalized_block_number: NumberFor, ) -> std::result::Result, Error> { - // The genesis block is never displaced, as it is part of the canonical chain. - let genesis_hash = self.info().genesis_hash; - let leaves = self.leaves()?.into_iter().filter(|h| *h != genesis_hash).collect::>(); + let leaves = self.leaves()?; let now = std::time::Instant::now(); debug!( @@ -323,6 +321,19 @@ pub trait Backend: })?); let leaf_number = current_header_metadata.number; + // The genesis block is part of the canonical chain. + let genesis_hash = self.info().genesis_hash; + if leaf_hash == genesis_hash { + result.displaced_leaves.push((leaf_number, leaf_hash)); + debug!( + target: crate::LOG_TARGET, + ?leaf_hash, + elapsed = ?now.elapsed(), + "Added genesis leaf to displaced leaves." + ); + continue + } + debug!( target: crate::LOG_TARGET, ?leaf_number, From 0442a19064004064f6b75b63247051e059d66eb7 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> Date: Tue, 23 Jul 2024 18:20:47 +0300 Subject: [PATCH 13/16] Update substrate/primitives/blockchain/src/backend.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- substrate/primitives/blockchain/src/backend.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/substrate/primitives/blockchain/src/backend.rs b/substrate/primitives/blockchain/src/backend.rs index c148708c1e44f..4f31b327fb1c3 100644 --- a/substrate/primitives/blockchain/src/backend.rs +++ b/substrate/primitives/blockchain/src/backend.rs @@ -322,7 +322,6 @@ pub trait Backend: let leaf_number = current_header_metadata.number; // The genesis block is part of the canonical chain. - let genesis_hash = self.info().genesis_hash; if leaf_hash == genesis_hash { result.displaced_leaves.push((leaf_number, leaf_hash)); debug!( From 848df8ffc8cfd1d3a697408fa6b7737437fdf4f6 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> Date: Tue, 23 Jul 2024 18:20:55 +0300 Subject: [PATCH 14/16] Update substrate/primitives/blockchain/src/backend.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- substrate/primitives/blockchain/src/backend.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/substrate/primitives/blockchain/src/backend.rs b/substrate/primitives/blockchain/src/backend.rs index 4f31b327fb1c3..fd0c5795cbfd2 100644 --- a/substrate/primitives/blockchain/src/backend.rs +++ b/substrate/primitives/blockchain/src/backend.rs @@ -307,6 +307,8 @@ pub trait Backend: let mut displaced_blocks_candidates = Vec::new(); + let genesis_hash = self.info().genesis_hash; + for leaf_hash in leaves { let mut current_header_metadata = MinimalBlockMetadata::from(&self.header_metadata(leaf_hash).map_err(|err| { From 21c7a108697a525ba8deb49dfb8ab7b41ac99678 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Tue, 23 Jul 2024 15:33:20 +0000 Subject: [PATCH 15/16] db/tests: Fix tests to include genesis in displaced Signed-off-by: Alexandru Vasile --- substrate/client/db/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/substrate/client/db/src/lib.rs b/substrate/client/db/src/lib.rs index acd165d916135..ba0cbc09d53da 100644 --- a/substrate/client/db/src/lib.rs +++ b/substrate/client/db/src/lib.rs @@ -3169,7 +3169,7 @@ pub(crate) mod tests { let displaced = blockchain.displaced_leaves_after_finalizing(a3_hash, a3_number).unwrap(); assert_eq!(blockchain.leaves().unwrap(), vec![a4_hash, genesis_hash]); - assert_eq!(displaced.displaced_leaves, vec![]); + assert_eq!(displaced.displaced_leaves, vec![(genesis_number, genesis_hash)]); assert_eq!(displaced.displaced_blocks, vec![]); } @@ -3177,7 +3177,7 @@ pub(crate) mod tests { let displaced = blockchain.displaced_leaves_after_finalizing(a4_hash, a4_number).unwrap(); assert_eq!(blockchain.leaves().unwrap(), vec![a4_hash, genesis_hash]); - assert_eq!(displaced.displaced_leaves, vec![]); + assert_eq!(displaced.displaced_leaves, vec![(genesis_number, genesis_hash)]); assert_eq!(displaced.displaced_blocks, vec![]); } From a04f6d21e0cb871d5146731cb9e4ca84a642d7b9 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Tue, 23 Jul 2024 18:27:37 +0000 Subject: [PATCH 16/16] zombinet/test: Fix zombinet test by escaping regex chars Signed-off-by: Alexandru Vasile --- .gitlab/pipeline/zombienet/substrate.yml | 9 --------- .../zombienet/0001-basic-warp-sync/test-warp-sync.zndsl | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/.gitlab/pipeline/zombienet/substrate.yml b/.gitlab/pipeline/zombienet/substrate.yml index 6226d0c237b74..2013ffd571cf3 100644 --- a/.gitlab/pipeline/zombienet/substrate.yml +++ b/.gitlab/pipeline/zombienet/substrate.yml @@ -51,15 +51,6 @@ # After the issue is fixed, we should replace it with a pruned version of the DB. DB_SNAPSHOT: "https://storage.googleapis.com/zombienet-db-snaps/substrate/0001-basic-warp-sync/chains-9677807d738b951e9f6c82e5fd15518eb0ae0419.tgz" DB_BLOCK_HEIGHT: 56687 - # The genesis hash of the snapshot. This can be determied by running the node over the snapshot using the following env variable for logs `RUST_LOG=state-db=trace". - # - # The logs will contain the genesis hash of the snapshot, similar to: - # - # ``` - # INFO main sc_service::client::client: 🔨 Initializing Genesis block/state (state: 0x971d…f0c1, header-hash: 0xb3fe…356c) - # TRACE main state-db: Inserted uncanonicalized changeset 0.0 0xb3fe8367d5b079908c9f72cc43cd28f6fa50665e0137fac5fc1050ead4b8356c (248 inserted, 0 deleted) - # ``` - DB_GENESIS_HASH: "0xb3fe8367d5b079908c9f72cc43cd28f6fa50665e0137fac5fc1050ead4b8356c" zombienet-substrate-0000-block-building: extends: diff --git a/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl b/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl index 1934a29eb7da7..26c9fac60735d 100644 --- a/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl +++ b/substrate/zombienet/0001-basic-warp-sync/test-warp-sync.zndsl @@ -22,10 +22,10 @@ dave: reports block height is at least 1 within 60 seconds dave: reports block height is at least {{DB_BLOCK_HEIGHT}} within 60 seconds dave: log line matches "Warp sync is complete" within 60 seconds +dave: log line matches "Checking for displaced leaves after finalization\. leaves\=\[0xc5e7b4cfd23932bb930e859865430a35f6741b4732d677822d492ca64cc8d059\]" within 10 seconds # State sync is logically part of warp sync dave: log line matches "State sync is complete" within 60 seconds dave: log line matches "Block history download is complete" within 10 seconds -dave: log line matches "Checking for displaced leaves after finalization. leaves=[]" within 10 seconds dave: count of log lines containing "error" is 0 within 10 seconds dave: count of log lines containing "verification failed" is 0 within 10 seconds