mirror of
https://github.com/tendermint/tendermint.git
synced 2026-02-04 02:52:07 +00:00
No need to duplicate information in this case. It a) requires extra efforts to keep both in sync b) nobody reads godoc documentation anyways.
147 lines
4.3 KiB
Go
147 lines
4.3 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
|
|
sm "github.com/tendermint/tendermint/state"
|
|
"github.com/tendermint/tendermint/types"
|
|
)
|
|
|
|
// BlockchainInfo gets block headers for minHeight <= height <= maxHeight.
|
|
// Block headers are returned in descending order (highest first).
|
|
// More: https://tendermint.com/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(blockStore.Height(), minHeight, maxHeight, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
logger.Debug("BlockchainInfoHandler", "maxHeight", maxHeight, "minHeight", minHeight)
|
|
|
|
blockMetas := []*types.BlockMeta{}
|
|
for height := maxHeight; height >= minHeight; height-- {
|
|
blockMeta := blockStore.LoadBlockMeta(height)
|
|
blockMetas = append(blockMetas, blockMeta)
|
|
}
|
|
|
|
return &ctypes.ResultBlockchainInfo{
|
|
LastHeight: blockStore.Height(),
|
|
BlockMetas: blockMetas}, nil
|
|
}
|
|
|
|
// error if either min or max are negative or min < max
|
|
// if 0, use 1 for min, latest block height for max
|
|
// enforce limit.
|
|
// error if min > max
|
|
func filterMinMax(height, min, max, limit int64) (int64, int64, error) {
|
|
// filter negatives
|
|
if min < 0 || max < 0 {
|
|
return min, max, fmt.Errorf("heights must be non-negative")
|
|
}
|
|
|
|
// adjust for default values
|
|
if min == 0 {
|
|
min = 1
|
|
}
|
|
if max == 0 {
|
|
max = height
|
|
}
|
|
|
|
// limit max to the height
|
|
max = cmn.MinInt64(height, max)
|
|
|
|
// limit min to within `limit` of max
|
|
// so the total number of blocks returned will be `limit`
|
|
min = cmn.MaxInt64(min, max-limit+1)
|
|
|
|
if min > max {
|
|
return min, max, fmt.Errorf("min height %d can't be greater than max height %d", min, max)
|
|
}
|
|
return min, max, nil
|
|
}
|
|
|
|
// Block gets block at a given height.
|
|
// If no height is provided, it will fetch the latest block.
|
|
// More: https://tendermint.com/rpc/#/Info/block
|
|
func Block(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultBlock, error) {
|
|
storeHeight := blockStore.Height()
|
|
height, err := getHeight(storeHeight, heightPtr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
blockMeta := blockStore.LoadBlockMeta(height)
|
|
block := blockStore.LoadBlock(height)
|
|
return &ctypes.ResultBlock{BlockMeta: blockMeta, Block: block}, nil
|
|
}
|
|
|
|
// Commit gets block commit at a given height.
|
|
// If no height is provided, it will fetch the commit for the latest block.
|
|
// More: https://tendermint.com/rpc/#/Info/commit
|
|
func Commit(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultCommit, error) {
|
|
storeHeight := blockStore.Height()
|
|
height, err := getHeight(storeHeight, heightPtr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
header := blockStore.LoadBlockMeta(height).Header
|
|
|
|
// If the next block has not been committed yet,
|
|
// use a non-canonical commit
|
|
if height == storeHeight {
|
|
commit := blockStore.LoadSeenCommit(height)
|
|
return ctypes.NewResultCommit(&header, commit, false), nil
|
|
}
|
|
|
|
// Return the canonical commit (comes from the block at height+1)
|
|
commit := blockStore.LoadBlockCommit(height)
|
|
return ctypes.NewResultCommit(&header, commit, true), nil
|
|
}
|
|
|
|
// BlockResults gets ABCIResults at a given height.
|
|
// If no height is provided, it will fetch results for the latest block.
|
|
//
|
|
// Results are for the height of the block containing the txs.
|
|
// Thus response.results.deliver_tx[5] is the results of executing
|
|
// getBlock(h).Txs[5]
|
|
// More: https://tendermint.com/rpc/#/Info/block_results
|
|
func BlockResults(ctx *rpctypes.Context, heightPtr *int64) (*ctypes.ResultBlockResults, error) {
|
|
storeHeight := blockStore.Height()
|
|
height, err := getHeight(storeHeight, heightPtr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
results, err := sm.LoadABCIResponses(stateDB, height)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res := &ctypes.ResultBlockResults{
|
|
Height: height,
|
|
Results: results,
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func getHeight(currentHeight int64, heightPtr *int64) (int64, error) {
|
|
if heightPtr != nil {
|
|
height := *heightPtr
|
|
if height <= 0 {
|
|
return 0, fmt.Errorf("Height must be greater than 0")
|
|
}
|
|
if height > currentHeight {
|
|
return 0, fmt.Errorf("Height must be less than or equal to the current blockchain height")
|
|
}
|
|
return height, nil
|
|
}
|
|
return currentHeight, nil
|
|
}
|