diff --git a/p2p/switch.go b/p2p/switch.go index 2a804ae8..b9696d24 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -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) diff --git a/rpc/http_handlers.go b/rpc/http_handlers.go index fccbec15..70e7046b 100644 --- a/rpc/http_handlers.go +++ b/rpc/http_handlers.go @@ -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) } diff --git a/rpc/net.go b/rpc/net.go new file mode 100644 index 00000000..f805cbad --- /dev/null +++ b/rpc/net.go @@ -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}) +} diff --git a/rpc/rpc.go b/rpc/rpc.go index 14d34112..718cb729 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -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 +}