Skip to content
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/optimism/flashblocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ workspace = true
[dependencies]
# reth
reth-optimism-primitives = { workspace = true, features = ["serde"] }
reth-optimism-evm.workspace = true
reth-chain-state = { workspace = true, features = ["serde"] }
reth-primitives-traits = { workspace = true, features = ["serde"] }
reth-execution-types = { workspace = true, features = ["serde"] }
Expand Down
9 changes: 6 additions & 3 deletions crates/optimism/flashblocks/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FlashBlockService, FlashBlockWsStream};
use crate::{ExecutionPayloadBaseV1, FlashBlockService, FlashBlockWsStream};
use futures_util::StreamExt;
use reth_evm::ConfigureEvm;
use reth_primitives_traits::{BlockTy, HeaderTy, NodePrimitives, ReceiptTy};
Expand All @@ -23,7 +23,10 @@ where
N: NodePrimitives,
EvmConfig: ConfigureEvm<
Primitives = N,
NextBlockEnvCtx: BuildPendingEnv<N::BlockHeader> + Unpin + 'static,
NextBlockEnvCtx: BuildPendingEnv<N::BlockHeader>
+ From<ExecutionPayloadBaseV1>
+ Unpin
+ 'static,
> + 'static,
Provider: StateProviderFactory
+ BlockReaderIdExt<
Expand All @@ -35,7 +38,7 @@ where
+ 'static,
{
let stream = FlashBlockWsStream::new(ws_url);
let mut service = FlashBlockService::new(stream, evm_config, provider, ());
let mut service = FlashBlockService::new(stream, evm_config, provider);
let (tx, rx) = watch::channel(None);

tokio::spawn(async move {
Expand Down
14 changes: 14 additions & 0 deletions crates/optimism/flashblocks/src/payload.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloy_eips::eip4895::Withdrawal;
use alloy_primitives::{Address, Bloom, Bytes, B256, U256};
use alloy_rpc_types_engine::PayloadId;
use reth_optimism_evm::OpNextBlockEnvAttributes;
use reth_optimism_primitives::OpReceipt;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
Expand Down Expand Up @@ -93,3 +94,16 @@ pub struct ExecutionPayloadFlashblockDeltaV1 {
/// The withdrawals root of the block.
pub withdrawals_root: B256,
}

impl From<ExecutionPayloadBaseV1> for OpNextBlockEnvAttributes {
fn from(value: ExecutionPayloadBaseV1) -> Self {
Self {
timestamp: value.timestamp,
suggested_fee_recipient: value.fee_recipient,
prev_randao: value.prev_randao,
gas_limit: value.gas_limit,
parent_beacon_block_root: Some(value.parent_beacon_block_root),
extra_data: value.extra_data,
}
}
}
28 changes: 14 additions & 14 deletions crates/optimism/flashblocks/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::FlashBlock;
use crate::{ExecutionPayloadBaseV1, FlashBlock};
use alloy_eips::{eip2718::WithEncoded, BlockNumberOrTag, Decodable2718};
use eyre::OptionExt;
use futures_util::{Stream, StreamExt};
use reth_chain_state::ExecutedBlock;
use reth_errors::RethError;
Expand All @@ -12,7 +13,6 @@ use reth_primitives_traits::{
AlloyBlockHeader, BlockTy, HeaderTy, NodePrimitives, ReceiptTy, SignedTransaction,
};
use reth_revm::{database::StateProviderDatabase, db::State};
use reth_rpc_eth_api::helpers::pending_block::PendingEnvBuilder;
use reth_rpc_eth_types::{EthApiError, PendingBlock};
use reth_storage_api::{BlockReaderIdExt, StateProviderFactory};
use std::{
Expand All @@ -31,33 +31,30 @@ pub struct FlashBlockService<
S,
EvmConfig: ConfigureEvm<Primitives = N, NextBlockEnvCtx: Unpin>,
Provider,
Builder,
> {
rx: S,
current: Option<PendingBlock<N>>,
blocks: Vec<FlashBlock>,
evm_config: EvmConfig,
provider: Provider,
builder: Builder,
}

impl<
N: NodePrimitives,
S,
EvmConfig: ConfigureEvm<Primitives = N, NextBlockEnvCtx: Unpin>,
EvmConfig: ConfigureEvm<Primitives = N, NextBlockEnvCtx: From<ExecutionPayloadBaseV1> + Unpin>,
Provider: StateProviderFactory
+ BlockReaderIdExt<
Header = HeaderTy<N>,
Block = BlockTy<N>,
Transaction = N::SignedTx,
Receipt = ReceiptTy<N>,
>,
Builder: PendingEnvBuilder<EvmConfig>,
> FlashBlockService<N, S, EvmConfig, Provider, Builder>
> FlashBlockService<N, S, EvmConfig, Provider>
{
/// Constructs a new `FlashBlockService` that receives [`FlashBlock`]s from `rx` stream.
pub const fn new(rx: S, evm_config: EvmConfig, provider: Provider, builder: Builder) -> Self {
Self { rx, current: None, blocks: Vec::new(), evm_config, provider, builder }
pub const fn new(rx: S, evm_config: EvmConfig, provider: Provider) -> Self {
Self { rx, current: None, blocks: Vec::new(), evm_config, provider }
}

/// Adds the `block` into the collection.
Expand Down Expand Up @@ -115,15 +112,19 @@ impl<
.latest_header()?
.ok_or(EthApiError::HeaderNotFound(BlockNumberOrTag::Latest.into()))?;

let latest_attrs = self.builder.pending_env_attributes(&latest)?;
let attrs = self
.blocks
.iter()
.find_map(|v| v.base.clone())
.ok_or_eyre("Missing base flashblock")?;

let state_provider = self.provider.history_by_block_hash(latest.hash())?;
let state = StateProviderDatabase::new(&state_provider);
let mut db = State::builder().with_database(state).with_bundle_update().build();

let mut builder = self
.evm_config
.builder_for_next_block(&mut db, &latest, latest_attrs)
.builder_for_next_block(&mut db, &latest, attrs.into())
.map_err(RethError::other)?;

builder.apply_pre_execution_changes()?;
Expand Down Expand Up @@ -161,16 +162,15 @@ impl<
impl<
N: NodePrimitives,
S: Stream<Item = eyre::Result<FlashBlock>> + Unpin,
EvmConfig: ConfigureEvm<Primitives = N, NextBlockEnvCtx: Unpin>,
EvmConfig: ConfigureEvm<Primitives = N, NextBlockEnvCtx: From<ExecutionPayloadBaseV1> + Unpin>,
Provider: StateProviderFactory
+ BlockReaderIdExt<
Header = HeaderTy<N>,
Block = BlockTy<N>,
Transaction = N::SignedTx,
Receipt = ReceiptTy<N>,
> + Unpin,
Builder: PendingEnvBuilder<EvmConfig>,
> Stream for FlashBlockService<N, S, EvmConfig, Provider, Builder>
> Stream for FlashBlockService<N, S, EvmConfig, Provider>
{
type Item = eyre::Result<PendingBlock<N>>;

Expand Down
10 changes: 8 additions & 2 deletions crates/optimism/rpc/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use reqwest::Url;
use reth_evm::ConfigureEvm;
use reth_node_api::{FullNodeComponents, FullNodeTypes, HeaderTy};
use reth_node_builder::rpc::{EthApiBuilder, EthApiCtx};
use reth_optimism_flashblocks::{launch_wss_flashblocks_service, FlashBlockRx};
use reth_optimism_flashblocks::{
launch_wss_flashblocks_service, ExecutionPayloadBaseV1, FlashBlockRx,
};
use reth_rpc::eth::{core::EthApiInner, DevSigner};
use reth_rpc_eth_api::{
helpers::{
Expand Down Expand Up @@ -390,7 +392,11 @@ impl<NetworkT> OpEthApiBuilder<NetworkT> {
impl<N, NetworkT> EthApiBuilder<N> for OpEthApiBuilder<NetworkT>
where
N: FullNodeComponents<
Evm: ConfigureEvm<NextBlockEnvCtx: BuildPendingEnv<HeaderTy<N::Types>> + Unpin>,
Evm: ConfigureEvm<
NextBlockEnvCtx: BuildPendingEnv<HeaderTy<N::Types>>
+ From<ExecutionPayloadBaseV1>
+ Unpin,
>,
>,
NetworkT: RpcTypes,
OpRpcConvert<N, NetworkT>: RpcConvert<Network = NetworkT>,
Expand Down
1 change: 1 addition & 0 deletions examples/custom-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ reth-codecs.workspace = true
reth-network-peers.workspace = true
reth-node-builder.workspace = true
reth-optimism-forks.workspace = true
reth-optimism-flashblocks.workspace = true
reth-db-api.workspace = true
reth-op = { workspace = true, features = ["node", "pool", "rpc"] }
reth-payload-builder.workspace = true
Expand Down
7 changes: 7 additions & 0 deletions examples/custom-node/src/evm/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use reth_op::{
node::{OpEvmConfig, OpNextBlockEnvAttributes, OpRethReceiptBuilder},
primitives::SignedTransaction,
};
use reth_optimism_flashblocks::ExecutionPayloadBaseV1;
use reth_rpc_api::eth::helpers::pending_block::BuildPendingEnv;
use std::sync::Arc;

Expand Down Expand Up @@ -136,6 +137,12 @@ pub struct CustomNextBlockEnvAttributes {
extension: u64,
}

impl From<ExecutionPayloadBaseV1> for CustomNextBlockEnvAttributes {
fn from(value: ExecutionPayloadBaseV1) -> Self {
Self { inner: value.into(), extension: 0 }
}
}
Comment on lines +140 to +144
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this seems fine


impl BuildPendingEnv<CustomHeader> for CustomNextBlockEnvAttributes {
fn build_pending_env(parent: &SealedHeader<CustomHeader>) -> Self {
Self {
Expand Down
Loading