Skip to main content

alloy_rpc_types_engine/
sidecar.rs

1//! Contains helpers for dealing with additional parameters of `newPayload` requests.
2
3use crate::{
4    BogotaPayloadFields, CancunPayloadFields, MaybeBogotaPayloadFields, MaybeCancunPayloadFields,
5    MaybePraguePayloadFields, PraguePayloadFields,
6};
7use alloc::vec::Vec;
8use alloy_consensus::{Block, BlockHeader, Transaction};
9use alloy_eips::eip7685::Requests;
10use alloy_primitives::{Bytes, B256};
11
12/// Container type for all available additional `newPayload` request parameters that are not present
13/// in the `ExecutionPayload` object itself.
14#[derive(Debug, Clone, Default)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
17pub struct ExecutionPayloadSidecar {
18    /// Cancun request params introduced in `engine_newPayloadV3` that are not present in the
19    /// `ExecutionPayload`.
20    cancun: MaybeCancunPayloadFields,
21    /// The EIP-7685 requests provided as additional request params to `engine_newPayloadV4` that
22    /// are not present in the `ExecutionPayload`.
23    prague: MaybePraguePayloadFields,
24    /// EIP-7805 inclusion-list transactions provided as an additional request parameter to
25    /// `engine_newPayloadV6` that are not present in the `ExecutionPayload`.
26    #[cfg_attr(
27        feature = "serde",
28        serde(default, skip_serializing_if = "MaybeBogotaPayloadFields::is_none")
29    )]
30    bogota: MaybeBogotaPayloadFields,
31}
32
33impl ExecutionPayloadSidecar {
34    /// Extracts the [`ExecutionPayloadSidecar`] from the given [`alloy_consensus::Block`].
35    ///
36    /// Returns [`ExecutionPayloadSidecar::none`] if the block does not contain any sidecar fields
37    /// (pre-cancun): `requests_hash`, `parent_beacon_block_root`, `blob_versioned_hashes`.
38    ///
39    /// Note: This returns [`RequestOrHash::Hash`](alloy_eips::eip7685::RequestsOrHash::Hash) for
40    /// the EIP-7685 requests.
41    ///
42    /// Bogota fields cannot be recovered from a block because inclusion-list transactions are not
43    /// committed separately in the execution payload.
44    pub fn from_block<T, H>(block: &Block<T, H>) -> Self
45    where
46        T: Transaction,
47        H: BlockHeader,
48    {
49        let cancun =
50            block.parent_beacon_block_root().map(|parent_beacon_block_root| CancunPayloadFields {
51                parent_beacon_block_root,
52                versioned_hashes: block.body.blob_versioned_hashes_iter().copied().collect(),
53            });
54
55        let prague = block.requests_hash().map(PraguePayloadFields::new);
56
57        match (cancun, prague) {
58            (Some(cancun), Some(prague)) => Self::v4(cancun, prague),
59            (Some(cancun), None) => Self::v3(cancun),
60            _ => Self::none(),
61        }
62    }
63
64    /// Returns a new empty instance (pre-cancun, v1, v2).
65    pub const fn none() -> Self {
66        Self {
67            cancun: MaybeCancunPayloadFields::none(),
68            prague: MaybePraguePayloadFields::none(),
69            bogota: MaybeBogotaPayloadFields::none(),
70        }
71    }
72
73    /// Creates a new instance for cancun with the cancun fields for `engine_newPayloadV3`.
74    pub fn v3(cancun: CancunPayloadFields) -> Self {
75        Self {
76            cancun: cancun.into(),
77            prague: MaybePraguePayloadFields::none(),
78            bogota: MaybeBogotaPayloadFields::none(),
79        }
80    }
81
82    /// Creates a new instance post prague for `engine_newPayloadV4`.
83    pub fn v4(cancun: CancunPayloadFields, prague: PraguePayloadFields) -> Self {
84        Self {
85            cancun: cancun.into(),
86            prague: prague.into(),
87            bogota: MaybeBogotaPayloadFields::none(),
88        }
89    }
90
91    /// Creates a new instance post Bogota for `engine_newPayloadV6`.
92    pub fn v6(
93        cancun: CancunPayloadFields,
94        prague: PraguePayloadFields,
95        bogota: BogotaPayloadFields,
96    ) -> Self {
97        Self { cancun: cancun.into(), prague: prague.into(), bogota: bogota.into() }
98    }
99
100    /// Sets the EIP-7805 inclusion-list transactions.
101    pub fn with_inclusion_list(mut self, inclusion_list_transactions: Vec<Bytes>) -> Self {
102        self.bogota = BogotaPayloadFields::new(inclusion_list_transactions).into();
103        self
104    }
105
106    /// Returns a reference to the [`CancunPayloadFields`].
107    pub const fn cancun(&self) -> Option<&CancunPayloadFields> {
108        self.cancun.as_ref()
109    }
110
111    /// Consumes the type and returns the [`CancunPayloadFields`]
112    pub fn into_cancun(self) -> Option<CancunPayloadFields> {
113        self.cancun.into_inner()
114    }
115
116    /// Returns a reference to the [`PraguePayloadFields`].
117    pub const fn prague(&self) -> Option<&PraguePayloadFields> {
118        self.prague.as_ref()
119    }
120
121    /// Consumes the type and returns the [`PraguePayloadFields`].
122    pub fn into_prague(self) -> Option<PraguePayloadFields> {
123        self.prague.into_inner()
124    }
125
126    /// Returns a reference to the [`BogotaPayloadFields`].
127    pub const fn bogota(&self) -> Option<&BogotaPayloadFields> {
128        self.bogota.as_ref()
129    }
130
131    /// Consumes the type and returns the [`BogotaPayloadFields`].
132    pub fn into_bogota(self) -> Option<BogotaPayloadFields> {
133        self.bogota.into_inner()
134    }
135
136    /// Returns the parent beacon block root, if any.
137    pub fn parent_beacon_block_root(&self) -> Option<B256> {
138        self.cancun.parent_beacon_block_root()
139    }
140
141    /// Returns the blob versioned hashes, if any.
142    pub fn versioned_hashes(&self) -> Option<&Vec<B256>> {
143        self.cancun.versioned_hashes()
144    }
145
146    /// Returns the EIP-7685 requests
147    ///
148    /// Note: if the [`PraguePayloadFields`] only contains the requests hash this will return
149    /// `None`.
150    pub fn requests(&self) -> Option<&Requests> {
151        self.prague.requests()
152    }
153
154    /// Calculates or retrieves the requests hash.
155    ///
156    /// - If the `prague` field contains a list of requests, it calculates the requests hash
157    ///   dynamically.
158    /// - If it contains a precomputed hash (used for testing), it returns that hash directly.
159    pub fn requests_hash(&self) -> Option<B256> {
160        self.prague.requests_hash()
161    }
162
163    /// Returns the EIP-7805 inclusion-list transactions, if any.
164    pub fn inclusion_list_transactions(&self) -> Option<&Vec<Bytes>> {
165        self.bogota.inclusion_list_transactions()
166    }
167}
168
169#[cfg(test)]
170mod tests {
171    use super::*;
172
173    #[test]
174    fn with_inclusion_list() {
175        use alloy_consensus::{BlockBody, Header, TxEnvelope};
176
177        let block: Block<TxEnvelope> = Block::new(Header::default(), BlockBody::default());
178        let transactions = vec![Bytes::from_static(&[0x01, 0x02])];
179        let sidecar =
180            ExecutionPayloadSidecar::from_block(&block).with_inclusion_list(transactions.clone());
181
182        assert_eq!(sidecar.inclusion_list_transactions(), Some(&transactions));
183    }
184
185    #[test]
186    #[cfg(feature = "serde")]
187    fn serde_sidecar_without_bogota_fields() {
188        let legacy = r#"{"cancun":{"fields":null},"prague":{"fields":null}}"#;
189        let sidecar: ExecutionPayloadSidecar = serde_json::from_str(legacy).unwrap();
190
191        assert!(sidecar.bogota().is_none());
192        assert_eq!(serde_json::to_string(&sidecar).unwrap(), legacy);
193    }
194
195    #[test]
196    #[cfg(feature = "serde")]
197    fn serde_sidecar_with_bogota_fields() {
198        let transactions = vec![Bytes::from_static(&[0x01, 0x02])];
199        let sidecar = ExecutionPayloadSidecar::v6(
200            CancunPayloadFields::default(),
201            PraguePayloadFields::default(),
202            BogotaPayloadFields::new(transactions.clone()),
203        );
204
205        let encoded = serde_json::to_string(&sidecar).unwrap();
206        let decoded: ExecutionPayloadSidecar = serde_json::from_str(&encoded).unwrap();
207
208        assert_eq!(decoded.inclusion_list_transactions(), Some(&transactions));
209    }
210}