Embed the topology in the NetworkBehaviour (#889)

* Embed the topology in the NetworkBehaviour

* Put topologies inside of Floodsub and Kad

* Fix core tests

* Fix chat example

* More work

* Some cleanup

* Restore external addresses system
This commit is contained in:
Pierre Krieger
2019-01-26 23:57:53 +01:00
committed by GitHub
parent 30c082dfe5
commit df923526ca
21 changed files with 818 additions and 749 deletions

View File

@ -21,11 +21,10 @@
use crate::listen_handler::IdentifyListenHandler;
use crate::periodic_id_handler::{PeriodicIdHandler, PeriodicIdHandlerEvent};
use crate::protocol::{IdentifyInfo, IdentifySender, IdentifySenderFuture};
use crate::topology::IdentifyTopology;
use futures::prelude::*;
use libp2p_core::protocols_handler::{ProtocolsHandler, ProtocolsHandlerSelect, ProtocolsHandlerUpgrErr};
use libp2p_core::swarm::{ConnectedPoint, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use libp2p_core::{Multiaddr, PeerId, either::EitherOutput};
use libp2p_core::{Multiaddr, PeerId, PublicKey, either::EitherOutput};
use smallvec::SmallVec;
use std::{collections::HashMap, collections::VecDeque, io};
use tokio_io::{AsyncRead, AsyncWrite};
@ -38,6 +37,8 @@ pub struct Identify<TSubstream> {
protocol_version: String,
/// Agent version to send back to remotes.
agent_version: String,
/// The public key of the local node. To report on the wire.
local_public_key: PublicKey,
/// For each peer we're connected to, the observed address to send back to it.
observed_addresses: HashMap<PeerId, Multiaddr>,
/// List of senders to answer, with the observed multiaddr.
@ -50,10 +51,11 @@ pub struct Identify<TSubstream> {
impl<TSubstream> Identify<TSubstream> {
/// Creates a `Identify`.
pub fn new(protocol_version: String, agent_version: String) -> Self {
pub fn new(protocol_version: String, agent_version: String, local_public_key: PublicKey) -> Self {
Identify {
protocol_version,
agent_version,
local_public_key,
observed_addresses: HashMap::new(),
to_answer: SmallVec::new(),
futures: SmallVec::new(),
@ -62,10 +64,9 @@ impl<TSubstream> Identify<TSubstream> {
}
}
impl<TSubstream, TTopology> NetworkBehaviour<TTopology> for Identify<TSubstream>
impl<TSubstream> NetworkBehaviour for Identify<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite,
TTopology: IdentifyTopology,
{
type ProtocolsHandler = ProtocolsHandlerSelect<IdentifyListenHandler<TSubstream>, PeriodicIdHandler<TSubstream>>;
type OutEvent = IdentifyEvent;
@ -74,6 +75,10 @@ where
IdentifyListenHandler::new().select(PeriodicIdHandler::new())
}
fn addresses_of_peer(&self, _: &PeerId) -> Vec<Multiaddr> {
Vec::new()
}
fn inject_connected(&mut self, peer_id: PeerId, endpoint: ConnectedPoint) {
let observed = match endpoint {
ConnectedPoint::Dialer { address } => address,
@ -124,7 +129,7 @@ where
fn poll(
&mut self,
params: &mut PollParameters<TTopology>,
params: &mut PollParameters,
) -> Async<
NetworkBehaviourAction<
<Self::ProtocolsHandler as ProtocolsHandler>::InEvent,
@ -132,12 +137,6 @@ where
>,
> {
if let Some(event) = self.events.pop_front() {
// We intercept identified events in order to insert the addresses in the topology.
if let NetworkBehaviourAction::GenerateEvent(IdentifyEvent::Identified { ref peer_id, ref info, .. }) = event {
let iter = info.listen_addrs.iter().cloned();
params.topology().add_identify_discovered_addrs(peer_id, iter);
}
return Async::Ready(event);
}
@ -150,7 +149,7 @@ where
.collect();
let send_back_info = IdentifyInfo {
public_key: params.local_public_key().clone(),
public_key: self.local_public_key.clone(),
protocol_version: self.protocol_version.clone(),
agent_version: self.agent_version.clone(),
listen_addrs: params.listened_addresses().cloned().collect(),

View File

@ -84,7 +84,6 @@ extern crate void;
pub use self::identify::{Identify, IdentifyEvent};
pub use self::id_transport::IdentifyTransport;
pub use self::protocol::IdentifyInfo;
pub use self::topology::IdentifyTopology;
pub mod listen_handler;
pub mod periodic_id_handler;
@ -93,4 +92,3 @@ pub mod protocol;
mod identify;
mod id_transport;
mod structs_proto;
mod topology;

View File

@ -1,43 +0,0 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use libp2p_core::{Multiaddr, PeerId};
use libp2p_core::topology::{MemoryTopology, Topology};
/// Trait required on the topology for the identify system to store addresses.
pub trait IdentifyTopology: Topology {
/// Adds to the topology an address discovered through identification.
///
/// > **Note**: Will never be called with the local peer ID.
fn add_identify_discovered_addrs<TIter>(&mut self, peer: &PeerId, addr: TIter)
where
TIter: Iterator<Item = Multiaddr>;
}
impl IdentifyTopology for MemoryTopology {
fn add_identify_discovered_addrs<TIter>(&mut self, peer: &PeerId, addr: TIter)
where
TIter: Iterator<Item = Multiaddr>,
{
for addr in addr {
self.add_address(peer.clone(), addr);
}
}
}