Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove tx_env_with_recovered from rpc crates #9158

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion crates/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
#[cfg(not(feature = "std"))]
extern crate alloc;

use core::ops::Deref;

use reth_chainspec::ChainSpec;
use reth_primitives::{
revm::env::{fill_block_env, fill_tx_env},
Address, Header, TransactionSigned, U256,
Address, Header, TransactionSigned, TransactionSignedEcRecovered, U256,
};
use revm::{inspector_handle_register, Database, Evm, EvmBuilder, GetInspector};
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, SpecId, TxEnv};
Expand Down Expand Up @@ -106,6 +108,13 @@ pub trait ConfigureEvm: ConfigureEvmEnv {
/// Default trait method implementation is done w.r.t. L1.
#[auto_impl::auto_impl(&, Arc)]
pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
/// Returns a [`TxEnv`] from a [`TransactionSignedEcRecovered`].
fn tx_env(&self, transaction: &TransactionSignedEcRecovered) -> TxEnv {
let mut tx_env = TxEnv::default();
self.fill_tx_env(&mut tx_env, transaction.deref(), transaction.signer());
tx_env
}

/// Fill transaction environment from a [`TransactionSigned`] and the given sender address.
fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
fill_tx_env(tx_env, transaction, sender)
Expand Down
16 changes: 10 additions & 6 deletions crates/rpc/rpc-eth-api/src/helpers/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use futures::Future;
use reth_evm::{ConfigureEvm, ConfigureEvmEnv};
use reth_primitives::{
revm::env::tx_env_with_recovered,
revm_primitives::{
BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ExecutionResult, HaltReason,
ResultAndState, TransactTo,
Expand Down Expand Up @@ -119,9 +118,11 @@ pub trait EthCall: Call + LoadPendingBlock {
// to be replayed
let transactions = block.into_transactions_ecrecovered().take(num_txs);
for tx in transactions {
let tx = tx_env_with_recovered(&tx);
let env =
EnvWithHandlerCfg::new_with_cfg_env(cfg.clone(), block_env.clone(), tx);
let env = EnvWithHandlerCfg::new_with_cfg_env(
cfg.clone(),
block_env.clone(),
Call::evm_config(&this).tx_env(&tx),
);
let (res, _) = this.transact(&mut db, env)?;
db.commit(res.state);
}
Expand Down Expand Up @@ -422,8 +423,11 @@ pub trait Call: LoadState + SpawnBlocking {
tx.hash,
)?;

let env =
EnvWithHandlerCfg::new_with_cfg_env(cfg, block_env, tx_env_with_recovered(&tx));
let env = EnvWithHandlerCfg::new_with_cfg_env(
cfg,
block_env,
Call::evm_config(&this).tx_env(&tx),
);

let (res, _) = this.transact(&mut db, env)?;
f(tx_info, res, db)
Expand Down
10 changes: 6 additions & 4 deletions crates/rpc/rpc-eth-api/src/helpers/pending_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ use std::time::{Duration, Instant};

use futures::Future;
use reth_chainspec::EthereumHardforks;
use reth_evm::ConfigureEvm;
use reth_evm::{ConfigureEvm, ConfigureEvmEnv};
use reth_execution_types::ExecutionOutcome;
use reth_primitives::{
constants::{eip4844::MAX_DATA_GAS_PER_BLOCK, BEACON_NONCE, EMPTY_ROOT_HASH},
proofs::calculate_transaction_root,
revm::env::tx_env_with_recovered,
revm_primitives::{
BlockEnv, CfgEnv, CfgEnvWithHandlerCfg, EVMError, Env, ExecutionResult, InvalidTransaction,
ResultAndState, SpecId,
Expand Down Expand Up @@ -292,8 +291,11 @@ pub trait LoadPendingBlock {
}

// Configure the environment for the block.
let env =
Env::boxed(cfg.cfg_env.clone(), block_env.clone(), tx_env_with_recovered(&tx));
let env = Env::boxed(
cfg.cfg_env.clone(),
block_env.clone(),
Self::evm_config(self).tx_env(&tx),
);

let mut evm = revm::Evm::builder().with_env(env).with_db(&mut db).build();

Expand Down
13 changes: 8 additions & 5 deletions crates/rpc/rpc-eth-api/src/helpers/trace.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Loads a pending block from database. Helper trait for `eth_` call and trace RPC methods.

use futures::Future;
use reth_evm::ConfigureEvm;
use reth_primitives::{revm::env::tx_env_with_recovered, B256};
use reth_evm::{ConfigureEvm, ConfigureEvmEnv};
use reth_primitives::B256;
use reth_revm::database::StateProviderDatabase;
use reth_rpc_eth_types::{
cache::db::{StateCacheDb, StateCacheDbRefMutWrapper, StateProviderTraitObjWrapper},
Expand Down Expand Up @@ -196,8 +196,11 @@ pub trait Trace: LoadState {
tx.hash,
)?;

let env =
EnvWithHandlerCfg::new_with_cfg_env(cfg, block_env, tx_env_with_recovered(&tx));
let env = EnvWithHandlerCfg::new_with_cfg_env(
cfg,
block_env,
Call::evm_config(&this).tx_env(&tx),
);
Comment on lines +199 to +203
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

me thinks we want a trait function for this as part of EthTransactions, but can do as followup

let (res, _) =
this.inspect(StateCacheDbRefMutWrapper(&mut db), env, &mut inspector)?;
f(tx_info, inspector, res, db)
Expand Down Expand Up @@ -308,7 +311,7 @@ pub trait Trace: LoadState {
block_number: Some(block_number),
base_fee: Some(base_fee),
};
let tx_env = tx_env_with_recovered(&tx);
let tx_env = Trace::evm_config(&this).tx_env(&tx);
(tx_info, tx_env)
})
.peekable();
Expand Down
29 changes: 21 additions & 8 deletions crates/rpc/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ use alloy_rlp::{Decodable, Encodable};
use async_trait::async_trait;
use jsonrpsee::core::RpcResult;
use reth_chainspec::EthereumHardforks;
use reth_evm::ConfigureEvmEnv;
use reth_primitives::{
revm::env::tx_env_with_recovered, Address, Block, BlockId, BlockNumberOrTag, Bytes,
TransactionSignedEcRecovered, Withdrawals, B256, U256,
Address, Block, BlockId, BlockNumberOrTag, Bytes, TransactionSignedEcRecovered, Withdrawals,
B256, U256,
};
use reth_provider::{
BlockReaderIdExt, ChainSpecProvider, EvmEnvProvider, HeaderProvider, StateProviderFactory,
TransactionVariant,
};
use reth_revm::database::StateProviderDatabase;
use reth_rpc_api::DebugApiServer;
use reth_rpc_eth_api::helpers::{EthApiSpec, EthTransactions, TraceExt};
use reth_rpc_eth_api::helpers::{Call, EthApiSpec, EthTransactions, TraceExt};
use reth_rpc_eth_types::{revm_utils::prepare_call_env, EthApiError, EthResult, StateCacheDb};
use reth_rpc_server_types::{result::internal_rpc_err, ToRpcResult};
use reth_rpc_types::{
Expand Down Expand Up @@ -99,9 +100,13 @@ where
let mut transactions = transactions.into_iter().enumerate().peekable();
while let Some((index, tx)) = transactions.next() {
let tx_hash = tx.hash;
let tx = tx_env_with_recovered(&tx);

let env = EnvWithHandlerCfg {
env: Env::boxed(cfg.cfg_env.clone(), block_env.clone(), tx),
env: Env::boxed(
cfg.cfg_env.clone(),
block_env.clone(),
Call::evm_config(this.eth_api()).tx_env(&tx),
),
handler_cfg: cfg.handler_cfg,
};
let (result, state_changes) = this.trace_transaction(
Expand Down Expand Up @@ -240,7 +245,11 @@ where
)?;

let env = EnvWithHandlerCfg {
env: Env::boxed(cfg.cfg_env.clone(), block_env, tx_env_with_recovered(&tx)),
env: Env::boxed(
cfg.cfg_env.clone(),
block_env,
Call::evm_config(this.eth_api()).tx_env(&tx),
),
handler_cfg: cfg.handler_cfg,
};

Expand Down Expand Up @@ -453,6 +462,7 @@ where
}

let this = self.clone();

self.inner
.eth_api
.spawn_with_state_at_block(at.into(), move |state| {
Expand All @@ -467,9 +477,12 @@ where

// Execute all transactions until index
for tx in transactions {
let tx = tx_env_with_recovered(&tx);
let env = EnvWithHandlerCfg {
env: Env::boxed(cfg.cfg_env.clone(), block_env.clone(), tx),
env: Env::boxed(
cfg.cfg_env.clone(),
block_env.clone(),
Call::evm_config(this.eth_api()).tx_env(&tx),
),
handler_cfg: cfg.handler_cfg,
};
let (res, _) = this.inner.eth_api.transact(&mut db, env)?;
Expand Down
13 changes: 9 additions & 4 deletions crates/rpc/rpc/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use reth_chainspec::EthereumHardforks;
use reth_consensus_common::calc::{
base_block_reward, base_block_reward_pre_merge, block_reward, ommer_reward,
};
use reth_primitives::{revm::env::tx_env_with_recovered, BlockId, Bytes, Header, B256, U256};
use reth_evm::ConfigureEvmEnv;
use reth_primitives::{BlockId, Bytes, Header, B256, U256};
use reth_provider::{BlockReader, ChainSpecProvider, EvmEnvProvider, StateProviderFactory};
use reth_revm::database::StateProviderDatabase;
use reth_rpc_api::TraceApiServer;
use reth_rpc_eth_api::helpers::TraceExt;
use reth_rpc_eth_api::helpers::{Call, TraceExt};
use reth_rpc_eth_types::{
error::{EthApiError, EthResult},
revm_utils::prepare_call_env,
Expand Down Expand Up @@ -113,8 +114,12 @@ where
let tx = recover_raw_transaction(tx)?;

let (cfg, block, at) = self.inner.eth_api.evm_env_at(block_id.unwrap_or_default()).await?;
let tx = tx_env_with_recovered(&tx.into_ecrecovered_transaction());
let env = EnvWithHandlerCfg::new_with_cfg_env(cfg, block, tx);

let env = EnvWithHandlerCfg::new_with_cfg_env(
cfg,
block,
Call::evm_config(self.eth_api()).tx_env(&tx.into_ecrecovered_transaction()),
);

let config = TracingInspectorConfig::from_parity_config(&trace_types);

Expand Down
Loading