diff --git a/src/lib.rs b/src/lib.rs index 3fca6fa..d7d0700 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -192,6 +192,11 @@ where self.network.add_address(peer, addr) } + /// Adds a batch of known `Multiaddr` & `PeerId` pairs. + pub fn add_addresses(&mut self, addresses: Vec<(PeerId, Multiaddr)>) { + self.network.add_addresses(addresses) + } + /// Removes a `Multiaddr` for a `PeerId`. pub fn remove_address(&mut self, peer: PeerId, addr: Multiaddr) { self.network.remove_address(peer, addr) diff --git a/src/net/mod.rs b/src/net/mod.rs index c2f281c..b7fd169 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -79,6 +79,7 @@ pub enum NetworkCommand { ListenOn(Multiaddr, UnboundedSender), AddExternalAddress(Multiaddr), AddAddress(PeerId, Multiaddr), + AddAddresses(Vec<(PeerId, Multiaddr)>), RemoveAddress(PeerId, Multiaddr), PrunePeers(Duration), Dial(PeerId), @@ -352,6 +353,10 @@ impl NetworkService { self.cmd(NetworkCommand::AddAddress(peer, addr)); } + pub fn add_addresses(&mut self, addresses: Vec<(PeerId, Multiaddr)>) { + self.cmd(NetworkCommand::AddAddresses(addresses)); + } + pub fn remove_address(&mut self, peer: PeerId, addr: Multiaddr) { self.cmd(NetworkCommand::RemoveAddress(peer, addr)); } @@ -661,6 +666,13 @@ async fn poll_swarm( .behaviour_mut() .add_address(&peer, addr, AddressSource::User); } + NetworkCommand::AddAddresses(addresses) => { + for (peer, addr) in addresses { + swarm + .behaviour_mut() + .add_address(&peer, addr, AddressSource::User); + } + } NetworkCommand::RemoveAddress(peer, addr) => { swarm.behaviour_mut().remove_address(&peer, &addr); }