diff options
author | obscuren <geffobscura@gmail.com> | 2015-05-07 04:54:21 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-05-07 04:54:21 +0800 |
commit | 062fa049d0322f17f8ba831203a609d048f623f1 (patch) | |
tree | 1600884f30471066b9f54cc26c2a9e95b9b20989 /eth/backend.go | |
parent | 24d44f35f2cf17b07330a7e7be79abc2b6b92fc6 (diff) | |
parent | 4accc187d5cf6a100d6c10c0e0f35780f52871a0 (diff) | |
download | go-tangerine-062fa049d0322f17f8ba831203a609d048f623f1.tar go-tangerine-062fa049d0322f17f8ba831203a609d048f623f1.tar.gz go-tangerine-062fa049d0322f17f8ba831203a609d048f623f1.tar.bz2 go-tangerine-062fa049d0322f17f8ba831203a609d048f623f1.tar.lz go-tangerine-062fa049d0322f17f8ba831203a609d048f623f1.tar.xz go-tangerine-062fa049d0322f17f8ba831203a609d048f623f1.tar.zst go-tangerine-062fa049d0322f17f8ba831203a609d048f623f1.zip |
fixed merge issue
Diffstat (limited to 'eth/backend.go')
-rw-r--r-- | eth/backend.go | 53 |
1 files changed, 49 insertions, 4 deletions
diff --git a/eth/backend.go b/eth/backend.go index 21ee1247b..791336d75 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -2,8 +2,12 @@ package eth import ( "crypto/ecdsa" + "encoding/json" "fmt" + "io/ioutil" + "os" "path" + "path/filepath" "strings" "time" @@ -36,6 +40,9 @@ var ( // ETH/DEV cpp-ethereum (poc-9.ethdev.com) discover.MustParseNode("enode://487611428e6c99a11a9795a6abe7b529e81315ca6aad66e2a2fc76e3adf263faba0d35466c2f8f68d561dbefa8878d4df5f1f2ddb1fbeab7f42ffb8cd328bd4a@5.1.83.226:30303"), } + + staticNodes = "static-nodes.json" // Path within <datadir> to search for the static node list + trustedNodes = "trusted-nodes.json" // Path within <datadir> to search for the trusted node list ) type Config struct { @@ -56,8 +63,7 @@ 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 // This key is used to identify the node on the network. @@ -96,6 +102,40 @@ func (cfg *Config) parseBootNodes() []*discover.Node { return ns } +// 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 nodes from the config file + blob, err := ioutil.ReadFile(path) + if err != nil { + 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 nodes: %v", err) + return nil + } + // Interpret the list as a discovery node array + var nodes []*discover.Node + for _, url := range nodelist { + if url == "" { + continue + } + node, err := discover.ParseNode(url) + if err != nil { + glog.V(logger.Error).Infof("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 +287,8 @@ func New(config *Config) (*Ethereum, error) { NAT: config.NAT, NoDial: !config.Dial, BootstrapNodes: config.parseBootNodes(), + StaticNodes: config.parseNodes(staticNodes), + TrustedNodes: config.parseNodes(trustedNodes), NodeDatabase: nodeDb, } if len(config.Port) > 0 { @@ -441,12 +483,15 @@ func (s *Ethereum) StartForTest() { s.txPool.Start() } -func (self *Ethereum) SuggestPeer(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.SuggestPeer(n) + self.net.AddPeer(n) return nil } |