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

Function GetLocal

src/net.cpp:166–193  ·  view source on GitHub ↗

Determine the "best" local address for a particular peer.

Source from the content-addressed store, hash-verified

164
165// Determine the "best" local address for a particular peer.
166[[nodiscard]] static std::optional<CService> GetLocal(const CNode& peer)
167{
168 if (!fListen) return std::nullopt;
169
170 std::optional<CService> addr;
171 int nBestScore = -1;
172 int nBestReachability = -1;
173 {
174 LOCK(g_maplocalhost_mutex);
175 for (const auto& [local_addr, local_service_info] : mapLocalHost) {
176 // For privacy reasons, don't advertise our privacy-network address
177 // to other networks and don't advertise our other-network address
178 // to privacy networks.
179 if (local_addr.GetNetwork() != peer.ConnectedThroughNetwork()
180 && (local_addr.IsPrivacyNet() || peer.IsConnectedThroughPrivacyNet())) {
181 continue;
182 }
183 const int nScore{local_service_info.nScore};
184 const int nReachability{local_addr.GetReachabilityFrom(peer.addr)};
185 if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore)) {
186 addr.emplace(CService{local_addr, local_service_info.nPort});
187 nBestReachability = nReachability;
188 nBestScore = nScore;
189 }
190 }
191 }
192 return addr;
193}
194
195//! Convert the serialized seeds into usable address objects.
196static std::vector<CAddress> ConvertSeeds(const std::vector<uint8_t> &vSeedsIn)

Callers 1

GetLocalAddressFunction · 0.85

Calls 5

GetNetworkMethod · 0.80
GetReachabilityFromMethod · 0.80
emplaceMethod · 0.80

Tested by

no test coverage detected