reth_payload_builder_primitives/
traits.rs

1use crate::{PayloadBuilderError, PayloadEvents};
2use alloy_rpc_types_engine::PayloadId;
3use reth_payload_primitives::{PayloadKind, PayloadTypes};
4use std::fmt::Debug;
5use tokio::sync::oneshot;
6
7/// A helper trait for internal usage to retrieve and resolve payloads.
8#[async_trait::async_trait]
9pub trait PayloadStoreExt<T: PayloadTypes>: Debug + Send + Sync + Unpin {
10    /// Resolves the payload job and returns the best payload that has been built so far.
11    async fn resolve_kind(
12        &self,
13        id: PayloadId,
14        kind: PayloadKind,
15    ) -> Option<Result<T::BuiltPayload, PayloadBuilderError>>;
16
17    /// Resolves the payload job as fast and possible and returns the best payload that has been
18    /// built so far.
19    async fn resolve(&self, id: PayloadId) -> Option<Result<T::BuiltPayload, PayloadBuilderError>> {
20        self.resolve_kind(id, PayloadKind::Earliest).await
21    }
22
23    /// Returns the best payload for the given identifier.
24    async fn best_payload(
25        &self,
26        id: PayloadId,
27    ) -> Option<Result<T::BuiltPayload, PayloadBuilderError>>;
28
29    /// Returns the payload attributes associated with the given identifier.
30    async fn payload_attributes(
31        &self,
32        id: PayloadId,
33    ) -> Option<Result<T::PayloadBuilderAttributes, PayloadBuilderError>>;
34}
35
36#[async_trait::async_trait]
37impl<T: PayloadTypes, P> PayloadStoreExt<T> for P
38where
39    P: PayloadBuilder<PayloadType = T>,
40{
41    async fn resolve_kind(
42        &self,
43        id: PayloadId,
44        kind: PayloadKind,
45    ) -> Option<Result<T::BuiltPayload, PayloadBuilderError>> {
46        Some(PayloadBuilder::resolve_kind(self, id, kind).await?.map_err(Into::into))
47    }
48
49    async fn best_payload(
50        &self,
51        id: PayloadId,
52    ) -> Option<Result<T::BuiltPayload, PayloadBuilderError>> {
53        Some(PayloadBuilder::best_payload(self, id).await?.map_err(Into::into))
54    }
55
56    async fn payload_attributes(
57        &self,
58        id: PayloadId,
59    ) -> Option<Result<T::PayloadBuilderAttributes, PayloadBuilderError>> {
60        Some(PayloadBuilder::payload_attributes(self, id).await?.map_err(Into::into))
61    }
62}
63
64/// A type that can request, subscribe to and resolve payloads.
65#[async_trait::async_trait]
66pub trait PayloadBuilder: Debug + Send + Sync + Unpin {
67    /// The Payload type for the builder.
68    type PayloadType: PayloadTypes;
69    /// The error type returned by the builder.
70    type Error: Into<PayloadBuilderError>;
71
72    /// Sends a message to the service to start building a new payload for the given payload.
73    ///
74    /// Returns a receiver that will receive the payload id.
75    fn send_new_payload(
76        &self,
77        attr: <Self::PayloadType as PayloadTypes>::PayloadBuilderAttributes,
78    ) -> oneshot::Receiver<Result<PayloadId, Self::Error>>;
79
80    /// Returns the best payload for the given identifier.
81    async fn best_payload(
82        &self,
83        id: PayloadId,
84    ) -> Option<Result<<Self::PayloadType as PayloadTypes>::BuiltPayload, Self::Error>>;
85
86    /// Resolves the payload job and returns the best payload that has been built so far.
87    async fn resolve_kind(
88        &self,
89        id: PayloadId,
90        kind: PayloadKind,
91    ) -> Option<Result<<Self::PayloadType as PayloadTypes>::BuiltPayload, Self::Error>>;
92
93    /// Resolves the payload job as fast and possible and returns the best payload that has been
94    /// built so far.
95    async fn resolve(
96        &self,
97        id: PayloadId,
98    ) -> Option<Result<<Self::PayloadType as PayloadTypes>::BuiltPayload, Self::Error>> {
99        self.resolve_kind(id, PayloadKind::Earliest).await
100    }
101
102    /// Sends a message to the service to subscribe to payload events.
103    /// Returns a receiver that will receive them.
104    async fn subscribe(&self) -> Result<PayloadEvents<Self::PayloadType>, Self::Error>;
105
106    /// Returns the payload attributes associated with the given identifier.
107    async fn payload_attributes(
108        &self,
109        id: PayloadId,
110    ) -> Option<Result<<Self::PayloadType as PayloadTypes>::PayloadBuilderAttributes, Self::Error>>;
111}