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.

138 lines
3.5 KiB

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