Skip to content

Commit ae8f797

Browse files
author
MarcoFalke
committed
Merge bitcoin#20210: net: assert CNode::m_inbound_onion is inbound in ctor, add getter, unit tests
86c4952 net: add CNode::IsInboundOnion() public getter and unit tests (Jon Atack) 6609eb8 net: assert CNode::m_inbound_onion is inbound in ctor (Jon Atack) 993d1ec test, fuzz: fix constructing CNode with invalid inbound_onion (Jon Atack) Pull request description: The goal of this PR is to be able to depend on `m_inbound_onion` in AttemptToEvictConnection in bitcoin#20197: - asserts `CNode::m_inbound_onion` is inbound in the CNode ctor to have a validity check at the class boundary - fixes a unit test and a fuzz utility that were passing invalid inbound onion values to the CNode ctor - drops an unneeded check in `CNode::ConnectedThroughNetwork()` for its inbound status - adds a public getter `IsInboundOnion()` that also allows unit testing it - adds unit test coverage ACKs for top commit: sipa: utACK 86c4952 LarryRuane: ACK 86c4952 vasild: ACK 86c4952 MarcoFalke: review ACK 86c4952 🐍 Tree-SHA512: 21109105bc4e5e03076fadd489204be00eac710c9de0127708ca2d0a10a048ff81f640f589a7429967ac3eb51d35fe24bb2b12e53e7aa3efbc47aaff6396d204
2 parents e75f91e + 86c4952 commit ae8f797

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

src/net.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ void CNode::SetAddrLocal(const CService& addrLocalIn) {
555555

556556
Network CNode::ConnectedThroughNetwork() const
557557
{
558-
return IsInboundConn() && m_inbound_onion ? NET_ONION : addr.GetNetClass();
558+
return m_inbound_onion ? NET_ONION : addr.GetNetClass();
559559
}
560560

561561
#undef X
@@ -2947,6 +2947,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
29472947
nMyStartingHeight(nMyStartingHeightIn),
29482948
m_inbound_onion(inbound_onion)
29492949
{
2950+
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);
29502951
hSocket = hSocketIn;
29512952
addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
29522953
if (conn_type_in != ConnectionType::BLOCK_RELAY) {

src/net.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ class CNode
10951095
CService addrLocal GUARDED_BY(cs_addrLocal);
10961096
mutable RecursiveMutex cs_addrLocal;
10971097

1098-
//! Whether this peer connected via our Tor onion service.
1098+
//! Whether this peer is an inbound onion, e.g. connected via our Tor onion service.
10991099
const bool m_inbound_onion{false};
11001100

11011101
public:
@@ -1219,6 +1219,9 @@ class CNode
12191219
void MaybeSetAddrName(const std::string& addrNameIn);
12201220

12211221
std::string ConnectionTypeAsString() const;
1222+
1223+
/** Whether this peer is an inbound onion, e.g. connected via our Tor onion service. */
1224+
bool IsInboundOnion() const { return m_inbound_onion; }
12221225
};
12231226

12241227
/** Return a timestamp in the future (in microseconds) for exponentially distributed events. */

src/test/fuzz/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ inline CNode ConsumeNode(FuzzedDataProvider& fuzzed_data_provider) noexcept
298298
const CAddress addr_bind = ConsumeAddress(fuzzed_data_provider);
299299
const std::string addr_name = fuzzed_data_provider.ConsumeRandomLengthString(64);
300300
const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray({ConnectionType::INBOUND, ConnectionType::OUTBOUND_FULL_RELAY, ConnectionType::MANUAL, ConnectionType::FEELER, ConnectionType::BLOCK_RELAY, ConnectionType::ADDR_FETCH});
301-
const bool inbound_onion = fuzzed_data_provider.ConsumeBool();
301+
const bool inbound_onion{conn_type == ConnectionType::INBOUND ? fuzzed_data_provider.ConsumeBool() : false};
302302
return {node_id, local_services, my_starting_height, socket, address, keyed_net_group, local_host_nonce, addr_bind, addr_name, conn_type, inbound_onion};
303303
}
304304

src/test/net_tests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test)
200200
BOOST_CHECK(pnode1->IsFeelerConn() == false);
201201
BOOST_CHECK(pnode1->IsAddrFetchConn() == false);
202202
BOOST_CHECK(pnode1->IsInboundConn() == false);
203+
BOOST_CHECK(pnode1->IsInboundOnion() == false);
203204
BOOST_CHECK_EQUAL(pnode1->ConnectedThroughNetwork(), Network::NET_IPV4);
204205

205206
std::unique_ptr<CNode> pnode2 = MakeUnique<CNode>(
@@ -214,20 +215,22 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test)
214215
BOOST_CHECK(pnode2->IsFeelerConn() == false);
215216
BOOST_CHECK(pnode2->IsAddrFetchConn() == false);
216217
BOOST_CHECK(pnode2->IsInboundConn() == true);
218+
BOOST_CHECK(pnode2->IsInboundOnion() == false);
217219
BOOST_CHECK_EQUAL(pnode2->ConnectedThroughNetwork(), Network::NET_IPV4);
218220

219221
std::unique_ptr<CNode> pnode3 = MakeUnique<CNode>(
220222
id++, NODE_NETWORK, height, hSocket, addr,
221223
/* nKeyedNetGroupIn = */ 0,
222224
/* nLocalHostNonceIn = */ 0,
223225
CAddress(), pszDest, ConnectionType::OUTBOUND_FULL_RELAY,
224-
/* inbound_onion = */ true);
226+
/* inbound_onion = */ false);
225227
BOOST_CHECK(pnode3->IsFullOutboundConn() == true);
226228
BOOST_CHECK(pnode3->IsManualConn() == false);
227229
BOOST_CHECK(pnode3->IsBlockOnlyConn() == false);
228230
BOOST_CHECK(pnode3->IsFeelerConn() == false);
229231
BOOST_CHECK(pnode3->IsAddrFetchConn() == false);
230232
BOOST_CHECK(pnode3->IsInboundConn() == false);
233+
BOOST_CHECK(pnode3->IsInboundOnion() == false);
231234
BOOST_CHECK_EQUAL(pnode3->ConnectedThroughNetwork(), Network::NET_IPV4);
232235

233236
std::unique_ptr<CNode> pnode4 = MakeUnique<CNode>(
@@ -242,6 +245,7 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test)
242245
BOOST_CHECK(pnode4->IsFeelerConn() == false);
243246
BOOST_CHECK(pnode4->IsAddrFetchConn() == false);
244247
BOOST_CHECK(pnode4->IsInboundConn() == true);
248+
BOOST_CHECK(pnode4->IsInboundOnion() == true);
245249
BOOST_CHECK_EQUAL(pnode4->ConnectedThroughNetwork(), Network::NET_ONION);
246250
}
247251

0 commit comments

Comments
 (0)