Skip to content

Commit db5e0d1

Browse files
committed
sweepbatcher: fix inaccuracies in fee estimations
Use SigHashDefault instead of SigHashAll in weight estimations. Actual code uses SigHashDefault, not SigHashAll. SigHashAll is 1wu larger. Use the actual destination address in weight estimator, not taproot always. In the test the type of address is P2WPKH, not taproot. Updated testSweepFetcher to calculate fee, fee rate and weight accurately and to compare the data observed in the published transaction with the estimates.
1 parent 44d9147 commit db5e0d1

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

sweepbatcher/greedy_batch_selection.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ func estimateBatchWeight(batch *batch) (feeDetails, error) {
199199

200200
// Add inputs.
201201
for _, sweep := range batch.sweeps {
202-
// TODO: it should be txscript.SigHashDefault.
203-
coopWeight.AddTaprootKeySpendInput(txscript.SigHashAll)
202+
coopWeight.AddTaprootKeySpendInput(txscript.SigHashDefault)
204203

205204
err = sweep.htlcSuccessEstimator(&nonCoopWeight)
206205
if err != nil {

sweepbatcher/greedy_batch_selection_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ const (
1717
lowFeeRate = chainfee.FeePerKwFloor
1818
highFeeRate = chainfee.SatPerKWeight(30000)
1919

20-
coopInputWeight = lntypes.WeightUnit(231)
20+
coopInputWeight = lntypes.WeightUnit(230)
2121
nonCoopInputWeight = lntypes.WeightUnit(521)
2222
nonCoopPenalty = nonCoopInputWeight - coopInputWeight
23-
coopNewBatchWeight = lntypes.WeightUnit(445)
23+
coopNewBatchWeight = lntypes.WeightUnit(444)
2424
nonCoopNewBatchWeight = coopNewBatchWeight + nonCoopPenalty
2525

2626
// p2pkhDiscount is weight discount P2PKH output has over P2TR output.

sweepbatcher/sweep_batch.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/lightninglabs/loop/labels"
2323
"github.com/lightninglabs/loop/loopdb"
2424
"github.com/lightninglabs/loop/swap"
25+
sweeppkg "github.com/lightninglabs/loop/sweep"
2526
"github.com/lightningnetwork/lnd/chainntnfs"
2627
"github.com/lightningnetwork/lnd/input"
2728
"github.com/lightningnetwork/lnd/keychain"
@@ -737,7 +738,10 @@ func (b *batch) publishBatch(ctx context.Context) (btcutil.Amount, error) {
737738
return fee, err
738739
}
739740

740-
weightEstimate.AddP2TROutput()
741+
err = sweeppkg.AddOutputEstimate(&weightEstimate, address)
742+
if err != nil {
743+
return fee, err
744+
}
741745

742746
weight := weightEstimate.Weight()
743747
feeForWeight := b.rbfCache.FeeRate.FeeForWeight(weight)
@@ -843,8 +847,7 @@ func (b *batch) publishBatchCoop(ctx context.Context) (btcutil.Amount,
843847
PreviousOutPoint: sweep.outpoint,
844848
})
845849

846-
// TODO: it should be txscript.SigHashDefault.
847-
weightEstimate.AddTaprootKeySpendInput(txscript.SigHashAll)
850+
weightEstimate.AddTaprootKeySpendInput(txscript.SigHashDefault)
848851
}
849852

850853
var address btcutil.Address
@@ -870,7 +873,10 @@ func (b *batch) publishBatchCoop(ctx context.Context) (btcutil.Amount,
870873
return fee, err, false
871874
}
872875

873-
weightEstimate.AddP2TROutput()
876+
err = sweeppkg.AddOutputEstimate(&weightEstimate, address)
877+
if err != nil {
878+
return fee, err, false
879+
}
874880

875881
weight := weightEstimate.Weight()
876882
feeForWeight := b.rbfCache.FeeRate.FeeForWeight(weight)

sweepbatcher/sweep_batcher_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/btcsuite/btcd/blockchain"
1213
"github.com/btcsuite/btcd/btcec/v2"
1314
"github.com/btcsuite/btcd/btcutil"
1415
"github.com/btcsuite/btcd/chaincfg/chainhash"
@@ -1937,7 +1938,7 @@ func testSweepFetcher(t *testing.T, store testStore,
19371938
// Provide min fee rate for the sweep.
19381939
feeRate := chainfee.SatPerKWeight(30000)
19391940
amt := btcutil.Amount(1_000_000)
1940-
weight := lntypes.WeightUnit(445) // Weight for 1-to-1 tx.
1941+
weight := lntypes.WeightUnit(396) // Weight for 1-to-1 tx.
19411942
expectedFee := feeRate.FeeForWeight(weight)
19421943

19431944
swap := &loopdb.LoopOutContract{
@@ -2000,8 +2001,9 @@ func testSweepFetcher(t *testing.T, store testStore,
20002001
}
20012002

20022003
batcher := NewBatcher(lnd.WalletKit, lnd.ChainNotifier, lnd.Signer,
2003-
testMuSig2SignSweep, testVerifySchnorrSig, lnd.ChainParams,
2004-
batcherStore, sweepFetcher, WithCustomFeeRate(customFeeRate))
2004+
nil, testVerifySchnorrSig, lnd.ChainParams,
2005+
batcherStore, sweepFetcher, WithCustomFeeRate(customFeeRate),
2006+
WithCustomSignMuSig2(testSignMuSig2func))
20052007

20062008
var wg sync.WaitGroup
20072009
wg.Add(1)
@@ -2052,6 +2054,12 @@ func testSweepFetcher(t *testing.T, store testStore,
20522054
out := btcutil.Amount(tx.TxOut[0].Value)
20532055
gotFee := amt - out
20542056
require.Equal(t, expectedFee, gotFee, "fees don't match")
2057+
gotWeight := lntypes.WeightUnit(
2058+
blockchain.GetTransactionWeight(btcutil.NewTx(tx)),
2059+
)
2060+
require.Equal(t, weight, gotWeight, "weights don't match")
2061+
gotFeeRate := chainfee.NewSatPerKWeight(gotFee, gotWeight)
2062+
require.Equal(t, feeRate, gotFeeRate, "fee rates don't match")
20552063

20562064
// Make sure we have stored the batch.
20572065
batches, err := batcherStore.FetchUnconfirmedSweepBatches(ctx)

0 commit comments

Comments
 (0)