Skip to content

Commit e1bc298

Browse files
committed
[net/refactor] Add block relay only connections to ConnectionType enum
1 parent 0e52a65 commit e1bc298

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

src/net.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ static CAddress GetBindAddress(SOCKET sock)
368368
return addr_bind;
369369
}
370370

371-
CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool block_relay_only)
371+
CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type)
372372
{
373373
assert(conn_type != ConnectionType::INBOUND);
374374

@@ -461,7 +461,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
461461
NodeId id = GetNewNodeId();
462462
uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
463463
CAddress addr_bind = GetBindAddress(hSocket);
464-
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", conn_type, block_relay_only);
464+
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", conn_type);
465465
pnode->AddRef();
466466

467467
// We're making a new connection, harvest entropy from the time (and our peer count)
@@ -1938,13 +1938,17 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
19381938

19391939
// Open this connection as block-relay-only if we're already at our
19401940
// full-relay capacity, but not yet at our block-relay peer limit.
1941-
// (It should not be possible for fFeeler to be set if we're not
1942-
// also at our block-relay peer limit, but check against that as
1943-
// well for sanity.)
1944-
bool block_relay_only = nOutboundBlockRelay < m_max_outbound_block_relay && !fFeeler && nOutboundFullRelay >= m_max_outbound_full_relay;
1941+
bool block_relay_only = nOutboundBlockRelay < m_max_outbound_block_relay && nOutboundFullRelay >= m_max_outbound_full_relay;
1942+
ConnectionType conn_type;
1943+
if(fFeeler) {
1944+
conn_type = ConnectionType::FEELER;
1945+
} else if (block_relay_only) {
1946+
conn_type = ConnectionType::BLOCK_RELAY;
1947+
} else {
1948+
conn_type = ConnectionType::OUTBOUND;
1949+
}
19451950

1946-
ConnectionType conn_type = (fFeeler ? ConnectionType::FEELER : ConnectionType::OUTBOUND);
1947-
OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, false, conn_type, block_relay_only);
1951+
OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, false, conn_type);
19481952
}
19491953
}
19501954
}
@@ -2031,7 +2035,7 @@ void CConnman::ThreadOpenAddedConnections()
20312035
}
20322036

20332037
// if successful, this moves the passed grant to the constructed node
2034-
void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool m_addr_fetch, ConnectionType conn_type, bool block_relay_only)
2038+
void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool m_addr_fetch, ConnectionType conn_type)
20352039
{
20362040
assert(conn_type != ConnectionType::INBOUND);
20372041

@@ -2052,7 +2056,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
20522056
} else if (FindNode(std::string(pszDest)))
20532057
return;
20542058

2055-
CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure, conn_type, block_relay_only);
2059+
CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure, conn_type);
20562060

20572061
if (!pnode)
20582062
return;
@@ -2733,7 +2737,7 @@ int CConnman::GetBestHeight() const
27332737

27342738
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
27352739

2736-
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)
2740+
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)
27372741
: nTimeConnected(GetSystemTimeInSeconds()),
27382742
addr(addrIn),
27392743
addrBind(addrBindIn),
@@ -2744,7 +2748,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
27442748
// Don't relay addr messages to peers that we connect to as block-relay-only
27452749
// peers (to prevent adversaries from inferring these links from addr
27462750
// traffic).
2747-
m_addr_known{block_relay_only ? nullptr : MakeUnique<CRollingBloomFilter>(5000, 0.001)},
2751+
m_addr_known{conn_type_in == ConnectionType::BLOCK_RELAY ? nullptr : MakeUnique<CRollingBloomFilter>(5000, 0.001)},
27482752
id(idIn),
27492753
nLocalHostNonce(nLocalHostNonceIn),
27502754
nLocalServices(nLocalServicesIn),
@@ -2753,7 +2757,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
27532757
hSocket = hSocketIn;
27542758
addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
27552759
hashContinue = uint256();
2756-
if (!block_relay_only) {
2760+
if (conn_type_in != ConnectionType::BLOCK_RELAY) {
27572761
m_tx_relay = MakeUnique<TxRelay>();
27582762
}
27592763

src/net.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ enum class ConnectionType {
118118
OUTBOUND,
119119
MANUAL,
120120
FEELER,
121+
BLOCK_RELAY,
121122
};
122123

123124
class NetEventsInterface;
@@ -203,7 +204,7 @@ class CConnman
203204
bool GetNetworkActive() const { return fNetworkActive; };
204205
bool GetUseAddrmanOutgoing() const { return m_use_addrman_outgoing; };
205206
void SetNetworkActive(bool active);
206-
void OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = nullptr, const char *strDest = nullptr, bool m_addr_fetch = false, ConnectionType conn_type = ConnectionType::OUTBOUND, bool block_relay_only = false);
207+
void OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = nullptr, const char *strDest = nullptr, bool m_addr_fetch = false, ConnectionType conn_type = ConnectionType::OUTBOUND);
207208
bool CheckIncomingNonce(uint64_t nonce);
208209

209210
bool ForNode(NodeId id, std::function<bool(CNode* pnode)> func);
@@ -368,7 +369,7 @@ class CConnman
368369
CNode* FindNode(const CService& addr);
369370

370371
bool AttemptToEvictConnection();
371-
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool block_relay_only);
372+
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type);
372373
void AddWhitelistPermissionFlags(NetPermissionFlags& flags, const CNetAddr &addr) const;
373374

374375
void DeleteNode(CNode* pnode);
@@ -862,7 +863,7 @@ class CNode
862863

863864
std::set<uint256> orphan_work_set;
864865

865-
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);
866+
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);
866867
~CNode();
867868
CNode(const CNode&) = delete;
868869
CNode& operator=(const CNode&) = delete;

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{}, ConnectionType::OUTBOUND, 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).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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ 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 ConnectionType conn_type = fuzzed_data_provider.PickValueInArray({ConnectionType::INBOUND, ConnectionType::OUTBOUND, ConnectionType::MANUAL, ConnectionType::FEELER});
48-
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{}, conn_type, block_relay_only).release());
47+
const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray({ConnectionType::INBOUND, ConnectionType::OUTBOUND, ConnectionType::MANUAL, ConnectionType::FEELER, ConnectionType::BLOCK_RELAY});
48+
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).release());
5049
CNode& p2p_node = *peers.back();
5150

5251
p2p_node.fSuccessfullyConnected = true;

0 commit comments

Comments
 (0)