@@ -136,7 +136,7 @@ func (net *Network) Config() *NetworkConfig {
136136// StartAll starts all nodes in the network
137137func (net * Network ) StartAll () error {
138138 for _ , node := range net .Nodes {
139- if node .Up {
139+ if node .Up () {
140140 continue
141141 }
142142 if err := net .Start (node .ID ()); err != nil {
@@ -149,7 +149,7 @@ func (net *Network) StartAll() error {
149149// StopAll stops all nodes in the network
150150func (net * Network ) StopAll () error {
151151 for _ , node := range net .Nodes {
152- if ! node .Up {
152+ if ! node .Up () {
153153 continue
154154 }
155155 if err := net .Stop (node .ID ()); err != nil {
@@ -174,7 +174,7 @@ func (net *Network) startWithSnapshots(id enode.ID, snapshots map[string][]byte)
174174 net .lock .Unlock ()
175175 return fmt .Errorf ("node %v does not exist" , id )
176176 }
177- if node .Up {
177+ if node .Up () {
178178 net .lock .Unlock ()
179179 return fmt .Errorf ("node %v already up" , id )
180180 }
@@ -184,7 +184,7 @@ func (net *Network) startWithSnapshots(id enode.ID, snapshots map[string][]byte)
184184 log .Warn ("Node startup failed" , "id" , id , "err" , err )
185185 return err
186186 }
187- node .Up = true
187+ node .SetUp ( true )
188188 log .Info ("Started node" , "id" , id )
189189 ev := NewEvent (node )
190190 net .lock .Unlock ()
@@ -219,7 +219,7 @@ func (net *Network) watchPeerEvents(id enode.ID, events chan *p2p.PeerEvent, sub
219219 if node == nil {
220220 return
221221 }
222- node .Up = false
222+ node .SetUp ( false )
223223 ev := NewEvent (node )
224224 net .events .Send (ev )
225225 }()
@@ -263,17 +263,17 @@ func (net *Network) Stop(id enode.ID) error {
263263 net .lock .Unlock ()
264264 return fmt .Errorf ("node %v does not exist" , id )
265265 }
266- if ! node .Up {
266+ if ! node .Up () {
267267 net .lock .Unlock ()
268268 return fmt .Errorf ("node %v already down" , id )
269269 }
270- node .Up = false
270+ node .SetUp ( false )
271271 net .lock .Unlock ()
272272
273273 err := node .Stop ()
274274 if err != nil {
275275 net .lock .Lock ()
276- node .Up = true
276+ node .SetUp ( true )
277277 net .lock .Unlock ()
278278 return err
279279 }
@@ -430,7 +430,7 @@ func (net *Network) GetRandomUpNode(excludeIDs ...enode.ID) *Node {
430430
431431func (net * Network ) getUpNodeIDs () (ids []enode.ID ) {
432432 for _ , node := range net .Nodes {
433- if node .Up {
433+ if node .Up () {
434434 ids = append (ids , node .ID ())
435435 }
436436 }
@@ -446,7 +446,7 @@ func (net *Network) GetRandomDownNode(excludeIDs ...enode.ID) *Node {
446446
447447func (net * Network ) getDownNodeIDs () (ids []enode.ID ) {
448448 for _ , node := range net .GetNodes () {
449- if ! node .Up {
449+ if ! node .Up () {
450450 ids = append (ids , node .ID ())
451451 }
452452 }
@@ -595,8 +595,21 @@ type Node struct {
595595 // Config if the config used to created the node
596596 Config * adapters.NodeConfig `json:"config"`
597597
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
600613}
601614
602615// ID returns the ID of the node
@@ -630,7 +643,7 @@ func (n *Node) MarshalJSON() ([]byte, error) {
630643 }{
631644 Info : n .NodeInfo (),
632645 Config : n .Config ,
633- Up : n .Up ,
646+ Up : n .Up () ,
634647 })
635648}
636649
@@ -653,10 +666,10 @@ type Conn struct {
653666
654667// nodesUp returns whether both nodes are currently up
655668func (c * Conn ) nodesUp () error {
656- if ! c .one .Up {
669+ if ! c .one .Up () {
657670 return fmt .Errorf ("one %v is not up" , c .One )
658671 }
659- if ! c .other .Up {
672+ if ! c .other .Up () {
660673 return fmt .Errorf ("other %v is not up" , c .Other )
661674 }
662675 return nil
@@ -728,7 +741,7 @@ func (net *Network) snapshot(addServices []string, removeServices []string) (*Sn
728741 }
729742 for i , node := range net .Nodes {
730743 snap .Nodes [i ] = NodeSnapshot {Node : * node }
731- if ! node .Up {
744+ if ! node .Up () {
732745 continue
733746 }
734747 snapshots , err := node .Snapshots ()
@@ -783,7 +796,7 @@ func (net *Network) Load(snap *Snapshot) error {
783796 if _ , err := net .NewNodeWithConfig (n .Node .Config ); err != nil {
784797 return err
785798 }
786- if ! n .Node .Up {
799+ if ! n .Node .Up () {
787800 continue
788801 }
789802 if err := net .startWithSnapshots (n .Node .Config .ID , n .Snapshots ); err != nil {
@@ -855,7 +868,7 @@ func (net *Network) Load(snap *Snapshot) error {
855868 // Start connecting.
856869 for _ , conn := range snap .Conns {
857870
858- if ! net .GetNode (conn .One ).Up || ! net .GetNode (conn .Other ).Up {
871+ if ! net .GetNode (conn .One ).Up () || ! net .GetNode (conn .Other ).Up () {
859872 //in this case, at least one of the nodes of a connection is not up,
860873 //so it would result in the snapshot `Load` to fail
861874 continue
@@ -909,7 +922,7 @@ func (net *Network) executeControlEvent(event *Event) {
909922}
910923
911924func (net * Network ) executeNodeEvent (e * Event ) error {
912- if ! e .Node .Up {
925+ if ! e .Node .Up () {
913926 return net .Stop (e .Node .ID ())
914927 }
915928
0 commit comments