Merge #19771: net: Replace enum CConnMan::NumConnections with enum class ConnectionDirection

c77de622dd8ef458f73b1a01a34629a7c4f49358 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 c77de622dd8ef458f73b1a01a34629a7c4f49358: patch looks correct & `enum class` is strictly better

Tree-SHA512: 40a1bf69d8ab2651b04ba6adbab789369a5a1a29a64ba764c3e6aab575b7943ea8dfd6e35b0abf5bcffa10e7265f4b523a93aa899c0fd581a84fc51ae5377b90
This commit is contained in:
MarcoFalke 2021-03-07 14:17:41 +01:00
commit 8c049fe9af
No known key found for this signature in database
GPG Key ID: D2EA4850E7528B25
9 changed files with 36 additions and 24 deletions

View File

@ -6,9 +6,10 @@
#define BITCOIN_INTERFACES_NODE_H #define BITCOIN_INTERFACES_NODE_H
#include <amount.h> // For CAmount #include <amount.h> // For CAmount
#include <net.h> // For CConnman::NumConnections #include <net.h> // For NodeId
#include <net_types.h> // For banmap_t #include <net_types.h> // For banmap_t
#include <netaddress.h> // For Network #include <netaddress.h> // For Network
#include <netbase.h> // For ConnectionDirection
#include <support/allocators/secure.h> // For SecureString #include <support/allocators/secure.h> // For SecureString
#include <util/translation.h> #include <util/translation.h>
@ -88,7 +89,7 @@ public:
virtual bool getProxy(Network net, proxyType& proxy_info) = 0; virtual bool getProxy(Network net, proxyType& proxy_info) = 0;
//! Get number of connections. //! Get number of connections.
virtual size_t getNodeCount(CConnman::NumConnections flags) = 0; virtual size_t getNodeCount(ConnectionDirection flags) = 0;
//! Get stats for connected nodes. //! Get stats for connected nodes.
using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>; using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;

View File

@ -2725,15 +2725,15 @@ bool CConnman::RemoveAddedNode(const std::string& strNode)
return false; return false;
} }
size_t CConnman::GetNodeCount(NumConnections flags) size_t CConnman::GetNodeCount(ConnectionDirection flags)
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
if (flags == CConnman::CONNECTIONS_ALL) // Shortcut if we want total if (flags == ConnectionDirection::Both) // Shortcut if we want total
return vNodes.size(); return vNodes.size();
int nNum = 0; int nNum = 0;
for (const auto& pnode : vNodes) { for (const auto& pnode : vNodes) {
if (flags & (pnode->IsInboundConn() ? CONNECTIONS_IN : CONNECTIONS_OUT)) { if (flags & (pnode->IsInboundConn() ? ConnectionDirection::In : ConnectionDirection::Out)) {
nNum++; nNum++;
} }
} }

View File

@ -17,6 +17,7 @@
#include <i2p.h> #include <i2p.h>
#include <net_permissions.h> #include <net_permissions.h>
#include <netaddress.h> #include <netaddress.h>
#include <netbase.h>
#include <optional.h> #include <optional.h>
#include <policy/feerate.h> #include <policy/feerate.h>
#include <protocol.h> #include <protocol.h>
@ -801,13 +802,6 @@ class CConnman
{ {
public: public:
enum NumConnections {
CONNECTIONS_NONE = 0,
CONNECTIONS_IN = (1U << 0),
CONNECTIONS_OUT = (1U << 1),
CONNECTIONS_ALL = (CONNECTIONS_IN | CONNECTIONS_OUT),
};
struct Options struct Options
{ {
ServiceFlags nLocalServices = NODE_NONE; ServiceFlags nLocalServices = NODE_NONE;
@ -976,7 +970,7 @@ public:
*/ */
bool AddConnection(const std::string& address, ConnectionType conn_type); bool AddConnection(const std::string& address, ConnectionType conn_type);
size_t GetNodeCount(NumConnections num); size_t GetNodeCount(ConnectionDirection);
void GetNodeStats(std::vector<CNodeStats>& vstats); void GetNodeStats(std::vector<CNodeStats>& vstats);
bool DisconnectNode(const std::string& node); bool DisconnectNode(const std::string& node);
bool DisconnectNode(const CSubNet& subnet); bool DisconnectNode(const CSubNet& subnet);

View File

@ -18,6 +18,7 @@
#include <memory> #include <memory>
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <type_traits>
#include <vector> #include <vector>
extern int nConnectTimeout; extern int nConnectTimeout;
@ -28,6 +29,22 @@ static const int DEFAULT_CONNECT_TIMEOUT = 5000;
//! -dns default //! -dns default
static const int DEFAULT_NAME_LOOKUP = true; static const int DEFAULT_NAME_LOOKUP = true;
enum class ConnectionDirection {
None = 0,
In = (1U << 0),
Out = (1U << 1),
Both = (In | Out),
};
static inline ConnectionDirection& operator|=(ConnectionDirection& a, ConnectionDirection b) {
using underlying = typename std::underlying_type<ConnectionDirection>::type;
a = ConnectionDirection(underlying(a) | underlying(b));
return a;
}
static inline bool operator&(ConnectionDirection a, ConnectionDirection b) {
using underlying = typename std::underlying_type<ConnectionDirection>::type;
return (underlying(a) & underlying(b));
}
class proxyType class proxyType
{ {
public: public:

View File

@ -96,7 +96,7 @@ public:
bool shutdownRequested() override { return ShutdownRequested(); } bool shutdownRequested() override { return ShutdownRequested(); }
void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); } void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); }
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); } bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
size_t getNodeCount(CConnman::NumConnections flags) override size_t getNodeCount(ConnectionDirection flags) override
{ {
return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0; return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0;
} }

View File

@ -71,14 +71,14 @@ ClientModel::~ClientModel()
int ClientModel::getNumConnections(unsigned int flags) const int ClientModel::getNumConnections(unsigned int flags) const
{ {
CConnman::NumConnections connections = CConnman::CONNECTIONS_NONE; ConnectionDirection connections = ConnectionDirection::None;
if(flags == CONNECTIONS_IN) if(flags == CONNECTIONS_IN)
connections = CConnman::CONNECTIONS_IN; connections = ConnectionDirection::In;
else if (flags == CONNECTIONS_OUT) else if (flags == CONNECTIONS_OUT)
connections = CConnman::CONNECTIONS_OUT; connections = ConnectionDirection::Out;
else if (flags == CONNECTIONS_ALL) else if (flags == CONNECTIONS_ALL)
connections = CConnman::CONNECTIONS_ALL; connections = ConnectionDirection::Both;
return m_node.getNodeCount(connections); return m_node.getNodeCount(connections);
} }

View File

@ -659,7 +659,7 @@ static RPCHelpMan getblocktemplate()
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
if (!Params().IsTestChain()) { if (!Params().IsTestChain()) {
if (node.connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0) { if (node.connman->GetNodeCount(ConnectionDirection::Both) == 0) {
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!"); throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");
} }

View File

@ -57,7 +57,7 @@ static RPCHelpMan getconnectioncount()
if(!node.connman) if(!node.connman)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
return (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_ALL); return (int)node.connman->GetNodeCount(ConnectionDirection::Both);
}, },
}; };
} }
@ -630,9 +630,9 @@ static RPCHelpMan getnetworkinfo()
obj.pushKV("timeoffset", GetTimeOffset()); obj.pushKV("timeoffset", GetTimeOffset());
if (node.connman) { if (node.connman) {
obj.pushKV("networkactive", node.connman->GetNetworkActive()); obj.pushKV("networkactive", node.connman->GetNetworkActive());
obj.pushKV("connections", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_ALL)); obj.pushKV("connections", (int)node.connman->GetNodeCount(ConnectionDirection::Both));
obj.pushKV("connections_in", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_IN)); obj.pushKV("connections_in", (int)node.connman->GetNodeCount(ConnectionDirection::In));
obj.pushKV("connections_out", (int)node.connman->GetNodeCount(CConnman::CONNECTIONS_OUT)); obj.pushKV("connections_out", (int)node.connman->GetNodeCount(ConnectionDirection::Out));
} }
obj.pushKV("networks", GetNetworksInfo()); obj.pushKV("networks", GetNetworksInfo());
obj.pushKV("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK())); obj.pushKV("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()));

View File

@ -95,7 +95,7 @@ FUZZ_TARGET_INIT(connman, initialize_connman)
(void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>()); (void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
}, },
[&] { [&] {
(void)connman.GetNodeCount(fuzzed_data_provider.PickValueInArray({CConnman::CONNECTIONS_NONE, CConnman::CONNECTIONS_IN, CConnman::CONNECTIONS_OUT, CConnman::CONNECTIONS_ALL})); (void)connman.GetNodeCount(fuzzed_data_provider.PickValueInArray({ConnectionDirection::None, ConnectionDirection::In, ConnectionDirection::Out, ConnectionDirection::Both}));
}, },
[&] { [&] {
connman.MarkAddressGood(random_address); connman.MarkAddressGood(random_address);