chainntnfs: rename TxConfNotifier -> TxNotifier

This commit is contained in:
Wilmer Paulino 2018-10-05 02:07:55 -07:00
parent f8789e9db0
commit 82f6fd7a91
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F
9 changed files with 231 additions and 233 deletions

@ -71,7 +71,7 @@ type BitcoindNotifier struct {
spendNotifications map[wire.OutPoint]map[uint64]*spendNotification
txConfNotifier *chainntnfs.TxConfNotifier
txNotifier *chainntnfs.TxNotifier
blockEpochClients map[uint64]*blockEpochRegistration
@ -142,7 +142,7 @@ func (b *BitcoindNotifier) Start() error {
return err
}
b.txConfNotifier = chainntnfs.NewTxConfNotifier(
b.txNotifier = chainntnfs.NewTxNotifier(
uint32(currentHeight), reorgSafetyLimit, b.confirmHintCache,
)
@ -184,7 +184,7 @@ func (b *BitcoindNotifier) Stop() error {
close(epochClient.epochChan)
}
b.txConfNotifier.TearDown()
b.txNotifier.TearDown()
return nil
}
@ -278,7 +278,7 @@ out:
// begin safely updating the height hint
// cache at tip, since any pending
// rescans have now completed.
err = b.txConfNotifier.UpdateConfDetails(
err = b.txNotifier.UpdateConfDetails(
*msg.TxID, confDetails,
)
if err != nil {
@ -330,7 +330,7 @@ out:
newBestBlock, missedBlocks, err :=
chainntnfs.HandleMissedBlocks(
b.chainConn,
b.txConfNotifier,
b.txNotifier,
b.bestBlock, item.Height,
true,
)
@ -369,7 +369,7 @@ out:
}
newBestBlock, err := chainntnfs.RewindChain(
b.chainConn, b.txConfNotifier,
b.chainConn, b.txNotifier,
b.bestBlock, item.Height-1,
)
if err != nil {
@ -621,7 +621,7 @@ func (b *BitcoindNotifier) handleBlockConnected(block chainntnfs.BlockEpoch) err
}
txns := btcutil.NewBlock(rawBlock).Transactions()
err = b.txConfNotifier.ConnectTip(
err = b.txNotifier.ConnectTip(
block.Hash, uint32(block.Height), txns)
if err != nil {
return fmt.Errorf("unable to connect tip: %v", err)
@ -959,11 +959,11 @@ func (b *BitcoindNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
chainntnfs.Log.Infof("New confirmation subscription: "+
"txid=%v, numconfs=%v", txid, numConfs)
// Register the conf notification with txconfnotifier. A non-nil value
// Register the conf notification with the TxNotifier. 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)
dispatch, err := b.txNotifier.Register(ntfn)
if err != nil {
return nil, err
}

@ -12,12 +12,11 @@ import (
)
// UnsafeStart starts the notifier with a specified best height and optional
// best hash. Its bestBlock and txConfNotifier are initialized with
// bestHeight and optionally bestHash. The parameter generateBlocks is
// necessary for the bitcoind notifier to ensure we drain all notifications up
// to syncHeight, since if they are generated ahead of UnsafeStart the chainConn
// may start up with an outdated best block and miss sending ntfns. Used for
// testing.
// best hash. Its bestBlock and txNotifier are initialized with bestHeight and
// optionally bestHash. The parameter generateBlocks is necessary for the
// bitcoind notifier to ensure we drain all notifications up to syncHeight,
// since if they are generated ahead of UnsafeStart the chainConn may start up
// with an outdated best block and miss sending ntfns. Used for testing.
func (b *BitcoindNotifier) UnsafeStart(bestHeight int32, bestHash *chainhash.Hash,
syncHeight int32, generateBlocks func() error) error {
@ -30,7 +29,7 @@ func (b *BitcoindNotifier) UnsafeStart(bestHeight int32, bestHash *chainhash.Has
return err
}
b.txConfNotifier = chainntnfs.NewTxConfNotifier(
b.txNotifier = chainntnfs.NewTxNotifier(
uint32(bestHeight), reorgSafetyLimit, b.confirmHintCache,
)

@ -76,7 +76,7 @@ type BtcdNotifier struct {
spendNotifications map[wire.OutPoint]map[uint64]*spendNotification
txConfNotifier *chainntnfs.TxConfNotifier
txNotifier *chainntnfs.TxNotifier
blockEpochClients map[uint64]*blockEpochRegistration
@ -166,7 +166,7 @@ func (b *BtcdNotifier) Start() error {
return err
}
b.txConfNotifier = chainntnfs.NewTxConfNotifier(
b.txNotifier = chainntnfs.NewTxNotifier(
uint32(currentHeight), reorgSafetyLimit, b.confirmHintCache,
)
@ -214,7 +214,7 @@ func (b *BtcdNotifier) Stop() error {
close(epochClient.epochChan)
}
b.txConfNotifier.TearDown()
b.txNotifier.TearDown()
return nil
}
@ -348,7 +348,7 @@ out:
// begin safely updating the height hint
// cache at tip, since any pending
// rescans have now completed.
err = b.txConfNotifier.UpdateConfDetails(
err = b.txNotifier.UpdateConfDetails(
*msg.TxID, confDetails,
)
if err != nil {
@ -398,7 +398,7 @@ out:
newBestBlock, missedBlocks, err :=
chainntnfs.HandleMissedBlocks(
b.chainConn,
b.txConfNotifier,
b.txNotifier,
b.bestBlock,
update.blockHeight,
true,
@ -436,7 +436,7 @@ out:
}
newBestBlock, err := chainntnfs.RewindChain(
b.chainConn, b.txConfNotifier, b.bestBlock,
b.chainConn, b.txNotifier, b.bestBlock,
update.blockHeight-1,
)
if err != nil {
@ -703,7 +703,7 @@ func (b *BtcdNotifier) handleBlockConnected(epoch chainntnfs.BlockEpoch) error {
connect: true,
}
err = b.txConfNotifier.ConnectTip(
err = b.txNotifier.ConnectTip(
&newBlock.hash, newBlock.height, newBlock.txns,
)
if err != nil {
@ -1019,11 +1019,11 @@ func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, _ []byte,
chainntnfs.Log.Infof("New confirmation subscription: "+
"txid=%v, numconfs=%v", txid, numConfs)
// Register the conf notification with txconfnotifier. A non-nil value
// Register the conf notification with the TxNotifier. 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)
dispatch, err := b.txNotifier.Register(ntfn)
if err != nil {
return nil, err
}

@ -11,12 +11,11 @@ import (
)
// UnsafeStart starts the notifier with a specified best height and optional
// best hash. Its bestBlock and txConfNotifier are initialized with
// bestHeight and optionally bestHash. The parameter generateBlocks is
// necessary for the bitcoind notifier to ensure we drain all notifications up
// to syncHeight, since if they are generated ahead of UnsafeStart the chainConn
// may start up with an outdated best block and miss sending ntfns. Used for
// testing.
// best hash. Its bestBlock and txNotifier are initialized with bestHeight and
// optionally bestHash. The parameter generateBlocks is necessary for the
// bitcoind notifier to ensure we drain all notifications up to syncHeight,
// since if they are generated ahead of UnsafeStart the chainConn may start up
// with an outdated best block and miss sending ntfns. Used for testing.
func (b *BtcdNotifier) UnsafeStart(bestHeight int32, bestHash *chainhash.Hash,
syncHeight int32, generateBlocks func() error) error {
@ -29,7 +28,7 @@ func (b *BtcdNotifier) UnsafeStart(bestHeight int32, bestHash *chainhash.Hash,
return err
}
b.txConfNotifier = chainntnfs.NewTxConfNotifier(
b.txNotifier = chainntnfs.NewTxNotifier(
uint32(bestHeight), reorgSafetyLimit, b.confirmHintCache,
)

@ -392,10 +392,10 @@ func GetClientMissedBlocks(chainConn ChainConn, clientBestBlock *BlockEpoch,
return missedBlocks, nil
}
// RewindChain handles internal state updates for the notifier's TxConfNotifier
// It has no effect if given a height greater than or equal to our current best
// RewindChain handles internal state updates for the notifier's TxNotifier It
// has no effect if given a height greater than or equal to our current best
// known height. It returns the new best block for the notifier.
func RewindChain(chainConn ChainConn, txConfNotifier *TxConfNotifier,
func RewindChain(chainConn ChainConn, txNotifier *TxNotifier,
currBestBlock BlockEpoch, targetHeight int32) (BlockEpoch, error) {
newBestBlock := BlockEpoch{
@ -414,7 +414,7 @@ func RewindChain(chainConn ChainConn, txConfNotifier *TxConfNotifier,
Log.Infof("Block disconnected from main chain: "+
"height=%v, sha=%v", height, newBestBlock.Hash)
err = txConfNotifier.DisconnectTip(uint32(height))
err = txNotifier.DisconnectTip(uint32(height))
if err != nil {
return newBestBlock, fmt.Errorf("unable to "+
" disconnect tip for height=%d: %v",
@ -436,7 +436,7 @@ func RewindChain(chainConn ChainConn, txConfNotifier *TxConfNotifier,
// returned in case a chain rewind occurs and partially completes before
// erroring. In the case where there is no rewind, the notifier's
// current best block is returned.
func HandleMissedBlocks(chainConn ChainConn, txConfNotifier *TxConfNotifier,
func HandleMissedBlocks(chainConn ChainConn, txNotifier *TxNotifier,
currBestBlock BlockEpoch, newHeight int32,
backendStoresReorgs bool) (BlockEpoch, []BlockEpoch, error) {
@ -462,7 +462,7 @@ func HandleMissedBlocks(chainConn ChainConn, txConfNotifier *TxConfNotifier,
"common ancestor: %v", err)
}
currBestBlock, err = RewindChain(chainConn, txConfNotifier,
currBestBlock, err = RewindChain(chainConn, txNotifier,
currBestBlock, startingHeight)
if err != nil {
return currBestBlock, nil, fmt.Errorf("unable to "+

@ -70,7 +70,7 @@ type NeutrinoNotifier struct {
spendNotifications map[wire.OutPoint]map[uint64]*spendNotification
txConfNotifier *chainntnfs.TxConfNotifier
txNotifier *chainntnfs.TxNotifier
blockEpochClients map[uint64]*blockEpochRegistration
@ -162,7 +162,7 @@ func (n *NeutrinoNotifier) Start() error {
neutrino.WatchInputs(zeroInput),
}
n.txConfNotifier = chainntnfs.NewTxConfNotifier(
n.txNotifier = chainntnfs.NewTxNotifier(
n.bestHeight, reorgSafetyLimit, n.confirmHintCache,
)
@ -206,7 +206,7 @@ func (n *NeutrinoNotifier) Stop() error {
close(epochClient.epochChan)
}
n.txConfNotifier.TearDown()
n.txNotifier.TearDown()
return nil
}
@ -350,7 +350,7 @@ out:
// begin safely updating the height hint
// cache at tip, since any pending
// rescans have now completed.
err = n.txConfNotifier.UpdateConfDetails(
err = n.txNotifier.UpdateConfDetails(
*msg.TxID, confDetails,
)
if err != nil {
@ -426,7 +426,7 @@ out:
_, missedBlocks, err :=
chainntnfs.HandleMissedBlocks(
n.chainConn,
n.txConfNotifier,
n.txNotifier,
bestBlock,
int32(update.height),
false,
@ -482,7 +482,7 @@ out:
Hash: hash,
}
newBestBlock, err := chainntnfs.RewindChain(
n.chainConn, n.txConfNotifier, notifierBestBlock,
n.chainConn, n.txNotifier, notifierBestBlock,
int32(update.height-1),
)
if err != nil {
@ -593,7 +593,7 @@ func (n *NeutrinoNotifier) handleBlockConnected(newBlock *filteredBlock) error {
// First process the block for our internal state. A new block has
// been connected to the main chain. Send out any N confirmation
// notifications which may have been triggered by this new block.
err := n.txConfNotifier.ConnectTip(
err := n.txNotifier.ConnectTip(
&newBlock.hash, newBlock.height, newBlock.txns,
)
if err != nil {
@ -928,11 +928,11 @@ func (n *NeutrinoNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
chainntnfs.Log.Infof("New confirmation subscription: "+
"txid=%v, numconfs=%v", txid, numConfs)
// Register the conf notification with txconfnotifier. A non-nil value
// Register the conf notification with the TxNotifier. 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 := n.txConfNotifier.Register(ntfn)
dispatch, err := n.txNotifier.Register(ntfn)
if err != nil {
return nil, err
}

@ -13,13 +13,14 @@ import (
)
// UnsafeStart starts the notifier with a specified best height and optional
// best hash. Its bestHeight, txConfNotifier and neutrino node are initialized
// with bestHeight. The parameter generateBlocks is necessary for the
// bitcoind notifier to ensure we drain all notifications up to syncHeight,
// since if they are generated ahead of UnsafeStart the chainConn may start
// up with an outdated best block and miss sending ntfns. Used for testing.
func (n *NeutrinoNotifier) UnsafeStart(bestHeight int32, bestHash *chainhash.Hash,
syncHeight int32, generateBlocks func() error) error {
// best hash. Its bestHeight, txNotifier and neutrino node are initialized with
// bestHeight. The parameter generateBlocks is necessary for the bitcoind
// notifier to ensure we drain all notifications up to syncHeight, since if they
// are generated ahead of UnsafeStart the chainConn may start up with an
// outdated best block and miss sending ntfns. Used for testing.
func (n *NeutrinoNotifier) UnsafeStart(bestHeight int32,
bestHash *chainhash.Hash, syncHeight int32,
generateBlocks func() error) error {
// We'll obtain the latest block height of the p2p node. We'll
// start the auto-rescan from this point. Once a caller actually wishes
@ -47,7 +48,7 @@ func (n *NeutrinoNotifier) UnsafeStart(bestHeight int32, bestHash *chainhash.Has
neutrino.WatchInputs(zeroInput),
}
n.txConfNotifier = chainntnfs.NewTxConfNotifier(
n.txNotifier = chainntnfs.NewTxNotifier(
uint32(bestHeight), reorgSafetyLimit, n.confirmHintCache,
)

@ -10,9 +10,9 @@ import (
)
var (
// ErrTxConfNotifierExiting is an error returned when attempting to
// interact with the TxConfNotifier but it been shut down.
ErrTxConfNotifierExiting = errors.New("TxConfNotifier is exiting")
// ErrTxNotifierExiting is an error returned when attempting to interact
// with the TxNotifier but it been shut down.
ErrTxNotifierExiting = errors.New("TxNotifier is exiting")
// ErrTxMaxConfs signals that the user requested a number of
// confirmations beyond the reorg safety limit.
@ -87,12 +87,12 @@ func NewConfirmationEvent(numConfs uint32) *ConfirmationEvent {
}
}
// TxConfNotifier is used to register transaction confirmation notifications and
// TxNotifier is used to register transaction confirmation notifications and
// dispatch them as the transactions confirm. A client can request to be
// notified when a particular transaction has sufficient on-chain confirmations
// (or be notified immediately if the tx already does), and the TxConfNotifier
// (or be notified immediately if the tx already does), and the TxNotifier
// will watch changes to the blockchain in order to satisfy these requests.
type TxConfNotifier struct {
type TxNotifier struct {
// currentHeight is the height of the tracked blockchain. It is used to
// determine the number of confirmations a tx has and ensure blocks are
// connected and disconnected in order.
@ -176,12 +176,12 @@ func newConfNtfnSet() *confNtfnSet {
}
}
// NewTxConfNotifier creates a TxConfNotifier. The current height of the
// blockchain is accepted as a parameter.
func NewTxConfNotifier(startHeight uint32, reorgSafetyLimit uint32,
hintCache ConfirmHintCache) *TxConfNotifier {
// NewTxNotifier creates a TxNotifier. The current height of the blockchain is
// accepted as a parameter.
func NewTxNotifier(startHeight uint32, reorgSafetyLimit uint32,
hintCache ConfirmHintCache) *TxNotifier {
return &TxConfNotifier{
return &TxNotifier{
currentHeight: startHeight,
reorgSafetyLimit: reorgSafetyLimit,
confNotifications: make(map[chainhash.Hash]*confNtfnSet),
@ -202,18 +202,18 @@ func NewTxConfNotifier(startHeight uint32, reorgSafetyLimit uint32,
// the confirmation details must be provided with the UpdateConfDetails method,
// otherwise we will wait for the transaction to confirm even though it already
// has.
func (tcn *TxConfNotifier) Register(
func (n *TxNotifier) Register(
ntfn *ConfNtfn) (*HistoricalConfDispatch, error) {
select {
case <-tcn.quit:
return nil, ErrTxConfNotifierExiting
case <-n.quit:
return nil, ErrTxNotifierExiting
default:
}
// Enforce that we will not dispatch confirmations beyond the reorg
// safety limit.
if ntfn.NumConfirmations > tcn.reorgSafetyLimit {
if ntfn.NumConfirmations > n.reorgSafetyLimit {
return nil, ErrTxMaxConfs
}
@ -222,7 +222,7 @@ func (tcn *TxConfNotifier) Register(
//
// TODO(conner): verify that all submitted height hints are identical.
startHeight := ntfn.HeightHint
hint, err := tcn.hintCache.QueryConfirmHint(*ntfn.TxID)
hint, err := n.hintCache.QueryConfirmHint(*ntfn.TxID)
if err == nil {
if hint > startHeight {
Log.Debugf("Using height hint %d retrieved "+
@ -234,15 +234,15 @@ func (tcn *TxConfNotifier) Register(
*ntfn.TxID, err)
}
tcn.Lock()
defer tcn.Unlock()
n.Lock()
defer n.Unlock()
confSet, ok := tcn.confNotifications[*ntfn.TxID]
confSet, ok := n.confNotifications[*ntfn.TxID]
if !ok {
// If this is the first registration for this txid, construct a
// confSet to coalesce all notifications for the same txid.
confSet = newConfNtfnSet()
tcn.confNotifications[*ntfn.TxID] = confSet
n.confNotifications[*ntfn.TxID] = confSet
}
confSet.ntfns[ntfn.ConfID] = ntfn
@ -257,7 +257,7 @@ func (tcn *TxConfNotifier) Register(
// client.
Log.Debugf("Attempting to dispatch conf for txid=%v "+
"on registration since rescan has finished", ntfn.TxID)
return nil, tcn.dispatchConfDetails(ntfn, confSet.details)
return nil, n.dispatchConfDetails(ntfn, confSet.details)
// A rescan is already in progress, return here to prevent dispatching
// another. When the scan returns, this notifications details will be
@ -274,7 +274,7 @@ func (tcn *TxConfNotifier) Register(
// If the provided or cached height hint indicates that the transaction
// is to be confirmed at a height greater than the conf notifier's
// current height, we'll refrain from spawning a historical dispatch.
if startHeight > tcn.currentHeight {
if startHeight > n.currentHeight {
Log.Debugf("Height hint is above current height, not dispatching "+
"historical rescan for txid=%v ", ntfn.TxID)
// Set the rescan status to complete, which will allow the conf
@ -294,7 +294,7 @@ func (tcn *TxConfNotifier) Register(
TxID: ntfn.TxID,
PkScript: ntfn.PkScript,
StartHeight: startHeight,
EndHeight: tcn.currentHeight,
EndHeight: n.currentHeight,
}
// Set this confSet's status to pending, ensuring subsequent
@ -310,23 +310,23 @@ func (tcn *TxConfNotifier) Register(
//
// NOTE: The notification should be registered first to ensure notifications are
// dispatched correctly.
func (tcn *TxConfNotifier) UpdateConfDetails(txid chainhash.Hash,
func (n *TxNotifier) UpdateConfDetails(txid chainhash.Hash,
details *TxConfirmation) error {
select {
case <-tcn.quit:
return ErrTxConfNotifierExiting
case <-n.quit:
return ErrTxNotifierExiting
default:
}
// Ensure we hold the lock throughout handling the notification to
// prevent the notifier from advancing its height underneath us.
tcn.Lock()
defer tcn.Unlock()
n.Lock()
defer n.Unlock()
// First, we'll determine whether we have an active notification for
// this transaction with the given ID.
confSet, ok := tcn.confNotifications[txid]
confSet, ok := n.confNotifications[txid]
if !ok {
return fmt.Errorf("no notification found with TxID %v", txid)
}
@ -354,7 +354,7 @@ func (tcn *TxConfNotifier) UpdateConfDetails(txid chainhash.Hash,
return nil
}
if details.BlockHeight > tcn.currentHeight {
if details.BlockHeight > n.currentHeight {
Log.Debugf("Conf details for txid=%v found above current "+
"height, waiting to dispatch at tip", txid)
return nil
@ -362,7 +362,7 @@ func (tcn *TxConfNotifier) UpdateConfDetails(txid chainhash.Hash,
Log.Debugf("Updating conf details for txid=%v details", txid)
err := tcn.hintCache.CommitConfirmHint(details.BlockHeight, txid)
err := n.hintCache.CommitConfirmHint(details.BlockHeight, txid)
if err != nil {
// The error is not fatal, so we should not return an error to
// the caller.
@ -374,7 +374,7 @@ func (tcn *TxConfNotifier) UpdateConfDetails(txid chainhash.Hash,
// notifications that have not yet been delivered.
confSet.details = details
for _, ntfn := range confSet.ntfns {
err = tcn.dispatchConfDetails(ntfn, details)
err = n.dispatchConfDetails(ntfn, details)
if err != nil {
return err
}
@ -386,7 +386,7 @@ func (tcn *TxConfNotifier) UpdateConfDetails(txid chainhash.Hash,
// dispatchConfDetails attempts to cache and dispatch details to a particular
// client if the transaction has sufficiently confirmed. If the provided details
// are nil, this method will be a no-op.
func (tcn *TxConfNotifier) dispatchConfDetails(
func (n *TxNotifier) dispatchConfDetails(
ntfn *ConfNtfn, details *TxConfirmation) error {
// If no details are provided, return early as we can't dispatch.
@ -401,7 +401,7 @@ func (tcn *TxConfNotifier) dispatchConfDetails(
// confirmations. If it has, we'll dispatch a confirmation
// notification to the caller.
confHeight := details.BlockHeight + ntfn.NumConfirmations - 1
if confHeight <= tcn.currentHeight {
if confHeight <= n.currentHeight {
Log.Infof("Dispatching %v conf notification for %v",
ntfn.NumConfirmations, ntfn.TxID)
@ -410,15 +410,15 @@ func (tcn *TxConfNotifier) dispatchConfDetails(
// confirmed.
select {
case ntfn.Event.Updates <- 0:
case <-tcn.quit:
return ErrTxConfNotifierExiting
case <-n.quit:
return ErrTxNotifierExiting
}
select {
case ntfn.Event.Confirmed <- details:
ntfn.dispatched = true
case <-tcn.quit:
return ErrTxConfNotifierExiting
case <-n.quit:
return ErrTxNotifierExiting
}
} else {
Log.Debugf("Queueing %v conf notification for %v at tip ",
@ -427,33 +427,33 @@ func (tcn *TxConfNotifier) dispatchConfDetails(
// Otherwise, we'll keep track of the notification
// request by the height at which we should dispatch the
// confirmation notification.
ntfnSet, exists := tcn.ntfnsByConfirmHeight[confHeight]
ntfnSet, exists := n.ntfnsByConfirmHeight[confHeight]
if !exists {
ntfnSet = make(map[*ConfNtfn]struct{})
tcn.ntfnsByConfirmHeight[confHeight] = ntfnSet
n.ntfnsByConfirmHeight[confHeight] = ntfnSet
}
ntfnSet[ntfn] = struct{}{}
// We'll also send an update to the client of how many
// confirmations are left for the transaction to be
// confirmed.
numConfsLeft := confHeight - tcn.currentHeight
numConfsLeft := confHeight - n.currentHeight
select {
case ntfn.Event.Updates <- numConfsLeft:
case <-tcn.quit:
return ErrTxConfNotifierExiting
case <-n.quit:
return ErrTxNotifierExiting
}
}
// As a final check, we'll also watch the transaction if it's
// still possible for it to get reorged out of the chain.
blockHeight := details.BlockHeight
reorgSafeHeight := blockHeight + tcn.reorgSafetyLimit
if reorgSafeHeight > tcn.currentHeight {
txSet, exists := tcn.txsByInitialHeight[blockHeight]
reorgSafeHeight := blockHeight + n.reorgSafetyLimit
if reorgSafeHeight > n.currentHeight {
txSet, exists := n.txsByInitialHeight[blockHeight]
if !exists {
txSet = make(map[chainhash.Hash]struct{})
tcn.txsByInitialHeight[blockHeight] = txSet
n.txsByInitialHeight[blockHeight] = txSet
}
txSet[*ntfn.TxID] = struct{}{}
}
@ -466,25 +466,25 @@ func (tcn *TxConfNotifier) dispatchConfDetails(
// Also, if any watched transactions now have the required number of
// confirmations as a result of this block being connected, this dispatches
// notifications.
func (tcn *TxConfNotifier) ConnectTip(blockHash *chainhash.Hash,
func (n *TxNotifier) ConnectTip(blockHash *chainhash.Hash,
blockHeight uint32, txns []*btcutil.Tx) error {
select {
case <-tcn.quit:
return ErrTxConfNotifierExiting
case <-n.quit:
return ErrTxNotifierExiting
default:
}
tcn.Lock()
defer tcn.Unlock()
n.Lock()
defer n.Unlock()
if blockHeight != tcn.currentHeight+1 {
if blockHeight != n.currentHeight+1 {
return fmt.Errorf("Received blocks out of order: "+
"current height=%d, new height=%d",
tcn.currentHeight, blockHeight)
n.currentHeight, blockHeight)
}
tcn.currentHeight++
tcn.reorgDepth = 0
n.currentHeight++
n.reorgDepth = 0
// Record any newly confirmed transactions by their confirmed height so
// that notifications get dispatched when the transactions reach their
@ -496,7 +496,7 @@ func (tcn *TxConfNotifier) ConnectTip(blockHash *chainhash.Hash,
// Check if we have any pending notifications for this txid. If
// none are found, we can proceed to the next transaction.
confSet, ok := tcn.confNotifications[*txHash]
confSet, ok := n.confNotifications[*txHash]
if !ok {
continue
}
@ -529,20 +529,20 @@ func (tcn *TxConfNotifier) ConnectTip(blockHash *chainhash.Hash,
// confirmations so that we can notify them when
// expected.
confHeight := blockHeight + ntfn.NumConfirmations - 1
ntfnSet, exists := tcn.ntfnsByConfirmHeight[confHeight]
ntfnSet, exists := n.ntfnsByConfirmHeight[confHeight]
if !exists {
ntfnSet = make(map[*ConfNtfn]struct{})
tcn.ntfnsByConfirmHeight[confHeight] = ntfnSet
n.ntfnsByConfirmHeight[confHeight] = ntfnSet
}
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]
txSet, exists := n.txsByInitialHeight[blockHeight]
if !exists {
txSet = make(map[chainhash.Hash]struct{})
tcn.txsByInitialHeight[blockHeight] = txSet
n.txsByInitialHeight[blockHeight] = txSet
}
txSet[*txHash] = struct{}{}
}
@ -556,11 +556,11 @@ func (tcn *TxConfNotifier) ConnectTip(blockHash *chainhash.Hash,
// this map doesn't tell us whether the transaction has confirmed or
// not, we'll need to look at txsByInitialHeight to determine so.
var txsToUpdateHints []chainhash.Hash
for confirmedTx := range tcn.txsByInitialHeight[tcn.currentHeight] {
for confirmedTx := range n.txsByInitialHeight[n.currentHeight] {
txsToUpdateHints = append(txsToUpdateHints, confirmedTx)
}
out:
for maybeUnconfirmedTx, confSet := range tcn.confNotifications {
for maybeUnconfirmedTx, confSet := range n.confNotifications {
// We shouldn't update the confirm hints if we still have a
// pending rescan in progress. We'll skip writing any for
// notification sets that haven't reached rescanComplete.
@ -568,7 +568,7 @@ out:
continue
}
for height, confirmedTxs := range tcn.txsByInitialHeight {
for height, confirmedTxs := range n.txsByInitialHeight {
// Skip the transactions that confirmed at the new block
// height as those have already been added.
if height == blockHeight {
@ -585,14 +585,14 @@ out:
}
if len(txsToUpdateHints) > 0 {
err := tcn.hintCache.CommitConfirmHint(
tcn.currentHeight, txsToUpdateHints...,
err := n.hintCache.CommitConfirmHint(
n.currentHeight, txsToUpdateHints...,
)
if err != nil {
// The error is not fatal, so we should not return an
// error to the caller.
Log.Errorf("Unable to update confirm hint to %d for "+
"%v: %v", tcn.currentHeight, txsToUpdateHints,
"%v: %v", n.currentHeight, txsToUpdateHints,
err)
}
}
@ -600,9 +600,9 @@ out:
// Next, we'll dispatch an update to all of the notification clients for
// our watched transactions with the number of confirmations left at
// this new height.
for _, txHashes := range tcn.txsByInitialHeight {
for _, txHashes := range n.txsByInitialHeight {
for txHash := range txHashes {
confSet := tcn.confNotifications[txHash]
confSet := n.confNotifications[txHash]
for _, ntfn := range confSet.ntfns {
txConfHeight := confSet.details.BlockHeight +
ntfn.NumConfirmations - 1
@ -619,8 +619,8 @@ out:
select {
case ntfn.Event.Updates <- numConfsLeft:
case <-tcn.quit:
return ErrTxConfNotifierExiting
case <-n.quit:
return ErrTxNotifierExiting
}
}
}
@ -628,8 +628,8 @@ out:
// Then, we'll dispatch notifications for all the transactions that have
// become confirmed at this new block height.
for ntfn := range tcn.ntfnsByConfirmHeight[blockHeight] {
confSet := tcn.confNotifications[*ntfn.TxID]
for ntfn := range n.ntfnsByConfirmHeight[blockHeight] {
confSet := n.confNotifications[*ntfn.TxID]
Log.Infof("Dispatching %v conf notification for %v",
ntfn.NumConfirmations, ntfn.TxID)
@ -637,21 +637,21 @@ out:
select {
case ntfn.Event.Confirmed <- confSet.details:
ntfn.dispatched = true
case <-tcn.quit:
return ErrTxConfNotifierExiting
case <-n.quit:
return ErrTxNotifierExiting
}
}
delete(tcn.ntfnsByConfirmHeight, tcn.currentHeight)
delete(n.ntfnsByConfirmHeight, n.currentHeight)
// Clear entries from confNotifications and confTxsByInitialHeight. We
// assume that reorgs deeper than the reorg safety limit do not happen,
// so we can clear out entries for the block that is now mature.
if tcn.currentHeight >= tcn.reorgSafetyLimit {
matureBlockHeight := tcn.currentHeight - tcn.reorgSafetyLimit
for txHash := range tcn.txsByInitialHeight[matureBlockHeight] {
delete(tcn.confNotifications, txHash)
if n.currentHeight >= n.reorgSafetyLimit {
matureBlockHeight := n.currentHeight - n.reorgSafetyLimit
for txHash := range n.txsByInitialHeight[matureBlockHeight] {
delete(n.confNotifications, txHash)
}
delete(tcn.txsByInitialHeight, matureBlockHeight)
delete(n.txsByInitialHeight, matureBlockHeight)
}
return nil
@ -661,47 +661,47 @@ out:
// a chain reorganization. If any watched transactions were included in this
// block, internal structures are updated to ensure a confirmation notification
// is not sent unless the transaction is included in the new chain.
func (tcn *TxConfNotifier) DisconnectTip(blockHeight uint32) error {
func (n *TxNotifier) DisconnectTip(blockHeight uint32) error {
select {
case <-tcn.quit:
return ErrTxConfNotifierExiting
case <-n.quit:
return ErrTxNotifierExiting
default:
}
tcn.Lock()
defer tcn.Unlock()
n.Lock()
defer n.Unlock()
if blockHeight != tcn.currentHeight {
if blockHeight != n.currentHeight {
return fmt.Errorf("Received blocks out of order: "+
"current height=%d, disconnected height=%d",
tcn.currentHeight, blockHeight)
n.currentHeight, blockHeight)
}
tcn.currentHeight--
tcn.reorgDepth++
n.currentHeight--
n.reorgDepth++
// Rewind the height hint for all watched transactions.
var txs []chainhash.Hash
for tx := range tcn.confNotifications {
for tx := range n.confNotifications {
txs = append(txs, tx)
}
err := tcn.hintCache.CommitConfirmHint(tcn.currentHeight, txs...)
err := n.hintCache.CommitConfirmHint(n.currentHeight, txs...)
if err != nil {
Log.Errorf("Unable to update confirm hint to %d for %v: %v",
tcn.currentHeight, txs, err)
n.currentHeight, txs, err)
return err
}
// We'll go through all of our watched transactions and attempt to drain
// their notification channels to ensure sending notifications to the
// clients is always non-blocking.
for initialHeight, txHashes := range tcn.txsByInitialHeight {
for initialHeight, txHashes := range n.txsByInitialHeight {
for txHash := range txHashes {
// If the transaction has been reorged out of the chain,
// we'll make sure to remove the cached confirmation
// details to prevent notifying clients with old
// information.
confSet := tcn.confNotifications[txHash]
confSet := n.confNotifications[txHash]
if initialHeight == blockHeight {
confSet.details = nil
}
@ -712,8 +712,8 @@ func (tcn *TxConfNotifier) DisconnectTip(blockHeight uint32) error {
// Updates channel are always non-blocking.
select {
case <-ntfn.Event.Updates:
case <-tcn.quit:
return ErrTxConfNotifierExiting
case <-n.quit:
return ErrTxNotifierExiting
default:
}
@ -722,7 +722,7 @@ func (tcn *TxConfNotifier) DisconnectTip(blockHeight uint32) error {
// disconnected. If it was, we'll need to
// dispatch a reorg notification to the client.
if initialHeight == blockHeight {
err := tcn.dispatchConfReorg(
err := n.dispatchConfReorg(
ntfn, blockHeight,
)
if err != nil {
@ -735,7 +735,7 @@ func (tcn *TxConfNotifier) DisconnectTip(blockHeight uint32) error {
// Finally, we can remove the transactions we're currently watching that
// were included in this block height.
delete(tcn.txsByInitialHeight, blockHeight)
delete(n.txsByInitialHeight, blockHeight)
return nil
}
@ -744,16 +744,16 @@ func (tcn *TxConfNotifier) DisconnectTip(blockHeight uint32) error {
// confirmation notification was already delivered.
//
// NOTE: This must be called with the TxNotifier's lock held.
func (tcn *TxConfNotifier) dispatchConfReorg(
ntfn *ConfNtfn, heightDisconnected uint32) error {
func (n *TxNotifier) dispatchConfReorg(ntfn *ConfNtfn,
heightDisconnected uint32) error {
// If the transaction's confirmation notification has yet to be
// dispatched, we'll need to clear its entry within the
// ntfnsByConfirmHeight index to prevent from notifiying the client once
// ntfnsByConfirmHeight index to prevent from notifying the client once
// the notifier reaches the confirmation height.
if !ntfn.dispatched {
confHeight := heightDisconnected + ntfn.NumConfirmations - 1
ntfnSet, exists := tcn.ntfnsByConfirmHeight[confHeight]
ntfnSet, exists := n.ntfnsByConfirmHeight[confHeight]
if exists {
delete(ntfnSet, ntfn)
}
@ -765,8 +765,8 @@ func (tcn *TxConfNotifier) dispatchConfReorg(
// ensure sends to the Confirmed channel are always non-blocking.
select {
case <-ntfn.Event.Confirmed:
case <-tcn.quit:
return ErrTxConfNotifierExiting
case <-n.quit:
return ErrTxNotifierExiting
default:
}
@ -775,24 +775,24 @@ func (tcn *TxConfNotifier) dispatchConfReorg(
// Send a negative confirmation notification to the client indicating
// how many blocks have been disconnected successively.
select {
case ntfn.Event.NegativeConf <- int32(tcn.reorgDepth):
case <-tcn.quit:
return ErrTxConfNotifierExiting
case ntfn.Event.NegativeConf <- int32(n.reorgDepth):
case <-n.quit:
return ErrTxNotifierExiting
}
return nil
}
// TearDown is to be called when the owner of the TxConfNotifier is exiting.
// This closes the event channels of all registered notifications that have
// not been dispatched yet.
func (tcn *TxConfNotifier) TearDown() {
tcn.Lock()
defer tcn.Unlock()
// TearDown is to be called when the owner of the TxNotifier is exiting. This
// closes the event channels of all registered notifications that have not been
// dispatched yet.
func (n *TxNotifier) TearDown() {
n.Lock()
defer n.Unlock()
close(tcn.quit)
close(n.quit)
for _, confSet := range tcn.confNotifications {
for _, confSet := range n.confNotifications {
for _, ntfn := range confSet.ntfns {
if ntfn.dispatched {
continue

@ -96,8 +96,8 @@ func newMockHintCache() *mockHintCache {
}
}
// TestTxConfFutureDispatch tests that the TxConfNotifier dispatches
// registered notifications when the transaction confirms after registration.
// TestTxConfFutureDispatch tests that the TxNotifier dispatches registered
// notifications when the transaction confirms after registration.
func TestTxConfFutureDispatch(t *testing.T) {
t.Parallel()
@ -113,10 +113,10 @@ func TestTxConfFutureDispatch(t *testing.T) {
)
hintCache := newMockHintCache()
tcn := chainntnfs.NewTxConfNotifier(10, 100, hintCache)
n := chainntnfs.NewTxNotifier(10, 100, hintCache)
// Create the test transactions and register them with the
// TxConfNotifier before including them in a block to receive future
// Create the test transactions and register them with the TxNotifier
// before including them in a block to receive future
// notifications.
tx1Hash := tx1.TxHash()
ntfn1 := chainntnfs.ConfNtfn{
@ -124,7 +124,7 @@ func TestTxConfFutureDispatch(t *testing.T) {
NumConfirmations: tx1NumConfs,
Event: chainntnfs.NewConfirmationEvent(tx1NumConfs),
}
if _, err := tcn.Register(&ntfn1); err != nil {
if _, err := n.Register(&ntfn1); err != nil {
t.Fatalf("unable to register ntfn: %v", err)
}
@ -134,7 +134,7 @@ func TestTxConfFutureDispatch(t *testing.T) {
NumConfirmations: tx2NumConfs,
Event: chainntnfs.NewConfirmationEvent(tx2NumConfs),
}
if _, err := tcn.Register(&ntfn2); err != nil {
if _, err := n.Register(&ntfn2); err != nil {
t.Fatalf("unable to register ntfn: %v", err)
}
@ -156,13 +156,13 @@ func TestTxConfFutureDispatch(t *testing.T) {
default:
}
// Include the transactions in a block and add it to the TxConfNotifier.
// Include the transactions in a block and add it to the TxNotifier.
// This should confirm tx1, but not tx2.
block1 := btcutil.NewBlock(&wire.MsgBlock{
Transactions: []*wire.MsgTx{&tx1, &tx2, &tx3},
})
err := tcn.ConnectTip(
err := n.ConnectTip(
block1.Hash(), 11, block1.Transactions(),
)
if err != nil {
@ -219,13 +219,13 @@ func TestTxConfFutureDispatch(t *testing.T) {
default:
}
// Create a new block and add it to the TxConfNotifier at the next
// height. This should confirm tx2.
// Create a new block and add it to the TxNotifier at the next height.
// This should confirm tx2.
block2 := btcutil.NewBlock(&wire.MsgBlock{
Transactions: []*wire.MsgTx{&tx3},
})
err = tcn.ConnectTip(block2.Hash(), 12, block2.Transactions())
err = n.ConnectTip(block2.Hash(), 12, block2.Transactions())
if err != nil {
t.Fatalf("Failed to connect block: %v", err)
}
@ -269,9 +269,8 @@ func TestTxConfFutureDispatch(t *testing.T) {
}
}
// TestTxConfHistoricalDispatch tests that the TxConfNotifier dispatches
// registered notifications when the transaction is confirmed before
// registration.
// TestTxConfHistoricalDispatch tests that the TxNotifier dispatches registered
// notifications when the transaction is confirmed before registration.
func TestTxConfHistoricalDispatch(t *testing.T) {
t.Parallel()
@ -287,9 +286,9 @@ func TestTxConfHistoricalDispatch(t *testing.T) {
)
hintCache := newMockHintCache()
tcn := chainntnfs.NewTxConfNotifier(10, 100, hintCache)
n := chainntnfs.NewTxNotifier(10, 100, hintCache)
// Create the test transactions at a height before the TxConfNotifier's
// Create the test transactions at a height before the TxNotifier's
// starting height so that they are confirmed once registering them.
tx1Hash := tx1.TxHash()
ntfn1 := chainntnfs.ConfNtfn{
@ -298,7 +297,7 @@ func TestTxConfHistoricalDispatch(t *testing.T) {
NumConfirmations: tx1NumConfs,
Event: chainntnfs.NewConfirmationEvent(tx1NumConfs),
}
if _, err := tcn.Register(&ntfn1); err != nil {
if _, err := n.Register(&ntfn1); err != nil {
t.Fatalf("unable to register ntfn: %v", err)
}
@ -309,7 +308,7 @@ func TestTxConfHistoricalDispatch(t *testing.T) {
NumConfirmations: tx2NumConfs,
Event: chainntnfs.NewConfirmationEvent(tx2NumConfs),
}
if _, err := tcn.Register(&ntfn2); err != nil {
if _, err := n.Register(&ntfn2); err != nil {
t.Fatalf("unable to register ntfn: %v", err)
}
@ -320,7 +319,7 @@ func TestTxConfHistoricalDispatch(t *testing.T) {
BlockHeight: 9,
TxIndex: 1,
}
err := tcn.UpdateConfDetails(tx1Hash, &txConf1)
err := n.UpdateConfDetails(tx1Hash, &txConf1)
if err != nil {
t.Fatalf("unable to update conf details: %v", err)
}
@ -353,7 +352,7 @@ func TestTxConfHistoricalDispatch(t *testing.T) {
BlockHeight: 9,
TxIndex: 2,
}
err = tcn.UpdateConfDetails(tx2Hash, &txConf2)
err = n.UpdateConfDetails(tx2Hash, &txConf2)
if err != nil {
t.Fatalf("unable to update conf details: %v", err)
}
@ -375,13 +374,13 @@ func TestTxConfHistoricalDispatch(t *testing.T) {
default:
}
// Create a new block and add it to the TxConfNotifier at the next
// height. This should confirm tx2.
// Create a new block and add it to the TxNotifier at the next height.
// This should confirm tx2.
block := btcutil.NewBlock(&wire.MsgBlock{
Transactions: []*wire.MsgTx{&tx3},
})
err = tcn.ConnectTip(block.Hash(), 11, block.Transactions())
err = n.ConnectTip(block.Hash(), 11, block.Transactions())
if err != nil {
t.Fatalf("Failed to connect block: %v", err)
}
@ -420,7 +419,7 @@ func TestTxConfHistoricalDispatch(t *testing.T) {
}
}
// TestTxConfChainReorg tests that TxConfNotifier dispatches Confirmed and
// TestTxConfChainReorg tests that TxNotifier dispatches Confirmed and
// NegativeConf notifications appropriately when there is a chain
// reorganization.
func TestTxConfChainReorg(t *testing.T) {
@ -439,7 +438,7 @@ func TestTxConfChainReorg(t *testing.T) {
)
hintCache := newMockHintCache()
tcn := chainntnfs.NewTxConfNotifier(7, 100, hintCache)
n := chainntnfs.NewTxNotifier(7, 100, hintCache)
// Tx 1 will be confirmed in block 9 and requires 2 confs.
tx1Hash := tx1.TxHash()
@ -448,11 +447,11 @@ func TestTxConfChainReorg(t *testing.T) {
NumConfirmations: tx1NumConfs,
Event: chainntnfs.NewConfirmationEvent(tx1NumConfs),
}
if _, err := tcn.Register(&ntfn1); err != nil {
if _, err := n.Register(&ntfn1); err != nil {
t.Fatalf("unable to register ntfn: %v", err)
}
if err := tcn.UpdateConfDetails(*ntfn1.TxID, nil); err != nil {
if err := n.UpdateConfDetails(*ntfn1.TxID, nil); err != nil {
t.Fatalf("unable to deliver conf details: %v", err)
}
@ -463,11 +462,11 @@ func TestTxConfChainReorg(t *testing.T) {
NumConfirmations: tx2NumConfs,
Event: chainntnfs.NewConfirmationEvent(tx2NumConfs),
}
if _, err := tcn.Register(&ntfn2); err != nil {
if _, err := n.Register(&ntfn2); err != nil {
t.Fatalf("unable to register ntfn: %v", err)
}
if err := tcn.UpdateConfDetails(*ntfn2.TxID, nil); err != nil {
if err := n.UpdateConfDetails(*ntfn2.TxID, nil); err != nil {
t.Fatalf("unable to deliver conf details: %v", err)
}
@ -478,11 +477,11 @@ func TestTxConfChainReorg(t *testing.T) {
NumConfirmations: tx3NumConfs,
Event: chainntnfs.NewConfirmationEvent(tx3NumConfs),
}
if _, err := tcn.Register(&ntfn3); err != nil {
if _, err := n.Register(&ntfn3); err != nil {
t.Fatalf("unable to register ntfn: %v", err)
}
if err := tcn.UpdateConfDetails(*ntfn3.TxID, nil); err != nil {
if err := n.UpdateConfDetails(*ntfn3.TxID, nil); err != nil {
t.Fatalf("unable to deliver conf details: %v", err)
}
@ -490,11 +489,11 @@ func TestTxConfChainReorg(t *testing.T) {
block1 := btcutil.NewBlock(&wire.MsgBlock{
Transactions: []*wire.MsgTx{&tx1},
})
err := tcn.ConnectTip(nil, 8, block1.Transactions())
err := n.ConnectTip(nil, 8, block1.Transactions())
if err != nil {
t.Fatalf("Failed to connect block: %v", err)
}
err = tcn.ConnectTip(nil, 9, nil)
err = n.ConnectTip(nil, 9, nil)
if err != nil {
t.Fatalf("Failed to connect block: %v", err)
}
@ -502,7 +501,7 @@ func TestTxConfChainReorg(t *testing.T) {
block2 := btcutil.NewBlock(&wire.MsgBlock{
Transactions: []*wire.MsgTx{&tx2, &tx3},
})
err = tcn.ConnectTip(nil, 10, block2.Transactions())
err = n.ConnectTip(nil, 10, block2.Transactions())
if err != nil {
t.Fatalf("Failed to connect block: %v", err)
}
@ -559,17 +558,17 @@ func TestTxConfChainReorg(t *testing.T) {
// The block that included tx2 and tx3 is disconnected and two next
// blocks without them are connected.
err = tcn.DisconnectTip(10)
err = n.DisconnectTip(10)
if err != nil {
t.Fatalf("Failed to connect block: %v", err)
}
err = tcn.ConnectTip(nil, 10, nil)
err = n.ConnectTip(nil, 10, nil)
if err != nil {
t.Fatalf("Failed to connect block: %v", err)
}
err = tcn.ConnectTip(nil, 11, nil)
err = n.ConnectTip(nil, 11, nil)
if err != nil {
t.Fatalf("Failed to connect block: %v", err)
}
@ -617,12 +616,12 @@ func TestTxConfChainReorg(t *testing.T) {
})
block4 := btcutil.NewBlock(&wire.MsgBlock{})
err = tcn.ConnectTip(block3.Hash(), 12, block3.Transactions())
err = n.ConnectTip(block3.Hash(), 12, block3.Transactions())
if err != nil {
t.Fatalf("Failed to connect block: %v", err)
}
err = tcn.ConnectTip(block4.Hash(), 13, block4.Transactions())
err = n.ConnectTip(block4.Hash(), 13, block4.Transactions())
if err != nil {
t.Fatalf("Failed to connect block: %v", err)
}
@ -701,9 +700,9 @@ func TestTxConfHeightHintCache(t *testing.T) {
tx2Height = 203
)
// Initialize our TxConfNotifier instance backed by a height hint cache.
// Initialize our TxNotifier instance backed by a height hint cache.
hintCache := newMockHintCache()
tcn := chainntnfs.NewTxConfNotifier(
n := chainntnfs.NewTxNotifier(
startingHeight, 100, hintCache,
)
@ -724,10 +723,10 @@ func TestTxConfHeightHintCache(t *testing.T) {
Event: chainntnfs.NewConfirmationEvent(2),
}
if _, err := tcn.Register(ntfn1); err != nil {
if _, err := n.Register(ntfn1); err != nil {
t.Fatalf("unable to register tx1: %v", err)
}
if _, err := tcn.Register(ntfn2); err != nil {
if _, err := n.Register(ntfn2); err != nil {
t.Fatalf("unable to register tx2: %v", err)
}
@ -754,7 +753,7 @@ func TestTxConfHeightHintCache(t *testing.T) {
Transactions: []*wire.MsgTx{&txDummy},
})
err = tcn.ConnectTip(
err = n.ConnectTip(
block1.Hash(), txDummyHeight, block1.Transactions(),
)
if err != nil {
@ -781,10 +780,10 @@ func TestTxConfHeightHintCache(t *testing.T) {
// Now, update the conf details reporting that the neither txn was found
// in the historical dispatch.
if err := tcn.UpdateConfDetails(tx1Hash, nil); err != nil {
if err := n.UpdateConfDetails(tx1Hash, nil); err != nil {
t.Fatalf("unable to update conf details: %v", err)
}
if err := tcn.UpdateConfDetails(tx2Hash, nil); err != nil {
if err := n.UpdateConfDetails(tx2Hash, nil); err != nil {
t.Fatalf("unable to update conf details: %v", err)
}
@ -794,7 +793,7 @@ func TestTxConfHeightHintCache(t *testing.T) {
Transactions: []*wire.MsgTx{&tx1},
})
err = tcn.ConnectTip(
err = n.ConnectTip(
block2.Hash(), tx1Height, block2.Transactions(),
)
if err != nil {
@ -828,7 +827,7 @@ func TestTxConfHeightHintCache(t *testing.T) {
Transactions: []*wire.MsgTx{&tx2},
})
err = tcn.ConnectTip(
err = n.ConnectTip(
block3.Hash(), tx2Height, block3.Transactions(),
)
if err != nil {
@ -858,7 +857,7 @@ func TestTxConfHeightHintCache(t *testing.T) {
// Finally, we'll attempt do disconnect the last block in order to
// simulate a chain reorg.
if err := tcn.DisconnectTip(tx2Height); err != nil {
if err := n.DisconnectTip(tx2Height); err != nil {
t.Fatalf("Failed to disconnect block: %v", err)
}
@ -894,20 +893,20 @@ func TestTxConfTearDown(t *testing.T) {
)
hintCache := newMockHintCache()
tcn := chainntnfs.NewTxConfNotifier(10, 100, hintCache)
n := chainntnfs.NewTxNotifier(10, 100, hintCache)
// Create the test transactions and register them with the
// TxConfNotifier to receive notifications.
// Create the test transactions and register them with the TxNotifier to
// receive notifications.
tx1Hash := tx1.TxHash()
ntfn1 := chainntnfs.ConfNtfn{
TxID: &tx1Hash,
NumConfirmations: 1,
Event: chainntnfs.NewConfirmationEvent(1),
}
if _, err := tcn.Register(&ntfn1); err != nil {
if _, err := n.Register(&ntfn1); err != nil {
t.Fatalf("unable to register ntfn: %v", err)
}
if err := tcn.UpdateConfDetails(*ntfn1.TxID, nil); err != nil {
if err := n.UpdateConfDetails(*ntfn1.TxID, nil); err != nil {
t.Fatalf("unable to update conf details: %v", err)
}
@ -917,20 +916,20 @@ func TestTxConfTearDown(t *testing.T) {
NumConfirmations: 2,
Event: chainntnfs.NewConfirmationEvent(2),
}
if _, err := tcn.Register(&ntfn2); err != nil {
if _, err := n.Register(&ntfn2); err != nil {
t.Fatalf("unable to register ntfn: %v", err)
}
if err := tcn.UpdateConfDetails(*ntfn2.TxID, nil); err != nil {
if err := n.UpdateConfDetails(*ntfn2.TxID, nil); err != nil {
t.Fatalf("unable to update conf details: %v", err)
}
// Include the transactions in a block and add it to the TxConfNotifier.
// Include the transactions in a block and add it to the TxNotifier.
// This should confirm tx1, but not tx2.
block := btcutil.NewBlock(&wire.MsgBlock{
Transactions: []*wire.MsgTx{&tx1, &tx2},
})
err := tcn.ConnectTip(block.Hash(), 11, block.Transactions())
err := n.ConnectTip(block.Hash(), 11, block.Transactions())
if err != nil {
t.Fatalf("Failed to connect block: %v", err)
}
@ -966,10 +965,10 @@ func TestTxConfTearDown(t *testing.T) {
// The notification channels should be closed for notifications that
// have not been dispatched yet, so we should not expect to receive any
// more updates.
tcn.TearDown()
n.TearDown()
// tx1 should not receive any more updates because it has already been
// confirmed and the TxConfNotifier has been shut down.
// confirmed and the TxNotifier has been shut down.
select {
case <-ntfn1.Event.Updates:
t.Fatal("Received unexpected confirmation update for tx1")
@ -979,7 +978,7 @@ func TestTxConfTearDown(t *testing.T) {
}
// tx2 should not receive any more updates after the notifications
// channels have been closed and the TxConfNotifier shut down.
// channels have been closed and the TxNotifier shut down.
select {
case _, more := <-ntfn2.Event.Updates:
if more {