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": "562dd7f579f0ecee8c94a11a3c1e378c1876f433",
  31. // "listen_addr": "192.168.1.2:26656",
  32. // "network": "test-chain-I6zScH",
  33. // "version": "0.19.0",
  34. // "channels": "4020212223303800",
  35. // "moniker": "Ethans-MacBook-Pro.local",
  36. // "other": [
  37. // "amino_version=0.9.8",
  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": "2D4D7055BE685E3CB2410603C92AD37AE557AC59",
  47. // "latest_app_hash": "0000000000000000",
  48. // "latest_block_height": 231,
  49. // "latest_block_time": "2018-04-27T23:18:08.459766485-04:00",
  50. // "catching_up": false
  51. // },
  52. // "validator_info": {
  53. // "address": "5875562FF0FFDECC895C20E32FC14988952E99E7",
  54. // "pub_key": {
  55. // "type": "tendermint/PubKeyEd25519",
  56. // "value": "PpDJRUrLG2RgFqYYjawfn/AcAgacSXpLFrmfYYQnuzE="
  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: p2pSwitch.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. }