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.

88 lines
3.6 KiB

  1. package psql
  2. // This file adds code to the psql package that is needed for integration with
  3. // v0.34, but which is not part of the original implementation.
  4. //
  5. // In v0.35, ADR 65 was implemented in which the TxIndexer and BlockIndexer
  6. // interfaces were merged into a hybrid EventSink interface. The Backport*
  7. // types defined here bridge the psql EventSink (which was built in terms of
  8. // the v0.35 interface) to the old interfaces.
  9. //
  10. // We took this narrower approach to backporting to avoid pulling in a much
  11. // wider-reaching set of changes in v0.35 that would have broken several of the
  12. // v0.34.x APIs. The result is sufficient to work with the node plumbing as it
  13. // exists in the v0.34 branch.
  14. import (
  15. "context"
  16. "errors"
  17. abci "github.com/tendermint/tendermint/abci/types"
  18. "github.com/tendermint/tendermint/libs/pubsub/query"
  19. "github.com/tendermint/tendermint/state/txindex"
  20. "github.com/tendermint/tendermint/types"
  21. )
  22. const (
  23. eventTypeBeginBlock = "begin_block"
  24. eventTypeEndBlock = "end_block"
  25. )
  26. // TxIndexer returns a bridge from es to the Tendermint v0.34 transaction indexer.
  27. func (es *EventSink) TxIndexer() BackportTxIndexer {
  28. return BackportTxIndexer{psql: es}
  29. }
  30. // BackportTxIndexer implements the txindex.TxIndexer interface by delegating
  31. // indexing operations to an underlying PostgreSQL event sink.
  32. type BackportTxIndexer struct{ psql *EventSink }
  33. // AddBatch indexes a batch of transactions in Postgres, as part of TxIndexer.
  34. func (b BackportTxIndexer) AddBatch(batch *txindex.Batch) error {
  35. return b.psql.IndexTxEvents(batch.Ops)
  36. }
  37. // Index indexes a single transaction result in Postgres, as part of TxIndexer.
  38. func (b BackportTxIndexer) Index(txr *abci.TxResult) error {
  39. return b.psql.IndexTxEvents([]*abci.TxResult{txr})
  40. }
  41. // Get is implemented to satisfy the TxIndexer interface, but is not supported
  42. // by the psql event sink and reports an error for all inputs.
  43. func (BackportTxIndexer) Get([]byte) (*abci.TxResult, error) {
  44. return nil, errors.New("the TxIndexer.Get method is not supported")
  45. }
  46. // Search is implemented to satisfy the TxIndexer interface, but it is not
  47. // supported by the psql event sink and reports an error for all inputs.
  48. func (BackportTxIndexer) Search(context.Context, *query.Query) ([]*abci.TxResult, error) {
  49. return nil, errors.New("the TxIndexer.Search method is not supported")
  50. }
  51. // BlockIndexer returns a bridge that implements the Tendermint v0.34 block
  52. // indexer interface, using the Postgres event sink as a backing store.
  53. func (es *EventSink) BlockIndexer() BackportBlockIndexer {
  54. return BackportBlockIndexer{psql: es}
  55. }
  56. // BackportBlockIndexer implements the indexer.BlockIndexer interface by
  57. // delegating indexing operations to an underlying PostgreSQL event sink.
  58. type BackportBlockIndexer struct{ psql *EventSink }
  59. // Has is implemented to satisfy the BlockIndexer interface, but it is not
  60. // supported by the psql event sink and reports an error for all inputs.
  61. func (BackportBlockIndexer) Has(height int64) (bool, error) {
  62. return false, errors.New("the BlockIndexer.Has method is not supported")
  63. }
  64. // Index indexes block begin and end events for the specified block. It is
  65. // part of the BlockIndexer interface.
  66. func (b BackportBlockIndexer) Index(block types.EventDataNewBlockHeader) error {
  67. return b.psql.IndexBlockEvents(block)
  68. }
  69. // Search is implemented to satisfy the BlockIndexer interface, but it is not
  70. // supported by the psql event sink and reports an error for all inputs.
  71. func (BackportBlockIndexer) Search(context.Context, *query.Query) ([]int64, error) {
  72. return nil, errors.New("the BlockIndexer.Search method is not supported")
  73. }