-
Notifications
You must be signed in to change notification settings - Fork 231
/
envelope.rs
1013 lines (916 loc) · 36.8 KB
/
envelope.rs
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
use core::fmt;
use crate::{Signed, Transaction, TxEip1559, TxEip2930, TxEip7702, TxLegacy};
use alloy_eips::{
eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718},
eip2930::AccessList,
};
use alloy_primitives::{TxKind, B256};
use alloy_rlp::{Decodable, Encodable, Header};
use crate::transaction::eip4844::{TxEip4844, TxEip4844Variant, TxEip4844WithSidecar};
/// Ethereum `TransactionType` flags as specified in EIPs [2718], [1559], [2930],
/// [4844], and [7702].
///
/// [2718]: https://eips.ethereum.org/EIPS/eip-2718
/// [1559]: https://eips.ethereum.org/EIPS/eip-1559
/// [2930]: https://eips.ethereum.org/EIPS/eip-2930
/// [4844]: https://eips.ethereum.org/EIPS/eip-4844
/// [7702]: https://eips.ethereum.org/EIPS/eip-7702
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[doc(alias = "TransactionType")]
pub enum TxType {
/// Legacy transaction type.
Legacy = 0,
/// EIP-2930 transaction type.
Eip2930 = 1,
/// EIP-1559 transaction type.
Eip1559 = 2,
/// EIP-4844 transaction type.
Eip4844 = 3,
/// EIP-7702 transaction type.
Eip7702 = 4,
}
impl From<TxType> for u8 {
fn from(value: TxType) -> Self {
value as Self
}
}
impl fmt::Display for TxType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Legacy => write!(f, "Legacy"),
Self::Eip2930 => write!(f, "EIP-2930"),
Self::Eip1559 => write!(f, "EIP-1559"),
Self::Eip4844 => write!(f, "EIP-4844"),
Self::Eip7702 => write!(f, "EIP-7702"),
}
}
}
#[cfg(any(test, feature = "arbitrary"))]
impl<'a> arbitrary::Arbitrary<'a> for TxType {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
Ok(u.int_in_range(0u8..=3)?.try_into().unwrap())
}
}
impl TryFrom<u8> for TxType {
type Error = Eip2718Error;
fn try_from(value: u8) -> Result<Self, Self::Error> {
Ok(match value {
0 => Self::Legacy,
1 => Self::Eip2930,
2 => Self::Eip1559,
3 => Self::Eip4844,
4 => Self::Eip7702,
_ => return Err(Eip2718Error::UnexpectedType(value)),
})
}
}
/// The Ethereum [EIP-2718] Transaction Envelope.
///
/// # Note:
///
/// This enum distinguishes between tagged and untagged legacy transactions, as
/// the in-protocol merkle tree may commit to EITHER 0-prefixed or raw.
/// Therefore we must ensure that encoding returns the precise byte-array that
/// was decoded, preserving the presence or absence of the `TransactionType`
/// flag.
///
/// [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
#[doc(alias = "TransactionEnvelope")]
#[non_exhaustive]
pub enum TxEnvelope {
/// An untagged [`TxLegacy`].
#[cfg_attr(feature = "serde", serde(rename = "0x0", alias = "0x00"))]
Legacy(Signed<TxLegacy>),
/// A [`TxEip2930`] tagged with type 1.
#[cfg_attr(feature = "serde", serde(rename = "0x1", alias = "0x01"))]
Eip2930(Signed<TxEip2930>),
/// A [`TxEip1559`] tagged with type 2.
#[cfg_attr(feature = "serde", serde(rename = "0x2", alias = "0x02"))]
Eip1559(Signed<TxEip1559>),
/// A TxEip4844 tagged with type 3.
/// An EIP-4844 transaction has two network representations:
/// 1 - The transaction itself, which is a regular RLP-encoded transaction and used to retrieve
/// historical transactions..
///
/// 2 - The transaction with a sidecar, which is the form used to
/// send transactions to the network.
#[cfg_attr(feature = "serde", serde(rename = "0x3", alias = "0x03"))]
Eip4844(Signed<TxEip4844Variant>),
/// A [`TxEip7702`] tagged with type 4.
#[cfg_attr(feature = "serde", serde(rename = "0x4", alias = "0x04"))]
Eip7702(Signed<TxEip7702>),
}
impl From<Signed<TxLegacy>> for TxEnvelope {
fn from(v: Signed<TxLegacy>) -> Self {
Self::Legacy(v)
}
}
impl From<Signed<TxEip2930>> for TxEnvelope {
fn from(v: Signed<TxEip2930>) -> Self {
Self::Eip2930(v)
}
}
impl From<Signed<TxEip1559>> for TxEnvelope {
fn from(v: Signed<TxEip1559>) -> Self {
Self::Eip1559(v)
}
}
impl From<Signed<TxEip4844Variant>> for TxEnvelope {
fn from(v: Signed<TxEip4844Variant>) -> Self {
Self::Eip4844(v)
}
}
impl From<Signed<TxEip4844>> for TxEnvelope {
fn from(v: Signed<TxEip4844>) -> Self {
let (tx, signature, hash) = v.into_parts();
Self::Eip4844(Signed::new_unchecked(tx.into(), signature, hash))
}
}
impl From<Signed<TxEip4844WithSidecar>> for TxEnvelope {
fn from(v: Signed<TxEip4844WithSidecar>) -> Self {
let (tx, signature, hash) = v.into_parts();
Self::Eip4844(Signed::new_unchecked(tx.into(), signature, hash))
}
}
impl From<Signed<TxEip7702>> for TxEnvelope {
fn from(v: Signed<TxEip7702>) -> Self {
Self::Eip7702(v)
}
}
impl TxEnvelope {
/// Returns true if the transaction is a legacy transaction.
#[inline]
pub const fn is_legacy(&self) -> bool {
matches!(self, Self::Legacy(_))
}
/// Returns true if the transaction is an EIP-2930 transaction.
#[inline]
pub const fn is_eip2930(&self) -> bool {
matches!(self, Self::Eip2930(_))
}
/// Returns true if the transaction is an EIP-1559 transaction.
#[inline]
pub const fn is_eip1559(&self) -> bool {
matches!(self, Self::Eip1559(_))
}
/// Returns true if the transaction is an EIP-4844 transaction.
#[inline]
pub const fn is_eip4844(&self) -> bool {
matches!(self, Self::Eip4844(_))
}
/// Returns true if the transaction is an EIP-7702 transaction.
#[inline]
pub const fn is_eip7702(&self) -> bool {
matches!(self, Self::Eip7702(_))
}
/// Returns the [`TxLegacy`] variant if the transaction is a legacy transaction.
pub const fn as_legacy(&self) -> Option<&Signed<TxLegacy>> {
match self {
Self::Legacy(tx) => Some(tx),
_ => None,
}
}
/// Returns the [`TxEip2930`] variant if the transaction is an EIP-2930 transaction.
pub const fn as_eip2930(&self) -> Option<&Signed<TxEip2930>> {
match self {
Self::Eip2930(tx) => Some(tx),
_ => None,
}
}
/// Returns the [`TxEip1559`] variant if the transaction is an EIP-1559 transaction.
pub const fn as_eip1559(&self) -> Option<&Signed<TxEip1559>> {
match self {
Self::Eip1559(tx) => Some(tx),
_ => None,
}
}
/// Returns the [`TxEip4844`] variant if the transaction is an EIP-4844 transaction.
pub const fn as_eip4844(&self) -> Option<&Signed<TxEip4844Variant>> {
match self {
Self::Eip4844(tx) => Some(tx),
_ => None,
}
}
/// Returns the [`TxEip7702`] variant if the transaction is an EIP-7702 transaction.
pub const fn as_eip7702(&self) -> Option<&Signed<TxEip7702>> {
match self {
Self::Eip7702(tx) => Some(tx),
_ => None,
}
}
/// Recover the signer of the transaction.
#[cfg(feature = "k256")]
pub fn recover_signer(
&self,
) -> Result<alloy_primitives::Address, alloy_primitives::SignatureError> {
match self {
Self::Legacy(tx) => tx.recover_signer(),
Self::Eip2930(tx) => tx.recover_signer(),
Self::Eip1559(tx) => tx.recover_signer(),
Self::Eip4844(tx) => tx.recover_signer(),
Self::Eip7702(tx) => tx.recover_signer(),
}
}
/// Calculate the signing hash for the transaction.
pub fn signature_hash(&self) -> B256 {
match self {
Self::Legacy(tx) => tx.signature_hash(),
Self::Eip2930(tx) => tx.signature_hash(),
Self::Eip1559(tx) => tx.signature_hash(),
Self::Eip4844(tx) => tx.signature_hash(),
Self::Eip7702(tx) => tx.signature_hash(),
}
}
/// Return the hash of the inner Signed.
#[doc(alias = "transaction_hash")]
pub const fn tx_hash(&self) -> &B256 {
match self {
Self::Legacy(tx) => tx.hash(),
Self::Eip2930(tx) => tx.hash(),
Self::Eip1559(tx) => tx.hash(),
Self::Eip4844(tx) => tx.hash(),
Self::Eip7702(tx) => tx.hash(),
}
}
/// Return the [`TxType`] of the inner txn.
#[doc(alias = "transaction_type")]
pub const fn tx_type(&self) -> TxType {
match self {
Self::Legacy(_) => TxType::Legacy,
Self::Eip2930(_) => TxType::Eip2930,
Self::Eip1559(_) => TxType::Eip1559,
Self::Eip4844(_) => TxType::Eip4844,
Self::Eip7702(_) => TxType::Eip7702,
}
}
/// Return the length of the inner txn, __without a type byte__.
pub fn inner_length(&self) -> usize {
match self {
Self::Legacy(t) => t.tx().fields_len() + t.signature().rlp_vrs_len(),
Self::Eip2930(t) => {
let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len();
Header { list: true, payload_length }.length() + payload_length
}
Self::Eip1559(t) => {
let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len();
Header { list: true, payload_length }.length() + payload_length
}
Self::Eip4844(t) => match t.tx() {
TxEip4844Variant::TxEip4844(tx) => {
let payload_length = tx.fields_len() + t.signature().rlp_vrs_len();
Header { list: true, payload_length }.length() + payload_length
}
TxEip4844Variant::TxEip4844WithSidecar(tx) => {
let inner_payload_length = tx.tx().fields_len() + t.signature().rlp_vrs_len();
let inner_header = Header { list: true, payload_length: inner_payload_length };
let outer_payload_length =
inner_header.length() + inner_payload_length + tx.sidecar.fields_len();
let outer_header = Header { list: true, payload_length: outer_payload_length };
outer_header.length() + outer_payload_length
}
},
Self::Eip7702(t) => {
let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len();
Header { list: true, payload_length }.length() + payload_length
}
}
}
/// Return the RLP payload length of the network-serialized wrapper
fn rlp_payload_length(&self) -> usize {
if let Self::Legacy(t) = self {
let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len();
return Header { list: true, payload_length }.length() + payload_length;
}
// length of inner tx body
let inner_length = self.inner_length();
// with tx type byte
inner_length + 1
}
}
impl Encodable for TxEnvelope {
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
self.network_encode(out)
}
fn length(&self) -> usize {
let mut payload_length = self.rlp_payload_length();
if !self.is_legacy() {
payload_length += Header { list: false, payload_length }.length();
}
payload_length
}
}
impl Decodable for TxEnvelope {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
match Self::network_decode(buf) {
Ok(t) => Ok(t),
Err(Eip2718Error::RlpError(e)) => Err(e),
Err(Eip2718Error::UnexpectedType(_)) => {
Err(alloy_rlp::Error::Custom("unexpected tx type"))
}
_ => Err(alloy_rlp::Error::Custom("unknown error decoding tx envelope")),
}
}
}
impl Decodable2718 for TxEnvelope {
fn typed_decode(ty: u8, buf: &mut &[u8]) -> Eip2718Result<Self> {
match ty.try_into().map_err(|_| alloy_rlp::Error::Custom("unexpected tx type"))? {
TxType::Eip2930 => Ok(TxEip2930::decode_signed_fields(buf)?.into()),
TxType::Eip1559 => Ok(TxEip1559::decode_signed_fields(buf)?.into()),
TxType::Eip4844 => Ok(TxEip4844Variant::decode_signed_fields(buf)?.into()),
TxType::Eip7702 => Ok(TxEip7702::decode_signed_fields(buf)?.into()),
TxType::Legacy => Err(Eip2718Error::UnexpectedType(0)),
}
}
fn fallback_decode(buf: &mut &[u8]) -> Eip2718Result<Self> {
Ok(TxLegacy::decode_signed_fields(buf)?.into())
}
}
impl Encodable2718 for TxEnvelope {
fn type_flag(&self) -> Option<u8> {
match self {
Self::Legacy(_) => None,
Self::Eip2930(_) => Some(TxType::Eip2930.into()),
Self::Eip1559(_) => Some(TxType::Eip1559.into()),
Self::Eip4844(_) => Some(TxType::Eip4844.into()),
Self::Eip7702(_) => Some(TxType::Eip7702.into()),
}
}
fn encode_2718_len(&self) -> usize {
self.inner_length() + !self.is_legacy() as usize
}
fn encode_2718(&self, out: &mut dyn alloy_rlp::BufMut) {
match self {
// Legacy transactions have no difference between network and 2718
Self::Legacy(tx) => tx.tx().encode_with_signature_fields(tx.signature(), out),
Self::Eip2930(tx) => {
tx.tx().encode_with_signature(tx.signature(), out, false);
}
Self::Eip1559(tx) => {
tx.tx().encode_with_signature(tx.signature(), out, false);
}
Self::Eip4844(tx) => {
tx.tx().encode_with_signature(tx.signature(), out, false);
}
Self::Eip7702(tx) => {
tx.tx().encode_with_signature(tx.signature(), out, false);
}
}
}
}
impl Transaction for TxEnvelope {
fn chain_id(&self) -> Option<alloy_primitives::ChainId> {
match self {
Self::Legacy(tx) => tx.tx().chain_id(),
Self::Eip2930(tx) => tx.tx().chain_id(),
Self::Eip1559(tx) => tx.tx().chain_id(),
Self::Eip4844(tx) => tx.tx().chain_id(),
Self::Eip7702(tx) => tx.tx().chain_id(),
}
}
fn gas_limit(&self) -> u128 {
match self {
Self::Legacy(tx) => tx.tx().gas_limit(),
Self::Eip2930(tx) => tx.tx().gas_limit(),
Self::Eip1559(tx) => tx.tx().gas_limit(),
Self::Eip4844(tx) => tx.tx().gas_limit(),
Self::Eip7702(tx) => tx.tx().gas_limit(),
}
}
fn gas_price(&self) -> Option<u128> {
match self {
Self::Legacy(tx) => tx.tx().gas_price(),
Self::Eip2930(tx) => tx.tx().gas_price(),
Self::Eip1559(tx) => tx.tx().gas_price(),
Self::Eip4844(tx) => tx.tx().gas_price(),
Self::Eip7702(tx) => tx.tx().gas_price(),
}
}
fn max_fee_per_gas(&self) -> u128 {
match self {
Self::Legacy(tx) => tx.tx().max_fee_per_gas(),
Self::Eip2930(tx) => tx.tx().max_fee_per_gas(),
Self::Eip1559(tx) => tx.tx().max_fee_per_gas(),
Self::Eip4844(tx) => tx.tx().max_fee_per_gas(),
Self::Eip7702(tx) => tx.tx().max_fee_per_gas(),
}
}
fn max_priority_fee_per_gas(&self) -> Option<u128> {
match self {
Self::Legacy(tx) => tx.tx().max_priority_fee_per_gas(),
Self::Eip2930(tx) => tx.tx().max_priority_fee_per_gas(),
Self::Eip1559(tx) => tx.tx().max_priority_fee_per_gas(),
Self::Eip4844(tx) => tx.tx().max_priority_fee_per_gas(),
Self::Eip7702(tx) => tx.tx().max_priority_fee_per_gas(),
}
}
fn priority_fee_or_price(&self) -> u128 {
match self {
Self::Legacy(tx) => tx.tx().priority_fee_or_price(),
Self::Eip2930(tx) => tx.tx().priority_fee_or_price(),
Self::Eip1559(tx) => tx.tx().priority_fee_or_price(),
Self::Eip4844(tx) => tx.tx().priority_fee_or_price(),
Self::Eip7702(tx) => tx.tx().priority_fee_or_price(),
}
}
fn max_fee_per_blob_gas(&self) -> Option<u128> {
match self {
Self::Legacy(tx) => tx.tx().max_fee_per_blob_gas(),
Self::Eip2930(tx) => tx.tx().max_fee_per_blob_gas(),
Self::Eip1559(tx) => tx.tx().max_fee_per_blob_gas(),
Self::Eip4844(tx) => tx.tx().max_fee_per_blob_gas(),
Self::Eip7702(tx) => tx.tx().max_fee_per_blob_gas(),
}
}
fn input(&self) -> &[u8] {
match self {
Self::Legacy(tx) => tx.tx().input(),
Self::Eip2930(tx) => tx.tx().input(),
Self::Eip1559(tx) => tx.tx().input(),
Self::Eip4844(tx) => tx.tx().input(),
Self::Eip7702(tx) => tx.tx().input(),
}
}
fn nonce(&self) -> u64 {
match self {
Self::Legacy(tx) => tx.tx().nonce(),
Self::Eip2930(tx) => tx.tx().nonce(),
Self::Eip1559(tx) => tx.tx().nonce(),
Self::Eip4844(tx) => tx.tx().nonce(),
Self::Eip7702(tx) => tx.tx().nonce(),
}
}
fn to(&self) -> TxKind {
match self {
Self::Legacy(tx) => tx.tx().to(),
Self::Eip2930(tx) => tx.tx().to(),
Self::Eip1559(tx) => tx.tx().to(),
Self::Eip4844(tx) => tx.tx().to(),
Self::Eip7702(tx) => tx.tx().to(),
}
}
fn value(&self) -> alloy_primitives::U256 {
match self {
Self::Legacy(tx) => tx.tx().value(),
Self::Eip2930(tx) => tx.tx().value(),
Self::Eip1559(tx) => tx.tx().value(),
Self::Eip4844(tx) => tx.tx().value(),
Self::Eip7702(tx) => tx.tx().value(),
}
}
fn ty(&self) -> u8 {
match self {
Self::Legacy(tx) => tx.tx().ty(),
Self::Eip2930(tx) => tx.tx().ty(),
Self::Eip1559(tx) => tx.tx().ty(),
Self::Eip4844(tx) => tx.tx().ty(),
Self::Eip7702(tx) => tx.tx().ty(),
}
}
fn access_list(&self) -> Option<&AccessList> {
match self {
Self::Legacy(tx) => tx.tx().access_list(),
Self::Eip2930(tx) => tx.tx().access_list(),
Self::Eip1559(tx) => tx.tx().access_list(),
Self::Eip4844(tx) => tx.tx().access_list(),
Self::Eip7702(tx) => tx.tx().access_list(),
}
}
fn blob_versioned_hashes(&self) -> Option<&[B256]> {
match self {
Self::Legacy(tx) => tx.tx().blob_versioned_hashes(),
Self::Eip2930(tx) => tx.tx().blob_versioned_hashes(),
Self::Eip1559(tx) => tx.tx().blob_versioned_hashes(),
Self::Eip4844(tx) => tx.tx().blob_versioned_hashes(),
Self::Eip7702(tx) => tx.tx().blob_versioned_hashes(),
}
}
fn authorization_list(&self) -> Option<&[alloy_eips::eip7702::SignedAuthorization]> {
match self {
Self::Legacy(tx) => tx.tx().authorization_list(),
Self::Eip2930(tx) => tx.tx().authorization_list(),
Self::Eip1559(tx) => tx.tx().authorization_list(),
Self::Eip4844(tx) => tx.tx().authorization_list(),
Self::Eip7702(tx) => tx.tx().authorization_list(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::transaction::SignableTransaction;
use alloy_eips::{
eip2930::{AccessList, AccessListItem},
eip4844::BlobTransactionSidecar,
eip7702::Authorization,
};
use alloy_primitives::{hex, Address, Parity, Signature, U256};
#[allow(unused_imports)]
use alloy_primitives::{Bytes, TxKind};
use std::{fs, path::PathBuf, str::FromStr, vec};
#[cfg(not(feature = "std"))]
use std::vec::Vec;
#[test]
#[cfg(feature = "k256")]
// Test vector from https://etherscan.io/tx/0xce4dc6d7a7549a98ee3b071b67e970879ff51b5b95d1c340bacd80fa1e1aab31
fn test_decode_live_1559_tx() {
use alloy_primitives::address;
let raw_tx = alloy_primitives::hex::decode("02f86f0102843b9aca0085029e7822d68298f094d9e1459a7a482635700cbc20bbaf52d495ab9c9680841b55ba3ac080a0c199674fcb29f353693dd779c017823b954b3c69dffa3cd6b2a6ff7888798039a028ca912de909e7e6cdef9cdcaf24c54dd8c1032946dfa1d85c206b32a9064fe8").unwrap();
let res = TxEnvelope::decode(&mut raw_tx.as_slice()).unwrap();
assert_eq!(res.tx_type(), TxType::Eip1559);
let tx = match res {
TxEnvelope::Eip1559(tx) => tx,
_ => unreachable!(),
};
assert_eq!(tx.tx().to, TxKind::Call(address!("D9e1459A7A482635700cBc20BBAF52D495Ab9C96")));
let from = tx.recover_signer().unwrap();
assert_eq!(from, address!("001e2b7dE757bA469a57bF6b23d982458a07eFcE"));
}
#[test]
#[cfg(feature = "k256")]
// Test vector from https://etherscan.io/tx/0x280cde7cdefe4b188750e76c888f13bd05ce9a4d7767730feefe8a0e50ca6fc4
fn test_decode_live_legacy_tx() {
use alloy_primitives::address;
let raw_tx = alloy_primitives::hex::decode("f9015482078b8505d21dba0083022ef1947a250d5630b4cf539739df2c5dacb4c659f2488d880c46549a521b13d8b8e47ff36ab50000000000000000000000000000000000000000000066ab5a608bd00a23f2fe000000000000000000000000000000000000000000000000000000000000008000000000000000000000000048c04ed5691981c42154c6167398f95e8f38a7ff00000000000000000000000000000000000000000000000000000000632ceac70000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006c6ee5e31d828de241282b9606c8e98ea48526e225a0c9077369501641a92ef7399ff81c21639ed4fd8fc69cb793cfa1dbfab342e10aa0615facb2f1bcf3274a354cfe384a38d0cc008a11c2dd23a69111bc6930ba27a8").unwrap();
let res = TxEnvelope::decode(&mut raw_tx.as_slice()).unwrap();
assert_eq!(res.tx_type(), TxType::Legacy);
let tx = match res {
TxEnvelope::Legacy(tx) => tx,
_ => unreachable!(),
};
assert_eq!(tx.tx().to, TxKind::Call(address!("7a250d5630B4cF539739dF2C5dAcb4c659F2488D")));
assert_eq!(
tx.hash().to_string(),
"0x280cde7cdefe4b188750e76c888f13bd05ce9a4d7767730feefe8a0e50ca6fc4"
);
let from = tx.recover_signer().unwrap();
assert_eq!(from, address!("a12e1462d0ceD572f396F58B6E2D03894cD7C8a4"));
}
#[test]
#[cfg(feature = "k256")]
// Test vector from https://sepolia.etherscan.io/tx/0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0
// Blobscan: https://sepolia.blobscan.com/tx/0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0
fn test_decode_live_4844_tx() {
use crate::Transaction;
use alloy_primitives::{address, b256};
// https://sepolia.etherscan.io/getRawTx?tx=0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0
let raw_tx = alloy_primitives::hex::decode("0x03f9011d83aa36a7820fa28477359400852e90edd0008252089411e9ca82a3a762b4b5bd264d4173a242e7a770648080c08504a817c800f8a5a0012ec3d6f66766bedb002a190126b3549fce0047de0d4c25cffce0dc1c57921aa00152d8e24762ff22b1cfd9f8c0683786a7ca63ba49973818b3d1e9512cd2cec4a0013b98c6c83e066d5b14af2b85199e3d4fc7d1e778dd53130d180f5077e2d1c7a001148b495d6e859114e670ca54fb6e2657f0cbae5b08063605093a4b3dc9f8f1a0011ac212f13c5dff2b2c6b600a79635103d6f580a4221079951181b25c7e654901a0c8de4cced43169f9aa3d36506363b2d2c44f6c49fc1fd91ea114c86f3757077ea01e11fdd0d1934eda0492606ee0bb80a7bf8f35cc5f86ec60fe5031ba48bfd544").unwrap();
let res = TxEnvelope::decode(&mut raw_tx.as_slice()).unwrap();
assert_eq!(res.tx_type(), TxType::Eip4844);
let tx = match res {
TxEnvelope::Eip4844(tx) => tx,
_ => unreachable!(),
};
assert_eq!(
tx.tx().to(),
TxKind::Call(address!("11E9CA82A3a762b4B5bd264d4173a242e7a77064"))
);
// Assert this is the correct variant of the EIP-4844 enum, which only contains the tx.
assert!(matches!(tx.tx(), TxEip4844Variant::TxEip4844(_)));
assert_eq!(
tx.tx().tx().blob_versioned_hashes,
vec![
b256!("012ec3d6f66766bedb002a190126b3549fce0047de0d4c25cffce0dc1c57921a"),
b256!("0152d8e24762ff22b1cfd9f8c0683786a7ca63ba49973818b3d1e9512cd2cec4"),
b256!("013b98c6c83e066d5b14af2b85199e3d4fc7d1e778dd53130d180f5077e2d1c7"),
b256!("01148b495d6e859114e670ca54fb6e2657f0cbae5b08063605093a4b3dc9f8f1"),
b256!("011ac212f13c5dff2b2c6b600a79635103d6f580a4221079951181b25c7e6549")
]
);
let from = tx.recover_signer().unwrap();
assert_eq!(from, address!("A83C816D4f9b2783761a22BA6FADB0eB0606D7B2"));
}
fn test_encode_decode_roundtrip<T: SignableTransaction<Signature>>(
tx: T,
signature: Option<Signature>,
) where
Signed<T>: Into<TxEnvelope>,
{
let signature = signature.unwrap_or_else(Signature::test_signature);
let tx_signed = tx.into_signed(signature);
let tx_envelope: TxEnvelope = tx_signed.into();
let encoded = tx_envelope.encoded_2718();
let decoded = TxEnvelope::decode_2718(&mut encoded.as_ref()).unwrap();
assert_eq!(encoded.len(), tx_envelope.encode_2718_len());
assert_eq!(decoded, tx_envelope);
}
#[test]
fn test_encode_decode_eip1559() {
let tx = TxEip1559 {
chain_id: 1u64,
nonce: 2,
max_fee_per_gas: 3,
max_priority_fee_per_gas: 4,
gas_limit: 5,
to: Address::left_padding_from(&[6]).into(),
value: U256::from(7_u64),
input: vec![8].into(),
access_list: Default::default(),
};
test_encode_decode_roundtrip(tx, None);
}
#[test]
fn test_encode_decode_eip1559_parity_eip155() {
let tx = TxEip1559 {
chain_id: 1u64,
nonce: 2,
max_fee_per_gas: 3,
max_priority_fee_per_gas: 4,
gas_limit: 5,
to: Address::left_padding_from(&[6]).into(),
value: U256::from(7_u64),
input: vec![8].into(),
access_list: Default::default(),
};
let signature = Signature::test_signature().with_parity(Parity::Eip155(42));
test_encode_decode_roundtrip(tx, Some(signature));
}
#[test]
fn test_encode_decode_eip2930_parity_eip155() {
let tx = TxEip2930 {
chain_id: 1u64,
nonce: 2,
gas_price: 3,
gas_limit: 4,
to: Address::left_padding_from(&[5]).into(),
value: U256::from(6_u64),
input: vec![7].into(),
access_list: Default::default(),
};
let signature = Signature::test_signature().with_parity(Parity::Eip155(42));
test_encode_decode_roundtrip(tx, Some(signature));
}
#[test]
fn test_encode_decode_eip4844_parity_eip155() {
let tx = TxEip4844 {
chain_id: 1,
nonce: 100,
max_fee_per_gas: 50_000_000_000,
max_priority_fee_per_gas: 1_000_000_000_000,
gas_limit: 1_000_000,
to: Address::random(),
value: U256::from(10e18),
input: Bytes::new(),
access_list: AccessList(vec![AccessListItem {
address: Address::random(),
storage_keys: vec![B256::random()],
}]),
blob_versioned_hashes: vec![B256::random()],
max_fee_per_blob_gas: 0,
};
let signature = Signature::test_signature().with_parity(Parity::Eip155(42));
test_encode_decode_roundtrip(tx, Some(signature));
}
#[test]
fn test_encode_decode_eip4844_sidecar_parity_eip155() {
let tx = TxEip4844 {
chain_id: 1,
nonce: 100,
max_fee_per_gas: 50_000_000_000,
max_priority_fee_per_gas: 1_000_000_000_000,
gas_limit: 1_000_000,
to: Address::random(),
value: U256::from(10e18),
input: Bytes::new(),
access_list: AccessList(vec![AccessListItem {
address: Address::random(),
storage_keys: vec![B256::random()],
}]),
blob_versioned_hashes: vec![B256::random()],
max_fee_per_blob_gas: 0,
};
let sidecar = BlobTransactionSidecar {
blobs: vec![[2; 131072].into()],
commitments: vec![[3; 48].into()],
proofs: vec![[4; 48].into()],
};
let tx = TxEip4844WithSidecar { tx, sidecar };
let signature = Signature::test_signature().with_parity(Parity::Eip155(42));
test_encode_decode_roundtrip(tx, Some(signature));
}
#[test]
fn test_encode_decode_eip4844_variant_parity_eip155() {
let tx = TxEip4844 {
chain_id: 1,
nonce: 100,
max_fee_per_gas: 50_000_000_000,
max_priority_fee_per_gas: 1_000_000_000_000,
gas_limit: 1_000_000,
to: Address::random(),
value: U256::from(10e18),
input: Bytes::new(),
access_list: AccessList(vec![AccessListItem {
address: Address::random(),
storage_keys: vec![B256::random()],
}]),
blob_versioned_hashes: vec![B256::random()],
max_fee_per_blob_gas: 0,
};
let tx = TxEip4844Variant::TxEip4844(tx);
let signature = Signature::test_signature().with_parity(Parity::Eip155(42));
test_encode_decode_roundtrip(tx, Some(signature));
}
#[test]
fn test_encode_decode_eip2930() {
let tx = TxEip2930 {
chain_id: 1u64,
nonce: 2,
gas_price: 3,
gas_limit: 4,
to: Address::left_padding_from(&[5]).into(),
value: U256::from(6_u64),
input: vec![7].into(),
access_list: AccessList(vec![AccessListItem {
address: Address::left_padding_from(&[8]),
storage_keys: vec![B256::left_padding_from(&[9])],
}]),
};
test_encode_decode_roundtrip(tx, None);
}
#[test]
fn test_encode_decode_eip7702() {
let tx = TxEip7702 {
chain_id: 1u64,
nonce: 2,
gas_limit: 3,
max_fee_per_gas: 4,
max_priority_fee_per_gas: 5,
to: Address::left_padding_from(&[5]),
value: U256::from(6_u64),
input: vec![7].into(),
access_list: AccessList(vec![AccessListItem {
address: Address::left_padding_from(&[8]),
storage_keys: vec![B256::left_padding_from(&[9])],
}]),
authorization_list: vec![(Authorization {
chain_id: U256::from(1),
address: Address::left_padding_from(&[10]),
nonce: 1u64,
})
.into_signed(Signature::from_str("48b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c8041b").unwrap())],
};
test_encode_decode_roundtrip(tx, None);
}
#[test]
fn test_encode_decode_transaction_list() {
let signature = Signature::test_signature();
let tx = TxEnvelope::Eip1559(
TxEip1559 {
chain_id: 1u64,
nonce: 2,
max_fee_per_gas: 3,
max_priority_fee_per_gas: 4,
gas_limit: 5,
to: Address::left_padding_from(&[6]).into(),
value: U256::from(7_u64),
input: vec![8].into(),
access_list: Default::default(),
}
.into_signed(signature),
);
let transactions = vec![tx.clone(), tx];
let encoded = alloy_rlp::encode(&transactions);
let decoded = Vec::<TxEnvelope>::decode(&mut &encoded[..]).unwrap();
assert_eq!(transactions, decoded);
}
#[test]
fn decode_encode_known_rpc_transaction() {
// test data pulled from hive test that sends blob transactions
let network_data_path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/rpc_blob_transaction.rlp");
let data = fs::read_to_string(network_data_path).expect("Unable to read file");
let hex_data = hex::decode(data.trim()).unwrap();
let tx: TxEnvelope = TxEnvelope::decode_2718(&mut hex_data.as_slice()).unwrap();
let encoded = tx.encoded_2718();
assert_eq!(encoded, hex_data);
assert_eq!(tx.encode_2718_len(), hex_data.len());
}
#[cfg(feature = "serde")]
fn test_serde_roundtrip<T: SignableTransaction<Signature>>(tx: T)
where
Signed<T>: Into<TxEnvelope>,
{
let signature = Signature::test_signature();
let tx_envelope: TxEnvelope = tx.into_signed(signature).into();
let serialized = serde_json::to_string(&tx_envelope).unwrap();
let deserialized: TxEnvelope = serde_json::from_str(&serialized).unwrap();
assert_eq!(tx_envelope, deserialized);
}
#[test]
#[cfg(feature = "serde")]
fn test_serde_roundtrip_legacy() {
let tx = TxLegacy {
chain_id: Some(1),
nonce: 100,
gas_price: 3_000_000_000,
gas_limit: 50_000,
to: Address::default().into(),
value: U256::from(10e18),
input: Bytes::new(),
};
test_serde_roundtrip(tx);
}
#[test]
#[cfg(feature = "serde")]
fn test_serde_roundtrip_eip1559() {
let tx = TxEip1559 {
chain_id: 1,
nonce: 100,
max_fee_per_gas: 50_000_000_000,
max_priority_fee_per_gas: 1_000_000_000_000,
gas_limit: 1_000_000,
to: TxKind::Create,
value: U256::from(10e18),
input: Bytes::new(),
access_list: AccessList(vec![AccessListItem {
address: Address::random(),
storage_keys: vec![B256::random()],
}]),
};
test_serde_roundtrip(tx);
}
#[test]
#[cfg(feature = "serde")]
fn test_serde_roundtrip_eip2930() {
let tx = TxEip2930 {
chain_id: u64::MAX,
nonce: u64::MAX,
gas_price: u128::MAX,
gas_limit: u128::MAX,
to: Address::random().into(),
value: U256::MAX,
input: Bytes::new(),
access_list: Default::default(),
};
test_serde_roundtrip(tx);
}
#[test]
#[cfg(feature = "serde")]
fn test_serde_roundtrip_eip4844() {
let tx = TxEip4844Variant::TxEip4844(TxEip4844 {
chain_id: 1,
nonce: 100,
max_fee_per_gas: 50_000_000_000,
max_priority_fee_per_gas: 1_000_000_000_000,
gas_limit: 1_000_000,
to: Address::random(),
value: U256::from(10e18),
input: Bytes::new(),
access_list: AccessList(vec![AccessListItem {
address: Address::random(),
storage_keys: vec![B256::random()],
}]),
blob_versioned_hashes: vec![B256::random()],
max_fee_per_blob_gas: 0,
});
test_serde_roundtrip(tx);
let tx = TxEip4844Variant::TxEip4844WithSidecar(TxEip4844WithSidecar {
tx: TxEip4844 {
chain_id: 1,
nonce: 100,
max_fee_per_gas: 50_000_000_000,
max_priority_fee_per_gas: 1_000_000_000_000,
gas_limit: 1_000_000,
to: Address::random(),
value: U256::from(10e18),
input: Bytes::new(),
access_list: AccessList(vec![AccessListItem {
address: Address::random(),
storage_keys: vec![B256::random()],
}]),
blob_versioned_hashes: vec![B256::random()],
max_fee_per_blob_gas: 0,
},
sidecar: Default::default(),
});
test_serde_roundtrip(tx);
}
#[test]
#[cfg(feature = "serde")]
fn test_serde_roundtrip_eip7702() {
let tx = TxEip7702 {
chain_id: u64::MAX,
nonce: u64::MAX,
gas_limit: u128::MAX,
max_fee_per_gas: u128::MAX,
max_priority_fee_per_gas: u128::MAX,
to: Address::random(),
value: U256::MAX,
input: Bytes::new(),
access_list: AccessList(vec![AccessListItem {