github.com/aakash4dev/cometbft@v0.38.2/state/txindex/indexer.go (about)

     1  package txindex
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"github.com/aakash4dev/cometbft/libs/log"
     8  
     9  	abci "github.com/aakash4dev/cometbft/abci/types"
    10  	"github.com/aakash4dev/cometbft/libs/pubsub/query"
    11  )
    12  
    13  // XXX/TODO: These types should be moved to the indexer package.
    14  
    15  //go:generate ../../scripts/mockery_generate.sh TxIndexer
    16  
    17  // TxIndexer interface defines methods to index and search transactions.
    18  type TxIndexer interface {
    19  	// AddBatch analyzes, indexes and stores a batch of transactions.
    20  	AddBatch(b *Batch) error
    21  
    22  	// Index analyzes, indexes and stores a single transaction.
    23  	Index(result *abci.TxResult) error
    24  
    25  	// Get returns the transaction specified by hash or nil if the transaction is not indexed
    26  	// or stored.
    27  	Get(hash []byte) (*abci.TxResult, error)
    28  
    29  	// Search allows you to query for transactions.
    30  	Search(ctx context.Context, q *query.Query) ([]*abci.TxResult, error)
    31  
    32  	//Set Logger
    33  	SetLogger(l log.Logger)
    34  }
    35  
    36  // Batch groups together multiple Index operations to be performed at the same time.
    37  // NOTE: Batch is NOT thread-safe and must not be modified after starting its execution.
    38  type Batch struct {
    39  	Ops []*abci.TxResult
    40  }
    41  
    42  // NewBatch creates a new Batch.
    43  func NewBatch(n int64) *Batch {
    44  	return &Batch{
    45  		Ops: make([]*abci.TxResult, n),
    46  	}
    47  }
    48  
    49  // Add or update an entry for the given result.Index.
    50  func (b *Batch) Add(result *abci.TxResult) error {
    51  	b.Ops[result.Index] = result
    52  	return nil
    53  }
    54  
    55  // Size returns the total number of operations inside the batch.
    56  func (b *Batch) Size() int {
    57  	return len(b.Ops)
    58  }
    59  
    60  // ErrorEmptyHash indicates empty hash
    61  var ErrorEmptyHash = errors.New("transaction hash cannot be empty")