From a6e7dce7b7047f55ef0d7356649ee3f352091c9d Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Thu, 22 Mar 2018 13:23:23 +0100 Subject: [PATCH] lnwallet/channel: reject received commitment with too many htlc sigs This commit adds a check that will make LightningChannel reject a received commitment if it is accompanied with too many HTLC signatures. This enforces the requirement in BOLT-2, saying: if num_htlcs is not equal to the number of HTLC outputs in the local commitment transaction: * MUST fail the channel. A test exercising the behaviour is added. --- lnwallet/channel.go | 7 +++++++ lnwallet/channel_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 4f69ed2a1..cefc3753e 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -3606,6 +3606,13 @@ func genHtlcSigValidationJobs(localCommitmentView *commitment, i++ } + // If we received a number of HTLC signatures that doesn't match our + // commitment, we'll return an error now. + if len(htlcSigs) != i { + return nil, fmt.Errorf("number of htlc sig mismatch. "+ + "Expected %v sigs, got %v", i, len(htlcSigs)) + } + return verifyJobs, nil } diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 2046ed3ad..345affb58 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -1601,6 +1601,35 @@ func TestHTLCSigNumber(t *testing.T) { t.Fatalf("Bob failed receiving commitment: %v", err) } + // ==================================================================== + // Test that Bob will not validate a received commitment if Alice sends + // signatures for HTLCs below the dust limit. + // ==================================================================== + aliceChannel, bobChannel, cleanUp = createChanWithHTLC(belowDust, + aboveDust) + defer cleanUp() + + // Alice should produce only one signature, since one HTLC is below + // dust. + aliceSig, aliceHtlcSigs, err = aliceChannel.SignNextCommitment() + if err != nil { + t.Fatalf("Error signing next commitment: %v", err) + } + + if len(aliceHtlcSigs) != 1 { + t.Fatalf("expected 1 htlc sig, instead got %v", + len(aliceHtlcSigs)) + } + + // Add an extra signature. + aliceHtlcSigs = append(aliceHtlcSigs, aliceHtlcSigs[0]) + + // Bob should reject these signatures since they don't match the number + // of HTLCs above dust. + err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + if err == nil { + t.Fatalf("Expected Bob to reject signatures") + } } // TestChannelBalanceDustLimit tests the condition when the remaining balance