From 26390b40217d438a9200cdd5d1255d527cba5bac Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Fri, 16 Feb 2018 15:38:31 +0100 Subject: [PATCH] p2p/sim: simpler logic for CreateNode HTTP endpoint --- p2p/simulations/adapters/types.go | 1 + p2p/simulations/http.go | 3 ++- p2p/simulations/http_test.go | 10 +++++++--- p2p/simulations/network.go | 20 ++++---------------- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/p2p/simulations/adapters/types.go b/p2p/simulations/adapters/types.go index 2169d68308..93860e3933 100644 --- a/p2p/simulations/adapters/types.go +++ b/p2p/simulations/adapters/types.go @@ -177,6 +177,7 @@ func RandomNodeConfig() *NodeConfig { } return &NodeConfig{ ID: id, + Name: fmt.Sprintf("node_%s", id.String()), PrivateKey: key, Port: port, } diff --git a/p2p/simulations/http.go b/p2p/simulations/http.go index 97dd742e88..24001f1949 100644 --- a/p2p/simulations/http.go +++ b/p2p/simulations/http.go @@ -561,7 +561,8 @@ func (s *Server) LoadSnapshot(w http.ResponseWriter, req *http.Request) { // CreateNode creates a node in the network using the given configuration func (s *Server) CreateNode(w http.ResponseWriter, req *http.Request) { - config := adapters.RandomNodeConfig() + config := &adapters.NodeConfig{} + err := json.NewDecoder(req.Body).Decode(config) if err != nil && err != io.EOF { http.Error(w, err.Error(), http.StatusBadRequest) diff --git a/p2p/simulations/http_test.go b/p2p/simulations/http_test.go index 677a8fb147..732d49f546 100644 --- a/p2p/simulations/http_test.go +++ b/p2p/simulations/http_test.go @@ -348,7 +348,8 @@ func startTestNetwork(t *testing.T, client *Client) []string { nodeCount := 2 nodeIDs := make([]string, nodeCount) for i := 0; i < nodeCount; i++ { - node, err := client.CreateNode(nil) + config := adapters.RandomNodeConfig() + node, err := client.CreateNode(config) if err != nil { t.Fatalf("error creating node: %s", err) } @@ -527,7 +528,9 @@ func TestHTTPNodeRPC(t *testing.T) { // start a node in the network client := NewClient(s.URL) - node, err := client.CreateNode(nil) + + config := adapters.RandomNodeConfig() + node, err := client.CreateNode(config) if err != nil { t.Fatalf("error creating node: %s", err) } @@ -589,7 +592,8 @@ func TestHTTPSnapshot(t *testing.T) { nodeCount := 2 nodes := make([]*p2p.NodeInfo, nodeCount) for i := 0; i < nodeCount; i++ { - node, err := client.CreateNode(nil) + config := adapters.RandomNodeConfig() + node, err := client.CreateNode(config) if err != nil { t.Fatalf("error creating node: %s", err) } diff --git a/p2p/simulations/network.go b/p2p/simulations/network.go index caf428ece1..08c5fcc82d 100644 --- a/p2p/simulations/network.go +++ b/p2p/simulations/network.go @@ -91,13 +91,6 @@ func (self *Network) NewNodeWithConfig(conf *adapters.NodeConfig) (*Node, error) self.lock.Lock() defer self.lock.Unlock() - // create a random ID and PrivateKey if not set - if conf.ID == (discover.NodeID{}) { - c := adapters.RandomNodeConfig() - conf.ID = c.ID - conf.PrivateKey = c.PrivateKey - } - id := conf.ID if conf.Reachable == nil { conf.Reachable = func(otherID discover.NodeID) bool { _, err := self.InitConn(conf.ID, otherID) @@ -105,14 +98,9 @@ func (self *Network) NewNodeWithConfig(conf *adapters.NodeConfig) (*Node, error) } } - // assign a name to the node if not set - if conf.Name == "" { - conf.Name = fmt.Sprintf("node%02d", len(self.Nodes)+1) - } - // check the node doesn't already exist - if node := self.getNode(id); node != nil { - return nil, fmt.Errorf("node with ID %q already exists", id) + if node := self.getNode(conf.ID); node != nil { + return nil, fmt.Errorf("node with ID %q already exists", conf.ID) } if node := self.getNodeByName(conf.Name); node != nil { return nil, fmt.Errorf("node with name %q already exists", conf.Name) @@ -132,8 +120,8 @@ func (self *Network) NewNodeWithConfig(conf *adapters.NodeConfig) (*Node, error) Node: adapterNode, Config: conf, } - log.Trace(fmt.Sprintf("node %v created", id)) - self.nodeMap[id] = len(self.Nodes) + log.Trace(fmt.Sprintf("node %v created", conf.ID)) + self.nodeMap[conf.ID] = len(self.Nodes) self.Nodes = append(self.Nodes, node) // emit a "control" event