Skip to content
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

sweepbatcher: add greedy batch selection algorithm #787

Merged
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.
hieblmi marked this conversation as resolved.
Show resolved Hide resolved
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
Loading