feat: migrate to alloy TxEip1559 (#10262)

Co-authored-by: Arsenii Kulikov <klkvrr@gmail.com>
This commit is contained in:
Aurélien
2024-08-31 19:36:28 +02:00
committed by GitHub
parent 17f6225fa5
commit 3a49a552c7
14 changed files with 149 additions and 299 deletions

View File

@ -16,7 +16,7 @@ reth-codecs-derive = { path = "./derive", default-features = false }
# eth
alloy-consensus = { workspace = true, optional = true }
alloy-eips = { workspace = true, optional = true }
alloy-eips = { workspace = true, optional = true, features = ["serde"] }
alloy-genesis = { workspace = true, optional = true }
alloy-primitives.workspace = true
alloy-trie = { workspace = true, optional = true }

View File

@ -0,0 +1,67 @@
use crate::Compact;
use alloy_consensus::TxEip1559 as AlloyTxEip1559;
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{Bytes, ChainId, TxKind, U256};
use serde::{Deserialize, Serialize};
/// [EIP-1559 Transaction](https://eips.ethereum.org/EIPS/eip-1559)
///
/// 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::TxEip1559`]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Compact, Default, Serialize, Deserialize)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
#[cfg_attr(test, crate::add_arbitrary_tests(compact))]
pub(crate) struct TxEip1559 {
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,
input: Bytes,
}
impl Compact for AlloyTxEip1559 {
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
let tx = TxEip1559 {
chain_id: self.chain_id,
nonce: self.nonce,
gas_limit: self.gas_limit as u64,
max_fee_per_gas: self.max_fee_per_gas,
max_priority_fee_per_gas: self.max_priority_fee_per_gas,
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, _) = TxEip1559::from_compact(buf, len);
let alloy_tx = Self {
chain_id: tx.chain_id,
nonce: tx.nonce,
gas_limit: tx.gas_limit.into(),
max_fee_per_gas: tx.max_fee_per_gas,
max_priority_fee_per_gas: tx.max_priority_fee_per_gas,
to: tx.to,
value: tx.value,
access_list: tx.access_list,
input: tx.input,
};
(alloy_tx, buf)
}
}

View File

@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Default, Compact, Serialize, Deserialize)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
#[cfg_attr(test, crate::add_arbitrary_tests(compact))]
struct TxLegacy {
pub(crate) struct TxLegacy {
/// Added as EIP-155: Simple replay attack protection
chain_id: Option<ChainId>,
/// A scalar value equal to the number of transactions sent by the sender; formally Tn.
@ -67,7 +67,7 @@ impl Compact for AlloyTxLegacy {
chain_id: tx.chain_id,
nonce: tx.nonce,
gas_price: tx.gas_price,
gas_limit: tx.gas_limit as u128,
gas_limit: tx.gas_limit.into(),
to: tx.to,
value: tx.value,
input: tx.input,
@ -76,19 +76,3 @@ impl Compact for AlloyTxLegacy {
(alloy_tx, buf)
}
}
#[cfg(test)]
mod tests {
use crate::alloy::transaction::TxLegacy;
// each value in the database has an extra field named flags that encodes metadata about other
// fields in the value, e.g. offset and length.
//
// 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
#[test]
fn test_ensure_backwards_compatibility() {
assert_eq!(TxLegacy::bitflag_encoded_bytes(), 3);
}
}

View File

@ -0,0 +1,20 @@
mod eip1559;
mod legacy;
#[cfg(test)]
mod tests {
// each value in the database has an extra field named flags that encodes metadata about other
// fields in the value, e.g. offset and length.
//
// 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};
#[test]
fn test_ensure_backwards_compatibility() {
assert_eq!(TxLegacy::bitflag_encoded_bytes(), 3);
assert_eq!(TxEip1559::bitflag_encoded_bytes(), 4);
}
}