From de0549fabb8be4dbaf382ee68ec1b702cb0c5c97 Mon Sep 17 00:00:00 2001 From: Péter Szilágyi Date: Wed, 29 Apr 2015 18:04:08 +0300 Subject: cmd/geth, cmd/mist, cmd/utils, eth, p2p: support trusted peers --- eth/backend.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) (limited to 'eth/backend.go') diff --git a/eth/backend.go b/eth/backend.go index c5fa328b0..f8d57c985 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -2,7 +2,10 @@ package eth import ( "crypto/ecdsa" + "encoding/json" "fmt" + "io/ioutil" + "os" "path" "strings" "time" @@ -56,10 +59,13 @@ type Config struct { MaxPeers int Port string - // This should be a space-separated list of - // discovery node URLs. + // Space-separated list of discovery node URLs BootNodes string + // Either a space-separated list of discovery node URLs, or a path to a json + // file containing such a list. + TrustedNodes string + // This key is used to identify the node on the network. // If nil, an ephemeral key is used. NodeKey *ecdsa.PrivateKey @@ -96,6 +102,46 @@ func (cfg *Config) parseBootNodes() []*discover.Node { return ns } +// parseTrustedNodes parses a list of discovery node URLs either given literally, +// or loaded from a .json file. +func (cfg *Config) parseTrustedNodes() []*discover.Node { + // Short circuit if no trusted nodes were given + if cfg.TrustedNodes == "" { + return nil + } + // Try to interpret the trusted node config as a .json file + if _, err := os.Stat(cfg.TrustedNodes); err == nil { + // Load the file from disk + blob, err := ioutil.ReadFile(cfg.TrustedNodes) + if err != nil { + glog.V(logger.Error).Infof("Failed to access trusted nodes: %v", err) + return nil + } + // Interpret the json contents + list := []string{} + if err := json.Unmarshal(blob, &list); err != nil { + glog.V(logger.Error).Infof("Failed to load trusted nodes: %v", err) + return nil + } + // Swap out the configuration for the actual nodes + cfg.TrustedNodes = strings.Join(list, " ") + } + // Interpret the list as a discovery node array + var nodes []*discover.Node + for _, url := range strings.Split(cfg.TrustedNodes, " ") { + if url == "" { + continue + } + node, err := discover.ParseNode(url) + if err != nil { + glog.V(logger.Error).Infof("Trusted node URL %s: %v\n", url, err) + continue + } + nodes = append(nodes, node) + } + return nodes +} + func (cfg *Config) nodeKey() (*ecdsa.PrivateKey, error) { // use explicit key from command line args if set if cfg.NodeKey != nil { @@ -247,6 +293,7 @@ func New(config *Config) (*Ethereum, error) { NAT: config.NAT, NoDial: !config.Dial, BootstrapNodes: config.parseBootNodes(), + TrustedNodes: config.parseTrustedNodes(), NodeDatabase: nodeDb, } if len(config.Port) > 0 { @@ -442,12 +489,13 @@ func (s *Ethereum) StartForTest() { s.txPool.Start() } -func (self *Ethereum) SuggestPeer(nodeURL string) error { +// TrustPeer injects a new node into the list of privileged nodes. +func (self *Ethereum) TrustPeer(nodeURL string) error { n, err := discover.ParseNode(nodeURL) if err != nil { return fmt.Errorf("invalid node URL: %v", err) } - self.net.SuggestPeer(n) + self.net.TrustPeer(n) return nil } -- cgit From 679c90b873f78b10a97a85ddd29b91bded9b0dd1 Mon Sep 17 00:00:00 2001 From: Péter Szilágyi Date: Wed, 29 Apr 2015 18:50:52 +0300 Subject: cmd/geth, cmd/utils, eth: internalize trusted node config file --- eth/backend.go | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) (limited to 'eth/backend.go') diff --git a/eth/backend.go b/eth/backend.go index f8d57c985..c69e4a27a 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "path" + "path/filepath" "strings" "time" @@ -39,6 +40,9 @@ var ( // ETH/DEV cpp-ethereum (poc-9.ethdev.com) discover.MustParseNode("enode://487611428e6c99a11a9795a6abe7b529e81315ca6aad66e2a2fc76e3adf263faba0d35466c2f8f68d561dbefa8878d4df5f1f2ddb1fbeab7f42ffb8cd328bd4a@5.1.83.226:30303"), } + + // Path within to search for the trusted node list + trustedNodes = "trusted-nodes.json" ) type Config struct { @@ -62,10 +66,6 @@ type Config struct { // Space-separated list of discovery node URLs BootNodes string - // Either a space-separated list of discovery node URLs, or a path to a json - // file containing such a list. - TrustedNodes string - // This key is used to identify the node on the network. // If nil, an ephemeral key is used. NodeKey *ecdsa.PrivateKey @@ -105,30 +105,27 @@ func (cfg *Config) parseBootNodes() []*discover.Node { // parseTrustedNodes parses a list of discovery node URLs either given literally, // or loaded from a .json file. func (cfg *Config) parseTrustedNodes() []*discover.Node { - // Short circuit if no trusted nodes were given - if cfg.TrustedNodes == "" { + // Short circuit if no trusted node config is present + path := filepath.Join(cfg.DataDir, trustedNodes) + if _, err := os.Stat(path); err != nil { + fmt.Println("nodes", nil) return nil } - // Try to interpret the trusted node config as a .json file - if _, err := os.Stat(cfg.TrustedNodes); err == nil { - // Load the file from disk - blob, err := ioutil.ReadFile(cfg.TrustedNodes) - if err != nil { - glog.V(logger.Error).Infof("Failed to access trusted nodes: %v", err) - return nil - } - // Interpret the json contents - list := []string{} - if err := json.Unmarshal(blob, &list); err != nil { - glog.V(logger.Error).Infof("Failed to load trusted nodes: %v", err) - return nil - } - // Swap out the configuration for the actual nodes - cfg.TrustedNodes = strings.Join(list, " ") + // Load the trusted nodes from the config file + blob, err := ioutil.ReadFile(path) + if err != nil { + glog.V(logger.Error).Infof("Failed to access trusted nodes: %v", err) + return nil + } + nodelist := []string{} + if err := json.Unmarshal(blob, &nodelist); err != nil { + glog.V(logger.Error).Infof("Failed to load trusted nodes: %v", err) + return nil } + fmt.Println("nodes", nodelist) // Interpret the list as a discovery node array var nodes []*discover.Node - for _, url := range strings.Split(cfg.TrustedNodes, " ") { + for _, url := range nodelist { if url == "" { continue } -- cgit From 701591b403a8bae8c1dfb648a49d587968ff0c6a Mon Sep 17 00:00:00 2001 From: Péter Szilágyi Date: Thu, 30 Apr 2015 16:15:29 +0300 Subject: cmd, eth, p2p: fix review issues enumerated by Felix --- eth/backend.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'eth/backend.go') diff --git a/eth/backend.go b/eth/backend.go index c69e4a27a..11e5c25e8 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -108,7 +108,6 @@ func (cfg *Config) parseTrustedNodes() []*discover.Node { // Short circuit if no trusted node config is present path := filepath.Join(cfg.DataDir, trustedNodes) if _, err := os.Stat(path); err != nil { - fmt.Println("nodes", nil) return nil } // Load the trusted nodes from the config file @@ -122,7 +121,6 @@ func (cfg *Config) parseTrustedNodes() []*discover.Node { glog.V(logger.Error).Infof("Failed to load trusted nodes: %v", err) return nil } - fmt.Println("nodes", nodelist) // Interpret the list as a discovery node array var nodes []*discover.Node for _, url := range nodelist { @@ -486,13 +484,15 @@ func (s *Ethereum) StartForTest() { s.txPool.Start() } -// TrustPeer injects a new node into the list of privileged nodes. -func (self *Ethereum) TrustPeer(nodeURL string) error { +// AddPeer connects to the given node and maintains the connection until the +// server is shut down. If the connection fails for any reason, the server will +// attempt to reconnect the peer. +func (self *Ethereum) AddPeer(nodeURL string) error { n, err := discover.ParseNode(nodeURL) if err != nil { return fmt.Errorf("invalid node URL: %v", err) } - self.net.TrustPeer(n) + self.net.AddPeer(n) return nil } -- cgit From 413ace37d3ba13a551f60e4089f2e0070c607970 Mon Sep 17 00:00:00 2001 From: Péter Szilágyi Date: Thu, 30 Apr 2015 19:32:48 +0300 Subject: eth, p2p: rename trusted nodes to static, drop inbound extra slots --- eth/backend.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'eth/backend.go') diff --git a/eth/backend.go b/eth/backend.go index 11e5c25e8..8aecfba5b 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -41,8 +41,8 @@ var ( discover.MustParseNode("enode://487611428e6c99a11a9795a6abe7b529e81315ca6aad66e2a2fc76e3adf263faba0d35466c2f8f68d561dbefa8878d4df5f1f2ddb1fbeab7f42ffb8cd328bd4a@5.1.83.226:30303"), } - // Path within to search for the trusted node list - trustedNodes = "trusted-nodes.json" + // Path within to search for the static node list + staticNodes = "static-nodes.json" ) type Config struct { @@ -102,23 +102,23 @@ func (cfg *Config) parseBootNodes() []*discover.Node { return ns } -// parseTrustedNodes parses a list of discovery node URLs either given literally, +// parseStaticNodes parses a list of discovery node URLs either given literally, // or loaded from a .json file. -func (cfg *Config) parseTrustedNodes() []*discover.Node { - // Short circuit if no trusted node config is present - path := filepath.Join(cfg.DataDir, trustedNodes) +func (cfg *Config) parseStaticNodes() []*discover.Node { + // Short circuit if no static node config is present + path := filepath.Join(cfg.DataDir, staticNodes) if _, err := os.Stat(path); err != nil { return nil } - // Load the trusted nodes from the config file + // Load the static nodes from the config file blob, err := ioutil.ReadFile(path) if err != nil { - glog.V(logger.Error).Infof("Failed to access trusted nodes: %v", err) + glog.V(logger.Error).Infof("Failed to access static nodes: %v", err) return nil } nodelist := []string{} if err := json.Unmarshal(blob, &nodelist); err != nil { - glog.V(logger.Error).Infof("Failed to load trusted nodes: %v", err) + glog.V(logger.Error).Infof("Failed to load static nodes: %v", err) return nil } // Interpret the list as a discovery node array @@ -129,7 +129,7 @@ func (cfg *Config) parseTrustedNodes() []*discover.Node { } node, err := discover.ParseNode(url) if err != nil { - glog.V(logger.Error).Infof("Trusted node URL %s: %v\n", url, err) + glog.V(logger.Error).Infof("Static node URL %s: %v\n", url, err) continue } nodes = append(nodes, node) @@ -288,7 +288,7 @@ func New(config *Config) (*Ethereum, error) { NAT: config.NAT, NoDial: !config.Dial, BootstrapNodes: config.parseBootNodes(), - TrustedNodes: config.parseTrustedNodes(), + StaticNodes: config.parseStaticNodes(), NodeDatabase: nodeDb, } if len(config.Port) > 0 { -- cgit From 4accc187d5cf6a100d6c10c0e0f35780f52871a0 Mon Sep 17 00:00:00 2001 From: Péter Szilágyi Date: Mon, 4 May 2015 13:59:51 +0300 Subject: eth, p2p: add trusted node list beside static list --- eth/backend.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'eth/backend.go') diff --git a/eth/backend.go b/eth/backend.go index 8aecfba5b..983dd8e4f 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -41,8 +41,8 @@ var ( discover.MustParseNode("enode://487611428e6c99a11a9795a6abe7b529e81315ca6aad66e2a2fc76e3adf263faba0d35466c2f8f68d561dbefa8878d4df5f1f2ddb1fbeab7f42ffb8cd328bd4a@5.1.83.226:30303"), } - // Path within to search for the static node list - staticNodes = "static-nodes.json" + staticNodes = "static-nodes.json" // Path within to search for the static node list + trustedNodes = "trusted-nodes.json" // Path within to search for the trusted node list ) type Config struct { @@ -102,23 +102,22 @@ func (cfg *Config) parseBootNodes() []*discover.Node { return ns } -// parseStaticNodes parses a list of discovery node URLs either given literally, -// or loaded from a .json file. -func (cfg *Config) parseStaticNodes() []*discover.Node { - // Short circuit if no static node config is present - path := filepath.Join(cfg.DataDir, staticNodes) +// parseNodes parses a list of discovery node URLs loaded from a .json file. +func (cfg *Config) parseNodes(file string) []*discover.Node { + // Short circuit if no node config is present + path := filepath.Join(cfg.DataDir, file) if _, err := os.Stat(path); err != nil { return nil } - // Load the static nodes from the config file + // Load the nodes from the config file blob, err := ioutil.ReadFile(path) if err != nil { - glog.V(logger.Error).Infof("Failed to access static nodes: %v", err) + glog.V(logger.Error).Infof("Failed to access nodes: %v", err) return nil } nodelist := []string{} if err := json.Unmarshal(blob, &nodelist); err != nil { - glog.V(logger.Error).Infof("Failed to load static nodes: %v", err) + glog.V(logger.Error).Infof("Failed to load nodes: %v", err) return nil } // Interpret the list as a discovery node array @@ -129,7 +128,7 @@ func (cfg *Config) parseStaticNodes() []*discover.Node { } node, err := discover.ParseNode(url) if err != nil { - glog.V(logger.Error).Infof("Static node URL %s: %v\n", url, err) + glog.V(logger.Error).Infof("Node URL %s: %v\n", url, err) continue } nodes = append(nodes, node) @@ -288,7 +287,8 @@ func New(config *Config) (*Ethereum, error) { NAT: config.NAT, NoDial: !config.Dial, BootstrapNodes: config.parseBootNodes(), - StaticNodes: config.parseStaticNodes(), + StaticNodes: config.parseNodes(staticNodes), + TrustedNodes: config.parseNodes(trustedNodes), NodeDatabase: nodeDb, } if len(config.Port) > 0 { -- cgit