github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/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 abci "github.com/lazyledger/lazyledger-core/abci/types" 11 "github.com/lazyledger/lazyledger-core/libs/db/memdb" 12 tmstate "github.com/lazyledger/lazyledger-core/proto/tendermint/state" 13 ctypes "github.com/lazyledger/lazyledger-core/rpc/core/types" 14 rpctypes "github.com/lazyledger/lazyledger-core/rpc/jsonrpc/types" 15 sm "github.com/lazyledger/lazyledger-core/state" 16 "github.com/lazyledger/lazyledger-core/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 := &tmstate.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(memdb.NewDB()) 84 err := env.StateStore.SaveABCIResponses(100, results) 85 require.NoError(t, err) 86 env.BlockStore = mockBlockStore{height: 100} 87 88 testCases := []struct { 89 height int64 90 wantErr bool 91 wantRes *ctypes.ResultBlockResults 92 }{ 93 {-1, true, nil}, 94 {0, true, nil}, 95 {101, true, nil}, 96 {100, false, &ctypes.ResultBlockResults{ 97 Height: 100, 98 TxsResults: results.DeliverTxs, 99 BeginBlockEvents: results.BeginBlock.Events, 100 EndBlockEvents: results.EndBlock.Events, 101 ValidatorUpdates: results.EndBlock.ValidatorUpdates, 102 ConsensusParamUpdates: results.EndBlock.ConsensusParamUpdates, 103 }}, 104 } 105 106 for _, tc := range testCases { 107 res, err := BlockResults(&rpctypes.Context{}, &tc.height) 108 if tc.wantErr { 109 assert.Error(t, err) 110 } else { 111 assert.NoError(t, err) 112 assert.Equal(t, tc.wantRes, res) 113 } 114 } 115 } 116 117 type mockBlockStore struct { 118 height int64 119 } 120 121 func (mockBlockStore) Base() int64 { return 1 } 122 func (store mockBlockStore) Height() int64 { return store.height } 123 func (store mockBlockStore) Size() int64 { return store.height } 124 func (mockBlockStore) LoadBaseMeta() *types.BlockMeta { return nil } 125 func (mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta { return nil } 126 func (mockBlockStore) LoadBlock(height int64) *types.Block { return nil } 127 func (mockBlockStore) LoadBlockByHash(hash []byte) *types.Block { return nil } 128 func (mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil } 129 func (mockBlockStore) LoadBlockCommit(height int64) *types.Commit { return nil } 130 func (mockBlockStore) LoadSeenCommit(height int64) *types.Commit { return nil } 131 func (mockBlockStore) PruneBlocks(height int64) (uint64, error) { return 0, nil } 132 func (mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) { 133 }