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