chore: Use traits from reth 1.5.0

This commit is contained in:
sprites0
2025-07-03 04:18:18 +00:00
parent a11335da80
commit 6a6f993766
9 changed files with 137 additions and 140 deletions

View File

@ -1,17 +1,15 @@
use super::HlEvmInner;
use crate::evm::{spec::HlSpecId, transaction::HlTxTr};
use reth_revm::context::ContextTr;
use revm::{
context::{Cfg, JournalOutput},
context_interface::{Block, JournalTr},
handler::instructions::EthInstructions,
interpreter::interpreter::EthInterpreter,
Context, Database,
context::Cfg, context_interface::Block, handler::instructions::EthInstructions,
interpreter::interpreter::EthInterpreter, Context, Database,
};
/// Trait that allows for hl HlEvm to be built.
pub trait HlBuilder: Sized {
/// Type of the context.
type Context;
type Context: ContextTr;
/// Build the hl with an inspector.
fn build_hl_with_inspector<INSP>(
@ -20,13 +18,12 @@ pub trait HlBuilder: Sized {
) -> HlEvmInner<Self::Context, INSP, EthInstructions<EthInterpreter, Self::Context>>;
}
impl<BLOCK, TX, CFG, DB, JOURNAL> HlBuilder for Context<BLOCK, TX, CFG, DB, JOURNAL>
impl<BLOCK, TX, CFG, DB> HlBuilder for Context<BLOCK, TX, CFG, DB>
where
BLOCK: Block,
TX: HlTxTr,
CFG: Cfg<Spec = HlSpecId>,
DB: Database,
JOURNAL: JournalTr<Database = DB, FinalOutput = JournalOutput>,
{
type Context = Self;

View File

@ -1,29 +1,26 @@
use super::HlEvmInner;
use crate::evm::{handler::HlHandler, spec::HlSpecId, transaction::HlTxTr};
use crate::evm::{spec::HlSpecId, transaction::HlTxTr};
use revm::{
context::{ContextSetters, JournalOutput},
context::{result::HaltReason, ContextSetters},
context_interface::{
result::{EVMError, ExecutionResult, ResultAndState},
Cfg, ContextTr, Database, JournalTr,
},
handler::{instructions::EthInstructions, EthFrame, EvmTr, Handler, PrecompileProvider},
inspector::{InspectCommitEvm, InspectEvm, Inspector, InspectorHandler, JournalExt},
handler::{instructions::EthInstructions, PrecompileProvider},
inspector::{InspectCommitEvm, InspectEvm, Inspector, JournalExt},
interpreter::{interpreter::EthInterpreter, InterpreterResult},
state::EvmState,
DatabaseCommit, ExecuteCommitEvm, ExecuteEvm,
};
// Type alias for HL context
pub trait HlContextTr:
ContextTr<Journal: JournalTr<FinalOutput = JournalOutput>, Tx: HlTxTr, Cfg: Cfg<Spec = HlSpecId>>
ContextTr<Journal: JournalTr<State = EvmState>, Tx: HlTxTr, Cfg: Cfg<Spec = HlSpecId>>
{
}
impl<T> HlContextTr for T where
T: ContextTr<
Journal: JournalTr<FinalOutput = JournalOutput>,
Tx: HlTxTr,
Cfg: Cfg<Spec = HlSpecId>,
>
T: ContextTr<Journal: JournalTr<State = EvmState>, Tx: HlTxTr, Cfg: Cfg<Spec = HlSpecId>>
{
}
@ -36,23 +33,32 @@ where
CTX: HlContextTr + ContextSetters,
PRECOMPILE: PrecompileProvider<CTX, Output = InterpreterResult>,
{
type Output = Result<ResultAndState, HlError<CTX>>;
type ExecutionResult = ExecutionResult<HaltReason>;
type State = EvmState;
type Error = HlError<CTX>;
type Tx = <CTX as ContextTr>::Tx;
type Block = <CTX as ContextTr>::Block;
fn set_tx(&mut self, tx: Self::Tx) {
self.0.ctx.set_tx(tx);
#[inline]
fn transact_one(&mut self, tx: Self::Tx) -> Result<Self::ExecutionResult, Self::Error> {
self.0.transact_one(tx)
}
#[inline]
fn finalize(&mut self) -> Self::State {
self.0.finalize()
}
#[inline]
fn set_block(&mut self, block: Self::Block) {
self.0.ctx.set_block(block);
self.0.set_block(block);
}
fn replay(&mut self) -> Self::Output {
let mut h = HlHandler::<_, _, EthFrame<_, _, _>>::new();
h.run(self)
#[inline]
fn replay(&mut self) -> Result<ResultAndState<HaltReason>, Self::Error> {
self.0.replay()
}
}
@ -62,13 +68,8 @@ where
CTX: HlContextTr<Db: DatabaseCommit> + ContextSetters,
PRECOMPILE: PrecompileProvider<CTX, Output = InterpreterResult>,
{
type CommitOutput = Result<ExecutionResult, HlError<CTX>>;
fn replay_commit(&mut self) -> Self::CommitOutput {
self.replay().map(|r| {
self.ctx().db().commit(r.state);
r.result
})
fn commit(&mut self, state: Self::State) {
self.0.commit(state);
}
}
@ -82,12 +83,11 @@ where
type Inspector = INSP;
fn set_inspector(&mut self, inspector: Self::Inspector) {
self.0.inspector = inspector;
self.0.set_inspector(inspector);
}
fn inspect_replay(&mut self) -> Self::Output {
let mut h = HlHandler::<_, _, EthFrame<_, _, _>>::new();
h.inspect_run(self)
fn inspect_one_tx(&mut self, tx: Self::Tx) -> Result<Self::ExecutionResult, Self::Error> {
self.0.inspect_one_tx(tx)
}
}

View File

@ -1,12 +1,13 @@
use revm::{
context::{ContextSetters, Evm as EvmCtx},
context::{ContextSetters, Evm, FrameStack},
context_interface::ContextTr,
handler::{
evm::{ContextDbError, FrameInitResult},
instructions::{EthInstructions, InstructionProvider},
EthPrecompiles, EvmTr, PrecompileProvider,
EthFrame, EthPrecompiles, EvmTr, FrameInitOrResult, FrameTr, PrecompileProvider,
},
inspector::{InspectorEvmTr, JournalExt},
interpreter::{interpreter::EthInterpreter, Interpreter, InterpreterAction, InterpreterTypes},
interpreter::{interpreter::EthInterpreter, InterpreterResult},
Inspector,
};
@ -14,19 +15,23 @@ pub mod builder;
pub mod ctx;
mod exec;
pub struct HlEvmInner<CTX, INSP, I = EthInstructions<EthInterpreter, CTX>, P = EthPrecompiles>(
pub EvmCtx<CTX, INSP, I, P>,
);
pub struct HlEvmInner<
CTX: ContextTr,
INSP,
I = EthInstructions<EthInterpreter, CTX>,
P = EthPrecompiles,
>(pub Evm<CTX, INSP, I, P, EthFrame<EthInterpreter>>);
impl<CTX: ContextTr, INSP>
HlEvmInner<CTX, INSP, EthInstructions<EthInterpreter, CTX>, EthPrecompiles>
{
pub fn new(ctx: CTX, inspector: INSP) -> Self {
Self(EvmCtx {
Self(Evm {
ctx,
inspector,
instruction: EthInstructions::new_mainnet(),
precompiles: EthPrecompiles::default(),
frame_stack: FrameStack::new(),
})
}
@ -42,12 +47,9 @@ impl<CTX: ContextTr, INSP>
impl<CTX, INSP, I, P> InspectorEvmTr for HlEvmInner<CTX, INSP, I, P>
where
CTX: ContextTr<Journal: JournalExt> + ContextSetters,
I: InstructionProvider<
Context = CTX,
InterpreterTypes: InterpreterTypes<Output = InterpreterAction>,
>,
I: InstructionProvider<Context = CTX, InterpreterTypes = EthInterpreter>,
INSP: Inspector<CTX, I::InterpreterTypes>,
P: PrecompileProvider<CTX>,
P: PrecompileProvider<CTX, Output = InterpreterResult>,
{
type Inspector = INSP;
@ -59,41 +61,29 @@ where
(&mut self.0.ctx, &mut self.0.inspector)
}
fn run_inspect_interpreter(
fn ctx_inspector_frame(
&mut self,
interpreter: &mut Interpreter<
<Self::Instructions as InstructionProvider>::InterpreterTypes,
>,
) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output
{
self.0.run_inspect_interpreter(interpreter)
) -> (&mut Self::Context, &mut Self::Inspector, &mut Self::Frame) {
(&mut self.0.ctx, &mut self.0.inspector, self.0.frame_stack.get())
}
fn ctx_inspector_frame_instructions(
&mut self,
) -> (&mut Self::Context, &mut Self::Inspector, &mut Self::Frame, &mut Self::Instructions) {
(&mut self.0.ctx, &mut self.0.inspector, self.0.frame_stack.get(), &mut self.0.instruction)
}
}
impl<CTX, INSP, I, P> EvmTr for HlEvmInner<CTX, INSP, I, P>
where
CTX: ContextTr,
I: InstructionProvider<
Context = CTX,
InterpreterTypes: InterpreterTypes<Output = InterpreterAction>,
>,
P: PrecompileProvider<CTX>,
I: InstructionProvider<Context = CTX, InterpreterTypes = EthInterpreter>,
P: PrecompileProvider<CTX, Output = InterpreterResult>,
{
type Context = CTX;
type Instructions = I;
type Precompiles = P;
fn run_interpreter(
&mut self,
interpreter: &mut Interpreter<
<Self::Instructions as InstructionProvider>::InterpreterTypes,
>,
) -> <<Self::Instructions as InstructionProvider>::InterpreterTypes as InterpreterTypes>::Output
{
let context = &mut self.0.ctx;
let instructions = &mut self.0.instruction;
interpreter.run_plain(instructions.instruction_table(), context)
}
type Frame = EthFrame<EthInterpreter>;
fn ctx(&mut self) -> &mut Self::Context {
&mut self.0.ctx
@ -110,6 +100,30 @@ where
fn ctx_precompiles(&mut self) -> (&mut Self::Context, &mut Self::Precompiles) {
(&mut self.0.ctx, &mut self.0.precompiles)
}
fn frame_stack(&mut self) -> &mut FrameStack<Self::Frame> {
&mut self.0.frame_stack
}
fn frame_init(
&mut self,
frame_input: <Self::Frame as FrameTr>::FrameInit,
) -> Result<FrameInitResult<'_, Self::Frame>, ContextDbError<Self::Context>> {
self.0.frame_init(frame_input)
}
fn frame_run(
&mut self,
) -> Result<FrameInitOrResult<Self::Frame>, ContextDbError<Self::Context>> {
self.0.frame_run()
}
fn frame_return_result(
&mut self,
result: <Self::Frame as FrameTr>::FrameResult,
) -> Result<Option<<Self::Frame as FrameTr>::FrameResult>, ContextDbError<Self::Context>> {
self.0.frame_return_result(result)
}
}
// #[cfg(test)]

View File

@ -135,7 +135,9 @@ impl<ChainSpec: EthChainSpec + HlHardforks> Consensus<HlBlock> for HlConsensus<C
mod reth_copy;
impl<ChainSpec: EthChainSpec + HlHardforks> FullConsensus<HlPrimitives> for HlConsensus<ChainSpec> {
impl<ChainSpec: EthChainSpec<Header = alloy_consensus::Header> + HlHardforks>
FullConsensus<HlPrimitives> for HlConsensus<ChainSpec>
{
fn validate_block_post_execution(
&self,
block: &RecoveredBlock<HlBlock>,

View File

@ -14,7 +14,6 @@ use alloy_consensus::{BlockHeader, Header, Transaction as _, TxReceipt, EMPTY_OM
use alloy_eips::merge::BEACON_NONCE;
use alloy_primitives::{Log, U256};
use reth_chainspec::{EthChainSpec, EthereumHardforks, Hardforks};
use reth_ethereum_forks::EthereumHardfork;
use reth_evm::{
block::{BlockExecutionError, BlockExecutorFactory, BlockExecutorFor},
eth::{receipt_builder::ReceiptBuilder, EthBlockExecutionCtx},
@ -67,7 +66,7 @@ where
..
} = input;
let timestamp = evm_env.block_env.timestamp;
let timestamp = evm_env.block_env.timestamp.saturating_to();
// Filter out system tx receipts
let transactions_for_root: Vec<TransactionSigned> =
@ -122,7 +121,7 @@ where
mix_hash: evm_env.block_env.prevrandao.unwrap_or_default(),
nonce: BEACON_NONCE.into(),
base_fee_per_gas: Some(evm_env.block_env.basefee),
number: evm_env.block_env.number,
number: evm_env.block_env.number.saturating_to(),
gas_limit: evm_env.block_env.gas_limit,
difficulty: evm_env.block_env.difficulty,
gas_used: *gas_used,
@ -264,8 +263,6 @@ where
}
}
const EIP1559_INITIAL_BASE_FEE: u64 = 0;
impl ConfigureEvm for HlEvmConfig
where
Self: Send + Sync + Unpin + Clone + 'static,
@ -297,7 +294,7 @@ where
CfgEnv::new().with_chain_id(self.chain_spec().chain().id()).with_spec(spec);
if let Some(blob_params) = &blob_params {
cfg_env.set_blob_max_count(blob_params.max_blob_count);
cfg_env.set_max_blobs_per_tx(blob_params.max_blobs_per_tx);
}
// TODO: enable only for system transactions
@ -315,9 +312,9 @@ where
let eth_spec = spec.into_eth_spec();
let block_env = BlockEnv {
number: header.number(),
number: U256::from(header.number()),
beneficiary: header.beneficiary(),
timestamp: header.timestamp(),
timestamp: U256::from(header.timestamp()),
difficulty: if eth_spec >= SpecId::MERGE { U256::ZERO } else { header.difficulty() },
prevrandao: if eth_spec >= SpecId::MERGE { header.mix_hash() } else { None },
gas_limit: header.gas_limit(),
@ -346,43 +343,19 @@ where
// if the parent block did not have excess blob gas (i.e. it was pre-cancun), but it is
// cancun now, we need to set the excess blob gas to the default value(0)
let blob_excess_gas_and_price = parent
.maybe_next_block_excess_blob_gas(
self.chain_spec().blob_params_at_timestamp(attributes.timestamp),
)
.or_else(|| (spec_id.into_eth_spec().is_enabled_in(SpecId::CANCUN)).then_some(0))
.map(|gas| BlobExcessGasAndPrice::new(gas, false));
let blob_excess_gas_and_price = spec_id
.into_eth_spec()
.is_enabled_in(SpecId::CANCUN)
.then_some(BlobExcessGasAndPrice { excess_blob_gas: 0, blob_gasprice: 0 });
let mut basefee = parent.next_block_base_fee(
let basefee = parent.next_block_base_fee(
self.chain_spec().base_fee_params_at_timestamp(attributes.timestamp),
);
let mut gas_limit = U256::from(parent.gas_limit);
// If we are on the London fork boundary, we need to multiply the parent's gas limit by the
// elasticity multiplier to get the new gas limit.
if self
.chain_spec()
.inner
.fork(EthereumHardfork::London)
.transitions_at_block(parent.number + 1)
{
let elasticity_multiplier = self
.chain_spec()
.base_fee_params_at_timestamp(attributes.timestamp)
.elasticity_multiplier;
// multiply the gas limit by the elasticity multiplier
gas_limit *= U256::from(elasticity_multiplier);
// set the base fee to the initial base fee from the EIP-1559 spec
basefee = Some(EIP1559_INITIAL_BASE_FEE)
}
let block_env = BlockEnv {
number: parent.number() + 1,
number: U256::from(parent.number() + 1),
beneficiary: attributes.suggested_fee_recipient,
timestamp: attributes.timestamp,
timestamp: U256::from(attributes.timestamp),
difficulty: U256::ZERO,
prevrandao: Some(attributes.prev_randao),
gas_limit: attributes.gas_limit,

View File

@ -16,8 +16,8 @@ use reth_evm::{
block::{BlockValidationError, CommitChanges},
eth::receipt_builder::ReceiptBuilder,
execute::{BlockExecutionError, BlockExecutor},
precompiles::{DynPrecompile, PrecompilesMap},
Database, Evm, FromRecoveredTx, FromTxWithEncoded, IntoTxEnv, OnStateHook, RecoveredTx,
precompiles::{DynPrecompile, PrecompileInput, PrecompilesMap},
Database, Evm, FromRecoveredTx, FromTxWithEncoded, IntoTxEnv, OnStateHook,
};
use reth_provider::BlockExecutionResult;
use reth_revm::State;
@ -227,8 +227,8 @@ where
for (address, precompile) in ctx.read_precompile_calls.iter() {
let precompile = precompile.clone();
precompiles_mut.apply_precompile(address, |_| {
Some(DynPrecompile::from(move |data: &[u8], gas: u64| {
run_precompile(&precompile, data, gas)
Some(DynPrecompile::from(move |input: PrecompileInput| -> PrecompileResult {
run_precompile(&precompile, input.data, input.gas)
}))
});
}

View File

@ -7,8 +7,8 @@ use crate::evm::{
spec::HlSpecId,
transaction::HlTxEnv,
};
use reth_evm::{precompiles::PrecompilesMap, EvmEnv, EvmFactory};
use reth_revm::{Context, Database};
use reth_evm::{precompiles::PrecompilesMap, Database, EvmEnv, EvmFactory};
use reth_revm::Context;
use revm::{
context::{
result::{EVMError, HaltReason},
@ -25,16 +25,15 @@ use revm::{
pub struct HlEvmFactory;
impl EvmFactory for HlEvmFactory {
type Evm<DB: Database<Error: Send + Sync + 'static>, I: Inspector<HlContext<DB>>> =
HlEvm<DB, I, Self::Precompiles>;
type Context<DB: Database<Error: Send + Sync + 'static>> = HlContext<DB>;
type Evm<DB: Database, I: Inspector<HlContext<DB>>> = HlEvm<DB, I, Self::Precompiles>;
type Context<DB: Database> = HlContext<DB>;
type Tx = HlTxEnv<TxEnv>;
type Error<DBError: core::error::Error + Send + Sync + 'static> = EVMError<DBError>;
type HaltReason = HaltReason;
type Spec = HlSpecId;
type Precompiles = PrecompilesMap;
fn create_evm<DB: Database<Error: Send + Sync + 'static>>(
fn create_evm<DB: Database>(
&self,
db: DB,
input: EvmEnv<HlSpecId>,

View File

@ -97,8 +97,7 @@ where
tx: Self::Tx,
) -> Result<ResultAndState<Self::HaltReason>, Self::Error> {
if self.inspect {
self.inner.set_tx(tx);
self.inner.inspect_replay()
self.inner.inspect_tx(tx)
} else {
self.inner.transact(tx)
}

View File

@ -3,7 +3,6 @@ use crate::{
node::{
primitives::TransactionSigned,
rpc::{HlEthApi, HlNodeCore},
HlBlock, HlPrimitives,
},
};
use alloy_consensus::{BlockHeader, ReceiptEnvelope, TxType};
@ -16,8 +15,7 @@ use reth::{
rpc::{
eth::EthApiTypes,
server_types::eth::{
error::FromEvmError, receipt::build_receipt, EthApiError, EthReceiptBuilder,
PendingBlock,
error::FromEvmError, receipt::build_receipt, EthApiError, PendingBlock,
},
types::{BlockId, TransactionReceipt},
},
@ -25,7 +23,8 @@ use reth::{
};
use reth_chainspec::{EthChainSpec, EthereumHardforks};
use reth_evm::{ConfigureEvm, NextBlockEnvAttributes};
use reth_primitives_traits::BlockBody as _;
use reth_primitives::NodePrimitives;
use reth_primitives_traits::{BlockBody as _, SignedTransaction as _};
use reth_provider::{
BlockReader, ChainSpecProvider, HeaderProvider, ProviderBlock, ProviderReceipt, ProviderTx,
StateProviderFactory,
@ -33,7 +32,7 @@ use reth_provider::{
use reth_rpc_eth_api::{
helpers::{EthBlocks, LoadBlock, LoadPendingBlock, LoadReceipt, SpawnBlocking},
types::RpcTypes,
FromEthApiError, RpcNodeCore, RpcNodeCoreExt, RpcReceipt,
FromEthApiError, RpcConvert, RpcNodeCore, RpcNodeCoreExt, RpcReceipt,
};
impl<N> EthBlocks for HlEthApi<N>
@ -68,7 +67,7 @@ where
.enumerate()
.map(|(idx, (tx, receipt))| {
let meta = TransactionMeta {
tx_hash: *tx.0.tx_hash(),
tx_hash: *tx.tx_hash(),
index: idx as u64,
block_hash,
block_number,
@ -76,8 +75,15 @@ where
excess_blob_gas,
timestamp,
};
EthReceiptBuilder::new(&tx.0, meta, receipt, &receipts, blob_params)
.map(|builder| builder.build())
build_receipt(tx, meta, receipt, &receipts, blob_params, |receipt_with_bloom| {
match receipt.tx_type {
TxType::Legacy => ReceiptEnvelope::Legacy(receipt_with_bloom),
TxType::Eip2930 => ReceiptEnvelope::Eip2930(receipt_with_bloom),
TxType::Eip1559 => ReceiptEnvelope::Eip1559(receipt_with_bloom),
TxType::Eip4844 => ReceiptEnvelope::Eip4844(receipt_with_bloom),
TxType::Eip7702 => ReceiptEnvelope::Eip7702(receipt_with_bloom),
}
})
})
.collect::<Result<Vec<_>, Self::Error>>()
.map(Some);
@ -108,17 +114,23 @@ where
Header = alloy_rpc_types_eth::Header<ProviderHeader<Self::Provider>>,
>,
Error: FromEvmError<Self::Evm>,
RpcConvert: RpcConvert<Network = Self::NetworkTypes>,
>,
N: RpcNodeCore<
Provider: BlockReaderIdExt<
Transaction = TransactionSigned,
Block = HlBlock,
Receipt = Receipt,
Header = alloy_consensus::Header,
> + ChainSpecProvider<ChainSpec: EthChainSpec + EthereumHardforks>
Provider: BlockReaderIdExt
+ ChainSpecProvider<ChainSpec: EthChainSpec + EthereumHardforks>
+ StateProviderFactory,
Pool: TransactionPool<Transaction: PoolTransaction<Consensus = ProviderTx<N::Provider>>>,
Evm: ConfigureEvm<Primitives = HlPrimitives, NextBlockEnvCtx = NextBlockEnvAttributes>,
Evm: ConfigureEvm<
Primitives = <Self as RpcNodeCore>::Primitives,
NextBlockEnvCtx: From<NextBlockEnvAttributes>,
>,
Primitives: NodePrimitives<
BlockHeader = ProviderHeader<Self::Provider>,
SignedTx = ProviderTx<Self::Provider>,
Receipt = ProviderReceipt<Self::Provider>,
Block = ProviderBlock<Self::Provider>,
>,
>,
{
#[inline]
@ -141,7 +153,8 @@ where
gas_limit: parent.gas_limit(),
parent_beacon_block_root: parent.parent_beacon_block_root(),
withdrawals: None,
})
}
.into())
}
}