1414// You should have received a copy of the GNU Lesser General Public License
1515// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616
17- package simulation
17+ package simulations
1818
1919import (
20+ "errors"
2021 "strings"
2122
2223 "github.com/ethereum/go-ethereum/p2p/enode"
2324)
2425
26+ var (
27+ ErrNodeNotFound = errors .New ("node not found" )
28+ ErrNoPivotNode = errors .New ("no pivot node set" )
29+ )
30+
2531// ConnectToPivotNode connects the node with provided NodeID
26- // to the pivot node, already set by Simulation .SetPivotNode method.
32+ // to the pivot node, already set by Network .SetPivotNode method.
2733// It is useful when constructing a star network topology
28- // when simulation adds and removes nodes dynamically.
29- func (s * Simulation ) ConnectToPivotNode (id enode.ID ) (err error ) {
30- pid := s . PivotNodeID ()
31- if pid == nil {
34+ // when Network adds and removes nodes dynamically.
35+ func (net * Network ) ConnectToPivotNode (id enode.ID ) (err error ) {
36+ pivot := net . GetPivotNode ()
37+ if pivot == nil {
3238 return ErrNoPivotNode
3339 }
34- return s .connect (* pid , id )
40+ return net .connect (pivot . ID () , id )
3541}
3642
3743// ConnectToLastNode connects the node with provided NodeID
3844// to the last node that is up, and avoiding connection to self.
3945// It is useful when constructing a chain network topology
40- // when simulation adds and removes nodes dynamically.
41- func (s * Simulation ) ConnectToLastNode (id enode.ID ) (err error ) {
42- ids := s . UpNodeIDs ()
46+ // when Network adds and removes nodes dynamically.
47+ func (net * Network ) ConnectToLastNode (id enode.ID ) (err error ) {
48+ ids := net . getUpNodeIDs ()
4349 l := len (ids )
4450 if l < 2 {
4551 return nil
4652 }
47- lid := ids [l - 1 ]
48- if lid == id {
49- lid = ids [l - 2 ]
53+ last := ids [l - 1 ]
54+ if last == id {
55+ last = ids [l - 2 ]
5056 }
51- return s .connect (lid , id )
57+ return net .connect (last , id )
5258}
5359
54- // ConnectToRandomNode connects the node with provieded NodeID
60+ // ConnectToRandomNode connects the node with provided NodeID
5561// to a random node that is up.
56- func (s * Simulation ) ConnectToRandomNode (id enode.ID ) (err error ) {
57- n := s . RandomUpNode (id )
58- if n == nil {
62+ func (net * Network ) ConnectToRandomNode (id enode.ID ) (err error ) {
63+ selected := net . GetRandomUpNode (id )
64+ if selected == nil {
5965 return ErrNodeNotFound
6066 }
61- return s .connect (n .ID , id )
67+ return net .connect (selected .ID () , id )
6268}
6369
6470// ConnectNodesFull connects all nodes one to another.
6571// It provides a complete connectivity in the network
6672// which should be rarely needed.
67- func (s * Simulation ) ConnectNodesFull (ids []enode.ID ) (err error ) {
73+ func (net * Network ) ConnectNodesFull (ids []enode.ID ) (err error ) {
6874 if ids == nil {
69- ids = s . UpNodeIDs ()
75+ ids = net . getUpNodeIDs ()
7076 }
71- l := len (ids )
72- for i := 0 ; i < l ; i ++ {
73- for j := i + 1 ; j < l ; j ++ {
74- err = s .connect (ids [i ], ids [j ])
75- if err != nil {
77+ for i , lid := range ids {
78+ for _ , rid := range ids [i + 1 :] {
79+ if err = net .connect (lid , rid ); err != nil {
7680 return err
7781 }
7882 }
@@ -82,14 +86,13 @@ func (s *Simulation) ConnectNodesFull(ids []enode.ID) (err error) {
8286
8387// ConnectNodesChain connects all nodes in a chain topology.
8488// If ids argument is nil, all nodes that are up will be connected.
85- func (s * Simulation ) ConnectNodesChain (ids []enode.ID ) (err error ) {
89+ func (net * Network ) ConnectNodesChain (ids []enode.ID ) (err error ) {
8690 if ids == nil {
87- ids = s . UpNodeIDs ()
91+ ids = net . getUpNodeIDs ()
8892 }
8993 l := len (ids )
9094 for i := 0 ; i < l - 1 ; i ++ {
91- err = s .connect (ids [i ], ids [i + 1 ])
92- if err != nil {
95+ if err := net .connect (ids [i ], ids [i + 1 ]); err != nil {
9396 return err
9497 }
9598 }
@@ -98,37 +101,32 @@ func (s *Simulation) ConnectNodesChain(ids []enode.ID) (err error) {
98101
99102// ConnectNodesRing connects all nodes in a ring topology.
100103// If ids argument is nil, all nodes that are up will be connected.
101- func (s * Simulation ) ConnectNodesRing (ids []enode.ID ) (err error ) {
104+ func (net * Network ) ConnectNodesRing (ids []enode.ID ) (err error ) {
102105 if ids == nil {
103- ids = s . UpNodeIDs ()
106+ ids = net . getUpNodeIDs ()
104107 }
105108 l := len (ids )
106109 if l < 2 {
107110 return nil
108111 }
109- for i := 0 ; i < l - 1 ; i ++ {
110- err = s .connect (ids [i ], ids [i + 1 ])
111- if err != nil {
112- return err
113- }
112+ if err := net .ConnectNodesChain (ids ); err != nil {
113+ return err
114114 }
115- return s .connect (ids [l - 1 ], ids [0 ])
115+ return net .connect (ids [l - 1 ], ids [0 ])
116116}
117117
118118// ConnectNodesStar connects all nodes in a star topology
119119// with the center at provided NodeID.
120120// If ids argument is nil, all nodes that are up will be connected.
121- func (s * Simulation ) ConnectNodesStar (id enode.ID , ids []enode.ID ) (err error ) {
121+ func (net * Network ) ConnectNodesStar (pivot enode.ID , ids []enode.ID ) (err error ) {
122122 if ids == nil {
123- ids = s . UpNodeIDs ()
123+ ids = net . getUpNodeIDs ()
124124 }
125- l := len (ids )
126- for i := 0 ; i < l ; i ++ {
127- if id == ids [i ] {
125+ for _ , id := range ids {
126+ if pivot == id {
128127 continue
129128 }
130- err = s .connect (id , ids [i ])
131- if err != nil {
129+ if err := net .connect (pivot , id ); err != nil {
132130 return err
133131 }
134132 }
@@ -138,17 +136,17 @@ func (s *Simulation) ConnectNodesStar(id enode.ID, ids []enode.ID) (err error) {
138136// ConnectNodesStarPivot connects all nodes in a star topology
139137// with the center at already set pivot node.
140138// If ids argument is nil, all nodes that are up will be connected.
141- func (s * Simulation ) ConnectNodesStarPivot (ids []enode.ID ) (err error ) {
142- id := s . PivotNodeID ()
143- if id == nil {
139+ func (net * Network ) ConnectNodesStarPivot (ids []enode.ID ) (err error ) {
140+ pivot := net . GetPivotNode ()
141+ if pivot == nil {
144142 return ErrNoPivotNode
145143 }
146- return s .ConnectNodesStar (* id , ids )
144+ return net .ConnectNodesStar (pivot . ID () , ids )
147145}
148146
149147// connect connects two nodes but ignores already connected error.
150- func (s * Simulation ) connect (oneID , otherID enode.ID ) error {
151- return ignoreAlreadyConnectedErr (s . Net .Connect (oneID , otherID ))
148+ func (net * Network ) connect (oneID , otherID enode.ID ) error {
149+ return ignoreAlreadyConnectedErr (net .Connect (oneID , otherID ))
152150}
153151
154152func ignoreAlreadyConnectedErr (err error ) error {
@@ -157,3 +155,22 @@ func ignoreAlreadyConnectedErr(err error) error {
157155 }
158156 return err
159157}
158+
159+ // SetPivotNode sets the NodeID of the network's pivot node.
160+ // Pivot node is just a specific node that should be treated
161+ // differently then other nodes in test. SetPivotNode and
162+ // GetPivotNode are just a convenient functions to set and
163+ // retrieve it.
164+ func (net * Network ) SetPivotNode (id enode.ID ) {
165+ net .lock .Lock ()
166+ defer net .lock .Unlock ()
167+ net .pivotNodeID = id
168+ }
169+
170+ // GetPivotNode returns NodeID of the pivot node set by
171+ // Network.SetPivotNode method.
172+ func (net * Network ) GetPivotNode () (node * Node ) {
173+ net .lock .RLock ()
174+ defer net .lock .RUnlock ()
175+ return net .getNode (net .pivotNodeID )
176+ }
0 commit comments