Repository navigation
Persist the peer before initiating a channel #1149
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
cryptokvc
wants to merge
2
commits into
lightningdevkit:main
Choose a base branch
from
cryptokvc:fix/open-channel-persist-peer-first
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.
+175
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -45,14 +45,19 @@ where | |
| pub(crate) async fn add_peer(&self, peer_info: PeerInfo) -> Result<(), Error> { | ||
| let _guard = self.mutation_lock.lock().await; | ||
| let data = { | ||
| let mut locked_peers = self.peers.write().expect("lock"); | ||
| let locked_peers = self.peers.read().expect("lock"); | ||
|
Contributor
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. Changes made here overlap with #1150, so whichever lands second will conflict. Could we keep this PR focused on the |
||
| if locked_peers.contains_key(&peer_info.node_id) { | ||
| return Ok(()); | ||
| } | ||
| locked_peers.insert(peer_info.node_id, peer_info); | ||
| PeerStoreSerWrapper(&locked_peers).encode() | ||
| let mut updated_peers = locked_peers.clone(); | ||
| updated_peers.insert(peer_info.node_id, peer_info.clone()); | ||
| PeerStoreSerWrapper(&updated_peers).encode() | ||
| }; | ||
| self.persist_peers(data).await | ||
| // Only update the in-memory set once the write succeeded, so a failed write can be | ||
| // retried: otherwise the next `add_peer` would find the peer and skip persisting it. | ||
| self.persist_peers(data).await?; | ||
| self.peers.write().expect("lock").insert(peer_info.node_id, peer_info); | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub(crate) async fn remove_peer(&self, node_id: &PublicKey) -> Result<(), Error> { | ||
|
|
@@ -277,4 +282,22 @@ mod tests { | |
| assert_eq!(Err(Error::PersistenceFailed), peer_store.remove_peer(&node_id).await); | ||
| assert_eq!(Some(peer_info), peer_store.get_peer(&node_id)); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn add_peer_does_not_mutate_memory_if_persist_fails() { | ||
| let store: Arc<DynStore> = Arc::new(DynStoreWrapper(FailingStore)); | ||
| let logger = Arc::new(TestLogger::new()); | ||
| let peer_store = PeerStore::new(store, logger); | ||
| let node_id = PublicKey::from_str( | ||
| "0276607124ebe6a6c9338517b6f485825b27c2dcc0b9fc2aa6a4c0df91194e5993", | ||
| ) | ||
| .unwrap(); | ||
| let peer_info = | ||
| PeerInfo { node_id, address: SocketAddress::from_str("127.0.0.1:9738").unwrap() }; | ||
|
|
||
| assert_eq!(Err(Error::PersistenceFailed), peer_store.add_peer(peer_info.clone()).await); | ||
| assert_eq!(None, peer_store.get_peer(&node_id)); | ||
| // A retry must attempt the write again rather than treating the peer as stored. | ||
| assert_eq!(Err(Error::PersistenceFailed), peer_store.add_peer(peer_info).await); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Since
get_peerandadd_peerare separate steps, a concurrent operation on the same peer could slip in between. For example, if twoopen_channelcalls to the same node overlap and one of them fails, the failing one could remove the peer even though the other one opened a channel. It's rare, but cheap to guard against. Could we also checklist_channels_with_counterparty(&node_id)before removing, likeremove_peer_after_reconnectdoes? That way, we never drop a peer we still have a channel with.