lbcd/blockchain/indexers/cfindex.go

199 lines
6.3 KiB
Go
Raw Normal View History

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 (
// cfIndexName is the human-readable name for the index.
cfIndexName = "committed filter index"
2017-01-10 15:38:15 +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 (
// cfBasicIndexKey is the name of the db bucket used to house the
// block hash -> Basic cf index (cf#0).
cfBasicIndexKey = []byte("cf0byhashidx")
// cfExtendedIndexKey is the name of the db bucket used to house the
// block hash -> Extended cf index (cf#1).
cfExtendedIndexKey = []byte("cf1byhashidx")
2017-01-10 15:38:15 +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
// 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
}
// 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
}
// Ensure the CfIndex type implements the Indexer interface.
var _ Indexer = (*CfIndex)(nil)
2017-01-10 15:38:15 +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
}
// 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 {
return cfBasicIndexKey
2017-01-10 15:38:15 +01:00
}
// Name returns the human-readable name of the index. This is part of the
// Indexer interface.
func (idx *CfIndex) Name() string {
return cfIndexName
2017-01-10 15:38:15 +01:00
}
// Create is invoked when the indexer manager determines the index needs to be
// created for the first time. It creates buckets for the two hash-based cf
// indexes (simple, extended).
func (idx *CfIndex) Create(dbTx database.Tx) error {
2017-01-10 15:38:15 +01:00
meta := dbTx.Metadata()
_, err := meta.CreateBucket(cfBasicIndexKey)
if err != nil {
return err
}
_, err = meta.CreateBucket(cfExtendedIndexKey)
2017-01-10 15:38:15 +01:00
return err
}
// 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.
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-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-19 11:49:40 +01:00
f, err := b.Build()
if err != nil {
return nil, err
}
2017-01-19 11:49:40 +01:00
return f.Bytes(), nil
}
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
}
// ConnectBlock is invoked by the index manager when a new block has been
// 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,
view *blockchain.UtxoViewpoint) error {
f, err := makeBasicFilterForBlock(block)
if err != nil {
return err
}
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
// 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,
view *blockchain.UtxoViewpoint) error {
return dbDeleteBasicEntry(dbTx, block.Hash())
2017-01-10 15:38:15 +01:00
}
func (idx *CfIndex) FilterByBlockHash(hash *chainhash.Hash) ([]byte, error) {
var filterBytes []byte
err := idx.db.View(func(dbTx database.Tx) error {
var err error
filterBytes, err = dbFetchBasicEntry(dbTx, hash)
return err
})
return filterBytes, err
}
// 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
// committed filters.
2017-01-10 15:38:15 +01:00
//
// It implements the Indexer interface which plugs into the IndexManager that in
// 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.
func NewCfIndex(db database.DB) *CfIndex {
return &CfIndex{db: db}
2017-01-10 15:38:15 +01:00
}
// DropCfIndex drops the CF index from the provided database if exists.
func DropCfIndex(db database.DB) error {
return dropIndex(db, cfBasicIndexKey, cfIndexName)
2017-01-10 15:38:15 +01:00
}