From 5316ae5dd8d90623f9bb883bb253fa6463ee4d34 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 13 Jul 2023 12:38:06 -0600 Subject: [PATCH] Convert GetLocal() to std::optional and remove out-param Co-authored-by: stickies-v --- src/net.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index a230b64a74d..a12c7a2e1cb 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -145,11 +145,12 @@ uint16_t GetListenPort() return static_cast(gArgs.GetIntArg("-port", Params().GetDefaultPort())); } -// find 'best' local address for a particular peer -[[nodiscard]] static bool GetLocal(CService& addr, const CNode& peer) +// Determine the "best" local address for a particular peer. +[[nodiscard]] static std::optional GetLocal(const CNode& peer) { - if (!fListen) return false; + if (!fListen) return std::nullopt; + std::optional addr; int nBestScore = -1; int nBestReachability = -1; { @@ -165,13 +166,13 @@ uint16_t GetListenPort() const int nScore{local_service_info.nScore}; const int nReachability{local_addr.GetReachabilityFrom(peer.addr)}; if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore)) { - addr = CService{local_addr, local_service_info.nPort}; + addr.emplace(CService{local_addr, local_service_info.nPort}); nBestReachability = nReachability; nBestScore = nScore; } } } - return nBestScore >= 0; + return addr; } //! Convert the serialized seeds into usable address objects. @@ -196,17 +197,13 @@ static std::vector ConvertSeeds(const std::vector &vSeedsIn) return vSeedsOut; } -// get best local address for a particular peer as a CAddress -// Otherwise, return the unroutable 0.0.0.0 but filled in with +// Determine the "best" local address for a particular peer. +// If none, return the unroutable 0.0.0.0 but filled in with // the normal parameters, since the IP may be changed to a useful // one by discovery. CService GetLocalAddress(const CNode& peer) { - CService addr; - if (GetLocal(addr, peer)) { - return addr; - } - return CService{CNetAddr(), GetListenPort()}; + return GetLocal(peer).value_or(CService{CNetAddr(), GetListenPort()}); } static int GetnScore(const CService& addr)