revm_database/states/
block_hash_cache.rs1use primitives::{alloy_primitives::B256, BLOCK_HASH_HISTORY};
2use std::boxed::Box;
3
4const BLOCK_HASH_HISTORY_USIZE: usize = BLOCK_HASH_HISTORY as usize;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct BlockHashCache {
9 hashes: Box<[(Option<u64>, B256); BLOCK_HASH_HISTORY_USIZE]>,
15}
16
17impl Default for BlockHashCache {
18 fn default() -> Self {
19 Self::new()
20 }
21}
22
23impl BlockHashCache {
24 #[inline]
26 pub fn new() -> Self {
27 Self {
28 hashes: Box::new([(None, B256::ZERO); BLOCK_HASH_HISTORY_USIZE]),
29 }
30 }
31
32 #[inline]
34 pub const fn insert(&mut self, block_number: u64, block_hash: B256) {
35 let index = (block_number % BLOCK_HASH_HISTORY) as usize;
36 self.hashes[index] = (Some(block_number), block_hash);
37 }
38
39 #[inline]
41 pub fn get(&self, block_number: u64) -> Option<B256> {
42 let index = (block_number % BLOCK_HASH_HISTORY) as usize;
43 let (stored_block_number, stored_hash) = self.hashes[index];
44 if Some(block_number) == stored_block_number {
45 Some(stored_hash)
46 } else {
47 None
48 }
49 }
50}