refactor: merge signed tx traits (#14622)

This commit is contained in:
Léa Narzis
2025-02-24 14:43:31 +01:00
committed by GitHub
parent 480c33f932
commit 340d7ba008
16 changed files with 53 additions and 66 deletions

View File

@ -18,8 +18,8 @@ use rand::{thread_rng, Rng};
use reth_chainspec::{ChainSpec, EthereumHardfork, MIN_TRANSACTION_GAS};
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_primitives::{
transaction::SignedTransactionIntoRecoveredExt, BlockBody, EthPrimitives, NodePrimitives,
Receipt, Recovered, RecoveredBlock, SealedBlock, SealedHeader, Transaction, TransactionSigned,
transaction::SignedTransaction, BlockBody, EthPrimitives, NodePrimitives, Receipt, Recovered,
RecoveredBlock, SealedBlock, SealedHeader, Transaction, TransactionSigned,
};
use reth_primitives_traits::{
proofs::{calculate_receipt_root, calculate_transaction_root, calculate_withdrawals_root},

View File

@ -19,7 +19,7 @@ use reth_evm::{
};
use reth_payload_primitives::EngineApiMessageVersion;
use reth_payload_validator::ExecutionPayloadValidator;
use reth_primitives::{transaction::SignedTransactionIntoRecoveredExt, Block, BlockBody, Receipt};
use reth_primitives::{Block, BlockBody, Receipt};
use reth_primitives_traits::{block::Block as _, proofs, SignedTransaction};
use reth_provider::{BlockReader, ExecutionOutcome, ProviderError, StateProviderFactory};
use reth_revm::{

View File

@ -8,8 +8,8 @@ use alloy_primitives::{Address, BlockHash, BlockNumber, TxHash};
use core::{fmt, ops::RangeInclusive};
use reth_execution_errors::{BlockExecutionError, InternalBlockExecutionError};
use reth_primitives_traits::{
transaction::signed::SignedTransactionIntoRecoveredExt, Block, BlockBody, NodePrimitives,
RecoveredBlock, SealedHeader, SignedTransaction,
transaction::signed::SignedTransaction, Block, BlockBody, NodePrimitives, RecoveredBlock,
SealedHeader,
};
use reth_trie_common::updates::TrieUpdates;
use revm_database::BundleState;

View File

@ -49,7 +49,7 @@ use reth_network_p2p::{
};
use reth_network_peers::PeerId;
use reth_network_types::ReputationChangeKind;
use reth_primitives::{transaction::SignedTransactionIntoRecoveredExt, TransactionSigned};
use reth_primitives::TransactionSigned;
use reth_primitives_traits::SignedTransaction;
use reth_tokio_util::EventStream;
use reth_transaction_pool::{

View File

@ -31,9 +31,7 @@ use reth_optimism_storage::predeploys;
use reth_payload_builder_primitives::PayloadBuilderError;
use reth_payload_primitives::PayloadBuilderAttributes;
use reth_payload_util::{BestPayloadTransactions, NoopPayloadTransactions, PayloadTransactions};
use reth_primitives::{
transaction::SignedTransactionIntoRecoveredExt, BlockBody, NodePrimitives, SealedHeader,
};
use reth_primitives::{transaction::SignedTransaction, BlockBody, NodePrimitives, SealedHeader};
use reth_primitives_traits::{block::Block as _, proofs, RecoveredBlock};
use reth_provider::{
BlockExecutionResult, HashedPostStateProvider, ProviderError, StateProofProvider,

View File

@ -2,7 +2,7 @@
use crate::{
block::{error::SealedBlockRecoveryError, SealedBlock},
transaction::signed::{RecoveryError, SignedTransactionIntoRecoveredExt},
transaction::signed::{RecoveryError, SignedTransaction},
Block, BlockBody, InMemorySize, SealedHeader,
};
use alloc::vec::Vec;

View File

@ -99,6 +99,41 @@ pub trait SignedTransaction:
fn recalculate_hash(&self) -> B256 {
keccak256(self.encoded_2718())
}
/// Tries to recover signer and return [`Recovered`] by cloning the type.
#[auto_impl(keep_default_for(&, Arc))]
fn try_clone_into_recovered(&self) -> Result<Recovered<Self>, RecoveryError> {
self.recover_signer().map(|signer| Recovered::new_unchecked(self.clone(), signer))
}
/// Tries to recover signer and return [`Recovered`].
///
/// Returns `Err(Self)` if the transaction's signature is invalid, see also
/// [`SignedTransaction::recover_signer`].
#[auto_impl(keep_default_for(&, Arc))]
fn try_into_recovered(self) -> Result<Recovered<Self>, Self> {
match self.recover_signer() {
Ok(signer) => Ok(Recovered::new_unchecked(self, signer)),
Err(_) => Err(self),
}
}
/// Consumes the type, recover signer and return [`Recovered`] _without
/// ensuring that the signature has a low `s` value_ (EIP-2).
///
/// Returns `None` if the transaction's signature is invalid.
#[auto_impl(keep_default_for(&, Arc))]
fn into_recovered_unchecked(self) -> Result<Recovered<Self>, RecoveryError> {
self.recover_signer_unchecked().map(|signer| Recovered::new_unchecked(self, signer))
}
/// Returns the [`Recovered`] transaction with the given sender.
///
/// Note: assumes the given signer is the signer of this transaction.
#[auto_impl(keep_default_for(&, Arc))]
fn with_signer(self, signer: Address) -> Recovered<Self> {
Recovered::new_unchecked(self, signer)
}
}
impl SignedTransaction for PooledTransaction {
@ -183,42 +218,6 @@ impl SignedTransaction for op_alloy_consensus::OpPooledTransaction {
}
}
/// Extension trait for [`SignedTransaction`] to convert it into [`Recovered`].
pub trait SignedTransactionIntoRecoveredExt: SignedTransaction {
/// Tries to recover signer and return [`Recovered`] by cloning the type.
fn try_clone_into_recovered(&self) -> Result<Recovered<Self>, RecoveryError> {
self.recover_signer().map(|signer| Recovered::new_unchecked(self.clone(), signer))
}
/// Tries to recover signer and return [`Recovered`].
///
/// Returns `Err(Self)` if the transaction's signature is invalid, see also
/// [`SignedTransaction::recover_signer`].
fn try_into_recovered(self) -> Result<Recovered<Self>, Self> {
match self.recover_signer() {
Ok(signer) => Ok(Recovered::new_unchecked(self, signer)),
Err(_) => Err(self),
}
}
/// Consumes the type, recover signer and return [`Recovered`] _without
/// ensuring that the signature has a low `s` value_ (EIP-2).
///
/// Returns `None` if the transaction's signature is invalid.
fn into_recovered_unchecked(self) -> Result<Recovered<Self>, RecoveryError> {
self.recover_signer_unchecked().map(|signer| Recovered::new_unchecked(self, signer))
}
/// Returns the [`Recovered`] transaction with the given sender.
///
/// Note: assumes the given signer is the signer of this transaction.
fn with_signer(self, signer: Address) -> Recovered<Self> {
Recovered::new_unchecked(self, signer)
}
}
impl<T> SignedTransactionIntoRecoveredExt for T where T: SignedTransaction {}
/// Opaque error type for sender recovery.
#[derive(Debug, Default, thiserror::Error)]
#[error("Failed to recover the signer")]

View File

@ -11,7 +11,7 @@ pub use reth_primitives_traits::{
error::{
InvalidTransactionError, TransactionConversionError, TryFromRecoveredTransactionError,
},
signed::SignedTransactionIntoRecoveredExt,
signed::SignedTransaction,
},
FillTxEnv, WithEncoded,
};

View File

@ -14,8 +14,7 @@ use alloy_primitives::{Address, Bytes, TxHash, B256};
use alloy_rpc_types_eth::{transaction::TransactionRequest, BlockNumberOrTag, TransactionInfo};
use futures::Future;
use reth_node_api::BlockBody;
use reth_primitives::{transaction::SignedTransactionIntoRecoveredExt, RecoveredBlock};
use reth_primitives_traits::SignedTransaction;
use reth_primitives::{transaction::SignedTransaction, RecoveredBlock};
use reth_provider::{
BlockNumReader, BlockReaderIdExt, ProviderBlock, ProviderReceipt, ProviderTx, ReceiptProvider,
TransactionsProvider,

View File

@ -1,7 +1,7 @@
//! Commonly used code snippets
use super::{EthApiError, EthResult};
use reth_primitives::{transaction::SignedTransactionIntoRecoveredExt, Recovered};
use reth_primitives::Recovered;
use reth_primitives_traits::SignedTransaction;
use std::future::Future;

View File

@ -19,7 +19,7 @@ use reth_chain_state::CanonStateNotification;
use reth_chainspec::{ChainSpecProvider, EthChainSpec};
use reth_execution_types::ChangedAccount;
use reth_fs_util::FsPathError;
use reth_primitives::{transaction::SignedTransactionIntoRecoveredExt, SealedHeader};
use reth_primitives::SealedHeader;
use reth_primitives_traits::{NodePrimitives, SignedTransaction};
use reth_storage_api::{errors::provider::ProviderError, BlockReaderIdExt, StateProviderFactory};
use reth_tasks::TaskSpawner;

View File

@ -4,9 +4,7 @@ use alloy_eips::{eip1559::MIN_PROTOCOL_BASE_FEE, eip2718::Encodable2718, eip2930
use alloy_primitives::{Address, Bytes, TxKind, B256, U256};
use rand::Rng;
use reth_chainspec::MAINNET;
use reth_primitives::{
transaction::SignedTransactionIntoRecoveredExt, Transaction, TransactionSigned,
};
use reth_primitives::{transaction::SignedTransaction, Transaction, TransactionSigned};
use reth_primitives_traits::crypto::secp256k1::sign_message;
/// A generator for transactions for testing purposes.

View File

@ -29,10 +29,7 @@ use rand::{
prelude::Distribution,
};
use reth_primitives::{
transaction::{
SignedTransactionIntoRecoveredExt, TransactionConversionError,
TryFromRecoveredTransactionError,
},
transaction::{TransactionConversionError, TryFromRecoveredTransactionError},
PooledTransaction, Recovered, Transaction, TransactionSigned, TxType,
};
use reth_primitives_traits::{InMemorySize, SignedTransaction};

View File

@ -17,9 +17,8 @@ use futures_util::{ready, Stream};
use reth_eth_wire_types::HandleMempoolData;
use reth_execution_types::ChangedAccount;
use reth_primitives::{
kzg::KzgSettings,
transaction::{SignedTransactionIntoRecoveredExt, TransactionConversionError},
PooledTransaction, Recovered, SealedBlock, TransactionSigned,
kzg::KzgSettings, transaction::TransactionConversionError, PooledTransaction, Recovered,
SealedBlock, TransactionSigned,
};
use reth_primitives_traits::{Block, InMemorySize, SignedTransaction};
#[cfg(feature = "serde")]

View File

@ -924,7 +924,7 @@ mod tests {
use alloy_consensus::Transaction;
use alloy_eips::eip2718::Decodable2718;
use alloy_primitives::{hex, U256};
use reth_primitives::{transaction::SignedTransactionIntoRecoveredExt, PooledTransaction};
use reth_primitives::{transaction::SignedTransaction, PooledTransaction};
use reth_provider::test_utils::{ExtendedAccount, MockEthProvider};
fn get_transaction() -> EthPooledTransaction {