mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-23 23:01:33 +00:00
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:
@ -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`.
|
||||
|
Reference in New Issue
Block a user