@@ -83,6 +83,13 @@ func AddNodeWithMsgEvents(enable bool) AddNodeOption {
83
83
}
84
84
}
85
85
86
+ // AddNodeAsBootNode toggles whether the node will be configured as a bootnode
87
+ func AddNodeAsBootNode (enable bool ) AddNodeOption {
88
+ return func (o * adapters.NodeConfig ) {
89
+ o .BootNode = enable
90
+ }
91
+ }
92
+
86
93
// AddNodeWithService specifies a service that should be
87
94
// started on a node. This option can be repeated as variadic
88
95
// argument toe AddNode and other add node related methods.
@@ -108,18 +115,16 @@ func (s *Simulation) AddNode(opts ...AddNodeOption) (id enode.ID, err error) {
108
115
109
116
// add ENR records to the underlying node
110
117
// most importantly the bzz overlay address
111
- //
112
- // for now we have no way of setting bootnodes or lightnodes in sims
113
- // so we just let them be set to false
114
- // they should perhaps be possible to override them with AddNodeOption
115
118
bzzPrivateKey , err := BzzPrivateKeyFromConfig (conf )
116
119
if err != nil {
117
120
return enode.ID {}, err
118
121
}
119
122
120
123
enodeParams := & network.EnodeParams {
121
124
PrivateKey : bzzPrivateKey ,
125
+ Bootnode : conf .BootNode ,
122
126
}
127
+
123
128
record , err := network .NewEnodeRecord (enodeParams )
124
129
conf .Record = * record
125
130
@@ -148,6 +153,29 @@ func (s *Simulation) AddNodes(count int, opts ...AddNodeOption) (ids []enode.ID,
148
153
return ids , nil
149
154
}
150
155
156
+ // AddBootNode creates a bootnode using AddNode(opts) and appends it to Simulation.bootNodes
157
+ func (s * Simulation ) AddBootNode (opts ... AddNodeOption ) (id enode.ID , err error ) {
158
+ opts = append (opts , AddNodeAsBootNode (true ))
159
+ id , err = s .AddNode (opts ... )
160
+ if err != nil {
161
+ return id , err
162
+ }
163
+
164
+ return id , nil
165
+ }
166
+
167
+ // AddBootNodes creates count number of bootnodes using AddNodes(count, opts)
168
+ // and appends them to Simulation.bootNodes
169
+ func (s * Simulation ) AddBootNodes (count int , opts ... AddNodeOption ) (ids []enode.ID , err error ) {
170
+ opts = append (opts , AddNodeAsBootNode (true ))
171
+ ids , err = s .AddNodes (count , opts ... )
172
+ if err != nil {
173
+ return nil , err
174
+ }
175
+
176
+ return ids , err
177
+ }
178
+
151
179
// AddNodesAndConnectFull is a helpper method that combines
152
180
// AddNodes and ConnectNodesFull. Only new nodes will be connected.
153
181
func (s * Simulation ) AddNodesAndConnectFull (count int , opts ... AddNodeOption ) (ids []enode.ID , err error ) {
@@ -226,6 +254,28 @@ func (s *Simulation) AddNodesAndConnectStar(count int, opts ...AddNodeOption) (i
226
254
return ids , nil
227
255
}
228
256
257
+ // AddNodesAndConnectToBootNode is a helper method that combines
258
+ // AddNodes, AddBootNode and ConnectNodesStar, where the center node is a new bootnode.
259
+ // The count parameter excludes the bootnode.
260
+ func (s * Simulation ) AddNodesAndConnectToBootNode (count int , opts ... AddNodeOption ) (ids []enode.ID , bootNodeID enode.ID , err error ) {
261
+ bootNodeID , err = s .AddBootNode (opts ... )
262
+ if err != nil {
263
+ return nil , bootNodeID , err
264
+ }
265
+
266
+ ids , err = s .AddNodes (count , opts ... )
267
+ if err != nil {
268
+ return nil , bootNodeID , err
269
+ }
270
+
271
+ err = s .Net .ConnectNodesStar (ids , bootNodeID )
272
+ if err != nil {
273
+ return nil , bootNodeID , err
274
+ }
275
+
276
+ return ids , bootNodeID , nil
277
+ }
278
+
229
279
// UploadSnapshot uploads a snapshot to the simulation
230
280
// This method tries to open the json file provided, applies the config to all nodes
231
281
// and then loads the snapshot into the Simulation network
@@ -267,19 +317,21 @@ func (s *Simulation) StartNode(id enode.ID) (err error) {
267
317
}
268
318
269
319
// StartRandomNode starts a random node.
270
- func (s * Simulation ) StartRandomNode () (id enode.ID , err error ) {
271
- n := s .Net .GetRandomDownNode ()
320
+ // Nodes can be excluded by providing their enode.ID.
321
+ func (s * Simulation ) StartRandomNode (excludeIDs ... enode.ID ) (id enode.ID , err error ) {
322
+ n := s .Net .GetRandomDownNode (excludeIDs ... )
272
323
if n == nil {
273
324
return id , ErrNodeNotFound
274
325
}
275
326
return n .ID (), s .Net .Start (n .ID ())
276
327
}
277
328
278
- // StartRandomNodes starts random nodes.
279
- func (s * Simulation ) StartRandomNodes (count int ) (ids []enode.ID , err error ) {
329
+ // StartRandomNodes starts random nodes. Nodes
330
+ // Nodes can be excluded by providing their enode.ID.
331
+ func (s * Simulation ) StartRandomNodes (count int , excludeIDs ... enode.ID ) (ids []enode.ID , err error ) {
280
332
ids = make ([]enode.ID , 0 , count )
281
333
for i := 0 ; i < count ; i ++ {
282
- n := s .Net .GetRandomDownNode ()
334
+ n := s .Net .GetRandomDownNode (excludeIDs ... )
283
335
if n == nil {
284
336
return nil , ErrNodeNotFound
285
337
}
@@ -298,30 +350,21 @@ func (s *Simulation) StopNode(id enode.ID) (err error) {
298
350
}
299
351
300
352
// StopRandomNode stops a random node.
301
- func (s * Simulation ) StopRandomNode (protect ... enode.ID ) (id enode.ID , err error ) {
302
- found := false
303
- var n * simulations.Node
304
- outer:
305
- for ! found {
306
- n = s .Net .GetRandomUpNode ()
307
- for _ , v := range protect {
308
- if bytes .Equal (n .ID ().Bytes (), v .Bytes ()) {
309
- continue outer
310
- }
311
- }
312
- found = true
313
- }
353
+ // Nodes can be excluded by providing their enode.ID.
354
+ func (s * Simulation ) StopRandomNode (excludeIDs ... enode.ID ) (id enode.ID , err error ) {
355
+ n := s .Net .GetRandomUpNode (excludeIDs ... )
314
356
if n == nil {
315
357
return id , ErrNodeNotFound
316
358
}
317
359
return n .ID (), s .Net .Stop (n .ID ())
318
360
}
319
361
320
362
// StopRandomNodes stops random nodes.
321
- func (s * Simulation ) StopRandomNodes (count int ) (ids []enode.ID , err error ) {
363
+ // Nodes can be excluded by providing their enode.ID.
364
+ func (s * Simulation ) StopRandomNodes (count int , excludeIDs ... enode.ID ) (ids []enode.ID , err error ) {
322
365
ids = make ([]enode.ID , 0 , count )
323
366
for i := 0 ; i < count ; i ++ {
324
- n := s .Net .GetRandomUpNode ()
367
+ n := s .Net .GetRandomUpNode (excludeIDs ... )
325
368
if n == nil {
326
369
return nil , ErrNodeNotFound
327
370
}
@@ -339,7 +382,7 @@ func init() {
339
382
rand .Seed (time .Now ().UnixNano ())
340
383
}
341
384
342
- // derive a private key for swarm for the node key
385
+ // BzzPrivateKeyFromConfig derives a private key for swarm for the node key
343
386
// returns the private key used to generate the bzz key
344
387
func BzzPrivateKeyFromConfig (conf * adapters.NodeConfig ) (* ecdsa.PrivateKey , error ) {
345
388
// pad the seed key some arbitrary data as ecdsa.GenerateKey takes 40 bytes seed data
0 commit comments