From 7e872566c46baff742ee5e20ca3652e6a99255bc Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Tue, 14 Aug 2018 17:54:21 -0700 Subject: [PATCH] chainntnfs: query the hint cache before registering a conf ntfn In this commit, we alter the different chain notifiers to query their height hint cache before registering a confimation notification. We do this as it's possible that the cache has a higher height hint, which can potentially reduce the amount of blocked fetched when attempting historical dispatches. --- chainntnfs/bitcoindnotify/bitcoind.go | 12 ++++++++++++ chainntnfs/btcdnotify/btcd.go | 12 ++++++++++++ chainntnfs/neutrinonotify/neutrino.go | 12 ++++++++++++ 3 files changed, 36 insertions(+) diff --git a/chainntnfs/bitcoindnotify/bitcoind.go b/chainntnfs/bitcoindnotify/bitcoind.go index 3fd41156..3fc60524 100644 --- a/chainntnfs/bitcoindnotify/bitcoind.go +++ b/chainntnfs/bitcoindnotify/bitcoind.go @@ -869,6 +869,18 @@ type confirmationNotification struct { func (b *BitcoindNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, _ []byte, numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent, error) { + // Before proceeding to register the notification, we'll query our + // height hint cache to determine whether a better one exists. + if hint, err := b.confirmHintCache.QueryConfirmHint(*txid); err == nil { + if hint > heightHint { + chainntnfs.Log.Debugf("Using height hint %d retrieved "+ + "from cache for %v", hint, txid) + heightHint = hint + } + } + + // Construct a notification request for the transaction and send it to + // the main event loop. ntfn := &confirmationNotification{ ConfNtfn: chainntnfs.ConfNtfn{ ConfID: atomic.AddUint64(&b.confClientCounter, 1), diff --git a/chainntnfs/btcdnotify/btcd.go b/chainntnfs/btcdnotify/btcd.go index 13ad76d7..d8b4c909 100644 --- a/chainntnfs/btcdnotify/btcd.go +++ b/chainntnfs/btcdnotify/btcd.go @@ -916,6 +916,18 @@ type confirmationNotification struct { func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, _ []byte, numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent, error) { + // Before proceeding to register the notification, we'll query our + // height hint cache to determine whether a better one exists. + if hint, err := b.confirmHintCache.QueryConfirmHint(*txid); err == nil { + if hint > heightHint { + chainntnfs.Log.Debugf("Using height hint %d retrieved "+ + "from cache for %v", hint, txid) + heightHint = hint + } + } + + // Construct a notification request for the transaction and send it to + // the main event loop. ntfn := &confirmationNotification{ ConfNtfn: chainntnfs.ConfNtfn{ ConfID: atomic.AddUint64(&b.confClientCounter, 1), diff --git a/chainntnfs/neutrinonotify/neutrino.go b/chainntnfs/neutrinonotify/neutrino.go index 7ce0934e..d3aa0d6d 100644 --- a/chainntnfs/neutrinonotify/neutrino.go +++ b/chainntnfs/neutrinonotify/neutrino.go @@ -869,6 +869,18 @@ func (n *NeutrinoNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, pkScript []byte, numConfs, heightHint uint32) (*chainntnfs.ConfirmationEvent, error) { + // Before proceeding to register the notification, we'll query our + // height hint cache to determine whether a better one exists. + if hint, err := n.confirmHintCache.QueryConfirmHint(*txid); err == nil { + if hint > heightHint { + chainntnfs.Log.Debugf("Using height hint %d retrieved "+ + "from cache for %v", hint, txid) + heightHint = hint + } + } + + // Construct a notification request for the transaction and send it to + // the main event loop. ntfn := &confirmationsNotification{ ConfNtfn: chainntnfs.ConfNtfn{ ConfID: atomic.AddUint64(&n.confClientCounter, 1),