diff --git a/crates/revm/src/db/in_memory_db.rs b/crates/revm/src/db/in_memory_db.rs index 217033ce07..e74693d02e 100644 --- a/crates/revm/src/db/in_memory_db.rs +++ b/crates/revm/src/db/in_memory_db.rs @@ -28,6 +28,7 @@ pub struct CacheDB { storage: Map>, contracts: Map, logs: Vec, + block_hashes: Map, db: ExtDB, } @@ -41,6 +42,7 @@ impl CacheDB { storage: Map::new(), contracts, logs: Vec::default(), + block_hashes: Map::new(), db, } } @@ -103,7 +105,14 @@ impl DatabaseCommit for CacheDB { impl Database for CacheDB { fn block_hash(&mut self, number: U256) -> H256 { - self.db.block_hash(number) + match self.block_hashes.entry(number) { + Entry::Occupied(entry) => *entry.get(), + Entry::Vacant(entry) => { + let hash = self.db.block_hash(number); + entry.insert(hash); + hash + } + } } fn basic(&mut self, address: H160) -> AccountInfo { @@ -155,7 +164,10 @@ impl Database for CacheDB { impl DatabaseRef for CacheDB { fn block_hash(&self, number: U256) -> H256 { - self.db.block_hash(number) + match self.block_hashes.get(&number) { + Some(entry) => *entry, + None => self.db.block_hash(number), + } } fn basic(&self, address: H160) -> AccountInfo { @@ -202,8 +214,10 @@ impl DatabaseRef for EmptyDB { } // History related - fn block_hash(&self, _number: U256) -> H256 { - H256::default() + fn block_hash(&self, number: U256) -> H256 { + let mut buffer: [u8; 4 * 8] = [0; 4 * 8]; + number.to_big_endian(&mut buffer); + H256::from_slice(&Keccak256::digest(&buffer)) } } diff --git a/crates/revm/src/specification.rs b/crates/revm/src/specification.rs index da634e2caa..69226a74b8 100644 --- a/crates/revm/src/specification.rs +++ b/crates/revm/src/specification.rs @@ -124,9 +124,8 @@ mod spec_impl { spec!(FRONTIER); } -pub use spec_impl::BERLIN::SpecImpl as BerlinSpec; -pub use spec_impl::BYZANTINE::SpecImpl as ByzantineSpec; -pub use spec_impl::FRONTIER::SpecImpl as FrontierSpec; -pub use spec_impl::ISTANBUL::SpecImpl as IstanbulSpec; -pub use spec_impl::LATEST::SpecImpl as LatestSpec; -pub use spec_impl::LONDON::SpecImpl as LondonSpec; +pub use spec_impl::{ + BERLIN::SpecImpl as BerlinSpec, BYZANTINE::SpecImpl as ByzantineSpec, + FRONTIER::SpecImpl as FrontierSpec, ISTANBUL::SpecImpl as IstanbulSpec, + LATEST::SpecImpl as LatestSpec, LONDON::SpecImpl as LondonSpec, +};