From a4c9f62c6b0512eaaa4fc0137b95e83568f74e13 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Sun, 26 Aug 2018 21:36:17 -0700 Subject: [PATCH] chainntnfs/bitcoind: use HistoricalConfDispatch in ntfn registry --- chainntnfs/bitcoindnotify/bitcoind.go | 34 +++++++++++++++------------ 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/chainntnfs/bitcoindnotify/bitcoind.go b/chainntnfs/bitcoindnotify/bitcoind.go index c592c3c8..32756903 100644 --- a/chainntnfs/bitcoindnotify/bitcoind.go +++ b/chainntnfs/bitcoindnotify/bitcoind.go @@ -254,13 +254,7 @@ out: } b.spendNotifications[op][msg.spendID] = msg - case *chainntnfs.ConfNtfn: - chainntnfs.Log.Infof("New confirmation "+ - "subscription: txid=%v, numconfs=%v", - msg.TxID, msg.NumConfirmations) - - currentHeight := uint32(b.bestBlock.Height) - + case *chainntnfs.HistoricalConfDispatch: // Look up whether the transaction is already // included in the active chain. We'll do this // in a goroutine to prevent blocking @@ -270,8 +264,7 @@ out: defer b.wg.Done() confDetails, _, err := b.historicalConfDetails( - msg.TxID, msg.HeightHint, - currentHeight, + msg.TxID, msg.StartHeight, msg.EndHeight, ) if err != nil { chainntnfs.Log.Error(err) @@ -286,8 +279,7 @@ out: // cache at tip, since any pending // rescans have now completed. err = b.txConfNotifier.UpdateConfDetails( - *msg.TxID, msg.ConfID, - confDetails, + *msg.TxID, confDetails, ) if err != nil { chainntnfs.Log.Error(err) @@ -453,7 +445,7 @@ func (b *BitcoindNotifier) handleRelevantTx(tx chain.RelevantTx, bestHeight int3 // historicalConfDetails looks up whether a transaction is already included in a // block in the active chain and, if so, returns details about the confirmation. func (b *BitcoindNotifier) historicalConfDetails(txid *chainhash.Hash, - heightHint, currentHeight uint32) (*chainntnfs.TxConfirmation, + startHeight, endHeight uint32) (*chainntnfs.TxConfirmation, chainntnfs.TxConfStatus, error) { // We'll first attempt to retrieve the transaction using the node's @@ -469,7 +461,7 @@ func (b *BitcoindNotifier) historicalConfDetails(txid *chainhash.Hash, case err != nil: chainntnfs.Log.Debugf("Failed getting conf details from "+ "index (%v), scanning manually", err) - return b.confDetailsManually(txid, heightHint, currentHeight) + return b.confDetailsManually(txid, startHeight, endHeight) // The transaction was found within the node's mempool. case txStatus == chainntnfs.TxFoundMempool: @@ -964,12 +956,24 @@ func (b *BitcoindNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, HeightHint: heightHint, } - if err := b.txConfNotifier.Register(ntfn); err != nil { + chainntnfs.Log.Infof("New confirmation subscription: "+ + "txid=%v, numconfs=%v", txid, numConfs) + + // Register the conf notification with txconfnotifier. A non-nil value + // for `dispatch` will be returned if we are required to perform a + // manual scan for the confirmation. Otherwise the notifier will begin + // watching at tip for the transaction to confirm. + dispatch, err := b.txConfNotifier.Register(ntfn) + if err != nil { return nil, err } + if dispatch == nil { + return ntfn.Event, nil + } + select { - case b.notificationRegistry <- ntfn: + case b.notificationRegistry <- dispatch: return ntfn.Event, nil case <-b.quit: return nil, ErrChainNotifierShuttingDown