diff --git a/chainntnfs/btcdnotify/btcd.go b/chainntnfs/btcdnotify/btcd.go index 29d020d0..5a60a83f 100644 --- a/chainntnfs/btcdnotify/btcd.go +++ b/chainntnfs/btcdnotify/btcd.go @@ -324,13 +324,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) - - bestHeight := 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 @@ -340,8 +334,7 @@ out: defer b.wg.Done() confDetails, _, err := b.historicalConfDetails( - msg.TxID, msg.HeightHint, - bestHeight, + msg.TxID, msg.StartHeight, msg.EndHeight, ) if err != nil { chainntnfs.Log.Error(err) @@ -356,8 +349,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) @@ -523,7 +515,7 @@ out: // 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 *BtcdNotifier) 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 @@ -539,7 +531,7 @@ func (b *BtcdNotifier) 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: @@ -638,15 +630,15 @@ func (b *BtcdNotifier) confDetailsFromTxIndex(txid *chainhash.Hash, // earliest height the transaction could have been included in, to the current // height in the chain. If the transaction is found, its confirmation details // are returned. Otherwise, nil is returned. -func (b *BtcdNotifier) confDetailsManually(txid *chainhash.Hash, heightHint, - currentHeight uint32) (*chainntnfs.TxConfirmation, +func (b *BtcdNotifier) confDetailsManually(txid *chainhash.Hash, startHeight, + endHeight uint32) (*chainntnfs.TxConfirmation, chainntnfs.TxConfStatus, error) { targetTxidStr := txid.String() // Begin scanning blocks at every height to determine where the // transaction was included in. - for height := heightHint; height <= currentHeight; height++ { + for height := startHeight; height <= endHeight; height++ { // Ensure we haven't been requested to shut down before // processing the next height. select { @@ -1024,12 +1016,24 @@ func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, _ []byte, 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