chainntnfs: rename TxConfNotifier -> TxNotifier

This commit is contained in:
Wilmer Paulino committed 2018-10-30 17:59:31 -07:00
1 parent f8789e9db0
commit 82f6fd7a91
9 files changed
+231 -233

No files matched your search

+116 -116
View File
@@ -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