Skip to content

Commit ae3acd1

Browse files
committed
rpc: Expose transport type via getpeerinfo
1 parent f52afc6 commit ae3acd1

File tree

7 files changed

+45
-1
lines changed

7 files changed

+45
-1
lines changed

src/net.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ void CNode::CopyStats(CNodeStats& stats)
704704
stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToStringAddrPort() : "";
705705

706706
X(m_conn_type);
707+
X(m_transport_type);
707708
}
708709
#undef X
709710

@@ -797,7 +798,7 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
797798
// to talk v1 or v2. if the first message is not VERSION, we reinterpret the
798799
// bytes as v2 ellswift
799800
if (gArgs.GetBoolArg("-v2transport", DEFAULT_V2_TRANSPORT) &&
800-
!m_prefer_p2p_v2 && IsInboundConn() &&
801+
m_transport_type == TransportProtocolType::V1 && IsInboundConn() &&
801802
mapRecvBytesPerMsgType.at(NetMsgType::VERSION) == 0 &&
802803
msg.m_type != NetMsgType::VERSION) {
803804
return false;
@@ -1377,6 +1378,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
13771378
.permission_flags = permission_flags,
13781379
.prefer_evict = discouraged,
13791380
.prefer_p2p_v2 = false});
1381+
pnode->m_transport_type = TransportProtocolType::V1;
13801382
pnode->AddRef();
13811383
m_msgproc->InitializeNode(*pnode, nodeServices);
13821384

@@ -1693,6 +1695,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
16931695
PushV2EllSwiftPubkey(pnode);
16941696
// We now know the peer prefers a BIP324 v2 connection
16951697
pnode->m_prefer_p2p_v2 = true;
1698+
pnode->m_transport_type = TransportProtocolType::V2;
16961699
}
16971700

16981701
// Keys are not derived because we don't have the peer ellswift yet, keep buffering.
@@ -1779,6 +1782,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
17791782
pnode->CloseSocketDisconnect();
17801783
} else {
17811784
pnode->m_bip324_shared_state->key_exchange_complete = true;
1785+
pnode->m_transport_type = TransportProtocolType::V2;
17821786
if (!pnode->IsInboundConn()) {
17831787
// Outbound peer has completed key exchange and can start the P2P protocol
17841788
m_msgproc->InitP2P(*pnode, nLocalServices);
@@ -2515,6 +2519,9 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
25152519
// Only the outbound peer knows that both sides support BIP324 transport
25162520
if (pnode->PreferV2Conn()) {
25172521
PushV2EllSwiftPubkey(pnode);
2522+
pnode->m_transport_type = TransportProtocolType::V2;
2523+
} else {
2524+
pnode->m_transport_type = TransportProtocolType::V1;
25182525
}
25192526
m_msgproc->InitializeNode(*pnode, nLocalServices);
25202527
{
@@ -3277,6 +3284,7 @@ CNode::CNode(NodeId idIn,
32773284
m_inbound_onion{inbound_onion},
32783285
m_prefer_evict{node_opts.prefer_evict},
32793286
nKeyedNetGroup{nKeyedNetGroupIn},
3287+
m_transport_type{node_opts.transport_type},
32803288
id{idIn},
32813289
nLocalHostNonce{nLocalHostNonceIn},
32823290
m_conn_type{conn_type_in},

src/net.h

+3
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ class CNodeStats
234234
Network m_network;
235235
uint32_t m_mapped_as;
236236
ConnectionType m_conn_type;
237+
TransportProtocolType m_transport_type;
237238
};
238239

239240

@@ -440,6 +441,7 @@ struct CNodeOptions
440441
std::unique_ptr<i2p::sam::Session> i2p_sam_session = nullptr;
441442
bool prefer_evict = false;
442443
bool prefer_p2p_v2 = false;
444+
TransportProtocolType transport_type = TransportProtocolType::V1;
443445
};
444446

445447
struct BIP324Session {
@@ -545,6 +547,7 @@ class CNode
545547
std::atomic_bool fPauseRecv{false};
546548
std::atomic_bool fPauseSend{false};
547549
std::unique_ptr<BIP324SharedState> m_bip324_shared_state;
550+
TransportProtocolType m_transport_type;
548551

549552
bool IsOutboundOrBlockRelayConn() const {
550553
switch (m_conn_type) {

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

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ 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+
4751
static RPCHelpMan getconnectioncount()
4852
{
4953
return RPCHelpMan{"getconnectioncount",
@@ -163,6 +167,7 @@ static RPCHelpMan getpeerinfo()
163167
{RPCResult::Type::STR, "connection_type", "Type of connection: \n" + Join(CONNECTION_TYPE_DOC, ",\n") + ".\n"
164168
"Please note this output is unlikely to be stable in upcoming releases as we iterate to\n"
165169
"best capture connection behaviors."},
170+
{RPCResult::Type::STR, "transport_protocol_type", "Type of transport protocol: \n" + Join(TRANSPORT_TYPE_DOC, ",\n") + ".\n"},
166171
}},
167172
}},
168173
},
@@ -267,6 +272,7 @@ static RPCHelpMan getpeerinfo()
267272
}
268273
obj.pushKV("bytesrecv_per_msg", recvPerMsgType);
269274
obj.pushKV("connection_type", ConnectionTypeAsString(stats.m_conn_type));
275+
obj.pushKV("transport_protocol_type", TransportTypeAsString(stats.m_transport_type));
270276

271277
ret.push_back(obj);
272278
}

test/functional/p2p_v2.py

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def run_test(self):
3535
# sync_all() verifies that the block tips match
3636
self.sync_all(self.nodes[0:2])
3737
assert_equal(self.nodes[1].getblockcount(), 5)
38+
assert_equal(self.nodes[1].getpeerinfo()[0]["transport_protocol_type"], "v2")
3839

3940
# V1 nodes can sync with each other
4041
assert_equal(self.nodes[2].getblockcount(), 0)
@@ -48,6 +49,7 @@ def run_test(self):
4849
self.sync_all(self.nodes[2:4])
4950
assert_equal(self.nodes[3].getblockcount(), 8)
5051
assert self.nodes[0].getbestblockhash() != self.nodes[2].getbestblockhash()
52+
assert_equal(self.nodes[2].getpeerinfo()[0]["transport_protocol_type"], "v1")
5153

5254
# V1 nodes can sync with V2 nodes
5355
self.disconnect_nodes(0, 1)
@@ -58,6 +60,7 @@ def run_test(self):
5860
self.sync_all(self.nodes[1:3])
5961
assert_equal(self.nodes[1].getblockcount(), 8)
6062
assert self.nodes[0].getbestblockhash() != self.nodes[1].getbestblockhash()
63+
assert_equal(self.nodes[2].getpeerinfo()[0]["transport_protocol_type"], "v1")
6164

6265
# V2 nodes can sync with V1 nodes
6366
self.disconnect_nodes(1, 2)
@@ -66,6 +69,7 @@ def run_test(self):
6669
self.connect_nodes(0, 3, False)
6770
self.sync_all([self.nodes[0], self.nodes[3]])
6871
assert_equal(self.nodes[0].getblockcount(), 8)
72+
assert_equal(self.nodes[0].getpeerinfo()[0]["transport_protocol_type"], "v1")
6973

7074
# V2 node mines another block and everyone gets it
7175
self.connect_nodes(0, 1, True)
@@ -85,6 +89,7 @@ def run_test(self):
8589
self.connect_nodes(1, 4, True)
8690
self.sync_all()
8791
assert_equal(self.nodes[4].getblockcount(), 11)
92+
assert_equal(self.nodes[4].getpeerinfo()[0]["transport_protocol_type"], "v1")
8893

8994

9095
if __name__ == '__main__':

test/functional/rpc_net.py

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def test_getpeerinfo(self):
145145
"synced_blocks": -1,
146146
"synced_headers": -1,
147147
"timeoffset": 0,
148+
"transport_protocol_type": "v1",
148149
"version": 0,
149150
},
150151
)

0 commit comments

Comments
 (0)