Skip to content

Commit

Permalink
feat: cache block hashes (#71)
Browse files Browse the repository at this point in the history
* feat: cache block hashes

Caches block hashes, but also computes a block hash based
on the block number if no block hash was returned from the
underlying database.

Retrieving block hashes can be expensive, but it is also
important that block hashes for different blocks
differ from each other

* chore: clippy

* refactor: move custom block hash to `EmptyDB`
  • Loading branch information
onbjerg committed Mar 10, 2022
1 parent 0f6c09f commit df012e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
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,
};

0 comments on commit df012e8

Please sign in to comment.