Skip to content

Commit 5b2f4cf

Browse files
committed
rpc: Expose transport type via getpeerinfo
1 parent b3ba9b8 commit 5b2f4cf

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
@@ -703,6 +703,7 @@ void CNode::CopyStats(CNodeStats& stats)
703703
stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToStringAddrPort() : "";
704704

705705
X(m_conn_type);
706+
X(m_transport_type);
706707
}
707708
#undef X
708709

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

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

16971700
// Keys are not derived because we don't have the peer ellswift yet, keep buffering.
@@ -1778,6 +1781,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
17781781
pnode->CloseSocketDisconnect();
17791782
} else {
17801783
pnode->m_bip324_shared_state->key_exchange_complete = true;
1784+
pnode->m_transport_type = TransportProtocolType::V2;
17811785
if (!pnode->IsInboundConn()) {
17821786
// Outbound peer has completed key exchange and can start the P2P protocol
17831787
m_msgproc->InitP2P(*pnode, nLocalServices);
@@ -2513,6 +2517,9 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
25132517
// Only the outbound peer knows that both sides support BIP324 transport
25142518
if (pnode->PreferV2Conn()) {
25152519
PushV2EllSwiftPubkey(pnode);
2520+
pnode->m_transport_type = TransportProtocolType::V2;
2521+
} else {
2522+
pnode->m_transport_type = TransportProtocolType::V1;
25162523
}
25172524
m_msgproc->InitializeNode(*pnode, nLocalServices);
25182525
{
@@ -3275,6 +3282,7 @@ CNode::CNode(NodeId idIn,
32753282
m_inbound_onion{inbound_onion},
32763283
m_prefer_evict{node_opts.prefer_evict},
32773284
nKeyedNetGroup{nKeyedNetGroupIn},
3285+
m_transport_type{node_opts.transport_type},
32783286
id{idIn},
32793287
nLocalHostNonce{nLocalHostNonceIn},
32803288
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)