@@ -51,6 +51,12 @@ static constexpr int DUMP_PEERS_INTERVAL = 15 * 60;
51
51
/* * Number of DNS seeds to query when the number of connections is low. */
52
52
static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3 ;
53
53
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
+
54
60
// We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
55
61
#define FEELER_SLEEP_WINDOW 1
56
62
@@ -1573,31 +1579,61 @@ void CConnman::ThreadDNSAddressSeed()
1573
1579
if (gArgs .GetBoolArg (" -forcednsseed" , DEFAULT_FORCEDNSSEED)) {
1574
1580
// When -forcednsseed is provided, query all.
1575
1581
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 ();
1576
1585
}
1577
1586
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
+
1578
1601
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;
1585
1604
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
+ }
1594
1630
}
1595
- seeds_right_now += DNSSEEDS_TO_QUERY_AT_ONCE;
1596
1631
}
1597
1632
1598
1633
if (interruptNet) {
1599
1634
return ;
1600
1635
}
1636
+
1601
1637
LogPrintf (" Loading addresses from DNS seed %s\n " , seed);
1602
1638
if (HaveNameProxy ()) {
1603
1639
AddOneShot (seed);
0 commit comments