chainntnfs: skip duplicate numConfsLeft notifications

This commit adds a new state to the `ConfNtfn` struct to start tracking
the number of confs left to be notified to avoid sending duplicate
notifications.
This commit is contained in:
yyforyongyu committed 2024-11-19 16:27:26 +08:00
1 parent 7695880c13
commit db6901c9f3
1 file changed
+27 -3
+27 -3
View File
@@ -244,20 +244,25 @@ type ConfNtfn struct {
// notification is to be sent. // notification is to be sent.
NumConfirmations uint32 NumConfirmations uint32
// Event contains references to the channels that the notifications are to // Event contains references to the channels that the notifications are
// be sent over. // to be sent over.
Event *ConfirmationEvent Event *ConfirmationEvent
// HeightHint is the minimum height in the chain that we expect to find // HeightHint is the minimum height in the chain that we expect to find
// this txid. // this txid.
HeightHint uint32 HeightHint uint32
// dispatched is false if the confirmed notification has not been sent yet. // dispatched is false if the confirmed notification has not been sent
// yet.
dispatched bool dispatched bool
// includeBlock is true if the dispatched notification should also have // includeBlock is true if the dispatched notification should also have
// the block included with it. // the block included with it.
includeBlock bool includeBlock bool
// numConfsLeft is the number of confirmations left to be sent to the
// subscriber.
numConfsLeft uint32
} }
// HistoricalConfDispatch parametrizes a manual rescan for a particular // HistoricalConfDispatch parametrizes a manual rescan for a particular
@@ -589,6 +594,7 @@ func (n *TxNotifier) newConfNtfn(txid *chainhash.Hash,
}), }),
HeightHint: heightHint, HeightHint: heightHint,
includeBlock: opts.includeBlock, includeBlock: opts.includeBlock,
numConfsLeft: numConfs,
}, nil }, nil
} }
@@ -1842,6 +1848,9 @@ func (n *TxNotifier) DisconnectTip(blockHeight uint32) error {
default: default:
} }
// We also reset the num of confs update.
ntfn.numConfsLeft = ntfn.NumConfirmations
// Then, we'll check if the current // Then, we'll check if the current
// transaction/output script was included in the // transaction/output script was included in the
// block currently being disconnected. If it // block currently being disconnected. If it
@@ -2081,7 +2090,22 @@ func (n *TxNotifier) TearDown() {
// notifyNumConfsLeft sends the number of confirmations left to the // notifyNumConfsLeft sends the number of confirmations left to the
// notification subscriber through the Event.Updates channel. // notification subscriber through the Event.Updates channel.
//
// NOTE: must be used with the TxNotifier's lock held.
func (n *TxNotifier) notifyNumConfsLeft(ntfn *ConfNtfn, num uint32) error { func (n *TxNotifier) notifyNumConfsLeft(ntfn *ConfNtfn, num uint32) error {
// If the number left is no less than the recorded value, we can skip
// sending it as it means this same value has already been sent before.
if num >= ntfn.numConfsLeft {
Log.Debugf("Skipped dispatched update (numConfsLeft=%v) for "+
"request %v conf_id=%v", num, ntfn.ConfRequest,
ntfn.ConfID)
return nil
}
// Update the number of confirmations left to the notification.
ntfn.numConfsLeft = num
select { select {
case ntfn.Event.Updates <- num: case ntfn.Event.Updates <- num:
case <-n.quit: case <-n.quit: