Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix: blockchain/backend: Skip genesis leaf to unblock syncing #5103

Merged
merged 18 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitlab/pipeline/zombienet/substrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions prdoc/pr_5103.prdoc
Original file line number Diff line number Diff line change
@@ -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
143 changes: 134 additions & 9 deletions substrate/primitives/blockchain/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ pub trait Backend<Block: BlockT>:
) -> std::result::Result<DisplacedLeavesAfterFinalization<Block>, Error> {
let leaves = self.leaves()?;

let now = std::time::Instant::now();
debug!(
target: crate::LOG_TARGET,
?leaves,
%finalized_block_hash,
?finalized_block_hash,
?finalized_block_number,
"Checking for displaced leaves after finalization."
);
Expand All @@ -277,11 +278,21 @@ pub trait Backend<Block: BlockT>:
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());
},
Err(e) => Err(e)?,
Err(e) => {
debug!(
target: crate::LOG_TARGET,
hash = ?finalized_block_hash,
err = ?e,
elapsed = ?now.elapsed(),
"Failed to fetch block.",
);
return Err(e);
},
};
finalized_chain.push_front(MinimalBlockMetadata::from(&current_finalized));

Expand All @@ -298,9 +309,39 @@ pub trait Backend<Block: BlockT>:

for leaf_hash in leaves {
lexnv marked this conversation as resolved.
Show resolved Hide resolved
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,
?leaf_hash,
?err,
elapsed = ?now.elapsed(),
"Failed to fetch leaf header.",
);
err
})?);
let leaf_number = current_header_metadata.number;

// The genesis block is part of the canonical chain.
let genesis_hash = self.info().genesis_hash;
lexnv marked this conversation as resolved.
Show resolved Hide resolved
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,
lexnv marked this conversation as resolved.
Show resolved Hide resolved
?leaf_hash,
elapsed = ?now.elapsed(),
"Handle displaced leaf.",
);

// Collect all block hashes until the height of the finalized block
displaced_blocks_candidates.clear();
while current_header_metadata.number > finalized_block_number {
Expand All @@ -312,8 +353,20 @@ pub trait Backend<Block: BlockT>:
current_header_metadata = *metadata_header;
},
None => {
current_header_metadata =
MinimalBlockMetadata::from(&self.header_metadata(parent_hash)?);
current_header_metadata = MinimalBlockMetadata::from(
&self.header_metadata(parent_hash).map_err(|err| {
debug!(
target: crate::LOG_TARGET,
?err,
?parent_hash,
lexnv marked this conversation as resolved.
Show resolved Hide resolved
?leaf_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);
Expand All @@ -324,6 +377,13 @@ pub trait Backend<Block: BlockT>:
// 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_hash,
elapsed = ?now.elapsed(),
"Leaf points to the finalized header, skipping for now.",
);

continue;
}

Expand All @@ -332,6 +392,15 @@ pub trait Backend<Block: BlockT>:
// 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,
elapsed = ?now.elapsed(),
"Looking for path from finalized block number to current leaf number"
);

// 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
Expand All @@ -348,11 +417,22 @@ pub trait Backend<Block: BlockT>:
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(e)?,
Err(err) => {
debug!(
target: crate::LOG_TARGET,
hash = ?to_fetch.parent,
number = ?to_fetch.number,
?err,
elapsed = ?now.elapsed(),
"Failed to fetch header for parent hash.",
);
return Err(err);
},
};
let metadata = MinimalBlockMetadata::from(&metadata);
let result = (metadata.number, metadata.hash);
Expand All @@ -361,6 +441,19 @@ pub trait Backend<Block: BlockT>:
},
};

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
Expand All @@ -372,20 +465,52 @@ pub trait Backend<Block: BlockT>:
// 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,
?leaf_hash,
elapsed = ?now.elapsed(),
"Found displaced leaf."
);
break;
}

// Store displaced block and look deeper for block on finalized chain
debug!(
target: crate::LOG_TARGET,
?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)?);
current_header_metadata = MinimalBlockMetadata::from(
&self.header_metadata(parent_hash).map_err(|err| {
debug!(
target: crate::LOG_TARGET,
?err,
?parent_hash,
elapsed = ?now.elapsed(),
"Failed to fetch header for parent during displaced block collection",
);
err
})?,
);
}
}

// There could be duplicates shared by multiple branches, clean them up
result.displaced_blocks.sort_unstable();
result.displaced_blocks.dedup();

debug!(
target: crate::LOG_TARGET,
%finalized_block_hash,
?finalized_block_number,
?result,
elapsed = ?now.elapsed(),
"Finished checking for displaced leaves after finalization.",
);

return Ok(result);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ chain_spec_path = "chain-spec.json"
[[relaychain.nodes]]
name = "dave"
validator = false
args = ["--sync warp"]
args = ["--sync warp -ldb::blockchain"]
Original file line number Diff line number Diff line change
Expand Up @@ -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=[]" 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
Loading