@@ -136,7 +136,7 @@ func (net *Network) Config() *NetworkConfig {
136
136
// StartAll starts all nodes in the network
137
137
func (net * Network ) StartAll () error {
138
138
for _ , node := range net .Nodes {
139
- if node .Up {
139
+ if node .Up () {
140
140
continue
141
141
}
142
142
if err := net .Start (node .ID ()); err != nil {
@@ -149,7 +149,7 @@ func (net *Network) StartAll() error {
149
149
// StopAll stops all nodes in the network
150
150
func (net * Network ) StopAll () error {
151
151
for _ , node := range net .Nodes {
152
- if ! node .Up {
152
+ if ! node .Up () {
153
153
continue
154
154
}
155
155
if err := net .Stop (node .ID ()); err != nil {
@@ -174,7 +174,7 @@ func (net *Network) startWithSnapshots(id enode.ID, snapshots map[string][]byte)
174
174
net .lock .Unlock ()
175
175
return fmt .Errorf ("node %v does not exist" , id )
176
176
}
177
- if node .Up {
177
+ if node .Up () {
178
178
net .lock .Unlock ()
179
179
return fmt .Errorf ("node %v already up" , id )
180
180
}
@@ -184,7 +184,7 @@ func (net *Network) startWithSnapshots(id enode.ID, snapshots map[string][]byte)
184
184
log .Warn ("Node startup failed" , "id" , id , "err" , err )
185
185
return err
186
186
}
187
- node .Up = true
187
+ node .SetUp ( true )
188
188
log .Info ("Started node" , "id" , id )
189
189
ev := NewEvent (node )
190
190
net .lock .Unlock ()
@@ -219,7 +219,7 @@ func (net *Network) watchPeerEvents(id enode.ID, events chan *p2p.PeerEvent, sub
219
219
if node == nil {
220
220
return
221
221
}
222
- node .Up = false
222
+ node .SetUp ( false )
223
223
ev := NewEvent (node )
224
224
net .events .Send (ev )
225
225
}()
@@ -263,17 +263,17 @@ func (net *Network) Stop(id enode.ID) error {
263
263
net .lock .Unlock ()
264
264
return fmt .Errorf ("node %v does not exist" , id )
265
265
}
266
- if ! node .Up {
266
+ if ! node .Up () {
267
267
net .lock .Unlock ()
268
268
return fmt .Errorf ("node %v already down" , id )
269
269
}
270
- node .Up = false
270
+ node .SetUp ( false )
271
271
net .lock .Unlock ()
272
272
273
273
err := node .Stop ()
274
274
if err != nil {
275
275
net .lock .Lock ()
276
- node .Up = true
276
+ node .SetUp ( true )
277
277
net .lock .Unlock ()
278
278
return err
279
279
}
@@ -430,7 +430,7 @@ func (net *Network) GetRandomUpNode(excludeIDs ...enode.ID) *Node {
430
430
431
431
func (net * Network ) getUpNodeIDs () (ids []enode.ID ) {
432
432
for _ , node := range net .Nodes {
433
- if node .Up {
433
+ if node .Up () {
434
434
ids = append (ids , node .ID ())
435
435
}
436
436
}
@@ -446,7 +446,7 @@ func (net *Network) GetRandomDownNode(excludeIDs ...enode.ID) *Node {
446
446
447
447
func (net * Network ) getDownNodeIDs () (ids []enode.ID ) {
448
448
for _ , node := range net .GetNodes () {
449
- if ! node .Up {
449
+ if ! node .Up () {
450
450
ids = append (ids , node .ID ())
451
451
}
452
452
}
@@ -595,8 +595,21 @@ type Node struct {
595
595
// Config if the config used to created the node
596
596
Config * adapters.NodeConfig `json:"config"`
597
597
598
- // Up tracks whether or not the node is running
599
- Up bool `json:"up"`
598
+ // up tracks whether or not the node is running
599
+ up bool `json:"up"`
600
+ upMu sync.RWMutex `json:"-"`
601
+ }
602
+
603
+ func (n * Node ) Up () bool {
604
+ n .upMu .RLock ()
605
+ defer n .upMu .RUnlock ()
606
+ return n .up
607
+ }
608
+
609
+ func (n * Node ) SetUp (up bool ) {
610
+ n .upMu .Lock ()
611
+ defer n .upMu .Unlock ()
612
+ n .up = up
600
613
}
601
614
602
615
// ID returns the ID of the node
@@ -630,7 +643,7 @@ func (n *Node) MarshalJSON() ([]byte, error) {
630
643
}{
631
644
Info : n .NodeInfo (),
632
645
Config : n .Config ,
633
- Up : n .Up ,
646
+ Up : n .Up () ,
634
647
})
635
648
}
636
649
@@ -653,10 +666,10 @@ type Conn struct {
653
666
654
667
// nodesUp returns whether both nodes are currently up
655
668
func (c * Conn ) nodesUp () error {
656
- if ! c .one .Up {
669
+ if ! c .one .Up () {
657
670
return fmt .Errorf ("one %v is not up" , c .One )
658
671
}
659
- if ! c .other .Up {
672
+ if ! c .other .Up () {
660
673
return fmt .Errorf ("other %v is not up" , c .Other )
661
674
}
662
675
return nil
@@ -728,7 +741,7 @@ func (net *Network) snapshot(addServices []string, removeServices []string) (*Sn
728
741
}
729
742
for i , node := range net .Nodes {
730
743
snap .Nodes [i ] = NodeSnapshot {Node : * node }
731
- if ! node .Up {
744
+ if ! node .Up () {
732
745
continue
733
746
}
734
747
snapshots , err := node .Snapshots ()
@@ -783,7 +796,7 @@ func (net *Network) Load(snap *Snapshot) error {
783
796
if _ , err := net .NewNodeWithConfig (n .Node .Config ); err != nil {
784
797
return err
785
798
}
786
- if ! n .Node .Up {
799
+ if ! n .Node .Up () {
787
800
continue
788
801
}
789
802
if err := net .startWithSnapshots (n .Node .Config .ID , n .Snapshots ); err != nil {
@@ -855,7 +868,7 @@ func (net *Network) Load(snap *Snapshot) error {
855
868
// Start connecting.
856
869
for _ , conn := range snap .Conns {
857
870
858
- if ! net .GetNode (conn .One ).Up || ! net .GetNode (conn .Other ).Up {
871
+ if ! net .GetNode (conn .One ).Up () || ! net .GetNode (conn .Other ).Up () {
859
872
//in this case, at least one of the nodes of a connection is not up,
860
873
//so it would result in the snapshot `Load` to fail
861
874
continue
@@ -909,7 +922,7 @@ func (net *Network) executeControlEvent(event *Event) {
909
922
}
910
923
911
924
func (net * Network ) executeNodeEvent (e * Event ) error {
912
- if ! e .Node .Up {
925
+ if ! e .Node .Up () {
913
926
return net .Stop (e .Node .ID ())
914
927
}
915
928
0 commit comments