Skip to content

Commit

Permalink
chore(forkid): simplify and add comment of the set_head_priv (paradig…
Browse files Browse the repository at this point in the history
…mxyz#11686)

Signed-off-by: jsvisa <[email protected]>
  • Loading branch information
jsvisa authored Oct 12, 2024
1 parent de736a5 commit 86c6ba5
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions crates/ethereum-forks/src/forkid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,32 +258,24 @@ impl ForkFilter {
}

fn set_head_priv(&mut self, head: Head) -> Option<ForkTransition> {
let recompute_cache = {
let head_in_past = match self.cache.epoch_start {
ForkFilterKey::Block(epoch_start_block) => head.number < epoch_start_block,
ForkFilterKey::Time(epoch_start_time) => head.timestamp < epoch_start_time,
};
let head_in_future = match self.cache.epoch_end {
Some(ForkFilterKey::Block(epoch_end_block)) => head.number >= epoch_end_block,
Some(ForkFilterKey::Time(epoch_end_time)) => head.timestamp >= epoch_end_time,
None => false,
};

head_in_past || head_in_future
let head_in_past = match self.cache.epoch_start {
ForkFilterKey::Block(epoch_start_block) => head.number < epoch_start_block,
ForkFilterKey::Time(epoch_start_time) => head.timestamp < epoch_start_time,
};

// recompute the cache
let transition = if recompute_cache {
let past = self.current();
self.cache = Cache::compute_cache(&self.forks, head);
Some(ForkTransition { current: self.current(), past })
} else {
None
let head_in_future = match self.cache.epoch_end {
Some(ForkFilterKey::Block(epoch_end_block)) => head.number >= epoch_end_block,
Some(ForkFilterKey::Time(epoch_end_time)) => head.timestamp >= epoch_end_time,
None => false,
};

self.head = head;

transition
// Recompute the cache if the head is in the past or future epoch.
(head_in_past || head_in_future).then(|| {
let past = self.current();
self.cache = Cache::compute_cache(&self.forks, head);
ForkTransition { current: self.current(), past }
})
}

/// Set the current head.
Expand Down

0 comments on commit 86c6ba5

Please sign in to comment.