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

Support block gap created by fast sync #5703

Merged
merged 10 commits into from
Nov 20, 2024
Prev Previous commit
Next Next commit
refactor: update insert_new_gap() to accept block_gap
liuchengxu committed Sep 15, 2024
commit 917c1538a8177cabe3dac52d9886944a3d471227
36 changes: 17 additions & 19 deletions substrate/client/db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1683,15 +1683,18 @@ impl<Block: BlockT> Backend<Block> {
let should_check_block_gap = !existing_header || !body_exists;

if should_check_block_gap {
let insert_new_gap = |transaction: &mut Transaction<DbHash>,
gap: BlockGap<NumberFor<Block>>| {
transaction.set(columns::META, meta_keys::BLOCK_GAP, &gap.encode());
transaction.set(
columns::META,
meta_keys::BLOCK_GAP_VERSION,
&BLOCK_GAP_CURRENT_VERSION.encode(),
);
};
let insert_new_gap =
|transaction: &mut Transaction<DbHash>,
new_gap: BlockGap<NumberFor<Block>>,
block_gap: &mut Option<BlockGap<NumberFor<Block>>>| {
transaction.set(columns::META, meta_keys::BLOCK_GAP, &new_gap.encode());
transaction.set(
columns::META,
meta_keys::BLOCK_GAP_VERSION,
&BLOCK_GAP_CURRENT_VERSION.encode(),
);
block_gap.replace(new_gap);
};

if let Some(mut gap) = block_gap {
match gap.gap_type {
@@ -1710,8 +1713,7 @@ impl<Block: BlockT> Backend<Block> {
block_gap = None;
debug!(target: "db", "Removed block gap.");
} else {
insert_new_gap(&mut transaction, gap);
block_gap = Some(gap);
insert_new_gap(&mut transaction, gap, &mut block_gap);
debug!(target: "db", "Update block gap. {block_gap:?}");
}
block_gap_updated = true;
@@ -1726,8 +1728,7 @@ impl<Block: BlockT> Backend<Block> {
number,
hash,
)?;
insert_new_gap(&mut transaction, gap);
block_gap = Some(gap);
insert_new_gap(&mut transaction, gap, &mut block_gap);
debug!(target: "db", "Update block gap. {block_gap:?}");
block_gap_updated = true;
// Gap decreased when downloading the full blocks.
@@ -1739,8 +1740,7 @@ impl<Block: BlockT> Backend<Block> {
block_gap = None;
debug!(target: "db", "Removed block gap.");
} else {
insert_new_gap(&mut transaction, gap);
block_gap = Some(gap);
insert_new_gap(&mut transaction, gap, &mut block_gap);
debug!(target: "db", "Update block gap. {block_gap:?}");
}
block_gap_updated = true;
@@ -1756,8 +1756,7 @@ impl<Block: BlockT> Backend<Block> {
end: number - One::one(),
gap_type: BlockGapType::MissingHeaderAndBody,
};
insert_new_gap(&mut transaction, gap);
block_gap = Some(gap);
insert_new_gap(&mut transaction, gap, &mut block_gap);
block_gap_updated = true;
debug!(target: "db", "Detected block gap (warp sync) {block_gap:?}");
} else if number == best_num + One::one() &&
@@ -1769,8 +1768,7 @@ impl<Block: BlockT> Backend<Block> {
end: number,
gap_type: BlockGapType::MissingBody,
};
insert_new_gap(&mut transaction, gap);
block_gap = Some(gap);
insert_new_gap(&mut transaction, gap, &mut block_gap);
block_gap_updated = true;
debug!(target: "db", "Detected block gap (fast sync) {block_gap:?}");
}