Skip to content

Commit 04fcae2

Browse files
karalabefjl
authored andcommitted
p2p: if no nodes are connected, attempt dialing bootnodes (#13874)
1 parent 542e42b commit 04fcae2

3 files changed

Lines changed: 121 additions & 9 deletions

File tree

‎p2p/dial.go‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ const (
3838
// once every few seconds.
3939
lookupInterval = 4 * time.Second
4040

41+
// If no peers are found for this amount of time, the initial bootnodes are
42+
// attempted to be connected.
43+
fallbackInterval = 20 * time.Second
44+
4145
// Endpoint resolution is throttled with bounded backoff.
4246
initialResolveDelay = 60 * time.Second
4347
maxResolveDelay = time.Hour
@@ -57,6 +61,9 @@ type dialstate struct {
5761
randomNodes []*discover.Node // filled from Table
5862
static map[discover.NodeID]*dialTask
5963
hist *dialHistory
64+
65+
start time.Time // time when the dialer was first used
66+
bootnodes []*discover.Node // default dials when there are no peers
6067
}
6168

6269
type discoverTable interface {
@@ -102,16 +109,18 @@ type waitExpireTask struct {
102109
time.Duration
103110
}
104111

105-
func newDialState(static []*discover.Node, ntab discoverTable, maxdyn int, netrestrict *netutil.Netlist) *dialstate {
112+
func newDialState(static []*discover.Node, bootnodes []*discover.Node, ntab discoverTable, maxdyn int, netrestrict *netutil.Netlist) *dialstate {
106113
s := &dialstate{
107114
maxDynDials: maxdyn,
108115
ntab: ntab,
109116
netrestrict: netrestrict,
110117
static: make(map[discover.NodeID]*dialTask),
111118
dialing: make(map[discover.NodeID]connFlag),
119+
bootnodes: make([]*discover.Node, len(bootnodes)),
112120
randomNodes: make([]*discover.Node, maxdyn/2),
113121
hist: new(dialHistory),
114122
}
123+
copy(s.bootnodes, bootnodes)
115124
for _, n := range static {
116125
s.addStatic(n)
117126
}
@@ -130,6 +139,10 @@ func (s *dialstate) removeStatic(n *discover.Node) {
130139
}
131140

132141
func (s *dialstate) newTasks(nRunning int, peers map[discover.NodeID]*Peer, now time.Time) []task {
142+
if s.start == (time.Time{}) {
143+
s.start = now
144+
}
145+
133146
var newtasks []task
134147
addDial := func(flag connFlag, n *discover.Node) bool {
135148
if err := s.checkDial(n, peers); err != nil {
@@ -169,7 +182,18 @@ func (s *dialstate) newTasks(nRunning int, peers map[discover.NodeID]*Peer, now
169182
newtasks = append(newtasks, t)
170183
}
171184
}
172-
185+
// If we don't have any peers whatsoever, try to dial a random bootnode. This
186+
// scenario is useful for the testnet (and private networks) where the discovery
187+
// table might be full of mostly bad peers, making it hard to find good ones.
188+
if len(peers) == 0 && len(s.bootnodes) > 0 && needDynDials > 0 && now.Sub(s.start) > fallbackInterval {
189+
bootnode := s.bootnodes[0]
190+
s.bootnodes = append(s.bootnodes[:0], s.bootnodes[1:]...)
191+
s.bootnodes = append(s.bootnodes, bootnode)
192+
193+
if addDial(dynDialedConn, bootnode) {
194+
needDynDials--
195+
}
196+
}
173197
// Use random nodes from the table for half of the necessary
174198
// dynamic dials.
175199
randomCandidates := needDynDials / 2

‎p2p/dial_test.go‎

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (t fakeTable) ReadRandomNodes(buf []*discover.Node) int { return copy(buf,
8787
// This test checks that dynamic dials are launched from discovery results.
8888
func TestDialStateDynDial(t *testing.T) {
8989
runDialTest(t, dialtest{
90-
init: newDialState(nil, fakeTable{}, 5, nil),
90+
init: newDialState(nil, nil, fakeTable{}, 5, nil),
9191
rounds: []round{
9292
// A discovery query is launched.
9393
{
@@ -219,6 +219,94 @@ func TestDialStateDynDial(t *testing.T) {
219219
})
220220
}
221221

222+
// Tests that bootnodes are dialed if no peers are connectd, but not otherwise.
223+
func TestDialStateDynDialBootnode(t *testing.T) {
224+
bootnodes := []*discover.Node{
225+
{ID: uintID(1)},
226+
{ID: uintID(2)},
227+
{ID: uintID(3)},
228+
}
229+
table := fakeTable{
230+
{ID: uintID(4)},
231+
{ID: uintID(5)},
232+
{ID: uintID(6)},
233+
{ID: uintID(7)},
234+
{ID: uintID(8)},
235+
}
236+
runDialTest(t, dialtest{
237+
init: newDialState(nil, bootnodes, table, 5, nil),
238+
rounds: []round{
239+
// 2 dynamic dials attempted, bootnodes pending fallback interval
240+
{
241+
new: []task{
242+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(4)}},
243+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(5)}},
244+
&discoverTask{},
245+
},
246+
},
247+
// No dials succeed, bootnodes still pending fallback interval
248+
{
249+
done: []task{
250+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(4)}},
251+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(5)}},
252+
},
253+
},
254+
// No dials succeed, bootnodes still pending fallback interval
255+
{},
256+
// No dials succeed, 2 dynamic dials attempted and 1 bootnode too as fallback interval was reached
257+
{
258+
new: []task{
259+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(1)}},
260+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(4)}},
261+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(5)}},
262+
},
263+
},
264+
// No dials succeed, 2nd bootnode is attempted
265+
{
266+
done: []task{
267+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(1)}},
268+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(4)}},
269+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(5)}},
270+
},
271+
new: []task{
272+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(2)}},
273+
},
274+
},
275+
// No dials succeed, 3rd bootnode is attempted
276+
{
277+
done: []task{
278+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(2)}},
279+
},
280+
new: []task{
281+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(3)}},
282+
},
283+
},
284+
// No dials succeed, 1st bootnode is attempted again, expired random nodes retried
285+
{
286+
done: []task{
287+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(3)}},
288+
},
289+
new: []task{
290+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(1)}},
291+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(4)}},
292+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(5)}},
293+
},
294+
},
295+
// Random dial succeeds, no more bootnodes are attempted
296+
{
297+
peers: []*Peer{
298+
{rw: &conn{flags: dynDialedConn, id: uintID(4)}},
299+
},
300+
done: []task{
301+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(1)}},
302+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(4)}},
303+
&dialTask{flags: dynDialedConn, dest: &discover.Node{ID: uintID(5)}},
304+
},
305+
},
306+
},
307+
})
308+
}
309+
222310
func TestDialStateDynDialFromTable(t *testing.T) {
223311
// This table always returns the same random nodes
224312
// in the order given below.
@@ -234,7 +322,7 @@ func TestDialStateDynDialFromTable(t *testing.T) {
234322
}
235323

236324
runDialTest(t, dialtest{
237-
init: newDialState(nil, table, 10, nil),
325+
init: newDialState(nil, nil, table, 10, nil),
238326
rounds: []round{
239327
// 5 out of 8 of the nodes returned by ReadRandomNodes are dialed.
240328
{
@@ -332,7 +420,7 @@ func TestDialStateNetRestrict(t *testing.T) {
332420
restrict.Add("127.0.2.0/24")
333421

334422
runDialTest(t, dialtest{
335-
init: newDialState(nil, table, 10, restrict),
423+
init: newDialState(nil, nil, table, 10, restrict),
336424
rounds: []round{
337425
{
338426
new: []task{
@@ -355,7 +443,7 @@ func TestDialStateStaticDial(t *testing.T) {
355443
}
356444

357445
runDialTest(t, dialtest{
358-
init: newDialState(wantStatic, fakeTable{}, 0, nil),
446+
init: newDialState(wantStatic, nil, fakeTable{}, 0, nil),
359447
rounds: []round{
360448
// Static dials are launched for the nodes that
361449
// aren't yet connected.
@@ -436,7 +524,7 @@ func TestDialStateCache(t *testing.T) {
436524
}
437525

438526
runDialTest(t, dialtest{
439-
init: newDialState(wantStatic, fakeTable{}, 0, nil),
527+
init: newDialState(wantStatic, nil, fakeTable{}, 0, nil),
440528
rounds: []round{
441529
// Static dials are launched for the nodes that
442530
// aren't yet connected.
@@ -498,7 +586,7 @@ func TestDialStateCache(t *testing.T) {
498586
func TestDialResolve(t *testing.T) {
499587
resolved := discover.NewNode(uintID(1), net.IP{127, 0, 55, 234}, 3333, 4444)
500588
table := &resolveMock{answer: resolved}
501-
state := newDialState(nil, table, 0, nil)
589+
state := newDialState(nil, nil, table, 0, nil)
502590

503591
// Check that the task is generated with an incomplete ID.
504592
dest := discover.NewNode(uintID(1), nil, 0, 0)

‎p2p/server.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func (srv *Server) Start() (err error) {
396396
if !srv.Discovery {
397397
dynPeers = 0
398398
}
399-
dialer := newDialState(srv.StaticNodes, srv.ntab, dynPeers, srv.NetRestrict)
399+
dialer := newDialState(srv.StaticNodes, srv.BootstrapNodes, srv.ntab, dynPeers, srv.NetRestrict)
400400

401401
// handshake
402402
srv.ourHandshake = &protoHandshake{Version: baseProtocolVersion, Name: srv.Name, ID: discover.PubkeyID(&srv.PrivateKey.PublicKey)}

0 commit comments

Comments
 (0)