github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/rpc/core/blocks_test.go (about) 1 package core 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 dbm "github.com/badrootd/nibiru-db" 11 12 abci "github.com/badrootd/nibiru-cometbft/abci/types" 13 cmtstate "github.com/badrootd/nibiru-cometbft/proto/tendermint/state" 14 ctypes "github.com/badrootd/nibiru-cometbft/rpc/core/types" 15 rpctypes "github.com/badrootd/nibiru-cometbft/rpc/jsonrpc/types" 16 sm "github.com/badrootd/nibiru-cometbft/state" 17 "github.com/badrootd/nibiru-cometbft/state/mocks" 18 ) 19 20 func TestBlockchainInfo(t *testing.T) { 21 cases := []struct { 22 min, max int64 23 base, height int64 24 limit int64 25 resultLength int64 26 wantErr bool 27 }{ 28 // min > max 29 {0, 0, 0, 0, 10, 0, true}, // min set to 1 30 {0, 1, 0, 0, 10, 0, true}, // max set to height (0) 31 {0, 0, 0, 1, 10, 1, false}, // max set to height (1) 32 {2, 0, 0, 1, 10, 0, true}, // max set to height (1) 33 {2, 1, 0, 5, 10, 0, true}, 34 35 // negative 36 {1, 10, 0, 14, 10, 10, false}, // control 37 {-1, 10, 0, 14, 10, 0, true}, 38 {1, -10, 0, 14, 10, 0, true}, 39 {-9223372036854775808, -9223372036854775788, 0, 100, 20, 0, true}, 40 41 // check base 42 {1, 1, 1, 1, 1, 1, false}, 43 {2, 5, 3, 5, 5, 3, false}, 44 45 // check limit and height 46 {1, 1, 0, 1, 10, 1, false}, 47 {1, 1, 0, 5, 10, 1, false}, 48 {2, 2, 0, 5, 10, 1, false}, 49 {1, 2, 0, 5, 10, 2, false}, 50 {1, 5, 0, 1, 10, 1, false}, 51 {1, 5, 0, 10, 10, 5, false}, 52 {1, 15, 0, 10, 10, 10, false}, 53 {1, 15, 0, 15, 10, 10, false}, 54 {1, 15, 0, 15, 20, 15, false}, 55 {1, 20, 0, 15, 20, 15, false}, 56 {1, 20, 0, 20, 20, 20, false}, 57 } 58 59 for i, c := range cases { 60 caseString := fmt.Sprintf("test %d failed", i) 61 min, max, err := filterMinMax(c.base, c.height, c.min, c.max, c.limit) 62 if c.wantErr { 63 require.Error(t, err, caseString) 64 } else { 65 require.NoError(t, err, caseString) 66 require.Equal(t, 1+max-min, c.resultLength, caseString) 67 } 68 } 69 } 70 71 func TestBlockResults(t *testing.T) { 72 results := &cmtstate.ABCIResponses{ 73 DeliverTxs: []*abci.ResponseDeliverTx{ 74 {Code: 0, Data: []byte{0x01}, Log: "ok"}, 75 {Code: 0, Data: []byte{0x02}, Log: "ok"}, 76 {Code: 1, Log: "not ok"}, 77 }, 78 EndBlock: &abci.ResponseEndBlock{}, 79 BeginBlock: &abci.ResponseBeginBlock{}, 80 } 81 82 env = &Environment{} 83 env.StateStore = sm.NewStore(dbm.NewMemDB(), sm.StoreOptions{ 84 DiscardABCIResponses: false, 85 }) 86 err := env.StateStore.SaveABCIResponses(100, results) 87 require.NoError(t, err) 88 mockstore := &mocks.BlockStore{} 89 mockstore.On("Height").Return(int64(100)) 90 mockstore.On("Base").Return(int64(1)) 91 env.BlockStore = mockstore 92 93 testCases := []struct { 94 height int64 95 wantErr bool 96 wantRes *ctypes.ResultBlockResults 97 }{ 98 {-1, true, nil}, 99 {0, true, nil}, 100 {101, true, nil}, 101 {100, false, &ctypes.ResultBlockResults{ 102 Height: 100, 103 TxsResults: results.DeliverTxs, 104 BeginBlockEvents: results.BeginBlock.Events, 105 EndBlockEvents: results.EndBlock.Events, 106 ValidatorUpdates: results.EndBlock.ValidatorUpdates, 107 ConsensusParamUpdates: results.EndBlock.ConsensusParamUpdates, 108 }}, 109 } 110 111 for _, tc := range testCases { 112 res, err := BlockResults(&rpctypes.Context{}, &tc.height) 113 if tc.wantErr { 114 assert.Error(t, err) 115 } else { 116 assert.NoError(t, err) 117 assert.Equal(t, tc.wantRes, res) 118 } 119 } 120 }