Documentation ¶
Overview ¶
Package indexers implements optional block chain indexes.
Index ¶
- Constants
- func AddrIndexBlock(data WriteAddrIdxData, block *types.SerializedBlock, stxos [][]byte)
- func AddrToKey(addr types.Address) ([AddrKeySize]byte, error)
- func DBPutAddrIndexEntry(bucket InternalBucket, addrKey [AddrKeySize]byte, blockID uint32, ...) error
- func DBRemoveAddrIndexEntries(bucket InternalBucket, addrKey [AddrKeySize]byte, count int, prefix []byte) error
- func UseLogger(logger l.Logger)
- type AddrIndex
- func (idx *AddrIndex) AddUnconfirmedTx(tx *types.Tx, pkScripts [][]byte)
- func (idx *AddrIndex) ConnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
- func (idx *AddrIndex) DB() model.DataBase
- func (idx *AddrIndex) DisconnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
- func (idx *AddrIndex) Init() error
- func (idx *AddrIndex) Name() string
- func (idx *AddrIndex) NeedsInputs() bool
- func (idx *AddrIndex) RemoveUnconfirmedTx(hash *hash.Hash)
- func (idx *AddrIndex) TxRegionsForAddress(addr types.Address, numToSkip, numRequested uint32, reverse bool) ([]*common.RetrievedTx, uint32, error)
- func (idx *AddrIndex) UnconfirmedTxnsForAddress(addr types.Address) []*types.Tx
- type BlockRegion
- type Config
- type FetchAddrLevelDataFunc
- type FetchBlockHashFunc
- type Indexer
- type InternalBucket
- type InvalidTxIndex
- func (idx *InvalidTxIndex) ConnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
- func (idx *InvalidTxIndex) DB() model.DataBase
- func (idx *InvalidTxIndex) DisconnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
- func (idx *InvalidTxIndex) Get(txid *hash.Hash) (*types.Transaction, error)
- func (idx *InvalidTxIndex) GetIdByHash(h *hash.Hash) (*hash.Hash, error)
- func (idx *InvalidTxIndex) Init() error
- func (idx *InvalidTxIndex) Name() string
- func (idx *InvalidTxIndex) UpdateMainTip(bh *hash.Hash, order uint64) error
- type Manager
- func (m *Manager) AddrIndex() *AddrIndex
- func (m *Manager) ConnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
- func (m *Manager) DisconnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
- func (m *Manager) GetIndex(name string) Indexer
- func (m *Manager) HasTx(txid *hash.Hash) bool
- func (m *Manager) Init() error
- func (m *Manager) InvalidTxIndex() *InvalidTxIndex
- func (m *Manager) IsDuplicateTx(txid *hash.Hash, blockHash *hash.Hash) bool
- func (m *Manager) TxHashIndex() *TxHashIndex
- func (m *Manager) TxIndex() *TxIndex
- func (m *Manager) UpdateMainTip(bh *hash.Hash, order uint64) error
- type NeedsInputser
- type TxHashIndex
- func (idx *TxHashIndex) ConnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
- func (idx *TxHashIndex) DisconnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
- func (idx *TxHashIndex) GetTxIdByHash(fullHash hash.Hash) (*hash.Hash, error)
- func (idx *TxHashIndex) Init() error
- func (idx *TxHashIndex) Name() string
- type TxIndex
- type WriteAddrIdxData
Constants ¶
const ( // addrIndexName is the human-readable name for the index. AddrIndexName = "address index" // addrKeySize is the number of bytes an address key consumes in the // index. It consists of 1 byte address type + 20 bytes hash160. AddrKeySize = 1 + 20 )
Variables ¶
This section is empty.
Functions ¶
func AddrIndexBlock ¶ added in v1.2.0
func AddrIndexBlock(data WriteAddrIdxData, block *types.SerializedBlock, stxos [][]byte)
AddrIndexBlock extract all of the standard addresses from all of the transactions in the parent of the passed block (if they were valid) and all of the stake transactions in the passed block, and maps each of them to the associated transaction using the passed map.
func AddrToKey ¶ added in v1.2.0
func AddrToKey(addr types.Address) ([AddrKeySize]byte, error)
addrToKey converts known address types to an addrindex key. An error is returned for unsupported types.
func DBPutAddrIndexEntry ¶ added in v1.2.0
func DBPutAddrIndexEntry(bucket InternalBucket, addrKey [AddrKeySize]byte, blockID uint32, txLoc types.TxLoc, prefix []byte) error
DBPutAddrIndexEntry updates the address index to include the provided entry according to the level-based scheme described in detail above.
func DBRemoveAddrIndexEntries ¶ added in v1.2.0
func DBRemoveAddrIndexEntries(bucket InternalBucket, addrKey [AddrKeySize]byte, count int, prefix []byte) error
DBRemoveAddrIndexEntries removes the specified number of entries from from the address index for the provided key. An assertion error will be returned if the count exceeds the total number of entries in the index.
Types ¶
type AddrIndex ¶
type AddrIndex struct {
// contains filtered or unexported fields
}
AddrIndex implements a transaction by address index. That is to say, it supports querying all transactions that reference a given address because they are either crediting or debiting the address. The returned transactions are ordered according to their order of appearance in the blockchain. In other words, first by block height and then by offset inside the block.
In addition, support is provided for a memory-only index of unconfirmed transactions such as those which are kept in the memory pool before inclusion in a block.
func NewAddrIndex ¶
NewAddrIndex returns a new instance of an indexer that is used to create a mapping of all addresses in the blockchain to the respective transactions that involve them.
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 seamlessly maintained along with the chain.
func (*AddrIndex) AddUnconfirmedTx ¶
AddUnconfirmedTx adds all addresses related to the transaction to the unconfirmed (memory-only) address index.
NOTE: This transaction MUST have already been validated by the memory pool before calling this function with it and have all of the inputs available in the provided utxo view. Failure to do so could result in some or all addresses not being indexed.
This function is safe for concurrent access.
func (*AddrIndex) ConnectBlock ¶
func (idx *AddrIndex) ConnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
ConnectBlock is invoked by the index manager when a new block has been connected to the main chain. This indexer adds a mapping for each address the transactions in the block involve.
This is part of the Indexer interface.
func (*AddrIndex) DisconnectBlock ¶
func (idx *AddrIndex) DisconnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
DisconnectBlock is invoked by the index manager when a block has been disconnected from the main chain. This indexer removes the address mappings each transaction in the block involve.
This is part of the Indexer interface.
func (*AddrIndex) Init ¶
Init is only provided to satisfy the Indexer interface as there is nothing to initialize for this index.
This is part of the Indexer interface.
func (*AddrIndex) Name ¶
Name returns the human-readable name of the index.
This is part of the Indexer interface.
func (*AddrIndex) NeedsInputs ¶
NeedsInputs signals that the index requires the referenced inputs in order to properly create the index.
This implements the NeedsInputser interface.
func (*AddrIndex) RemoveUnconfirmedTx ¶
RemoveUnconfirmedTx removes the passed transaction from the unconfirmed (memory-only) address index.
This function is safe for concurrent access.
func (*AddrIndex) TxRegionsForAddress ¶
func (idx *AddrIndex) TxRegionsForAddress(addr types.Address, numToSkip, numRequested uint32, reverse bool) ([]*common.RetrievedTx, uint32, error)
TxRegionsForAddress returns a slice of block regions which identify each transaction that involves the passed address according to the specified number to skip, number requested, and whether or not the results should be reversed. It also returns the number actually skipped since it could be less in the case where there are not enough entries.
NOTE: These results only include transactions confirmed in blocks. See the UnconfirmedTxnsForAddress method for obtaining unconfirmed transactions that involve a given address.
This function is safe for concurrent access.
func (*AddrIndex) UnconfirmedTxnsForAddress ¶
UnconfirmedTxnsForAddress returns all transactions currently in the unconfirmed (memory-only) address index that involve the passed address. Unsupported address types are ignored and will result in no results.
This function is safe for concurrent access.
type BlockRegion ¶ added in v1.2.0
func DBFetchAddrIndexEntries ¶ added in v1.2.0
func DBFetchAddrIndexEntries(fetchBlockHash FetchBlockHashFunc, fetchAddrLevelData FetchAddrLevelDataFunc, addrKey [AddrKeySize]byte, numToSkip, numRequested uint32, reverse bool, prefix []byte) ([]BlockRegion, uint32, error)
DBFetchAddrIndexEntries returns block regions for transactions referenced by the given address key and the number of entries skipped since it could have been less in the case where there are less total entries than the requested number of entries to skip.
type Config ¶ added in v1.0.19
func DefaultConfig ¶ added in v1.0.19
func DefaultConfig() *Config
type FetchAddrLevelDataFunc ¶ added in v1.2.0
type FetchBlockHashFunc ¶ added in v1.2.0
fetchBlockHashFunc defines a callback function to use in order to convert a serialized block ID to an associated block hash.
type Indexer ¶
type Indexer interface { // Name returns the human-readable name of the index. Name() string // Init is invoked when the index manager is first initializing the // index. This differs from the Create method in that it is called on // every load, including the case the index was just created. Init() error // ConnectBlock is invoked when the index manager is notified that a new // block has been connected to the main chain. ConnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error // DisconnectBlock is invoked when the index manager is notified that a // block has been disconnected from the main chain. DisconnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error }
Indexer provides a generic interface for an indexer that is managed by an index manager such as the Manager type provided by this package.
type InternalBucket ¶ added in v1.2.0
type InternalBucket interface { Get(key []byte) []byte Put(key []byte, value []byte) error Delete(key []byte) error }
InternalBucket is an abstraction over a database bucket. It is used to make the code easier to test since it allows mock objects in the tests to only implement these functions instead of everything a database.Bucket supports.
type InvalidTxIndex ¶ added in v1.0.19
type InvalidTxIndex struct {
// contains filtered or unexported fields
}
func NewInvalidTxIndex ¶ added in v1.0.19
func NewInvalidTxIndex(consensus model.Consensus) *InvalidTxIndex
func (*InvalidTxIndex) ConnectBlock ¶ added in v1.0.19
func (idx *InvalidTxIndex) ConnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
func (*InvalidTxIndex) DB ¶ added in v1.2.0
func (idx *InvalidTxIndex) DB() model.DataBase
func (*InvalidTxIndex) DisconnectBlock ¶ added in v1.0.19
func (idx *InvalidTxIndex) DisconnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
func (*InvalidTxIndex) Get ¶ added in v1.0.19
func (idx *InvalidTxIndex) Get(txid *hash.Hash) (*types.Transaction, error)
func (*InvalidTxIndex) GetIdByHash ¶ added in v1.0.19
func (*InvalidTxIndex) Init ¶ added in v1.0.19
func (idx *InvalidTxIndex) Init() error
func (*InvalidTxIndex) Name ¶ added in v1.0.19
func (idx *InvalidTxIndex) Name() string
func (*InvalidTxIndex) UpdateMainTip ¶ added in v1.0.19
func (idx *InvalidTxIndex) UpdateMainTip(bh *hash.Hash, order uint64) error
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager defines an index manager that manages multiple optional indexes and implements the blockchain.IndexManager interface so it can be seamlessly plugged into normal chain processing.
func NewManager ¶
NewManager returns a new index manager with the provided indexes enabled.
The manager returned satisfies the blockchain.IndexManager interface and thus cleanly plugs into the normal blockchain processing path.
func (*Manager) ConnectBlock ¶
func (m *Manager) ConnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
ConnectBlock must be invoked when a block is extending the main chain. It keeps track of the state of each index it is managing, performs some sanity checks, and invokes each indexer.
This is part of the blockchain.IndexManager interface.
func (*Manager) DisconnectBlock ¶
func (m *Manager) DisconnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
DisconnectBlock must be invoked when a block is being disconnected from the end of the main chain. It keeps track of the state of each index it is managing, performs some sanity checks, and invokes each indexer to remove the index entries associated with the block.
This is part of the blockchain.IndexManager interface.
func (*Manager) Init ¶
Init initializes the enabled indexes. This is called during chain initialization and primarily consists of catching up all indexes to the current best chain tip. This is necessary since each index can be disabled and re-enabled at any time and attempting to catch-up indexes at the same time new blocks are being downloaded would lead to an overall longer time to catch up due to the I/O contention.
This is part of the blockchain.IndexManager interface.
func (*Manager) InvalidTxIndex ¶ added in v1.0.19
func (m *Manager) InvalidTxIndex() *InvalidTxIndex
func (*Manager) IsDuplicateTx ¶
HasTransaction
func (*Manager) TxHashIndex ¶ added in v1.2.0
func (m *Manager) TxHashIndex() *TxHashIndex
type NeedsInputser ¶
type NeedsInputser interface {
NeedsInputs() bool
}
NeedsInputser provides a generic interface for an indexer to specify the it requires the ability to look up inputs for a transaction.
type TxHashIndex ¶ added in v1.2.0
type TxHashIndex struct {
// contains filtered or unexported fields
}
func NewTxHashIndex ¶ added in v1.2.0
func NewTxHashIndex(consensus model.Consensus) *TxHashIndex
func (*TxHashIndex) ConnectBlock ¶ added in v1.2.0
func (idx *TxHashIndex) ConnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
func (*TxHashIndex) DisconnectBlock ¶ added in v1.2.0
func (idx *TxHashIndex) DisconnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
func (*TxHashIndex) GetTxIdByHash ¶ added in v1.2.0
func (*TxHashIndex) Init ¶ added in v1.2.0
func (idx *TxHashIndex) Init() error
func (*TxHashIndex) Name ¶ added in v1.2.0
func (idx *TxHashIndex) Name() string
type TxIndex ¶
type TxIndex struct {
// contains filtered or unexported fields
}
TxIndex implements a transaction by hash index. That is to say, it supports querying all transactions by their hash.
func NewTxIndex ¶
NewTxIndex returns a new instance of an indexer that is used to create a mapping of the hashes of all transactions in the blockchain to the respective block, location within the block, and size of the transaction.
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 seamlessly maintained along with the chain.
func (*TxIndex) ConnectBlock ¶
func (idx *TxIndex) ConnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
ConnectBlock is invoked by the index manager when a new block has been connected to the main chain. This indexer adds a hash-to-transaction mapping for every transaction in the passed block.
This is part of the Indexer interface.
func (*TxIndex) DisconnectBlock ¶
func (idx *TxIndex) DisconnectBlock(sblock *types.SerializedBlock, block model.Block, stxos [][]byte) error
DisconnectBlock is invoked by the index manager when a block has been disconnected from the main chain. This indexer removes the hash-to-transaction mapping for every transaction in the block.
This is part of the Indexer interface.
type WriteAddrIdxData ¶ added in v1.2.0
type WriteAddrIdxData map[[AddrKeySize]byte][]int
WriteAddrIdxData represents the address index data to be written for one block. It consists of the address mapped to an ordered list of the transactions that involve the address in block. It is ordered so the transactions can be stored in the order they appear in the block.