mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 23:26:44 +00:00
p2p/simulations: Support stopping, starting and deleting networks
Signed-off-by: Lewis Marshall <lewis@lmars.net>
This commit is contained in:
parent
c9d42b9cf3
commit
39992d5e1b
2 changed files with 82 additions and 0 deletions
|
|
@ -59,6 +59,21 @@ func (c *Client) GetNetwork(networkID string) (*Network, error) {
|
||||||
return network, c.Get(fmt.Sprintf("/networks/%s", networkID), network)
|
return network, c.Get(fmt.Sprintf("/networks/%s", networkID), network)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StartNetwork starts all existing nodes in a simulation network
|
||||||
|
func (c *Client) StartNetwork(networkID string) error {
|
||||||
|
return c.Post(fmt.Sprintf("/networks/%s/start", networkID), nil, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopNetwork stops all existing nodes in a simulation network
|
||||||
|
func (c *Client) StopNetwork(networkID string) error {
|
||||||
|
return c.Post(fmt.Sprintf("/networks/%s/stop", networkID), nil, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteNetwork stops and deletes a simulation network
|
||||||
|
func (c *Client) DeleteNetwork(networkID string) error {
|
||||||
|
return c.Delete(fmt.Sprintf("/networks/%s", networkID))
|
||||||
|
}
|
||||||
|
|
||||||
// CreateSnapshot creates a network snapshot
|
// CreateSnapshot creates a network snapshot
|
||||||
func (c *Client) CreateSnapshot(networkID string) (*Snapshot, error) {
|
func (c *Client) CreateSnapshot(networkID string) (*Snapshot, error) {
|
||||||
snap := &Snapshot{}
|
snap := &Snapshot{}
|
||||||
|
|
@ -276,6 +291,9 @@ func NewServer(config *ServerConfig) *Server {
|
||||||
s.POST("/networks", s.CreateNetwork)
|
s.POST("/networks", s.CreateNetwork)
|
||||||
s.GET("/networks", s.GetNetworks)
|
s.GET("/networks", s.GetNetworks)
|
||||||
s.GET("/networks/:netid", s.GetNetwork)
|
s.GET("/networks/:netid", s.GetNetwork)
|
||||||
|
s.POST("/networks/:netid/start", s.StartNetwork)
|
||||||
|
s.POST("/networks/:netid/stop", s.StopNetwork)
|
||||||
|
s.DELETE("/networks/:netid", s.DeleteNetwork)
|
||||||
s.GET("/networks/:netid/events", s.StreamNetworkEvents)
|
s.GET("/networks/:netid/events", s.StreamNetworkEvents)
|
||||||
s.GET("/networks/:netid/snapshot", s.CreateSnapshot)
|
s.GET("/networks/:netid/snapshot", s.CreateSnapshot)
|
||||||
s.POST("/networks/:netid/snapshot", s.LoadSnapshot)
|
s.POST("/networks/:netid/snapshot", s.LoadSnapshot)
|
||||||
|
|
@ -341,6 +359,46 @@ func (s *Server) GetNetwork(w http.ResponseWriter, req *http.Request) {
|
||||||
s.JSON(w, http.StatusOK, network)
|
s.JSON(w, http.StatusOK, network)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StartNetwork starts all nodes in a network
|
||||||
|
func (s *Server) StartNetwork(w http.ResponseWriter, req *http.Request) {
|
||||||
|
network := req.Context().Value("network").(*Network)
|
||||||
|
|
||||||
|
if err := network.StartAll(); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopNetwork stops all nodes in a network
|
||||||
|
func (s *Server) StopNetwork(w http.ResponseWriter, req *http.Request) {
|
||||||
|
network := req.Context().Value("network").(*Network)
|
||||||
|
|
||||||
|
if err := network.StopAll(); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteNetwork stops all nodes in a network and deletes it
|
||||||
|
func (s *Server) DeleteNetwork(w http.ResponseWriter, req *http.Request) {
|
||||||
|
network := req.Context().Value("network").(*Network)
|
||||||
|
|
||||||
|
if err := network.StopAll(); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.mtx.Lock()
|
||||||
|
delete(s.networks, network.Id)
|
||||||
|
s.mtx.Unlock()
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
//Get the info for a particular mocker
|
//Get the info for a particular mocker
|
||||||
func (s *Server) GetMocker(w http.ResponseWriter, req *http.Request) {
|
func (s *Server) GetMocker(w http.ResponseWriter, req *http.Request) {
|
||||||
m := make(map[string]string)
|
m := make(map[string]string)
|
||||||
|
|
|
||||||
|
|
@ -260,6 +260,30 @@ func (self *Conn) nodesUp() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *Network) StartAll() error {
|
||||||
|
for _, node := range self.Nodes {
|
||||||
|
if node.Up {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := self.Start(node.ID()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Network) StopAll() error {
|
||||||
|
for _, node := range self.Nodes {
|
||||||
|
if !node.Up {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := self.Stop(node.ID()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Start(id) starts up the node (relevant only for instance with own p2p or remote)
|
// Start(id) starts up the node (relevant only for instance with own p2p or remote)
|
||||||
func (self *Network) Start(id *adapters.NodeId) error {
|
func (self *Network) Start(id *adapters.NodeId) error {
|
||||||
return self.startWithSnapshot(id, nil)
|
return self.startWithSnapshot(id, nil)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue