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 (
|
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
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-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-01-19 14:20:41 +01:00
|
|
|
// cfBasicIndexKey is the name of the db bucket used to house the
|
2017-01-19 15:55:39 +01:00
|
|
|
// block hash -> Basic cf index (cf#0).
|
2017-01-19 14:20:41 +01:00
|
|
|
cfBasicIndexKey = []byte("cf0byhashidx")
|
|
|
|
// cfExtendedIndexKey is the name of the db bucket used to house the
|
2017-01-19 15:55:39 +01:00
|
|
|
// block hash -> Extended cf index (cf#1).
|
2017-01-19 14:20:41 +01:00
|
|
|
cfExtendedIndexKey = []byte("cf1byhashidx")
|
2017-01-10 15:38:15 +01:00
|
|
|
)
|
|
|
|
|
2017-01-19 15:55:39 +01:00
|
|
|
// dbFetchBasicEntry() retrieves a block's basic filter. An entry's absence is
|
|
|
|
// not considered an error. The filter is returned serialized.
|
|
|
|
func dbFetchBasicEntry(dbTx database.Tx, h *chainhash.Hash) ([]byte, error) {
|
|
|
|
idx := dbTx.Metadata().Bucket(cfBasicIndexKey)
|
|
|
|
return idx.Get(h[:]), nil
|
|
|
|
}
|
2017-01-12 14:25:45 +01:00
|
|
|
|
2017-01-19 15:55:39 +01:00
|
|
|
// dbFetchExtendedEntry() retrieves a block's extended filter. An entry's
|
|
|
|
// absence is not considered an error. The filter is returned serialized.
|
|
|
|
func dbFetchExtendedEntry(dbTx database.Tx, h *chainhash.Hash) ([]byte, error) {
|
|
|
|
idx := dbTx.Metadata().Bucket(cfExtendedIndexKey)
|
|
|
|
return idx.Get(h[:]), nil
|
2017-01-12 14:25:45 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 15:55:39 +01:00
|
|
|
// dbStoreBasicEntry() stores a block's basic filter.
|
|
|
|
func dbStoreBasicEntry(dbTx database.Tx, h *chainhash.Hash, f []byte) error {
|
|
|
|
idx := dbTx.Metadata().Bucket(cfBasicIndexKey)
|
|
|
|
return idx.Put(h[:], f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// dbStoreBasicEntry() stores a block's extended filter.
|
|
|
|
func dbStoreExtendedEntry(dbTx database.Tx, h *chainhash.Hash, f []byte) error {
|
|
|
|
idx := dbTx.Metadata().Bucket(cfExtendedIndexKey)
|
|
|
|
return idx.Put(h[:], f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// dbDeleteBasicEntry() deletes a block's basic filter.
|
|
|
|
func dbDeleteBasicEntry(dbTx database.Tx, h *chainhash.Hash) error {
|
|
|
|
idx := dbTx.Metadata().Bucket(cfBasicIndexKey)
|
|
|
|
return idx.Delete(h[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// dbDeleteExtendedEntry() deletes a block's extended filter.
|
|
|
|
func dbDeleteExtendedEntry(dbTx database.Tx, h *chainhash.Hash) error {
|
|
|
|
idx := dbTx.Metadata().Bucket(cfExtendedIndexKey)
|
|
|
|
return idx.Delete(h[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// CfIndex implements a committed filter (cf) by hash index.
|
|
|
|
type CfIndex struct {
|
2017-01-10 15:38:15 +01:00
|
|
|
db database.DB
|
|
|
|
}
|
|
|
|
|
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-01-19 14:20:41 +01:00
|
|
|
return cfBasicIndexKey
|
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-01-19 14:20:41 +01:00
|
|
|
// Create is invoked when the indexer manager determines the index needs to be
|
2017-01-19 15:55:39 +01:00
|
|
|
// 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-01-19 14:20:41 +01:00
|
|
|
_, err := meta.CreateBucket(cfBasicIndexKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = meta.CreateBucket(cfExtendedIndexKey)
|
2017-01-10 15:38:15 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-19 15:55:39 +01:00
|
|
|
// makeBasicFilter() builds a block's basic filter, which consists of all
|
2017-01-20 14:21:49 +01:00
|
|
|
// outpoints and pkscript data pushes referenced by transactions within the
|
|
|
|
// 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-01-19 11:49:40 +01:00
|
|
|
for _, tx := range block.Transactions() {
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
|
|
|
b.AddOutPoint(txIn.PreviousOutPoint)
|
|
|
|
}
|
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-01-19 11:49:40 +01:00
|
|
|
return f.Bytes(), nil
|
2017-01-12 11:55:31 +01:00
|
|
|
}
|
|
|
|
|
2017-01-20 14:21:49 +01:00
|
|
|
// makeExtendedFilter() builds a block's extended filter, which consists of
|
|
|
|
// all tx hashes and sigscript data pushes contained in the block.
|
|
|
|
func makeExtendedFilterForBlock(block *btcutil.Block) ([]byte, error) {
|
|
|
|
b := builder.WithKeyHash(block.Hash())
|
|
|
|
_, err := b.Key()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, tx := range block.Transactions() {
|
|
|
|
b.AddHash(tx.Hash())
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
|
|
|
b.AddScript(txIn.SignatureScript)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f, err := b.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2017-01-12 11:55:31 +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-01-12 11:55:31 +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-01-19 15:55:39 +01:00
|
|
|
return dbStoreBasicEntry(dbTx, block.Hash(), f)
|
2017-01-10 15:38:15 +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-01-16 12:50:29 +01:00
|
|
|
view *blockchain.UtxoViewpoint) error {
|
2017-01-19 15:55:39 +01:00
|
|
|
return dbDeleteBasicEntry(dbTx, block.Hash())
|
2017-01-10 15:38:15 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 15:55:39 +01:00
|
|
|
func (idx *CfIndex) FilterByBlockHash(hash *chainhash.Hash) ([]byte, error) {
|
2017-01-12 16:28:27 +01:00
|
|
|
var filterBytes []byte
|
|
|
|
err := idx.db.View(func(dbTx database.Tx) error {
|
|
|
|
var err error
|
2017-01-19 15:55:39 +01:00
|
|
|
filterBytes, err = dbFetchBasicEntry(dbTx, hash)
|
2017-01-12 16:28:27 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
return filterBytes, err
|
|
|
|
}
|
|
|
|
|
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-01-19 15:55:39 +01:00
|
|
|
func NewCfIndex(db database.DB) *CfIndex {
|
|
|
|
return &CfIndex{db: db}
|
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-01-19 14:20:41 +01:00
|
|
|
return dropIndex(db, cfBasicIndexKey, cfIndexName)
|
2017-01-10 15:38:15 +01:00
|
|
|
}
|