Skip to content

Commit b239f36

Browse files
Add support for block hashes
1 parent 0254975 commit b239f36

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use alloy::primitives::{B256, U256};
2+
use serde::Deserialize;
3+
use std::collections::HashMap;
4+
5+
#[derive(Debug, Deserialize, Clone)]
6+
#[serde(rename_all = "camelCase")]
7+
pub struct BlockInfo {
8+
pub number: u64,
9+
pub hash: B256,
10+
}
11+
12+
#[derive(Debug, Deserialize)]
13+
pub struct BlockHashes(pub Vec<BlockInfo>);
14+
15+
impl BlockHashes {
16+
pub fn into_array(self, block_number: u64) -> [U256; 256] {
17+
let mut array = [U256::ZERO; 256];
18+
let mut map = HashMap::<u64, U256>::new();
19+
self.0.into_iter().for_each(|info| {
20+
map.insert(info.number, info.hash.into());
21+
});
22+
// Add values for most recent 256 block, if present
23+
for offset in 1..=256 {
24+
if let Some(hash) = map.get(&(block_number - offset)) {
25+
array[(offset - 1) as usize] = *hash;
26+
}
27+
}
28+
array
29+
}
30+
}

tests/instances/eth_runner/src/main.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(slice_as_array)]
22

3+
use block_hashes::BlockHashes;
34
use clap::Parser;
45
use post_check::post_check;
56
use prestate::{populate_prestate, DiffTrace, PrestateTrace};
@@ -8,6 +9,7 @@ use std::fs::{self, File};
89
use std::io::BufReader;
910

1011
mod block;
12+
mod block_hashes;
1113
mod post_check;
1214
mod prestate;
1315
mod receipts;
@@ -35,6 +37,10 @@ struct Args {
3537
#[arg(long)]
3638
receipts: String,
3739

40+
/// Path to the block hashes JSON file (optional)
41+
#[arg(long)]
42+
block_hashes: Option<String>,
43+
3844
/// If set, the leaves of the tree are put in random
3945
/// positions to emulate real-world costs
4046
#[arg(long, action = clap::ArgAction::SetTrue)]
@@ -51,9 +57,14 @@ fn run<const RANDOMIZED: bool>(
5157
transactions: Vec<Vec<u8>>,
5258
receipts: Vec<receipts::TransactionReceipt>,
5359
diff_trace: DiffTrace,
60+
block_hashes: Option<BlockHashes>,
5461
) -> anyhow::Result<()> {
5562
chain.set_last_block_number(block_number - 1);
5663

64+
if let Some(block_hashes) = block_hashes {
65+
chain.set_block_hashes(block_hashes.into_array(block_number))
66+
}
67+
5768
let prestate_cache = populate_prestate(&mut chain, ps_trace);
5869

5970
let output = chain.run_block(transactions, Some(block_context), None);
@@ -84,6 +95,10 @@ fn main() -> anyhow::Result<()> {
8495
let diff_file = File::open(&args.difftrace)?;
8596
let diff_reader = BufReader::new(diff_file);
8697
let diff_trace: DiffTrace = serde_json::from_reader(diff_reader)?;
98+
let block_hashes: Option<BlockHashes> = args.block_hashes.map(|path| {
99+
let hashes = fs::read_to_string(&path).expect("valid block hashes path");
100+
serde_json::from_str(&hashes).expect("valid block hashes JSON")
101+
});
87102

88103
let block: block::Block = serde_json::from_str(&block).expect("valid block JSON");
89104
let block_number = block.result.header.number;
@@ -130,6 +145,7 @@ fn main() -> anyhow::Result<()> {
130145
transactions,
131146
receipts,
132147
diff_trace,
148+
block_hashes,
133149
)
134150
} else {
135151
let chain = Chain::empty(Some(1));
@@ -142,6 +158,7 @@ fn main() -> anyhow::Result<()> {
142158
transactions,
143159
receipts,
144160
diff_trace,
161+
block_hashes,
145162
)
146163
}
147164
}

tests/rig/src/chain.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ impl<const RANDOMIZED_TREE: bool> Chain<RANDOMIZED_TREE> {
110110
self.block_number = prev
111111
}
112112

113+
pub fn set_block_hashes(&mut self, block_hashes: [U256; 256]) {
114+
self.block_hashes = block_hashes
115+
}
116+
113117
///
114118
/// Run block with given transactions and block context.
115119
/// If block context is `None` default testing values will be used.

0 commit comments

Comments
 (0)