chore: rm pooledtx element type (#13286)

This commit is contained in:
Matthias Seitz
2024-12-13 13:58:40 +01:00
committed by GitHub
parent 088925c08a
commit acc125a528
13 changed files with 214 additions and 993 deletions

View File

@ -2,20 +2,24 @@
use alloc::vec::Vec;
use alloy_consensus::{
transaction::RlpEcdsaTx, SignableTransaction, Signed, Transaction as _, TxEip1559, TxEip2930,
TxEip4844, TxEip4844Variant, TxEip7702, TxLegacy, Typed2718, TypedTransaction,
transaction::{PooledTransaction, RlpEcdsaTx},
SignableTransaction, Signed, Transaction as _, TxEip1559, TxEip2930, TxEip4844,
TxEip4844Variant, TxEip4844WithSidecar, TxEip7702, TxLegacy, Typed2718, TypedTransaction,
};
use alloy_eips::{
eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718},
eip2930::AccessList,
eip4844::BlobTransactionSidecar,
eip7702::SignedAuthorization,
};
use alloy_primitives::{
keccak256, Address, Bytes, ChainId, PrimitiveSignature as Signature, TxHash, TxKind, B256, U256,
};
use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header};
pub use compat::FillTxEnv;
use core::hash::{Hash, Hasher};
use derive_more::{AsRef, Deref};
pub use meta::TransactionMeta;
use once_cell as _;
#[cfg(not(feature = "std"))]
use once_cell::sync::{Lazy as LazyLock, OnceCell as OnceLock};
@ -23,24 +27,20 @@ use once_cell::sync::{Lazy as LazyLock, OnceCell as OnceLock};
use op_alloy_consensus::DepositTransaction;
#[cfg(feature = "optimism")]
use op_alloy_consensus::TxDeposit;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use reth_primitives_traits::{InMemorySize, SignedTransaction};
use revm_primitives::{AuthorizationList, TxEnv};
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use std::sync::{LazyLock, OnceLock};
pub use compat::FillTxEnv;
pub use meta::TransactionMeta;
pub use pooled::{PooledTransactionsElement, PooledTransactionsElementEcRecovered};
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
pub use reth_primitives_traits::{
transaction::error::{
InvalidTransactionError, TransactionConversionError, TryFromRecoveredTransactionError,
},
WithEncoded,
};
pub use sidecar::BlobTransaction;
use reth_primitives_traits::{InMemorySize, SignedTransaction};
use revm_primitives::{AuthorizationList, TxEnv};
use serde::{Deserialize, Serialize};
pub use signature::{recover_signer, recover_signer_unchecked};
#[cfg(feature = "std")]
use std::sync::{LazyLock, OnceLock};
pub use tx_type::TxType;
/// Handling transaction signature operations, including signature recovery,
@ -52,7 +52,6 @@ pub(crate) mod access_list;
mod compat;
mod meta;
mod pooled;
mod sidecar;
mod tx_type;
#[cfg(any(test, feature = "reth-codec"))]
@ -857,7 +856,7 @@ impl TransactionSigned {
/// Tries to convert a [`TransactionSigned`] into a [`PooledTransactionsElement`].
///
/// This function used as a helper to convert from a decoded p2p broadcast message to
/// [`PooledTransactionsElement`]. Since [`BlobTransaction`] is disallowed to be broadcasted on
/// [`PooledTransactionsElement`]. Since EIP4844 variants are disallowed to be broadcasted on
/// p2p, return an err if `tx` is [`Transaction::Eip4844`].
pub fn try_into_pooled(self) -> Result<PooledTransactionsElement, Self> {
let hash = self.hash();
@ -882,6 +881,32 @@ impl TransactionSigned {
}
}
/// Converts from an EIP-4844 [`RecoveredTx`] to a
/// [`PooledTransactionsElementEcRecovered`] with the given sidecar.
///
/// Returns an `Err` containing the original `TransactionSigned` if the transaction is not
/// EIP-4844.
pub fn try_into_pooled_eip4844(
self,
sidecar: BlobTransactionSidecar,
) -> Result<PooledTransactionsElement, Self> {
let hash = self.hash();
Ok(match self {
// If the transaction is an EIP-4844 transaction...
Self { transaction: Transaction::Eip4844(tx), signature, .. } => {
// Construct a pooled eip488 tx with the provided sidecar.
PooledTransactionsElement::Eip4844(Signed::new_unchecked(
TxEip4844WithSidecar { tx, sidecar },
signature,
hash,
))
}
// If the transaction is not EIP-4844, return an error with the original
// transaction.
_ => return Err(self),
})
}
/// Transaction hash. Used to identify transaction.
pub fn hash(&self) -> TxHash {
*self.tx_hash()
@ -1165,6 +1190,26 @@ impl From<RecoveredTx> for TransactionSigned {
}
}
impl TryFrom<TransactionSigned> for PooledTransaction {
type Error = TransactionConversionError;
fn try_from(tx: TransactionSigned) -> Result<Self, Self::Error> {
tx.try_into_pooled().map_err(|_| TransactionConversionError::UnsupportedForP2P)
}
}
impl From<PooledTransaction> for TransactionSigned {
fn from(tx: PooledTransaction) -> Self {
match tx {
PooledTransaction::Legacy(signed) => signed.into(),
PooledTransaction::Eip2930(signed) => signed.into(),
PooledTransaction::Eip1559(signed) => signed.into(),
PooledTransaction::Eip4844(signed) => signed.into(),
PooledTransaction::Eip7702(signed) => signed.into(),
}
}
}
impl Encodable for TransactionSigned {
/// This encodes the transaction _with_ the signature, and an rlp header.
///
@ -1406,6 +1451,13 @@ impl From<Signed<Transaction>> for TransactionSigned {
}
}
impl From<Signed<TxEip4844WithSidecar>> for TransactionSigned {
fn from(value: Signed<TxEip4844WithSidecar>) -> Self {
let (tx, sig, hash) = value.into_parts();
Self::new(tx.tx.into(), sig, hash)
}
}
impl From<TransactionSigned> for Signed<Transaction> {
fn from(value: TransactionSigned) -> Self {
let (tx, sig, hash) = value.into_parts();