From b9d392e29d5f6b0f631cd01bf1b554484947f79e Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Fri, 26 Jan 2024 05:57:33 +0100 Subject: [PATCH] small refactoring in `ethereum-forks` crate (#6234) --- crates/ethereum-forks/src/forkid.rs | 36 ++++++++++++++++++----------- crates/ethereum-forks/src/head.rs | 6 +---- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/crates/ethereum-forks/src/forkid.rs b/crates/ethereum-forks/src/forkid.rs index 39fa8a0c72..876d407db7 100644 --- a/crates/ethereum-forks/src/forkid.rs +++ b/crates/ethereum-forks/src/forkid.rs @@ -92,7 +92,7 @@ impl PartialOrd for ForkFilterKey { impl Ord for ForkFilterKey { fn cmp(&self, other: &Self) -> Ordering { match (self, other) { - (ForkFilterKey::Block(a), ForkFilterKey::Block(b)) => a.cmp(b), + (ForkFilterKey::Block(a), ForkFilterKey::Block(b)) | (ForkFilterKey::Time(a), ForkFilterKey::Time(b)) => a.cmp(b), (ForkFilterKey::Block(_), ForkFilterKey::Time(_)) => Ordering::Less, _ => Ordering::Greater, @@ -194,6 +194,8 @@ impl ForkFilter { ForkFilterKey::Block(_) => true, ForkFilterKey::Time(time) => *time > genesis_timestamp, }) + .collect::>() + .into_iter() .fold( (BTreeMap::from([(ForkFilterKey::Block(0), genesis_fork_hash)]), genesis_fork_hash), |(mut acc, base_hash), key| { @@ -204,8 +206,10 @@ impl ForkFilter { ) .0; + // Compute cache based on filtered forks and the current head. let cache = Cache::compute_cache(&forks, head); + // Create and return a new `ForkFilter`. Self { forks, head, cache } } @@ -224,16 +228,14 @@ impl ForkFilter { head_in_past || head_in_future }; - let mut transition = None; - // recompute the cache - if recompute_cache { + let transition = if recompute_cache { let past = self.current(); - self.cache = Cache::compute_cache(&self.forks, head); - - transition = Some(ForkTransition { current: self.current(), past }) - } + Some(ForkTransition { current: self.current(), past }) + } else { + None + }; self.head = head; @@ -347,19 +349,23 @@ struct Cache { impl Cache { /// Compute cache. fn compute_cache(forks: &BTreeMap, head: Head) -> Self { + // Prepare vectors to store past and future forks. let mut past = Vec::with_capacity(forks.len()); let mut future = Vec::with_capacity(forks.len()); + // Initialize variables to track the epoch range. let mut epoch_start = ForkFilterKey::Block(0); let mut epoch_end = None; + + // Iterate through forks and categorize them into past and future. for (key, hash) in forks { - let active = if let ForkFilterKey::Block(block) = key { - *block <= head.number - } else if let ForkFilterKey::Time(time) = key { - *time <= head.timestamp - } else { - unreachable!() + // Check if the fork is active based on its type (Block or Time). + let active = match key { + ForkFilterKey::Block(block) => *block <= head.number, + ForkFilterKey::Time(time) => *time <= head.timestamp, }; + + // Categorize forks into past or future based on activity. if active { epoch_start = *key; past.push((*key, *hash)); @@ -371,11 +377,13 @@ impl Cache { } } + // Create ForkId using the last past fork's hash and the next epoch start. let fork_id = ForkId { hash: past.last().expect("there is always at least one - genesis - fork hash; qed").1, next: epoch_end.unwrap_or(ForkFilterKey::Block(0)).into(), }; + // Return the computed cache. Self { epoch_start, epoch_end, past, future, fork_id } } } diff --git a/crates/ethereum-forks/src/head.rs b/crates/ethereum-forks/src/head.rs index 18bebed5fa..411853e0d3 100644 --- a/crates/ethereum-forks/src/head.rs +++ b/crates/ethereum-forks/src/head.rs @@ -44,11 +44,7 @@ impl Head { total_difficulty: U256, timestamp: u64, ) { - self.number = number; - self.hash = hash; - self.difficulty = difficulty; - self.total_difficulty = total_difficulty; - self.timestamp = timestamp; + *self = Self { number, hash, difficulty, total_difficulty, timestamp }; } /// Checks if the head block is an empty block (i.e., has default values).