forked from cerc-io/plugeth
p2p/simulations: fix data race on swarm/network/simulations (#18464)
This commit is contained in:
parent
a6e0285320
commit
85a79b3ad3
@ -168,24 +168,28 @@ func (net *Network) Start(id enode.ID) error {
|
|||||||
// snapshots
|
// snapshots
|
||||||
func (net *Network) startWithSnapshots(id enode.ID, snapshots map[string][]byte) error {
|
func (net *Network) startWithSnapshots(id enode.ID, snapshots map[string][]byte) error {
|
||||||
net.lock.Lock()
|
net.lock.Lock()
|
||||||
defer net.lock.Unlock()
|
|
||||||
|
|
||||||
node := net.getNode(id)
|
node := net.getNode(id)
|
||||||
if node == nil {
|
if node == nil {
|
||||||
|
net.lock.Unlock()
|
||||||
return fmt.Errorf("node %v does not exist", id)
|
return fmt.Errorf("node %v does not exist", id)
|
||||||
}
|
}
|
||||||
if node.Up {
|
if node.Up {
|
||||||
|
net.lock.Unlock()
|
||||||
return fmt.Errorf("node %v already up", id)
|
return fmt.Errorf("node %v already up", id)
|
||||||
}
|
}
|
||||||
log.Trace("Starting node", "id", id, "adapter", net.nodeAdapter.Name())
|
log.Trace("Starting node", "id", id, "adapter", net.nodeAdapter.Name())
|
||||||
if err := node.Start(snapshots); err != nil {
|
if err := node.Start(snapshots); err != nil {
|
||||||
|
net.lock.Unlock()
|
||||||
log.Warn("Node startup failed", "id", id, "err", err)
|
log.Warn("Node startup failed", "id", id, "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
node.Up = true
|
node.Up = true
|
||||||
log.Info("Started node", "id", id)
|
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
|
// subscribe to peer events
|
||||||
client, err := node.Client()
|
client, err := node.Client()
|
||||||
@ -210,12 +214,14 @@ func (net *Network) watchPeerEvents(id enode.ID, events chan *p2p.PeerEvent, sub
|
|||||||
// assume the node is now down
|
// assume the node is now down
|
||||||
net.lock.Lock()
|
net.lock.Lock()
|
||||||
defer net.lock.Unlock()
|
defer net.lock.Unlock()
|
||||||
|
|
||||||
node := net.getNode(id)
|
node := net.getNode(id)
|
||||||
if node == nil {
|
if node == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
node.Up = false
|
node.Up = false
|
||||||
net.events.Send(NewEvent(node))
|
ev := NewEvent(node)
|
||||||
|
net.events.Send(ev)
|
||||||
}()
|
}()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -254,9 +260,11 @@ func (net *Network) Stop(id enode.ID) error {
|
|||||||
net.lock.Lock()
|
net.lock.Lock()
|
||||||
node := net.getNode(id)
|
node := net.getNode(id)
|
||||||
if node == nil {
|
if node == nil {
|
||||||
|
net.lock.Unlock()
|
||||||
return fmt.Errorf("node %v does not exist", id)
|
return fmt.Errorf("node %v does not exist", id)
|
||||||
}
|
}
|
||||||
if !node.Up {
|
if !node.Up {
|
||||||
|
net.lock.Unlock()
|
||||||
return fmt.Errorf("node %v already down", id)
|
return fmt.Errorf("node %v already down", id)
|
||||||
}
|
}
|
||||||
node.Up = false
|
node.Up = false
|
||||||
@ -270,7 +278,10 @@ func (net *Network) Stop(id enode.ID) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Info("Stopped node", "id", id, "err", 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user