diff --git a/p2p/dial.go b/p2p/dial.go index 58075c5a933ca19c2a0dc7234433cfaeabae160d..d15458f4d3573a2ea98990389c1e1a8e01261235 100644 --- a/p2p/dial.go +++ b/p2p/dial.go @@ -240,7 +240,7 @@ loop: for { // Launch new dials if slots are available. slots := d.freeDialSlots() - slots -= d.startStaticDials(slots) + d.startStaticDials() if slots > 0 { nodesCh = d.nodesIn } else { @@ -421,14 +421,13 @@ func (d *dialScheduler) checkDial(n *enode.Node) error { } // startStaticDials starts n static dial tasks. -func (d *dialScheduler) startStaticDials(n int) (started int) { - for started = 0; started < n && len(d.staticPool) > 0; started++ { +func (d *dialScheduler) startStaticDials() { + for len(d.staticPool) > 0 { idx := d.rand.Intn(len(d.staticPool)) task := d.staticPool[idx] d.startDial(task) d.removeFromStaticPool(idx) } - return started } // updateStaticPool attempts to move the given static dial back into staticPool. diff --git a/p2p/dial_test.go b/p2p/dial_test.go index 99c54bfe4c0dfd2225143e9f5839508710b663e7..c6746861f60b82a43019039ecad66a950e449254 100644 --- a/p2p/dial_test.go +++ b/p2p/dial_test.go @@ -179,19 +179,22 @@ func TestDialSchedStaticDial(t *testing.T) { d.addStatic(newNode(uintID(0x05), "127.0.0.5:30303")) d.addStatic(newNode(uintID(0x06), "127.0.0.6:30303")) d.addStatic(newNode(uintID(0x07), "127.0.0.7:30303")) - d.addStatic(newNode(uintID(0x08), "127.0.0.8:30303")) - d.addStatic(newNode(uintID(0x09), "127.0.0.9:30303")) }, wantNewDials: []*enode.Node{ newNode(uintID(0x03), "127.0.0.3:30303"), newNode(uintID(0x04), "127.0.0.4:30303"), newNode(uintID(0x05), "127.0.0.5:30303"), newNode(uintID(0x06), "127.0.0.6:30303"), + newNode(uintID(0x07), "127.0.0.7:30303"), }, }, // Dial to 0x03 completes, filling a peer slot. One slot remains, // two dials are launched to attempt to fill it. { + update: func(d *dialScheduler) { + d.addStatic(newNode(uintID(0x08), "127.0.0.8:30303")) + d.addStatic(newNode(uintID(0x09), "127.0.0.9:30303")) + }, succeeded: []enode.ID{ uintID(0x03), }, @@ -235,7 +238,7 @@ func TestDialSchedRemoveStatic(t *testing.T) { maxDialPeers: 1, } runDialTest(t, config, []dialTestRound{ - // Add static nodes. + // Add static nodes. Ignore maxActiveDials config { update: func(d *dialScheduler) { d.addStatic(newNode(uintID(0x01), "127.0.0.1:30303")) @@ -244,10 +247,15 @@ func TestDialSchedRemoveStatic(t *testing.T) { }, wantNewDials: []*enode.Node{ newNode(uintID(0x01), "127.0.0.1:30303"), + newNode(uintID(0x02), "127.0.0.2:30303"), + newNode(uintID(0x03), "127.0.0.3:30303"), }, }, // Dial to 0x01 fails. { + update: func(d *dialScheduler) { + d.addStatic(newNode(uintID(0x04), "127.0.0.4:30303")) + }, failed: []enode.ID{ uintID(0x01), }, @@ -255,7 +263,7 @@ func TestDialSchedRemoveStatic(t *testing.T) { uintID(0x01): nil, }, wantNewDials: []*enode.Node{ - newNode(uintID(0x02), "127.0.0.2:30303"), + newNode(uintID(0x04), "127.0.0.4:30303"), }, }, // All static nodes are removed. 0x01 is in history, 0x02 is being @@ -265,6 +273,7 @@ func TestDialSchedRemoveStatic(t *testing.T) { d.removeStatic(newNode(uintID(0x01), "127.0.0.1:30303")) d.removeStatic(newNode(uintID(0x02), "127.0.0.2:30303")) d.removeStatic(newNode(uintID(0x03), "127.0.0.3:30303")) + d.removeStatic(newNode(uintID(0x04), "127.0.0.4:30303")) }, failed: []enode.ID{ uintID(0x02), @@ -278,39 +287,6 @@ func TestDialSchedRemoveStatic(t *testing.T) { }) } -// This test checks that static dials are selected at random. -func TestDialSchedManyStaticNodes(t *testing.T) { - t.Parallel() - - config := dialConfig{maxDialPeers: 2} - runDialTest(t, config, []dialTestRound{ - { - peersAdded: []*conn{ - {flags: dynDialedConn, node: newNode(uintID(0xFFFE), "")}, - {flags: dynDialedConn, node: newNode(uintID(0xFFFF), "")}, - }, - update: func(d *dialScheduler) { - for id := uint16(0); id < 2000; id++ { - n := newNode(uintID(id), "127.0.0.1:30303") - d.addStatic(n) - } - }, - }, - { - peersRemoved: []enode.ID{ - uintID(0xFFFE), - uintID(0xFFFF), - }, - wantNewDials: []*enode.Node{ - newNode(uintID(0x0085), "127.0.0.1:30303"), - newNode(uintID(0x02dc), "127.0.0.1:30303"), - newNode(uintID(0x0285), "127.0.0.1:30303"), - newNode(uintID(0x00cb), "127.0.0.1:30303"), - }, - }, - }) -} - // This test checks that past dials are not retried for some time. func TestDialSchedHistory(t *testing.T) { t.Parallel()