sweep: remove block subscription in UtxoSweeper and TxPublisher
This commit removes the independent block subscriptions in `UtxoSweeper` and `TxPublisher`. These subsystems now listen to the `BlockbeatChan` for new blocks.
This commit is contained in:
2 files changed
+18
-55
No files matched your search
+9
-22
@@ -802,13 +802,8 @@ func (t *TxPublisher) Start() error {
|
|||||||
return fmt.Errorf("TxPublisher started more than once")
|
return fmt.Errorf("TxPublisher started more than once")
|
||||||
}
|
}
|
||||||
|
|
||||||
blockEvent, err := t.cfg.Notifier.RegisterBlockEpochNtfn(nil)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("register block epoch ntfn: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.wg.Add(1)
|
t.wg.Add(1)
|
||||||
go t.monitor(blockEvent)
|
go t.monitor()
|
||||||
|
|
||||||
log.Debugf("TxPublisher started")
|
log.Debugf("TxPublisher started")
|
||||||
|
|
||||||
@@ -836,33 +831,25 @@ func (t *TxPublisher) Stop() error {
|
|||||||
// to be bumped. If so, it will attempt to bump the fee of the tx.
|
// to be bumped. If so, it will attempt to bump the fee of the tx.
|
||||||
//
|
//
|
||||||
// NOTE: Must be run as a goroutine.
|
// NOTE: Must be run as a goroutine.
|
||||||
func (t *TxPublisher) monitor(blockEvent *chainntnfs.BlockEpochEvent) {
|
func (t *TxPublisher) monitor() {
|
||||||
defer blockEvent.Cancel()
|
|
||||||
defer t.wg.Done()
|
defer t.wg.Done()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case epoch, ok := <-blockEvent.Epochs:
|
case beat := <-t.BlockbeatChan:
|
||||||
if !ok {
|
height := beat.Height()
|
||||||
// We should stop the publisher before stopping
|
log.Debugf("TxPublisher received new block: %v", height)
|
||||||
// the chain service. Otherwise it indicates an
|
|
||||||
// error.
|
|
||||||
log.Error("Block epoch channel closed, exit " +
|
|
||||||
"monitor")
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("TxPublisher received new block: %v",
|
|
||||||
epoch.Height)
|
|
||||||
|
|
||||||
// Update the best known height for the publisher.
|
// Update the best known height for the publisher.
|
||||||
t.currentHeight.Store(epoch.Height)
|
t.currentHeight.Store(height)
|
||||||
|
|
||||||
// Check all monitored txns to see if any of them needs
|
// Check all monitored txns to see if any of them needs
|
||||||
// to be bumped.
|
// to be bumped.
|
||||||
t.processRecords()
|
t.processRecords()
|
||||||
|
|
||||||
|
// Notify we've processed the block.
|
||||||
|
t.NotifyBlockProcessed(beat, nil)
|
||||||
|
|
||||||
case <-t.quit:
|
case <-t.quit:
|
||||||
log.Debug("Fee bumper stopped, exit monitor")
|
log.Debug("Fee bumper stopped, exit monitor")
|
||||||
return
|
return
|
||||||
|
|||||||
+9
-33
@@ -452,21 +452,12 @@ func (s *UtxoSweeper) Start() error {
|
|||||||
// not change from here on.
|
// not change from here on.
|
||||||
s.relayFeeRate = s.cfg.FeeEstimator.RelayFeePerKW()
|
s.relayFeeRate = s.cfg.FeeEstimator.RelayFeePerKW()
|
||||||
|
|
||||||
// We need to register for block epochs and retry sweeping every block.
|
|
||||||
// We should get a notification with the current best block immediately
|
|
||||||
// if we don't provide any epoch. We'll wait for that in the collector.
|
|
||||||
blockEpochs, err := s.cfg.Notifier.RegisterBlockEpochNtfn(nil)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("register block epoch ntfn: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start sweeper main loop.
|
// Start sweeper main loop.
|
||||||
s.wg.Add(1)
|
s.wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer blockEpochs.Cancel()
|
|
||||||
defer s.wg.Done()
|
defer s.wg.Done()
|
||||||
|
|
||||||
s.collector(blockEpochs.Epochs)
|
s.collector()
|
||||||
|
|
||||||
// The collector exited and won't longer handle incoming
|
// The collector exited and won't longer handle incoming
|
||||||
// requests. This can happen on shutdown, when the block
|
// requests. This can happen on shutdown, when the block
|
||||||
@@ -657,17 +648,8 @@ func (s *UtxoSweeper) removeConflictSweepDescendants(
|
|||||||
|
|
||||||
// collector is the sweeper main loop. It processes new inputs, spend
|
// collector is the sweeper main loop. It processes new inputs, spend
|
||||||
// notifications and counts down to publication of the sweep tx.
|
// notifications and counts down to publication of the sweep tx.
|
||||||
func (s *UtxoSweeper) collector(blockEpochs <-chan *chainntnfs.BlockEpoch) {
|
func (s *UtxoSweeper) collector() {
|
||||||
// We registered for the block epochs with a nil request. The notifier
|
defer s.wg.Done()
|
||||||
// should send us the current best block immediately. So we need to wait
|
|
||||||
// for it here because we need to know the current best height.
|
|
||||||
select {
|
|
||||||
case bestBlock := <-blockEpochs:
|
|
||||||
s.currentHeight = bestBlock.Height
|
|
||||||
|
|
||||||
case <-s.quit:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// Clean inputs, which will remove inputs that are swept,
|
// Clean inputs, which will remove inputs that are swept,
|
||||||
@@ -737,25 +719,16 @@ func (s *UtxoSweeper) collector(blockEpochs <-chan *chainntnfs.BlockEpoch) {
|
|||||||
|
|
||||||
// A new block comes in, update the bestHeight, perform a check
|
// A new block comes in, update the bestHeight, perform a check
|
||||||
// over all pending inputs and publish sweeping txns if needed.
|
// over all pending inputs and publish sweeping txns if needed.
|
||||||
case epoch, ok := <-blockEpochs:
|
case beat := <-s.BlockbeatChan:
|
||||||
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.
|
// Update the sweeper to the best height.
|
||||||
s.currentHeight = epoch.Height
|
s.currentHeight = beat.Height()
|
||||||
|
|
||||||
// Update the inputs with the latest height.
|
// Update the inputs with the latest height.
|
||||||
inputs := s.updateSweeperInputs()
|
inputs := s.updateSweeperInputs()
|
||||||
|
|
||||||
log.Debugf("Received new block: height=%v, attempt "+
|
log.Debugf("Received new block: height=%v, attempt "+
|
||||||
"sweeping %d inputs:\n%s",
|
"sweeping %d inputs:\n%s",
|
||||||
epoch.Height, len(inputs),
|
s.currentHeight, len(inputs),
|
||||||
lnutils.NewLogClosure(func() string {
|
lnutils.NewLogClosure(func() string {
|
||||||
inps := make(
|
inps := make(
|
||||||
[]input.Input, 0, len(inputs),
|
[]input.Input, 0, len(inputs),
|
||||||
@@ -770,6 +743,9 @@ func (s *UtxoSweeper) collector(blockEpochs <-chan *chainntnfs.BlockEpoch) {
|
|||||||
// Attempt to sweep any pending inputs.
|
// Attempt to sweep any pending inputs.
|
||||||
s.sweepPendingInputs(inputs)
|
s.sweepPendingInputs(inputs)
|
||||||
|
|
||||||
|
// Notify we've processed the block.
|
||||||
|
s.NotifyBlockProcessed(beat, nil)
|
||||||
|
|
||||||
case <-s.quit:
|
case <-s.quit:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user