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