[net processing] Extract addr send functionality into MaybeSendAddr()

Reviewer hint: review with

 `git diff --color-moved=dimmed-zebra --ignore-all-space`
This commit is contained in:
John Newbery 2020-07-09 10:51:20 +01:00
parent 4ad4abcf07
commit ad719297f2

View File

@ -322,6 +322,9 @@ private:
* to time out. */
void MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now);
/** Send `addr` messages on a regular schedule. */
void MaybeSendAddr(CNode* pto, std::chrono::microseconds current_time);
const CChainParams& m_chainparams;
CConnman& m_connman;
/** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
@ -4138,59 +4141,10 @@ void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::mic
}
}
namespace {
class CompareInvMempoolOrder
{
CTxMemPool *mp;
bool m_wtxid_relay;
public:
explicit CompareInvMempoolOrder(CTxMemPool *_mempool, bool use_wtxid)
{
mp = _mempool;
m_wtxid_relay = use_wtxid;
}
bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
{
/* As std::make_heap produces a max-heap, we want the entries with the
* fewest ancestors/highest fee to sort later. */
return mp->CompareDepthAndScore(*b, *a, m_wtxid_relay);
}
};
}
bool PeerManagerImpl::SendMessages(CNode* pto)
{
PeerRef peer = GetPeerRef(pto->GetId());
if (!peer) return false;
const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
// We must call MaybeDiscourageAndDisconnect first, to ensure that we'll
// disconnect misbehaving peers even before the version handshake is complete.
if (MaybeDiscourageAndDisconnect(*pto, *peer)) return true;
// Don't send anything until the version handshake is complete
if (!pto->fSuccessfullyConnected || pto->fDisconnect)
return true;
// If we get here, the outgoing message serialization version is set and can't change.
const CNetMsgMaker msgMaker(pto->GetCommonVersion());
const auto current_time = GetTime<std::chrono::microseconds>();
MaybeSendPing(*pto, *peer, current_time);
// MaybeSendPing may have marked peer for disconnection
if (pto->fDisconnect) return true;
{
LOCK(cs_main);
CNodeState &state = *State(pto->GetId());
// Address refresh broadcast
void PeerManagerImpl::MaybeSendAddr(CNode* pto, std::chrono::microseconds current_time)
{
LOCK(pto->m_addr_send_times_mutex);
const CNetMsgMaker msgMaker(pto->GetCommonVersion());
if (fListen && pto->RelayAddrsWithConn() &&
!m_chainman.ActiveChainstate().IsInitialBlockDownload() &&
@ -4251,7 +4205,59 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
if (pto->vAddrToSend.capacity() > 40)
pto->vAddrToSend.shrink_to_fit();
}
} // pto->m_addr_send_times_mutex
}
namespace {
class CompareInvMempoolOrder
{
CTxMemPool *mp;
bool m_wtxid_relay;
public:
explicit CompareInvMempoolOrder(CTxMemPool *_mempool, bool use_wtxid)
{
mp = _mempool;
m_wtxid_relay = use_wtxid;
}
bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
{
/* As std::make_heap produces a max-heap, we want the entries with the
* fewest ancestors/highest fee to sort later. */
return mp->CompareDepthAndScore(*b, *a, m_wtxid_relay);
}
};
}
bool PeerManagerImpl::SendMessages(CNode* pto)
{
PeerRef peer = GetPeerRef(pto->GetId());
if (!peer) return false;
const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
// We must call MaybeDiscourageAndDisconnect first, to ensure that we'll
// disconnect misbehaving peers even before the version handshake is complete.
if (MaybeDiscourageAndDisconnect(*pto, *peer)) return true;
// Don't send anything until the version handshake is complete
if (!pto->fSuccessfullyConnected || pto->fDisconnect)
return true;
// If we get here, the outgoing message serialization version is set and can't change.
const CNetMsgMaker msgMaker(pto->GetCommonVersion());
const auto current_time = GetTime<std::chrono::microseconds>();
MaybeSendPing(*pto, *peer, current_time);
// MaybeSendPing may have marked peer for disconnection
if (pto->fDisconnect) return true;
MaybeSendAddr(pto, current_time);
{
LOCK(cs_main);
CNodeState &state = *State(pto->GetId());
// Start block sync
if (pindexBestHeader == nullptr)