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.

138 lines
4.0 KiB

7 years ago
  1. package core
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "github.com/tendermint/tendermint/p2p"
  7. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  8. rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  9. )
  10. // NetInfo returns network info.
  11. // More: https://docs.tendermint.com/master/rpc/#/Info/net_info
  12. func (env *Environment) NetInfo(ctx *rpctypes.Context) (*ctypes.ResultNetInfo, error) {
  13. peersList := env.P2PPeers.Peers().List()
  14. peers := make([]ctypes.Peer, 0, len(peersList))
  15. for _, peer := range peersList {
  16. peers = append(peers, ctypes.Peer{
  17. NodeInfo: peer.NodeInfo(),
  18. IsOutbound: peer.IsOutbound(),
  19. ConnectionStatus: peer.Status(),
  20. RemoteIP: peer.RemoteIP().String(),
  21. })
  22. }
  23. // TODO: Should we include PersistentPeers and Seeds in here?
  24. // PRO: useful info
  25. // CON: privacy
  26. return &ctypes.ResultNetInfo{
  27. Listening: env.P2PTransport.IsListening(),
  28. Listeners: env.P2PTransport.Listeners(),
  29. NPeers: len(peers),
  30. Peers: peers,
  31. }, nil
  32. }
  33. // UnsafeDialSeeds dials the given seeds (comma-separated id@IP:PORT).
  34. func (env *Environment) UnsafeDialSeeds(ctx *rpctypes.Context, seeds []string) (*ctypes.ResultDialSeeds, error) {
  35. if len(seeds) == 0 {
  36. return &ctypes.ResultDialSeeds{}, fmt.Errorf("%w: no seeds provided", ctypes.ErrInvalidRequest)
  37. }
  38. env.Logger.Info("DialSeeds", "seeds", seeds)
  39. if err := env.P2PPeers.DialPeersAsync(seeds); err != nil {
  40. return &ctypes.ResultDialSeeds{}, err
  41. }
  42. return &ctypes.ResultDialSeeds{Log: "Dialing seeds in progress. See /net_info for details"}, nil
  43. }
  44. // UnsafeDialPeers dials the given peers (comma-separated id@IP:PORT),
  45. // optionally making them persistent.
  46. func (env *Environment) UnsafeDialPeers(
  47. ctx *rpctypes.Context,
  48. peers []string,
  49. persistent, unconditional, private bool) (*ctypes.ResultDialPeers, error) {
  50. if len(peers) == 0 {
  51. return &ctypes.ResultDialPeers{}, fmt.Errorf("%w: no peers provided", ctypes.ErrInvalidRequest)
  52. }
  53. ids, err := getIDs(peers)
  54. if err != nil {
  55. return &ctypes.ResultDialPeers{}, err
  56. }
  57. env.Logger.Info("DialPeers", "peers", peers, "persistent",
  58. persistent, "unconditional", unconditional, "private", private)
  59. if persistent {
  60. if err := env.P2PPeers.AddPersistentPeers(peers); err != nil {
  61. return &ctypes.ResultDialPeers{}, err
  62. }
  63. }
  64. if private {
  65. if err := env.P2PPeers.AddPrivatePeerIDs(ids); err != nil {
  66. return &ctypes.ResultDialPeers{}, err
  67. }
  68. }
  69. if unconditional {
  70. if err := env.P2PPeers.AddUnconditionalPeerIDs(ids); err != nil {
  71. return &ctypes.ResultDialPeers{}, err
  72. }
  73. }
  74. if err := env.P2PPeers.DialPeersAsync(peers); err != nil {
  75. return &ctypes.ResultDialPeers{}, err
  76. }
  77. return &ctypes.ResultDialPeers{Log: "Dialing peers in progress. See /net_info for details"}, nil
  78. }
  79. // Genesis returns genesis file.
  80. // More: https://docs.tendermint.com/master/rpc/#/Info/genesis
  81. func (env *Environment) Genesis(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error) {
  82. if len(env.genChunks) > 1 {
  83. return nil, errors.New("genesis response is large, please use the genesis_chunked API instead")
  84. }
  85. return &ctypes.ResultGenesis{Genesis: env.GenDoc}, nil
  86. }
  87. func (env *Environment) GenesisChunked(ctx *rpctypes.Context, chunk uint) (*ctypes.ResultGenesisChunk, error) {
  88. if env.genChunks == nil {
  89. return nil, fmt.Errorf("service configuration error, genesis chunks are not initialized")
  90. }
  91. if len(env.genChunks) == 0 {
  92. return nil, fmt.Errorf("service configuration error, there are no chunks")
  93. }
  94. id := int(chunk)
  95. if id > len(env.genChunks)-1 {
  96. return nil, fmt.Errorf("there are %d chunks, %d is invalid", len(env.genChunks)-1, id)
  97. }
  98. return &ctypes.ResultGenesisChunk{
  99. TotalChunks: len(env.genChunks),
  100. ChunkNumber: id,
  101. Data: env.genChunks[id],
  102. }, nil
  103. }
  104. func getIDs(peers []string) ([]string, error) {
  105. ids := make([]string, 0, len(peers))
  106. for _, peer := range peers {
  107. spl := strings.Split(peer, "@")
  108. if len(spl) != 2 {
  109. return nil, p2p.ErrNetAddressNoID{Addr: peer}
  110. }
  111. ids = append(ids, spl[0])
  112. }
  113. return ids, nil
  114. }