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.

127 lines
3.1 KiB

7 years ago
  1. package core
  2. import (
  3. "github.com/pkg/errors"
  4. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  5. )
  6. // Get network info.
  7. //
  8. // ```shell
  9. // curl 'localhost:26657/net_info'
  10. // ```
  11. //
  12. // ```go
  13. // client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
  14. // info, err := client.NetInfo()
  15. // ```
  16. //
  17. // > The above command returns JSON structured like this:
  18. //
  19. // ```json
  20. // {
  21. // "error": "",
  22. // "result": {
  23. // "n_peers": 0,
  24. // "peers": [],
  25. // "listeners": [
  26. // "Listener(@10.0.2.15:26656)"
  27. // ],
  28. // "listening": true
  29. // },
  30. // "id": "",
  31. // "jsonrpc": "2.0"
  32. // }
  33. // ```
  34. func NetInfo() (*ctypes.ResultNetInfo, error) {
  35. listening := p2pSwitch.IsListening()
  36. listeners := []string{}
  37. for _, listener := range p2pSwitch.Listeners() {
  38. listeners = append(listeners, listener.String())
  39. }
  40. peers := []ctypes.Peer{}
  41. for _, peer := range p2pSwitch.Peers().List() {
  42. peers = append(peers, ctypes.Peer{
  43. NodeInfo: peer.NodeInfo(),
  44. IsOutbound: peer.IsOutbound(),
  45. ConnectionStatus: peer.Status(),
  46. })
  47. }
  48. // TODO: Should we include PersistentPeers and Seeds in here?
  49. // PRO: useful info
  50. // CON: privacy
  51. return &ctypes.ResultNetInfo{
  52. Listening: listening,
  53. Listeners: listeners,
  54. NPeers: len(peers),
  55. Peers: peers,
  56. }, nil
  57. }
  58. func UnsafeDialSeeds(seeds []string) (*ctypes.ResultDialSeeds, error) {
  59. if len(seeds) == 0 {
  60. return &ctypes.ResultDialSeeds{}, errors.New("No seeds provided")
  61. }
  62. // starts go routines to dial each peer after random delays
  63. logger.Info("DialSeeds", "addrBook", addrBook, "seeds", seeds)
  64. err := p2pSwitch.DialPeersAsync(addrBook, seeds, false)
  65. if err != nil {
  66. return &ctypes.ResultDialSeeds{}, err
  67. }
  68. return &ctypes.ResultDialSeeds{"Dialing seeds in progress. See /net_info for details"}, nil
  69. }
  70. func UnsafeDialPeers(peers []string, persistent bool) (*ctypes.ResultDialPeers, error) {
  71. if len(peers) == 0 {
  72. return &ctypes.ResultDialPeers{}, errors.New("No peers provided")
  73. }
  74. // starts go routines to dial each peer after random delays
  75. logger.Info("DialPeers", "addrBook", addrBook, "peers", peers, "persistent", persistent)
  76. err := p2pSwitch.DialPeersAsync(addrBook, peers, persistent)
  77. if err != nil {
  78. return &ctypes.ResultDialPeers{}, err
  79. }
  80. return &ctypes.ResultDialPeers{"Dialing peers in progress. See /net_info for details"}, nil
  81. }
  82. // Get genesis file.
  83. //
  84. // ```shell
  85. // curl 'localhost:26657/genesis'
  86. // ```
  87. //
  88. // ```go
  89. // client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
  90. // genesis, err := client.Genesis()
  91. // ```
  92. //
  93. // > The above command returns JSON structured like this:
  94. //
  95. // ```json
  96. // {
  97. // "error": "",
  98. // "result": {
  99. // "genesis": {
  100. // "app_hash": "",
  101. // "validators": [
  102. // {
  103. // "name": "",
  104. // "power": 10,
  105. // "pub_key": {
  106. // "data": "68DFDA7E50F82946E7E8546BED37944A422CD1B831E70DF66BA3B8430593944D",
  107. // "type": "ed25519"
  108. // }
  109. // }
  110. // ],
  111. // "chain_id": "test-chain-6UTNIN",
  112. // "genesis_time": "2017-05-29T15:05:41.671Z"
  113. // }
  114. // },
  115. // "id": "",
  116. // "jsonrpc": "2.0"
  117. // }
  118. // ```
  119. func Genesis() (*ctypes.ResultGenesis, error) {
  120. return &ctypes.ResultGenesis{genDoc}, nil
  121. }