mirror of
https://github.com/tendermint/tendermint.git
synced 2026-07-27 02:22:40 +00:00
rpc/core: more docs and a test for /blockchain endpoint (#5417)
Closes #5339
This commit is contained in:
@@ -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
@@ -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{
|
||||
|
||||
@@ -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).
|
||||
|
||||
Reference in New Issue
Block a user