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.

66 lines
2.1 KiB

7 years ago
6 years ago
  1. package indexer
  2. import (
  3. "context"
  4. "errors"
  5. abci "github.com/tendermint/tendermint/abci/types"
  6. "github.com/tendermint/tendermint/libs/pubsub/query"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. // TxIndexer interface defines methods to index and search transactions.
  10. type TxIndexer interface {
  11. // Index analyzes, indexes and stores transactions. For indexing multiple
  12. // Transacions must guarantee the Index of the TxResult is in order.
  13. // See Batch struct.
  14. Index(results []*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. // BlockIndexer defines an interface contract for indexing block events.
  22. type BlockIndexer interface {
  23. // Has returns true if the given height has been indexed. An error is returned
  24. // upon database query failure.
  25. Has(height int64) (bool, error)
  26. // Index indexes BeginBlock and EndBlock events for a given block by its height.
  27. Index(types.EventDataNewBlockHeader) error
  28. // Search performs a query for block heights that match a given BeginBlock
  29. // and Endblock event search criteria.
  30. Search(ctx context.Context, q *query.Query) ([]int64, error)
  31. }
  32. // Batch groups together multiple Index operations to be performed at the same time.
  33. // NOTE: Batch is NOT thread-safe and must not be modified after starting its execution.
  34. type Batch struct {
  35. Ops []*abci.TxResult
  36. Pending int64
  37. }
  38. // NewBatch creates a new Batch.
  39. func NewBatch(n int64) *Batch {
  40. return &Batch{Ops: make([]*abci.TxResult, n), Pending: n}
  41. }
  42. // Add or update an entry for the given result.Index.
  43. func (b *Batch) Add(result *abci.TxResult) error {
  44. if b.Ops[result.Index] == nil {
  45. b.Pending--
  46. b.Ops[result.Index] = result
  47. }
  48. return nil
  49. }
  50. // Size returns the total number of operations inside the batch.
  51. func (b *Batch) Size() int { return len(b.Ops) }
  52. // ErrorEmptyHash indicates empty hash
  53. var ErrorEmptyHash = errors.New("transaction hash cannot be empty")