Skip to content

Commit

Permalink
Merge pull request #117 from jmg-duarte/master
Browse files Browse the repository at this point in the history
Add batching support for AddAddress
  • Loading branch information
rkuhn authored Jul 24, 2023
2 parents db72675 + dceaba0 commit 0696dd7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub enum NetworkCommand {
ListenOn(Multiaddr, UnboundedSender<ListenerEvent>),
AddExternalAddress(Multiaddr),
AddAddress(PeerId, Multiaddr),
AddAddresses(Vec<(PeerId, Multiaddr)>),
RemoveAddress(PeerId, Multiaddr),
PrunePeers(Duration),
Dial(PeerId),
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -661,6 +666,13 @@ async fn poll_swarm<P: StoreParams>(
.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);
}
Expand Down

0 comments on commit 0696dd7

Please sign in to comment.