2017-01-10 15:38:15 +01:00
|
|
|
// Copyright (c) 2017 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package indexers
|
|
|
|
|
|
|
|
import (
|
2017-02-01 13:12:30 +01:00
|
|
|
"errors"
|
2017-02-02 03:51:23 +01:00
|
|
|
|
2017-01-10 15:38:15 +01:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2017-02-02 03:51:23 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2017-01-12 14:25:45 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2017-01-10 15:38:15 +01:00
|
|
|
"github.com/btcsuite/btcd/database"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2017-01-19 11:49:40 +01:00
|
|
|
"github.com/btcsuite/btcutil/gcs/builder"
|
2017-02-01 13:12:30 +01:00
|
|
|
"github.com/btcsuite/fastsha256"
|
2017-01-10 15:38:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-01-18 09:09:05 +01:00
|
|
|
// cfIndexName is the human-readable name for the index.
|
2017-01-19 14:20:41 +01:00
|
|
|
cfIndexName = "committed filter index"
|
2017-01-10 15:38:15 +01:00
|
|
|
)
|
|
|
|
|
2017-01-19 15:55:39 +01:00
|
|
|
// Committed filters come in two flavours: basic and extended. They are
|
|
|
|
// generated and dropped in pairs, and both are indexed by a block's hash.
|
|
|
|
// Besides holding different content, they also live in different buckets.
|
2017-01-10 15:38:15 +01:00
|
|
|
var (
|
2017-03-02 11:20:54 +01:00
|
|
|
// cfIndexParentBucketKey is the name of the parent bucket used to house
|
|
|
|
// the index. The rest of the buckets live below this bucket.
|
|
|
|
cfIndexParentBucketKey = []byte("cfindexparentbucket")
|
2017-01-19 14:20:41 +01:00
|
|
|
// cfBasicIndexKey is the name of the db bucket used to house the
|
2017-01-31 11:01:21 +01:00
|
|
|
// block hash -> basic cf index (cf#0).
|
2017-01-19 14:20:41 +01:00
|
|
|
cfBasicIndexKey = []byte("cf0byhashidx")
|
2017-01-31 11:01:21 +01:00
|
|
|
// cfBasicHeaderKey is the name of the db bucket used to house the
|
|
|
|
// block hash -> basic cf header index (cf#0).
|
|
|
|
cfBasicHeaderKey = []byte("cf0headerbyhashidx")
|
2017-01-19 14:20:41 +01:00
|
|
|
// cfExtendedIndexKey is the name of the db bucket used to house the
|
2017-01-31 11:01:21 +01:00
|
|
|
// block hash -> extended cf index (cf#1).
|
2017-01-19 14:20:41 +01:00
|
|
|
cfExtendedIndexKey = []byte("cf1byhashidx")
|
2017-01-31 11:01:21 +01:00
|
|
|
// cfExtendedHeaderKey is the name of the db bucket used to house the
|
|
|
|
// block hash -> extended cf header index (cf#1).
|
|
|
|
cfExtendedHeaderKey = []byte("cf1headerbyhashidx")
|
2017-01-10 15:38:15 +01:00
|
|
|
)
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// dbFetchFilter retrieves a block's basic or extended filter. A filter's
|
2017-02-01 10:50:36 +01:00
|
|
|
// absence is not considered an error.
|
2017-02-01 13:12:30 +01:00
|
|
|
func dbFetchFilter(dbTx database.Tx, key []byte, h *chainhash.Hash) ([]byte, error) {
|
2017-03-02 11:20:54 +01:00
|
|
|
idx := dbTx.Metadata().Bucket(cfIndexParentBucketKey).Bucket(key)
|
2017-02-01 10:50:36 +01:00
|
|
|
return idx.Get(h[:]), nil
|
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// dbFetchFilterHeader retrieves a block's basic or extended filter header.
|
2017-02-01 10:50:36 +01:00
|
|
|
// A filter's absence is not considered an error.
|
2017-02-01 13:12:30 +01:00
|
|
|
func dbFetchFilterHeader(dbTx database.Tx, key []byte, h *chainhash.Hash) ([]byte, error) {
|
2017-03-02 11:20:54 +01:00
|
|
|
idx := dbTx.Metadata().Bucket(cfIndexParentBucketKey).Bucket(key)
|
2017-02-01 13:12:30 +01:00
|
|
|
fh := idx.Get(h[:])
|
|
|
|
if len(fh) != fastsha256.Size {
|
|
|
|
return nil, errors.New("invalid filter header length")
|
|
|
|
}
|
|
|
|
return fh, nil
|
2017-02-01 10:50:36 +01:00
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// dbStoreFilter stores a block's basic or extended filter.
|
2017-02-01 13:12:30 +01:00
|
|
|
func dbStoreFilter(dbTx database.Tx, key []byte, h *chainhash.Hash, f []byte) error {
|
2017-03-02 11:20:54 +01:00
|
|
|
idx := dbTx.Metadata().Bucket(cfIndexParentBucketKey).Bucket(key)
|
2017-01-19 15:55:39 +01:00
|
|
|
return idx.Put(h[:], f)
|
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// dbStoreFilterHeader stores a block's basic or extended filter header.
|
2017-02-01 13:12:30 +01:00
|
|
|
func dbStoreFilterHeader(dbTx database.Tx, key []byte, h *chainhash.Hash, fh []byte) error {
|
|
|
|
if len(fh) != fastsha256.Size {
|
|
|
|
return errors.New("invalid filter header length")
|
|
|
|
}
|
2017-03-02 11:20:54 +01:00
|
|
|
idx := dbTx.Metadata().Bucket(cfIndexParentBucketKey).Bucket(key)
|
2017-02-01 13:12:30 +01:00
|
|
|
return idx.Put(h[:], fh)
|
2017-01-19 15:55:39 +01:00
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// dbDeleteFilter deletes a filter's basic or extended filter.
|
2017-02-01 13:12:30 +01:00
|
|
|
func dbDeleteFilter(dbTx database.Tx, key []byte, h *chainhash.Hash) error {
|
2017-03-02 11:20:54 +01:00
|
|
|
idx := dbTx.Metadata().Bucket(cfIndexParentBucketKey).Bucket(key)
|
2017-01-19 15:55:39 +01:00
|
|
|
return idx.Delete(h[:])
|
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// dbDeleteFilterHeader deletes a filter's basic or extended filter header.
|
2017-02-01 13:12:30 +01:00
|
|
|
func dbDeleteFilterHeader(dbTx database.Tx, key []byte, h *chainhash.Hash) error {
|
2017-03-02 11:20:54 +01:00
|
|
|
idx := dbTx.Metadata().Bucket(cfIndexParentBucketKey).Bucket(key)
|
2017-01-19 15:55:39 +01:00
|
|
|
return idx.Delete(h[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// CfIndex implements a committed filter (cf) by hash index.
|
|
|
|
type CfIndex struct {
|
2017-02-02 03:51:23 +01:00
|
|
|
db database.DB
|
|
|
|
chainParams *chaincfg.Params
|
2017-01-10 15:38:15 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 15:55:39 +01:00
|
|
|
// Ensure the CfIndex type implements the Indexer interface.
|
|
|
|
var _ Indexer = (*CfIndex)(nil)
|
2017-01-10 15:38:15 +01:00
|
|
|
|
2017-01-19 15:55:39 +01:00
|
|
|
// Init initializes the hash-based cf index. This is part of the Indexer
|
|
|
|
// interface.
|
|
|
|
func (idx *CfIndex) Init() error {
|
|
|
|
return nil // Nothing to do.
|
2017-01-10 15:38:15 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 15:55:39 +01:00
|
|
|
// Key returns the database key to use for the index as a byte slice. This is
|
|
|
|
// part of the Indexer interface.
|
|
|
|
func (idx *CfIndex) Key() []byte {
|
2017-03-02 11:20:54 +01:00
|
|
|
return cfIndexParentBucketKey
|
2017-01-10 15:38:15 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 15:55:39 +01:00
|
|
|
// Name returns the human-readable name of the index. This is part of the
|
|
|
|
// Indexer interface.
|
|
|
|
func (idx *CfIndex) Name() string {
|
2017-01-18 09:09:05 +01:00
|
|
|
return cfIndexName
|
2017-01-10 15:38:15 +01:00
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// Create is invoked when the indexer manager determines the index needs to
|
2017-02-01 13:12:30 +01:00
|
|
|
// be created for the first time. It creates buckets for the two hash-based cf
|
2017-01-19 14:20:41 +01:00
|
|
|
// indexes (simple, extended).
|
2017-01-19 15:55:39 +01:00
|
|
|
func (idx *CfIndex) Create(dbTx database.Tx) error {
|
2017-01-10 15:38:15 +01:00
|
|
|
meta := dbTx.Metadata()
|
2017-03-02 11:20:54 +01:00
|
|
|
cfIndexParentBucket, err := meta.CreateBucket(cfIndexParentBucketKey)
|
2017-01-19 14:20:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-02 11:20:54 +01:00
|
|
|
_, err = cfIndexParentBucket.CreateBucket(cfBasicIndexKey)
|
2017-01-31 11:01:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-02 11:20:54 +01:00
|
|
|
_, err = cfIndexParentBucket.CreateBucket(cfBasicHeaderKey)
|
2017-01-31 11:01:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-02 11:20:54 +01:00
|
|
|
_, err = cfIndexParentBucket.CreateBucket(cfExtendedIndexKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = cfIndexParentBucket.CreateBucket(cfExtendedHeaderKey)
|
2017-02-02 03:51:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
firstHeader := make([]byte, chainhash.HashSize)
|
|
|
|
err = dbStoreFilterHeader(
|
|
|
|
dbTx,
|
|
|
|
cfBasicHeaderKey,
|
|
|
|
&idx.chainParams.GenesisBlock.Header.PrevBlock,
|
|
|
|
firstHeader,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = dbStoreFilterHeader(
|
|
|
|
dbTx,
|
|
|
|
cfExtendedHeaderKey,
|
|
|
|
&idx.chainParams.GenesisBlock.Header.PrevBlock,
|
|
|
|
firstHeader,
|
|
|
|
)
|
2017-01-10 15:38:15 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// makeBasicFilterForBlock builds a block's basic filter, which consists of
|
2017-02-01 13:12:30 +01:00
|
|
|
// all outpoints and pkscript data pushes referenced by transactions within the
|
2017-01-20 14:21:49 +01:00
|
|
|
// block.
|
2017-01-19 15:55:39 +01:00
|
|
|
func makeBasicFilterForBlock(block *btcutil.Block) ([]byte, error) {
|
2017-01-19 11:49:40 +01:00
|
|
|
b := builder.WithKeyHash(block.Hash())
|
|
|
|
_, err := b.Key()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-01-11 17:09:08 +01:00
|
|
|
}
|
2017-02-02 18:44:22 +01:00
|
|
|
for i, tx := range block.Transactions() {
|
|
|
|
// Skip the inputs for the coinbase transaction
|
|
|
|
if i != 0 {
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
|
|
|
b.AddOutPoint(txIn.PreviousOutPoint)
|
|
|
|
}
|
2017-01-19 11:49:40 +01:00
|
|
|
}
|
2017-01-19 17:09:36 +01:00
|
|
|
for _, txOut := range tx.MsgTx().TxOut {
|
|
|
|
b.AddScript(txOut.PkScript)
|
|
|
|
}
|
2017-01-11 17:09:08 +01:00
|
|
|
}
|
2017-01-19 11:49:40 +01:00
|
|
|
f, err := b.Build()
|
2017-01-12 11:55:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-22 03:28:17 +01:00
|
|
|
return f.NBytes(), nil
|
2017-01-12 11:55:31 +01:00
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// makeExtendedFilterForBlock builds a block's extended filter, which consists
|
2017-02-01 13:12:30 +01:00
|
|
|
// of all tx hashes and sigscript data pushes contained in the block.
|
2017-01-20 14:21:49 +01:00
|
|
|
func makeExtendedFilterForBlock(block *btcutil.Block) ([]byte, error) {
|
|
|
|
b := builder.WithKeyHash(block.Hash())
|
|
|
|
_, err := b.Key()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-02 18:44:22 +01:00
|
|
|
for i, tx := range block.Transactions() {
|
2017-01-20 14:21:49 +01:00
|
|
|
b.AddHash(tx.Hash())
|
2017-02-02 18:44:22 +01:00
|
|
|
// Skip the inputs for the coinbase transaction
|
|
|
|
if i != 0 {
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
|
|
|
b.AddScript(txIn.SignatureScript)
|
|
|
|
}
|
2017-01-20 14:21:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f, err := b.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-22 03:28:17 +01:00
|
|
|
return f.NBytes(), nil
|
2017-01-20 14:21:49 +01:00
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// makeHeaderForFilter implements the chaining logic between filters, where
|
2017-02-01 13:12:30 +01:00
|
|
|
// a filter's header is defined as sha256(sha256(filter) + previousFilterHeader).
|
|
|
|
func makeHeaderForFilter(f, pfh []byte) []byte {
|
|
|
|
fhash := fastsha256.Sum256(f)
|
|
|
|
chain := make([]byte, 0, 2*fastsha256.Size)
|
|
|
|
chain = append(chain, fhash[:]...)
|
|
|
|
chain = append(chain, pfh...)
|
|
|
|
fh := fastsha256.Sum256(chain)
|
|
|
|
return fh[:]
|
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// storeFilter stores a given filter, and performs the steps needed to
|
2017-02-01 13:12:30 +01:00
|
|
|
// generate the filter's header.
|
|
|
|
func storeFilter(dbTx database.Tx, block *btcutil.Block, f []byte, extended bool) error {
|
|
|
|
// Figure out which buckets to use.
|
|
|
|
fkey := cfBasicIndexKey
|
|
|
|
hkey := cfBasicHeaderKey
|
|
|
|
if extended {
|
|
|
|
fkey = cfExtendedIndexKey
|
|
|
|
hkey = cfExtendedHeaderKey
|
|
|
|
}
|
|
|
|
// Start by storing the filter.
|
|
|
|
h := block.Hash()
|
|
|
|
err := dbStoreFilter(dbTx, fkey, h, f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Then fetch the previous block's filter header.
|
|
|
|
ph := &block.MsgBlock().Header.PrevBlock
|
|
|
|
pfh, err := dbFetchFilterHeader(dbTx, hkey, ph)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Construct the new block's filter header, and store it.
|
|
|
|
fh := makeHeaderForFilter(f, pfh)
|
2017-02-02 03:51:23 +01:00
|
|
|
return dbStoreFilterHeader(dbTx, hkey, h, fh)
|
2017-02-01 13:12:30 +01:00
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// ConnectBlock is invoked by the index manager when a new block has been
|
2017-01-19 15:55:39 +01:00
|
|
|
// connected to the main chain. This indexer adds a hash-to-cf mapping for
|
|
|
|
// every passed block. This is part of the Indexer interface.
|
|
|
|
func (idx *CfIndex) ConnectBlock(dbTx database.Tx, block *btcutil.Block,
|
2017-02-02 03:51:23 +01:00
|
|
|
view *blockchain.UtxoViewpoint) error {
|
2017-01-19 15:55:39 +01:00
|
|
|
f, err := makeBasicFilterForBlock(block)
|
2017-01-11 17:09:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-01 13:12:30 +01:00
|
|
|
err = storeFilter(dbTx, block, f, false)
|
2017-01-31 10:53:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err = makeExtendedFilterForBlock(block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-01 13:12:30 +01:00
|
|
|
return storeFilter(dbTx, block, f, true)
|
2017-01-10 15:38:15 +01:00
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// DisconnectBlock is invoked by the index manager when a block has been
|
2017-01-19 15:55:39 +01:00
|
|
|
// disconnected from the main chain. This indexer removes the hash-to-cf
|
|
|
|
// mapping for every passed block. This is part of the Indexer interface.
|
|
|
|
func (idx *CfIndex) DisconnectBlock(dbTx database.Tx, block *btcutil.Block,
|
2017-02-02 03:51:23 +01:00
|
|
|
view *blockchain.UtxoViewpoint) error {
|
2017-02-01 13:12:30 +01:00
|
|
|
err := dbDeleteFilter(dbTx, cfBasicIndexKey, block.Hash())
|
2017-01-31 10:56:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-01 13:12:30 +01:00
|
|
|
return dbDeleteFilter(dbTx, cfExtendedIndexKey, block.Hash())
|
2017-01-10 15:38:15 +01:00
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// FilterByBlockHash returns the serialized contents of a block's basic or
|
2017-02-01 14:43:09 +01:00
|
|
|
// extended committed filter.
|
|
|
|
func (idx *CfIndex) FilterByBlockHash(h *chainhash.Hash, extended bool) ([]byte, error) {
|
|
|
|
var f []byte
|
2017-01-12 16:28:27 +01:00
|
|
|
err := idx.db.View(func(dbTx database.Tx) error {
|
|
|
|
var err error
|
2017-02-01 13:12:30 +01:00
|
|
|
key := cfBasicIndexKey
|
2017-01-20 16:01:19 +01:00
|
|
|
if extended {
|
2017-02-01 13:12:30 +01:00
|
|
|
key = cfExtendedIndexKey
|
2017-01-20 16:01:19 +01:00
|
|
|
}
|
2017-02-01 14:43:09 +01:00
|
|
|
f, err = dbFetchFilter(dbTx, key, h)
|
2017-01-12 16:28:27 +01:00
|
|
|
return err
|
|
|
|
})
|
2017-02-01 14:43:09 +01:00
|
|
|
return f, err
|
|
|
|
}
|
|
|
|
|
2017-02-02 03:51:23 +01:00
|
|
|
// FilterHeaderByBlockHash returns the serialized contents of a block's basic
|
2017-02-01 14:43:09 +01:00
|
|
|
// or extended committed filter header.
|
|
|
|
func (idx *CfIndex) FilterHeaderByBlockHash(h *chainhash.Hash, extended bool) ([]byte, error) {
|
|
|
|
var fh []byte
|
|
|
|
err := idx.db.View(func(dbTx database.Tx) error {
|
|
|
|
var err error
|
|
|
|
key := cfBasicHeaderKey
|
|
|
|
if extended {
|
|
|
|
key = cfExtendedHeaderKey
|
|
|
|
}
|
|
|
|
fh, err = dbFetchFilterHeader(dbTx, key, h)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return fh, err
|
2017-01-12 16:28:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 15:55:39 +01:00
|
|
|
// NewCfIndex returns a new instance of an indexer that is used to create a
|
2017-01-10 15:38:15 +01:00
|
|
|
// mapping of the hashes of all blocks in the blockchain to their respective
|
2017-01-19 15:55:39 +01:00
|
|
|
// committed filters.
|
2017-01-10 15:38:15 +01:00
|
|
|
//
|
|
|
|
// It implements the Indexer interface which plugs into the IndexManager that in
|
2017-01-19 15:55:39 +01:00
|
|
|
// turn is used by the blockchain package. This allows the index to be
|
2017-01-10 15:38:15 +01:00
|
|
|
// seamlessly maintained along with the chain.
|
2017-02-02 03:51:23 +01:00
|
|
|
func NewCfIndex(db database.DB, chainParams *chaincfg.Params) *CfIndex {
|
|
|
|
return &CfIndex{db: db, chainParams: chainParams}
|
2017-01-10 15:38:15 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 15:55:39 +01:00
|
|
|
// DropCfIndex drops the CF index from the provided database if exists.
|
|
|
|
func DropCfIndex(db database.DB) error {
|
2017-03-02 11:20:54 +01:00
|
|
|
return dropIndex(db, cfIndexParentBucketKey, cfIndexName)
|
2017-01-10 15:38:15 +01:00
|
|
|
}
|