alloy_consensus/transaction/
eip4844.rs1use super::{RlpEcdsaDecodableTx, RlpEcdsaEncodableTx, TxEip4844Sidecar};
2use crate::{SignableTransaction, Signed, Transaction, TxType};
3use alloc::vec::Vec;
4use alloy_eips::{
5 eip2718::IsTyped2718,
6 eip2930::AccessList,
7 eip4844::{BlobTransactionSidecar, DATA_GAS_PER_BLOB},
8 eip7594::{Decodable7594, Encodable7594},
9 eip7702::SignedAuthorization,
10 Typed2718,
11};
12use alloy_primitives::{Address, Bytes, ChainId, Signature, TxKind, B256, U256};
13use alloy_rlp::{BufMut, Decodable, Encodable, Header};
14use core::{fmt, mem};
15
16#[cfg(feature = "kzg")]
17use alloy_eips::eip4844::BlobTransactionValidationError;
18
19#[derive(Clone, Debug, PartialEq, Eq, Hash)]
26#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
27#[cfg_attr(feature = "serde", derive(serde::Serialize))]
28#[cfg_attr(feature = "serde", serde(untagged))]
29#[doc(alias = "Eip4844TransactionVariant")]
30pub enum TxEip4844Variant<T = BlobTransactionSidecar> {
31 TxEip4844(TxEip4844),
33 TxEip4844WithSidecar(TxEip4844WithSidecar<T>),
35}
36
37#[cfg(feature = "serde")]
38impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for TxEip4844Variant<T> {
39 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
40 where
41 D: serde::Deserializer<'de>,
42 {
43 #[derive(serde::Deserialize)]
44 struct TxEip4844SerdeHelper<Sidecar> {
45 #[serde(flatten)]
46 #[doc(alias = "transaction")]
47 tx: TxEip4844,
48 #[serde(flatten)]
49 sidecar: Option<Sidecar>,
50 }
51
52 let tx = TxEip4844SerdeHelper::<T>::deserialize(deserializer)?;
53
54 if let Some(sidecar) = tx.sidecar {
55 Ok(TxEip4844WithSidecar::from_tx_and_sidecar(tx.tx, sidecar).into())
56 } else {
57 Ok(tx.tx.into())
58 }
59 }
60}
61
62impl<T> From<Signed<TxEip4844>> for Signed<TxEip4844Variant<T>> {
63 fn from(value: Signed<TxEip4844>) -> Self {
64 let (tx, signature, hash) = value.into_parts();
65 Self::new_unchecked(TxEip4844Variant::TxEip4844(tx), signature, hash)
66 }
67}
68
69impl<T: Encodable7594> From<Signed<TxEip4844WithSidecar<T>>> for Signed<TxEip4844Variant<T>> {
70 fn from(value: Signed<TxEip4844WithSidecar<T>>) -> Self {
71 let (tx, signature, hash) = value.into_parts();
72 Self::new_unchecked(TxEip4844Variant::TxEip4844WithSidecar(tx), signature, hash)
73 }
74}
75
76impl<T> From<TxEip4844WithSidecar<T>> for TxEip4844Variant<T> {
77 fn from(tx: TxEip4844WithSidecar<T>) -> Self {
78 Self::TxEip4844WithSidecar(tx)
79 }
80}
81
82impl<T> From<TxEip4844> for TxEip4844Variant<T> {
83 fn from(tx: TxEip4844) -> Self {
84 Self::TxEip4844(tx)
85 }
86}
87
88impl From<(TxEip4844, BlobTransactionSidecar)> for TxEip4844Variant<BlobTransactionSidecar> {
89 fn from((tx, sidecar): (TxEip4844, BlobTransactionSidecar)) -> Self {
90 TxEip4844WithSidecar::from_tx_and_sidecar(tx, sidecar).into()
91 }
92}
93
94impl<T> From<TxEip4844Variant<T>> for TxEip4844 {
95 fn from(tx: TxEip4844Variant<T>) -> Self {
96 match tx {
97 TxEip4844Variant::TxEip4844(tx) => tx,
98 TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx,
99 }
100 }
101}
102
103impl<T> AsRef<TxEip4844> for TxEip4844Variant<T> {
104 fn as_ref(&self) -> &TxEip4844 {
105 match self {
106 Self::TxEip4844(tx) => tx,
107 Self::TxEip4844WithSidecar(tx) => &tx.tx,
108 }
109 }
110}
111
112impl<T> AsMut<TxEip4844> for TxEip4844Variant<T> {
113 fn as_mut(&mut self) -> &mut TxEip4844 {
114 match self {
115 Self::TxEip4844(tx) => tx,
116 Self::TxEip4844WithSidecar(tx) => &mut tx.tx,
117 }
118 }
119}
120
121impl AsRef<Self> for TxEip4844 {
122 fn as_ref(&self) -> &Self {
123 self
124 }
125}
126
127impl AsMut<Self> for TxEip4844 {
128 fn as_mut(&mut self) -> &mut Self {
129 self
130 }
131}
132
133impl<T> TxEip4844Variant<T> {
134 #[doc(alias = "transaction_type")]
136 pub const fn tx_type() -> TxType {
137 TxType::Eip4844
138 }
139
140 #[doc(alias = "transaction")]
142 pub const fn tx(&self) -> &TxEip4844 {
143 match self {
144 Self::TxEip4844(tx) => tx,
145 Self::TxEip4844WithSidecar(tx) => tx.tx(),
146 }
147 }
148
149 pub const fn as_with_sidecar(&self) -> Option<&TxEip4844WithSidecar<T>> {
151 match self {
152 Self::TxEip4844WithSidecar(tx) => Some(tx),
153 _ => None,
154 }
155 }
156
157 pub fn try_into_4844_with_sidecar(self) -> Result<TxEip4844WithSidecar<T>, Self> {
160 match self {
161 Self::TxEip4844WithSidecar(tx) => Ok(tx),
162 _ => Err(self),
163 }
164 }
165
166 pub const fn sidecar(&self) -> Option<&T> {
168 match self {
169 Self::TxEip4844WithSidecar(tx) => Some(tx.sidecar()),
170 _ => None,
171 }
172 }
173}
174
175impl<T: TxEip4844Sidecar> TxEip4844Variant<T> {
176 #[cfg(feature = "kzg")]
180 pub fn validate(
181 &self,
182 proof_settings: &c_kzg::KzgSettings,
183 ) -> Result<(), BlobTransactionValidationError> {
184 match self {
185 Self::TxEip4844(_) => Err(BlobTransactionValidationError::MissingSidecar),
186 Self::TxEip4844WithSidecar(tx) => tx.validate_blob(proof_settings),
187 }
188 }
189
190 #[inline]
192 pub fn size(&self) -> usize {
193 match self {
194 Self::TxEip4844(tx) => tx.size(),
195 Self::TxEip4844WithSidecar(tx) => tx.size(),
196 }
197 }
198}
199
200impl<T> Transaction for TxEip4844Variant<T>
201where
202 T: fmt::Debug + Send + Sync + 'static,
203{
204 #[inline]
205 fn chain_id(&self) -> Option<ChainId> {
206 match self {
207 Self::TxEip4844(tx) => Some(tx.chain_id),
208 Self::TxEip4844WithSidecar(tx) => Some(tx.tx().chain_id),
209 }
210 }
211
212 #[inline]
213 fn nonce(&self) -> u64 {
214 match self {
215 Self::TxEip4844(tx) => tx.nonce,
216 Self::TxEip4844WithSidecar(tx) => tx.tx().nonce,
217 }
218 }
219
220 #[inline]
221 fn gas_limit(&self) -> u64 {
222 match self {
223 Self::TxEip4844(tx) => tx.gas_limit,
224 Self::TxEip4844WithSidecar(tx) => tx.tx().gas_limit,
225 }
226 }
227
228 #[inline]
229 fn gas_price(&self) -> Option<u128> {
230 None
231 }
232
233 #[inline]
234 fn max_fee_per_gas(&self) -> u128 {
235 match self {
236 Self::TxEip4844(tx) => tx.max_fee_per_gas(),
237 Self::TxEip4844WithSidecar(tx) => tx.max_fee_per_gas(),
238 }
239 }
240
241 #[inline]
242 fn max_priority_fee_per_gas(&self) -> Option<u128> {
243 match self {
244 Self::TxEip4844(tx) => tx.max_priority_fee_per_gas(),
245 Self::TxEip4844WithSidecar(tx) => tx.max_priority_fee_per_gas(),
246 }
247 }
248
249 #[inline]
250 fn max_fee_per_blob_gas(&self) -> Option<u128> {
251 match self {
252 Self::TxEip4844(tx) => tx.max_fee_per_blob_gas(),
253 Self::TxEip4844WithSidecar(tx) => tx.max_fee_per_blob_gas(),
254 }
255 }
256
257 #[inline]
258 fn priority_fee_or_price(&self) -> u128 {
259 match self {
260 Self::TxEip4844(tx) => tx.priority_fee_or_price(),
261 Self::TxEip4844WithSidecar(tx) => tx.priority_fee_or_price(),
262 }
263 }
264
265 fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
266 match self {
267 Self::TxEip4844(tx) => tx.effective_gas_price(base_fee),
268 Self::TxEip4844WithSidecar(tx) => tx.effective_gas_price(base_fee),
269 }
270 }
271
272 #[inline]
273 fn is_dynamic_fee(&self) -> bool {
274 match self {
275 Self::TxEip4844(tx) => tx.is_dynamic_fee(),
276 Self::TxEip4844WithSidecar(tx) => tx.is_dynamic_fee(),
277 }
278 }
279
280 #[inline]
281 fn kind(&self) -> TxKind {
282 match self {
283 Self::TxEip4844(tx) => tx.to,
284 Self::TxEip4844WithSidecar(tx) => tx.tx.to,
285 }
286 .into()
287 }
288
289 #[inline]
290 fn is_create(&self) -> bool {
291 false
292 }
293
294 #[inline]
295 fn value(&self) -> U256 {
296 match self {
297 Self::TxEip4844(tx) => tx.value,
298 Self::TxEip4844WithSidecar(tx) => tx.tx.value,
299 }
300 }
301
302 #[inline]
303 fn input(&self) -> &Bytes {
304 match self {
305 Self::TxEip4844(tx) => tx.input(),
306 Self::TxEip4844WithSidecar(tx) => tx.tx().input(),
307 }
308 }
309
310 #[inline]
311 fn access_list(&self) -> Option<&AccessList> {
312 match self {
313 Self::TxEip4844(tx) => tx.access_list(),
314 Self::TxEip4844WithSidecar(tx) => tx.access_list(),
315 }
316 }
317
318 #[inline]
319 fn blob_versioned_hashes(&self) -> Option<&[B256]> {
320 match self {
321 Self::TxEip4844(tx) => tx.blob_versioned_hashes(),
322 Self::TxEip4844WithSidecar(tx) => tx.blob_versioned_hashes(),
323 }
324 }
325
326 #[inline]
327 fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
328 None
329 }
330}
331impl Typed2718 for TxEip4844 {
332 fn ty(&self) -> u8 {
333 TxType::Eip4844 as u8
334 }
335}
336
337impl<T: Encodable7594> RlpEcdsaEncodableTx for TxEip4844Variant<T> {
338 fn rlp_encoded_fields_length(&self) -> usize {
339 match self {
340 Self::TxEip4844(inner) => inner.rlp_encoded_fields_length(),
341 Self::TxEip4844WithSidecar(inner) => inner.rlp_encoded_fields_length(),
342 }
343 }
344
345 fn rlp_encode_fields(&self, out: &mut dyn alloy_rlp::BufMut) {
346 match self {
347 Self::TxEip4844(inner) => inner.rlp_encode_fields(out),
348 Self::TxEip4844WithSidecar(inner) => inner.rlp_encode_fields(out),
349 }
350 }
351
352 fn rlp_header_signed(&self, signature: &Signature) -> Header {
353 match self {
354 Self::TxEip4844(inner) => inner.rlp_header_signed(signature),
355 Self::TxEip4844WithSidecar(inner) => inner.rlp_header_signed(signature),
356 }
357 }
358
359 fn rlp_encode_signed(&self, signature: &Signature, out: &mut dyn BufMut) {
360 match self {
361 Self::TxEip4844(inner) => inner.rlp_encode_signed(signature, out),
362 Self::TxEip4844WithSidecar(inner) => inner.rlp_encode_signed(signature, out),
363 }
364 }
365
366 fn tx_hash_with_type(&self, signature: &Signature, ty: u8) -> alloy_primitives::TxHash {
367 match self {
368 Self::TxEip4844(inner) => inner.tx_hash_with_type(signature, ty),
369 Self::TxEip4844WithSidecar(inner) => inner.tx_hash_with_type(signature, ty),
370 }
371 }
372}
373
374impl<T: Encodable7594 + Decodable7594> RlpEcdsaDecodableTx for TxEip4844Variant<T> {
375 const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };
376
377 fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
378 let needle = &mut &**buf;
379
380 let trial = &mut &**buf;
383
384 if Header::decode(needle).is_ok_and(|h| h.list) {
391 if let Ok(tx) = TxEip4844WithSidecar::rlp_decode_fields(trial) {
392 *buf = *trial;
393 return Ok(tx.into());
394 }
395 }
396 TxEip4844::rlp_decode_fields(buf).map(Into::into)
397 }
398
399 fn rlp_decode_with_signature(buf: &mut &[u8]) -> alloy_rlp::Result<(Self, Signature)> {
400 let needle = &mut &**buf;
403
404 let trial = &mut &**buf;
407
408 Header::decode(needle)?;
410
411 if Header::decode(needle).is_ok_and(|h| h.list) {
418 if let Ok((tx, signature)) = TxEip4844WithSidecar::rlp_decode_with_signature(trial) {
419 *buf = *trial;
422 return Ok((tx.into(), signature));
423 }
424 }
425 TxEip4844::rlp_decode_with_signature(buf).map(|(tx, signature)| (tx.into(), signature))
426 }
427}
428
429impl<T> Typed2718 for TxEip4844Variant<T> {
430 fn ty(&self) -> u8 {
431 TxType::Eip4844 as u8
432 }
433}
434
435impl IsTyped2718 for TxEip4844 {
436 fn is_type(type_id: u8) -> bool {
437 matches!(type_id, 0x03)
438 }
439}
440
441impl<T> SignableTransaction<Signature> for TxEip4844Variant<T>
442where
443 T: fmt::Debug + Send + Sync + 'static,
444{
445 fn set_chain_id(&mut self, chain_id: ChainId) {
446 match self {
447 Self::TxEip4844(inner) => {
448 inner.set_chain_id(chain_id);
449 }
450 Self::TxEip4844WithSidecar(inner) => {
451 inner.set_chain_id(chain_id);
452 }
453 }
454 }
455
456 fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) {
457 self.tx().encode_for_signing(out);
463 }
464
465 fn payload_len_for_signature(&self) -> usize {
466 self.tx().payload_len_for_signature()
467 }
468}
469
470#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
474#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
475#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
476#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
477#[doc(alias = "Eip4844Transaction", alias = "TransactionEip4844", alias = "Eip4844Tx")]
478pub struct TxEip4844 {
479 #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
481 pub chain_id: ChainId,
482 #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
484 pub nonce: u64,
485 #[cfg_attr(
491 feature = "serde",
492 serde(with = "alloy_serde::quantity", rename = "gas", alias = "gasLimit")
493 )]
494 pub gas_limit: u64,
495 #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
507 pub max_fee_per_gas: u128,
508 #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
516 pub max_priority_fee_per_gas: u128,
517 pub to: Address,
519 pub value: U256,
524 pub access_list: AccessList,
530
531 pub blob_versioned_hashes: Vec<B256>,
533
534 #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
538 pub max_fee_per_blob_gas: u128,
539
540 pub input: Bytes,
546}
547
548impl TxEip4844 {
549 #[inline]
551 pub fn blob_gas(&self) -> u64 {
552 self.blob_versioned_hashes.len() as u64 * DATA_GAS_PER_BLOB
554 }
555
556 #[cfg(feature = "kzg")]
570 pub fn validate_blob<T: TxEip4844Sidecar>(
571 &self,
572 sidecar: &T,
573 proof_settings: &c_kzg::KzgSettings,
574 ) -> Result<(), BlobTransactionValidationError> {
575 sidecar.validate(&self.blob_versioned_hashes, proof_settings)
576 }
577
578 #[doc(alias = "transaction_type")]
580 pub const fn tx_type() -> TxType {
581 TxType::Eip4844
582 }
583
584 pub const fn with_sidecar<T>(self, sidecar: T) -> TxEip4844WithSidecar<T> {
586 TxEip4844WithSidecar::from_tx_and_sidecar(self, sidecar)
587 }
588
589 #[inline]
591 pub fn size(&self) -> usize {
592 mem::size_of::<ChainId>() + mem::size_of::<u64>() + mem::size_of::<u64>() + mem::size_of::<u128>() + mem::size_of::<u128>() + mem::size_of::<Address>() + mem::size_of::<U256>() + self.access_list.size() + self.input.len() + self.blob_versioned_hashes.capacity() * mem::size_of::<B256>() + mem::size_of::<u128>() }
604}
605
606impl RlpEcdsaEncodableTx for TxEip4844 {
607 fn rlp_encoded_fields_length(&self) -> usize {
608 self.chain_id.length()
609 + self.nonce.length()
610 + self.gas_limit.length()
611 + self.max_fee_per_gas.length()
612 + self.max_priority_fee_per_gas.length()
613 + self.to.length()
614 + self.value.length()
615 + self.access_list.length()
616 + self.blob_versioned_hashes.length()
617 + self.max_fee_per_blob_gas.length()
618 + self.input.0.length()
619 }
620
621 fn rlp_encode_fields(&self, out: &mut dyn alloy_rlp::BufMut) {
622 self.chain_id.encode(out);
623 self.nonce.encode(out);
624 self.max_priority_fee_per_gas.encode(out);
625 self.max_fee_per_gas.encode(out);
626 self.gas_limit.encode(out);
627 self.to.encode(out);
628 self.value.encode(out);
629 self.input.0.encode(out);
630 self.access_list.encode(out);
631 self.max_fee_per_blob_gas.encode(out);
632 self.blob_versioned_hashes.encode(out);
633 }
634}
635
636impl RlpEcdsaDecodableTx for TxEip4844 {
637 const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };
638
639 fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
640 Ok(Self {
641 chain_id: Decodable::decode(buf)?,
642 nonce: Decodable::decode(buf)?,
643 max_priority_fee_per_gas: Decodable::decode(buf)?,
644 max_fee_per_gas: Decodable::decode(buf)?,
645 gas_limit: Decodable::decode(buf)?,
646 to: Decodable::decode(buf)?,
647 value: Decodable::decode(buf)?,
648 input: Decodable::decode(buf)?,
649 access_list: Decodable::decode(buf)?,
650 max_fee_per_blob_gas: Decodable::decode(buf)?,
651 blob_versioned_hashes: Decodable::decode(buf)?,
652 })
653 }
654}
655
656impl SignableTransaction<Signature> for TxEip4844 {
657 fn set_chain_id(&mut self, chain_id: ChainId) {
658 self.chain_id = chain_id;
659 }
660
661 fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) {
662 out.put_u8(Self::tx_type() as u8);
663 self.encode(out);
664 }
665
666 fn payload_len_for_signature(&self) -> usize {
667 self.length() + 1
668 }
669}
670
671impl Transaction for TxEip4844 {
672 #[inline]
673 fn chain_id(&self) -> Option<ChainId> {
674 Some(self.chain_id)
675 }
676
677 #[inline]
678 fn nonce(&self) -> u64 {
679 self.nonce
680 }
681
682 #[inline]
683 fn gas_limit(&self) -> u64 {
684 self.gas_limit
685 }
686
687 #[inline]
688 fn gas_price(&self) -> Option<u128> {
689 None
690 }
691
692 #[inline]
693 fn max_fee_per_gas(&self) -> u128 {
694 self.max_fee_per_gas
695 }
696
697 #[inline]
698 fn max_priority_fee_per_gas(&self) -> Option<u128> {
699 Some(self.max_priority_fee_per_gas)
700 }
701
702 #[inline]
703 fn max_fee_per_blob_gas(&self) -> Option<u128> {
704 Some(self.max_fee_per_blob_gas)
705 }
706
707 #[inline]
708 fn priority_fee_or_price(&self) -> u128 {
709 self.max_priority_fee_per_gas
710 }
711
712 fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
713 alloy_eips::eip1559::calc_effective_gas_price(
714 self.max_fee_per_gas,
715 self.max_priority_fee_per_gas,
716 base_fee,
717 )
718 }
719
720 #[inline]
721 fn is_dynamic_fee(&self) -> bool {
722 true
723 }
724
725 #[inline]
726 fn kind(&self) -> TxKind {
727 self.to.into()
728 }
729
730 #[inline]
731 fn is_create(&self) -> bool {
732 false
733 }
734
735 #[inline]
736 fn value(&self) -> U256 {
737 self.value
738 }
739
740 #[inline]
741 fn input(&self) -> &Bytes {
742 &self.input
743 }
744
745 #[inline]
746 fn access_list(&self) -> Option<&AccessList> {
747 Some(&self.access_list)
748 }
749
750 #[inline]
751 fn blob_versioned_hashes(&self) -> Option<&[B256]> {
752 Some(&self.blob_versioned_hashes)
753 }
754
755 #[inline]
756 fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
757 None
758 }
759}
760
761impl Encodable for TxEip4844 {
762 fn encode(&self, out: &mut dyn BufMut) {
763 self.rlp_encode(out);
764 }
765
766 fn length(&self) -> usize {
767 self.rlp_encoded_length()
768 }
769}
770
771impl Decodable for TxEip4844 {
772 fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
773 Self::rlp_decode(buf)
774 }
775}
776
777impl<T> From<TxEip4844WithSidecar<T>> for TxEip4844 {
778 fn from(tx_with_sidecar: TxEip4844WithSidecar<T>) -> Self {
780 tx_with_sidecar.tx
781 }
782}
783
784#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
794#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
795#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
796#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
797#[doc(alias = "Eip4844TransactionWithSidecar", alias = "Eip4844TxWithSidecar")]
798pub struct TxEip4844WithSidecar<T = BlobTransactionSidecar> {
799 #[cfg_attr(feature = "serde", serde(flatten))]
801 #[doc(alias = "transaction")]
802 pub tx: TxEip4844,
803 #[cfg_attr(feature = "serde", serde(flatten))]
805 pub sidecar: T,
806}
807
808impl<T> TxEip4844WithSidecar<T> {
809 #[doc(alias = "from_transaction_and_sidecar")]
811 pub const fn from_tx_and_sidecar(tx: TxEip4844, sidecar: T) -> Self {
812 Self { tx, sidecar }
813 }
814
815 #[doc(alias = "transaction_type")]
817 pub const fn tx_type() -> TxType {
818 TxEip4844::tx_type()
819 }
820
821 #[doc(alias = "transaction")]
823 pub const fn tx(&self) -> &TxEip4844 {
824 &self.tx
825 }
826
827 pub const fn sidecar(&self) -> &T {
829 &self.sidecar
830 }
831
832 pub fn into_sidecar(self) -> T {
834 self.sidecar
835 }
836
837 pub fn into_parts(self) -> (TxEip4844, T) {
839 (self.tx, self.sidecar)
840 }
841}
842
843impl<T: TxEip4844Sidecar> TxEip4844WithSidecar<T> {
844 #[cfg(feature = "kzg")]
848 pub fn validate_blob(
849 &self,
850 proof_settings: &c_kzg::KzgSettings,
851 ) -> Result<(), BlobTransactionValidationError> {
852 self.tx.validate_blob(&self.sidecar, proof_settings)
853 }
854
855 #[inline]
857 pub fn size(&self) -> usize {
858 self.tx.size() + self.sidecar.size()
859 }
860}
861
862impl<T> SignableTransaction<Signature> for TxEip4844WithSidecar<T>
863where
864 T: fmt::Debug + Send + Sync + 'static,
865{
866 fn set_chain_id(&mut self, chain_id: ChainId) {
867 self.tx.chain_id = chain_id;
868 }
869
870 fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) {
871 self.tx.encode_for_signing(out);
877 }
878
879 fn payload_len_for_signature(&self) -> usize {
880 self.tx.payload_len_for_signature()
883 }
884}
885
886impl<T> Transaction for TxEip4844WithSidecar<T>
887where
888 T: fmt::Debug + Send + Sync + 'static,
889{
890 #[inline]
891 fn chain_id(&self) -> Option<ChainId> {
892 self.tx.chain_id()
893 }
894
895 #[inline]
896 fn nonce(&self) -> u64 {
897 self.tx.nonce()
898 }
899
900 #[inline]
901 fn gas_limit(&self) -> u64 {
902 self.tx.gas_limit()
903 }
904
905 #[inline]
906 fn gas_price(&self) -> Option<u128> {
907 self.tx.gas_price()
908 }
909
910 #[inline]
911 fn max_fee_per_gas(&self) -> u128 {
912 self.tx.max_fee_per_gas()
913 }
914
915 #[inline]
916 fn max_priority_fee_per_gas(&self) -> Option<u128> {
917 self.tx.max_priority_fee_per_gas()
918 }
919
920 #[inline]
921 fn max_fee_per_blob_gas(&self) -> Option<u128> {
922 self.tx.max_fee_per_blob_gas()
923 }
924
925 #[inline]
926 fn priority_fee_or_price(&self) -> u128 {
927 self.tx.priority_fee_or_price()
928 }
929
930 fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
931 self.tx.effective_gas_price(base_fee)
932 }
933
934 #[inline]
935 fn is_dynamic_fee(&self) -> bool {
936 self.tx.is_dynamic_fee()
937 }
938
939 #[inline]
940 fn kind(&self) -> TxKind {
941 self.tx.kind()
942 }
943
944 #[inline]
945 fn is_create(&self) -> bool {
946 false
947 }
948
949 #[inline]
950 fn value(&self) -> U256 {
951 self.tx.value()
952 }
953
954 #[inline]
955 fn input(&self) -> &Bytes {
956 self.tx.input()
957 }
958
959 #[inline]
960 fn access_list(&self) -> Option<&AccessList> {
961 Some(&self.tx.access_list)
962 }
963
964 #[inline]
965 fn blob_versioned_hashes(&self) -> Option<&[B256]> {
966 self.tx.blob_versioned_hashes()
967 }
968
969 #[inline]
970 fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
971 None
972 }
973}
974
975impl<T> Typed2718 for TxEip4844WithSidecar<T> {
976 fn ty(&self) -> u8 {
977 TxType::Eip4844 as u8
978 }
979}
980
981impl<T: Encodable7594> RlpEcdsaEncodableTx for TxEip4844WithSidecar<T> {
982 fn rlp_encoded_fields_length(&self) -> usize {
983 self.sidecar.encode_7594_len() + self.tx.rlp_encoded_length()
984 }
985
986 fn rlp_encode_fields(&self, out: &mut dyn alloy_rlp::BufMut) {
987 self.tx.rlp_encode(out);
988 self.sidecar.encode_7594(out);
989 }
990
991 fn rlp_header_signed(&self, signature: &Signature) -> Header {
992 let payload_length =
993 self.tx.rlp_encoded_length_with_signature(signature) + self.sidecar.encode_7594_len();
994 Header { list: true, payload_length }
995 }
996
997 fn rlp_encode_signed(&self, signature: &Signature, out: &mut dyn BufMut) {
998 self.rlp_header_signed(signature).encode(out);
999 self.tx.rlp_encode_signed(signature, out);
1000 self.sidecar.encode_7594(out);
1001 }
1002
1003 fn tx_hash_with_type(&self, signature: &Signature, ty: u8) -> alloy_primitives::TxHash {
1004 self.tx.tx_hash_with_type(signature, ty)
1006 }
1007}
1008
1009impl<T: Encodable7594 + Decodable7594> RlpEcdsaDecodableTx for TxEip4844WithSidecar<T> {
1010 const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };
1011
1012 fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
1013 let tx = TxEip4844::rlp_decode(buf)?;
1014 let sidecar = T::decode_7594(buf)?;
1015 Ok(Self { tx, sidecar })
1016 }
1017
1018 fn rlp_decode_with_signature(buf: &mut &[u8]) -> alloy_rlp::Result<(Self, Signature)> {
1019 let header = Header::decode(buf)?;
1020 if !header.list {
1021 return Err(alloy_rlp::Error::UnexpectedString);
1022 }
1023 let remaining = buf.len();
1024
1025 let (tx, signature) = TxEip4844::rlp_decode_with_signature(buf)?;
1026 let sidecar = T::decode_7594(buf)?;
1027
1028 if buf.len() + header.payload_length != remaining {
1029 return Err(alloy_rlp::Error::UnexpectedLength);
1030 }
1031
1032 Ok((Self { tx, sidecar }, signature))
1033 }
1034}
1035
1036#[cfg(test)]
1037mod tests {
1038 use super::{BlobTransactionSidecar, TxEip4844, TxEip4844WithSidecar};
1039 use crate::{
1040 transaction::{eip4844::TxEip4844Variant, RlpEcdsaDecodableTx},
1041 SignableTransaction, TxEnvelope,
1042 };
1043 use alloy_eips::{
1044 eip2930::AccessList, eip4844::env_settings::EnvKzgSettings,
1045 eip7594::BlobTransactionSidecarVariant, Encodable2718 as _,
1046 };
1047 use alloy_primitives::{address, b256, bytes, hex, Signature, U256};
1048 use alloy_rlp::{Decodable, Encodable};
1049 use assert_matches::assert_matches;
1050 use std::path::PathBuf;
1051
1052 #[test]
1053 fn different_sidecar_same_hash() {
1054 let tx = TxEip4844 {
1057 chain_id: 1,
1058 nonce: 1,
1059 max_priority_fee_per_gas: 1,
1060 max_fee_per_gas: 1,
1061 gas_limit: 1,
1062 to: Default::default(),
1063 value: U256::from(1),
1064 access_list: Default::default(),
1065 blob_versioned_hashes: vec![Default::default()],
1066 max_fee_per_blob_gas: 1,
1067 input: Default::default(),
1068 };
1069 let sidecar = BlobTransactionSidecar {
1070 blobs: vec![[2; 131072].into()],
1071 commitments: vec![[3; 48].into()],
1072 proofs: vec![[4; 48].into()],
1073 };
1074 let mut tx = TxEip4844WithSidecar { tx, sidecar };
1075 let signature = Signature::test_signature();
1076
1077 let expected_signed = tx.clone().into_signed(signature);
1079
1080 tx.sidecar = BlobTransactionSidecar {
1082 blobs: vec![[1; 131072].into()],
1083 commitments: vec![[1; 48].into()],
1084 proofs: vec![[1; 48].into()],
1085 };
1086
1087 let actual_signed = tx.into_signed(signature);
1089
1090 assert_eq!(expected_signed.hash(), actual_signed.hash());
1092
1093 let expected_envelope: TxEnvelope = expected_signed.into();
1095 let actual_envelope: TxEnvelope = actual_signed.into();
1096
1097 let len = expected_envelope.length();
1099 let mut buf = Vec::with_capacity(len);
1100 expected_envelope.encode(&mut buf);
1101 assert_eq!(buf.len(), len);
1102
1103 assert_eq!(buf.len(), actual_envelope.length());
1106
1107 let decoded = TxEnvelope::decode(&mut &buf[..]).unwrap();
1109 assert_eq!(decoded, expected_envelope);
1110 }
1111
1112 #[test]
1113 fn test_4844_variant_into_signed_correct_hash() {
1114 let tx =
1116 TxEip4844 {
1117 chain_id: 1,
1118 nonce: 15435,
1119 gas_limit: 8000000,
1120 max_fee_per_gas: 10571233596,
1121 max_priority_fee_per_gas: 1000000000,
1122 to: address!("a8cb082a5a689e0d594d7da1e2d72a3d63adc1bd"),
1123 value: U256::ZERO,
1124 access_list: AccessList::default(),
1125 blob_versioned_hashes: vec![
1126 b256!("01e5276d91ac1ddb3b1c2d61295211220036e9a04be24c00f76916cc2659d004"),
1127 b256!("0128eb58aff09fd3a7957cd80aa86186d5849569997cdfcfa23772811b706cc2"),
1128 ],
1129 max_fee_per_blob_gas: 1,
1130 input: bytes!("701f58c50000000000000000000000000000000000000000000000000000000000073fb1ed12e288def5b439ea074b398dbb4c967f2852baac3238c5fe4b62b871a59a6d00000000000000000000000000000000000000000000000000000000123971da000000000000000000000000000000000000000000000000000000000000000ac39b2a24e1dbdd11a1e7bd7c0f4dfd7d9b9cfa0997d033ad05f961ba3b82c6c83312c967f10daf5ed2bffe309249416e03ee0b101f2b84d2102b9e38b0e4dfdf0000000000000000000000000000000000000000000000000000000066254c8b538dcc33ecf5334bbd294469f9d4fd084a3090693599a46d6c62567747cbc8660000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000073fb20000000000000000000000000000000000000000000000000000000066254da10000000000000000000000000000000000000000000000000000000012397d5e20b09b263779fda4171c341e720af8fa469621ff548651f8dbbc06c2d320400c000000000000000000000000000000000000000000000000000000000000000b50a833bb11af92814e99c6ff7cf7ba7042827549d6f306a04270753702d897d8fc3c411b99159939ac1c16d21d3057ddc8b2333d1331ab34c938cff0eb29ce2e43241c170344db6819f76b1f1e0ab8206f3ec34120312d275c4f5bbea7f5c55700000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000480000000000000000000000000000000000000000000000000000000000000031800000000000000000000000000000000000000000000800b0000000000000000000000000000000000000000000000000000000000000004ed12e288def5b439ea074b398dbb4c967f2852baac3238c5fe4b62b871a59a6d00000ca8000000000000000000000000000000000000800b000000000000000000000000000000000000000000000000000000000000000300000000000000000000000066254da100000000000000000000000066254e9d00010ca80000000000000000000000000000000000008001000000000000000000000000000000000000000000000000000000000000000550a833bb11af92814e99c6ff7cf7ba7042827549d6f306a04270753702d897d800010ca800000000000000000000000000000000000080010000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000b00010ca8000000000000000000000000000000000000801100000000000000000000000000000000000000000000000000000000000000075c1cd5bd0fd333ce9d7c8edfc79f43b8f345b4a394f6aba12a2cc78ce4012ed700010ca80000000000000000000000000000000000008011000000000000000000000000000000000000000000000000000000000000000845392775318aa47beaafbdc827da38c9f1e88c3bdcabba2cb493062e17cbf21e00010ca800000000000000000000000000000000000080080000000000000000000000000000000000000000000000000000000000000000c094e20e7ac9b433f44a5885e3bdc07e51b309aeb993caa24ba84a661ac010c100010ca800000000000000000000000000000000000080080000000000000000000000000000000000000000000000000000000000000001ab42db8f4ed810bdb143368a2b641edf242af6e3d0de8b1486e2b0e7880d431100010ca8000000000000000000000000000000000000800800000000000000000000000000000000000000000000000000000000000000022d94e4cc4525e4e2d81e8227b6172e97076431a2cf98792d978035edd6e6f3100000000000000000000000000000000000000000000000000000000000000000000000000000012101c74dfb80a80fccb9a4022b2406f79f56305e6a7c931d30140f5d372fe793837e93f9ec6b8d89a9d0ab222eeb27547f66b90ec40fbbdd2a4936b0b0c19ca684ff78888fbf5840d7c8dc3c493b139471750938d7d2c443e2d283e6c5ee9fde3765a756542c42f002af45c362b4b5b1687a8fc24cbf16532b903f7bb289728170dcf597f5255508c623ba247735538376f494cdcdd5bd0c4cb067526eeda0f4745a28d8baf8893ecc1b8cee80690538d66455294a028da03ff2add9d8a88e6ee03ba9ffe3ad7d91d6ac9c69a1f28c468f00fe55eba5651a2b32dc2458e0d14b4dd6d0173df255cd56aa01e8e38edec17ea8933f68543cbdc713279d195551d4211bed5c91f77259a695e6768f6c4b110b2158fcc42423a96dcc4e7f6fddb3e2369d00000000000000000000000000000000000000000000000000000000000000") };
1131 let variant = TxEip4844Variant::<BlobTransactionSidecar>::TxEip4844(tx);
1132
1133 let signature = Signature::new(
1134 b256!("6c173c3c8db3e3299f2f728d293b912c12e75243e3aa66911c2329b58434e2a4").into(),
1135 b256!("7dd4d1c228cedc5a414a668ab165d9e888e61e4c3b44cd7daf9cdcc4cec5d6b2").into(),
1136 false,
1137 );
1138
1139 let signed = variant.into_signed(signature);
1140 assert_eq!(
1141 *signed.hash(),
1142 b256!("93fc9daaa0726c3292a2e939df60f7e773c6a6a726a61ce43f4a217c64d85e87")
1143 );
1144 }
1145
1146 #[test]
1147 fn decode_raw_7594_rlp() {
1148 let kzg_settings = EnvKzgSettings::default();
1149 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/7594rlp");
1150 let dir = std::fs::read_dir(path).expect("Unable to read folder");
1151 for entry in dir {
1152 let entry = entry.unwrap();
1153 let content = std::fs::read_to_string(entry.path()).unwrap();
1154 let raw = hex::decode(content.trim()).unwrap();
1155 let tx = TxEip4844WithSidecar::<BlobTransactionSidecarVariant>::eip2718_decode(
1156 &mut raw.as_ref(),
1157 )
1158 .map_err(|err| {
1159 panic!("Failed to decode transaction: {:?} {:?}", err, entry.path());
1160 })
1161 .unwrap();
1162
1163 let encoded = tx.encoded_2718();
1165 assert_eq!(encoded.as_slice(), &raw[..], "{:?}", entry.path());
1166
1167 let TxEip4844WithSidecar { tx, sidecar } = tx.tx();
1168 assert_matches!(sidecar, BlobTransactionSidecarVariant::Eip7594(_));
1169
1170 let result = sidecar.validate(&tx.blob_versioned_hashes, kzg_settings.get());
1171 assert_matches!(result, Ok(()));
1172 }
1173 }
1174
1175 #[test]
1176 fn decode_raw_7594_rlp_invalid() {
1177 let kzg_settings = EnvKzgSettings::default();
1178 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/7594rlp-invalid");
1179 let dir = std::fs::read_dir(path).expect("Unable to read folder");
1180 for entry in dir {
1181 let entry = entry.unwrap();
1182
1183 if entry.path().file_name().and_then(|f| f.to_str()) == Some("0.rlp") {
1184 continue;
1185 }
1186
1187 let content = std::fs::read_to_string(entry.path()).unwrap();
1188 let raw = hex::decode(content.trim()).unwrap();
1189 let tx = TxEip4844WithSidecar::<BlobTransactionSidecarVariant>::eip2718_decode(
1190 &mut raw.as_ref(),
1191 )
1192 .map_err(|err| {
1193 panic!("Failed to decode transaction: {:?} {:?}", err, entry.path());
1194 })
1195 .unwrap();
1196
1197 let encoded = tx.encoded_2718();
1199 assert_eq!(encoded.as_slice(), &raw[..], "{:?}", entry.path());
1200
1201 let TxEip4844WithSidecar { tx, sidecar } = tx.tx();
1202 assert_matches!(sidecar, BlobTransactionSidecarVariant::Eip7594(_));
1203
1204 let result = sidecar.validate(&tx.blob_versioned_hashes, kzg_settings.get());
1205 assert_matches!(result, Err(_));
1206 }
1207 }
1208}