Skip to content

Commit 051cdb3

Browse files
knstPastaPastaPasta
authored andcommitted
refactor: new helpers in net_processing for external handlers
That's a prior work for removing circular dependencies over net_processing
1 parent bb1d46c commit 051cdb3

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/net_processing.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ class PeerManagerImpl final : public PeerManager
249249
bool IsBanned(NodeId pnode) override EXCLUSIVE_LOCKS_REQUIRED(cs_main);
250250

251251
private:
252+
/** Helper to process result of external handlers of message */
253+
void ProcessPeerMsgRet(const PeerMsgRet& ret, CNode& pfrom);
254+
252255
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
253256
void ConsiderEviction(CNode& pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
254257

@@ -2800,6 +2803,11 @@ void PeerManagerImpl::ProcessBlock(CNode& pfrom, const std::shared_ptr<const CBl
28002803
}
28012804
}
28022805

2806+
void PeerManagerImpl::ProcessPeerMsgRet(const PeerMsgRet& ret, CNode& pfrom)
2807+
{
2808+
if (!ret) Misbehaving(pfrom.GetId(), ret.error().score, ret.error().message);
2809+
}
2810+
28032811
void PeerManagerImpl::ProcessMessage(
28042812
CNode& pfrom,
28052813
const std::string& msg_type,

src/net_types.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,31 @@
55
#ifndef BITCOIN_NET_TYPES_H
66
#define BITCOIN_NET_TYPES_H
77

8+
#include <util/expected.h>
9+
810
#include <map>
11+
#include <string>
912

1013
class CBanEntry;
1114
class CSubNet;
1215

1316
using banmap_t = std::map<CSubNet, CBanEntry>;
1417

18+
struct MisbehavingError
19+
{
20+
int score;
21+
std::string message;
22+
23+
MisbehavingError(int s) : score{s} {}
24+
25+
// Constructor does a perfect forwarding reference
26+
template <typename T>
27+
MisbehavingError(int s, T&& msg) :
28+
score{s},
29+
message{std::forward<T>(msg)}
30+
{}
31+
};
32+
33+
using PeerMsgRet = tl::expected<void, MisbehavingError>;
34+
1535
#endif // BITCOIN_NET_TYPES_H

0 commit comments

Comments
 (0)