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.

126 lines
3.2 KiB

  1. package core
  2. import (
  3. "bytes"
  4. "time"
  5. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  6. sm "github.com/tendermint/tendermint/state"
  7. "github.com/tendermint/tendermint/types"
  8. cmn "github.com/tendermint/tmlibs/common"
  9. )
  10. // Get Tendermint status including node info, pubkey, latest block
  11. // hash, app hash, block height and time.
  12. //
  13. // ```shell
  14. // curl 'localhost:46657/status'
  15. // ```
  16. //
  17. // ```go
  18. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  19. // result, err := client.Status()
  20. // ```
  21. //
  22. // > The above command returns JSON structured like this:
  23. //
  24. // ```json
  25. // {
  26. // "result": {
  27. // "syncing": false,
  28. // "latest_block_time": "2017-12-07T18:19:47.617Z",
  29. // "latest_block_height": 6,
  30. // "latest_app_hash": "",
  31. // "latest_block_hash": "A63D0C3307DEDCCFCC82ED411AE9108B70B29E02",
  32. // "pub_key": {
  33. // "data": "8C9A68070CBE33F9C445862BA1E9D96A75CEB68C0CF6ADD3652D07DCAC5D0380",
  34. // "type": "ed25519"
  35. // },
  36. // "node_info": {
  37. // "other": [
  38. // "wire_version=0.7.2",
  39. // "p2p_version=0.5.0",
  40. // "consensus_version=v1/0.2.2",
  41. // "rpc_version=0.7.0/3",
  42. // "tx_index=on",
  43. // "rpc_addr=tcp://0.0.0.0:46657"
  44. // ],
  45. // "version": "0.13.0-14ccc8b",
  46. // "listen_addr": "10.0.2.15:46656",
  47. // "remote_addr": "",
  48. // "network": "test-chain-qhVCa2",
  49. // "moniker": "vagrant-ubuntu-trusty-64",
  50. // "pub_key": "844981FE99ABB19F7816F2D5E94E8A74276AB1153760A7799E925C75401856C6",
  51. // "validator_status": {
  52. // "voting_power": 10
  53. // }
  54. // }
  55. // },
  56. // "id": "",
  57. // "jsonrpc": "2.0"
  58. // }
  59. // ```
  60. func Status() (*ctypes.ResultStatus, error) {
  61. latestHeight := blockStore.Height()
  62. var (
  63. latestBlockMeta *types.BlockMeta
  64. latestBlockHash cmn.HexBytes
  65. latestAppHash cmn.HexBytes
  66. latestBlockTimeNano int64
  67. )
  68. if latestHeight != 0 {
  69. latestBlockMeta = blockStore.LoadBlockMeta(latestHeight)
  70. latestBlockHash = latestBlockMeta.BlockID.Hash
  71. latestAppHash = latestBlockMeta.Header.AppHash
  72. latestBlockTimeNano = latestBlockMeta.Header.Time.UnixNano()
  73. }
  74. latestBlockTime := time.Unix(0, latestBlockTimeNano)
  75. result := &ctypes.ResultStatus{
  76. NodeInfo: p2pSwitch.NodeInfo(),
  77. PubKey: pubKey,
  78. LatestBlockHash: latestBlockHash,
  79. LatestAppHash: latestAppHash,
  80. LatestBlockHeight: latestHeight,
  81. LatestBlockTime: latestBlockTime,
  82. Syncing: consensusReactor.FastSync(),
  83. }
  84. // add ValidatorStatus if node is a validator
  85. if val := validatorAtHeight(latestHeight); val != nil {
  86. result.ValidatorStatus = ctypes.ValidatorStatus{
  87. VotingPower: val.VotingPower,
  88. }
  89. }
  90. return result, nil
  91. }
  92. func validatorAtHeight(h int64) *types.Validator {
  93. lastBlockHeight, vals := consensusState.GetValidators()
  94. privValAddress := pubKey.Address()
  95. // if we're still at height h, search in the current validator set
  96. if lastBlockHeight == h {
  97. for _, val := range vals {
  98. if bytes.Equal(val.Address, privValAddress) {
  99. return val
  100. }
  101. }
  102. }
  103. // if we've moved to the next height, retrieve the validator set from DB
  104. if lastBlockHeight > h {
  105. vals, err := sm.LoadValidators(stateDB, h)
  106. if err != nil {
  107. // should not happen
  108. return nil
  109. }
  110. _, val := vals.GetByAddress(privValAddress)
  111. return val
  112. }
  113. return nil
  114. }