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.

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