Skip to content

Commit 198cbde

Browse files
committed
feat: introduce new bridge interface NetHandler to separate network logic and consensus
1 parent 98600eb commit 198cbde

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed

src/init.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ void Interrupt(NodeContext& node)
250250
if (node.active_ctx) {
251251
node.active_ctx->Interrupt();
252252
}
253+
if (node.peerman) {
254+
node.peerman->InterruptHandlers();
255+
}
253256
if (node.llmq_ctx) {
254257
node.llmq_ctx->Interrupt();
255258
}
@@ -285,7 +288,10 @@ void PrepareShutdown(NodeContext& node)
285288
StopREST();
286289
StopRPC();
287290
StopHTTPServer();
291+
if (node.peerman) node.peerman->RemoveHandlers();
292+
288293
if (node.active_ctx) node.active_ctx->Stop();
294+
if (node.peerman) node.peerman->StopHandlers();
289295
if (node.llmq_ctx) node.llmq_ctx->Stop();
290296

291297
for (const auto& client : node.chain_clients) {
@@ -2286,6 +2292,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
22862292
// ********************************************************* Step 10a: schedule Dash-specific tasks
22872293

22882294
node.llmq_ctx->Start(*node.peerman);
2295+
node.peerman->StartHandlers();
22892296
if (node.active_ctx) node.active_ctx->Start(*node.connman, *node.peerman);
22902297

22912298
node.scheduler->scheduleEvery(std::bind(&CNetFulfilledRequestManager::DoMaintenance, std::ref(*node.netfulfilledman)), std::chrono::minutes{1});

src/net_processing.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,21 @@ class PeerManagerImpl final : public PeerManager
637637
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) override;
638638
bool IsBanned(NodeId pnode) override EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_peer_mutex);
639639
size_t GetRequestedObjectCount(NodeId nodeid) const override EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
640+
641+
/** Implements external handlers logic */
642+
void AddExtraHandler(std::unique_ptr<NetHandler>&& handler) override;
643+
void RemoveHandlers() override;
644+
void StartHandlers() override;
645+
void StopHandlers() override;
646+
void InterruptHandlers() override;
647+
648+
/** Implement PeerManagerInternal */
649+
void PeerMisbehaving(const NodeId pnode, const int howmuch, const std::string& message = "") override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
650+
void PeerEraseObjectRequest(const NodeId nodeid, const CInv& inv) override EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
651+
void PeerRelayInv(const CInv& inv) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
652+
void PeerRelayInvFiltered(const CInv& inv, const CTransaction& relatedTx) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
653+
void PeerRelayInvFiltered(const CInv& inv, const uint256& relatedTxHash) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
654+
void PeerAskPeersForTransaction(const uint256& txid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
640655
private:
641656
void _RelayTransaction(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_peer_mutex);
642657

@@ -1067,6 +1082,8 @@ class PeerManagerImpl final : public PeerManager
10671082
std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(g_msgproc_mutex);
10681083
/** Offset into vExtraTxnForCompact to insert the next tx */
10691084
size_t vExtraTxnForCompactIt GUARDED_BY(g_msgproc_mutex) = 0;
1085+
1086+
std::vector<std::unique_ptr<NetHandler>> m_handlers;
10701087
};
10711088

10721089
// Keeps track of the time (in microseconds) when transactions were requested last time
@@ -1625,6 +1642,46 @@ size_t PeerManagerImpl::GetRequestedObjectCount(NodeId nodeid) const
16251642
return state->m_object_download.m_object_process_time.size();
16261643
}
16271644

1645+
void PeerManagerImpl::AddExtraHandler(std::unique_ptr<NetHandler>&& handler)
1646+
{
1647+
assert(handler != nullptr);
1648+
if (auto i = dynamic_cast<CValidationInterface*>(handler.get()); i != nullptr) {
1649+
RegisterValidationInterface(i);
1650+
}
1651+
m_handlers.emplace_back(std::move(handler));
1652+
}
1653+
1654+
void PeerManagerImpl::RemoveHandlers()
1655+
{
1656+
InterruptHandlers();
1657+
StopHandlers();
1658+
m_handlers.clear();
1659+
}
1660+
1661+
void PeerManagerImpl::StartHandlers()
1662+
{
1663+
for (auto& handler : m_handlers) {
1664+
handler->Start();
1665+
}
1666+
}
1667+
1668+
void PeerManagerImpl::StopHandlers()
1669+
{
1670+
for (auto& handler : m_handlers) {
1671+
if (auto i = dynamic_cast<CValidationInterface*>(handler.get()); i != nullptr) {
1672+
UnregisterValidationInterface(i);
1673+
}
1674+
handler->Stop();
1675+
}
1676+
}
1677+
1678+
void PeerManagerImpl::InterruptHandlers()
1679+
{
1680+
for (auto& handler : m_handlers) {
1681+
handler->Interrupt();
1682+
}
1683+
}
1684+
16281685
void PeerManagerImpl::UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
16291686
{
16301687
LOCK(cs_main);
@@ -5395,6 +5452,9 @@ void PeerManagerImpl::ProcessMessage(
53955452
}
53965453

53975454
PostProcessMessage(m_llmq_ctx->isman->ProcessMessage(pfrom.GetId(), msg_type, vRecv), pfrom.GetId());
5455+
for (const auto& handler : m_handlers) {
5456+
handler->ProcessMessage(pfrom, msg_type, vRecv);
5457+
}
53985458
return;
53995459
}
54005460

@@ -6470,3 +6530,33 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
64706530
} // release cs_main
64716531
return true;
64726532
}
6533+
6534+
void PeerManagerImpl::PeerMisbehaving(const NodeId pnode, const int howmuch, const std::string& message)
6535+
{
6536+
Misbehaving(pnode, howmuch, message);
6537+
}
6538+
6539+
void PeerManagerImpl::PeerEraseObjectRequest(const NodeId nodeid, const CInv& inv)
6540+
{
6541+
EraseObjectRequest(nodeid, inv);
6542+
}
6543+
6544+
void PeerManagerImpl::PeerRelayInv(const CInv& inv)
6545+
{
6546+
RelayInv(inv);
6547+
}
6548+
6549+
void PeerManagerImpl::PeerRelayInvFiltered(const CInv& inv, const CTransaction& relatedTx)
6550+
{
6551+
RelayInvFiltered(inv, relatedTx);
6552+
}
6553+
6554+
void PeerManagerImpl::PeerRelayInvFiltered(const CInv& inv, const uint256& relatedTxHash)
6555+
{
6556+
RelayInvFiltered(inv, relatedTxHash);
6557+
}
6558+
6559+
void PeerManagerImpl::PeerAskPeersForTransaction(const uint256& txid)
6560+
{
6561+
AskPeersForTransaction(txid);
6562+
}

src/net_processing.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,36 @@ struct CNodeStateStats {
5555
ServiceFlags their_services;
5656
};
5757

58-
class PeerManager : public CValidationInterface, public NetEventsInterface
58+
class PeerManagerInternal
59+
{
60+
public:
61+
virtual void PeerMisbehaving(const NodeId pnode, const int howmuch, const std::string& message = "") = 0;
62+
virtual void PeerEraseObjectRequest(const NodeId nodeid, const CInv& inv) = 0;
63+
virtual void PeerRelayInv(const CInv& inv) = 0;
64+
virtual void PeerRelayInvFiltered(const CInv& inv, const CTransaction& relatedTx) = 0;
65+
virtual void PeerRelayInvFiltered(const CInv& inv, const uint256& relatedTxHash) = 0;
66+
virtual void PeerAskPeersForTransaction(const uint256& txid) = 0;
67+
};
68+
69+
class NetHandler
70+
{
71+
public:
72+
NetHandler(PeerManagerInternal* peer_manager) : m_peer_manager{Assert(peer_manager)} {}
73+
virtual ~NetHandler() {
74+
Interrupt();
75+
Stop();
76+
}
77+
78+
virtual void Start() {}
79+
virtual void Stop() {}
80+
virtual void Interrupt() {}
81+
virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv) {}
82+
protected:
83+
PeerManagerInternal* m_peer_manager;
84+
};
85+
86+
87+
class PeerManager : public CValidationInterface, public NetEventsInterface, public PeerManagerInternal
5988
{
6089
public:
6190
static std::unique_ptr<PeerManager> make(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman,
@@ -133,6 +162,12 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
133162
virtual bool IsBanned(NodeId pnode) = 0;
134163

135164
virtual size_t GetRequestedObjectCount(NodeId nodeid) const = 0;
165+
166+
virtual void AddExtraHandler(std::unique_ptr<NetHandler>&& handler) = 0;
167+
virtual void RemoveHandlers() = 0;
168+
virtual void StartHandlers() = 0;
169+
virtual void StopHandlers() = 0;
170+
virtual void InterruptHandlers() = 0;
136171
};
137172

138173
#endif // BITCOIN_NET_PROCESSING_H

0 commit comments

Comments
 (0)