MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / ThreadDNSAddressSeed

Method ThreadDNSAddressSeed

src/net.cpp:2284–2422  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2282}
2283
2284void CConnman::ThreadDNSAddressSeed()
2285{
2286 int outbound_connection_count = 0;
2287
2288 if (!gArgs.GetArgs("-seednode").empty()) {
2289 auto start = NodeClock::now();
2290 constexpr std::chrono::seconds SEEDNODE_TIMEOUT = 30s;
2291 LogInfo("-seednode enabled. Trying the provided seeds for %d seconds before defaulting to the dnsseeds.\n", SEEDNODE_TIMEOUT.count());
2292 while (!m_interrupt_net->interrupted()) {
2293 if (!m_interrupt_net->sleep_for(500ms)) {
2294 return;
2295 }
2296
2297 // Abort if we have spent enough time without reaching our target.
2298 // Giving seed nodes 30 seconds so this does not become a race against fixedseeds (which triggers after 1 min)
2299 if (NodeClock::now() > start + SEEDNODE_TIMEOUT) {
2300 LogInfo("Couldn't connect to enough peers via seed nodes. Handing fetch logic to the DNS seeds.\n");
2301 break;
2302 }
2303
2304 outbound_connection_count = GetFullOutboundConnCount();
2305 if (outbound_connection_count >= SEED_OUTBOUND_CONNECTION_THRESHOLD) {
2306 LogInfo("P2P peers available. Finished fetching data from seed nodes.\n");
2307 break;
2308 }
2309 }
2310 }
2311
2312 FastRandomContext rng;
2313 std::vector<std::string> seeds = m_params.DNSSeeds();
2314 std::shuffle(seeds.begin(), seeds.end(), rng);
2315 int seeds_right_now = 0; // Number of seeds left before testing if we have enough connections
2316
2317 if (gArgs.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED)) {
2318 // When -forcednsseed is provided, query all.
2319 seeds_right_now = seeds.size();
2320 } else if (addrman.get().Size() == 0) {
2321 // If we have no known peers, query all.
2322 // This will occur on the first run, or if peers.dat has been
2323 // deleted.
2324 seeds_right_now = seeds.size();
2325 }
2326
2327 // Proceed with dnsseeds if seednodes hasn't reached the target or if forcednsseed is set
2328 if (outbound_connection_count < SEED_OUTBOUND_CONNECTION_THRESHOLD || seeds_right_now) {
2329 // goal: only query DNS seed if address need is acute
2330 // * If we have a reasonable number of peers in addrman, spend
2331 // some time trying them first. This improves user privacy by
2332 // creating fewer identifying DNS requests, reduces trust by
2333 // giving seeds less influence on the network topology, and
2334 // reduces traffic to the seeds.
2335 // * When querying DNS seeds query a few at once, this ensures
2336 // that we don't give DNS seeds the ability to eclipse nodes
2337 // that query them.
2338 // * If we continue having problems, eventually query all the
2339 // DNS seeds, and if that fails too, also try the fixed seeds.
2340 // (done in ThreadOpenConnections)
2341 int found = 0;

Callers

nothing calls this directly

Calls 15

HaveNameProxyFunction · 0.85
SeedsServiceFlagsFunction · 0.85
LookupHostFunction · 0.85
CServiceClass · 0.85
GetArgsMethod · 0.80
countMethod · 0.80
GetBoolArgMethod · 0.80
SetInternalMethod · 0.80
rand_uniform_delayMethod · 0.80
CAddressClass · 0.70
emptyMethod · 0.45
interruptedMethod · 0.45

Tested by

no test coverage detected