Browse Source

rpc/core: more docs and a test for /blockchain endpoint (#5417)

Closes #5339
pull/5426/head
Anton Kaliaev 4 years ago
committed by GitHub
parent
commit
2672b91ab0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 6 deletions
  1. +30
    -0
      rpc/client/rpc_test.go
  2. +14
    -5
      rpc/core/blocks.go
  3. +6
    -1
      rpc/openapi/openapi.yaml

+ 30
- 0
rpc/client/rpc_test.go View File

@ -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)


+ 14
- 5
rpc/core/blocks.go View File

@ -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{


+ 6
- 1
rpc/openapi/openapi.yaml View File

@ -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).


Loading…
Cancel
Save