-
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
Open
chahat-101
wants to merge
1
commit into
lightningdevkit:main
Choose a base branch
from
chahat-101:fix-peer-socket-update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
||
| 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); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.