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.

139 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. })
  53. }
  54. // TODO: Should we include PersistentPeers and Seeds in here?
  55. // PRO: useful info
  56. // CON: privacy
  57. return &ctypes.ResultNetInfo{
  58. Listening: p2pTransport.IsListening(),
  59. Listeners: p2pTransport.Listeners(),
  60. NPeers: len(peers),
  61. Peers: peers,
  62. }, nil
  63. }
  64. func UnsafeDialSeeds(seeds []string) (*ctypes.ResultDialSeeds, error) {
  65. if len(seeds) == 0 {
  66. return &ctypes.ResultDialSeeds{}, errors.New("No seeds provided")
  67. }
  68. // starts go routines to dial each peer after random delays
  69. logger.Info("DialSeeds", "addrBook", addrBook, "seeds", seeds)
  70. err := p2pPeers.DialPeersAsync(addrBook, seeds, false)
  71. if err != nil {
  72. return &ctypes.ResultDialSeeds{}, err
  73. }
  74. return &ctypes.ResultDialSeeds{"Dialing seeds in progress. See /net_info for details"}, nil
  75. }
  76. func UnsafeDialPeers(peers []string, persistent bool) (*ctypes.ResultDialPeers, error) {
  77. if len(peers) == 0 {
  78. return &ctypes.ResultDialPeers{}, errors.New("No peers provided")
  79. }
  80. // starts go routines to dial each peer after random delays
  81. logger.Info("DialPeers", "addrBook", addrBook, "peers", peers, "persistent", persistent)
  82. err := p2pPeers.DialPeersAsync(addrBook, peers, persistent)
  83. if err != nil {
  84. return &ctypes.ResultDialPeers{}, err
  85. }
  86. return &ctypes.ResultDialPeers{"Dialing peers in progress. See /net_info for details"}, nil
  87. }
  88. // Get genesis file.
  89. //
  90. // ```shell
  91. // curl 'localhost:26657/genesis'
  92. // ```
  93. //
  94. // ```go
  95. // client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
  96. // err := client.Start()
  97. // if err != nil {
  98. // // handle error
  99. // }
  100. // defer client.Stop()
  101. // genesis, err := client.Genesis()
  102. // ```
  103. //
  104. // > The above command returns JSON structured like this:
  105. //
  106. // ```json
  107. // {
  108. // "error": "",
  109. // "result": {
  110. // "genesis": {
  111. // "app_hash": "",
  112. // "validators": [
  113. // {
  114. // "name": "",
  115. // "power": "10",
  116. // "pub_key": {
  117. // "data": "68DFDA7E50F82946E7E8546BED37944A422CD1B831E70DF66BA3B8430593944D",
  118. // "type": "ed25519"
  119. // }
  120. // }
  121. // ],
  122. // "chain_id": "test-chain-6UTNIN",
  123. // "genesis_time": "2017-05-29T15:05:41.671Z"
  124. // }
  125. // },
  126. // "id": "",
  127. // "jsonrpc": "2.0"
  128. // }
  129. // ```
  130. func Genesis() (*ctypes.ResultGenesis, error) {
  131. return &ctypes.ResultGenesis{genDoc}, nil
  132. }