Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
babe: detect and skip epochs on client
  • Loading branch information
andresilva committed Jun 21, 2022
commit 5df7281c53c9175b729c3001eca80ce586043046
62 changes: 58 additions & 4 deletions client/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvid
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
use sp_runtime::{
generic::{BlockId, OpaqueDigestItemId},
traits::{Block as BlockT, Header, NumberFor, SaturatedConversion, Saturating, Zero},
traits::{Block as BlockT, Header, NumberFor, One, SaturatedConversion, Saturating, Zero},
DigestItem,
};

Expand Down Expand Up @@ -1355,15 +1355,43 @@ pub struct BabeBlockImport<Block: BlockT, Client, I> {
inner: I,
client: Arc<Client>,
epoch_changes: SharedEpochChanges<Block, Epoch>,
genesis_slot: Option<Slot>,
config: Config,
}

impl<Block: BlockT, Client, I> BabeBlockImport<Block, Client, I>
where
Client: HeaderBackend<Block>,
{
fn genesis_slot(&mut self) -> Option<Slot> {
if self.genesis_slot.is_some() {
return self.genesis_slot.clone()
}

let genesis_slot =
self.client.header(BlockId::Number(One::one())).ok().flatten().map(|header| {
let pre_digest = find_pre_digest::<Block>(&header).expect(
"valid babe headers must contain a predigest; header has been already verified; qed",
);

pre_digest.slot()
});

if genesis_slot.is_some() {
self.genesis_slot = genesis_slot.clone();
}

genesis_slot
}
}

impl<Block: BlockT, I: Clone, Client> Clone for BabeBlockImport<Block, Client, I> {
fn clone(&self) -> Self {
BabeBlockImport {
inner: self.inner.clone(),
client: self.client.clone(),
epoch_changes: self.epoch_changes.clone(),
genesis_slot: self.genesis_slot.clone(),
config: self.config.clone(),
}
}
Expand All @@ -1376,7 +1404,7 @@ impl<Block: BlockT, Client, I> BabeBlockImport<Block, Client, I> {
block_import: I,
config: Config,
) -> Self {
BabeBlockImport { client, inner: block_import, epoch_changes, config }
BabeBlockImport { client, inner: block_import, epoch_changes, config, genesis_slot: None }
}
}

Expand Down Expand Up @@ -1515,6 +1543,8 @@ where
))
}

let genesis_slot = self.genesis_slot();

// if there's a pending epoch we'll save the previous epoch changes here
// this way we can revert it if there's any error
let mut old_epoch_changes = None;
Expand Down Expand Up @@ -1581,8 +1611,8 @@ where
if let Some(next_epoch_descriptor) = next_epoch_digest {
old_epoch_changes = Some((*epoch_changes).clone());

let viable_epoch = epoch_changes
.viable_epoch(&epoch_descriptor, |slot| {
let mut viable_epoch = epoch_changes
.viable_epoch_mut(&epoch_descriptor, |slot| {
Epoch::genesis(&self.config.genesis_config, slot)
})
.ok_or_else(|| {
Expand All @@ -1600,6 +1630,29 @@ where
log::Level::Info
};

if let Some(genesis_slot) = genesis_slot {
let epoch_index = sp_consensus_babe::epoch_index(
slot,
genesis_slot,
viable_epoch.as_ref().duration,
);

if epoch_index != viable_epoch.as_ref().epoch_index {
warn!(target: "babe", "👶 Epoch(s) skipped: from {} to {}",
viable_epoch.as_ref().epoch_index, epoch_index,
);

let epoch_start = sp_consensus_babe::epoch_start_slot(
epoch_index,
genesis_slot,
viable_epoch.as_ref().duration,
);

viable_epoch.as_mut().epoch_index = epoch_index;
viable_epoch.as_mut().start_slot = epoch_start;
}
}

log!(target: "babe",
log_level,
"👶 New epoch {} launching at block {} (block slot {} >= start slot {}).",
Expand Down Expand Up @@ -1627,6 +1680,7 @@ where
let prune_and_import = || {
prune_finalized(self.client.clone(), &mut epoch_changes)?;

epoch_changes.sync();
epoch_changes
.import(
descendent_query(&*self.client),
Expand Down
22 changes: 22 additions & 0 deletions client/consensus/epochs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,28 @@ where
}
}

pub fn sync(&mut self) {
self.inner = self.inner.clone().map(&mut |hash, number, epoch_header| {
let epoch = self.epochs.get(&(*hash, *number)).unwrap();

let epoch_data = match epoch {
PersistedEpoch::Genesis(..) => return epoch_header,
PersistedEpoch::Regular(epoch_data) => epoch_data,
};

let epoch_header = match epoch_header {
PersistedEpochHeader::Genesis(..) => epoch_header,
PersistedEpochHeader::Regular(mut epoch_header) => {
epoch_header.start_slot = epoch_data.start_slot();
epoch_header.end_slot = epoch_data.end_slot();
PersistedEpochHeader::Regular(epoch_header)
},
};

epoch_header
});
}

/// Prune out finalized epochs, except for the ancestor of the finalized
/// block. The given slot should be the slot number at which the finalized
/// block was authored.
Expand Down