sweep: make sweeper block-driven instead of time-driven

This commit changes the source that drives the state changes in the
sweeper. Previously we used a ticker with default interval of 30s to
trigger sweepings periodically. The assumption is, within this 30s we'd
batch multiple inputs into one transaction to maximize profits. However,
the efficacy of this batch is questionable.

At a high level, we can put our inputs into two categories - one that's
forced, and one that's not. For forced inputs, we should sweep them
immediately as the force flag indicates they are very urgent, eg,
CPFPing the force closing tx. For non-forced inputs, such as anchors
or HTLCs with CLTV that's far away, we can wait to sweep them till a new
block comes in and triggers the sweeping process.

Eventually, all inputs will be deadline-aware, and the sweeper will
consult our fee bumper about the most economical fee rate to be used for
a given deadline. Since the deadlines here are blockstamp, it's also
easier to manage them if the sweeper is also using blockstamp instead of
timestamp.
This commit is contained in:
yyforyongyu committed 2024-04-19 21:33:24 +08:00
1 parent df4e51e2e0
commit a7e9c08baf
3 files changed
+57 -23

No files matched your search

+18 -16
View File
@@ -631,12 +631,6 @@ func (s *UtxoSweeper) collector(blockEpochs <-chan *chainntnfs.BlockEpoch) {
return
}
// Create a ticker based on the config duration.
ticker := time.NewTicker(s.cfg.TickerDuration)
defer ticker.Stop()
log.Debugf("Sweep ticker started")
for {
// Clean inputs, which will remove inputs that are swept,
// failed, or excluded from the sweeper and return inputs that
@@ -651,6 +645,13 @@ func (s *UtxoSweeper) collector(blockEpochs <-chan *chainntnfs.BlockEpoch) {
case input := <-s.newInputs:
s.handleNewInput(input)
// If this input is forced, we perform an sweep
// immediately.
if input.params.Force {
inputs = s.updateSweeperInputs()
s.sweepPendingInputs(inputs)
}
// A spend of one of our inputs is detected. Signal sweep
// results to the caller(s).
case spend := <-s.spendChan:
@@ -670,14 +671,6 @@ func (s *UtxoSweeper) collector(blockEpochs <-chan *chainntnfs.BlockEpoch) {
err: err,
}
// The timer expires and we are going to (re)sweep.
case <-ticker.C:
log.Debugf("Sweep ticker ticks, attempt sweeping %d "+
"inputs", len(inputs))
// Sweep the remaining pending inputs.
s.sweepPendingInputs(inputs)
// A new block comes in, update the bestHeight.
//
// TODO(yy): this is where we check our published transactions
@@ -685,13 +678,22 @@ func (s *UtxoSweeper) collector(blockEpochs <-chan *chainntnfs.BlockEpoch) {
// bumper to get an updated fee rate.
case epoch, ok := <-blockEpochs:
if !ok {
// We should stop the sweeper before stopping
// the chain service. Otherwise it indicates an
// error.
log.Error("Block epoch channel closed")
return
}
// Update the sweeper to the best height.
s.currentHeight = epoch.Height
log.Debugf("New block: height=%v, sha=%v",
epoch.Height, epoch.Hash)
log.Debugf("Received new block: height=%v, attempt "+
"sweeping %d inputs", epoch.Height, len(inputs))
// Attempt to sweep any pending inputs.
s.sweepPendingInputs(inputs)
case <-s.quit:
return