lnwallet: add verbose log for ErrBelowChanReserve

This commit is contained in:
yyforyongyu 2022-10-14 15:32:58 +08:00
parent 4292c05e04
commit 47c5809081
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
2 changed files with 36 additions and 45 deletions

View File

@ -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.
ourReserve := lnwire.NewMSatFromSatoshis(
lc.channelState.LocalChanCfg.ChanReserve,
)
theirReserve := lnwire.NewMSatFromSatoshis(
lc.channelState.RemoteChanCfg.ChanReserve,
)
switch {
case ourBalance < ourInitialBalance &&
ourBalance < lnwire.NewMSatFromSatoshis(
lc.channelState.LocalChanCfg.ChanReserve):
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)
return ErrBelowChanReserve
case theirBalance < theirInitialBalance &&
theirBalance < lnwire.NewMSatFromSatoshis(
lc.channelState.RemoteChanCfg.ChanReserve):
return 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

View File

@ -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