Skip to content

Commit 8c049fe

Browse files
author
MarcoFalke
committed
Merge #19771: net: Replace enum CConnMan::NumConnections with enum class ConnectionDirection
c77de62 net: Replace enum CConnMan::NumConnections with enum class ConnectionDirection (Luke Dashjr) Pull request description: Refactor split out of #17167 ACKs for top commit: practicalswift: cr ACK c77de62: patch looks correct & `enum class` is strictly better Tree-SHA512: 40a1bf69d8ab2651b04ba6adbab789369a5a1a29a64ba764c3e6aab575b7943ea8dfd6e35b0abf5bcffa10e7265f4b523a93aa899c0fd581a84fc51ae5377b90
2 parents 8c21562 + c77de62 commit 8c049fe

File tree

9 files changed

+36
-24
lines changed

9 files changed

+36
-24
lines changed

src/interfaces/node.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
#define BITCOIN_INTERFACES_NODE_H
77

88
#include <amount.h> // For CAmount
9-
#include <net.h> // For CConnman::NumConnections
9+
#include <net.h> // For NodeId
1010
#include <net_types.h> // For banmap_t
1111
#include <netaddress.h> // For Network
12+
#include <netbase.h> // For ConnectionDirection
1213
#include <support/allocators/secure.h> // For SecureString
1314
#include <util/translation.h>
1415

@@ -88,7 +89,7 @@ class Node
8889
virtual bool getProxy(Network net, proxyType& proxy_info) = 0;
8990

9091
//! Get number of connections.
91-
virtual size_t getNodeCount(CConnman::NumConnections flags) = 0;
92+
virtual size_t getNodeCount(ConnectionDirection flags) = 0;
9293

9394
//! Get stats for connected nodes.
9495
using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;

src/net.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,15 +2725,15 @@ bool CConnman::RemoveAddedNode(const std::string& strNode)
27252725
return false;
27262726
}
27272727

2728-
size_t CConnman::GetNodeCount(NumConnections flags)
2728+
size_t CConnman::GetNodeCount(ConnectionDirection flags)
27292729
{
27302730
LOCK(cs_vNodes);
2731-
if (flags == CConnman::CONNECTIONS_ALL) // Shortcut if we want total
2731+
if (flags == ConnectionDirection::Both) // Shortcut if we want total
27322732
return vNodes.size();
27332733

27342734
int nNum = 0;
27352735
for (const auto& pnode : vNodes) {
2736-
if (flags & (pnode->IsInboundConn() ? CONNECTIONS_IN : CONNECTIONS_OUT)) {
2736+
if (flags & (pnode->IsInboundConn() ? ConnectionDirection::In : ConnectionDirection::Out)) {
27372737
nNum++;
27382738
}
27392739
}

src/net.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <i2p.h>
1818
#include <net_permissions.h>
1919
#include <netaddress.h>
20+
#include <netbase.h>
2021
#include <optional.h>
2122
#include <policy/feerate.h>
2223
#include <protocol.h>
@@ -801,13 +802,6 @@ class CConnman
801802
{
802803
public:
803804

804-
enum NumConnections {
805-
CONNECTIONS_NONE = 0,
806-
CONNECTIONS_IN = (1U << 0),
807-
CONNECTIONS_OUT = (1U << 1),
808-
CONNECTIONS_ALL = (CONNECTIONS_IN | CONNECTIONS_OUT),
809-
};
810-
811805
struct Options
812806
{
813807
ServiceFlags nLocalServices = NODE_NONE;
@@ -976,7 +970,7 @@ class CConnman
976970
*/
977971
bool AddConnection(const std::string& address, ConnectionType conn_type);
978972

979-
size_t GetNodeCount(NumConnections num);
973+
size_t GetNodeCount(ConnectionDirection);
980974
void GetNodeStats(std::vector<CNodeStats>& vstats);
981975
bool DisconnectNode(const std::string& node);
982976
bool DisconnectNode(const CSubNet& subnet);

src/netbase.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <memory>
1919
#include <stdint.h>
2020
#include <string>
21+
#include <type_traits>
2122
#include <vector>
2223

2324
extern int nConnectTimeout;
@@ -28,6 +29,22 @@ static const int DEFAULT_CONNECT_TIMEOUT = 5000;
2829
//! -dns default
2930
static const int DEFAULT_NAME_LOOKUP = true;
3031

32+
enum class ConnectionDirection {
33+
None = 0,
34+
In = (1U << 0),
35+
Out = (1U << 1),
36+
Both = (In | Out),
37+
};
38+
static inline ConnectionDirection& operator|=(ConnectionDirection& a, ConnectionDirection b) {
39+
using underlying = typename std::underlying_type<ConnectionDirection>::type;
40+
a = ConnectionDirection(underlying(a) | underlying(b));
41+
return a;
42+
}
43+
static inline bool operator&(ConnectionDirection a, ConnectionDirection b) {
44+
using underlying = typename std::underlying_type<ConnectionDirection>::type;
45+
return (underlying(a) & underlying(b));
46+
}
47+
3148
class proxyType
3249
{
3350
public:

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class NodeImpl : public Node
9696
bool shutdownRequested() override { return ShutdownRequested(); }
9797
void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); }
9898
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
99-
size_t getNodeCount(CConnman::NumConnections flags) override
99+
size_t getNodeCount(ConnectionDirection flags) override
100100
{
101101
return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0;
102102
}

src/qt/clientmodel.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ ClientModel::~ClientModel()
7171

7272
int ClientModel::getNumConnections(unsigned int flags) const
7373
{
74-
CConnman::NumConnections connections = CConnman::CONNECTIONS_NONE;
74+
ConnectionDirection connections = ConnectionDirection::None;
7575

7676
if(flags == CONNECTIONS_IN)
77-
connections = CConnman::CONNECTIONS_IN;
77+
connections = ConnectionDirection::In;
7878
else if (flags == CONNECTIONS_OUT)
79-
connections = CConnman::CONNECTIONS_OUT;
79+
connections = ConnectionDirection::Out;
8080
else if (flags == CONNECTIONS_ALL)
81-
connections = CConnman::CONNECTIONS_ALL;
81+
connections = ConnectionDirection::Both;
8282

8383
return m_node.getNodeCount(connections);
8484
}

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ static RPCHelpMan getblocktemplate()
659659
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
660660

661661
if (!Params().IsTestChain()) {
662-
if (node.connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0) {
662+
if (node.connman->GetNodeCount(ConnectionDirection::Both) == 0) {
663663
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");
664664
}
665665

src/rpc/net.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static RPCHelpMan getconnectioncount()
5757
if(!node.connman)
5858
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
5959

60-
return (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_ALL);
60+
return (int)node.connman->GetNodeCount(ConnectionDirection::Both);
6161
},
6262
};
6363
}
@@ -630,9 +630,9 @@ static RPCHelpMan getnetworkinfo()
630630
obj.pushKV("timeoffset", GetTimeOffset());
631631
if (node.connman) {
632632
obj.pushKV("networkactive", node.connman->GetNetworkActive());
633-
obj.pushKV("connections", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_ALL));
634-
obj.pushKV("connections_in", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_IN));
635-
obj.pushKV("connections_out", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_OUT));
633+
obj.pushKV("connections", (int)node.connman->GetNodeCount(ConnectionDirection::Both));
634+
obj.pushKV("connections_in", (int)node.connman->GetNodeCount(ConnectionDirection::In));
635+
obj.pushKV("connections_out", (int)node.connman->GetNodeCount(ConnectionDirection::Out));
636636
}
637637
obj.pushKV("networks", GetNetworksInfo());
638638
obj.pushKV("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()));

src/test/fuzz/connman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ FUZZ_TARGET_INIT(connman, initialize_connman)
9595
(void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
9696
},
9797
[&] {
98-
(void)connman.GetNodeCount(fuzzed_data_provider.PickValueInArray({CConnman::CONNECTIONS_NONE, CConnman::CONNECTIONS_IN, CConnman::CONNECTIONS_OUT, CConnman::CONNECTIONS_ALL}));
98+
(void)connman.GetNodeCount(fuzzed_data_provider.PickValueInArray({ConnectionDirection::None, ConnectionDirection::In, ConnectionDirection::Out, ConnectionDirection::Both}));
9999
},
100100
[&] {
101101
connman.MarkAddressGood(random_address);

0 commit comments

Comments
 (0)