Skip to content

Commit 8fefedb

Browse files
Merge #7121: backport: Merge bitcoin#28136, 27581, 18919, 25634, 25284, 27829
b39ec50 Merge bitcoin#27829: rpc: fix data optionality for RPC calls. (Andrew Chow) 8e95d2a Merge bitcoin#28136: refactor: move GetServicesNames from rpc/util.{h,cpp} to rpc/net.cpp (Andrew Chow) 8eeff95 Merge bitcoin#27581: net: Continuous ASMap health check (Andrew Chow) 98e57c7 Merge bitcoin#18919: test: Add gettransaction test for "coin-join" tx (fanquake) Pull request description: Backports ACKs for top commit: UdjinM6: utACK b39ec50 Tree-SHA512: 4c83b871efaa16833cf7b862d5bbdb45292c4e20d1c1269c3664f93a7e619a3e1c734388b6d6c6d95de654eac69dfaa1c266ecddf968fa9a45b0af6647b9b87b
2 parents 5c2f6c6 + b39ec50 commit 8fefedb

20 files changed

Lines changed: 149 additions & 33 deletions

src/addrman.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ int AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const
808808
return -1;
809809
}
810810

811-
std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
811+
std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered) const
812812
{
813813
AssertLockHeld(cs);
814814

@@ -838,7 +838,7 @@ std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct
838838
if (network != std::nullopt && ai.GetNetClass() != network) continue;
839839

840840
// Filter for quality
841-
if (ai.IsTerrible(now)) continue;
841+
if (ai.IsTerrible(now) && filtered) continue;
842842

843843
addresses.push_back(ai);
844844
}
@@ -1209,11 +1209,11 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select(bool new_only, std::optiona
12091209
return addrRet;
12101210
}
12111211

1212-
std::vector<CAddress> AddrManImpl::GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
1212+
std::vector<CAddress> AddrManImpl::GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered) const
12131213
{
12141214
LOCK(cs);
12151215
Check();
1216-
auto addresses = GetAddr_(max_addresses, max_pct, network);
1216+
auto addresses = GetAddr_(max_addresses, max_pct, network, filtered);
12171217
Check();
12181218
return addresses;
12191219
}
@@ -1315,9 +1315,9 @@ std::pair<CAddress, NodeSeconds> AddrMan::Select(bool new_only, std::optional<Ne
13151315
return m_impl->Select(new_only, network);
13161316
}
13171317

1318-
std::vector<CAddress> AddrMan::GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
1318+
std::vector<CAddress> AddrMan::GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered) const
13191319
{
1320-
return m_impl->GetAddr(max_addresses, max_pct, network);
1320+
return m_impl->GetAddr(max_addresses, max_pct, network, filtered);
13211321
}
13221322

13231323
void AddrMan::Connected(const CService& addr, NodeSeconds time)

src/addrman.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,11 @@ class AddrMan
177177
* @param[in] max_addresses Maximum number of addresses to return (0 = all).
178178
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
179179
* @param[in] network Select only addresses of this network (nullopt = all).
180+
* @param[in] filtered Select only addresses that are considered good quality (false = all).
180181
*
181182
* @return A vector of randomly selected addresses from vRandom.
182183
*/
183-
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const;
184+
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const;
184185

185186
/** We have successfully connected to this peer. Calling this function
186187
* updates the CAddress's nTime, which is used in our IsTerrible()

src/addrman_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class AddrManImpl
130130
std::pair<CAddress, NodeSeconds> Select(bool new_only, std::optional<Network> network) const
131131
EXCLUSIVE_LOCKS_REQUIRED(!cs);
132132

133-
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
133+
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const
134134
EXCLUSIVE_LOCKS_REQUIRED(!cs);
135135

136136
void Connected(const CService& addr, NodeSeconds time)
@@ -262,7 +262,7 @@ class AddrManImpl
262262
* */
263263
int GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
264264

265-
std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network) const EXCLUSIVE_LOCKS_REQUIRED(cs);
265+
std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
266266

267267
void Connected_(const CService& addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
268268

src/httprpc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <crypto/hmac_sha256.h>
88
#include <httpserver.h>
9+
#include <netaddress.h>
910
#include <rpc/protocol.h>
1011
#include <rpc/server.h>
1112
#include <util/strencodings.h>

src/net.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4083,6 +4083,12 @@ bool CConnman::Start(CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_met
40834083
// Dump network addresses
40844084
scheduler.scheduleEvery([this] { DumpAddresses(); }, DUMP_PEERS_INTERVAL);
40854085

4086+
// Run the ASMap Health check once and then schedule it to run every 24h.
4087+
if (m_netgroupman.UsingASMap()) {
4088+
ASMapHealthCheck();
4089+
scheduler.scheduleEvery([this] { ASMapHealthCheck(); }, ASMAP_HEALTH_CHECK_INTERVAL);
4090+
}
4091+
40864092
return true;
40874093
}
40884094

@@ -4218,9 +4224,9 @@ CConnman::~CConnman()
42184224
Stop();
42194225
}
42204226

4221-
std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
4227+
std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered) const
42224228
{
4223-
std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct, network);
4229+
std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct, network, filtered);
42244230
if (m_banman) {
42254231
addresses.erase(std::remove_if(addresses.begin(), addresses.end(),
42264232
[this](const CAddress& addr){return m_banman->IsDiscouraged(addr) || m_banman->IsBanned(addr);}),
@@ -4945,6 +4951,19 @@ void CConnman::PerformReconnections()
49454951
}
49464952
}
49474953

4954+
void CConnman::ASMapHealthCheck()
4955+
{
4956+
const std::vector<CAddress> v4_addrs{GetAddresses(/*max_addresses=*/ 0, /*max_pct=*/ 0, Network::NET_IPV4, /*filtered=*/ false)};
4957+
const std::vector<CAddress> v6_addrs{GetAddresses(/*max_addresses=*/ 0, /*max_pct=*/ 0, Network::NET_IPV6, /*filtered=*/ false)};
4958+
std::vector<CNetAddr> clearnet_addrs;
4959+
clearnet_addrs.reserve(v4_addrs.size() + v6_addrs.size());
4960+
std::transform(v4_addrs.begin(), v4_addrs.end(), std::back_inserter(clearnet_addrs),
4961+
[](const CAddress& addr) { return static_cast<CNetAddr>(addr); });
4962+
std::transform(v6_addrs.begin(), v6_addrs.end(), std::back_inserter(clearnet_addrs),
4963+
[](const CAddress& addr) { return static_cast<CNetAddr>(addr); });
4964+
m_netgroupman.ASMapHealthCheck(clearnet_addrs);
4965+
}
4966+
49484967
// Dump binary message to file, with timestamp.
49494968
static void CaptureMessageToFile(const CAddress& addr,
49504969
const std::string& msg_type,

src/net.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ static const bool DEFAULT_BLOCKSONLY = false;
111111
static const int64_t DEFAULT_PEER_CONNECT_TIMEOUT = 60;
112112
/** Number of file descriptors required for message capture **/
113113
static const int NUM_FDS_MESSAGE_CAPTURE = 1;
114+
/** Interval for ASMap Health Check **/
115+
static constexpr std::chrono::hours ASMAP_HEALTH_CHECK_INTERVAL{24};
114116

115117
static constexpr bool DEFAULT_FORCEDNSSEED{false};
116118
static constexpr bool DEFAULT_DNSSEED{true};
@@ -1295,6 +1297,7 @@ friend class CNode;
12951297
void OpenMasternodeConnection(const CAddress& addrConnect, bool use_v2transport, MasternodeProbeConn probe = MasternodeProbeConn::IsConnection)
12961298
EXCLUSIVE_LOCKS_REQUIRED(!m_nodes_mutex, !m_unused_i2p_sessions_mutex, !mutexMsgProc, !cs_mapSocketToNode);
12971299
bool CheckIncomingNonce(uint64_t nonce) const EXCLUSIVE_LOCKS_REQUIRED(!m_nodes_mutex);
1300+
void ASMapHealthCheck();
12981301

12991302
// alias for thread safety annotations only, not defined
13001303
SharedMutex& GetNodesMutex() const LOCK_RETURNED(m_nodes_mutex);
@@ -1405,8 +1408,9 @@ friend class CNode;
14051408
* @param[in] max_addresses Maximum number of addresses to return (0 = all).
14061409
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
14071410
* @param[in] network Select only addresses of this network (nullopt = all).
1411+
* @param[in] filtered Select only addresses that are considered high quality (false = all).
14081412
*/
1409-
std::vector<CAddress> GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network) const;
1413+
std::vector<CAddress> GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const;
14101414

14111415
/**
14121416
* Cache is used to minimize topology leaks, so it should

src/netgroup.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <netgroup.h>
66

77
#include <hash.h>
8+
#include <logging.h>
89
#include <util/asmap.h>
910

1011
uint256 NetGroupManager::GetAsmapChecksum() const
@@ -109,3 +110,23 @@ uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const
109110
uint32_t mapped_as = Interpret(m_asmap, ip_bits);
110111
return mapped_as;
111112
}
113+
114+
void NetGroupManager::ASMapHealthCheck(const std::vector<CNetAddr>& clearnet_addrs) const {
115+
std::set<uint32_t> clearnet_asns{};
116+
int unmapped_count{0};
117+
118+
for (const auto& addr : clearnet_addrs) {
119+
uint32_t asn = GetMappedAS(addr);
120+
if (asn == 0) {
121+
++unmapped_count;
122+
continue;
123+
}
124+
clearnet_asns.insert(asn);
125+
}
126+
127+
LogPrintf("ASMap Health Check: %i clearnet peers are mapped to %i ASNs with %i peers being unmapped\n", clearnet_addrs.size(), clearnet_asns.size(), unmapped_count);
128+
}
129+
130+
bool NetGroupManager::UsingASMap() const {
131+
return m_asmap.size() > 0;
132+
}

src/netgroup.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ class NetGroupManager {
4141
*/
4242
uint32_t GetMappedAS(const CNetAddr& address) const;
4343

44+
/**
45+
* Analyze and log current health of ASMap based buckets.
46+
*/
47+
void ASMapHealthCheck(const std::vector<CNetAddr>& clearnet_addrs) const;
48+
49+
/**
50+
* Indicates whether ASMap is being used for clearnet bucketing.
51+
*/
52+
bool UsingASMap() const;
53+
4454
private:
4555
/** Compressed IP->ASN mapping, loaded from a file when a node starts.
4656
*

src/rest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <sync.h>
3030
#include <txmempool.h>
3131
#include <util/check.h>
32+
#include <util/strencodings.h>
3233
#include <validation.h>
3334
#include <version.h>
3435

src/rpc/mempool.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <util/moneystr.h>
2323
#include <validation.h>
2424
#include <util/system.h>
25+
#include <util/strencodings.h>
2526
#include <util/time.h>
2627

2728
using node::DEFAULT_MAX_RAW_TX_FEE_RATE;

0 commit comments

Comments
 (0)