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.

92 lines
3.6 KiB

7 years ago
  1. package core
  2. import (
  3. cm "github.com/tendermint/tendermint/consensus"
  4. cstypes "github.com/tendermint/tendermint/consensus/types"
  5. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  6. "github.com/tendermint/tendermint/types"
  7. )
  8. // Get the validator set at the given block height.
  9. // If no height is provided, it will fetch the current validator set.
  10. //
  11. // ```shell
  12. // curl 'localhost:46657/validators'
  13. // ```
  14. //
  15. // ```go
  16. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  17. // state, err := client.Validators()
  18. // ```
  19. //
  20. // > The above command returns JSON structured like this:
  21. //
  22. // ```json
  23. // {
  24. // "error": "",
  25. // "result": {
  26. // "validators": [
  27. // {
  28. // "accum": 0,
  29. // "voting_power": 10,
  30. // "pub_key": {
  31. // "data": "68DFDA7E50F82946E7E8546BED37944A422CD1B831E70DF66BA3B8430593944D",
  32. // "type": "ed25519"
  33. // },
  34. // "address": "E89A51D60F68385E09E716D353373B11F8FACD62"
  35. // }
  36. // ],
  37. // "block_height": 5241
  38. // },
  39. // "id": "",
  40. // "jsonrpc": "2.0"
  41. // }
  42. // ```
  43. func Validators(heightPtr *int64) (*ctypes.ResultValidators, error) {
  44. if heightPtr == nil {
  45. blockHeight, validators := consensusState.GetValidators()
  46. return &ctypes.ResultValidators{blockHeight, validators}, nil
  47. }
  48. height := *heightPtr
  49. state := consensusState.GetState()
  50. validators, err := state.LoadValidators(height)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return &ctypes.ResultValidators{height, validators.Validators}, nil
  55. }
  56. // Dump consensus state.
  57. //
  58. // ```shell
  59. // curl 'localhost:46657/dump_consensus_state'
  60. // ```
  61. //
  62. // ```go
  63. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  64. // state, err := client.DumpConsensusState()
  65. // ```
  66. //
  67. // > The above command returns JSON structured like this:
  68. //
  69. // ```json
  70. // {
  71. // "error": "",
  72. // "result": {
  73. // "peer_round_states": [],
  74. // "round_state": "RoundState{\n H:3537 R:0 S:RoundStepNewHeight\n StartTime: 2017-05-31 12:32:31.178653883 +0000 UTC\n CommitTime: 2017-05-31 12:32:30.178653883 +0000 UTC\n Validators: ValidatorSet{\n Proposer: Validator{E89A51D60F68385E09E716D353373B11F8FACD62 {PubKeyEd25519{68DFDA7E50F82946E7E8546BED37944A422CD1B831E70DF66BA3B8430593944D}} VP:10 A:0}\n Validators:\n Validator{E89A51D60F68385E09E716D353373B11F8FACD62 {PubKeyEd25519{68DFDA7E50F82946E7E8546BED37944A422CD1B831E70DF66BA3B8430593944D}} VP:10 A:0}\n }\n Proposal: <nil>\n ProposalBlock: nil-PartSet nil-Block\n LockedRound: 0\n LockedBlock: nil-PartSet nil-Block\n Votes: HeightVoteSet{H:3537 R:0~0\n VoteSet{H:3537 R:0 T:1 +2/3:<nil> BA{1:_} map[]}\n VoteSet{H:3537 R:0 T:2 +2/3:<nil> BA{1:_} map[]}\n }\n LastCommit: VoteSet{H:3536 R:0 T:2 +2/3:B7F988FBCDC68F9320E346EECAA76E32F6054654:1:673BE7C01F74 BA{1:X} map[]}\n LastValidators: ValidatorSet{\n Proposer: Validator{E89A51D60F68385E09E716D353373B11F8FACD62 {PubKeyEd25519{68DFDA7E50F82946E7E8546BED37944A422CD1B831E70DF66BA3B8430593944D}} VP:10 A:0}\n Validators:\n Validator{E89A51D60F68385E09E716D353373B11F8FACD62 {PubKeyEd25519{68DFDA7E50F82946E7E8546BED37944A422CD1B831E70DF66BA3B8430593944D}} VP:10 A:0}\n }\n}"
  75. // },
  76. // "id": "",
  77. // "jsonrpc": "2.0"
  78. // }
  79. // ```
  80. func DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) {
  81. peerRoundStates := make(map[string]*cstypes.PeerRoundState)
  82. for _, peer := range p2pSwitch.Peers().List() {
  83. peerState := peer.Get(types.PeerStateKey).(*cm.PeerState)
  84. peerRoundState := peerState.GetRoundState()
  85. peerRoundStates[peer.Key()] = peerRoundState
  86. }
  87. return &ctypes.ResultDumpConsensusState{consensusState.GetRoundState(), peerRoundStates}, nil
  88. }