From b28145b69ede75d562c3ab0ab4abfe7859322ca1 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Wed, 3 Oct 2018 13:43:44 -0700 Subject: [PATCH] chainntnfs/tx_notifier: consume reorg notification for transactions on block inclusion In this commit, we'll attempt to consume a reorg notification for a transaction that was previously reorged out of the chain upon block inclusion to ensure that it is not lingering due to a client not handling it the first time. --- chainntnfs/txconfnotifier.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/chainntnfs/txconfnotifier.go b/chainntnfs/txconfnotifier.go index b7b6b69a5..72f023e55 100644 --- a/chainntnfs/txconfnotifier.go +++ b/chainntnfs/txconfnotifier.go @@ -494,6 +494,9 @@ func (tcn *TxConfNotifier) ConnectTip(blockHash *chainhash.Hash, Log.Debugf("Block contains txid=%v, constructing details", txHash) + // If we have any, we'll record its confirmed height so that + // notifications get dispatched when the transaction reaches the + // clients' desired number of confirmations. details := &TxConfirmation{ BlockHash: blockHash, BlockHeight: blockHeight, @@ -505,6 +508,18 @@ func (tcn *TxConfNotifier) ConnectTip(blockHash *chainhash.Hash, for _, ntfn := range confSet.ntfns { ntfn.details = details + // In the event that this notification was aware that + // the transaction was reorged out of the chain, we'll + // consume the reorg notification if it hasn't been done + // yet already. + select { + case <-ntfn.Event.NegativeConf: + default: + } + + // We'll note this client's required number of + // confirmations so that we can notify them when + // expected. confHeight := blockHeight + ntfn.NumConfirmations - 1 ntfnSet, exists := tcn.ntfnsByConfirmHeight[confHeight] if !exists { @@ -513,6 +528,9 @@ func (tcn *TxConfNotifier) ConnectTip(blockHash *chainhash.Hash, } ntfnSet[ntfn] = struct{}{} + // We'll also note the initial confirmation height in + // order to correctly handle dispatching notifications + // when the transaction gets reorged out of the chain. txSet, exists := tcn.txsByInitialHeight[blockHeight] if !exists { txSet = make(map[chainhash.Hash]struct{}) @@ -602,7 +620,7 @@ out: // Then, we'll dispatch notifications for all the transactions that have // become confirmed at this new block height. - for ntfn := range tcn.ntfnsByConfirmHeight[tcn.currentHeight] { + for ntfn := range tcn.ntfnsByConfirmHeight[blockHeight] { Log.Infof("Dispatching %v conf notification for %v", ntfn.NumConfirmations, ntfn.TxID)