alloy_eips/
eip7685.rs

1//! [EIP-7685]: General purpose execution layer requests
2//!
3//! [EIP-7685]: https://eips.ethereum.org/EIPS/eip-7685
4
5use alloc::vec::Vec;
6use alloy_primitives::{b256, Bytes, B256};
7use derive_more::{Deref, DerefMut, From, IntoIterator};
8
9/// The empty requests hash.
10///
11/// This is equivalent to `sha256("")`
12pub const EMPTY_REQUESTS_HASH: B256 =
13    b256!("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
14
15/// A container of EIP-7685 requests.
16///
17/// The container only holds the `requests` as defined by their respective EIPs. The first byte of
18/// each element is the `request_type` and the remaining bytes are the `request_data`.
19#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, Deref, DerefMut, From, IntoIterator)]
20#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22pub struct Requests(Vec<Bytes>);
23
24impl Requests {
25    /// Create a new [`Requests`] container from an iterator of items convertible to [`Bytes`].
26    pub fn from_requests<T: Into<Bytes>>(requests: impl IntoIterator<Item = T>) -> Self {
27        Self(requests.into_iter().map(Into::into).collect())
28    }
29    /// Construct a new [`Requests`] container with the given capacity.
30    pub fn with_capacity(capacity: usize) -> Self {
31        Self(Vec::with_capacity(capacity))
32    }
33
34    /// Construct a new [`Requests`] container.
35    ///
36    /// This function assumes that the request type byte is already included as the
37    /// first byte in the provided `Bytes` blob.
38    pub const fn new(requests: Vec<Bytes>) -> Self {
39        Self(requests)
40    }
41
42    /// Add a new request into the container.
43    pub fn push_request(&mut self, request: Bytes) {
44        // Omit empty requests.
45        if request.len() == 1 {
46            return;
47        }
48        self.0.push(request);
49    }
50
51    /// Adds a new request with the given request type into the container.
52    pub fn push_request_with_type(
53        &mut self,
54        request_type: u8,
55        request: impl IntoIterator<Item = u8>,
56    ) {
57        let mut request = request.into_iter().peekable();
58        // Omit empty requests.
59        if request.peek().is_none() {
60            return;
61        }
62        self.0.push(core::iter::once(request_type).chain(request).collect());
63    }
64
65    /// Consumes [`Requests`] and returns the inner raw opaque requests.
66    ///
67    /// # Note
68    ///
69    /// These requests include the `request_type` as the first byte in each
70    /// `Bytes` element, followed by the `requests_data`.
71    pub fn take(self) -> Vec<Bytes> {
72        self.0
73    }
74
75    /// Get an iterator over the requests.
76    pub fn iter(&self) -> core::slice::Iter<'_, Bytes> {
77        self.0.iter()
78    }
79
80    /// Calculate the requests hash as defined in EIP-7685 for the requests.
81    ///
82    /// The requests hash is defined as
83    ///
84    /// ```text
85    /// sha256(sha256(requests_0) ++ sha256(requests_1) ++ ...)
86    /// ```
87    ///
88    /// Each request in the container is expected to already have the `request_type` prepended
89    /// to its corresponding `requests_data`. This function directly calculates the hash based
90    /// on the combined `request_type` and `requests_data`.
91    ///
92    /// Empty requests are omitted from the hash calculation.
93    /// Requests are sorted by their `request_type` before hashing, see also [Ordering](https://eips.ethereum.org/EIPS/eip-7685#ordering)
94    #[cfg(feature = "sha2")]
95    pub fn requests_hash(&self) -> B256 {
96        use sha2::{Digest, Sha256};
97        let mut hash = Sha256::new();
98
99        let mut requests: Vec<_> = self
100            .0
101            .iter()
102            .filter(|req| {
103                // filter out all requests that are empty or only have the type byte
104                // <type-id> <data>
105                req.len() > 1
106            })
107            .collect();
108
109        // requests should only contain unique types: `id [r1,r2,..]`
110        requests.sort_unstable_by_key(|req| {
111            // SAFETY: only includes non-empty requests
112            req[0]
113        });
114
115        for req in requests {
116            let mut req_hash = Sha256::new();
117            req_hash.update(req);
118            hash.update(req_hash.finalize());
119        }
120        B256::new(hash.finalize().into())
121    }
122
123    /// Extend this container with requests from another container.
124    pub fn extend(&mut self, other: Self) {
125        self.0.extend(other.take());
126    }
127}
128
129/// A list of requests or a precomputed requests hash.
130///
131/// For testing purposes, the `Hash` variant stores a precomputed requests hash. This can be useful
132/// when the exact contents of the requests are unnecessary, and only a consistent hash value is
133/// needed to simulate the presence of requests without holding actual data.
134#[derive(Debug, Clone, PartialEq, Eq, Hash, derive_more::From)]
135#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
136#[cfg_attr(feature = "serde", serde(untagged))]
137#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
138pub enum RequestsOrHash {
139    /// Stores a list of requests, allowing for dynamic requests hash calculation.
140    Requests(Requests),
141    /// Stores a precomputed requests hash, used primarily for testing or mocking because the
142    /// header only contains the hash.
143    Hash(B256),
144}
145
146impl RequestsOrHash {
147    /// Returns the requests hash for the enum instance.
148    ///
149    /// - If the instance contains a list of requests, this function calculates the hash using
150    ///   `requests_hash` of the [`Requests`] struct.
151    /// - If it contains a precomputed hash, it returns that hash directly.
152    #[cfg(feature = "sha2")]
153    pub fn requests_hash(&self) -> B256 {
154        match self {
155            Self::Requests(requests) => requests.requests_hash(),
156            Self::Hash(precomputed_hash) => *precomputed_hash,
157        }
158    }
159
160    /// Returns an instance with the [`EMPTY_REQUESTS_HASH`].
161    pub const fn empty() -> Self {
162        Self::Hash(EMPTY_REQUESTS_HASH)
163    }
164
165    /// Returns the requests, if any.
166    pub const fn requests(&self) -> Option<&Requests> {
167        match self {
168            Self::Requests(requests) => Some(requests),
169            Self::Hash(_) => None,
170        }
171    }
172
173    /// Returns `true` if the variant is a list of requests.
174    pub const fn is_requests(&self) -> bool {
175        matches!(self, Self::Requests(_))
176    }
177
178    /// Returns `true` if the variant is a precomputed hash.
179    pub const fn is_hash(&self) -> bool {
180        matches!(self, Self::Hash(_))
181    }
182}
183
184impl Default for RequestsOrHash {
185    fn default() -> Self {
186        Self::Requests(Requests::default())
187    }
188}
189
190impl From<Vec<Bytes>> for RequestsOrHash {
191    fn from(requests: Vec<Bytes>) -> Self {
192        Self::Requests(requests.into())
193    }
194}
195
196#[cfg(test)]
197mod tests {
198    use super::*;
199
200    #[test]
201    fn test_extend() {
202        // Test extending a Requests container with another Requests container
203        let mut reqs1 = Requests::new(vec![Bytes::from(vec![0x01, 0x02])]);
204        let reqs2 =
205            Requests::new(vec![Bytes::from(vec![0x03, 0x04]), Bytes::from(vec![0x05, 0x06])]);
206
207        // Extend reqs1 with reqs2
208        reqs1.extend(reqs2);
209
210        // Ensure the requests are correctly combined
211        assert_eq!(reqs1.0.len(), 3);
212        assert_eq!(
213            reqs1.0,
214            vec![
215                Bytes::from(vec![0x01, 0x02]),
216                Bytes::from(vec![0x03, 0x04]),
217                Bytes::from(vec![0x05, 0x06])
218            ]
219        );
220    }
221
222    #[test]
223    #[cfg(feature = "sha2")]
224    fn test_consistent_requests_hash() {
225        // We test that the empty requests hash is consistent with the EIP-7685 definition.
226        assert_eq!(Requests::default().requests_hash(), EMPTY_REQUESTS_HASH);
227
228        // Test to hash a non-empty vector of requests.
229        assert_eq!(
230            Requests(vec![
231                Bytes::from(vec![0x00, 0x0a, 0x0b, 0x0c]),
232                Bytes::from(vec![0x01, 0x0d, 0x0e, 0x0f])
233            ])
234            .requests_hash(),
235            b256!("be3a57667b9bb9e0275019c0faf0f415fdc8385a408fd03e13a5c50615e3530c"),
236        );
237    }
238}