Skip to main content

foundry_evm_core/env/
block.rs

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