Skip to content

Commit 26304b4

Browse files
committed
[net/refactor] Introduce an enum to distinguish type of connection
- extract inbound & outbound types
1 parent 3f1b714 commit 26304b4

File tree

6 files changed

+20
-18
lines changed

6 files changed

+20
-18
lines changed

src/net.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
459459
NodeId id = GetNewNodeId();
460460
uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
461461
CAddress addr_bind = GetBindAddress(hSocket);
462-
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", false, block_relay_only);
462+
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", ConnectionType::OUTBOUND, block_relay_only);
463463
pnode->AddRef();
464464

465465
// We're making a new connection, harvest entropy from the time (and our peer count)
@@ -1048,7 +1048,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10481048
if (NetPermissions::HasFlag(permissionFlags, PF_BLOOMFILTER)) {
10491049
nodeServices = static_cast<ServiceFlags>(nodeServices | NODE_BLOOM);
10501050
}
1051-
CNode* pnode = new CNode(id, nodeServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", true);
1051+
CNode* pnode = new CNode(id, nodeServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", ConnectionType::INBOUND);
10521052
pnode->AddRef();
10531053
pnode->m_permissionFlags = permissionFlags;
10541054
// If this flag is present, the user probably expect that RPC and QT report it as whitelisted (backward compatibility)
@@ -2748,11 +2748,11 @@ int CConnman::GetBestHeight() const
27482748

27492749
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
27502750

2751-
CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, bool fInboundIn, bool block_relay_only)
2751+
CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool block_relay_only)
27522752
: nTimeConnected(GetSystemTimeInSeconds()),
27532753
addr(addrIn),
27542754
addrBind(addrBindIn),
2755-
fInbound(fInboundIn),
2755+
fInbound(conn_type_in == ConnectionType::INBOUND),
27562756
nKeyedNetGroup(nKeyedNetGroupIn),
27572757
// Don't relay addr messages to peers that we connect to as block-relay-only
27582758
// peers (to prevent adversaries from inferring these links from addr

src/net.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ struct CSerializedNetMsg
113113
std::string m_type;
114114
};
115115

116+
enum class ConnectionType {
117+
INBOUND,
118+
OUTBOUND,
119+
};
116120

117121
class NetEventsInterface;
118122
class CConnman
@@ -856,7 +860,7 @@ class CNode
856860

857861
std::set<uint256> orphan_work_set;
858862

859-
CNode(NodeId id, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress &addrBindIn, const std::string &addrNameIn = "", bool fInboundIn = false, bool block_relay_only = false);
863+
CNode(NodeId id, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress &addrBindIn, const std::string &addrNameIn = "", ConnectionType conn_type_in = ConnectionType::OUTBOUND, bool block_relay_only = false);
860864
~CNode();
861865
CNode(const CNode&) = delete;
862866
CNode& operator=(const CNode&) = delete;

src/test/denialofservice_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
8484

8585
// Mock an outbound peer
8686
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
87-
CNode dummyNode1(id++, ServiceFlags(NODE_NETWORK|NODE_WITNESS), 0, INVALID_SOCKET, addr1, 0, 0, CAddress(), "", /*fInboundIn=*/ false);
87+
CNode dummyNode1(id++, ServiceFlags(NODE_NETWORK|NODE_WITNESS), 0, INVALID_SOCKET, addr1, 0, 0, CAddress(), "", ConnectionType::OUTBOUND);
8888
dummyNode1.SetSendVersion(PROTOCOL_VERSION);
8989

9090
peerLogic->InitializeNode(&dummyNode1);
@@ -136,7 +136,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
136136
static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic, CConnmanTest* connman)
137137
{
138138
CAddress addr(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE);
139-
vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK|NODE_WITNESS), 0, INVALID_SOCKET, addr, 0, 0, CAddress(), "", /*fInboundIn=*/ false));
139+
vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK|NODE_WITNESS), 0, INVALID_SOCKET, addr, 0, 0, CAddress(), "", ConnectionType::OUTBOUND));
140140
CNode &node = *vNodes.back();
141141
node.SetSendVersion(PROTOCOL_VERSION);
142142

@@ -227,7 +227,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
227227

228228
banman->ClearBanned();
229229
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
230-
CNode dummyNode1(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr1, 0, 0, CAddress(), "", true);
230+
CNode dummyNode1(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr1, 0, 0, CAddress(), "", ConnectionType::INBOUND);
231231
dummyNode1.SetSendVersion(PROTOCOL_VERSION);
232232
peerLogic->InitializeNode(&dummyNode1);
233233
dummyNode1.nVersion = 1;
@@ -244,7 +244,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
244244
BOOST_CHECK(!banman->IsDiscouraged(ip(0xa0b0c001|0x0000ff00))); // Different IP, not discouraged
245245

246246
CAddress addr2(ip(0xa0b0c002), NODE_NONE);
247-
CNode dummyNode2(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr2, 1, 1, CAddress(), "", true);
247+
CNode dummyNode2(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr2, 1, 1, CAddress(), "", ConnectionType::INBOUND);
248248
dummyNode2.SetSendVersion(PROTOCOL_VERSION);
249249
peerLogic->InitializeNode(&dummyNode2);
250250
dummyNode2.nVersion = 1;
@@ -286,7 +286,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
286286
SetMockTime(nStartTime); // Overrides future calls to GetTime()
287287

288288
CAddress addr(ip(0xa0b0c001), NODE_NONE);
289-
CNode dummyNode(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr, 4, 4, CAddress(), "", true);
289+
CNode dummyNode(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr, 4, 4, CAddress(), "", ConnectionType::INBOUND);
290290
dummyNode.SetSendVersion(PROTOCOL_VERSION);
291291
peerLogic->InitializeNode(&dummyNode);
292292
dummyNode.nVersion = 1;

src/test/fuzz/process_message.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
8080
return;
8181
}
8282
CDataStream random_bytes_data_stream{fuzzed_data_provider.ConsumeRemainingBytes<unsigned char>(), SER_NETWORK, PROTOCOL_VERSION};
83-
CNode& p2p_node = *MakeUnique<CNode>(0, ServiceFlags(NODE_NETWORK | NODE_WITNESS | NODE_BLOOM), 0, INVALID_SOCKET, CAddress{CService{in_addr{0x0100007f}, 7777}, NODE_NETWORK}, 0, 0, CAddress{}, std::string{}, false).release();
83+
CNode& p2p_node = *MakeUnique<CNode>(0, ServiceFlags(NODE_NETWORK | NODE_WITNESS | NODE_BLOOM), 0, INVALID_SOCKET, CAddress{CService{in_addr{0x0100007f}, 7777}, NODE_NETWORK}, 0, 0, CAddress{}, std::string{}, ConnectionType::OUTBOUND, false).release();
8484
p2p_node.fSuccessfullyConnected = true;
8585
p2p_node.nVersion = PROTOCOL_VERSION;
8686
p2p_node.SetSendVersion(PROTOCOL_VERSION);

src/test/fuzz/process_messages.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ void test_one_input(const std::vector<uint8_t>& buffer)
4444
const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
4545
for (int i = 0; i < num_peers_to_add; ++i) {
4646
const ServiceFlags service_flags = ServiceFlags(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
47-
const bool inbound{fuzzed_data_provider.ConsumeBool()};
47+
const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray({ConnectionType::INBOUND, ConnectionType::OUTBOUND});
4848
const bool block_relay_only{fuzzed_data_provider.ConsumeBool()};
49-
peers.push_back(MakeUnique<CNode>(i, service_flags, 0, INVALID_SOCKET, CAddress{CService{in_addr{0x0100007f}, 7777}, NODE_NETWORK}, 0, 0, CAddress{}, std::string{}, inbound, block_relay_only).release());
49+
peers.push_back(MakeUnique<CNode>(i, service_flags, 0, INVALID_SOCKET, CAddress{CService{in_addr{0x0100007f}, 7777}, NODE_NETWORK}, 0, 0, CAddress{}, std::string{}, conn_type, block_relay_only).release());
5050
CNode& p2p_node = *peers.back();
5151

5252
p2p_node.fSuccessfullyConnected = true;

src/test/net_tests.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,13 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test)
180180

181181
CAddress addr = CAddress(CService(ipv4Addr, 7777), NODE_NETWORK);
182182
std::string pszDest;
183-
bool fInboundIn = false;
184183

185184
// Test that fFeeler is false by default.
186-
std::unique_ptr<CNode> pnode1 = MakeUnique<CNode>(id++, NODE_NETWORK, height, hSocket, addr, 0, 0, CAddress(), pszDest, fInboundIn);
185+
std::unique_ptr<CNode> pnode1 = MakeUnique<CNode>(id++, NODE_NETWORK, height, hSocket, addr, 0, 0, CAddress(), pszDest, ConnectionType::OUTBOUND);
187186
BOOST_CHECK(pnode1->fInbound == false);
188187
BOOST_CHECK(pnode1->fFeeler == false);
189188

190-
fInboundIn = true;
191-
std::unique_ptr<CNode> pnode2 = MakeUnique<CNode>(id++, NODE_NETWORK, height, hSocket, addr, 1, 1, CAddress(), pszDest, fInboundIn);
189+
std::unique_ptr<CNode> pnode2 = MakeUnique<CNode>(id++, NODE_NETWORK, height, hSocket, addr, 1, 1, CAddress(), pszDest, ConnectionType::INBOUND);
192190
BOOST_CHECK(pnode2->fInbound == true);
193191
BOOST_CHECK(pnode2->fFeeler == false);
194192
}
@@ -214,7 +212,7 @@ BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
214212
in_addr ipv4AddrPeer;
215213
ipv4AddrPeer.s_addr = 0xa0b0c001;
216214
CAddress addr = CAddress(CService(ipv4AddrPeer, 7777), NODE_NETWORK);
217-
std::unique_ptr<CNode> pnode = MakeUnique<CNode>(0, NODE_NETWORK, 0, INVALID_SOCKET, addr, 0, 0, CAddress{}, std::string{}, false);
215+
std::unique_ptr<CNode> pnode = MakeUnique<CNode>(0, NODE_NETWORK, 0, INVALID_SOCKET, addr, 0, 0, CAddress{}, std::string{}, ConnectionType::OUTBOUND);
218216
pnode->fSuccessfullyConnected.store(true);
219217

220218
// the peer claims to be reaching us via IPv6

0 commit comments

Comments
 (0)