Finalize target block after warp sync (#2696)

* Finalize state after warp sync

* Update cargo.lock

* Apply code review suggestion

Co-authored-by: Sebastian Kunert <skunert49@gmail.com>

* Update lock

---------

Co-authored-by: Sebastian Kunert <skunert49@gmail.com>
This commit is contained in:
Davide Galassi
2023-06-07 13:34:20 +02:00
committed by GitHub
parent cab52dc1f4
commit 3135baf75e
3 changed files with 209 additions and 199 deletions
+193 -193
View File
File diff suppressed because it is too large Load Diff
@@ -362,14 +362,18 @@ where
/// Add a new imported block information to the monitor. /// Add a new imported block information to the monitor.
pub fn block_imported(&mut self, number: NumberFor<Block>, hash: Block::Hash) { pub fn block_imported(&mut self, number: NumberFor<Block>, hash: Block::Hash) {
let finalized_num = self.backend.blockchain().info().finalized_number;
if number > finalized_num {
// Only blocks above the last finalized block should be added to the monitor
self.import_counter += One::one(); self.import_counter += One::one();
self.freshness.insert(hash, self.import_counter); self.freshness.insert(hash, self.import_counter);
self.levels.entry(number).or_default().insert(hash); self.levels.entry(number).or_default().insert(hash);
}
// Do cleanup once in a while, we are allowed to have some obsolete information.
let finalized_num = self.backend.blockchain().info().finalized_number;
let delta: u32 = finalized_num.saturating_sub(self.lowest_level).unique_saturated_into(); let delta: u32 = finalized_num.saturating_sub(self.lowest_level).unique_saturated_into();
if delta >= CLEANUP_THRESHOLD { if delta >= CLEANUP_THRESHOLD {
// Do cleanup once in a while, we are allowed to have some obsolete information.
for i in 0..delta { for i in 0..delta {
let number = self.lowest_level + i.unique_saturated_into(); let number = self.lowest_level + i.unique_saturated_into();
self.levels.remove(&number).map(|level| { self.levels.remove(&number).map(|level| {
@@ -378,7 +382,6 @@ where
}) })
}); });
} }
self.lowest_level = finalized_num; self.lowest_level = finalized_num;
} }
} }
@@ -147,6 +147,13 @@ where
let hash = params.post_hash(); let hash = params.post_hash();
let number = *params.header.number(); let number = *params.header.number();
if params.with_state() {
// Force imported state finality.
// Required for warp sync. We assume that preconditions have been
// checked properly and we are importing a finalized block with state.
params.finalized = true;
}
// Best block is determined by the relay chain, or if we are doing the initial sync // Best block is determined by the relay chain, or if we are doing the initial sync
// we import all blocks as new best. // we import all blocks as new best.
params.fork_choice = Some(sc_consensus::ForkChoiceStrategy::Custom( params.fork_choice = Some(sc_consensus::ForkChoiceStrategy::Custom(