Skip to content

Commit 83afe80

Browse files
committed
loopout: re-target sweep's feerate every block
Add type loopOutSweepFeerateProvider which determines confTarget based on distance to swap expiration, then determines feerate and fee using. Fee rate is plugged into sweepbatcher using WithCustomFeeRate. When determining confTarget, there are few adjustments over raw distance to cltv_expiry: - make sure confTarget is positive (if the swap has expired, raw distance is negative) - If confTarget is less than or equal to DefaultSweepConfTargetDelta (10), cap it with urgentSweepConfTarget and apply fee factor (1.1x). Also, if feerate is less than floor (1 sat/vbyte), then the floor is used. DefaultSweepConfTargetDelta was decreased from 18 to 10. 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 ba46579 commit 83afe80

File tree

4 files changed

+527
-6
lines changed

4 files changed

+527
-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: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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+
"github.com/lightninglabs/loop/utils"
13+
"github.com/lightningnetwork/lnd/input"
14+
"github.com/lightningnetwork/lnd/lntypes"
15+
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
16+
)
17+
18+
// sweeper provides fee, fee rate and weight by confTarget.
19+
type sweeper interface {
20+
// GetSweepFeeDetails calculates the required tx fee to spend to
21+
// destAddr. It takes a function that is expected to add the weight of
22+
// the input to the weight estimator. It also takes a label used for
23+
// logging. It returns also the fee rate and transaction weight.
24+
GetSweepFeeDetails(ctx context.Context,
25+
addInputEstimate func(*input.TxWeightEstimator) error,
26+
destAddr btcutil.Address, sweepConfTarget int32, label string) (
27+
btcutil.Amount, chainfee.SatPerKWeight, lntypes.WeightUnit,
28+
error)
29+
}
30+
31+
// loopOutFetcher provides the loop out swap with the given hash.
32+
type loopOutFetcher interface {
33+
// FetchLoopOutSwap returns the loop out swap with the given hash.
34+
FetchLoopOutSwap(ctx context.Context,
35+
hash lntypes.Hash) (*loopdb.LoopOut, error)
36+
}
37+
38+
// heightGetter returns current height known to the swap server.
39+
type heightGetter func() int32
40+
41+
// loopOutSweepFeerateProvider provides sweepbatcher with the info about swap's
42+
// current feerate for loop-out sweep.
43+
type loopOutSweepFeerateProvider struct {
44+
// sweeper provides fee, fee rate and weight by confTarget.
45+
sweeper sweeper
46+
47+
// loopOutFetcher loads LoopOut from DB by swap hash.
48+
loopOutFetcher loopOutFetcher
49+
50+
// chainParams are the chain parameters of the chain that is used by
51+
// swaps.
52+
chainParams *chaincfg.Params
53+
54+
// getHeight returns current height known to the swap server.
55+
getHeight heightGetter
56+
}
57+
58+
// newLoopOutSweepFeerateProvider builds and returns new instance of
59+
// loopOutSweepFeerateProvider.
60+
func newLoopOutSweepFeerateProvider(sweeper sweeper,
61+
loopOutFetcher loopOutFetcher, chainParams *chaincfg.Params,
62+
getHeight heightGetter) (*loopOutSweepFeerateProvider, error) {
63+
64+
return &loopOutSweepFeerateProvider{
65+
sweeper: sweeper,
66+
loopOutFetcher: loopOutFetcher,
67+
chainParams: chainParams,
68+
getHeight: getHeight,
69+
}, nil
70+
}
71+
72+
// GetMinFeeRate returns minimum required feerate for a sweep by swap hash.
73+
func (p *loopOutSweepFeerateProvider) GetMinFeeRate(ctx context.Context,
74+
swapHash lntypes.Hash) (chainfee.SatPerKWeight, error) {
75+
76+
_, feeRate, err := p.GetConfTargetAndFeeRate(ctx, swapHash)
77+
78+
return feeRate, err
79+
}
80+
81+
// GetConfTargetAndFeeRate returns conf target and minimum required feerate
82+
// for a sweep by swap hash.
83+
func (p *loopOutSweepFeerateProvider) GetConfTargetAndFeeRate(
84+
ctx context.Context, swapHash lntypes.Hash) (int32,
85+
chainfee.SatPerKWeight, error) {
86+
87+
// Load the loop-out from DB.
88+
loopOut, err := p.loopOutFetcher.FetchLoopOutSwap(ctx, swapHash)
89+
if err != nil {
90+
return 0, 0, fmt.Errorf("failed to load swap %x from DB: %w",
91+
swapHash[:6], err)
92+
}
93+
94+
contract := loopOut.Contract
95+
if contract == nil {
96+
return 0, 0, fmt.Errorf("loop-out %x has nil Contract",
97+
swapHash[:6])
98+
}
99+
100+
// Determine if we can keyspend.
101+
htlcVersion := utils.GetHtlcScriptVersion(contract.ProtocolVersion)
102+
canKeyspend := htlcVersion >= swap.HtlcV3
103+
104+
// Find addInputToEstimator function.
105+
var addInputToEstimator func(e *input.TxWeightEstimator) error
106+
if canKeyspend {
107+
// Assume the server is cooperative and we produce keyspend.
108+
addInputToEstimator = func(e *input.TxWeightEstimator) error {
109+
e.AddTaprootKeySpendInput(txscript.SigHashDefault)
110+
return nil
111+
}
112+
} else {
113+
// Get the HTLC script for our swap.
114+
htlc, err := utils.GetHtlc(
115+
swapHash, &contract.SwapContract, p.chainParams,
116+
)
117+
if err != nil {
118+
return 0, 0, fmt.Errorf("failed to get HTLC: %w", err)
119+
}
120+
addInputToEstimator = htlc.AddSuccessToEstimator
121+
}
122+
123+
// Transaction weight might be important for feeRate, in case of high
124+
// priority proportional fee, so we accurately assess the size of input.
125+
// The size of output is almost the same for all types, so use P2TR.
126+
var destAddr *btcutil.AddressTaproot
127+
128+
// Get current height.
129+
height := p.getHeight()
130+
if height == 0 {
131+
return 0, 0, fmt.Errorf("got zero best block height")
132+
}
133+
134+
// blocksUntilExpiry is the number of blocks until the htlc timeout path
135+
// opens for the client to sweep.
136+
blocksUntilExpiry := contract.CltvExpiry - height
137+
138+
// Find confTarget. If the sweep has expired, use confTarget=1, because
139+
// confTarget must be positive.
140+
confTarget := blocksUntilExpiry
141+
if confTarget <= 0 {
142+
log.Infof("Swap %x has expired (blocksUntilExpiry=%d), using "+
143+
"confTarget=1 for it.", swapHash[:6], blocksUntilExpiry)
144+
145+
confTarget = 1
146+
}
147+
148+
feeFactor := float64(1.0)
149+
150+
// If confTarget is less than or equal to DefaultSweepConfTargetDelta,
151+
// cap it with urgentSweepConfTarget and apply fee factor.
152+
if confTarget <= DefaultSweepConfTargetDelta {
153+
// If confTarget is already <= urgentSweepConfTarget, don't
154+
// increase it.
155+
newConfTarget := int32(urgentSweepConfTarget)
156+
if confTarget < newConfTarget {
157+
newConfTarget = confTarget
158+
}
159+
160+
log.Infof("Swap %x is about to expire (blocksUntilExpiry=%d), "+
161+
"reducing its confTarget from %d to %d and multiplying"+
162+
" feerate by %v.", swapHash[:6], blocksUntilExpiry,
163+
confTarget, newConfTarget, urgentSweepConfTargetFactor)
164+
165+
confTarget = newConfTarget
166+
feeFactor = urgentSweepConfTargetFactor
167+
}
168+
169+
// Construct the label.
170+
label := fmt.Sprintf("loopout-sweep-%x", swapHash[:6])
171+
172+
// Estimate confTarget and feeRate.
173+
fee, feeRate, weight, err := p.sweeper.GetSweepFeeDetails(
174+
ctx, addInputToEstimator, destAddr, confTarget, label,
175+
)
176+
if err != nil {
177+
return 0, 0, fmt.Errorf("fee estimator failed, swapHash=%x, "+
178+
"confTarget=%d: %w", swapHash[:6], confTarget, err)
179+
}
180+
181+
// Multiply fee and feerate by fee factor.
182+
fee = btcutil.Amount(float64(fee) * feeFactor)
183+
feeRate = chainfee.SatPerKWeight(float64(feeRate) * feeFactor)
184+
185+
// Sanity check. Make sure fee rate is not too low.
186+
const minFeeRate = chainfee.AbsoluteFeePerKwFloor
187+
if feeRate < minFeeRate {
188+
log.Infof("Got too low fee rate for swap %x: %v. "+
189+
"Increasing it to %v.", swapHash[:6], feeRate, minFeeRate)
190+
191+
feeRate = minFeeRate
192+
193+
if fee < feeRate.FeeForWeight(weight) {
194+
fee = feeRate.FeeForWeight(weight)
195+
}
196+
}
197+
198+
log.Debugf("Estimated for swap %x: feeRate=%s, confTarget=%d.",
199+
swapHash[:6], feeRate, confTarget)
200+
201+
return confTarget, feeRate, nil
202+
}

0 commit comments

Comments
 (0)