connect.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. )
  25. // ConnectToLastNode connects the node with provided NodeID
  26. // to the last node that is up, and avoiding connection to self.
  27. // It is useful when constructing a chain network topology
  28. // when Network adds and removes nodes dynamically.
  29. func (net *Network) ConnectToLastNode(id enode.ID) (err error) {
  30. net.lock.Lock()
  31. defer net.lock.Unlock()
  32. ids := net.getUpNodeIDs()
  33. l := len(ids)
  34. if l < 2 {
  35. return nil
  36. }
  37. last := ids[l-1]
  38. if last == id {
  39. last = ids[l-2]
  40. }
  41. return net.connectNotConnected(last, id)
  42. }
  43. // ConnectToRandomNode connects the node with provided NodeID
  44. // to a random node that is up.
  45. func (net *Network) ConnectToRandomNode(id enode.ID) (err error) {
  46. net.lock.Lock()
  47. defer net.lock.Unlock()
  48. selected := net.getRandomUpNode(id)
  49. if selected == nil {
  50. return ErrNodeNotFound
  51. }
  52. return net.connectNotConnected(selected.ID(), id)
  53. }
  54. // ConnectNodesFull connects all nodes one to another.
  55. // It provides a complete connectivity in the network
  56. // which should be rarely needed.
  57. func (net *Network) ConnectNodesFull(ids []enode.ID) (err error) {
  58. net.lock.Lock()
  59. defer net.lock.Unlock()
  60. if ids == nil {
  61. ids = net.getUpNodeIDs()
  62. }
  63. for i, lid := range ids {
  64. for _, rid := range ids[i+1:] {
  65. if err = net.connectNotConnected(lid, rid); err != nil {
  66. return err
  67. }
  68. }
  69. }
  70. return nil
  71. }
  72. // ConnectNodesChain connects all nodes in a chain topology.
  73. // If ids argument is nil, all nodes that are up will be connected.
  74. func (net *Network) ConnectNodesChain(ids []enode.ID) (err error) {
  75. net.lock.Lock()
  76. defer net.lock.Unlock()
  77. return net.connectNodesChain(ids)
  78. }
  79. func (net *Network) connectNodesChain(ids []enode.ID) (err error) {
  80. if ids == nil {
  81. ids = net.getUpNodeIDs()
  82. }
  83. l := len(ids)
  84. for i := 0; i < l-1; i++ {
  85. if err := net.connectNotConnected(ids[i], ids[i+1]); err != nil {
  86. return err
  87. }
  88. }
  89. return nil
  90. }
  91. // ConnectNodesRing connects all nodes in a ring topology.
  92. // If ids argument is nil, all nodes that are up will be connected.
  93. func (net *Network) ConnectNodesRing(ids []enode.ID) (err error) {
  94. net.lock.Lock()
  95. defer net.lock.Unlock()
  96. if ids == nil {
  97. ids = net.getUpNodeIDs()
  98. }
  99. l := len(ids)
  100. if l < 2 {
  101. return nil
  102. }
  103. if err := net.connectNodesChain(ids); err != nil {
  104. return err
  105. }
  106. return net.connectNotConnected(ids[l-1], ids[0])
  107. }
  108. // ConnectNodesStar connects all nodes into a star topology
  109. // If ids argument is nil, all nodes that are up will be connected.
  110. func (net *Network) ConnectNodesStar(ids []enode.ID, center enode.ID) (err error) {
  111. net.lock.Lock()
  112. defer net.lock.Unlock()
  113. if ids == nil {
  114. ids = net.getUpNodeIDs()
  115. }
  116. for _, id := range ids {
  117. if center == id {
  118. continue
  119. }
  120. if err := net.connectNotConnected(center, id); err != nil {
  121. return err
  122. }
  123. }
  124. return nil
  125. }
  126. func (net *Network) connectNotConnected(oneID, otherID enode.ID) error {
  127. return ignoreAlreadyConnectedErr(net.connect(oneID, otherID))
  128. }
  129. func ignoreAlreadyConnectedErr(err error) error {
  130. if err == nil || strings.Contains(err.Error(), "already connected") {
  131. return nil
  132. }
  133. return err
  134. }