From 323bad2718866534eda0e61170996f7e37bfb0c2 Mon Sep 17 00:00:00 2001 From: Abner Zheng Date: Mon, 26 Feb 2024 21:23:00 +0800 Subject: [PATCH] Replace async trait with ->impl Future (#6791) --- Cargo.lock | 2 -- crates/net/network/Cargo.toml | 1 - .../tests/it/clique/clique_middleware.rs | 2 -- crates/rpc/rpc-api/src/admin.rs | 1 - crates/rpc/rpc-api/src/bundle.rs | 2 -- crates/rpc/rpc-api/src/engine.rs | 1 - crates/rpc/rpc-api/src/eth.rs | 1 - crates/rpc/rpc-api/src/mev.rs | 1 - crates/rpc/rpc-api/src/txpool.rs | 1 - crates/rpc/rpc-api/src/validation.rs | 1 - crates/rpc/rpc-api/src/web3.rs | 1 - crates/transaction-pool/Cargo.toml | 1 - crates/transaction-pool/src/lib.rs | 1 - crates/transaction-pool/src/noop.rs | 2 -- crates/transaction-pool/src/traits.rs | 27 ++++++++++--------- crates/transaction-pool/src/validate/eth.rs | 1 - crates/transaction-pool/src/validate/mod.rs | 21 ++++++++------- crates/transaction-pool/src/validate/task.rs | 1 - examples/network-txpool.rs | 1 - 19 files changed, 26 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 08ac206a9..1e212fe9b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6248,7 +6248,6 @@ dependencies = [ "alloy-node-bindings", "alloy-rlp", "aquamarine", - "async-trait", "auto_impl", "criterion", "derive_more", @@ -6949,7 +6948,6 @@ dependencies = [ "alloy-rlp", "aquamarine", "assert_matches", - "async-trait", "auto_impl", "bitflags 2.4.2", "criterion", diff --git a/crates/net/network/Cargo.toml b/crates/net/network/Cargo.toml index 7e60a3c09..cacd2e663 100644 --- a/crates/net/network/Cargo.toml +++ b/crates/net/network/Cargo.toml @@ -60,7 +60,6 @@ tracing.workspace = true fnv = "1.0" thiserror.workspace = true parking_lot.workspace = true -async-trait.workspace = true linked_hash_set = "0.1" linked-hash-map = "0.5.6" rand.workspace = true diff --git a/crates/net/network/tests/it/clique/clique_middleware.rs b/crates/net/network/tests/it/clique/clique_middleware.rs index 7e21a20bc..12faf3b27 100644 --- a/crates/net/network/tests/it/clique/clique_middleware.rs +++ b/crates/net/network/tests/it/clique/clique_middleware.rs @@ -1,7 +1,6 @@ #![allow(unreachable_pub)] //! Helper extension traits for working with clique providers. -use async_trait::async_trait; use enr::k256::ecdsa::SigningKey; use ethers_core::{ types::{transaction::eip2718::TypedTransaction, Address, Block, BlockNumber, H256}, @@ -51,7 +50,6 @@ pub type CliqueMiddlewareError = CliqueError<::Error>; /// Extension trait for [`Middleware`](ethers_providers::Middleware) to provide clique specific /// functionality. -#[async_trait(?Send)] pub trait CliqueMiddleware: Send + Sync + Middleware { /// Enable mining on the clique geth instance by importing and unlocking the signer account /// derived from given private key and password. diff --git a/crates/rpc/rpc-api/src/admin.rs b/crates/rpc/rpc-api/src/admin.rs index b7656953b..624780113 100644 --- a/crates/rpc/rpc-api/src/admin.rs +++ b/crates/rpc/rpc-api/src/admin.rs @@ -5,7 +5,6 @@ use reth_rpc_types::{NodeInfo, PeerInfo}; /// Admin namespace rpc interface that gives access to several non-standard RPC methods. #[cfg_attr(not(feature = "client"), rpc(server, namespace = "admin"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "admin"))] -#[async_trait::async_trait] pub trait AdminApi { /// Adds the given node record to the peerset. #[method(name = "addPeer")] diff --git a/crates/rpc/rpc-api/src/bundle.rs b/crates/rpc/rpc-api/src/bundle.rs index bb0734902..429f6948f 100644 --- a/crates/rpc/rpc-api/src/bundle.rs +++ b/crates/rpc/rpc-api/src/bundle.rs @@ -12,7 +12,6 @@ use reth_rpc_types::{ /// A subset of the [EthBundleApi] API interface that only supports `eth_callBundle`. #[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))] -#[async_trait::async_trait] pub trait EthCallBundleApi { /// `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. @@ -28,7 +27,6 @@ pub trait EthCallBundleApi { /// See also #[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))] -#[async_trait::async_trait] pub trait EthBundleApi { /// `eth_sendBundle` can be used to send your bundles to the builder. #[method(name = "sendBundle")] diff --git a/crates/rpc/rpc-api/src/engine.rs b/crates/rpc/rpc-api/src/engine.rs index 6320afc84..50f4b89b0 100644 --- a/crates/rpc/rpc-api/src/engine.rs +++ b/crates/rpc/rpc-api/src/engine.rs @@ -165,7 +165,6 @@ pub trait EngineApi { /// Specifically for the engine auth server: #[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))] -#[async_trait] pub trait EngineEthApi { /// Returns an object with data about the sync status or false. #[method(name = "syncing")] diff --git a/crates/rpc/rpc-api/src/eth.rs b/crates/rpc/rpc-api/src/eth.rs index ff12af3ec..88923df60 100644 --- a/crates/rpc/rpc-api/src/eth.rs +++ b/crates/rpc/rpc-api/src/eth.rs @@ -12,7 +12,6 @@ use reth_rpc_types::{ /// Eth rpc interface: #[cfg_attr(not(feature = "client"), rpc(server, namespace = "eth"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "eth"))] -#[async_trait] pub trait EthApi { /// Returns the protocol version encoded as a string. #[method(name = "protocolVersion")] diff --git a/crates/rpc/rpc-api/src/mev.rs b/crates/rpc/rpc-api/src/mev.rs index 774ff9349..008535276 100644 --- a/crates/rpc/rpc-api/src/mev.rs +++ b/crates/rpc/rpc-api/src/mev.rs @@ -6,7 +6,6 @@ use reth_rpc_types::{ /// Mev rpc interface. #[cfg_attr(not(feature = "client"), rpc(server, namespace = "mev"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "mev"))] -#[async_trait::async_trait] pub trait MevApi { /// Submitting bundles to the relay. It takes in a bundle and provides a bundle hash as a /// return value. diff --git a/crates/rpc/rpc-api/src/txpool.rs b/crates/rpc/rpc-api/src/txpool.rs index 550b21e26..0fd9c9515 100644 --- a/crates/rpc/rpc-api/src/txpool.rs +++ b/crates/rpc/rpc-api/src/txpool.rs @@ -5,7 +5,6 @@ use reth_rpc_types::txpool::{TxpoolContent, TxpoolContentFrom, TxpoolInspect, Tx /// Txpool rpc interface. #[cfg_attr(not(feature = "client"), rpc(server, namespace = "txpool"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "txpool"))] -#[async_trait::async_trait] pub trait TxPoolApi { /// 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. diff --git a/crates/rpc/rpc-api/src/validation.rs b/crates/rpc/rpc-api/src/validation.rs index a59926190..deff73e47 100644 --- a/crates/rpc/rpc-api/src/validation.rs +++ b/crates/rpc/rpc-api/src/validation.rs @@ -6,7 +6,6 @@ use reth_rpc_types::relay::{BuilderBlockValidationRequest, BuilderBlockValidatio /// Block validation rpc interface. #[cfg_attr(not(feature = "client"), rpc(server, namespace = "flashbots"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "flashbots"))] -#[async_trait::async_trait] pub trait BlockSubmissionValidationApi { /// A Request to validate a block submission. #[method(name = "validateBuilderSubmissionV1")] diff --git a/crates/rpc/rpc-api/src/web3.rs b/crates/rpc/rpc-api/src/web3.rs index 36d221156..cf3887cbc 100644 --- a/crates/rpc/rpc-api/src/web3.rs +++ b/crates/rpc/rpc-api/src/web3.rs @@ -4,7 +4,6 @@ use reth_primitives::{Bytes, B256}; /// Web3 rpc interface. #[cfg_attr(not(feature = "client"), rpc(server, namespace = "web3"))] #[cfg_attr(feature = "client", rpc(server, client, namespace = "web3"))] -#[async_trait::async_trait] pub trait Web3Api { /// Returns current client version. #[method(name = "clientVersion")] diff --git a/crates/transaction-pool/Cargo.toml b/crates/transaction-pool/Cargo.toml index 41dece430..faf8a541d 100644 --- a/crates/transaction-pool/Cargo.toml +++ b/crates/transaction-pool/Cargo.toml @@ -29,7 +29,6 @@ alloy-rlp.workspace = true reth-revm = { workspace = true, optional = true } # async/futures -async-trait.workspace = true futures-util.workspace = true parking_lot.workspace = true tokio = { workspace = true, default-features = false, features = ["sync"] } diff --git a/crates/transaction-pool/src/lib.rs b/crates/transaction-pool/src/lib.rs index c9e46252c..b15bac5d7 100644 --- a/crates/transaction-pool/src/lib.rs +++ b/crates/transaction-pool/src/lib.rs @@ -318,7 +318,6 @@ where } /// implements the `TransactionPool` interface for various transaction pool API consumers. -#[async_trait::async_trait] impl TransactionPool for Pool where V: TransactionValidator, diff --git a/crates/transaction-pool/src/noop.rs b/crates/transaction-pool/src/noop.rs index 98395d294..fdf037b76 100644 --- a/crates/transaction-pool/src/noop.rs +++ b/crates/transaction-pool/src/noop.rs @@ -29,7 +29,6 @@ use tokio::sync::{mpsc, mpsc::Receiver}; #[non_exhaustive] pub struct NoopTransactionPool; -#[async_trait::async_trait] impl TransactionPool for NoopTransactionPool { type Transaction = EthPooledTransaction; @@ -254,7 +253,6 @@ pub struct MockTransactionValidator { _marker: PhantomData, } -#[async_trait::async_trait] impl TransactionValidator for MockTransactionValidator { type Transaction = T; diff --git a/crates/transaction-pool/src/traits.rs b/crates/transaction-pool/src/traits.rs index fd06c912d..53ffa668b 100644 --- a/crates/transaction-pool/src/traits.rs +++ b/crates/transaction-pool/src/traits.rs @@ -21,6 +21,7 @@ use serde::{Deserialize, Serialize}; use std::{ collections::{HashMap, HashSet}, fmt, + future::Future, pin::Pin, sync::Arc, 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 /// for a wrapped `Arc` type, see also [`Pool`](crate::Pool). -#[async_trait::async_trait] #[auto_impl::auto_impl(Arc)] pub trait TransactionPool: Send + Sync + Clone { /// The transaction type of the pool @@ -55,19 +55,22 @@ pub trait TransactionPool: Send + Sync + Clone { /// p2p network. /// /// Consumer: P2P - async fn add_external_transaction(&self, transaction: Self::Transaction) -> PoolResult { - self.add_transaction(TransactionOrigin::External, transaction).await + fn add_external_transaction( + &self, + transaction: Self::Transaction, + ) -> impl Future> + Send { + self.add_transaction(TransactionOrigin::External, transaction) } /// Imports all _external_ transactions /// /// /// Consumer: Utility - async fn add_external_transactions( + fn add_external_transactions( &self, transactions: Vec, - ) -> Vec> { - self.add_transactions(TransactionOrigin::External, transactions).await + ) -> impl Future>> + Send { + self.add_transactions(TransactionOrigin::External, transactions) } /// Adds an _unvalidated_ transaction into the pool and subscribe to state changes. @@ -76,31 +79,31 @@ pub trait TransactionPool: Send + Sync + Clone { /// given transaction. /// /// Consumer: Custom - async fn add_transaction_and_subscribe( + fn add_transaction_and_subscribe( &self, origin: TransactionOrigin, transaction: Self::Transaction, - ) -> PoolResult; + ) -> impl Future> + Send; /// Adds an _unvalidated_ transaction into the pool. /// /// Consumer: RPC - async fn add_transaction( + fn add_transaction( &self, origin: TransactionOrigin, transaction: Self::Transaction, - ) -> PoolResult; + ) -> impl Future> + Send; /// Adds the given _unvalidated_ transaction into the pool. /// /// Returns a list of results. /// /// Consumer: RPC - async fn add_transactions( + fn add_transactions( &self, origin: TransactionOrigin, transactions: Vec, - ) -> Vec>; + ) -> impl Future>> + Send; /// Returns a new transaction change event stream for the given transaction. /// diff --git a/crates/transaction-pool/src/validate/eth.rs b/crates/transaction-pool/src/validate/eth.rs index 70f19ad84..b8cfa49ca 100644 --- a/crates/transaction-pool/src/validate/eth.rs +++ b/crates/transaction-pool/src/validate/eth.rs @@ -67,7 +67,6 @@ where } } -#[async_trait::async_trait] impl TransactionValidator for EthTransactionValidator where Client: StateProviderFactory + BlockReaderIdExt, diff --git a/crates/transaction-pool/src/validate/mod.rs b/crates/transaction-pool/src/validate/mod.rs index f2966745e..5d0650a45 100644 --- a/crates/transaction-pool/src/validate/mod.rs +++ b/crates/transaction-pool/src/validate/mod.rs @@ -9,7 +9,7 @@ use reth_primitives::{ Address, BlobTransactionSidecar, IntoRecoveredTransaction, SealedBlock, TransactionSignedEcRecovered, TxHash, B256, U256, }; -use std::{fmt, time::Instant}; +use std::{fmt, future::Future, time::Instant}; mod constants; mod eth; @@ -142,7 +142,6 @@ impl ValidTransaction { } /// Provides support for validating transaction at any given state of the chain -#[async_trait::async_trait] pub trait TransactionValidator: Send + Sync { /// The transaction type to validate. type Transaction: PoolTransaction; @@ -172,25 +171,27 @@ pub trait TransactionValidator: Send + Sync { /// function. /// /// See [TransactionValidationTaskExecutor] for a reference implementation. - async fn validate_transaction( + fn validate_transaction( &self, origin: TransactionOrigin, transaction: Self::Transaction, - ) -> TransactionValidationOutcome; + ) -> impl Future> + Send; /// Validates a batch of transactions. /// /// Must return all outcomes for the given transactions in the same order. /// /// See also [Self::validate_transaction]. - async fn validate_transactions( + fn validate_transactions( &self, transactions: Vec<(TransactionOrigin, Self::Transaction)>, - ) -> Vec> { - futures_util::future::join_all( - transactions.into_iter().map(|(origin, tx)| self.validate_transaction(origin, tx)), - ) - .await + ) -> impl Future>> + Send { + async { + futures_util::future::join_all( + transactions.into_iter().map(|(origin, tx)| self.validate_transaction(origin, tx)), + ) + .await + } } /// Invoked when the head block changes. diff --git a/crates/transaction-pool/src/validate/task.rs b/crates/transaction-pool/src/validate/task.rs index c54a80201..19c1085bf 100644 --- a/crates/transaction-pool/src/validate/task.rs +++ b/crates/transaction-pool/src/validate/task.rs @@ -153,7 +153,6 @@ impl TransactionValidationTaskExecutor { } } -#[async_trait::async_trait] impl TransactionValidator for TransactionValidationTaskExecutor where V: TransactionValidator + Clone + 'static, diff --git a/examples/network-txpool.rs b/examples/network-txpool.rs index d0e29e490..0af120a89 100644 --- a/examples/network-txpool.rs +++ b/examples/network-txpool.rs @@ -72,7 +72,6 @@ async fn main() -> eyre::Result<()> { #[non_exhaustive] struct OkValidator; -#[async_trait::async_trait] impl TransactionValidator for OkValidator { type Transaction = EthPooledTransaction;