feat: use reth-ethereum-primitives (#13830)

This commit is contained in:
Arsenii Kulikov
2025-01-17 04:22:21 +04:00
committed by GitHub
parent 7e972ea23d
commit 8efe441cc0
67 changed files with 941 additions and 3025 deletions

View File

@ -83,7 +83,6 @@ optimism = [
"alloy-consensus",
"dep:derive_more",
"dep:serde",
"reth-primitives/optimism",
"reth-optimism-evm/optimism",
"reth-provider/optimism",
"reth-node-core/optimism",

View File

@ -1,6 +1,6 @@
use alloy_consensus::{
transaction::{from_eip155_value, RlpEcdsaTx},
Header, TxEip1559, TxEip2930, TxEip4844, TxEip7702, TxLegacy,
Header, TxEip1559, TxEip2930, TxEip7702, TxLegacy,
};
use alloy_eips::{
eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718},
@ -12,9 +12,8 @@ use alloy_primitives::{
};
use alloy_rlp::{Decodable, Error as RlpError, RlpDecodable};
use derive_more::{AsRef, Deref};
use op_alloy_consensus::TxDeposit;
use op_alloy_consensus::{OpTxType, OpTypedTransaction, TxDeposit};
use reth_downloaders::file_client::FileClientError;
use reth_primitives::transaction::{Transaction, TxType};
use serde::{Deserialize, Serialize};
use tokio_util::codec::Decoder;
@ -83,17 +82,7 @@ pub struct TransactionSigned {
/// Raw transaction info
#[deref]
#[as_ref]
pub transaction: Transaction,
}
impl Default for TransactionSigned {
fn default() -> Self {
Self {
hash: Default::default(),
signature: Signature::test_signature(),
transaction: Default::default(),
}
}
pub transaction: OpTypedTransaction,
}
impl AsRef<Self> for TransactionSigned {
@ -113,7 +102,10 @@ impl TransactionSigned {
/// Create a new signed transaction from a transaction and its signature.
///
/// This will also calculate the transaction hash using its encoding.
pub fn from_transaction_and_signature(transaction: Transaction, signature: Signature) -> Self {
pub fn from_transaction_and_signature(
transaction: OpTypedTransaction,
signature: Signature,
) -> Self {
let mut initial_tx = Self { transaction, hash: Default::default(), signature };
initial_tx.hash = initial_tx.recalculate_hash();
initial_tx
@ -190,7 +182,7 @@ impl TransactionSigned {
// so decoding methods do not need to manually advance the buffer
pub fn decode_rlp_legacy_transaction(data: &mut &[u8]) -> alloy_rlp::Result<Self> {
let (transaction, hash, signature) = Self::decode_rlp_legacy_transaction_tuple(data)?;
let signed = Self { transaction: Transaction::Legacy(transaction), hash, signature };
let signed = Self { transaction: OpTypedTransaction::Legacy(transaction), hash, signature };
Ok(signed)
}
}
@ -229,55 +221,58 @@ impl Decodable for TransactionSigned {
impl Encodable2718 for TransactionSigned {
fn type_flag(&self) -> Option<u8> {
match self.transaction.tx_type() {
TxType::Legacy => None,
OpTxType::Legacy => None,
tx_type => Some(tx_type as u8),
}
}
fn encode_2718_len(&self) -> usize {
match &self.transaction {
Transaction::Legacy(legacy_tx) => legacy_tx.eip2718_encoded_length(&self.signature),
Transaction::Eip2930(access_list_tx) => {
OpTypedTransaction::Legacy(legacy_tx) => {
legacy_tx.eip2718_encoded_length(&self.signature)
}
OpTypedTransaction::Eip2930(access_list_tx) => {
access_list_tx.eip2718_encoded_length(&self.signature)
}
Transaction::Eip1559(dynamic_fee_tx) => {
OpTypedTransaction::Eip1559(dynamic_fee_tx) => {
dynamic_fee_tx.eip2718_encoded_length(&self.signature)
}
Transaction::Eip4844(blob_tx) => blob_tx.eip2718_encoded_length(&self.signature),
Transaction::Eip7702(set_code_tx) => {
OpTypedTransaction::Eip7702(set_code_tx) => {
set_code_tx.eip2718_encoded_length(&self.signature)
}
Transaction::Deposit(deposit_tx) => deposit_tx.eip2718_encoded_length(),
OpTypedTransaction::Deposit(deposit_tx) => deposit_tx.eip2718_encoded_length(),
}
}
fn encode_2718(&self, out: &mut dyn alloy_rlp::BufMut) {
self.transaction.eip2718_encode(&self.signature, out)
match &self.transaction {
OpTypedTransaction::Legacy(tx) => tx.eip2718_encode(&self.signature, out),
OpTypedTransaction::Eip2930(tx) => tx.eip2718_encode(&self.signature, out),
OpTypedTransaction::Eip1559(tx) => tx.eip2718_encode(&self.signature, out),
OpTypedTransaction::Eip7702(tx) => tx.eip2718_encode(&self.signature, out),
OpTypedTransaction::Deposit(tx) => tx.encode_2718(out),
}
}
}
impl Decodable2718 for TransactionSigned {
fn typed_decode(ty: u8, buf: &mut &[u8]) -> Eip2718Result<Self> {
match ty.try_into().map_err(|_| Eip2718Error::UnexpectedType(ty))? {
TxType::Legacy => Err(Eip2718Error::UnexpectedType(0)),
TxType::Eip2930 => {
OpTxType::Legacy => Err(Eip2718Error::UnexpectedType(0)),
OpTxType::Eip2930 => {
let (tx, signature, hash) = TxEip2930::rlp_decode_signed(buf)?.into_parts();
Ok(Self { transaction: Transaction::Eip2930(tx), signature, hash })
Ok(Self { transaction: OpTypedTransaction::Eip2930(tx), signature, hash })
}
TxType::Eip1559 => {
OpTxType::Eip1559 => {
let (tx, signature, hash) = TxEip1559::rlp_decode_signed(buf)?.into_parts();
Ok(Self { transaction: Transaction::Eip1559(tx), signature, hash })
Ok(Self { transaction: OpTypedTransaction::Eip1559(tx), signature, hash })
}
TxType::Eip7702 => {
OpTxType::Eip7702 => {
let (tx, signature, hash) = TxEip7702::rlp_decode_signed(buf)?.into_parts();
Ok(Self { transaction: Transaction::Eip7702(tx), signature, hash })
Ok(Self { transaction: OpTypedTransaction::Eip7702(tx), signature, hash })
}
TxType::Eip4844 => {
let (tx, signature, hash) = TxEip4844::rlp_decode_signed(buf)?.into_parts();
Ok(Self { transaction: Transaction::Eip4844(tx), signature, hash })
}
TxType::Deposit => Ok(Self::from_transaction_and_signature(
Transaction::Deposit(TxDeposit::rlp_decode(buf)?),
OpTxType::Deposit => Ok(Self::from_transaction_and_signature(
OpTypedTransaction::Deposit(TxDeposit::rlp_decode(buf)?),
TxDeposit::signature(),
)),
}
@ -291,8 +286,9 @@ impl Decodable2718 for TransactionSigned {
#[cfg(test)]
mod tests {
use crate::ovm_file_codec::TransactionSigned;
use alloy_consensus::Typed2718;
use alloy_primitives::{address, hex, TxKind, B256, U256};
use reth_primitives::transaction::Transaction;
use op_alloy_consensus::OpTypedTransaction;
const DEPOSIT_FUNCTION_SELECTOR: [u8; 4] = [0xb6, 0xb5, 0x5f, 0x25];
use alloy_rlp::Decodable;
@ -305,7 +301,7 @@ mod tests {
// Verify deposit transaction
let deposit_tx = match &deposit_decoded.transaction {
Transaction::Legacy(ref tx) => tx,
OpTypedTransaction::Legacy(ref tx) => tx,
_ => panic!("Expected legacy transaction for NFT deposit"),
};
@ -345,7 +341,7 @@ mod tests {
assert!(system_decoded.is_legacy());
let system_tx = match &system_decoded.transaction {
Transaction::Legacy(ref tx) => tx,
OpTypedTransaction::Legacy(ref tx) => tx,
_ => panic!("Expected Legacy transaction"),
};

View File

@ -5,9 +5,9 @@ use alloy_primitives::{
Address, Bloom, Bytes, B256,
};
use alloy_rlp::{Decodable, RlpDecodable};
use op_alloy_consensus::OpDepositReceipt;
use op_alloy_consensus::{OpDepositReceipt, OpTxType};
use reth_optimism_primitives::OpReceipt;
use reth_primitives::{Log, Receipt, TxType};
use reth_primitives::{Log, Receipt};
use tokio_util::codec::Decoder;
use reth_downloaders::{file_client::FileClientError, receipt_file_client::ReceiptWithBlockNumber};
@ -92,49 +92,27 @@ pub struct OpGethReceipt {
#[rlp(trailing)]
struct OpGethReceiptContainer(Option<OpGethReceipt>);
impl TryFrom<OpGethReceipt> for Receipt {
type Error = &'static str;
impl TryFrom<OpGethReceipt> for OpReceipt {
type Error = FileClientError;
fn try_from(exported_receipt: OpGethReceipt) -> Result<Self, Self::Error> {
let OpGethReceipt { tx_type, status, cumulative_gas_used, logs, .. } = exported_receipt;
#[allow(clippy::needless_update)]
Ok(Self {
tx_type: TxType::try_from(tx_type.to_be_bytes()[0])?,
success: status != 0,
cumulative_gas_used,
logs,
..Default::default()
})
}
}
impl TryFrom<OpGethReceipt> for OpReceipt {
type Error = &'static str;
fn try_from(exported_receipt: OpGethReceipt) -> Result<Self, Self::Error> {
let Receipt {
tx_type,
success,
cumulative_gas_used,
logs,
deposit_nonce,
deposit_receipt_version,
} = exported_receipt.try_into()?;
let tx_type = OpTxType::try_from(tx_type.to_be_bytes()[0])
.map_err(|e| FileClientError::Rlp(e.into(), vec![tx_type]))?;
let receipt =
alloy_consensus::Receipt { status: success.into(), cumulative_gas_used, logs };
alloy_consensus::Receipt { status: (status != 0).into(), cumulative_gas_used, logs };
match tx_type {
TxType::Legacy => Ok(Self::Legacy(receipt)),
TxType::Eip2930 => Ok(Self::Eip2930(receipt)),
TxType::Eip1559 => Ok(Self::Eip1559(receipt)),
TxType::Eip7702 => Ok(Self::Eip7702(receipt)),
TxType::Eip4844 => Err("EIP-4844 receipts are not supported for OP"),
TxType::Deposit => Ok(Self::Deposit(OpDepositReceipt {
OpTxType::Legacy => Ok(Self::Legacy(receipt)),
OpTxType::Eip2930 => Ok(Self::Eip2930(receipt)),
OpTxType::Eip1559 => Ok(Self::Eip1559(receipt)),
OpTxType::Eip7702 => Ok(Self::Eip7702(receipt)),
OpTxType::Deposit => Ok(Self::Deposit(OpDepositReceipt {
inner: receipt,
deposit_nonce,
deposit_receipt_version,
deposit_nonce: None,
deposit_receipt_version: None,
})),
}
}
@ -142,6 +120,7 @@ impl TryFrom<OpGethReceipt> for OpReceipt {
#[cfg(test)]
pub(crate) mod test {
use alloy_consensus::{Receipt, TxReceipt};
use alloy_primitives::{hex, LogData};
use super::*;
@ -156,12 +135,12 @@ pub(crate) mod test {
let receipt = receipt_block_1();
OpGethReceipt {
tx_type: receipt.receipt.tx_type as u8,
tx_type: receipt.receipt.tx_type() as u8,
post_state: Bytes::default(),
status: receipt.receipt.success as u64,
cumulative_gas_used: receipt.receipt.cumulative_gas_used,
status: receipt.receipt.status() as u64,
cumulative_gas_used: receipt.receipt.cumulative_gas_used(),
bloom: Bloom::from(hex!("00000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000400000000000100000000000000200000000002000000000000001000000000000000000004000000000000000000000000000040000400000100400000000000000100000000000000000000000000000020000000000000000000000000000000000000000000000001000000000000000000000100000000000000000000000000000000000000000000000000000000000000088000000080000000000010000000000000000000000000000800008000120000000000000000000000000000000002000")),
logs: receipt.receipt.logs,
logs: receipt.receipt.logs().to_vec(),
tx_hash: B256::from(hex!("5e77a04531c7c107af1882d76cbff9486d0a9aa53701c30888509d4f5f2b003a")), contract_address: Address::from(hex!("0000000000000000000000000000000000000000")), gas_used: 202813,
block_hash: B256::from(hex!("bee7192e575af30420cae0c7776304ac196077ee72b048970549e4f08e875453")),
block_number: receipt.number,
@ -173,7 +152,7 @@ pub(crate) mod test {
}
}
pub(crate) fn receipt_block_1() -> ReceiptWithBlockNumber {
pub(crate) fn receipt_block_1() -> ReceiptWithBlockNumber<OpReceipt> {
let log_1 = Log {
address: Address::from(hex!("8ce8c13d816fe6daf12d6fd9e4952e1fc88850af")),
data: LogData::new(
@ -233,20 +212,16 @@ pub(crate) mod test {
.unwrap(),
};
let mut receipt = Receipt {
tx_type: TxType::Legacy,
success: true,
let receipt = OpReceipt::Legacy(Receipt {
status: true.into(),
cumulative_gas_used: 202813,
..Default::default()
};
// #[allow(clippy::needless_update)] not recognised, ..Default::default() needed so optimism
// feature must not be brought into scope
receipt.logs = vec![log_1, log_2, log_3];
logs: vec![log_1, log_2, log_3],
});
ReceiptWithBlockNumber { receipt, number: 1 }
}
pub(crate) fn receipt_block_2() -> ReceiptWithBlockNumber {
pub(crate) fn receipt_block_2() -> ReceiptWithBlockNumber<OpReceipt> {
let log_1 = Log {
address: Address::from(hex!("8ce8c13d816fe6daf12d6fd9e4952e1fc88850af")),
data: LogData::new(
@ -285,20 +260,16 @@ pub(crate) mod test {
.unwrap(),
};
let mut receipt = Receipt {
tx_type: TxType::Legacy,
success: true,
let receipt = OpReceipt::Legacy(Receipt {
status: true.into(),
cumulative_gas_used: 116237,
..Default::default()
};
// #[allow(clippy::needless_update)] not recognised, ..Default::default() needed so optimism
// feature must not be brought into scope
receipt.logs = vec![log_1, log_2];
logs: vec![log_1, log_2],
});
ReceiptWithBlockNumber { receipt, number: 2 }
}
pub(crate) fn receipt_block_3() -> ReceiptWithBlockNumber {
pub(crate) fn receipt_block_3() -> ReceiptWithBlockNumber<OpReceipt> {
let log_1 = Log {
address: Address::from(hex!("8ce8c13d816fe6daf12d6fd9e4952e1fc88850af")),
data: LogData::new(
@ -337,15 +308,11 @@ pub(crate) mod test {
.unwrap(),
};
let mut receipt = Receipt {
tx_type: TxType::Legacy,
success: true,
let receipt = OpReceipt::Legacy(Receipt {
status: true.into(),
cumulative_gas_used: 116237,
..Default::default()
};
// #[allow(clippy::needless_update)] not recognised, ..Default::default() needed so optimism
// feature must not be brought into scope
receipt.logs = vec![log_1, log_2];
logs: vec![log_1, log_2],
});
ReceiptWithBlockNumber { receipt, number: 3 }
}