1 // Copyright (c) 2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
6 Package indexers implements optional block chain indexes.
13 "github.com/btcsuite/btcd/blockchain"
14 "github.com/btcsuite/btcd/database"
15 "github.com/btcsuite/btcutil"
19 // byteOrder is the preferred byte order used for serializing numeric
20 // fields for storage in the database.
21 byteOrder = binary.LittleEndian
24 // NeedsInputser provides a generic interface for an indexer to specify the it
25 // requires the ability to look up inputs for a transaction.
26 type NeedsInputser interface {
30 // Indexer provides a generic interface for an indexer that is managed by an
31 // index manager such as the Manager type provided by this package.
32 type Indexer interface {
33 // Key returns the key of the index as a byte slice.
36 // Name returns the human-readable name of the index.
39 // Create is invoked when the indexer manager determines the index needs
40 // to be created for the first time.
41 Create(dbTx database.Tx) error
43 // Init is invoked when the index manager is first initializing the
44 // index. This differs from the Create method in that it is called on
45 // every load, including the case the index was just created.
48 // ConnectBlock is invoked when the index manager is notified that a new
49 // block has been connected to the main chain.
50 ConnectBlock(dbTx database.Tx, block *btcutil.Block, view *blockchain.UtxoViewpoint) error
52 // DisconnectBlock is invoked when the index manager is notified that a
53 // block has been disconnected from the main chain.
54 DisconnectBlock(dbTx database.Tx, block *btcutil.Block, view *blockchain.UtxoViewpoint) error
57 // AssertError identifies an error that indicates an internal code consistency
58 // issue and should be treated as a critical and unrecoverable error.
59 type AssertError string
61 // Error returns the assertion error as a huma-readable string and satisfies
62 // the error interface.
63 func (e AssertError) Error() string {
64 return "assertion failed: " + string(e)
67 // errDeserialize signifies that a problem was encountered when deserializing
69 type errDeserialize string
71 // Error implements the error interface.
72 func (e errDeserialize) Error() string {
76 // isDeserializeErr returns whether or not the passed error is an errDeserialize
78 func isDeserializeErr(err error) bool {
79 _, ok := err.(errDeserialize)
83 // internalBucket is an abstraction over a database bucket. It is used to make
84 // the code easier to test since it allows mock objects in the tests to only
85 // implement these functions instead of everything a database.Bucket supports.
86 type internalBucket interface {
87 Get(key []byte) []byte
88 Put(key []byte, value []byte) error
89 Delete(key []byte) error