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.

67 lines
2.0 KiB

7 years ago
7 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. // 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. // 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. }
  37. // NewBatch creates a new Batch.
  38. func NewBatch(n int64) *Batch {
  39. return &Batch{
  40. Ops: make([]*abci.TxResult, n),
  41. }
  42. }
  43. // Add or update an entry for the given result.Index.
  44. func (b *Batch) Add(result *abci.TxResult) error {
  45. b.Ops[result.Index] = result
  46. return nil
  47. }
  48. // Size returns the total number of operations inside the batch.
  49. func (b *Batch) Size() int {
  50. return len(b.Ops)
  51. }
  52. // ErrorEmptyHash indicates empty hash
  53. var ErrorEmptyHash = errors.New("transaction hash cannot be empty")