connect.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2018 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. package simulations
  17. import (
  18. "errors"
  19. "strings"
  20. "github.com/ethereum/go-ethereum/p2p/enode"
  21. )
  22. var (
  23. ErrNodeNotFound = errors.New("node not found")
  24. ErrNoPivotNode = errors.New("no pivot node set")
  25. )
  26. // ConnectToPivotNode connects the node with provided NodeID
  27. // to the pivot node, already set by Network.SetPivotNode method.
  28. // It is useful when constructing a star network topology
  29. // when Network adds and removes nodes dynamically.
  30. func (net *Network) ConnectToPivotNode(id enode.ID) (err error) {
  31. pivot := net.GetPivotNode()
  32. if pivot == nil {
  33. return ErrNoPivotNode
  34. }
  35. return net.connect(pivot.ID(), id)
  36. }
  37. // ConnectToLastNode connects the node with provided NodeID
  38. // to the last node that is up, and avoiding connection to self.
  39. // It is useful when constructing a chain network topology
  40. // when Network adds and removes nodes dynamically.
  41. func (net *Network) ConnectToLastNode(id enode.ID) (err error) {
  42. ids := net.getUpNodeIDs()
  43. l := len(ids)
  44. if l < 2 {
  45. return nil
  46. }
  47. last := ids[l-1]
  48. if last == id {
  49. last = ids[l-2]
  50. }
  51. return net.connect(last, id)
  52. }
  53. // ConnectToRandomNode connects the node with provided NodeID
  54. // to a random node that is up.
  55. func (net *Network) ConnectToRandomNode(id enode.ID) (err error) {
  56. selected := net.GetRandomUpNode(id)
  57. if selected == nil {
  58. return ErrNodeNotFound
  59. }
  60. return net.connect(selected.ID(), id)
  61. }
  62. // ConnectNodesFull connects all nodes one to another.
  63. // It provides a complete connectivity in the network
  64. // which should be rarely needed.
  65. func (net *Network) ConnectNodesFull(ids []enode.ID) (err error) {
  66. if ids == nil {
  67. ids = net.getUpNodeIDs()
  68. }
  69. for i, lid := range ids {
  70. for _, rid := range ids[i+1:] {
  71. if err = net.connect(lid, rid); err != nil {
  72. return err
  73. }
  74. }
  75. }
  76. return nil
  77. }
  78. // ConnectNodesChain connects all nodes in a chain topology.
  79. // If ids argument is nil, all nodes that are up will be connected.
  80. func (net *Network) ConnectNodesChain(ids []enode.ID) (err error) {
  81. if ids == nil {
  82. ids = net.getUpNodeIDs()
  83. }
  84. l := len(ids)
  85. for i := 0; i < l-1; i++ {
  86. if err := net.connect(ids[i], ids[i+1]); err != nil {
  87. return err
  88. }
  89. }
  90. return nil
  91. }
  92. // ConnectNodesRing connects all nodes in a ring topology.
  93. // If ids argument is nil, all nodes that are up will be connected.
  94. func (net *Network) ConnectNodesRing(ids []enode.ID) (err error) {
  95. if ids == nil {
  96. ids = net.getUpNodeIDs()
  97. }
  98. l := len(ids)
  99. if l < 2 {
  100. return nil
  101. }
  102. if err := net.ConnectNodesChain(ids); err != nil {
  103. return err
  104. }
  105. return net.connect(ids[l-1], ids[0])
  106. }
  107. // ConnectNodesStar connects all nodes in a star topology
  108. // with the center at provided NodeID.
  109. // If ids argument is nil, all nodes that are up will be connected.
  110. func (net *Network) ConnectNodesStar(pivot enode.ID, ids []enode.ID) (err error) {
  111. if ids == nil {
  112. ids = net.getUpNodeIDs()
  113. }
  114. for _, id := range ids {
  115. if pivot == id {
  116. continue
  117. }
  118. if err := net.connect(pivot, id); err != nil {
  119. return err
  120. }
  121. }
  122. return nil
  123. }
  124. // ConnectNodesStarPivot connects all nodes in a star topology
  125. // with the center at already set pivot node.
  126. // If ids argument is nil, all nodes that are up will be connected.
  127. func (net *Network) ConnectNodesStarPivot(ids []enode.ID) (err error) {
  128. pivot := net.GetPivotNode()
  129. if pivot == nil {
  130. return ErrNoPivotNode
  131. }
  132. return net.ConnectNodesStar(pivot.ID(), ids)
  133. }
  134. // connect connects two nodes but ignores already connected error.
  135. func (net *Network) connect(oneID, otherID enode.ID) error {
  136. return ignoreAlreadyConnectedErr(net.Connect(oneID, otherID))
  137. }
  138. func ignoreAlreadyConnectedErr(err error) error {
  139. if err == nil || strings.Contains(err.Error(), "already connected") {
  140. return nil
  141. }
  142. return err
  143. }
  144. // SetPivotNode sets the NodeID of the network's pivot node.
  145. // Pivot node is just a specific node that should be treated
  146. // differently then other nodes in test. SetPivotNode and
  147. // GetPivotNode are just a convenient functions to set and
  148. // retrieve it.
  149. func (net *Network) SetPivotNode(id enode.ID) {
  150. net.lock.Lock()
  151. defer net.lock.Unlock()
  152. net.pivotNodeID = id
  153. }
  154. // GetPivotNode returns NodeID of the pivot node set by
  155. // Network.SetPivotNode method.
  156. func (net *Network) GetPivotNode() (node *Node) {
  157. net.lock.RLock()
  158. defer net.lock.RUnlock()
  159. return net.getNode(net.pivotNodeID)
  160. }