revme/cmd/blockchaintest/
pre_block.rs

1//! Pre block state transition
2
3use context::{Block, ContextTr};
4use database::State;
5use primitives::{address, hardfork::SpecId, Address, B256};
6use revm::{handler::EvmTr, Database, SystemCallCommitEvm};
7
8/// Pre block state transition
9///
10/// # Note
11///
12/// Contains only withdrawal processing. And it is missing block hash system call.
13pub fn pre_block_transition<
14    'a,
15    DB: Database + 'a,
16    EVM: SystemCallCommitEvm<Error: core::fmt::Debug>
17        + EvmTr<Context: ContextTr<Db = &'a mut State<DB>>>,
18>(
19    evm: &mut EVM,
20    spec: SpecId,
21    parent_block_hash: Option<B256>,
22    parent_beacon_block_root: Option<B256>,
23) {
24    // skip system calls for block number 0 (Gensis block)
25    if evm.ctx().block().number() == 0 {
26        return;
27    }
28
29    // blockhash system call
30    if let Some(parent_block_hash) = parent_block_hash {
31        system_call_eip2935_blockhash(spec, parent_block_hash, evm);
32    }
33
34    if let Some(parent_beacon_block_root) = parent_beacon_block_root {
35        system_call_eip4844_beacon_root(spec, parent_beacon_block_root, evm);
36    }
37}
38
39pub const HISTORY_STORAGE_ADDRESS: Address = address!("0x0000F90827F1C53a10cb7A02335B175320002935");
40
41/// Blockhash system callEIP-2935
42#[inline]
43pub(crate) fn system_call_eip2935_blockhash(
44    spec: SpecId,
45    parent_block_hash: B256,
46    evm: &mut impl SystemCallCommitEvm<Error: core::fmt::Debug>,
47) -> bool {
48    if !spec.is_enabled_in(SpecId::PRAGUE) {
49        return true;
50    }
51
52    let _ = match evm.system_call_commit(HISTORY_STORAGE_ADDRESS, parent_block_hash.0.into()) {
53        Ok(res) => res,
54        Err(e) => {
55            panic!("System call failed: {e:?}");
56        }
57    };
58
59    true
60}
61
62pub const BEACON_ROOTS_ADDRESS: Address = address!("000F3df6D732807Ef1319fB7B8bB8522d0Beac02");
63
64/// Beacon root system call EIP-4788
65pub(crate) fn system_call_eip4844_beacon_root(
66    spec: SpecId,
67    parent_beacon_block_root: B256,
68    evm: &mut impl SystemCallCommitEvm<Error: core::fmt::Debug>,
69) -> bool {
70    if !spec.is_enabled_in(SpecId::CANCUN) {
71        return true;
72    }
73
74    let _ = match evm.system_call_commit(BEACON_ROOTS_ADDRESS, parent_beacon_block_root.0.into()) {
75        Ok(res) => res,
76        Err(e) => {
77            panic!("System call failed: {e:?}");
78        }
79    };
80
81    true
82}