Skip to content

Commit

Permalink
Merge pull request #787 from starius/sweepbatcher-greedy-batch-selection
Browse files Browse the repository at this point in the history
sweepbatcher: add greedy batch selection algorithm
  • Loading branch information
starius committed Jul 18, 2024
2 parents e8384be + 7a61d5e commit 4c49039
Show file tree
Hide file tree
Showing 7 changed files with 1,269 additions and 129 deletions.
7 changes: 7 additions & 0 deletions loopout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"context"
"errors"
"math"
"os"
"testing"
"time"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btclog"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/sweep"
Expand Down Expand Up @@ -255,6 +257,11 @@ func TestCustomSweepConfTarget(t *testing.T) {
func testCustomSweepConfTarget(t *testing.T) {
defer test.Guard(t)()

// Setup logger for sweepbatcher.
logger := btclog.NewBackend(os.Stdout).Logger("SWEEP")
logger.SetLevel(btclog.LevelTrace)
sweepbatcher.UseLogger(logger)

lnd := test.NewMockLnd()
ctx := test.NewContext(t, lnd)
server := newServerMock(lnd)
Expand Down
40 changes: 28 additions & 12 deletions sweep/sweeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ func (s *Sweeper) GetSweepFee(ctx context.Context,
return fee, err
}

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

// Calculate weight for this tx.
var weightEstimate input.TxWeightEstimator

// Add output.
if err := AddOutputEstimate(&weightEstimate, destAddr); err != nil {
return 0, 0, 0, fmt.Errorf("failed to add output weight "+
"estimate: %w", err)
}

// Add input.
err = addInputEstimate(&weightEstimate)
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to add input weight "+
"estimate: %w", err)
}

// Find weight.
weight := weightEstimate.Weight()

return feeRate.FeeForWeight(weight), feeRate, weight, nil
}

// AddOutputEstimate adds output to weight estimator.
func AddOutputEstimate(weightEstimate *input.TxWeightEstimator,
destAddr btcutil.Address) error {

switch destAddr.(type) {
case *btcutil.AddressWitnessScriptHash:
weightEstimate.AddP2WSHOutput()
Expand All @@ -224,16 +248,8 @@ func (s *Sweeper) GetSweepFeeDetails(ctx context.Context,
weightEstimate.AddP2TROutput()

default:
return 0, 0, 0, fmt.Errorf("estimate fee: unknown address "+
"type %T", destAddr)
return fmt.Errorf("unknown address type %T", destAddr)
}

err = addInputEstimate(&weightEstimate)
if err != nil {
return 0, 0, 0, err
}

weight := weightEstimate.Weight()

return feeRate.FeeForWeight(weight), feeRate, weight, nil
return nil
}
Loading

0 comments on commit 4c49039

Please sign in to comment.