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