1use crate::broadcast::{decode_list_with_memory_budget, decode_list_with_memory_budget_by};
5use alloc::vec::Vec;
6use alloy_consensus::{
7 constants::EIP4844_TX_TYPE_ID,
8 transaction::{PooledTransaction, TxHashRef},
9};
10use alloy_eips::eip7594::EIP_7594_WRAPPER_VERSION;
11use alloy_primitives::B256;
12use alloy_rlp::{Decodable, Header, RlpDecodableWrapper, RlpEncodableWrapper, EMPTY_LIST_CODE};
13use derive_more::{Constructor, Deref, IntoIterator};
14use reth_codecs_derive::add_arbitrary_tests;
15use reth_primitives_traits::InMemorySize;
16
17pub use crate::cells::{Cells, GetCells};
18
19#[derive(
21 Clone,
22 Debug,
23 PartialEq,
24 Eq,
25 RlpEncodableWrapper,
26 RlpDecodableWrapper,
27 Default,
28 Deref,
29 IntoIterator,
30)]
31#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
32#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
33#[add_arbitrary_tests(rlp)]
34pub struct GetPooledTransactions(
35 pub Vec<B256>,
37);
38
39impl<T> From<Vec<T>> for GetPooledTransactions
40where
41 T: Into<B256>,
42{
43 fn from(hashes: Vec<T>) -> Self {
44 Self(hashes.into_iter().map(|h| h.into()).collect())
45 }
46}
47
48impl InMemorySize for GetPooledTransactions {
49 fn size(&self) -> usize {
50 self.0.len() * core::mem::size_of::<B256>()
51 }
52}
53
54#[derive(
63 Clone,
64 Debug,
65 PartialEq,
66 Eq,
67 RlpEncodableWrapper,
68 RlpDecodableWrapper,
69 IntoIterator,
70 Deref,
71 Constructor,
72)]
73#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
74pub struct PooledTransactions<T = PooledTransaction>(
75 pub Vec<T>,
77);
78
79impl<T: Decodable + InMemorySize> PooledTransactions<T> {
80 pub fn decode_with_memory_budget(
84 buf: &mut &[u8],
85 memory_budget: usize,
86 ) -> alloy_rlp::Result<Self> {
87 decode_list_with_memory_budget(buf, memory_budget).map(Self)
88 }
89
90 pub fn decode_eth72_with_memory_budget(
98 buf: &mut &[u8],
99 memory_budget: usize,
100 ) -> alloy_rlp::Result<Self> {
101 decode_list_with_memory_budget_by(buf, memory_budget, |buf| {
102 if !is_eth72_pooled_transaction(buf)? {
103 return Err(alloy_rlp::Error::Custom(
104 "expected a blobless eth/72 pooled transaction",
105 ))
106 }
107 T::decode(buf)
108 })
109 .map(Self)
110 }
111}
112
113impl<T: TxHashRef> PooledTransactions<T> {
114 pub fn hashes(&self) -> impl Iterator<Item = B256> + '_ {
116 self.iter().map(|tx| *tx.tx_hash())
117 }
118}
119
120impl<T, U> TryFrom<Vec<U>> for PooledTransactions<T>
121where
122 T: TryFrom<U>,
123{
124 type Error = T::Error;
125
126 fn try_from(txs: Vec<U>) -> Result<Self, Self::Error> {
127 txs.into_iter().map(T::try_from).collect()
128 }
129}
130
131impl<T> FromIterator<T> for PooledTransactions<T> {
132 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
133 Self(iter.into_iter().collect())
134 }
135}
136
137impl<T> Default for PooledTransactions<T> {
138 fn default() -> Self {
139 Self(Default::default())
140 }
141}
142
143fn is_eth72_pooled_transaction(mut buf: &[u8]) -> alloy_rlp::Result<bool> {
149 let envelope = Header::decode(&mut buf)?;
150 if envelope.list || buf.first() != Some(&EIP4844_TX_TYPE_ID) {
151 return Ok(true)
152 }
153 let mut fields = Header::decode_bytes(&mut &buf[1..], true)?;
154 Header::decode_bytes(&mut fields, true)?;
156 Ok(fields.starts_with(&[EIP_7594_WRAPPER_VERSION, EMPTY_LIST_CODE]))
157}
158
159#[cfg(test)]
160mod tests {
161 use crate::{message::RequestPair, GetPooledTransactions, PooledTransactions};
162 use alloy_consensus::{
163 transaction::PooledTransaction, SignableTransaction, TxEip1559, TxEip4844,
164 TxEip4844WithSidecar, TxLegacy,
165 };
166 use alloy_eips::{
167 eip4844::{Blob, BlobTransactionSidecar, Bytes48},
168 eip7594::{BlobTransactionSidecarEip7594, BlobTransactionSidecarVariant},
169 };
170 use alloy_primitives::{address, b256, hex, Address, Signature, TxKind, B256, U256};
171 use alloy_rlp::{Decodable, Encodable};
172 use reth_chainspec::MIN_TRANSACTION_GAS;
173 use reth_ethereum_primitives::{PooledTransactionVariant, Transaction, TransactionSigned};
174 use std::str::FromStr;
175
176 #[test]
177 fn encode_get_pooled_transactions() {
179 let expected = hex!(
180 "f847820457f842a000000000000000000000000000000000000000000000000000000000deadc0dea000000000000000000000000000000000000000000000000000000000feedbeef"
181 );
182 let mut data = vec![];
183 let request = RequestPair {
184 request_id: 1111,
185 message: GetPooledTransactions(vec![
186 b256!("00000000000000000000000000000000000000000000000000000000deadc0de"),
187 b256!("00000000000000000000000000000000000000000000000000000000feedbeef"),
188 ]),
189 };
190 request.encode(&mut data);
191 assert_eq!(data, expected);
192 }
193
194 #[test]
195 fn decode_get_pooled_transactions() {
197 let data = hex!(
198 "f847820457f842a000000000000000000000000000000000000000000000000000000000deadc0dea000000000000000000000000000000000000000000000000000000000feedbeef"
199 );
200 let request = RequestPair::<GetPooledTransactions>::decode(&mut &data[..]).unwrap();
201 assert_eq!(
202 request,
203 RequestPair {
204 request_id: 1111,
205 message: GetPooledTransactions(vec![
206 b256!("00000000000000000000000000000000000000000000000000000000deadc0de"),
207 b256!("00000000000000000000000000000000000000000000000000000000feedbeef"),
208 ])
209 }
210 );
211 }
212
213 #[test]
214 fn encode_pooled_transactions() {
216 let expected = hex!(
217 "f8d7820457f8d2f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb"
218 );
219 let mut data = vec![];
220 let txs = vec![
221 TransactionSigned::new_unhashed(
222 Transaction::Legacy(TxLegacy {
223 chain_id: Some(1),
224 nonce: 0x8u64,
225 gas_price: 0x4a817c808,
226 gas_limit: 0x2e248,
227 to: TxKind::Call(Address::repeat_byte(0x35)),
228 value: U256::from(0x200u64),
229 input: Default::default(),
230 }),
231 Signature::new(
232 U256::from_str(
233 "0x64b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12",
234 )
235 .unwrap(),
236 U256::from_str(
237 "0x64b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10",
238 )
239 .unwrap(),
240 false,
241 ),
242 ),
243 TransactionSigned::new_unhashed(
244 Transaction::Legacy(TxLegacy {
245 chain_id: Some(1),
246 nonce: 0x09u64,
247 gas_price: 0x4a817c809,
248 gas_limit: 0x33450,
249 to: TxKind::Call(Address::repeat_byte(0x35)),
250 value: U256::from(0x2d9u64),
251 input: Default::default(),
252 }),
253 Signature::new(
254 U256::from_str(
255 "0x52f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb",
256 )
257 .unwrap(),
258 U256::from_str(
259 "0x52f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb",
260 )
261 .unwrap(),
262 false,
263 ),
264 ),
265 ];
266 let message: Vec<PooledTransaction> = txs
267 .into_iter()
268 .map(|tx| {
269 PooledTransaction::try_from(tx)
270 .expect("Failed to convert TransactionSigned to PooledTransaction")
271 })
272 .collect();
273 let request = RequestPair {
274 request_id: 1111,
275 message: PooledTransactions(message), };
278 request.encode(&mut data);
279 assert_eq!(data, expected);
280 }
281
282 #[test]
283 fn decode_pooled_transactions() {
285 let data = hex!(
286 "f8d7820457f8d2f867088504a817c8088302e2489435353535353535353535353535353535353535358202008025a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12a064b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10f867098504a817c809830334509435353535353535353535353535353535353535358202d98025a052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afba052f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb"
287 );
288 let txs = vec![
289 TransactionSigned::new_unhashed(
290 Transaction::Legacy(TxLegacy {
291 chain_id: Some(1),
292 nonce: 0x8u64,
293 gas_price: 0x4a817c808,
294 gas_limit: 0x2e248,
295 to: TxKind::Call(Address::repeat_byte(0x35)),
296 value: U256::from(0x200u64),
297 input: Default::default(),
298 }),
299 Signature::new(
300 U256::from_str(
301 "0x64b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c12",
302 )
303 .unwrap(),
304 U256::from_str(
305 "0x64b1702d9298fee62dfeccc57d322a463ad55ca201256d01f62b45b2e1c21c10",
306 )
307 .unwrap(),
308 false,
309 ),
310 ),
311 TransactionSigned::new_unhashed(
312 Transaction::Legacy(TxLegacy {
313 chain_id: Some(1),
314 nonce: 0x09u64,
315 gas_price: 0x4a817c809,
316 gas_limit: 0x33450,
317 to: TxKind::Call(Address::repeat_byte(0x35)),
318 value: U256::from(0x2d9u64),
319 input: Default::default(),
320 }),
321 Signature::new(
322 U256::from_str(
323 "0x52f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb",
324 )
325 .unwrap(),
326 U256::from_str(
327 "0x52f8f61201b2b11a78d6e866abc9c3db2ae8631fa656bfe5cb53668255367afb",
328 )
329 .unwrap(),
330 false,
331 ),
332 ),
333 ];
334 let message: Vec<PooledTransaction> = txs
335 .into_iter()
336 .map(|tx| {
337 PooledTransaction::try_from(tx)
338 .expect("Failed to convert TransactionSigned to PooledTransaction")
339 })
340 .collect();
341 let expected = RequestPair { request_id: 1111, message: PooledTransactions(message) };
342
343 let request = RequestPair::<PooledTransactions>::decode(&mut &data[..]).unwrap();
344 assert_eq!(request, expected);
345 }
346
347 #[test]
348 fn decode_pooled_transactions_network() {
349 let data = hex!(
350 "f9022980f90225f8650f84832156008287fb94cf7f9e66af820a19257a2108375b180b0ec491678204d2802ca035b7bfeb9ad9ece2cbafaaf8e202e706b4cfaeb233f46198f00b44d4a566a981a0612638fb29427ca33b9a3be2a0a561beecfe0269655be160d35e72d366a6a860b87502f872041a8459682f008459682f0d8252089461815774383099e24810ab832a5b2a5425c154d58829a2241af62c000080c001a059e6b67f48fb32e7e570dfb11e042b5ad2e55e3ce3ce9cd989c7e06e07feeafda0016b83f4f980694ed2eee4d10667242b1f40dc406901b34125b008d334d47469f86b0384773594008398968094d3e8763675e4c425df46cc3b5c0f6cbdac39604687038d7ea4c68000802ba0ce6834447c0a4193c40382e6c57ae33b241379c5418caac9cdc18d786fd12071a03ca3ae86580e94550d7c071e3a02eadb5a77830947c9225165cf9100901bee88f86b01843b9aca00830186a094d3e8763675e4c425df46cc3b5c0f6cbdac3960468702769bb01b2a00802ba0e24d8bd32ad906d6f8b8d7741e08d1959df021698b19ee232feba15361587d0aa05406ad177223213df262cb66ccbb2f46bfdccfdfbbb5ffdda9e2c02d977631daf86b02843b9aca00830186a094d3e8763675e4c425df46cc3b5c0f6cbdac39604687038d7ea4c68000802ba00eb96ca19e8a77102767a41fc85a36afd5c61ccb09911cec5d3e86e193d9c5aea03a456401896b1b6055311536bf00a718568c744d8c1f9df59879e8350220ca18"
351 );
352 let decoded_transactions =
353 RequestPair::<PooledTransactions>::decode(&mut &data[..]).unwrap();
354 let txs = vec![
355 TransactionSigned::new_unhashed(
356 Transaction::Legacy(TxLegacy {
357 chain_id: Some(4),
358 nonce: 15u64,
359 gas_price: 2200000000,
360 gas_limit: 34811,
361 to: TxKind::Call(address!("cf7f9e66af820a19257a2108375b180b0ec49167")),
362 value: U256::from(1234u64),
363 input: Default::default(),
364 }),
365 Signature::new(
366 U256::from_str(
367 "0x35b7bfeb9ad9ece2cbafaaf8e202e706b4cfaeb233f46198f00b44d4a566a981",
368 )
369 .unwrap(),
370 U256::from_str(
371 "0x612638fb29427ca33b9a3be2a0a561beecfe0269655be160d35e72d366a6a860",
372 )
373 .unwrap(),
374 true,
375 ),
376 ),
377 TransactionSigned::new_unhashed(
378 Transaction::Eip1559(TxEip1559 {
379 chain_id: 4,
380 nonce: 26u64,
381 max_priority_fee_per_gas: 1500000000,
382 max_fee_per_gas: 1500000013,
383 gas_limit: MIN_TRANSACTION_GAS,
384 to: TxKind::Call(address!("61815774383099e24810ab832a5b2a5425c154d5")),
385 value: U256::from(3000000000000000000u64),
386 input: Default::default(),
387 access_list: Default::default(),
388 }),
389 Signature::new(
390 U256::from_str(
391 "0x59e6b67f48fb32e7e570dfb11e042b5ad2e55e3ce3ce9cd989c7e06e07feeafd",
392 )
393 .unwrap(),
394 U256::from_str(
395 "0x016b83f4f980694ed2eee4d10667242b1f40dc406901b34125b008d334d47469",
396 )
397 .unwrap(),
398 true,
399 ),
400 ),
401 TransactionSigned::new_unhashed(
402 Transaction::Legacy(TxLegacy {
403 chain_id: Some(4),
404 nonce: 3u64,
405 gas_price: 2000000000,
406 gas_limit: 10000000,
407 to: TxKind::Call(address!("d3e8763675e4c425df46cc3b5c0f6cbdac396046")),
408 value: U256::from(1000000000000000u64),
409 input: Default::default(),
410 }),
411 Signature::new(
412 U256::from_str(
413 "0xce6834447c0a4193c40382e6c57ae33b241379c5418caac9cdc18d786fd12071",
414 )
415 .unwrap(),
416 U256::from_str(
417 "0x3ca3ae86580e94550d7c071e3a02eadb5a77830947c9225165cf9100901bee88",
418 )
419 .unwrap(),
420 false,
421 ),
422 ),
423 TransactionSigned::new_unhashed(
424 Transaction::Legacy(TxLegacy {
425 chain_id: Some(4),
426 nonce: 1u64,
427 gas_price: 1000000000,
428 gas_limit: 100000,
429 to: TxKind::Call(address!("d3e8763675e4c425df46cc3b5c0f6cbdac396046")),
430 value: U256::from(693361000000000u64),
431 input: Default::default(),
432 }),
433 Signature::new(
434 U256::from_str(
435 "0xe24d8bd32ad906d6f8b8d7741e08d1959df021698b19ee232feba15361587d0a",
436 )
437 .unwrap(),
438 U256::from_str(
439 "0x5406ad177223213df262cb66ccbb2f46bfdccfdfbbb5ffdda9e2c02d977631da",
440 )
441 .unwrap(),
442 false,
443 ),
444 ),
445 TransactionSigned::new_unhashed(
446 Transaction::Legacy(TxLegacy {
447 chain_id: Some(4),
448 nonce: 2u64,
449 gas_price: 1000000000,
450 gas_limit: 100000,
451 to: TxKind::Call(address!("d3e8763675e4c425df46cc3b5c0f6cbdac396046")),
452 value: U256::from(1000000000000000u64),
453 input: Default::default(),
454 }),
455 Signature::new(
456 U256::from_str(
457 "0xeb96ca19e8a77102767a41fc85a36afd5c61ccb09911cec5d3e86e193d9c5ae",
458 )
459 .unwrap(),
460 U256::from_str(
461 "0x3a456401896b1b6055311536bf00a718568c744d8c1f9df59879e8350220ca18",
462 )
463 .unwrap(),
464 false,
465 ),
466 ),
467 ];
468 let message: Vec<PooledTransaction> = txs
469 .into_iter()
470 .map(|tx| {
471 PooledTransaction::try_from(tx)
472 .expect("Failed to convert TransactionSigned to PooledTransaction")
473 })
474 .collect();
475 let expected_transactions =
476 RequestPair { request_id: 0, message: PooledTransactions(message) };
477
478 for (decoded, expected) in
480 decoded_transactions.message.0.iter().zip(expected_transactions.message.0.iter())
481 {
482 assert_eq!(decoded, expected);
483 }
484
485 assert_eq!(decoded_transactions, expected_transactions);
486 }
487
488 #[test]
489 fn encode_pooled_transactions_network() {
490 let expected = hex!(
491 "f9022980f90225f8650f84832156008287fb94cf7f9e66af820a19257a2108375b180b0ec491678204d2802ca035b7bfeb9ad9ece2cbafaaf8e202e706b4cfaeb233f46198f00b44d4a566a981a0612638fb29427ca33b9a3be2a0a561beecfe0269655be160d35e72d366a6a860b87502f872041a8459682f008459682f0d8252089461815774383099e24810ab832a5b2a5425c154d58829a2241af62c000080c001a059e6b67f48fb32e7e570dfb11e042b5ad2e55e3ce3ce9cd989c7e06e07feeafda0016b83f4f980694ed2eee4d10667242b1f40dc406901b34125b008d334d47469f86b0384773594008398968094d3e8763675e4c425df46cc3b5c0f6cbdac39604687038d7ea4c68000802ba0ce6834447c0a4193c40382e6c57ae33b241379c5418caac9cdc18d786fd12071a03ca3ae86580e94550d7c071e3a02eadb5a77830947c9225165cf9100901bee88f86b01843b9aca00830186a094d3e8763675e4c425df46cc3b5c0f6cbdac3960468702769bb01b2a00802ba0e24d8bd32ad906d6f8b8d7741e08d1959df021698b19ee232feba15361587d0aa05406ad177223213df262cb66ccbb2f46bfdccfdfbbb5ffdda9e2c02d977631daf86b02843b9aca00830186a094d3e8763675e4c425df46cc3b5c0f6cbdac39604687038d7ea4c68000802ba00eb96ca19e8a77102767a41fc85a36afd5c61ccb09911cec5d3e86e193d9c5aea03a456401896b1b6055311536bf00a718568c744d8c1f9df59879e8350220ca18"
492 );
493 let txs = vec![
494 TransactionSigned::new_unhashed(
495 Transaction::Legacy(TxLegacy {
496 chain_id: Some(4),
497 nonce: 15u64,
498 gas_price: 2200000000,
499 gas_limit: 34811,
500 to: TxKind::Call(address!("cf7f9e66af820a19257a2108375b180b0ec49167")),
501 value: U256::from(1234u64),
502 input: Default::default(),
503 }),
504 Signature::new(
505 U256::from_str(
506 "0x35b7bfeb9ad9ece2cbafaaf8e202e706b4cfaeb233f46198f00b44d4a566a981",
507 )
508 .unwrap(),
509 U256::from_str(
510 "0x612638fb29427ca33b9a3be2a0a561beecfe0269655be160d35e72d366a6a860",
511 )
512 .unwrap(),
513 true,
514 ),
515 ),
516 TransactionSigned::new_unhashed(
517 Transaction::Eip1559(TxEip1559 {
518 chain_id: 4,
519 nonce: 26u64,
520 max_priority_fee_per_gas: 1500000000,
521 max_fee_per_gas: 1500000013,
522 gas_limit: MIN_TRANSACTION_GAS,
523 to: TxKind::Call(address!("61815774383099e24810ab832a5b2a5425c154d5")),
524 value: U256::from(3000000000000000000u64),
525 input: Default::default(),
526 access_list: Default::default(),
527 }),
528 Signature::new(
529 U256::from_str(
530 "0x59e6b67f48fb32e7e570dfb11e042b5ad2e55e3ce3ce9cd989c7e06e07feeafd",
531 )
532 .unwrap(),
533 U256::from_str(
534 "0x016b83f4f980694ed2eee4d10667242b1f40dc406901b34125b008d334d47469",
535 )
536 .unwrap(),
537 true,
538 ),
539 ),
540 TransactionSigned::new_unhashed(
541 Transaction::Legacy(TxLegacy {
542 chain_id: Some(4),
543 nonce: 3u64,
544 gas_price: 2000000000,
545 gas_limit: 10000000,
546 to: TxKind::Call(address!("d3e8763675e4c425df46cc3b5c0f6cbdac396046")),
547 value: U256::from(1000000000000000u64),
548 input: Default::default(),
549 }),
550 Signature::new(
551 U256::from_str(
552 "0xce6834447c0a4193c40382e6c57ae33b241379c5418caac9cdc18d786fd12071",
553 )
554 .unwrap(),
555 U256::from_str(
556 "0x3ca3ae86580e94550d7c071e3a02eadb5a77830947c9225165cf9100901bee88",
557 )
558 .unwrap(),
559 false,
560 ),
561 ),
562 TransactionSigned::new_unhashed(
563 Transaction::Legacy(TxLegacy {
564 chain_id: Some(4),
565 nonce: 1u64,
566 gas_price: 1000000000,
567 gas_limit: 100000,
568 to: TxKind::Call(address!("d3e8763675e4c425df46cc3b5c0f6cbdac396046")),
569 value: U256::from(693361000000000u64),
570 input: Default::default(),
571 }),
572 Signature::new(
573 U256::from_str(
574 "0xe24d8bd32ad906d6f8b8d7741e08d1959df021698b19ee232feba15361587d0a",
575 )
576 .unwrap(),
577 U256::from_str(
578 "0x5406ad177223213df262cb66ccbb2f46bfdccfdfbbb5ffdda9e2c02d977631da",
579 )
580 .unwrap(),
581 false,
582 ),
583 ),
584 TransactionSigned::new_unhashed(
585 Transaction::Legacy(TxLegacy {
586 chain_id: Some(4),
587 nonce: 2u64,
588 gas_price: 1000000000,
589 gas_limit: 100000,
590 to: TxKind::Call(address!("d3e8763675e4c425df46cc3b5c0f6cbdac396046")),
591 value: U256::from(1000000000000000u64),
592 input: Default::default(),
593 }),
594 Signature::new(
595 U256::from_str(
596 "0xeb96ca19e8a77102767a41fc85a36afd5c61ccb09911cec5d3e86e193d9c5ae",
597 )
598 .unwrap(),
599 U256::from_str(
600 "0x3a456401896b1b6055311536bf00a718568c744d8c1f9df59879e8350220ca18",
601 )
602 .unwrap(),
603 false,
604 ),
605 ),
606 ];
607 let message: Vec<PooledTransaction> = txs
608 .into_iter()
609 .map(|tx| {
610 PooledTransaction::try_from(tx)
611 .expect("Failed to convert TransactionSigned to PooledTransaction")
612 })
613 .collect();
614 let transactions = RequestPair { request_id: 0, message: PooledTransactions(message) };
615
616 let mut encoded = vec![];
617 transactions.encode(&mut encoded);
618 assert_eq!(encoded.len(), transactions.length());
619 let encoded_str = hex::encode(encoded);
620 let expected_str = hex::encode(expected);
621 assert_eq!(encoded_str.len(), expected_str.len());
622 assert_eq!(encoded_str, expected_str);
623 }
624
625 #[test]
626 fn eth72_pooled_transactions_reject_blobs() {
627 let blob_tx = |sidecar: BlobTransactionSidecarVariant| {
628 PooledTransactionVariant::Eip4844(
629 TxEip4844WithSidecar {
630 tx: TxEip4844 { blob_versioned_hashes: vec![B256::ZERO], ..Default::default() },
631 sidecar,
632 }
633 .into_signed(Signature::test_signature()),
634 )
635 };
636 let full = blob_tx(
637 BlobTransactionSidecarEip7594::new(
638 vec![Blob::repeat_byte(1)],
639 vec![Bytes48::repeat_byte(2)],
640 vec![Bytes48::repeat_byte(3); 128],
641 )
642 .into(),
643 );
644 let mut without_blobs = full.clone();
645 without_blobs.clear_eip7594_blobs();
646 let legacy = blob_tx(
648 BlobTransactionSidecar::new(Vec::new(), vec![Bytes48::repeat_byte(2)], Vec::new())
649 .into(),
650 );
651 let eip1559 = PooledTransactionVariant::Eip1559(
652 TxEip1559::default().into_signed(Signature::test_signature()),
653 );
654 let legacy_tx = PooledTransactionVariant::Legacy(
655 TxLegacy::default().into_signed(Signature::test_signature()),
656 );
657
658 for (tx, fits_eth72) in [
659 (without_blobs, true),
660 (eip1559, true),
661 (legacy_tx, true),
662 (full, false),
663 (legacy, false),
664 ] {
665 let response = PooledTransactions(vec![tx]);
666 let encoded = alloy_rlp::encode(&response);
667 let eth72 = PooledTransactions::decode_eth72_with_memory_budget(
668 &mut encoded.as_slice(),
669 usize::MAX,
670 );
671 if fits_eth72 {
672 assert_eq!(eth72, Ok(response.clone()));
673 } else {
674 assert_eq!(
675 eth72,
676 Err(alloy_rlp::Error::Custom("expected a blobless eth/72 pooled transaction"))
677 );
678 }
679 assert_eq!(
680 PooledTransactions::decode_with_memory_budget(&mut encoded.as_slice(), usize::MAX),
681 Ok(response)
682 );
683 }
684 }
685}