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

cumulus-consensus-common: block import: delayed_best_block flag added #2001

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
38 changes: 28 additions & 10 deletions cumulus/client/consensus/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ impl<B: BlockT> ParachainConsensus<B> for Box<dyn ParachainConsensus<B> + Send +

/// Parachain specific block import.
///
/// This is used to set `block_import_params.fork_choice` to `false` as long as the block origin is
/// not `NetworkInitialSync`. The best block for parachains is determined by the relay chain.
/// Meaning we will update the best block, as it is included by the relay-chain.
/// If `update_fork_choice` is `true` this is used to set `block_import_params.fork_choice` to
/// `false` as long as the block origin is not `NetworkInitialSync`. The best block for parachains
/// is determined by the relay chain. Meaning we will update the best block, as it is included by
/// the relay-chain.
michalkucharczyk marked this conversation as resolved.
Show resolved Hide resolved
pub struct ParachainBlockImport<Block: BlockT, BI, BE> {
inner: BI,
monitor: Option<SharedData<LevelMonitor<Block, BE>>>,
update_fork_choice: bool,
michalkucharczyk marked this conversation as resolved.
Show resolved Hide resolved
}

impl<Block: BlockT, BI, BE: Backend<Block>> ParachainBlockImport<Block, BI, BE> {
Expand All @@ -141,13 +143,27 @@ impl<Block: BlockT, BI, BE: Backend<Block>> ParachainBlockImport<Block, BI, BE>
let monitor =
level_limit.map(|level_limit| SharedData::new(LevelMonitor::new(level_limit, backend)));

Self { inner, monitor }
Self { inner, monitor, update_fork_choice: false }
}

/// Create a new instance which allows to update `params.fork_choice`.
///
/// The number of leaves per level limit is set to `LevelLimit::Default`.
pub fn new_with_update_fork_choice_enabled(inner: BI, backend: Arc<BE>) -> Self {
michalkucharczyk marked this conversation as resolved.
Show resolved Hide resolved
Self {
update_fork_choice: true,
..Self::new_with_limit(inner, backend, LevelLimit::Default)
}
}
}

impl<Block: BlockT, I: Clone, BE> Clone for ParachainBlockImport<Block, I, BE> {
fn clone(&self) -> Self {
ParachainBlockImport { inner: self.inner.clone(), monitor: self.monitor.clone() }
ParachainBlockImport {
inner: self.inner.clone(),
monitor: self.monitor.clone(),
update_fork_choice: self.update_fork_choice,
}
}
}

Expand Down Expand Up @@ -182,11 +198,13 @@ where
params.finalized = true;
}

// Best block is determined by the relay chain, or if we are doing the initial sync
// we import all blocks as new best.
params.fork_choice = Some(sc_consensus::ForkChoiceStrategy::Custom(
params.origin == sp_consensus::BlockOrigin::NetworkInitialSync,
));
if self.update_fork_choice {
// Best block is determined by the relay chain, or if we are doing the initial sync
// we import all blocks as new best.
params.fork_choice = Some(sc_consensus::ForkChoiceStrategy::Custom(
params.origin == sp_consensus::BlockOrigin::NetworkInitialSync,
));
}

let maybe_lock = self.monitor.as_ref().map(|monitor_lock| {
let mut monitor = monitor_lock.shared_data_locked();
Expand Down
6 changes: 4 additions & 2 deletions cumulus/client/consensus/common/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,8 @@ fn find_potential_parents_aligned_with_pending() {

let backend = Arc::new(Backend::new_test(1000, 1));
let client = Arc::new(TestClientBuilder::with_backend(backend.clone()).build());
let mut para_import = ParachainBlockImport::new(client.clone(), backend.clone());
let mut para_import =
ParachainBlockImport::new_with_update_fork_choice_enabled(client.clone(), backend.clone());

let relay_parent = relay_hash_from_block_num(10);
// Choose different relay parent for alternative chain to get new hashes.
Expand Down Expand Up @@ -1279,7 +1280,8 @@ fn find_potential_parents_aligned_no_pending() {

let backend = Arc::new(Backend::new_test(1000, 1));
let client = Arc::new(TestClientBuilder::with_backend(backend.clone()).build());
let mut para_import = ParachainBlockImport::new(client.clone(), backend.clone());
let mut para_import =
ParachainBlockImport::new_with_update_fork_choice_enabled(client.clone(), backend.clone());

let relay_parent = relay_hash_from_block_num(10);
// Choose different relay parent for alternative chain to get new hashes.
Expand Down
Loading