Skip to content

Commit

Permalink
Remove libp2p dependency from sc-network-sync (#4974)
Browse files Browse the repository at this point in the history
## Issue
#4858

## Description
This PR removes `libp2p::request_response::OutboundFailure` from
`substrate/client/network/sync/src/engine.rs`. This way, the dependency
with the library `libp2p` is removed from `sc-network-sync`.

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: command-bot <>
Co-authored-by: Dmitry Markin <dmitry@markin.tech>
Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 18, 2024
1 parent 5524c11 commit 7063395
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 9 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions prdoc/pr_4974.prdoc
Original file line number Diff line number Diff line change
@@ -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
74 changes: 69 additions & 5 deletions substrate/client/network/src/request_responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<request_response::OutboundFailure> 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<request_response::InboundFailure> 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)]
Expand Down Expand Up @@ -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
{
Expand All @@ -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!(
Expand All @@ -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))
Expand All @@ -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))
},
Expand Down
1 change: 0 additions & 1 deletion substrate/client/network/sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 1 addition & 2 deletions substrate/client/network/sync/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 7063395

Please sign in to comment.