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