Skip to content

Commit

Permalink
Limit block production to one block per slot in basic collator (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusrodri authored Mar 13, 2024
1 parent bde5ab3 commit 8e29a2c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions client/consensus/src/collators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ where
pub struct SlotClaim<Pub> {
author_pub: Pub,
pre_digest: Vec<DigestItem>,
slot: Slot,
}

impl<Pub: Clone> SlotClaim<Pub> {
Expand All @@ -276,6 +277,7 @@ impl<Pub: Clone> SlotClaim<Pub> {
SlotClaim {
author_pub: author_pub.clone(),
pre_digest: pre_digest_data::<P>(slot, author_pub),
slot,
}
}

Expand All @@ -288,6 +290,11 @@ impl<Pub: Clone> SlotClaim<Pub> {
pub fn pre_digest(&self) -> &Vec<DigestItem> {
&self.pre_digest
}

/// Get the slot assigned to this claim.
pub fn slot(&self) -> Slot {
self.slot
}
}

/// Attempt to claim a slot locally.
Expand Down
15 changes: 15 additions & 0 deletions client/consensus/src/collators/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ pub async fn run<Block, P, BI, CIDP, Client, RClient, SO, Proposer, CS, GOH>(
collator_util::Collator::<Block, P, _, _, _, _, _>::new(params)
};

let mut last_processed_slot = 0;

while let Some(request) = collation_requests.next().await {
macro_rules! reject_with_error {
($err:expr) => {{
Expand Down Expand Up @@ -211,6 +213,18 @@ pub async fn run<Block, P, BI, CIDP, Client, RClient, SO, Proposer, CS, GOH>(
Ok(Some(h)) => h,
};

// With async backing this function will be called every relay chain block.
//
// Most parachains currently run with 12 seconds slots and thus, they would try to
// produce multiple blocks per slot which very likely would fail on chain. Thus, we have
// this "hack" to only produce on block per slot.
//
// With https://github.com/paritytech/polkadot-sdk/issues/3168 this implementation will be
// obsolete and also the underlying issue will be fixed.
if last_processed_slot >= *claim.slot() {
continue;
}

let (parachain_inherent_data, other_inherent_data) = try_request!(
collator
.create_inherent_data(*request.relay_parent(), validation_data, parent_hash, None,)
Expand Down Expand Up @@ -244,5 +258,6 @@ pub async fn run<Block, P, BI, CIDP, Client, RClient, SO, Proposer, CS, GOH>(
request.complete(None);
tracing::debug!(target: crate::LOG_TARGET, "No block proposal");
}
last_processed_slot = *claim.slot();
}
}

0 comments on commit 8e29a2c

Please sign in to comment.