Skip to content

sweepbatcher: batch change outputs #976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions loopout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func testCustomSweepConfTarget(t *testing.T) {
// yields a much higher fee rate.
ctx.Lnd.SetFeeEstimate(testReq.SweepConfTarget, 250)
ctx.Lnd.SetFeeEstimate(DefaultSweepConfTarget, 10000)
ctx.Lnd.SetMinRelayFee(250)

cfg := newSwapConfig(
&lnd.LndServices, loopdb.NewStoreMock(t), server, nil,
Expand Down
17 changes: 17 additions & 0 deletions sweepbatcher/greedy_batch_selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ func estimateBatchWeight(batch *batch) (feeDetails, error) {
err)
}

// Add change output weights. Change outputs with identical pkscript
// will be consolidated into a single output.
changeOutputs := make(map[string]struct{})
for _, s := range batch.sweeps {
if s.change == nil {
continue
}

pkScriptString := string(s.change.PkScript)
if _, has := changeOutputs[pkScriptString]; has {
continue
}

weight.AddOutput(s.change.PkScript)
changeOutputs[pkScriptString] = struct{}{}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please add a test case to TestEstimateBatchWeight.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added "two sweeps regular batch with identical change".

}

// Add inputs.
for _, sweep := range batch.sweeps {
if sweep.nonCoopHint || sweep.coopFailed {
Expand Down
118 changes: 112 additions & 6 deletions sweepbatcher/greedy_batch_selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/input"
Expand All @@ -16,24 +17,29 @@ import (

// Useful constants for tests.
const (
lowFeeRate = chainfee.FeePerKwFloor
highFeeRate = chainfee.SatPerKWeight(30000)
lowFeeRate = chainfee.FeePerKwFloor
mediumFeeRate = lowFeeRate + 200
highFeeRate = chainfee.SatPerKWeight(30000)

coopInputWeight = lntypes.WeightUnit(230)
batchOutputWeight = lntypes.WeightUnit(343)
nonCoopInputWeight = lntypes.WeightUnit(393)
nonCoopPenalty = nonCoopInputWeight - coopInputWeight
coopNewBatchWeight = lntypes.WeightUnit(444)
nonCoopNewBatchWeight = coopNewBatchWeight + nonCoopPenalty
changeOutputWeight = lntypes.WeightUnit(input.P2TROutputSize)

// p2pkhDiscount is weight discount P2PKH output has over P2TR output.
p2pkhDiscount = lntypes.WeightUnit(
input.P2TROutputSize-input.P2PKHOutputSize,
) * 4

coopTwoSweepBatchWeight = coopNewBatchWeight + coopInputWeight
nonCoopTwoSweepBatchWeight = coopTwoSweepBatchWeight + 2*nonCoopPenalty
v2v3BatchWeight = nonCoopTwoSweepBatchWeight - 25
mixedTwoSweepBatchWeight = coopTwoSweepBatchWeight + nonCoopPenalty
coopTwoSweepBatchWeight = coopNewBatchWeight + coopInputWeight
coopSingleSweepChangeBatchWeight = coopInputWeight + batchOutputWeight + changeOutputWeight
coopDoubleSweepChangeBatchWeight = 2*coopInputWeight + batchOutputWeight + changeOutputWeight
nonCoopTwoSweepBatchWeight = coopTwoSweepBatchWeight + 2*nonCoopPenalty
v2v3BatchWeight = nonCoopTwoSweepBatchWeight - 25
mixedTwoSweepBatchWeight = coopTwoSweepBatchWeight + nonCoopPenalty
)

// testHtlcV2SuccessEstimator adds weight of non-cooperative input to estimator
Expand Down Expand Up @@ -265,6 +271,13 @@ func TestEstimateBatchWeight(t *testing.T) {
se3 := testHtlcV3SuccessEstimator
trAddr := (*btcutil.AddressTaproot)(nil)

changeAddr := "bc1pdx9ggvtjjcpaqfqk375qhdmzx9xu8dcu7w94lqfcxhh0rj" +
"lwyyeq5ryn6r"
changeAddress, err := btcutil.DecodeAddress(changeAddr, nil)
require.NoError(t, err)
changePkscript, err := txscript.PayToAddrScript(changeAddress)
require.NoError(t, err)

cases := []struct {
name string
batch *batch
Expand All @@ -290,6 +303,58 @@ func TestEstimateBatchWeight(t *testing.T) {
},
},

{
name: "one sweep regular batch with change",
batch: &batch{
id: 1,
rbfCache: rbfCache{
FeeRate: lowFeeRate,
},
sweeps: map[wire.OutPoint]sweep{
outpoint1: {
htlcSuccessEstimator: se3,
change: &wire.TxOut{
PkScript: changePkscript,
},
},
},
},
wantBatchFeeDetails: feeDetails{
BatchId: 1,
FeeRate: lowFeeRate,
Weight: coopSingleSweepChangeBatchWeight,
},
},

{
name: "two sweeps regular batch with identical change",
batch: &batch{
id: 1,
rbfCache: rbfCache{
FeeRate: lowFeeRate,
},
sweeps: map[wire.OutPoint]sweep{
outpoint1: {
htlcSuccessEstimator: se3,
change: &wire.TxOut{
PkScript: changePkscript,
},
},
outpoint2: {
htlcSuccessEstimator: se3,
change: &wire.TxOut{
PkScript: changePkscript,
},
},
},
},
wantBatchFeeDetails: feeDetails{
BatchId: 1,
FeeRate: lowFeeRate,
Weight: coopDoubleSweepChangeBatchWeight,
},
},

{
name: "two sweeps regular batch",
batch: &batch{
Expand Down Expand Up @@ -778,6 +843,47 @@ func TestSelectBatches(t *testing.T) {
},
wantBestBatchesIds: []int32{1, newBatchSignal},
},

{
name: "low fee change sweep, placed in new batch",
batches: []feeDetails{
{
BatchId: 1,
FeeRate: mediumFeeRate,
Weight: coopNewBatchWeight,
},
},
sweep: feeDetails{
FeeRate: lowFeeRate,
Weight: coopInputWeight + changeOutputWeight,
},
oneSweepBatch: feeDetails{
FeeRate: lowFeeRate,
Weight: coopNewBatchWeight,
},
wantBestBatchesIds: []int32{newBatchSignal, 1},
},

{
name: "low fee sweep without change, placed in " +
"existing batch",
batches: []feeDetails{
{
BatchId: 1,
FeeRate: mediumFeeRate,
Weight: coopNewBatchWeight,
},
},
sweep: feeDetails{
FeeRate: lowFeeRate,
Weight: coopInputWeight,
},
oneSweepBatch: feeDetails{
FeeRate: lowFeeRate,
Weight: coopNewBatchWeight,
},
wantBestBatchesIds: []int32{1, newBatchSignal},
},
}

for _, tc := range cases {
Expand Down
Loading