reth_codecs/alloy/transaction/
mod.rs

1//! Compact implementation for transaction types
2use crate::Compact;
3use alloy_consensus::{
4    transaction::{RlpEcdsaEncodableTx, TxEip1559, TxEip2930, TxEip7702, TxLegacy},
5    EthereumTypedTransaction, TxType,
6};
7use alloy_primitives::bytes::BufMut;
8
9impl<Eip4844> Compact for EthereumTypedTransaction<Eip4844>
10where
11    Eip4844: Compact + RlpEcdsaEncodableTx,
12{
13    fn to_compact<B>(&self, buf: &mut B) -> usize
14    where
15        B: BufMut + AsMut<[u8]>,
16    {
17        let identifier = self.tx_type().to_compact(buf);
18        match self {
19            Self::Legacy(tx) => tx.to_compact(buf),
20            Self::Eip2930(tx) => tx.to_compact(buf),
21            Self::Eip1559(tx) => tx.to_compact(buf),
22            Self::Eip4844(tx) => tx.to_compact(buf),
23            Self::Eip7702(tx) => tx.to_compact(buf),
24        };
25        identifier
26    }
27
28    fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8]) {
29        let (tx_type, buf) = TxType::from_compact(buf, identifier);
30
31        match tx_type {
32            TxType::Legacy => {
33                let (tx, buf) = TxLegacy::from_compact(buf, buf.len());
34                (Self::Legacy(tx), buf)
35            }
36            TxType::Eip4844 => {
37                let (tx, buf) = Eip4844::from_compact(buf, buf.len());
38                (Self::Eip4844(tx), buf)
39            }
40            TxType::Eip7702 => {
41                let (tx, buf) = TxEip7702::from_compact(buf, buf.len());
42                (Self::Eip7702(tx), buf)
43            }
44            TxType::Eip1559 => {
45                let (tx, buf) = TxEip1559::from_compact(buf, buf.len());
46                (Self::Eip1559(tx), buf)
47            }
48            TxType::Eip2930 => {
49                let (tx, buf) = TxEip2930::from_compact(buf, buf.len());
50                (Self::Eip2930(tx), buf)
51            }
52        }
53    }
54}
55
56cond_mod!(eip1559, eip2930, eip4844, eip7702, legacy, txtype);
57
58mod ethereum;
59#[cfg(all(feature = "test-utils", feature = "op"))]
60pub mod optimism;
61#[cfg(all(not(feature = "test-utils"), feature = "op"))]
62mod optimism;
63
64#[cfg(test)]
65mod tests {
66
67    // each value in the database has an extra field named flags that encodes metadata about other
68    // fields in the value, e.g. offset and length.
69    //
70    // this check is to ensure we do not inadvertently add too many fields to a struct which would
71    // expand the flags field and break backwards compatibility
72
73    use crate::{
74        alloy::{
75            header::Header,
76            transaction::{
77                eip1559::TxEip1559, eip2930::TxEip2930, eip4844::TxEip4844, eip7702::TxEip7702,
78                legacy::TxLegacy,
79            },
80        },
81        test_utils::test_decode,
82    };
83    use alloy_primitives::hex;
84
85    #[test]
86    fn test_ensure_backwards_compatibility() {
87        assert_eq!(TxEip4844::bitflag_encoded_bytes(), 5);
88        assert_eq!(TxLegacy::bitflag_encoded_bytes(), 3);
89        assert_eq!(TxEip1559::bitflag_encoded_bytes(), 4);
90        assert_eq!(TxEip2930::bitflag_encoded_bytes(), 3);
91        assert_eq!(TxEip7702::bitflag_encoded_bytes(), 4);
92    }
93
94    #[cfg(feature = "op")]
95    #[test]
96    fn test_ensure_backwards_compatibility_optimism() {
97        assert_eq!(crate::alloy::transaction::optimism::TxDeposit::bitflag_encoded_bytes(), 2);
98    }
99
100    #[test]
101    fn test_decode_header() {
102        test_decode::<Header>(&hex!(
103            "01000000fbbb564baeafd064b979c2ac032df5cd987098066a8c6969514dfb8ecfbf043e667fa19efcc00d1dd197c309a3cc42dec820cd627af8f7f38f3274f842406891b22624431d0ea858422db8415b1181f8d19befbd21287debaf98a94e84b3ec20be846f35abfbf743ee3eda4fdda6a6f9124d295da97e26eaa1cedd09936f0a3c560b6bc10316dba5e82abd21afcf519a985feb09a6ce7fba2e8163b10f06c99828b8049c29b993d88d1d112dca60a03ebd8ebc6d69a7e1f301ca6d67c21fe0949d67bca251edf36c96a2cf7c84d98fc60a53988ac95820f434eb35280d98c8ba4d7484e7ee8fefd63591ad4c937ccaaea23871d05c77bac754c5759b34cf9b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
104        ));
105    }
106
107    #[test]
108    fn test_decode_eip1559() {
109        test_decode::<TxEip1559>(&hex!(
110            "88086110b81b05bc5bb59ec3e4cd44e895a9dcb2656d5003e2f64ecb2e15443898cc1cc19af19ca96fc2b4eafc4abc26e4bbd70a3ddb10b7530b65eea128f4095c97164f712c04239902c1b08acf3949d4687123cdd72d5c73df113d2dc6ed7e519f410ace5553ca805975240a208b57013532de78c5cb407423ea11921ab11b13e93ef35d4d01c9a23166c4d627987545fe4675528d0ab111b0a1dc83fba0a4e1cd5c826a94db3f"
111        ));
112    }
113
114    #[test]
115    fn test_decode_eip2930() {
116        test_decode::<TxEip2930>(&hex!(
117            "7810833fce14e3e2921e94fd3727eb71e91551d2c1e029697a654bfab510f3963aa57074015e152065d1c807f8830079fb0aeadc251d248eaec7147e78580ed638c4e667827775e24270edd5aad475776533ece65373afa71722bfeba3c900"
118        ));
119    }
120
121    #[test]
122    fn test_decode_eip4844() {
123        test_decode::<TxEip4844>(&hex!(
124            "88086110025c359180ea680b5007c856f9e1ad4d1be7a5019feb42133f4fc4bdf74da1b457ab787462385a28a1bf8edb401adabf3ff21ac18f695e30180348ea67246fc4dc25e88add12b7c317651a0ce08946d98dbbe5b38883aa758a0f247e23b0fe3ac1bcc43d7212c984d6ccc770d70135890c9a07d715cacb9032c90d539d0b3d209a8d600178bcfb416fd489e5d5dd56d9cfc6addae810ae70bdaee65672b871dc2b3f35ec00dbaa0d872f78cb58b3199984c608c8ba"
125        ));
126    }
127
128    #[test]
129    fn test_decode_eip7702() {
130        test_decode::<TxEip7702>(&hex!(
131            "8808210881415c034feba383d7a6efd3f2601309b33a6d682ad47168cac0f7a5c5136a33370e5e7ca7f570d5530d7a0d18bf5eac33583fdc27b6580f61e8cbd34d6de596f925c1f353188feb2c1e9e20de82a80b57f0be425d8c5896280d4f5f66cdcfba256d0c9ac8abd833859a62ec019501b4585fa176f048de4f88b93bdefecfcaf4d8f0dd04767bc683a4569c893632e44ba9d53f90d758125c9b24c0192a649166520cd5eecbc110b53eda400cf184b8ef9932c81d0deb2ea27dfa863392a87bfd53af3ec67379f20992501e76e387cbe3933861beead1b49649383cf8b2a2d5c6d04b7edc376981ed9b12cf7199fe7fabf5198659e001bed40922969b82a6cd000000000000"
132        ));
133    }
134
135    #[test]
136    fn test_decode_legacy() {
137        test_decode::<TxLegacy>(&hex!(
138            "112210080a8ba06a8d108540bb3140e9f71a0812c46226f9ea77ae880d98d19fe27e5911801175c3b32620b2e887af0296af343526e439b775ee3b1c06750058e9e5fc4cd5965c3010f86184"
139        ));
140    }
141
142    #[cfg(feature = "op")]
143    #[test]
144    fn test_decode_deposit() {
145        test_decode::<op_alloy_consensus::TxDeposit>(&hex!(
146            "8108ac8f15983d59b6ae4911a00ff7bfcd2e53d2950926f8c82c12afad02861c46fcb293e776204052725e1c08ff2e9ff602ca916357601fa972a14094891fe3598b718758f22c46f163c18bcaa6296ce87e5267ef3fd932112842fbbf79011548cdf067d93ce6098dfc0aaf5a94531e439f30d6dfd0c6"
147        ));
148    }
149}