link+lnwallet: move bandwidth channel reserve validation into channel

Since we want to handle the edge case where paying the HTLC fee would
take the initiator below the reserve, we move the subtraction of the
reserve into availableBalance where this calculation will be performed.
This commit is contained in:
Johan T. Halseth committed 2020-02-19 12:27:42 +01:00
1 parent 58dec10680
commit 5e89d5b6c2
3 files changed
+33 -16

No files matched your search

+17 -2
View File
@@ -6057,6 +6057,17 @@ func (lc *LightningChannel) availableCommitmentBalance(view *htlcView) (
return 0, 0
}
// We can never spend from the channel reserve, so we'll subtract it
// from our available balance.
ourReserve := lnwire.NewMSatFromSatoshis(
lc.channelState.LocalChanCfg.ChanReserve,
)
if ourReserve <= ourBalance {
ourBalance -= ourReserve
} else {
ourBalance = 0
}
// Given the commitment weight, find the commitment fee in case of no
// added HTLC output.
feePerKw := filteredView.feePerKw
@@ -6067,6 +6078,9 @@ func (lc *LightningChannel) availableCommitmentBalance(view *htlcView) (
// If we are the channel initiator, we must to subtract the commitment
// fee from our available balance.
if lc.channelState.IsInitiator {
if ourBalance < baseCommitFee {
return 0, commitWeight
}
ourBalance -= baseCommitFee
}
@@ -6278,13 +6292,14 @@ func (lc *LightningChannel) MaxFeeRate(maxAllocation float64) chainfee.SatPerKWe
// The maximum fee depends of the available balance that can be
// committed towards fees.
balance, weight := lc.availableBalance()
commit := lc.channelState.LocalCommitment
feeBalance := float64(
balance.ToSatoshis() + lc.channelState.LocalCommitment.CommitFee,
commit.LocalBalance.ToSatoshis() + commit.CommitFee,
)
maxFee := feeBalance * maxAllocation
// Ensure the fee rate doesn't dip below the fee floor.
_, weight := lc.availableBalance()
maxFeeRate := maxFee / (float64(weight) / 1000)
return chainfee.SatPerKWeight(
math.Max(maxFeeRate, float64(chainfee.FeePerKwFloor)),
+10 -3
View File
@@ -4599,6 +4599,10 @@ func TestChanAvailableBandwidth(t *testing.T) {
}
defer cleanUp()
aliceReserve := lnwire.NewMSatFromSatoshis(
aliceChannel.channelState.LocalChanCfg.ChanReserve,
)
assertBandwidthEstimateCorrect := func(aliceInitiate bool) {
// With the HTLC's added, we'll now query the AvailableBalance
// method for the current available channel bandwidth from
@@ -4625,11 +4629,14 @@ func TestChanAvailableBandwidth(t *testing.T) {
// Now, we'll obtain the current available bandwidth in Alice's
// latest commitment and compare that to the prior estimate.
aliceBalance := aliceChannel.channelState.LocalCommitment.LocalBalance
if aliceBalance != aliceAvailableBalance {
// The balance we have available for new HTLCs should be the
// current local commitment balance, minus the channel reserve.
expBalance := aliceBalance - aliceReserve
if expBalance != aliceAvailableBalance {
_, _, line, _ := runtime.Caller(1)
t.Fatalf("line: %v, incorrect balance: expected %v, "+
"got %v", line, aliceBalance,
aliceAvailableBalance)
"got %v", line, expBalance, aliceAvailableBalance)
}
}