Skip to content

Commit 5316ae5

Browse files
jonatackstickies-v
andcommitted
Convert GetLocal() to std::optional and remove out-param
Co-authored-by: stickies-v <stickies-v@protonmail.com>
1 parent f1304db commit 5316ae5

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/net.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@ uint16_t GetListenPort()
145145
return static_cast<uint16_t>(gArgs.GetIntArg("-port", Params().GetDefaultPort()));
146146
}
147147

148-
// find 'best' local address for a particular peer
149-
[[nodiscard]] static bool GetLocal(CService& addr, const CNode& peer)
148+
// Determine the "best" local address for a particular peer.
149+
[[nodiscard]] static std::optional<CService> GetLocal(const CNode& peer)
150150
{
151-
if (!fListen) return false;
151+
if (!fListen) return std::nullopt;
152152

153+
std::optional<CService> addr;
153154
int nBestScore = -1;
154155
int nBestReachability = -1;
155156
{
@@ -165,13 +166,13 @@ uint16_t GetListenPort()
165166
const int nScore{local_service_info.nScore};
166167
const int nReachability{local_addr.GetReachabilityFrom(peer.addr)};
167168
if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore)) {
168-
addr = CService{local_addr, local_service_info.nPort};
169+
addr.emplace(CService{local_addr, local_service_info.nPort});
169170
nBestReachability = nReachability;
170171
nBestScore = nScore;
171172
}
172173
}
173174
}
174-
return nBestScore >= 0;
175+
return addr;
175176
}
176177

177178
//! Convert the serialized seeds into usable address objects.
@@ -196,17 +197,13 @@ static std::vector<CAddress> ConvertSeeds(const std::vector<uint8_t> &vSeedsIn)
196197
return vSeedsOut;
197198
}
198199

199-
// get best local address for a particular peer as a CAddress
200-
// Otherwise, return the unroutable 0.0.0.0 but filled in with
200+
// Determine the "best" local address for a particular peer.
201+
// If none, return the unroutable 0.0.0.0 but filled in with
201202
// the normal parameters, since the IP may be changed to a useful
202203
// one by discovery.
203204
CService GetLocalAddress(const CNode& peer)
204205
{
205-
CService addr;
206-
if (GetLocal(addr, peer)) {
207-
return addr;
208-
}
209-
return CService{CNetAddr(), GetListenPort()};
206+
return GetLocal(peer).value_or(CService{CNetAddr(), GetListenPort()});
210207
}
211208

212209
static int GetnScore(const CService& addr)

0 commit comments

Comments
 (0)