Skip to content

Commit a568b82

Browse files
committedJan 9, 2021
net_processing: split PeerManager into interface and implementation classes
1 parent 0df3d3f commit a568b82

File tree

6 files changed

+104
-72
lines changed

6 files changed

+104
-72
lines changed
 

‎src/init.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1410,8 +1410,8 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
14101410
ChainstateManager& chainman = *Assert(node.chainman);
14111411

14121412
assert(!node.peerman);
1413-
node.peerman = std::make_unique<PeerManager>(chainparams, *node.connman, node.banman.get(),
1414-
*node.scheduler, chainman, *node.mempool, ignores_incoming_txs);
1413+
node.peerman = PeerManager::make(chainparams, *node.connman, node.banman.get(),
1414+
*node.scheduler, chainman, *node.mempool, ignores_incoming_txs);
14151415
RegisterValidationInterface(node.peerman.get());
14161416

14171417
// sanitize comments per BIP-0014, format user agent and check total size

‎src/net_processing.cpp

+48-35
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ static void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vec
683683

684684
} // namespace
685685

686-
void PeerManager::PushNodeVersion(CNode& pnode, int64_t nTime)
686+
void PeerManagerImpl::PushNodeVersion(CNode& pnode, int64_t nTime)
687687
{
688688
// Note that pnode->GetLocalServices() is a reflection of the local
689689
// services we were offering when the CNode object was created for this
@@ -709,7 +709,7 @@ void PeerManager::PushNodeVersion(CNode& pnode, int64_t nTime)
709709
}
710710
}
711711

712-
void PeerManager::AddTxAnnouncement(const CNode& node, const GenTxid& gtxid, std::chrono::microseconds current_time)
712+
void PeerManagerImpl::AddTxAnnouncement(const CNode& node, const GenTxid& gtxid, std::chrono::microseconds current_time)
713713
{
714714
AssertLockHeld(::cs_main); // For m_txrequest
715715
NodeId nodeid = node.GetId();
@@ -745,7 +745,8 @@ void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
745745
if (state) state->m_last_block_announcement = time_in_seconds;
746746
}
747747

748-
void PeerManager::InitializeNode(CNode *pnode) {
748+
void PeerManagerImpl::InitializeNode(CNode *pnode)
749+
{
749750
CAddress addr = pnode->addr;
750751
std::string addrName = pnode->GetAddrName();
751752
NodeId nodeid = pnode->GetId();
@@ -764,7 +765,7 @@ void PeerManager::InitializeNode(CNode *pnode) {
764765
}
765766
}
766767

767-
void PeerManager::ReattemptInitialBroadcast(CScheduler& scheduler) const
768+
void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler) const
768769
{
769770
std::set<uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
770771

@@ -785,7 +786,8 @@ void PeerManager::ReattemptInitialBroadcast(CScheduler& scheduler) const
785786
scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
786787
}
787788

788-
void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
789+
void PeerManagerImpl::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime)
790+
{
789791
NodeId nodeid = node.GetId();
790792
fUpdateConnectionTime = false;
791793
LOCK(cs_main);
@@ -839,14 +841,14 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
839841
LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
840842
}
841843

842-
PeerRef PeerManager::GetPeerRef(NodeId id) const
844+
PeerRef PeerManagerImpl::GetPeerRef(NodeId id) const
843845
{
844846
LOCK(m_peer_mutex);
845847
auto it = m_peer_map.find(id);
846848
return it != m_peer_map.end() ? it->second : nullptr;
847849
}
848850

849-
PeerRef PeerManager::RemovePeer(NodeId id)
851+
PeerRef PeerManagerImpl::RemovePeer(NodeId id)
850852
{
851853
PeerRef ret;
852854
LOCK(m_peer_mutex);
@@ -858,7 +860,8 @@ PeerRef PeerManager::RemovePeer(NodeId id)
858860
return ret;
859861
}
860862

861-
bool PeerManager::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
863+
bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
864+
{
862865
{
863866
LOCK(cs_main);
864867
CNodeState* state = State(nodeid);
@@ -1015,7 +1018,7 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
10151018
return nEvicted;
10161019
}
10171020

1018-
void PeerManager::Misbehaving(const NodeId pnode, const int howmuch, const std::string& message)
1021+
void PeerManagerImpl::Misbehaving(const NodeId pnode, const int howmuch, const std::string& message)
10191022
{
10201023
assert(howmuch > 0);
10211024

@@ -1033,8 +1036,8 @@ void PeerManager::Misbehaving(const NodeId pnode, const int howmuch, const std::
10331036
}
10341037
}
10351038

1036-
bool PeerManager::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
1037-
bool via_compact_block, const std::string& message)
1039+
bool PeerManagerImpl::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
1040+
bool via_compact_block, const std::string& message)
10381041
{
10391042
switch (state.GetResult()) {
10401043
case BlockValidationResult::BLOCK_RESULT_UNSET:
@@ -1083,7 +1086,7 @@ bool PeerManager::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationSt
10831086
return false;
10841087
}
10851088

1086-
bool PeerManager::MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state, const std::string& message)
1089+
bool PeerManagerImpl::MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state, const std::string& message)
10871090
{
10881091
switch (state.GetResult()) {
10891092
case TxValidationResult::TX_RESULT_UNSET:
@@ -1129,9 +1132,16 @@ static bool BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Para
11291132
(GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, consensusParams) < STALE_RELAY_AGE_LIMIT);
11301133
}
11311134

1132-
PeerManager::PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
1133-
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
1134-
bool ignore_incoming_txs)
1135+
std::unique_ptr<PeerManager> PeerManager::make(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
1136+
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
1137+
bool ignore_incoming_txs)
1138+
{
1139+
return std::make_unique<PeerManagerImpl>(chainparams, connman, banman, scheduler, chainman, pool, ignore_incoming_txs);
1140+
}
1141+
1142+
PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
1143+
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
1144+
bool ignore_incoming_txs)
11351145
: m_chainparams(chainparams),
11361146
m_connman(connman),
11371147
m_banman(banman),
@@ -1171,7 +1181,7 @@ PeerManager::PeerManager(const CChainParams& chainparams, CConnman& connman, Ban
11711181
* block, remember the recently confirmed transactions, and delete tracked
11721182
* announcements for them. Also save the time of the last tip update.
11731183
*/
1174-
void PeerManager::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
1184+
void PeerManagerImpl::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
11751185
{
11761186
{
11771187
LOCK(g_cs_orphans);
@@ -1222,7 +1232,7 @@ void PeerManager::BlockConnected(const std::shared_ptr<const CBlock>& pblock, co
12221232
}
12231233
}
12241234

1225-
void PeerManager::BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex)
1235+
void PeerManagerImpl::BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex)
12261236
{
12271237
// To avoid relay problems with transactions that were previously
12281238
// confirmed, clear our filter of recently confirmed transactions whenever
@@ -1247,7 +1257,8 @@ static bool fWitnessesPresentInMostRecentCompactBlock GUARDED_BY(cs_most_recent_
12471257
* Maintain state about the best-seen block and fast-announce a compact block
12481258
* to compatible peers.
12491259
*/
1250-
void PeerManager::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) {
1260+
void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock)
1261+
{
12511262
std::shared_ptr<const CBlockHeaderAndShortTxIDs> pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs> (*pblock, true);
12521263
const CNetMsgMaker msgMaker(PROTOCOL_VERSION);
12531264

@@ -1294,9 +1305,9 @@ void PeerManager::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_
12941305
* Update our best height and announce any block hashes which weren't previously
12951306
* in ::ChainActive() to our peers.
12961307
*/
1297-
void PeerManager::UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload)
1308+
void PeerManagerImpl::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
12981309
{
1299-
m_best_height = pindexNew->nHeight;
1310+
SetBestHeight(pindexNew->nHeight);
13001311
SetServiceFlagsIBDCache(!fInitialDownload);
13011312

13021313
// Don't relay inventory during initial block download.
@@ -1333,7 +1344,8 @@ void PeerManager::UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockInde
13331344
* Handle invalid block rejection and consequent peer discouragement, maintain which
13341345
* peers announce compact blocks.
13351346
*/
1336-
void PeerManager::BlockChecked(const CBlock& block, const BlockValidationState& state) {
1347+
void PeerManagerImpl::BlockChecked(const CBlock& block, const BlockValidationState& state)
1348+
{
13371349
LOCK(cs_main);
13381350

13391351
const uint256 hash(block.GetHash());
@@ -1761,7 +1773,8 @@ static uint32_t GetFetchFlags(const CNode& pfrom) EXCLUSIVE_LOCKS_REQUIRED(cs_ma
17611773
return nFetchFlags;
17621774
}
17631775

1764-
void PeerManager::SendBlockTransactions(CNode& pfrom, const CBlock& block, const BlockTransactionsRequest& req) {
1776+
void PeerManagerImpl::SendBlockTransactions(CNode& pfrom, const CBlock& block, const BlockTransactionsRequest& req)
1777+
{
17651778
BlockTransactions resp(req);
17661779
for (size_t i = 0; i < req.indexes.size(); i++) {
17671780
if (req.indexes[i] >= block.vtx.size()) {
@@ -1776,9 +1789,9 @@ void PeerManager::SendBlockTransactions(CNode& pfrom, const CBlock& block, const
17761789
m_connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCKTXN, resp));
17771790
}
17781791

1779-
void PeerManager::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
1780-
const std::vector<CBlockHeader>& headers,
1781-
bool via_compact_block)
1792+
void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
1793+
const std::vector<CBlockHeader>& headers,
1794+
bool via_compact_block)
17821795
{
17831796
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
17841797
size_t nCount = headers.size();
@@ -1970,7 +1983,7 @@ void PeerManager::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
19701983
* may be added to if accepting an orphan causes its children to be
19711984
* reconsidered.
19721985
*/
1973-
void PeerManager::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
1986+
void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
19741987
{
19751988
AssertLockHeld(cs_main);
19761989
AssertLockHeld(g_cs_orphans);
@@ -2267,9 +2280,9 @@ static void ProcessGetCFCheckPt(CNode& peer, CDataStream& vRecv, const CChainPar
22672280
connman.PushMessage(&peer, std::move(msg));
22682281
}
22692282

2270-
void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
2271-
const std::chrono::microseconds time_received,
2272-
const std::atomic<bool>& interruptMsgProc)
2283+
void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
2284+
const std::chrono::microseconds time_received,
2285+
const std::atomic<bool>& interruptMsgProc)
22732286
{
22742287
LogPrint(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(msg_type), vRecv.size(), pfrom.GetId());
22752288

@@ -3756,7 +3769,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
37563769
return;
37573770
}
37583771

3759-
bool PeerManager::MaybeDiscourageAndDisconnect(CNode& pnode)
3772+
bool PeerManagerImpl::MaybeDiscourageAndDisconnect(CNode& pnode)
37603773
{
37613774
const NodeId peer_id{pnode.GetId()};
37623775
PeerRef peer = GetPeerRef(peer_id);
@@ -3798,7 +3811,7 @@ bool PeerManager::MaybeDiscourageAndDisconnect(CNode& pnode)
37983811
return true;
37993812
}
38003813

3801-
bool PeerManager::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgProc)
3814+
bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgProc)
38023815
{
38033816
bool fMoreWork = false;
38043817

@@ -3873,7 +3886,7 @@ bool PeerManager::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgP
38733886
return fMoreWork;
38743887
}
38753888

3876-
void PeerManager::ConsiderEviction(CNode& pto, int64_t time_in_seconds)
3889+
void PeerManagerImpl::ConsiderEviction(CNode& pto, int64_t time_in_seconds)
38773890
{
38783891
AssertLockHeld(cs_main);
38793892

@@ -3926,7 +3939,7 @@ void PeerManager::ConsiderEviction(CNode& pto, int64_t time_in_seconds)
39263939
}
39273940
}
39283941

3929-
void PeerManager::EvictExtraOutboundPeers(int64_t time_in_seconds)
3942+
void PeerManagerImpl::EvictExtraOutboundPeers(int64_t time_in_seconds)
39303943
{
39313944
// If we have any extra block-relay-only peers, disconnect the youngest unless
39323945
// it's given us a block -- in which case, compare with the second-youngest, and
@@ -4027,7 +4040,7 @@ void PeerManager::EvictExtraOutboundPeers(int64_t time_in_seconds)
40274040
}
40284041
}
40294042

4030-
void PeerManager::CheckForStaleTipAndEvictPeers()
4043+
void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
40314044
{
40324045
LOCK(cs_main);
40334046

@@ -4074,7 +4087,7 @@ class CompareInvMempoolOrder
40744087
};
40754088
}
40764089

4077-
bool PeerManager::SendMessages(CNode* pto)
4090+
bool PeerManagerImpl::SendMessages(CNode* pto)
40784091
{
40794092
PeerRef peer = GetPeerRef(pto->GetId());
40804093
const Consensus::Params& consensusParams = m_chainparams.GetConsensus();

‎src/net_processing.h

+42-23
Original file line numberDiff line numberDiff line change
@@ -94,50 +94,69 @@ struct Peer {
9494

9595
using PeerRef = std::shared_ptr<Peer>;
9696

97-
class PeerManager final : public CValidationInterface, public NetEventsInterface {
97+
class PeerManager : public CValidationInterface, public NetEventsInterface
98+
{
9899
public:
99-
PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
100-
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
101-
bool ignore_incoming_txs);
102-
103-
/** Overridden from CValidationInterface. */
104-
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override;
105-
void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override;
106-
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
107-
void BlockChecked(const CBlock& block, const BlockValidationState& state) override;
108-
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override;
109-
110-
/** Implement NetEventsInterface */
111-
void InitializeNode(CNode* pnode) override;
112-
void FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) override;
113-
bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
114-
bool SendMessages(CNode* pto) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing);
100+
static std::unique_ptr<PeerManager> make(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
101+
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
102+
bool ignore_incoming_txs);
103+
virtual ~PeerManager() { }
115104

116105
/** Get statistics from node state */
117-
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats);
106+
virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) = 0;
118107

119108
/** Whether this node ignores txs received over p2p. */
120-
bool IgnoresIncomingTxs() { return m_ignore_incoming_txs; };
109+
virtual bool IgnoresIncomingTxs() = 0;
121110

122111
/** Set the best height */
123-
void SetBestHeight(int height) { m_best_height = height; };
112+
virtual void SetBestHeight(int height) = 0;
124113

125114
/**
126115
* Increment peer's misbehavior score. If the new value >= DISCOURAGEMENT_THRESHOLD, mark the node
127116
* to be discouraged, meaning the peer might be disconnected and added to the discouragement filter.
128117
* Public for unit testing.
129118
*/
130-
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message);
119+
virtual void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message) = 0;
131120

132121
/**
133122
* Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound.
134123
* Public for unit testing.
135124
*/
136-
void CheckForStaleTipAndEvictPeers();
125+
virtual void CheckForStaleTipAndEvictPeers() = 0;
137126

138127
/** Process a single message from a peer. Public for fuzz testing */
128+
virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
129+
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) = 0;
130+
};
131+
132+
class PeerManagerImpl final : public PeerManager
133+
{
134+
public:
135+
PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
136+
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
137+
bool ignore_incoming_txs);
138+
139+
/** Overridden from CValidationInterface. */
140+
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override;
141+
void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override;
142+
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
143+
void BlockChecked(const CBlock& block, const BlockValidationState& state) override;
144+
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override;
145+
146+
/** Implement NetEventsInterface */
147+
void InitializeNode(CNode* pnode) override;
148+
void FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) override;
149+
bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
150+
bool SendMessages(CNode* pto) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing);
151+
152+
/** Implement PeerManager */
153+
void CheckForStaleTipAndEvictPeers() override;
154+
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) override;
155+
bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
156+
void SetBestHeight(int height) override { m_best_height = height; };
157+
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message) override;
139158
void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
140-
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc);
159+
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override;
141160

142161
private:
143162
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */

0 commit comments

Comments
 (0)
Please sign in to comment.