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.

140 lines
3.3 KiB

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