From 70b86e596e4c54813a02b31437f3aedbf0355924 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Thu, 22 Mar 2018 13:04:57 +0100 Subject: [PATCH] lnwallet/channel: use remote dustlimit when generating HTLC sigs This commit fixes an issue which would arise in some cases when the local and remote dust limits would differ, resulting in lnd not producing the expected number of HTLC signatures. This was a result of checking dust against the local instead of the remote dust limit. A test exercising the scenario is added. --- lnwallet/channel.go | 2 +- lnwallet/channel_test.go | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index eb0adeaf..4f69ed2a 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -2540,7 +2540,7 @@ func genRemoteHtlcSigJobs(keyRing *CommitmentKeyRing, remoteCommitView *commitment) ([]signJob, chan struct{}, error) { txHash := remoteCommitView.txn.TxHash() - dustLimit := localChanCfg.DustLimit + dustLimit := remoteChanCfg.DustLimit feePerKw := remoteCommitView.feePerKw // With the keys generated, we'll make a slice with enough capacity to diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 01e158d8..2046ed3a 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -1503,6 +1503,7 @@ func TestHTLCSigNumber(t *testing.T) { } feePerKw := feePerVSize.FeePerKWeight() + belowDust := btcutil.Amount(500) + htlcTimeoutFee(feePerKw) aboveDust := btcutil.Amount(1400) + htlcSuccessFee(feePerKw) // =================================================================== @@ -1554,6 +1555,52 @@ func TestHTLCSigNumber(t *testing.T) { t.Fatalf("Expected Bob to reject signatures") } + // ============================================================== + // Test that sigs are not returned for HTLCs below dust limit. + // ============================================================== + aliceChannel, bobChannel, cleanUp = createChanWithHTLC(belowDust) + defer cleanUp() + + aliceSig, aliceHtlcSigs, err = aliceChannel.SignNextCommitment() + if err != nil { + t.Fatalf("Error signing next commitment: %v", err) + } + + // Since the HTLC is below Bob's dust limit, Alice won't need to send + // any signatures for this HTLC. + if len(aliceHtlcSigs) != 0 { + t.Fatalf("expected no htlc sigs, instead got %v", + len(aliceHtlcSigs)) + } + + err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + if err != nil { + t.Fatalf("Bob failed receiving commitment: %v", err) + } + + // ================================================================ + // Test that sigs are correctly returned for HTLCs above dust limit. + // ================================================================ + aliceChannel, bobChannel, cleanUp = createChanWithHTLC(aboveDust) + defer cleanUp() + + aliceSig, aliceHtlcSigs, err = aliceChannel.SignNextCommitment() + if err != nil { + t.Fatalf("Error signing next commitment: %v", err) + } + + // Since the HTLC is above Bob's dust limit, Alice should send a + // signature for this HTLC. + if len(aliceHtlcSigs) != 1 { + t.Fatalf("expected 1 htlc sig, instead got %v", + len(aliceHtlcSigs)) + } + + err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + if err != nil { + t.Fatalf("Bob failed receiving commitment: %v", err) + } + } // TestChannelBalanceDustLimit tests the condition when the remaining balance