Gives seednode priority over dnsseed if both are provided

pull/28016/head
Sergi Delgado Segura 1 year ago
parent 3310a965bd
commit 3120a4678a

@ -2172,7 +2172,30 @@ void CConnman::ThreadDNSAddressSeed()
std::vector<std::string> seeds = m_params.DNSSeeds();
Shuffle(seeds.begin(), seeds.end(), rng);
int seeds_right_now = 0; // Number of seeds left before testing if we have enough connections
int found = 0;
int target_outbound_connections = 2;
int outbound_connection_count = 0;
auto start = NodeClock::now();
if (gArgs.IsArgSet("-seednode")) {
LogPrintf("-seednode enabled. Trying the provided seeds before defaulting to the dnsseeds.\n");
while (!interruptNet) {
if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
return;
// Abort if we have spent enough time without reaching our target.
// Giving seed nodes 30 seconds so this does not become a race against fixedseeds (which triggers after 1 min)
if (NodeClock::now() > start + 30s) {
LogPrintf("Couldn't connect to enough peers via seed nodes. Handing fetch logic to the DNS seeds.\n");
break;
}
outbound_connection_count = GetFullOutboundConnCount();
if (outbound_connection_count >= target_outbound_connections) {
LogPrintf("P2P peers available. Finished fetching data from seed nodes.\n");
break;
}
}
}
if (gArgs.GetBoolArg("-forcednsseed", DEFAULT_FORCEDNSSEED)) {
// When -forcednsseed is provided, query all.
@ -2184,6 +2207,8 @@ void CConnman::ThreadDNSAddressSeed()
seeds_right_now = seeds.size();
}
// Proceed with dnsseeds if seednodes hasn't reached the target or if forcednsseed is set
if (outbound_connection_count < target_outbound_connections || seeds_right_now) {
// goal: only query DNS seed if address need is acute
// * If we have a reasonable number of peers in addrman, spend
// some time trying them first. This improves user privacy by
@ -2196,6 +2221,7 @@ void CConnman::ThreadDNSAddressSeed()
// * If we continue having problems, eventually query all the
// DNS seeds, and if that fails too, also try the fixed seeds.
// (done in ThreadOpenConnections)
int found = 0;
const std::chrono::seconds seeds_wait_time = (addrman.Size() >= DNSSEEDS_DELAY_PEER_THRESHOLD ? DNSSEEDS_DELAY_MANY_PEERS : DNSSEEDS_DELAY_FEW_PEERS);
for (const std::string& seed : seeds) {
@ -2213,14 +2239,7 @@ void CConnman::ThreadDNSAddressSeed()
if (!interruptNet.sleep_for(w)) return;
to_wait -= w;
int nRelevant = 0;
{
LOCK(m_nodes_mutex);
for (const CNode* pnode : m_nodes) {
if (pnode->fSuccessfullyConnected && pnode->IsFullOutboundConn()) ++nRelevant;
}
}
if (nRelevant >= 2) {
if (GetFullOutboundConnCount() >= target_outbound_connections) {
if (found > 0) {
LogPrintf("%d addresses found from DNS seeds\n", found);
LogPrintf("P2P peers available. Finished DNS seeding.\n");
@ -2280,6 +2299,9 @@ void CConnman::ThreadDNSAddressSeed()
--seeds_right_now;
}
LogPrintf("%d addresses found from DNS seeds\n", found);
} else {
LogPrintf("Skipping DNS seeds. Enough peers have been found\n");
}
}
void CConnman::DumpAddresses()
@ -2330,6 +2352,19 @@ void CConnman::StartExtraBlockRelayPeers()
m_start_extra_block_relay_peers = true;
}
// Return the number of outbound connections that are full relay (not blocks only)
int CConnman::GetFullOutboundConnCount() const
{
int nRelevant = 0;
{
LOCK(m_nodes_mutex);
for (const CNode* pnode : m_nodes) {
if (pnode->fSuccessfullyConnected && pnode->IsFullOutboundConn()) ++nRelevant;
}
}
return nRelevant;
}
// Return the number of peers we have over our outbound connection limit
// Exclude peers that are marked for disconnect, or are going to be
// disconnected soon (eg ADDR_FETCH and FEELER)

@ -1175,6 +1175,8 @@ public:
void StartExtraBlockRelayPeers();
// Count the number of full-relay peer we have.
int GetFullOutboundConnCount() const;
// Return the number of outbound peers we have in excess of our target (eg,
// if we previously called SetTryNewOutboundPeer(true), and have since set
// to false, we may have extra peers that we wish to disconnect). This may

Loading…
Cancel
Save