feat: simplify PayloadBuilder setup (#14276)

This commit is contained in:
Arsenii Kulikov
2025-02-07 00:44:16 +04:00
committed by GitHub
parent 63d5feab33
commit 1f1eabc428
32 changed files with 278 additions and 366 deletions

View File

@ -26,7 +26,6 @@ alloy-primitives = { workspace = true, optional = true }
alloy-rpc-types = { workspace = true, features = ["engine"] }
# async
async-trait.workspace = true
tokio = { workspace = true, features = ["sync"] }
tokio-stream.workspace = true
futures-util.workspace = true

View File

@ -11,9 +11,7 @@ use alloy_consensus::BlockHeader;
use alloy_rpc_types::engine::PayloadId;
use futures_util::{future::FutureExt, Stream, StreamExt};
use reth_chain_state::CanonStateNotification;
use reth_payload_builder_primitives::{
Events, PayloadBuilder, PayloadBuilderError, PayloadEvents, PayloadStoreExt,
};
use reth_payload_builder_primitives::{Events, PayloadBuilderError, PayloadEvents};
use reth_payload_primitives::{BuiltPayload, PayloadBuilderAttributes, PayloadKind, PayloadTypes};
use reth_primitives_traits::NodePrimitives;
use std::{
@ -38,7 +36,7 @@ type PayloadFuture<P> = Pin<Box<dyn Future<Output = Result<P, PayloadBuilderErro
/// API).
#[derive(Debug)]
pub struct PayloadStore<T: PayloadTypes> {
inner: Arc<dyn PayloadStoreExt<T>>,
inner: Arc<PayloadBuilderHandle<T>>,
}
impl<T> PayloadStore<T>
@ -91,10 +89,7 @@ where
T: PayloadTypes,
{
/// Create a new instance
pub fn new<P>(inner: P) -> Self
where
P: PayloadStoreExt<T> + 'static,
{
pub fn new(inner: PayloadBuilderHandle<T>) -> Self {
Self { inner: Arc::new(inner) }
}
}
@ -117,36 +112,40 @@ pub struct PayloadBuilderHandle<T: PayloadTypes> {
to_service: mpsc::UnboundedSender<PayloadServiceCommand<T>>,
}
// === impl PayloadBuilderHandle ===
impl<T: PayloadTypes> PayloadBuilderHandle<T> {
/// Creates a new payload builder handle for the given channel.
///
/// Note: this is only used internally by the [`PayloadBuilderService`] to manage the payload
/// building flow See [`PayloadBuilderService::poll`] for implementation details.
pub const fn new(to_service: mpsc::UnboundedSender<PayloadServiceCommand<T>>) -> Self {
Self { to_service }
}
#[async_trait::async_trait]
impl<T> PayloadBuilder for PayloadBuilderHandle<T>
where
T: PayloadTypes,
{
type PayloadType = T;
type Error = PayloadBuilderError;
fn send_new_payload(
/// Sends a message to the service to start building a new payload for the given payload.
///
/// Returns a receiver that will receive the payload id.
pub fn send_new_payload(
&self,
attr: <Self::PayloadType as PayloadTypes>::PayloadBuilderAttributes,
) -> Receiver<Result<PayloadId, Self::Error>> {
attr: T::PayloadBuilderAttributes,
) -> Receiver<Result<PayloadId, PayloadBuilderError>> {
let (tx, rx) = oneshot::channel();
let _ = self.to_service.send(PayloadServiceCommand::BuildNewPayload(attr, tx));
rx
}
/// Returns the best payload for the given identifier.
/// Note: this does not resolve the job if it's still in progress.
async fn best_payload(
pub async fn best_payload(
&self,
id: PayloadId,
) -> Option<Result<<Self::PayloadType as PayloadTypes>::BuiltPayload, Self::Error>> {
) -> Option<Result<T::BuiltPayload, PayloadBuilderError>> {
let (tx, rx) = oneshot::channel();
self.to_service.send(PayloadServiceCommand::BestPayload(id, tx)).ok()?;
rx.await.ok()?
}
async fn resolve_kind(
/// Resolves the payload job and returns the best payload that has been built so far.
pub async fn resolve_kind(
&self,
id: PayloadId,
kind: PayloadKind,
@ -159,7 +158,9 @@ where
}
}
async fn subscribe(&self) -> Result<PayloadEvents<Self::PayloadType>, Self::Error> {
/// Sends a message to the service to subscribe to payload events.
/// Returns a receiver that will receive them.
pub async fn subscribe(&self) -> Result<PayloadEvents<T>, PayloadBuilderError> {
let (tx, rx) = oneshot::channel();
let _ = self.to_service.send(PayloadServiceCommand::Subscribe(tx));
Ok(PayloadEvents { receiver: rx.await? })
@ -168,7 +169,7 @@ where
/// Returns the payload attributes associated with the given identifier.
///
/// Note: this returns the attributes of the payload and does not resolve the job.
async fn payload_attributes(
pub async fn payload_attributes(
&self,
id: PayloadId,
) -> Option<Result<T::PayloadBuilderAttributes, PayloadBuilderError>> {
@ -178,19 +179,6 @@ where
}
}
impl<T> PayloadBuilderHandle<T>
where
T: PayloadTypes,
{
/// Creates a new payload builder handle for the given channel.
///
/// Note: this is only used internally by the [`PayloadBuilderService`] to manage the payload
/// building flow See [`PayloadBuilderService::poll`] for implementation details.
pub const fn new(to_service: mpsc::UnboundedSender<PayloadServiceCommand<T>>) -> Self {
Self { to_service }
}
}
impl<T> Clone for PayloadBuilderHandle<T>
where
T: PayloadTypes,