optimism: use op-alloy TxDeposit (#10667)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Thomas Coratger
2024-09-05 08:50:33 -07:00
committed by GitHub
parent c267c1caf7
commit b0fddef46a
11 changed files with 104 additions and 251 deletions

View File

@@ -3,6 +3,8 @@ mod eip2930;
mod eip4844;
mod eip7702;
mod legacy;
#[cfg(feature = "optimism")]
mod optimism;
#[cfg(test)]
mod tests {
@@ -13,7 +15,9 @@ 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::{
#[cfg(feature = "optimism")]
use crate::alloy::transaction::optimism::TxDeposit;
use crate::alloy::transaction::{
eip1559::TxEip1559, eip2930::TxEip2930, eip4844::TxEip4844, eip7702::TxEip7702,
legacy::TxLegacy,
};
@@ -26,4 +30,10 @@ mod tests {
assert_eq!(TxEip2930::bitflag_encoded_bytes(), 3);
assert_eq!(TxEip7702::bitflag_encoded_bytes(), 4);
}
#[cfg(feature = "optimism")]
#[test]
fn test_ensure_backwards_compatibility_optimism() {
assert_eq!(TxDeposit::bitflag_encoded_bytes(), 2);
}
}

View File

@@ -0,0 +1,61 @@
use crate::Compact;
use alloy_primitives::{Address, Bytes, TxKind, B256, U256};
use op_alloy_consensus::TxDeposit as AlloyTxDeposit;
use reth_codecs_derive::add_arbitrary_tests;
use serde::{Deserialize, Serialize};
/// Deposit transactions, also known as deposits are initiated on L1, and executed on L2.
///
/// 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 [`op_alloy_consensus::TxDeposit`]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize, Compact)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
#[add_arbitrary_tests(compact)]
pub(crate) struct TxDeposit {
source_hash: B256,
from: Address,
to: TxKind,
mint: Option<u128>,
value: U256,
gas_limit: u64,
is_system_transaction: bool,
input: Bytes,
}
impl Compact for AlloyTxDeposit {
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
let tx = TxDeposit {
source_hash: self.source_hash,
from: self.from,
to: self.to,
mint: self.mint,
value: self.value,
gas_limit: self.gas_limit as u64,
is_system_transaction: self.is_system_transaction,
input: self.input.clone(),
};
tx.to_compact(buf)
}
fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
let (tx, _) = TxDeposit::from_compact(buf, len);
let alloy_tx = Self {
source_hash: tx.source_hash,
from: tx.from,
to: tx.to,
mint: tx.mint,
value: tx.value,
gas_limit: tx.gas_limit as u128,
is_system_transaction: tx.is_system_transaction,
input: tx.input,
};
(alloy_tx, buf)
}
}