OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / blockchain / indexers / common.go
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.
4
5 /*
6 Package indexers implements optional block chain indexes.
7 */
8 package indexers
9
10 import (
11         "encoding/binary"
12
13         "github.com/btcsuite/btcd/blockchain"
14         "github.com/btcsuite/btcd/database"
15         "github.com/btcsuite/btcutil"
16 )
17
18 var (
19         // byteOrder is the preferred byte order used for serializing numeric
20         // fields for storage in the database.
21         byteOrder = binary.LittleEndian
22 )
23
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 {
27         NeedsInputs() bool
28 }
29
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.
34         Key() []byte
35
36         // Name returns the human-readable name of the index.
37         Name() string
38
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
42
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.
46         Init() error
47
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
51
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
55 }
56
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
60
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)
65 }
66
67 // errDeserialize signifies that a problem was encountered when deserializing
68 // data.
69 type errDeserialize string
70
71 // Error implements the error interface.
72 func (e errDeserialize) Error() string {
73         return string(e)
74 }
75
76 // isDeserializeErr returns whether or not the passed error is an errDeserialize
77 // error.
78 func isDeserializeErr(err error) bool {
79         _, ok := err.(errDeserialize)
80         return ok
81 }
82
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
90 }