chore(forkid): simplify and add comment of the set_head_priv (#11686)

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
Delweng
2024-10-12 23:18:43 +08:00
committed by GitHub
parent de736a53cc
commit 86c6ba5d8d

View File

@ -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.