From c43506dee9c0ec06b8f44b77f186131ae96d8f52 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Thu, 26 Jul 2018 21:30:15 -0700 Subject: [PATCH] chainntnfs: add unique ID field to track conf ntfns within notifier --- chainntnfs/bitcoindnotify/bitcoind.go | 2 ++ chainntnfs/btcdnotify/btcd.go | 2 ++ chainntnfs/neutrinonotify/neutrino.go | 8 +++++--- chainntnfs/txconfnotifier.go | 8 ++++++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/chainntnfs/bitcoindnotify/bitcoind.go b/chainntnfs/bitcoindnotify/bitcoind.go index 2139e1f6..dc898a04 100644 --- a/chainntnfs/bitcoindnotify/bitcoind.go +++ b/chainntnfs/bitcoindnotify/bitcoind.go @@ -54,6 +54,7 @@ type chainUpdate struct { // chain client. Multiple concurrent clients are supported. All notifications // are achieved via non-blocking sends on client channels. type BitcoindNotifier struct { + confClientCounter uint64 // To be used atomically. spendClientCounter uint64 // To be used atomically. epochClientCounter uint64 // To be used atomically. @@ -716,6 +717,7 @@ func (b *BitcoindNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, ntfn := &confirmationNotification{ ConfNtfn: chainntnfs.ConfNtfn{ + ConfID: atomic.AddUint64(&b.confClientCounter, 1), TxID: txid, NumConfirmations: numConfs, Event: chainntnfs.NewConfirmationEvent(numConfs), diff --git a/chainntnfs/btcdnotify/btcd.go b/chainntnfs/btcdnotify/btcd.go index 371be550..5e6ee526 100644 --- a/chainntnfs/btcdnotify/btcd.go +++ b/chainntnfs/btcdnotify/btcd.go @@ -61,6 +61,7 @@ type txUpdate struct { // notifications. Multiple concurrent clients are supported. All notifications // are achieved via non-blocking sends on client channels. type BtcdNotifier struct { + confClientCounter uint64 // To be used aotmically. spendClientCounter uint64 // To be used atomically. epochClientCounter uint64 // To be used atomically. @@ -800,6 +801,7 @@ func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, ntfn := &confirmationNotification{ ConfNtfn: chainntnfs.ConfNtfn{ + ConfID: atomic.AddUint64(&b.confClientCounter, 1), TxID: txid, NumConfirmations: numConfs, Event: chainntnfs.NewConfirmationEvent(numConfs), diff --git a/chainntnfs/neutrinonotify/neutrino.go b/chainntnfs/neutrinonotify/neutrino.go index 2ab2ad05..d811f0e5 100644 --- a/chainntnfs/neutrinonotify/neutrino.go +++ b/chainntnfs/neutrinonotify/neutrino.go @@ -48,12 +48,13 @@ var ( // TODO(roasbeef): heavily consolidate with NeutrinoNotifier code // * maybe combine into single package? type NeutrinoNotifier struct { - started int32 // To be used atomically. - stopped int32 // To be used atomically. - + confClientCounter uint64 // To be used atomically. spendClientCounter uint64 // To be used atomically. epochClientCounter uint64 // To be used atomically. + started int32 // To be used atomically. + stopped int32 // To be used atomically. + heightMtx sync.RWMutex bestHeight uint32 @@ -696,6 +697,7 @@ func (n *NeutrinoNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, ntfn := &confirmationsNotification{ ConfNtfn: chainntnfs.ConfNtfn{ + ConfID: atomic.AddUint64(&n.confClientCounter, 1), TxID: txid, NumConfirmations: numConfs, Event: chainntnfs.NewConfirmationEvent(numConfs), diff --git a/chainntnfs/txconfnotifier.go b/chainntnfs/txconfnotifier.go index fef53547..3fe08e40 100644 --- a/chainntnfs/txconfnotifier.go +++ b/chainntnfs/txconfnotifier.go @@ -18,6 +18,10 @@ var ( // once the target transaction gets sufficient confirmations. The client is // asynchronously notified via the ConfirmationEvent channels. type ConfNtfn struct { + // ConfID uniquely identifies the confirmation notification request for + // the specified transaction. + ConfID uint64 + // TxID is the hash of the transaction for which confirmation notifications // are requested. TxID *chainhash.Hash @@ -72,7 +76,7 @@ type TxConfNotifier struct { // confNotifications is an index of notification requests by transaction // hash. - confNotifications map[chainhash.Hash][]*ConfNtfn + confNotifications map[chainhash.Hash]map[uint64]*ConfNtfn // txsByInitialHeight is an index of watched transactions by the height // that they are included at in the blockchain. This is tracked so that @@ -95,7 +99,7 @@ func NewTxConfNotifier(startHeight uint32, reorgSafetyLimit uint32) *TxConfNotif return &TxConfNotifier{ currentHeight: startHeight, reorgSafetyLimit: reorgSafetyLimit, - confNotifications: make(map[chainhash.Hash][]*ConfNtfn), + confNotifications: make(map[chainhash.Hash]map[uint64]*ConfNtfn), txsByInitialHeight: make(map[uint32]map[chainhash.Hash]struct{}), ntfnsByConfirmHeight: make(map[uint32]map[*ConfNtfn]struct{}), quit: make(chan struct{}),