indexers

package
v0.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package indexers implements optional block chain indexes.

Index

Constants

View Source
const (
	// TrimmingInterval is the interval number for each cache trimming.
	TrimmingInterval = 10000

	// MaxCacheInputsCountPerTransaction is the max inputs count of transaction for cache.
	MaxCacheInputsCountPerTransaction = 100
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AssertError

type AssertError string

AssertError identifies an error that indicates an internal code consistency issue and should be treated as a critical and unrecoverable error.

func (AssertError) Error

func (e AssertError) Error() string

Error returns the assertion error as a huma-readable string and satisfies the error interface.

type Checkpoint

type Checkpoint struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Checkpoint hold all unspent txn cache to recover from scratch.

func NewCheckpoint

func NewCheckpoint(unspentIndex *UnspentIndex) *Checkpoint

func (*Checkpoint) DataExtension

func (c *Checkpoint) DataExtension() string

func (*Checkpoint) Deserialize

func (c *Checkpoint) Deserialize(r io.Reader) (err error)

func (*Checkpoint) EffectivePeriod

func (c *Checkpoint) EffectivePeriod() uint32

func (*Checkpoint) Generator

func (c *Checkpoint) Generator() func(buf []byte) checkpoint.ICheckPoint

func (*Checkpoint) GetHeight

func (c *Checkpoint) GetHeight() uint32

func (*Checkpoint) Key

func (c *Checkpoint) Key() string

func (*Checkpoint) LogError

func (c *Checkpoint) LogError(err error)

func (*Checkpoint) OnBlockSaved

func (c *Checkpoint) OnBlockSaved(block *types.DposBlock)

func (*Checkpoint) OnInit

func (c *Checkpoint) OnInit()

func (*Checkpoint) OnRollbackSeekTo added in v0.8.0

func (c *Checkpoint) OnRollbackSeekTo(height uint32)

func (*Checkpoint) OnRollbackTo

func (c *Checkpoint) OnRollbackTo(height uint32) error

func (*Checkpoint) Priority

func (c *Checkpoint) Priority() checkpoint.Priority

func (*Checkpoint) SavePeriod

func (c *Checkpoint) SavePeriod() uint32

func (*Checkpoint) Serialize

func (c *Checkpoint) Serialize(w io.Writer) (err error)

func (*Checkpoint) SetHeight

func (c *Checkpoint) SetHeight(height uint32)

func (*Checkpoint) Snapshot

func (c *Checkpoint) Snapshot() checkpoint.ICheckPoint

func (*Checkpoint) StartHeight

func (c *Checkpoint) StartHeight() uint32

type IChain

type IChain interface {
	MainChainHasBlock(height uint32, hash *common.Uint256) bool
	GetBlockByHeight(height uint32) (*types.Block, error)
	GetHeight() uint32
}

type ITxStore

type ITxStore interface {
	// FetchTx retrieval a transaction and a block hash where it
	// located by transaction hash
	FetchTx(txID common.Uint256) (*types.Transaction, uint32, error)
}

type IndexManager

type IndexManager interface {
	// Init is invoked during chain initialize in order to allow the index
	// manager to initialize itself and any indexes it is managing.  The
	// channel parameter specifies a channel the caller can close to signal
	// that the process should be interrupted.  It can be nil if that
	// behavior is not desired.
	Init(IChain, <-chan struct{}) error

	// ConnectBlock is invoked when a new block has been connected to the
	// main chain.
	ConnectBlock(database.Tx, *types.Block) error

	// DisconnectBlock is invoked when a block has been disconnected from
	// the main chain.
	DisconnectBlock(database.Tx, *types.Block) error

	// FetchTx retrieval a transaction and a block hash where it
	// located by transaction hash
	FetchTx(txID common.Uint256) (*types.Transaction, uint32, error)

	// FetchUnspent retrieval the unspent set of transaction by its hash
	FetchUnspent(txID common.Uint256) ([]uint16, error)

	// FetchUTXO retrieval the utxo set of a account address
	FetchUTXO(programHash *common.Uint168) ([]*types.UTXO, error)

	// IsTx3Exist use to find if tx3 exist in db
	IsTx3Exist(txHash *common.Uint256) bool

	// IsSideChainReturnDepositExist use to find if return deposit exist in db
	IsSideChainReturnDepositExist(txHash *common.Uint256) bool
}

IndexManager provides a generic interface that the is called when blocks are connected and disconnected to and from the tip of the main chain for the purpose of supporting optional indexes.

type Indexer

type Indexer interface {
	// Key returns the key of the index as a byte slice.
	Key() []byte

	// Name returns the human-readable name of the index.
	Name() string

	// Create is invoked when the indexer manager determines the index needs
	// to be created for the first time.
	Create(dbTx database.Tx) error

	// 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 a new block has been connected to the
	// main chain.
	ConnectBlock(database.Tx, *types.Block) error

	// DisconnectBlock is invoked when a block has been disconnected from
	// the main chain.
	DisconnectBlock(database.Tx, *types.Block) 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 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

func NewManager(db database.DB, params *config.Params) *Manager

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(dbTx database.Tx, block *types.Block) 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(dbTx database.Tx, block *types.Block) 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) FetchTx

func (m *Manager) FetchTx(txID common.Uint256) (*types.Transaction, uint32, error)

func (*Manager) FetchUTXO

func (m *Manager) FetchUTXO(programHash *common.Uint168) ([]*types.UTXO, error)

func (*Manager) FetchUnspent

func (m *Manager) FetchUnspent(txID common.Uint256) ([]uint16, error)

func (*Manager) Init

func (m *Manager) Init(chain IChain, interrupt <-chan struct{}) error

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) IsSideChainReturnDepositExist added in v0.8.0

func (m *Manager) IsSideChainReturnDepositExist(txHash *common.Uint256) bool

func (*Manager) IsTx3Exist

func (m *Manager) IsTx3Exist(txHash *common.Uint256) bool

type ReturnDepositIndex added in v0.8.0

type ReturnDepositIndex struct {
	// contains filtered or unexported fields
}

returnDepositIndex implements returnDeposit hash set which come from side chain.

func NewReturnDepositIndex added in v0.8.0

func NewReturnDepositIndex(db database.DB) *ReturnDepositIndex

NewreturnDepositIndex returns a new instance of an indexer that is used to create a mapping of the program hashes of all addresses be used in the blockchain to the their utxo.

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 (*ReturnDepositIndex) ConnectBlock added in v0.8.0

func (idx *ReturnDepositIndex) ConnectBlock(dbTx database.Tx, block *types.Block) error

ConnectBlock is invoked by the index manager when a new block has been connected to the main chain. This indexer maintains a returnDeposit hash mapping for every transaction in the passed block.

This is part of the Indexer interface.

func (*ReturnDepositIndex) Create added in v0.8.0

func (idx *ReturnDepositIndex) Create(dbTx database.Tx) error

Create is invoked when the indexer manager determines the index needs to be created for the first time. It creates the buckets for the returnDeposit index.

This is part of the Indexer interface.

func (*ReturnDepositIndex) DisconnectBlock added in v0.8.0

func (idx *ReturnDepositIndex) DisconnectBlock(dbTx database.Tx, block *types.Block) error

DisconnectBlock is invoked by the index manager when a block has been disconnected from the main chain. This indexer removes the returnDeposit hash mapping for every transaction in the block.

This is part of the Indexer interface.

func (*ReturnDepositIndex) Init added in v0.8.0

func (idx *ReturnDepositIndex) Init() error

Init initializes the hash-based returnDeposit index. This is part of the Indexer interface.

func (*ReturnDepositIndex) Key added in v0.8.0

func (idx *ReturnDepositIndex) Key() []byte

Key returns the database key to use for the index as a byte slice.

This is part of the Indexer interface.

func (*ReturnDepositIndex) Name added in v0.8.0

func (idx *ReturnDepositIndex) Name() string

Name returns the human-readable name of the index.

This is part of the Indexer interface.

type Tx3Index

type Tx3Index struct {
	// contains filtered or unexported fields
}

Tx3Index implements tx3 hash set which come from side chain.

func NewTx3Index

func NewTx3Index(db database.DB) *Tx3Index

NewTx3Index returns a new instance of an indexer that is used to create a mapping of the program hashes of all addresses be used in the blockchain to the their utxo.

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 (*Tx3Index) ConnectBlock

func (idx *Tx3Index) ConnectBlock(dbTx database.Tx, block *types.Block) error

ConnectBlock is invoked by the index manager when a new block has been connected to the main chain. This indexer maintains a tx3 hash mapping for every transaction in the passed block.

This is part of the Indexer interface.

func (*Tx3Index) Create

func (idx *Tx3Index) Create(dbTx database.Tx) error

Create is invoked when the indexer manager determines the index needs to be created for the first time. It creates the buckets for the tx3 index.

This is part of the Indexer interface.

func (*Tx3Index) DisconnectBlock

func (idx *Tx3Index) DisconnectBlock(dbTx database.Tx, block *types.Block) error

DisconnectBlock is invoked by the index manager when a block has been disconnected from the main chain. This indexer removes the tx3 hash mapping for every transaction in the block.

This is part of the Indexer interface.

func (*Tx3Index) Init

func (idx *Tx3Index) Init() error

Init initializes the hash-based tx3 index. This is part of the Indexer interface.

func (*Tx3Index) Key

func (idx *Tx3Index) Key() []byte

Key returns the database key to use for the index as a byte slice.

This is part of the Indexer interface.

func (*Tx3Index) Name

func (idx *Tx3Index) Name() string

Name returns the human-readable name of the index.

This is part of the Indexer interface.

type TxCache

type TxCache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewTxCache

func NewTxCache(params *config.Params) *TxCache

func (*TxCache) Deserialize

func (t *TxCache) Deserialize(r io.Reader) (err error)

func (*TxCache) Serialize

func (t *TxCache) Serialize(w io.Writer) (err error)

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

func NewTxIndex(db database.DB) *TxIndex

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(dbTx database.Tx, block *types.Block) 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) Create

func (idx *TxIndex) Create(dbTx database.Tx) error

Create is invoked when the indexer manager determines the index needs to be created for the first time. It creates the buckets for the hash-based transaction index and the internal block ID indexes.

This is part of the Indexer interface.

func (*TxIndex) DisconnectBlock

func (idx *TxIndex) DisconnectBlock(dbTx database.Tx, block *types.Block) 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.

func (*TxIndex) Init

func (idx *TxIndex) Init() error

Init initializes the hash-based transaction index. In particular, it finds the highest used block ID and stores it for later use when connecting or disconnecting blocks.

This is part of the Indexer interface.

func (*TxIndex) Key

func (idx *TxIndex) Key() []byte

Key returns the database key to use for the index as a byte slice.

This is part of the Indexer interface.

func (*TxIndex) Name

func (idx *TxIndex) Name() string

Name returns the human-readable name of the index.

This is part of the Indexer interface.

func (*TxIndex) TxBlockRegion

func (idx *TxIndex) TxBlockRegion(hash *common.Uint256) (*database.BlockRegion, error)

TxBlockRegion returns the block region for the provided transaction hash from the transaction index. The block region can in turn be used to load the raw transaction bytes. When there is no entry for the provided hash, nil will be returned for the both the entry and the error.

This function is safe for concurrent access.

type TxInfo

type TxInfo struct {
	// contains filtered or unexported fields
}

func (*TxInfo) Deserialize

func (t *TxInfo) Deserialize(r io.Reader) (err error)

func (*TxInfo) Serialize

func (t *TxInfo) Serialize(w io.Writer) (err error)

type UnspentIndex

type UnspentIndex struct {
	// contains filtered or unexported fields
}

UnspentIndex implements a unspent set by tx hash index. That is to say, it supports querying all unspent index by their tx hash.

func NewUnspentIndex

func NewUnspentIndex(db database.DB, params *config.Params) *UnspentIndex

NewUnspentIndex 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 index of output which unspent in 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 (*UnspentIndex) ConnectBlock

func (idx *UnspentIndex) ConnectBlock(dbTx database.Tx, block *types.Block) error

ConnectBlock is invoked by the index manager when a new block has been connected to the main chain. This indexer maintains a hash-to-unspent mapping for every transaction in the passed block.

This is part of the Indexer interface.

func (*UnspentIndex) Create

func (idx *UnspentIndex) Create(dbTx database.Tx) error

Create is invoked when the indexer manager determines the index needs to be created for the first time. It creates the buckets for the unspent index.

This is part of the Indexer interface.

func (*UnspentIndex) DisconnectBlock

func (idx *UnspentIndex) DisconnectBlock(dbTx database.Tx, block *types.Block) error

DisconnectBlock is invoked by the index manager when a block has been disconnected from the main chain. This indexer removes the hash-to-unspent mapping for every transaction in the block.

This is part of the Indexer interface.

func (*UnspentIndex) FetchTx

func (idx *UnspentIndex) FetchTx(txID common.Uint256) (*types.Transaction, uint32, error)

func (*UnspentIndex) Init

func (idx *UnspentIndex) Init() error

Init initializes the hash-based unspent index. This is part of the Indexer interface.

func (*UnspentIndex) Key

func (idx *UnspentIndex) Key() []byte

Key returns the database key to use for the index as a byte slice.

This is part of the Indexer interface.

func (*UnspentIndex) Name

func (idx *UnspentIndex) Name() string

Name returns the human-readable name of the index.

This is part of the Indexer interface.

type UtxoIndex

type UtxoIndex struct {
	// contains filtered or unexported fields
}

UtxoIndex implements a utxo by tx hash index.

func NewUtxoIndex

func NewUtxoIndex(db database.DB, store ITxStore) *UtxoIndex

NewUtxoIndex returns a new instance of an indexer that is used to create a mapping of the program hashes of all addresses be used in the blockchain to the their utxo.

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 (*UtxoIndex) ConnectBlock

func (idx *UtxoIndex) ConnectBlock(dbTx database.Tx, block *types.Block) error

ConnectBlock is invoked by the index manager when a new block has been connected to the main chain. This indexer maintains a hash-to-utxo mapping for every transaction in the passed block.

This is part of the Indexer interface.

func (*UtxoIndex) Create

func (idx *UtxoIndex) Create(dbTx database.Tx) error

Create is invoked when the indexer manager determines the index needs to be created for the first time. It creates the buckets for the utxo index.

This is part of the Indexer interface.

func (*UtxoIndex) DisconnectBlock

func (idx *UtxoIndex) DisconnectBlock(dbTx database.Tx, block *types.Block) error

DisconnectBlock is invoked by the index manager when a block has been disconnected from the main chain. This indexer removes the hash-to-utxos mapping for every transaction in the block.

This is part of the Indexer interface.

func (*UtxoIndex) Init

func (idx *UtxoIndex) Init() error

Init initializes the hash-based utxo index. This is part of the Indexer interface.

func (*UtxoIndex) Key

func (idx *UtxoIndex) Key() []byte

Key returns the database key to use for the index as a byte slice.

This is part of the Indexer interface.

func (*UtxoIndex) Name

func (idx *UtxoIndex) Name() string

Name returns the human-readable name of the index.

This is part of the Indexer interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL