Skip to content

Commit 54db549

Browse files
committed
p2p: add static node dialing test
1 parent e82ddd9 commit 54db549

File tree

2 files changed

+108
-13
lines changed

2 files changed

+108
-13
lines changed

p2p/server.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ type Server struct {
100100

101101
ourHandshake *protoHandshake
102102

103-
lock sync.RWMutex // protects running, peers and the trust fields
104-
running bool
105-
peers map[discover.NodeID]*Peer
106-
statics map[discover.NodeID]*discover.Node // Map of currently static remote nodes
107-
staticDial chan *discover.Node // Dial request channel reserved for the static nodes
103+
lock sync.RWMutex // protects running, peers and the trust fields
104+
running bool
105+
peers map[discover.NodeID]*Peer
106+
staticNodes map[discover.NodeID]*discover.Node // Map of currently maintained static remote nodes
107+
staticDial chan *discover.Node // Dial request channel reserved for the static nodes
108+
staticCycle time.Duration // Overrides staticPeerCheckInterval, used for testing
108109

109110
ntab *discover.Table
110111
listener net.Listener
@@ -144,7 +145,7 @@ func (srv *Server) AddPeer(node *discover.Node) {
144145
srv.lock.Lock()
145146
defer srv.lock.Unlock()
146147

147-
srv.statics[node.ID] = node
148+
srv.staticNodes[node.ID] = node
148149
}
149150

150151
// Broadcast sends an RLP-encoded message to all connected peers.
@@ -207,9 +208,9 @@ func (srv *Server) Start() (err error) {
207208
srv.peers = make(map[discover.NodeID]*Peer)
208209

209210
// Create the current trust map, and the associated dialing channel
210-
srv.statics = make(map[discover.NodeID]*discover.Node)
211+
srv.staticNodes = make(map[discover.NodeID]*discover.Node)
211212
for _, node := range srv.StaticNodes {
212-
srv.statics[node.ID] = node
213+
srv.staticNodes[node.ID] = node
213214
}
214215
srv.staticDial = make(chan *discover.Node)
215216

@@ -345,17 +346,23 @@ func (srv *Server) listenLoop() {
345346
// staticNodesLoop is responsible for periodically checking that static
346347
// connections are actually live, and requests dialing if not.
347348
func (srv *Server) staticNodesLoop() {
348-
tick := time.Tick(staticPeerCheckInterval)
349+
// Create a default maintenance ticker, but override it requested
350+
cycle := staticPeerCheckInterval
351+
if srv.staticCycle != 0 {
352+
cycle = srv.staticCycle
353+
}
354+
tick := time.NewTicker(cycle)
355+
349356
for {
350357
select {
351358
case <-srv.quit:
352359
return
353360

354-
case <-tick:
361+
case <-tick.C:
355362
// Collect all the non-connected static nodes
356363
needed := []*discover.Node{}
357364
srv.lock.RLock()
358-
for id, node := range srv.statics {
365+
for id, node := range srv.staticNodes {
359366
if _, ok := srv.peers[id]; !ok {
360367
needed = append(needed, node)
361368
}
@@ -473,7 +480,7 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
473480
srv.lock.RLock()
474481
atcap := len(srv.peers) == srv.MaxPeers
475482
if dest != nil {
476-
if _, ok := srv.statics[dest.ID]; ok {
483+
if _, ok := srv.staticNodes[dest.ID]; ok {
477484
atcap = false
478485
}
479486
}
@@ -536,7 +543,7 @@ func (srv *Server) addPeer(id discover.NodeID, p *Peer) (bool, DiscReason) {
536543
// in the pool, or if it's of no use.
537544
func (srv *Server) checkPeer(id discover.NodeID) (bool, DiscReason) {
538545
// First up, figure out if the peer is static
539-
_, static := srv.statics[id]
546+
_, static := srv.staticNodes[id]
540547

541548
// Make sure the peer passes all required checks
542549
switch {

p2p/server_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,94 @@ func TestServerDisconnectAtCap(t *testing.T) {
219219
}
220220
}
221221

222+
// Tests that static peers are (re)connected, and done so even above max peers.
223+
func TestServerStaticPeers(t *testing.T) {
224+
defer testlog(t).detach()
225+
226+
// Create a test server with limited connection slots
227+
started := make(chan *Peer)
228+
server := &Server{
229+
ListenAddr: "127.0.0.1:0",
230+
PrivateKey: newkey(),
231+
MaxPeers: 3,
232+
newPeerHook: func(p *Peer) { started <- p },
233+
staticCycle: time.Second,
234+
}
235+
if err := server.Start(); err != nil {
236+
t.Fatal(err)
237+
}
238+
defer server.Stop()
239+
240+
// Fill up all the slots on the server
241+
dialer := &net.Dialer{Deadline: time.Now().Add(3 * time.Second)}
242+
for i := 0; i < server.MaxPeers; i++ {
243+
// Establish a new connection
244+
conn, err := dialer.Dial("tcp", server.ListenAddr)
245+
if err != nil {
246+
t.Fatalf("conn %d: dial error: %v", i, err)
247+
}
248+
defer conn.Close()
249+
250+
// Run the handshakes just like a real peer would, and wait for completion
251+
key := newkey()
252+
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
253+
if _, err = setupConn(conn, key, shake, server.Self(), false); err != nil {
254+
t.Fatalf("conn %d: unexpected error: %v", i, err)
255+
}
256+
<-started
257+
}
258+
// Open a TCP listener to accept static connections
259+
listener, err := net.Listen("tcp", "127.0.0.1:0")
260+
if err != nil {
261+
t.Fatalf("failed to setup listener: %v", err)
262+
}
263+
defer listener.Close()
264+
265+
connected := make(chan net.Conn)
266+
go func() {
267+
for i := 0; i < 3; i++ {
268+
conn, err := listener.Accept()
269+
if err == nil {
270+
connected <- conn
271+
}
272+
}
273+
}()
274+
// Inject a static node and wait for a remote dial, then redial, then nothing
275+
addr := listener.Addr().(*net.TCPAddr)
276+
static := &discover.Node{
277+
ID: discover.PubkeyID(&newkey().PublicKey),
278+
IP: addr.IP,
279+
TCPPort: addr.Port,
280+
}
281+
server.AddPeer(static)
282+
283+
select {
284+
case conn := <-connected:
285+
// Close the first connection, expect redial
286+
conn.Close()
287+
288+
case <-time.After(2 * server.staticCycle):
289+
t.Fatalf("remote dial timeout")
290+
}
291+
292+
select {
293+
case conn := <-connected:
294+
// Keep the second connection, don't expect redial
295+
defer conn.Close()
296+
297+
case <-time.After(2 * server.staticCycle):
298+
t.Fatalf("remote re-dial timeout")
299+
}
300+
301+
select {
302+
case <-time.After(2 * server.staticCycle):
303+
// Timeout as no dial occurred
304+
305+
case <-connected:
306+
t.Fatalf("connected node dialed")
307+
}
308+
}
309+
222310
/*
223311
// Tests that trusted peers and can connect above max peer caps.
224312
func TestServerTrustedPeers(t *testing.T) {

0 commit comments

Comments
 (0)