Skip to content

lnwallet: apply BIP69+CLTV tie-break to HTLC signature order#4121

Merged
Roasbeef merged 5 commits into
lightningnetwork:masterfrom
cfromknecht:sort-htlc-sigs
Apr 4, 2020
Merged

lnwallet: apply BIP69+CLTV tie-break to HTLC signature order#4121
Roasbeef merged 5 commits into
lightningnetwork:masterfrom
cfromknecht:sort-htlc-sigs

Conversation

@cfromknecht
Copy link
Copy Markdown
Contributor

Fixes #4118

This PR fixes an issue in which HTLC signatures sent to the remote peer could be improperly ordered. This can occur when multiple HTLCs share the identical payment hashes and amounts, but have differing CLTV values. In particular, if these HTLCs are added in such a way that subsequence has descending CLTVs, the HTLC signatures would not reflect the HTLC tie-breaker used to sort the commitment.

#3412 partially fixed the issue, by ensuring that we properly sort the actual commitment and second-level transaction refer to the proper txid in their previous outpoint, however it failed to apply this sorting when generating the signing jobs and hence the final ordering that is sent on the wire.

Comment thread lnwallet/channel_test.go Outdated
@cfromknecht cfromknecht removed the request for review from Roasbeef March 27, 2020 21:56
@cfromknecht cfromknecht added this to the 0.10.0 milestone Mar 30, 2020
Comment thread lnwallet/channel.go Outdated
Comment thread lnwallet/commitment.go Outdated
Comment thread lnwallet/channel_test.go Outdated
Copy link
Copy Markdown
Contributor

@wpaulino wpaulino left a comment

Choose a reason for hiding this comment

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

One small nit regarding d2fa2b5 commit's message: the test addition is no longer in the prior commit.

@cfromknecht
Copy link
Copy Markdown
Contributor Author

One small nit regarding d2fa2b5 commit's message: the test addition is no longer in the prior commit.

Fixed

Copy link
Copy Markdown
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

The diff looks solid, other than some questions I have on the first commitment, it isn't yet clear to me that some of these changes are actually required. I may very well be missing something though.

Comment thread lnwallet/channel.go Outdated
Comment thread lnwallet/channel.go Outdated
We currently write each HTLCs OutputIndex to disk, but we don't use it
when restoring. The restoration is modified to use these directly, since
we will have lost access to the sorting of CLTVs after the initial
signing process.
This commit fixes lightningnetwork#4118 by properly sorting the HTLC signatures sent
over the wire to match the BOLT3 BIP69+CLTV sorting of the commitment
outputs.

To do so, we expose the slice of cltv deltas for HTLCs on the unsigned
commitment after applying the commitment sorting. This will be used to
locate the proper output index, as the CLTV serves as a tie breaker
between HTLCs that otherwise have the same payment hash and amount.

Note that lightningnetwork#3412 fixed the issue partially by ensuring the commitment was
constructed properly (and the second-level prev outpoint's txid was
correct), but failed to address that the HTLC signatures were still sent
out in the incorrect order. With this, we pass the test case introduce
in the next commit.
This commit adds a test to exercise that HTLC signatures are sent in the
correct order, i.e. they match the sorting of the HTLC outputs on the
commitment after applying BOLT 3's BIP69+CLTV sort.
This commit adds an additional santity check that rejects zero-value
HTLCs, preventing them from being added to the channel state even if the
channel config's minhtlc value is zero.
@cfromknecht cfromknecht removed the request for review from halseth April 3, 2020 01:04
Copy link
Copy Markdown
Contributor

@halseth halseth left a comment

Choose a reason for hiding this comment

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

LGTM ⚡️

@cfromknecht cfromknecht requested a review from Roasbeef April 3, 2020 13:49
Copy link
Copy Markdown
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🍺

thanks for investigating the restart case!

@Roasbeef Roasbeef merged commit 173f7d8 into lightningnetwork:master Apr 4, 2020
@cfromknecht cfromknecht deleted the sort-htlc-sigs branch April 4, 2020 05:48
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.

CLTV tie-breaking doesn't seem to be working

4 participants