2017-05-22 04:35:27 +03:00
|
|
|
package chainview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/btcsuite/btcutil/gcs/builder"
|
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
2018-07-12 03:33:42 +03:00
|
|
|
"github.com/lightninglabs/neutrino"
|
2017-05-22 04:35:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// CfFilteredChainView is an implementation of the FilteredChainView interface
|
|
|
|
// which is supported by an underlying Bitcoin light client which supports
|
|
|
|
// client side filtering of Golomb Coded Sets. Rather than fetching all the
|
2018-02-07 06:11:11 +03:00
|
|
|
// blocks, the light client is able to query filters locally, to test if an
|
2017-06-06 05:44:54 +03:00
|
|
|
// item in a block modifies any of our watched set of UTXOs.
|
2017-05-22 04:35:27 +03:00
|
|
|
type CfFilteredChainView struct {
|
2018-06-01 01:41:41 +03:00
|
|
|
started int32 // To be used atomically.
|
|
|
|
stopped int32 // To be used atomically.
|
2017-05-22 04:35:27 +03:00
|
|
|
|
|
|
|
// p2pNode is a pointer to the running GCS-filter supported Bitcoin
|
|
|
|
// light clientl
|
|
|
|
p2pNode *neutrino.ChainService
|
|
|
|
|
|
|
|
// chainView is the active rescan which only watches our specified
|
|
|
|
// sub-set of the UTXO set.
|
2018-07-12 03:33:42 +03:00
|
|
|
chainView *neutrino.Rescan
|
2017-05-22 04:35:27 +03:00
|
|
|
|
|
|
|
// rescanErrChan is the channel that any errors encountered during the
|
|
|
|
// rescan will be sent over.
|
|
|
|
rescanErrChan <-chan error
|
|
|
|
|
2017-10-02 18:15:42 +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-22 04:35:27 +03:00
|
|
|
|
|
|
|
// chainFilter is the
|
|
|
|
filterMtx sync.RWMutex
|
2018-05-09 06:42:06 +03:00
|
|
|
chainFilter map[wire.OutPoint][]byte
|
2017-05-22 04:35:27 +03:00
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure CfFilteredChainView implements the
|
|
|
|
// chainview.FilteredChainView.
|
|
|
|
var _ FilteredChainView = (*CfFilteredChainView)(nil)
|
|
|
|
|
|
|
|
// NewCfFilteredChainView creates a new instance of the CfFilteredChainView
|
|
|
|
// which is connected to an active neutrino node.
|
|
|
|
//
|
2018-04-18 05:02:04 +03:00
|
|
|
// NOTE: The node should already be running and syncing before being passed into
|
2017-05-22 04:35:27 +03:00
|
|
|
// this function.
|
|
|
|
func NewCfFilteredChainView(node *neutrino.ChainService) (*CfFilteredChainView, error) {
|
|
|
|
return &CfFilteredChainView{
|
2017-10-02 18:15:42 +03:00
|
|
|
blockQueue: newBlockEventQueue(),
|
2017-05-22 04:35:27 +03:00
|
|
|
quit: make(chan struct{}),
|
|
|
|
rescanErrChan: make(chan error),
|
2018-05-09 06:42:06 +03:00
|
|
|
chainFilter: make(map[wire.OutPoint][]byte),
|
2017-05-22 04:35:27 +03:00
|
|
|
p2pNode: node,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start kicks off the FilteredChainView implementation. This function must be
|
|
|
|
// called before any calls to UpdateFilter can be processed.
|
2017-05-24 04:13:45 +03:00
|
|
|
//
|
2017-05-22 04:35:27 +03:00
|
|
|
// NOTE: This is part of the FilteredChainView interface.
|
|
|
|
func (c *CfFilteredChainView) Start() error {
|
|
|
|
// Already started?
|
|
|
|
if atomic.AddInt32(&c.started, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("FilteredChainView starting")
|
|
|
|
|
|
|
|
// First, we'll obtain the latest block height of the p2p node. We'll
|
|
|
|
// start the auto-rescan from this point. Once a caller actually wishes
|
|
|
|
// to register a chain view, the rescan state will be rewound
|
|
|
|
// accordingly.
|
2017-06-05 07:06:52 +03:00
|
|
|
bestHeader, bestHeight, err := c.p2pNode.BlockHeaders.ChainTip()
|
2017-05-22 04:35:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
startingPoint := &waddrmgr.BlockStamp{
|
|
|
|
Height: int32(bestHeight),
|
|
|
|
Hash: bestHeader.BlockHash(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll create our set of rescan options. Currently it's
|
2018-04-18 05:02:04 +03:00
|
|
|
// required that an user MUST set a addr/outpoint/txid when creating a
|
2017-05-22 04:35:27 +03:00
|
|
|
// rescan. To get around this, we'll add a "zero" outpoint, that won't
|
|
|
|
// actually be matched.
|
|
|
|
var zeroPoint wire.OutPoint
|
|
|
|
rescanOptions := []neutrino.RescanOption{
|
|
|
|
neutrino.StartBlock(startingPoint),
|
|
|
|
neutrino.QuitChan(c.quit),
|
|
|
|
neutrino.NotificationHandlers(
|
2017-08-25 04:54:17 +03:00
|
|
|
rpcclient.NotificationHandlers{
|
2017-05-22 04:35:27 +03:00
|
|
|
OnFilteredBlockConnected: c.onFilteredBlockConnected,
|
|
|
|
OnFilteredBlockDisconnected: c.onFilteredBlockDisconnected,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
neutrino.WatchOutPoints(zeroPoint),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we'll create our rescan struct, start it, and launch all
|
|
|
|
// the goroutines we need to operate this FilteredChainView instance.
|
|
|
|
c.chainView = c.p2pNode.NewRescan(rescanOptions...)
|
|
|
|
c.rescanErrChan = c.chainView.Start()
|
|
|
|
|
2017-10-02 18:15:42 +03:00
|
|
|
c.blockQueue.Start()
|
|
|
|
|
2017-05-22 04:35:27 +03:00
|
|
|
c.wg.Add(1)
|
|
|
|
go c.chainFilterer()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals all active goroutines for a graceful shutdown.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the FilteredChainView interface.
|
|
|
|
func (c *CfFilteredChainView) Stop() error {
|
|
|
|
// Already shutting down?
|
|
|
|
if atomic.AddInt32(&c.stopped, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("FilteredChainView stopping")
|
|
|
|
|
|
|
|
close(c.quit)
|
2017-10-02 18:15:42 +03:00
|
|
|
c.blockQueue.Stop()
|
2017-05-22 04:35:27 +03:00
|
|
|
c.wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (c *CfFilteredChainView) onFilteredBlockConnected(height int32,
|
|
|
|
header *wire.BlockHeader, txns []*btcutil.Tx) {
|
|
|
|
|
|
|
|
mtxs := make([]*wire.MsgTx, len(txns))
|
|
|
|
for i, tx := range txns {
|
|
|
|
mtx := tx.MsgTx()
|
|
|
|
mtxs[i] = mtx
|
|
|
|
|
|
|
|
for _, txIn := range mtx.TxIn {
|
|
|
|
c.filterMtx.Lock()
|
|
|
|
delete(c.chainFilter, txIn.PreviousOutPoint)
|
|
|
|
c.filterMtx.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-02 18:15:42 +03:00
|
|
|
block := &FilteredBlock{
|
|
|
|
Hash: header.BlockHash(),
|
|
|
|
Height: uint32(height),
|
|
|
|
Transactions: mtxs,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.blockQueue.Add(&blockEvent{
|
|
|
|
eventType: connected,
|
|
|
|
block: block,
|
|
|
|
})
|
2017-05-22 04:35:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// onFilteredBlockDisconnected is a callback which is executed once a block is
|
|
|
|
// disconnected from the end of the main chain.
|
|
|
|
func (c *CfFilteredChainView) onFilteredBlockDisconnected(height int32,
|
|
|
|
header *wire.BlockHeader) {
|
|
|
|
|
2017-10-02 18:15:42 +03:00
|
|
|
log.Debugf("got disconnected block at height %d: %v", height,
|
|
|
|
header.BlockHash())
|
|
|
|
|
2017-09-28 06:13:02 +03:00
|
|
|
filteredBlock := &FilteredBlock{
|
|
|
|
Hash: header.BlockHash(),
|
|
|
|
Height: uint32(height),
|
|
|
|
}
|
|
|
|
|
2017-10-02 18:15:42 +03:00
|
|
|
c.blockQueue.Add(&blockEvent{
|
|
|
|
eventType: disconnected,
|
|
|
|
block: filteredBlock,
|
|
|
|
})
|
2017-05-22 04:35:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// chainFilterer is the primary coordination goroutine within the
|
2017-10-02 18:15:42 +03:00
|
|
|
// CfFilteredChainView. This goroutine handles errors from the running rescan.
|
2017-05-22 04:35:27 +03:00
|
|
|
func (c *CfFilteredChainView) chainFilterer() {
|
|
|
|
defer c.wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-c.rescanErrChan:
|
|
|
|
log.Errorf("Error encountered during rescan: %v", err)
|
|
|
|
case <-c.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterBlock takes a block hash, and returns a FilteredBlocks which is the
|
|
|
|
// result of applying the current registered UTXO sub-set on the block
|
2017-05-24 04:13:45 +03:00
|
|
|
// corresponding to that block hash. If any watched UTXO's are spent by the
|
2017-05-22 04:35:27 +03:00
|
|
|
// selected lock, then the internal chainFilter will also be updated.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the FilteredChainView interface.
|
|
|
|
func (c *CfFilteredChainView) FilterBlock(blockHash *chainhash.Hash) (*FilteredBlock, error) {
|
2017-06-09 07:56:00 +03:00
|
|
|
// First, we'll fetch the block header itself so we can obtain the
|
|
|
|
// height which is part of our return value.
|
|
|
|
_, blockHeight, err := c.p2pNode.BlockHeaders.FetchHeader(blockHash)
|
2017-05-22 04:35:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-09 07:56:00 +03:00
|
|
|
filteredBlock := &FilteredBlock{
|
|
|
|
Hash: *blockHash,
|
|
|
|
Height: blockHeight,
|
|
|
|
}
|
|
|
|
|
2017-06-09 09:10:46 +03:00
|
|
|
// If we don't have any items within our current chain filter, then we
|
|
|
|
// can exit early as we don't need to fetch the filter.
|
|
|
|
c.filterMtx.RLock()
|
2017-06-09 22:12:01 +03:00
|
|
|
if len(c.chainFilter) == 0 {
|
|
|
|
c.filterMtx.RUnlock()
|
2017-06-09 09:10:46 +03:00
|
|
|
return filteredBlock, nil
|
|
|
|
}
|
2017-06-09 22:12:01 +03:00
|
|
|
c.filterMtx.RUnlock()
|
2017-06-09 09:10:46 +03:00
|
|
|
|
2017-06-09 07:56:00 +03:00
|
|
|
// Next, using the block, hash, we'll fetch the compact filter for this
|
|
|
|
// block. We only require the regular filter as we're just looking for
|
|
|
|
// outpoint that have been spent.
|
2017-10-11 22:27:15 +03:00
|
|
|
filter, err := c.p2pNode.GetCFilter(*blockHash, wire.GCSFilterRegular)
|
2017-06-09 07:56:00 +03:00
|
|
|
if err != nil {
|
2017-06-09 09:10:46 +03:00
|
|
|
return nil, err
|
2017-06-09 07:56:00 +03:00
|
|
|
}
|
|
|
|
|
2018-05-04 01:21:56 +03:00
|
|
|
if filter == nil {
|
|
|
|
return nil, fmt.Errorf("Unable to fetch filter")
|
|
|
|
}
|
|
|
|
|
2017-06-09 07:56:00 +03:00
|
|
|
// Before we can match the filter, we'll need to map each item in our
|
|
|
|
// chain filter to the representation that included in the compact
|
|
|
|
// filters.
|
|
|
|
c.filterMtx.RLock()
|
|
|
|
relevantPoints := make([][]byte, 0, len(c.chainFilter))
|
2018-05-09 06:42:06 +03:00
|
|
|
for _, filterEntry := range c.chainFilter {
|
|
|
|
relevantPoints = append(relevantPoints, filterEntry)
|
2017-06-09 07:56:00 +03:00
|
|
|
}
|
|
|
|
c.filterMtx.RUnlock()
|
|
|
|
|
|
|
|
// With our relevant points constructed, we can finally match against
|
|
|
|
// the retrieved filter.
|
|
|
|
matched, err := filter.MatchAny(builder.DeriveKey(blockHash),
|
|
|
|
relevantPoints)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there wasn't a match, then we'll return the filtered block as is
|
|
|
|
// (void of any transactions).
|
|
|
|
if !matched {
|
|
|
|
return filteredBlock, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach this point, then there was a match, so we'll need to
|
|
|
|
// fetch the block itself so we can scan it for any actual matches (as
|
|
|
|
// there's a fp rate).
|
|
|
|
block, err := c.p2pNode.GetBlockFromNetwork(*blockHash)
|
2017-05-22 04:35:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we'll step through the block, input by input, to see if any
|
|
|
|
// transactions spend any outputs from our watched sub-set of the UTXO
|
|
|
|
// set.
|
|
|
|
for _, tx := range block.Transactions() {
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
|
|
|
prevOp := txIn.PreviousOutPoint
|
2017-06-06 22:01:15 +03:00
|
|
|
|
|
|
|
c.filterMtx.RLock()
|
|
|
|
_, ok := c.chainFilter[prevOp]
|
|
|
|
c.filterMtx.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
2017-06-09 07:56:00 +03:00
|
|
|
filteredBlock.Transactions = append(
|
|
|
|
filteredBlock.Transactions,
|
|
|
|
tx.MsgTx(),
|
|
|
|
)
|
2017-05-22 04:35:27 +03:00
|
|
|
|
|
|
|
c.filterMtx.Lock()
|
|
|
|
delete(c.chainFilter, prevOp)
|
|
|
|
c.filterMtx.Unlock()
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 07:56:00 +03:00
|
|
|
return filteredBlock, nil
|
2017-05-22 04:35:27 +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.
|
2017-10-02 18:15:42 +03:00
|
|
|
func (c *CfFilteredChainView) UpdateFilter(ops []wire.OutPoint,
|
|
|
|
updateHeight uint32) error {
|
2017-11-07 02:55:14 +03:00
|
|
|
|
2017-10-02 18:15:42 +03:00
|
|
|
log.Debugf("Updating chain filter with new UTXO's: %v", ops)
|
|
|
|
|
2017-11-07 02:55:14 +03:00
|
|
|
// First, we'll update the current chain view, by adding any new
|
|
|
|
// UTXO's, ignoring duplicates in the process.
|
2017-10-02 18:15:42 +03:00
|
|
|
c.filterMtx.Lock()
|
|
|
|
for _, op := range ops {
|
2018-05-09 06:42:06 +03:00
|
|
|
c.chainFilter[op] = builder.OutPointToFilterEntry(op)
|
2017-06-09 22:12:01 +03:00
|
|
|
}
|
2017-10-02 18:15:42 +03:00
|
|
|
c.filterMtx.Unlock()
|
|
|
|
|
2017-11-07 02:55:14 +03:00
|
|
|
// With our internal chain view update, we'll craft a new update to the
|
|
|
|
// chainView which includes our new UTXO's, and current update height.
|
2017-10-02 18:15:42 +03:00
|
|
|
rescanUpdate := []neutrino.UpdateOption{
|
|
|
|
neutrino.AddOutPoints(ops...),
|
|
|
|
neutrino.Rewind(updateHeight),
|
|
|
|
neutrino.DisableDisconnectedNtfns(true),
|
2017-06-09 22:12:01 +03:00
|
|
|
}
|
2017-10-02 18:15:42 +03:00
|
|
|
err := c.chainView.Update(rescanUpdate...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to update rescan: %v", err)
|
2017-05-22 04:35:27 +03:00
|
|
|
}
|
2017-10-02 18:15:42 +03:00
|
|
|
return nil
|
2017-05-22 04:35:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (c *CfFilteredChainView) FilteredBlocks() <-chan *FilteredBlock {
|
2017-10-02 18:15:42 +03:00
|
|
|
return c.blockQueue.newBlocks
|
2017-05-22 04:35:27 +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 (c *CfFilteredChainView) DisconnectedBlocks() <-chan *FilteredBlock {
|
2017-10-02 18:15:42 +03:00
|
|
|
return c.blockQueue.staleBlocks
|
2017-05-22 04:35:27 +03:00
|
|
|
}
|