Skip to content

Commit b25d29a

Browse files
cumulus-consensus-common: block import: delayed_best_block flag added (#2001)
This PR adds the `delayed_best_block` flag to `ParachainBlockImport`. If not set, the `params.fork_choice` is not updated (to `ForkChoiceStrategy::Custom`) during the block import. When `delayed_best_block` is set to `false` all parachain blocks on the [longest fork](https://github.com/paritytech/polkadot-sdk/blob/552be4800d9e4b480f79a300fc531783e04be099/substrate/client/service/src/client/client.rs#L708-L709) will be notified as the best block, allowing transaction pool to be updated with every imported block. Otherwise imported blocks will not be notified as best blocks (`fork_choice=ForkChoiceStrategy::Custom(false)`), and transaction pool would be updated only with best block received from relay-chain. Improvement for: #1202 --------- Co-authored-by: Bastian Köcher <[email protected]>
1 parent 552be48 commit b25d29a

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

cumulus/client/consensus/common/src/lib.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,15 @@ impl<B: BlockT> ParachainConsensus<B> for Box<dyn ParachainConsensus<B> + Send +
111111

112112
/// Parachain specific block import.
113113
///
114-
/// This is used to set `block_import_params.fork_choice` to `false` as long as the block origin is
115-
/// not `NetworkInitialSync`. The best block for parachains is determined by the relay chain.
116-
/// Meaning we will update the best block, as it is included by the relay-chain.
114+
/// Specialized block import for parachains. It supports to delay setting the best block until the
115+
/// relay chain has included a candidate in its best block. By default the delayed best block
116+
/// setting is disabled. The block import also monitors the imported blocks and prunes by default if
117+
/// there are too many blocks at the same height. Too many blocks at the same height can for example
118+
/// happen if the relay chain is rejecting the parachain blocks in the validation.
117119
pub struct ParachainBlockImport<Block: BlockT, BI, BE> {
118120
inner: BI,
119121
monitor: Option<SharedData<LevelMonitor<Block, BE>>>,
122+
delayed_best_block: bool,
120123
}
121124

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

144-
Self { inner, monitor }
147+
Self { inner, monitor, delayed_best_block: false }
148+
}
149+
150+
/// Create a new instance which delays setting the best block.
151+
///
152+
/// The number of leaves per level limit is set to `LevelLimit::Default`.
153+
pub fn new_with_delayed_best_block(inner: BI, backend: Arc<BE>) -> Self {
154+
Self {
155+
delayed_best_block: true,
156+
..Self::new_with_limit(inner, backend, LevelLimit::Default)
157+
}
145158
}
146159
}
147160

148161
impl<Block: BlockT, I: Clone, BE> Clone for ParachainBlockImport<Block, I, BE> {
149162
fn clone(&self) -> Self {
150-
ParachainBlockImport { inner: self.inner.clone(), monitor: self.monitor.clone() }
163+
ParachainBlockImport {
164+
inner: self.inner.clone(),
165+
monitor: self.monitor.clone(),
166+
delayed_best_block: self.delayed_best_block,
167+
}
151168
}
152169
}
153170

@@ -182,11 +199,13 @@ where
182199
params.finalized = true;
183200
}
184201

185-
// Best block is determined by the relay chain, or if we are doing the initial sync
186-
// we import all blocks as new best.
187-
params.fork_choice = Some(sc_consensus::ForkChoiceStrategy::Custom(
188-
params.origin == sp_consensus::BlockOrigin::NetworkInitialSync,
189-
));
202+
if self.delayed_best_block {
203+
// Best block is determined by the relay chain, or if we are doing the initial sync
204+
// we import all blocks as new best.
205+
params.fork_choice = Some(sc_consensus::ForkChoiceStrategy::Custom(
206+
params.origin == sp_consensus::BlockOrigin::NetworkInitialSync,
207+
));
208+
}
190209

191210
let maybe_lock = self.monitor.as_ref().map(|monitor_lock| {
192211
let mut monitor = monitor_lock.shared_data_locked();

cumulus/client/consensus/common/src/tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,8 @@ fn find_potential_parents_aligned_with_pending() {
11241124

11251125
let backend = Arc::new(Backend::new_test(1000, 1));
11261126
let client = Arc::new(TestClientBuilder::with_backend(backend.clone()).build());
1127-
let mut para_import = ParachainBlockImport::new(client.clone(), backend.clone());
1127+
let mut para_import =
1128+
ParachainBlockImport::new_with_delayed_best_block(client.clone(), backend.clone());
11281129

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

12801281
let backend = Arc::new(Backend::new_test(1000, 1));
12811282
let client = Arc::new(TestClientBuilder::with_backend(backend.clone()).build());
1282-
let mut para_import = ParachainBlockImport::new(client.clone(), backend.clone());
1283+
let mut para_import =
1284+
ParachainBlockImport::new_with_delayed_best_block(client.clone(), backend.clone());
12831285

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

0 commit comments

Comments
 (0)