Connect to previous peer

This commit is contained in:
obscuren 2014-09-16 16:36:27 +02:00
parent 66e309c5c4
commit 1549a29c9d
2 changed files with 64 additions and 41 deletions

View File

@ -2,9 +2,11 @@ package eth
import ( import (
"container/list" "container/list"
"encoding/json"
"fmt" "fmt"
"math/rand" "math/rand"
"net" "net"
"path"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -31,9 +33,7 @@ var ethlogger = ethlog.NewLogger("SERV")
func eachPeer(peers *list.List, callback func(*Peer, *list.Element)) { func eachPeer(peers *list.List, callback func(*Peer, *list.Element)) {
// Loop thru the peers and close them (if we had them) // Loop thru the peers and close them (if we had them)
for e := peers.Front(); e != nil; e = e.Next() { for e := peers.Front(); e != nil; e = e.Next() {
if peer, ok := e.Value.(*Peer); ok { callback(e.Value.(*Peer), e)
callback(peer, e)
}
} }
} }
@ -399,47 +399,57 @@ func (s *Ethereum) Start(seed bool) {
} }
func (s *Ethereum) Seed() { func (s *Ethereum) Seed() {
ethlogger.Debugln("Retrieving seed nodes") var ips []string
data, _ := ethutil.ReadAllFile(path.Join(ethutil.Config.ExecPath, "known_peers.json"))
// Eth-Go Bootstrapping json.Unmarshal([]byte(data), &ips)
ips, er := net.LookupIP("seed.bysh.me") if len(ips) > 0 {
if er == nil {
peers := []string{}
for _, ip := range ips { for _, ip := range ips {
node := fmt.Sprintf("%s:%d", ip.String(), 30303) ethlogger.Infoln("Connecting to previous peer ", ip)
ethlogger.Debugln("Found DNS Go Peer:", node) s.ConnectToPeer(ip)
peers = append(peers, node)
} }
s.ProcessPeerList(peers) } else {
} ethlogger.Debugln("Retrieving seed nodes")
// Official DNS Bootstrapping // Eth-Go Bootstrapping
_, nodes, err := net.LookupSRV("eth", "tcp", "ethereum.org") ips, er := net.LookupIP("seed.bysh.me")
if err == nil { if er == nil {
peers := []string{} peers := []string{}
// Iterate SRV nodes for _, ip := range ips {
for _, n := range nodes { node := fmt.Sprintf("%s:%d", ip.String(), 30303)
target := n.Target ethlogger.Debugln("Found DNS Go Peer:", node)
port := strconv.Itoa(int(n.Port)) peers = append(peers, node)
// Resolve target to ip (Go returns list, so may resolve to multiple ips?)
addr, err := net.LookupHost(target)
if err == nil {
for _, a := range addr {
// Build string out of SRV port and Resolved IP
peer := net.JoinHostPort(a, port)
ethlogger.Debugln("Found DNS Bootstrap Peer:", peer)
peers = append(peers, peer)
}
} else {
ethlogger.Debugln("Couldn't resolve :", target)
} }
s.ProcessPeerList(peers)
} }
// Connect to Peer list
s.ProcessPeerList(peers)
}
// XXX tmp // Official DNS Bootstrapping
s.ConnectToPeer(seedNodeAddress) _, nodes, err := net.LookupSRV("eth", "tcp", "ethereum.org")
if err == nil {
peers := []string{}
// Iterate SRV nodes
for _, n := range nodes {
target := n.Target
port := strconv.Itoa(int(n.Port))
// Resolve target to ip (Go returns list, so may resolve to multiple ips?)
addr, err := net.LookupHost(target)
if err == nil {
for _, a := range addr {
// Build string out of SRV port and Resolved IP
peer := net.JoinHostPort(a, port)
ethlogger.Debugln("Found DNS Bootstrap Peer:", peer)
peers = append(peers, peer)
}
} else {
ethlogger.Debugln("Couldn't resolve :", target)
}
}
// Connect to Peer list
s.ProcessPeerList(peers)
}
// XXX tmp
s.ConnectToPeer(seedNodeAddress)
}
} }
func (s *Ethereum) peerHandler(listener net.Listener) { func (s *Ethereum) peerHandler(listener net.Listener) {
@ -459,6 +469,13 @@ func (s *Ethereum) Stop() {
// Close the database // Close the database
defer s.db.Close() defer s.db.Close()
var ips []string
eachPeer(s.peers, func(p *Peer, e *list.Element) {
ips = append(ips, p.conn.RemoteAddr().String())
})
d, _ := json.MarshalIndent(ips, "", " ")
ethutil.WriteFile(path.Join(ethutil.Config.ExecPath, "known_peers.json"), d)
eachPeer(s.peers, func(p *Peer, e *list.Element) { eachPeer(s.peers, func(p *Peer, e *list.Element) {
p.Stop() p.Stop()
}) })

12
peer.go
View File

@ -680,7 +680,7 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
var ( var (
p2pVersion = c.Get(0).Uint() p2pVersion = c.Get(0).Uint()
clientId = c.Get(1).Str() clientId = c.Get(1).Str()
caps = c.Get(2).Raw() caps = c.Get(2)
port = c.Get(3).Uint() port = c.Get(3).Uint()
pub = c.Get(4).Bytes() pub = c.Get(4).Bytes()
) )
@ -734,11 +734,17 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
p.ethereum.PushPeer(p) p.ethereum.PushPeer(p)
p.ethereum.reactor.Post("peerList", p.ethereum.Peers()) p.ethereum.reactor.Post("peerList", p.ethereum.Peers())
ethlogger.Infof("Added peer (%s) %d / %d (%v)\n", p.conn.RemoteAddr(), p.ethereum.Peers().Len(), p.ethereum.MaxPeers, caps) ethlogger.Infof("Added peer (%s) %d / %d (%v)\n", p.conn.RemoteAddr(), p.ethereum.Peers().Len(), p.ethereum.MaxPeers, caps.Raw())
peerlogger.Debugln(p) peerlogger.Debugln(p)
p.pushStatus() capsIt := caps.NewIterator()
for capsIt.Next() {
switch capsIt.Value().Str() {
case "eth":
p.pushStatus()
}
}
} }
func (p *Peer) String() string { func (p *Peer) String() string {