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.

121 lines
2.9 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:46657/net_info'
  10. // ```
  11. //
  12. // ```go
  13. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/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. // "peers": [],
  24. // "listeners": [
  25. // "Listener(@10.0.2.15:46656)"
  26. // ],
  27. // "listening": true
  28. // },
  29. // "id": "",
  30. // "jsonrpc": "2.0"
  31. // }
  32. // ```
  33. func NetInfo() (*ctypes.ResultNetInfo, error) {
  34. listening := p2pSwitch.IsListening()
  35. listeners := []string{}
  36. for _, listener := range p2pSwitch.Listeners() {
  37. listeners = append(listeners, listener.String())
  38. }
  39. peers := []ctypes.Peer{}
  40. for _, peer := range p2pSwitch.Peers().List() {
  41. peers = append(peers, ctypes.Peer{
  42. NodeInfo: peer.NodeInfo(),
  43. IsOutbound: peer.IsOutbound(),
  44. ConnectionStatus: peer.Status(),
  45. })
  46. }
  47. return &ctypes.ResultNetInfo{
  48. Listening: listening,
  49. Listeners: listeners,
  50. Peers: peers,
  51. }, nil
  52. }
  53. func UnsafeDialSeeds(seeds []string) (*ctypes.ResultDialSeeds, error) {
  54. if len(seeds) == 0 {
  55. return &ctypes.ResultDialSeeds{}, errors.New("No seeds provided")
  56. }
  57. // starts go routines to dial each peer after random delays
  58. logger.Info("DialSeeds", "addrBook", addrBook, "seeds", seeds)
  59. err := p2pSwitch.DialPeersAsync(addrBook, seeds, false)
  60. if err != nil {
  61. return &ctypes.ResultDialSeeds{}, err
  62. }
  63. return &ctypes.ResultDialSeeds{"Dialing seeds in progress. See /net_info for details"}, nil
  64. }
  65. func UnsafeDialPeers(peers []string, persistent bool) (*ctypes.ResultDialPeers, error) {
  66. if len(peers) == 0 {
  67. return &ctypes.ResultDialPeers{}, errors.New("No peers provided")
  68. }
  69. // starts go routines to dial each peer after random delays
  70. logger.Info("DialPeers", "addrBook", addrBook, "peers", peers, "persistent", persistent)
  71. err := p2pSwitch.DialPeersAsync(addrBook, peers, persistent)
  72. if err != nil {
  73. return &ctypes.ResultDialPeers{}, err
  74. }
  75. return &ctypes.ResultDialPeers{"Dialing peers in progress. See /net_info for details"}, nil
  76. }
  77. // Get genesis file.
  78. //
  79. // ```shell
  80. // curl 'localhost:46657/genesis'
  81. // ```
  82. //
  83. // ```go
  84. // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
  85. // genesis, err := client.Genesis()
  86. // ```
  87. //
  88. // > The above command returns JSON structured like this:
  89. //
  90. // ```json
  91. // {
  92. // "error": "",
  93. // "result": {
  94. // "genesis": {
  95. // "app_hash": "",
  96. // "validators": [
  97. // {
  98. // "name": "",
  99. // "power": 10,
  100. // "pub_key": {
  101. // "data": "68DFDA7E50F82946E7E8546BED37944A422CD1B831E70DF66BA3B8430593944D",
  102. // "type": "ed25519"
  103. // }
  104. // }
  105. // ],
  106. // "chain_id": "test-chain-6UTNIN",
  107. // "genesis_time": "2017-05-29T15:05:41.671Z"
  108. // }
  109. // },
  110. // "id": "",
  111. // "jsonrpc": "2.0"
  112. // }
  113. // ```
  114. func Genesis() (*ctypes.ResultGenesis, error) {
  115. return &ctypes.ResultGenesis{genDoc}, nil
  116. }