Skip to content

Commit

Permalink
lnwallet: add verbose log for ErrBelowChanReserve
Browse files Browse the repository at this point in the history
  • Loading branch information
yyforyongyu committed Jan 18, 2023
1 parent 4292c05 commit 47c5809
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 46 deletions.
35 changes: 23 additions & 12 deletions lnwallet/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
48 changes: 14 additions & 34 deletions lnwallet/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 47c5809

Please sign in to comment.