blockchain/indexers: add a bit more line spacing to cfindex.go

This commit is contained in:
Olaoluwa Osuntokun 2017-04-27 19:44:43 -07:00
parent 1c5f25bbf2
commit e0943a84bd

View file

@ -29,15 +29,19 @@ var (
// cfIndexParentBucketKey is the name of the parent bucket used to house // cfIndexParentBucketKey is the name of the parent bucket used to house
// the index. The rest of the buckets live below this bucket. // the index. The rest of the buckets live below this bucket.
cfIndexParentBucketKey = []byte("cfindexparentbucket") cfIndexParentBucketKey = []byte("cfindexparentbucket")
// cfBasicIndexKey is the name of the db bucket used to house the // cfBasicIndexKey is the name of the db bucket used to house the
// block hash -> basic cf index (cf#0). // block hash -> basic cf index (cf#0).
cfBasicIndexKey = []byte("cf0byhashidx") cfBasicIndexKey = []byte("cf0byhashidx")
// cfBasicHeaderKey is the name of the db bucket used to house the // cfBasicHeaderKey is the name of the db bucket used to house the
// block hash -> basic cf header index (cf#0). // block hash -> basic cf header index (cf#0).
cfBasicHeaderKey = []byte("cf0headerbyhashidx") cfBasicHeaderKey = []byte("cf0headerbyhashidx")
// cfExtendedIndexKey is the name of the db bucket used to house the // cfExtendedIndexKey is the name of the db bucket used to house the
// block hash -> extended cf index (cf#1). // block hash -> extended cf index (cf#1).
cfExtendedIndexKey = []byte("cf1byhashidx") cfExtendedIndexKey = []byte("cf1byhashidx")
// cfExtendedHeaderKey is the name of the db bucket used to house the // cfExtendedHeaderKey is the name of the db bucket used to house the
// block hash -> extended cf header index (cf#1). // block hash -> extended cf header index (cf#1).
cfExtendedHeaderKey = []byte("cf1headerbyhashidx") cfExtendedHeaderKey = []byte("cf1headerbyhashidx")
@ -54,10 +58,12 @@ func dbFetchFilter(dbTx database.Tx, key []byte, h *chainhash.Hash) ([]byte, err
// A filter's absence is not considered an error. // A filter's absence is not considered an error.
func dbFetchFilterHeader(dbTx database.Tx, key []byte, h *chainhash.Hash) ([]byte, error) { func dbFetchFilterHeader(dbTx database.Tx, key []byte, h *chainhash.Hash) ([]byte, error) {
idx := dbTx.Metadata().Bucket(cfIndexParentBucketKey).Bucket(key) idx := dbTx.Metadata().Bucket(cfIndexParentBucketKey).Bucket(key)
fh := idx.Get(h[:]) fh := idx.Get(h[:])
if len(fh) != fastsha256.Size { if len(fh) != fastsha256.Size {
return nil, errors.New("invalid filter header length") return nil, errors.New("invalid filter header length")
} }
return fh, nil return fh, nil
} }
@ -120,6 +126,7 @@ func (idx *CfIndex) Name() string {
// indexes (simple, extended). // indexes (simple, extended).
func (idx *CfIndex) Create(dbTx database.Tx) error { func (idx *CfIndex) Create(dbTx database.Tx) error {
meta := dbTx.Metadata() meta := dbTx.Metadata()
cfIndexParentBucket, err := meta.CreateBucket(cfIndexParentBucketKey) cfIndexParentBucket, err := meta.CreateBucket(cfIndexParentBucketKey)
if err != nil { if err != nil {
return err return err
@ -140,6 +147,7 @@ func (idx *CfIndex) Create(dbTx database.Tx) error {
if err != nil { if err != nil {
return err return err
} }
firstHeader := make([]byte, chainhash.HashSize) firstHeader := make([]byte, chainhash.HashSize)
err = dbStoreFilterHeader( err = dbStoreFilterHeader(
dbTx, dbTx,
@ -150,19 +158,20 @@ func (idx *CfIndex) Create(dbTx database.Tx) error {
if err != nil { if err != nil {
return err return err
} }
err = dbStoreFilterHeader(
return dbStoreFilterHeader(
dbTx, dbTx,
cfExtendedHeaderKey, cfExtendedHeaderKey,
&idx.chainParams.GenesisBlock.Header.PrevBlock, &idx.chainParams.GenesisBlock.Header.PrevBlock,
firstHeader, firstHeader,
) )
return err
} }
// storeFilter stores a given filter, and performs the steps needed to // storeFilter stores a given filter, and performs the steps needed to
// generate the filter's header. // generate the filter's header.
func storeFilter(dbTx database.Tx, block *btcutil.Block, f *gcs.Filter, func storeFilter(dbTx database.Tx, block *btcutil.Block, f *gcs.Filter,
extended bool) error { extended bool) error {
// Figure out which buckets to use. // Figure out which buckets to use.
fkey := cfBasicIndexKey fkey := cfBasicIndexKey
hkey := cfBasicHeaderKey hkey := cfBasicHeaderKey
@ -170,18 +179,21 @@ func storeFilter(dbTx database.Tx, block *btcutil.Block, f *gcs.Filter,
fkey = cfExtendedIndexKey fkey = cfExtendedIndexKey
hkey = cfExtendedHeaderKey hkey = cfExtendedHeaderKey
} }
// Start by storing the filter. // Start by storing the filter.
h := block.Hash() h := block.Hash()
err := dbStoreFilter(dbTx, fkey, h, f.NBytes()) err := dbStoreFilter(dbTx, fkey, h, f.NBytes())
if err != nil { if err != nil {
return err return err
} }
// Then fetch the previous block's filter header. // Then fetch the previous block's filter header.
ph := &block.MsgBlock().Header.PrevBlock ph := &block.MsgBlock().Header.PrevBlock
pfh, err := dbFetchFilterHeader(dbTx, hkey, ph) pfh, err := dbFetchFilterHeader(dbTx, hkey, ph)
if err != nil { if err != nil {
return err return err
} }
// Construct the new block's filter header, and store it. // Construct the new block's filter header, and store it.
prevHeader, err := chainhash.NewHash(pfh) prevHeader, err := chainhash.NewHash(pfh)
if err != nil { if err != nil {
@ -196,18 +208,21 @@ func storeFilter(dbTx database.Tx, block *btcutil.Block, f *gcs.Filter,
// every passed block. This is part of the Indexer interface. // every passed block. This is part of the Indexer interface.
func (idx *CfIndex) ConnectBlock(dbTx database.Tx, block *btcutil.Block, func (idx *CfIndex) ConnectBlock(dbTx database.Tx, block *btcutil.Block,
view *blockchain.UtxoViewpoint) error { view *blockchain.UtxoViewpoint) error {
f, err := builder.BuildBasicFilter(block.MsgBlock()) f, err := builder.BuildBasicFilter(block.MsgBlock())
if err != nil { if err != nil {
return err return err
} }
err = storeFilter(dbTx, block, f, false)
if err != nil { if err := storeFilter(dbTx, block, f, false); err != nil {
return err return err
} }
f, err = builder.BuildExtFilter(block.MsgBlock()) f, err = builder.BuildExtFilter(block.MsgBlock())
if err != nil { if err != nil {
return err return err
} }
return storeFilter(dbTx, block, f, true) return storeFilter(dbTx, block, f, true)
} }
@ -216,10 +231,12 @@ func (idx *CfIndex) ConnectBlock(dbTx database.Tx, block *btcutil.Block,
// mapping for every passed block. This is part of the Indexer interface. // mapping for every passed block. This is part of the Indexer interface.
func (idx *CfIndex) DisconnectBlock(dbTx database.Tx, block *btcutil.Block, func (idx *CfIndex) DisconnectBlock(dbTx database.Tx, block *btcutil.Block,
view *blockchain.UtxoViewpoint) error { view *blockchain.UtxoViewpoint) error {
err := dbDeleteFilter(dbTx, cfBasicIndexKey, block.Hash()) err := dbDeleteFilter(dbTx, cfBasicIndexKey, block.Hash())
if err != nil { if err != nil {
return err return err
} }
return dbDeleteFilter(dbTx, cfExtendedIndexKey, block.Hash()) return dbDeleteFilter(dbTx, cfExtendedIndexKey, block.Hash())
} }
@ -233,6 +250,7 @@ func (idx *CfIndex) FilterByBlockHash(h *chainhash.Hash, extended bool) ([]byte,
if extended { if extended {
key = cfExtendedIndexKey key = cfExtendedIndexKey
} }
f, err = dbFetchFilter(dbTx, key, h) f, err = dbFetchFilter(dbTx, key, h)
return err return err
}) })
@ -249,6 +267,7 @@ func (idx *CfIndex) FilterHeaderByBlockHash(h *chainhash.Hash, extended bool) ([
if extended { if extended {
key = cfExtendedHeaderKey key = cfExtendedHeaderKey
} }
fh, err = dbFetchFilterHeader(dbTx, key, h) fh, err = dbFetchFilterHeader(dbTx, key, h)
return err return err
}) })
@ -259,8 +278,8 @@ func (idx *CfIndex) FilterHeaderByBlockHash(h *chainhash.Hash, extended bool) ([
// mapping of the hashes of all blocks in the blockchain to their respective // mapping of the hashes of all blocks in the blockchain to their respective
// committed filters. // committed filters.
// //
// It implements the Indexer interface which plugs into the IndexManager that in // It implements the Indexer interface which plugs into the IndexManager that
// turn is used by the blockchain package. This allows the index to be // in turn is used by the blockchain package. This allows the index to be
// seamlessly maintained along with the chain. // seamlessly maintained along with the chain.
func NewCfIndex(db database.DB, chainParams *chaincfg.Params) *CfIndex { func NewCfIndex(db database.DB, chainParams *chaincfg.Params) *CfIndex {
return &CfIndex{db: db, chainParams: chainParams} return &CfIndex{db: db, chainParams: chainParams}