Clean up the public API of sc-network-gossip (#5001)

This commit is contained in:
Pierre Krieger
2020-02-20 13:27:36 +01:00
committed by GitHub
parent 8cdf98c773
commit 5bf644b768
5 changed files with 13 additions and 24 deletions
-1
View File
@@ -6255,7 +6255,6 @@ dependencies = [
name = "sc-network-gossip" name = "sc-network-gossip"
version = "0.8.0" version = "0.8.0"
dependencies = [ dependencies = [
"futures 0.1.29",
"futures 0.3.4", "futures 0.3.4",
"futures-timer 3.0.1", "futures-timer 3.0.1",
"libp2p", "libp2p",
@@ -44,20 +44,12 @@ pub(crate) struct TestNetwork {
sender: mpsc::UnboundedSender<Event>, sender: mpsc::UnboundedSender<Event>,
} }
impl TestNetwork { impl sc_network_gossip::Network<Block> for TestNetwork {
fn event_stream_03(&self) -> Pin<Box<dyn futures::Stream<Item = NetworkEvent> + Send>> { fn event_stream(&self) -> Pin<Box<dyn Stream<Item = NetworkEvent> + Send>> {
let (tx, rx) = mpsc::unbounded(); let (tx, rx) = mpsc::unbounded();
let _ = self.sender.unbounded_send(Event::EventStream(tx)); let _ = self.sender.unbounded_send(Event::EventStream(tx));
Box::pin(rx) Box::pin(rx)
} }
}
impl sc_network_gossip::Network<Block> for TestNetwork {
fn event_stream(&self) -> Box<dyn futures01::Stream<Item = NetworkEvent, Error = ()> + Send> {
Box::new(
self.event_stream_03().map(Ok::<_, ()>).compat()
)
}
fn report_peer(&self, who: sc_network::PeerId, cost_benefit: sc_network::ReputationChange) { fn report_peer(&self, who: sc_network::PeerId, cost_benefit: sc_network::ReputationChange) {
let _ = self.sender.unbounded_send(Event::Report(who, cost_benefit)); let _ = self.sender.unbounded_send(Event::Report(who, cost_benefit));
+3 -4
View File
@@ -7,13 +7,12 @@ authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
log = "0.4.8" futures = "0.3.1"
futures = { version = "0.3.1", features = ["compat"] }
wasm-timer = "0.2"
futures-timer = "3.0.1" futures-timer = "3.0.1"
futures01 = { package = "futures", version = "0.1.29" }
libp2p = { version = "0.16.0", default-features = false, features = ["libp2p-websocket"] } libp2p = { version = "0.16.0", default-features = false, features = ["libp2p-websocket"] }
log = "0.4.8"
lru = "0.1.2" lru = "0.1.2"
parking_lot = "0.10.0" parking_lot = "0.10.0"
sc-network = { version = "0.8", path = "../network" } sc-network = { version = "0.8", path = "../network" }
sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" } sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" }
wasm-timer = "0.2"
@@ -20,8 +20,7 @@ use crate::state_machine::{ConsensusGossip, TopicNotification, PERIODIC_MAINTENA
use sc_network::message::generic::ConsensusMessage; use sc_network::message::generic::ConsensusMessage;
use sc_network::{Event, ReputationChange}; use sc_network::{Event, ReputationChange};
use futures::{prelude::*, channel::mpsc, compat::Compat01As03}; use futures::{prelude::*, channel::mpsc};
use futures01::stream::Stream as Stream01;
use libp2p::PeerId; use libp2p::PeerId;
use parking_lot::Mutex; use parking_lot::Mutex;
use sp_runtime::{traits::Block as BlockT, ConsensusEngineId}; use sp_runtime::{traits::Block as BlockT, ConsensusEngineId};
@@ -38,7 +37,7 @@ struct GossipEngineInner<B: BlockT> {
state_machine: ConsensusGossip<B>, state_machine: ConsensusGossip<B>,
network: Box<dyn Network<B> + Send>, network: Box<dyn Network<B> + Send>,
periodic_maintenance_interval: futures_timer::Delay, periodic_maintenance_interval: futures_timer::Delay,
network_event_stream: Compat01As03<Box<dyn Stream01<Error = (), Item = Event> + Send>>, network_event_stream: Pin<Box<dyn Stream<Item = Event> + Send>>,
engine_id: ConsensusEngineId, engine_id: ConsensusEngineId,
} }
@@ -64,7 +63,7 @@ impl<B: BlockT> GossipEngine<B> {
state_machine, state_machine,
network: Box::new(network), network: Box::new(network),
periodic_maintenance_interval: futures_timer::Delay::new(PERIODIC_MAINTENANCE_INTERVAL), periodic_maintenance_interval: futures_timer::Delay::new(PERIODIC_MAINTENANCE_INTERVAL),
network_event_stream: Compat01As03::new(network_event_stream), network_event_stream,
engine_id, engine_id,
})); }));
@@ -178,7 +177,7 @@ impl<B: BlockT> Future for GossipEngineInner<B> {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = &mut *self; let this = &mut *self;
while let Poll::Ready(Some(Ok(event))) = this.network_event_stream.poll_next_unpin(cx) { while let Poll::Ready(Some(event)) = this.network_event_stream.poll_next_unpin(cx) {
match event { match event {
Event::NotificationStreamOpened { remote, engine_id: msg_engine_id, roles } => { Event::NotificationStreamOpened { remote, engine_id: msg_engine_id, roles } => {
if msg_engine_id != this.engine_id { if msg_engine_id != this.engine_id {
+4 -4
View File
@@ -61,7 +61,7 @@ pub use self::validator::{DiscardAll, MessageIntent, Validator, ValidatorContext
use futures::prelude::*; use futures::prelude::*;
use sc_network::{specialization::NetworkSpecialization, Event, ExHashT, NetworkService, PeerId, ReputationChange}; use sc_network::{specialization::NetworkSpecialization, Event, ExHashT, NetworkService, PeerId, ReputationChange};
use sp_runtime::{traits::Block as BlockT, ConsensusEngineId}; use sp_runtime::{traits::Block as BlockT, ConsensusEngineId};
use std::sync::Arc; use std::{pin::Pin, sync::Arc};
mod bridge; mod bridge;
mod state_machine; mod state_machine;
@@ -70,7 +70,7 @@ mod validator;
/// Abstraction over a network. /// Abstraction over a network.
pub trait Network<B: BlockT> { pub trait Network<B: BlockT> {
/// Returns a stream of events representing what happens on the network. /// Returns a stream of events representing what happens on the network.
fn event_stream(&self) -> Box<dyn futures01::Stream<Item = Event, Error = ()> + Send>; fn event_stream(&self) -> Pin<Box<dyn Stream<Item = Event> + Send>>;
/// Adjust the reputation of a node. /// Adjust the reputation of a node.
fn report_peer(&self, peer_id: PeerId, reputation: ReputationChange); fn report_peer(&self, peer_id: PeerId, reputation: ReputationChange);
@@ -97,8 +97,8 @@ pub trait Network<B: BlockT> {
} }
impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Network<B> for Arc<NetworkService<B, S, H>> { impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Network<B> for Arc<NetworkService<B, S, H>> {
fn event_stream(&self) -> Box<dyn futures01::Stream<Item = Event, Error = ()> + Send> { fn event_stream(&self) -> Pin<Box<dyn Stream<Item = Event> + Send>> {
Box::new(NetworkService::event_stream(self).map(|v| Ok::<_, ()>(v)).compat()) Box::pin(NetworkService::event_stream(self))
} }
fn report_peer(&self, peer_id: PeerId, reputation: ReputationChange) { fn report_peer(&self, peer_id: PeerId, reputation: ReputationChange) {