tx-pool: add try_from_consensus for PoolTransaction (#10504)

This commit is contained in:
Thomas Coratger
2024-08-25 07:53:18 -07:00
committed by GitHub
parent 6459acb839
commit dfcfe8d271
4 changed files with 23 additions and 9 deletions

View File

@ -471,10 +471,7 @@ pub trait EthTransactions: LoadTransaction {
let recovered = let recovered =
signed_tx.into_ecrecovered().ok_or(EthApiError::InvalidTransactionSignature)?; signed_tx.into_ecrecovered().ok_or(EthApiError::InvalidTransactionSignature)?;
let pool_transaction = match recovered.try_into() { let pool_transaction = <<Self as LoadTransaction>::Pool as TransactionPool>::Transaction::try_from_consensus(recovered).map_err(|_| EthApiError::TransactionConversionError)?;
Ok(converted) => converted,
Err(_) => return Err(EthApiError::TransactionConversionError.into()),
};
// submit the transaction to the pool with a `Local` origin // submit the transaction to the pool with a `Local` origin
let hash = LoadTransaction::pool(self) let hash = LoadTransaction::pool(self)

View File

@ -344,7 +344,8 @@ pub async fn maintain_transaction_pool<Client, P, St, Tasks>(
<<P as TransactionPool>::Transaction as PoolTransaction>::from_pooled(tx) <<P as TransactionPool>::Transaction as PoolTransaction>::from_pooled(tx)
}) })
} else { } else {
tx.try_into().ok()
<P::Transaction as PoolTransaction>::try_from_consensus(tx).ok()
} }
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -588,7 +589,7 @@ where
.filter_map(|tx| tx.try_ecrecovered()) .filter_map(|tx| tx.try_ecrecovered())
.filter_map(|tx| { .filter_map(|tx| {
// Filter out errors // Filter out errors
tx.try_into().ok() <P::Transaction as PoolTransaction>::try_from_consensus(tx).ok()
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();

View File

@ -558,10 +558,16 @@ impl MockTransaction {
} }
impl PoolTransaction for MockTransaction { impl PoolTransaction for MockTransaction {
type TryFromConsensusError = TryFromRecoveredTransactionError;
type Consensus = TransactionSignedEcRecovered; type Consensus = TransactionSignedEcRecovered;
type Pooled = PooledTransactionsElementEcRecovered; type Pooled = PooledTransactionsElementEcRecovered;
fn try_from_consensus(tx: Self::Consensus) -> Result<Self, Self::TryFromConsensusError> {
tx.try_into()
}
fn into_consensus(self) -> Self::Consensus { fn into_consensus(self) -> Self::Consensus {
self.into() self.into()
} }

View File

@ -773,15 +773,19 @@ impl BestTransactionsAttributes {
} }
/// Trait for transaction types used inside the pool /// Trait for transaction types used inside the pool
pub trait PoolTransaction: pub trait PoolTransaction: fmt::Debug + Send + Sync + Clone {
fmt::Debug + Send + Sync + Clone + TryFrom<TransactionSignedEcRecovered> /// Associated error type for the `try_from_consensus` method.
{ type TryFromConsensusError;
/// Associated type representing the raw consensus variant of the transaction. /// Associated type representing the raw consensus variant of the transaction.
type Consensus: From<Self> + TryInto<Self>; type Consensus: From<Self> + TryInto<Self>;
/// Associated type representing the recovered pooled variant of the transaction. /// Associated type representing the recovered pooled variant of the transaction.
type Pooled: Into<Self>; type Pooled: Into<Self>;
/// Define a method to convert from the `Consensus` type to `Self`
fn try_from_consensus(tx: Self::Consensus) -> Result<Self, Self::TryFromConsensusError>;
/// Define a method to convert from the `Self` type to `Consensus` /// Define a method to convert from the `Self` type to `Consensus`
fn into_consensus(self) -> Self::Consensus; fn into_consensus(self) -> Self::Consensus;
@ -1024,10 +1028,16 @@ impl From<PooledTransactionsElementEcRecovered> for EthPooledTransaction {
} }
impl PoolTransaction for EthPooledTransaction { impl PoolTransaction for EthPooledTransaction {
type TryFromConsensusError = TryFromRecoveredTransactionError;
type Consensus = TransactionSignedEcRecovered; type Consensus = TransactionSignedEcRecovered;
type Pooled = PooledTransactionsElementEcRecovered; type Pooled = PooledTransactionsElementEcRecovered;
fn try_from_consensus(tx: Self::Consensus) -> Result<Self, Self::TryFromConsensusError> {
tx.try_into()
}
fn into_consensus(self) -> Self::Consensus { fn into_consensus(self) -> Self::Consensus {
self.into() self.into()
} }