Merge Protocol and ProtocolBehaviour (#2962)

* Pass the peerset config to ProtocolBehaviour

* Don't pass the protocol versions

* Move fields from protocol_behaviour.rs to protocol.rs

* Remove LocalNetworkOut

* Move CustomProtos from protocol_behaviour.rs to protocol.rs

* Remove ProtocolBehaviour

* Inline poll()

* Force Behaviour to use Protocol

* Don't even attempt to have working tests

* Remove NetworkOut trait

* Line widths
This commit is contained in:
Pierre Krieger
2019-07-05 16:06:07 +02:00
committed by GitHub
parent 6761ef1655
commit 489a5ce9d7
5 changed files with 408 additions and 774 deletions
+12 -15
View File
@@ -22,11 +22,11 @@ use std::time::Duration;
use log::{warn, error, info};
use libp2p::core::swarm::NetworkBehaviour;
use libp2p::core::{nodes::Substream, transport::boxed::Boxed, muxing::StreamMuxerBox};
use libp2p::core::{transport::boxed::Boxed, muxing::StreamMuxerBox};
use libp2p::{Multiaddr, multihash::Multihash};
use futures::{prelude::*, sync::oneshot, sync::mpsc};
use parking_lot::{Mutex, RwLock};
use crate::protocol_behaviour::ProtocolBehaviour;
use crate::protocol::Protocol;
use crate::{behaviour::{Behaviour, BehaviourOut}, parse_str_addr};
use crate::{NetworkState, NetworkStateNotConnectedPeer, NetworkStatePeer};
use crate::{transport, config::NodeKeyConfig, config::NonReservedPeerMode};
@@ -149,14 +149,13 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkWorker
}
}
// Build the peerset.
let (peerset, peerset_handle) = peerset::Peerset::from_config(peerset::PeersetConfig {
let peerset_config = peerset::PeersetConfig {
in_peers: params.network_config.in_peers,
out_peers: params.network_config.out_peers,
bootnodes,
reserved_only: params.network_config.non_reserved_mode == NonReservedPeerMode::Deny,
reserved_nodes,
});
};
// Private and public keys configuration.
if let NodeKeyConfig::Secp256k1(_) = params.network_config.node_key {
@@ -171,7 +170,7 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkWorker
let is_offline = Arc::new(AtomicBool::new(true));
let is_major_syncing = Arc::new(AtomicBool::new(false));
let peers: Arc<RwLock<HashMap<PeerId, ConnectedPeer<B>>>> = Arc::new(Default::default());
let protocol = ProtocolBehaviour::new(
let (protocol, peerset_handle) = Protocol::new(
protocol::ProtocolConfig { roles: params.roles },
params.chain,
params.on_demand.as_ref().map(|od| od.checker().clone())
@@ -180,9 +179,7 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkWorker
params.transaction_pool,
params.finality_proof_provider,
params.protocol_id,
&((protocol::MIN_VERSION as u8)..=(protocol::CURRENT_VERSION as u8)).collect::<Vec<u8>>(),
peerset,
peerset_handle.clone(),
peerset_config,
)?;
// Build the swarm.
@@ -727,13 +724,13 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> Future for Ne
ProtocolMsg::BlockFinalized(hash, header) =>
network_service.user_protocol_mut().on_block_finalized(hash, &header),
ProtocolMsg::ExecuteWithSpec(task) => {
let (protocol, mut net_out) = network_service.user_protocol_mut().protocol_context_lock();
let (mut context, spec) = protocol.specialization_lock(&mut net_out);
let protocol = network_service.user_protocol_mut();
let (mut context, spec) = protocol.specialization_lock();
task.call_box(spec, &mut context);
},
ProtocolMsg::ExecuteWithGossip(task) => {
let (protocol, mut net_out) = network_service.user_protocol_mut().protocol_context_lock();
let (mut context, gossip) = protocol.consensus_gossip_lock(&mut net_out);
let protocol = network_service.user_protocol_mut();
let (mut context, gossip) = protocol.consensus_gossip_lock();
task.call_box(gossip, &mut context);
}
ProtocolMsg::GossipConsensusMessage(topic, engine_id, message, recipient) =>
@@ -774,7 +771,7 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> Future for Ne
let outcome = match poll_value {
Ok(Async::NotReady) => break,
Ok(Async::Ready(Some(BehaviourOut::Behaviour(outcome)))) => outcome,
Ok(Async::Ready(Some(BehaviourOut::SubstrateAction(outcome)))) => outcome,
Ok(Async::Ready(Some(BehaviourOut::Dht(ev)))) => {
network_service.user_protocol_mut()
.on_event(Event::Dht(ev));
@@ -812,5 +809,5 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> Future for Ne
/// The libp2p swarm, customized for our needs.
type Swarm<B, S, H> = libp2p::core::Swarm<
Boxed<(PeerId, StreamMuxerBox), io::Error>,
Behaviour<ProtocolBehaviour<B, S, H>, CustomMessageOutcome<B>, Substream<StreamMuxerBox>>
Behaviour<B, S, H>
>;