@@ -49,6 +49,9 @@ static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed"
49
49
// Dump addresses to peers.dat every 15 minutes (900s)
50
50
static constexpr int DUMP_PEERS_INTERVAL = 15 * 60 ;
51
51
52
+ /* * Number of DNS seeds to query when the number of connections is low. */
53
+ static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3 ;
54
+
52
55
// We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
53
56
#define FEELER_SLEEP_WINDOW 1
54
57
@@ -1508,35 +1511,41 @@ void StopMapPort()
1508
1511
1509
1512
void CConnman::ThreadDNSAddressSeed ()
1510
1513
{
1511
- // goal: only query DNS seeds if address need is acute
1512
- // Avoiding DNS seeds when we don't need them improves user privacy by
1513
- // creating fewer identifying DNS requests, reduces trust by giving seeds
1514
- // less influence on the network topology, and reduces traffic to the seeds.
1515
- if ((addrman.size () > 0 ) &&
1516
- (!gArgs .GetBoolArg (" -forcednsseed" , DEFAULT_FORCEDNSSEED))) {
1517
- if (!interruptNet.sleep_for (std::chrono::seconds (11 )))
1518
- return ;
1514
+ FastRandomContext rng;
1515
+ std::vector<std::string> seeds = Params ().DNSSeeds ();
1516
+ Shuffle (seeds.begin (), seeds.end (), rng);
1517
+ int seeds_right_now = 0 ; // Number of seeds left before testing if we have enough connections
1518
+ int found = 0 ;
1519
1519
1520
- LOCK (cs_vNodes);
1521
- int nRelevant = 0 ;
1522
- for (const CNode* pnode : vNodes) {
1523
- nRelevant += pnode->fSuccessfullyConnected && !pnode->fFeeler && !pnode->fOneShot && !pnode->m_manual_connection && !pnode->fInbound ;
1524
- }
1525
- if (nRelevant >= 2 ) {
1526
- LogPrintf (" P2P peers available. Skipped DNS seeding.\n " );
1527
- return ;
1528
- }
1520
+ if (gArgs .GetBoolArg (" -forcednsseed" , DEFAULT_FORCEDNSSEED)) {
1521
+ // When -forcednsseed is provided, query all.
1522
+ seeds_right_now = seeds.size ();
1529
1523
}
1530
1524
1531
- const std::vector<std::string> &vSeeds = Params ().DNSSeeds ();
1532
- int found = 0 ;
1525
+ for (const std::string& seed : seeds) {
1526
+ // goal: only query DNS seed if address need is acute
1527
+ // Avoiding DNS seeds when we don't need them improves user privacy by
1528
+ // creating fewer identifying DNS requests, reduces trust by giving seeds
1529
+ // less influence on the network topology, and reduces traffic to the seeds.
1530
+ if (addrman.size () > 0 && seeds_right_now == 0 ) {
1531
+ if (!interruptNet.sleep_for (std::chrono::seconds (11 ))) return ;
1533
1532
1534
- LogPrintf (" Loading addresses from DNS seeds (could take a while)\n " );
1533
+ LOCK (cs_vNodes);
1534
+ int nRelevant = 0 ;
1535
+ for (const CNode* pnode : vNodes) {
1536
+ nRelevant += pnode->fSuccessfullyConnected && !pnode->fFeeler && !pnode->fOneShot && !pnode->m_manual_connection && !pnode->fInbound ;
1537
+ }
1538
+ if (nRelevant >= 2 ) {
1539
+ LogPrintf (" P2P peers available. Skipped DNS seeding.\n " );
1540
+ return ;
1541
+ }
1542
+ seeds_right_now += DNSSEEDS_TO_QUERY_AT_ONCE;
1543
+ }
1535
1544
1536
- for (const std::string &seed : vSeeds) {
1537
1545
if (interruptNet) {
1538
1546
return ;
1539
1547
}
1548
+ LogPrintf (" Loading addresses from DNS seed %s\n " , seed);
1540
1549
if (HaveNameProxy ()) {
1541
1550
AddOneShot (seed);
1542
1551
} else {
@@ -1549,13 +1558,11 @@ void CConnman::ThreadDNSAddressSeed()
1549
1558
continue ;
1550
1559
}
1551
1560
unsigned int nMaxIPs = 256 ; // Limits number of IPs learned from a DNS seed
1552
- if (LookupHost (host.c_str (), vIPs, nMaxIPs, true ))
1553
- {
1554
- for (const CNetAddr& ip : vIPs)
1555
- {
1561
+ if (LookupHost (host.c_str (), vIPs, nMaxIPs, true )) {
1562
+ for (const CNetAddr& ip : vIPs) {
1556
1563
int nOneDay = 24 *3600 ;
1557
1564
CAddress addr = CAddress (CService (ip, Params ().GetDefaultPort ()), requiredServiceBits);
1558
- addr.nTime = GetTime () - 3 *nOneDay - GetRand (4 *nOneDay); // use a random age between 3 and 7 days old
1565
+ addr.nTime = GetTime () - 3 *nOneDay - rng. randrange (4 *nOneDay); // use a random age between 3 and 7 days old
1559
1566
vAdd.push_back (addr);
1560
1567
found++;
1561
1568
}
@@ -1566,8 +1573,8 @@ void CConnman::ThreadDNSAddressSeed()
1566
1573
AddOneShot (seed);
1567
1574
}
1568
1575
}
1576
+ --seeds_right_now;
1569
1577
}
1570
-
1571
1578
LogPrintf (" %d addresses found from DNS seeds\n " , found);
1572
1579
}
1573
1580
0 commit comments