Skip to content

Make payments not duplicatively fail/succeed on reload/reconnect #918

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

Merged
merged 4 commits into from
May 20, 2021

Conversation

TheBlueMatt
Copy link
Collaborator

We currently generate duplicative PaymentFailed/PaymentSent events
in two cases:

a) If we receive a update_fulfill_htlc message, followed by a
disconnect, then a resend of the same update_fulfill_htlc
message, we will generate a PaymentSent event for each message.

b) When a Channel is closed, any outbound HTLCs which were relayed
through it are simply dropped when the Channel is. From there,
the ChannelManager relies on the ChannelMonitor having a copy of
the relevant fail-/claim-back data and processes the HTLC
fail/claim when the ChannelMonitor tells it to.

If, due to an on-chain event, an HTLC is failed/claimed, and
then we serialize the ChannelManager, but do not re-serialize
the relevant ChannelMonitor, we may end up getting a duplicative
event.

In order to provide the expected consistency, we add explicit
tracking of pending outbound payments using their unique
session_priv field which is generated when the payment is sent.
Then, before generating PaymentFailed/PaymentSent events, we check
that the session_priv for the payment is still pending.

Thix fixes #209.

@codecov
Copy link

codecov bot commented May 9, 2021

Codecov Report

Merging #918 (864375e) into main (5d74cae) will decrease coverage by 0.19%.
The diff coverage is 95.86%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #918      +/-   ##
==========================================
- Coverage   90.45%   90.26%   -0.20%     
==========================================
  Files          59       59              
  Lines       29785    30569     +784     
==========================================
+ Hits        26941    27592     +651     
- Misses       2844     2977     +133     
Impacted Files Coverage Δ
lightning/src/util/events.rs 17.27% <ø> (ø)
lightning/src/ln/channelmanager.rs 83.46% <88.88%> (+0.05%) ⬆️
lightning/src/ln/functional_tests.rs 96.85% <100.00%> (+0.07%) ⬆️
lightning/src/ln/msgs.rs 88.24% <0.00%> (-0.05%) ⬇️
lightning/src/ln/peer_handler.rs 46.95% <0.00%> (+2.78%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5d74cae...864375e. Read the comment docs.

Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice usability improvement. Still reviewing but I think I just have one docs question

@TheBlueMatt TheBlueMatt force-pushed the 2021-05-dup-claims branch from dd9dcad to a2247b1 Compare May 10, 2021 18:39
@TheBlueMatt TheBlueMatt force-pushed the 2021-05-dup-claims branch 2 times, most recently from b45341e to da33f5c Compare May 13, 2021 22:29
@TheBlueMatt
Copy link
Collaborator Author

Squashed with no diff from 853838c1 to da33f5c3

@TheBlueMatt TheBlueMatt force-pushed the 2021-05-dup-claims branch 2 times, most recently from dbaf591 to 74dc019 Compare May 20, 2021 15:24
@TheBlueMatt
Copy link
Collaborator Author

Rebased on latest upstream and added trivial cleanups to address Jeff's comments above. Would like a once-over from one of the existing reviewers.

@jkczyz
Copy link
Contributor

jkczyz commented May 20, 2021

Rebased on latest upstream and added trivial cleanups to address Jeff's comments above. Would like a once-over from one of the existing reviewers.

LGTM

We currently generate duplicative PaymentFailed/PaymentSent events
in two cases:

a) If we receive a update_fulfill_htlc message, followed by a
   disconnect, then a resend of the same update_fulfill_htlc
   message, we will generate a PaymentSent event for each message.

b) When a Channel is closed, any outbound HTLCs which were relayed
   through it are simply dropped when the Channel is. From there,
   the ChannelManager relies on the ChannelMonitor having a copy of
   the relevant fail-/claim-back data and processes the HTLC
   fail/claim when the ChannelMonitor tells it to.

   If, due to an on-chain event, an HTLC is failed/claimed, and
   then we serialize the ChannelManager, but do not re-serialize
   the relevant ChannelMonitor, we may end up getting a duplicative
   event.

In order to provide the expected consistency, we add explicit
tracking of pending outbound payments using their unique
session_priv field which is generated when the payment is sent.
Then, before generating PaymentFailed/PaymentSent events, we check
that the session_priv for the payment is still pending.

Thix fixes lightningdevkit#209.
@TheBlueMatt TheBlueMatt force-pushed the 2021-05-dup-claims branch from 74dc019 to 864375e Compare May 20, 2021 16:38
@TheBlueMatt
Copy link
Collaborator Author

Squashed and added one additional commit to address the fuzz failures this introduced.

@@ -179,7 +179,7 @@ impl KeysInterface for KeyProvider {
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_id]).unwrap(),
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, self.node_id]).unwrap(),
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_id]).unwrap(),
[id, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
[id as u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take it we don't need to use four bytes here because we won't exhaust the one-byte space?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, we only ever create up to two of them per peer.

@jkczyz
Copy link
Contributor

jkczyz commented May 20, 2021

ACK 864375e

Tested locally.

@TheBlueMatt TheBlueMatt merged commit b6de281 into lightningdevkit:main May 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PaymentSent/PaymentFailed events can be duplicative
3 participants