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.

133 lines
3.4 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: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. // "syncing": 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. latestHeight := blockStore.Height()
  65. var (
  66. latestBlockMeta *types.BlockMeta
  67. latestBlockHash cmn.HexBytes
  68. latestAppHash cmn.HexBytes
  69. latestBlockTimeNano int64
  70. )
  71. if latestHeight != 0 {
  72. latestBlockMeta = blockStore.LoadBlockMeta(latestHeight)
  73. latestBlockHash = latestBlockMeta.BlockID.Hash
  74. latestAppHash = latestBlockMeta.Header.AppHash
  75. latestBlockTimeNano = latestBlockMeta.Header.Time.UnixNano()
  76. }
  77. latestBlockTime := time.Unix(0, latestBlockTimeNano)
  78. var votingPower int64
  79. if val := validatorAtHeight(latestHeight); val != nil {
  80. votingPower = val.VotingPower
  81. }
  82. result := &ctypes.ResultStatus{
  83. NodeInfo: p2pSwitch.NodeInfo(),
  84. SyncInfo: ctypes.SyncInfo{
  85. LatestBlockHash: latestBlockHash,
  86. LatestAppHash: latestAppHash,
  87. LatestBlockHeight: latestHeight,
  88. LatestBlockTime: latestBlockTime,
  89. Syncing: consensusReactor.FastSync(),
  90. },
  91. ValidatorInfo: ctypes.ValidatorInfo{
  92. Address: pubKey.Address(),
  93. PubKey: pubKey,
  94. VotingPower: votingPower,
  95. },
  96. }
  97. return result, nil
  98. }
  99. func validatorAtHeight(h int64) *types.Validator {
  100. lastBlockHeight, vals := consensusState.GetValidators()
  101. privValAddress := pubKey.Address()
  102. // if we're still at height h, search in the current validator set
  103. if lastBlockHeight == h {
  104. for _, val := range vals {
  105. if bytes.Equal(val.Address, privValAddress) {
  106. return val
  107. }
  108. }
  109. }
  110. // if we've moved to the next height, retrieve the validator set from DB
  111. if lastBlockHeight > h {
  112. vals, err := sm.LoadValidators(stateDB, h)
  113. if err != nil {
  114. // should not happen
  115. return nil
  116. }
  117. _, val := vals.GetByAddress(privValAddress)
  118. return val
  119. }
  120. return nil
  121. }