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