mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: integrate TxEnv conversions (#14630)
This commit is contained in:
@ -7,6 +7,7 @@ use crate::{
|
||||
use alloc::{boxed::Box, sync::Arc, vec::Vec};
|
||||
use alloy_consensus::{Header, Transaction};
|
||||
use alloy_eips::{eip4895::Withdrawals, eip6110, eip7685::Requests};
|
||||
use alloy_evm::FromRecoveredTx;
|
||||
use alloy_primitives::{Address, B256};
|
||||
use reth_chainspec::{ChainSpec, EthereumHardfork, EthereumHardforks, MAINNET};
|
||||
use reth_evm::{
|
||||
@ -16,7 +17,7 @@ use reth_evm::{
|
||||
},
|
||||
state_change::post_block_balance_increments,
|
||||
system_calls::{OnStateHook, StateChangePostBlockSource, StateChangeSource, SystemCaller},
|
||||
ConfigureEvm, ConfigureEvmEnv, Database, Evm,
|
||||
ConfigureEvm, Database, Evm,
|
||||
};
|
||||
use reth_execution_types::BlockExecutionResult;
|
||||
use reth_primitives::{
|
||||
@ -76,7 +77,7 @@ where
|
||||
DB: Database,
|
||||
{
|
||||
let evm = self.evm_config.evm_for_block(db, block.header());
|
||||
EthExecutionStrategy::new(evm, block.sealed_block(), &self.chain_spec, &self.evm_config)
|
||||
EthExecutionStrategy::new(evm, block.sealed_block(), &self.chain_spec)
|
||||
}
|
||||
}
|
||||
|
||||
@ -118,11 +119,9 @@ impl<'a> From<&'a SealedBlock> for EthBlockExecutionInput<'a> {
|
||||
|
||||
/// Block execution strategy for Ethereum.
|
||||
#[derive(Debug)]
|
||||
pub struct EthExecutionStrategy<'a, Evm, EvmConfig> {
|
||||
pub struct EthExecutionStrategy<'a, Evm> {
|
||||
/// Reference to the [`ChainSpec`].
|
||||
chain_spec: &'a ChainSpec,
|
||||
/// How to configure the EVM.
|
||||
evm_config: EvmConfig,
|
||||
|
||||
/// Input for block execution.
|
||||
input: EthBlockExecutionInput<'a>,
|
||||
@ -137,18 +136,16 @@ pub struct EthExecutionStrategy<'a, Evm, EvmConfig> {
|
||||
gas_used: u64,
|
||||
}
|
||||
|
||||
impl<'a, Evm, EvmConfig> EthExecutionStrategy<'a, Evm, EvmConfig> {
|
||||
impl<'a, Evm> EthExecutionStrategy<'a, Evm> {
|
||||
/// Creates a new [`EthExecutionStrategy`]
|
||||
pub fn new(
|
||||
evm: Evm,
|
||||
input: impl Into<EthBlockExecutionInput<'a>>,
|
||||
chain_spec: &'a ChainSpec,
|
||||
evm_config: EvmConfig,
|
||||
) -> Self {
|
||||
Self {
|
||||
evm,
|
||||
chain_spec,
|
||||
evm_config,
|
||||
input: input.into(),
|
||||
receipts: Vec::new(),
|
||||
gas_used: 0,
|
||||
@ -157,11 +154,10 @@ impl<'a, Evm, EvmConfig> EthExecutionStrategy<'a, Evm, EvmConfig> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'db, DB, E, EvmConfig> BlockExecutionStrategy for EthExecutionStrategy<'_, E, EvmConfig>
|
||||
impl<'db, DB, E> BlockExecutionStrategy for EthExecutionStrategy<'_, E>
|
||||
where
|
||||
DB: Database + 'db,
|
||||
E: Evm<DB = &'db mut State<DB>, Tx = EvmConfig::TxEnv>,
|
||||
EvmConfig: ConfigureEvmEnv<Transaction = TransactionSigned>,
|
||||
E: Evm<DB = &'db mut State<DB>, Tx: FromRecoveredTx<TransactionSigned>>,
|
||||
{
|
||||
type Error = BlockExecutionError;
|
||||
type Primitives = EthPrimitives;
|
||||
@ -194,12 +190,11 @@ where
|
||||
.into())
|
||||
}
|
||||
|
||||
let tx_env = self.evm_config.tx_env(tx.clone());
|
||||
let hash = tx.hash();
|
||||
|
||||
// Execute transaction.
|
||||
let result_and_state =
|
||||
self.evm.transact(tx_env).map_err(move |err| BlockExecutionError::evm(err, *hash))?;
|
||||
self.evm.transact(&tx).map_err(move |err| BlockExecutionError::evm(err, *hash))?;
|
||||
self.system_caller
|
||||
.on_state(StateChangeSource::Transaction(self.receipts.len()), &result_and_state.state);
|
||||
let ResultAndState { result, state } = result_and_state;
|
||||
|
||||
@ -17,8 +17,8 @@
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::{borrow::Borrow, sync::Arc};
|
||||
use alloy_consensus::{transaction::Recovered, BlockHeader, Header};
|
||||
use alloc::sync::Arc;
|
||||
use alloy_consensus::{BlockHeader, Header};
|
||||
pub use alloy_evm::EthEvm;
|
||||
use alloy_evm::EthEvmFactory;
|
||||
use alloy_primitives::U256;
|
||||
@ -26,7 +26,6 @@ use core::{convert::Infallible, fmt::Debug};
|
||||
use reth_chainspec::{ChainSpec, EthChainSpec, MAINNET};
|
||||
use reth_evm::{ConfigureEvm, ConfigureEvmEnv, EvmEnv, NextBlockEnvAttributes};
|
||||
use reth_primitives::TransactionSigned;
|
||||
use reth_primitives_traits::transaction::execute::FillTxEnv;
|
||||
use reth_revm::{
|
||||
context::{BlockEnv, CfgEnv, TxEnv},
|
||||
context_interface::block::BlobExcessGasAndPrice,
|
||||
@ -77,17 +76,6 @@ impl ConfigureEvmEnv for EthEvmConfig {
|
||||
type TxEnv = TxEnv;
|
||||
type Spec = SpecId;
|
||||
|
||||
fn tx_env<T: Borrow<Self::Transaction>>(
|
||||
&self,
|
||||
transaction: impl Borrow<Recovered<T>>,
|
||||
) -> Self::TxEnv {
|
||||
let transaction = transaction.borrow();
|
||||
|
||||
let mut tx_env = TxEnv::default();
|
||||
transaction.tx().borrow().fill_tx_env(&mut tx_env, transaction.signer());
|
||||
tx_env
|
||||
}
|
||||
|
||||
fn evm_env(&self, header: &Self::Header) -> EvmEnv {
|
||||
let spec = config::revm_spec(self.chain_spec(), header);
|
||||
|
||||
|
||||
@ -190,7 +190,6 @@ where
|
||||
withdrawals: Some(&attributes.withdrawals),
|
||||
},
|
||||
&chain_spec,
|
||||
&evm_config,
|
||||
);
|
||||
|
||||
strategy.apply_pre_execution_changes().map_err(|err| {
|
||||
|
||||
@ -18,6 +18,7 @@ reth-primitives-traits.workspace = true
|
||||
reth-zstd-compressors = { workspace = true, optional = true }
|
||||
|
||||
# ethereum
|
||||
alloy-evm.workspace = true
|
||||
alloy-eips = { workspace = true, features = ["k256"] }
|
||||
alloy-primitives.workspace = true
|
||||
alloy-network = { workspace = true, optional = true }
|
||||
@ -68,6 +69,7 @@ std = [
|
||||
"alloy-serde?/std",
|
||||
"alloy-rpc-types-eth?/std",
|
||||
"revm-context/std",
|
||||
"alloy-evm/std",
|
||||
]
|
||||
reth-codec = [
|
||||
"std",
|
||||
|
||||
@ -10,6 +10,7 @@ use alloy_eips::{
|
||||
eip2930::AccessList,
|
||||
eip7702::SignedAuthorization,
|
||||
};
|
||||
use alloy_evm::FromRecoveredTx;
|
||||
use alloy_primitives::{
|
||||
keccak256, Address, Bytes, ChainId, PrimitiveSignature as Signature, TxHash, TxKind, B256, U256,
|
||||
};
|
||||
@ -19,7 +20,7 @@ use reth_primitives_traits::{
|
||||
crypto::secp256k1::{recover_signer, recover_signer_unchecked},
|
||||
sync::OnceLock,
|
||||
transaction::{error::TransactionConversionError, signed::RecoveryError},
|
||||
FillTxEnv, InMemorySize, SignedTransaction,
|
||||
InMemorySize, SignedTransaction,
|
||||
};
|
||||
use revm_context::TxEnv;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -750,10 +751,10 @@ impl reth_codecs::Compact for TransactionSigned {
|
||||
}
|
||||
}
|
||||
|
||||
impl FillTxEnv<TxEnv> for TransactionSigned {
|
||||
fn fill_tx_env(&self, tx_env: &mut TxEnv, sender: Address) {
|
||||
*tx_env = match self.as_ref() {
|
||||
Transaction::Legacy(tx) => TxEnv {
|
||||
impl FromRecoveredTx<TransactionSigned> for TxEnv {
|
||||
fn from_recovered_tx(tx: &TransactionSigned, sender: Address) -> Self {
|
||||
match tx.as_ref() {
|
||||
Transaction::Legacy(tx) => Self {
|
||||
gas_limit: tx.gas_limit,
|
||||
gas_price: tx.gas_price,
|
||||
gas_priority_fee: None,
|
||||
@ -769,7 +770,7 @@ impl FillTxEnv<TxEnv> for TransactionSigned {
|
||||
tx_type: 0,
|
||||
caller: sender,
|
||||
},
|
||||
Transaction::Eip2930(tx) => TxEnv {
|
||||
Transaction::Eip2930(tx) => Self {
|
||||
gas_limit: tx.gas_limit,
|
||||
gas_price: tx.gas_price,
|
||||
gas_priority_fee: None,
|
||||
@ -785,7 +786,7 @@ impl FillTxEnv<TxEnv> for TransactionSigned {
|
||||
tx_type: 1,
|
||||
caller: sender,
|
||||
},
|
||||
Transaction::Eip1559(tx) => TxEnv {
|
||||
Transaction::Eip1559(tx) => Self {
|
||||
gas_limit: tx.gas_limit,
|
||||
gas_price: tx.max_fee_per_gas,
|
||||
gas_priority_fee: Some(tx.max_priority_fee_per_gas),
|
||||
@ -801,7 +802,7 @@ impl FillTxEnv<TxEnv> for TransactionSigned {
|
||||
tx_type: 2,
|
||||
caller: sender,
|
||||
},
|
||||
Transaction::Eip4844(tx) => TxEnv {
|
||||
Transaction::Eip4844(tx) => Self {
|
||||
gas_limit: tx.gas_limit,
|
||||
gas_price: tx.max_fee_per_gas,
|
||||
gas_priority_fee: Some(tx.max_priority_fee_per_gas),
|
||||
@ -817,7 +818,7 @@ impl FillTxEnv<TxEnv> for TransactionSigned {
|
||||
tx_type: 3,
|
||||
caller: sender,
|
||||
},
|
||||
Transaction::Eip7702(tx) => TxEnv {
|
||||
Transaction::Eip7702(tx) => Self {
|
||||
gas_limit: tx.gas_limit,
|
||||
gas_price: tx.max_fee_per_gas,
|
||||
gas_priority_fee: Some(tx.max_priority_fee_per_gas),
|
||||
|
||||
Reference in New Issue
Block a user