forked from cerc-io/plugeth
Connect to previous peer
This commit is contained in:
parent
66e309c5c4
commit
1549a29c9d
23
ethereum.go
23
ethereum.go
@ -2,9 +2,11 @@ package eth
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -31,9 +33,7 @@ var ethlogger = ethlog.NewLogger("SERV")
|
||||
func eachPeer(peers *list.List, callback func(*Peer, *list.Element)) {
|
||||
// Loop thru the peers and close them (if we had them)
|
||||
for e := peers.Front(); e != nil; e = e.Next() {
|
||||
if peer, ok := e.Value.(*Peer); ok {
|
||||
callback(peer, e)
|
||||
}
|
||||
callback(e.Value.(*Peer), e)
|
||||
}
|
||||
}
|
||||
|
||||
@ -399,6 +399,15 @@ func (s *Ethereum) Start(seed bool) {
|
||||
}
|
||||
|
||||
func (s *Ethereum) Seed() {
|
||||
var ips []string
|
||||
data, _ := ethutil.ReadAllFile(path.Join(ethutil.Config.ExecPath, "known_peers.json"))
|
||||
json.Unmarshal([]byte(data), &ips)
|
||||
if len(ips) > 0 {
|
||||
for _, ip := range ips {
|
||||
ethlogger.Infoln("Connecting to previous peer ", ip)
|
||||
s.ConnectToPeer(ip)
|
||||
}
|
||||
} else {
|
||||
ethlogger.Debugln("Retrieving seed nodes")
|
||||
|
||||
// Eth-Go Bootstrapping
|
||||
@ -440,6 +449,7 @@ func (s *Ethereum) Seed() {
|
||||
|
||||
// XXX tmp
|
||||
s.ConnectToPeer(seedNodeAddress)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Ethereum) peerHandler(listener net.Listener) {
|
||||
@ -459,6 +469,13 @@ func (s *Ethereum) Stop() {
|
||||
// Close the database
|
||||
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) {
|
||||
p.Stop()
|
||||
})
|
||||
|
10
peer.go
10
peer.go
@ -680,7 +680,7 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
|
||||
var (
|
||||
p2pVersion = c.Get(0).Uint()
|
||||
clientId = c.Get(1).Str()
|
||||
caps = c.Get(2).Raw()
|
||||
caps = c.Get(2)
|
||||
port = c.Get(3).Uint()
|
||||
pub = c.Get(4).Bytes()
|
||||
)
|
||||
@ -734,11 +734,17 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
|
||||
p.ethereum.PushPeer(p)
|
||||
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)
|
||||
|
||||
capsIt := caps.NewIterator()
|
||||
for capsIt.Next() {
|
||||
switch capsIt.Value().Str() {
|
||||
case "eth":
|
||||
p.pushStatus()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Peer) String() string {
|
||||
|
Loading…
Reference in New Issue
Block a user