Skip to content

Commit 9016cbc

Browse files
authored
fix(peer-store/memory-store): don't store remote address as listener
On connection establishment, only the dialer should store the remote's address in the peer store. From the listener perspective, there is no information if the observed remote address is actually a listening address of the peer. Pull-Request: #6010.
1 parent 61a8341 commit 9016cbc

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

misc/peer-store/src/memory_store.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::{
1515
};
1616

1717
use libp2p_core::{Multiaddr, PeerId};
18-
use libp2p_swarm::{DialError, FromSwarm};
18+
use libp2p_swarm::{behaviour::ConnectionEstablished, DialError, FromSwarm};
1919
use lru::LruCache;
2020

2121
use super::Store;
@@ -191,21 +191,23 @@ impl<T> Store for MemoryStore<T> {
191191
self.push_event_and_wake(crate::store::Event::RecordUpdated(info.peer_id));
192192
}
193193
}
194-
FromSwarm::ConnectionEstablished(info) => {
194+
FromSwarm::ConnectionEstablished(ConnectionEstablished {
195+
peer_id,
196+
failed_addresses,
197+
endpoint,
198+
..
199+
}) if endpoint.is_dialer() => {
195200
let mut is_record_updated = false;
196201
if self.config.remove_addr_on_dial_error {
197-
for failed_addr in info.failed_addresses {
202+
for failed_addr in *failed_addresses {
198203
is_record_updated |=
199-
self.remove_address_silent(&info.peer_id, failed_addr, false);
204+
self.remove_address_silent(peer_id, failed_addr, false);
200205
}
201206
}
202-
is_record_updated |= self.update_address_silent(
203-
&info.peer_id,
204-
info.endpoint.get_remote_address(),
205-
false,
206-
);
207+
is_record_updated |=
208+
self.update_address_silent(peer_id, endpoint.get_remote_address(), false);
207209
if is_record_updated {
208-
self.push_event_and_wake(crate::store::Event::RecordUpdated(info.peer_id));
210+
self.push_event_and_wake(crate::store::Event::RecordUpdated(*peer_id));
209211
}
210212
}
211213
FromSwarm::DialFailure(info) => {
@@ -302,9 +304,7 @@ impl Config {
302304
}
303305
/// If set to `true`, the store will remove addresses if the swarm indicates a dial failure.
304306
/// More specifically:
305-
/// - Failed dials indicated in
306-
/// [`ConnectionEstablished`](libp2p_swarm::behaviour::ConnectionEstablished)'s
307-
/// `failed_addresses` will be removed.
307+
/// - Failed dials indicated in [`ConnectionEstablished`]'s `failed_addresses` will be removed.
308308
/// - [`DialError::LocalPeerId`] causes the full peer entry to be removed.
309309
/// - On [`DialError::WrongPeerId`], the address will be removed from the incorrect peer's
310310
/// record and re-added to the correct peer's record.
@@ -499,6 +499,7 @@ mod test {
499499
rt.block_on(async {
500500
let (listen_addr, _) = swarm1.listen().with_memory_addr_external().await;
501501
let swarm1_peer_id = *swarm1.local_peer_id();
502+
let swarm2_peer_id = *swarm2.local_peer_id();
502503
swarm2.dial(listen_addr.clone()).expect("dial to succeed");
503504
let handle = spawn_wait_conn_established(swarm1);
504505
swarm2
@@ -508,12 +509,17 @@ mod test {
508509
})
509510
.await;
510511
let mut swarm1 = handle.await.expect("future to complete");
512+
expect_record_update(&mut swarm2, swarm1_peer_id).await;
511513
assert!(swarm2
512514
.behaviour()
513515
.address_of_peer(&swarm1_peer_id)
514516
.expect("swarm should be connected and record about it should be created")
515517
.any(|addr| *addr == listen_addr));
516-
expect_record_update(&mut swarm1, *swarm2.local_peer_id()).await;
518+
// Address from connection is not stored on the listener side.
519+
assert!(swarm1
520+
.behaviour()
521+
.address_of_peer(&swarm2_peer_id)
522+
.is_none());
517523
let (new_listen_addr, _) = swarm1.listen().with_memory_addr_external().await;
518524
let handle = spawn_wait_conn_established(swarm1);
519525
swarm2
@@ -590,15 +596,18 @@ mod test {
590596
.await
591597
.expect("future to complete");
592598
let mut swarm1 = handle.await.expect("future to complete");
593-
// expexting update from direct connection.
594599
expect_record_update(&mut swarm2, swarm1_peer_id).await;
595600
assert!(swarm2
596601
.behaviour()
597602
.peer_store
598603
.address_of_peer(&swarm1_peer_id)
599604
.expect("swarm should be connected and record about it should be created")
600605
.any(|addr| *addr == listen_addr));
601-
expect_record_update(&mut swarm1, *swarm2.local_peer_id()).await;
606+
assert!(swarm1
607+
.behaviour()
608+
.peer_store
609+
.address_of_peer(&swarm2_peer_id)
610+
.is_none());
602611
swarm1.next_swarm_event().await; // skip `identify::Event::Sent`
603612
swarm1.next_swarm_event().await; // skip `identify::Event::Received`
604613
let (new_listen_addr, _) = swarm1.listen().with_memory_addr_external().await;

0 commit comments

Comments
 (0)