Skip to content

Commit 1f07c37

Browse files
authored
Merge pull request #882 from starius/always-mixed-batches
sweepbatcher: always enabled mixed batches
2 parents a5e7a50 + 2c38de1 commit 1f07c37

5 files changed

+222
-837
lines changed

sweepbatcher/greedy_batch_selection.go

+19-70
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (b *Batcher) greedyAddSweep(ctx context.Context, sweep *sweep) error {
5959
// Run the algorithm. Get batchId of possible batches, sorted from best
6060
// to worst.
6161
batchesIds, err := selectBatches(
62-
batches, sweepFeeDetails, newBatchFeeDetails, b.mixedBatch,
62+
batches, sweepFeeDetails, newBatchFeeDetails,
6363
)
6464
if err != nil {
6565
return fmt.Errorf("batch selection algorithm failed for sweep "+
@@ -133,13 +133,10 @@ func estimateSweepFeeIncrement(s *sweep) (feeDetails, feeDetails, error) {
133133
// Create feeDetails for sweep.
134134
sweepFeeDetails := feeDetails{
135135
FeeRate: s.minFeeRate,
136-
NonCoopHint: s.nonCoopHint || s.coopFailed,
137136
IsExternalAddr: s.isExternalAddr,
138137

139138
// Calculate sweep weight as a difference.
140-
MixedWeight: fd2.MixedWeight - fd1.MixedWeight,
141-
CoopWeight: fd2.CoopWeight - fd1.CoopWeight,
142-
NonCoopWeight: fd2.NonCoopWeight - fd1.NonCoopWeight,
139+
Weight: fd2.Weight - fd1.Weight,
143140
}
144141

145142
return sweepFeeDetails, fd1, nil
@@ -158,14 +155,6 @@ func estimateBatchWeight(batch *batch) (feeDetails, error) {
158155
batch.rbfCache.FeeRate)
159156
}
160157

161-
// Find if the batch has at least one non-cooperative sweep.
162-
hasNonCoop := false
163-
for _, sweep := range batch.sweeps {
164-
if sweep.nonCoopHint || sweep.coopFailed {
165-
hasNonCoop = true
166-
}
167-
}
168-
169158
// Find some sweep of the batch. It is used if there is just one sweep.
170159
var theSweep sweep
171160
for _, sweep := range batch.sweeps {
@@ -186,21 +175,11 @@ func estimateBatchWeight(batch *batch) (feeDetails, error) {
186175
destAddr = (*btcutil.AddressTaproot)(nil)
187176
}
188177

189-
// Make three estimators: for mixed, coop and non-coop cases.
190-
var mixedWeight, coopWeight, nonCoopWeight input.TxWeightEstimator
178+
// Make a weight estimator.
179+
var weight input.TxWeightEstimator
191180

192181
// Add output weight to the estimator.
193-
err := sweeppkg.AddOutputEstimate(&mixedWeight, destAddr)
194-
if err != nil {
195-
return feeDetails{}, fmt.Errorf("sweep.AddOutputEstimate: %w",
196-
err)
197-
}
198-
err = sweeppkg.AddOutputEstimate(&coopWeight, destAddr)
199-
if err != nil {
200-
return feeDetails{}, fmt.Errorf("sweep.AddOutputEstimate: %w",
201-
err)
202-
}
203-
err = sweeppkg.AddOutputEstimate(&nonCoopWeight, destAddr)
182+
err := sweeppkg.AddOutputEstimate(&weight, destAddr)
204183
if err != nil {
205184
return feeDetails{}, fmt.Errorf("sweep.AddOutputEstimate: %w",
206185
err)
@@ -209,34 +188,23 @@ func estimateBatchWeight(batch *batch) (feeDetails, error) {
209188
// Add inputs.
210189
for _, sweep := range batch.sweeps {
211190
if sweep.nonCoopHint || sweep.coopFailed {
212-
err = sweep.htlcSuccessEstimator(&mixedWeight)
191+
err = sweep.htlcSuccessEstimator(&weight)
213192
if err != nil {
214193
return feeDetails{}, fmt.Errorf(
215194
"htlcSuccessEstimator failed: %w", err,
216195
)
217196
}
218197
} else {
219-
mixedWeight.AddTaprootKeySpendInput(
198+
weight.AddTaprootKeySpendInput(
220199
txscript.SigHashDefault,
221200
)
222201
}
223-
224-
coopWeight.AddTaprootKeySpendInput(txscript.SigHashDefault)
225-
226-
err = sweep.htlcSuccessEstimator(&nonCoopWeight)
227-
if err != nil {
228-
return feeDetails{}, fmt.Errorf("htlcSuccessEstimator "+
229-
"failed: %w", err)
230-
}
231202
}
232203

233204
return feeDetails{
234205
BatchId: batch.id,
235206
FeeRate: batch.rbfCache.FeeRate,
236-
MixedWeight: mixedWeight.Weight(),
237-
CoopWeight: coopWeight.Weight(),
238-
NonCoopWeight: nonCoopWeight.Weight(),
239-
NonCoopHint: hasNonCoop,
207+
Weight: weight.Weight(),
240208
IsExternalAddr: theSweep.isExternalAddr,
241209
}, nil
242210
}
@@ -250,26 +218,13 @@ const newBatchSignal = -1
250218
type feeDetails struct {
251219
BatchId int32
252220
FeeRate chainfee.SatPerKWeight
253-
MixedWeight lntypes.WeightUnit
254-
CoopWeight lntypes.WeightUnit
255-
NonCoopWeight lntypes.WeightUnit
256-
NonCoopHint bool
221+
Weight lntypes.WeightUnit
257222
IsExternalAddr bool
258223
}
259224

260225
// fee returns fee of onchain transaction representing this instance.
261-
func (e feeDetails) fee(mixedBatch bool) btcutil.Amount {
262-
var weight lntypes.WeightUnit
263-
switch {
264-
case mixedBatch:
265-
weight = e.MixedWeight
266-
case e.NonCoopHint:
267-
weight = e.NonCoopWeight
268-
default:
269-
weight = e.CoopWeight
270-
}
271-
272-
return e.FeeRate.FeeForWeight(weight)
226+
func (e feeDetails) fee() btcutil.Amount {
227+
return e.FeeRate.FeeForWeight(e.Weight)
273228
}
274229

275230
// combine returns new feeDetails, combining properties.
@@ -282,20 +237,15 @@ func (e1 feeDetails) combine(e2 feeDetails) feeDetails {
282237

283238
return feeDetails{
284239
FeeRate: feeRate,
285-
MixedWeight: e1.MixedWeight + e2.MixedWeight,
286-
CoopWeight: e1.CoopWeight + e2.CoopWeight,
287-
NonCoopWeight: e1.NonCoopWeight + e2.NonCoopWeight,
288-
NonCoopHint: e1.NonCoopHint || e2.NonCoopHint,
240+
Weight: e1.Weight + e2.Weight,
289241
IsExternalAddr: e1.IsExternalAddr || e2.IsExternalAddr,
290242
}
291243
}
292244

293245
// selectBatches returns the list of id of batches sorted from best to worst.
294246
// Creation a new batch is encoded as newBatchSignal. For each batch its fee
295-
// rate and a set of weights are provided: weight in case of a mixed batch,
296-
// weight in case of cooperative spending and weight in case non-cooperative
297-
// spending. Also, a hint is provided to signal what spending path will be used
298-
// by the batch.
247+
// rate and a weight is provided. Also, a hint is provided to signal which
248+
// spending path will be used by the batch.
299249
//
300250
// The same data is also provided for the sweep for which we are selecting a
301251
// batch to add. In case of the sweep weights are weight deltas resulted from
@@ -308,10 +258,9 @@ func (e1 feeDetails) combine(e2 feeDetails) feeDetails {
308258
//
309259
// Each fee details has also IsExternalAddr flag. There is a rule that sweeps
310260
// having flag IsExternalAddr must go in individual batches. Cooperative
311-
// spending is only available if all the sweeps support cooperative spending
312-
// path of in a mixed batch.
313-
func selectBatches(batches []feeDetails, sweep, oneSweepBatch feeDetails,
314-
mixedBatch bool) ([]int32, error) {
261+
// spending may only be available for some sweeps supporting it, not for all.
262+
func selectBatches(batches []feeDetails,
263+
sweep, oneSweepBatch feeDetails) ([]int32, error) {
315264

316265
// If the sweep has IsExternalAddr flag, the sweep can't be added to
317266
// a batch, so create new batch for it.
@@ -332,7 +281,7 @@ func selectBatches(batches []feeDetails, sweep, oneSweepBatch feeDetails,
332281
// creation with this sweep only in it. The cost is its full fee.
333282
alternatives = append(alternatives, alternative{
334283
batchId: newBatchSignal,
335-
cost: oneSweepBatch.fee(mixedBatch),
284+
cost: oneSweepBatch.fee(),
336285
})
337286

338287
// Try to add the sweep to every batch, calculate the costs and
@@ -348,7 +297,7 @@ func selectBatches(batches []feeDetails, sweep, oneSweepBatch feeDetails,
348297
combinedBatch := batch.combine(sweep)
349298

350299
// The cost is the fee increase.
351-
cost := combinedBatch.fee(mixedBatch) - batch.fee(mixedBatch)
300+
cost := combinedBatch.fee() - batch.fee()
352301

353302
// The cost must be positive, because we added a sweep.
354303
if cost <= 0 {

0 commit comments

Comments
 (0)