-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathtransaction.rs
More file actions
3133 lines (2937 loc) · 125 KB
/
transaction.rs
File metadata and controls
3133 lines (2937 loc) · 125 KB
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 std::{cmp::min, fmt::Display};
use crate::utils::keccak;
use bytes::Bytes;
use ethereum_types::{Address, H256, Signature, U256};
pub use mempool::MempoolTransaction;
use rkyv::{Archive, Deserialize as RDeserialize, Serialize as RSerialize};
use secp256k1::{Message, ecdsa::RecoveryId};
use serde::{Serialize, ser::SerializeStruct};
pub use serde_impl::{AccessListEntry, GenericTransaction, GenericTransactionError};
use sha3::{Digest, Keccak256};
use ethrex_rlp::{
constants::RLP_NULL,
decode::{RLPDecode, get_rlp_bytes_item_payload, is_encoded_as_bytes},
encode::{PayloadRLPEncode, RLPEncode},
error::RLPDecodeError,
structs::{Decoder, Encoder},
};
use crate::types::{AccessList, AuthorizationList, BlobsBundle};
use once_cell::sync::OnceCell;
// The `#[serde(untagged)]` attribute allows the `Transaction` enum to be serialized without
// a tag indicating the variant type. This means that Serde will serialize the enum's variants
// directly according to the structure of the variant itself.
// For each variant, Serde will use the serialization logic implemented
// for the inner type of that variant (like `LegacyTransaction`, `EIP2930Transaction`, etc.).
// The serialization will fail if the data does not match the structure of any variant.
//
// A custom Deserialization method is implemented to match the specific transaction `type`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, RSerialize, RDeserialize, Archive)]
#[serde(untagged)]
pub enum Transaction {
LegacyTransaction(LegacyTransaction),
EIP2930Transaction(EIP2930Transaction),
EIP1559Transaction(EIP1559Transaction),
EIP4844Transaction(EIP4844Transaction),
EIP7702Transaction(EIP7702Transaction),
PrivilegedL2Transaction(PrivilegedL2Transaction),
}
/// The same as a Transaction enum, only that blob transactions are in wrapped format, including
/// the blobs bundle.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum P2PTransaction {
LegacyTransaction(LegacyTransaction),
EIP2930Transaction(EIP2930Transaction),
EIP1559Transaction(EIP1559Transaction),
EIP4844TransactionWithBlobs(WrappedEIP4844Transaction),
EIP7702Transaction(EIP7702Transaction),
PrivilegedL2Transaction(PrivilegedL2Transaction),
}
impl TryInto<Transaction> for P2PTransaction {
type Error = String;
fn try_into(self) -> Result<Transaction, Self::Error> {
match self {
P2PTransaction::LegacyTransaction(itx) => Ok(Transaction::LegacyTransaction(itx)),
P2PTransaction::EIP2930Transaction(itx) => Ok(Transaction::EIP2930Transaction(itx)),
P2PTransaction::EIP1559Transaction(itx) => Ok(Transaction::EIP1559Transaction(itx)),
P2PTransaction::EIP7702Transaction(itx) => Ok(Transaction::EIP7702Transaction(itx)),
P2PTransaction::PrivilegedL2Transaction(itx) => {
Ok(Transaction::PrivilegedL2Transaction(itx))
}
_ => Err("Can't convert blob p2p transaction into regular transaction. Blob bundle would be lost.".to_string()),
}
}
}
impl RLPEncode for P2PTransaction {
fn encode(&self, buf: &mut dyn bytes::BufMut) {
match self {
P2PTransaction::LegacyTransaction(t) => t.encode(buf),
tx => Bytes::copy_from_slice(&tx.encode_canonical_to_vec()).encode(buf),
};
}
}
impl RLPDecode for P2PTransaction {
fn decode_unfinished(rlp: &[u8]) -> Result<(Self, &[u8]), RLPDecodeError> {
if is_encoded_as_bytes(rlp)? {
// Adjust the encoding to get the payload
let payload = get_rlp_bytes_item_payload(rlp)?;
let tx_type = payload.first().ok_or(RLPDecodeError::InvalidLength)?;
let tx_encoding = &payload.get(1..).ok_or(RLPDecodeError::InvalidLength)?;
// Look at the first byte to check if it corresponds to a TransactionType
match *tx_type {
// Legacy
0x0 => LegacyTransaction::decode_unfinished(tx_encoding)
.map(|(tx, rem)| (P2PTransaction::LegacyTransaction(tx), rem)), // TODO: check if this is a real case scenario
// EIP2930
0x1 => EIP2930Transaction::decode_unfinished(tx_encoding)
.map(|(tx, rem)| (P2PTransaction::EIP2930Transaction(tx), rem)),
// EIP1559
0x2 => EIP1559Transaction::decode_unfinished(tx_encoding)
.map(|(tx, rem)| (P2PTransaction::EIP1559Transaction(tx), rem)),
// EIP4844
0x3 => WrappedEIP4844Transaction::decode_unfinished(tx_encoding)
.map(|(tx, rem)| (P2PTransaction::EIP4844TransactionWithBlobs(tx), rem)),
// EIP7702
0x4 => EIP7702Transaction::decode_unfinished(tx_encoding)
.map(|(tx, rem)| (P2PTransaction::EIP7702Transaction(tx), rem)),
// PrivilegedL2
0x7e => PrivilegedL2Transaction::decode_unfinished(tx_encoding)
.map(|(tx, rem)| (P2PTransaction::PrivilegedL2Transaction(tx), rem)),
ty => Err(RLPDecodeError::Custom(format!(
"Invalid transaction type: {ty}"
))),
}
} else {
// LegacyTransaction
LegacyTransaction::decode_unfinished(rlp)
.map(|(tx, rem)| (P2PTransaction::LegacyTransaction(tx), rem))
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WrappedEIP4844Transaction {
pub tx: EIP4844Transaction,
pub wrapper_version: Option<u8>,
pub blobs_bundle: BlobsBundle,
}
impl RLPEncode for WrappedEIP4844Transaction {
fn encode(&self, buf: &mut dyn bytes::BufMut) {
let encoder = Encoder::new(buf);
encoder
.encode_field(&self.tx)
.encode_optional_field(&self.wrapper_version)
.encode_field(&self.blobs_bundle.blobs)
.encode_field(&self.blobs_bundle.commitments)
.encode_field(&self.blobs_bundle.proofs)
.finish();
}
}
impl RLPDecode for WrappedEIP4844Transaction {
fn decode_unfinished(rlp: &[u8]) -> Result<(WrappedEIP4844Transaction, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
let (tx, decoder) = decoder.decode_field("tx")?;
let (wrapper_version, decoder) = decoder.decode_optional_field();
let (blobs, decoder) = decoder.decode_field("blobs")?;
let (commitments, decoder) = decoder.decode_field("commitments")?;
let (proofs, decoder) = decoder.decode_field("proofs")?;
let wrapped = WrappedEIP4844Transaction {
tx,
wrapper_version,
blobs_bundle: BlobsBundle {
blobs,
commitments,
proofs,
version: wrapper_version.unwrap_or_default(),
},
};
Ok((wrapped, decoder.finish()?))
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default, RSerialize, RDeserialize, Archive)]
pub struct LegacyTransaction {
pub nonce: u64,
pub gas_price: u64,
pub gas: u64,
/// The recipient of the transaction.
/// Create transactions contain a [`null`](RLP_NULL) value in this field.
pub to: TxKind,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub value: U256,
#[rkyv(with=crate::rkyv_utils::BytesWrapper)]
pub data: Bytes,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub v: U256,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub r: U256,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub s: U256,
#[rkyv(with=rkyv::with::Skip)]
pub inner_hash: OnceCell<H256>,
}
#[derive(Clone, Debug, PartialEq, Eq, Default, RSerialize, RDeserialize, Archive)]
pub struct EIP2930Transaction {
pub chain_id: u64,
pub nonce: u64,
pub gas_price: u64,
pub gas_limit: u64,
pub to: TxKind,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub value: U256,
#[rkyv(with=crate::rkyv_utils::BytesWrapper)]
pub data: Bytes,
#[rkyv(with=rkyv::with::Map<crate::rkyv_utils::AccessListItemWrapper>)]
pub access_list: AccessList,
pub signature_y_parity: bool,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub signature_r: U256,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub signature_s: U256,
#[rkyv(with=rkyv::with::Skip)]
pub inner_hash: OnceCell<H256>,
}
#[derive(Clone, Debug, PartialEq, Eq, Default, RSerialize, RDeserialize, Archive)]
pub struct EIP1559Transaction {
pub chain_id: u64,
pub nonce: u64,
pub max_priority_fee_per_gas: u64,
pub max_fee_per_gas: u64,
pub gas_limit: u64,
pub to: TxKind,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub value: U256,
#[rkyv(with=crate::rkyv_utils::BytesWrapper)]
pub data: Bytes,
#[rkyv(with=rkyv::with::Map<crate::rkyv_utils::AccessListItemWrapper>)]
pub access_list: AccessList,
pub signature_y_parity: bool,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub signature_r: U256,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub signature_s: U256,
#[rkyv(with=rkyv::with::Skip)]
pub inner_hash: OnceCell<H256>,
}
#[derive(Clone, Debug, PartialEq, Eq, Default, RSerialize, RDeserialize, Archive)]
pub struct EIP4844Transaction {
pub chain_id: u64,
pub nonce: u64,
pub max_priority_fee_per_gas: u64,
pub max_fee_per_gas: u64,
pub gas: u64,
#[rkyv(with=crate::rkyv_utils::H160Wrapper)]
pub to: Address,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub value: U256,
#[rkyv(with=crate::rkyv_utils::BytesWrapper)]
pub data: Bytes,
#[rkyv(with=rkyv::with::Map<crate::rkyv_utils::AccessListItemWrapper>)]
pub access_list: AccessList,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub max_fee_per_blob_gas: U256,
#[rkyv(with=rkyv::with::Map<crate::rkyv_utils::H256Wrapper>)]
pub blob_versioned_hashes: Vec<H256>,
pub signature_y_parity: bool,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub signature_r: U256,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub signature_s: U256,
#[rkyv(with=rkyv::with::Skip)]
pub inner_hash: OnceCell<H256>,
}
#[derive(Clone, Debug, PartialEq, Eq, Default, RSerialize, RDeserialize, Archive)]
pub struct EIP7702Transaction {
pub chain_id: u64,
pub nonce: u64,
pub max_priority_fee_per_gas: u64,
pub max_fee_per_gas: u64,
pub gas_limit: u64,
#[rkyv(with=crate::rkyv_utils::H160Wrapper)]
pub to: Address,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub value: U256,
#[rkyv(with=crate::rkyv_utils::BytesWrapper)]
pub data: Bytes,
#[rkyv(with=rkyv::with::Map<crate::rkyv_utils::AccessListItemWrapper>)]
pub access_list: AccessList,
pub authorization_list: AuthorizationList,
pub signature_y_parity: bool,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub signature_r: U256,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub signature_s: U256,
#[rkyv(with=rkyv::with::Skip)]
pub inner_hash: OnceCell<H256>,
}
#[derive(Clone, Debug, PartialEq, Eq, Default, RSerialize, RDeserialize, Archive)]
pub struct PrivilegedL2Transaction {
pub chain_id: u64,
pub nonce: u64,
pub max_priority_fee_per_gas: u64,
pub max_fee_per_gas: u64,
pub gas_limit: u64,
pub to: TxKind,
#[rkyv(with=crate::rkyv_utils::U256Wrapper)]
pub value: U256,
#[rkyv(with=crate::rkyv_utils::BytesWrapper)]
pub data: Bytes,
#[rkyv(with=rkyv::with::Map<crate::rkyv_utils::AccessListItemWrapper>)]
pub access_list: AccessList,
#[rkyv(with=crate::rkyv_utils::H160Wrapper)]
pub from: Address,
#[rkyv(with=rkyv::with::Skip)]
pub inner_hash: OnceCell<H256>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum TxType {
#[default]
Legacy = 0x00,
EIP2930 = 0x01,
EIP1559 = 0x02,
EIP4844 = 0x03,
EIP7702 = 0x04,
// We take the same approach as Optimism to define the privileged tx prefix
// https://github.com/ethereum-optimism/specs/blob/c6903a3b2cad575653e1f5ef472debb573d83805/specs/protocol/deposits.md#the-deposited-transaction-type
Privileged = 0x7e,
}
impl From<TxType> for u8 {
fn from(val: TxType) -> Self {
match val {
TxType::Legacy => 0x00,
TxType::EIP2930 => 0x01,
TxType::EIP1559 => 0x02,
TxType::EIP4844 => 0x03,
TxType::EIP7702 => 0x04,
TxType::Privileged => 0x7e,
}
}
}
impl Display for TxType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TxType::Legacy => write!(f, "Legacy"),
TxType::EIP2930 => write!(f, "EIP2930"),
TxType::EIP1559 => write!(f, "EIP1559"),
TxType::EIP4844 => write!(f, "EIP4844"),
TxType::EIP7702 => write!(f, "EIP7702"),
TxType::Privileged => write!(f, "Privileged"),
}
}
}
impl Transaction {
pub fn tx_type(&self) -> TxType {
match self {
Transaction::LegacyTransaction(_) => TxType::Legacy,
Transaction::EIP2930Transaction(_) => TxType::EIP2930,
Transaction::EIP1559Transaction(_) => TxType::EIP1559,
Transaction::EIP4844Transaction(_) => TxType::EIP4844,
Transaction::EIP7702Transaction(_) => TxType::EIP7702,
Transaction::PrivilegedL2Transaction(_) => TxType::Privileged,
}
}
fn calc_effective_gas_price(&self, base_fee_per_gas: Option<u64>) -> Option<u64> {
if self.max_fee_per_gas()? < base_fee_per_gas? {
// This is invalid, can't calculate
return None;
}
let priority_fee_per_gas = min(
self.max_priority_fee()?,
self.max_fee_per_gas()?.saturating_sub(base_fee_per_gas?),
);
Some(priority_fee_per_gas + base_fee_per_gas?)
}
pub fn effective_gas_price(&self, base_fee_per_gas: Option<u64>) -> Option<u64> {
match self.tx_type() {
TxType::Legacy => Some(self.gas_price()),
TxType::EIP2930 => Some(self.gas_price()),
TxType::EIP1559 => self.calc_effective_gas_price(base_fee_per_gas),
TxType::EIP4844 => self.calc_effective_gas_price(base_fee_per_gas),
TxType::EIP7702 => self.calc_effective_gas_price(base_fee_per_gas),
TxType::Privileged => Some(self.gas_price()),
}
}
pub fn cost_without_base_fee(&self) -> Option<U256> {
let price = match self.tx_type() {
TxType::Legacy => self.gas_price(),
TxType::EIP2930 => self.gas_price(),
TxType::EIP1559 => self.max_fee_per_gas()?,
TxType::EIP4844 => self.max_fee_per_gas()?,
TxType::EIP7702 => self.max_fee_per_gas()?,
TxType::Privileged => self.gas_price(),
};
Some(U256::saturating_add(
U256::saturating_mul(price.into(), self.gas_limit().into()),
self.value(),
))
}
}
impl RLPEncode for Transaction {
/// Transactions can be encoded in the following formats:
/// A) Legacy transactions: rlp(LegacyTransaction)
/// B) Non legacy transactions: rlp(Bytes) where Bytes represents the canonical encoding for the transaction as a bytes object.
/// Checkout [Transaction::encode_canonical] for more information
fn encode(&self, buf: &mut dyn bytes::BufMut) {
match self {
Transaction::LegacyTransaction(t) => t.encode(buf),
tx => Bytes::copy_from_slice(&tx.encode_canonical_to_vec()).encode(buf),
};
}
}
impl RLPDecode for Transaction {
/// Transactions can be encoded in the following formats:
/// A) Legacy transactions: rlp(LegacyTransaction)
/// B) Non legacy transactions: rlp(Bytes) where Bytes represents the canonical encoding for the transaction as a bytes object.
/// Checkout [Transaction::decode_canonical] for more information
fn decode_unfinished(rlp: &[u8]) -> Result<(Self, &[u8]), RLPDecodeError> {
if is_encoded_as_bytes(rlp)? {
// Adjust the encoding to get the payload
let payload = get_rlp_bytes_item_payload(rlp)?;
let tx_type = payload.first().ok_or(RLPDecodeError::InvalidLength)?;
let tx_encoding = &payload.get(1..).ok_or(RLPDecodeError::InvalidLength)?;
// Look at the first byte to check if it corresponds to a TransactionType
match *tx_type {
// Legacy
0x0 => LegacyTransaction::decode_unfinished(tx_encoding)
.map(|(tx, rem)| (Transaction::LegacyTransaction(tx), rem)), // TODO: check if this is a real case scenario
// EIP2930
0x1 => EIP2930Transaction::decode_unfinished(tx_encoding)
.map(|(tx, rem)| (Transaction::EIP2930Transaction(tx), rem)),
// EIP1559
0x2 => EIP1559Transaction::decode_unfinished(tx_encoding)
.map(|(tx, rem)| (Transaction::EIP1559Transaction(tx), rem)),
// EIP4844
0x3 => EIP4844Transaction::decode_unfinished(tx_encoding)
.map(|(tx, rem)| (Transaction::EIP4844Transaction(tx), rem)),
// EIP7702
0x4 => EIP7702Transaction::decode_unfinished(tx_encoding)
.map(|(tx, rem)| (Transaction::EIP7702Transaction(tx), rem)),
// PrivilegedL2
0x7e => PrivilegedL2Transaction::decode_unfinished(tx_encoding)
.map(|(tx, rem)| (Transaction::PrivilegedL2Transaction(tx), rem)),
ty => Err(RLPDecodeError::Custom(format!(
"Invalid transaction type: {ty}"
))),
}
} else {
// LegacyTransaction
LegacyTransaction::decode_unfinished(rlp)
.map(|(tx, rem)| (Transaction::LegacyTransaction(tx), rem))
}
}
}
/// The transaction's kind: call or create.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, RSerialize, RDeserialize, Archive)]
pub enum TxKind {
Call(#[rkyv(with=crate::rkyv_utils::H160Wrapper)] Address),
#[default]
Create,
}
impl RLPEncode for TxKind {
fn encode(&self, buf: &mut dyn bytes::BufMut) {
match self {
Self::Call(address) => address.encode(buf),
Self::Create => buf.put_u8(RLP_NULL),
}
}
}
impl RLPDecode for TxKind {
fn decode_unfinished(rlp: &[u8]) -> Result<(Self, &[u8]), RLPDecodeError> {
let first_byte = rlp.first().ok_or(RLPDecodeError::InvalidLength)?;
if *first_byte == RLP_NULL {
return Ok((Self::Create, &rlp[1..]));
}
Address::decode_unfinished(rlp).map(|(t, rest)| (Self::Call(t), rest))
}
}
impl RLPEncode for LegacyTransaction {
fn encode(&self, buf: &mut dyn bytes::BufMut) {
Encoder::new(buf)
.encode_field(&self.nonce)
.encode_field(&self.gas_price)
.encode_field(&self.gas)
.encode_field(&self.to)
.encode_field(&self.value)
.encode_field(&self.data)
.encode_field(&self.v)
.encode_field(&self.r)
.encode_field(&self.s)
.finish();
}
}
impl RLPEncode for EIP2930Transaction {
fn encode(&self, buf: &mut dyn bytes::BufMut) {
Encoder::new(buf)
.encode_field(&self.chain_id)
.encode_field(&self.nonce)
.encode_field(&self.gas_price)
.encode_field(&self.gas_limit)
.encode_field(&self.to)
.encode_field(&self.value)
.encode_field(&self.data)
.encode_field(&self.access_list)
.encode_field(&self.signature_y_parity)
.encode_field(&self.signature_r)
.encode_field(&self.signature_s)
.finish()
}
}
impl RLPEncode for EIP1559Transaction {
fn encode(&self, buf: &mut dyn bytes::BufMut) {
Encoder::new(buf)
.encode_field(&self.chain_id)
.encode_field(&self.nonce)
.encode_field(&self.max_priority_fee_per_gas)
.encode_field(&self.max_fee_per_gas)
.encode_field(&self.gas_limit)
.encode_field(&self.to)
.encode_field(&self.value)
.encode_field(&self.data)
.encode_field(&self.access_list)
.encode_field(&self.signature_y_parity)
.encode_field(&self.signature_r)
.encode_field(&self.signature_s)
.finish()
}
}
impl RLPEncode for EIP4844Transaction {
fn encode(&self, buf: &mut dyn bytes::BufMut) {
Encoder::new(buf)
.encode_field(&self.chain_id)
.encode_field(&self.nonce)
.encode_field(&self.max_priority_fee_per_gas)
.encode_field(&self.max_fee_per_gas)
.encode_field(&self.gas)
.encode_field(&self.to)
.encode_field(&self.value)
.encode_field(&self.data)
.encode_field(&self.access_list)
.encode_field(&self.max_fee_per_blob_gas)
.encode_field(&self.blob_versioned_hashes)
.encode_field(&self.signature_y_parity)
.encode_field(&self.signature_r)
.encode_field(&self.signature_s)
.finish()
}
}
impl EIP4844Transaction {
pub fn rlp_encode_as_pooled_tx(
&self,
buf: &mut dyn bytes::BufMut,
tx_blobs_bundle: &BlobsBundle,
) {
buf.put_bytes(TxType::EIP4844.into(), 1);
self.encode(buf);
let mut encoded_blobs = Vec::new();
Encoder::new(&mut encoded_blobs)
.encode_field(&tx_blobs_bundle.blobs)
.encode_field(&tx_blobs_bundle.commitments)
.encode_field(&tx_blobs_bundle.proofs)
.finish();
buf.put_slice(&encoded_blobs);
}
pub fn rlp_length_as_pooled_tx(&self, blobs_bundle: &BlobsBundle) -> usize {
let mut buf = Vec::new();
self.rlp_encode_as_pooled_tx(&mut buf, blobs_bundle);
buf.len()
}
pub fn rlp_encode_as_pooled_tx_to_vec(&self, blobs_bundle: &BlobsBundle) -> Vec<u8> {
let mut buf = Vec::new();
self.rlp_encode_as_pooled_tx(&mut buf, blobs_bundle);
buf
}
}
impl RLPEncode for EIP7702Transaction {
fn encode(&self, buf: &mut dyn bytes::BufMut) {
Encoder::new(buf)
.encode_field(&self.chain_id)
.encode_field(&self.nonce)
.encode_field(&self.max_priority_fee_per_gas)
.encode_field(&self.max_fee_per_gas)
.encode_field(&self.gas_limit)
.encode_field(&self.to)
.encode_field(&self.value)
.encode_field(&self.data)
.encode_field(&self.access_list)
.encode_field(&self.authorization_list)
.encode_field(&self.signature_y_parity)
.encode_field(&self.signature_r)
.encode_field(&self.signature_s)
.finish()
}
}
impl RLPEncode for PrivilegedL2Transaction {
fn encode(&self, buf: &mut dyn bytes::BufMut) {
Encoder::new(buf)
.encode_field(&self.chain_id)
.encode_field(&self.nonce)
.encode_field(&self.max_priority_fee_per_gas)
.encode_field(&self.max_fee_per_gas)
.encode_field(&self.gas_limit)
.encode_field(&self.to)
.encode_field(&self.value)
.encode_field(&self.data)
.encode_field(&self.access_list)
.encode_field(&self.from)
.finish()
}
}
impl PayloadRLPEncode for Transaction {
fn encode_payload(&self, buf: &mut dyn bytes::BufMut) {
match self {
Transaction::LegacyTransaction(tx) => tx.encode_payload(buf),
Transaction::EIP1559Transaction(tx) => tx.encode_payload(buf),
Transaction::EIP2930Transaction(tx) => tx.encode_payload(buf),
Transaction::EIP4844Transaction(tx) => tx.encode_payload(buf),
Transaction::EIP7702Transaction(tx) => tx.encode_payload(buf),
Transaction::PrivilegedL2Transaction(tx) => tx.encode_payload(buf),
}
}
}
impl PayloadRLPEncode for LegacyTransaction {
fn encode_payload(&self, buf: &mut dyn bytes::BufMut) {
Encoder::new(buf)
.encode_field(&self.nonce)
.encode_field(&self.gas_price)
.encode_field(&self.gas)
.encode_field(&self.to)
.encode_field(&self.value)
.encode_field(&self.data)
.finish();
}
}
impl PayloadRLPEncode for EIP1559Transaction {
fn encode_payload(&self, buf: &mut dyn bytes::BufMut) {
Encoder::new(buf)
.encode_field(&self.chain_id)
.encode_field(&self.nonce)
.encode_field(&self.max_priority_fee_per_gas)
.encode_field(&self.max_fee_per_gas)
.encode_field(&self.gas_limit)
.encode_field(&self.to)
.encode_field(&self.value)
.encode_field(&self.data)
.encode_field(&self.access_list)
.finish();
}
}
impl PayloadRLPEncode for EIP2930Transaction {
fn encode_payload(&self, buf: &mut dyn bytes::BufMut) {
Encoder::new(buf)
.encode_field(&self.chain_id)
.encode_field(&self.nonce)
.encode_field(&self.gas_price)
.encode_field(&self.gas_limit)
.encode_field(&self.to)
.encode_field(&self.value)
.encode_field(&self.data)
.encode_field(&self.access_list)
.finish();
}
}
impl PayloadRLPEncode for EIP4844Transaction {
fn encode_payload(&self, buf: &mut dyn bytes::BufMut) {
Encoder::new(buf)
.encode_field(&self.chain_id)
.encode_field(&self.nonce)
.encode_field(&self.max_priority_fee_per_gas)
.encode_field(&self.max_fee_per_gas)
.encode_field(&self.gas)
.encode_field(&self.to)
.encode_field(&self.value)
.encode_field(&self.data)
.encode_field(&self.access_list)
.encode_field(&self.max_fee_per_blob_gas)
.encode_field(&self.blob_versioned_hashes)
.finish();
}
}
impl PayloadRLPEncode for EIP7702Transaction {
fn encode_payload(&self, buf: &mut dyn bytes::BufMut) {
Encoder::new(buf)
.encode_field(&self.chain_id)
.encode_field(&self.nonce)
.encode_field(&self.max_priority_fee_per_gas)
.encode_field(&self.max_fee_per_gas)
.encode_field(&self.gas_limit)
.encode_field(&self.to)
.encode_field(&self.value)
.encode_field(&self.data)
.encode_field(&self.access_list)
.encode_field(&self.authorization_list)
.finish();
}
}
impl PayloadRLPEncode for PrivilegedL2Transaction {
fn encode_payload(&self, buf: &mut dyn bytes::BufMut) {
Encoder::new(buf)
.encode_field(&self.chain_id)
.encode_field(&self.nonce)
.encode_field(&self.max_priority_fee_per_gas)
.encode_field(&self.max_fee_per_gas)
.encode_field(&self.gas_limit)
.encode_field(&self.to)
.encode_field(&self.value)
.encode_field(&self.data)
.encode_field(&self.access_list)
.encode_field(&self.from)
.finish();
}
}
impl RLPDecode for LegacyTransaction {
fn decode_unfinished(rlp: &[u8]) -> Result<(LegacyTransaction, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
let (nonce, decoder) = decoder.decode_field("nonce")?;
let (gas_price, decoder) = decoder.decode_field("gas_price")?;
let (gas, decoder) = decoder.decode_field("gas")?;
let (to, decoder) = decoder.decode_field("to")?;
let (value, decoder) = decoder.decode_field("value")?;
let (data, decoder) = decoder.decode_field("data")?;
let (v, decoder) = decoder.decode_field("v")?;
let (r, decoder) = decoder.decode_field("r")?;
let (s, decoder) = decoder.decode_field("s")?;
let inner_hash = OnceCell::new();
let tx = LegacyTransaction {
nonce,
gas_price,
gas,
to,
value,
data,
v,
r,
s,
inner_hash,
};
Ok((tx, decoder.finish()?))
}
}
impl RLPDecode for EIP2930Transaction {
fn decode_unfinished(rlp: &[u8]) -> Result<(EIP2930Transaction, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
let (chain_id, decoder) = decoder.decode_field("chain_id")?;
let (nonce, decoder) = decoder.decode_field("nonce")?;
let (gas_price, decoder) = decoder.decode_field("gas_price")?;
let (gas_limit, decoder) = decoder.decode_field("gas_limit")?;
let (to, decoder) = decoder.decode_field("to")?;
let (value, decoder) = decoder.decode_field("value")?;
let (data, decoder) = decoder.decode_field("data")?;
let (access_list, decoder) = decoder.decode_field("access_list")?;
let (signature_y_parity, decoder) = decoder.decode_field("signature_y_parity")?;
let (signature_r, decoder) = decoder.decode_field("signature_r")?;
let (signature_s, decoder) = decoder.decode_field("signature_s")?;
let inner_hash = OnceCell::new();
let tx = EIP2930Transaction {
chain_id,
nonce,
gas_price,
gas_limit,
to,
value,
data,
access_list,
signature_y_parity,
signature_r,
signature_s,
inner_hash,
};
Ok((tx, decoder.finish()?))
}
}
impl RLPDecode for EIP1559Transaction {
fn decode_unfinished(rlp: &[u8]) -> Result<(EIP1559Transaction, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
let (chain_id, decoder) = decoder.decode_field("chain_id")?;
let (nonce, decoder) = decoder.decode_field("nonce")?;
let (max_priority_fee_per_gas, decoder) =
decoder.decode_field("max_priority_fee_per_gas")?;
let (max_fee_per_gas, decoder) = decoder.decode_field("max_fee_per_gas")?;
let (gas_limit, decoder) = decoder.decode_field("gas_limit")?;
let (to, decoder) = decoder.decode_field("to")?;
let (value, decoder) = decoder.decode_field("value")?;
let (data, decoder) = decoder.decode_field("data")?;
let (access_list, decoder) = decoder.decode_field("access_list")?;
let (signature_y_parity, decoder) = decoder.decode_field("signature_y_parity")?;
let (signature_r, decoder) = decoder.decode_field("signature_r")?;
let (signature_s, decoder) = decoder.decode_field("signature_s")?;
let inner_hash = OnceCell::new();
let tx = EIP1559Transaction {
chain_id,
nonce,
max_priority_fee_per_gas,
max_fee_per_gas,
gas_limit,
to,
value,
data,
access_list,
signature_y_parity,
signature_r,
signature_s,
inner_hash,
};
Ok((tx, decoder.finish()?))
}
}
impl RLPDecode for EIP4844Transaction {
fn decode_unfinished(rlp: &[u8]) -> Result<(EIP4844Transaction, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
let (chain_id, decoder) = decoder.decode_field("chain_id")?;
let (nonce, decoder) = decoder.decode_field("nonce")?;
let (max_priority_fee_per_gas, decoder) =
decoder.decode_field("max_priority_fee_per_gas")?;
let (max_fee_per_gas, decoder) = decoder.decode_field("max_fee_per_gas")?;
let (gas, decoder) = decoder.decode_field("gas")?;
let (to, decoder) = decoder.decode_field("to")?;
let (value, decoder) = decoder.decode_field("value")?;
let (data, decoder) = decoder.decode_field("data")?;
let (access_list, decoder) = decoder.decode_field("access_list")?;
let (max_fee_per_blob_gas, decoder) = decoder.decode_field("max_fee_per_blob_gas")?;
let (blob_versioned_hashes, decoder) = decoder.decode_field("blob_versioned_hashes")?;
let (signature_y_parity, decoder) = decoder.decode_field("signature_y_parity")?;
let (signature_r, decoder) = decoder.decode_field("signature_r")?;
let (signature_s, decoder) = decoder.decode_field("signature_s")?;
let inner_hash = OnceCell::new();
let tx = EIP4844Transaction {
chain_id,
nonce,
max_priority_fee_per_gas,
max_fee_per_gas,
gas,
to,
value,
data,
access_list,
max_fee_per_blob_gas,
blob_versioned_hashes,
signature_y_parity,
signature_r,
signature_s,
inner_hash,
};
Ok((tx, decoder.finish()?))
}
}
impl RLPDecode for EIP7702Transaction {
fn decode_unfinished(rlp: &[u8]) -> Result<(EIP7702Transaction, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
let (chain_id, decoder) = decoder.decode_field("chain_id")?;
let (nonce, decoder) = decoder.decode_field("nonce")?;
let (max_priority_fee_per_gas, decoder) =
decoder.decode_field("max_priority_fee_per_gas")?;
let (max_fee_per_gas, decoder) = decoder.decode_field("max_fee_per_gas")?;
let (gas_limit, decoder) = decoder.decode_field("gas_limit")?;
let (to, decoder) = decoder.decode_field("to")?;
let (value, decoder) = decoder.decode_field("value")?;
let (data, decoder) = decoder.decode_field("data")?;
let (access_list, decoder) = decoder.decode_field("access_list")?;
let (authorization_list, decoder) = decoder.decode_field("authorization_list")?;
let (signature_y_parity, decoder) = decoder.decode_field("signature_y_parity")?;
let (signature_r, decoder) = decoder.decode_field("signature_r")?;
let (signature_s, decoder) = decoder.decode_field("signature_s")?;
let inner_hash = OnceCell::new();
let tx = EIP7702Transaction {
chain_id,
nonce,
max_priority_fee_per_gas,
max_fee_per_gas,
gas_limit,
to,
value,
data,
access_list,
authorization_list,
signature_y_parity,
signature_r,
signature_s,
inner_hash,
};
Ok((tx, decoder.finish()?))
}
}
impl RLPDecode for PrivilegedL2Transaction {
fn decode_unfinished(rlp: &[u8]) -> Result<(PrivilegedL2Transaction, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
let (chain_id, decoder) = decoder.decode_field("chain_id")?;
let (nonce, decoder) = decoder.decode_field("nonce")?;
let (max_priority_fee_per_gas, decoder) =
decoder.decode_field("max_priority_fee_per_gas")?;
let (max_fee_per_gas, decoder) = decoder.decode_field("max_fee_per_gas")?;
let (gas_limit, decoder) = decoder.decode_field::<u64>("gas_limit")?;
let (to, decoder) = decoder.decode_field("to")?;
let (value, decoder) = decoder.decode_field("value")?;
let (data, decoder) = decoder.decode_field("data")?;
let (access_list, decoder) = decoder.decode_field("access_list")?;
let (from, decoder) = decoder.decode_field("from")?;
let inner_hash = OnceCell::new();
let tx = PrivilegedL2Transaction {
chain_id,
nonce,
max_priority_fee_per_gas,
max_fee_per_gas,
gas_limit,
to,
value,
data,
access_list,
from,
inner_hash,
};
Ok((tx, decoder.finish()?))
}
}
impl Transaction {
pub fn sender(&self) -> Result<Address, secp256k1::Error> {
match self {
Transaction::LegacyTransaction(tx) => {
let signature_y_parity = match self.chain_id() {
Some(chain_id) => tx.v.as_u64().saturating_sub(35 + chain_id * 2) != 0,
None => tx.v.as_u64().saturating_sub(27) != 0,
};
let mut buf = vec![];
match self.chain_id() {
None => Encoder::new(&mut buf)
.encode_field(&tx.nonce)
.encode_field(&tx.gas_price)
.encode_field(&tx.gas)
.encode_field(&tx.to)
.encode_field(&tx.value)
.encode_field(&tx.data)
.finish(),
Some(chain_id) => Encoder::new(&mut buf)
.encode_field(&tx.nonce)
.encode_field(&tx.gas_price)
.encode_field(&tx.gas)
.encode_field(&tx.to)
.encode_field(&tx.value)
.encode_field(&tx.data)
.encode_field(&chain_id)
.encode_field(&0u8)
.encode_field(&0u8)
.finish(),
}
let mut sig = [0u8; 65];
sig[..32].copy_from_slice(&tx.r.to_big_endian());
sig[32..64].copy_from_slice(&tx.s.to_big_endian());
sig[64] = signature_y_parity as u8;
recover_address_from_message(Signature::from_slice(&sig), &Bytes::from(buf))
}
Transaction::EIP2930Transaction(tx) => {
let mut buf = vec![self.tx_type() as u8];
Encoder::new(&mut buf)
.encode_field(&tx.chain_id)
.encode_field(&tx.nonce)
.encode_field(&tx.gas_price)
.encode_field(&tx.gas_limit)
.encode_field(&tx.to)
.encode_field(&tx.value)
.encode_field(&tx.data)
.encode_field(&tx.access_list)
.finish();
let mut sig = [0u8; 65];
sig[..32].copy_from_slice(&tx.signature_r.to_big_endian());
sig[32..64].copy_from_slice(&tx.signature_s.to_big_endian());
sig[64] = tx.signature_y_parity as u8;
recover_address_from_message(Signature::from_slice(&sig), &Bytes::from(buf))
}
Transaction::EIP1559Transaction(tx) => {
let mut buf = vec![self.tx_type() as u8];
Encoder::new(&mut buf)
.encode_field(&tx.chain_id)
.encode_field(&tx.nonce)
.encode_field(&tx.max_priority_fee_per_gas)