Skip to content

Commit

Permalink
p2p/simulations: fix data race on swarm/network/simulations (ethereum…
Browse files Browse the repository at this point in the history
  • Loading branch information
acud authored and nonsense committed Jan 22, 2019
1 parent a6e0285 commit 85a79b3
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions p2p/simulations/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,24 +168,28 @@ func (net *Network) Start(id enode.ID) error {
// snapshots
func (net *Network) startWithSnapshots(id enode.ID, snapshots map[string][]byte) error {
net.lock.Lock()
defer net.lock.Unlock()

node := net.getNode(id)
if node == nil {
net.lock.Unlock()
return fmt.Errorf("node %v does not exist", id)
}
if node.Up {
net.lock.Unlock()
return fmt.Errorf("node %v already up", id)
}
log.Trace("Starting node", "id", id, "adapter", net.nodeAdapter.Name())
if err := node.Start(snapshots); err != nil {
net.lock.Unlock()
log.Warn("Node startup failed", "id", id, "err", err)
return err
}
node.Up = true
log.Info("Started node", "id", id)
ev := NewEvent(node)
net.lock.Unlock()

net.events.Send(NewEvent(node))
net.events.Send(ev)

// subscribe to peer events
client, err := node.Client()
Expand All @@ -210,12 +214,14 @@ func (net *Network) watchPeerEvents(id enode.ID, events chan *p2p.PeerEvent, sub
// assume the node is now down
net.lock.Lock()
defer net.lock.Unlock()

node := net.getNode(id)
if node == nil {
return
}
node.Up = false
net.events.Send(NewEvent(node))
ev := NewEvent(node)
net.events.Send(ev)
}()
for {
select {
Expand Down Expand Up @@ -254,9 +260,11 @@ func (net *Network) Stop(id enode.ID) error {
net.lock.Lock()
node := net.getNode(id)
if node == nil {
net.lock.Unlock()
return fmt.Errorf("node %v does not exist", id)
}
if !node.Up {
net.lock.Unlock()
return fmt.Errorf("node %v already down", id)
}
node.Up = false
Expand All @@ -270,7 +278,10 @@ func (net *Network) Stop(id enode.ID) error {
return err
}
log.Info("Stopped node", "id", id, "err", err)
net.events.Send(ControlEvent(node))
net.lock.Lock()
ev := ControlEvent(node)
net.lock.Unlock()
net.events.Send(ev)
return nil
}

Expand Down

0 comments on commit 85a79b3

Please sign in to comment.