Skip to content

Commit 021a04a

Browse files
committed
net_processing: Move some static functions to PeerManager
- BlockRequestAllowed - AlreadyHaveBlock - ProcessGetBlockData - PrepareBlockFilterRequest - ProcessGetCFilters - ProcessGetCFHeaders - ProcessGetCFCheckPt Moved out of anonymous namespace: - ProcessBlockAvailability - UpdateBlockAvailability - CanDirectFetch
1 parent 91c5b68 commit 021a04a

File tree

1 file changed

+61
-44
lines changed

1 file changed

+61
-44
lines changed

src/net_processing.cpp

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,24 @@ class PeerManagerImpl final : public PeerManager
472472
std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(g_cs_orphans);
473473
/** Offset into vExtraTxnForCompact to insert the next tx */
474474
size_t vExtraTxnForCompactIt GUARDED_BY(g_cs_orphans) = 0;
475+
476+
void ProcessBlockAvailability(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
477+
void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
478+
bool CanDirectFetch(const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
479+
bool BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
480+
bool AlreadyHaveBlock(const uint256& block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
481+
void ProcessGetBlockData(CNode& pfrom, Peer& peer, const CChainParams& chainparams, const CInv& inv, CConnman& connman);
482+
bool PrepareBlockFilterRequest(CNode& peer, const CChainParams& chain_params,
483+
BlockFilterType filter_type, uint32_t start_height,
484+
const uint256& stop_hash, uint32_t max_height_diff,
485+
const CBlockIndex*& stop_index,
486+
BlockFilterIndex*& filter_index);
487+
void ProcessGetCFilters(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
488+
CConnman& connman);
489+
void ProcessGetCFHeaders(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
490+
CConnman& connman);
491+
void ProcessGetCFCheckPt(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
492+
CConnman& connman);
475493
};
476494
} // namespace
477495

@@ -684,41 +702,6 @@ bool PeerManagerImpl::MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, co
684702
return true;
685703
}
686704

687-
/** Check whether the last unknown block a peer advertised is not yet known. */
688-
static void ProcessBlockAvailability(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
689-
CNodeState *state = State(nodeid);
690-
assert(state != nullptr);
691-
692-
if (!state->hashLastUnknownBlock.IsNull()) {
693-
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(state->hashLastUnknownBlock);
694-
if (pindex && pindex->nChainWork > 0) {
695-
if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
696-
state->pindexBestKnownBlock = pindex;
697-
}
698-
state->hashLastUnknownBlock.SetNull();
699-
}
700-
}
701-
}
702-
703-
/** Update tracking information about which blocks a peer is assumed to have. */
704-
static void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
705-
CNodeState *state = State(nodeid);
706-
assert(state != nullptr);
707-
708-
ProcessBlockAvailability(nodeid);
709-
710-
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
711-
if (pindex && pindex->nChainWork > 0) {
712-
// An actually better block was announced.
713-
if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
714-
state->pindexBestKnownBlock = pindex;
715-
}
716-
} else {
717-
// An unknown block was announced; just assume that the latest one is the best one.
718-
state->hashLastUnknownBlock = hash;
719-
}
720-
}
721-
722705
void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid)
723706
{
724707
AssertLockHeld(cs_main);
@@ -768,7 +751,7 @@ bool PeerManagerImpl::TipMayBeStale()
768751
return m_last_tip_update < GetTime() - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty();
769752
}
770753

771-
static bool CanDirectFetch(const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
754+
bool PeerManagerImpl::CanDirectFetch(const Consensus::Params &consensusParams)
772755
{
773756
return ::ChainActive().Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams.nPowTargetSpacing * 20;
774757
}
@@ -782,6 +765,41 @@ static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIV
782765
return false;
783766
}
784767

768+
/** Check whether the last unknown block a peer advertised is not yet known. */
769+
void PeerManagerImpl::ProcessBlockAvailability(NodeId nodeid) {
770+
CNodeState *state = State(nodeid);
771+
assert(state != nullptr);
772+
773+
if (!state->hashLastUnknownBlock.IsNull()) {
774+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(state->hashLastUnknownBlock);
775+
if (pindex && pindex->nChainWork > 0) {
776+
if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
777+
state->pindexBestKnownBlock = pindex;
778+
}
779+
state->hashLastUnknownBlock.SetNull();
780+
}
781+
}
782+
}
783+
784+
/** Update tracking information about which blocks a peer is assumed to have. */
785+
void PeerManagerImpl::UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
786+
CNodeState *state = State(nodeid);
787+
assert(state != nullptr);
788+
789+
ProcessBlockAvailability(nodeid);
790+
791+
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
792+
if (pindex && pindex->nChainWork > 0) {
793+
// An actually better block was announced.
794+
if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
795+
state->pindexBestKnownBlock = pindex;
796+
}
797+
} else {
798+
// An unknown block was announced; just assume that the latest one is the best one.
799+
state->hashLastUnknownBlock = hash;
800+
}
801+
}
802+
785803
void PeerManagerImpl::FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller)
786804
{
787805
if (count == 0)
@@ -1200,7 +1218,7 @@ bool PeerManagerImpl::MaybePunishNodeForTx(NodeId nodeid, const TxValidationStat
12001218
// active chain if they are no more than a month older (both in time, and in
12011219
// best equivalent proof of work) than the best header chain we know about and
12021220
// we fully-validated them at some point.
1203-
static bool BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1221+
bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Params& consensusParams)
12041222
{
12051223
AssertLockHeld(cs_main);
12061224
if (::ChainActive().Contains(pindex)) return true;
@@ -1454,7 +1472,7 @@ bool PeerManagerImpl::AlreadyHaveTx(const GenTxid& gtxid)
14541472
return recentRejects->contains(hash) || m_mempool.exists(gtxid);
14551473
}
14561474

1457-
bool static AlreadyHaveBlock(const uint256& block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1475+
bool PeerManagerImpl::AlreadyHaveBlock(const uint256& block_hash)
14581476
{
14591477
return g_chainman.m_blockman.LookupBlockIndex(block_hash) != nullptr;
14601478
}
@@ -1535,7 +1553,7 @@ static void RelayAddress(const CNode& originator,
15351553
connman.ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));
15361554
}
15371555

1538-
void static ProcessGetBlockData(CNode& pfrom, Peer& peer, const CChainParams& chainparams, const CInv& inv, CConnman& connman)
1556+
void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CChainParams& chainparams, const CInv& inv, CConnman& connman)
15391557
{
15401558
bool send = false;
15411559
std::shared_ptr<const CBlock> a_recent_block;
@@ -2118,7 +2136,7 @@ void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
21182136
* @param[out] filter_index The filter index, if the request can be serviced.
21192137
* @return True if the request can be serviced.
21202138
*/
2121-
static bool PrepareBlockFilterRequest(CNode& peer, const CChainParams& chain_params,
2139+
bool PeerManagerImpl::PrepareBlockFilterRequest(CNode& peer, const CChainParams& chain_params,
21222140
BlockFilterType filter_type, uint32_t start_height,
21232141
const uint256& stop_hash, uint32_t max_height_diff,
21242142
const CBlockIndex*& stop_index,
@@ -2181,7 +2199,7 @@ static bool PrepareBlockFilterRequest(CNode& peer, const CChainParams& chain_par
21812199
* @param[in] chain_params Chain parameters
21822200
* @param[in] connman Pointer to the connection manager
21832201
*/
2184-
static void ProcessGetCFilters(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
2202+
void PeerManagerImpl::ProcessGetCFilters(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
21852203
CConnman& connman)
21862204
{
21872205
uint8_t filter_type_ser;
@@ -2223,7 +2241,7 @@ static void ProcessGetCFilters(CNode& peer, CDataStream& vRecv, const CChainPara
22232241
* @param[in] chain_params Chain parameters
22242242
* @param[in] connman Pointer to the connection manager
22252243
*/
2226-
static void ProcessGetCFHeaders(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
2244+
void PeerManagerImpl::ProcessGetCFHeaders(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
22272245
CConnman& connman)
22282246
{
22292247
uint8_t filter_type_ser;
@@ -2278,7 +2296,7 @@ static void ProcessGetCFHeaders(CNode& peer, CDataStream& vRecv, const CChainPar
22782296
* @param[in] chain_params Chain parameters
22792297
* @param[in] connman Pointer to the connection manager
22802298
*/
2281-
static void ProcessGetCFCheckPt(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
2299+
void PeerManagerImpl::ProcessGetCFCheckPt(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
22822300
CConnman& connman)
22832301
{
22842302
uint8_t filter_type_ser;
@@ -4747,4 +4765,3 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
47474765
} // release cs_main
47484766
return true;
47494767
}
4750-

0 commit comments

Comments
 (0)