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.

59 lines
1.5 KiB

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