2016-01-07 00:03:17 +03:00
|
|
|
package btcdnotify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
2016-12-15 12:07:12 +03:00
|
|
|
"errors"
|
2016-01-07 00:03:17 +03:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2016-02-27 03:30:14 +03:00
|
|
|
"time"
|
2016-01-07 00:03:17 +03:00
|
|
|
|
2016-08-04 08:13:10 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcd/btcjson"
|
2017-01-06 00:56:27 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
"github.com/roasbeef/btcrpcclient"
|
|
|
|
"github.com/roasbeef/btcutil"
|
2016-01-07 00:03:17 +03:00
|
|
|
)
|
|
|
|
|
2016-08-04 08:13:10 +03:00
|
|
|
const (
|
|
|
|
|
|
|
|
// notifierType uniquely identifies this concrete implementation of the
|
|
|
|
// ChainNotifier interface.
|
|
|
|
notifierType = "btcd"
|
|
|
|
)
|
|
|
|
|
2016-12-14 02:32:44 +03:00
|
|
|
var (
|
2017-02-24 16:32:33 +03:00
|
|
|
// ErrChainNotifierShuttingDown is used when we are trying to
|
|
|
|
// measure a spend notification when notifier is already stopped.
|
2016-12-14 02:32:44 +03:00
|
|
|
ErrChainNotifierShuttingDown = errors.New("chainntnfs: system interrupt " +
|
|
|
|
"while attempting to register for spend notification.")
|
|
|
|
)
|
|
|
|
|
2016-09-23 05:12:12 +03:00
|
|
|
// chainUpdate encapsulates an update to the current main chain. This struct is
|
|
|
|
// used as an element within an unbounded queue in order to avoid blocking the
|
|
|
|
// main rpc dispatch rule.
|
|
|
|
type chainUpdate struct {
|
2017-01-06 00:56:27 +03:00
|
|
|
blockHash *chainhash.Hash
|
2016-09-23 05:12:12 +03:00
|
|
|
blockHeight int32
|
|
|
|
}
|
|
|
|
|
|
|
|
// txUpdate encapsulates a transaction related notification sent from btcd to
|
|
|
|
// the registered RPC client. This struct is used as an element within an
|
|
|
|
// unbounded queue in order to avoid blocking the main rpc dispatch rule.
|
|
|
|
type txUpdate struct {
|
|
|
|
tx *btcutil.Tx
|
|
|
|
details *btcjson.BlockDetails
|
|
|
|
}
|
|
|
|
|
2017-02-21 03:31:16 +03:00
|
|
|
// TODO(roasbeef): generalize struct below:
|
|
|
|
// * move chans to config, allow outside callers to handle send conditions
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// BtcdNotifier implements the ChainNotifier interface using btcd's websockets
|
|
|
|
// notifications. Multiple concurrent clients are supported. All notifications
|
|
|
|
// are achieved via non-blocking sends on client channels.
|
2016-01-07 00:03:17 +03:00
|
|
|
type BtcdNotifier struct {
|
2017-02-21 03:31:16 +03:00
|
|
|
spendClientCounter uint64 // To be used atomically.
|
|
|
|
epochClientCounter uint64 // To be used atomically.
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
started int32 // To be used atomically.
|
|
|
|
stopped int32 // To be used atomically.
|
2016-02-17 01:46:18 +03:00
|
|
|
|
2016-02-27 03:30:14 +03:00
|
|
|
chainConn *btcrpcclient.Client
|
2016-01-07 00:03:17 +03:00
|
|
|
|
2017-02-21 03:31:16 +03:00
|
|
|
notificationCancels chan interface{}
|
2016-01-07 00:03:17 +03:00
|
|
|
notificationRegistry chan interface{}
|
|
|
|
|
2017-02-21 03:31:16 +03:00
|
|
|
spendNotifications map[wire.OutPoint]map[uint64]*spendNotification
|
2016-09-08 21:27:07 +03:00
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
confNotifications map[chainhash.Hash][]*confirmationsNotification
|
2016-09-08 21:27:07 +03:00
|
|
|
confHeap *confirmationHeap
|
|
|
|
|
2017-05-06 01:53:09 +03:00
|
|
|
blockEpochClients map[uint64]*blockEpochRegistration
|
2016-01-07 00:03:17 +03:00
|
|
|
|
2016-02-27 03:30:14 +03:00
|
|
|
disconnectedBlockHashes chan *blockNtfn
|
2016-09-23 05:12:12 +03:00
|
|
|
|
|
|
|
chainUpdates []*chainUpdate
|
|
|
|
chainUpdateSignal chan struct{}
|
|
|
|
chainUpdateMtx sync.Mutex
|
|
|
|
|
|
|
|
txUpdates []*txUpdate
|
|
|
|
txUpdateSignal chan struct{}
|
|
|
|
txUpdateMtx sync.Mutex
|
2016-01-07 00:03:17 +03:00
|
|
|
|
2016-02-17 01:46:18 +03:00
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan struct{}
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// Ensure BtcdNotifier implements the ChainNotifier interface at compile time.
|
2016-01-07 00:03:17 +03:00
|
|
|
var _ chainntnfs.ChainNotifier = (*BtcdNotifier)(nil)
|
|
|
|
|
2016-08-04 08:13:10 +03:00
|
|
|
// New returns a new BtcdNotifier instance. This function assumes the btcd node
|
2016-11-28 06:17:27 +03:00
|
|
|
// detailed in the passed configuration is already running, and willing to
|
|
|
|
// accept new websockets clients.
|
2016-08-04 08:13:10 +03:00
|
|
|
func New(config *btcrpcclient.ConnConfig) (*BtcdNotifier, error) {
|
2016-02-27 03:30:14 +03:00
|
|
|
notifier := &BtcdNotifier{
|
2017-02-21 03:31:16 +03:00
|
|
|
notificationCancels: make(chan interface{}),
|
2016-01-07 00:03:17 +03:00
|
|
|
notificationRegistry: make(chan interface{}),
|
|
|
|
|
2017-05-06 01:53:09 +03:00
|
|
|
blockEpochClients: make(map[uint64]*blockEpochRegistration),
|
2017-02-21 03:31:16 +03:00
|
|
|
|
|
|
|
spendNotifications: make(map[wire.OutPoint]map[uint64]*spendNotification),
|
|
|
|
|
|
|
|
confNotifications: make(map[chainhash.Hash][]*confirmationsNotification),
|
|
|
|
confHeap: newConfirmationHeap(),
|
2016-01-07 00:03:17 +03:00
|
|
|
|
2016-02-27 03:30:14 +03:00
|
|
|
disconnectedBlockHashes: make(chan *blockNtfn, 20),
|
2016-09-23 05:12:12 +03:00
|
|
|
|
|
|
|
chainUpdateSignal: make(chan struct{}),
|
|
|
|
txUpdateSignal: make(chan struct{}),
|
2016-01-07 00:03:17 +03:00
|
|
|
|
|
|
|
quit: make(chan struct{}),
|
2016-02-27 03:30:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ntfnCallbacks := &btcrpcclient.NotificationHandlers{
|
|
|
|
OnBlockConnected: notifier.onBlockConnected,
|
|
|
|
OnBlockDisconnected: notifier.onBlockDisconnected,
|
|
|
|
OnRedeemingTx: notifier.onRedeemingTx,
|
|
|
|
}
|
|
|
|
|
2016-08-04 08:13:10 +03:00
|
|
|
// Disable connecting to btcd within the btcrpcclient.New method. We
|
|
|
|
// defer establishing the connection to our .Start() method.
|
2016-02-27 03:30:14 +03:00
|
|
|
config.DisableConnectOnNew = true
|
|
|
|
config.DisableAutoReconnect = false
|
|
|
|
chainConn, err := btcrpcclient.New(config, ntfnCallbacks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
notifier.chainConn = chainConn
|
|
|
|
|
|
|
|
return notifier, nil
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// Start connects to the running btcd node over websockets, registers for block
|
|
|
|
// notifications, and finally launches all related helper goroutines.
|
2016-01-07 00:03:17 +03:00
|
|
|
func (b *BtcdNotifier) Start() error {
|
|
|
|
// Already started?
|
|
|
|
if atomic.AddInt32(&b.started, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// Connect to btcd, and register for notifications on connected, and
|
|
|
|
// disconnected blocks.
|
2016-02-27 03:30:14 +03:00
|
|
|
if err := b.chainConn.Connect(20); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.chainConn.NotifyBlocks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-25 03:42:13 +03:00
|
|
|
_, currentHeight, err := b.chainConn.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-07 00:03:17 +03:00
|
|
|
b.wg.Add(1)
|
2016-12-25 03:42:13 +03:00
|
|
|
go b.notificationDispatcher(currentHeight)
|
2016-01-07 00:03:17 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// Stop shutsdown the BtcdNotifier.
|
2016-01-07 00:03:17 +03:00
|
|
|
func (b *BtcdNotifier) Stop() error {
|
|
|
|
// Already shutting down?
|
|
|
|
if atomic.AddInt32(&b.stopped, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// Shutdown the rpc client, this gracefully disconnects from btcd, and
|
|
|
|
// cleans up all related resources.
|
2016-02-27 03:30:14 +03:00
|
|
|
b.chainConn.Shutdown()
|
|
|
|
|
2016-01-07 00:03:17 +03:00
|
|
|
close(b.quit)
|
|
|
|
b.wg.Wait()
|
|
|
|
|
2016-02-27 04:04:14 +03:00
|
|
|
// Notify all pending clients of our shutdown by closing the related
|
|
|
|
// notification channels.
|
2016-11-28 06:17:27 +03:00
|
|
|
for _, spendClients := range b.spendNotifications {
|
|
|
|
for _, spendClient := range spendClients {
|
|
|
|
close(spendClient.spendChan)
|
|
|
|
}
|
2016-02-27 04:04:14 +03:00
|
|
|
}
|
2016-09-09 21:25:12 +03:00
|
|
|
for _, confClients := range b.confNotifications {
|
|
|
|
for _, confClient := range confClients {
|
|
|
|
close(confClient.finConf)
|
|
|
|
close(confClient.negativeConf)
|
|
|
|
}
|
2016-02-27 04:04:14 +03:00
|
|
|
}
|
2016-12-16 00:53:36 +03:00
|
|
|
for _, epochClient := range b.blockEpochClients {
|
2017-05-06 01:53:09 +03:00
|
|
|
close(epochClient.epochChan)
|
2016-12-16 00:53:36 +03:00
|
|
|
}
|
2016-02-27 04:04:14 +03:00
|
|
|
|
2016-01-07 00:03:17 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// blockNtfn packages a notification of a connected/disconnected block along
|
|
|
|
// with its height at the time.
|
2016-02-27 03:30:14 +03:00
|
|
|
type blockNtfn struct {
|
2017-01-06 00:56:27 +03:00
|
|
|
sha *chainhash.Hash
|
2016-02-27 03:30:14 +03:00
|
|
|
height int32
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// onBlockConnected implements on OnBlockConnected callback for btcrpcclient.
|
2016-09-23 05:12:12 +03:00
|
|
|
// Ingesting a block updates the wallet's internal utxo state based on the
|
|
|
|
// outputs created and destroyed within each block.
|
2017-01-06 00:56:27 +03:00
|
|
|
func (b *BtcdNotifier) onBlockConnected(hash *chainhash.Hash, height int32, t time.Time) {
|
2016-09-23 05:12:12 +03:00
|
|
|
// Append this new chain update to the end of the queue of new chain
|
|
|
|
// updates.
|
|
|
|
b.chainUpdateMtx.Lock()
|
|
|
|
b.chainUpdates = append(b.chainUpdates, &chainUpdate{hash, height})
|
|
|
|
b.chainUpdateMtx.Unlock()
|
|
|
|
|
|
|
|
// Launch a goroutine to signal the notification dispatcher that a new
|
|
|
|
// block update is available. We do this in a new goroutine in order to
|
|
|
|
// avoid blocking the main loop of the rpc client.
|
|
|
|
go func() {
|
|
|
|
b.chainUpdateSignal <- struct{}{}
|
|
|
|
}()
|
2016-02-27 03:30:14 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// onBlockDisconnected implements on OnBlockDisconnected callback for btcrpcclient.
|
2017-01-06 00:56:27 +03:00
|
|
|
func (b *BtcdNotifier) onBlockDisconnected(hash *chainhash.Hash, height int32, t time.Time) {
|
2016-02-27 03:30:14 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// onRedeemingTx implements on OnRedeemingTx callback for btcrpcclient.
|
2016-09-23 05:12:12 +03:00
|
|
|
func (b *BtcdNotifier) onRedeemingTx(tx *btcutil.Tx, details *btcjson.BlockDetails) {
|
2016-11-28 06:17:27 +03:00
|
|
|
// Append this new transaction update to the end of the queue of new
|
|
|
|
// chain updates.
|
2016-09-23 05:12:12 +03:00
|
|
|
b.txUpdateMtx.Lock()
|
|
|
|
b.txUpdates = append(b.txUpdates, &txUpdate{tx, details})
|
|
|
|
b.txUpdateMtx.Unlock()
|
|
|
|
|
|
|
|
// Launch a goroutine to signal the notification dispatcher that a new
|
|
|
|
// transaction update is available. We do this in a new goroutine in
|
|
|
|
// order to avoid blocking the main loop of the rpc client.
|
|
|
|
go func() {
|
|
|
|
b.txUpdateSignal <- struct{}{}
|
|
|
|
}()
|
2016-02-27 03:30:14 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// notificationDispatcher is the primary goroutine which handles client
|
|
|
|
// notification registrations, as well as notification dispatches.
|
2016-12-25 03:42:13 +03:00
|
|
|
func (b *BtcdNotifier) notificationDispatcher(currentHeight int32) {
|
2016-01-07 00:03:17 +03:00
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
2017-02-21 03:31:16 +03:00
|
|
|
case cancelMsg := <-b.notificationCancels:
|
|
|
|
switch msg := cancelMsg.(type) {
|
|
|
|
case *spendCancel:
|
|
|
|
chainntnfs.Log.Infof("Cancelling spend "+
|
|
|
|
"notification for out_point=%v, "+
|
|
|
|
"spend_id=%v", msg.op, msg.spendID)
|
|
|
|
|
|
|
|
// Before we attempt to close the spendChan,
|
|
|
|
// ensure that the notification hasn't already
|
|
|
|
// yet been dispatched.
|
|
|
|
if outPointClients, ok := b.spendNotifications[msg.op]; ok {
|
|
|
|
close(outPointClients[msg.spendID].spendChan)
|
|
|
|
delete(b.spendNotifications[msg.op], msg.spendID)
|
|
|
|
}
|
2017-07-30 05:19:28 +03:00
|
|
|
|
2017-02-21 03:31:16 +03:00
|
|
|
case *epochCancel:
|
|
|
|
chainntnfs.Log.Infof("Cancelling epoch "+
|
|
|
|
"notification, epoch_id=%v", msg.epochID)
|
|
|
|
|
2017-06-08 03:04:27 +03:00
|
|
|
// First, close the cancel channel for this
|
|
|
|
// specific client, and wait for the client to
|
|
|
|
// exit.
|
2017-05-06 01:53:09 +03:00
|
|
|
close(b.blockEpochClients[msg.epochID].cancelChan)
|
2017-06-08 03:04:27 +03:00
|
|
|
b.blockEpochClients[msg.epochID].wg.Wait()
|
|
|
|
|
|
|
|
// Once the client has exited, we can then
|
|
|
|
// safely close the channel used to send epoch
|
|
|
|
// notifications, in order to notify any
|
|
|
|
// listeners that the intent has been
|
|
|
|
// cancelled.
|
2017-05-06 01:53:09 +03:00
|
|
|
close(b.blockEpochClients[msg.epochID].epochChan)
|
2017-02-21 03:31:16 +03:00
|
|
|
delete(b.blockEpochClients, msg.epochID)
|
2017-04-27 07:08:16 +03:00
|
|
|
|
2017-02-21 03:31:16 +03:00
|
|
|
}
|
2016-01-07 00:03:17 +03:00
|
|
|
case registerMsg := <-b.notificationRegistry:
|
|
|
|
switch msg := registerMsg.(type) {
|
|
|
|
case *spendNotification:
|
2016-09-08 21:27:07 +03:00
|
|
|
chainntnfs.Log.Infof("New spend subscription: "+
|
|
|
|
"utxo=%v", msg.targetOutpoint)
|
2016-11-28 06:17:27 +03:00
|
|
|
op := *msg.targetOutpoint
|
2017-02-21 03:31:16 +03:00
|
|
|
|
|
|
|
if _, ok := b.spendNotifications[op]; !ok {
|
|
|
|
b.spendNotifications[op] = make(map[uint64]*spendNotification)
|
|
|
|
}
|
|
|
|
b.spendNotifications[op][msg.spendID] = msg
|
2016-01-07 00:03:17 +03:00
|
|
|
case *confirmationsNotification:
|
2016-06-21 07:33:41 +03:00
|
|
|
chainntnfs.Log.Infof("New confirmations "+
|
|
|
|
"subscription: txid=%v, numconfs=%v",
|
|
|
|
*msg.txid, msg.numConfirmations)
|
2016-12-09 03:15:58 +03:00
|
|
|
|
|
|
|
// If the notification can be partially or
|
|
|
|
// fully dispatched, then we can skip the first
|
|
|
|
// phase for ntfns.
|
|
|
|
if b.attemptHistoricalDispatch(msg, currentHeight) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-09-09 21:25:12 +03:00
|
|
|
txid := *msg.txid
|
|
|
|
b.confNotifications[txid] = append(b.confNotifications[txid], msg)
|
2016-09-08 21:27:07 +03:00
|
|
|
case *blockEpochRegistration:
|
|
|
|
chainntnfs.Log.Infof("New block epoch subscription")
|
2017-05-06 01:53:09 +03:00
|
|
|
b.blockEpochClients[msg.epochID] = msg
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
2017-02-21 03:31:16 +03:00
|
|
|
|
2016-02-27 03:30:14 +03:00
|
|
|
case staleBlockHash := <-b.disconnectedBlockHashes:
|
|
|
|
// TODO(roasbeef): re-orgs
|
|
|
|
// * second channel to notify of confirmation decrementing
|
|
|
|
// re-org?
|
|
|
|
// * notify of negative confirmations
|
2016-09-08 21:27:07 +03:00
|
|
|
chainntnfs.Log.Warnf("Block disconnected from main "+
|
|
|
|
"chain: %v", staleBlockHash)
|
2017-02-21 03:31:16 +03:00
|
|
|
|
2016-09-23 05:12:12 +03:00
|
|
|
case <-b.chainUpdateSignal:
|
|
|
|
// A new update is available, so pop the new chain
|
|
|
|
// update from the front of the update queue.
|
|
|
|
b.chainUpdateMtx.Lock()
|
|
|
|
update := b.chainUpdates[0]
|
|
|
|
b.chainUpdates[0] = nil // Set to nil to prevent GC leak.
|
|
|
|
b.chainUpdates = b.chainUpdates[1:]
|
|
|
|
b.chainUpdateMtx.Unlock()
|
|
|
|
|
2016-12-09 03:15:58 +03:00
|
|
|
currentHeight = update.blockHeight
|
|
|
|
|
2016-09-23 05:12:12 +03:00
|
|
|
newBlock, err := b.chainConn.GetBlock(update.blockHash)
|
2016-02-27 03:30:14 +03:00
|
|
|
if err != nil {
|
2016-09-08 21:27:07 +03:00
|
|
|
chainntnfs.Log.Errorf("Unable to get block: %v", err)
|
2016-02-27 03:30:14 +03:00
|
|
|
continue
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 07:33:41 +03:00
|
|
|
chainntnfs.Log.Infof("New block: height=%v, sha=%v",
|
2016-09-23 05:12:12 +03:00
|
|
|
update.blockHeight, update.blockHash)
|
2016-06-21 07:33:41 +03:00
|
|
|
|
2017-02-22 10:24:23 +03:00
|
|
|
b.notifyBlockEpochs(update.blockHeight,
|
2016-09-23 05:12:12 +03:00
|
|
|
update.blockHash)
|
2016-09-08 21:27:07 +03:00
|
|
|
|
2016-09-23 05:12:12 +03:00
|
|
|
newHeight := update.blockHeight
|
2016-12-25 03:42:13 +03:00
|
|
|
for i, tx := range newBlock.Transactions {
|
2016-02-27 03:30:14 +03:00
|
|
|
// Check if the inclusion of this transaction
|
|
|
|
// within a block by itself triggers a block
|
|
|
|
// confirmation threshold, if so send a
|
2016-11-28 06:17:27 +03:00
|
|
|
// notification. Otherwise, place the
|
|
|
|
// notification on a heap to be triggered in
|
|
|
|
// the future once additional confirmations are
|
|
|
|
// attained.
|
2017-01-06 00:56:27 +03:00
|
|
|
txSha := tx.TxHash()
|
2016-12-25 03:42:13 +03:00
|
|
|
b.checkConfirmationTrigger(&txSha, update, i)
|
2016-02-27 03:30:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
b.notifyConfs(newHeight)
|
2017-02-21 03:31:16 +03:00
|
|
|
|
2016-09-23 05:12:12 +03:00
|
|
|
case <-b.txUpdateSignal:
|
|
|
|
// A new update is available, so pop the new chain
|
|
|
|
// update from the front of the update queue.
|
|
|
|
b.txUpdateMtx.Lock()
|
|
|
|
newSpend := b.txUpdates[0]
|
|
|
|
b.txUpdates[0] = nil // Set to nil to prevent GC leak.
|
|
|
|
b.txUpdates = b.txUpdates[1:]
|
|
|
|
b.txUpdateMtx.Unlock()
|
|
|
|
|
|
|
|
spendingTx := newSpend.tx
|
|
|
|
|
2016-02-27 03:30:14 +03:00
|
|
|
// First, check if this transaction spends an output
|
|
|
|
// that has an existing spend notification for it.
|
2016-09-23 05:12:12 +03:00
|
|
|
for i, txIn := range spendingTx.MsgTx().TxIn {
|
2016-02-27 03:30:14 +03:00
|
|
|
prevOut := txIn.PreviousOutPoint
|
|
|
|
|
|
|
|
// If this transaction indeed does spend an
|
2016-11-28 06:17:27 +03:00
|
|
|
// output which we have a registered
|
|
|
|
// notification for, then create a spend
|
|
|
|
// summary, finally sending off the details to
|
|
|
|
// the notification subscriber.
|
|
|
|
if clients, ok := b.spendNotifications[prevOut]; ok {
|
2017-01-06 00:56:27 +03:00
|
|
|
spenderSha := newSpend.tx.Hash()
|
2017-05-11 02:52:27 +03:00
|
|
|
spendDetails := &chainntnfs.SpendDetail{
|
|
|
|
SpentOutPoint: &prevOut,
|
|
|
|
SpenderTxHash: spenderSha,
|
|
|
|
SpendingTx: spendingTx.MsgTx(),
|
|
|
|
SpenderInputIndex: uint32(i),
|
|
|
|
}
|
|
|
|
// TODO(roasbeef): after change to
|
|
|
|
// loadfilter, only notify on block
|
|
|
|
// inclusion?
|
|
|
|
if newSpend.details != nil {
|
|
|
|
spendDetails.SpendingHeight = newSpend.details.Height
|
|
|
|
} else {
|
2017-05-24 04:18:45 +03:00
|
|
|
spendDetails.SpendingHeight = currentHeight + 1
|
2017-05-11 02:52:27 +03:00
|
|
|
}
|
2016-11-28 06:17:27 +03:00
|
|
|
|
2017-05-11 02:52:27 +03:00
|
|
|
for _, ntfn := range clients {
|
2016-11-28 06:17:27 +03:00
|
|
|
chainntnfs.Log.Infof("Dispatching "+
|
|
|
|
"spend notification for "+
|
|
|
|
"outpoint=%v", ntfn.targetOutpoint)
|
|
|
|
ntfn.spendChan <- spendDetails
|
2016-01-07 00:03:17 +03:00
|
|
|
|
2017-08-03 10:18:33 +03:00
|
|
|
// Close spendChan to ensure that any calls to Cancel will not
|
|
|
|
// block. This is safe to do since the channel is buffered, and the
|
|
|
|
// message can still be read by the receiver.
|
|
|
|
close(ntfn.spendChan)
|
|
|
|
}
|
2016-02-27 03:30:14 +03:00
|
|
|
delete(b.spendNotifications, prevOut)
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
}
|
2017-02-21 03:31:16 +03:00
|
|
|
|
2016-01-07 00:03:17 +03:00
|
|
|
case <-b.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
2016-02-27 03:30:14 +03:00
|
|
|
b.wg.Done()
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
2016-12-09 03:15:58 +03:00
|
|
|
// attemptHistoricalDispatch tries to use historical information to decide if a
|
2016-12-27 06:45:01 +03:00
|
|
|
// notification ca be dispatched immediately, or is partially confirmed so it
|
2016-12-09 03:15:58 +03:00
|
|
|
// can skip straight to the confirmations heap.
|
|
|
|
func (b *BtcdNotifier) attemptHistoricalDispatch(msg *confirmationsNotification,
|
|
|
|
currentHeight int32) bool {
|
|
|
|
|
2016-12-27 06:45:01 +03:00
|
|
|
chainntnfs.Log.Infof("Attempting to trigger dispatch for %v from "+
|
|
|
|
"historical chain", msg.txid)
|
|
|
|
|
2016-12-09 03:15:58 +03:00
|
|
|
// If the transaction already has some or all of the confirmations,
|
2016-12-27 06:45:01 +03:00
|
|
|
// then we may be able to dispatch it immediately.
|
2016-12-09 03:15:58 +03:00
|
|
|
tx, err := b.chainConn.GetRawTransactionVerbose(msg.txid)
|
2017-01-06 00:56:27 +03:00
|
|
|
if err != nil || tx == nil || tx.BlockHash == "" {
|
2017-05-05 01:09:19 +03:00
|
|
|
if err != nil {
|
2017-05-17 04:46:50 +03:00
|
|
|
chainntnfs.Log.Warnf("unable to query for txid(%v): %v",
|
2017-05-05 01:09:19 +03:00
|
|
|
msg.txid, err)
|
|
|
|
}
|
2016-12-27 06:45:01 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// As we need to fully populate the returned TxConfirmation struct,
|
|
|
|
// grab the block in which the transaction was confirmed so we can
|
|
|
|
// locate its exact index within the block.
|
2017-01-06 00:56:27 +03:00
|
|
|
blockHash, err := chainhash.NewHashFromStr(tx.BlockHash)
|
2016-12-27 06:45:01 +03:00
|
|
|
if err != nil {
|
|
|
|
chainntnfs.Log.Errorf("unable to get block hash %v for "+
|
|
|
|
"historical dispatch: %v", tx.BlockHash, err)
|
|
|
|
return false
|
|
|
|
}
|
2017-05-16 03:47:03 +03:00
|
|
|
block, err := b.chainConn.GetBlockVerbose(blockHash)
|
2016-12-09 03:15:58 +03:00
|
|
|
if err != nil {
|
2016-12-27 06:45:01 +03:00
|
|
|
chainntnfs.Log.Errorf("unable to get block hash: %v", err)
|
2016-12-09 03:15:58 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-12-27 06:45:01 +03:00
|
|
|
// If the block obtained, locate the transaction's index within the
|
|
|
|
// block so we can give the subscriber full confirmation details.
|
|
|
|
var txIndex uint32
|
2017-05-16 03:47:03 +03:00
|
|
|
targetTxidStr := msg.txid.String()
|
|
|
|
for i, txHash := range block.Tx {
|
|
|
|
if txHash == targetTxidStr {
|
2016-12-27 06:45:01 +03:00
|
|
|
txIndex = uint32(i)
|
2017-01-03 17:13:56 +03:00
|
|
|
break
|
2016-12-27 06:45:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 03:42:13 +03:00
|
|
|
confDetails := &chainntnfs.TxConfirmation{
|
2016-12-27 06:45:01 +03:00
|
|
|
BlockHash: blockHash,
|
2017-05-16 03:47:03 +03:00
|
|
|
BlockHeight: uint32(block.Height),
|
2016-12-27 06:45:01 +03:00
|
|
|
TxIndex: txIndex,
|
2016-12-25 03:42:13 +03:00
|
|
|
}
|
|
|
|
|
2016-12-09 03:15:58 +03:00
|
|
|
// If the transaction has more that enough confirmations, then we can
|
2016-12-25 03:42:13 +03:00
|
|
|
// dispatch it immediately after obtaining for information w.r.t
|
2016-12-27 06:45:01 +03:00
|
|
|
// exactly *when* if got all its confirmations.
|
2016-12-09 03:15:58 +03:00
|
|
|
if uint32(tx.Confirmations) >= msg.numConfirmations {
|
2016-12-25 03:42:13 +03:00
|
|
|
msg.finConf <- confDetails
|
2016-12-09 03:15:58 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-25 03:42:13 +03:00
|
|
|
// Otherwise, the transaction has only been *partially* confirmed, so
|
|
|
|
// we need to insert it into the confirmation heap.
|
2016-12-09 03:15:58 +03:00
|
|
|
confsLeft := msg.numConfirmations - uint32(tx.Confirmations)
|
|
|
|
confHeight := uint32(currentHeight) + confsLeft
|
|
|
|
heapEntry := &confEntry{
|
|
|
|
msg,
|
2016-12-25 03:42:13 +03:00
|
|
|
confDetails,
|
2016-12-09 03:15:58 +03:00
|
|
|
confHeight,
|
|
|
|
}
|
|
|
|
heap.Push(b.confHeap, heapEntry)
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-09-08 21:27:07 +03:00
|
|
|
// notifyBlockEpochs notifies all registered block epoch clients of the newly
|
|
|
|
// connected block to the main chain.
|
2017-01-06 00:56:27 +03:00
|
|
|
func (b *BtcdNotifier) notifyBlockEpochs(newHeight int32, newSha *chainhash.Hash) {
|
2016-09-08 21:27:07 +03:00
|
|
|
epoch := &chainntnfs.BlockEpoch{
|
|
|
|
Height: newHeight,
|
|
|
|
Hash: newSha,
|
|
|
|
}
|
|
|
|
|
2017-05-06 01:53:09 +03:00
|
|
|
for _, epochClient := range b.blockEpochClients {
|
2017-04-04 15:20:07 +03:00
|
|
|
b.wg.Add(1)
|
2017-06-08 03:04:27 +03:00
|
|
|
epochClient.wg.Add(1)
|
|
|
|
go func(ntfnChan chan *chainntnfs.BlockEpoch, cancelChan chan struct{},
|
2017-06-08 03:07:27 +03:00
|
|
|
clientWg *sync.WaitGroup) {
|
2017-06-08 03:04:27 +03:00
|
|
|
|
|
|
|
defer clientWg.Done()
|
2017-04-04 15:20:07 +03:00
|
|
|
defer b.wg.Done()
|
|
|
|
|
2017-04-01 12:26:57 +03:00
|
|
|
select {
|
|
|
|
case ntfnChan <- epoch:
|
2017-05-06 01:53:09 +03:00
|
|
|
|
|
|
|
case <-cancelChan:
|
|
|
|
return
|
|
|
|
|
2017-04-01 12:26:57 +03:00
|
|
|
case <-b.quit:
|
|
|
|
return
|
|
|
|
}
|
2017-06-08 03:04:27 +03:00
|
|
|
|
2017-06-08 03:07:27 +03:00
|
|
|
}(epochClient.epochChan, epochClient.cancelChan, &epochClient.wg)
|
2016-09-08 21:27:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// notifyConfs examines the current confirmation heap, sending off any
|
|
|
|
// notifications which have been triggered by the connection of a new block at
|
|
|
|
// newBlockHeight.
|
2016-02-27 03:30:14 +03:00
|
|
|
func (b *BtcdNotifier) notifyConfs(newBlockHeight int32) {
|
|
|
|
// If the heap is empty, we have nothing to do.
|
|
|
|
if b.confHeap.Len() == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-25 09:16:25 +03:00
|
|
|
// Traverse our confirmation heap. The heap is a
|
|
|
|
// min-heap, so the confirmation notification which requires
|
|
|
|
// the smallest block-height will always be at the top
|
|
|
|
// of the heap. If a confirmation notification is eligible
|
|
|
|
// for triggering, then fire it off, and check if another
|
|
|
|
// is eligible until there are no more eligible entries.
|
|
|
|
nextConf := heap.Pop(b.confHeap).(*confEntry)
|
2016-02-27 03:30:14 +03:00
|
|
|
for nextConf.triggerHeight <= uint32(newBlockHeight) {
|
2016-12-25 03:42:13 +03:00
|
|
|
// TODO(roasbeef): shake out possible of by one in height calc
|
|
|
|
// for historical dispatches
|
|
|
|
nextConf.finConf <- nextConf.initialConfDetails
|
2016-02-25 09:16:25 +03:00
|
|
|
|
2016-02-27 03:30:14 +03:00
|
|
|
if b.confHeap.Len() == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-25 09:16:25 +03:00
|
|
|
nextConf = heap.Pop(b.confHeap).(*confEntry)
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
2016-02-25 09:16:25 +03:00
|
|
|
|
|
|
|
heap.Push(b.confHeap, nextConf)
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// checkConfirmationTrigger determines if the passed txSha included at blockHeight
|
|
|
|
// triggers any single confirmation notifications. In the event that the txid
|
|
|
|
// matches, yet needs additional confirmations, it is added to the confirmation
|
|
|
|
// heap to be triggered at a later time.
|
|
|
|
// TODO(roasbeef): perhaps lookup, then track by inputs instead?
|
2017-01-06 00:56:27 +03:00
|
|
|
func (b *BtcdNotifier) checkConfirmationTrigger(txSha *chainhash.Hash,
|
2016-12-25 03:42:13 +03:00
|
|
|
newTip *chainUpdate, txIndex int) {
|
2016-09-09 21:25:12 +03:00
|
|
|
|
2016-02-25 09:16:25 +03:00
|
|
|
// If a confirmation notification has been registered
|
|
|
|
// for this txid, then either trigger a notification
|
|
|
|
// event if only a single confirmation notification was
|
|
|
|
// requested, or place the notification on the
|
|
|
|
// confirmation heap for future usage.
|
2016-09-09 21:25:12 +03:00
|
|
|
if confClients, ok := b.confNotifications[*txSha]; ok {
|
2017-05-06 01:53:09 +03:00
|
|
|
// Either all of the registered confirmations will be
|
2016-09-09 21:25:12 +03:00
|
|
|
// dispatched due to a single confirmation, or added to the
|
2017-05-06 01:53:09 +03:00
|
|
|
// conf head. Therefor we unconditionally delete the registered
|
2016-09-09 21:25:12 +03:00
|
|
|
// confirmations from the staging zone.
|
|
|
|
defer func() {
|
|
|
|
delete(b.confNotifications, *txSha)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, confClient := range confClients {
|
2016-12-25 03:42:13 +03:00
|
|
|
confDetails := &chainntnfs.TxConfirmation{
|
|
|
|
BlockHash: newTip.blockHash,
|
|
|
|
BlockHeight: uint32(newTip.blockHeight),
|
|
|
|
TxIndex: uint32(txIndex),
|
|
|
|
}
|
|
|
|
|
2016-09-09 21:25:12 +03:00
|
|
|
if confClient.numConfirmations == 1 {
|
|
|
|
chainntnfs.Log.Infof("Dispatching single conf "+
|
|
|
|
"notification, sha=%v, height=%v", txSha,
|
2016-12-25 03:42:13 +03:00
|
|
|
newTip.blockHeight)
|
|
|
|
confClient.finConf <- confDetails
|
2016-09-09 21:25:12 +03:00
|
|
|
continue
|
|
|
|
}
|
2016-02-25 09:16:25 +03:00
|
|
|
|
2016-09-09 21:25:12 +03:00
|
|
|
// The registered notification requires more
|
|
|
|
// than one confirmation before triggering. So
|
|
|
|
// we create a heapConf entry for this notification.
|
|
|
|
// The heapConf allows us to easily keep track of
|
|
|
|
// which notification(s) we should fire off with
|
|
|
|
// each incoming block.
|
2016-12-25 03:42:13 +03:00
|
|
|
confClient.initialConfirmHeight = uint32(newTip.blockHeight)
|
2017-02-23 22:07:01 +03:00
|
|
|
finalConfHeight := confClient.initialConfirmHeight + confClient.numConfirmations - 1
|
2016-09-09 21:25:12 +03:00
|
|
|
heapEntry := &confEntry{
|
|
|
|
confClient,
|
2016-12-25 03:42:13 +03:00
|
|
|
confDetails,
|
2016-09-09 21:25:12 +03:00
|
|
|
finalConfHeight,
|
|
|
|
}
|
|
|
|
heap.Push(b.confHeap, heapEntry)
|
2016-02-25 09:16:25 +03:00
|
|
|
}
|
|
|
|
}
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// spendNotification couples a target outpoint along with the channel used for
|
|
|
|
// notifications once a spend of the outpoint has been detected.
|
2016-01-07 00:03:17 +03:00
|
|
|
type spendNotification struct {
|
2016-02-17 01:46:18 +03:00
|
|
|
targetOutpoint *wire.OutPoint
|
2016-01-07 00:03:17 +03:00
|
|
|
|
2016-02-17 01:46:18 +03:00
|
|
|
spendChan chan *chainntnfs.SpendDetail
|
2017-02-21 03:31:16 +03:00
|
|
|
|
|
|
|
spendID uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// spendCancel is a message sent to the BtcdNotifier when a client wishes to
|
|
|
|
// cancel an outstanding spend notification that has yet to be dispatched.
|
|
|
|
type spendCancel struct {
|
|
|
|
// op is the target outpoint of the notification to be cancelled.
|
|
|
|
op wire.OutPoint
|
|
|
|
|
|
|
|
// spendID the ID of the notification to cancel.
|
|
|
|
spendID uint64
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// RegisterSpendNtfn registers an intent to be notified once the target
|
2016-02-27 04:31:07 +03:00
|
|
|
// outpoint has been spent by a transaction on-chain. Once a spend of the target
|
|
|
|
// outpoint has been detected, the details of the spending event will be sent
|
|
|
|
// across the 'Spend' channel.
|
2017-05-11 03:00:18 +03:00
|
|
|
func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
|
|
|
|
_ uint32) (*chainntnfs.SpendEvent, error) {
|
2016-11-30 11:00:20 +03:00
|
|
|
|
2016-02-27 03:30:14 +03:00
|
|
|
if err := b.chainConn.NotifySpent([]*wire.OutPoint{outpoint}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-07 00:03:17 +03:00
|
|
|
|
|
|
|
ntfn := &spendNotification{
|
2016-02-17 01:46:18 +03:00
|
|
|
targetOutpoint: outpoint,
|
|
|
|
spendChan: make(chan *chainntnfs.SpendDetail, 1),
|
2017-02-21 03:31:16 +03:00
|
|
|
spendID: atomic.AddUint64(&b.spendClientCounter, 1),
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
2016-12-15 12:07:12 +03:00
|
|
|
select {
|
|
|
|
case <-b.quit:
|
2016-12-14 02:32:44 +03:00
|
|
|
return nil, ErrChainNotifierShuttingDown
|
2016-12-15 12:07:12 +03:00
|
|
|
case b.notificationRegistry <- ntfn:
|
|
|
|
}
|
2016-01-07 00:03:17 +03:00
|
|
|
|
2016-11-30 11:00:20 +03:00
|
|
|
// The following conditional checks to ensure that when a spend notification
|
|
|
|
// is registered, the output hasn't already been spent. If the output
|
|
|
|
// is no longer in the UTXO set, the chain will be rescanned from the point
|
|
|
|
// where the output was added. The rescan will dispatch the notification.
|
|
|
|
txout, err := b.chainConn.GetTxOut(&outpoint.Hash, outpoint.Index, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if txout == nil {
|
|
|
|
transaction, err := b.chainConn.GetRawTransactionVerbose(&outpoint.Hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
blockhash, err := chainhash.NewHashFromStr(transaction.BlockHash)
|
2016-11-30 11:00:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ops := []*wire.OutPoint{outpoint}
|
|
|
|
if err := b.chainConn.Rescan(blockhash, nil, ops); err != nil {
|
|
|
|
chainntnfs.Log.Errorf("Rescan for spend notification txout failed: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 02:28:32 +03:00
|
|
|
return &chainntnfs.SpendEvent{
|
|
|
|
Spend: ntfn.spendChan,
|
2017-02-21 03:31:16 +03:00
|
|
|
Cancel: func() {
|
2017-07-30 05:19:28 +03:00
|
|
|
cancel := &spendCancel{
|
2017-02-21 03:31:16 +03:00
|
|
|
op: *outpoint,
|
|
|
|
spendID: ntfn.spendID,
|
2017-07-30 05:19:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Submit spend cancellation to notification dispatcher.
|
|
|
|
select {
|
|
|
|
case b.notificationCancels <- cancel:
|
2017-07-30 06:28:48 +03:00
|
|
|
// Cancellation is being handled, drain the spend chan until it is
|
|
|
|
// closed before yielding to the caller.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case _, ok := <-ntfn.spendChan:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-b.quit:
|
2017-08-02 03:14:01 +03:00
|
|
|
return
|
2017-07-30 06:28:48 +03:00
|
|
|
}
|
2017-07-30 05:19:28 +03:00
|
|
|
}
|
2017-02-21 03:31:16 +03:00
|
|
|
case <-b.quit:
|
|
|
|
}
|
|
|
|
},
|
2017-02-10 02:28:32 +03:00
|
|
|
}, nil
|
2016-02-17 01:46:18 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// confirmationNotification represents a client's intent to receive a
|
|
|
|
// notification once the target txid reaches numConfirmations confirmations.
|
2016-02-17 01:46:18 +03:00
|
|
|
type confirmationsNotification struct {
|
2017-01-06 00:56:27 +03:00
|
|
|
txid *chainhash.Hash
|
2016-02-17 01:46:18 +03:00
|
|
|
|
|
|
|
initialConfirmHeight uint32
|
|
|
|
numConfirmations uint32
|
|
|
|
|
2016-12-25 03:42:13 +03:00
|
|
|
finConf chan *chainntnfs.TxConfirmation
|
2016-02-27 04:31:07 +03:00
|
|
|
negativeConf chan int32 // TODO(roasbeef): re-org funny business
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
// RegisterConfirmationsNtfn registers a notification with BtcdNotifier
|
2016-02-27 04:31:07 +03:00
|
|
|
// which will be triggered once the txid reaches numConfs number of
|
|
|
|
// confirmations.
|
2017-01-06 00:56:27 +03:00
|
|
|
func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
|
2017-05-11 03:00:18 +03:00
|
|
|
numConfs, _ uint32) (*chainntnfs.ConfirmationEvent, error) {
|
2016-01-07 00:03:17 +03:00
|
|
|
|
|
|
|
ntfn := &confirmationsNotification{
|
|
|
|
txid: txid,
|
|
|
|
numConfirmations: numConfs,
|
2016-12-25 03:42:13 +03:00
|
|
|
finConf: make(chan *chainntnfs.TxConfirmation, 1),
|
2016-02-27 03:30:14 +03:00
|
|
|
negativeConf: make(chan int32, 1),
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
2016-12-15 12:07:12 +03:00
|
|
|
select {
|
|
|
|
case <-b.quit:
|
2016-12-14 02:32:44 +03:00
|
|
|
return nil, ErrChainNotifierShuttingDown
|
2016-12-15 12:07:12 +03:00
|
|
|
case b.notificationRegistry <- ntfn:
|
|
|
|
return &chainntnfs.ConfirmationEvent{
|
|
|
|
Confirmed: ntfn.finConf,
|
|
|
|
NegativeConf: ntfn.negativeConf,
|
|
|
|
}, nil
|
|
|
|
}
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
2016-06-21 07:31:05 +03:00
|
|
|
|
2016-09-08 21:27:07 +03:00
|
|
|
// blockEpochRegistration represents a client's intent to receive a
|
|
|
|
// notification with each newly connected block.
|
|
|
|
type blockEpochRegistration struct {
|
2017-06-08 03:04:27 +03:00
|
|
|
epochID uint64
|
|
|
|
|
2016-09-08 21:27:07 +03:00
|
|
|
epochChan chan *chainntnfs.BlockEpoch
|
2017-02-21 03:31:16 +03:00
|
|
|
|
2017-05-06 01:53:09 +03:00
|
|
|
cancelChan chan struct{}
|
|
|
|
|
2017-06-08 03:04:27 +03:00
|
|
|
wg sync.WaitGroup
|
2017-02-21 03:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// epochCancel is a message sent to the BtcdNotifier when a client wishes to
|
|
|
|
// cancel an outstanding epoch notification that has yet to be dispatched.
|
|
|
|
type epochCancel struct {
|
|
|
|
epochID uint64
|
2016-09-08 21:27:07 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 07:31:05 +03:00
|
|
|
// RegisterBlockEpochNtfn returns a BlockEpochEvent which subscribes the
|
2017-02-21 03:31:16 +03:00
|
|
|
// caller to receive notifications, of each new block connected to the main
|
2016-06-21 07:31:05 +03:00
|
|
|
// chain.
|
2016-09-08 21:27:07 +03:00
|
|
|
func (b *BtcdNotifier) RegisterBlockEpochNtfn() (*chainntnfs.BlockEpochEvent, error) {
|
|
|
|
registration := &blockEpochRegistration{
|
2017-05-06 01:53:09 +03:00
|
|
|
epochChan: make(chan *chainntnfs.BlockEpoch, 20),
|
|
|
|
cancelChan: make(chan struct{}),
|
|
|
|
epochID: atomic.AddUint64(&b.epochClientCounter, 1),
|
2016-09-08 21:27:07 +03:00
|
|
|
}
|
|
|
|
|
2016-12-15 12:07:12 +03:00
|
|
|
select {
|
|
|
|
case <-b.quit:
|
|
|
|
return nil, errors.New("chainntnfs: system interrupt while " +
|
|
|
|
"attempting to register for block epoch notification.")
|
|
|
|
case b.notificationRegistry <- registration:
|
|
|
|
return &chainntnfs.BlockEpochEvent{
|
|
|
|
Epochs: registration.epochChan,
|
2017-02-21 03:31:16 +03:00
|
|
|
Cancel: func() {
|
2017-04-27 07:08:16 +03:00
|
|
|
cancel := &epochCancel{
|
2017-02-21 03:31:16 +03:00
|
|
|
epochID: registration.epochID,
|
2017-04-27 07:08:16 +03:00
|
|
|
}
|
|
|
|
|
2017-07-30 05:19:28 +03:00
|
|
|
// Submit epoch cancellation to notification dispatcher.
|
2017-04-27 07:08:16 +03:00
|
|
|
select {
|
|
|
|
case b.notificationCancels <- cancel:
|
2017-07-30 06:28:48 +03:00
|
|
|
// Cancellation is being handled, drain the epoch channel until it is
|
|
|
|
// closed before yielding to caller.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case _, ok := <-registration.epochChan:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-b.quit:
|
2017-08-02 03:14:01 +03:00
|
|
|
return
|
2017-07-30 06:28:48 +03:00
|
|
|
}
|
2017-04-27 07:08:16 +03:00
|
|
|
}
|
2017-02-21 03:31:16 +03:00
|
|
|
case <-b.quit:
|
|
|
|
}
|
|
|
|
},
|
2016-12-15 12:07:12 +03:00
|
|
|
}, nil
|
|
|
|
}
|
2016-06-21 07:31:05 +03:00
|
|
|
}
|