You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.5 KiB

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