trevm/evm/
has_tx.rs

1use crate::{helpers::Ctx, EvmErrored, HasBlock, HasCfg, HasTx, Trevm, Tx};
2use alloy::primitives::{Address, Bytes, U256};
3use revm::{
4    context::{result::EVMError, ContextSetters, ContextTr, Transaction as _, TxEnv},
5    primitives::TxKind,
6    state::AccountInfo,
7    Database, DatabaseRef, Inspector,
8};
9
10impl<Db, Insp, TrevmState> Trevm<Db, Insp, TrevmState>
11where
12    Db: Database,
13    Insp: Inspector<Ctx<Db>>,
14    TrevmState: HasTx,
15{
16    #[cfg(feature = "call")]
17    pub(crate) fn try_with_call_filler<NewState: HasCfg + HasBlock>(
18        self,
19        filler: &crate::fillers::CallFiller,
20        f: impl FnOnce(Self) -> Result<Trevm<Db, Insp, NewState>, EvmErrored<Db, Insp>>,
21    ) -> Result<Trevm<Db, Insp, NewState>, EvmErrored<Db, Insp>> {
22        // override all relevant env bits
23        self.try_with_cfg(filler, |this| this.try_with_block(filler, f))
24    }
25
26    /// Convenience function to use the estimator to fill both Cfg and Tx, and
27    /// run a fallible function.
28    #[cfg(feature = "estimate_gas")]
29    pub(crate) fn try_with_estimate_gas_filler<E>(
30        self,
31        filler: &crate::fillers::GasEstimationFiller,
32        f: impl FnOnce(Self) -> Result<Self, EvmErrored<Db, Insp, E>>,
33    ) -> Result<Self, EvmErrored<Db, Insp, E>> {
34        self.try_with_cfg(filler, |this| this.try_with_tx(filler, f))
35    }
36
37    /// Get a reference to the loaded tx env that will be executed.
38    pub fn tx(&self) -> &TxEnv {
39        self.inner.tx()
40    }
41    /// True if the transaction is a simple transfer.
42    pub fn is_transfer(&self) -> bool {
43        self.inner.tx().input().is_empty() && self.to().is_call()
44    }
45
46    /// True if the transaction is a contract creation.
47    pub fn is_create(&self) -> bool {
48        self.to().is_create()
49    }
50
51    /// Get a reference to the transaction input data, which will be used as
52    /// calldata or initcode during EVM execution.
53    pub fn input(&self) -> &Bytes {
54        self.tx().input()
55    }
56
57    /// Read the target of the transaction.
58    pub fn to(&self) -> TxKind {
59        self.tx().kind()
60    }
61
62    /// Read the value in wei of the transaction.
63    pub fn value(&self) -> U256 {
64        self.tx().value()
65    }
66
67    /// Get the gas limit of the loaded transaction.
68    pub fn gas_limit(&self) -> u64 {
69        self.tx().gas_limit()
70    }
71
72    /// Get the gas price of the loaded transaction.
73    pub fn gas_price(&self) -> u128 {
74        self.tx().gas_price()
75    }
76
77    /// Get the address of the caller.
78    pub fn caller(&self) -> Address {
79        self.tx().caller()
80    }
81
82    /// Get the account of the caller. Error if the DB errors.
83    pub fn caller_account(&mut self) -> Result<AccountInfo, EVMError<<Db as Database>::Error>> {
84        self.try_read_account(self.caller())
85            .map(Option::unwrap_or_default)
86            .map_err(EVMError::Database)
87    }
88
89    /// Get the address of the callee. `None` if `Self::is_create` is true.
90    pub fn callee(&self) -> Option<Address> {
91        self.to().into()
92    }
93
94    /// Get the account of the callee.
95    ///
96    /// Returns as follows:
97    /// - if `Self::is_create` is true, `Ok(None)`
98    /// - if the callee account does not exist, `Ok(AccountInfo::default())`
99    /// - if the DB errors, `Err(EVMError::Database(err))`
100    pub fn callee_account(
101        &mut self,
102    ) -> Result<Option<AccountInfo>, EVMError<<Db as Database>::Error>> {
103        self.callee().map_or(Ok(None), |addr| {
104            self.try_read_account(addr)
105                .map(Option::unwrap_or_default)
106                .map(Some)
107                .map_err(EVMError::Database)
108        })
109    }
110
111    /// Get the account of the callee. `None` if `Self::is_create` is true,
112    /// error if the DB errors.
113    pub fn callee_account_ref(&self) -> Result<Option<AccountInfo>, <Db as DatabaseRef>::Error>
114    where
115        Db: DatabaseRef,
116    {
117        self.callee().map_or(Ok(None), |addr| self.try_read_account_ref(addr))
118    }
119
120    /// Run a function with the provided transaction, then restore the previous
121    /// transaction.
122    pub fn with_tx<T, F, NewState>(mut self, t: &T, f: F) -> Trevm<Db, Insp, NewState>
123    where
124        T: Tx,
125        F: FnOnce(Self) -> Trevm<Db, Insp, NewState>,
126        NewState: HasTx,
127    {
128        let previous = self.inner.tx().clone();
129        t.fill_tx(&mut self.inner);
130        let mut this = f(self);
131        this.inner.ctx.set_tx(previous);
132        this
133    }
134
135    /// Run a fallible function with the provided transaction, then restore the
136    /// previous transaction.
137    pub fn try_with_tx<T, F, NewState, E>(
138        mut self,
139        t: &T,
140        f: F,
141    ) -> Result<Trevm<Db, Insp, NewState>, EvmErrored<Db, Insp, E>>
142    where
143        T: Tx,
144        F: FnOnce(Self) -> Result<Trevm<Db, Insp, NewState>, EvmErrored<Db, Insp, E>>,
145        NewState: HasTx,
146    {
147        let previous = self.inner.tx().clone();
148        t.fill_tx(&mut self.inner);
149        match f(self) {
150            Ok(mut evm) => {
151                evm.inner.ctx.set_tx(previous);
152                Ok(evm)
153            }
154            Err(mut evm) => {
155                evm.inner.ctx.set_tx(previous);
156                Err(evm)
157            }
158        }
159    }
160
161    /// Return the maximum gas that the caller can purchase. This is the balance
162    /// of the caller divided by the gas price.
163    pub fn caller_gas_allowance(&mut self) -> Result<u64, EVMError<<Db as Database>::Error>> {
164        // Avoid DB read if gas price is zero
165        let gas_price = self.gas_price();
166        self.try_gas_allowance(self.caller(), gas_price).map_err(EVMError::Database)
167    }
168
169    /// This function caps the gas limit of the transaction to the allowance of
170    /// the caller.
171    ///
172    /// This is useful for e.g. call simulation, where the exact amount of gas
173    /// used is less important than ensuring that the call succeeds and returns
174    /// a meaningful result.
175    ///
176    /// # Returns
177    ///
178    /// The gas limit after the operation.
179    pub fn cap_tx_gas_to_allowance(&mut self) -> Result<u64, EVMError<<Db as Database>::Error>> {
180        let allowance = self.caller_gas_allowance()?;
181
182        self.inner.modify_tx(|tx| tx.gas_limit = tx.gas_limit.min(allowance));
183
184        Ok(self.gas_limit())
185    }
186
187    /// Cap the gas limit of the transaction to the minimum of the block gas
188    /// limit and the transaction's gas limit.
189    ///
190    /// This is useful for ensuring that the transaction does not exceed the
191    /// block gas limit, e.g. during call simulation.
192    ///
193    /// # Returns
194    ///
195    /// The gas limit after the operation.
196    pub fn cap_tx_gas_to_block_limit(&mut self) -> u64 {
197        let block_gas_limit = self.block_gas_limit();
198
199        self.inner.modify_tx(|tx| tx.gas_limit = tx.gas_limit.min(block_gas_limit));
200
201        self.tx().gas_limit
202    }
203
204    /// This function caps the gas limit of the transaction to the minimum of
205    /// the block limit and the caller's gas allowance.
206    ///
207    /// This is equivalent to calling [`Self::cap_tx_gas_to_block_limit`] and
208    /// [`Self::cap_tx_gas_to_allowance`] in sequence.
209    ///
210    /// # Returns
211    ///
212    /// The gas limit after the operation.
213    pub fn cap_tx_gas(&mut self) -> Result<u64, EVMError<<Db as Database>::Error>> {
214        self.cap_tx_gas_to_block_limit();
215        self.cap_tx_gas_to_allowance()
216    }
217}
218
219#[cfg(test)]
220mod tests {
221    use crate::{
222        fillers::DisableChainIdCheck,
223        test_utils::{test_trevm_with_funds, ALICE, BOB, LOG_DEPLOYED_BYTECODE},
224        NoopBlock, NoopCfg, TrevmBuilder,
225    };
226    use alloy::{
227        consensus::constants::ETH_TO_WEI,
228        network::{TransactionBuilder, TransactionBuilder7702},
229        primitives::{Address, U256},
230        rpc::types::{Authorization, TransactionRequest},
231        signers::SignerSync,
232    };
233    use revm::{
234        context::transaction::AuthorizationTr,
235        database::InMemoryDB,
236        primitives::{bytes, hardfork::SpecId},
237        state::Bytecode,
238    };
239
240    #[test]
241    fn test_estimate_gas_simple_transfer() {
242        let trevm = test_trevm_with_funds(&[
243            (ALICE.address(), U256::from(ETH_TO_WEI)),
244            (BOB.address(), U256::from(ETH_TO_WEI)),
245        ]);
246
247        let tx = TransactionRequest::default()
248            .from(ALICE.address())
249            .to(BOB.address())
250            .value(U256::from(ETH_TO_WEI / 2));
251
252        let (estimation, _trevm) =
253            trevm.fill_cfg(&NoopCfg).fill_block(&NoopBlock).fill_tx(&tx).estimate_gas().unwrap();
254
255        assert!(estimation.is_success());
256        // The gas used should correspond to a simple transfer.
257        assert_eq!(estimation.gas_used(), 21000);
258    }
259
260    #[test]
261    fn test_7702_authorization_estimation() {
262        // Insert the LogContract code
263        let db = InMemoryDB::default();
264        let log_address = Address::repeat_byte(0x32);
265
266        // Set up trevm, and test balances.
267        let mut trevm =
268            TrevmBuilder::new().with_db(db).with_spec_id(SpecId::PRAGUE).build_trevm().unwrap();
269        let _ = trevm.test_set_balance(ALICE.address(), U256::from(ETH_TO_WEI));
270        let _ = trevm.set_bytecode_unchecked(log_address, Bytecode::new_raw(LOG_DEPLOYED_BYTECODE));
271
272        // Bob will sign the authorization.
273        let authorization = Authorization {
274            chain_id: U256::ZERO,
275            address: log_address,
276            // We know Bob's nonce is 0.
277            nonce: 0,
278        };
279        let signature = BOB.sign_hash_sync(&authorization.signature_hash()).unwrap();
280        let signed_authorization = authorization.into_signed(signature);
281        assert_eq!(signed_authorization.authority().unwrap(), BOB.address());
282
283        let tx = TransactionRequest::default()
284            .from(ALICE.address())
285            .to(BOB.address())
286            .with_authorization_list(vec![signed_authorization])
287            .with_input(bytes!("0x7b3ab2d0")); // emitHello()
288
289        let (estimation, trevm) = trevm
290            .fill_cfg(&DisableChainIdCheck)
291            .fill_block(&NoopBlock)
292            .fill_tx(&tx)
293            .estimate_gas()
294            .unwrap();
295
296        assert!(estimation.is_success());
297
298        let tx = tx.with_gas_limit(estimation.limit());
299
300        let output = trevm.clear_tx().fill_tx(&tx).run().unwrap().accept();
301
302        assert!(output.0.is_success());
303        assert_eq!(output.0.logs().len(), 1);
304    }
305}
306
307// Some code above and documentation is adapted from the revm crate, and is
308// reproduced here under the terms of the MIT license.
309//
310// MIT License
311//
312// Copyright (c) 2021-2024 draganrakita
313//
314// Permission is hereby granted, free of charge, to any person obtaining a copy
315// of this software and associated documentation files (the "Software"), to deal
316// in the Software without restriction, including without limitation the rights
317// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
318// copies of the Software, and to permit persons to whom the Software is
319// furnished to do so, subject to the following conditions:
320//
321// The above copyright notice and this permission notice shall be included in all
322// copies or substantial portions of the Software.
323//
324// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
325// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
326// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
327// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
328// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
329// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
330// SOFTWARE.
331
332// Some code above is reproduced from `reth`. It is reused here under the MIT
333// license.
334//
335// The MIT License (MIT)
336//
337// Copyright (c) 2022-2024 Reth Contributors
338//
339// Permission is hereby granted, free of charge, to any person obtaining a copy
340// of this software and associated documentation files (the "Software"), to deal
341// in the Software without restriction, including without limitation the rights
342// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
343// copies of the Software, and to permit persons to whom the Software is
344// furnished to do so, subject to the following conditions:
345//
346// The above copyright notice and this permission notice shall be included in
347// all copies or substantial portions of the Software.
348//
349// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
350// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
351// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
352// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
353// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
354// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
355// THE SOFTWARE.