• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 11219354629

07 Oct 2024 03:56PM UTC coverage: 58.585% (-0.2%) from 58.814%
11219354629

Pull #9147

github

ziggie1984
fixup! sqlc: migration up script for payments.
Pull Request #9147: [Part 1|3] Introduce SQL Payment schema into LND

130227 of 222287 relevant lines covered (58.59%)

29106.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.1
/sweep/aggregator.go
1
package sweep
2

3
import (
4
        "sort"
5

6
        "github.com/btcsuite/btcd/btcutil"
7
        "github.com/btcsuite/btcd/wire"
8
        "github.com/lightningnetwork/lnd/input"
9
        "github.com/lightningnetwork/lnd/lntypes"
10
        "github.com/lightningnetwork/lnd/lnwallet"
11
        "github.com/lightningnetwork/lnd/lnwallet/chainfee"
12
)
13

14
// UtxoAggregator defines an interface that takes a list of inputs and
15
// aggregate them into groups. Each group is used as the inputs to create a
16
// sweeping transaction.
17
type UtxoAggregator interface {
18
        // ClusterInputs takes a list of inputs and groups them into input
19
        // sets. Each input set will be used to create a sweeping transaction.
20
        ClusterInputs(inputs InputsMap) []InputSet
21
}
22

23
// BudgetAggregator is a budget-based aggregator that creates clusters based on
24
// deadlines and budgets of inputs.
25
type BudgetAggregator struct {
26
        // estimator is used when crafting sweep transactions to estimate the
27
        // necessary fee relative to the expected size of the sweep
28
        // transaction.
29
        estimator chainfee.Estimator
30

31
        // maxInputs specifies the maximum number of inputs allowed in a single
32
        // sweep tx.
33
        maxInputs uint32
34
}
35

36
// Compile-time constraint to ensure BudgetAggregator implements UtxoAggregator.
37
var _ UtxoAggregator = (*BudgetAggregator)(nil)
38

39
// NewBudgetAggregator creates a new instance of a BudgetAggregator.
40
func NewBudgetAggregator(estimator chainfee.Estimator,
41
        maxInputs uint32) *BudgetAggregator {
42

43
        return &BudgetAggregator{
44
                estimator: estimator,
45
                maxInputs: maxInputs,
46
        }
6✔
47
}
6✔
48

6✔
49
// clusterGroup defines an alias for a set of inputs that are to be grouped.
6✔
50
type clusterGroup map[int32][]SweeperInput
6✔
51

6✔
52
// ClusterInputs creates a list of input sets from pending inputs.
6✔
53
// 1. filter out inputs whose budget cannot cover min relay fee.
6✔
54
// 2. filter a list of exclusive inputs.
55
// 3. group the inputs into clusters based on their deadline height.
56
// 4. sort the inputs in each cluster by their budget.
57
// 5. optionally split a cluster if it exceeds the max input limit.
58
// 6. create input sets from each of the clusters.
59
// 7. create input sets for each of the exclusive inputs.
60
func (b *BudgetAggregator) ClusterInputs(inputs InputsMap) []InputSet {
61
        // Filter out inputs that have a budget below min relay fee.
62
        filteredInputs := b.filterInputs(inputs)
63

64
        // Create clusters to group inputs based on their deadline height.
65
        clusters := make(clusterGroup, len(filteredInputs))
66

3✔
67
        // exclusiveInputs is a set of inputs that are not to be included in
3✔
68
        // any cluster. These inputs can only be swept independently as there's
3✔
69
        // no guarantee which input will be confirmed first, which means
3✔
70
        // grouping exclusive inputs may jeopardize non-exclusive inputs.
3✔
71
        exclusiveInputs := make(map[wire.OutPoint]clusterGroup)
3✔
72

3✔
73
        // Iterate all the inputs and group them based on their specified
3✔
74
        // deadline heights.
3✔
75
        for _, input := range filteredInputs {
3✔
76
                // Get deadline height, and use the specified default deadline
3✔
77
                // height if it's not set.
3✔
78
                height := input.DeadlineHeight
3✔
79

3✔
80
                // Put exclusive inputs in their own set.
3✔
81
                if input.params.ExclusiveGroup != nil {
12✔
82
                        log.Tracef("Input %v is exclusive", input.OutPoint())
9✔
83
                        exclusiveInputs[input.OutPoint()] = clusterGroup{
9✔
84
                                height: []SweeperInput{*input},
9✔
85
                        }
9✔
86

9✔
87
                        continue
12✔
88
                }
3✔
89

3✔
90
                cluster, ok := clusters[height]
3✔
91
                if !ok {
3✔
92
                        cluster = make([]SweeperInput, 0)
3✔
93
                }
3✔
94

95
                cluster = append(cluster, *input)
96
                clusters[height] = cluster
8✔
97
        }
13✔
98

5✔
99
        // Now that we have the clusters, we can create the input sets.
5✔
100
        //
101
        // NOTE: cannot pre-allocate the slice since we don't know the number
8✔
102
        // of input sets in advance.
8✔
103
        inputSets := make([]InputSet, 0)
104
        for height, cluster := range clusters {
105
                // Sort the inputs by their economical value.
106
                sortedInputs := b.sortInputs(cluster)
107

108
                // Split on locktimes if they are different.
109
                splitClusters := splitOnLocktime(sortedInputs)
3✔
110

8✔
111
                // Create input sets from the cluster.
5✔
112
                for _, cluster := range splitClusters {
5✔
113
                        sets := b.createInputSets(cluster, height)
5✔
114
                        inputSets = append(inputSets, sets...)
5✔
115
                }
5✔
116
        }
5✔
117

5✔
118
        // Create input sets from the exclusive inputs.
10✔
119
        for _, cluster := range exclusiveInputs {
5✔
120
                for height, input := range cluster {
5✔
121
                        sets := b.createInputSets(input, height)
5✔
122
                        inputSets = append(inputSets, sets...)
123
                }
124
        }
125

6✔
126
        return inputSets
6✔
127
}
3✔
128

3✔
129
// createInputSet takes a set of inputs which share the same deadline height
3✔
130
// and turns them into a list of `InputSet`, each set is then used to create a
131
// sweep transaction.
132
//
3✔
133
// TODO(yy): by the time we call this method, all the invalid/uneconomical
134
// inputs have been filtered out, all the inputs have been sorted based on
135
// their budgets, and we are about to create input sets. The only thing missing
136
// here is, we need to group the inputs here even further based on whether
137
// their budgets can cover the starting fee rate used for this input set.
138
func (b *BudgetAggregator) createInputSets(inputs []SweeperInput,
139
        deadlineHeight int32) []InputSet {
140

141
        // sets holds the InputSets that we will return.
142
        sets := make([]InputSet, 0)
143

144
        // Copy the inputs to a new slice so we can modify it.
145
        remainingInputs := make([]SweeperInput, len(inputs))
10✔
146
        copy(remainingInputs, inputs)
10✔
147

10✔
148
        // If the number of inputs is greater than the max inputs allowed, we
10✔
149
        // will split them into smaller clusters.
10✔
150
        for uint32(len(remainingInputs)) > b.maxInputs {
10✔
151
                log.Tracef("Cluster has %v inputs, max is %v, dividing...",
10✔
152
                        len(inputs), b.maxInputs)
10✔
153

10✔
154
                // Copy the inputs to be put into the new set, and update the
10✔
155
                // remaining inputs by removing currentInputs.
10✔
156
                currentInputs := make([]SweeperInput, b.maxInputs)
12✔
157
                copy(currentInputs, remainingInputs[:b.maxInputs])
2✔
158
                remainingInputs = remainingInputs[b.maxInputs:]
2✔
159

2✔
160
                // Create an InputSet using the max allowed number of inputs.
2✔
161
                set, err := NewBudgetInputSet(
2✔
162
                        currentInputs, deadlineHeight,
2✔
163
                )
2✔
164
                if err != nil {
2✔
165
                        log.Errorf("unable to create input set: %v", err)
2✔
166

2✔
167
                        continue
2✔
168
                }
2✔
169

2✔
170
                sets = append(sets, set)
3✔
171
        }
1✔
172

1✔
173
        // Create an InputSet from the remaining inputs.
1✔
174
        if len(remainingInputs) > 0 {
175
                set, err := NewBudgetInputSet(
176
                        remainingInputs, deadlineHeight,
1✔
177
                )
178
                if err != nil {
179
                        log.Errorf("unable to create input set: %v", err)
180
                        return nil
20✔
181
                }
10✔
182

10✔
183
                sets = append(sets, set)
10✔
184
        }
10✔
185

×
186
        return sets
×
187
}
×
188

189
// filterInputs filters out inputs that have,
10✔
190
// - a budget below the min relay fee.
191
// - a budget below its requested starting fee.
192
// - a required output that's below the dust.
10✔
193
func (b *BudgetAggregator) filterInputs(inputs InputsMap) InputsMap {
194
        // Get the current min relay fee for this round.
195
        minFeeRate := b.estimator.RelayFeePerKW()
196

197
        // filterInputs stores a map of inputs that has a budget that at least
198
        // can pay the minimal fee.
199
        filteredInputs := make(InputsMap, len(inputs))
4✔
200

4✔
201
        // Iterate all the inputs and filter out the ones whose budget cannot
4✔
202
        // cover the min fee.
4✔
203
        for _, pi := range inputs {
4✔
204
                op := pi.OutPoint()
4✔
205

4✔
206
                // Get the size of the witness and skip if there's an error.
4✔
207
                witnessSize, _, err := pi.WitnessType().SizeUpperBound()
4✔
208
                if err != nil {
4✔
209
                        log.Warnf("Skipped input=%v: cannot get its size: %v",
21✔
210
                                op, err)
17✔
211

17✔
212
                        continue
17✔
213
                }
17✔
214

18✔
215
                //nolint:lll
1✔
216
                // Calculate the size if the input is included in the tx.
1✔
217
                //
1✔
218
                // NOTE: When including this input, we need to account the
1✔
219
                // non-witness data which is expressed in vb.
220
                //
221
                // TODO(yy): This is not accurate for tapscript input. We need
222
                // to unify calculations used in the `TxWeightEstimator` inside
223
                // `input/size.go` and `weightEstimator` in
224
                // `weight_estimator.go`. And calculate the expected weights
225
                // similar to BOLT-3:
226
                // https://github.com/lightning/bolts/blob/master/03-transactions.md#appendix-a-expected-weights
227
                wu := lntypes.VByte(input.InputSize).ToWU() + witnessSize
228

229
                // Skip inputs that has too little budget.
230
                minFee := minFeeRate.FeeForWeight(wu)
231
                if pi.params.Budget < minFee {
232
                        log.Warnf("Skipped input=%v: has budget=%v, but the "+
233
                                "min fee requires %v (feerate=%v), size=%v", op,
16✔
234
                                pi.params.Budget, minFee,
16✔
235
                                minFeeRate.FeePerVByte(), wu.ToVB())
16✔
236

16✔
237
                        continue
20✔
238
                }
4✔
239

4✔
240
                // Skip inputs that has cannot cover its starting fees.
4✔
241
                startingFeeRate := pi.params.StartingFeeRate.UnwrapOr(
4✔
242
                        chainfee.SatPerKWeight(0),
4✔
243
                )
4✔
244
                startingFee := startingFeeRate.FeeForWeight(wu)
245
                if pi.params.Budget < startingFee {
246
                        log.Errorf("Skipped input=%v: has budget=%v, but the "+
247
                                "starting fee requires %v (feerate=%v), "+
12✔
248
                                "size=%v", op, pi.params.Budget, startingFee,
12✔
249
                                startingFeeRate.FeePerVByte(), wu.ToVB())
12✔
250

12✔
251
                        continue
12✔
252
                }
×
253

×
254
                // If the input comes with a required tx out that is below
×
255
                // dust, we won't add it.
×
256
                //
×
257
                // NOTE: only HtlcSecondLevelAnchorInput returns non-nil
×
258
                // RequiredTxOut.
259
                reqOut := pi.RequiredTxOut()
260
                if reqOut != nil {
261
                        if isDustOutput(reqOut) {
262
                                log.Errorf("Rejected input=%v due to dust "+
263
                                        "required output=%v", op, reqOut.Value)
264

265
                                continue
12✔
266
                        }
15✔
267
                }
4✔
268

1✔
269
                filteredInputs[op] = pi
1✔
270
        }
1✔
271

1✔
272
        return filteredInputs
273
}
274

275
// sortInputs sorts the inputs based on their economical value.
11✔
276
//
277
// NOTE: besides the forced inputs, the sorting won't make any difference
278
// because all the inputs are added to the same set. The exception is when the
4✔
279
// number of inputs exceeds the maxInputs limit, it requires us to split them
280
// into smaller clusters. In that case, the sorting will make a difference as
281
// the budgets of the clusters will be different.
282
func (b *BudgetAggregator) sortInputs(inputs []SweeperInput) []SweeperInput {
283
        // sortedInputs is the final list of inputs sorted by their economical
284
        // value.
285
        sortedInputs := make([]SweeperInput, 0, len(inputs))
286

287
        // Copy the inputs.
288
        sortedInputs = append(sortedInputs, inputs...)
6✔
289

6✔
290
        // Sort the inputs based on their budgets.
6✔
291
        //
6✔
292
        // NOTE: We can implement more sophisticated algorithm as the budget
6✔
293
        // left is a function f(minFeeRate, size) = b1 - s1 * r > b2 - s2 * r,
6✔
294
        // where b1 and b2 are budgets, s1 and s2 are sizes of the inputs.
6✔
295
        sort.Slice(sortedInputs, func(i, j int) bool {
6✔
296
                left := sortedInputs[i].params.Budget
6✔
297
                right := sortedInputs[j].params.Budget
6✔
298

6✔
299
                // Make sure forced inputs are always put in the front.
6✔
300
                leftForce := sortedInputs[i].params.Immediate
6✔
301
                rightForce := sortedInputs[j].params.Immediate
16✔
302

10✔
303
                // If both are forced inputs, we return the one with the higher
10✔
304
                // budget. If neither are forced inputs, we also return the one
10✔
305
                // with the higher budget.
10✔
306
                if leftForce == rightForce {
10✔
307
                        return left > right
10✔
308
                }
10✔
309

10✔
310
                // Otherwise, it's either the left or the right is forced. We
10✔
311
                // can simply return `leftForce` here as, if it's true, the
10✔
312
                // left is forced and should be put in the front. Otherwise,
17✔
313
                // the right is forced and should be put in the front.
7✔
314
                return leftForce
7✔
315
        })
316

317
        return sortedInputs
318
}
319

320
// splitOnLocktime splits the list of inputs based on their locktime.
3✔
321
//
322
// TODO(yy): this is a temporary hack as the blocks are not synced among the
323
// contractcourt and the sweeper.
6✔
324
func splitOnLocktime(inputs []SweeperInput) map[uint32][]SweeperInput {
325
        result := make(map[uint32][]SweeperInput)
326
        noLocktimeInputs := make([]SweeperInput, 0, len(inputs))
327

328
        // mergeLocktime is the locktime that we use to merge all the
329
        // nolocktime inputs into.
330
        var mergeLocktime uint32
7✔
331

7✔
332
        // Iterate all inputs and split them based on their locktimes.
7✔
333
        for _, inp := range inputs {
7✔
334
                locktime, required := inp.RequiredLockTime()
7✔
335
                if !required {
7✔
336
                        log.Tracef("No locktime required for input=%v",
7✔
337
                                inp.OutPoint())
7✔
338

7✔
339
                        noLocktimeInputs = append(noLocktimeInputs, inp)
23✔
340

16✔
341
                        continue
28✔
342
                }
12✔
343

12✔
344
                log.Tracef("Split input=%v on locktime=%v", inp.OutPoint(),
12✔
345
                        locktime)
12✔
346

12✔
347
                // Get the slice - the slice will be initialized if not found.
12✔
348
                inputList := result[locktime]
349

350
                // Add the input to the list.
6✔
351
                inputList = append(inputList, inp)
6✔
352

6✔
353
                // Update the map.
6✔
354
                result[locktime] = inputList
6✔
355

6✔
356
                // Update the merge locktime.
6✔
357
                mergeLocktime = locktime
6✔
358
        }
6✔
359

6✔
360
        // If there are locktime inputs, we will merge the no locktime inputs
6✔
361
        // to the last locktime group found.
6✔
362
        if len(result) > 0 {
6✔
363
                log.Tracef("No locktime inputs has been merged to locktime=%v",
6✔
364
                        mergeLocktime)
365
                result[mergeLocktime] = append(
366
                        result[mergeLocktime], noLocktimeInputs...,
367
                )
368
        } else {
10✔
369
                // Otherwise just return the no locktime inputs.
3✔
370
                result[mergeLocktime] = noLocktimeInputs
3✔
371
        }
3✔
372

3✔
373
        return result
3✔
374
}
9✔
375

6✔
376
// isDustOutput checks if the given output is considered as dust.
6✔
377
func isDustOutput(output *wire.TxOut) bool {
6✔
378
        // Fetch the dust limit for this output.
379
        dustLimit := lnwallet.DustLimitForSize(len(output.PkScript))
7✔
380

381
        // If the output is below the dust limit, we consider it dust.
382
        return btcutil.Amount(output.Value) < dustLimit
383
}
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc