refactor: new type of message processing result for resolving circular dependency over PeerManager

This commit is contained in:
Konstantin Akimov 2024-09-21 14:07:12 +07:00
parent d54b3eeb7b
commit 09565fe6cc
No known key found for this signature in database
GPG Key ID: 2176C4A5D01EA524
11 changed files with 56 additions and 26 deletions

View File

@ -8,7 +8,7 @@
#include <coinjoin/util.h> #include <coinjoin/util.h>
#include <coinjoin/coinjoin.h> #include <coinjoin/coinjoin.h>
#include <net_types.h> #include <protocol.h>
#include <util/ranges.h> #include <util/ranges.h>
#include <util/translation.h> #include <util/translation.h>

View File

@ -7,7 +7,7 @@
#include <coinjoin/coinjoin.h> #include <coinjoin/coinjoin.h>
#include <net_types.h> #include <protocol.h>
class CActiveMasternodeManager; class CActiveMasternodeManager;
class CChainState; class CChainState;

View File

@ -6,7 +6,7 @@
#define BITCOIN_EVO_MNAUTH_H #define BITCOIN_EVO_MNAUTH_H
#include <bls/bls.h> #include <bls/bls.h>
#include <net_types.h> #include <protocol.h>
#include <serialize.h> #include <serialize.h>
class CActiveMasternodeManager; class CActiveMasternodeManager;

View File

@ -10,7 +10,7 @@
#include <cachemap.h> #include <cachemap.h>
#include <cachemultimap.h> #include <cachemultimap.h>
#include <net_types.h> #include <protocol.h>
#include <util/check.h> #include <util/check.h>
#include <optional> #include <optional>

View File

@ -9,8 +9,8 @@
#include <chain.h> #include <chain.h>
#include <consensus/params.h> #include <consensus/params.h>
#include <net_types.h>
#include <primitives/block.h> #include <primitives/block.h>
#include <protocol.h>
#include <saltedhasher.h> #include <saltedhasher.h>
#include <sync.h> #include <sync.h>

View File

@ -13,8 +13,8 @@
#include <bls/bls.h> #include <bls/bls.h>
#include <bls/bls_worker.h> #include <bls/bls_worker.h>
#include <protocol.h>
#include <net_types.h>
#include <gsl/pointers.h> #include <gsl/pointers.h>
#include <atomic> #include <atomic>

View File

@ -8,7 +8,7 @@
#include <bls/bls.h> #include <bls/bls.h>
#include <consensus/params.h> #include <consensus/params.h>
#include <gsl/pointers.h> #include <gsl/pointers.h>
#include <net_types.h> #include <protocol.h>
#include <random.h> #include <random.h>
#include <saltedhasher.h> #include <saltedhasher.h>
#include <sync.h> #include <sync.h>

View File

@ -584,8 +584,9 @@ public:
bool IsInvInFilter(NodeId nodeid, const uint256& hash) const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex); bool IsInvInFilter(NodeId nodeid, const uint256& hash) const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
private: private:
/** Helper to process result of external handlers of message */ /** Helpers to process result of external handlers of message */
void ProcessPeerMsgRet(const PeerMsgRet& ret, CNode& pfrom) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex); void ProcessPeerMsgRet(const PeerMsgRet& ret, CNode& pfrom) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
void PostProcessMessage(MessageProcessingResult&& ret, NodeId node) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */ /** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
void ConsiderEviction(CNode& pto, Peer& peer, std::chrono::seconds time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_msgproc_mutex); void ConsiderEviction(CNode& pto, Peer& peer, std::chrono::seconds time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_msgproc_mutex);
@ -3309,6 +3310,16 @@ void PeerManagerImpl::ProcessPeerMsgRet(const PeerMsgRet& ret, CNode& pfrom)
if (!ret) Misbehaving(pfrom.GetId(), ret.error().score, ret.error().message); if (!ret) Misbehaving(pfrom.GetId(), ret.error().score, ret.error().message);
} }
void PeerManagerImpl::PostProcessMessage(MessageProcessingResult&& ret, NodeId node)
{
if (ret.m_error) {
Misbehaving(node, ret.m_error->score, ret.m_error->message);
}
if (ret.m_inventory) {
RelayInv(ret.m_inventory.value(), MIN_PEER_PROTO_VERSION);
}
}
void PeerManagerImpl::ProcessMessage( void PeerManagerImpl::ProcessMessage(
CNode& pfrom, CNode& pfrom,
const std::string& msg_type, const std::string& msg_type,

View File

@ -127,6 +127,9 @@ public:
virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv, virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0; const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0;
/** Finish message processing. Used for some specific messages */
virtual void PostProcessMessage(MessageProcessingResult&& ret, NodeId node = -1) = 0;
/** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */ /** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */
virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0; virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0;

View File

@ -5,8 +5,6 @@
#ifndef BITCOIN_NET_TYPES_H #ifndef BITCOIN_NET_TYPES_H
#define BITCOIN_NET_TYPES_H #define BITCOIN_NET_TYPES_H
#include <util/expected.h>
#include <cstdint> #include <cstdint>
#include <map> #include <map>
#include <string> #include <string>
@ -60,21 +58,5 @@ UniValue BanMapToJson(const banmap_t& bans);
*/ */
void BanMapFromJson(const UniValue& bans_json, banmap_t& bans); void BanMapFromJson(const UniValue& bans_json, banmap_t& bans);
struct MisbehavingError
{
int score;
std::string message;
MisbehavingError(int s) : score{s} {}
// Constructor does a perfect forwarding reference
template <typename T>
MisbehavingError(int s, T&& msg) :
score{s},
message{std::forward<T>(msg)}
{}
};
using PeerMsgRet = tl::expected<void, MisbehavingError>;
#endif // BITCOIN_NET_TYPES_H #endif // BITCOIN_NET_TYPES_H

View File

@ -17,6 +17,9 @@
#include <uint256.h> #include <uint256.h>
#include <version.h> #include <version.h>
#include <util/expected.h>
#include <limits> #include <limits>
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
@ -561,5 +564,36 @@ public:
uint256 hash; uint256 hash;
}; };
struct MisbehavingError
{
int score;
std::string message;
MisbehavingError(int s) : score{s} {}
// Constructor does a perfect forwarding reference
template <typename T>
MisbehavingError(int s, T&& msg) :
score{s},
message{std::forward<T>(msg)}
{}
};
using PeerMsgRet = tl::expected<void, MisbehavingError>;
struct MessageProcessingResult
{
std::optional<MisbehavingError> m_error;
std::optional<CInv> m_inventory;
MessageProcessingResult() = default;
MessageProcessingResult(MisbehavingError error) :
m_error(error)
{}
MessageProcessingResult(CInv inv) :
m_inventory(inv)
{
}
};
#endif // BITCOIN_PROTOCOL_H #endif // BITCOIN_PROTOCOL_H