reth_codecs/alloy/transaction/
legacy.rs

1//! Compact implementation for [`AlloyTxLegacy`]
2
3use crate::Compact;
4use alloy_consensus::TxLegacy as AlloyTxLegacy;
5use alloy_primitives::{Bytes, ChainId, TxKind, U256};
6
7/// Legacy transaction.
8#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)]
9#[reth_codecs(crate = "crate")]
10#[cfg_attr(
11    any(test, feature = "test-utils"),
12    derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize),
13    crate::add_arbitrary_tests(crate, compact)
14)]
15#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
16pub(crate) struct TxLegacy {
17    /// Added as EIP-155: Simple replay attack protection
18    chain_id: Option<ChainId>,
19    /// A scalar value equal to the number of transactions sent by the sender; formally Tn.
20    nonce: u64,
21    /// A scalar value equal to the number of
22    /// Wei to be paid per unit of gas for all computation
23    /// costs incurred as a result of the execution of this transaction; formally Tp.
24    ///
25    /// As ethereum circulation is around 120mil eth as of 2022 that is around
26    /// 120000000000000000000000000 wei we are safe to use u128 as its max number is:
27    /// 340282366920938463463374607431768211455
28    gas_price: u128,
29    /// A scalar value equal to the maximum
30    /// amount of gas that should be used in executing
31    /// this transaction. This is paid up-front, before any
32    /// computation is done and may not be increased
33    /// later; formally Tg.
34    gas_limit: u64,
35    /// The 160-bit address of the message call’s recipient or, for a contract creation
36    /// transaction, ∅, used here to denote the only member of B0 ; formally Tt.
37    to: TxKind,
38    /// A scalar value equal to the number of Wei to
39    /// be transferred to the message call’s recipient or,
40    /// in the case of contract creation, as an endowment
41    /// to the newly created account; formally Tv.
42    value: U256,
43    /// Input has two uses depending if transaction is Create or Call (if `to` field is None or
44    /// Some). pub init: An unlimited size byte array specifying the
45    /// EVM-code for the account initialisation procedure CREATE,
46    /// data: An unlimited size byte array specifying the
47    /// input data of the message call, formally Td.
48    input: Bytes,
49}
50
51impl Compact for AlloyTxLegacy {
52    fn to_compact<B>(&self, buf: &mut B) -> usize
53    where
54        B: bytes::BufMut + AsMut<[u8]>,
55    {
56        let tx = TxLegacy {
57            chain_id: self.chain_id,
58            nonce: self.nonce,
59            gas_price: self.gas_price,
60            gas_limit: self.gas_limit,
61            to: self.to,
62            value: self.value,
63            input: self.input.clone(),
64        };
65
66        tx.to_compact(buf)
67    }
68
69    fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
70        let (tx, _) = TxLegacy::from_compact(buf, len);
71
72        let alloy_tx = Self {
73            chain_id: tx.chain_id,
74            nonce: tx.nonce,
75            gas_price: tx.gas_price,
76            gas_limit: tx.gas_limit,
77            to: tx.to,
78            value: tx.value,
79            input: tx.input,
80        };
81
82        (alloy_tx, buf)
83    }
84}