Changeset View
Changeset View
Standalone View
Standalone View
src/net.cpp
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | |||||
#include <algorithm> | #include <algorithm> | ||||
#include <cmath> | #include <cmath> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <limits> | #include <limits> | ||||
#include <optional> | #include <optional> | ||||
#include <unordered_map> | #include <unordered_map> | ||||
/** Maximum number of block-relay-only anchor connections */ | |||||
static constexpr size_t MAX_BLOCK_RELAY_ONLY_ANCHORS = 2; | |||||
static_assert(MAX_BLOCK_RELAY_ONLY_ANCHORS <= | |||||
static_cast<size_t>(MAX_BLOCK_RELAY_ONLY_CONNECTIONS), | |||||
"MAX_BLOCK_RELAY_ONLY_ANCHORS must not exceed " | |||||
"MAX_BLOCK_RELAY_ONLY_CONNECTIONS."); | |||||
/** Anchor IP address database file name */ | |||||
const char *const ANCHORS_DATABASE_FILENAME = "anchors.dat"; | |||||
// How often to dump addresses to peers.dat | // How often to dump addresses to peers.dat | ||||
static constexpr std::chrono::minutes DUMP_PEERS_INTERVAL{15}; | static constexpr std::chrono::minutes DUMP_PEERS_INTERVAL{15}; | ||||
/** | /** | ||||
* Number of DNS seeds to query when the number of connections is low. | * Number of DNS seeds to query when the number of connections is low. | ||||
*/ | */ | ||||
static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3; | static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3; | ||||
▲ Show 20 Lines • Show All 2,655 Lines • ▼ Show 20 Lines | int64_t nStart = GetTimeMillis(); | ||||
} else { | } else { | ||||
// Addrman can be in an inconsistent state after failure, reset it | // Addrman can be in an inconsistent state after failure, reset it | ||||
addrman.Clear(); | addrman.Clear(); | ||||
LogPrintf("Invalid or missing peers.dat; recreating\n"); | LogPrintf("Invalid or missing peers.dat; recreating\n"); | ||||
DumpAddresses(); | DumpAddresses(); | ||||
} | } | ||||
} | } | ||||
if (m_use_addrman_outgoing) { | |||||
// Load addresses from anchors.dat | |||||
m_anchors = ReadAnchors(config->GetChainParams(), | |||||
GetDataDir() / ANCHORS_DATABASE_FILENAME); | |||||
if (m_anchors.size() > MAX_BLOCK_RELAY_ONLY_ANCHORS) { | |||||
m_anchors.resize(MAX_BLOCK_RELAY_ONLY_ANCHORS); | |||||
} | |||||
LogPrintf( | |||||
"%i block-relay-only anchors will be tried for connections.\n", | |||||
m_anchors.size()); | |||||
} | |||||
uiInterface.InitMessage(_("Starting network threads...").translated); | uiInterface.InitMessage(_("Starting network threads...").translated); | ||||
fAddressesInitialized = true; | fAddressesInitialized = true; | ||||
if (semOutbound == nullptr) { | if (semOutbound == nullptr) { | ||||
// initialize semaphore | // initialize semaphore | ||||
semOutbound = std::make_unique<CSemaphore>( | semOutbound = std::make_unique<CSemaphore>( | ||||
std::min(m_max_outbound, nMaxConnections)); | std::min(m_max_outbound, nMaxConnections)); | ||||
▲ Show 20 Lines • Show All 125 Lines • ▼ Show 20 Lines | if (threadSocketHandler.joinable()) { | ||||
threadSocketHandler.join(); | threadSocketHandler.join(); | ||||
} | } | ||||
} | } | ||||
void CConnman::StopNodes() { | void CConnman::StopNodes() { | ||||
if (fAddressesInitialized) { | if (fAddressesInitialized) { | ||||
DumpAddresses(); | DumpAddresses(); | ||||
fAddressesInitialized = false; | fAddressesInitialized = false; | ||||
if (m_use_addrman_outgoing) { | |||||
// Anchor connections are only dumped during clean shutdown. | |||||
std::vector<CAddress> anchors_to_dump = | |||||
GetCurrentBlockRelayOnlyConns(); | |||||
if (anchors_to_dump.size() > MAX_BLOCK_RELAY_ONLY_ANCHORS) { | |||||
anchors_to_dump.resize(MAX_BLOCK_RELAY_ONLY_ANCHORS); | |||||
} | |||||
DumpAnchors(config->GetChainParams(), | |||||
GetDataDir() / ANCHORS_DATABASE_FILENAME, | |||||
anchors_to_dump); | |||||
} | |||||
} | } | ||||
// Close sockets | // Close sockets | ||||
LOCK(cs_vNodes); | LOCK(cs_vNodes); | ||||
for (CNode *pnode : vNodes) { | for (CNode *pnode : vNodes) { | ||||
pnode->CloseSocketDisconnect(); | pnode->CloseSocketDisconnect(); | ||||
} | } | ||||
for (ListenSocket &hListenSocket : vhListenSocket) { | for (ListenSocket &hListenSocket : vhListenSocket) { | ||||
▲ Show 20 Lines • Show All 535 Lines • Show Last 20 Lines |