-
Notifications
You must be signed in to change notification settings - Fork 1.1k
aura/slot_based: Reduce authoring duration of the last produced block #10154
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
base: lexnv/es-westend
Are you sure you want to change the base?
Changes from 16 commits
000df20
3b0b715
34d6b7a
9c19866
774871b
cccae18
e5fa9f6
e48868e
a82895f
7a21bad
da80cea
190a4ac
0cd8b63
aa01b93
4851062
c943bdc
32e1235
624909f
a25b927
6569846
6a47b55
3538f0d
509b22a
0c7f02e
2106f44
dfcf0f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -37,6 +37,24 @@ use std::{ | |||||
| /// Defensive mechanism, corresponds to 12 cores at 6 second block time. | ||||||
| const BLOCK_PRODUCTION_MINIMUM_INTERVAL_MS: Duration = Duration::from_millis(500); | ||||||
|
|
||||||
| /// Theoretically, the block production is capped at `BLOCK_PRODUCTION_MINIMUM_INTERVAL_MS`. | ||||||
| /// In practice, there might be slight deviations due to timing inaccuracies and delays. | ||||||
| /// | ||||||
| /// This constant is taken into account while adjusting the authoring duration to fit into the slot. | ||||||
| /// Therefore, it will only reduce the authoring duration if we are within the | ||||||
| /// `BLOCK_PRODUCTION_ADJUSTMENT_MS` threshold of the next slot. | ||||||
| /// | ||||||
| /// ### 12 cores 500ms blocks | ||||||
| /// | ||||||
| /// For example, for 12 cores 500ms blocks: the next slot is scheduled in 490ms due to delays. | ||||||
| /// In that case, we still want to attempt producing the block, as missing the slot would be worse | ||||||
| /// than producing slightly too fast. | ||||||
| const BLOCK_PRODUCTION_THRESHOLD_MS: Duration = Duration::from_millis(50); | ||||||
|
|
||||||
| /// The amount of time the authoring duration of the last block production attempt | ||||||
| /// should be reduced by to fit into the slot timing. | ||||||
| const BLOCK_PRODUCTION_ADJUSTMENT_MS: Duration = Duration::from_millis(1000); | ||||||
|
|
||||||
| #[derive(Debug)] | ||||||
| pub(crate) struct SlotInfo { | ||||||
| pub timestamp: Timestamp, | ||||||
|
|
@@ -46,7 +64,7 @@ pub(crate) struct SlotInfo { | |||||
| /// Manages block-production timings based on chain parameters and assigned cores. | ||||||
| #[derive(Debug)] | ||||||
| pub(crate) struct SlotTimer<Block, Client, P> { | ||||||
| /// Client that is used for runtime calls | ||||||
| /// Parachain client that is used for runtime calls | ||||||
| client: Arc<Client>, | ||||||
| /// Offset the current time by this duration. | ||||||
| time_offset: Duration, | ||||||
|
|
@@ -147,7 +165,7 @@ where | |||||
| time_offset, | ||||||
| last_reported_core_num: None, | ||||||
| relay_slot_duration, | ||||||
| last_reported_slot: None, | ||||||
| last_reported_slot: Default::default(), | ||||||
| _marker: Default::default(), | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -173,6 +191,115 @@ where | |||||
| )) | ||||||
| } | ||||||
|
|
||||||
| /// Compute the time until the next slot changes. | ||||||
| fn compute_time_until_next_slot_change(&mut self) -> Result<(Duration, Slot), ()> { | ||||||
| let Ok(slot_duration) = crate::slot_duration(&*self.client) else { | ||||||
| tracing::error!(target: LOG_TARGET, "Failed to fetch slot duration from runtime."); | ||||||
| return Err(()) | ||||||
| }; | ||||||
|
|
||||||
| let now = duration_now(); | ||||||
| let now = now.as_millis().saturating_sub(self.time_offset.as_millis()); | ||||||
|
|
||||||
| let last_reported_slot = self.last_reported_slot.unwrap_or_default(); | ||||||
| let Some(last_slot_timestamp) = last_reported_slot.timestamp(slot_duration) else { | ||||||
| tracing::error!(target: LOG_TARGET, "Failed to obtain the last slot timestamp"); | ||||||
| return Err(()) | ||||||
| }; | ||||||
|
|
||||||
| // Compute when the next different slot starts. | ||||||
| let next_different_slot_time = | ||||||
| last_slot_timestamp.as_millis() + slot_duration.as_millis() as u64; | ||||||
| let remaining_millis = next_different_slot_time.saturating_sub(now as u64); | ||||||
| let next_aura_slot = | ||||||
| Slot::from_timestamp(Timestamp::from(next_different_slot_time as u64), slot_duration); | ||||||
|
|
||||||
| Ok((Duration::from_millis(remaining_millis as u64), next_aura_slot)) | ||||||
| } | ||||||
|
|
||||||
| /// Adjust the authoring duration to fit into the slot timing. | ||||||
| /// | ||||||
| /// Returns the adjusted authoring duration and the slot that it corresponds to. | ||||||
| pub fn adjust_authoring_duration( | ||||||
| &mut self, | ||||||
| mut authoring_duration: Duration, | ||||||
| ) -> Option<Duration> { | ||||||
| let Ok((duration, next_slot)) = self.time_until_next_slot() else { | ||||||
|
||||||
| tracing::error!( | ||||||
| target: LOG_TARGET, | ||||||
| "Failed to fetch slot duration from runtime. Using unadjusted authoring duration." | ||||||
| ); | ||||||
| return Some(authoring_duration); | ||||||
| }; | ||||||
|
|
||||||
| let Ok((next_duration_change, next_slot_change)) = | ||||||
|
||||||
| let Ok((next_duration_change, next_slot_change)) = | |
| let Ok((duration_until_next_slot, next_slot)) = |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| let deadline = next_duration_change.saturating_sub(BLOCK_PRODUCTION_ADJUSTMENT_MS); | |
| let duration_until_deadline = next_duration_change.saturating_sub(BLOCK_PRODUCTION_ADJUSTMENT_MS); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the record, everything here currently assumes that every slot changes brings a different author. Someone could run a single-collator parachain which does not need to respect these offsets. We would break that use-case.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can be more generous and increase BLOCK_PRODUCTION_THRESHOLD_MS to 100ms or something. I introduced the original BLOCK_PRODUCTION_THRESHOLD_MS not because blocks below 500ms are not viable at all, but because I wanted to prevent that someone assigns 36 cores to their chain and we arrives at <200ms blocks. So I chose this arbitrary value that we only produce blocks every 500ms for now.
In our case here, producing a 400ms block is also okay.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| title: 'aura/slot_based: Reduce authoring duration of the last produced block ' | ||
| doc: | ||
| - audience: Node Dev | ||
| description: "This PR reduces the authoring duration of the last parachain produced\ | ||
| \ block. Effectively ensures that the next author has sufficient time to import\ | ||
| \ the previous block.\n\n- Only the last authoring duration is reduced by 500ms\ | ||
| \ (`BLOCK_PRODUCTION_ADJUSTMENT_MS`)\n- The authorizing duration was already capped\ | ||
| \ at a minimum of 500ms (`BLOCK_PRODUCTION_MINIMUM_INTERVAL_MS`)\n- The number\ | ||
| \ of para blocks that the relay chain can fit is inferred by the `relay slot duration\ | ||
| \ / default authoring time`\n- `SlotInfo` is adjusted to account for the number\ | ||
| \ of para blocks produced so far\n- While at it, have added a few extra trace\ | ||
| \ logs\n\n### Testing Done\n\nTested on top of:\n- https://github.com/paritytech/polkadot-sdk/pull/9880\n\ | ||
| \n```\nDEBUG tokio-runtime-worker aura::cumulus: [Parachain] New block production\ | ||
|
||
| \ opportunity. slot_duration=SlotDuration(6000) aura_slot=Slot(293621792)\nDEBUG\ | ||
| \ tokio-runtime-worker aura::cumulus: [Parachain] Parachain slot adjusted to relay\ | ||
| \ chain. timestamp=Timestamp(1761730746000) slot=Slot(293621791)\nDEBUG tokio-runtime-worker\ | ||
| \ aura::cumulus: [Parachain] Going to claim core relay_parent=0xa486a45c3a10e9ade7d4ddefef7615d4b001d47c962013386b95719d8f37aeae\ | ||
| \ core_selector=CoreSelector(0) claim_queue_offset=ClaimQueueOffset(1)\nDEBUG\ | ||
| \ tokio-runtime-worker aura::cumulus: [Parachain] Building block. unincluded_segment_len=0\ | ||
| \ relay_parent=0xa486\u2026aeae relay_parent_num=61 relay_parent_offset=1 included_hash=0x21c3\u2026\ | ||
| ac1b included_num=84 parent=0x21c3\u2026ac1b slot=Slot(293621791)\nDEBUG tokio-runtime-worker\ | ||
| \ aura::cumulus: [Parachain] Expected to produce for 3 cores but only have 1 slots.\ | ||
| \ Attempting to produce multiple blocks per slot. block_production_interval=2s\n\ | ||
| DEBUG tokio-runtime-worker aura::cumulus: [Parachain] Adjusting authoring duration\ | ||
| \ for slot. blocks_produced_for_slot=1 blocks_produced_per_relay_slot=3 authoring_duration=2s\ | ||
| \ duration=1.99s\nDEBUG tokio-runtime-worker aura::cumulus: [Parachain] Adjusted\ | ||
| \ proposal duration. duration=1.99s\n\n INFO tokio-runtime-worker sc_basic_authorship::basic_authorship:\ | ||
| \ [Parachain] \U0001F381 Prepared block for proposing at 85 (8 ms) hash: 0x7579638578324c2f7b5b9f8af0f2bb1aa238f665db70dfcd1ddcb77475733e28;\ | ||
| \ parent_hash: 0x21c3\u2026ac1b; end: NoMoreTransactions; extrinsics_count: 2\n\ | ||
| \ INFO tokio-runtime-worker substrate: [Parachain] \U0001F195 Imported #85 (0x21c3\u2026\ | ||
| ac1b \u2192 0x183a\u20265b0d)\n\nDEBUG tokio-runtime-worker aura::cumulus: [Parachain]\ | ||
| \ Expected to produce for 3 cores but only have 1 slots. Attempting to produce\ | ||
| \ multiple blocks per slot. block_production_interval=2s\nTRACE tokio-runtime-worker\ | ||
| \ aura::cumulus: [Parachain] Determined next block production opportunity. time_until_next_attempt=1.977s\ | ||
| \ aura_slot=Slot(293621792) last_reported=LastReportedSlot { slot: Some(Slot(293621792)),\ | ||
| \ blocks_produced: 1 }\nTRACE tokio-runtime-worker aura::cumulus: [Parachain]\ | ||
| \ Sleeping until the next slot. time_until_next_attempt=1.977s\nDEBUG tokio-runtime-worker\ | ||
| \ aura::cumulus: [Parachain] New block production opportunity. slot_duration=SlotDuration(6000)\ | ||
| \ aura_slot=Slot(293621792)\nDEBUG tokio-runtime-worker aura::cumulus: [Parachain]\ | ||
| \ Parachain slot adjusted to relay chain. timestamp=Timestamp(1761730746000) slot=Slot(293621791)\n\ | ||
| DEBUG tokio-runtime-worker aura::cumulus: [Parachain] Going to claim core relay_parent=0xa486a45c3a10e9ade7d4ddefef7615d4b001d47c962013386b95719d8f37aeae\ | ||
| \ core_selector=CoreSelector(1) claim_queue_offset=ClaimQueueOffset(1)\nDEBUG\ | ||
| \ tokio-runtime-worker aura::cumulus: [Parachain] Building block. unincluded_segment_len=1\ | ||
| \ relay_parent=0xa486\u2026aeae relay_parent_num=61 relay_parent_offset=1 included_hash=0x21c3\u2026\ | ||
| ac1b included_num=84 parent=0x183a\u20265b0d slot=Slot(293621791)\nDEBUG tokio-runtime-worker\ | ||
| \ aura::cumulus: [Parachain] Expected to produce for 3 cores but only have 1 slots.\ | ||
| \ Attempting to produce multiple blocks per slot. block_production_interval=2s\n\ | ||
| DEBUG tokio-runtime-worker aura::cumulus: [Parachain] Adjusting authoring duration\ | ||
| \ for slot. blocks_produced_for_slot=2 blocks_produced_per_relay_slot=3 authoring_duration=2s\ | ||
| \ duration=1.988s\n\nDEBUG tokio-runtime-worker aura::cumulus: [Parachain] Adjusted\ | ||
| \ proposal duration. duration=1.988s\n INFO tokio-runtime-worker sc_basic_authorship::basic_authorship:\ | ||
| \ [Parachain] \U0001F381 Prepared block for proposing at 86 (232 ms) hash: 0x17b4f3fc6f13a8bad577d3e12f026a8c51b37fecd33551beb32e059fc510c941;\ | ||
| \ parent_hash: 0x183a\u20265b0d; end: NoMoreTransactions; extrinsics_count: 2\n\ | ||
| \ INFO tokio-runtime-worker substrate: [Parachain] \U0001F195 Imported #86 (0x183a\u2026\ | ||
| 5b0d \u2192 0x9504\u2026a66d)\nDEBUG tokio-runtime-worker aura::cumulus: [Parachain]\ | ||
| \ Expected to produce for 3 cores but only have 1 slots. Attempting to produce\ | ||
| \ multiple blocks per slot. block_production_interval=2s\nTRACE tokio-runtime-worker\ | ||
| \ aura::cumulus: [Parachain] Determined next block production opportunity. time_until_next_attempt=1.751s\ | ||
| \ aura_slot=Slot(293621792) last_reported=LastReportedSlot { slot: Some(Slot(293621792)),\ | ||
| \ blocks_produced: 2 }\nTRACE tokio-runtime-worker aura::cumulus: [Parachain]\ | ||
| \ Sleeping until the next slot. time_until_next_attempt=1.751s\nDEBUG tokio-runtime-worker\ | ||
| \ aura::cumulus: [Parachain] New block production opportunity. slot_duration=SlotDuration(6000)\ | ||
| \ aura_slot=Slot(293621792)\nDEBUG tokio-runtime-worker aura::cumulus: [Parachain]\ | ||
| \ Parachain slot adjusted to relay chain. timestamp=Timestamp(1761730746000) slot=Slot(293621791)\n\ | ||
| DEBUG tokio-runtime-worker aura::cumulus: [Parachain] Going to claim core relay_parent=0xa486a45c3a10e9ade7d4ddefef7615d4b001d47c962013386b95719d8f37aeae\ | ||
| \ core_selector=CoreSelector(2) claim_queue_offset=ClaimQueueOffset(1)\nDEBUG\ | ||
| \ tokio-runtime-worker aura::cumulus: [Parachain] Building block. unincluded_segment_len=2\ | ||
| \ relay_parent=0xa486\u2026aeae relay_parent_num=61 relay_parent_offset=1 included_hash=0x21c3\u2026\ | ||
| ac1b included_num=84 parent=0x9504\u2026a66d slot=Slot(293621791)\nDEBUG tokio-runtime-worker\ | ||
| \ aura::cumulus: [Parachain] Expected to produce for 3 cores but only have 1 slots.\ | ||
| \ Attempting to produce multiple blocks per slot. block_production_interval=2s\n\ | ||
| \nDEBUG tokio-runtime-worker aura::cumulus: [Parachain] Adjusting authoring duration\ | ||
| \ for slot. blocks_produced_for_slot=3 blocks_produced_per_relay_slot=3 authoring_duration=2s\ | ||
| \ duration=1.99s\nDEBUG tokio-runtime-worker aura::cumulus: [Parachain] Adjusted\ | ||
| \ the last block production attempt for the slot. aura_slot=Slot(293621793) blocks_produced_for_slot=3\ | ||
| \ blocks_produced_per_relay_slot=3 authoring_duration=1.49s\nDEBUG tokio-runtime-worker\ | ||
| \ aura::cumulus: [Parachain] Adjusted proposal duration. duration=1.49s\n```\n\ | ||
| \n- The first two blocks are adjusted to `duration=1.99s` and `duration=1.988s`\ | ||
| \ respectively\n- The last block is adjusted to `duration=1.49s` (ie reduced by\ | ||
| \ another 500ms)\n\nPart of: https://github.com/paritytech/polkadot-sdk/issues/9848" | ||
| crates: | ||
| - name: cumulus-client-consensus-aura | ||
| bump: patch | ||
Uh oh!
There was an error while loading. Please reload this page.