2017-11-13 22:55:22 +03:00
|
|
|
package chainntnfs
|
|
|
|
|
|
|
|
import (
|
2018-03-19 22:22:44 +03:00
|
|
|
"errors"
|
2017-11-13 22:55:22 +03:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConfNtfn represents a notifier client's request to receive a notification
|
|
|
|
// once the target transaction gets sufficient confirmations. The client is
|
|
|
|
// asynchronously notified via the ConfirmationEvent channels.
|
|
|
|
type ConfNtfn struct {
|
2017-12-18 05:40:05 +03:00
|
|
|
// TxID is the hash of the transaction for which confirmation notifications
|
2017-11-13 22:55:22 +03:00
|
|
|
// are requested.
|
|
|
|
TxID *chainhash.Hash
|
|
|
|
|
|
|
|
// NumConfirmations is the number of confirmations after which the
|
|
|
|
// notification is to be sent.
|
|
|
|
NumConfirmations uint32
|
|
|
|
|
|
|
|
// Event contains references to the channels that the notifications are to
|
|
|
|
// be sent over.
|
|
|
|
Event *ConfirmationEvent
|
|
|
|
|
|
|
|
// details describes the transaction's position is the blockchain. May be
|
|
|
|
// nil for unconfirmed transactions.
|
|
|
|
details *TxConfirmation
|
|
|
|
|
|
|
|
// dispatched is false if the confirmed notification has not been sent yet.
|
|
|
|
dispatched bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfirmationEvent constructs a new ConfirmationEvent with newly opened
|
|
|
|
// channels.
|
2018-03-19 21:48:44 +03:00
|
|
|
func NewConfirmationEvent(numConfs uint32) *ConfirmationEvent {
|
2017-11-13 22:55:22 +03:00
|
|
|
return &ConfirmationEvent{
|
|
|
|
Confirmed: make(chan *TxConfirmation, 1),
|
2018-03-19 21:48:44 +03:00
|
|
|
Updates: make(chan uint32, numConfs),
|
2017-11-13 22:55:22 +03:00
|
|
|
NegativeConf: make(chan int32, 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxConfNotifier 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
|
|
|
|
// will watch changes to the blockchain in order to satisfy these requests.
|
|
|
|
type TxConfNotifier 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.
|
|
|
|
currentHeight uint32
|
|
|
|
|
|
|
|
// reorgSafetyLimit is the chain depth beyond which it is assumed a block
|
|
|
|
// will not be reorganized out of the chain. This is used to determine when
|
|
|
|
// to prune old confirmation requests so that reorgs are handled correctly.
|
|
|
|
// The coinbase maturity period is a reasonable value to use.
|
|
|
|
reorgSafetyLimit uint32
|
|
|
|
|
2017-11-14 04:32:11 +03:00
|
|
|
// reorgDepth is the depth of a chain organization that this system is being
|
|
|
|
// informed of. This is incremented as long as a sequence of blocks are
|
|
|
|
// disconnected without being interrupted by a new block.
|
|
|
|
reorgDepth uint32
|
|
|
|
|
2017-11-13 22:55:22 +03:00
|
|
|
// confNotifications is an index of notification requests by transaction
|
|
|
|
// hash.
|
|
|
|
confNotifications map[chainhash.Hash][]*ConfNtfn
|
|
|
|
|
2018-03-19 22:04:19 +03:00
|
|
|
// txsByInitialHeight is an index of watched transactions by the height
|
2017-11-13 22:55:22 +03:00
|
|
|
// that they are included at in the blockchain. This is tracked so that
|
2018-03-19 22:04:19 +03:00
|
|
|
// incorrect notifications are not sent if a transaction is reorganized
|
|
|
|
// out of the chain and so that negative confirmations can be recognized.
|
|
|
|
txsByInitialHeight map[uint32]map[chainhash.Hash]struct{}
|
2017-11-13 22:55:22 +03:00
|
|
|
|
|
|
|
// ntfnsByConfirmHeight is an index of notification requests by the height
|
|
|
|
// at which the transaction will have sufficient confirmations.
|
|
|
|
ntfnsByConfirmHeight map[uint32]map[*ConfNtfn]struct{}
|
2017-12-05 00:30:33 +03:00
|
|
|
|
|
|
|
// quit is closed in order to signal that the notifier is gracefully
|
|
|
|
// exiting.
|
|
|
|
quit chan struct{}
|
2017-11-13 22:55:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTxConfNotifier creates a TxConfNotifier. The current height of the
|
|
|
|
// blockchain is accepted as a parameter.
|
|
|
|
func NewTxConfNotifier(startHeight uint32, reorgSafetyLimit uint32) *TxConfNotifier {
|
|
|
|
return &TxConfNotifier{
|
2018-03-19 22:04:19 +03:00
|
|
|
currentHeight: startHeight,
|
|
|
|
reorgSafetyLimit: reorgSafetyLimit,
|
|
|
|
confNotifications: make(map[chainhash.Hash][]*ConfNtfn),
|
|
|
|
txsByInitialHeight: make(map[uint32]map[chainhash.Hash]struct{}),
|
|
|
|
ntfnsByConfirmHeight: make(map[uint32]map[*ConfNtfn]struct{}),
|
|
|
|
quit: make(chan struct{}),
|
2017-11-13 22:55:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register handles a new notification request. The client will be notified when
|
|
|
|
// the transaction gets a sufficient number of confirmations on the blockchain.
|
|
|
|
// If the transaction has already been included in a block on the chain, the
|
|
|
|
// confirmation details must be given as the txConf argument, otherwise it
|
|
|
|
// should be nil. If the transaction already has the sufficient number of
|
|
|
|
// confirmations, this dispatches the notification immediately.
|
2017-12-05 00:30:33 +03:00
|
|
|
func (tcn *TxConfNotifier) Register(ntfn *ConfNtfn, txConf *TxConfirmation) error {
|
|
|
|
select {
|
|
|
|
case <-tcn.quit:
|
|
|
|
return fmt.Errorf("TxConfNotifier is exiting")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:55:22 +03:00
|
|
|
if txConf == nil || txConf.BlockHeight > tcn.currentHeight {
|
|
|
|
// Transaction is unconfirmed.
|
|
|
|
tcn.confNotifications[*ntfn.TxID] =
|
|
|
|
append(tcn.confNotifications[*ntfn.TxID], ntfn)
|
2017-12-05 00:30:33 +03:00
|
|
|
return nil
|
2017-11-13 22:55:22 +03:00
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// If the transaction already has the required confirmations, we'll
|
|
|
|
// dispatch the notification immediately.
|
2017-11-13 22:55:22 +03:00
|
|
|
confHeight := txConf.BlockHeight + ntfn.NumConfirmations - 1
|
|
|
|
if confHeight <= tcn.currentHeight {
|
|
|
|
Log.Infof("Dispatching %v conf notification for %v",
|
|
|
|
ntfn.NumConfirmations, ntfn.TxID)
|
2018-03-19 22:22:44 +03:00
|
|
|
|
|
|
|
// We'll send a 0 value to the Updates channel, indicating that
|
|
|
|
// the transaction has already been confirmed.
|
|
|
|
select {
|
|
|
|
case <-tcn.quit:
|
|
|
|
return fmt.Errorf("TxConfNotifier is exiting")
|
|
|
|
case ntfn.Event.Updates <- 0:
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:30:33 +03:00
|
|
|
select {
|
|
|
|
case <-tcn.quit:
|
|
|
|
return fmt.Errorf("TxConfNotifier is exiting")
|
|
|
|
case ntfn.Event.Confirmed <- txConf:
|
|
|
|
ntfn.dispatched = true
|
|
|
|
}
|
2017-11-13 22:55:22 +03:00
|
|
|
} else {
|
2018-03-19 22:22:44 +03:00
|
|
|
// Otherwise, we'll record the transaction along with the height
|
|
|
|
// at which we should notify the client.
|
2017-11-13 22:55:22 +03:00
|
|
|
ntfn.details = txConf
|
|
|
|
ntfnSet, exists := tcn.ntfnsByConfirmHeight[confHeight]
|
|
|
|
if !exists {
|
|
|
|
ntfnSet = make(map[*ConfNtfn]struct{})
|
|
|
|
tcn.ntfnsByConfirmHeight[confHeight] = ntfnSet
|
|
|
|
}
|
|
|
|
ntfnSet[ntfn] = struct{}{}
|
2018-03-19 22:22:44 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
select {
|
|
|
|
case ntfn.Event.Updates <- numConfsLeft:
|
|
|
|
case <-tcn.quit:
|
|
|
|
return errors.New("TxConfNotifier is exiting")
|
|
|
|
}
|
2017-11-13 22:55:22 +03:00
|
|
|
}
|
|
|
|
|
2018-03-19 22:04:19 +03:00
|
|
|
// As a final check, we'll also watch the transaction if it's still
|
|
|
|
// possible for it to get reorganized out of the chain.
|
2017-11-13 22:56:25 +03:00
|
|
|
if txConf.BlockHeight+tcn.reorgSafetyLimit > tcn.currentHeight {
|
2017-11-13 22:55:22 +03:00
|
|
|
tcn.confNotifications[*ntfn.TxID] =
|
|
|
|
append(tcn.confNotifications[*ntfn.TxID], ntfn)
|
2018-03-19 22:04:19 +03:00
|
|
|
|
|
|
|
txSet, exists := tcn.txsByInitialHeight[txConf.BlockHeight]
|
|
|
|
if !exists {
|
|
|
|
txSet = make(map[chainhash.Hash]struct{})
|
|
|
|
tcn.txsByInitialHeight[txConf.BlockHeight] = txSet
|
|
|
|
}
|
|
|
|
txSet[*ntfn.TxID] = struct{}{}
|
2017-11-13 22:55:22 +03:00
|
|
|
}
|
2017-12-05 00:30:33 +03:00
|
|
|
|
|
|
|
return nil
|
2017-11-13 22:55:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectTip handles a new block extending the current chain. This checks each
|
|
|
|
// transaction in the block to see if any watched transactions are included.
|
|
|
|
// 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,
|
|
|
|
blockHeight uint32, txns []*btcutil.Tx) error {
|
|
|
|
|
2017-12-05 00:30:33 +03:00
|
|
|
select {
|
|
|
|
case <-tcn.quit:
|
|
|
|
return fmt.Errorf("TxConfNotifier is exiting")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:55:22 +03:00
|
|
|
if blockHeight != tcn.currentHeight+1 {
|
|
|
|
return fmt.Errorf("Received blocks out of order: "+
|
|
|
|
"current height=%d, new height=%d",
|
|
|
|
tcn.currentHeight, blockHeight)
|
|
|
|
}
|
|
|
|
tcn.currentHeight++
|
2017-11-14 04:32:11 +03:00
|
|
|
tcn.reorgDepth = 0
|
2017-11-13 22:55:22 +03:00
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// Record any newly confirmed transactions by their confirmed height so
|
|
|
|
// that notifications get dispatched when the transactions reach their
|
|
|
|
// required number of confirmations. We'll also watch these transactions
|
|
|
|
// at the height they were included in the chain so reorgs can be
|
|
|
|
// handled correctly.
|
2017-11-13 22:55:22 +03:00
|
|
|
for _, tx := range txns {
|
|
|
|
txHash := tx.Hash()
|
|
|
|
for _, ntfn := range tcn.confNotifications[*txHash] {
|
|
|
|
ntfn.details = &TxConfirmation{
|
|
|
|
BlockHash: blockHash,
|
|
|
|
BlockHeight: blockHeight,
|
|
|
|
TxIndex: uint32(tx.Index()),
|
|
|
|
}
|
|
|
|
|
|
|
|
confHeight := blockHeight + ntfn.NumConfirmations - 1
|
|
|
|
ntfnSet, exists := tcn.ntfnsByConfirmHeight[confHeight]
|
|
|
|
if !exists {
|
|
|
|
ntfnSet = make(map[*ConfNtfn]struct{})
|
|
|
|
tcn.ntfnsByConfirmHeight[confHeight] = ntfnSet
|
|
|
|
}
|
|
|
|
ntfnSet[ntfn] = struct{}{}
|
|
|
|
|
2018-03-19 22:04:19 +03:00
|
|
|
txSet, exists := tcn.txsByInitialHeight[blockHeight]
|
|
|
|
if !exists {
|
|
|
|
txSet = make(map[chainhash.Hash]struct{})
|
|
|
|
tcn.txsByInitialHeight[blockHeight] = txSet
|
|
|
|
}
|
|
|
|
txSet[*txHash] = struct{}{}
|
2017-11-13 22:55:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// 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 txHash := range txHashes {
|
|
|
|
for _, ntfn := range tcn.confNotifications[txHash] {
|
|
|
|
// If the transaction still hasn't been included
|
|
|
|
// in a block, we'll skip it.
|
|
|
|
if ntfn.details == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
txConfHeight := ntfn.details.BlockHeight +
|
|
|
|
ntfn.NumConfirmations - 1
|
|
|
|
numConfsLeft := txConfHeight - blockHeight
|
|
|
|
|
|
|
|
// Since we don't clear notifications until
|
|
|
|
// transactions are no longer under the risk of
|
|
|
|
// being reorganized out of the chain, we'll
|
|
|
|
// skip sending updates for transactions that
|
|
|
|
// have already been confirmed.
|
|
|
|
if int32(numConfsLeft) < 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ntfn.Event.Updates <- numConfsLeft:
|
|
|
|
case <-tcn.quit:
|
|
|
|
return errors.New("TxConfNotifier is exiting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, we'll dispatch notifications for all the transactions that have
|
|
|
|
// become confirmed at this new block height.
|
2017-11-13 22:55:22 +03:00
|
|
|
for ntfn := range tcn.ntfnsByConfirmHeight[tcn.currentHeight] {
|
|
|
|
Log.Infof("Dispatching %v conf notification for %v",
|
|
|
|
ntfn.NumConfirmations, ntfn.TxID)
|
2017-12-05 00:30:33 +03:00
|
|
|
select {
|
|
|
|
case ntfn.Event.Confirmed <- ntfn.details:
|
|
|
|
ntfn.dispatched = true
|
|
|
|
case <-tcn.quit:
|
|
|
|
return fmt.Errorf("TxConfNotifier is exiting")
|
|
|
|
}
|
2017-11-13 22:55:22 +03:00
|
|
|
}
|
|
|
|
delete(tcn.ntfnsByConfirmHeight, tcn.currentHeight)
|
|
|
|
|
|
|
|
// Clear entries from confNotifications and confTxsByInitialHeight. We
|
2018-03-19 22:04:19 +03:00
|
|
|
// 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.
|
2017-11-13 22:56:25 +03:00
|
|
|
if tcn.currentHeight >= tcn.reorgSafetyLimit {
|
|
|
|
matureBlockHeight := tcn.currentHeight - tcn.reorgSafetyLimit
|
2018-03-19 22:04:19 +03:00
|
|
|
for txHash := range tcn.txsByInitialHeight[matureBlockHeight] {
|
|
|
|
delete(tcn.confNotifications, txHash)
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
2018-03-19 22:04:19 +03:00
|
|
|
delete(tcn.txsByInitialHeight, matureBlockHeight)
|
2017-11-13 22:55:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisconnectTip handles the tip of the current chain being disconnected during
|
|
|
|
// 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 {
|
2017-12-05 00:30:33 +03:00
|
|
|
select {
|
|
|
|
case <-tcn.quit:
|
|
|
|
return fmt.Errorf("TxConfNotifier is exiting")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:55:22 +03:00
|
|
|
if blockHeight != tcn.currentHeight {
|
|
|
|
return fmt.Errorf("Received blocks out of order: "+
|
|
|
|
"current height=%d, disconnected height=%d",
|
|
|
|
tcn.currentHeight, blockHeight)
|
|
|
|
}
|
|
|
|
tcn.currentHeight--
|
2017-11-14 04:32:11 +03:00
|
|
|
tcn.reorgDepth++
|
2017-11-13 22:55:22 +03:00
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// 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 txHash := range txHashes {
|
|
|
|
for _, ntfn := range tcn.confNotifications[txHash] {
|
|
|
|
// First, we'll attempt to drain an update
|
|
|
|
// from each notification to ensure sends to the
|
|
|
|
// Updates channel are always non-blocking.
|
2017-11-14 04:32:11 +03:00
|
|
|
select {
|
2018-03-19 22:22:44 +03:00
|
|
|
case <-ntfn.Event.Updates:
|
2017-12-05 00:30:33 +03:00
|
|
|
case <-tcn.quit:
|
2018-03-19 22:22:44 +03:00
|
|
|
return errors.New("TxConfNotifier is exiting")
|
|
|
|
default:
|
2017-11-14 04:32:11 +03:00
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// Then, we'll check if the current transaction
|
|
|
|
// was included in the block currently being
|
|
|
|
// disconnected. If it was, we'll need to take
|
|
|
|
// some necessary precautions.
|
|
|
|
if initialHeight == blockHeight {
|
|
|
|
// If the transaction's confirmation notification
|
|
|
|
// has already been dispatched, we'll attempt to
|
|
|
|
// notify the client it was reorged out of the chain.
|
|
|
|
if ntfn.dispatched {
|
|
|
|
// Attempt to drain the confirmation notification
|
|
|
|
// to ensure sends to the Confirmed channel are
|
|
|
|
// always non-blocking.
|
|
|
|
select {
|
|
|
|
case <-ntfn.Event.Confirmed:
|
|
|
|
case <-tcn.quit:
|
|
|
|
return errors.New("TxConfNotifier is exiting")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
ntfn.dispatched = false
|
|
|
|
|
|
|
|
// 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 errors.New("TxConfNotifier is exiting")
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, since the transactions was reorged out
|
|
|
|
// of the chain, we can safely remove its accompanying
|
|
|
|
// confirmation notification.
|
|
|
|
confHeight := blockHeight + ntfn.NumConfirmations - 1
|
|
|
|
ntfnSet, exists := tcn.ntfnsByConfirmHeight[confHeight]
|
|
|
|
if !exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(ntfnSet, ntfn)
|
|
|
|
}
|
2017-11-13 22:55:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 22:04:19 +03:00
|
|
|
|
|
|
|
// Finally, we can remove the transactions we're currently watching that
|
|
|
|
// were included in this block height.
|
|
|
|
delete(tcn.txsByInitialHeight, blockHeight)
|
2017-11-13 22:55:22 +03:00
|
|
|
|
|
|
|
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() {
|
2017-12-05 00:30:33 +03:00
|
|
|
close(tcn.quit)
|
|
|
|
|
2017-11-13 22:55:22 +03:00
|
|
|
for _, ntfns := range tcn.confNotifications {
|
|
|
|
for _, ntfn := range ntfns {
|
|
|
|
if ntfn.dispatched {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
close(ntfn.Event.Confirmed)
|
2018-03-19 21:48:44 +03:00
|
|
|
close(ntfn.Event.Updates)
|
2017-11-13 22:55:22 +03:00
|
|
|
close(ntfn.Event.NegativeConf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|