Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions crates/revm/src/db/in_memory_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct CacheDB<ExtDB: DatabaseRef> {
storage: Map<H160, Map<U256, U256>>,
contracts: Map<H256, Bytes>,
logs: Vec<Log>,
block_hashes: Map<U256, H256>,
db: ExtDB,
}

Expand All @@ -41,6 +42,7 @@ impl<ExtDB: DatabaseRef> CacheDB<ExtDB> {
storage: Map::new(),
contracts,
logs: Vec::default(),
block_hashes: Map::new(),
db,
}
}
Expand Down Expand Up @@ -103,7 +105,14 @@ impl<ExtDB: DatabaseRef> DatabaseCommit for CacheDB<ExtDB> {

impl<ExtDB: DatabaseRef> Database for CacheDB<ExtDB> {
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 {
Expand Down Expand Up @@ -155,7 +164,10 @@ impl<ExtDB: DatabaseRef> Database for CacheDB<ExtDB> {

impl<ExtDB: DatabaseRef> DatabaseRef for CacheDB<ExtDB> {
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 {
Expand Down Expand Up @@ -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))
}
}

Expand Down
11 changes: 5 additions & 6 deletions crates/revm/src/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};