Move block/state/warpc sync requests/responses to ChainSync (#12739)

* Move block/state/warpc sync requests/responses to `ChainSync`

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* Apply review suggestions

* cargo-fmt + doc fix

* Fix tests

Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
Aaro Altonen
2022-11-22 10:19:17 +02:00
committed by GitHub
parent 4cb24da8f2
commit 1b5d52deb2
16 changed files with 1094 additions and 1126 deletions
@@ -16,13 +16,16 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use sc_network_common::service::{NetworkPeers, NetworkSyncForkRequest};
use sp_runtime::traits::{Block as BlockT, NumberFor};
pub use libp2p::{identity::error::SigningError, kad::record::Key as KademliaKey};
use futures::channel::oneshot;
use libp2p::{Multiaddr, PeerId};
use sc_network_common::{config::MultiaddrWithPeerId, protocol::ProtocolName};
use sc_network_common::{
config::MultiaddrWithPeerId,
protocol::ProtocolName,
request_responses::{IfDisconnected, RequestFailure},
service::{NetworkPeers, NetworkRequest, NetworkSyncForkRequest},
};
use sc_peerset::ReputationChange;
use sp_runtime::traits::{Block as BlockT, NumberFor};
use std::collections::HashSet;
mockall::mock! {
@@ -72,4 +75,23 @@ mockall::mock! {
fn remove_from_peers_set(&self, protocol: ProtocolName, peers: Vec<PeerId>);
fn sync_num_connected(&self) -> usize;
}
#[async_trait::async_trait]
impl NetworkRequest for Network {
async fn request(
&self,
target: PeerId,
protocol: ProtocolName,
request: Vec<u8>,
connect: IfDisconnected,
) -> Result<Vec<u8>, RequestFailure>;
fn start_request(
&self,
target: PeerId,
protocol: ProtocolName,
request: Vec<u8>,
tx: oneshot::Sender<Result<Vec<u8>, RequestFailure>>,
connect: IfDisconnected,
);
}
}
@@ -16,17 +16,21 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use futures::StreamExt;
use futures::{channel::oneshot, StreamExt};
use libp2p::PeerId;
use sc_network_common::{protocol::ProtocolName, service::NetworkPeers};
use sc_network_common::{
protocol::ProtocolName,
request_responses::{IfDisconnected, RequestFailure},
service::{NetworkPeers, NetworkRequest},
};
use sc_peerset::ReputationChange;
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use std::sync::Arc;
/// Network-related services required by `sc-network-sync`
pub trait Network: NetworkPeers {}
pub trait Network: NetworkPeers + NetworkRequest {}
impl<T> Network for T where T: NetworkPeers {}
impl<T> Network for T where T: NetworkPeers + NetworkRequest {}
/// Network service provider for `ChainSync`
///
@@ -43,6 +47,15 @@ pub enum ToServiceCommand {
/// Call `NetworkPeers::report_peer()`
ReportPeer(PeerId, ReputationChange),
/// Call `NetworkRequest::start_request()`
StartRequest(
PeerId,
ProtocolName,
Vec<u8>,
oneshot::Sender<Result<Vec<u8>, RequestFailure>>,
IfDisconnected,
),
}
/// Handle that is (temporarily) passed to `ChainSync` so it can
@@ -67,6 +80,20 @@ impl NetworkServiceHandle {
pub fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName) {
let _ = self.tx.unbounded_send(ToServiceCommand::DisconnectPeer(who, protocol));
}
/// Send request to peer
pub fn start_request(
&self,
who: PeerId,
protocol: ProtocolName,
request: Vec<u8>,
tx: oneshot::Sender<Result<Vec<u8>, RequestFailure>>,
connect: IfDisconnected,
) {
let _ = self
.tx
.unbounded_send(ToServiceCommand::StartRequest(who, protocol, request, tx, connect));
}
}
impl NetworkServiceProvider {
@@ -85,6 +112,8 @@ impl NetworkServiceProvider {
service.disconnect_peer(peer, protocol_name),
ToServiceCommand::ReportPeer(peer, reputation_change) =>
service.report_peer(peer, reputation_change),
ToServiceCommand::StartRequest(peer, protocol, request, tx, connect) =>
service.start_request(peer, protocol, request, tx, connect),
}
}
}