Skip to content

Commit 75641c3

Browse files
committed
sweepbatcher: always fill sweep.minFeeRate
Use customFeeRate if it is provided, otherwise use wallet's EstimateFeeRate. Added flag SkipNextBump to rbfCache to avoid extra bumping upon updating fee rate externally. Fix test TestSweepBatcherCloseDuringAdding, it didn't have confTarget and failed in wallet.EstimateFeeRate call.
1 parent 5dac7ca commit 75641c3

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

sweepbatcher/sweep_batch.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ type rbfCache struct {
156156

157157
// FeeRate is the last used fee rate we used to publish a batch tx.
158158
FeeRate chainfee.SatPerKWeight
159+
160+
// SkipNextBump instructs updateRbfRate to skip one fee bumping.
161+
// It is set upon updating FeeRate externally.
162+
SkipNextBump bool
159163
}
160164

161165
// batch is a collection of sweeps that are published together.
@@ -417,6 +421,7 @@ func (b *batch) addSweep(ctx context.Context, sweep *sweep) (bool, error) {
417421
if b.primarySweepID == sweep.swapHash {
418422
b.cfg.batchConfTarget = sweep.confTarget
419423
b.rbfCache.FeeRate = sweep.minFeeRate
424+
b.rbfCache.SkipNextBump = true
420425
}
421426

422427
return true, nil
@@ -459,6 +464,7 @@ func (b *batch) addSweep(ctx context.Context, sweep *sweep) (bool, error) {
459464
b.primarySweepID = sweep.swapHash
460465
b.cfg.batchConfTarget = sweep.confTarget
461466
b.rbfCache.FeeRate = sweep.minFeeRate
467+
b.rbfCache.SkipNextBump = true
462468

463469
// We also need to start the spend monitor for this new primary
464470
// sweep.
@@ -476,6 +482,7 @@ func (b *batch) addSweep(ctx context.Context, sweep *sweep) (bool, error) {
476482
// the batch is the basis for fee bumps.
477483
if b.rbfCache.FeeRate < sweep.minFeeRate {
478484
b.rbfCache.FeeRate = sweep.minFeeRate
485+
b.rbfCache.SkipNextBump = true
479486
}
480487

481488
return true, b.persistSweep(ctx, *sweep, false)
@@ -1141,10 +1148,15 @@ func (b *batch) updateRbfRate(ctx context.Context) error {
11411148
// If the feeRate is unset then we never published before, so we
11421149
// retrieve the fee estimate from our wallet.
11431150
if b.rbfCache.FeeRate == 0 {
1151+
// We set minFeeRate in each sweep, so fee rate is expected to
1152+
// be initiated here.
1153+
b.log.Warnf("rbfCache.FeeRate is 0, which must not happen.")
1154+
11441155
if b.cfg.batchConfTarget == 0 {
11451156
b.log.Warnf("updateRbfRate called with zero " +
11461157
"batchConfTarget")
11471158
}
1159+
11481160
b.log.Infof("initializing rbf fee rate for conf target=%v",
11491161
b.cfg.batchConfTarget)
11501162
rate, err := b.wallet.EstimateFeeRate(
@@ -1157,8 +1169,13 @@ func (b *batch) updateRbfRate(ctx context.Context) error {
11571169
// Set the initial value for our fee rate.
11581170
b.rbfCache.FeeRate = rate
11591171
} else if !b.cfg.noBumping {
1160-
// Bump the fee rate by the configured step.
1161-
b.rbfCache.FeeRate += defaultFeeRateStep
1172+
if b.rbfCache.SkipNextBump {
1173+
// Skip fee bumping, unset the flag, to bump next time.
1174+
b.rbfCache.SkipNextBump = false
1175+
} else {
1176+
// Bump the fee rate by the configured step.
1177+
b.rbfCache.FeeRate += defaultFeeRateStep
1178+
}
11621179
}
11631180

11641181
b.rbfCache.LastHeight = b.currentHeight

sweepbatcher/sweep_batcher.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,8 @@ func (b *Batcher) loadSweep(ctx context.Context, swapHash lntypes.Hash,
885885
swapHash[:6], err)
886886
}
887887

888-
// minFeeRate is 0 by default. If customFeeRate is not provided, then
889-
// rbfCache.FeeRate is also 0 and method batch.updateRbfRate() updates
890-
// it to current fee rate according to batchConfTarget.
888+
// Find minimum fee rate for the sweep. Use customFeeRate if it is
889+
// provided, otherwise use wallet's EstimateFeeRate.
891890
var minFeeRate chainfee.SatPerKWeight
892891
if b.customFeeRate != nil {
893892
minFeeRate, err = b.customFeeRate(ctx, swapHash)
@@ -899,6 +898,17 @@ func (b *Batcher) loadSweep(ctx context.Context, swapHash lntypes.Hash,
899898
return nil, fmt.Errorf("min fee rate too low (%v) for "+
900899
"%x", minFeeRate, swapHash[:6])
901900
}
901+
} else {
902+
if s.ConfTarget == 0 {
903+
log.Warnf("Fee estimation was requested for zero "+
904+
"confTarget for sweep %x.", swapHash[:6])
905+
}
906+
minFeeRate, err = b.wallet.EstimateFeeRate(ctx, s.ConfTarget)
907+
if err != nil {
908+
return nil, fmt.Errorf("failed to estimate fee rate "+
909+
"for %x, confTarget=%d: %w", swapHash[:6],
910+
s.ConfTarget, err)
911+
}
902912
}
903913

904914
return &sweep{

sweepbatcher/sweep_batcher_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,8 +2110,9 @@ func testSweepBatcherCloseDuringAdding(t *testing.T, store testStore,
21102110
Preimage: lntypes.Preimage{i},
21112111
},
21122112

2113-
DestAddr: destAddr,
2114-
SwapInvoice: swapInvoice,
2113+
DestAddr: destAddr,
2114+
SwapInvoice: swapInvoice,
2115+
SweepConfTarget: 111,
21152116
}
21162117

21172118
err = store.CreateLoopOut(ctx, swapHash, swap)

0 commit comments

Comments
 (0)