Skip to content

Commit 3e68efa

Browse files
committed
[net] Move checks from GetLocalAddrForPeer to caller
GetLocalAddrForPeer() is only called in one place. The checks inside that function make more sense to be carried out be the caller: - fSuccessfullyConnected is already checked at the top of SendMessages(), so must be true when we call GetLocalAddrForPeer() - fListen can go into the conditional before GetLocalAddrForPeer() is called.
1 parent d21d2b2 commit 3e68efa

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

src/net.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -203,27 +203,24 @@ bool IsPeerAddrLocalGood(CNode *pnode)
203203

204204
Optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
205205
{
206-
if (fListen && pnode->fSuccessfullyConnected)
206+
CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
207+
if (gArgs.GetBoolArg("-addrmantest", false)) {
208+
// use IPv4 loopback during addrmantest
209+
addrLocal = CAddress(CService(LookupNumeric("127.0.0.1", GetListenPort())), pnode->GetLocalServices());
210+
}
211+
// If discovery is enabled, sometimes give our peer the address it
212+
// tells us that it sees us as in case it has a better idea of our
213+
// address than we do.
214+
FastRandomContext rng;
215+
if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
216+
rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0))
207217
{
208-
CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
209-
if (gArgs.GetBoolArg("-addrmantest", false)) {
210-
// use IPv4 loopback during addrmantest
211-
addrLocal = CAddress(CService(LookupNumeric("127.0.0.1", GetListenPort())), pnode->GetLocalServices());
212-
}
213-
// If discovery is enabled, sometimes give our peer the address it
214-
// tells us that it sees us as in case it has a better idea of our
215-
// address than we do.
216-
FastRandomContext rng;
217-
if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
218-
rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0))
219-
{
220-
addrLocal.SetIP(pnode->GetAddrLocal());
221-
}
222-
if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false))
223-
{
224-
LogPrint(BCLog::NET, "Advertising address %s to peer=%d\n", addrLocal.ToString(), pnode->GetId());
225-
return addrLocal;
226-
}
218+
addrLocal.SetIP(pnode->GetAddrLocal());
219+
}
220+
if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false))
221+
{
222+
LogPrint(BCLog::NET, "Advertising address %s to peer=%d\n", addrLocal.ToString(), pnode->GetId());
223+
return addrLocal;
227224
}
228225
// Address is unroutable. Don't advertise.
229226
return nullopt;

src/net_processing.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4416,7 +4416,9 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
44164416
// Address refresh broadcast
44174417
auto current_time = GetTime<std::chrono::microseconds>();
44184418

4419-
if (pto->RelayAddrsWithConn() && !::ChainstateActive().IsInitialBlockDownload() && pto->m_next_local_addr_send < current_time) {
4419+
if (fListen && pto->RelayAddrsWithConn() &&
4420+
!::ChainstateActive().IsInitialBlockDownload() &&
4421+
pto->m_next_local_addr_send < current_time) {
44204422
// If we've sent before, clear the bloom filter for the peer, so that our
44214423
// self-announcement will actually go out.
44224424
// This might be unnecessary if the bloom filter has already rolled

0 commit comments

Comments
 (0)