chore: implement traits directly for futures::Either (#8172)

This commit is contained in:
Matthias Seitz
2024-05-10 12:36:45 +02:00
committed by GitHub
parent b86c6a5911
commit a8bbab2470
4 changed files with 25 additions and 41 deletions

View File

@@ -434,7 +434,7 @@ mod tests {
use assert_matches::assert_matches;
use futures::poll;
use reth_db::{mdbx::DatabaseEnv, test_utils::TempDatabase};
use reth_interfaces::{p2p::either::EitherDownloader, test_utils::TestFullBlockClient};
use reth_interfaces::{p2p::either::Either, test_utils::TestFullBlockClient};
use reth_primitives::{
constants::ETHEREUM_BLOCK_GAS_LIMIT, stage::StageCheckpoint, BlockBody, ChainSpecBuilder,
Header, PruneModes, SealedHeader, MAINNET,
@@ -543,15 +543,15 @@ mod tests {
self,
pipeline: Pipeline<DB>,
chain_spec: Arc<ChainSpec>,
) -> EngineSyncController<DB, EitherDownloader<Client, TestFullBlockClient>>
) -> EngineSyncController<DB, Either<Client, TestFullBlockClient>>
where
DB: Database + 'static,
Client: HeadersClient + BodiesClient + Clone + Unpin + 'static,
{
let client = self
.client
.map(EitherDownloader::Left)
.unwrap_or_else(|| EitherDownloader::Right(TestFullBlockClient::default()));
.map(Either::Left)
.unwrap_or_else(|| Either::Right(TestFullBlockClient::default()));
EngineSyncController::new(
pipeline,

View File

@@ -17,7 +17,7 @@ use reth_ethereum_engine_primitives::EthEngineTypes;
use reth_evm::{either::Either, test_utils::MockExecutorProvider};
use reth_evm_ethereum::execute::EthExecutorProvider;
use reth_interfaces::{
p2p::{bodies::client::BodiesClient, either::EitherDownloader, headers::client::HeadersClient},
p2p::{bodies::client::BodiesClient, headers::client::HeadersClient},
sync::NoopSyncStateUpdater,
test_utils::NoopFullBlockClient,
};
@@ -42,7 +42,7 @@ type DatabaseEnv = TempDatabase<DE>;
type TestBeaconConsensusEngine<Client> = BeaconConsensusEngine<
Arc<DatabaseEnv>,
BlockchainProvider<Arc<DatabaseEnv>>,
Arc<EitherDownloader<Client, NoopFullBlockClient>>,
Arc<Either<Client, NoopFullBlockClient>>,
EthEngineTypes,
>;
@@ -111,7 +111,7 @@ impl<DB> TestEnv<DB> {
}
// TODO: add with_consensus in case we want to use the TestConsensus purposeful failure - this
// would require similar patterns to how we use with_client and the EitherDownloader
// would require similar patterns to how we use with_client and the downloader
/// Represents either a real consensus engine, or a test consensus engine.
#[derive(Debug, Default)]
enum TestConsensusConfig {
@@ -331,8 +331,8 @@ where
// use either noop client or a user provided client (for example TestFullBlockClient)
let client = Arc::new(
self.client
.map(EitherDownloader::Left)
.unwrap_or_else(|| EitherDownloader::Right(NoopFullBlockClient::default())),
.map(Either::Left)
.unwrap_or_else(|| Either::Right(NoopFullBlockClient::default())),
);
// use either test executor or real executor

View File

@@ -1,42 +1,35 @@
//! Support for different download types.
use crate::p2p::{
bodies::client::BodiesClient,
download::DownloadClient,
headers::client::{HeadersClient, HeadersRequest},
priority::Priority,
};
use futures::future::Either;
use reth_primitives::B256;
/// A downloader that combines two different downloaders/client implementations that have the same
/// associated types.
#[derive(Debug, Clone)]
pub enum EitherDownloader<A, B> {
/// The first downloader variant
Left(A),
/// The second downloader variant
Right(B),
}
pub use futures::future::Either;
impl<A, B> DownloadClient for EitherDownloader<A, B>
impl<A, B> DownloadClient for Either<A, B>
where
A: DownloadClient,
B: DownloadClient,
{
fn report_bad_message(&self, peer_id: reth_network_types::PeerId) {
match self {
EitherDownloader::Left(a) => a.report_bad_message(peer_id),
EitherDownloader::Right(b) => b.report_bad_message(peer_id),
Either::Left(a) => a.report_bad_message(peer_id),
Either::Right(b) => b.report_bad_message(peer_id),
}
}
fn num_connected_peers(&self) -> usize {
match self {
EitherDownloader::Left(a) => a.num_connected_peers(),
EitherDownloader::Right(b) => b.num_connected_peers(),
Either::Left(a) => a.num_connected_peers(),
Either::Right(b) => b.num_connected_peers(),
}
}
}
impl<A, B> BodiesClient for EitherDownloader<A, B>
impl<A, B> BodiesClient for Either<A, B>
where
A: BodiesClient,
B: BodiesClient,
@@ -49,17 +42,13 @@ where
priority: Priority,
) -> Self::Output {
match self {
EitherDownloader::Left(a) => {
Either::Left(a.get_block_bodies_with_priority(hashes, priority))
}
EitherDownloader::Right(b) => {
Either::Right(b.get_block_bodies_with_priority(hashes, priority))
}
Either::Left(a) => Either::Left(a.get_block_bodies_with_priority(hashes, priority)),
Either::Right(b) => Either::Right(b.get_block_bodies_with_priority(hashes, priority)),
}
}
}
impl<A, B> HeadersClient for EitherDownloader<A, B>
impl<A, B> HeadersClient for Either<A, B>
where
A: HeadersClient,
B: HeadersClient,
@@ -72,12 +61,8 @@ where
priority: Priority,
) -> Self::Output {
match self {
EitherDownloader::Left(a) => {
Either::Left(a.get_headers_with_priority(request, priority))
}
EitherDownloader::Right(b) => {
Either::Right(b.get_headers_with_priority(request, priority))
}
Either::Left(a) => Either::Left(a.get_headers_with_priority(request, priority)),
Either::Right(b) => Either::Right(b.get_headers_with_priority(request, priority)),
}
}
}

View File

@@ -19,7 +19,6 @@ use reth_blockchain_tree::{
};
use reth_consensus::Consensus;
use reth_exex::{ExExContext, ExExHandle, ExExManager, ExExManagerHandle};
use reth_interfaces::p2p::either::EitherDownloader;
use reth_network::NetworkEvents;
use reth_node_api::{FullNodeComponents, FullNodeTypes};
use reth_node_core::{
@@ -327,7 +326,7 @@ where
debug!(target: "reth::cli", "Spawning auto mine task");
ctx.task_executor().spawn(Box::pin(task));
(pipeline, EitherDownloader::Left(client))
(pipeline, Either::Left(client))
} else {
let pipeline = crate::setup::build_networked_pipeline(
ctx.node_config(),
@@ -345,7 +344,7 @@ where
)
.await?;
(pipeline, EitherDownloader::Right(network_client.clone()))
(pipeline, Either::Right(network_client.clone()))
};
let pipeline_events = pipeline.events();