reth_codecs/alloy/transaction/
eip2930.rs

1//! Compact implementation for [`AlloyTxEip2930`]
2
3use crate::Compact;
4use alloy_consensus::TxEip2930 as AlloyTxEip2930;
5use alloy_eips::eip2930::AccessList;
6use alloy_primitives::{Bytes, ChainId, TxKind, U256};
7use reth_codecs_derive::add_arbitrary_tests;
8
9/// Transaction with an [`AccessList`] ([EIP-2930](https://eips.ethereum.org/EIPS/eip-2930)).
10///
11/// This is a helper type to use derive on it instead of manually managing `bitfield`.
12///
13/// By deriving `Compact` here, any future changes or enhancements to the `Compact` derive
14/// will automatically apply to this type.
15///
16/// Notice: Make sure this struct is 1:1 with [`alloy_consensus::TxEip2930`]
17#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)]
18#[reth_codecs(crate = "crate")]
19#[cfg_attr(
20    any(test, feature = "test-utils"),
21    derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
22)]
23#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
24#[add_arbitrary_tests(crate, compact)]
25pub(crate) struct TxEip2930 {
26    chain_id: ChainId,
27    nonce: u64,
28    gas_price: u128,
29    gas_limit: u64,
30    to: TxKind,
31    value: U256,
32    access_list: AccessList,
33    input: Bytes,
34}
35
36impl Compact for AlloyTxEip2930 {
37    fn to_compact<B>(&self, buf: &mut B) -> usize
38    where
39        B: bytes::BufMut + AsMut<[u8]>,
40    {
41        let tx = TxEip2930 {
42            chain_id: self.chain_id,
43            nonce: self.nonce,
44            gas_price: self.gas_price,
45            gas_limit: self.gas_limit,
46            to: self.to,
47            value: self.value,
48            access_list: self.access_list.clone(),
49            input: self.input.clone(),
50        };
51        tx.to_compact(buf)
52    }
53
54    fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
55        let (tx, _) = TxEip2930::from_compact(buf, len);
56        let alloy_tx = Self {
57            chain_id: tx.chain_id,
58            nonce: tx.nonce,
59            gas_price: tx.gas_price,
60            gas_limit: tx.gas_limit,
61            to: tx.to,
62            value: tx.value,
63            access_list: tx.access_list,
64            input: tx.input,
65        };
66        (alloy_tx, buf)
67    }
68}