discover.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Contains all the wrappers from the accounts package to support client side enode
  17. // management on mobile platforms.
  18. package geth
  19. import (
  20. "errors"
  21. "github.com/ethereum/go-ethereum/cmd/utils"
  22. "github.com/ethereum/go-ethereum/p2p/discover"
  23. )
  24. // MainnetBootnodes returns the enode URLs of the P2P bootstrap nodes running
  25. // on the main network.
  26. //
  27. // Note, this needs to be a method to prevent gomobile generating a setter for it.
  28. func MainnetBootnodes() *Enodes {
  29. nodes := &Enodes{nodes: make([]*discover.Node, len(utils.MainnetBootnodes))}
  30. for i, node := range utils.MainnetBootnodes {
  31. nodes.nodes[i] = node
  32. }
  33. return nodes
  34. }
  35. // TestnetBootnodes returns the enode URLs of the P2P bootstrap nodes running
  36. // on the test network.
  37. //
  38. // Note, this needs to be a method to prevent gomobile generating a setter for it.
  39. func TestnetBootnodes() *Enodes {
  40. nodes := &Enodes{nodes: make([]*discover.Node, len(utils.TestnetBootnodes))}
  41. for i, node := range utils.TestnetBootnodes {
  42. nodes.nodes[i] = node
  43. }
  44. return nodes
  45. }
  46. // Enode represents a host on the network.
  47. type Enode struct {
  48. node *discover.Node
  49. }
  50. // NewEnode parses a node designator.
  51. //
  52. // There are two basic forms of node designators
  53. // - incomplete nodes, which only have the public key (node ID)
  54. // - complete nodes, which contain the public key and IP/Port information
  55. //
  56. // For incomplete nodes, the designator must look like one of these
  57. //
  58. // enode://<hex node id>
  59. // <hex node id>
  60. //
  61. // For complete nodes, the node ID is encoded in the username portion
  62. // of the URL, separated from the host by an @ sign. The hostname can
  63. // only be given as an IP address, DNS domain names are not allowed.
  64. // The port in the host name section is the TCP listening port. If the
  65. // TCP and UDP (discovery) ports differ, the UDP port is specified as
  66. // query parameter "discport".
  67. //
  68. // In the following example, the node URL describes
  69. // a node with IP address 10.3.58.6, TCP listening port 30303
  70. // and UDP discovery port 30301.
  71. //
  72. // enode://<hex node id>@10.3.58.6:30303?discport=30301
  73. func NewEnode(rawurl string) (*Enode, error) {
  74. node, err := discover.ParseNode(rawurl)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return &Enode{node}, nil
  79. }
  80. // Enodes represents a slice of accounts.
  81. type Enodes struct{ nodes []*discover.Node }
  82. // NewEnodes creates a slice of uninitialized enodes.
  83. func NewEnodes(size int) *Enodes {
  84. return &Enodes{
  85. nodes: make([]*discover.Node, size),
  86. }
  87. }
  88. // NewEnodesEmpty creates an empty slice of Enode values.
  89. func NewEnodesEmpty() *Enodes {
  90. return NewEnodes(0)
  91. }
  92. // Size returns the number of enodes in the slice.
  93. func (e *Enodes) Size() int {
  94. return len(e.nodes)
  95. }
  96. // Get returns the enode at the given index from the slice.
  97. func (e *Enodes) Get(index int) (*Enode, error) {
  98. if index < 0 || index >= len(e.nodes) {
  99. return nil, errors.New("index out of bounds")
  100. }
  101. return &Enode{e.nodes[index]}, nil
  102. }
  103. // Set sets the enode at the given index in the slice.
  104. func (e *Enodes) Set(index int, enode *Enode) error {
  105. if index < 0 || index >= len(e.nodes) {
  106. return errors.New("index out of bounds")
  107. }
  108. e.nodes[index] = enode.node
  109. return nil
  110. }
  111. // Append adds a new enode element to the end of the slice.
  112. func (e *Enodes) Append(enode *Enode) {
  113. e.nodes = append(e.nodes, enode.node)
  114. }