eip2930: use alloy TxEip2930 (#10623)

This commit is contained in:
Thomas Coratger
2024-09-01 06:53:19 -07:00
committed by GitHub
parent 7269cf2853
commit 578b52afd0
9 changed files with 123 additions and 311 deletions

View File

@ -0,0 +1,62 @@
use crate::Compact;
use alloy_consensus::transaction::TxEip2930 as AlloyTxEip2930;
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{Bytes, ChainId, TxKind, U256};
use reth_codecs_derive::add_arbitrary_tests;
use serde::{Deserialize, Serialize};
/// Transaction with an [`AccessList`] ([EIP-2930](https://eips.ethereum.org/EIPS/eip-2930)).
///
/// This is a helper type to use derive on it instead of manually managing `bitfield`.
///
/// By deriving `Compact` here, any future changes or enhancements to the `Compact` derive
/// will automatically apply to this type.
///
/// Notice: Make sure this struct is 1:1 with [`alloy_consensus::transaction::TxEip2930`]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize, Compact)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
#[add_arbitrary_tests(compact)]
pub(crate) struct TxEip2930 {
chain_id: ChainId,
nonce: u64,
gas_price: u128,
gas_limit: u64,
to: TxKind,
value: U256,
access_list: AccessList,
input: Bytes,
}
impl Compact for AlloyTxEip2930 {
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
let tx = TxEip2930 {
chain_id: self.chain_id,
nonce: self.nonce,
gas_price: self.gas_price,
gas_limit: self.gas_limit as u64,
to: self.to,
value: self.value,
access_list: self.access_list.clone(),
input: self.input.clone(),
};
tx.to_compact(buf)
}
fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
let (tx, _) = TxEip2930::from_compact(buf, len);
let alloy_tx = Self {
chain_id: tx.chain_id,
nonce: tx.nonce,
gas_price: tx.gas_price,
gas_limit: tx.gas_limit as u128,
to: tx.to,
value: tx.value,
access_list: tx.access_list,
input: tx.input,
};
(alloy_tx, buf)
}
}

View File

@ -1,4 +1,5 @@
mod eip1559;
mod eip2930;
mod legacy;
#[cfg(test)]
@ -10,11 +11,12 @@ mod tests {
// this check is to ensure we do not inadvertently add too many fields to a struct which would
// expand the flags field and break backwards compatibility
use super::{eip1559::TxEip1559, legacy::TxLegacy};
use super::{eip1559::TxEip1559, eip2930::TxEip2930, legacy::TxLegacy};
#[test]
fn test_ensure_backwards_compatibility() {
assert_eq!(TxLegacy::bitflag_encoded_bytes(), 3);
assert_eq!(TxEip1559::bitflag_encoded_bytes(), 4);
assert_eq!(TxEip2930::bitflag_encoded_bytes(), 3);
}
}