reth_payload_builder_primitives/
traits.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use crate::{PayloadBuilderError, PayloadEvents};
use alloy_rpc_types_engine::PayloadId;
use reth_payload_primitives::{PayloadKind, PayloadTypes};
use std::fmt::Debug;
use tokio::sync::oneshot;

/// A helper trait for internal usage to retrieve and resolve payloads.
#[async_trait::async_trait]
pub trait PayloadStoreExt<T: PayloadTypes>: Debug + Send + Sync + Unpin {
    /// Resolves the payload job and returns the best payload that has been built so far.
    async fn resolve_kind(
        &self,
        id: PayloadId,
        kind: PayloadKind,
    ) -> Option<Result<T::BuiltPayload, PayloadBuilderError>>;

    /// Resolves the payload job as fast and possible and returns the best payload that has been
    /// built so far.
    async fn resolve(&self, id: PayloadId) -> Option<Result<T::BuiltPayload, PayloadBuilderError>> {
        self.resolve_kind(id, PayloadKind::Earliest).await
    }

    /// Returns the best payload for the given identifier.
    async fn best_payload(
        &self,
        id: PayloadId,
    ) -> Option<Result<T::BuiltPayload, PayloadBuilderError>>;

    /// Returns the payload attributes associated with the given identifier.
    async fn payload_attributes(
        &self,
        id: PayloadId,
    ) -> Option<Result<T::PayloadBuilderAttributes, PayloadBuilderError>>;
}

#[async_trait::async_trait]
impl<T: PayloadTypes, P> PayloadStoreExt<T> for P
where
    P: PayloadBuilder<PayloadType = T>,
{
    async fn resolve_kind(
        &self,
        id: PayloadId,
        kind: PayloadKind,
    ) -> Option<Result<T::BuiltPayload, PayloadBuilderError>> {
        Some(PayloadBuilder::resolve_kind(self, id, kind).await?.map_err(Into::into))
    }

    async fn best_payload(
        &self,
        id: PayloadId,
    ) -> Option<Result<T::BuiltPayload, PayloadBuilderError>> {
        Some(PayloadBuilder::best_payload(self, id).await?.map_err(Into::into))
    }

    async fn payload_attributes(
        &self,
        id: PayloadId,
    ) -> Option<Result<T::PayloadBuilderAttributes, PayloadBuilderError>> {
        Some(PayloadBuilder::payload_attributes(self, id).await?.map_err(Into::into))
    }
}

/// A type that can request, subscribe to and resolve payloads.
#[async_trait::async_trait]
pub trait PayloadBuilder: Debug + Send + Sync + Unpin {
    /// The Payload type for the builder.
    type PayloadType: PayloadTypes;
    /// The error type returned by the builder.
    type Error: Into<PayloadBuilderError>;

    /// Sends a message to the service to start building a new payload for the given payload.
    ///
    /// Returns a receiver that will receive the payload id.
    fn send_new_payload(
        &self,
        attr: <Self::PayloadType as PayloadTypes>::PayloadBuilderAttributes,
    ) -> oneshot::Receiver<Result<PayloadId, Self::Error>>;

    /// Returns the best payload for the given identifier.
    async fn best_payload(
        &self,
        id: PayloadId,
    ) -> Option<Result<<Self::PayloadType as PayloadTypes>::BuiltPayload, Self::Error>>;

    /// Resolves the payload job and returns the best payload that has been built so far.
    async fn resolve_kind(
        &self,
        id: PayloadId,
        kind: PayloadKind,
    ) -> Option<Result<<Self::PayloadType as PayloadTypes>::BuiltPayload, Self::Error>>;

    /// Resolves the payload job as fast and possible and returns the best payload that has been
    /// built so far.
    async fn resolve(
        &self,
        id: PayloadId,
    ) -> Option<Result<<Self::PayloadType as PayloadTypes>::BuiltPayload, Self::Error>> {
        self.resolve_kind(id, PayloadKind::Earliest).await
    }

    /// Sends a message to the service to subscribe to payload events.
    /// Returns a receiver that will receive them.
    async fn subscribe(&self) -> Result<PayloadEvents<Self::PayloadType>, Self::Error>;

    /// Returns the payload attributes associated with the given identifier.
    async fn payload_attributes(
        &self,
        id: PayloadId,
    ) -> Option<Result<<Self::PayloadType as PayloadTypes>::PayloadBuilderAttributes, Self::Error>>;
}