diff --git a/Cargo.lock b/Cargo.lock index 3985f9f994b3..50e42667b942 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19275,7 +19275,6 @@ dependencies = [ "fork-tree", "futures", "futures-timer", - "libp2p", "log", "mockall 0.11.4", "parity-scale-codec", diff --git a/prdoc/pr_4974.prdoc b/prdoc/pr_4974.prdoc new file mode 100644 index 000000000000..f764ea3f46fd --- /dev/null +++ b/prdoc/pr_4974.prdoc @@ -0,0 +1,15 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: "Remove libp2p dependency from sc-network-sync" + +doc: + - audience: Node Dev + description: | + This PR removes `libp2p::request_response::OutboundFailure` from `substrate/client/network/sync/src/engine.rs`. + +crates: + - name: sc-network + bump: patch + - name: sc-network-sync + bump: patch diff --git a/substrate/client/network/src/request_responses.rs b/substrate/client/network/src/request_responses.rs index 3671d76ea630..6c2631924df4 100644 --- a/substrate/client/network/src/request_responses.rs +++ b/substrate/client/network/src/request_responses.rs @@ -64,7 +64,69 @@ use std::{ time::{Duration, Instant}, }; -pub use libp2p::request_response::{Config, InboundFailure, OutboundFailure, RequestId}; +pub use libp2p::request_response::{Config, RequestId}; + +/// Possible failures occurring in the context of sending an outbound request and receiving the +/// response. +#[derive(Debug, thiserror::Error)] +pub enum OutboundFailure { + /// The request could not be sent because a dialing attempt failed. + #[error("Failed to dial the requested peer")] + DialFailure, + /// The request timed out before a response was received. + #[error("Timeout while waiting for a response")] + Timeout, + /// The connection closed before a response was received. + #[error("Connection was closed before a response was received")] + ConnectionClosed, + /// The remote supports none of the requested protocols. + #[error("The remote supports none of the requested protocols")] + UnsupportedProtocols, +} + +impl From for OutboundFailure { + fn from(out: request_response::OutboundFailure) -> Self { + match out { + request_response::OutboundFailure::DialFailure => OutboundFailure::DialFailure, + request_response::OutboundFailure::Timeout => OutboundFailure::Timeout, + request_response::OutboundFailure::ConnectionClosed => + OutboundFailure::ConnectionClosed, + request_response::OutboundFailure::UnsupportedProtocols => + OutboundFailure::UnsupportedProtocols, + } + } +} + +/// Possible failures occurring in the context of receiving an inbound request and sending a +/// response. +#[derive(Debug, thiserror::Error)] +pub enum InboundFailure { + /// The inbound request timed out, either while reading the incoming request or before a + /// response is sent + #[error("Timeout while receiving request or sending response")] + Timeout, + /// The connection closed before a response could be send. + #[error("Connection was closed before a response could be sent")] + ConnectionClosed, + /// The local peer supports none of the protocols requested by the remote. + #[error("The local peer supports none of the protocols requested by the remote")] + UnsupportedProtocols, + /// The local peer failed to respond to an inbound request + #[error("The response channel was dropped without sending a response to the remote")] + ResponseOmission, +} + +impl From for InboundFailure { + fn from(out: request_response::InboundFailure) -> Self { + match out { + request_response::InboundFailure::ResponseOmission => InboundFailure::ResponseOmission, + request_response::InboundFailure::Timeout => InboundFailure::Timeout, + request_response::InboundFailure::ConnectionClosed => InboundFailure::ConnectionClosed, + request_response::InboundFailure::UnsupportedProtocols => + InboundFailure::UnsupportedProtocols, + } + } +} /// Error in a request. #[derive(Debug, thiserror::Error)] @@ -808,7 +870,9 @@ impl NetworkBehaviour for RequestResponsesBehaviour { }) => { // Try using the fallback request if the protocol was not // supported. - if let OutboundFailure::UnsupportedProtocols = error { + if let request_response::OutboundFailure::UnsupportedProtocols = + error + { if let Some((fallback_request, fallback_protocol)) = fallback_request { @@ -829,7 +893,7 @@ impl NetworkBehaviour for RequestResponsesBehaviour { } if response_tx - .send(Err(RequestFailure::Network(error.clone()))) + .send(Err(RequestFailure::Network(error.clone().into()))) .is_err() { log::debug!( @@ -856,7 +920,7 @@ impl NetworkBehaviour for RequestResponsesBehaviour { peer, protocol: protocol.clone(), duration: started.elapsed(), - result: Err(RequestFailure::Network(error)), + result: Err(RequestFailure::Network(error.into())), }; return Poll::Ready(ToSwarm::GenerateEvent(out)) @@ -873,7 +937,7 @@ impl NetworkBehaviour for RequestResponsesBehaviour { let out = Event::InboundRequest { peer, protocol: protocol.clone(), - result: Err(ResponseFailure::Network(error)), + result: Err(ResponseFailure::Network(error.into())), }; return Poll::Ready(ToSwarm::GenerateEvent(out)) }, diff --git a/substrate/client/network/sync/Cargo.toml b/substrate/client/network/sync/Cargo.toml index b29a9ccaaf1a..378b7c12e9b7 100644 --- a/substrate/client/network/sync/Cargo.toml +++ b/substrate/client/network/sync/Cargo.toml @@ -25,7 +25,6 @@ async-trait = { workspace = true } codec = { features = ["derive"], workspace = true, default-features = true } futures = { workspace = true } futures-timer = { workspace = true } -libp2p = { workspace = true } log = { workspace = true, default-features = true } mockall = { workspace = true } prost = { workspace = true } diff --git a/substrate/client/network/sync/src/engine.rs b/substrate/client/network/sync/src/engine.rs index aafbd950202d..96c1750b3116 100644 --- a/substrate/client/network/sync/src/engine.rs +++ b/substrate/client/network/sync/src/engine.rs @@ -42,7 +42,6 @@ use crate::{ use codec::{Decode, DecodeAll, Encode}; use futures::{channel::oneshot, FutureExt, StreamExt}; -use libp2p::request_response::OutboundFailure; use log::{debug, error, trace, warn}; use prometheus_endpoint::{ register, Counter, Gauge, MetricSource, Opts, PrometheusError, Registry, SourcedGauge, U64, @@ -56,7 +55,7 @@ use sc_consensus::{import_queue::ImportQueueService, IncomingBlock}; use sc_network::{ config::{FullNetworkConfiguration, NotificationHandshake, ProtocolId, SetConfig}, peer_store::PeerStoreProvider, - request_responses::{IfDisconnected, RequestFailure}, + request_responses::{IfDisconnected, OutboundFailure, RequestFailure}, service::{ traits::{Direction, NotificationConfig, NotificationEvent, ValidationResult}, NotificationMetrics,