Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 33 additions & 0 deletions lib/src/chain/blocks_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,39 @@ impl<T> NonFinalizedTree<T> {
self.blocks_trigger_gp_change.clear();
}

/// Updates the consensus algorithm of the finalized block.
///
/// This is useful when the chain starts with `Unknown` consensus and later
/// discovers the actual consensus parameters (e.g. Aura authorities).
///
/// Non-finalized blocks that were verified under the old consensus are cleared,
/// as they may have been verified with incorrect rules.
pub fn set_finalized_consensus(
&mut self,
consensus: chain_information::ChainInformationConsensus,
) {
self.finalized_consensus = match consensus {
chain_information::ChainInformationConsensus::Unknown => FinalizedConsensus::Unknown,
chain_information::ChainInformationConsensus::Aura {
finalized_authorities_list,
slot_duration,
} => FinalizedConsensus::Aura {
authorities_list: Arc::new(finalized_authorities_list),
slot_duration,
},
chain_information::ChainInformationConsensus::Babe {
finalized_block_epoch_information,
finalized_next_epoch_transition,
slots_per_epoch,
} => FinalizedConsensus::Babe {
slots_per_epoch,
block_epoch_information: finalized_block_epoch_information.map(Arc::from),
next_epoch_transition: Arc::from(finalized_next_epoch_transition),
},
};
self.clear();
}

/// Returns true if there isn't any non-finalized block in the chain.
pub fn is_empty(&self) -> bool {
self.blocks.is_empty()
Expand Down
14 changes: 14 additions & 0 deletions lib/src/sync/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ impl<TRq, TSrc, TBl> AllSync<TRq, TSrc, TBl> {
all_forks.as_chain_information()
}

/// Updates the consensus algorithm of the finalized block in-place.
///
/// Clears any non-finalized blocks, as they may have been verified with
/// incorrect consensus rules. Peers and requests are unaffected.
pub fn set_finalized_consensus(
&mut self,
consensus: chain_information::ChainInformationConsensus,
) {
let Some(all_forks) = &mut self.all_forks else {
unreachable!()
};
all_forks.set_finalized_consensus(consensus);
}

/// Returns the current status of the syncing.
pub fn status(&'_ self) -> Status<'_, TSrc> {
// TODO:
Expand Down
10 changes: 10 additions & 0 deletions lib/src/sync/all_forks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,16 @@ impl<TBl, TRq, TSrc> AllForksSync<TBl, TRq, TSrc> {
self.chain.as_chain_information()
}

/// Updates the consensus algorithm of the finalized block.
///
/// See [`blocks_tree::NonFinalizedTree::set_finalized_consensus`] for details.
pub fn set_finalized_consensus(
&mut self,
consensus: chain_information::ChainInformationConsensus,
) {
self.chain.set_finalized_consensus(consensus);
}

/// Returns the header of the finalized block.
pub fn finalized_block_header(&self) -> &[u8] {
self.chain.finalized_block_header()
Expand Down
Loading
Loading