Skip to content

Commit db6901c

Browse files
committed
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.
1 parent 7695880 commit db6901c

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

chainntnfs/txnotifier.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,20 +244,25 @@ type ConfNtfn struct {
244244
// notification is to be sent.
245245
NumConfirmations uint32
246246

247-
// Event contains references to the channels that the notifications are to
248-
// be sent over.
247+
// Event contains references to the channels that the notifications are
248+
// to be sent over.
249249
Event *ConfirmationEvent
250250

251251
// HeightHint is the minimum height in the chain that we expect to find
252252
// this txid.
253253
HeightHint uint32
254254

255-
// dispatched is false if the confirmed notification has not been sent yet.
255+
// dispatched is false if the confirmed notification has not been sent
256+
// yet.
256257
dispatched bool
257258

258259
// includeBlock is true if the dispatched notification should also have
259260
// the block included with it.
260261
includeBlock bool
262+
263+
// numConfsLeft is the number of confirmations left to be sent to the
264+
// subscriber.
265+
numConfsLeft uint32
261266
}
262267

263268
// HistoricalConfDispatch parametrizes a manual rescan for a particular
@@ -589,6 +594,7 @@ func (n *TxNotifier) newConfNtfn(txid *chainhash.Hash,
589594
}),
590595
HeightHint: heightHint,
591596
includeBlock: opts.includeBlock,
597+
numConfsLeft: numConfs,
592598
}, nil
593599
}
594600

@@ -1842,6 +1848,9 @@ func (n *TxNotifier) DisconnectTip(blockHeight uint32) error {
18421848
default:
18431849
}
18441850

1851+
// We also reset the num of confs update.
1852+
ntfn.numConfsLeft = ntfn.NumConfirmations
1853+
18451854
// Then, we'll check if the current
18461855
// transaction/output script was included in the
18471856
// block currently being disconnected. If it
@@ -2081,7 +2090,22 @@ func (n *TxNotifier) TearDown() {
20812090

20822091
// notifyNumConfsLeft sends the number of confirmations left to the
20832092
// notification subscriber through the Event.Updates channel.
2093+
//
2094+
// NOTE: must be used with the TxNotifier's lock held.
20842095
func (n *TxNotifier) notifyNumConfsLeft(ntfn *ConfNtfn, num uint32) error {
2096+
// If the number left is no less than the recorded value, we can skip
2097+
// sending it as it means this same value has already been sent before.
2098+
if num >= ntfn.numConfsLeft {
2099+
Log.Debugf("Skipped dispatched update (numConfsLeft=%v) for "+
2100+
"request %v conf_id=%v", num, ntfn.ConfRequest,
2101+
ntfn.ConfID)
2102+
2103+
return nil
2104+
}
2105+
2106+
// Update the number of confirmations left to the notification.
2107+
ntfn.numConfsLeft = num
2108+
20852109
select {
20862110
case ntfn.Event.Updates <- num:
20872111
case <-n.quit:

0 commit comments

Comments
 (0)