aboutsummaryrefslogtreecommitdiffstats
path: root/p2p
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-05-04 18:08:42 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-05-04 18:08:42 +0800
commit54db54931e10c37a6ef2cd3fca36a875b02140c4 (patch)
treea557d727c87a46458d6324d473fb6651230725d5 /p2p
parente82ddd9198f6c26f9aeba603d2e0fadd5a60bf96 (diff)
downloadgo-tangerine-54db54931e10c37a6ef2cd3fca36a875b02140c4.tar
go-tangerine-54db54931e10c37a6ef2cd3fca36a875b02140c4.tar.gz
go-tangerine-54db54931e10c37a6ef2cd3fca36a875b02140c4.tar.bz2
go-tangerine-54db54931e10c37a6ef2cd3fca36a875b02140c4.tar.lz
go-tangerine-54db54931e10c37a6ef2cd3fca36a875b02140c4.tar.xz
go-tangerine-54db54931e10c37a6ef2cd3fca36a875b02140c4.tar.zst
go-tangerine-54db54931e10c37a6ef2cd3fca36a875b02140c4.zip
p2p: add static node dialing test
Diffstat (limited to 'p2p')
-rw-r--r--p2p/server.go33
-rw-r--r--p2p/server_test.go88
2 files changed, 108 insertions, 13 deletions
diff --git a/p2p/server.go b/p2p/server.go
index 546b22744..3099190b6 100644
--- a/p2p/server.go
+++ b/p2p/server.go
@@ -100,11 +100,12 @@ type Server struct {
ourHandshake *protoHandshake
- lock sync.RWMutex // protects running, peers and the trust fields
- running bool
- peers map[discover.NodeID]*Peer
- statics map[discover.NodeID]*discover.Node // Map of currently static remote nodes
- staticDial chan *discover.Node // Dial request channel reserved for the static nodes
+ lock sync.RWMutex // protects running, peers and the trust fields
+ running bool
+ peers map[discover.NodeID]*Peer
+ staticNodes map[discover.NodeID]*discover.Node // Map of currently maintained static remote nodes
+ staticDial chan *discover.Node // Dial request channel reserved for the static nodes
+ staticCycle time.Duration // Overrides staticPeerCheckInterval, used for testing
ntab *discover.Table
listener net.Listener
@@ -144,7 +145,7 @@ func (srv *Server) AddPeer(node *discover.Node) {
srv.lock.Lock()
defer srv.lock.Unlock()
- srv.statics[node.ID] = node
+ srv.staticNodes[node.ID] = node
}
// Broadcast sends an RLP-encoded message to all connected peers.
@@ -207,9 +208,9 @@ func (srv *Server) Start() (err error) {
srv.peers = make(map[discover.NodeID]*Peer)
// Create the current trust map, and the associated dialing channel
- srv.statics = make(map[discover.NodeID]*discover.Node)
+ srv.staticNodes = make(map[discover.NodeID]*discover.Node)
for _, node := range srv.StaticNodes {
- srv.statics[node.ID] = node
+ srv.staticNodes[node.ID] = node
}
srv.staticDial = make(chan *discover.Node)
@@ -345,17 +346,23 @@ func (srv *Server) listenLoop() {
// staticNodesLoop is responsible for periodically checking that static
// connections are actually live, and requests dialing if not.
func (srv *Server) staticNodesLoop() {
- tick := time.Tick(staticPeerCheckInterval)
+ // Create a default maintenance ticker, but override it requested
+ cycle := staticPeerCheckInterval
+ if srv.staticCycle != 0 {
+ cycle = srv.staticCycle
+ }
+ tick := time.NewTicker(cycle)
+
for {
select {
case <-srv.quit:
return
- case <-tick:
+ case <-tick.C:
// Collect all the non-connected static nodes
needed := []*discover.Node{}
srv.lock.RLock()
- for id, node := range srv.statics {
+ for id, node := range srv.staticNodes {
if _, ok := srv.peers[id]; !ok {
needed = append(needed, node)
}
@@ -473,7 +480,7 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
srv.lock.RLock()
atcap := len(srv.peers) == srv.MaxPeers
if dest != nil {
- if _, ok := srv.statics[dest.ID]; ok {
+ if _, ok := srv.staticNodes[dest.ID]; ok {
atcap = false
}
}
@@ -536,7 +543,7 @@ func (srv *Server) addPeer(id discover.NodeID, p *Peer) (bool, DiscReason) {
// in the pool, or if it's of no use.
func (srv *Server) checkPeer(id discover.NodeID) (bool, DiscReason) {
// First up, figure out if the peer is static
- _, static := srv.statics[id]
+ _, static := srv.staticNodes[id]
// Make sure the peer passes all required checks
switch {
diff --git a/p2p/server_test.go b/p2p/server_test.go
index b48361235..606fa9386 100644
--- a/p2p/server_test.go
+++ b/p2p/server_test.go
@@ -219,6 +219,94 @@ func TestServerDisconnectAtCap(t *testing.T) {
}
}
+// Tests that static peers are (re)connected, and done so even above max peers.
+func TestServerStaticPeers(t *testing.T) {
+ defer testlog(t).detach()
+
+ // Create a test server with limited connection slots
+ started := make(chan *Peer)
+ server := &Server{
+ ListenAddr: "127.0.0.1:0",
+ PrivateKey: newkey(),
+ MaxPeers: 3,
+ newPeerHook: func(p *Peer) { started <- p },
+ staticCycle: time.Second,
+ }
+ if err := server.Start(); err != nil {
+ t.Fatal(err)
+ }
+ defer server.Stop()
+
+ // Fill up all the slots on the server
+ dialer := &net.Dialer{Deadline: time.Now().Add(3 * time.Second)}
+ for i := 0; i < server.MaxPeers; i++ {
+ // Establish a new connection
+ conn, err := dialer.Dial("tcp", server.ListenAddr)
+ if err != nil {
+ t.Fatalf("conn %d: dial error: %v", i, err)
+ }
+ defer conn.Close()
+
+ // Run the handshakes just like a real peer would, and wait for completion
+ key := newkey()
+ shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
+ if _, err = setupConn(conn, key, shake, server.Self(), false); err != nil {
+ t.Fatalf("conn %d: unexpected error: %v", i, err)
+ }
+ <-started
+ }
+ // Open a TCP listener to accept static connections
+ listener, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatalf("failed to setup listener: %v", err)
+ }
+ defer listener.Close()
+
+ connected := make(chan net.Conn)
+ go func() {
+ for i := 0; i < 3; i++ {
+ conn, err := listener.Accept()
+ if err == nil {
+ connected <- conn
+ }
+ }
+ }()
+ // Inject a static node and wait for a remote dial, then redial, then nothing
+ addr := listener.Addr().(*net.TCPAddr)
+ static := &discover.Node{
+ ID: discover.PubkeyID(&newkey().PublicKey),
+ IP: addr.IP,
+ TCPPort: addr.Port,
+ }
+ server.AddPeer(static)
+
+ select {
+ case conn := <-connected:
+ // Close the first connection, expect redial
+ conn.Close()
+
+ case <-time.After(2 * server.staticCycle):
+ t.Fatalf("remote dial timeout")
+ }
+
+ select {
+ case conn := <-connected:
+ // Keep the second connection, don't expect redial
+ defer conn.Close()
+
+ case <-time.After(2 * server.staticCycle):
+ t.Fatalf("remote re-dial timeout")
+ }
+
+ select {
+ case <-time.After(2 * server.staticCycle):
+ // Timeout as no dial occurred
+
+ case <-connected:
+ t.Fatalf("connected node dialed")
+ }
+}
+
/*
// Tests that trusted peers and can connect above max peer caps.
func TestServerTrustedPeers(t *testing.T) {