From dceaba041f58d1f4ac1fcef983db65df76c4baf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Thu, 20 Jul 2023 14:17:32 +0100 Subject: [PATCH] Add batching support for AddAddress --- src/lib.rs | 5 +++++ src/net/mod.rs | 12 ++++++++++++ 2 files changed, 17 insertions(+) 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); }