github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/state/txindex/indexer.go (about)

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