alloy-consensus 2.0.4

Ethereum consensus interface
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
use super::{RlpEcdsaDecodableTx, RlpEcdsaEncodableTx, TxEip4844Sidecar};
use crate::{SignableTransaction, Signed, Transaction, TxType};
use alloc::vec::Vec;
use alloy_eips::{
    eip2718::IsTyped2718,
    eip2930::AccessList,
    eip4844::{BlobTransactionSidecar, DATA_GAS_PER_BLOB},
    eip7594::{Decodable7594, Encodable7594},
    eip7702::SignedAuthorization,
    Typed2718,
};
use alloy_primitives::{Address, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use core::fmt;

#[cfg(feature = "kzg")]
use alloy_eips::eip4844::BlobTransactionValidationError;
use alloy_eips::eip7594::{BlobTransactionSidecarEip7594, BlobTransactionSidecarVariant};

/// [EIP-4844 Blob Transaction](https://eips.ethereum.org/EIPS/eip-4844#blob-transaction)
///
/// A transaction with blob hashes and max blob fee.
/// It can either be a standalone transaction, mainly seen when retrieving historical transactions,
/// or a transaction with a sidecar, which is used when submitting a transaction to the network and
/// when receiving and sending transactions during the gossip stage.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[doc(alias = "Eip4844TransactionVariant")]
pub enum TxEip4844Variant<T = BlobTransactionSidecarVariant> {
    /// A standalone transaction with blob hashes and max blob fee.
    TxEip4844(TxEip4844),
    /// A transaction with a sidecar, which contains the blob data, commitments, and proofs.
    TxEip4844WithSidecar(TxEip4844WithSidecar<T>),
}

#[cfg(feature = "serde")]
impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for TxEip4844Variant<T> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(serde::Deserialize)]
        struct TxEip4844SerdeHelper<Sidecar> {
            #[serde(flatten)]
            #[doc(alias = "transaction")]
            tx: TxEip4844,
            #[serde(flatten)]
            sidecar: Option<Sidecar>,
        }

        let tx = TxEip4844SerdeHelper::<T>::deserialize(deserializer)?;

        if let Some(sidecar) = tx.sidecar {
            Ok(TxEip4844WithSidecar::from_tx_and_sidecar(tx.tx, sidecar).into())
        } else {
            Ok(tx.tx.into())
        }
    }
}

impl<T> From<Signed<TxEip4844>> for Signed<TxEip4844Variant<T>> {
    fn from(value: Signed<TxEip4844>) -> Self {
        let (tx, signature, hash) = value.into_parts();
        Self::new_unchecked(TxEip4844Variant::TxEip4844(tx), signature, hash)
    }
}

impl<T: Encodable7594> From<Signed<TxEip4844WithSidecar<T>>> for Signed<TxEip4844Variant<T>> {
    fn from(value: Signed<TxEip4844WithSidecar<T>>) -> Self {
        let (tx, signature, hash) = value.into_parts();
        Self::new_unchecked(TxEip4844Variant::TxEip4844WithSidecar(tx), signature, hash)
    }
}

impl From<TxEip4844WithSidecar<BlobTransactionSidecar>>
    for TxEip4844Variant<BlobTransactionSidecarVariant>
{
    fn from(tx: TxEip4844WithSidecar<BlobTransactionSidecar>) -> Self {
        Self::TxEip4844WithSidecar(tx.map_sidecar(Into::into))
    }
}

impl From<TxEip4844Variant<BlobTransactionSidecar>>
    for TxEip4844Variant<BlobTransactionSidecarVariant>
{
    fn from(value: TxEip4844Variant<BlobTransactionSidecar>) -> Self {
        value.map_sidecar(Into::into)
    }
}

impl From<TxEip4844Variant<BlobTransactionSidecarEip7594>>
    for TxEip4844Variant<BlobTransactionSidecarVariant>
{
    fn from(value: TxEip4844Variant<BlobTransactionSidecarEip7594>) -> Self {
        value.map_sidecar(Into::into)
    }
}

impl<T> From<TxEip4844WithSidecar<T>> for TxEip4844Variant<T> {
    fn from(tx: TxEip4844WithSidecar<T>) -> Self {
        Self::TxEip4844WithSidecar(tx)
    }
}

impl<T> From<TxEip4844> for TxEip4844Variant<T> {
    fn from(tx: TxEip4844) -> Self {
        Self::TxEip4844(tx)
    }
}

impl From<(TxEip4844, BlobTransactionSidecar)> for TxEip4844Variant<BlobTransactionSidecar> {
    fn from((tx, sidecar): (TxEip4844, BlobTransactionSidecar)) -> Self {
        TxEip4844WithSidecar::from_tx_and_sidecar(tx, sidecar).into()
    }
}

impl<T> From<TxEip4844Variant<T>> for TxEip4844 {
    fn from(tx: TxEip4844Variant<T>) -> Self {
        match tx {
            TxEip4844Variant::TxEip4844(tx) => tx,
            TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx,
        }
    }
}

impl<T> AsRef<TxEip4844> for TxEip4844Variant<T> {
    fn as_ref(&self) -> &TxEip4844 {
        match self {
            Self::TxEip4844(tx) => tx,
            Self::TxEip4844WithSidecar(tx) => &tx.tx,
        }
    }
}

impl<T> AsMut<TxEip4844> for TxEip4844Variant<T> {
    fn as_mut(&mut self) -> &mut TxEip4844 {
        match self {
            Self::TxEip4844(tx) => tx,
            Self::TxEip4844WithSidecar(tx) => &mut tx.tx,
        }
    }
}

impl AsRef<Self> for TxEip4844 {
    fn as_ref(&self) -> &Self {
        self
    }
}

impl AsMut<Self> for TxEip4844 {
    fn as_mut(&mut self) -> &mut Self {
        self
    }
}

impl<T> TxEip4844Variant<T> {
    /// Get the transaction type.
    #[doc(alias = "transaction_type")]
    pub const fn tx_type() -> TxType {
        TxType::Eip4844
    }

    /// Get access to the inner tx [TxEip4844].
    #[doc(alias = "transaction")]
    pub const fn tx(&self) -> &TxEip4844 {
        match self {
            Self::TxEip4844(tx) => tx,
            Self::TxEip4844WithSidecar(tx) => tx.tx(),
        }
    }

    /// Strips the sidecar from this variant type leaving [`Self::TxEip4844`].
    ///
    /// Returns the sidecar if it was [`Self::TxEip4844WithSidecar`].
    pub fn take_sidecar(&mut self) -> Option<T> {
        // Use a placeholder to temporarily replace self
        let placeholder = Self::TxEip4844(TxEip4844::default());
        match core::mem::replace(self, placeholder) {
            tx @ Self::TxEip4844(_) => {
                // Put the original transaction back
                *self = tx;
                None
            }
            Self::TxEip4844WithSidecar(tx) => {
                let (tx, sidecar) = tx.into_parts();
                *self = Self::TxEip4844(tx);
                Some(sidecar)
            }
        }
    }

    /// Strips the sidecar from the variant and returns both the transaction and the sidecar
    /// separately, keeping the same sidecar type parameter.
    ///
    /// This method consumes the variant and returns:
    /// - A [`TxEip4844Variant<T>`] containing only the transaction (always
    ///   [`TxEip4844Variant::TxEip4844`])
    /// - An [`Option<T>`] containing the sidecar if it existed
    ///
    /// This is a convenience wrapper around [`strip_sidecar_into`](Self::strip_sidecar_into)
    /// that keeps the same type parameter.
    ///
    /// # Examples
    ///
    /// ```
    /// # use alloy_consensus::TxEip4844Variant;
    /// # use alloy_eips::eip4844::BlobTransactionSidecar;
    /// # fn example(variant: TxEip4844Variant<BlobTransactionSidecar>) {
    /// // Strip and extract the sidecar (type parameter stays the same)
    /// let (tx_variant, maybe_sidecar) = variant.strip_sidecar();
    ///
    /// if let Some(sidecar) = maybe_sidecar {
    ///     // Process the sidecar separately
    ///     println!("Sidecar has {} blobs", sidecar.blobs.len());
    /// }
    /// # }
    /// ```
    pub fn strip_sidecar(self) -> (Self, Option<T>) {
        self.strip_sidecar_into()
    }

    /// Strips the sidecar from the variant and returns both the transaction and the sidecar
    /// separately, converting to a different sidecar type parameter.
    ///
    /// This method consumes the variant and returns:
    /// - A [`TxEip4844Variant<U>`] containing only the transaction (always
    ///   [`TxEip4844Variant::TxEip4844`])
    /// - An [`Option<T>`] containing the sidecar if it existed
    ///
    /// This is useful when you need to:
    /// - Extract the sidecar for separate processing
    /// - Convert to a variant with a different sidecar type parameter
    /// - Separate the transaction data from blob data
    ///
    /// # Examples
    ///
    /// ```
    /// # use alloy_consensus::TxEip4844Variant;
    /// # use alloy_eips::eip4844::BlobTransactionSidecar;
    /// # use alloy_eips::eip7594::BlobTransactionSidecarVariant;
    /// # fn example(variant: TxEip4844Variant<BlobTransactionSidecar>) {
    /// // Strip and convert to a different type parameter
    /// let (tx_variant, maybe_sidecar): (TxEip4844Variant<BlobTransactionSidecarVariant>, _) =
    ///     variant.strip_sidecar_into();
    ///
    /// if let Some(sidecar) = maybe_sidecar {
    ///     // Process the sidecar separately
    ///     println!("Sidecar has {} blobs", sidecar.blobs.len());
    /// }
    /// # }
    /// ```
    pub fn strip_sidecar_into<U>(self) -> (TxEip4844Variant<U>, Option<T>) {
        match self {
            Self::TxEip4844(tx) => (TxEip4844Variant::TxEip4844(tx), None),
            Self::TxEip4844WithSidecar(tx) => {
                let (tx, sidecar) = tx.into_parts();
                (TxEip4844Variant::TxEip4844(tx), Some(sidecar))
            }
        }
    }

    /// Drops the sidecar from the variant and returns only the transaction, keeping the same
    /// sidecar type parameter.
    ///
    /// This is a convenience method that discards the sidecar, returning only the transaction
    /// without a sidecar (always [`TxEip4844Variant::TxEip4844`]).
    ///
    /// This is equivalent to calling [`strip_sidecar`](Self::strip_sidecar) and taking only the
    /// first element of the tuple.
    ///
    /// # Examples
    ///
    /// ```
    /// # use alloy_consensus::TxEip4844Variant;
    /// # use alloy_eips::eip4844::BlobTransactionSidecar;
    /// # fn example(variant: TxEip4844Variant<BlobTransactionSidecar>) {
    /// // Drop the sidecar, keeping only the transaction
    /// let tx_without_sidecar = variant.drop_sidecar();
    /// # }
    /// ```
    pub fn drop_sidecar(self) -> Self {
        self.strip_sidecar().0
    }

    /// Drops the sidecar from the variant and returns only the transaction, converting to a
    /// different sidecar type parameter.
    ///
    /// This is a convenience method that discards the sidecar, returning only the transaction
    /// without a sidecar (always [`TxEip4844Variant::TxEip4844`]).
    ///
    /// This is equivalent to calling [`strip_sidecar_into`](Self::strip_sidecar_into) and taking
    /// only the first element of the tuple.
    ///
    /// # Examples
    ///
    /// ```
    /// # use alloy_consensus::TxEip4844Variant;
    /// # use alloy_eips::eip4844::BlobTransactionSidecar;
    /// # use alloy_eips::eip7594::BlobTransactionSidecarVariant;
    /// # fn example(variant: TxEip4844Variant<BlobTransactionSidecar>) {
    /// // Drop the sidecar and convert to a different type parameter
    /// let tx_without_sidecar: TxEip4844Variant<BlobTransactionSidecarVariant> =
    ///     variant.drop_sidecar_into();
    /// # }
    /// ```
    pub fn drop_sidecar_into<U>(self) -> TxEip4844Variant<U> {
        self.strip_sidecar_into().0
    }

    /// Returns the [`TxEip4844WithSidecar`] if it has a sidecar
    pub const fn as_with_sidecar(&self) -> Option<&TxEip4844WithSidecar<T>> {
        match self {
            Self::TxEip4844WithSidecar(tx) => Some(tx),
            _ => None,
        }
    }

    /// Tries to unwrap the [`TxEip4844WithSidecar`] returns the transaction as error if it is not a
    /// [`TxEip4844WithSidecar`]
    pub fn try_into_4844_with_sidecar(self) -> Result<TxEip4844WithSidecar<T>, Self> {
        match self {
            Self::TxEip4844WithSidecar(tx) => Ok(tx),
            _ => Err(self),
        }
    }

    /// Returns the sidecar if this is [`TxEip4844Variant::TxEip4844WithSidecar`].
    pub const fn sidecar(&self) -> Option<&T> {
        match self {
            Self::TxEip4844WithSidecar(tx) => Some(tx.sidecar()),
            _ => None,
        }
    }

    /// Maps the sidecar to a new type.
    pub fn map_sidecar<U>(self, f: impl FnOnce(T) -> U) -> TxEip4844Variant<U> {
        match self {
            Self::TxEip4844(tx) => TxEip4844Variant::TxEip4844(tx),
            Self::TxEip4844WithSidecar(tx) => {
                TxEip4844Variant::TxEip4844WithSidecar(tx.map_sidecar(f))
            }
        }
    }

    /// Maps the sidecar to a new type, returning an error if the mapping fails.
    pub fn try_map_sidecar<U, E>(
        self,
        f: impl FnOnce(T) -> Result<U, E>,
    ) -> Result<TxEip4844Variant<U>, E> {
        match self {
            Self::TxEip4844(tx) => Ok(TxEip4844Variant::TxEip4844(tx)),
            Self::TxEip4844WithSidecar(tx) => {
                tx.try_map_sidecar(f).map(TxEip4844Variant::TxEip4844WithSidecar)
            }
        }
    }
}

impl<T: TxEip4844Sidecar> TxEip4844Variant<T> {
    /// Verifies that the transaction's blob data, commitments, and proofs are all valid.
    ///
    /// See also [TxEip4844::validate_blob]
    #[cfg(feature = "kzg")]
    pub fn validate(
        &self,
        proof_settings: &c_kzg::KzgSettings,
    ) -> Result<(), BlobTransactionValidationError> {
        match self {
            Self::TxEip4844(_) => Err(BlobTransactionValidationError::MissingSidecar),
            Self::TxEip4844WithSidecar(tx) => tx.validate_blob(proof_settings),
        }
    }

    /// Calculates a heuristic for the in-memory size of the [TxEip4844Variant] transaction.
    #[inline]
    pub fn size(&self) -> usize {
        match self {
            Self::TxEip4844(tx) => tx.size(),
            Self::TxEip4844WithSidecar(tx) => tx.size(),
        }
    }
}

impl TxEip4844Variant<BlobTransactionSidecar> {
    /// Converts this legacy EIP-4844 sidecar into an EIP-7594 sidecar with the default settings.
    ///
    /// This requires computing cell KZG proofs from the blob data using the KZG trusted setup.
    /// Each blob produces `CELLS_PER_EXT_BLOB` cell proofs.
    #[cfg(feature = "kzg")]
    pub fn try_into_7594(
        self,
    ) -> Result<TxEip4844Variant<alloy_eips::eip7594::BlobTransactionSidecarEip7594>, c_kzg::Error>
    {
        self.try_into_7594_with_settings(
            alloy_eips::eip4844::env_settings::EnvKzgSettings::Default.get(),
        )
    }

    /// Converts this legacy EIP-4844 sidecar into an EIP-7594 sidecar with the given settings.
    ///
    /// This requires computing cell KZG proofs from the blob data using the KZG trusted setup.
    /// Each blob produces `CELLS_PER_EXT_BLOB` cell proofs.
    #[cfg(feature = "kzg")]
    pub fn try_into_7594_with_settings(
        self,
        settings: &c_kzg::KzgSettings,
    ) -> Result<TxEip4844Variant<alloy_eips::eip7594::BlobTransactionSidecarEip7594>, c_kzg::Error>
    {
        self.try_map_sidecar(|sidecar| sidecar.try_into_7594(settings))
    }
}

#[cfg(feature = "kzg")]
impl TxEip4844Variant<alloy_eips::eip7594::BlobTransactionSidecarVariant> {
    /// Attempts to convert this transaction's sidecar into the EIP-7594 format using default KZG
    /// settings.
    ///
    /// For EIP-4844 sidecars, this computes cell KZG proofs from the blob data. If the sidecar is
    /// already in EIP-7594 format, it returns itself unchanged.
    ///
    /// # Returns
    ///
    /// - `Ok(TxEip4844Variant<alloy_eips::eip7594::BlobTransactionSidecarVariant>)` - The
    ///   transaction with converted sidecar
    /// - `Err(c_kzg::Error)` - If KZG proof computation fails
    pub fn try_convert_into_eip7594(self) -> Result<Self, c_kzg::Error> {
        self.try_convert_into_eip7594_with_settings(
            alloy_eips::eip4844::env_settings::EnvKzgSettings::Default.get(),
        )
    }

    /// Attempts to convert this transaction's sidecar into the EIP-7594 format using custom KZG
    /// settings.
    ///
    /// For EIP-4844 sidecars, this computes cell KZG proofs from the blob data using the
    /// provided KZG settings. If the sidecar is already in EIP-7594 format, it returns itself
    /// unchanged.
    ///
    /// # Arguments
    ///
    /// * `settings` - The KZG settings to use for computing cell proofs
    ///
    /// # Returns
    ///
    /// - `Ok(TxEip4844Variant<alloy_eips::eip7594::BlobTransactionSidecarVariant>)` - The
    ///   transaction with converted sidecar
    /// - `Err(c_kzg::Error)` - If KZG proof computation fails
    pub fn try_convert_into_eip7594_with_settings(
        self,
        settings: &c_kzg::KzgSettings,
    ) -> Result<Self, c_kzg::Error> {
        self.try_map_sidecar(|sidecar| sidecar.try_convert_into_eip7594_with_settings(settings))
    }
}

impl<T> Transaction for TxEip4844Variant<T>
where
    T: fmt::Debug + Send + Sync + 'static,
{
    #[inline]
    fn chain_id(&self) -> Option<ChainId> {
        match self {
            Self::TxEip4844(tx) => Some(tx.chain_id),
            Self::TxEip4844WithSidecar(tx) => Some(tx.tx().chain_id),
        }
    }

    #[inline]
    fn nonce(&self) -> u64 {
        match self {
            Self::TxEip4844(tx) => tx.nonce,
            Self::TxEip4844WithSidecar(tx) => tx.tx().nonce,
        }
    }

    #[inline]
    fn gas_limit(&self) -> u64 {
        match self {
            Self::TxEip4844(tx) => tx.gas_limit,
            Self::TxEip4844WithSidecar(tx) => tx.tx().gas_limit,
        }
    }

    #[inline]
    fn gas_price(&self) -> Option<u128> {
        None
    }

    #[inline]
    fn max_fee_per_gas(&self) -> u128 {
        match self {
            Self::TxEip4844(tx) => tx.max_fee_per_gas(),
            Self::TxEip4844WithSidecar(tx) => tx.max_fee_per_gas(),
        }
    }

    #[inline]
    fn max_priority_fee_per_gas(&self) -> Option<u128> {
        match self {
            Self::TxEip4844(tx) => tx.max_priority_fee_per_gas(),
            Self::TxEip4844WithSidecar(tx) => tx.max_priority_fee_per_gas(),
        }
    }

    #[inline]
    fn max_fee_per_blob_gas(&self) -> Option<u128> {
        match self {
            Self::TxEip4844(tx) => tx.max_fee_per_blob_gas(),
            Self::TxEip4844WithSidecar(tx) => tx.max_fee_per_blob_gas(),
        }
    }

    #[inline]
    fn priority_fee_or_price(&self) -> u128 {
        match self {
            Self::TxEip4844(tx) => tx.priority_fee_or_price(),
            Self::TxEip4844WithSidecar(tx) => tx.priority_fee_or_price(),
        }
    }

    fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
        match self {
            Self::TxEip4844(tx) => tx.effective_gas_price(base_fee),
            Self::TxEip4844WithSidecar(tx) => tx.effective_gas_price(base_fee),
        }
    }

    #[inline]
    fn is_dynamic_fee(&self) -> bool {
        match self {
            Self::TxEip4844(tx) => tx.is_dynamic_fee(),
            Self::TxEip4844WithSidecar(tx) => tx.is_dynamic_fee(),
        }
    }

    #[inline]
    fn kind(&self) -> TxKind {
        match self {
            Self::TxEip4844(tx) => tx.to,
            Self::TxEip4844WithSidecar(tx) => tx.tx.to,
        }
        .into()
    }

    #[inline]
    fn is_create(&self) -> bool {
        false
    }

    #[inline]
    fn value(&self) -> U256 {
        match self {
            Self::TxEip4844(tx) => tx.value,
            Self::TxEip4844WithSidecar(tx) => tx.tx.value,
        }
    }

    #[inline]
    fn input(&self) -> &Bytes {
        match self {
            Self::TxEip4844(tx) => tx.input(),
            Self::TxEip4844WithSidecar(tx) => tx.tx().input(),
        }
    }

    #[inline]
    fn access_list(&self) -> Option<&AccessList> {
        match self {
            Self::TxEip4844(tx) => tx.access_list(),
            Self::TxEip4844WithSidecar(tx) => tx.access_list(),
        }
    }

    #[inline]
    fn blob_versioned_hashes(&self) -> Option<&[B256]> {
        match self {
            Self::TxEip4844(tx) => tx.blob_versioned_hashes(),
            Self::TxEip4844WithSidecar(tx) => tx.blob_versioned_hashes(),
        }
    }

    #[inline]
    fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
        None
    }
}
impl Typed2718 for TxEip4844 {
    fn ty(&self) -> u8 {
        TxType::Eip4844 as u8
    }
}

impl<T: Encodable7594> RlpEcdsaEncodableTx for TxEip4844Variant<T> {
    fn rlp_encoded_fields_length(&self) -> usize {
        match self {
            Self::TxEip4844(inner) => inner.rlp_encoded_fields_length(),
            Self::TxEip4844WithSidecar(inner) => inner.rlp_encoded_fields_length(),
        }
    }

    fn rlp_encode_fields(&self, out: &mut dyn alloy_rlp::BufMut) {
        match self {
            Self::TxEip4844(inner) => inner.rlp_encode_fields(out),
            Self::TxEip4844WithSidecar(inner) => inner.rlp_encode_fields(out),
        }
    }

    fn rlp_header_signed(&self, signature: &Signature) -> Header {
        match self {
            Self::TxEip4844(inner) => inner.rlp_header_signed(signature),
            Self::TxEip4844WithSidecar(inner) => inner.rlp_header_signed(signature),
        }
    }

    fn rlp_encode_signed(&self, signature: &Signature, out: &mut dyn BufMut) {
        match self {
            Self::TxEip4844(inner) => inner.rlp_encode_signed(signature, out),
            Self::TxEip4844WithSidecar(inner) => inner.rlp_encode_signed(signature, out),
        }
    }

    fn tx_hash_with_type(&self, signature: &Signature, ty: u8) -> alloy_primitives::TxHash {
        match self {
            Self::TxEip4844(inner) => inner.tx_hash_with_type(signature, ty),
            Self::TxEip4844WithSidecar(inner) => inner.tx_hash_with_type(signature, ty),
        }
    }
}

impl<T: Encodable7594 + Decodable7594> RlpEcdsaDecodableTx for TxEip4844Variant<T> {
    const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

    fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
        let needle = &mut &**buf;

        // We also need to do a trial decoding of WithSidecar to see if it
        // works. The trial ref is consumed to look for a WithSidecar.
        let trial = &mut &**buf;

        // If the next bytes are a header, one of 3 things is true:
        // - If the header is a list, this is a WithSidecar tx
        // - If there is no header, this is a non-sidecar tx with a single-byte chain ID.
        // - If there is a string header, this is a non-sidecar tx with a multi-byte chain ID.
        // To check these, we first try to decode the header. If it fails or is
        // not a list, we lmow that it is a non-sidecar transaction.
        if Header::decode(needle).is_ok_and(|h| h.list) {
            if let Ok(tx) = TxEip4844WithSidecar::rlp_decode_fields(trial) {
                *buf = *trial;
                return Ok(tx.into());
            }
        }
        TxEip4844::rlp_decode_fields(buf).map(Into::into)
    }

    fn rlp_decode_with_signature(buf: &mut &[u8]) -> alloy_rlp::Result<(Self, Signature)> {
        // We need to determine if this has a sidecar tx or not. The needle ref
        // is consumed to look for headers.
        let needle = &mut &**buf;

        // We also need to do a trial decoding of WithSidecar to see if it
        // works. The original ref is consumed to look for a WithSidecar.
        let trial = &mut &**buf;

        // First we decode the outer header
        Header::decode(needle)?;

        // If the next bytes are a header, one of 3 things is true:
        // - If the header is a list, this is a WithSidecar tx
        // - If there is no header, this is a non-sidecar tx with a single-byte chain ID.
        // - If there is a string header, this is a non-sidecar tx with a multi-byte chain ID.
        // To check these, we first try to decode the header. If it fails or is
        // not a list, we lmow that it is a non-sidecar transaction.
        if Header::decode(needle).is_ok_and(|h| h.list) {
            if let Ok((tx, signature)) = TxEip4844WithSidecar::rlp_decode_with_signature(trial) {
                // If successful, we need to consume the trial buffer up to
                // the same point.
                *buf = *trial;
                return Ok((tx.into(), signature));
            }
        }
        TxEip4844::rlp_decode_with_signature(buf).map(|(tx, signature)| (tx.into(), signature))
    }
}

impl<T> Typed2718 for TxEip4844Variant<T> {
    fn ty(&self) -> u8 {
        TxType::Eip4844 as u8
    }
}

impl IsTyped2718 for TxEip4844 {
    fn is_type(type_id: u8) -> bool {
        matches!(type_id, 0x03)
    }
}

impl<T> SignableTransaction<Signature> for TxEip4844Variant<T>
where
    T: fmt::Debug + Send + Sync + 'static,
{
    fn set_chain_id(&mut self, chain_id: ChainId) {
        match self {
            Self::TxEip4844(inner) => {
                inner.set_chain_id(chain_id);
            }
            Self::TxEip4844WithSidecar(inner) => {
                inner.set_chain_id(chain_id);
            }
        }
    }

    fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) {
        // A signature for a [TxEip4844WithSidecar] is a signature over the [TxEip4844Variant]
        // EIP-2718 payload fields:
        // (BLOB_TX_TYPE ||
        //   rlp([chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to, value,
        //     data, access_list, max_fee_per_blob_gas, blob_versioned_hashes]))
        self.tx().encode_for_signing(out);
    }

    fn payload_len_for_signature(&self) -> usize {
        self.tx().payload_len_for_signature()
    }
}

/// [EIP-4844 Blob Transaction](https://eips.ethereum.org/EIPS/eip-4844#blob-transaction)
///
/// A transaction with blob hashes and max blob fee. It does not have the Blob sidecar.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[doc(alias = "Eip4844Transaction", alias = "TransactionEip4844", alias = "Eip4844Tx")]
pub struct TxEip4844 {
    /// Added as EIP-pub 155: Simple replay attack protection
    #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
    pub chain_id: ChainId,
    /// A scalar value equal to the number of transactions sent by the sender; formally Tn.
    #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
    pub nonce: u64,
    /// A scalar value equal to the maximum
    /// amount of gas that should be used in executing
    /// this transaction. This is paid up-front, before any
    /// computation is done and may not be increased
    /// later; formally Tg.
    #[cfg_attr(
        feature = "serde",
        serde(with = "alloy_serde::quantity", rename = "gas", alias = "gasLimit")
    )]
    pub gas_limit: u64,
    /// A scalar value equal to the maximum total fee per unit of gas
    /// the sender is willing to pay. The actual fee paid per gas is
    /// the minimum of this and `base_fee + max_priority_fee_per_gas`.
    ///
    /// As ethereum circulation is around 120mil eth as of 2022 that is around
    /// 120000000000000000000000000 wei we are safe to use u128 as its max number is:
    /// 340282366920938463463374607431768211455
    ///
    /// This is also known as `GasFeeCap`
    #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
    pub max_fee_per_gas: u128,
    /// Max Priority fee that transaction is paying
    ///
    /// As ethereum circulation is around 120mil eth as of 2022 that is around
    /// 120000000000000000000000000 wei we are safe to use u128 as its max number is:
    /// 340282366920938463463374607431768211455
    ///
    /// This is also known as `GasTipCap`
    #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
    pub max_priority_fee_per_gas: u128,
    /// The 160-bit address of the message call’s recipient.
    pub to: Address,
    /// A scalar value equal to the number of Wei to
    /// be transferred to the message call’s recipient or,
    /// in the case of contract creation, as an endowment
    /// to the newly created account; formally Tv.
    pub value: U256,
    /// The accessList specifies a list of addresses and storage keys;
    /// these addresses and storage keys are added into the `accessed_addresses`
    /// and `accessed_storage_keys` global sets (introduced in EIP-2929).
    /// A gas cost is charged, though at a discount relative to the cost of
    /// accessing outside the list.
    pub access_list: AccessList,

    /// It contains a vector of fixed size hash(32 bytes)
    pub blob_versioned_hashes: Vec<B256>,

    /// Max fee per data gas
    ///
    /// aka BlobFeeCap or blobGasFeeCap
    #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
    pub max_fee_per_blob_gas: u128,

    /// Input has two uses depending if transaction is Create or Call (if `to` field is None or
    /// Some). pub init: An unlimited size byte array specifying the
    /// EVM-code for the account initialisation procedure CREATE,
    /// data: An unlimited size byte array specifying the
    /// input data of the message call, formally Td.
    pub input: Bytes,
}

impl TxEip4844 {
    /// Returns the total gas for all blobs in this transaction.
    #[inline]
    pub const fn blob_gas(&self) -> u64 {
        // SAFETY: we don't expect u64::MAX / DATA_GAS_PER_BLOB hashes in a single transaction
        self.blob_versioned_hashes.len() as u64 * DATA_GAS_PER_BLOB
    }

    /// Verifies that the given blob data, commitments, and proofs are all valid for this
    /// transaction.
    ///
    /// Takes as input the [KzgSettings](c_kzg::KzgSettings), which should contain the parameters
    /// derived from the KZG trusted setup.
    ///
    /// This ensures that the blob transaction payload has the same number of blob data elements,
    /// commitments, and proofs. Each blob data element is verified against its commitment and
    /// proof.
    ///
    /// Returns [BlobTransactionValidationError::InvalidProof] if any blob KZG proof in the response
    /// fails to verify, or if the versioned hashes in the transaction do not match the actual
    /// commitment versioned hashes.
    #[cfg(feature = "kzg")]
    pub fn validate_blob<T: TxEip4844Sidecar>(
        &self,
        sidecar: &T,
        proof_settings: &c_kzg::KzgSettings,
    ) -> Result<(), BlobTransactionValidationError> {
        sidecar.validate(&self.blob_versioned_hashes, proof_settings)
    }

    /// Get transaction type.
    #[doc(alias = "transaction_type")]
    pub const fn tx_type() -> TxType {
        TxType::Eip4844
    }

    /// Attaches the blob sidecar to the transaction
    pub const fn with_sidecar<T>(self, sidecar: T) -> TxEip4844WithSidecar<T> {
        TxEip4844WithSidecar::from_tx_and_sidecar(self, sidecar)
    }

    /// Calculates a heuristic for the in-memory size of the [TxEip4844Variant] transaction.
    #[inline]
    pub fn size(&self) -> usize {
        size_of::<Self>()
            + self.access_list.size()
            + self.input.len()
            + self.blob_versioned_hashes.capacity() * size_of::<B256>()
    }
}

impl RlpEcdsaEncodableTx for TxEip4844 {
    fn rlp_encoded_fields_length(&self) -> usize {
        self.chain_id.length()
            + self.nonce.length()
            + self.gas_limit.length()
            + self.max_fee_per_gas.length()
            + self.max_priority_fee_per_gas.length()
            + self.to.length()
            + self.value.length()
            + self.access_list.length()
            + self.blob_versioned_hashes.length()
            + self.max_fee_per_blob_gas.length()
            + self.input.0.length()
    }

    fn rlp_encode_fields(&self, out: &mut dyn alloy_rlp::BufMut) {
        self.chain_id.encode(out);
        self.nonce.encode(out);
        self.max_priority_fee_per_gas.encode(out);
        self.max_fee_per_gas.encode(out);
        self.gas_limit.encode(out);
        self.to.encode(out);
        self.value.encode(out);
        self.input.0.encode(out);
        self.access_list.encode(out);
        self.max_fee_per_blob_gas.encode(out);
        self.blob_versioned_hashes.encode(out);
    }
}

impl RlpEcdsaDecodableTx for TxEip4844 {
    const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

    fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
        Ok(Self {
            chain_id: Decodable::decode(buf)?,
            nonce: Decodable::decode(buf)?,
            max_priority_fee_per_gas: Decodable::decode(buf)?,
            max_fee_per_gas: Decodable::decode(buf)?,
            gas_limit: Decodable::decode(buf)?,
            to: Decodable::decode(buf)?,
            value: Decodable::decode(buf)?,
            input: Decodable::decode(buf)?,
            access_list: Decodable::decode(buf)?,
            max_fee_per_blob_gas: Decodable::decode(buf)?,
            blob_versioned_hashes: Decodable::decode(buf)?,
        })
    }
}

impl SignableTransaction<Signature> for TxEip4844 {
    fn set_chain_id(&mut self, chain_id: ChainId) {
        self.chain_id = chain_id;
    }

    fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) {
        out.put_u8(Self::tx_type() as u8);
        self.encode(out);
    }

    fn payload_len_for_signature(&self) -> usize {
        self.length() + 1
    }
}

impl Transaction for TxEip4844 {
    #[inline]
    fn chain_id(&self) -> Option<ChainId> {
        Some(self.chain_id)
    }

    #[inline]
    fn nonce(&self) -> u64 {
        self.nonce
    }

    #[inline]
    fn gas_limit(&self) -> u64 {
        self.gas_limit
    }

    #[inline]
    fn gas_price(&self) -> Option<u128> {
        None
    }

    #[inline]
    fn max_fee_per_gas(&self) -> u128 {
        self.max_fee_per_gas
    }

    #[inline]
    fn max_priority_fee_per_gas(&self) -> Option<u128> {
        Some(self.max_priority_fee_per_gas)
    }

    #[inline]
    fn max_fee_per_blob_gas(&self) -> Option<u128> {
        Some(self.max_fee_per_blob_gas)
    }

    #[inline]
    fn priority_fee_or_price(&self) -> u128 {
        self.max_priority_fee_per_gas
    }

    fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
        alloy_eips::eip1559::calc_effective_gas_price(
            self.max_fee_per_gas,
            self.max_priority_fee_per_gas,
            base_fee,
        )
    }

    #[inline]
    fn is_dynamic_fee(&self) -> bool {
        true
    }

    #[inline]
    fn kind(&self) -> TxKind {
        self.to.into()
    }

    #[inline]
    fn is_create(&self) -> bool {
        false
    }

    #[inline]
    fn value(&self) -> U256 {
        self.value
    }

    #[inline]
    fn input(&self) -> &Bytes {
        &self.input
    }

    #[inline]
    fn access_list(&self) -> Option<&AccessList> {
        Some(&self.access_list)
    }

    #[inline]
    fn blob_versioned_hashes(&self) -> Option<&[B256]> {
        Some(&self.blob_versioned_hashes)
    }

    #[inline]
    fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
        None
    }
}

impl Encodable for TxEip4844 {
    fn encode(&self, out: &mut dyn BufMut) {
        self.rlp_encode(out);
    }

    fn length(&self) -> usize {
        self.rlp_encoded_length()
    }
}

impl Decodable for TxEip4844 {
    fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
        Self::rlp_decode(buf)
    }
}

impl<T> From<TxEip4844WithSidecar<T>> for TxEip4844 {
    /// Consumes the [TxEip4844WithSidecar] and returns the inner [TxEip4844].
    fn from(tx_with_sidecar: TxEip4844WithSidecar<T>) -> Self {
        tx_with_sidecar.tx
    }
}

/// [EIP-4844 Blob Transaction](https://eips.ethereum.org/EIPS/eip-4844#blob-transaction)
///
/// A transaction with blob hashes and max blob fee, which also includes the
/// [BlobTransactionSidecar]. This is the full type sent over the network as a raw transaction. It
/// wraps a [TxEip4844] to include the sidecar and the ability to decode it properly.
///
/// This is defined in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#networking) as an element
/// of a `PooledTransactions` response, and is also used as the format for sending raw transactions
/// through the network (eth_sendRawTransaction/eth_sendTransaction).
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[doc(alias = "Eip4844TransactionWithSidecar", alias = "Eip4844TxWithSidecar")]
pub struct TxEip4844WithSidecar<T = BlobTransactionSidecarVariant> {
    /// The actual transaction.
    #[cfg_attr(feature = "serde", serde(flatten))]
    #[doc(alias = "transaction")]
    pub tx: TxEip4844,
    /// The sidecar.
    #[cfg_attr(feature = "serde", serde(flatten))]
    pub sidecar: T,
}

impl<T> TxEip4844WithSidecar<T> {
    /// Constructs a new [TxEip4844WithSidecar] from a [TxEip4844] and a sidecar.
    #[doc(alias = "from_transaction_and_sidecar")]
    pub const fn from_tx_and_sidecar(tx: TxEip4844, sidecar: T) -> Self {
        Self { tx, sidecar }
    }

    /// Get the transaction type.
    #[doc(alias = "transaction_type")]
    pub const fn tx_type() -> TxType {
        TxEip4844::tx_type()
    }

    /// Get access to the inner tx [TxEip4844].
    #[doc(alias = "transaction")]
    pub const fn tx(&self) -> &TxEip4844 {
        &self.tx
    }

    /// Get access to the inner sidecar.
    pub const fn sidecar(&self) -> &T {
        &self.sidecar
    }

    /// Consumes the [TxEip4844WithSidecar] and returns the inner sidecar.
    pub fn into_sidecar(self) -> T {
        self.sidecar
    }

    /// Consumes the [TxEip4844WithSidecar] and returns the inner [TxEip4844] and a sidecar.
    pub fn into_parts(self) -> (TxEip4844, T) {
        (self.tx, self.sidecar)
    }

    /// Maps the sidecar to a new type.
    pub fn map_sidecar<U>(self, f: impl FnOnce(T) -> U) -> TxEip4844WithSidecar<U> {
        TxEip4844WithSidecar { tx: self.tx, sidecar: f(self.sidecar) }
    }

    /// Maps the sidecar to a new type, returning an error if the mapping fails.
    pub fn try_map_sidecar<U, E>(
        self,
        f: impl FnOnce(T) -> Result<U, E>,
    ) -> Result<TxEip4844WithSidecar<U>, E> {
        Ok(TxEip4844WithSidecar { tx: self.tx, sidecar: f(self.sidecar)? })
    }
}

impl TxEip4844WithSidecar<BlobTransactionSidecar> {
    /// Converts this legacy EIP-4844 sidecar into an EIP-7594 sidecar with the default settings.
    ///
    /// This requires computing cell KZG proofs from the blob data using the KZG trusted setup.
    /// Each blob produces `CELLS_PER_EXT_BLOB` cell proofs.
    #[cfg(feature = "kzg")]
    pub fn try_into_7594(
        self,
    ) -> Result<
        TxEip4844WithSidecar<alloy_eips::eip7594::BlobTransactionSidecarEip7594>,
        c_kzg::Error,
    > {
        self.try_into_7594_with_settings(
            alloy_eips::eip4844::env_settings::EnvKzgSettings::Default.get(),
        )
    }

    /// Converts this legacy EIP-4844 sidecar into an EIP-7594 sidecar with the given settings.
    ///
    /// This requires computing cell KZG proofs from the blob data using the KZG trusted setup.
    /// Each blob produces `CELLS_PER_EXT_BLOB` cell proofs.
    #[cfg(feature = "kzg")]
    pub fn try_into_7594_with_settings(
        self,
        settings: &c_kzg::KzgSettings,
    ) -> Result<
        TxEip4844WithSidecar<alloy_eips::eip7594::BlobTransactionSidecarEip7594>,
        c_kzg::Error,
    > {
        self.try_map_sidecar(|sidecar| sidecar.try_into_7594(settings))
    }
}

impl<T: TxEip4844Sidecar> TxEip4844WithSidecar<T> {
    /// Verifies that the transaction's blob data, commitments, and proofs are all valid.
    ///
    /// See also [TxEip4844::validate_blob]
    #[cfg(feature = "kzg")]
    pub fn validate_blob(
        &self,
        proof_settings: &c_kzg::KzgSettings,
    ) -> Result<(), BlobTransactionValidationError> {
        self.tx.validate_blob(&self.sidecar, proof_settings)
    }

    /// Calculates a heuristic for the in-memory size of the [TxEip4844WithSidecar] transaction.
    #[inline]
    pub fn size(&self) -> usize {
        self.tx.size() + self.sidecar.size()
    }
}

impl<T> SignableTransaction<Signature> for TxEip4844WithSidecar<T>
where
    T: fmt::Debug + Send + Sync + 'static,
{
    fn set_chain_id(&mut self, chain_id: ChainId) {
        self.tx.chain_id = chain_id;
    }

    fn encode_for_signing(&self, out: &mut dyn alloy_rlp::BufMut) {
        // A signature for a [TxEip4844WithSidecar] is a signature over the [TxEip4844] EIP-2718
        // payload fields:
        // (BLOB_TX_TYPE ||
        //   rlp([chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to, value,
        //     data, access_list, max_fee_per_blob_gas, blob_versioned_hashes]))
        self.tx.encode_for_signing(out);
    }

    fn payload_len_for_signature(&self) -> usize {
        // The payload length is the length of the `transaction_payload_body` list.
        // The sidecar is NOT included.
        self.tx.payload_len_for_signature()
    }
}

impl<T> Transaction for TxEip4844WithSidecar<T>
where
    T: fmt::Debug + Send + Sync + 'static,
{
    #[inline]
    fn chain_id(&self) -> Option<ChainId> {
        self.tx.chain_id()
    }

    #[inline]
    fn nonce(&self) -> u64 {
        self.tx.nonce()
    }

    #[inline]
    fn gas_limit(&self) -> u64 {
        self.tx.gas_limit()
    }

    #[inline]
    fn gas_price(&self) -> Option<u128> {
        self.tx.gas_price()
    }

    #[inline]
    fn max_fee_per_gas(&self) -> u128 {
        self.tx.max_fee_per_gas()
    }

    #[inline]
    fn max_priority_fee_per_gas(&self) -> Option<u128> {
        self.tx.max_priority_fee_per_gas()
    }

    #[inline]
    fn max_fee_per_blob_gas(&self) -> Option<u128> {
        self.tx.max_fee_per_blob_gas()
    }

    #[inline]
    fn priority_fee_or_price(&self) -> u128 {
        self.tx.priority_fee_or_price()
    }

    fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
        self.tx.effective_gas_price(base_fee)
    }

    #[inline]
    fn is_dynamic_fee(&self) -> bool {
        self.tx.is_dynamic_fee()
    }

    #[inline]
    fn kind(&self) -> TxKind {
        self.tx.kind()
    }

    #[inline]
    fn is_create(&self) -> bool {
        false
    }

    #[inline]
    fn value(&self) -> U256 {
        self.tx.value()
    }

    #[inline]
    fn input(&self) -> &Bytes {
        self.tx.input()
    }

    #[inline]
    fn access_list(&self) -> Option<&AccessList> {
        Some(&self.tx.access_list)
    }

    #[inline]
    fn blob_versioned_hashes(&self) -> Option<&[B256]> {
        self.tx.blob_versioned_hashes()
    }

    #[inline]
    fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
        None
    }
}

impl<T> Typed2718 for TxEip4844WithSidecar<T> {
    fn ty(&self) -> u8 {
        TxType::Eip4844 as u8
    }
}

impl<T: Encodable7594> RlpEcdsaEncodableTx for TxEip4844WithSidecar<T> {
    fn rlp_encoded_fields_length(&self) -> usize {
        self.sidecar.encode_7594_len() + self.tx.rlp_encoded_length()
    }

    fn rlp_encode_fields(&self, out: &mut dyn alloy_rlp::BufMut) {
        self.tx.rlp_encode(out);
        self.sidecar.encode_7594(out);
    }

    fn rlp_header_signed(&self, signature: &Signature) -> Header {
        let payload_length =
            self.tx.rlp_encoded_length_with_signature(signature) + self.sidecar.encode_7594_len();
        Header { list: true, payload_length }
    }

    fn rlp_encode_signed(&self, signature: &Signature, out: &mut dyn BufMut) {
        self.rlp_header_signed(signature).encode(out);
        self.tx.rlp_encode_signed(signature, out);
        self.sidecar.encode_7594(out);
    }

    fn tx_hash_with_type(&self, signature: &Signature, ty: u8) -> alloy_primitives::TxHash {
        // eip4844 tx_hash is always based on the non-sidecar encoding
        self.tx.tx_hash_with_type(signature, ty)
    }
}

impl<T: Encodable7594 + Decodable7594> RlpEcdsaDecodableTx for TxEip4844WithSidecar<T> {
    const DEFAULT_TX_TYPE: u8 = { Self::tx_type() as u8 };

    fn rlp_decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
        let tx = TxEip4844::rlp_decode(buf)?;
        let sidecar = T::decode_7594(buf)?;
        Ok(Self { tx, sidecar })
    }

    fn rlp_decode_with_signature(buf: &mut &[u8]) -> alloy_rlp::Result<(Self, Signature)> {
        let header = Header::decode(buf)?;
        if !header.list {
            return Err(alloy_rlp::Error::UnexpectedString);
        }
        let remaining = buf.len();

        let (tx, signature) = TxEip4844::rlp_decode_with_signature(buf)?;
        let sidecar = T::decode_7594(buf)?;

        if buf.len() + header.payload_length != remaining {
            return Err(alloy_rlp::Error::UnexpectedLength);
        }

        Ok((Self { tx, sidecar }, signature))
    }
}

#[cfg(test)]
mod tests {
    use super::{BlobTransactionSidecar, TxEip4844, TxEip4844WithSidecar};
    use crate::{
        transaction::{eip4844::TxEip4844Variant, RlpEcdsaDecodableTx},
        SignableTransaction, TxEnvelope,
    };
    use alloy_eips::{
        eip2930::AccessList, eip4844::env_settings::EnvKzgSettings,
        eip7594::BlobTransactionSidecarVariant, Encodable2718 as _,
    };
    use alloy_primitives::{address, b256, bytes, hex, Signature, U256};
    use alloy_rlp::{Decodable, Encodable};
    use assert_matches::assert_matches;
    use std::path::PathBuf;

    #[test]
    fn different_sidecar_same_hash() {
        // this should make sure that the hash calculated for the `into_signed` conversion does not
        // change if the sidecar is different
        let tx = TxEip4844 {
            chain_id: 1,
            nonce: 1,
            max_priority_fee_per_gas: 1,
            max_fee_per_gas: 1,
            gas_limit: 1,
            to: Default::default(),
            value: U256::from(1),
            access_list: Default::default(),
            blob_versioned_hashes: vec![Default::default()],
            max_fee_per_blob_gas: 1,
            input: Default::default(),
        };
        let sidecar = BlobTransactionSidecar {
            blobs: vec![[2; 131072].into()],
            commitments: vec![[3; 48].into()],
            proofs: vec![[4; 48].into()],
        };
        let mut tx = TxEip4844WithSidecar { tx, sidecar };
        let signature = Signature::test_signature();

        // turn this transaction into_signed
        let expected_signed = tx.clone().into_signed(signature);

        // change the sidecar, adding a single (blob, commitment, proof) pair
        tx.sidecar = BlobTransactionSidecar {
            blobs: vec![[1; 131072].into()],
            commitments: vec![[1; 48].into()],
            proofs: vec![[1; 48].into()],
        };

        // turn this transaction into_signed
        let actual_signed = tx.into_signed(signature);

        // the hashes should be the same
        assert_eq!(expected_signed.hash(), actual_signed.hash());

        // convert to envelopes
        let expected_envelope: TxEnvelope = expected_signed.into();
        let actual_envelope: TxEnvelope = actual_signed.into();

        // now encode the transaction and check the length
        let len = expected_envelope.length();
        let mut buf = Vec::with_capacity(len);
        expected_envelope.encode(&mut buf);
        assert_eq!(buf.len(), len);

        // ensure it's also the same size that `actual` claims to be, since we just changed the
        // sidecar values.
        assert_eq!(buf.len(), actual_envelope.length());

        // now decode the transaction and check the values
        let decoded = TxEnvelope::decode(&mut &buf[..]).unwrap();
        assert_eq!(decoded, expected_envelope);
    }

    #[test]
    fn test_4844_variant_into_signed_correct_hash() {
        // Taken from <https://etherscan.io/tx/0x93fc9daaa0726c3292a2e939df60f7e773c6a6a726a61ce43f4a217c64d85e87>
        let tx =
            TxEip4844 {
                chain_id: 1,
                nonce: 15435,
                gas_limit: 8000000,
                max_fee_per_gas: 10571233596,
                max_priority_fee_per_gas: 1000000000,
                to: address!("a8cb082a5a689e0d594d7da1e2d72a3d63adc1bd"),
                value: U256::ZERO,
                access_list: AccessList::default(),
                blob_versioned_hashes: vec![
                    b256!("01e5276d91ac1ddb3b1c2d61295211220036e9a04be24c00f76916cc2659d004"),
                    b256!("0128eb58aff09fd3a7957cd80aa86186d5849569997cdfcfa23772811b706cc2"),
                ],
                max_fee_per_blob_gas: 1,
                input: bytes!("701f58c50000000000000000000000000000000000000000000000000000000000073fb1ed12e288def5b439ea074b398dbb4c967f2852baac3238c5fe4b62b871a59a6d00000000000000000000000000000000000000000000000000000000123971da000000000000000000000000000000000000000000000000000000000000000ac39b2a24e1dbdd11a1e7bd7c0f4dfd7d9b9cfa0997d033ad05f961ba3b82c6c83312c967f10daf5ed2bffe309249416e03ee0b101f2b84d2102b9e38b0e4dfdf0000000000000000000000000000000000000000000000000000000066254c8b538dcc33ecf5334bbd294469f9d4fd084a3090693599a46d6c62567747cbc8660000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000073fb20000000000000000000000000000000000000000000000000000000066254da10000000000000000000000000000000000000000000000000000000012397d5e20b09b263779fda4171c341e720af8fa469621ff548651f8dbbc06c2d320400c000000000000000000000000000000000000000000000000000000000000000b50a833bb11af92814e99c6ff7cf7ba7042827549d6f306a04270753702d897d8fc3c411b99159939ac1c16d21d3057ddc8b2333d1331ab34c938cff0eb29ce2e43241c170344db6819f76b1f1e0ab8206f3ec34120312d275c4f5bbea7f5c55700000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000480000000000000000000000000000000000000000000000000000000000000031800000000000000000000000000000000000000000000800b0000000000000000000000000000000000000000000000000000000000000004ed12e288def5b439ea074b398dbb4c967f2852baac3238c5fe4b62b871a59a6d00000ca8000000000000000000000000000000000000800b000000000000000000000000000000000000000000000000000000000000000300000000000000000000000066254da100000000000000000000000066254e9d00010ca80000000000000000000000000000000000008001000000000000000000000000000000000000000000000000000000000000000550a833bb11af92814e99c6ff7cf7ba7042827549d6f306a04270753702d897d800010ca800000000000000000000000000000000000080010000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000b00010ca8000000000000000000000000000000000000801100000000000000000000000000000000000000000000000000000000000000075c1cd5bd0fd333ce9d7c8edfc79f43b8f345b4a394f6aba12a2cc78ce4012ed700010ca80000000000000000000000000000000000008011000000000000000000000000000000000000000000000000000000000000000845392775318aa47beaafbdc827da38c9f1e88c3bdcabba2cb493062e17cbf21e00010ca800000000000000000000000000000000000080080000000000000000000000000000000000000000000000000000000000000000c094e20e7ac9b433f44a5885e3bdc07e51b309aeb993caa24ba84a661ac010c100010ca800000000000000000000000000000000000080080000000000000000000000000000000000000000000000000000000000000001ab42db8f4ed810bdb143368a2b641edf242af6e3d0de8b1486e2b0e7880d431100010ca8000000000000000000000000000000000000800800000000000000000000000000000000000000000000000000000000000000022d94e4cc4525e4e2d81e8227b6172e97076431a2cf98792d978035edd6e6f3100000000000000000000000000000000000000000000000000000000000000000000000000000012101c74dfb80a80fccb9a4022b2406f79f56305e6a7c931d30140f5d372fe793837e93f9ec6b8d89a9d0ab222eeb27547f66b90ec40fbbdd2a4936b0b0c19ca684ff78888fbf5840d7c8dc3c493b139471750938d7d2c443e2d283e6c5ee9fde3765a756542c42f002af45c362b4b5b1687a8fc24cbf16532b903f7bb289728170dcf597f5255508c623ba247735538376f494cdcdd5bd0c4cb067526eeda0f4745a28d8baf8893ecc1b8cee80690538d66455294a028da03ff2add9d8a88e6ee03ba9ffe3ad7d91d6ac9c69a1f28c468f00fe55eba5651a2b32dc2458e0d14b4dd6d0173df255cd56aa01e8e38edec17ea8933f68543cbdc713279d195551d4211bed5c91f77259a695e6768f6c4b110b2158fcc42423a96dcc4e7f6fddb3e2369d00000000000000000000000000000000000000000000000000000000000000") };
        let variant = TxEip4844Variant::<BlobTransactionSidecar>::TxEip4844(tx);

        let signature = Signature::new(
            b256!("6c173c3c8db3e3299f2f728d293b912c12e75243e3aa66911c2329b58434e2a4").into(),
            b256!("7dd4d1c228cedc5a414a668ab165d9e888e61e4c3b44cd7daf9cdcc4cec5d6b2").into(),
            false,
        );

        let signed = variant.into_signed(signature);
        assert_eq!(
            *signed.hash(),
            b256!("93fc9daaa0726c3292a2e939df60f7e773c6a6a726a61ce43f4a217c64d85e87")
        );
    }

    #[test]
    fn decode_raw_7594_rlp() {
        let kzg_settings = EnvKzgSettings::default();
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/7594rlp");
        let dir = std::fs::read_dir(path).expect("Unable to read folder");
        for entry in dir {
            let entry = entry.unwrap();
            let content = std::fs::read_to_string(entry.path()).unwrap();
            let raw = hex::decode(content.trim()).unwrap();
            let tx = TxEip4844WithSidecar::<BlobTransactionSidecarVariant>::eip2718_decode(
                &mut raw.as_ref(),
            )
            .map_err(|err| {
                panic!("Failed to decode transaction: {:?} {:?}", err, entry.path());
            })
            .unwrap();

            // Test roundtrip
            let encoded = tx.encoded_2718();
            assert_eq!(encoded.as_slice(), &raw[..], "{:?}", entry.path());

            let TxEip4844WithSidecar { tx, sidecar } = tx.tx();
            assert_matches!(sidecar, BlobTransactionSidecarVariant::Eip7594(_));

            let result = sidecar.validate(&tx.blob_versioned_hashes, kzg_settings.get());
            assert_matches!(result, Ok(()));
        }
    }

    #[test]
    fn decode_raw_7594_rlp_invalid() {
        let kzg_settings = EnvKzgSettings::default();
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/7594rlp-invalid");
        let dir = std::fs::read_dir(path).expect("Unable to read folder");
        for entry in dir {
            let entry = entry.unwrap();

            if entry.path().file_name().and_then(|f| f.to_str()) == Some("0.rlp") {
                continue;
            }

            let content = std::fs::read_to_string(entry.path()).unwrap();
            let raw = hex::decode(content.trim()).unwrap();
            let tx = TxEip4844WithSidecar::<BlobTransactionSidecarVariant>::eip2718_decode(
                &mut raw.as_ref(),
            )
            .map_err(|err| {
                panic!("Failed to decode transaction: {:?} {:?}", err, entry.path());
            })
            .unwrap();

            // Test roundtrip
            let encoded = tx.encoded_2718();
            assert_eq!(encoded.as_slice(), &raw[..], "{:?}", entry.path());

            let TxEip4844WithSidecar { tx, sidecar } = tx.tx();
            assert_matches!(sidecar, BlobTransactionSidecarVariant::Eip7594(_));

            let result = sidecar.validate(&tx.blob_versioned_hashes, kzg_settings.get());
            assert_matches!(result, Err(_));
        }
    }
}