eip7702: use alloy TxEip7702 (#10617)

This commit is contained in:
Thomas Coratger
2024-09-02 11:16:25 -07:00
committed by GitHub
parent aa42dbcf30
commit 659375d776
8 changed files with 195 additions and 411 deletions

View File

@ -0,0 +1,71 @@
use crate::Compact;
use alloy_consensus::transaction::TxEip7702 as AlloyTxEip7702;
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{Bytes, ChainId, TxKind, U256};
use reth_codecs_derive::add_arbitrary_tests;
use serde::{Deserialize, Serialize};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
/// [EIP-7702 Set Code Transaction](https://eips.ethereum.org/EIPS/eip-7702)
///
/// 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::TxEip7702`]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize, Compact)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
#[add_arbitrary_tests(compact)]
pub(crate) struct TxEip7702 {
chain_id: ChainId,
nonce: u64,
gas_limit: u64,
max_fee_per_gas: u128,
max_priority_fee_per_gas: u128,
to: TxKind,
value: U256,
access_list: AccessList,
authorization_list: Vec<SignedAuthorization>,
input: Bytes,
}
impl Compact for AlloyTxEip7702 {
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
let tx = TxEip7702 {
chain_id: self.chain_id,
nonce: self.nonce,
max_fee_per_gas: self.max_fee_per_gas,
max_priority_fee_per_gas: self.max_priority_fee_per_gas,
gas_limit: self.gas_limit as u64,
to: self.to,
value: self.value,
input: self.input.clone(),
access_list: self.access_list.clone(),
authorization_list: self.authorization_list.clone(),
};
tx.to_compact(buf)
}
fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
let (tx, _) = TxEip7702::from_compact(buf, len);
let alloy_tx = Self {
chain_id: tx.chain_id,
nonce: tx.nonce,
max_fee_per_gas: tx.max_fee_per_gas,
max_priority_fee_per_gas: tx.max_priority_fee_per_gas,
gas_limit: tx.gas_limit as u128,
to: tx.to,
value: tx.value,
input: tx.input,
access_list: tx.access_list,
authorization_list: tx.authorization_list,
};
(alloy_tx, buf)
}
}

View File

@ -1,6 +1,7 @@
mod eip1559;
mod eip2930;
mod eip4844;
mod eip7702;
mod legacy;
#[cfg(test)]
@ -12,7 +13,10 @@ 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, eip2930::TxEip2930, eip4844::TxEip4844, legacy::TxLegacy};
use super::{
eip1559::TxEip1559, eip2930::TxEip2930, eip4844::TxEip4844, eip7702::TxEip7702,
legacy::TxLegacy,
};
#[test]
fn test_ensure_backwards_compatibility() {
@ -20,5 +24,6 @@ mod tests {
assert_eq!(TxLegacy::bitflag_encoded_bytes(), 3);
assert_eq!(TxEip1559::bitflag_encoded_bytes(), 4);
assert_eq!(TxEip2930::bitflag_encoded_bytes(), 3);
assert_eq!(TxEip7702::bitflag_encoded_bytes(), 4);
}
}