-
Notifications
You must be signed in to change notification settings - Fork 116
Update peer socket address when peer already existsRefresh the stored address if an existing peer is added again #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,12 +43,21 @@ where | |
| pub(crate) fn add_peer(&self, peer_info: PeerInfo) -> Result<(), Error> { | ||
| let mut locked_peers = self.peers.write().unwrap(); | ||
|
|
||
| if locked_peers.contains_key(&peer_info.node_id) { | ||
| return Ok(()); | ||
| match locked_peers.get_mut(&peer_info.node_id) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why we bother to first look it up if the following behavior is identical to
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense — I missed the inbound-channel case where we shouldn’t overwrite an existing peer address. |
||
| Some(existing_peer) => { | ||
| if existing_peer.address != peer_info.address { | ||
| *existing_peer = peer_info; | ||
| self.persist_peers(&*locked_peers)?; | ||
| } | ||
| }, | ||
|
|
||
| None => { | ||
| locked_peers.insert(peer_info.node_id, peer_info); | ||
| self.persist_peers(&*locked_peers)?; | ||
| }, | ||
| } | ||
|
|
||
| locked_peers.insert(peer_info.node_id, peer_info); | ||
| self.persist_peers(&*locked_peers) | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub(crate) fn remove_peer(&self, node_id: &PublicKey) -> Result<(), Error> { | ||
|
|
@@ -195,4 +204,26 @@ mod tests { | |
| assert_eq!(peers[0], expected_peer_info); | ||
| assert_eq!(deser_peer_store.get_peer(&node_id), Some(expected_peer_info)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn peer_address_is_updated_if_peer_exists() { | ||
| let store: Arc<DynStore> = Arc::new(DynStoreWrapper(InMemoryStore::new())); | ||
| let logger = Arc::new(TestLogger::new()); | ||
| let peer_store = PeerStore::new(Arc::clone(&store), Arc::clone(&logger)); | ||
|
|
||
| let node_id = PublicKey::from_str( | ||
| "0276607124ebe6a6c9338517b6f485825b27c2dcc0b9fc2aa6a4c0df91194e5993", | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let addr1 = SocketAddress::from_str("127.0.0.1:9738").unwrap(); | ||
| let addr2 = SocketAddress::from_str("127.0.0.1:9739").unwrap(); | ||
|
|
||
| peer_store.add_peer(PeerInfo { node_id, address: addr1 }).unwrap(); | ||
|
|
||
| peer_store.add_peer(PeerInfo { node_id, address: addr2.clone() }).unwrap(); | ||
|
|
||
| let peer = peer_store.get_peer(&node_id).unwrap(); | ||
| assert_eq!(peer.address, addr2); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't just drop this, as there are instances where we'd
add_peerbut don't want to override the peer address if we already have one (e.g., when adding peers for inbound channels inevent.rs). To solves this, we should probably just introduce anoverride: boolflag that lets us not return early. Alternatively, we could have two different methods for updating or inserting a peer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make sure I implement this the way you expect: would you prefer:
(a) keeping add_peer but adding an explicit override_existing: bool, or
(b) splitting this into two methods (e.g. add_peer_if_missing and upsert_peer)?
I’m happy to update call sites accordingly once I know which behavior you want to be the default.