From 47c58090819e09e47321cecb607ce1e6f4179c73 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Fri, 14 Oct 2022 15:32:58 +0800 Subject: [PATCH] lnwallet: add verbose log for `ErrBelowChanReserve` --- lnwallet/channel.go | 35 +++++++++++++++++++---------- lnwallet/channel_test.go | 48 ++++++++++++---------------------------- 2 files changed, 37 insertions(+), 46 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index d68d38b786..83a146be07 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -3566,9 +3566,12 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter, // balance to satisfy the final evaluated HTLC's. switch { case int64(ourBalance) < 0: - return ErrBelowChanReserve + return fmt.Errorf("%w: negative local balance", + ErrBelowChanReserve) + case int64(theirBalance) < 0: - return ErrBelowChanReserve + return fmt.Errorf("%w: negative remote balance", + ErrBelowChanReserve) } // Ensure that the fee being applied is enough to be relayed across the @@ -3580,17 +3583,25 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter, // If the added HTLCs will decrease the balance, make sure they won't // dip the local and remote balances below the channel reserves. - switch { - case ourBalance < ourInitialBalance && - ourBalance < lnwire.NewMSatFromSatoshis( - lc.channelState.LocalChanCfg.ChanReserve): - - return ErrBelowChanReserve - case theirBalance < theirInitialBalance && - theirBalance < lnwire.NewMSatFromSatoshis( - lc.channelState.RemoteChanCfg.ChanReserve): + ourReserve := lnwire.NewMSatFromSatoshis( + lc.channelState.LocalChanCfg.ChanReserve, + ) + theirReserve := lnwire.NewMSatFromSatoshis( + lc.channelState.RemoteChanCfg.ChanReserve, + ) - return ErrBelowChanReserve + switch { + case ourBalance < ourInitialBalance && ourBalance < ourReserve: + lc.log.Debugf("Funds below chan reserve: ourBalance=%v, "+ + "ourReserve=%v", ourBalance, ourReserve) + return fmt.Errorf("%w: our balance below chan reserve", + ErrBelowChanReserve) + + case theirBalance < theirInitialBalance && theirBalance < theirReserve: + lc.log.Debugf("Funds below chan reserve: theirBalance=%v, "+ + "theirReserve=%v", theirBalance, theirReserve) + return fmt.Errorf("%w: their balance below chan reserve", + ErrBelowChanReserve) } // validateUpdates take a set of updates, and validates them against diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 1ed6e6e3e2..d3cec2d263 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -2737,10 +2737,7 @@ func TestAddHTLCNegativeBalance(t *testing.T) { htlcAmt = lnwire.NewMSatFromSatoshis(2 * btcutil.SatoshiPerBitcoin) htlc, _ := createHTLC(numHTLCs+1, htlcAmt) _, err = aliceChannel.AddHTLC(htlc, nil) - if err != ErrBelowChanReserve { - t.Fatalf("expected balance below channel reserve, instead "+ - "got: %v", err) - } + require.ErrorIs(t, err, ErrBelowChanReserve) } // assertNoChanSyncNeeded is a helper function that asserts that upon restart, @@ -5642,10 +5639,8 @@ func TestDesyncHTLCs(t *testing.T) { // balance is unavailable. htlcAmt = lnwire.NewMSatFromSatoshis(1 * btcutil.SatoshiPerBitcoin) htlc, _ = createHTLC(1, htlcAmt) - if _, err = aliceChannel.AddHTLC(htlc, nil); err != ErrBelowChanReserve { - t.Fatalf("expected ErrInsufficientBalance, instead received: %v", - err) - } + _, err = aliceChannel.AddHTLC(htlc, nil) + require.ErrorIs(t, err, ErrBelowChanReserve) // Now do a state transition, which will ACK the FailHTLC, making Alice // able to add the new HTLC. @@ -6063,14 +6058,11 @@ func TestChanReserve(t *testing.T) { htlc, _ = createHTLC(bobIndex, htlcAmt) bobIndex++ _, err := bobChannel.AddHTLC(htlc, nil) - if err != ErrBelowChanReserve { - t.Fatalf("expected ErrBelowChanReserve, instead received: %v", err) - } + require.ErrorIs(t, err, ErrBelowChanReserve) // Alice will reject this htlc upon receiving the htlc. - if _, err := aliceChannel.ReceiveHTLC(htlc); err != ErrBelowChanReserve { - t.Fatalf("expected ErrBelowChanReserve, instead received: %v", err) - } + _, err = aliceChannel.ReceiveHTLC(htlc) + require.ErrorIs(t, err, ErrBelowChanReserve) // We must setup the channels again, since a violation of the channel // constraints leads to channel shutdown. @@ -6105,14 +6097,11 @@ func TestChanReserve(t *testing.T) { htlc, _ = createHTLC(aliceIndex, htlcAmt) aliceIndex++ _, err = aliceChannel.AddHTLC(htlc, nil) - if err != ErrBelowChanReserve { - t.Fatalf("expected ErrBelowChanReserve, instead received: %v", err) - } + require.ErrorIs(t, err, ErrBelowChanReserve) // Likewise, Bob will reject receiving the htlc because of the same reason. - if _, err := bobChannel.ReceiveHTLC(htlc); err != ErrBelowChanReserve { - t.Fatalf("expected ErrBelowChanReserve, instead received: %v", err) - } + _, err = bobChannel.ReceiveHTLC(htlc) + require.ErrorIs(t, err, ErrBelowChanReserve) // We must setup the channels again, since a violation of the channel // constraints leads to channel shutdown. @@ -6218,22 +6207,15 @@ func TestChanReserveRemoteInitiator(t *testing.T) { // Bob should refuse to add this HTLC, since he realizes it will create // an invalid commitment. _, err = bobChannel.AddHTLC(htlc, nil) - if err != ErrBelowChanReserve { - t.Fatalf("expected ErrBelowChanReserve, instead received: %v", - err) - } + require.ErrorIs(t, err, ErrBelowChanReserve) // Of course Alice will also not have enough balance to add it herself. _, err = aliceChannel.AddHTLC(htlc, nil) - if err != ErrBelowChanReserve { - t.Fatalf("expected ErrBelowChanReserve, instead received: %v", - err) - } + require.ErrorIs(t, err, ErrBelowChanReserve) // Same for Alice, she should refuse to accept this second HTLC. - if _, err := aliceChannel.ReceiveHTLC(htlc); err != ErrBelowChanReserve { - t.Fatalf("expected ErrBelowChanReserve, instead received: %v", err) - } + _, err = aliceChannel.ReceiveHTLC(htlc) + require.ErrorIs(t, err, ErrBelowChanReserve) } // TestChanReserveLocalInitiatorDustHtlc tests that fee the initiator must pay @@ -6276,9 +6258,7 @@ func TestChanReserveLocalInitiatorDustHtlc(t *testing.T) { // Alice should realize that the fee she must pay to add this HTLC to // the local commitment would take her below the channel reserve. _, err = aliceChannel.AddHTLC(htlc, nil) - if err != ErrBelowChanReserve { - t.Fatalf("expected ErrBelowChanReserve, instead received: %v", err) - } + require.ErrorIs(t, err, ErrBelowChanReserve) } // TestMinHTLC tests that the ErrBelowMinHTLC error is thrown if an HTLC is added