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
2.9 KiB

  1. package core
  2. import (
  3. "errors"
  4. "testing"
  5. "github.com/stretchr/testify/mock"
  6. "github.com/stretchr/testify/require"
  7. abcitypes "github.com/tendermint/tendermint/abci/types"
  8. "github.com/tendermint/tendermint/libs/log"
  9. tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
  10. rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  11. "github.com/tendermint/tendermint/state/indexer"
  12. "github.com/tendermint/tendermint/state/mocks"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. const (
  16. height int64 = 10
  17. base int64 = 1
  18. )
  19. func TestUnsafeReIndex(t *testing.T) {
  20. mockBlockStore := &mocks.BlockStore{}
  21. mockStateStore := &mocks.Store{}
  22. mockEventSink := &mocks.EventSink{}
  23. env := &Environment{
  24. BlockStore: mockBlockStore,
  25. StateStore: mockStateStore,
  26. EventSinks: []indexer.EventSink{mockEventSink},
  27. Logger: log.TestingLogger()}
  28. mockBlockStore.
  29. On("Base").Return(base).
  30. On("Height").Return(height).
  31. On("LoadBlock", base+1).Return(nil).Once().
  32. On("LoadBlock", base+1).Return(&types.Block{Data: types.Data{Txs: types.Txs{make(types.Tx, 1)}}})
  33. mockEventSink.
  34. On("Type").Return(indexer.NULL).Once().
  35. On("Type").Return(indexer.KV).
  36. On("IndexBlockEvents", mock.AnythingOfType("types.EventDataNewBlockHeader")).Return(errors.New("")).Once().
  37. On("IndexBlockEvents", mock.AnythingOfType("types.EventDataNewBlockHeader")).Return(nil).
  38. On("IndexTxEvents", mock.AnythingOfType("[]*types.TxResult")).Return(errors.New("")).Once().
  39. On("IndexTxEvents", mock.AnythingOfType("[]*types.TxResult")).Return(nil)
  40. dtx := abcitypes.ResponseDeliverTx{}
  41. abciResp := &tmstate.ABCIResponses{
  42. DeliverTxs: []*abcitypes.ResponseDeliverTx{&dtx},
  43. EndBlock: &abcitypes.ResponseEndBlock{},
  44. BeginBlock: &abcitypes.ResponseBeginBlock{},
  45. }
  46. mockStateStore.
  47. On("LoadABCIResponses", base+1).Return(nil, errors.New("")).Once().
  48. On("LoadABCIResponses", base+1).Return(abciResp, nil)
  49. testCases := []struct {
  50. startHeight int64
  51. endHeight int64
  52. enableSink bool
  53. isErr bool
  54. }{
  55. {base, 0, false, true}, // the start height less equal than the base height
  56. {height + 1, 0, false, true}, // the start height greater than the store height
  57. {base + 1, base, false, true}, // the end height less equal than the base height
  58. {height, height - 1, false, true}, // the start height greater than the end height
  59. {base + 1, height + 1, false, true}, // the end height will be the same as the store height and no eventsink error
  60. {base + 1, base + 1, true, true}, // LoadBlock error
  61. {base + 1, base + 1, true, true}, // LoadABCIResponses error
  62. {base + 1, base + 1, true, true}, // index block event error
  63. {base + 1, base + 1, true, true}, // index tx event error
  64. {base + 1, base + 1, true, false},
  65. }
  66. for _, tc := range testCases {
  67. res, err := env.UnsafeReIndex(&rpctypes.Context{}, tc.startHeight, tc.endHeight)
  68. if tc.isErr {
  69. require.Error(t, err)
  70. } else {
  71. require.NoError(t, err)
  72. require.NotNil(t, res)
  73. }
  74. }
  75. }