multi: apply the new type lntypes.WeightUnit

This commit is contained in:
yyforyongyu committed 2024-05-25 13:37:13 +08:00
1 parent dc9a0b31c0
commit 8da68bb7db
36 files changed
+178 -133

No files matched your search

+4 -3
View File
@@ -5,6 +5,7 @@ import (
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/lntypes"
)
const (
@@ -59,13 +60,13 @@ func (s SatPerKVByte) String() string {
type SatPerKWeight btcutil.Amount
// NewSatPerKWeight creates a new fee rate in sat/kw.
func NewSatPerKWeight(fee btcutil.Amount, weight uint64) SatPerKWeight {
return SatPerKWeight(fee.MulF64(1000 / float64(weight)))
func NewSatPerKWeight(fee btcutil.Amount, wu lntypes.WeightUnit) SatPerKWeight {
return SatPerKWeight(fee.MulF64(1000 / float64(wu)))
}
// FeeForWeight calculates the fee resulting from this fee rate and the given
// weight in weight units (wu).
func (s SatPerKWeight) FeeForWeight(wu int64) btcutil.Amount {
func (s SatPerKWeight) FeeForWeight(wu lntypes.WeightUnit) btcutil.Amount {
// The resulting fee is rounded down, as specified in BOLT#03.
return btcutil.Amount(s) * btcutil.Amount(wu) / 1000
}
+1 -1
View File
@@ -240,7 +240,7 @@ func calcCoopCloseFee(chanType channeldb.ChannelType,
weightEstimator.AddTxOutput(remoteOutput)
}
totalWeight := int64(weightEstimator.Weight())
totalWeight := weightEstimator.Weight()
return idealFeeRate.FeeForWeight(totalWeight)
}
+2 -2
View File
@@ -116,7 +116,7 @@ func calculateFees(utxos []wallet.Coin, feeRate chainfee.SatPerKWeight,
// Estimate the fee required for a transaction without a change
// output.
totalWeight := int64(weightEstimate.Weight())
totalWeight := weightEstimate.Weight()
requiredFeeNoChange := feeRate.FeeForWeight(totalWeight)
// Estimate the fee required for a transaction with a change output.
@@ -137,7 +137,7 @@ func calculateFees(utxos []wallet.Coin, feeRate chainfee.SatPerKWeight,
// Now that we have added the change output, redo the fee
// estimate.
totalWeight = int64(weightEstimate.Weight())
totalWeight = weightEstimate.Weight()
requiredFeeWithChange := feeRate.FeeForWeight(totalWeight)
return requiredFeeNoChange, requiredFeeWithChange, nil
+1 -1
View File
@@ -50,7 +50,7 @@ func fundingFee(feeRate chainfee.SatPerKWeight, numInput int, // nolint:unparam
weightEstimate.AddP2TROutput()
}
totalWeight := int64(weightEstimate.Weight())
totalWeight := weightEstimate.Weight()
return feeRate.FeeForWeight(totalWeight)
}
+12 -10
View File
@@ -28,6 +28,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain"
@@ -3800,7 +3801,7 @@ func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate {
// an htlc even if our channel is drained locally.
// See: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md
func CalcFeeBuffer(feePerKw chainfee.SatPerKWeight,
commitWeight int64) lnwire.MilliSatoshi {
commitWeight lntypes.WeightUnit) lnwire.MilliSatoshi {
// Account for a 100% in fee rate increase.
bufferFeePerKw := 2 * feePerKw
@@ -3873,7 +3874,7 @@ func (b BufferType) String() string {
// and verifies that it does not become negative. This function returns the new
// balance and the exact buffer amount (excluding the commitment fee).
func (lc *LightningChannel) applyCommitFee(
balance lnwire.MilliSatoshi, commitWeight int64,
balance lnwire.MilliSatoshi, commitWeight lntypes.WeightUnit,
feePerKw chainfee.SatPerKWeight,
buffer BufferType) (lnwire.MilliSatoshi, lnwire.MilliSatoshi, error) {
@@ -4730,8 +4731,8 @@ func (lc *LightningChannel) ProcessChanSyncMsg(
// If the updateState boolean is set true, the add and remove heights of the
// HTLCs will be set to the next commitment height.
func (lc *LightningChannel) computeView(view *htlcView, remoteChain bool,
updateState bool) (lnwire.MilliSatoshi, lnwire.MilliSatoshi, int64,
*htlcView, error) {
updateState bool) (lnwire.MilliSatoshi, lnwire.MilliSatoshi,
lntypes.WeightUnit, *htlcView, error) {
commitChain := lc.localCommitChain
dustLimit := lc.channelState.LocalChanCfg.DustLimit
@@ -4792,7 +4793,7 @@ func (lc *LightningChannel) computeView(view *htlcView, remoteChain bool,
// Now go through all HTLCs at this stage, to calculate the total
// weight, needed to calculate the transaction fee.
var totalHtlcWeight int64
var totalHtlcWeight lntypes.WeightUnit
for _, htlc := range filteredHTLCView.ourUpdates {
if HtlcIsDust(
lc.channelState.ChanType, false, !remoteChain,
@@ -7522,7 +7523,7 @@ type AnchorResolution struct {
CommitFee btcutil.Amount
// CommitWeight is the weight of the commit tx.
CommitWeight int64
CommitWeight lntypes.WeightUnit
}
// LocalForceCloseSummary describes the final commitment state before the
@@ -8200,7 +8201,7 @@ func NewAnchorResolution(chanState *channeldb.OpenChannel,
return &AnchorResolution{
CommitAnchor: *outPoint,
AnchorSignDescriptor: *signDesc,
CommitWeight: weight,
CommitWeight: lntypes.WeightUnit(weight),
CommitFee: fee,
}, nil
}
@@ -8226,7 +8227,7 @@ func (lc *LightningChannel) AvailableBalance() lnwire.MilliSatoshi {
// this method. Additionally, the total weight of the next to be created
// commitment is returned for accounting purposes.
func (lc *LightningChannel) availableBalance(
buffer BufferType) (lnwire.MilliSatoshi, int64) {
buffer BufferType) (lnwire.MilliSatoshi, lntypes.WeightUnit) {
// We'll grab the current set of log updates that the remote has
// ACKed.
@@ -8265,8 +8266,9 @@ func (lc *LightningChannel) availableBalance(
// commitment, increasing the commitment fee we must pay as an initiator,
// eating into our balance. It will make sure we won't violate the channel
// reserve constraints for this amount.
func (lc *LightningChannel) availableCommitmentBalance(view *htlcView,
remoteChain bool, buffer BufferType) (lnwire.MilliSatoshi, int64) {
func (lc *LightningChannel) availableCommitmentBalance(
view *htlcView, remoteChain bool,
buffer BufferType) (lnwire.MilliSatoshi, lntypes.WeightUnit) {
// Compute the current balances for this commitment. This will take
// into account HTLCs to determine the commit weight, which the
+5 -5
View File
@@ -832,7 +832,7 @@ func TestForceClose(t *testing.T) {
type forceCloseTestCase struct {
chanType channeldb.ChannelType
expectedCommitWeight int64
expectedCommitWeight lntypes.WeightUnit
anchorAmt btcutil.Amount
}
@@ -4803,7 +4803,7 @@ func TestChanAvailableBandwidth(t *testing.T) {
)
assertBandwidthEstimateCorrect := func(aliceInitiate bool,
numNonDustHtlcsOnCommit int64) {
numNonDustHtlcsOnCommit lntypes.WeightUnit) {
// With the HTLC's added, we'll now query the AvailableBalance
// method for the current available channel bandwidth from
@@ -5189,7 +5189,7 @@ func TestChanCommitWeightDustHtlcs(t *testing.T) {
// fromt the given channel's POV.
// When sending htlcs we enforce the feebuffer on the commitment
// transaction.
remoteCommitWeight := func(lc *LightningChannel) int64 {
remoteCommitWeight := func(lc *LightningChannel) lntypes.WeightUnit {
remoteACKedIndex := lc.localCommitChain.tip().theirMessageIndex
htlcView := lc.fetchHTLCView(remoteACKedIndex,
lc.localUpdateLog.logIndex)
@@ -10454,8 +10454,8 @@ func TestApplyCommitmentFee(t *testing.T) {
balanceBelowReserve = lnwire.NewMSatFromSatoshis(5_000)
// commitment weight with an additional htlc.
commitWeight int64 = input.BaseAnchorCommitmentTxWeight +
input.HTLCWeight
commitWeight lntypes.WeightUnit = input.
BaseAnchorCommitmentTxWeight + input.HTLCWeight
// fee rate of 10 sat/vbyte.
feePerKw = chainfee.SatPerKWeight(2500)
+3 -2
View File
@@ -12,6 +12,7 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire"
)
@@ -471,7 +472,7 @@ func SecondLevelHtlcScript(chanType channeldb.ChannelType, initiator bool,
}
// CommitWeight returns the base commitment weight before adding HTLCs.
func CommitWeight(chanType channeldb.ChannelType) int64 {
func CommitWeight(chanType channeldb.ChannelType) lntypes.WeightUnit {
switch {
case chanType.IsTaproot():
return input.TaprootCommitWeight
@@ -724,7 +725,7 @@ func (cb *CommitmentBuilder) createUnsignedCommitmentTx(ourBalance,
// by the current fee-per-kw, then divide by 1000 to get the proper
// fee.
totalCommitWeight := CommitWeight(cb.chanState.ChanType) +
input.HTLCWeight*numHTLCs
lntypes.WeightUnit(input.HTLCWeight*numHTLCs)
// With the weight known, we can now calculate the commitment fee,
// ensuring that we account for any dust outputs trimmed above.
+6 -3
View File
@@ -13,6 +13,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
"github.com/lightningnetwork/lnd/lnwire"
)
@@ -241,13 +242,15 @@ func NewChannelReservation(capacity, localFundingAmt btcutil.Amount,
// Based on the channel type, we determine the initial commit weight
// and fee.
commitWeight := int64(input.CommitWeight)
commitWeight := input.CommitWeight
if req.CommitType.IsTaproot() {
commitWeight = input.TaprootCommitWeight
} else if req.CommitType.HasAnchors() {
commitWeight = int64(input.AnchorCommitWeight)
commitWeight = input.AnchorCommitWeight
}
commitFee := req.CommitFeePerKw.FeeForWeight(commitWeight)
commitFee := req.CommitFeePerKw.FeeForWeight(
lntypes.WeightUnit(commitWeight),
)
localFundingMSat := lnwire.NewMSatFromSatoshis(localFundingAmt)
// TODO(halseth): make method take remote funding amount directly
+5 -3
View File
@@ -16,6 +16,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain"
@@ -506,9 +507,10 @@ func calcStaticFee(chanType channeldb.ChannelType, numHTLCs int) btcutil.Amount
htlcWeight = 172
feePerKw = btcutil.Amount(24/4) * 1000
)
return feePerKw *
(btcutil.Amount(CommitWeight(chanType) +
htlcWeight*int64(numHTLCs))) / 1000
htlcsWeight := htlcWeight * int64(numHTLCs)
totalWeight := CommitWeight(chanType) + lntypes.WeightUnit(htlcsWeight)
return feePerKw * (btcutil.Amount(totalWeight)) / 1000
}
// ForceStateTransition executes the necessary interaction between the two
+1 -1
View File
@@ -913,7 +913,7 @@ func createTestChannelsForVectors(tc *testContext, chanType channeldb.ChannelTyp
// Create the initial commitment transactions for the channel.
feePerKw := chainfee.SatPerKWeight(feeRate)
commitWeight := int64(input.CommitWeight)
commitWeight := lntypes.WeightUnit(input.CommitWeight)
if chanType.HasAnchors() {
commitWeight = input.AnchorCommitWeight
}