reth_codecs/alloy/transaction/
optimism.rsuse alloy_consensus::constants::EIP7702_TX_TYPE_ID;
use crate::Compact;
use alloy_primitives::{Address, Bytes, TxKind, B256, U256};
use op_alloy_consensus::{OpTxType, OpTypedTransaction, TxDeposit as AlloyTxDeposit, DEPOSIT_TX_TYPE_ID};
use reth_codecs_derive::add_arbitrary_tests;
use crate::txtype::{COMPACT_EXTENDED_IDENTIFIER_FLAG, COMPACT_IDENTIFIER_EIP1559, COMPACT_IDENTIFIER_EIP2930, COMPACT_IDENTIFIER_LEGACY};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)]
#[cfg_attr(
any(test, feature = "test-utils"),
derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(feature = "test-utils", allow(unreachable_pub), visibility::make(pub))]
#[reth_codecs(crate = "crate")]
#[add_arbitrary_tests(crate, 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,
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,
is_system_transaction: tx.is_system_transaction,
input: tx.input,
};
(alloy_tx, buf)
}
}
impl crate::Compact for OpTxType {
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
use crate::txtype::*;
match self {
Self::Legacy => COMPACT_IDENTIFIER_LEGACY,
Self::Eip2930 => COMPACT_IDENTIFIER_EIP2930,
Self::Eip1559 => COMPACT_IDENTIFIER_EIP1559,
Self::Eip7702 => {
buf.put_u8(EIP7702_TX_TYPE_ID);
COMPACT_EXTENDED_IDENTIFIER_FLAG
}
Self::Deposit => {
buf.put_u8(op_alloy_consensus::DEPOSIT_TX_TYPE_ID);
COMPACT_EXTENDED_IDENTIFIER_FLAG
}
}
}
fn from_compact(mut buf: &[u8], identifier: usize) -> (Self, &[u8]) {
use bytes::Buf;
(
match identifier {
COMPACT_IDENTIFIER_LEGACY => Self::Legacy,
COMPACT_IDENTIFIER_EIP2930 => Self::Eip2930,
COMPACT_IDENTIFIER_EIP1559 => Self::Eip1559,
COMPACT_EXTENDED_IDENTIFIER_FLAG => {
let extended_identifier = buf.get_u8();
match extended_identifier {
EIP7702_TX_TYPE_ID => Self::Eip7702,
op_alloy_consensus::DEPOSIT_TX_TYPE_ID => Self::Deposit,
_ => panic!("Unsupported TxType identifier: {extended_identifier}"),
}
}
_ => panic!("Unknown identifier for TxType: {identifier}"),
},
buf,
)
}
}
impl Compact for OpTypedTransaction {
fn to_compact<B>(&self, out: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
match self {
Self::Legacy(tx) => tx.to_compact(out),
Self::Eip2930(tx) => tx.to_compact(out),
Self::Eip1559(tx) => tx.to_compact(out),
Self::Eip7702(tx) => tx.to_compact(out),
Self::Deposit(tx) => tx.to_compact(out),
}
}
fn from_compact(mut buf: &[u8], identifier: usize) -> (Self, &[u8]) {
use bytes::Buf;
match identifier {
COMPACT_IDENTIFIER_LEGACY => {
let (tx, buf) = Compact::from_compact(buf, buf.len());
(Self::Legacy(tx), buf)
}
COMPACT_IDENTIFIER_EIP2930 => {
let (tx, buf) = Compact::from_compact(buf, buf.len());
(Self::Eip2930(tx), buf)
}
COMPACT_IDENTIFIER_EIP1559 => {
let (tx, buf) = Compact::from_compact(buf, buf.len());
(Self::Eip1559(tx), buf)
}
COMPACT_EXTENDED_IDENTIFIER_FLAG => {
let identifier = buf.get_u8();
match identifier {
EIP7702_TX_TYPE_ID => {
let (tx, buf) = Compact::from_compact(buf, buf.len());
(Self::Eip7702(tx), buf)
}
DEPOSIT_TX_TYPE_ID => {
let (tx, buf) = Compact::from_compact(buf, buf.len());
(Self::Deposit(tx), buf)
}
_ => unreachable!(
"Junk data in database: unknown Transaction variant: {identifier}"
),
}
}
_ => unreachable!("Junk data in database: unknown Transaction variant: {identifier}"),
}
}
}