Skip to content

Commit 4c49039

Browse files
authored
Merge pull request #787 from starius/sweepbatcher-greedy-batch-selection
sweepbatcher: add greedy batch selection algorithm
2 parents e8384be + 7a61d5e commit 4c49039

File tree

7 files changed

+1269
-129
lines changed

7 files changed

+1269
-129
lines changed

loopout_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"context"
55
"errors"
66
"math"
7+
"os"
78
"testing"
89
"time"
910

1011
"github.com/btcsuite/btcd/blockchain"
1112
"github.com/btcsuite/btcd/btcec/v2"
1213
"github.com/btcsuite/btcd/btcutil"
1314
"github.com/btcsuite/btcd/wire"
15+
"github.com/btcsuite/btclog"
1416
"github.com/lightninglabs/lndclient"
1517
"github.com/lightninglabs/loop/loopdb"
1618
"github.com/lightninglabs/loop/sweep"
@@ -255,6 +257,11 @@ func TestCustomSweepConfTarget(t *testing.T) {
255257
func testCustomSweepConfTarget(t *testing.T) {
256258
defer test.Guard(t)()
257259

260+
// Setup logger for sweepbatcher.
261+
logger := btclog.NewBackend(os.Stdout).Logger("SWEEP")
262+
logger.SetLevel(btclog.LevelTrace)
263+
sweepbatcher.UseLogger(logger)
264+
258265
lnd := test.NewMockLnd()
259266
ctx := test.NewContext(t, lnd)
260267
server := newServerMock(lnd)

sweep/sweeper.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ func (s *Sweeper) GetSweepFee(ctx context.Context,
191191
return fee, err
192192
}
193193

194-
// GetSweepFee calculates the required tx fee to spend to P2WKH. It takes a
195-
// function that is expected to add the weight of the input to the weight
194+
// GetSweepFeeDetails calculates the required tx fee to spend to P2WKH. It takes
195+
// a function that is expected to add the weight of the input to the weight
196196
// estimator. It returns also the fee rate and transaction weight.
197197
func (s *Sweeper) GetSweepFeeDetails(ctx context.Context,
198198
addInputEstimate func(*input.TxWeightEstimator) error,
@@ -207,6 +207,30 @@ func (s *Sweeper) GetSweepFeeDetails(ctx context.Context,
207207

208208
// Calculate weight for this tx.
209209
var weightEstimate input.TxWeightEstimator
210+
211+
// Add output.
212+
if err := AddOutputEstimate(&weightEstimate, destAddr); err != nil {
213+
return 0, 0, 0, fmt.Errorf("failed to add output weight "+
214+
"estimate: %w", err)
215+
}
216+
217+
// Add input.
218+
err = addInputEstimate(&weightEstimate)
219+
if err != nil {
220+
return 0, 0, 0, fmt.Errorf("failed to add input weight "+
221+
"estimate: %w", err)
222+
}
223+
224+
// Find weight.
225+
weight := weightEstimate.Weight()
226+
227+
return feeRate.FeeForWeight(weight), feeRate, weight, nil
228+
}
229+
230+
// AddOutputEstimate adds output to weight estimator.
231+
func AddOutputEstimate(weightEstimate *input.TxWeightEstimator,
232+
destAddr btcutil.Address) error {
233+
210234
switch destAddr.(type) {
211235
case *btcutil.AddressWitnessScriptHash:
212236
weightEstimate.AddP2WSHOutput()
@@ -224,16 +248,8 @@ func (s *Sweeper) GetSweepFeeDetails(ctx context.Context,
224248
weightEstimate.AddP2TROutput()
225249

226250
default:
227-
return 0, 0, 0, fmt.Errorf("estimate fee: unknown address "+
228-
"type %T", destAddr)
251+
return fmt.Errorf("unknown address type %T", destAddr)
229252
}
230253

231-
err = addInputEstimate(&weightEstimate)
232-
if err != nil {
233-
return 0, 0, 0, err
234-
}
235-
236-
weight := weightEstimate.Weight()
237-
238-
return feeRate.FeeForWeight(weight), feeRate, weight, nil
254+
return nil
239255
}

0 commit comments

Comments
 (0)