Skip to content

Commit

Permalink
refactor: integrate BuiltPayload::Primitives (#13484)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
klkvr and mattsse authored Dec 23, 2024
1 parent 4c1208e commit af1c9b7
Show file tree
Hide file tree
Showing 24 changed files with 136 additions and 78 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/reth-bench/src/bench/new_payload_fcu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Command {
let versioned_hashes: Vec<B256> =
block.body.blob_versioned_hashes_iter().copied().collect();
let parent_beacon_block_root = block.parent_beacon_block_root;
let payload = block_to_payload(block);
let payload = block_to_payload(block).0;

debug!(?block_number, "Sending payload",);

Expand Down
2 changes: 1 addition & 1 deletion bin/reth-bench/src/bench/new_payload_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Command {
let versioned_hashes: Vec<B256> =
block.body.blob_versioned_hashes_iter().copied().collect();
let parent_beacon_block_root = block.parent_beacon_block_root;
let payload = block_to_payload(block);
let payload = block_to_payload(block).0;

let block_number = payload.block_number();

Expand Down
2 changes: 1 addition & 1 deletion crates/e2e-test-utils/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ where
let tx_hash = self.rpc.inject_tx(raw_tx).await?;
let (payload, eth_attr) = self.advance_block().await?;
let block_hash = payload.block().hash();
let block_number = payload.block().number;
let block_number = payload.block().number();
self.assert_new_block(tx_hash, block_hash, block_number).await?;
chain.push((payload, eth_attr));
}
Expand Down
3 changes: 2 additions & 1 deletion crates/e2e-test-utils/src/payload.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use futures_util::StreamExt;
use reth_node_api::BlockBody;
use reth_payload_builder::{PayloadBuilderHandle, PayloadId};
use reth_payload_builder_primitives::{Events, PayloadBuilder};
use reth_payload_primitives::{BuiltPayload, PayloadBuilderAttributes, PayloadTypes};
Expand Down Expand Up @@ -57,7 +58,7 @@ impl<T: PayloadTypes> PayloadTestContext<T> {
pub async fn wait_for_built_payload(&self, payload_id: PayloadId) {
loop {
let payload = self.payload_builder.best_payload(payload_id).await.unwrap().unwrap();
if payload.block().body.transactions.is_empty() {
if payload.block().body.transactions().is_empty() {
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
continue
}
Expand Down
1 change: 0 additions & 1 deletion crates/engine/local/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ reth-payload-builder-primitives.workspace = true
reth-payload-primitives.workspace = true
reth-provider.workspace = true
reth-prune.workspace = true
reth-rpc-types-compat.workspace = true
reth-transaction-pool.workspace = true
reth-stages-api.workspace = true

Expand Down
31 changes: 8 additions & 23 deletions crates/engine/local/src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
use alloy_consensus::BlockHeader;
use alloy_primitives::{TxHash, B256};
use alloy_rpc_types_engine::{CancunPayloadFields, ExecutionPayloadSidecar, ForkchoiceState};
use alloy_rpc_types_engine::ForkchoiceState;
use eyre::OptionExt;
use futures_util::{stream::Fuse, StreamExt};
use reth_chainspec::EthereumHardforks;
use reth_engine_primitives::{BeaconEngineMessage, EngineApiMessageVersion, EngineTypes};
use reth_payload_builder::PayloadBuilderHandle;
use reth_payload_builder_primitives::PayloadBuilder;
use reth_payload_primitives::{BuiltPayload, PayloadAttributesBuilder, PayloadKind, PayloadTypes};
use reth_provider::{BlockReader, ChainSpecProvider};
use reth_rpc_types_compat::engine::payload::block_to_payload;
use reth_provider::BlockReader;
use reth_transaction_pool::TransactionPool;
use std::{
future::Future,
Expand Down Expand Up @@ -75,9 +73,7 @@ impl Future for MiningMode {

/// Local miner advancing the chain/
#[derive(Debug)]
pub struct LocalMiner<EngineT: EngineTypes, Provider, B> {
/// Provider to read the current tip of the chain.
provider: Provider,
pub struct LocalMiner<EngineT: EngineTypes, B> {
/// The payload attribute builder for the engine
payload_attributes_builder: B,
/// Sender for events to engine.
Expand All @@ -92,15 +88,14 @@ pub struct LocalMiner<EngineT: EngineTypes, Provider, B> {
last_block_hashes: Vec<B256>,
}

impl<EngineT, Provider, B> LocalMiner<EngineT, Provider, B>
impl<EngineT, B> LocalMiner<EngineT, B>
where
EngineT: EngineTypes,
Provider: BlockReader + ChainSpecProvider<ChainSpec: EthereumHardforks> + 'static,
B: PayloadAttributesBuilder<<EngineT as PayloadTypes>::PayloadAttributes>,
{
/// Spawns a new [`LocalMiner`] with the given parameters.
pub fn spawn_new(
provider: Provider,
provider: impl BlockReader,
payload_attributes_builder: B,
to_engine: UnboundedSender<BeaconEngineMessage<EngineT>>,
mode: MiningMode,
Expand All @@ -110,7 +105,6 @@ where
provider.sealed_header(provider.best_block_number().unwrap()).unwrap().unwrap();

let miner = Self {
provider,
payload_attributes_builder,
to_engine,
mode,
Expand Down Expand Up @@ -211,21 +205,12 @@ where

let block = payload.block();

let cancun_fields =
self.provider.chain_spec().is_cancun_active_at_timestamp(block.timestamp).then(|| {
CancunPayloadFields {
parent_beacon_block_root: block.parent_beacon_block_root.unwrap(),
versioned_hashes: block.body.blob_versioned_hashes_iter().copied().collect(),
}
});

let (tx, rx) = oneshot::channel();
let (payload, sidecar) = EngineT::block_to_payload(payload.block().clone());
self.to_engine.send(BeaconEngineMessage::NewPayload {
payload: block_to_payload(payload.block().clone()),
payload,
// todo: prague support
sidecar: cancun_fields
.map(ExecutionPayloadSidecar::v3)
.unwrap_or_else(ExecutionPayloadSidecar::none),
sidecar,
tx,
})?;

Expand Down
9 changes: 8 additions & 1 deletion crates/engine/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use reth_payload_primitives::{
PayloadTypes,
};
use reth_payload_primitives::{InvalidPayloadAttributesError, PayloadAttributes};
use reth_primitives::SealedBlockFor;
use reth_primitives::{NodePrimitives, SealedBlockFor};
use reth_primitives_traits::Block;
use serde::{de::DeserializeOwned, ser::Serialize};

Expand Down Expand Up @@ -80,6 +80,13 @@ pub trait EngineTypes:
+ Send
+ Sync
+ 'static;

/// Converts a [`BuiltPayload`] into an [`ExecutionPayload`] and [`ExecutionPayloadSidecar`].
fn block_to_payload(
block: SealedBlockFor<
<<Self::BuiltPayload as BuiltPayload>::Primitives as NodePrimitives>::Block,
>,
) -> (ExecutionPayload, ExecutionPayloadSidecar);
}

/// Type that validates an [`ExecutionPayload`].
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/util/src/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ where
.seal_slow();

Ok((
block_to_payload(reorg_block),
block_to_payload(reorg_block).0,
// todo(onbjerg): how do we support execution requests?
reorg_target
.header
Expand Down
19 changes: 15 additions & 4 deletions crates/ethereum/engine-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ pub use alloy_rpc_types_engine::{
};
pub use payload::{EthBuiltPayload, EthPayloadBuilderAttributes};
use reth_chainspec::ChainSpec;
use reth_engine_primitives::{EngineTypes, EngineValidator, PayloadValidator};
use reth_engine_primitives::{BuiltPayload, EngineTypes, EngineValidator, PayloadValidator};
use reth_payload_primitives::{
validate_version_specific_fields, EngineApiMessageVersion, EngineObjectValidationError,
PayloadOrAttributes, PayloadTypes,
};
use reth_payload_validator::ExecutionPayloadValidator;
use reth_primitives::{Block, SealedBlock};
use reth_primitives::{Block, NodePrimitives, SealedBlock, SealedBlockFor};
use reth_rpc_types_compat::engine::payload::block_to_payload;

/// The types used in the default mainnet ethereum beacon consensus engine.
#[derive(Debug, Default, Clone, serde::Deserialize, serde::Serialize)]
Expand All @@ -39,9 +40,11 @@ impl<T: PayloadTypes> PayloadTypes for EthEngineTypes<T> {
type PayloadBuilderAttributes = T::PayloadBuilderAttributes;
}

impl<T: PayloadTypes> EngineTypes for EthEngineTypes<T>
impl<T> EngineTypes for EthEngineTypes<T>
where
T::BuiltPayload: TryInto<ExecutionPayloadV1>
T: PayloadTypes,
T::BuiltPayload: BuiltPayload<Primitives: NodePrimitives<Block = reth_primitives::Block>>
+ TryInto<ExecutionPayloadV1>
+ TryInto<ExecutionPayloadEnvelopeV2>
+ TryInto<ExecutionPayloadEnvelopeV3>
+ TryInto<ExecutionPayloadEnvelopeV4>,
Expand All @@ -50,6 +53,14 @@ where
type ExecutionPayloadEnvelopeV2 = ExecutionPayloadEnvelopeV2;
type ExecutionPayloadEnvelopeV3 = ExecutionPayloadEnvelopeV3;
type ExecutionPayloadEnvelopeV4 = ExecutionPayloadEnvelopeV4;

fn block_to_payload(
block: SealedBlockFor<
<<Self::BuiltPayload as BuiltPayload>::Primitives as NodePrimitives>::Block,
>,
) -> (ExecutionPayload, ExecutionPayloadSidecar) {
block_to_payload(block)
}
}

/// A default payload type for [`EthEngineTypes`]
Expand Down
25 changes: 7 additions & 18 deletions crates/ethereum/engine-primitives/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use alloy_rpc_types_engine::{
};
use reth_chain_state::ExecutedBlock;
use reth_payload_primitives::{BuiltPayload, PayloadBuilderAttributes};
use reth_primitives::SealedBlock;
use reth_primitives::{EthPrimitives, SealedBlock};
use reth_rpc_types_compat::engine::payload::{
block_to_payload_v1, block_to_payload_v3, convert_block_to_payload_field_v2,
};
Expand Down Expand Up @@ -89,6 +89,8 @@ impl EthBuiltPayload {
}

impl BuiltPayload for EthBuiltPayload {
type Primitives = EthPrimitives;

fn block(&self) -> &SealedBlock {
&self.block
}
Expand All @@ -107,6 +109,8 @@ impl BuiltPayload for EthBuiltPayload {
}

impl BuiltPayload for &EthBuiltPayload {
type Primitives = EthPrimitives;

fn block(&self) -> &SealedBlock {
(**self).block()
}
Expand Down Expand Up @@ -166,24 +170,9 @@ impl From<EthBuiltPayload> for ExecutionPayloadEnvelopeV3 {

impl From<EthBuiltPayload> for ExecutionPayloadEnvelopeV4 {
fn from(value: EthBuiltPayload) -> Self {
let EthBuiltPayload { block, fees, sidecars, requests, .. } = value;

Self {
envelope_inner: ExecutionPayloadEnvelopeV3 {
execution_payload: block_to_payload_v3(Arc::unwrap_or_clone(block)),
block_value: fees,
// From the engine API spec:
//
// > Client software **MAY** use any heuristics to decide whether to set
// `shouldOverrideBuilder` flag or not. If client software does not implement any
// heuristic this flag **SHOULD** be set to `false`.
//
// Spec:
// <https://github.com/ethereum/execution-apis/blob/fe8e13c288c592ec154ce25c534e26cb7ce0530d/src/engine/cancun.md#specification-2>
should_override_builder: false,
blobs_bundle: sidecars.into_iter().map(Into::into).collect::<Vec<_>>().into(),
},
execution_requests: requests.unwrap_or_default(),
execution_requests: value.requests.clone().unwrap_or_default(),
envelope_inner: value.into(),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/node/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use reth_db_api::{
database_metrics::{DatabaseMetadata, DatabaseMetrics},
Database,
};
use reth_engine_primitives::EngineTypes;
use reth_engine_primitives::{BuiltPayload, EngineTypes};
use reth_trie_db::StateCommitment;

/// The type that configures the essential types of an Ethereum-like node.
Expand All @@ -41,7 +41,7 @@ pub trait NodeTypes: Send + Sync + Unpin + 'static {
/// The type that configures an Ethereum-like node with an engine for consensus.
pub trait NodeTypesWithEngine: NodeTypes {
/// The node's engine types, defining the interaction with the consensus engine.
type Engine: EngineTypes;
type Engine: EngineTypes<BuiltPayload: BuiltPayload<Primitives = Self::Primitives>>;
}

/// A helper trait that is downstream of the [`NodeTypesWithEngine`] trait and adds database to the
Expand Down Expand Up @@ -225,7 +225,7 @@ where
impl<P, E, C, SC, S> NodeTypesWithEngine for AnyNodeTypesWithEngine<P, E, C, SC, S>
where
P: NodePrimitives + Send + Sync + Unpin + 'static,
E: EngineTypes + Send + Sync + Unpin,
E: EngineTypes<BuiltPayload: BuiltPayload<Primitives = P>> + Send + Sync + Unpin,
C: EthChainSpec<Header = P::BlockHeader> + 'static,
SC: StateCommitment,
S: Default + Send + Sync + Unpin + Debug + 'static,
Expand Down
1 change: 1 addition & 0 deletions crates/optimism/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ reth-revm = { workspace = true, features = ["std"] }
reth-beacon-consensus.workspace = true
reth-trie-db.workspace = true
reth-rpc-server-types.workspace = true
reth-rpc-types-compat.workspace = true
reth-tasks = { workspace = true, optional = true }

# op-reth
Expand Down
15 changes: 13 additions & 2 deletions crates/optimism/node/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use reth_node_api::{
EngineObjectValidationError, MessageValidationKind, PayloadOrAttributes, PayloadTypes,
VersionSpecificValidationError,
},
validate_version_specific_fields, EngineTypes, EngineValidator, PayloadValidator,
validate_version_specific_fields, BuiltPayload, EngineTypes, EngineValidator, NodePrimitives,
PayloadValidator,
};
use reth_optimism_chainspec::OpChainSpec;
use reth_optimism_forks::{OpHardfork, OpHardforks};
use reth_optimism_payload_builder::{OpBuiltPayload, OpPayloadBuilderAttributes};
use reth_payload_validator::ExecutionPayloadValidator;
use reth_primitives::{Block, SealedBlockFor};
use reth_rpc_types_compat::engine::payload::block_to_payload;
use std::sync::Arc;

/// The types used in the optimism beacon consensus engine.
Expand All @@ -36,7 +38,8 @@ impl<T: PayloadTypes> PayloadTypes for OpEngineTypes<T> {

impl<T: PayloadTypes> EngineTypes for OpEngineTypes<T>
where
T::BuiltPayload: TryInto<ExecutionPayloadV1>
T::BuiltPayload: BuiltPayload<Primitives: NodePrimitives<Block = reth_primitives::Block>>
+ TryInto<ExecutionPayloadV1>
+ TryInto<ExecutionPayloadEnvelopeV2>
+ TryInto<OpExecutionPayloadEnvelopeV3>
+ TryInto<OpExecutionPayloadEnvelopeV4>,
Expand All @@ -45,6 +48,14 @@ where
type ExecutionPayloadEnvelopeV2 = ExecutionPayloadEnvelopeV2;
type ExecutionPayloadEnvelopeV3 = OpExecutionPayloadEnvelopeV3;
type ExecutionPayloadEnvelopeV4 = OpExecutionPayloadEnvelopeV4;

fn block_to_payload(
block: SealedBlockFor<
<<Self::BuiltPayload as BuiltPayload>::Primitives as NodePrimitives>::Block,
>,
) -> (ExecutionPayload, ExecutionPayloadSidecar) {
block_to_payload(block)
}
}

/// A default payload type for [`OpEngineTypes`]
Expand Down
6 changes: 4 additions & 2 deletions crates/optimism/payload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ reth-optimism-chainspec.workspace = true
reth-optimism-consensus.workspace = true
reth-optimism-evm.workspace = true
reth-optimism-forks.workspace = true
reth-optimism-primitives.workspace = true

# ethereum
revm.workspace = true
Expand All @@ -57,5 +58,6 @@ optimism = [
"reth-optimism-evm/optimism",
"revm/optimism",
"reth-execution-types/optimism",
"reth-optimism-consensus/optimism"
]
"reth-optimism-consensus/optimism",
"reth-optimism-primitives/optimism"
]
Loading

0 comments on commit af1c9b7

Please sign in to comment.