2017-05-11 03:12:32 +03:00
|
|
|
package chainview
|
|
|
|
|
|
|
|
import (
|
2017-10-02 18:13:39 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
2017-05-11 03:12:32 +03:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcjson"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2017-05-11 03:12:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// BtcdFilteredChainView is an implementation of the FilteredChainView
|
|
|
|
// interface which is backed by an active websockets connection to btcd.
|
|
|
|
type BtcdFilteredChainView struct {
|
2018-06-01 01:41:41 +03:00
|
|
|
started int32 // To be used atomically.
|
|
|
|
stopped int32 // To be used atomically.
|
2017-05-11 03:12:32 +03:00
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
// bestHeight is the height of the latest block added to the
|
|
|
|
// blockQueue from the onFilteredConnectedMethod. It is used to
|
|
|
|
// determine up to what height we would need to rescan in case
|
|
|
|
// of a filter update.
|
|
|
|
bestHeightMtx sync.Mutex
|
|
|
|
bestHeight uint32
|
2017-05-11 03:12:32 +03:00
|
|
|
|
2017-08-25 04:54:17 +03:00
|
|
|
btcdConn *rpcclient.Client
|
2017-05-11 03:12:32 +03:00
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
// blockEventQueue is the ordered queue used to keep the order
|
|
|
|
// of connected and disconnected blocks sent to the reader of the
|
|
|
|
// chainView.
|
|
|
|
blockQueue *blockEventQueue
|
2017-05-11 03:12:32 +03:00
|
|
|
|
|
|
|
// filterUpdates is a channel in which updates to the utxo filter
|
|
|
|
// attached to this instance are sent over.
|
|
|
|
filterUpdates chan filterUpdate
|
|
|
|
|
|
|
|
// chainFilter is the set of utox's that we're currently watching
|
|
|
|
// spends for within the chain.
|
2017-10-02 18:13:39 +03:00
|
|
|
filterMtx sync.RWMutex
|
2017-05-11 03:12:32 +03:00
|
|
|
chainFilter map[wire.OutPoint]struct{}
|
|
|
|
|
|
|
|
// filterBlockReqs is a channel in which requests to filter select
|
|
|
|
// blocks will be sent over.
|
|
|
|
filterBlockReqs chan *filterBlockReq
|
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure BtcdFilteredChainView implements the
|
|
|
|
// chainview.FilteredChainView.
|
|
|
|
var _ FilteredChainView = (*BtcdFilteredChainView)(nil)
|
|
|
|
|
|
|
|
// NewBtcdFilteredChainView creates a new instance of a FilteredChainView from
|
|
|
|
// RPC credentials for an active btcd instance.
|
2017-08-25 04:54:17 +03:00
|
|
|
func NewBtcdFilteredChainView(config rpcclient.ConnConfig) (*BtcdFilteredChainView, error) {
|
2017-05-11 03:12:32 +03:00
|
|
|
chainView := &BtcdFilteredChainView{
|
2017-10-02 18:13:39 +03:00
|
|
|
chainFilter: make(map[wire.OutPoint]struct{}),
|
|
|
|
filterUpdates: make(chan filterUpdate),
|
|
|
|
filterBlockReqs: make(chan *filterBlockReq),
|
|
|
|
quit: make(chan struct{}),
|
2017-05-11 03:12:32 +03:00
|
|
|
}
|
|
|
|
|
2017-08-25 04:54:17 +03:00
|
|
|
ntfnCallbacks := &rpcclient.NotificationHandlers{
|
2017-10-02 18:13:39 +03:00
|
|
|
OnFilteredBlockConnected: chainView.onFilteredBlockConnected,
|
|
|
|
OnFilteredBlockDisconnected: chainView.onFilteredBlockDisconnected,
|
2017-05-11 03:12:32 +03:00
|
|
|
}
|
|
|
|
|
2017-08-25 04:54:17 +03:00
|
|
|
// Disable connecting to btcd within the rpcclient.New method. We
|
2017-05-11 03:12:32 +03:00
|
|
|
// defer establishing the connection to our .Start() method.
|
|
|
|
config.DisableConnectOnNew = true
|
|
|
|
config.DisableAutoReconnect = false
|
2017-08-25 04:54:17 +03:00
|
|
|
chainConn, err := rpcclient.New(&config, ntfnCallbacks)
|
2017-05-11 03:12:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chainView.btcdConn = chainConn
|
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
chainView.blockQueue = newBlockEventQueue()
|
|
|
|
|
2017-05-11 03:12:32 +03:00
|
|
|
return chainView, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts all goroutines necessary for normal operation.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the FilteredChainView interface.
|
|
|
|
func (b *BtcdFilteredChainView) Start() error {
|
|
|
|
// Already started?
|
|
|
|
if atomic.AddInt32(&b.started, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("FilteredChainView starting")
|
|
|
|
|
|
|
|
// Connect to btcd, and register for notifications on connected, and
|
|
|
|
// disconnected blocks.
|
|
|
|
if err := b.btcdConn.Connect(20); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.btcdConn.NotifyBlocks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
_, bestHeight, err := b.btcdConn.GetBestBlock()
|
2017-05-11 03:12:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
b.bestHeightMtx.Lock()
|
|
|
|
b.bestHeight = uint32(bestHeight)
|
|
|
|
b.bestHeightMtx.Unlock()
|
|
|
|
|
|
|
|
b.blockQueue.Start()
|
2017-05-11 03:12:32 +03:00
|
|
|
|
|
|
|
b.wg.Add(1)
|
|
|
|
go b.chainFilterer()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops all goroutines which we launched by the prior call to the Start
|
|
|
|
// method.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the FilteredChainView interface.
|
|
|
|
func (b *BtcdFilteredChainView) Stop() error {
|
|
|
|
// Already shutting down?
|
|
|
|
if atomic.AddInt32(&b.stopped, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown the rpc client, this gracefully disconnects from btcd, and
|
|
|
|
// cleans up all related resources.
|
|
|
|
b.btcdConn.Shutdown()
|
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
b.blockQueue.Stop()
|
|
|
|
|
2017-05-11 03:12:32 +03:00
|
|
|
log.Infof("FilteredChainView stopping")
|
|
|
|
|
|
|
|
close(b.quit)
|
|
|
|
b.wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
// onFilteredBlockConnected is called for each block that's connected to the
|
|
|
|
// end of the main chain. Based on our current chain filter, the block may or
|
|
|
|
// may not include any relevant transactions.
|
|
|
|
func (b *BtcdFilteredChainView) onFilteredBlockConnected(height int32,
|
|
|
|
header *wire.BlockHeader, txns []*btcutil.Tx) {
|
|
|
|
|
|
|
|
mtxs := make([]*wire.MsgTx, len(txns))
|
2018-07-06 06:11:06 +03:00
|
|
|
b.filterMtx.Lock()
|
2017-10-02 18:13:39 +03:00
|
|
|
for i, tx := range txns {
|
|
|
|
mtx := tx.MsgTx()
|
|
|
|
mtxs[i] = mtx
|
|
|
|
|
|
|
|
for _, txIn := range mtx.TxIn {
|
|
|
|
// We can delete this outpoint from the chainFilter, as
|
|
|
|
// we just received a block where it was spent. In case
|
|
|
|
// of a reorg, this outpoint might get "un-spent", but
|
|
|
|
// that's okay since it would never be wise to consider
|
|
|
|
// the channel open again (since a spending transaction
|
|
|
|
// exists on the network).
|
|
|
|
delete(b.chainFilter, txIn.PreviousOutPoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-07-06 06:11:06 +03:00
|
|
|
b.filterMtx.Unlock()
|
2017-05-11 03:12:32 +03:00
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
// We record the height of the last connected block added to the
|
|
|
|
// blockQueue such that we can scan up to this height in case of
|
|
|
|
// a rescan. It must be protected by a mutex since a filter update
|
|
|
|
// might be trying to read it concurrently.
|
|
|
|
b.bestHeightMtx.Lock()
|
|
|
|
b.bestHeight = uint32(height)
|
|
|
|
b.bestHeightMtx.Unlock()
|
|
|
|
|
|
|
|
block := &FilteredBlock{
|
|
|
|
Hash: header.BlockHash(),
|
|
|
|
Height: uint32(height),
|
|
|
|
Transactions: mtxs,
|
|
|
|
}
|
|
|
|
|
|
|
|
b.blockQueue.Add(&blockEvent{
|
|
|
|
eventType: connected,
|
|
|
|
block: block,
|
|
|
|
})
|
2017-05-11 03:12:32 +03:00
|
|
|
}
|
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
// onFilteredBlockDisconnected is a callback which is executed once a block is
|
|
|
|
// disconnected from the end of the main chain.
|
|
|
|
func (b *BtcdFilteredChainView) onFilteredBlockDisconnected(height int32,
|
|
|
|
header *wire.BlockHeader) {
|
|
|
|
|
|
|
|
log.Debugf("got disconnected block at height %d: %v", height,
|
|
|
|
header.BlockHash())
|
|
|
|
|
|
|
|
filteredBlock := &FilteredBlock{
|
|
|
|
Hash: header.BlockHash(),
|
|
|
|
Height: uint32(height),
|
|
|
|
}
|
2017-05-11 03:12:32 +03:00
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
b.blockQueue.Add(&blockEvent{
|
|
|
|
eventType: disconnected,
|
|
|
|
block: filteredBlock,
|
|
|
|
})
|
2017-05-11 03:12:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// filterBlockReq houses a request to manually filter a block specified by
|
|
|
|
// block hash.
|
|
|
|
type filterBlockReq struct {
|
|
|
|
blockHash *chainhash.Hash
|
|
|
|
resp chan *FilteredBlock
|
|
|
|
err chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterBlock takes a block hash, and returns a FilteredBlocks which is the
|
|
|
|
// result of applying the current registered UTXO sub-set on the block
|
|
|
|
// corresponding to that block hash. If any watched UTOX's are spent by the
|
|
|
|
// selected lock, then the internal chainFilter will also be updated.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the FilteredChainView interface.
|
|
|
|
func (b *BtcdFilteredChainView) FilterBlock(blockHash *chainhash.Hash) (*FilteredBlock, error) {
|
|
|
|
req := &filterBlockReq{
|
|
|
|
blockHash: blockHash,
|
|
|
|
resp: make(chan *FilteredBlock, 1),
|
|
|
|
err: make(chan error, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case b.filterBlockReqs <- req:
|
|
|
|
case <-b.quit:
|
|
|
|
return nil, fmt.Errorf("FilteredChainView shutting down")
|
|
|
|
}
|
|
|
|
|
|
|
|
return <-req.resp, <-req.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// chainFilterer is the primary goroutine which: listens for new blocks coming
|
2018-02-07 06:11:11 +03:00
|
|
|
// and dispatches the relevant FilteredBlock notifications, updates the filter
|
2017-05-11 03:12:32 +03:00
|
|
|
// due to requests by callers, and finally is able to preform targeted block
|
|
|
|
// filtration.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): change to use loadfilter RPC's
|
|
|
|
func (b *BtcdFilteredChainView) chainFilterer() {
|
|
|
|
defer b.wg.Done()
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// filterBlock is a helper function that scans the given block, and
|
2017-05-11 03:12:32 +03:00
|
|
|
// notes which transactions spend outputs which are currently being
|
|
|
|
// watched. Additionally, the chain filter will also be updated by
|
|
|
|
// removing any spent outputs.
|
|
|
|
filterBlock := func(blk *wire.MsgBlock) []*wire.MsgTx {
|
2018-07-06 06:11:06 +03:00
|
|
|
b.filterMtx.Lock()
|
|
|
|
defer b.filterMtx.Unlock()
|
|
|
|
|
2017-05-11 03:12:32 +03:00
|
|
|
var filteredTxns []*wire.MsgTx
|
|
|
|
for _, tx := range blk.Transactions {
|
2018-07-06 06:11:06 +03:00
|
|
|
var txAlreadyFiltered bool
|
2017-05-11 03:12:32 +03:00
|
|
|
for _, txIn := range tx.TxIn {
|
|
|
|
prevOp := txIn.PreviousOutPoint
|
2018-07-06 06:11:06 +03:00
|
|
|
if _, ok := b.chainFilter[prevOp]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
2017-05-11 03:12:32 +03:00
|
|
|
|
2018-07-06 06:11:06 +03:00
|
|
|
delete(b.chainFilter, prevOp)
|
2017-05-11 03:12:32 +03:00
|
|
|
|
2018-07-06 06:11:06 +03:00
|
|
|
// Only add this txn to our list of filtered
|
|
|
|
// txns if it is the first previous outpoint to
|
|
|
|
// cause a match.
|
|
|
|
if txAlreadyFiltered {
|
|
|
|
continue
|
2017-05-11 03:12:32 +03:00
|
|
|
}
|
2018-07-06 06:11:06 +03:00
|
|
|
|
|
|
|
filteredTxns = append(filteredTxns, tx)
|
|
|
|
txAlreadyFiltered = true
|
|
|
|
|
2017-05-11 03:12:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredTxns
|
|
|
|
}
|
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
decodeJSONBlock := func(block *btcjson.RescannedBlock,
|
|
|
|
height uint32) (*FilteredBlock, error) {
|
|
|
|
hash, err := chainhash.NewHashFromStr(block.Hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-11 03:12:32 +03:00
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
}
|
|
|
|
txs := make([]*wire.MsgTx, 0, len(block.Transactions))
|
|
|
|
for _, str := range block.Transactions {
|
|
|
|
b, err := hex.DecodeString(str)
|
2017-05-11 03:12:32 +03:00
|
|
|
if err != nil {
|
2017-10-02 18:13:39 +03:00
|
|
|
return nil, err
|
2017-05-11 03:12:32 +03:00
|
|
|
}
|
2017-10-02 18:13:39 +03:00
|
|
|
tx := &wire.MsgTx{}
|
|
|
|
err = tx.Deserialize(bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
txs = append(txs, tx)
|
|
|
|
}
|
|
|
|
return &FilteredBlock{
|
|
|
|
Hash: *hash,
|
|
|
|
Height: height,
|
|
|
|
Transactions: txs,
|
|
|
|
}, nil
|
|
|
|
}
|
2017-05-11 03:12:32 +03:00
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
for {
|
|
|
|
select {
|
2017-05-11 03:12:32 +03:00
|
|
|
// The caller has just sent an update to the current chain
|
|
|
|
// filter, so we'll apply the update, possibly rewinding our
|
|
|
|
// state partially.
|
|
|
|
case update := <-b.filterUpdates:
|
2017-10-02 18:13:39 +03:00
|
|
|
|
2017-05-11 03:12:32 +03:00
|
|
|
// First, we'll add all the new UTXO's to the set of
|
|
|
|
// watched UTXO's, eliminating any duplicates in the
|
|
|
|
// process.
|
|
|
|
log.Debugf("Updating chain filter with new UTXO's: %v",
|
|
|
|
update.newUtxos)
|
2018-07-06 06:11:06 +03:00
|
|
|
|
|
|
|
b.filterMtx.Lock()
|
2017-05-11 03:12:32 +03:00
|
|
|
for _, newOp := range update.newUtxos {
|
|
|
|
b.chainFilter[newOp] = struct{}{}
|
|
|
|
}
|
2018-07-06 06:11:06 +03:00
|
|
|
b.filterMtx.Unlock()
|
2017-05-11 03:12:32 +03:00
|
|
|
|
2017-10-02 18:13:39 +03:00
|
|
|
// Apply the new TX filter to btcd, which will cause
|
|
|
|
// all following notifications from and calls to it
|
|
|
|
// return blocks filtered with the new filter.
|
|
|
|
b.btcdConn.LoadTxFilter(false, []btcutil.Address{},
|
|
|
|
update.newUtxos)
|
|
|
|
|
|
|
|
// All blocks gotten after we loaded the filter will
|
|
|
|
// have the filter applied, but we will need to rescan
|
|
|
|
// the blocks up to the height of the block we last
|
|
|
|
// added to the blockQueue.
|
|
|
|
b.bestHeightMtx.Lock()
|
|
|
|
bestHeight := b.bestHeight
|
|
|
|
b.bestHeightMtx.Unlock()
|
|
|
|
|
2017-05-11 03:12:32 +03:00
|
|
|
// If the update height matches our best known height,
|
|
|
|
// then we don't need to do any rewinding.
|
2017-10-02 18:13:39 +03:00
|
|
|
if update.updateHeight == bestHeight {
|
2017-05-11 03:12:32 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll rewind the state to ensure the
|
|
|
|
// caller doesn't miss any relevant notifications.
|
|
|
|
// Starting from the height _after_ the update height,
|
2017-10-02 18:13:39 +03:00
|
|
|
// we'll walk forwards, rescanning one block at a time
|
|
|
|
// with btcd applying the newly loaded filter to each
|
|
|
|
// block.
|
|
|
|
for i := update.updateHeight + 1; i < bestHeight+1; i++ {
|
2017-05-11 03:12:32 +03:00
|
|
|
blockHash, err := b.btcdConn.GetBlockHash(int64(i))
|
|
|
|
if err != nil {
|
2017-10-02 18:13:39 +03:00
|
|
|
log.Warnf("Unable to get block hash "+
|
|
|
|
"for block at height %d: %v",
|
|
|
|
i, err)
|
2017-05-11 03:12:32 +03:00
|
|
|
continue
|
|
|
|
}
|
2017-10-02 18:13:39 +03:00
|
|
|
|
|
|
|
// To avoid dealing with the case where a reorg
|
|
|
|
// is happening while we rescan, we scan one
|
|
|
|
// block at a time, skipping blocks that might
|
|
|
|
// have gone missing.
|
|
|
|
rescanned, err := b.btcdConn.RescanBlocks(
|
|
|
|
[]chainhash.Hash{*blockHash})
|
2017-05-11 03:12:32 +03:00
|
|
|
if err != nil {
|
2017-10-02 18:13:39 +03:00
|
|
|
log.Warnf("Unable to rescan block "+
|
|
|
|
"with hash %v at height %d: %v",
|
|
|
|
blockHash, i, err)
|
2017-05-11 03:12:32 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-11 03:59:37 +03:00
|
|
|
// If no block was returned from the rescan, it
|
|
|
|
// means no matching transactions were found.
|
2017-10-02 18:13:39 +03:00
|
|
|
if len(rescanned) != 1 {
|
2017-11-11 03:59:37 +03:00
|
|
|
log.Tracef("rescan of block %v at "+
|
|
|
|
"height=%d yielded no "+
|
|
|
|
"transactions", blockHash, i)
|
2017-10-02 18:13:39 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
decoded, err := decodeJSONBlock(
|
|
|
|
&rescanned[0], uint32(i))
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to decode block: %v",
|
|
|
|
err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b.blockQueue.Add(&blockEvent{
|
|
|
|
eventType: connected,
|
|
|
|
block: decoded,
|
|
|
|
})
|
2017-05-11 03:12:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We've received a new request to manually filter a block.
|
|
|
|
case req := <-b.filterBlockReqs:
|
|
|
|
// First we'll fetch the block itself as well as some
|
|
|
|
// additional information including its height.
|
|
|
|
block, err := b.btcdConn.GetBlock(req.blockHash)
|
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
req.resp <- nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
header, err := b.btcdConn.GetBlockHeaderVerbose(req.blockHash)
|
|
|
|
if err != nil {
|
|
|
|
req.err <- err
|
|
|
|
req.resp <- nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once we have this info, we can directly filter the
|
|
|
|
// block and dispatch the proper notification.
|
|
|
|
req.resp <- &FilteredBlock{
|
|
|
|
Hash: *req.blockHash,
|
|
|
|
Height: uint32(header.Height),
|
|
|
|
Transactions: filterBlock(block),
|
|
|
|
}
|
|
|
|
req.err <- err
|
|
|
|
|
|
|
|
case <-b.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterUpdate is a message sent to the chainFilterer to update the current
|
|
|
|
// chainFilter state.
|
|
|
|
type filterUpdate struct {
|
|
|
|
newUtxos []wire.OutPoint
|
|
|
|
updateHeight uint32
|
2017-06-09 22:12:01 +03:00
|
|
|
done chan struct{}
|
2017-05-11 03:12:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateFilter updates the UTXO filter which is to be consulted when creating
|
|
|
|
// FilteredBlocks to be sent to subscribed clients. This method is cumulative
|
|
|
|
// meaning repeated calls to this method should _expand_ the size of the UTXO
|
|
|
|
// sub-set currently being watched. If the set updateHeight is _lower_ than
|
|
|
|
// the best known height of the implementation, then the state should be
|
|
|
|
// rewound to ensure all relevant notifications are dispatched.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the FilteredChainView interface.
|
|
|
|
func (b *BtcdFilteredChainView) UpdateFilter(ops []wire.OutPoint, updateHeight uint32) error {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case b.filterUpdates <- filterUpdate{
|
|
|
|
newUtxos: ops,
|
|
|
|
updateHeight: updateHeight,
|
|
|
|
}:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case <-b.quit:
|
|
|
|
return fmt.Errorf("chain filter shutting down")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilteredBlocks returns the channel that filtered blocks are to be sent over.
|
|
|
|
// Each time a block is connected to the end of a main chain, and appropriate
|
|
|
|
// FilteredBlock which contains the transactions which mutate our watched UTXO
|
|
|
|
// set is to be returned.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the FilteredChainView interface.
|
|
|
|
func (b *BtcdFilteredChainView) FilteredBlocks() <-chan *FilteredBlock {
|
2017-10-02 18:13:39 +03:00
|
|
|
return b.blockQueue.newBlocks
|
2017-05-11 03:12:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DisconnectedBlocks returns a receive only channel which will be sent upon
|
|
|
|
// with the empty filtered blocks of blocks which are disconnected from the
|
|
|
|
// main chain in the case of a re-org.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the FilteredChainView interface.
|
|
|
|
func (b *BtcdFilteredChainView) DisconnectedBlocks() <-chan *FilteredBlock {
|
2017-10-02 18:13:39 +03:00
|
|
|
return b.blockQueue.staleBlocks
|
2017-05-11 03:12:32 +03:00
|
|
|
}
|