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.

175 lines
4.3 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. const consensusTimeout = time.Second
  105. func validatorAtHeight(h int64) *types.Validator {
  106. lastBlockHeight, vals := getValidatorsWithTimeout(
  107. consensusState,
  108. consensusTimeout,
  109. )
  110. if lastBlockHeight == -1 {
  111. return nil
  112. }
  113. privValAddress := pubKey.Address()
  114. // If we're still at height h, search in the current validator set.
  115. if lastBlockHeight == h {
  116. for _, val := range vals {
  117. if bytes.Equal(val.Address, privValAddress) {
  118. return val
  119. }
  120. }
  121. }
  122. // If we've moved to the next height, retrieve the validator set from DB.
  123. if lastBlockHeight > h {
  124. vals, err := sm.LoadValidators(stateDB, h)
  125. if err != nil {
  126. return nil // should not happen
  127. }
  128. _, val := vals.GetByAddress(privValAddress)
  129. return val
  130. }
  131. return nil
  132. }
  133. type validatorRetriever interface {
  134. GetValidators() (int64, []*types.Validator)
  135. }
  136. // NOTE: Consensus might halt, but we still need to process RPC requests (at
  137. // least for endpoints whole output does not depend on consensus state).
  138. func getValidatorsWithTimeout(
  139. vr validatorRetriever,
  140. t time.Duration,
  141. ) (int64, []*types.Validator) {
  142. resultCh := make(chan struct {
  143. lastBlockHeight int64
  144. vals []*types.Validator
  145. })
  146. go func() {
  147. h, v := vr.GetValidators()
  148. resultCh <- struct {
  149. lastBlockHeight int64
  150. vals []*types.Validator
  151. }{h, v}
  152. }()
  153. select {
  154. case res := <-resultCh:
  155. return res.lastBlockHeight, res.vals
  156. case <-time.After(t):
  157. return -1, []*types.Validator{}
  158. }
  159. }