Skip to content

Commit 7413f11

Browse files
authored
feat: ConfigureEvm::NextBlockEnvCtx (paradigmxyz#14801)
1 parent 6a4a1e1 commit 7413f11

File tree

17 files changed

+150
-79
lines changed

17 files changed

+150
-79
lines changed

crates/ethereum/evm/src/execute.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
dao_fork::{DAO_HARDFORK_ACCOUNTS, DAO_HARDFORK_BENEFICIARY},
55
EthEvmConfig,
66
};
7-
use alloc::{boxed::Box, sync::Arc, vec::Vec};
7+
use alloc::{borrow::Cow, boxed::Box, sync::Arc, vec::Vec};
88
use alloy_consensus::{Header, Transaction};
99
use alloy_eips::{eip4895::Withdrawals, eip6110, eip7685::Requests};
1010
use alloy_evm::FromRecoveredTx;
@@ -17,8 +17,7 @@ use reth_evm::{
1717
},
1818
state_change::post_block_balance_increments,
1919
system_calls::{OnStateHook, StateChangePostBlockSource, StateChangeSource, SystemCaller},
20-
Database, Evm, EvmEnv, EvmFactory, EvmFor, InspectorFor, NextBlockEnvAttributes,
21-
TransactionEnv,
20+
Database, Evm, EvmEnv, EvmFactory, EvmFor, InspectorFor, TransactionEnv,
2221
};
2322
use reth_execution_types::BlockExecutionResult;
2423
use reth_primitives::{
@@ -48,20 +47,20 @@ where
4847
parent_hash: block.header().parent_hash,
4948
parent_beacon_block_root: block.header().parent_beacon_block_root,
5049
ommers: &block.body().ommers,
51-
withdrawals: block.body().withdrawals.as_ref(),
50+
withdrawals: block.body().withdrawals.as_ref().map(Cow::Borrowed),
5251
}
5352
}
5453

55-
fn context_for_next_block<'a>(
54+
fn context_for_next_block(
5655
&self,
5756
parent: &SealedHeader,
58-
attributes: NextBlockEnvAttributes<'a>,
59-
) -> Self::ExecutionCtx<'a> {
57+
attributes: Self::NextBlockEnvCtx,
58+
) -> Self::ExecutionCtx<'_> {
6059
EthBlockExecutionCtx {
6160
parent_hash: parent.hash(),
6261
parent_beacon_block_root: attributes.parent_beacon_block_root,
6362
ommers: &[],
64-
withdrawals: attributes.withdrawals,
63+
withdrawals: attributes.withdrawals.map(Cow::Owned),
6564
}
6665
}
6766

@@ -79,7 +78,7 @@ where
7978
}
8079

8180
/// Context for Ethereum block execution.
82-
#[derive(Debug, Clone, Copy)]
81+
#[derive(Debug, Clone)]
8382
pub struct EthBlockExecutionCtx<'a> {
8483
/// Parent block hash.
8584
pub parent_hash: B256,
@@ -88,7 +87,7 @@ pub struct EthBlockExecutionCtx<'a> {
8887
/// Block ommers
8988
pub ommers: &'a [Header],
9089
/// Block withdrawals.
91-
pub withdrawals: Option<&'a Withdrawals>,
90+
pub withdrawals: Option<Cow<'a, Withdrawals>>,
9291
}
9392

9493
/// Block execution strategy for Ethereum.
@@ -216,7 +215,7 @@ where
216215
self.chain_spec,
217216
self.evm.block(),
218217
self.ctx.ommers,
219-
self.ctx.withdrawals,
218+
self.ctx.withdrawals.as_deref(),
220219
);
221220

222221
// Irregular state change at Ethereum DAO hardfork

crates/ethereum/evm/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ where
9696
type Error = Infallible;
9797
type TxEnv = EvmF::Tx;
9898
type Spec = SpecId;
99+
type NextBlockEnvCtx = NextBlockEnvAttributes;
99100

100101
fn evm_env(&self, header: &Self::Header) -> EvmEnv {
101102
let spec = config::revm_spec(self.chain_spec(), header);
@@ -123,7 +124,7 @@ where
123124
fn next_evm_env(
124125
&self,
125126
parent: &Self::Header,
126-
attributes: NextBlockEnvAttributes<'_>,
127+
attributes: &NextBlockEnvAttributes,
127128
) -> Result<EvmEnv, Self::Error> {
128129
// ensure we're not missing any timestamp based hardforks
129130
let spec_id = revm_spec_by_timestamp_and_block_number(

crates/ethereum/node/src/node.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use reth_ethereum_engine_primitives::{
99
EthBuiltPayload, EthPayloadAttributes, EthPayloadBuilderAttributes,
1010
};
1111
use reth_ethereum_primitives::{EthPrimitives, PooledTransaction};
12-
use reth_evm::{execute::BasicBlockExecutorProvider, ConfigureEvm};
12+
use reth_evm::{execute::BasicBlockExecutorProvider, ConfigureEvm, NextBlockEnvAttributes};
1313
use reth_network::{EthNetworkPrimitives, NetworkHandle, PeersInfo};
1414
use reth_node_api::{AddOnsContext, BlockTy, FullNodeComponents, NodeAddOns, ReceiptTy, TxTy};
1515
use reth_node_builder::{
@@ -178,7 +178,7 @@ where
178178
Primitives = EthPrimitives,
179179
Engine = EthEngineTypes,
180180
>,
181-
Evm: ConfigureEvm<TxEnv = TxEnv>,
181+
Evm: ConfigureEvm<TxEnv = TxEnv, NextBlockEnvCtx = NextBlockEnvAttributes>,
182182
>,
183183
EthApiError: FromEvmError<N::Evm>,
184184
{
@@ -218,7 +218,7 @@ where
218218
Primitives = EthPrimitives,
219219
Engine = EthEngineTypes,
220220
>,
221-
Evm: ConfigureEvm<TxEnv = TxEnv>,
221+
Evm: ConfigureEvm<TxEnv = TxEnv, NextBlockEnvCtx = NextBlockEnvAttributes>,
222222
>,
223223
EthApiError: FromEvmError<N::Evm>,
224224
{

crates/ethereum/payload/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ impl<Pool, Client, EvmConfig> EthereumPayloadBuilder<Pool, Client, EvmConfig> {
8484
// Default implementation of [PayloadBuilder] for unit type
8585
impl<Pool, Client, EvmConfig> PayloadBuilder for EthereumPayloadBuilder<Pool, Client, EvmConfig>
8686
where
87-
EvmConfig: BlockExecutionStrategyFactory<Primitives = EthPrimitives>,
87+
EvmConfig: BlockExecutionStrategyFactory<
88+
Primitives = EthPrimitives,
89+
NextBlockEnvCtx = NextBlockEnvAttributes,
90+
>,
8891
Client: StateProviderFactory + ChainSpecProvider<ChainSpec = ChainSpec> + Clone,
8992
Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TransactionSigned>>,
9093
{
@@ -139,7 +142,10 @@ pub fn default_ethereum_payload<EvmConfig, Client, Pool, F>(
139142
best_txs: F,
140143
) -> Result<BuildOutcome<EthBuiltPayload>, PayloadBuilderError>
141144
where
142-
EvmConfig: BlockExecutionStrategyFactory<Primitives = EthPrimitives>,
145+
EvmConfig: BlockExecutionStrategyFactory<
146+
Primitives = EthPrimitives,
147+
NextBlockEnvCtx = NextBlockEnvAttributes,
148+
>,
143149
Client: StateProviderFactory + ChainSpecProvider<ChainSpec = ChainSpec>,
144150
Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TransactionSigned>>,
145151
F: FnOnce(BestTransactionsAttributes) -> BestTransactionsIter<Pool>,
@@ -158,7 +164,7 @@ where
158164
prev_randao: attributes.prev_randao(),
159165
gas_limit: builder_config.gas_limit(parent_header.gas_limit),
160166
parent_beacon_block_root: attributes.parent_beacon_block_root(),
161-
withdrawals: Some(attributes.withdrawals()),
167+
withdrawals: Some(attributes.withdrawals().clone()),
162168
};
163169

164170
let mut strategy = evm_config

crates/evm/src/execute.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
use alloy_consensus::BlockHeader;
44
use alloy_evm::Evm;
55
// Re-export execution types
6-
use crate::{
7-
system_calls::OnStateHook, ConfigureEvmFor, Database, EvmFor, InspectorFor,
8-
NextBlockEnvAttributes,
9-
};
6+
use crate::{system_calls::OnStateHook, ConfigureEvmFor, Database, EvmFor, InspectorFor};
107
use alloc::{boxed::Box, vec::Vec};
118
use alloy_primitives::{
129
map::{DefaultHashBuilder, HashMap},
@@ -235,7 +232,7 @@ pub trait BlockExecutionStrategy {
235232
///
236233
/// Strategy is required to provide a way to obtain [`ExecutionCtx`] from either a complete
237234
/// [`SealedBlock`] (in case of execution of an externally obtained block), or from a parent header
238-
/// along with [`NextBlockEnvAttributes`] (in the case of block building).
235+
/// along with [`crate::ConfigureEvmEnv::NextBlockEnvCtx`] (in the case of block building).
239236
///
240237
/// For more context on the strategy design, see the documentation for [`BlockExecutionStrategy`].
241238
///
@@ -265,11 +262,11 @@ pub trait BlockExecutionStrategyFactory: ConfigureEvmFor<Self::Primitives> + 'st
265262

266263
/// Returns the configured [`BlockExecutionStrategyFactory::ExecutionCtx`] for `parent + 1`
267264
/// block.
268-
fn context_for_next_block<'a>(
265+
fn context_for_next_block(
269266
&self,
270267
parent: &SealedHeader<<Self::Primitives as NodePrimitives>::BlockHeader>,
271-
attributes: NextBlockEnvAttributes<'a>,
272-
) -> Self::ExecutionCtx<'a>;
268+
attributes: Self::NextBlockEnvCtx,
269+
) -> Self::ExecutionCtx<'_>;
273270

274271
/// Creates a strategy with given EVM and execution context.
275272
fn create_strategy<'a, DB, I>(
@@ -297,9 +294,9 @@ pub trait BlockExecutionStrategyFactory: ConfigureEvmFor<Self::Primitives> + 'st
297294
&'a self,
298295
db: &'a mut State<DB>,
299296
parent: &'a SealedHeader<<Self::Primitives as NodePrimitives>::BlockHeader>,
300-
attributes: NextBlockEnvAttributes<'a>,
297+
attributes: Self::NextBlockEnvCtx,
301298
) -> Result<Self::Strategy<'a, DB, NoOpInspector>, Self::Error> {
302-
let evm_env = self.next_evm_env(parent, attributes)?;
299+
let evm_env = self.next_evm_env(parent, &attributes)?;
303300
let evm = self.evm_with_env(db, evm_env);
304301
let ctx = self.context_for_next_block(parent, attributes);
305302
Ok(self.create_strategy(evm, ctx))

crates/evm/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone {
161161
/// Identifier of the EVM specification.
162162
type Spec: Debug + Copy + Send + Sync + 'static;
163163

164+
/// Context required for configuring next block environment.
165+
///
166+
/// Contains values that can't be derived from the parent block.
167+
type NextBlockEnvCtx: Debug + Clone;
168+
164169
/// Returns a [`TxEnv`] from a transaction and [`Address`].
165170
fn tx_env(&self, transaction: impl IntoTxEnv<Self::TxEnv>) -> Self::TxEnv {
166171
transaction.into_tx_env()
@@ -177,16 +182,16 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone {
177182
fn next_evm_env(
178183
&self,
179184
parent: &Self::Header,
180-
attributes: NextBlockEnvAttributes<'_>,
185+
attributes: &Self::NextBlockEnvCtx,
181186
) -> Result<EvmEnv<Self::Spec>, Self::Error>;
182187
}
183188

184189
/// Represents additional attributes required to configure the next block.
185190
/// This is used to configure the next block's environment
186191
/// [`ConfigureEvmEnv::next_evm_env`] and contains fields that can't be derived from the
187192
/// parent header alone (attributes that are determined by the CL.)
188-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
189-
pub struct NextBlockEnvAttributes<'a> {
193+
#[derive(Debug, Clone, PartialEq, Eq)]
194+
pub struct NextBlockEnvAttributes {
190195
/// The timestamp of the next block.
191196
pub timestamp: u64,
192197
/// The suggested fee recipient for the next block.
@@ -198,7 +203,7 @@ pub struct NextBlockEnvAttributes<'a> {
198203
/// The parent beacon block root.
199204
pub parent_beacon_block_root: Option<B256>,
200205
/// Withdrawals
201-
pub withdrawals: Option<&'a Withdrawals>,
206+
pub withdrawals: Option<Withdrawals>,
202207
}
203208

204209
/// Abstraction over transaction environment.

crates/optimism/evm/src/config.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
use alloy_consensus::BlockHeader;
22
use reth_optimism_forks::OpHardforks;
33
use revm_optimism::OpSpecId;
4+
use revm_primitives::{Address, Bytes, B256};
5+
6+
/// Context relevant for execution of a next block w.r.t OP.
7+
#[derive(Debug, Clone, PartialEq, Eq)]
8+
pub struct OpNextBlockEnvAttributes {
9+
/// The timestamp of the next block.
10+
pub timestamp: u64,
11+
/// The suggested fee recipient for the next block.
12+
pub suggested_fee_recipient: Address,
13+
/// The randomness value for the next block.
14+
pub prev_randao: B256,
15+
/// Block gas limit.
16+
pub gas_limit: u64,
17+
/// The parent beacon block root.
18+
pub parent_beacon_block_root: Option<B256>,
19+
/// Encoded EIP-1559 parameters to include into block's `extra_data` field.
20+
pub extra_data: Bytes,
21+
}
422

523
/// Map the latest active hardfork at the given header to a revm [`OpSpecId`].
624
pub fn revm_spec(chain_spec: impl OpHardforks, header: impl BlockHeader) -> OpSpecId {

crates/optimism/evm/src/execute.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use reth_evm::{
1818
},
1919
state_change::post_block_balance_increments,
2020
system_calls::{OnStateHook, StateChangePostBlockSource, StateChangeSource, SystemCaller},
21-
Database, Evm, EvmFor, InspectorFor, NextBlockEnvAttributes,
21+
Database, Evm, EvmFor, InspectorFor,
2222
};
2323
use reth_execution_types::BlockExecutionResult;
2424
use reth_optimism_chainspec::OpChainSpec;
@@ -48,11 +48,11 @@ where
4848
}
4949
}
5050

51-
fn context_for_next_block<'a>(
51+
fn context_for_next_block(
5252
&self,
5353
parent: &SealedHeader<N::BlockHeader>,
54-
attributes: NextBlockEnvAttributes<'a>,
55-
) -> Self::ExecutionCtx<'a> {
54+
attributes: Self::NextBlockEnvCtx,
55+
) -> Self::ExecutionCtx<'_> {
5656
OpBlockExecutionCtx {
5757
parent_hash: parent.hash(),
5858
parent_beacon_block_root: attributes.parent_beacon_block_root,

crates/optimism/evm/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use alloy_primitives::U256;
1818
use core::fmt::Debug;
1919
use op_alloy_consensus::EIP1559ParamError;
2020
use reth_chainspec::EthChainSpec;
21-
use reth_evm::{ConfigureEvm, ConfigureEvmEnv, EvmEnv, NextBlockEnvAttributes};
21+
use reth_evm::{ConfigureEvm, ConfigureEvmEnv, EvmEnv};
2222
use reth_optimism_chainspec::OpChainSpec;
2323
use reth_optimism_consensus::next_block_base_fee;
2424
use reth_optimism_forks::OpHardforks;
@@ -32,7 +32,7 @@ use revm::{
3232
use revm_optimism::{OpHaltReason, OpSpecId, OpTransaction};
3333

3434
mod config;
35-
pub use config::{revm_spec, revm_spec_by_timestamp_after_bedrock};
35+
pub use config::{revm_spec, revm_spec_by_timestamp_after_bedrock, OpNextBlockEnvAttributes};
3636
mod execute;
3737
pub use execute::*;
3838
pub mod l1;
@@ -98,6 +98,7 @@ where
9898
type Error = EIP1559ParamError;
9999
type TxEnv = OpTransaction<TxEnv>;
100100
type Spec = OpSpecId;
101+
type NextBlockEnvCtx = OpNextBlockEnvAttributes;
101102

102103
fn evm_env(&self, header: &Self::Header) -> EvmEnv<Self::Spec> {
103104
let spec = config::revm_spec(self.chain_spec(), header);
@@ -132,7 +133,7 @@ where
132133
fn next_evm_env(
133134
&self,
134135
parent: &Self::Header,
135-
attributes: NextBlockEnvAttributes<'_>,
136+
attributes: &Self::NextBlockEnvCtx,
136137
) -> Result<EvmEnv<Self::Spec>, Self::Error> {
137138
// ensure we're not missing any timestamp based hardforks
138139
let spec_id = revm_spec_by_timestamp_after_bedrock(&self.chain_spec, attributes.timestamp);

crates/optimism/node/src/node.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use reth_node_builder::{
2929
};
3030
use reth_optimism_chainspec::OpChainSpec;
3131
use reth_optimism_consensus::OpBeaconConsensus;
32-
use reth_optimism_evm::{BasicOpReceiptBuilder, OpEvmConfig};
32+
use reth_optimism_evm::{BasicOpReceiptBuilder, OpEvmConfig, OpNextBlockEnvAttributes};
3333
use reth_optimism_forks::OpHardforks;
3434
use reth_optimism_payload_builder::{
3535
builder::OpPayloadTransactions,
@@ -279,7 +279,10 @@ where
279279
Storage = OpStorage,
280280
Engine = OpEngineTypes,
281281
>,
282-
Evm: ConfigureEvmEnv<TxEnv = revm_optimism::OpTransaction<TxEnv>>,
282+
Evm: ConfigureEvmEnv<
283+
TxEnv = revm_optimism::OpTransaction<TxEnv>,
284+
NextBlockEnvCtx = OpNextBlockEnvAttributes,
285+
>,
283286
>,
284287
OpEthApiError: FromEvmError<N::Evm>,
285288
<<N as FullNodeComponents>::Pool as TransactionPool>::Transaction: MaybeConditionalTransaction,
@@ -351,7 +354,10 @@ where
351354
Storage = OpStorage,
352355
Engine = OpEngineTypes,
353356
>,
354-
Evm: ConfigureEvm<TxEnv = revm_optimism::OpTransaction<TxEnv>>,
357+
Evm: ConfigureEvm<
358+
TxEnv = revm_optimism::OpTransaction<TxEnv>,
359+
NextBlockEnvCtx = OpNextBlockEnvAttributes,
360+
>,
355361
>,
356362
OpEthApiError: FromEvmError<N::Evm>,
357363
<<N as FullNodeComponents>::Pool as TransactionPool>::Transaction: MaybeConditionalTransaction,

0 commit comments

Comments
 (0)