Skip to content

Commit 4170b46

Browse files
committed
p2p: Integrate DumpAnchors() and ReadAnchors() into CConnman
1 parent bad16af commit 4170b46

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/net.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed"
4646

4747
#include <math.h>
4848

49+
/** Maximum number of block-relay-only anchor connections */
50+
static constexpr size_t MAX_BLOCK_RELAY_ONLY_ANCHORS = 2;
51+
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.");
52+
/** Anchor IP address database file name */
53+
const char* const ANCHORS_DATABASE_FILENAME = "anchors.dat";
54+
4955
// How often to dump addresses to peers.dat
5056
static constexpr std::chrono::minutes DUMP_PEERS_INTERVAL{15};
5157

@@ -2431,6 +2437,15 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
24312437
}
24322438
}
24332439

2440+
if (m_use_addrman_outgoing) {
2441+
// Load addresses from anchors.dat
2442+
m_anchors = ReadAnchors(GetDataDir() / ANCHORS_DATABASE_FILENAME);
2443+
if (m_anchors.size() > MAX_BLOCK_RELAY_ONLY_ANCHORS) {
2444+
m_anchors.resize(MAX_BLOCK_RELAY_ONLY_ANCHORS);
2445+
}
2446+
LogPrintf("%i block-relay-only anchors will be tried for connections.\n", m_anchors.size());
2447+
}
2448+
24342449
uiInterface.InitMessage(_("Starting network threads...").translated);
24352450

24362451
fAddressesInitialized = true;
@@ -2546,6 +2561,15 @@ void CConnman::StopNodes()
25462561
if (fAddressesInitialized) {
25472562
DumpAddresses();
25482563
fAddressesInitialized = false;
2564+
2565+
if (m_use_addrman_outgoing) {
2566+
// Anchor connections are only dumped during clean shutdown.
2567+
std::vector<CAddress> anchors_to_dump = GetCurrentBlockRelayOnlyConns();
2568+
if (anchors_to_dump.size() > MAX_BLOCK_RELAY_ONLY_ANCHORS) {
2569+
anchors_to_dump.resize(MAX_BLOCK_RELAY_ONLY_ANCHORS);
2570+
}
2571+
DumpAnchors(GetDataDir() / ANCHORS_DATABASE_FILENAME, anchors_to_dump);
2572+
}
25492573
}
25502574

25512575
// Close sockets

src/net.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,12 @@ class CConnman
566566
/** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
567567
BanMan* m_banman;
568568

569+
/**
570+
* Addresses that were saved during the previous clean shutdown. We'll
571+
* attempt to make block-relay-only connections to them.
572+
*/
573+
std::vector<CAddress> m_anchors;
574+
569575
/** SipHasher seeds for deterministic randomness */
570576
const uint64_t nSeed0, nSeed1;
571577

0 commit comments

Comments
 (0)