2016-01-07 00:03:17 +03:00
|
|
|
package btcdnotify
|
|
|
|
|
|
|
|
import (
|
2016-02-25 09:16:25 +03:00
|
|
|
"bytes"
|
2016-01-07 00:03:17 +03:00
|
|
|
"container/heap"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcwallet/chain"
|
2016-02-25 09:16:25 +03:00
|
|
|
btcwallet "github.com/btcsuite/btcwallet/wallet"
|
2016-01-07 00:03:17 +03:00
|
|
|
"github.com/btcsuite/btcwallet/wtxmgr"
|
2016-01-16 21:45:54 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntfs"
|
2016-01-07 00:03:17 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// BtcdNotifier...
|
|
|
|
type BtcdNotifier struct {
|
2016-02-17 01:46:18 +03:00
|
|
|
started int32 // To be used atomically
|
|
|
|
stopped int32 // To be used atomically
|
|
|
|
|
2016-02-25 09:16:25 +03:00
|
|
|
ntfnSource *btcwallet.NotificationServer
|
|
|
|
chainConn *chain.RPCClient
|
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
|
|
|
|
|
|
|
|
connectedBlocks <-chan wtxmgr.BlockMeta
|
|
|
|
disconnectedBlocks <-chan wtxmgr.BlockMeta
|
|
|
|
relevantTxs <-chan chain.RelevantTx
|
|
|
|
|
2016-02-17 01:46:18 +03:00
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan struct{}
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ chainntnfs.ChainNotifier = (*BtcdNotifier)(nil)
|
|
|
|
|
|
|
|
// NewBtcdNotifier...
|
2016-02-25 09:16:25 +03:00
|
|
|
// TODO(roasbeef): chain client + notification sever
|
|
|
|
// * use server for notifications
|
|
|
|
// * when asked for spent, request via client
|
|
|
|
func NewBtcdNotifier(ntfnSource *btcwallet.NotificationServer,
|
|
|
|
chainConn *chain.RPCClient) (*BtcdNotifier, error) {
|
|
|
|
|
2016-01-07 00:03:17 +03:00
|
|
|
return &BtcdNotifier{
|
2016-02-25 09:16:25 +03:00
|
|
|
ntfnSource: ntfnSource,
|
|
|
|
chainConn: chainConn,
|
|
|
|
|
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(),
|
|
|
|
|
|
|
|
connectedBlocks: make(chan wtxmgr.BlockMeta),
|
|
|
|
disconnectedBlocks: make(chan wtxmgr.BlockMeta),
|
|
|
|
relevantTxs: make(chan chain.RelevantTx),
|
|
|
|
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start...
|
|
|
|
func (b *BtcdNotifier) Start() error {
|
|
|
|
// Already started?
|
|
|
|
if atomic.AddInt32(&b.started, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b.wg.Add(1)
|
|
|
|
go b.notificationDispatcher()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop...
|
|
|
|
func (b *BtcdNotifier) Stop() error {
|
|
|
|
// Already shutting down?
|
|
|
|
if atomic.AddInt32(&b.stopped, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
close(b.quit)
|
|
|
|
b.wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// notificationDispatcher...
|
|
|
|
func (b *BtcdNotifier) notificationDispatcher() {
|
2016-02-25 09:16:25 +03:00
|
|
|
ntfnClient := b.ntfnSource.TransactionNotifications()
|
|
|
|
|
2016-01-07 00:03:17 +03:00
|
|
|
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:
|
|
|
|
b.confNotifications[*msg.txid] = msg
|
|
|
|
}
|
2016-02-25 09:16:25 +03:00
|
|
|
case txNtfn := <-ntfnClient.C:
|
|
|
|
// We're only concerned with newly mined blocks which
|
|
|
|
// may or may not include transactions we are interested
|
|
|
|
// in.
|
|
|
|
if txNtfn.AttachedBlocks == nil {
|
2016-01-07 00:03:17 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-02-25 09:16:25 +03:00
|
|
|
newBlocks := txNtfn.AttachedBlocks
|
|
|
|
for _, block := range newBlocks {
|
|
|
|
blockHeight := uint32(block.Height)
|
|
|
|
|
|
|
|
// Examine all transactions within the block
|
|
|
|
// in order to determine if this block includes a
|
|
|
|
// transactions spending one of the registered
|
|
|
|
// outpoints of interest.
|
|
|
|
for _, txSummary := range block.Transactions {
|
|
|
|
txBytes := bytes.NewReader(txSummary.Transaction)
|
|
|
|
tx := wire.NewMsgTx()
|
|
|
|
if err := tx.Deserialize(txBytes); err != nil {
|
|
|
|
// TODO(roasbeef): err
|
|
|
|
fmt.Println("unable to des tx: ", err)
|
|
|
|
continue
|
|
|
|
}
|
2016-01-07 00:03:17 +03:00
|
|
|
|
2016-02-25 09:16:25 +03:00
|
|
|
// 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.TxSha()
|
|
|
|
b.checkConfirmationTrigger(&txSha, blockHeight)
|
|
|
|
|
|
|
|
// Next, examine all the inputs spent, firing
|
|
|
|
// of a notification if it spends any of the
|
|
|
|
// outpoints within the set of our registered
|
|
|
|
// outputs.
|
|
|
|
b.checkSpendTrigger(tx)
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
2016-02-25 09:16:25 +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(blockHeight)
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): re-orgs
|
|
|
|
// * second channel to notify of confirmation decrementing
|
|
|
|
// re-org?
|
2016-02-17 01:46:18 +03:00
|
|
|
// * notify of negative confirmations
|
2016-02-25 09:16:25 +03:00
|
|
|
fmt.Println(txNtfn.DetachedBlocks)
|
2016-01-07 00:03:17 +03:00
|
|
|
case <-b.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 09:16:25 +03:00
|
|
|
// notifyConfs...
|
|
|
|
func (b *BtcdNotifier) notifyConfs(newBlockHeight uint32) {
|
|
|
|
// 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)
|
|
|
|
for nextConf.triggerHeight <= newBlockHeight {
|
|
|
|
nextConf.finConf <- struct{}{}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkSpendTrigger...
|
|
|
|
func (b *BtcdNotifier) checkSpendTrigger(tx *wire.MsgTx) {
|
|
|
|
// First, check if this transaction spends an output
|
|
|
|
// that has an existing spend notification for it.
|
|
|
|
for i, txIn := range tx.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 := tx.TxSha()
|
|
|
|
spendDetails := &chainntnfs.SpendDetail{
|
|
|
|
SpentOutPoint: ntfn.targetOutpoint,
|
|
|
|
SpenderTxHash: &spenderSha,
|
|
|
|
// TODO(roasbeef): copy tx?
|
|
|
|
SpendingTx: tx,
|
|
|
|
SpenderInputIndex: uint32(i),
|
|
|
|
}
|
|
|
|
|
|
|
|
ntfn.spendChan <- spendDetails
|
|
|
|
delete(b.spendNotifications, prevOut)
|
|
|
|
}
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
2016-02-25 09:16:25 +03:00
|
|
|
}
|
2016-01-07 00:03:17 +03:00
|
|
|
|
2016-02-25 09:16:25 +03:00
|
|
|
// checkConfirmationTrigger...
|
|
|
|
func (b *BtcdNotifier) checkConfirmationTrigger(txSha *wire.ShaHash,
|
|
|
|
blockHeight uint32) {
|
|
|
|
// 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 {
|
|
|
|
if confNtfn.numConfirmations == 1 {
|
|
|
|
confNtfn.finConf <- struct{}{}
|
|
|
|
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.
|
|
|
|
confNtfn.initialConfirmHeight = blockHeight
|
|
|
|
heapEntry := &confEntry{
|
|
|
|
confNtfn,
|
|
|
|
confNtfn.initialConfirmHeight + confNtfn.numConfirmations,
|
|
|
|
}
|
|
|
|
heap.Push(b.confHeap, heapEntry)
|
|
|
|
}
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// spendNotification....
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterSpendNotification...
|
|
|
|
// NOTE: eventChan MUST be buffered
|
2016-02-17 01:46:18 +03:00
|
|
|
func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint) (*chainntnfs.SpendEvent, error) {
|
2016-01-07 00:03:17 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): also register with rpc client? bool?
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// confirmationNotification...
|
|
|
|
// TODO(roasbeef): re-org funny business
|
|
|
|
type confirmationsNotification struct {
|
|
|
|
txid *wire.ShaHash
|
|
|
|
|
|
|
|
initialConfirmHeight uint32
|
|
|
|
numConfirmations uint32
|
|
|
|
|
|
|
|
finConf chan struct{}
|
|
|
|
negativeConf chan uint32
|
2016-01-07 00:03:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterConfirmationsNotification...
|
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-02-17 01:46:18 +03:00
|
|
|
finConf: make(chan struct{}, 1),
|
|
|
|
negativeConf: make(chan uint32, 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
|
|
|
}
|