2014-11-30 18:31:44 -08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2015-01-06 15:51:41 -08:00
|
|
|
|
|
|
|
. "github.com/tendermint/tendermint/block"
|
|
|
|
. "github.com/tendermint/tendermint/common"
|
2014-11-30 18:31:44 -08:00
|
|
|
)
|
|
|
|
|
2015-01-06 15:51:41 -08:00
|
|
|
type BlockchainInfoResponse struct {
|
|
|
|
LastHeight uint
|
|
|
|
BlockMetas []*BlockMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-01-07 01:15:39 -08:00
|
|
|
minHeight = MaxUint(1, maxHeight-20)
|
2015-01-06 15:51:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
blockMetas := []*BlockMeta{}
|
|
|
|
for height := minHeight; height <= maxHeight; height++ {
|
|
|
|
blockMetas = append(blockMetas, blockStore.LoadBlockMeta(height))
|
|
|
|
}
|
|
|
|
|
|
|
|
res := BlockchainInfoResponse{
|
|
|
|
LastHeight: blockStore.Height(),
|
|
|
|
BlockMetas: blockMetas,
|
|
|
|
}
|
2014-11-30 18:31:44 -08:00
|
|
|
|
2015-01-06 15:51:41 -08:00
|
|
|
WriteAPIResponse(w, API_OK, res)
|
|
|
|
return
|
2014-11-30 18:31:44 -08:00
|
|
|
}
|
2015-01-07 01:15:39 -08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func BlockHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
block := blockStore.LoadBlock(height)
|
|
|
|
WriteAPIResponse(w, API_OK, block)
|
|
|
|
return
|
|
|
|
}
|