revme/cmd/blockchaintest/
post_block.rs1use revm::{
2 context::{Block, ContextTr, JournalTr},
3 handler::EvmTr,
4 primitives::{address, hardfork::SpecId, Address, Bytes, ONE_ETHER, ONE_GWEI, U256},
5 statetest_types::blockchain::Withdrawal,
6 Database, DatabaseCommit, SystemCallCommitEvm,
7};
8
9#[inline]
18pub fn post_block_transition<
19 'a,
20 DB: Database + DatabaseCommit + 'a,
21 EVM: SystemCallCommitEvm<Error: core::fmt::Debug> + EvmTr<Context: ContextTr<Db = DB>>,
22>(
23 evm: &mut EVM,
24 block: impl Block,
25 withdrawals: &[Withdrawal],
26 spec: SpecId,
27) {
28 let block_reward = block_reward(spec, 0);
30 if block_reward != 0 {
31 evm.ctx_mut()
32 .journal_mut()
33 .balance_incr(block.beneficiary(), U256::from(block_reward))
34 .expect("Db actions to pass");
35 }
36
37 if spec.is_enabled_in(SpecId::SHANGHAI) {
39 for withdrawal in withdrawals {
40 evm.ctx_mut()
41 .journal_mut()
42 .balance_incr(
43 withdrawal.address,
44 withdrawal.amount.saturating_mul(U256::from(ONE_GWEI)),
45 )
46 .expect("Db actions to pass");
47 }
48 }
49
50 evm.commit_inner();
51
52 if spec.is_enabled_in(SpecId::PRAGUE) {
54 system_call_eip7002_withdrawal_request(evm);
55 }
56
57 if spec.is_enabled_in(SpecId::PRAGUE) {
59 system_call_eip7251_consolidation_request(evm);
60 }
61}
62
63#[inline]
65pub const fn block_reward(spec: SpecId, ommers: usize) -> u128 {
66 if spec.is_enabled_in(SpecId::MERGE) {
67 return 0;
68 }
69
70 let reward = if spec.is_enabled_in(SpecId::CONSTANTINOPLE) {
71 ONE_ETHER * 2
72 } else if spec.is_enabled_in(SpecId::BYZANTIUM) {
73 ONE_ETHER * 3
74 } else {
75 ONE_ETHER * 5
76 };
77
78 reward + (reward >> 5) * ommers as u128
79}
80
81pub const WITHDRAWAL_REQUEST_ADDRESS: Address =
82 address!("0x00000961Ef480Eb55e80D19ad83579A64c007002");
83
84pub(crate) fn system_call_eip7002_withdrawal_request(
86 evm: &mut impl SystemCallCommitEvm<Error: core::fmt::Debug>,
87) {
88 let _ = match evm.system_call_commit(WITHDRAWAL_REQUEST_ADDRESS, Bytes::new()) {
90 Ok(res) => res,
91 Err(e) => {
92 panic!("System call failed: {e:?}");
93 }
94 };
95}
96
97pub const CONSOLIDATION_REQUEST_ADDRESS: Address =
98 address!("0x0000BBdDc7CE488642fb579F8B00f3a590007251");
99
100pub(crate) fn system_call_eip7251_consolidation_request(
102 evm: &mut impl SystemCallCommitEvm<Error: core::fmt::Debug>,
103) {
104 let _ = match evm.system_call_commit(CONSOLIDATION_REQUEST_ADDRESS, Bytes::new()) {
105 Ok(res) => res,
106 Err(e) => {
107 panic!("System call failed: {e:?}");
108 }
109 };
110}