multi: apply the new type lntypes.WeightUnit

This commit is contained in:
yyforyongyu
2024-05-24 21:56:30 +08:00
parent dc9a0b31c0
commit 8da68bb7db
36 changed files with 178 additions and 133 deletions

View File

@@ -71,7 +71,7 @@ type TxInfo struct {
Fee btcutil.Amount
// Weight is the weight of the tx.
Weight int64
Weight lntypes.WeightUnit
}
// String returns a human readable version of the tx info.

View File

@@ -9,6 +9,7 @@ import (
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/stretchr/testify/mock"
)
@@ -157,10 +158,10 @@ func (m *MockWitnessType) WitnessGenerator(signer Signer,
// if it would be included in a tx. It also returns if the output itself is a
// nested p2sh output, if so then we need to take into account the extra
// sigScript data size.
func (m *MockWitnessType) SizeUpperBound() (int, bool, error) {
func (m *MockWitnessType) SizeUpperBound() (lntypes.WeightUnit, bool, error) {
args := m.Called()
return args.Int(0), args.Bool(1), args.Error(2)
return args.Get(0).(lntypes.WeightUnit), args.Bool(1), args.Error(2)
}
// AddWeightEstimation adds the estimated size of the witness in bytes to the

View File

@@ -5,6 +5,7 @@ import (
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/lightningnetwork/lnd/lntypes"
)
const (
@@ -863,7 +864,7 @@ type TxWeightEstimator struct {
inputCount uint32
outputCount uint32
inputSize int
inputWitnessSize int
inputWitnessSize lntypes.WeightUnit
outputSize int
}
@@ -888,7 +889,9 @@ func (twe *TxWeightEstimator) AddP2WKHInput() *TxWeightEstimator {
// AddWitnessInput updates the weight estimate to account for an additional
// input spending a native pay-to-witness output. This accepts the total size
// of the witness as a parameter.
func (twe *TxWeightEstimator) AddWitnessInput(witnessSize int) *TxWeightEstimator {
func (twe *TxWeightEstimator) AddWitnessInput(
witnessSize lntypes.WeightUnit) *TxWeightEstimator {
twe.inputSize += InputSize
twe.inputWitnessSize += witnessSize
twe.inputCount++
@@ -905,7 +908,8 @@ func (twe *TxWeightEstimator) AddWitnessInput(witnessSize int) *TxWeightEstimato
// NOTE: The leaf witness size must be calculated without the byte that accounts
// for the number of witness elements, only the total size of all elements on
// the stack that are consumed by the revealed script should be counted.
func (twe *TxWeightEstimator) AddTapscriptInput(leafWitnessSize int,
func (twe *TxWeightEstimator) AddTapscriptInput(
leafWitnessSize lntypes.WeightUnit,
tapscript *waddrmgr.Tapscript) *TxWeightEstimator {
// We add 1 byte for the total number of witness elements.
@@ -915,7 +919,9 @@ func (twe *TxWeightEstimator) AddTapscriptInput(leafWitnessSize int,
1 + len(tapscript.ControlBlock.InclusionProof)
twe.inputSize += InputSize
twe.inputWitnessSize += leafWitnessSize + controlBlockWitnessSize
twe.inputWitnessSize += leafWitnessSize + lntypes.WeightUnit(
controlBlockWitnessSize,
)
twe.inputCount++
twe.hasWitness = true
@@ -956,7 +962,9 @@ func (twe *TxWeightEstimator) AddNestedP2WKHInput() *TxWeightEstimator {
// AddNestedP2WSHInput updates the weight estimate to account for an additional
// input spending a P2SH output with a nested P2WSH redeem script.
func (twe *TxWeightEstimator) AddNestedP2WSHInput(witnessSize int) *TxWeightEstimator {
func (twe *TxWeightEstimator) AddNestedP2WSHInput(
witnessSize lntypes.WeightUnit) *TxWeightEstimator {
twe.inputSize += InputSize + NestedP2WSHSize
twe.inputWitnessSize += witnessSize
twe.inputCount++
@@ -1027,11 +1035,12 @@ func (twe *TxWeightEstimator) AddOutput(pkScript []byte) *TxWeightEstimator {
}
// Weight gets the estimated weight of the transaction.
func (twe *TxWeightEstimator) Weight() int {
func (twe *TxWeightEstimator) Weight() lntypes.WeightUnit {
txSizeStripped := BaseTxSize +
wire.VarIntSerializeSize(uint64(twe.inputCount)) + twe.inputSize +
wire.VarIntSerializeSize(uint64(twe.outputCount)) + twe.outputSize
weight := txSizeStripped * witnessScaleFactor
weight := lntypes.WeightUnit(txSizeStripped * witnessScaleFactor)
if twe.hasWitness {
weight += WitnessHeaderSize + twe.inputWitnessSize
}
@@ -1041,5 +1050,5 @@ func (twe *TxWeightEstimator) Weight() int {
// VSize gets the estimated virtual size of the transactions, in vbytes.
func (twe *TxWeightEstimator) VSize() int {
// A tx's vsize is 1/4 of the weight, rounded up.
return (twe.Weight() + witnessScaleFactor - 1) / witnessScaleFactor
return int(twe.Weight().ToVB())
}

View File

@@ -297,11 +297,12 @@ func TestTxWeightEstimator(t *testing.T) {
tx.AddTxOut(&wire.TxOut{PkScript: p2shScript})
}
expectedWeight := blockchain.GetTransactionWeight(btcutil.NewTx(tx))
if weightEstimate.Weight() != int(expectedWeight) {
t.Errorf("Case %d: Got wrong weight: expected %d, got %d",
i, expectedWeight, weightEstimate.Weight())
}
expectedWeight := blockchain.GetTransactionWeight(
btcutil.NewTx(tx),
)
require.EqualValuesf(t, expectedWeight, weightEstimate.Weight(),
"Case %d: Got wrong weight: expected %d, got %d",
i, expectedWeight, weightEstimate.Weight())
}
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/lntypes"
)
// WitnessGenerator represents a function that is able to generate the final
@@ -32,7 +33,7 @@ type WitnessType interface {
// WitnessType if it would be included in a tx. It also returns if the
// output itself is a nested p2sh output, if so then we need to take
// into account the extra sigScript data size.
SizeUpperBound() (int, bool, error)
SizeUpperBound() (lntypes.WeightUnit, bool, error)
// AddWeightEstimation adds the estimated size of the witness in bytes
// to the given weight estimator.
@@ -726,7 +727,9 @@ func (wt StandardWitnessType) WitnessGenerator(signer Signer,
// sigScript data size.
//
// NOTE: This is part of the WitnessType interface.
func (wt StandardWitnessType) SizeUpperBound() (int, bool, error) {
func (wt StandardWitnessType) SizeUpperBound() (lntypes.WeightUnit,
bool, error) {
switch wt {
// Outputs on a remote commitment transaction that pay directly to us.
case CommitSpendNoDelayTweakless:
@@ -743,7 +746,8 @@ func (wt StandardWitnessType) SizeUpperBound() (int, bool, error) {
case LeaseCommitmentTimeLock:
size := ToLocalTimeoutWitnessSize +
LeaseWitnessScriptSizeOverhead
return size, false, nil
return lntypes.WeightUnit(size), false, nil
// 1 CSV time locked output to us on remote commitment.
case CommitmentToRemoteConfirmed:
@@ -751,7 +755,8 @@ func (wt StandardWitnessType) SizeUpperBound() (int, bool, error) {
case LeaseCommitmentToRemoteConfirmed:
size := ToRemoteConfirmedWitnessSize +
LeaseWitnessScriptSizeOverhead
return size, false, nil
return lntypes.WeightUnit(size), false, nil
// Anchor output on the commitment transaction.
case CommitmentAnchor:
@@ -765,7 +770,8 @@ func (wt StandardWitnessType) SizeUpperBound() (int, bool, error) {
case LeaseHtlcOfferedTimeoutSecondLevel:
size := ToLocalTimeoutWitnessSize +
LeaseWitnessScriptSizeOverhead
return size, false, nil
return lntypes.WeightUnit(size), false, nil
// Input to the outgoing HTLC second layer timeout transaction.
case HtlcOfferedTimeoutSecondLevelInputConfirmed:
@@ -779,7 +785,8 @@ func (wt StandardWitnessType) SizeUpperBound() (int, bool, error) {
case LeaseHtlcAcceptedSuccessSecondLevel:
size := ToLocalTimeoutWitnessSize +
LeaseWitnessScriptSizeOverhead
return size, false, nil
return lntypes.WeightUnit(size), false, nil
// Input to the incoming second-layer HTLC success transaction.
case HtlcAcceptedSuccessSecondLevelInputConfirmed: