2016-01-07 00:03:17 +03:00
|
|
|
package btcdnotify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2016-02-27 03:30:14 +03:00
|
|
|
"time"
|
2016-01-07 00:03:17 +03:00
|
|
|
|
2016-01-16 21:45:54 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntfs"
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcd/btcjson"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
"github.com/roasbeef/btcrpcclient"
|
|
|
|
"github.com/roasbeef/btcutil"
|
2016-01-07 00:03:17 +03:00
|
|
|
)
|
|
|
|
|
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 {
|
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
|
|
|
|
|
|
|
notificationRegistry chan interface{}
|
|
|
|
|
2016-02-17 01:46:18 +03:00
|
|
|
// TODO(roasbeef): make map point to slices? Would allow for multiple
|
|
|
|
// clients to listen for same spend. Would we ever need this?
|
2016-01-07 00:03:17 +03:00
|
|
|
spendNotifications map[wire.OutPoint]*spendNotification
|
|
|
|
confNotifications map[wire.ShaHash]*confirmationsNotification
|
|
|
|
confHeap *confirmationHeap
|
|
|
|
|
2016-02-27 03:30:14 +03:00
|
|
|
connectedBlockHashes chan *blockNtfn
|
|
|
|
disconnectedBlockHashes chan *blockNtfn
|
|
|
|
relevantTxs chan *btcutil.Tx
|
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-02-27 04:31:07 +03:00
|
|
|
// NewBtcdNotifier returns a new BtcdNotifier instance. This function assumes
|
|
|
|
// the btcd node detailed in the passed configuration is already running, and
|
|
|
|
// willing to accept new websockets clients.
|
2016-02-27 03:30:14 +03:00
|
|
|
func NewBtcdNotifier(config *btcrpcclient.ConnConfig) (*BtcdNotifier, error) {
|
|
|
|
notifier := &BtcdNotifier{
|
2016-01-07 00:03:17 +03:00
|
|
|
notificationRegistry: make(chan interface{}),
|
|
|
|
|
|
|
|
spendNotifications: make(map[wire.OutPoint]*spendNotification),
|
|
|
|
confNotifications: make(map[wire.ShaHash]*confirmationsNotification),
|
|
|
|
confHeap: newConfirmationHeap(),
|
|
|
|
|
2016-02-27 03:30:14 +03:00
|
|
|
connectedBlockHashes: make(chan *blockNtfn, 20),
|
|
|
|
disconnectedBlockHashes: make(chan *blockNtfn, 20),
|
|
|
|
relevantTxs: make(chan *btcutil.Tx, 100),
|
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-02-27 04:31:07 +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-01-07 00:03:17 +03:00
|
|
|
b.wg.Add(1)
|
|
|
|
go b.notificationDispatcher()
|
|
|
|
|
|
|
|
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.
|
|
|
|
for _, spendClient := range b.spendNotifications {
|
|
|
|
close(spendClient.spendChan)
|
|
|
|
}
|
|
|
|
for _, confClient := range b.confNotifications {
|
|
|
|
close(confClient.finConf)
|
|
|
|
close(confClient.negativeConf)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
sha *wire.ShaHash
|
|
|
|
height int32
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// onBlockConnected implements on OnBlockConnected callback for btcrpcclient.
|
2016-02-27 03:30:14 +03:00
|
|
|
func (b *BtcdNotifier) onBlockConnected(hash *wire.ShaHash, height int32, t time.Time) {
|
|
|
|
select {
|
|
|
|
case b.connectedBlockHashes <- &blockNtfn{hash, height}:
|
|
|
|
case <-b.quit:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// onBlockDisconnected implements on OnBlockDisconnected callback for btcrpcclient.
|
2016-02-27 03:30:14 +03:00
|
|
|
func (b *BtcdNotifier) onBlockDisconnected(hash *wire.ShaHash, height int32, t time.Time) {
|
|
|
|
b.onBlockDisconnected(hash, height, t)
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// onRedeemingTx implements on OnRedeemingTx callback for btcrpcclient.
|
2016-02-27 03:30:14 +03:00
|
|
|
func (b *BtcdNotifier) onRedeemingTx(transaction *btcutil.Tx, details *btcjson.BlockDetails) {
|
|
|
|
select {
|
|
|
|
case b.relevantTxs <- transaction:
|
|
|
|
case <-b.quit:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// notificationDispatcher is the primary goroutine which handles client
|
|
|
|
// notification registrations, as well as notification dispatches.
|
2016-01-07 00:03:17 +03:00
|
|
|
func (b *BtcdNotifier) notificationDispatcher() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case registerMsg := <-b.notificationRegistry:
|
|
|
|
switch msg := registerMsg.(type) {
|
|
|
|
case *spendNotification:
|
2016-02-17 01:46:18 +03:00
|
|
|
b.spendNotifications[*msg.targetOutpoint] = 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-01-07 00:03:17 +03:00
|
|
|
b.confNotifications[*msg.txid] = msg
|
|
|
|
}
|
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
|
|
|
|
fmt.Println(staleBlockHash)
|
|
|
|
case connectedBlock := <-b.connectedBlockHashes:
|
|
|
|
newBlock, err := b.chainConn.GetBlock(connectedBlock.sha)
|
|
|
|
if err != nil {
|
|
|
|
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",
|
|
|
|
connectedBlock.height, connectedBlock.sha)
|
|
|
|
|
2016-02-27 03:30:14 +03:00
|
|
|
newHeight := connectedBlock.height
|
|
|
|
for _, tx := range newBlock.Transactions() {
|
|
|
|
// Check if the inclusion of this transaction
|
|
|
|
// within a block by itself triggers a block
|
|
|
|
// confirmation threshold, if so send a
|
|
|
|
// notification. Otherwise, place the notification
|
|
|
|
// on a heap to be triggered in the future once
|
|
|
|
// additional confirmations are attained.
|
|
|
|
txSha := tx.Sha()
|
|
|
|
b.checkConfirmationTrigger(txSha, newHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
case newSpend := <-b.relevantTxs:
|
|
|
|
// First, check if this transaction spends an output
|
|
|
|
// that has an existing spend notification for it.
|
|
|
|
for i, txIn := range newSpend.MsgTx().TxIn {
|
|
|
|
prevOut := txIn.PreviousOutPoint
|
|
|
|
|
|
|
|
// If this transaction indeed does spend an
|
|
|
|
// output which we have a registered notification
|
|
|
|
// for, then create a spend summary, finally
|
|
|
|
// sending off the details to the notification
|
|
|
|
// subscriber.
|
|
|
|
if ntfn, ok := b.spendNotifications[prevOut]; ok {
|
|
|
|
spenderSha := newSpend.Sha()
|
|
|
|
spendDetails := &chainntnfs.SpendDetail{
|
|
|
|
SpentOutPoint: ntfn.targetOutpoint,
|
|
|
|
SpenderTxHash: spenderSha,
|
|
|
|
// TODO(roasbeef): copy tx?
|
|
|
|
SpendingTx: newSpend.MsgTx(),
|
|
|
|
SpenderInputIndex: uint32(i),
|
2016-02-25 09:16:25 +03:00
|
|
|
}
|
2016-01-07 00:03:17 +03:00
|
|
|
|
2016-02-27 03:30:14 +03:00
|
|
|
ntfn.spendChan <- spendDetails
|
|
|
|
delete(b.spendNotifications, prevOut)
|
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-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-06-21 07:33:41 +03:00
|
|
|
nextConf.finConf <- newBlockHeight
|
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?
|
2016-02-27 03:30:14 +03:00
|
|
|
func (b *BtcdNotifier) checkConfirmationTrigger(txSha *wire.ShaHash, blockHeight int32) {
|
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.
|
|
|
|
if confNtfn, ok := b.confNotifications[*txSha]; ok {
|
2016-02-27 03:30:14 +03:00
|
|
|
delete(b.confNotifications, *txSha)
|
2016-02-25 09:16:25 +03:00
|
|
|
if confNtfn.numConfirmations == 1 {
|
2016-06-21 07:33:41 +03:00
|
|
|
chainntnfs.Log.Infof("Dispatching single conf "+
|
|
|
|
"notification, sha=%v, height=%v", txSha,
|
|
|
|
blockHeight)
|
|
|
|
confNtfn.finConf <- blockHeight
|
2016-02-25 09:16:25 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-02-27 03:30:14 +03:00
|
|
|
confNtfn.initialConfirmHeight = uint32(blockHeight)
|
|
|
|
finalConfHeight := uint32(confNtfn.initialConfirmHeight + confNtfn.numConfirmations - 1)
|
2016-02-25 09:16:25 +03:00
|
|
|
heapEntry := &confEntry{
|
|
|
|
confNtfn,
|
2016-02-27 03:30:14 +03:00
|
|
|
finalConfHeight,
|
2016-02-25 09:16:25 +03:00
|
|
|
}
|
|
|
|
heap.Push(b.confHeap, heapEntry)
|
|
|
|
}
|
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
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// RegisterSpendNotification registers an intent to be notified once the target
|
|
|
|
// 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.
|
2016-02-17 01:46:18 +03:00
|
|
|
func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint) (*chainntnfs.SpendEvent, error) {
|
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),
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
b.notificationRegistry <- ntfn
|
|
|
|
|
2016-02-17 01:46:18 +03:00
|
|
|
return &chainntnfs.SpendEvent{ntfn.spendChan}, nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
txid *wire.ShaHash
|
|
|
|
|
|
|
|
initialConfirmHeight uint32
|
|
|
|
numConfirmations uint32
|
|
|
|
|
2016-06-21 07:33:41 +03:00
|
|
|
finConf chan int32
|
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
|
|
|
}
|
|
|
|
|
2016-02-27 04:31:07 +03:00
|
|
|
// RegisterConfirmationsNotification registers a notification with BtcdNotifier
|
|
|
|
// which will be triggered once the txid reaches numConfs number of
|
|
|
|
// confirmations.
|
2016-02-17 01:46:18 +03:00
|
|
|
func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *wire.ShaHash,
|
|
|
|
numConfs uint32) (*chainntnfs.ConfirmationEvent, error) {
|
2016-01-07 00:03:17 +03:00
|
|
|
|
|
|
|
ntfn := &confirmationsNotification{
|
|
|
|
txid: txid,
|
|
|
|
numConfirmations: numConfs,
|
2016-06-21 07:33:41 +03:00
|
|
|
finConf: make(chan int32, 1),
|
2016-02-27 03:30:14 +03:00
|
|
|
negativeConf: make(chan int32, 1),
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
b.notificationRegistry <- ntfn
|
|
|
|
|
2016-02-17 01:46:18 +03:00
|
|
|
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
|
|
|
|
|
|
|
// RegisterBlockEpochNtfn returns a BlockEpochEvent which subscribes the
|
|
|
|
// caller to receive notificationsm, of each new block connected to the main
|
|
|
|
// chain.
|
|
|
|
func (b *BtcdNotifier) RegisterBlockEpochNtfn(targetHeight int32) (*chainntnfs.BlockEpochEvent, error) {
|
|
|
|
// TODO(roasbeef): implement
|
|
|
|
return nil, nil
|
|
|
|
}
|