This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Babe: bad epoch index with skipped epochs and warp sync #13135
Merged
+187
−67
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6df123d
Detect and correct epoch-index for skipped epochs
davxy 0b7cf3e
Code refactory
davxy 24c9c73
Epoch index should be also be fixed for secondary claims with VRF
davxy 5501bcd
Fix typo
davxy 7117e94
Make clippy happy
davxy 30ada17
Fix typo
davxy bdc3e40
Merge branch 'master' into babe-epoch-index-correction
davxy 404ed0f
Trigger pipeline
davxy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -209,8 +209,9 @@ impl From<sp_consensus_babe::Epoch> for Epoch { | |
| } | ||
|
|
||
| impl Epoch { | ||
| /// Create the genesis epoch (epoch #0). This is defined to start at the slot of | ||
| /// the first block, so that has to be provided. | ||
| /// Create the genesis epoch (epoch #0). | ||
| /// | ||
| /// This is defined to start at the slot of the first block, so that has to be provided. | ||
| pub fn genesis(genesis_config: &BabeConfiguration, slot: Slot) -> Epoch { | ||
| Epoch { | ||
| epoch_index: 0, | ||
|
|
@@ -224,6 +225,38 @@ impl Epoch { | |
| }, | ||
| } | ||
| } | ||
|
|
||
| /// Clone and tweak epoch information to refer to the specified slot. | ||
| /// | ||
| /// All the information which depends on the slot value is recomputed and assigned | ||
| /// to the returned epoch instance. | ||
| /// | ||
| /// The `slot` must be greater than or equal the original epoch start slot, | ||
| /// if is less this operation is equivalent to a simple clone. | ||
| pub fn clone_for_slot(&self, slot: Slot) -> Epoch { | ||
| let mut epoch = self.clone(); | ||
|
|
||
| let skipped_epochs = *slot.saturating_sub(self.start_slot) / self.duration; | ||
|
|
||
| let epoch_index = epoch.epoch_index.checked_add(skipped_epochs).expect( | ||
| "epoch number is u64; it should be strictly smaller than number of slots; \ | ||
| slots relate in some way to wall clock time; \ | ||
| if u64 is not enough we should crash for safety; qed.", | ||
| ); | ||
|
|
||
| let start_slot = skipped_epochs | ||
| .checked_mul(epoch.duration) | ||
| .and_then(|skipped_slots| epoch.start_slot.checked_add(skipped_slots)) | ||
| .expect( | ||
| "slot number is u64; it should relate in some way to wall clock time; \ | ||
| if u64 is not enough we should crash for safety; qed.", | ||
| ); | ||
|
|
||
| epoch.epoch_index = epoch_index; | ||
| epoch.start_slot = Slot::from(start_slot); | ||
|
|
||
| epoch | ||
| } | ||
| } | ||
|
|
||
| /// Errors encountered by the babe authorship task. | ||
|
|
@@ -1540,13 +1573,12 @@ where | |
| }; | ||
|
|
||
| if viable_epoch.as_ref().end_slot() <= slot { | ||
| // some epochs must have been skipped as our current slot | ||
| // fits outside the current epoch. we will figure out | ||
| // which epoch it belongs to and we will re-use the same | ||
| // data for that epoch | ||
| let mut epoch_data = viable_epoch.as_mut(); | ||
| let skipped_epochs = | ||
| *slot.saturating_sub(epoch_data.start_slot) / epoch_data.duration; | ||
| // Some epochs must have been skipped as our current slot fits outside the | ||
| // current epoch. We will figure out which epoch it belongs to and we will | ||
| // re-use the same data for that epoch | ||
| let epoch = viable_epoch.as_mut(); | ||
| let prev_index = epoch.epoch_index; | ||
| *epoch = epoch.clone_for_slot(slot); | ||
|
|
||
| // NOTE: notice that we are only updating a local copy of the `Epoch`, this | ||
|
||
| // makes it so that when we insert the next epoch into `EpochChanges` below | ||
|
|
@@ -1558,27 +1590,11 @@ where | |
| // use for a given slot, we will search in-depth with the predicate | ||
| // `epoch.start_slot <= slot` which will still match correctly without updating | ||
| // `start_slot` to the correct value as below. | ||
| let epoch_index = epoch_data.epoch_index.checked_add(skipped_epochs).expect( | ||
| "epoch number is u64; it should be strictly smaller than number of slots; \ | ||
| slots relate in some way to wall clock time; \ | ||
| if u64 is not enough we should crash for safety; qed.", | ||
| ); | ||
|
|
||
| let start_slot = skipped_epochs | ||
| .checked_mul(epoch_data.duration) | ||
| .and_then(|skipped_slots| epoch_data.start_slot.checked_add(skipped_slots)) | ||
| .expect( | ||
| "slot number is u64; it should relate in some way to wall clock time; \ | ||
| if u64 is not enough we should crash for safety; qed.", | ||
| ); | ||
|
|
||
| warn!( | ||
| target: LOG_TARGET, | ||
| "👶 Epoch(s) skipped: from {} to {}", epoch_data.epoch_index, epoch_index, | ||
| "👶 Epoch(s) skipped: from {} to {}", prev_index, epoch.epoch_index, | ||
| ); | ||
|
|
||
| epoch_data.epoch_index = epoch_index; | ||
| epoch_data.start_slot = Slot::from(start_slot); | ||
| } | ||
|
|
||
| log!( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.