Skip to main content

foundry_evm_core/env/
block.rs

1//! Mutable block environments.
2
3use alloy_primitives::{Address, B256, U256};
4use revm::{
5    context::{Block, BlockEnv},
6    context_interface::block::BlobExcessGasAndPrice,
7};
8use tempo_revm::TempoBlockEnv;
9
10/// Extension of [`Block`] with mutable setters, allowing EVM-agnostic mutation of block fields.
11pub trait FoundryBlock: Block {
12    /// Sets the block number.
13    fn set_number(&mut self, number: U256);
14
15    /// Sets the slot number.
16    fn set_slot_num(&mut self, slot_num: u64);
17
18    /// Sets the beneficiary (coinbase) address.
19    fn set_beneficiary(&mut self, beneficiary: Address);
20
21    /// Sets the block timestamp.
22    fn set_timestamp(&mut self, timestamp: U256);
23
24    /// Sets the gas limit.
25    fn set_gas_limit(&mut self, gas_limit: u64);
26
27    /// Sets the base fee per gas.
28    fn set_basefee(&mut self, basefee: u64);
29
30    /// Sets the block difficulty.
31    fn set_difficulty(&mut self, difficulty: U256);
32
33    /// Sets the prevrandao value.
34    fn set_prevrandao(&mut self, prevrandao: Option<B256>);
35
36    /// Sets the excess blob gas and blob gasprice.
37    fn set_blob_excess_gas_and_price(
38        &mut self,
39        _excess_blob_gas: u64,
40        _base_fee_update_fraction: u64,
41    );
42
43    /// Sets the blob gas price (`block.blobbasefee`), keeping the excess blob gas.
44    fn set_blob_gasprice(&mut self, blob_gasprice: u128);
45
46    // Tempo methods
47
48    /// Returns the milliseconds portion of the block timestamp.
49    fn timestamp_millis_part(&self) -> u64 {
50        0
51    }
52
53    /// Sets the milliseconds portion of the block timestamp.
54    fn set_timestamp_millis_part(&mut self, _millis: u64) {}
55}
56
57impl FoundryBlock for BlockEnv {
58    fn set_number(&mut self, number: U256) {
59        self.number = number;
60    }
61
62    fn set_slot_num(&mut self, slot_num: u64) {
63        self.slot_num = slot_num;
64    }
65
66    fn set_beneficiary(&mut self, beneficiary: Address) {
67        self.beneficiary = beneficiary;
68    }
69
70    fn set_timestamp(&mut self, timestamp: U256) {
71        self.timestamp = timestamp;
72    }
73
74    fn set_gas_limit(&mut self, gas_limit: u64) {
75        self.gas_limit = gas_limit;
76    }
77
78    fn set_basefee(&mut self, basefee: u64) {
79        self.basefee = basefee;
80    }
81
82    fn set_difficulty(&mut self, difficulty: U256) {
83        self.difficulty = difficulty;
84    }
85
86    fn set_prevrandao(&mut self, prevrandao: Option<B256>) {
87        self.prevrandao = prevrandao;
88    }
89
90    fn set_blob_excess_gas_and_price(
91        &mut self,
92        excess_blob_gas: u64,
93        base_fee_update_fraction: u64,
94    ) {
95        self.set_blob_excess_gas_and_price(excess_blob_gas, base_fee_update_fraction);
96    }
97
98    fn set_blob_gasprice(&mut self, blob_gasprice: u128) {
99        let excess_blob_gas = self.blob_excess_gas_and_price.map_or(0, |blob| blob.excess_blob_gas);
100        self.blob_excess_gas_and_price =
101            Some(BlobExcessGasAndPrice { excess_blob_gas, blob_gasprice });
102    }
103}
104
105impl FoundryBlock for TempoBlockEnv {
106    fn set_number(&mut self, number: U256) {
107        self.inner.set_number(number);
108    }
109
110    fn set_slot_num(&mut self, slot_num: u64) {
111        self.inner.set_slot_num(slot_num);
112    }
113
114    fn set_beneficiary(&mut self, beneficiary: Address) {
115        self.inner.set_beneficiary(beneficiary);
116    }
117
118    fn set_timestamp(&mut self, timestamp: U256) {
119        self.inner.set_timestamp(timestamp);
120    }
121
122    fn set_gas_limit(&mut self, gas_limit: u64) {
123        self.inner.set_gas_limit(gas_limit);
124    }
125
126    fn set_basefee(&mut self, basefee: u64) {
127        self.inner.set_basefee(basefee);
128    }
129
130    fn set_difficulty(&mut self, difficulty: U256) {
131        self.inner.set_difficulty(difficulty);
132    }
133
134    fn set_prevrandao(&mut self, prevrandao: Option<B256>) {
135        self.inner.set_prevrandao(prevrandao);
136    }
137
138    fn set_blob_excess_gas_and_price(
139        &mut self,
140        _excess_blob_gas: u64,
141        _base_fee_update_fraction: u64,
142    ) {
143    }
144
145    fn set_blob_gasprice(&mut self, _blob_gasprice: u128) {}
146
147    fn timestamp_millis_part(&self) -> u64 {
148        self.timestamp_millis_part
149    }
150
151    fn set_timestamp_millis_part(&mut self, millis: u64) {
152        self.timestamp_millis_part = millis;
153    }
154}