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.

64 lines
1.7 KiB

  1. package txindex_test
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. abci "github.com/tendermint/tendermint/abci/types"
  8. "github.com/tendermint/tendermint/libs/db"
  9. "github.com/tendermint/tendermint/libs/log"
  10. "github.com/tendermint/tendermint/state/txindex"
  11. "github.com/tendermint/tendermint/state/txindex/kv"
  12. "github.com/tendermint/tendermint/types"
  13. )
  14. func TestIndexerServiceIndexesBlocks(t *testing.T) {
  15. // event bus
  16. eventBus := types.NewEventBus()
  17. eventBus.SetLogger(log.TestingLogger())
  18. err := eventBus.Start()
  19. require.NoError(t, err)
  20. defer eventBus.Stop()
  21. // tx indexer
  22. store := db.NewMemDB()
  23. txIndexer := kv.NewTxIndex(store, kv.IndexAllTags())
  24. service := txindex.NewIndexerService(txIndexer, eventBus)
  25. service.SetLogger(log.TestingLogger())
  26. err = service.Start()
  27. require.NoError(t, err)
  28. defer service.Stop()
  29. // publish block with txs
  30. eventBus.PublishEventNewBlockHeader(types.EventDataNewBlockHeader{
  31. Header: types.Header{Height: 1, NumTxs: 2},
  32. })
  33. txResult1 := &types.TxResult{
  34. Height: 1,
  35. Index: uint32(0),
  36. Tx: types.Tx("foo"),
  37. Result: abci.ResponseDeliverTx{Code: 0},
  38. }
  39. eventBus.PublishEventTx(types.EventDataTx{*txResult1})
  40. txResult2 := &types.TxResult{
  41. Height: 1,
  42. Index: uint32(1),
  43. Tx: types.Tx("bar"),
  44. Result: abci.ResponseDeliverTx{Code: 0},
  45. }
  46. eventBus.PublishEventTx(types.EventDataTx{*txResult2})
  47. time.Sleep(100 * time.Millisecond)
  48. // check the result
  49. res, err := txIndexer.Get(types.Tx("foo").Hash())
  50. assert.NoError(t, err)
  51. assert.Equal(t, txResult1, res)
  52. res, err = txIndexer.Get(types.Tx("bar").Hash())
  53. assert.NoError(t, err)
  54. assert.Equal(t, txResult2, res)
  55. }