aboutsummaryrefslogtreecommitdiffstats
path: root/p2p
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-04-30 21:15:29 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-04-30 21:15:29 +0800
commit701591b403a8bae8c1dfb648a49d587968ff0c6a (patch)
tree9e253db29faf15056fa72f7a0094206b5ef39beb /p2p
parent1528dbc17101597348eefe3f3fb8d4f0d5c54b3c (diff)
downloadgo-tangerine-701591b403a8bae8c1dfb648a49d587968ff0c6a.tar
go-tangerine-701591b403a8bae8c1dfb648a49d587968ff0c6a.tar.gz
go-tangerine-701591b403a8bae8c1dfb648a49d587968ff0c6a.tar.bz2
go-tangerine-701591b403a8bae8c1dfb648a49d587968ff0c6a.tar.lz
go-tangerine-701591b403a8bae8c1dfb648a49d587968ff0c6a.tar.xz
go-tangerine-701591b403a8bae8c1dfb648a49d587968ff0c6a.tar.zst
go-tangerine-701591b403a8bae8c1dfb648a49d587968ff0c6a.zip
cmd, eth, p2p: fix review issues enumerated by Felix
Diffstat (limited to 'p2p')
-rw-r--r--p2p/server.go31
-rw-r--r--p2p/server_test.go2
2 files changed, 12 insertions, 21 deletions
diff --git a/p2p/server.go b/p2p/server.go
index d8c5ecd77..dbb2e5f9e 100644
--- a/p2p/server.go
+++ b/p2p/server.go
@@ -100,10 +100,9 @@ type Server struct {
ourHandshake *protoHandshake
- lock sync.RWMutex // protects running and peers
- running bool
- peers map[discover.NodeID]*Peer
-
+ lock sync.RWMutex // protects running, peers and the trust fields
+ running bool
+ peers map[discover.NodeID]*Peer
trusts map[discover.NodeID]*discover.Node // Map of currently trusted remote nodes
trustDial chan *discover.Node // Dial request channel reserved for the trusted nodes
@@ -138,8 +137,10 @@ func (srv *Server) PeerCount() int {
return n
}
-// TrustPeer inserts a node into the list of privileged nodes.
-func (srv *Server) TrustPeer(node *discover.Node) {
+// 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 (srv *Server) AddPeer(node *discover.Node) {
srv.lock.Lock()
defer srv.lock.Unlock()
@@ -246,7 +247,7 @@ func (srv *Server) Start() (err error) {
glog.V(logger.Warn).Infoln("I will be kind-of useless, neither dialing nor listening.")
}
// maintain the trusted peers
- go srv.trustLoop()
+ go srv.trustedNodesLoop()
srv.running = true
return nil
@@ -341,16 +342,13 @@ func (srv *Server) listenLoop() {
}
}
-// trustLoop is responsible for periodically checking that trusted connections
-// are actually live, and requests dialing if not.
-func (srv *Server) trustLoop() {
- // Create a ticker for verifying trusted connections
+// trustedNodesLoop is responsible for periodically checking that trusted
+// connections are actually live, and requests dialing if not.
+func (srv *Server) trustedNodesLoop() {
tick := time.Tick(trustedPeerCheckInterval)
-
for {
select {
case <-srv.quit:
- // Termination requested, simple return
return
case <-tick:
@@ -369,10 +367,7 @@ func (srv *Server) trustLoop() {
glog.V(logger.Debug).Infof("Dialing trusted peer %v", node)
select {
case srv.trustDial <- node:
- // Ok, dialing
-
case <-srv.quit:
- // Terminating, return
return
}
}
@@ -547,16 +542,12 @@ func (srv *Server) checkPeer(id discover.NodeID) (bool, DiscReason) {
switch {
case !srv.running:
return false, DiscQuitting
-
case !trusted && len(srv.peers) >= srv.MaxPeers:
return false, DiscTooManyPeers
-
case srv.peers[id] != nil:
return false, DiscAlreadyConnected
-
case id == srv.ntab.Self().ID:
return false, DiscSelf
-
default:
return true, 0
}
diff --git a/p2p/server_test.go b/p2p/server_test.go
index a79679ac1..3e3fd6cc0 100644
--- a/p2p/server_test.go
+++ b/p2p/server_test.go
@@ -260,7 +260,7 @@ func TestServerTrustedPeers(t *testing.T) {
trusted := &discover.Node{
ID: discover.PubkeyID(&key.PublicKey),
}
- server.TrustPeer(trusted)
+ server.AddPeer(trusted)
conn, err := dialer.Dial("tcp", server.ListenAddr)
if err != nil {