Skip to content
Merged
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
72 changes: 60 additions & 12 deletions packages/evm/core/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,8 +799,15 @@ impl DatabaseRef for PersistentDB {
}
}

fn block_hash_ref(&self, _number: u64) -> Result<B256, Self::Error> {
todo!()
fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error> {
let txn = self.env.read_txn()?;
let inner = self.inner.borrow_mut();

let data = inner.blocks.get(&txn, &number)?;
match data {
Some(data) => Ok(data.hash),
None => Ok(B256::ZERO),
}
}
}

Expand Down Expand Up @@ -1728,14 +1735,14 @@ mod tests {
use crate::{
compression::CompressedBincode,
db::{
CommitKey, CommitReceipts, LegacyAddressWrapper, PendingCommit, PersistentDB,
PersistentDBOptions, StaticStringWrapper, StringWrapper,
BlockHeaderData, CommitKey, CommitReceipts, LegacyAddressWrapper, PendingCommit,
PersistentDB, PersistentDBOptions, StaticStringWrapper, StringWrapper,
},
legacy::{LegacyAccountAttributes, LegacyAddress},
receipt::TxReceipt,
};
use alloy_primitives::{B256, U256, address, b256};
use revm::{primitives::HashMap, state::AccountInfo};
use revm::{Database, primitives::HashMap, state::AccountInfo};

use heed::{BytesDecode, BytesEncode};

Expand Down Expand Up @@ -1816,14 +1823,43 @@ mod tests {
}

#[test]
fn test_read_receipts() {
let path = tempfile::Builder::new()
.prefix("evm.mdb")
.tempdir()
.unwrap();
fn test_block_hash() {
let mut db = create_temp_database();

let db = PersistentDB::new(PersistentDBOptions::new(path.path().to_path_buf()))
.expect("database");
let hash = db.block_hash(1).unwrap();
assert_eq!(hash, B256::ZERO);

{
let mut wtxn = db.env.write_txn().unwrap();
let inner = db.inner.borrow_mut();

inner
.blocks
.put(
&mut wtxn,
&1,
&CompressedBincode(&BlockHeaderData {
hash: b256!(
"0000000000000000000000000000000000000000000000000000000000000001"
),
..Default::default()
}),
)
.unwrap();

wtxn.commit().unwrap();
}

let hash = db.block_hash(1).unwrap();
assert_eq!(
hash,
b256!("0000000000000000000000000000000000000000000000000000000000000001")
);
}

#[test]
fn test_read_receipts() {
let db = create_temp_database();

let target_block = 100;
let mut total_receipts = 0;
Expand Down Expand Up @@ -1900,4 +1936,16 @@ mod tests {
assert_eq!(read_block_number, target_block);
assert_eq!(read_receipts, total_receipts);
}

fn create_temp_database() -> PersistentDB {
let path = tempfile::Builder::new()
.prefix("evm.mdb")
.tempdir()
.unwrap();

let db = PersistentDB::new(PersistentDBOptions::new(path.path().to_path_buf()))
.expect("database");

db
}
}
Loading