You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.2 KiB

  1. package core
  2. import (
  3. "io/ioutil"
  4. "github.com/tendermint/tendermint/binary"
  5. dbm "github.com/tendermint/tendermint/db"
  6. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  7. sm "github.com/tendermint/tendermint/state"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. //-----------------------------------------------------------------------------
  11. func Status() (*ctypes.ResponseStatus, error) {
  12. db := dbm.NewMemDB()
  13. genesisState := sm.MakeGenesisStateFromFile(db, config.GetString("genesis_file"))
  14. genesisHash := genesisState.Hash()
  15. latestHeight := blockStore.Height()
  16. var (
  17. latestBlockMeta *types.BlockMeta
  18. latestBlockHash []byte
  19. latestBlockTime int64
  20. )
  21. if latestHeight != 0 {
  22. latestBlockMeta = blockStore.LoadBlockMeta(latestHeight)
  23. latestBlockHash = latestBlockMeta.Hash
  24. latestBlockTime = latestBlockMeta.Header.Time.UnixNano()
  25. }
  26. return &ctypes.ResponseStatus{
  27. Moniker: config.GetString("moniker"),
  28. ChainID: config.GetString("chain_id"),
  29. Version: config.GetString("version"),
  30. GenesisHash: genesisHash,
  31. PubKey: privValidator.PubKey,
  32. LatestBlockHash: latestBlockHash,
  33. LatestBlockHeight: latestHeight,
  34. LatestBlockTime: latestBlockTime}, nil
  35. }
  36. //-----------------------------------------------------------------------------
  37. func NetInfo() (*ctypes.ResponseNetInfo, error) {
  38. listening := p2pSwitch.IsListening()
  39. listeners := []string{}
  40. for _, listener := range p2pSwitch.Listeners() {
  41. listeners = append(listeners, listener.String())
  42. }
  43. peers := []ctypes.Peer{}
  44. for _, peer := range p2pSwitch.Peers().List() {
  45. peers = append(peers, ctypes.Peer{
  46. NodeInfo: *peer.NodeInfo,
  47. IsOutbound: peer.IsOutbound(),
  48. })
  49. }
  50. return &ctypes.ResponseNetInfo{
  51. Listening: listening,
  52. Listeners: listeners,
  53. Peers: peers,
  54. }, nil
  55. }
  56. //-----------------------------------------------------------------------------
  57. // cache the genesis structure
  58. var genDoc *sm.GenesisDoc
  59. func Genesis() (*sm.GenesisDoc, error) {
  60. if genDoc == nil {
  61. b, err := ioutil.ReadFile(config.GetString("genesis_file"))
  62. if err != nil {
  63. return nil, err
  64. }
  65. binary.ReadJSON(&genDoc, b, &err)
  66. if err != nil {
  67. return nil, err
  68. }
  69. }
  70. return genDoc, nil
  71. }