From 2672b91ab099b8b02f3afabae4a0a745acd93c3f Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Mon, 28 Sep 2020 19:13:00 +0400 Subject: [PATCH] rpc/core: more docs and a test for /blockchain endpoint (#5417) Closes #5339 --- rpc/client/rpc_test.go | 30 ++++++++++++++++++++++++++++++ rpc/core/blocks.go | 19 ++++++++++++++----- rpc/openapi/openapi.yaml | 7 ++++++- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/rpc/client/rpc_test.go b/rpc/client/rpc_test.go index 0ecc03db0..d57df7c6c 100644 --- a/rpc/client/rpc_test.go +++ b/rpc/client/rpc_test.go @@ -302,6 +302,36 @@ func TestAppCalls(t *testing.T) { } } +func TestBlockchainInfo(t *testing.T) { + for i, c := range GetClients() { + err := client.WaitForHeight(c, 10, nil) + require.NoError(t, err) + + res, err := c.BlockchainInfo(context.Background(), 0, 0) + require.Nil(t, err, "%d: %+v", i, err) + assert.True(t, res.LastHeight > 0) + assert.True(t, len(res.BlockMetas) > 0) + + res, err = c.BlockchainInfo(context.Background(), 1, 1) + require.Nil(t, err, "%d: %+v", i, err) + assert.True(t, res.LastHeight > 0) + assert.True(t, len(res.BlockMetas) == 1) + + res, err = c.BlockchainInfo(context.Background(), 1, 10000) + require.Nil(t, err, "%d: %+v", i, err) + assert.True(t, res.LastHeight > 0) + assert.True(t, len(res.BlockMetas) < 100) + for _, m := range res.BlockMetas { + assert.NotNil(t, m) + } + + res, err = c.BlockchainInfo(context.Background(), 10000, 1) + require.NotNil(t, err) + assert.Nil(t, res) + assert.Contains(t, err.Error(), "can't be greater than max") + } +} + func TestBroadcastTxSync(t *testing.T) { require := require.New(t) diff --git a/rpc/core/blocks.go b/rpc/core/blocks.go index e5a3f568f..c5b2d4928 100644 --- a/rpc/core/blocks.go +++ b/rpc/core/blocks.go @@ -10,11 +10,18 @@ import ( ) // BlockchainInfo gets block headers for minHeight <= height <= maxHeight. -// Block headers are returned in descending order (highest first). +// +// If maxHeight does not yet exist, blocks up to the current height will be +// returned. If minHeight does not exist (due to pruning), earliest existing +// height will be used. +// +// At most 20 items will be returned. Block headers are returned in descending +// order (highest first). +// // More: https://docs.tendermint.com/master/rpc/#/Info/blockchain func BlockchainInfo(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) { - // maximum 20 block metas const limit int64 = 20 + var err error minHeight, maxHeight, err = filterMinMax( env.BlockStore.Base(), @@ -25,12 +32,14 @@ func BlockchainInfo(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes. if err != nil { return nil, err } - env.Logger.Debug("BlockchainInfoHandler", "maxHeight", maxHeight, "minHeight", minHeight) + env.Logger.Debug("BlockchainInfo", "maxHeight", maxHeight, "minHeight", minHeight) - blockMetas := []*types.BlockMeta{} + blockMetas := make([]*types.BlockMeta, 0, maxHeight-minHeight+1) for height := maxHeight; height >= minHeight; height-- { blockMeta := env.BlockStore.LoadBlockMeta(height) - blockMetas = append(blockMetas, blockMeta) + if blockMeta != nil { + blockMetas = append(blockMetas, blockMeta) + } } return &ctypes.ResultBlockchainInfo{ diff --git a/rpc/openapi/openapi.yaml b/rpc/openapi/openapi.yaml index 14d354b05..2956b0fe5 100644 --- a/rpc/openapi/openapi.yaml +++ b/rpc/openapi/openapi.yaml @@ -625,7 +625,12 @@ paths: description: | Get block headers for minHeight <= height maxHeight. - At most 20 items will be returned. + If maxHeight does not yet exist, blocks up to the current height will + be returned. If minHeight does not exist (due to pruning), earliest + existing height will be used. + + At most 20 items will be returned. Block headers are returned in + descending order (highest first). responses: "200": description: Block headers, returned in descending order (highest first).