tendermint/rpc/core/net.go

74 lines
2.1 KiB
Go
Raw Normal View History

package core
import (
"io/ioutil"
2015-04-01 17:30:16 -07:00
dbm "github.com/tendermint/tendermint/db"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
2015-04-01 17:30:16 -07:00
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
)
//-----------------------------------------------------------------------------
func Status() (*ctypes.ResponseStatus, error) {
db := dbm.NewMemDB()
genesisState := sm.MakeGenesisStateFromFile(db, config.GetString("genesis_file"))
genesisHash := genesisState.Hash()
latestHeight := blockStore.Height()
var (
latestBlockMeta *types.BlockMeta
latestBlockHash []byte
latestBlockTime int64
)
if latestHeight != 0 {
latestBlockMeta = blockStore.LoadBlockMeta(latestHeight)
latestBlockHash = latestBlockMeta.Hash
latestBlockTime = latestBlockMeta.Header.Time.UnixNano()
}
2015-05-12 19:03:05 -07:00
return &ctypes.ResponseStatus{
Moniker: config.GetString("moniker"),
ChainID: config.GetString("chain_id"),
Version: config.GetString("version"),
2015-05-12 19:03:05 -07:00
GenesisHash: genesisHash,
PubKey: privValidator.PubKey,
LatestBlockHash: latestBlockHash,
LatestBlockHeight: latestHeight,
LatestBlockTime: latestBlockTime}, nil
}
//-----------------------------------------------------------------------------
func NetInfo() (*ctypes.ResponseNetInfo, error) {
listening := p2pSwitch.IsListening()
listeners := []string{}
for _, listener := range p2pSwitch.Listeners() {
listeners = append(listeners, listener.String())
}
2015-04-17 18:22:44 -07:00
peers := []ctypes.Peer{}
for _, peer := range p2pSwitch.Peers().List() {
2015-04-17 18:22:44 -07:00
peers = append(peers, ctypes.Peer{
NodeInfo: *peer.NodeInfo,
2015-04-17 18:22:44 -07:00
IsOutbound: peer.IsOutbound(),
})
}
return &ctypes.ResponseNetInfo{
Listening: listening,
Listeners: listeners,
Peers: peers,
}, nil
}
//-----------------------------------------------------------------------------
// returns pointer because the rpc-gen code returns nil (TODO!)
func Genesis() (*string, error) {
b, err := ioutil.ReadFile(config.GetString("genesis_file"))
if err != nil {
2015-05-29 14:53:57 -07:00
return nil, err
}
2015-05-29 14:53:57 -07:00
ret := string(b)
return &ret, nil
}