github.com/number571/tendermint@v0.34.11-gost/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/tendermint/tm-db" 11 12 abci "github.com/number571/tendermint/abci/types" 13 tmstate "github.com/number571/tendermint/proto/tendermint/state" 14 ctypes "github.com/number571/tendermint/rpc/core/types" 15 rpctypes "github.com/number571/tendermint/rpc/jsonrpc/types" 16 sm "github.com/number571/tendermint/state" 17 "github.com/number571/tendermint/types" 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 29 // min > max 30 {0, 0, 0, 0, 10, 0, true}, // min set to 1 31 {0, 1, 0, 0, 10, 0, true}, // max set to height (0) 32 {0, 0, 0, 1, 10, 1, false}, // max set to height (1) 33 {2, 0, 0, 1, 10, 0, true}, // max set to height (1) 34 {2, 1, 0, 5, 10, 0, true}, 35 36 // negative 37 {1, 10, 0, 14, 10, 10, false}, // control 38 {-1, 10, 0, 14, 10, 0, true}, 39 {1, -10, 0, 14, 10, 0, true}, 40 {-9223372036854775808, -9223372036854775788, 0, 100, 20, 0, true}, 41 42 // check base 43 {1, 1, 1, 1, 1, 1, false}, 44 {2, 5, 3, 5, 5, 3, false}, 45 46 // check limit and height 47 {1, 1, 0, 1, 10, 1, false}, 48 {1, 1, 0, 5, 10, 1, false}, 49 {2, 2, 0, 5, 10, 1, false}, 50 {1, 2, 0, 5, 10, 2, false}, 51 {1, 5, 0, 1, 10, 1, false}, 52 {1, 5, 0, 10, 10, 5, false}, 53 {1, 15, 0, 10, 10, 10, false}, 54 {1, 15, 0, 15, 10, 10, false}, 55 {1, 15, 0, 15, 20, 15, false}, 56 {1, 20, 0, 15, 20, 15, false}, 57 {1, 20, 0, 20, 20, 20, false}, 58 } 59 60 for i, c := range cases { 61 caseString := fmt.Sprintf("test %d failed", i) 62 min, max, err := filterMinMax(c.base, c.height, c.min, c.max, c.limit) 63 if c.wantErr { 64 require.Error(t, err, caseString) 65 } else { 66 require.NoError(t, err, caseString) 67 require.Equal(t, 1+max-min, c.resultLength, caseString) 68 } 69 } 70 } 71 72 func TestBlockResults(t *testing.T) { 73 results := &tmstate.ABCIResponses{ 74 DeliverTxs: []*abci.ResponseDeliverTx{ 75 {Code: 0, Data: []byte{0x01}, Log: "ok", GasUsed: 10}, 76 {Code: 0, Data: []byte{0x02}, Log: "ok", GasUsed: 5}, 77 {Code: 1, Log: "not ok", GasUsed: 0}, 78 }, 79 EndBlock: &abci.ResponseEndBlock{}, 80 BeginBlock: &abci.ResponseBeginBlock{}, 81 } 82 83 env := &Environment{} 84 env.StateStore = sm.NewStore(dbm.NewMemDB()) 85 err := env.StateStore.SaveABCIResponses(100, results) 86 require.NoError(t, err) 87 env.BlockStore = mockBlockStore{height: 100} 88 89 testCases := []struct { 90 height int64 91 wantErr bool 92 wantRes *ctypes.ResultBlockResults 93 }{ 94 {-1, true, nil}, 95 {0, true, nil}, 96 {101, true, nil}, 97 {100, false, &ctypes.ResultBlockResults{ 98 Height: 100, 99 TxsResults: results.DeliverTxs, 100 TotalGasUsed: 15, 101 BeginBlockEvents: results.BeginBlock.Events, 102 EndBlockEvents: results.EndBlock.Events, 103 ValidatorUpdates: results.EndBlock.ValidatorUpdates, 104 ConsensusParamUpdates: results.EndBlock.ConsensusParamUpdates, 105 }}, 106 } 107 108 for _, tc := range testCases { 109 res, err := env.BlockResults(&rpctypes.Context{}, &tc.height) 110 if tc.wantErr { 111 assert.Error(t, err) 112 } else { 113 assert.NoError(t, err) 114 assert.Equal(t, tc.wantRes, res) 115 } 116 } 117 } 118 119 type mockBlockStore struct { 120 height int64 121 } 122 123 func (mockBlockStore) Base() int64 { return 1 } 124 func (store mockBlockStore) Height() int64 { return store.height } 125 func (store mockBlockStore) Size() int64 { return store.height } 126 func (mockBlockStore) LoadBaseMeta() *types.BlockMeta { return nil } 127 func (mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta { return nil } 128 func (mockBlockStore) LoadBlock(height int64) *types.Block { return nil } 129 func (mockBlockStore) LoadBlockByHash(hash []byte) *types.Block { return nil } 130 func (mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil } 131 func (mockBlockStore) LoadBlockCommit(height int64) *types.Commit { return nil } 132 func (mockBlockStore) LoadSeenCommit(height int64) *types.Commit { return nil } 133 func (mockBlockStore) PruneBlocks(height int64) (uint64, error) { return 0, nil } 134 func (mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) { 135 }