tendermint/rpc/core/blocks.go

72 lines
2.2 KiB
Go
Raw Normal View History

package core
import (
"fmt"
. "github.com/tendermint/go-common"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/types"
)
//-----------------------------------------------------------------------------
2016-12-12 11:12:13 -08:00
// TODO: limit/permission on (max - min)
func BlockchainInfo(minHeight, maxHeight int) (*ctypes.ResultBlockchainInfo, error) {
if maxHeight == 0 {
maxHeight = blockStore.Height()
} else {
maxHeight = MinInt(blockStore.Height(), maxHeight)
}
if minHeight == 0 {
minHeight = MaxInt(1, maxHeight-20)
}
2016-10-14 17:27:50 -07:00
log.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{blockStore.Height(), blockMetas}, nil
}
//-----------------------------------------------------------------------------
2016-02-08 00:48:58 -08:00
func Block(height int) (*ctypes.ResultBlock, error) {
if height == 0 {
2015-07-11 18:01:21 -07:00
return nil, fmt.Errorf("Height must be greater than 0")
}
if height > blockStore.Height() {
2015-07-11 18:01:21 -07:00
return nil, fmt.Errorf("Height must be less than the current blockchain height")
}
blockMeta := blockStore.LoadBlockMeta(height)
block := blockStore.LoadBlock(height)
2016-02-08 00:48:58 -08:00
return &ctypes.ResultBlock{blockMeta, block}, nil
}
2017-02-09 19:44:43 -08:00
//-----------------------------------------------------------------------------
func Commit(height int) (*ctypes.ResultCommit, error) {
if height == 0 {
return nil, fmt.Errorf("Height must be greater than 0")
}
storeHeight := blockStore.Height()
if height > storeHeight {
return nil, fmt.Errorf("Height must be less than or equal to the current blockchain height")
}
2017-02-14 12:33:14 -08:00
header := blockStore.LoadBlockMeta(height).Header
2017-02-09 19:44:43 -08:00
// If the next block has not been committed yet,
// use a non-canonical commit
if height == storeHeight+1 {
commit := blockStore.LoadSeenCommit(height)
2017-02-14 12:33:14 -08:00
return &ctypes.ResultCommit{header, commit, false}, nil
2017-02-09 19:44:43 -08:00
}
// Return the canonical commit (comes from the block at height+1)
commit := blockStore.LoadBlockCommit(height)
2017-02-14 12:33:14 -08:00
return &ctypes.ResultCommit{header, commit, true}, nil
2017-02-09 19:44:43 -08:00
}