Skip to content

Commit 657b82c

Browse files
committed
Merge #19084: net: improve code documentation for dns seed behaviour
5cb7ee6 net: improve code documentation for dns seed behaviour (Anthony Towns) Pull request description: Some better internal documentation post #16939 ACKs for top commit: hebasto: ACK 5cb7ee6 naumenkogs: ACK 5cb7ee6 ariard: ACK 5cb7ee6 fanquake: ACK 5cb7ee6 - thanks for following up. Tree-SHA512: 5a12680651cd1e0436aed1cadcbf50047ffeabfdc9f7f2f81fa176c30b10673fc960154c73ec34ed08ace1a000a81cedd44d67587883152654dee46065991b45
2 parents 3657aee + 5cb7ee6 commit 657b82c

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/net.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,17 @@ static constexpr std::chrono::minutes DUMP_PEERS_INTERVAL{15};
5353
static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3;
5454

5555
/** How long to delay before querying DNS seeds
56+
*
57+
* If we have more than THRESHOLD entries in addrman, then it's likely
58+
* that we got those addresses from having previously connected to the P2P
59+
* network, and that we'll be able to successfully reconnect to the P2P
60+
* network via contacting one of them. So if that's the case, spend a
61+
* little longer trying to connect to known peers before querying the
62+
* DNS seeds.
5663
*/
57-
static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS{11}; // 11sec
58-
static constexpr std::chrono::seconds DNSSEEDS_DELAY_MANY_PEERS{300}; // 5min
59-
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
64+
static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS{11};
65+
static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS{5};
66+
static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" peers
6067

6168
// We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
6269
#define FEELER_SLEEP_WINDOW 1
@@ -1595,6 +1602,8 @@ void CConnman::ThreadDNSAddressSeed()
15951602
seeds_right_now = seeds.size();
15961603
} else if (addrman.size() == 0) {
15971604
// If we have no known peers, query all.
1605+
// This will occur on the first run, or if peers.dat has been
1606+
// deleted.
15981607
seeds_right_now = seeds.size();
15991608
}
16001609

@@ -1620,6 +1629,9 @@ void CConnman::ThreadDNSAddressSeed()
16201629
LogPrintf("Waiting %d seconds before querying DNS seeds.\n", seeds_wait_time.count());
16211630
std::chrono::seconds to_wait = seeds_wait_time;
16221631
while (to_wait.count() > 0) {
1632+
// if sleeping for the MANY_PEERS interval, wake up
1633+
// early to see if we have enough peers and can stop
1634+
// this thread entirely freeing up its resources
16231635
std::chrono::seconds w = std::min(DNSSEEDS_DELAY_FEW_PEERS, to_wait);
16241636
if (!interruptNet.sleep_for(w)) return;
16251637
to_wait -= w;
@@ -1646,7 +1658,7 @@ void CConnman::ThreadDNSAddressSeed()
16461658

16471659
if (interruptNet) return;
16481660

1649-
// hold off on querying seeds if p2p network deactivated
1661+
// hold off on querying seeds if P2P network deactivated
16501662
if (!fNetworkActive) {
16511663
LogPrintf("Waiting for network to be reactivated before querying DNS seeds.\n");
16521664
do {
@@ -1797,6 +1809,9 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17971809
return;
17981810

17991811
// Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1812+
// Note that we only do this if we started with an empty peers.dat,
1813+
// (in which case we will query DNS seeds immediately) *and* the DNS
1814+
// seeds have not returned any results.
18001815
if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
18011816
static bool done = false;
18021817
if (!done) {

0 commit comments

Comments
 (0)