Store information about the local node in the topology (#772)

* Store information about the local node in the topology

* Fix build

* Store the external addresses in the topology
This commit is contained in:
Pierre Krieger
2018-12-13 19:06:13 +01:00
committed by GitHub
parent e2ff74994c
commit 40a503fd63
9 changed files with 87 additions and 54 deletions

View File

@ -56,17 +56,11 @@ where TTransport: Transport,
/// if we're not connected to them.
topology: TTopology,
/// Public key of the local node.
local_public_key: PublicKey,
/// List of protocols that the behaviour says it supports.
supported_protocols: SmallVec<[Vec<u8>; 16]>,
/// List of multiaddresses we're listening on.
listened_addrs: SmallVec<[Multiaddr; 8]>,
/// List of multiaddresses we're listening on after NAT traversal.
external_addresses: SmallVec<[Multiaddr; 8]>,
}
impl<TTransport, TBehaviour, TTopology> Deref for Swarm<TTransport, TBehaviour, TTopology>
@ -121,7 +115,7 @@ where TBehaviour: NetworkBehaviour<TTopology>,
{
/// Builds a new `Swarm`.
#[inline]
pub fn new(transport: TTransport, mut behaviour: TBehaviour, topology: TTopology, local_public_key: PublicKey) -> Self {
pub fn new(transport: TTransport, mut behaviour: TBehaviour, topology: TTopology) -> Self {
let supported_protocols = behaviour
.new_handler()
.listen_protocol()
@ -130,17 +124,14 @@ where TBehaviour: NetworkBehaviour<TTopology>,
.map(|info| info.protocol_name().to_vec())
.collect();
let local_peer_id = local_public_key.clone().into_peer_id();
let raw_swarm = RawSwarm::new(transport, local_peer_id.clone());
let raw_swarm = RawSwarm::new(transport, topology.local_peer_id().clone());
Swarm {
raw_swarm,
behaviour,
topology,
local_public_key,
supported_protocols,
listened_addrs: SmallVec::new(),
external_addresses: SmallVec::new(),
}
}
@ -278,10 +269,7 @@ where TBehaviour: NetworkBehaviour<TTopology>,
topology: &mut self.topology,
supported_protocols: &self.supported_protocols,
listened_addrs: &self.listened_addrs,
external_addresses: &self.external_addresses,
nat_traversal: &move |a, b| transport.nat_traversal(a, b),
local_public_key: &self.local_public_key,
local_peer_id: &self.raw_swarm.local_peer_id(),
};
self.behaviour.poll(&mut parameters)
};
@ -304,11 +292,7 @@ where TBehaviour: NetworkBehaviour<TTopology>,
}
},
Async::Ready(NetworkBehaviourAction::ReportObservedAddr { address }) => {
for addr in self.raw_swarm.nat_traversal(&address) {
// TODO: is it a good idea to add these addresses permanently? what about
// a TTL instead?
self.external_addresses.push(addr);
}
self.topology.add_local_external_addrs(self.raw_swarm.nat_traversal(&address));
},
}
}
@ -358,10 +342,7 @@ pub struct PollParameters<'a, TTopology: 'a> {
topology: &'a mut TTopology,
supported_protocols: &'a [Vec<u8>],
listened_addrs: &'a [Multiaddr],
external_addresses: &'a [Multiaddr],
nat_traversal: &'a dyn Fn(&Multiaddr, &Multiaddr) -> Option<Multiaddr>,
local_public_key: &'a PublicKey,
local_peer_id: &'a PeerId,
}
impl<'a, TTopology> PollParameters<'a, TTopology> {
@ -382,30 +363,35 @@ impl<'a, TTopology> PollParameters<'a, TTopology> {
self.supported_protocols.iter().map(AsRef::as_ref)
}
/// Returns the list of the addresses we're listening on
/// Returns the list of the addresses we're listening on.
#[inline]
pub fn listened_addresses(&self) -> impl ExactSizeIterator<Item = &Multiaddr> {
self.listened_addrs.iter()
}
/// Returns the list of the addresses we're listening on, after accounting for NAT traversal.
///
/// This corresponds to the elements produced with `ReportObservedAddr`.
/// Returns the list of the addresses nodes can use to reach us.
#[inline]
pub fn external_addresses(&self) -> impl ExactSizeIterator<Item = &Multiaddr> {
self.external_addresses.iter()
pub fn external_addresses<'b>(&'b mut self) -> impl ExactSizeIterator<Item = Multiaddr> + 'b
where TTopology: Topology
{
let local_peer_id = self.topology.local_peer_id().clone();
self.topology.addresses_of_peer(&local_peer_id).into_iter()
}
/// Returns the public key of the local node.
#[inline]
pub fn local_public_key(&self) -> &PublicKey {
self.local_public_key
pub fn local_public_key(&self) -> &PublicKey
where TTopology: Topology
{
self.topology.local_public_key()
}
/// Returns the peer id of the local node.
#[inline]
pub fn local_peer_id(&self) -> &PeerId {
self.local_peer_id
pub fn local_peer_id(&self) -> &PeerId
where TTopology: Topology
{
self.topology.local_peer_id()
}
/// Calls the `nat_traversal` method on the underlying transport of the `Swarm`.

View File

@ -19,25 +19,49 @@
// DEALINGS IN THE SOFTWARE.
use std::collections::HashMap;
use {Multiaddr, PeerId};
use {Multiaddr, PeerId, PublicKey};
/// Storage for the network topology.
///
/// The topology should also store information about the local node, including its public key, its
/// `PeerId`, and the addresses it's advertising.
pub trait Topology {
/// Returns the addresses to try use to reach the given peer.
///
/// > **Note**: Keep in mind that `peer` can be the local node.
fn addresses_of_peer(&mut self, peer: &PeerId) -> Vec<Multiaddr>;
/// Adds an address that other nodes can use to connect to our local node.
///
/// > **Note**: Should later be returned when calling `addresses_of_peer()` with the `PeerId`
/// > of the local node.
fn add_local_external_addrs<TIter>(&mut self, addrs: TIter)
where TIter: Iterator<Item = Multiaddr>;
/// Returns the `PeerId` of the local node.
fn local_peer_id(&self) -> &PeerId;
/// Returns the public key of the local node.
fn local_public_key(&self) -> &PublicKey;
}
/// Topology of the network stored in memory.
pub struct MemoryTopology {
list: HashMap<PeerId, Vec<Multiaddr>>,
local_peer_id: PeerId,
local_public_key: PublicKey,
}
impl MemoryTopology {
/// Creates an empty topology.
#[inline]
pub fn empty() -> MemoryTopology {
pub fn empty(pubkey: PublicKey) -> MemoryTopology {
let local_peer_id = pubkey.clone().into_peer_id();
MemoryTopology {
list: Default::default()
list: Default::default(),
local_peer_id,
local_public_key: pubkey,
}
}
@ -69,15 +93,27 @@ impl MemoryTopology {
}
}
impl Default for MemoryTopology {
#[inline]
fn default() -> MemoryTopology {
MemoryTopology::empty()
}
}
impl Topology for MemoryTopology {
fn addresses_of_peer(&mut self, peer: &PeerId) -> Vec<Multiaddr> {
self.list.get(peer).map(|v| v.clone()).unwrap_or(Vec::new())
}
fn add_local_external_addrs<TIter>(&mut self, addrs: TIter)
where TIter: Iterator<Item = Multiaddr>
{
for addr in addrs {
let id = self.local_peer_id.clone();
self.add_address(id, addr);
}
}
#[inline]
fn local_peer_id(&self) -> &PeerId {
&self.local_peer_id
}
#[inline]
fn local_public_key(&self) -> &PublicKey {
&self.local_public_key
}
}