tendermint/rpc/blocks.go

56 lines
1.5 KiB
Go
Raw Normal View History

2014-11-30 18:31:44 -08:00
package rpc
import (
"net/http"
2015-01-06 15:51:41 -08:00
2015-01-15 22:43:15 -08:00
blk "github.com/tendermint/tendermint/block"
2015-01-06 15:51:41 -08:00
. "github.com/tendermint/tendermint/common"
2014-11-30 18:31:44 -08:00
)
2015-01-06 15:51:41 -08:00
func BlockchainInfoHandler(w http.ResponseWriter, r *http.Request) {
minHeight, _ := GetParamUint(r, "min_height")
maxHeight, _ := GetParamUint(r, "max_height")
if maxHeight == 0 {
maxHeight = blockStore.Height()
2015-01-07 01:15:39 -08:00
} else {
maxHeight = MinUint(blockStore.Height(), maxHeight)
2015-01-06 15:51:41 -08:00
}
if minHeight == 0 {
minHeight = uint(MaxInt(1, int(maxHeight)-20))
2015-01-06 15:51:41 -08:00
}
log.Debug("BlockchainInfoHandler", "maxHeight", maxHeight, "minHeight", minHeight)
2015-01-06 15:51:41 -08:00
2015-01-15 22:43:15 -08:00
blockMetas := []*blk.BlockMeta{}
for height := maxHeight; height >= minHeight; height-- {
2015-01-08 14:57:19 -08:00
blockMeta := blockStore.LoadBlockMeta(height)
blockMetas = append(blockMetas, blockMeta)
2015-01-06 15:51:41 -08:00
}
2015-01-11 23:12:33 -08:00
WriteAPIResponse(w, API_OK, struct {
LastHeight uint
2015-01-15 22:43:15 -08:00
BlockMetas []*blk.BlockMeta
2015-01-11 23:12:33 -08:00
}{blockStore.Height(), blockMetas})
2014-11-30 18:31:44 -08:00
}
2015-01-07 01:15:39 -08:00
//-----------------------------------------------------------------------------
2015-01-11 23:12:33 -08:00
func GetBlockHandler(w http.ResponseWriter, r *http.Request) {
2015-01-07 01:15:39 -08:00
height, _ := GetParamUint(r, "height")
if height == 0 {
WriteAPIResponse(w, API_INVALID_PARAM, "height must be greater than 1")
return
}
if height > blockStore.Height() {
WriteAPIResponse(w, API_INVALID_PARAM, "height must be less than the current blockchain height")
return
}
blockMeta := blockStore.LoadBlockMeta(height)
2015-01-15 22:43:15 -08:00
block := blockStore.LoadBlock(height)
2015-01-11 23:12:33 -08:00
WriteAPIResponse(w, API_OK, struct {
2015-01-15 22:43:15 -08:00
BlockMeta *blk.BlockMeta
Block *blk.Block
}{blockMeta, block})
2015-01-07 01:15:39 -08:00
}