Skip to content

Commit 7cfee83

Browse files
authored
Merge pull request #804 from starius/fix-AddSuccessToEstimator
swap: fix leaf estimation in AddSuccessToEstimator
2 parents a5180ff + fa8703f commit 7cfee83

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

swap/htlc.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,6 @@ func (h *Htlc) GenSuccessWitness(receiverSig []byte,
266266

267267
// AddSuccessToEstimator adds a successful spend to a weight estimator.
268268
func (h *Htlc) AddSuccessToEstimator(estimator *input.TxWeightEstimator) error {
269-
maxSuccessWitnessSize := h.MaxSuccessWitnessSize()
270-
271269
switch h.OutputType {
272270
case HtlcP2TR:
273271
// Generate tapscript.
@@ -283,10 +281,20 @@ func (h *Htlc) AddSuccessToEstimator(estimator *input.TxWeightEstimator) error {
283281
trHtlc.InternalPubKey, successLeaf, timeoutLeafHash[:],
284282
)
285283

286-
estimator.AddTapscriptInput(maxSuccessWitnessSize, tapscript)
284+
// The leaf witness size must be calculated without the byte
285+
// that accounts for the number of witness elements, only the
286+
// total size of all elements on the stack that are consumed by
287+
// the revealed script should be counted. Consumed elements:
288+
// - sigLength: 1 byte
289+
// - sig: 64 bytes
290+
// - preimage_length: 1 byte
291+
// - preimage: 32 bytes
292+
const leafWitnessSize = 1 + 64 + 1 + 32
293+
294+
estimator.AddTapscriptInput(leafWitnessSize, tapscript)
287295

288296
case HtlcP2WSH:
289-
estimator.AddWitnessInput(maxSuccessWitnessSize)
297+
estimator.AddWitnessInput(h.MaxSuccessWitnessSize())
290298
}
291299

292300
return nil

sweepbatcher/greedy_batch_selection_test.go

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const (
1818
highFeeRate = chainfee.SatPerKWeight(30000)
1919

2020
coopInputWeight = lntypes.WeightUnit(230)
21-
nonCoopInputWeight = lntypes.WeightUnit(521)
21+
nonCoopInputWeight = lntypes.WeightUnit(393)
2222
nonCoopPenalty = nonCoopInputWeight - coopInputWeight
2323
coopNewBatchWeight = lntypes.WeightUnit(444)
2424
nonCoopNewBatchWeight = coopNewBatchWeight + nonCoopPenalty
@@ -31,7 +31,7 @@ const (
3131
coopTwoSweepBatchWeight = coopNewBatchWeight + coopInputWeight
3232
nonCoopTwoSweepBatchWeight = coopTwoSweepBatchWeight +
3333
2*nonCoopPenalty
34-
v2v3BatchWeight = nonCoopTwoSweepBatchWeight - 153
34+
v2v3BatchWeight = nonCoopTwoSweepBatchWeight - 25
3535
)
3636

3737
// testHtlcV2SuccessEstimator adds weight of non-cooperative input to estimator
@@ -523,6 +523,37 @@ func TestSelectBatches(t *testing.T) {
523523
NonCoopWeight: nonCoopNewBatchWeight,
524524
NonCoopHint: true,
525525
},
526+
wantBestBatchesIds: []int32{2, newBatchSignal, 1},
527+
},
528+
529+
{
530+
name: "high fee noncoop sweep, large batches",
531+
batches: []feeDetails{
532+
{
533+
BatchId: 1,
534+
FeeRate: lowFeeRate,
535+
CoopWeight: 10000,
536+
NonCoopWeight: 15000,
537+
},
538+
{
539+
BatchId: 2,
540+
FeeRate: highFeeRate,
541+
CoopWeight: 10000,
542+
NonCoopWeight: 15000,
543+
},
544+
},
545+
sweep: feeDetails{
546+
FeeRate: highFeeRate,
547+
CoopWeight: coopInputWeight,
548+
NonCoopWeight: nonCoopInputWeight,
549+
NonCoopHint: true,
550+
},
551+
oneSweepBatch: feeDetails{
552+
FeeRate: highFeeRate,
553+
CoopWeight: coopNewBatchWeight,
554+
NonCoopWeight: nonCoopNewBatchWeight,
555+
NonCoopHint: true,
556+
},
526557
wantBestBatchesIds: []int32{newBatchSignal, 2, 1},
527558
},
528559

@@ -586,6 +617,37 @@ func TestSelectBatches(t *testing.T) {
586617
NonCoopWeight: nonCoopNewBatchWeight,
587618
NonCoopHint: true,
588619
},
620+
wantBestBatchesIds: []int32{1, newBatchSignal, 2},
621+
},
622+
623+
{
624+
name: "low fee noncoop sweep, large batches",
625+
batches: []feeDetails{
626+
{
627+
BatchId: 1,
628+
FeeRate: lowFeeRate,
629+
CoopWeight: 10000,
630+
NonCoopWeight: 15000,
631+
},
632+
{
633+
BatchId: 2,
634+
FeeRate: highFeeRate,
635+
CoopWeight: 10000,
636+
NonCoopWeight: 15000,
637+
},
638+
},
639+
sweep: feeDetails{
640+
FeeRate: lowFeeRate,
641+
CoopWeight: coopInputWeight,
642+
NonCoopWeight: nonCoopInputWeight,
643+
NonCoopHint: true,
644+
},
645+
oneSweepBatch: feeDetails{
646+
FeeRate: lowFeeRate,
647+
CoopWeight: coopNewBatchWeight,
648+
NonCoopWeight: nonCoopNewBatchWeight,
649+
NonCoopHint: true,
650+
},
589651
wantBestBatchesIds: []int32{newBatchSignal, 1, 2},
590652
},
591653

0 commit comments

Comments
 (0)