Skip to content

Commit d12df7b

Browse files
committed
Address review comments.
1 parent e0ad69a commit d12df7b

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

core/src/network.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ where
187187
///
188188
/// The translation is transport-specific. See [`Transport::address_translation`].
189189
pub fn address_translation<'a>(&'a self, observed_addr: &'a Multiaddr)
190-
-> impl Iterator<Item = Multiaddr> + 'a
190+
-> Vec<Multiaddr>
191191
where
192192
TMuxer: 'a,
193193
THandler: 'a,
@@ -201,7 +201,7 @@ where
201201
addrs.sort_unstable();
202202
addrs.dedup();
203203

204-
addrs.into_iter()
204+
addrs
205205
}
206206

207207
/// Returns the peer id of the local node.

swarm/src/lib.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,18 @@ where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
414414
/// [`NetworkBehaviourAction::ReportObservedAddr`] or explicitly
415415
/// through this method.
416416
pub fn add_external_address(&mut self, a: Multiaddr, s: AddressScore) -> AddAddressResult {
417-
self.behaviour.inject_new_external_addr(&a);
418-
self.external_addrs.add(a, s)
417+
let result = self.external_addrs.add(a.clone(), s);
418+
let expired = match &result {
419+
AddAddressResult::Inserted { expired } => {
420+
self.behaviour.inject_new_external_addr(&a);
421+
expired
422+
}
423+
AddAddressResult::Updated { expired } => expired,
424+
};
425+
for a in expired {
426+
self.behaviour.inject_expired_external_addr(&a.addr);
427+
}
428+
result
419429
}
420430

421431
/// Removes an external address of the local node, regardless of
@@ -425,8 +435,12 @@ where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
425435
/// Returns `true` if the address existed and was removed, `false`
426436
/// otherwise.
427437
pub fn remove_external_address(&mut self, addr: &Multiaddr) -> bool {
428-
self.behaviour.inject_expired_external_addr(addr);
429-
self.external_addrs.remove(addr)
438+
if self.external_addrs.remove(addr) {
439+
self.behaviour.inject_expired_external_addr(addr);
440+
true
441+
} else {
442+
false
443+
}
430444
}
431445

432446
/// Bans a peer by its peer ID.
@@ -736,10 +750,7 @@ where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
736750
},
737751
Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { address, score }) => {
738752
for addr in this.network.address_translation(&address) {
739-
if this.external_addrs.iter().all(|a| a.addr != addr) {
740-
this.behaviour.inject_new_external_addr(&addr);
741-
}
742-
this.external_addrs.add(addr, score);
753+
this.add_external_address(addr, score);
743754
}
744755
},
745756
}

swarm/src/registry.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ impl Default for Addresses {
168168
/// The result of adding an address to an ordered list of
169169
/// addresses with associated scores.
170170
pub enum AddAddressResult {
171-
Inserted,
172-
Updated,
171+
Inserted { expired: SmallVec<[AddressRecord; 8]> },
172+
Updated { expired: SmallVec<[AddressRecord; 8]> },
173173
}
174174

175175
impl Addresses {
@@ -206,8 +206,11 @@ impl Addresses {
206206
}
207207

208208
// Remove addresses that have a score of 0.
209+
let mut expired = SmallVec::new();
209210
while self.registry.last().map(|e| e.score.is_zero()).unwrap_or(false) {
210-
self.registry.pop();
211+
if let Some(addr) = self.registry.pop() {
212+
expired.push(addr);
213+
}
211214
}
212215

213216
// If the address score is finite, remember this report.
@@ -220,13 +223,13 @@ impl Addresses {
220223
if r.addr == addr {
221224
r.score = r.score + score;
222225
isort(&mut self.registry);
223-
return AddAddressResult::Updated
226+
return AddAddressResult::Updated { expired }
224227
}
225228
}
226229

227230
// It is a new record.
228231
self.registry.push(AddressRecord::new(addr, score));
229-
AddAddressResult::Inserted
232+
AddAddressResult::Inserted { expired }
230233
}
231234

232235
/// Explicitly remove an address from the collection.

0 commit comments

Comments
 (0)