Skip to content

Commit

Permalink
Witnesser WIP: Update ChainProgress enum variant (#5494)
Browse files Browse the repository at this point in the history
Include both ranges in `ChainProgress::Reorg` variant.
  • Loading branch information
MxmUrw authored and kylezs committed Dec 17, 2024
1 parent 17375b3 commit 58225cd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn validate_vote<ChainBlockHash, ChainBlockNumber>(
pub enum ChainProgress<ChainBlockNumber> {
// Block witnesser will discard any elections that were started for this range and start them
// again since we've detected a reorg
Reorg(RangeInclusive<ChainBlockNumber>),
Reorg { removed: RangeInclusive<ChainBlockNumber>, added: RangeInclusive<ChainBlockNumber> },
// the chain is just progressing as a normal chain of hashes
Continuous(RangeInclusive<ChainBlockNumber>),
// there was no update to the witnessed block headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ pub struct MergeInfo<H, N> {

impl<H, N: Copy> MergeInfo<H, N> {
pub fn into_chain_progress(&self) -> Option<ChainProgress<N>> {
if let (Some(first), Some(last)) = (self.added.front(), self.added.back()) {
if self.removed.len() == 0 {
Some(ChainProgress::Continuous(first.block_height..=last.block_height))
if let (Some(first_added), Some(last_added)) = (self.added.front(), self.added.back()) {
if let (Some(first_removed), Some(last_removed)) =
(self.removed.front(), self.removed.back())
{
Some(ChainProgress::Reorg {
removed: first_removed.block_height..=first_removed.block_height,
added: first_added.block_height..=last_added.block_height,
})
} else {
Some(ChainProgress::Reorg(first.block_height..=last.block_height))
Some(ChainProgress::Continuous(first_added.block_height..=last_added.block_height))
}
} else {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl<

let last_seen_root = match chain_progress {
ChainProgress::WaitingForFirstConsensus => return Ok(()),
ChainProgress::Reorg(reorg_range) => {
ChainProgress::Reorg{ removed: reorg_range, added: new_range } => {
// Delete any elections that are ongoing for any blocks in the reorg range.
for (i, election_identifier) in election_identifiers.into_iter().enumerate() {
let election = ElectoralAccess::election_mut(election_identifier);
Expand All @@ -193,7 +193,7 @@ impl<

// TODO: Wrap with safe mode, no new elections.
for root in
reorg_range.clone().step_by(Into::<u64>::into(Chain::WITNESS_PERIOD) as usize)
new_range.clone().step_by(Into::<u64>::into(Chain::WITNESS_PERIOD) as usize)
{
log::info!("New election for root: {:?}", root);
ElectoralAccess::new_election(
Expand All @@ -212,7 +212,7 @@ impl<
// prevent double dispatches. By keeping the state, if we have a reorg we can check
// against the state in the process_block_data hook to ensure we don't double
// dispatch.
*reorg_range.end()
*new_range.end()
},
ChainProgress::None(last_block_root_seen) => *last_block_root_seen,
ChainProgress::Continuous(witness_range) => *witness_range.start(),
Expand Down

0 comments on commit 58225cd

Please sign in to comment.