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.

162 lines
4.4 KiB

  1. package core
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "github.com/tendermint/tendermint/internal/p2p"
  7. ctypes "github.com/tendermint/tendermint/rpc/coretypes"
  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. var peers []ctypes.Peer
  14. switch {
  15. case env.P2PPeers != nil:
  16. peersList := env.P2PPeers.Peers().List()
  17. peers = make([]ctypes.Peer, 0, len(peersList))
  18. for _, peer := range peersList {
  19. peers = append(peers, ctypes.Peer{
  20. ID: peer.ID(),
  21. URL: peer.SocketAddr().String(),
  22. })
  23. }
  24. case env.PeerManager != nil:
  25. peerList := env.PeerManager.Peers()
  26. for _, peer := range peerList {
  27. addrs := env.PeerManager.Addresses(peer)
  28. if len(addrs) == 0 {
  29. continue
  30. }
  31. peers = append(peers, ctypes.Peer{
  32. ID: peer,
  33. URL: addrs[0].String(),
  34. })
  35. }
  36. default:
  37. return nil, errors.New("peer management system does not support NetInfo responses")
  38. }
  39. return &ctypes.ResultNetInfo{
  40. Listening: env.P2PTransport.IsListening(),
  41. Listeners: env.P2PTransport.Listeners(),
  42. NPeers: len(peers),
  43. Peers: peers,
  44. }, nil
  45. }
  46. // UnsafeDialSeeds dials the given seeds (comma-separated id@IP:PORT).
  47. func (env *Environment) UnsafeDialSeeds(ctx *rpctypes.Context, seeds []string) (*ctypes.ResultDialSeeds, error) {
  48. if env.P2PPeers == nil {
  49. return nil, errors.New("peer management system does not support this operation")
  50. }
  51. if len(seeds) == 0 {
  52. return &ctypes.ResultDialSeeds{}, fmt.Errorf("%w: no seeds provided", ctypes.ErrInvalidRequest)
  53. }
  54. env.Logger.Info("DialSeeds", "seeds", seeds)
  55. if err := env.P2PPeers.DialPeersAsync(seeds); err != nil {
  56. return &ctypes.ResultDialSeeds{}, err
  57. }
  58. return &ctypes.ResultDialSeeds{Log: "Dialing seeds in progress. See /net_info for details"}, nil
  59. }
  60. // UnsafeDialPeers dials the given peers (comma-separated id@IP:PORT),
  61. // optionally making them persistent.
  62. func (env *Environment) UnsafeDialPeers(
  63. ctx *rpctypes.Context,
  64. peers []string,
  65. persistent, unconditional, private bool) (*ctypes.ResultDialPeers, error) {
  66. if env.P2PPeers == nil {
  67. return nil, errors.New("peer management system does not support this operation")
  68. }
  69. if len(peers) == 0 {
  70. return &ctypes.ResultDialPeers{}, fmt.Errorf("%w: no peers provided", ctypes.ErrInvalidRequest)
  71. }
  72. ids, err := getIDs(peers)
  73. if err != nil {
  74. return &ctypes.ResultDialPeers{}, err
  75. }
  76. env.Logger.Info("DialPeers", "peers", peers, "persistent",
  77. persistent, "unconditional", unconditional, "private", private)
  78. if persistent {
  79. if err := env.P2PPeers.AddPersistentPeers(peers); err != nil {
  80. return &ctypes.ResultDialPeers{}, err
  81. }
  82. }
  83. if private {
  84. if err := env.P2PPeers.AddPrivatePeerIDs(ids); err != nil {
  85. return &ctypes.ResultDialPeers{}, err
  86. }
  87. }
  88. if unconditional {
  89. if err := env.P2PPeers.AddUnconditionalPeerIDs(ids); err != nil {
  90. return &ctypes.ResultDialPeers{}, err
  91. }
  92. }
  93. if err := env.P2PPeers.DialPeersAsync(peers); err != nil {
  94. return &ctypes.ResultDialPeers{}, err
  95. }
  96. return &ctypes.ResultDialPeers{Log: "Dialing peers in progress. See /net_info for details"}, nil
  97. }
  98. // Genesis returns genesis file.
  99. // More: https://docs.tendermint.com/master/rpc/#/Info/genesis
  100. func (env *Environment) Genesis(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error) {
  101. if len(env.genChunks) > 1 {
  102. return nil, errors.New("genesis response is large, please use the genesis_chunked API instead")
  103. }
  104. return &ctypes.ResultGenesis{Genesis: env.GenDoc}, nil
  105. }
  106. func (env *Environment) GenesisChunked(ctx *rpctypes.Context, chunk uint) (*ctypes.ResultGenesisChunk, error) {
  107. if env.genChunks == nil {
  108. return nil, fmt.Errorf("service configuration error, genesis chunks are not initialized")
  109. }
  110. if len(env.genChunks) == 0 {
  111. return nil, fmt.Errorf("service configuration error, there are no chunks")
  112. }
  113. id := int(chunk)
  114. if id > len(env.genChunks)-1 {
  115. return nil, fmt.Errorf("there are %d chunks, %d is invalid", len(env.genChunks)-1, id)
  116. }
  117. return &ctypes.ResultGenesisChunk{
  118. TotalChunks: len(env.genChunks),
  119. ChunkNumber: id,
  120. Data: env.genChunks[id],
  121. }, nil
  122. }
  123. func getIDs(peers []string) ([]string, error) {
  124. ids := make([]string, 0, len(peers))
  125. for _, peer := range peers {
  126. spl := strings.Split(peer, "@")
  127. if len(spl) != 2 {
  128. return nil, p2p.ErrNetAddressNoID{Addr: peer}
  129. }
  130. ids = append(ids, spl[0])
  131. }
  132. return ids, nil
  133. }