github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/blockchain/indexers/common.go (about)

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