sweep: add new method handleSweep

This commit is contained in:
yyforyongyu 2023-10-12 17:02:40 +08:00
parent f1d0f9f74e
commit 62b5869f87
No known key found for this signature in database
GPG key ID: 9BCD95C4FF296868

View file

@ -635,29 +635,7 @@ func (s *UtxoSweeper) collector(blockEpochs <-chan *chainntnfs.BlockEpoch) {
// The timer expires and we are going to (re)sweep. // The timer expires and we are going to (re)sweep.
case <-s.timer: case <-s.timer:
log.Debugf("Sweep timer expired") log.Debugf("Sweep timer expired")
s.handleSweep(bestHeight)
// Set timer to nil so we know that a new timer needs to
// be started when new inputs arrive.
s.timer = nil
// We'll attempt to cluster all of our inputs with
// similar fee rates. Before attempting to sweep them,
// we'll sort them in descending fee rate order. We do
// this to ensure any inputs which have had their fee
// rate bumped are broadcast first in order enforce the
// RBF policy.
inputClusters := s.createInputClusters()
sort.Slice(inputClusters, func(i, j int) bool {
return inputClusters[i].sweepFeeRate >
inputClusters[j].sweepFeeRate
})
for _, cluster := range inputClusters {
err := s.sweepCluster(cluster, bestHeight)
if err != nil {
log.Errorf("input cluster sweep: %v",
err)
}
}
// A new block comes in. Things may have changed, so we retry a // A new block comes in. Things may have changed, so we retry a
// sweep. // sweep.
@ -1666,6 +1644,31 @@ func (s *UtxoSweeper) handleInputSpent(spend *chainntnfs.SpendDetail,
} }
} }
// handleSweep is called when the ticker fires. It will create clusters and
// attempt to create and publish the sweeping transactions.
func (s *UtxoSweeper) handleSweep(bestHeight int32) {
// Set timer to nil so we know that a new timer needs to be started
// when new inputs arrive.
s.timer = nil
// We'll attempt to cluster all of our inputs with similar fee rates.
// Before attempting to sweep them, we'll sort them in descending fee
// rate order. We do this to ensure any inputs which have had their fee
// rate bumped are broadcast first in order enforce the RBF policy.
inputClusters := s.createInputClusters()
sort.Slice(inputClusters, func(i, j int) bool {
return inputClusters[i].sweepFeeRate >
inputClusters[j].sweepFeeRate
})
for _, cluster := range inputClusters {
err := s.sweepCluster(cluster, bestHeight)
if err != nil {
log.Errorf("input cluster sweep: %v", err)
}
}
}
// init initializes the random generator for random input rescheduling. // init initializes the random generator for random input rescheduling.
func init() { func init() {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())