use crate::{helpers::Ctx, EvmErrored, HasBlock, HasCfg, HasTx, Trevm, Tx};
use alloy::{
eips::eip7825::MAX_TX_GAS_LIMIT_OSAKA,
primitives::{Address, Bytes, U256},
};
use revm::{
context::{result::EVMError, ContextSetters, ContextTr, Transaction as _, TxEnv},
primitives::{hardfork::SpecId, TxKind},
state::AccountInfo,
Database, DatabaseRef, Inspector,
};
impl<Db, Insp, TrevmState> Trevm<Db, Insp, TrevmState>
where
Db: Database,
Insp: Inspector<Ctx<Db>>,
TrevmState: HasTx,
{
#[cfg(feature = "call")]
pub(crate) fn try_with_call_filler<NewState: HasCfg + HasBlock>(
self,
filler: &crate::fillers::CallFiller,
f: impl FnOnce(Self) -> Result<Trevm<Db, Insp, NewState>, EvmErrored<Db, Insp>>,
) -> Result<Trevm<Db, Insp, NewState>, EvmErrored<Db, Insp>> {
self.try_with_cfg(filler, |this| this.try_with_block(filler, f))
}
#[cfg(feature = "estimate_gas")]
pub(crate) fn try_with_estimate_gas_filler<E>(
self,
filler: &crate::fillers::GasEstimationFiller,
f: impl FnOnce(Self) -> Result<Self, EvmErrored<Db, Insp, E>>,
) -> Result<Self, EvmErrored<Db, Insp, E>> {
self.try_with_cfg(filler, |this| this.try_with_tx(filler, f))
}
pub fn tx(&self) -> &TxEnv {
self.inner.tx()
}
pub fn is_transfer(&self) -> bool {
self.inner.tx().input().is_empty() && self.to().is_call()
}
pub fn is_create(&self) -> bool {
self.to().is_create()
}
pub fn input(&self) -> &Bytes {
self.tx().input()
}
pub fn to(&self) -> TxKind {
self.tx().kind()
}
pub fn value(&self) -> U256 {
self.tx().value()
}
pub fn gas_limit(&self) -> u64 {
self.tx().gas_limit()
}
pub fn gas_price(&self) -> u128 {
self.tx().gas_price()
}
pub fn caller(&self) -> Address {
self.tx().caller()
}
pub fn caller_account(&mut self) -> Result<AccountInfo, EVMError<<Db as Database>::Error>> {
self.try_read_account(self.caller())
.map(Option::unwrap_or_default)
.map_err(EVMError::Database)
}
pub fn callee(&self) -> Option<Address> {
self.to().into()
}
pub fn callee_account(
&mut self,
) -> Result<Option<AccountInfo>, EVMError<<Db as Database>::Error>> {
self.callee().map_or(Ok(None), |addr| {
self.try_read_account(addr)
.map(Option::unwrap_or_default)
.map(Some)
.map_err(EVMError::Database)
})
}
pub fn callee_account_ref(&self) -> Result<Option<AccountInfo>, <Db as DatabaseRef>::Error>
where
Db: DatabaseRef,
{
self.callee().map_or(Ok(None), |addr| self.try_read_account_ref(addr))
}
pub fn with_tx<T, F, NewState>(mut self, t: &T, f: F) -> Trevm<Db, Insp, NewState>
where
T: Tx,
F: FnOnce(Self) -> Trevm<Db, Insp, NewState>,
NewState: HasTx,
{
let previous = self.inner.tx().clone();
t.fill_tx(&mut self.inner);
let mut this = f(self);
this.inner.ctx.set_tx(previous);
this
}
pub fn try_with_tx<T, F, NewState, E>(
mut self,
t: &T,
f: F,
) -> Result<Trevm<Db, Insp, NewState>, EvmErrored<Db, Insp, E>>
where
T: Tx,
F: FnOnce(Self) -> Result<Trevm<Db, Insp, NewState>, EvmErrored<Db, Insp, E>>,
NewState: HasTx,
{
let previous = self.inner.tx().clone();
t.fill_tx(&mut self.inner);
match f(self) {
Ok(mut evm) => {
evm.inner.ctx.set_tx(previous);
Ok(evm)
}
Err(mut evm) => {
evm.inner.ctx.set_tx(previous);
Err(evm)
}
}
}
pub fn caller_gas_allowance(&mut self) -> Result<u64, EVMError<<Db as Database>::Error>> {
let gas_price = self.gas_price();
self.try_gas_allowance(self.caller(), gas_price).map_err(EVMError::Database)
}
pub fn cap_tx_gas_to_allowance(&mut self) -> Result<u64, EVMError<<Db as Database>::Error>> {
let allowance = self.caller_gas_allowance()?;
self.inner.modify_tx(|tx| tx.gas_limit = tx.gas_limit.min(allowance));
Ok(self.gas_limit())
}
pub fn cap_tx_gas_to_block_limit(&mut self) -> u64 {
let block_gas_limit = self.block_gas_limit();
self.inner.modify_tx(|tx| tx.gas_limit = tx.gas_limit.min(block_gas_limit));
self.tx().gas_limit
}
pub fn cap_tx_gas(&mut self) -> Result<u64, EVMError<<Db as Database>::Error>> {
self.cap_tx_gas_to_block_limit();
if self.spec_id() >= SpecId::OSAKA {
self.cap_tx_gas_to_eip7825();
}
self.cap_tx_gas_to_allowance()
}
pub fn cap_tx_gas_to_eip7825(&mut self) -> u64 {
self.inner.modify_tx(|tx| tx.gas_limit = tx.gas_limit.min(MAX_TX_GAS_LIMIT_OSAKA));
self.tx().gas_limit
}
}
#[cfg(test)]
mod tests {
use crate::{
fillers::DisableChainIdCheck,
test_utils::{test_trevm_with_funds, ALICE, BOB, LOG_DEPLOYED_BYTECODE},
NoopBlock, NoopCfg, TrevmBuilder,
};
use alloy::{
consensus::constants::ETH_TO_WEI,
network::{TransactionBuilder, TransactionBuilder7702},
primitives::{Address, U256},
rpc::types::{Authorization, TransactionRequest},
signers::SignerSync,
};
use revm::{
context::transaction::AuthorizationTr,
database::InMemoryDB,
primitives::{bytes, hardfork::SpecId},
state::Bytecode,
};
#[test]
fn test_estimate_gas_simple_transfer() {
let trevm = test_trevm_with_funds(&[
(ALICE.address(), U256::from(ETH_TO_WEI)),
(BOB.address(), U256::from(ETH_TO_WEI)),
]);
let tx = TransactionRequest::default()
.from(ALICE.address())
.to(BOB.address())
.value(U256::from(ETH_TO_WEI / 2));
let (estimation, _trevm) =
trevm.fill_cfg(&NoopCfg).fill_block(&NoopBlock).fill_tx(&tx).estimate_gas().unwrap();
assert!(estimation.is_success());
assert_eq!(estimation.gas_used(), 21000);
}
#[test]
fn test_7702_authorization_estimation() {
let db = InMemoryDB::default();
let log_address = Address::repeat_byte(0x32);
let mut trevm = TrevmBuilder::new().with_db(db).with_spec_id(SpecId::OSAKA).build_trevm();
let _ = trevm.test_set_balance(ALICE.address(), U256::from(ETH_TO_WEI));
let _ = trevm.set_bytecode_unchecked(log_address, Bytecode::new_raw(LOG_DEPLOYED_BYTECODE));
let authorization = Authorization {
chain_id: U256::ZERO,
address: log_address,
nonce: 0,
};
let signature = BOB.sign_hash_sync(&authorization.signature_hash()).unwrap();
let signed_authorization = authorization.into_signed(signature);
assert_eq!(signed_authorization.authority().unwrap(), BOB.address());
let tx = TransactionRequest::default()
.from(ALICE.address())
.to(BOB.address())
.with_authorization_list(vec![signed_authorization])
.with_input(bytes!("0x7b3ab2d0"));
let (estimation, trevm) = trevm
.fill_cfg(&DisableChainIdCheck)
.fill_block(&NoopBlock)
.fill_tx(&tx)
.estimate_gas()
.unwrap();
assert!(estimation.is_success());
let tx = tx.with_gas_limit(estimation.limit());
let output = trevm.clear_tx().fill_tx(&tx).run().unwrap().accept();
assert!(output.0.is_success());
assert_eq!(output.0.logs().len(), 1);
}
}