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.

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