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.

136 lines
3.5 KiB

  1. package core
  2. import (
  3. "bytes"
  4. "time"
  5. cmn "github.com/tendermint/tendermint/libs/common"
  6. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  7. sm "github.com/tendermint/tendermint/state"
  8. "github.com/tendermint/tendermint/types"
  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:26657/status'
  15. // ```
  16. //
  17. // ```go
  18. // client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
  19. // result, err := client.Status()
  20. // ```
  21. //
  22. // > The above command returns JSON structured like this:
  23. //
  24. // ```json
  25. // {
  26. // "jsonrpc": "2.0",
  27. // "id": "",
  28. // "result": {
  29. // "node_info": {
  30. // "id": "53729852020041b956e86685e24394e0bee4373f",
  31. // "listen_addr": "10.0.2.15:26656",
  32. // "network": "test-chain-Y1OHx6",
  33. // "version": "0.24.0-2ce1abc2",
  34. // "channels": "4020212223303800",
  35. // "moniker": "ubuntu-xenial",
  36. // "other": {
  37. // "amino_version": "0.12.0",
  38. // "p2p_version": "0.5.0",
  39. // "consensus_version": "v1/0.2.2",
  40. // "rpc_version": "0.7.0/3",
  41. // "tx_index": "on",
  42. // "rpc_addr": "tcp://0.0.0.0:26657"
  43. // }
  44. // },
  45. // "sync_info": {
  46. // "latest_block_hash": "F51538DA498299F4C57AC8162AAFA0254CE08286",
  47. // "latest_app_hash": "0000000000000000",
  48. // "latest_block_height": "18",
  49. // "latest_block_time": "2018-09-17T11:42:19.149920551Z",
  50. // "catching_up": false
  51. // },
  52. // "validator_info": {
  53. // "address": "D9F56456D7C5793815D0E9AF07C3A355D0FC64FD",
  54. // "pub_key": {
  55. // "type": "tendermint/PubKeyEd25519",
  56. // "value": "wVxKNtEsJmR4vvh651LrVoRguPs+6yJJ9Bz174gw9DM="
  57. // },
  58. // "voting_power": "10"
  59. // }
  60. // }
  61. // }
  62. // ```
  63. func Status() (*ctypes.ResultStatus, error) {
  64. var latestHeight int64 = -1
  65. if consensusReactor.FastSync() {
  66. latestHeight = blockStore.Height()
  67. } else {
  68. latestHeight = consensusState.GetLastHeight()
  69. }
  70. var (
  71. latestBlockMeta *types.BlockMeta
  72. latestBlockHash cmn.HexBytes
  73. latestAppHash cmn.HexBytes
  74. latestBlockTimeNano int64
  75. )
  76. if latestHeight != 0 {
  77. latestBlockMeta = blockStore.LoadBlockMeta(latestHeight)
  78. latestBlockHash = latestBlockMeta.BlockID.Hash
  79. latestAppHash = latestBlockMeta.Header.AppHash
  80. latestBlockTimeNano = latestBlockMeta.Header.Time.UnixNano()
  81. }
  82. latestBlockTime := time.Unix(0, latestBlockTimeNano)
  83. var votingPower int64
  84. if val := validatorAtHeight(latestHeight); val != nil {
  85. votingPower = val.VotingPower
  86. }
  87. result := &ctypes.ResultStatus{
  88. NodeInfo: p2pTransport.NodeInfo(),
  89. SyncInfo: ctypes.SyncInfo{
  90. LatestBlockHash: latestBlockHash,
  91. LatestAppHash: latestAppHash,
  92. LatestBlockHeight: latestHeight,
  93. LatestBlockTime: latestBlockTime,
  94. CatchingUp: consensusReactor.FastSync(),
  95. },
  96. ValidatorInfo: ctypes.ValidatorInfo{
  97. Address: pubKey.Address(),
  98. PubKey: pubKey,
  99. VotingPower: votingPower,
  100. },
  101. }
  102. return result, nil
  103. }
  104. func validatorAtHeight(h int64) *types.Validator {
  105. privValAddress := pubKey.Address()
  106. // If we're still at height h, search in the current validator set.
  107. lastBlockHeight, vals := consensusState.GetValidators()
  108. if lastBlockHeight == h {
  109. for _, val := range vals {
  110. if bytes.Equal(val.Address, privValAddress) {
  111. return val
  112. }
  113. }
  114. }
  115. // If we've moved to the next height, retrieve the validator set from DB.
  116. if lastBlockHeight > h {
  117. vals, err := sm.LoadValidators(stateDB, h)
  118. if err != nil {
  119. return nil // should not happen
  120. }
  121. _, val := vals.GetByAddress(privValAddress)
  122. return val
  123. }
  124. return nil
  125. }