alloy_consensus/transaction/
pooled.rs

1//! Defines the exact transaction variant that is allowed to be propagated over the eth p2p
2//! protocol.
3
4use super::EthereumTxEnvelope;
5use crate::{error::ValueError, Signed, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar};
6use alloy_eips::eip7594::Encodable7594;
7
8/// All possible transactions that can be included in a response to `GetPooledTransactions`.
9/// A response to `GetPooledTransactions`. This can include either a blob transaction, or a
10/// non-4844 signed transaction.
11///
12/// The difference between this and the [`EthereumTxEnvelope<TxEip4844Variant<T>>`] is that this
13/// type always requires the [`TxEip4844WithSidecar`] variant, because EIP-4844 transaction can only
14/// be propagated with the sidecar over p2p.
15pub type PooledTransaction = EthereumTxEnvelope<TxEip4844WithSidecar>;
16
17impl<T: Encodable7594> EthereumTxEnvelope<TxEip4844WithSidecar<T>> {
18    /// Converts the transaction into [`EthereumTxEnvelope<TxEip4844Variant<T>>`].
19    pub fn into_envelope(self) -> EthereumTxEnvelope<TxEip4844Variant<T>> {
20        match self {
21            Self::Legacy(tx) => tx.into(),
22            Self::Eip2930(tx) => tx.into(),
23            Self::Eip1559(tx) => tx.into(),
24            Self::Eip7702(tx) => tx.into(),
25            Self::Eip4844(tx) => tx.into(),
26        }
27    }
28}
29
30impl<T: Encodable7594> TryFrom<Signed<TxEip4844Variant<T>>>
31    for EthereumTxEnvelope<TxEip4844WithSidecar<T>>
32{
33    type Error = ValueError<Signed<TxEip4844Variant<T>>>;
34
35    fn try_from(value: Signed<TxEip4844Variant<T>>) -> Result<Self, Self::Error> {
36        let (value, signature, hash) = value.into_parts();
37        match value {
38            tx @ TxEip4844Variant::TxEip4844(_) => Err(ValueError::new_static(
39                Signed::new_unchecked(tx, signature, hash),
40                "pooled transaction requires 4844 sidecar",
41            )),
42            TxEip4844Variant::TxEip4844WithSidecar(tx) => {
43                Ok(Signed::new_unchecked(tx, signature, hash).into())
44            }
45        }
46    }
47}
48
49impl<T: Encodable7594> TryFrom<EthereumTxEnvelope<TxEip4844Variant<T>>>
50    for EthereumTxEnvelope<TxEip4844WithSidecar<T>>
51{
52    type Error = ValueError<EthereumTxEnvelope<TxEip4844Variant<T>>>;
53
54    fn try_from(value: EthereumTxEnvelope<TxEip4844Variant<T>>) -> Result<Self, Self::Error> {
55        value.try_into_pooled()
56    }
57}
58
59impl<T: Encodable7594> TryFrom<EthereumTxEnvelope<TxEip4844>>
60    for EthereumTxEnvelope<TxEip4844WithSidecar<T>>
61{
62    type Error = ValueError<EthereumTxEnvelope<TxEip4844>>;
63
64    fn try_from(value: EthereumTxEnvelope<TxEip4844>) -> Result<Self, Self::Error> {
65        value.try_into_pooled()
66    }
67}
68
69impl<T: Encodable7594> From<EthereumTxEnvelope<TxEip4844WithSidecar<T>>>
70    for EthereumTxEnvelope<TxEip4844Variant<T>>
71{
72    fn from(tx: EthereumTxEnvelope<TxEip4844WithSidecar<T>>) -> Self {
73        tx.into_envelope()
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80    use crate::Transaction;
81    use alloy_eips::{Decodable2718, Encodable2718};
82    use alloy_primitives::{address, hex, Bytes};
83    use alloy_rlp::Decodable;
84    use std::path::PathBuf;
85
86    #[test]
87    fn invalid_legacy_pooled_decoding_input_too_short() {
88        let input_too_short = [
89            // this should fail because the payload length is longer than expected
90            &hex!("d90b0280808bc5cd028083c5cdfd9e407c56565656")[..],
91            // these should fail decoding
92            //
93            // The `c1` at the beginning is a list header, and the rest is a valid legacy
94            // transaction, BUT the payload length of the list header is 1, and the payload is
95            // obviously longer than one byte.
96            &hex!("c10b02808083c5cd028883c5cdfd9e407c56565656"),
97            &hex!("c10b0280808bc5cd028083c5cdfd9e407c56565656"),
98            // this one is 19 bytes, and the buf is long enough, but the transaction will not
99            // consume that many bytes.
100            &hex!("d40b02808083c5cdeb8783c5acfd9e407c5656565656"),
101            &hex!("d30102808083c5cd02887dc5cdfd9e64fd9e407c56"),
102        ];
103
104        for hex_data in &input_too_short {
105            let input_rlp = &mut &hex_data[..];
106            let res = PooledTransaction::decode(input_rlp);
107
108            assert!(
109                res.is_err(),
110                "expected err after decoding rlp input: {:x?}",
111                Bytes::copy_from_slice(hex_data)
112            );
113
114            // this is a legacy tx so we can attempt the same test with decode_enveloped
115            let input_rlp = &mut &hex_data[..];
116            let res = PooledTransaction::decode_2718(input_rlp);
117
118            assert!(
119                res.is_err(),
120                "expected err after decoding enveloped rlp input: {:x?}",
121                Bytes::copy_from_slice(hex_data)
122            );
123        }
124    }
125
126    // <https://holesky.etherscan.io/tx/0x7f60faf8a410a80d95f7ffda301d5ab983545913d3d789615df3346579f6c849>
127    #[test]
128    fn decode_eip1559_enveloped() {
129        let data = hex!("02f903d382426882ba09832dc6c0848674742682ed9694714b6a4ea9b94a8a7d9fd362ed72630688c8898c80b90364492d24749189822d8512430d3f3ff7a2ede675ac08265c08e2c56ff6fdaa66dae1cdbe4a5d1d7809f3e99272d067364e597542ac0c369d69e22a6399c3e9bee5da4b07e3f3fdc34c32c3d88aa2268785f3e3f8086df0934b10ef92cfffc2e7f3d90f5e83302e31382e302d64657600000000000000000000000000000000000000000000569e75fc77c1a856f6daaf9e69d8a9566ca34aa47f9133711ce065a571af0cfd000000000000000000000000e1e210594771824dad216568b91c9cb4ceed361c00000000000000000000000000000000000000000000000000000000000546e00000000000000000000000000000000000000000000000000000000000e4e1c00000000000000000000000000000000000000000000000000000000065d6750c00000000000000000000000000000000000000000000000000000000000f288000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002cf600000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000000f1628e56fa6d8c50e5b984a58c0df14de31c7b857ce7ba499945b99252976a93d06dcda6776fc42167fbe71cb59f978f5ef5b12577a90b132d14d9c6efa528076f0161d7bf03643cfc5490ec5084f4a041db7f06c50bd97efa08907ba79ddcac8b890f24d12d8db31abbaaf18985d54f400449ee0559a4452afe53de5853ce090000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000003e800000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000064ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000c080a01428023fc54a27544abc421d5d017b9a7c5936ad501cbdecd0d9d12d04c1a033a0753104bbf1c87634d6ff3f0ffa0982710612306003eb022363b57994bdef445a"
130);
131
132        let res = PooledTransaction::decode_2718(&mut &data[..]).unwrap();
133        assert_eq!(res.to(), Some(address!("714b6a4ea9b94a8a7d9fd362ed72630688c8898c")));
134    }
135
136    #[test]
137    fn legacy_valid_pooled_decoding() {
138        // d3 <- payload length, d3 - c0 = 0x13 = 19
139        // 0b <- nonce
140        // 02 <- gas_price
141        // 80 <- gas_limit
142        // 80 <- to (Create)
143        // 83 c5cdeb <- value
144        // 87 83c5acfd9e407c <- input
145        // 56 <- v (eip155, so modified with a chain id)
146        // 56 <- r
147        // 56 <- s
148        let data = &hex!("d30b02808083c5cdeb8783c5acfd9e407c565656")[..];
149
150        let input_rlp = &mut &data[..];
151        let res = PooledTransaction::decode(input_rlp);
152        assert!(res.is_ok());
153        assert!(input_rlp.is_empty());
154
155        // we can also decode_enveloped
156        let res = PooledTransaction::decode_2718(&mut &data[..]);
157        assert!(res.is_ok());
158    }
159
160    #[test]
161    fn decode_encode_raw_4844_rlp() {
162        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/4844rlp");
163        let dir = std::fs::read_dir(path).expect("Unable to read folder");
164        for entry in dir {
165            let entry = entry.unwrap();
166            let content = std::fs::read_to_string(entry.path()).unwrap();
167            let raw = hex::decode(content.trim()).unwrap();
168            let tx = PooledTransaction::decode_2718(&mut raw.as_ref())
169                .map_err(|err| {
170                    panic!("Failed to decode transaction: {:?} {:?}", err, entry.path());
171                })
172                .unwrap();
173            // We want to test only EIP-4844 transactions
174            assert!(tx.is_eip4844());
175            let encoded = tx.encoded_2718();
176            assert_eq!(encoded.as_slice(), &raw[..], "{:?}", entry.path());
177        }
178    }
179
180    #[test]
181    #[cfg(feature = "kzg")]
182    fn convert_to_eip7594() {
183        let kzg_settings = alloy_eips::eip4844::env_settings::EnvKzgSettings::default();
184        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata/4844rlp");
185        let dir = std::fs::read_dir(path).expect("Unable to read folder");
186        for entry in dir {
187            let entry = entry.unwrap();
188            let content = std::fs::read_to_string(entry.path()).unwrap();
189            let raw = hex::decode(content.trim()).unwrap();
190            let PooledTransaction::Eip4844(tx) = PooledTransaction::decode_2718(&mut raw.as_ref())
191                .map_err(|err| {
192                    panic!("Failed to decode transaction: {:?} {:?}", err, entry.path());
193                })
194                .unwrap()
195            else {
196                panic!("Expected EIP-4844 transaction");
197            };
198            let tx = tx.into_parts().0;
199            assert!(!tx.sidecar.blobs.is_empty());
200            assert!(tx.validate_blob(kzg_settings.get()).is_ok());
201
202            let tx =
203                tx.try_map_sidecar(|sidecar| sidecar.try_into_7594(kzg_settings.get())).unwrap();
204
205            assert!(!tx.sidecar.blobs.is_empty());
206            assert!(tx.validate_blob(kzg_settings.get()).is_ok());
207        }
208    }
209}