1use alloc::vec::Vec;
6use alloy_primitives::{b256, Bytes, B256};
7use derive_more::{Deref, DerefMut, From, IntoIterator};
8
9pub const EMPTY_REQUESTS_HASH: B256 =
13 b256!("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
14
15#[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
24#[cfg(feature = "ssz")]
25impl ssz::Encode for Requests {
26 fn is_ssz_fixed_len() -> bool {
27 <Vec<Bytes> as ssz::Encode>::is_ssz_fixed_len()
28 }
29
30 fn ssz_fixed_len() -> usize {
31 <Vec<Bytes> as ssz::Encode>::ssz_fixed_len()
32 }
33
34 fn ssz_bytes_len(&self) -> usize {
35 self.0.ssz_bytes_len()
36 }
37
38 fn ssz_append(&self, buf: &mut Vec<u8>) {
39 self.0.ssz_append(buf);
40 }
41}
42
43#[cfg(feature = "ssz")]
44impl ssz::Decode for Requests {
45 fn is_ssz_fixed_len() -> bool {
46 <Vec<Bytes> as ssz::Decode>::is_ssz_fixed_len()
47 }
48
49 fn ssz_fixed_len() -> usize {
50 <Vec<Bytes> as ssz::Decode>::ssz_fixed_len()
51 }
52
53 fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> {
54 <Vec<Bytes> as ssz::Decode>::from_ssz_bytes(bytes).map(Self)
55 }
56}
57
58impl Requests {
59 pub fn from_requests<T: Into<Bytes>>(requests: impl IntoIterator<Item = T>) -> Self {
61 Self(requests.into_iter().map(Into::into).collect())
62 }
63 pub fn with_capacity(capacity: usize) -> Self {
65 Self(Vec::with_capacity(capacity))
66 }
67
68 pub const fn new(requests: Vec<Bytes>) -> Self {
73 Self(requests)
74 }
75
76 pub fn push_request(&mut self, request: Bytes) {
78 if request.len() == 1 {
80 return;
81 }
82 self.0.push(request);
83 }
84
85 pub fn push_request_with_type(
87 &mut self,
88 request_type: u8,
89 request: impl IntoIterator<Item = u8>,
90 ) {
91 let mut request = request.into_iter().peekable();
92 if request.peek().is_none() {
94 return;
95 }
96 self.0.push(core::iter::once(request_type).chain(request).collect());
97 }
98
99 pub fn take(self) -> Vec<Bytes> {
106 self.0
107 }
108
109 pub fn iter(&self) -> core::slice::Iter<'_, Bytes> {
111 self.0.iter()
112 }
113
114 #[cfg(feature = "sha2")]
129 pub fn requests_hash(&self) -> B256 {
130 use sha2::{Digest, Sha256};
131 let mut hash = Sha256::new();
132
133 let mut requests: Vec<_> = self
134 .0
135 .iter()
136 .filter(|req| {
137 req.len() > 1
140 })
141 .collect();
142
143 requests.sort_unstable_by_key(|req| {
145 req[0]
147 });
148
149 for req in requests {
150 let mut req_hash = Sha256::new();
151 req_hash.update(req);
152 hash.update(req_hash.finalize());
153 }
154 B256::new(hash.finalize().into())
155 }
156
157 pub fn extend(&mut self, other: Self) {
159 self.0.extend(other.take());
160 }
161}
162
163#[derive(Debug, Clone, PartialEq, Eq, Hash, derive_more::From)]
169#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
170#[cfg_attr(feature = "serde", serde(untagged))]
171#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
172pub enum RequestsOrHash {
173 Requests(Requests),
175 Hash(B256),
178}
179
180impl RequestsOrHash {
181 #[cfg(feature = "sha2")]
187 pub fn requests_hash(&self) -> B256 {
188 match self {
189 Self::Requests(requests) => requests.requests_hash(),
190 Self::Hash(precomputed_hash) => *precomputed_hash,
191 }
192 }
193
194 pub const fn empty() -> Self {
196 Self::Hash(EMPTY_REQUESTS_HASH)
197 }
198
199 pub const fn requests(&self) -> Option<&Requests> {
201 match self {
202 Self::Requests(requests) => Some(requests),
203 Self::Hash(_) => None,
204 }
205 }
206
207 pub const fn is_requests(&self) -> bool {
209 matches!(self, Self::Requests(_))
210 }
211
212 pub const fn is_hash(&self) -> bool {
214 matches!(self, Self::Hash(_))
215 }
216}
217
218impl Default for RequestsOrHash {
219 fn default() -> Self {
220 Self::Requests(Requests::default())
221 }
222}
223
224impl From<Vec<Bytes>> for RequestsOrHash {
225 fn from(requests: Vec<Bytes>) -> Self {
226 Self::Requests(requests.into())
227 }
228}
229
230#[cfg(test)]
231mod tests {
232 use super::*;
233
234 #[test]
235 fn test_extend() {
236 let mut reqs1 = Requests::new(vec![Bytes::from(vec![0x01, 0x02])]);
238 let reqs2 =
239 Requests::new(vec![Bytes::from(vec![0x03, 0x04]), Bytes::from(vec![0x05, 0x06])]);
240
241 reqs1.extend(reqs2);
243
244 assert_eq!(reqs1.0.len(), 3);
246 assert_eq!(
247 reqs1.0,
248 vec![
249 Bytes::from(vec![0x01, 0x02]),
250 Bytes::from(vec![0x03, 0x04]),
251 Bytes::from(vec![0x05, 0x06])
252 ]
253 );
254 }
255
256 #[test]
257 #[cfg(feature = "sha2")]
258 fn test_consistent_requests_hash() {
259 assert_eq!(Requests::default().requests_hash(), EMPTY_REQUESTS_HASH);
261
262 assert_eq!(
264 Requests(vec![
265 Bytes::from(vec![0x00, 0x0a, 0x0b, 0x0c]),
266 Bytes::from(vec![0x01, 0x0d, 0x0e, 0x0f])
267 ])
268 .requests_hash(),
269 b256!("be3a57667b9bb9e0275019c0faf0f415fdc8385a408fd03e13a5c50615e3530c"),
270 );
271 }
272}