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