github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/cmd/cometbft/commands/reindex_event_test.go (about) 1 package commands 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "github.com/spf13/cobra" 9 "github.com/stretchr/testify/mock" 10 "github.com/stretchr/testify/require" 11 12 dbm "github.com/badrootd/nibiru-db" 13 14 abcitypes "github.com/badrootd/nibiru-cometbft/abci/types" 15 cmtcfg "github.com/badrootd/nibiru-cometbft/config" 16 protocmtstate "github.com/badrootd/nibiru-cometbft/proto/tendermint/state" 17 blockmocks "github.com/badrootd/nibiru-cometbft/state/indexer/mocks" 18 "github.com/badrootd/nibiru-cometbft/state/mocks" 19 txmocks "github.com/badrootd/nibiru-cometbft/state/txindex/mocks" 20 "github.com/badrootd/nibiru-cometbft/types" 21 ) 22 23 const ( 24 height int64 = 10 25 base int64 = 2 26 ) 27 28 func setupReIndexEventCmd() *cobra.Command { 29 reIndexEventCmd := &cobra.Command{ 30 Use: ReIndexEventCmd.Use, 31 Run: func(cmd *cobra.Command, args []string) {}, 32 } 33 34 _ = reIndexEventCmd.ExecuteContext(context.Background()) 35 36 return reIndexEventCmd 37 } 38 39 func TestReIndexEventCheckHeight(t *testing.T) { 40 mockBlockStore := &mocks.BlockStore{} 41 mockBlockStore. 42 On("Base").Return(base). 43 On("Height").Return(height) 44 45 testCases := []struct { 46 startHeight int64 47 endHeight int64 48 validHeight bool 49 }{ 50 {0, 0, true}, 51 {0, base, true}, 52 {0, base - 1, false}, 53 {0, height, true}, 54 {0, height + 1, true}, 55 {0, 0, true}, 56 {base - 1, 0, false}, 57 {base, 0, true}, 58 {base, base, true}, 59 {base, base - 1, false}, 60 {base, height, true}, 61 {base, height + 1, true}, 62 {height, 0, true}, 63 {height, base, false}, 64 {height, height - 1, false}, 65 {height, height, true}, 66 {height, height + 1, true}, 67 {height + 1, 0, false}, 68 } 69 70 for _, tc := range testCases { 71 startHeight = tc.startHeight 72 endHeight = tc.endHeight 73 74 err := checkValidHeight(mockBlockStore) 75 if tc.validHeight { 76 require.NoError(t, err) 77 } else { 78 require.Error(t, err) 79 } 80 } 81 } 82 83 func TestLoadEventSink(t *testing.T) { 84 testCases := []struct { 85 sinks string 86 connURL string 87 loadErr bool 88 }{ 89 {"", "", true}, 90 {"NULL", "", true}, 91 {"KV", "", false}, 92 {"PSQL", "", true}, // true because empty connect url 93 // skip to test PSQL connect with correct url 94 {"UnsupportedSinkType", "wrongUrl", true}, 95 } 96 97 for idx, tc := range testCases { 98 cfg := cmtcfg.TestConfig() 99 cfg.TxIndex.Indexer = tc.sinks 100 cfg.TxIndex.PsqlConn = tc.connURL 101 _, _, err := loadEventSinks(cfg) 102 if tc.loadErr { 103 require.Error(t, err, idx) 104 } else { 105 require.NoError(t, err, idx) 106 } 107 } 108 } 109 110 func TestLoadBlockStore(t *testing.T) { 111 cfg := cmtcfg.TestConfig() 112 cfg.DBPath = t.TempDir() 113 _, _, err := loadStateAndBlockStore(cfg) 114 require.Error(t, err) 115 116 _, err = dbm.NewDB("blockstore", dbm.GoLevelDBBackend, cfg.DBDir()) 117 require.NoError(t, err) 118 119 // Get StateStore 120 _, err = dbm.NewDB("state", dbm.GoLevelDBBackend, cfg.DBDir()) 121 require.NoError(t, err) 122 123 bs, ss, err := loadStateAndBlockStore(cfg) 124 require.NoError(t, err) 125 require.NotNil(t, bs) 126 require.NotNil(t, ss) 127 } 128 129 func TestReIndexEvent(t *testing.T) { 130 mockBlockStore := &mocks.BlockStore{} 131 mockStateStore := &mocks.Store{} 132 mockBlockIndexer := &blockmocks.BlockIndexer{} 133 mockTxIndexer := &txmocks.TxIndexer{} 134 135 mockBlockStore. 136 On("Base").Return(base). 137 On("Height").Return(height). 138 On("LoadBlock", base).Return(nil).Once(). 139 On("LoadBlock", base).Return(&types.Block{Data: types.Data{Txs: types.Txs{make(types.Tx, 1)}}}). 140 On("LoadBlock", height).Return(&types.Block{Data: types.Data{Txs: types.Txs{make(types.Tx, 1)}}}) 141 142 dtx := abcitypes.ResponseDeliverTx{} 143 abciResp := &protocmtstate.ABCIResponses{ 144 DeliverTxs: []*abcitypes.ResponseDeliverTx{&dtx}, 145 EndBlock: &abcitypes.ResponseEndBlock{}, 146 BeginBlock: &abcitypes.ResponseBeginBlock{}, 147 } 148 149 mockBlockIndexer. 150 On("Index", mock.AnythingOfType("types.EventDataNewBlockHeader")).Return(errors.New("")).Once(). 151 On("Index", mock.AnythingOfType("types.EventDataNewBlockHeader")).Return(nil) 152 153 mockTxIndexer. 154 On("AddBatch", mock.AnythingOfType("*txindex.Batch")).Return(errors.New("")).Once(). 155 On("AddBatch", mock.AnythingOfType("*txindex.Batch")).Return(nil) 156 157 mockStateStore. 158 On("LoadABCIResponses", base).Return(nil, errors.New("")).Once(). 159 On("LoadABCIResponses", base).Return(abciResp, nil). 160 On("LoadABCIResponses", height).Return(abciResp, nil) 161 162 testCases := []struct { 163 startHeight int64 164 endHeight int64 165 reIndexErr bool 166 }{ 167 {base, height, true}, // LoadBlock error 168 {base, height, true}, // LoadABCIResponses error 169 {base, height, true}, // index block event error 170 {base, height, true}, // index tx event error 171 {base, base, false}, 172 {height, height, false}, 173 } 174 175 for _, tc := range testCases { 176 args := eventReIndexArgs{ 177 startHeight: tc.startHeight, 178 endHeight: tc.endHeight, 179 blockIndexer: mockBlockIndexer, 180 txIndexer: mockTxIndexer, 181 blockStore: mockBlockStore, 182 stateStore: mockStateStore, 183 } 184 185 err := eventReIndex(setupReIndexEventCmd(), args) 186 if tc.reIndexErr { 187 require.Error(t, err) 188 } else { 189 require.NoError(t, err) 190 } 191 } 192 }