github.com/okex/exchain@v1.8.0/libs/tendermint/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/okex/exchain/libs/tm-db" 11 12 abci "github.com/okex/exchain/libs/tendermint/abci/types" 13 ctypes "github.com/okex/exchain/libs/tendermint/rpc/core/types" 14 rpctypes "github.com/okex/exchain/libs/tendermint/rpc/jsonrpc/types" 15 sm "github.com/okex/exchain/libs/tendermint/state" 16 "github.com/okex/exchain/libs/tendermint/types" 17 ) 18 19 func TestBlockchainInfo(t *testing.T) { 20 cases := []struct { 21 min, max int64 22 base, height int64 23 limit int64 24 resultLength int64 25 wantErr bool 26 }{ 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 := &sm.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.StateDB = dbm.NewMemDB() 84 sm.SaveABCIResponses(env.StateDB, 100, results) 85 env.BlockStore = mockBlockStore{height: 100} 86 87 testCases := []struct { 88 height int64 89 wantErr bool 90 wantRes *ctypes.ResultBlockResults 91 }{ 92 {-1, true, nil}, 93 {0, true, nil}, 94 {101, true, nil}, 95 {100, false, &ctypes.ResultBlockResults{ 96 Height: 100, 97 TxsResults: results.DeliverTxs, 98 BeginBlockEvents: results.BeginBlock.Events, 99 EndBlockEvents: results.EndBlock.Events, 100 ValidatorUpdates: results.EndBlock.ValidatorUpdates, 101 ConsensusParamUpdates: results.EndBlock.ConsensusParamUpdates, 102 }}, 103 } 104 105 for _, tc := range testCases { 106 res, err := BlockResults(&rpctypes.Context{}, &tc.height) 107 if tc.wantErr { 108 assert.Error(t, err) 109 } else { 110 assert.NoError(t, err) 111 assert.Equal(t, tc.wantRes, res) 112 } 113 } 114 } 115 116 type mockBlockStore struct { 117 height int64 118 } 119 120 func (mockBlockStore) Base() int64 { return 1 } 121 func (store mockBlockStore) Height() int64 { return store.height } 122 func (store mockBlockStore) Size() int64 { return store.height } 123 func (mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta { return nil } 124 func (mockBlockStore) LoadBlock(height int64) *types.Block { return nil } 125 func (mockBlockStore) LoadBlockByHash(hash []byte) *types.Block { return nil } 126 func (mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil } 127 func (mockBlockStore) LoadBlockCommit(height int64) *types.Commit { return nil } 128 func (mockBlockStore) LoadSeenCommit(height int64) *types.Commit { return nil } 129 func (mockBlockStore) PruneBlocks(height int64) (uint64, error) { return 0, nil } 130 func (mockBlockStore) DeleteBlocksFromTop(height int64) (uint64, error) { return 0, nil } 131 func (mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) { 132 }