Skip to content

Commit 27efdad

Browse files
committed
loopout: re-target sweep's feerate every block
Add type loopOutSweepFeerateProvider which uses new sweeper package (SweepFeeProvider), configures it with the constants of loopout.go (DefaultSweepConfTargetDelta, urgentSweepConfTarget, etc) and provides fee rate (and confTarget, used only in tests) for a swap. Fee rate is plugged into sweepbatcher using WithCustomFeeRate. Added new constants to loopout.go: urgentSweepConfTarget, urgentSweepConfTargetFactor. The value of DefaultSweepConfTargetDelta to 10. Previous value was 18. The variable sets the number of blocks until expiry, when aggressive mode enables. 18 is too early. Every block 100 sats/kw fee bump is disabled. Sweepbatcher re-targets feerate every block according to current mempool conditions and the number of blocks until expiry. Added tests for loopOutSweepFeerateProvider simulating various conditions.
1 parent 7f809c7 commit 27efdad

File tree

4 files changed

+497
-6
lines changed

4 files changed

+497
-6
lines changed

client.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,45 @@ func NewClient(dbDir string, loopDB loopdb.SwapStore,
185185
"NewSweepFetcherFromSwapStore failed: %w", err)
186186
}
187187

188+
// There is circular dependency between executor and sweepbatcher, as
189+
// executor stores sweepbatcher and sweepbatcher depends on
190+
// executor.height() though loopOutSweepFeerateProvider.
191+
var executor *executor
192+
193+
// getHeight returns current height, according to executor.
194+
getHeight := func() int32 {
195+
if executor == nil {
196+
// This must not happen, because executor is set in this
197+
// function, before sweepbatcher.Run is called.
198+
log.Errorf("getHeight called when executor is nil")
199+
200+
return 0
201+
}
202+
203+
return executor.height()
204+
}
205+
206+
loopOutSweepFeerateProvider, err := newLoopOutSweepFeerateProvider(
207+
sweeper, loopDB, cfg.Lnd.ChainParams, getHeight,
208+
)
209+
if err != nil {
210+
return nil, nil, fmt.Errorf("newLoopOutSweepFeerateProvider "+
211+
"failed: %w", err)
212+
}
213+
188214
batcher := sweepbatcher.NewBatcher(
189215
cfg.Lnd.WalletKit, cfg.Lnd.ChainNotifier, cfg.Lnd.Signer,
190216
swapServerClient.MultiMuSig2SignSweep, verifySchnorrSig,
191217
cfg.Lnd.ChainParams, sweeperDb, sweepStore,
218+
219+
// Disable 100 sats/kw fee bump every block and retarget feerate
220+
// every block according to the current mempool condition.
221+
sweepbatcher.WithCustomFeeRate(
222+
loopOutSweepFeerateProvider.GetMinFeeRate,
223+
),
192224
)
193225

194-
executor := newExecutor(&executorConfig{
226+
executor = newExecutor(&executorConfig{
195227
lnd: cfg.Lnd,
196228
store: loopDB,
197229
sweeper: sweeper,

loopout.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,19 @@ const (
5252
DefaultHtlcConfTarget = 6
5353

5454
// DefaultSweepConfTargetDelta is the delta of blocks from a Loop Out
55-
// swap's expiration height at which we begin to use the default sweep
56-
// confirmation target.
57-
//
58-
// TODO(wilmer): tune?
59-
DefaultSweepConfTargetDelta = DefaultSweepConfTarget * 2
55+
// swap's expiration height at which we begin to cap sweep confirmation
56+
// target with urgentSweepConfTarget and multiply feerate by factor
57+
// urgentSweepConfTargetFactor.
58+
DefaultSweepConfTargetDelta = 10
59+
60+
// urgentSweepConfTarget is the confirmation target we'll use when the
61+
// loop-out swap is about to expire (<= DefaultSweepConfTargetDelta
62+
// blocks to expire).
63+
urgentSweepConfTarget = 3
64+
65+
// urgentSweepConfTargetFactor is the factor we apply to fee-rate of
66+
// loop-out sweep if it is about to expire.
67+
urgentSweepConfTargetFactor = 1.1
6068
)
6169

6270
// loopOutSwap contains all the in-memory state related to a pending loop out

loopout_feerate.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package loop
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/btcsuite/btcd/btcutil"
8+
"github.com/btcsuite/btcd/chaincfg"
9+
"github.com/btcsuite/btcd/txscript"
10+
"github.com/lightninglabs/loop/loopdb"
11+
"github.com/lightninglabs/loop/swap"
12+
sweeperpkg "github.com/lightninglabs/loop/sweeper"
13+
"github.com/lightninglabs/loop/utils"
14+
"github.com/lightningnetwork/lnd/input"
15+
"github.com/lightningnetwork/lnd/lntypes"
16+
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
17+
)
18+
19+
// sweepFeeGetter calculates sweep's confTarget and fee in sats.
20+
type sweepFeeGetter interface {
21+
// SweepFeeRate calculates sweep's confTarget and fee in sats.
22+
SweepFeeRate(ctx context.Context, amt btcutil.Amount,
23+
addInputToEstimator func(e *input.TxWeightEstimator) error,
24+
destAddr btcutil.Address, cltvExpiry, height int32,
25+
label string) (int32, btcutil.Amount, chainfee.SatPerKWeight,
26+
lntypes.WeightUnit, error)
27+
}
28+
29+
// loopOutFetcher provides the loop out swap with the given hash.
30+
type loopOutFetcher interface {
31+
// FetchLoopOutSwap returns the loop out swap with the given hash.
32+
FetchLoopOutSwap(ctx context.Context,
33+
hash lntypes.Hash) (*loopdb.LoopOut, error)
34+
}
35+
36+
// heightGetter returns current height known to the swap server.
37+
type heightGetter func() int32
38+
39+
// loopOutSweepFeerateProvider provides sweepbatcher with the info about swap's
40+
// current feerate for loop-out sweep.
41+
type loopOutSweepFeerateProvider struct {
42+
// sweepFeeGetter incorporates the logic of determining swap feerate.
43+
sweepFeeGetter sweepFeeGetter
44+
45+
// loopOutFetcher loads LoopOut from DB by swap hash.
46+
loopOutFetcher loopOutFetcher
47+
48+
// chainParams are the chain parameters of the chain that is used by
49+
// swaps.
50+
chainParams *chaincfg.Params
51+
52+
// getHeight returns current height known to the swap server.
53+
getHeight heightGetter
54+
}
55+
56+
// newLoopOutSweepFeerateProvider builds and returns new instance of
57+
// loopOutSweepFeerateProvider.
58+
func newLoopOutSweepFeerateProvider(sweeper sweeperpkg.Sweeper,
59+
loopOutFetcher loopOutFetcher, chainParams *chaincfg.Params,
60+
getHeight heightGetter) (*loopOutSweepFeerateProvider, error) {
61+
62+
// Initialize sweep fee provider for loop-out's.
63+
sweepFeeProvider := &sweeperpkg.SweepFeeProvider{
64+
Sweeper: sweeper,
65+
66+
DefaultSweepConfTargetDelta: DefaultSweepConfTargetDelta,
67+
UrgentSweepConfTarget: urgentSweepConfTarget,
68+
DefaultSweepConfTargetFactor: 1.0,
69+
UrgentSweepConfTargetFactor: urgentSweepConfTargetFactor,
70+
DisableHighPrioLogic: true,
71+
}
72+
73+
if err := sweepFeeProvider.Validate(); err != nil {
74+
return nil, fmt.Errorf("sweep fee provider failed: %w", err)
75+
}
76+
77+
return &loopOutSweepFeerateProvider{
78+
sweepFeeGetter: sweepFeeProvider,
79+
loopOutFetcher: loopOutFetcher,
80+
chainParams: chainParams,
81+
getHeight: getHeight,
82+
}, nil
83+
}
84+
85+
// GetMinFeeRate returns minimum required feerate for a sweep by swap hash.
86+
func (p *loopOutSweepFeerateProvider) GetMinFeeRate(ctx context.Context,
87+
swapHash lntypes.Hash) (chainfee.SatPerKWeight, error) {
88+
89+
_, feeRate, err := p.GetConfTargetAndFeeRate(ctx, swapHash)
90+
91+
return feeRate, err
92+
}
93+
94+
// GetConfTargetAndFeeRate returns conf target and minimum required feerate
95+
// for a sweep by swap hash.
96+
func (p *loopOutSweepFeerateProvider) GetConfTargetAndFeeRate(
97+
ctx context.Context, swapHash lntypes.Hash) (int32,
98+
chainfee.SatPerKWeight, error) {
99+
100+
// Load the loop-out from DB.
101+
loopOut, err := p.loopOutFetcher.FetchLoopOutSwap(ctx, swapHash)
102+
if err != nil {
103+
return 0, 0, fmt.Errorf("failed to load swap %x from DB: %w",
104+
swapHash[:6], err)
105+
}
106+
107+
contract := loopOut.Contract
108+
if contract == nil {
109+
return 0, 0, fmt.Errorf("loop-out %x has nil Contract",
110+
swapHash[:6])
111+
}
112+
113+
// Determine if we can keyspend.
114+
htlcVersion := utils.GetHtlcScriptVersion(contract.ProtocolVersion)
115+
canKeyspend := htlcVersion >= swap.HtlcV3
116+
117+
// Find addInputToEstimator function.
118+
var addInputToEstimator func(e *input.TxWeightEstimator) error
119+
if canKeyspend {
120+
// Assume the server is cooperative and we produce keyspend.
121+
addInputToEstimator = func(e *input.TxWeightEstimator) error {
122+
e.AddTaprootKeySpendInput(txscript.SigHashDefault)
123+
return nil
124+
}
125+
} else {
126+
// Get the HTLC script for our swap.
127+
htlc, err := utils.GetHtlc(
128+
swapHash, &contract.SwapContract, p.chainParams,
129+
)
130+
if err != nil {
131+
return 0, 0, fmt.Errorf("failed to get HTLC: %w", err)
132+
}
133+
addInputToEstimator = htlc.AddSuccessToEstimator
134+
}
135+
136+
// Transaction weight might be important for feeRate, in case of high
137+
// priority proportional fee, so we accurately assess the size of input.
138+
// The size of output is almost the same for all types, so use P2TR.
139+
var destAddr *btcutil.AddressTaproot
140+
141+
// Get current height.
142+
height := p.getHeight()
143+
if height == 0 {
144+
return 0, 0, fmt.Errorf("got zero best block height")
145+
}
146+
147+
// Construct the label.
148+
label := fmt.Sprintf("loopout-sweep-%x", swapHash[:6])
149+
150+
// Estimate confTarget and feeRate.
151+
confTarget, _, feeRate, _, err := p.sweepFeeGetter.SweepFeeRate(
152+
ctx, contract.AmountRequested, addInputToEstimator, destAddr,
153+
contract.CltvExpiry, height, label,
154+
)
155+
if err != nil {
156+
return 0, 0, fmt.Errorf("failed to determine "+
157+
"confTarget and feeRate for swap hash %x: %w",
158+
swapHash[:6], err)
159+
}
160+
161+
log.Debugf("Estimated for swap %x: feeRate=%s, confTarget=%d.",
162+
swapHash[:6], feeRate, confTarget)
163+
164+
return confTarget, feeRate, nil
165+
}

0 commit comments

Comments
 (0)