Replace async trait with ->impl Future (#6791)

This commit is contained in:
Abner Zheng
2024-02-26 21:23:00 +08:00
committed by GitHub
parent f90822ada2
commit 323bad2718
19 changed files with 26 additions and 43 deletions

2
Cargo.lock generated
View File

@ -6248,7 +6248,6 @@ dependencies = [
"alloy-node-bindings", "alloy-node-bindings",
"alloy-rlp", "alloy-rlp",
"aquamarine", "aquamarine",
"async-trait",
"auto_impl", "auto_impl",
"criterion", "criterion",
"derive_more", "derive_more",
@ -6949,7 +6948,6 @@ dependencies = [
"alloy-rlp", "alloy-rlp",
"aquamarine", "aquamarine",
"assert_matches", "assert_matches",
"async-trait",
"auto_impl", "auto_impl",
"bitflags 2.4.2", "bitflags 2.4.2",
"criterion", "criterion",

View File

@ -60,7 +60,6 @@ tracing.workspace = true
fnv = "1.0" fnv = "1.0"
thiserror.workspace = true thiserror.workspace = true
parking_lot.workspace = true parking_lot.workspace = true
async-trait.workspace = true
linked_hash_set = "0.1" linked_hash_set = "0.1"
linked-hash-map = "0.5.6" linked-hash-map = "0.5.6"
rand.workspace = true rand.workspace = true

View File

@ -1,7 +1,6 @@
#![allow(unreachable_pub)] #![allow(unreachable_pub)]
//! Helper extension traits for working with clique providers. //! Helper extension traits for working with clique providers.
use async_trait::async_trait;
use enr::k256::ecdsa::SigningKey; use enr::k256::ecdsa::SigningKey;
use ethers_core::{ use ethers_core::{
types::{transaction::eip2718::TypedTransaction, Address, Block, BlockNumber, H256}, types::{transaction::eip2718::TypedTransaction, Address, Block, BlockNumber, H256},
@ -51,7 +50,6 @@ pub type CliqueMiddlewareError<M> = CliqueError<<M as Middleware>::Error>;
/// Extension trait for [`Middleware`](ethers_providers::Middleware) to provide clique specific /// Extension trait for [`Middleware`](ethers_providers::Middleware) to provide clique specific
/// functionality. /// functionality.
#[async_trait(?Send)]
pub trait CliqueMiddleware: Send + Sync + Middleware { pub trait CliqueMiddleware: Send + Sync + Middleware {
/// Enable mining on the clique geth instance by importing and unlocking the signer account /// Enable mining on the clique geth instance by importing and unlocking the signer account
/// derived from given private key and password. /// derived from given private key and password.

View File

@ -5,7 +5,6 @@ use reth_rpc_types::{NodeInfo, PeerInfo};
/// Admin namespace rpc interface that gives access to several non-standard RPC methods. /// Admin namespace rpc interface that gives access to several non-standard RPC methods.
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "admin"))] #[cfg_attr(not(feature = "client"), rpc(server, namespace = "admin"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "admin"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "admin"))]
#[async_trait::async_trait]
pub trait AdminApi { pub trait AdminApi {
/// Adds the given node record to the peerset. /// Adds the given node record to the peerset.
#[method(name = "addPeer")] #[method(name = "addPeer")]

View File

@ -12,7 +12,6 @@ use reth_rpc_types::{
/// A subset of the [EthBundleApi] API interface that only supports `eth_callBundle`. /// A subset of the [EthBundleApi] API interface that only supports `eth_callBundle`.
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))] #[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))]
#[async_trait::async_trait]
pub trait EthCallBundleApi { pub trait EthCallBundleApi {
/// `eth_callBundle` can be used to simulate a bundle against a specific block number, /// `eth_callBundle` can be used to simulate a bundle against a specific block number,
/// including simulating a bundle at the top of the next block. /// including simulating a bundle at the top of the next block.
@ -28,7 +27,6 @@ pub trait EthCallBundleApi {
/// See also <https://docs.flashbots.net/flashbots-auction/searchers/advanced/rpc-endpoint> /// See also <https://docs.flashbots.net/flashbots-auction/searchers/advanced/rpc-endpoint>
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))] #[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))]
#[async_trait::async_trait]
pub trait EthBundleApi { pub trait EthBundleApi {
/// `eth_sendBundle` can be used to send your bundles to the builder. /// `eth_sendBundle` can be used to send your bundles to the builder.
#[method(name = "sendBundle")] #[method(name = "sendBundle")]

View File

@ -165,7 +165,6 @@ pub trait EngineApi<Engine: EngineTypes> {
/// Specifically for the engine auth server: <https://github.com/ethereum/execution-apis/blob/main/src/engine/common.md#underlying-protocol> /// Specifically for the engine auth server: <https://github.com/ethereum/execution-apis/blob/main/src/engine/common.md#underlying-protocol>
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))] #[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))]
#[async_trait]
pub trait EngineEthApi { pub trait EngineEthApi {
/// Returns an object with data about the sync status or false. /// Returns an object with data about the sync status or false.
#[method(name = "syncing")] #[method(name = "syncing")]

View File

@ -12,7 +12,6 @@ use reth_rpc_types::{
/// Eth rpc interface: <https://ethereum.github.io/execution-apis/api-documentation/> /// Eth rpc interface: <https://ethereum.github.io/execution-apis/api-documentation/>
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))] #[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))]
#[async_trait]
pub trait EthApi { pub trait EthApi {
/// Returns the protocol version encoded as a string. /// Returns the protocol version encoded as a string.
#[method(name = "protocolVersion")] #[method(name = "protocolVersion")]

View File

@ -6,7 +6,6 @@ use reth_rpc_types::{
/// Mev rpc interface. /// Mev rpc interface.
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "mev"))] #[cfg_attr(not(feature = "client"), rpc(server, namespace = "mev"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "mev"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "mev"))]
#[async_trait::async_trait]
pub trait MevApi { pub trait MevApi {
/// Submitting bundles to the relay. It takes in a bundle and provides a bundle hash as a /// Submitting bundles to the relay. It takes in a bundle and provides a bundle hash as a
/// return value. /// return value.

View File

@ -5,7 +5,6 @@ use reth_rpc_types::txpool::{TxpoolContent, TxpoolContentFrom, TxpoolInspect, Tx
/// Txpool rpc interface. /// Txpool rpc interface.
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "txpool"))] #[cfg_attr(not(feature = "client"), rpc(server, namespace = "txpool"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "txpool"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "txpool"))]
#[async_trait::async_trait]
pub trait TxPoolApi { pub trait TxPoolApi {
/// Returns the number of transactions currently pending for inclusion in the next block(s), as /// Returns the number of transactions currently pending for inclusion in the next block(s), as
/// well as the ones that are being scheduled for future execution only. /// well as the ones that are being scheduled for future execution only.

View File

@ -6,7 +6,6 @@ use reth_rpc_types::relay::{BuilderBlockValidationRequest, BuilderBlockValidatio
/// Block validation rpc interface. /// Block validation rpc interface.
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "flashbots"))] #[cfg_attr(not(feature = "client"), rpc(server, namespace = "flashbots"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "flashbots"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "flashbots"))]
#[async_trait::async_trait]
pub trait BlockSubmissionValidationApi { pub trait BlockSubmissionValidationApi {
/// A Request to validate a block submission. /// A Request to validate a block submission.
#[method(name = "validateBuilderSubmissionV1")] #[method(name = "validateBuilderSubmissionV1")]

View File

@ -4,7 +4,6 @@ use reth_primitives::{Bytes, B256};
/// Web3 rpc interface. /// Web3 rpc interface.
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "web3"))] #[cfg_attr(not(feature = "client"), rpc(server, namespace = "web3"))]
#[cfg_attr(feature = "client", rpc(server, client, namespace = "web3"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "web3"))]
#[async_trait::async_trait]
pub trait Web3Api { pub trait Web3Api {
/// Returns current client version. /// Returns current client version.
#[method(name = "clientVersion")] #[method(name = "clientVersion")]

View File

@ -29,7 +29,6 @@ alloy-rlp.workspace = true
reth-revm = { workspace = true, optional = true } reth-revm = { workspace = true, optional = true }
# async/futures # async/futures
async-trait.workspace = true
futures-util.workspace = true futures-util.workspace = true
parking_lot.workspace = true parking_lot.workspace = true
tokio = { workspace = true, default-features = false, features = ["sync"] } tokio = { workspace = true, default-features = false, features = ["sync"] }

View File

@ -318,7 +318,6 @@ where
} }
/// implements the `TransactionPool` interface for various transaction pool API consumers. /// implements the `TransactionPool` interface for various transaction pool API consumers.
#[async_trait::async_trait]
impl<V, T, S> TransactionPool for Pool<V, T, S> impl<V, T, S> TransactionPool for Pool<V, T, S>
where where
V: TransactionValidator, V: TransactionValidator,

View File

@ -29,7 +29,6 @@ use tokio::sync::{mpsc, mpsc::Receiver};
#[non_exhaustive] #[non_exhaustive]
pub struct NoopTransactionPool; pub struct NoopTransactionPool;
#[async_trait::async_trait]
impl TransactionPool for NoopTransactionPool { impl TransactionPool for NoopTransactionPool {
type Transaction = EthPooledTransaction; type Transaction = EthPooledTransaction;
@ -254,7 +253,6 @@ pub struct MockTransactionValidator<T> {
_marker: PhantomData<T>, _marker: PhantomData<T>,
} }
#[async_trait::async_trait]
impl<T: PoolTransaction> TransactionValidator for MockTransactionValidator<T> { impl<T: PoolTransaction> TransactionValidator for MockTransactionValidator<T> {
type Transaction = T; type Transaction = T;

View File

@ -21,6 +21,7 @@ use serde::{Deserialize, Serialize};
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
fmt, fmt,
future::Future,
pin::Pin, pin::Pin,
sync::Arc, sync::Arc,
task::{Context, Poll}, task::{Context, Poll},
@ -35,7 +36,6 @@ use tokio::sync::mpsc::Receiver;
/// ///
/// Note: This requires `Clone` for convenience, since it is assumed that this will be implemented /// Note: This requires `Clone` for convenience, since it is assumed that this will be implemented
/// for a wrapped `Arc` type, see also [`Pool`](crate::Pool). /// for a wrapped `Arc` type, see also [`Pool`](crate::Pool).
#[async_trait::async_trait]
#[auto_impl::auto_impl(Arc)] #[auto_impl::auto_impl(Arc)]
pub trait TransactionPool: Send + Sync + Clone { pub trait TransactionPool: Send + Sync + Clone {
/// The transaction type of the pool /// The transaction type of the pool
@ -55,19 +55,22 @@ pub trait TransactionPool: Send + Sync + Clone {
/// p2p network. /// p2p network.
/// ///
/// Consumer: P2P /// Consumer: P2P
async fn add_external_transaction(&self, transaction: Self::Transaction) -> PoolResult<TxHash> { fn add_external_transaction(
self.add_transaction(TransactionOrigin::External, transaction).await &self,
transaction: Self::Transaction,
) -> impl Future<Output = PoolResult<TxHash>> + Send {
self.add_transaction(TransactionOrigin::External, transaction)
} }
/// Imports all _external_ transactions /// Imports all _external_ transactions
/// ///
/// ///
/// Consumer: Utility /// Consumer: Utility
async fn add_external_transactions( fn add_external_transactions(
&self, &self,
transactions: Vec<Self::Transaction>, transactions: Vec<Self::Transaction>,
) -> Vec<PoolResult<TxHash>> { ) -> impl Future<Output = Vec<PoolResult<TxHash>>> + Send {
self.add_transactions(TransactionOrigin::External, transactions).await self.add_transactions(TransactionOrigin::External, transactions)
} }
/// Adds an _unvalidated_ transaction into the pool and subscribe to state changes. /// Adds an _unvalidated_ transaction into the pool and subscribe to state changes.
@ -76,31 +79,31 @@ pub trait TransactionPool: Send + Sync + Clone {
/// given transaction. /// given transaction.
/// ///
/// Consumer: Custom /// Consumer: Custom
async fn add_transaction_and_subscribe( fn add_transaction_and_subscribe(
&self, &self,
origin: TransactionOrigin, origin: TransactionOrigin,
transaction: Self::Transaction, transaction: Self::Transaction,
) -> PoolResult<TransactionEvents>; ) -> impl Future<Output = PoolResult<TransactionEvents>> + Send;
/// Adds an _unvalidated_ transaction into the pool. /// Adds an _unvalidated_ transaction into the pool.
/// ///
/// Consumer: RPC /// Consumer: RPC
async fn add_transaction( fn add_transaction(
&self, &self,
origin: TransactionOrigin, origin: TransactionOrigin,
transaction: Self::Transaction, transaction: Self::Transaction,
) -> PoolResult<TxHash>; ) -> impl Future<Output = PoolResult<TxHash>> + Send;
/// Adds the given _unvalidated_ transaction into the pool. /// Adds the given _unvalidated_ transaction into the pool.
/// ///
/// Returns a list of results. /// Returns a list of results.
/// ///
/// Consumer: RPC /// Consumer: RPC
async fn add_transactions( fn add_transactions(
&self, &self,
origin: TransactionOrigin, origin: TransactionOrigin,
transactions: Vec<Self::Transaction>, transactions: Vec<Self::Transaction>,
) -> Vec<PoolResult<TxHash>>; ) -> impl Future<Output = Vec<PoolResult<TxHash>>> + Send;
/// Returns a new transaction change event stream for the given transaction. /// Returns a new transaction change event stream for the given transaction.
/// ///

View File

@ -67,7 +67,6 @@ where
} }
} }
#[async_trait::async_trait]
impl<Client, Tx> TransactionValidator for EthTransactionValidator<Client, Tx> impl<Client, Tx> TransactionValidator for EthTransactionValidator<Client, Tx>
where where
Client: StateProviderFactory + BlockReaderIdExt, Client: StateProviderFactory + BlockReaderIdExt,

View File

@ -9,7 +9,7 @@ use reth_primitives::{
Address, BlobTransactionSidecar, IntoRecoveredTransaction, SealedBlock, Address, BlobTransactionSidecar, IntoRecoveredTransaction, SealedBlock,
TransactionSignedEcRecovered, TxHash, B256, U256, TransactionSignedEcRecovered, TxHash, B256, U256,
}; };
use std::{fmt, time::Instant}; use std::{fmt, future::Future, time::Instant};
mod constants; mod constants;
mod eth; mod eth;
@ -142,7 +142,6 @@ impl<T: PoolTransaction> ValidTransaction<T> {
} }
/// Provides support for validating transaction at any given state of the chain /// Provides support for validating transaction at any given state of the chain
#[async_trait::async_trait]
pub trait TransactionValidator: Send + Sync { pub trait TransactionValidator: Send + Sync {
/// The transaction type to validate. /// The transaction type to validate.
type Transaction: PoolTransaction; type Transaction: PoolTransaction;
@ -172,26 +171,28 @@ pub trait TransactionValidator: Send + Sync {
/// function. /// function.
/// ///
/// See [TransactionValidationTaskExecutor] for a reference implementation. /// See [TransactionValidationTaskExecutor] for a reference implementation.
async fn validate_transaction( fn validate_transaction(
&self, &self,
origin: TransactionOrigin, origin: TransactionOrigin,
transaction: Self::Transaction, transaction: Self::Transaction,
) -> TransactionValidationOutcome<Self::Transaction>; ) -> impl Future<Output = TransactionValidationOutcome<Self::Transaction>> + Send;
/// Validates a batch of transactions. /// Validates a batch of transactions.
/// ///
/// Must return all outcomes for the given transactions in the same order. /// Must return all outcomes for the given transactions in the same order.
/// ///
/// See also [Self::validate_transaction]. /// See also [Self::validate_transaction].
async fn validate_transactions( fn validate_transactions(
&self, &self,
transactions: Vec<(TransactionOrigin, Self::Transaction)>, transactions: Vec<(TransactionOrigin, Self::Transaction)>,
) -> Vec<TransactionValidationOutcome<Self::Transaction>> { ) -> impl Future<Output = Vec<TransactionValidationOutcome<Self::Transaction>>> + Send {
async {
futures_util::future::join_all( futures_util::future::join_all(
transactions.into_iter().map(|(origin, tx)| self.validate_transaction(origin, tx)), transactions.into_iter().map(|(origin, tx)| self.validate_transaction(origin, tx)),
) )
.await .await
} }
}
/// Invoked when the head block changes. /// Invoked when the head block changes.
/// ///

View File

@ -153,7 +153,6 @@ impl<V> TransactionValidationTaskExecutor<V> {
} }
} }
#[async_trait::async_trait]
impl<V> TransactionValidator for TransactionValidationTaskExecutor<V> impl<V> TransactionValidator for TransactionValidationTaskExecutor<V>
where where
V: TransactionValidator + Clone + 'static, V: TransactionValidator + Clone + 'static,

View File

@ -72,7 +72,6 @@ async fn main() -> eyre::Result<()> {
#[non_exhaustive] #[non_exhaustive]
struct OkValidator; struct OkValidator;
#[async_trait::async_trait]
impl TransactionValidator for OkValidator { impl TransactionValidator for OkValidator {
type Transaction = EthPooledTransaction; type Transaction = EthPooledTransaction;