Use bounded channels for network -> sync -> import (#1874)

* use bounded channels for network -> sync -> import

* bound at 4

* indent

* use return value of handle_network_msg
This commit is contained in:
Gregory Terzian
2019-02-27 16:19:57 +08:00
committed by Gav Wood
parent 64685c0536
commit 5282615416
4 changed files with 100 additions and 61 deletions
+11 -7
View File
@@ -45,7 +45,7 @@ use network_libp2p::{NodeIndex, ProtocolId};
use parity_codec::Encode;
use parking_lot::Mutex;
use primitives::{H256, Ed25519AuthorityId};
use crate::protocol::{Context, Protocol, ProtocolMsg};
use crate::protocol::{Context, FromNetworkMsg, Protocol, ProtocolMsg};
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{AuthorityIdFor, Block as BlockT, Digest, DigestItem, Header, NumberFor};
use runtime_primitives::Justification;
@@ -121,6 +121,7 @@ pub struct Peer<D> {
pub is_offline: Arc<AtomicBool>,
pub is_major_syncing: Arc<AtomicBool>,
client: Arc<PeersClient>,
network_to_protocol_sender: Sender<FromNetworkMsg<Block>>,
pub protocol_sender: Sender<ProtocolMsg<Block, DummySpecialization>>,
network_port: Mutex<NetworkPort<Block>>,
@@ -137,6 +138,7 @@ impl<D> Peer<D> {
is_major_syncing: Arc<AtomicBool>,
client: Arc<PeersClient>,
import_queue: Box<ImportQueue<Block>>,
network_to_protocol_sender: Sender<FromNetworkMsg<Block>>,
protocol_sender: Sender<ProtocolMsg<Block, DummySpecialization>>,
network_sender: NetworkChan<Block>,
network_port: NetworkPort<Block>,
@@ -147,6 +149,7 @@ impl<D> Peer<D> {
is_offline,
is_major_syncing,
client,
network_to_protocol_sender,
protocol_sender,
import_queue,
network_sender,
@@ -198,21 +201,21 @@ impl<D> Peer<D> {
/// Called on connection to other indicated peer.
fn on_connect(&self, other: NodeIndex) {
let _ = self.protocol_sender.send(ProtocolMsg::PeerConnected(other, String::new()));
let _ = self.network_to_protocol_sender.send(FromNetworkMsg::PeerConnected(other, String::new()));
}
/// Called on disconnect from other indicated peer.
fn on_disconnect(&self, other: NodeIndex) {
let _ = self
.protocol_sender
.send(ProtocolMsg::PeerDisconnected(other, String::new()));
.network_to_protocol_sender
.send(FromNetworkMsg::PeerDisconnected(other, String::new()));
}
/// Receive a message from another peer. Return a set of peers to disconnect.
fn receive_message(&self, from: NodeIndex, msg: Message<Block>) {
let _ = self
.protocol_sender
.send(ProtocolMsg::CustomMessage(from, msg));
.network_to_protocol_sender
.send(FromNetworkMsg::CustomMessage(from, msg));
}
/// Produce the next pending message to send to another peer.
@@ -480,7 +483,7 @@ pub trait TestNetFactory: Sized {
let specialization = DummySpecialization {};
let is_offline = Arc::new(AtomicBool::new(true));
let is_major_syncing = Arc::new(AtomicBool::new(false));
let protocol_sender = Protocol::new(
let (protocol_sender, network_to_protocol_sender) = Protocol::new(
is_offline.clone(),
is_major_syncing.clone(),
network_sender.clone(),
@@ -497,6 +500,7 @@ pub trait TestNetFactory: Sized {
is_major_syncing,
client,
import_queue,
network_to_protocol_sender,
protocol_sender,
network_sender,
network_port,