Skip to content

Commit e29128b

Browse files
committed
rpc: Expose transport type via getpeerinfo
1 parent 0395fc8 commit e29128b

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

src/net.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ void CNode::CopyStats(CNodeStats& stats)
688688
stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
689689

690690
X(m_conn_type);
691+
X(m_transport_type);
691692
}
692693
#undef X
693694

@@ -1329,6 +1330,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
13291330
.prefer_evict = discouraged,
13301331
},
13311332
/*prefer_p2p_v2*/ false);
1333+
pnode->m_transport_type = TransportProtocolType::V1;
13321334
pnode->AddRef();
13331335
m_msgproc->InitializeNode(*pnode, nodeServices);
13341336

@@ -1627,6 +1629,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
16271629
PushV2EllSwiftPubkey(pnode);
16281630
// We now know the peer prefers a BIP324 v2 connection
16291631
pnode->m_prefer_p2p_v2 = true;
1632+
pnode->m_transport_type = TransportProtocolType::V2;
16301633
}
16311634

16321635
// Keys are not derived because we don't have the peer ellswift yet, keep buffering.
@@ -1695,6 +1698,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
16951698
pnode->CloseSocketDisconnect();
16961699
} else {
16971700
pnode->v2_key_exchange_complete = true;
1701+
pnode->m_transport_type = TransportProtocolType::V2;
16981702
if (!pnode->IsInboundConn()) {
16991703
// Outbound peer has completed key exchange and can start the P2P protocol
17001704
m_msgproc->InitP2P(*pnode, nLocalServices);
@@ -2401,6 +2405,9 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
24012405
// Only the outbound peer knows that both sides support BIP324 transport
24022406
if (pnode->PreferV2Conn()) {
24032407
PushV2EllSwiftPubkey(pnode);
2408+
pnode->m_transport_type = TransportProtocolType::V2;
2409+
} else {
2410+
pnode->m_transport_type = TransportProtocolType::V1;
24042411
}
24052412
m_msgproc->InitializeNode(*pnode, nLocalServices);
24062413
{
@@ -3154,7 +3161,8 @@ CNode::CNode(NodeId idIn,
31543161
ConnectionType conn_type_in,
31553162
bool inbound_onion,
31563163
CNodeOptions&& node_opts,
3157-
bool prefer_p2p_v2)
3164+
bool prefer_p2p_v2,
3165+
TransportProtocolType transport_type_in)
31583166
: m_permission_flags{node_opts.permission_flags},
31593167
m_sock{sock},
31603168
m_connected{GetTime<std::chrono::seconds>()},
@@ -3164,6 +3172,7 @@ CNode::CNode(NodeId idIn,
31643172
m_inbound_onion{inbound_onion},
31653173
m_prefer_evict{node_opts.prefer_evict},
31663174
nKeyedNetGroup{nKeyedNetGroupIn},
3175+
m_transport_type{transport_type_in},
31673176
id{idIn},
31683177
nLocalHostNonce{nLocalHostNonceIn},
31693178
m_conn_type{conn_type_in},

src/net.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ class CNodeStats
233233
Network m_network;
234234
uint32_t m_mapped_as;
235235
ConnectionType m_conn_type;
236+
TransportProtocolType m_transport_type;
236237
};
237238

238239

@@ -527,6 +528,7 @@ class CNode
527528
const uint64_t nKeyedNetGroup;
528529
std::atomic_bool fPauseRecv{false};
529530
std::atomic_bool fPauseSend{false};
531+
TransportProtocolType m_transport_type;
530532
std::atomic_bool v2_key_exchange_complete{false};
531533
std::atomic_bool m_authenticated_v2_garbage{false};
532534
bool v2_garbage_terminated{false};
@@ -653,7 +655,8 @@ class CNode
653655
ConnectionType conn_type_in,
654656
bool inbound_onion,
655657
CNodeOptions&& node_opts = {},
656-
bool prefer_p2p_v2 = false);
658+
bool prefer_p2p_v2 = false,
659+
TransportProtocolType transport_type_in = TransportProtocolType::V1);
657660
CNode(const CNode&) = delete;
658661
CNode& operator=(const CNode&) = delete;
659662

src/node/connection_types.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@ std::string ConnectionTypeAsString(ConnectionType conn_type)
2424

2525
assert(false);
2626
}
27+
28+
std::string TransportTypeAsString(TransportProtocolType transport_type)
29+
{
30+
switch (transport_type) {
31+
case TransportProtocolType::V1:
32+
return "v1";
33+
case TransportProtocolType::V2:
34+
return "v2";
35+
} // no default case, so the compiler can warn about missing cases
36+
37+
assert(false);
38+
}

src/node/connection_types.h

+9
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,13 @@ enum class ConnectionType {
7979
/** Convert ConnectionType enum to a string value */
8080
std::string ConnectionTypeAsString(ConnectionType conn_type);
8181

82+
/** Transport layer version */
83+
enum class TransportProtocolType {
84+
V1, // unencrypted, plaintext protocol
85+
V2, // BIP324 protocol
86+
};
87+
88+
/** Convert TransportProtocolType enum to a string value */
89+
std::string TransportTypeAsString(TransportProtocolType transport_type);
90+
8291
#endif // BITCOIN_NODE_CONNECTION_TYPES_H

src/rpc/net.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ const std::vector<std::string> CONNECTION_TYPE_DOC{
4444
"feeler (short-lived automatic connection for testing addresses)"
4545
};
4646

47+
const std::vector<std::string> TRANSPORT_TYPE_DOC{
48+
"v1 (plaintext transport protocol)",
49+
"v2 (BIP324 encrypted transport protocol)"
50+
};
51+
4752
static RPCHelpMan getconnectioncount()
4853
{
4954
return RPCHelpMan{"getconnectioncount",
@@ -163,6 +168,7 @@ static RPCHelpMan getpeerinfo()
163168
{RPCResult::Type::STR, "connection_type", "Type of connection: \n" + Join(CONNECTION_TYPE_DOC, ",\n") + ".\n"
164169
"Please note this output is unlikely to be stable in upcoming releases as we iterate to\n"
165170
"best capture connection behaviors."},
171+
{RPCResult::Type::STR, "transport_protocol_type", "Type of transport protocol: \n" + Join(TRANSPORT_TYPE_DOC, ",\n") + ".\n"},
166172
}},
167173
}},
168174
},
@@ -263,6 +269,7 @@ static RPCHelpMan getpeerinfo()
263269
}
264270
obj.pushKV("bytesrecv_per_msg", recvPerMsgType);
265271
obj.pushKV("connection_type", ConnectionTypeAsString(stats.m_conn_type));
272+
obj.pushKV("transport_protocol_type", TransportTypeAsString(stats.m_transport_type));
266273

267274
ret.push_back(obj);
268275
}

test/functional/p2p_v2.py

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def run_test(self):
3131
# sync_all() verifies that the block tips match
3232
self.sync_all(self.nodes[0:2])
3333
assert_equal(self.nodes[1].getblockcount(), 5)
34+
assert_equal(self.nodes[1].getpeerinfo()[0]["transport_protocol_type"], "v2")
3435

3536
# V1 nodes can sync with each other
3637
assert_equal(self.nodes[2].getblockcount(), 0)
@@ -42,6 +43,7 @@ def run_test(self):
4243
self.sync_all(self.nodes[2:4])
4344
assert_equal(self.nodes[3].getblockcount(), 8)
4445
assert self.nodes[0].getbestblockhash() != self.nodes[2].getbestblockhash()
46+
assert_equal(self.nodes[2].getpeerinfo()[0]["transport_protocol_type"], "v1")
4547

4648
# V1 nodes can sync with V2 nodes
4749
self.disconnect_nodes(0, 1)
@@ -50,12 +52,14 @@ def run_test(self):
5052
self.sync_all(self.nodes[1:3])
5153
assert_equal(self.nodes[1].getblockcount(), 8)
5254
assert self.nodes[0].getbestblockhash() != self.nodes[1].getbestblockhash()
55+
assert_equal(self.nodes[2].getpeerinfo()[0]["transport_protocol_type"], "v1")
5356

5457
# V2 nodes can sync with V1 nodes
5558
self.disconnect_nodes(1, 2)
5659
self.connect_nodes(0, 3, False)
5760
self.sync_all([self.nodes[0], self.nodes[3]])
5861
assert_equal(self.nodes[0].getblockcount(), 8)
62+
assert_equal(self.nodes[0].getpeerinfo()[0]["transport_protocol_type"], "v1")
5963

6064
# V2 node mines another block and everyone gets it
6165
self.connect_nodes(0, 1, True)
@@ -74,6 +78,7 @@ def run_test(self):
7478
self.connect_nodes(1, 4, True)
7579
self.sync_all()
7680
assert_equal(self.nodes[4].getblockcount(), 11)
81+
assert_equal(self.nodes[4].getpeerinfo()[0]["transport_protocol_type"], "v1")
7782

7883

7984
if __name__ == '__main__':

0 commit comments

Comments
 (0)