mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-06-20 22:04:31 +02:00
Merge pull request #9291 from yyforyongyu/fix-log-unit
lnwallet: log the amounts in the same unit
This commit is contained in:
commit
41c2521e8a
@ -3595,11 +3595,13 @@ func (b BufferType) String() string {
|
|||||||
|
|
||||||
// applyCommitFee applies the commitFee including a buffer to the balance amount
|
// applyCommitFee applies the commitFee including a buffer to the balance amount
|
||||||
// and verifies that it does not become negative. This function returns the new
|
// and verifies that it does not become negative. This function returns the new
|
||||||
// balance and the exact buffer amount (excluding the commitment fee).
|
// balance, the exact buffer amount (excluding the commitment fee) and the
|
||||||
|
// commitment fee.
|
||||||
func (lc *LightningChannel) applyCommitFee(
|
func (lc *LightningChannel) applyCommitFee(
|
||||||
balance lnwire.MilliSatoshi, commitWeight lntypes.WeightUnit,
|
balance lnwire.MilliSatoshi, commitWeight lntypes.WeightUnit,
|
||||||
feePerKw chainfee.SatPerKWeight,
|
feePerKw chainfee.SatPerKWeight,
|
||||||
buffer BufferType) (lnwire.MilliSatoshi, lnwire.MilliSatoshi, error) {
|
buffer BufferType) (lnwire.MilliSatoshi, lnwire.MilliSatoshi,
|
||||||
|
lnwire.MilliSatoshi, error) {
|
||||||
|
|
||||||
commitFee := feePerKw.FeeForWeight(commitWeight)
|
commitFee := feePerKw.FeeForWeight(commitWeight)
|
||||||
commitFeeMsat := lnwire.NewMSatFromSatoshis(commitFee)
|
commitFeeMsat := lnwire.NewMSatFromSatoshis(commitFee)
|
||||||
@ -3614,14 +3616,16 @@ func (lc *LightningChannel) applyCommitFee(
|
|||||||
// Make sure that we are the initiator of the channel before we
|
// Make sure that we are the initiator of the channel before we
|
||||||
// apply the FeeBuffer.
|
// apply the FeeBuffer.
|
||||||
if !lc.channelState.IsInitiator {
|
if !lc.channelState.IsInitiator {
|
||||||
return 0, 0, ErrFeeBufferNotInitiator
|
return 0, 0, 0, ErrFeeBufferNotInitiator
|
||||||
}
|
}
|
||||||
|
|
||||||
// The FeeBuffer already includes the commitFee.
|
// The FeeBuffer already includes the commitFee.
|
||||||
bufferAmt = CalcFeeBuffer(feePerKw, commitWeight)
|
bufferAmt = CalcFeeBuffer(feePerKw, commitWeight)
|
||||||
if bufferAmt < balance {
|
if bufferAmt < balance {
|
||||||
newBalance := balance - bufferAmt
|
newBalance := balance - bufferAmt
|
||||||
return newBalance, bufferAmt - commitFeeMsat, nil
|
|
||||||
|
return newBalance, bufferAmt - commitFeeMsat,
|
||||||
|
commitFeeMsat, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// The AdditionalHtlc buffer type does NOT keep a FeeBuffer but solely
|
// The AdditionalHtlc buffer type does NOT keep a FeeBuffer but solely
|
||||||
@ -3635,7 +3639,7 @@ func (lc *LightningChannel) applyCommitFee(
|
|||||||
bufferAmt = commitFeeMsat + additionalHtlcFee
|
bufferAmt = commitFeeMsat + additionalHtlcFee
|
||||||
newBalance := balance - bufferAmt
|
newBalance := balance - bufferAmt
|
||||||
if bufferAmt < balance {
|
if bufferAmt < balance {
|
||||||
return newBalance, additionalHtlcFee, nil
|
return newBalance, additionalHtlcFee, commitFeeMsat, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// The default case does not account for any buffer on the local balance
|
// The default case does not account for any buffer on the local balance
|
||||||
@ -3643,13 +3647,13 @@ func (lc *LightningChannel) applyCommitFee(
|
|||||||
default:
|
default:
|
||||||
if commitFeeMsat < balance {
|
if commitFeeMsat < balance {
|
||||||
newBalance := balance - commitFeeMsat
|
newBalance := balance - commitFeeMsat
|
||||||
return newBalance, 0, nil
|
return newBalance, 0, commitFeeMsat, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We still return the amount and bufferAmt here to log them at a later
|
// We still return the amount and bufferAmt here to log them at a later
|
||||||
// stage.
|
// stage.
|
||||||
return balance, bufferAmt, ErrBelowChanReserve
|
return balance, bufferAmt, commitFeeMsat, ErrBelowChanReserve
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateCommitmentSanity is used to validate the current state of the
|
// validateCommitmentSanity is used to validate the current state of the
|
||||||
@ -3707,17 +3711,21 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
|
|||||||
// includes also a buffer type. Depending on whether we are the opener
|
// includes also a buffer type. Depending on whether we are the opener
|
||||||
// of the channel we either want to enforce a buffer on the local
|
// of the channel we either want to enforce a buffer on the local
|
||||||
// amount.
|
// amount.
|
||||||
var bufferAmt lnwire.MilliSatoshi
|
var (
|
||||||
|
bufferAmt lnwire.MilliSatoshi
|
||||||
|
commitFee lnwire.MilliSatoshi
|
||||||
|
)
|
||||||
|
|
||||||
if lc.channelState.IsInitiator {
|
if lc.channelState.IsInitiator {
|
||||||
ourBalance, bufferAmt, err = lc.applyCommitFee(
|
ourBalance, bufferAmt, commitFee, err = lc.applyCommitFee(
|
||||||
ourBalance, commitWeight, feePerKw, buffer)
|
ourBalance, commitWeight, feePerKw, buffer,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
commitFee := feePerKw.FeeForWeight(commitWeight)
|
|
||||||
lc.log.Errorf("Cannot pay for the CommitmentFee of "+
|
lc.log.Errorf("Cannot pay for the CommitmentFee of "+
|
||||||
"the ChannelState: ourBalance is negative "+
|
"the ChannelState: ourBalance is negative "+
|
||||||
"after applying the fee: ourBalance=%v, "+
|
"after applying the fee: ourBalance=%v, "+
|
||||||
"commitFee=%v, feeBuffer=%v (type=%v) "+
|
"commitFee=%v, feeBuffer=%v (type=%v) "+
|
||||||
"local_chan_initiator", int64(ourBalance),
|
"local_chan_initiator", ourBalance,
|
||||||
commitFee, bufferAmt, buffer)
|
commitFee, bufferAmt, buffer)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
@ -3730,17 +3738,16 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
|
|||||||
// stuck because locally we will never put another outgoing HTLC
|
// stuck because locally we will never put another outgoing HTLC
|
||||||
// on the channel state. The FeeBuffer should ONLY be enforced
|
// on the channel state. The FeeBuffer should ONLY be enforced
|
||||||
// if we locally pay for the commitment transaction.
|
// if we locally pay for the commitment transaction.
|
||||||
theirBalance, bufferAmt, err = lc.applyCommitFee(
|
theirBalance, bufferAmt, commitFee, err = lc.applyCommitFee(
|
||||||
theirBalance, commitWeight, feePerKw, NoBuffer)
|
theirBalance, commitWeight, feePerKw, NoBuffer,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
commitFee := feePerKw.FeeForWeight(commitWeight)
|
|
||||||
lc.log.Errorf("Cannot pay for the CommitmentFee "+
|
lc.log.Errorf("Cannot pay for the CommitmentFee "+
|
||||||
"of the ChannelState: theirBalance is "+
|
"of the ChannelState: theirBalance is "+
|
||||||
"negative after applying the fee: "+
|
"negative after applying the fee: "+
|
||||||
"theiBalance=%v, commitFee=%v, feeBuffer=%v "+
|
"theirBalance=%v, commitFee=%v, feeBuffer=%v "+
|
||||||
"(type=%v) remote_chan_initiator",
|
"(type=%v) remote_chan_initiator",
|
||||||
int64(theirBalance), commitFee, bufferAmt,
|
theirBalance, commitFee, bufferAmt, buffer)
|
||||||
buffer)
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -3757,10 +3764,6 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
|
|||||||
lc.channelState.RemoteChanCfg.ChanReserve,
|
lc.channelState.RemoteChanCfg.ChanReserve,
|
||||||
)
|
)
|
||||||
|
|
||||||
// Calculate the commitment fee to log the information if needed.
|
|
||||||
commitFee := feePerKw.FeeForWeight(commitWeight)
|
|
||||||
commitFeeMsat := lnwire.NewMSatFromSatoshis(commitFee)
|
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
// TODO(ziggie): Allow the peer dip us below the channel reserve when
|
// TODO(ziggie): Allow the peer dip us below the channel reserve when
|
||||||
// our local balance would increase during this commitment dance or
|
// our local balance would increase during this commitment dance or
|
||||||
@ -3774,7 +3777,7 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
|
|||||||
lc.log.Debugf("Funds below chan reserve: ourBalance=%v, "+
|
lc.log.Debugf("Funds below chan reserve: ourBalance=%v, "+
|
||||||
"ourReserve=%v, commitFee=%v, feeBuffer=%v "+
|
"ourReserve=%v, commitFee=%v, feeBuffer=%v "+
|
||||||
"chan_initiator=%v", ourBalance, ourReserve,
|
"chan_initiator=%v", ourBalance, ourReserve,
|
||||||
commitFeeMsat, bufferAmt, lc.channelState.IsInitiator)
|
commitFee, bufferAmt, lc.channelState.IsInitiator)
|
||||||
|
|
||||||
return fmt.Errorf("%w: our balance below chan reserve",
|
return fmt.Errorf("%w: our balance below chan reserve",
|
||||||
ErrBelowChanReserve)
|
ErrBelowChanReserve)
|
||||||
@ -8796,7 +8799,7 @@ func (lc *LightningChannel) availableCommitmentBalance(view *HtlcView,
|
|||||||
// Make sure we do not overwrite `ourBalance` that's why we
|
// Make sure we do not overwrite `ourBalance` that's why we
|
||||||
// declare bufferAmt beforehand.
|
// declare bufferAmt beforehand.
|
||||||
var bufferAmt lnwire.MilliSatoshi
|
var bufferAmt lnwire.MilliSatoshi
|
||||||
ourBalance, bufferAmt, err = lc.applyCommitFee(
|
ourBalance, bufferAmt, commitFee, err = lc.applyCommitFee(
|
||||||
ourBalance, futureCommitWeight, feePerKw, buffer,
|
ourBalance, futureCommitWeight, feePerKw, buffer,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -8806,8 +8809,7 @@ func (lc *LightningChannel) availableCommitmentBalance(view *HtlcView,
|
|||||||
"after applying the fee: ourBalance=%v, "+
|
"after applying the fee: ourBalance=%v, "+
|
||||||
"current commitFee(w/o additional htlc)=%v, "+
|
"current commitFee(w/o additional htlc)=%v, "+
|
||||||
"feeBuffer=%v (type=%v) local_chan_initiator",
|
"feeBuffer=%v (type=%v) local_chan_initiator",
|
||||||
int64(ourBalance), commitFee,
|
ourBalance, commitFee, bufferAmt, buffer)
|
||||||
bufferAmt, buffer)
|
|
||||||
|
|
||||||
return 0, commitWeight
|
return 0, commitWeight
|
||||||
}
|
}
|
||||||
|
@ -10299,6 +10299,7 @@ func TestApplyCommitmentFee(t *testing.T) {
|
|||||||
balance lnwire.MilliSatoshi
|
balance lnwire.MilliSatoshi
|
||||||
expectedBalance lnwire.MilliSatoshi
|
expectedBalance lnwire.MilliSatoshi
|
||||||
expectedBufferAmt lnwire.MilliSatoshi
|
expectedBufferAmt lnwire.MilliSatoshi
|
||||||
|
expectedCommitFee lnwire.MilliSatoshi
|
||||||
bufferAmt lnwire.MilliSatoshi
|
bufferAmt lnwire.MilliSatoshi
|
||||||
expectedErr error
|
expectedErr error
|
||||||
}{
|
}{
|
||||||
@ -10309,6 +10310,7 @@ func TestApplyCommitmentFee(t *testing.T) {
|
|||||||
balance: balance,
|
balance: balance,
|
||||||
expectedBalance: balance - feeBuffer,
|
expectedBalance: balance - feeBuffer,
|
||||||
expectedBufferAmt: feeBuffer - commitFee,
|
expectedBufferAmt: feeBuffer - commitFee,
|
||||||
|
expectedCommitFee: commitFee,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "apply feebuffer remote initiator",
|
name: "apply feebuffer remote initiator",
|
||||||
@ -10324,6 +10326,7 @@ func TestApplyCommitmentFee(t *testing.T) {
|
|||||||
balance: balance,
|
balance: balance,
|
||||||
expectedBalance: balance - commitFee - additionalHtlc,
|
expectedBalance: balance - commitFee - additionalHtlc,
|
||||||
expectedBufferAmt: additionalHtlc,
|
expectedBufferAmt: additionalHtlc,
|
||||||
|
expectedCommitFee: commitFee,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "apply NoBuffer",
|
name: "apply NoBuffer",
|
||||||
@ -10332,6 +10335,7 @@ func TestApplyCommitmentFee(t *testing.T) {
|
|||||||
balance: balance,
|
balance: balance,
|
||||||
expectedBalance: balance - commitFee,
|
expectedBalance: balance - commitFee,
|
||||||
expectedBufferAmt: 0,
|
expectedBufferAmt: 0,
|
||||||
|
expectedCommitFee: commitFee,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "apply FeeBuffer balance negative",
|
name: "apply FeeBuffer balance negative",
|
||||||
@ -10341,18 +10345,22 @@ func TestApplyCommitmentFee(t *testing.T) {
|
|||||||
expectedBalance: balanceBelowReserve,
|
expectedBalance: balanceBelowReserve,
|
||||||
expectedErr: ErrBelowChanReserve,
|
expectedErr: ErrBelowChanReserve,
|
||||||
expectedBufferAmt: feeBuffer,
|
expectedBufferAmt: feeBuffer,
|
||||||
|
expectedCommitFee: commitFee,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
tc := tc
|
tc := tc
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
balance, bufferAmt, err := tc.channel.applyCommitFee(
|
//nolint:lll
|
||||||
tc.balance, commitWeight, feePerKw, tc.buffer)
|
balance, bufferAmt, commitFee, err := tc.channel.applyCommitFee(
|
||||||
|
tc.balance, commitWeight, feePerKw, tc.buffer,
|
||||||
|
)
|
||||||
|
|
||||||
require.ErrorIs(t, err, tc.expectedErr)
|
require.ErrorIs(t, err, tc.expectedErr)
|
||||||
require.Equal(t, tc.expectedBalance, balance)
|
require.Equal(t, tc.expectedBalance, balance)
|
||||||
require.Equal(t, tc.expectedBufferAmt, bufferAmt)
|
require.Equal(t, tc.expectedBufferAmt, bufferAmt)
|
||||||
|
require.Equal(t, tc.expectedCommitFee, commitFee)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user