Skip to main content

reth_payload_builder/
traits.rs

1//! Trait abstractions used by the payload crate.
2
3use alloy_rpc_types::engine::PayloadId;
4use reth_chain_state::CanonStateNotification;
5use reth_payload_builder_primitives::PayloadBuilderError;
6use reth_payload_primitives::{BuiltPayload, PayloadAttributes, PayloadKind};
7use reth_primitives_traits::NodePrimitives;
8use std::future::Future;
9
10use crate::service::BuildNewPayload;
11
12/// A type that can build a payload.
13///
14/// This type is a [`Future`] that resolves when the job is done (e.g. complete, timed out) or it
15/// failed. It's not supposed to return the best payload built when it resolves, instead
16/// [`PayloadJob::best_payload`] should be used for that.
17///
18/// A `PayloadJob` must always be prepared to return the best payload built so far to ensure there
19/// is a valid payload to deliver to the CL, so it does not miss a slot, even if the payload is
20/// empty.
21///
22/// Note: A `PayloadJob` need to be cancel safe because it might be dropped after the CL has requested the payload via `engine_getPayloadV1` (see also [engine API docs](https://github.com/ethereum/execution-apis/blob/6709c2a795b707202e93c4f2867fa0bf2640a84f/src/engine/paris.md#engine_getpayloadv1))
23pub trait PayloadJob: Future<Output = Result<(), PayloadBuilderError>> {
24    /// Represents the payload attributes type that is used to spawn this payload job.
25    type PayloadAttributes: PayloadAttributes + std::fmt::Debug;
26    /// Represents the future that resolves the block that's returned to the CL.
27    type ResolvePayloadFuture: Future<Output = Result<Self::BuiltPayload, PayloadBuilderError>>
28        + Send
29        + 'static;
30    /// Represents the built payload type that is returned to the CL.
31    type BuiltPayload: BuiltPayload + Clone + std::fmt::Debug;
32
33    /// Returns the best payload that has been built so far.
34    ///
35    /// Note: This is never called by the CL.
36    fn best_payload(&self) -> Result<Self::BuiltPayload, PayloadBuilderError>;
37
38    /// Returns the payload attributes for the payload being built.
39    fn payload_attributes(&self) -> Result<Self::PayloadAttributes, PayloadBuilderError>;
40
41    /// Returns the payload timestamp for the payload being built.
42    /// The default implementation allocates full attributes only to
43    /// extract the timestamp. Provide your own implementation if you
44    /// need performance here.
45    fn payload_timestamp(&self) -> Result<u64, PayloadBuilderError> {
46        Ok(self.payload_attributes()?.timestamp())
47    }
48
49    /// Called when the payload is requested by the CL.
50    ///
51    /// This is invoked on [`engine_getPayloadV2`](https://github.com/ethereum/execution-apis/blob/main/src/engine/shanghai.md#engine_getpayloadv2) and [`engine_getPayloadV1`](https://github.com/ethereum/execution-apis/blob/main/src/engine/paris.md#engine_getpayloadv1).
52    ///
53    /// The timeout for returning the payload to the CL is 1s, thus the future returned should
54    /// resolve in under 1 second.
55    ///
56    /// Ideally this is the best payload built so far, or an empty block without transactions, if
57    /// nothing has been built yet.
58    ///
59    /// According to the spec:
60    /// > Client software MAY stop the corresponding build process after serving this call.
61    ///
62    /// It is at the discretion of the implementer whether the build job should be kept alive or
63    /// terminated.
64    ///
65    /// If this returns [`KeepPayloadJobAlive::Yes`], then the [`PayloadJob`] will be polled
66    /// once more. If this returns [`KeepPayloadJobAlive::No`] then the [`PayloadJob`] will be
67    /// dropped after this call.
68    ///
69    /// The [`PayloadKind`] determines how the payload should be resolved in the
70    /// `ResolvePayloadFuture`. [`PayloadKind::Earliest`] should return the earliest available
71    /// payload (as fast as possible), e.g. racing an empty payload job against a pending job if
72    /// there's no payload available yet. [`PayloadKind::WaitForPending`] is allowed to wait
73    /// until a built payload is available.
74    fn resolve_kind(
75        &mut self,
76        kind: PayloadKind,
77    ) -> (Self::ResolvePayloadFuture, KeepPayloadJobAlive);
78
79    /// Resolves the payload as fast as possible.
80    ///
81    /// See also [`PayloadJob::resolve_kind`]
82    fn resolve(&mut self) -> (Self::ResolvePayloadFuture, KeepPayloadJobAlive) {
83        self.resolve_kind(PayloadKind::Earliest)
84    }
85}
86
87/// Whether the payload job should be kept alive or terminated after the payload was requested by
88/// the CL.
89#[derive(Debug, Clone, Copy, PartialEq, Eq)]
90pub enum KeepPayloadJobAlive {
91    /// Keep the job alive.
92    Yes,
93    /// Terminate the job.
94    No,
95}
96
97/// A type that knows how to create new jobs for creating payloads.
98pub trait PayloadJobGenerator {
99    /// The type that manages the lifecycle of a payload.
100    ///
101    /// This type is a future that yields better payloads.
102    type Job: PayloadJob;
103
104    /// Creates the initial payload and a new [`PayloadJob`] that yields better payloads over time.
105    ///
106    /// This is called when the CL requests a new payload job via a fork choice update.
107    ///
108    /// # Note
109    ///
110    /// This is expected to initially build a new (empty) payload without transactions, so it can be
111    /// returned directly.
112    fn new_payload_job(
113        &self,
114        input: BuildNewPayload<<Self::Job as PayloadJob>::PayloadAttributes>,
115        id: PayloadId,
116    ) -> Result<Self::Job, PayloadBuilderError>;
117
118    /// Handles new chain state events
119    ///
120    /// This is intended for any logic that needs to be run when the chain state changes or used to
121    /// use the in memory state for the head block.
122    fn on_new_state<N: NodePrimitives>(&mut self, new_state: CanonStateNotification<N>) {
123        let _ = new_state;
124    }
125}