6
6
"github.com/btcsuite/btcd/btcutil"
7
7
"github.com/btcsuite/btcd/chaincfg"
8
8
"github.com/btcsuite/btcd/chaincfg/chainhash"
9
+ "github.com/btcsuite/btcd/txscript"
9
10
"github.com/btcsuite/btcd/wire"
10
11
"github.com/lightninglabs/loop/swap"
11
12
"github.com/lightningnetwork/lnd/input"
@@ -16,24 +17,28 @@ import (
16
17
17
18
// Useful constants for tests.
18
19
const (
19
- lowFeeRate = chainfee .FeePerKwFloor
20
- highFeeRate = chainfee .SatPerKWeight (30000 )
20
+ lowFeeRate = chainfee .FeePerKwFloor
21
+ mediumFeeRate = lowFeeRate + 200
22
+ highFeeRate = chainfee .SatPerKWeight (30000 )
21
23
22
24
coopInputWeight = lntypes .WeightUnit (230 )
25
+ batchOutputWeight = lntypes .WeightUnit (343 )
23
26
nonCoopInputWeight = lntypes .WeightUnit (393 )
24
27
nonCoopPenalty = nonCoopInputWeight - coopInputWeight
25
28
coopNewBatchWeight = lntypes .WeightUnit (444 )
26
29
nonCoopNewBatchWeight = coopNewBatchWeight + nonCoopPenalty
30
+ changeOutputWeight = lntypes .WeightUnit (input .P2TROutputSize )
27
31
28
32
// p2pkhDiscount is weight discount P2PKH output has over P2TR output.
29
33
p2pkhDiscount = lntypes .WeightUnit (
30
34
input .P2TROutputSize - input .P2PKHOutputSize ,
31
35
) * 4
32
36
33
- coopTwoSweepBatchWeight = coopNewBatchWeight + coopInputWeight
34
- nonCoopTwoSweepBatchWeight = coopTwoSweepBatchWeight + 2 * nonCoopPenalty
35
- v2v3BatchWeight = nonCoopTwoSweepBatchWeight - 25
36
- mixedTwoSweepBatchWeight = coopTwoSweepBatchWeight + nonCoopPenalty
37
+ coopTwoSweepBatchWeight = coopNewBatchWeight + coopInputWeight
38
+ coopSingleSweepChangeBatchWeight = coopInputWeight + batchOutputWeight + changeOutputWeight
39
+ nonCoopTwoSweepBatchWeight = coopTwoSweepBatchWeight + 2 * nonCoopPenalty
40
+ v2v3BatchWeight = nonCoopTwoSweepBatchWeight - 25
41
+ mixedTwoSweepBatchWeight = coopTwoSweepBatchWeight + nonCoopPenalty
37
42
)
38
43
39
44
// testHtlcV2SuccessEstimator adds weight of non-cooperative input to estimator
@@ -265,6 +270,13 @@ func TestEstimateBatchWeight(t *testing.T) {
265
270
se3 := testHtlcV3SuccessEstimator
266
271
trAddr := (* btcutil .AddressTaproot )(nil )
267
272
273
+ changeAddr := "bc1pdx9ggvtjjcpaqfqk375qhdmzx9xu8dcu7w94lqfcxhh0rj" +
274
+ "lwyyeq5ryn6r"
275
+ changeAddress , err := btcutil .DecodeAddress (changeAddr , nil )
276
+ require .NoError (t , err )
277
+ changePkscript , err := txscript .PayToAddrScript (changeAddress )
278
+ require .NoError (t , err )
279
+
268
280
cases := []struct {
269
281
name string
270
282
batch * batch
@@ -290,6 +302,29 @@ func TestEstimateBatchWeight(t *testing.T) {
290
302
},
291
303
},
292
304
305
+ {
306
+ name : "one sweep regular batch with change" ,
307
+ batch : & batch {
308
+ id : 1 ,
309
+ rbfCache : rbfCache {
310
+ FeeRate : lowFeeRate ,
311
+ },
312
+ sweeps : map [wire.OutPoint ]sweep {
313
+ outpoint1 : {
314
+ htlcSuccessEstimator : se3 ,
315
+ change : & wire.TxOut {
316
+ PkScript : changePkscript ,
317
+ },
318
+ },
319
+ },
320
+ },
321
+ wantBatchFeeDetails : feeDetails {
322
+ BatchId : 1 ,
323
+ FeeRate : lowFeeRate ,
324
+ Weight : coopSingleSweepChangeBatchWeight ,
325
+ },
326
+ },
327
+
293
328
{
294
329
name : "two sweeps regular batch" ,
295
330
batch : & batch {
@@ -778,6 +813,47 @@ func TestSelectBatches(t *testing.T) {
778
813
},
779
814
wantBestBatchesIds : []int32 {1 , newBatchSignal },
780
815
},
816
+
817
+ {
818
+ name : "low fee change sweep, placed in new batch" ,
819
+ batches : []feeDetails {
820
+ {
821
+ BatchId : 1 ,
822
+ FeeRate : mediumFeeRate ,
823
+ Weight : coopNewBatchWeight ,
824
+ },
825
+ },
826
+ sweep : feeDetails {
827
+ FeeRate : lowFeeRate ,
828
+ Weight : coopInputWeight + changeOutputWeight ,
829
+ },
830
+ oneSweepBatch : feeDetails {
831
+ FeeRate : lowFeeRate ,
832
+ Weight : coopNewBatchWeight ,
833
+ },
834
+ wantBestBatchesIds : []int32 {newBatchSignal , 1 },
835
+ },
836
+
837
+ {
838
+ name : "high fee change sweep, placed in existing " +
839
+ "medium batch" ,
840
+ batches : []feeDetails {
841
+ {
842
+ BatchId : 1 ,
843
+ FeeRate : mediumFeeRate ,
844
+ Weight : coopNewBatchWeight ,
845
+ },
846
+ },
847
+ sweep : feeDetails {
848
+ FeeRate : highFeeRate ,
849
+ Weight : coopInputWeight + changeOutputWeight ,
850
+ },
851
+ oneSweepBatch : feeDetails {
852
+ FeeRate : highFeeRate ,
853
+ Weight : coopNewBatchWeight ,
854
+ },
855
+ wantBestBatchesIds : []int32 {newBatchSignal , 1 },
856
+ },
781
857
}
782
858
783
859
for _ , tc := range cases {
0 commit comments