Skip to content

Commit

Permalink
make PayloadStore operate on PayloadBuilder (#12460)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
stevencartavia and mattsse authored Nov 12, 2024
1 parent 6c1833d commit c44edf5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
5 changes: 3 additions & 2 deletions crates/node/builder/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use reth_node_core::{
version::{CARGO_PKG_VERSION, CLIENT_CODE, NAME_CLIENT, VERGEN_GIT_SHA},
};
use reth_payload_builder::PayloadStore;
use reth_payload_primitives::PayloadBuilder;
use reth_provider::providers::ProviderNodeTypes;
use reth_rpc::{
eth::{EthApiTypes, FullEthApiServer},
Expand Down Expand Up @@ -402,7 +403,7 @@ impl<N, EthApi, EV> NodeAddOns<N> for RpcAddOns<N, EthApi, EV>
where
N: FullNodeComponents<
Types: ProviderNodeTypes,
PayloadBuilder: Into<PayloadStore<<N::Types as NodeTypesWithEngine>::Engine>>,
PayloadBuilder: PayloadBuilder<PayloadType = <N::Types as NodeTypesWithEngine>::Engine>,
>,
EthApi: EthApiTypes + FullEthApiServer + AddDevSigners + Unpin + 'static,
EV: EngineValidatorBuilder<N>,
Expand All @@ -426,7 +427,7 @@ where
node.provider().clone(),
config.chain.clone(),
beacon_engine_handle,
node.payload_builder().clone().into(),
PayloadStore::new(node.payload_builder().clone()),
node.pool().clone(),
Box::new(node.task_executor().clone()),
client,
Expand Down
8 changes: 4 additions & 4 deletions crates/optimism/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use reth_chainspec::{EthChainSpec, Hardforks};
use reth_evm::{execute::BasicBlockExecutorProvider, ConfigureEvm};
use reth_network::{NetworkConfig, NetworkHandle, NetworkManager, PeersInfo};
use reth_node_api::{
AddOnsContext, EngineValidator, FullNodeComponents, NodeAddOns, NodePrimitives,
AddOnsContext, EngineValidator, FullNodeComponents, NodeAddOns, NodePrimitives, PayloadBuilder,
};
use reth_node_builder::{
components::{
Expand All @@ -23,7 +23,7 @@ use reth_optimism_consensus::OpBeaconConsensus;
use reth_optimism_evm::{OpEvmConfig, OpExecutionStrategyFactory};
use reth_optimism_payload_builder::builder::OpPayloadTransactions;
use reth_optimism_rpc::OpEthApi;
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService, PayloadStore};
use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService};
use reth_primitives::{Block, Header, Receipt, TransactionSigned, TxType};
use reth_provider::CanonStateSubscriptions;
use reth_tracing::tracing::{debug, info};
Expand Down Expand Up @@ -152,7 +152,7 @@ impl<N> NodeAddOns<N> for OpAddOns<N>
where
N: FullNodeComponents<
Types: NodeTypes<ChainSpec = OpChainSpec>,
PayloadBuilder: Into<PayloadStore<<N::Types as NodeTypesWithEngine>::Engine>>,
PayloadBuilder: PayloadBuilder<PayloadType = <N::Types as NodeTypesWithEngine>::Engine>,
>,
OpEngineValidator: EngineValidator<<N::Types as NodeTypesWithEngine>::Engine>,
{
Expand All @@ -170,7 +170,7 @@ impl<N> RethRpcAddOns<N> for OpAddOns<N>
where
N: FullNodeComponents<
Types: NodeTypes<ChainSpec = OpChainSpec>,
PayloadBuilder: Into<PayloadStore<<N::Types as NodeTypesWithEngine>::Engine>>,
PayloadBuilder: PayloadBuilder<PayloadType = <N::Types as NodeTypesWithEngine>::Engine>,
>,
OpEngineValidator: EngineValidator<<N::Types as NodeTypesWithEngine>::Engine>,
{
Expand Down
22 changes: 14 additions & 8 deletions crates/payload/builder/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ use futures_util::{future::FutureExt, Stream, StreamExt};
use reth_chain_state::CanonStateNotification;
use reth_payload_primitives::{
BuiltPayload, Events, PayloadBuilder, PayloadBuilderAttributes, PayloadBuilderError,
PayloadEvents, PayloadKind, PayloadTypes,
PayloadEvents, PayloadKind, PayloadStoreExt, PayloadTypes,
};
use std::{
fmt,
future::Future,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use tokio::sync::{
Expand All @@ -30,13 +31,14 @@ use tracing::{debug, info, trace, warn};
type PayloadFuture<P> = Pin<Box<dyn Future<Output = Result<P, PayloadBuilderError>> + Send + Sync>>;

/// A communication channel to the [`PayloadBuilderService`] that can retrieve payloads.
///
/// This type is intended to be used to retrieve payloads from the service (e.g. from the engine
/// API).
#[derive(Debug)]
pub struct PayloadStore<T: PayloadTypes> {
inner: PayloadBuilderHandle<T>,
inner: Arc<dyn PayloadStoreExt<T>>,
}

// === impl PayloadStore ===

impl<T> PayloadStore<T>
where
T: PayloadTypes,
Expand Down Expand Up @@ -82,12 +84,16 @@ where
}
}

impl<T> Clone for PayloadStore<T>
impl<T> PayloadStore<T>
where
T: PayloadTypes,
{
fn clone(&self) -> Self {
Self { inner: self.inner.clone() }
/// Create a new instance
pub fn new<P>(inner: P) -> Self
where
P: PayloadStoreExt<T> + 'static,
{
Self { inner: Arc::new(inner) }
}
}

Expand All @@ -96,7 +102,7 @@ where
T: PayloadTypes,
{
fn from(inner: PayloadBuilderHandle<T>) -> Self {
Self { inner }
Self::new(inner)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/payload/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use crate::events::{Events, PayloadEvents};
mod traits;
pub use traits::{
BuiltPayload, PayloadAttributes, PayloadAttributesBuilder, PayloadBuilder,
PayloadBuilderAttributes,
PayloadBuilderAttributes, PayloadStoreExt,
};

mod payload;
Expand Down
60 changes: 59 additions & 1 deletion crates/payload/primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use alloy_primitives::{Address, B256, U256};
use alloy_rpc_types_engine::{PayloadAttributes as EthPayloadAttributes, PayloadId};
use reth_chain_state::ExecutedBlock;
use reth_primitives::SealedBlock;
use std::fmt::Debug;
use tokio::sync::oneshot;

/// A type that can request, subscribe to and resolve payloads.
#[async_trait::async_trait]
pub trait PayloadBuilder: Send + Sync + Unpin {
pub trait PayloadBuilder: Debug + Send + Sync + Unpin {
/// The Payload type for the builder.
type PayloadType: PayloadTypes;
/// The error type returned by the builder.
Expand Down Expand Up @@ -58,6 +59,63 @@ pub trait PayloadBuilder: Send + Sync + Unpin {
) -> Option<Result<<Self::PayloadType as PayloadTypes>::PayloadBuilderAttributes, Self::Error>>;
}

/// A helper trait for internal usage to retrieve and resolve payloads.
#[async_trait::async_trait]
pub trait PayloadStoreExt<T: PayloadTypes>: Debug + Send + Sync + Unpin {
/// Resolves the payload job and returns the best payload that has been built so far.
async fn resolve_kind(
&self,
id: PayloadId,
kind: PayloadKind,
) -> Option<Result<T::BuiltPayload, PayloadBuilderError>>;

/// Resolves the payload job as fast and possible and returns the best payload that has been
/// built so far.
async fn resolve(&self, id: PayloadId) -> Option<Result<T::BuiltPayload, PayloadBuilderError>> {
self.resolve_kind(id, PayloadKind::Earliest).await
}

/// Returns the best payload for the given identifier.
async fn best_payload(
&self,
id: PayloadId,
) -> Option<Result<T::BuiltPayload, PayloadBuilderError>>;

/// Returns the payload attributes associated with the given identifier.
async fn payload_attributes(
&self,
id: PayloadId,
) -> Option<Result<T::PayloadBuilderAttributes, PayloadBuilderError>>;
}

#[async_trait::async_trait]
impl<T: PayloadTypes, P> PayloadStoreExt<T> for P
where
P: PayloadBuilder<PayloadType = T>,
{
async fn resolve_kind(
&self,
id: PayloadId,
kind: PayloadKind,
) -> Option<Result<T::BuiltPayload, PayloadBuilderError>> {
Some(PayloadBuilder::resolve_kind(self, id, kind).await?.map_err(Into::into))
}

async fn best_payload(
&self,
id: PayloadId,
) -> Option<Result<T::BuiltPayload, PayloadBuilderError>> {
Some(PayloadBuilder::best_payload(self, id).await?.map_err(Into::into))
}

async fn payload_attributes(
&self,
id: PayloadId,
) -> Option<Result<T::PayloadBuilderAttributes, PayloadBuilderError>> {
Some(PayloadBuilder::payload_attributes(self, id).await?.map_err(Into::into))
}
}

/// Represents a built payload type that contains a built [`SealedBlock`] and can be converted into
/// engine API execution payloads.
pub trait BuiltPayload: Send + Sync + std::fmt::Debug {
Expand Down

0 comments on commit c44edf5

Please sign in to comment.