rpc: add status and net info

This commit is contained in:
Ethan Buchman 2015-03-16 22:30:32 -07:00 committed by Jae Kwon
parent f55609a792
commit 7aa1d67c97
4 changed files with 55 additions and 4 deletions

View File

@ -216,6 +216,10 @@ func (sw *Switch) StopPeerGracefully(peer *Peer) {
sw.doRemovePeer(peer, nil)
}
func (sw *Switch) IsListening() bool {
return sw.listeners.Size() > 0
}
func (sw *Switch) doAddPeer(peer *Peer) {
for _, reactor := range sw.reactors {
reactor.AddPeer(peer)

View File

@ -7,10 +7,18 @@ import (
func initHandlers() {
http.HandleFunc("/blockchain", BlockchainInfoHandler)
http.HandleFunc("/get_block", GetBlockHandler)
http.HandleFunc("/broadcast_tx", BroadcastTxHandler)
http.HandleFunc("/gen_priv_account", GenPrivAccountHandler)
http.HandleFunc("/develop/gen_priv_account", GenPrivAccountHandler)
http.HandleFunc("/get_account", GetAccountHandler)
http.HandleFunc("/list_accounts", ListAccountsHandler)
http.HandleFunc("/sign_tx", SignTxHandler)
http.HandleFunc("/develop/list_accounts", ListAccountsHandler)
http.HandleFunc("/broadcast_tx", BroadcastTxHandler)
http.HandleFunc("/list_validators", ListValidatorsHandler)
http.HandleFunc("/develop/sign_tx", SignTxHandler)
//http.HandleFunc("/call", CallHandler)
//http.HandleFunc("/get_storage", GetStorageHandler)
http.HandleFunc("/net_info", NetInfoHandler)
http.HandleFunc("/status", StatusHandler)
}

33
rpc/net.go Normal file
View File

@ -0,0 +1,33 @@
package rpc
import (
"github.com/tendermint/tendermint/config"
"net/http"
)
func StatusHandler(w http.ResponseWriter, r *http.Request) {
genesisHash := blockStore.LoadBlockMeta(0).Hash
latestHeight := blockStore.Height()
latestBlockMeta := blockStore.LoadBlockMeta(latestHeight)
latestBlockHash := latestBlockMeta.Hash
latestBlockTime := latestBlockMeta.Header.Time.UnixNano()
WriteAPIResponse(w, API_OK, struct {
GenesisHash []byte
LatestBlockHash []byte
LatestBlockHeight uint
LatestBlockTime int64 // nano
Network string
}{genesisHash, latestBlockHash, latestHeight, latestBlockTime, config.App.GetString("Network")})
}
func NetInfoHandler(w http.ResponseWriter, r *http.Request) {
o, i, _ := p2pSwitch.NumPeers()
numPeers := o + i
listening := p2pSwitch.IsListening()
network := config.App.GetString("Network")
WriteAPIResponse(w, API_OK, struct {
NumPeers int
Listening bool
Network string
}{numPeers, listening, network})
}

View File

@ -4,11 +4,13 @@ import (
blk "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/consensus"
mempl "github.com/tendermint/tendermint/mempool"
"github.com/tendermint/tendermint/p2p"
)
var blockStore *blk.BlockStore
var consensusState *consensus.ConsensusState
var mempoolReactor *mempl.MempoolReactor
var p2pSwitch *p2p.Switch
func SetRPCBlockStore(bs *blk.BlockStore) {
blockStore = bs
@ -21,3 +23,7 @@ func SetRPCConsensusState(cs *consensus.ConsensusState) {
func SetRPCMempoolReactor(mr *mempl.MempoolReactor) {
mempoolReactor = mr
}
func SetSwitch(sw *p2p.Switch) {
p2pSwitch = sw
}