Skip to content

Commit fa5894f

Browse files
committed
DNS seeds: wait for 5m instead of 11s if 1000+ peers are known
If 1000 potential peers are known, wait for 5m before querying DNS seeds for more peers, since eventually the addresses we already know should get us connected. Also check every 11s whether we've got enough active outbounds that DNS seeds aren't worth querying, and exit the dnsseed thread early if so.
1 parent 646f0ad commit fa5894f

File tree

1 file changed

+51
-15
lines changed

1 file changed

+51
-15
lines changed

src/net.cpp

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ static constexpr int DUMP_PEERS_INTERVAL = 15 * 60;
5151
/** Number of DNS seeds to query when the number of connections is low. */
5252
static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3;
5353

54+
/** How long to delay before querying DNS seeds
55+
*/
56+
static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS{11}; // 11sec
57+
static constexpr std::chrono::seconds DNSSEEDS_DELAY_MANY_PEERS{300}; // 5min
58+
static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" peers -- you should only get this many if you've been on the live network
59+
5460
// We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
5561
#define FEELER_SLEEP_WINDOW 1
5662

@@ -1573,31 +1579,61 @@ void CConnman::ThreadDNSAddressSeed()
15731579
if (gArgs.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED)) {
15741580
// When -forcednsseed is provided, query all.
15751581
seeds_right_now = seeds.size();
1582+
} else if (addrman.size() == 0) {
1583+
// If we have no known peers, query all.
1584+
seeds_right_now = seeds.size();
15761585
}
15771586

1587+
// goal: only query DNS seed if address need is acute
1588+
// * If we have a reasonable number of peers in addrman, spend
1589+
// some time trying them first. This improves user privacy by
1590+
// creating fewer identifying DNS requests, reduces trust by
1591+
// giving seeds less influence on the network topology, and
1592+
// reduces traffic to the seeds.
1593+
// * When querying DNS seeds query a few at once, this ensures
1594+
// that we don't give DNS seeds the ability to eclipse nodes
1595+
// that query them.
1596+
// * If we continue having problems, eventually query all the
1597+
// DNS seeds, and if that fails too, also try the fixed seeds.
1598+
// (done in ThreadOpenConnections)
1599+
const std::chrono::seconds seeds_wait_time = (addrman.size() >= DNSSEEDS_DELAY_PEER_THRESHOLD ? DNSSEEDS_DELAY_MANY_PEERS : DNSSEEDS_DELAY_FEW_PEERS);
1600+
15781601
for (const std::string& seed : seeds) {
1579-
// goal: only query DNS seed if address need is acute
1580-
// Avoiding DNS seeds when we don't need them improves user privacy by
1581-
// creating fewer identifying DNS requests, reduces trust by giving seeds
1582-
// less influence on the network topology, and reduces traffic to the seeds.
1583-
if (addrman.size() > 0 && seeds_right_now == 0) {
1584-
if (!interruptNet.sleep_for(std::chrono::seconds(11))) return;
1602+
if (seeds_right_now == 0) {
1603+
seeds_right_now += DNSSEEDS_TO_QUERY_AT_ONCE;
15851604

1586-
LOCK(cs_vNodes);
1587-
int nRelevant = 0;
1588-
for (const CNode* pnode : vNodes) {
1589-
nRelevant += pnode->fSuccessfullyConnected && !pnode->fFeeler && !pnode->fOneShot && !pnode->m_manual_connection && !pnode->fInbound;
1590-
}
1591-
if (nRelevant >= 2) {
1592-
LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1593-
return;
1605+
if (addrman.size() > 0) {
1606+
LogPrintf("Waiting %d seconds before querying DNS seeds.\n", seeds_wait_time.count());
1607+
std::chrono::seconds to_wait = seeds_wait_time;
1608+
while (to_wait.count() > 0) {
1609+
std::chrono::seconds w = std::min(DNSSEEDS_DELAY_FEW_PEERS, to_wait);
1610+
if (!interruptNet.sleep_for(w)) return;
1611+
to_wait -= w;
1612+
1613+
int nRelevant = 0;
1614+
{
1615+
LOCK(cs_vNodes);
1616+
for (const CNode* pnode : vNodes) {
1617+
nRelevant += pnode->fSuccessfullyConnected && !pnode->fFeeler && !pnode->fOneShot && !pnode->m_manual_connection && !pnode->fInbound;
1618+
}
1619+
}
1620+
if (nRelevant >= 2) {
1621+
if (found > 0) {
1622+
LogPrintf("%d addresses found from DNS seeds\n", found);
1623+
LogPrintf("P2P peers available. Finished DNS seeding.\n");
1624+
} else {
1625+
LogPrintf("P2P peers available. Skipped DNS seeding.\n");
1626+
}
1627+
return;
1628+
}
1629+
}
15941630
}
1595-
seeds_right_now += DNSSEEDS_TO_QUERY_AT_ONCE;
15961631
}
15971632

15981633
if (interruptNet) {
15991634
return;
16001635
}
1636+
16011637
LogPrintf("Loading addresses from DNS seed %s\n", seed);
16021638
if (HaveNameProxy()) {
16031639
AddOneShot(seed);

0 commit comments

Comments
 (0)