Skip to content

Commit 21e4054

Browse files
committed
rpc: Expose transport type via getpeerinfo
1 parent bec3adb commit 21e4054

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
@@ -687,6 +687,7 @@ void CNode::CopyStats(CNodeStats& stats)
687687
stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
688688

689689
X(m_conn_type);
690+
X(m_transport_type);
690691
}
691692
#undef X
692693

@@ -780,7 +781,7 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
780781
// to talk v1 or v2. if the first message is not VERSION, we reinterpret the
781782
// bytes as v2 ellswift
782783
if (gArgs.GetBoolArg("-v2transport", DEFAULT_V2_TRANSPORT) &&
783-
!m_prefer_p2p_v2 && IsInboundConn() &&
784+
m_transport_type == TransportProtocolType::V1 && IsInboundConn() &&
784785
mapRecvBytesPerMsgType.at(NetMsgType::VERSION) == 0 &&
785786
msg.m_type != NetMsgType::VERSION) {
786787
return false;
@@ -1346,6 +1347,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
13461347
.permission_flags = permission_flags,
13471348
.prefer_evict = discouraged,
13481349
.prefer_p2p_v2 = false});
1350+
pnode->m_transport_type = TransportProtocolType::V1;
13491351
pnode->AddRef();
13501352
m_msgproc->InitializeNode(*pnode, nodeServices);
13511353

@@ -1658,6 +1660,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
16581660
PushV2EllSwiftPubkey(pnode);
16591661
// We now know the peer prefers a BIP324 v2 connection
16601662
pnode->m_prefer_p2p_v2 = true;
1663+
pnode->m_transport_type = TransportProtocolType::V2;
16611664
}
16621665

16631666
// Keys are not derived because we don't have the peer ellswift yet, keep buffering.
@@ -1744,6 +1747,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
17441747
pnode->CloseSocketDisconnect();
17451748
} else {
17461749
pnode->m_bip324_shared_state->key_exchange_complete = true;
1750+
pnode->m_transport_type = TransportProtocolType::V2;
17471751
if (!pnode->IsInboundConn()) {
17481752
// Outbound peer has completed key exchange and can start the P2P protocol
17491753
m_msgproc->InitP2P(*pnode, nLocalServices);
@@ -2472,6 +2476,9 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
24722476
// Only the outbound peer knows that both sides support BIP324 transport
24732477
if (pnode->PreferV2Conn()) {
24742478
PushV2EllSwiftPubkey(pnode);
2479+
pnode->m_transport_type = TransportProtocolType::V2;
2480+
} else {
2481+
pnode->m_transport_type = TransportProtocolType::V1;
24752482
}
24762483
m_msgproc->InitializeNode(*pnode, nLocalServices);
24772484
{
@@ -3234,6 +3241,7 @@ CNode::CNode(NodeId idIn,
32343241
m_inbound_onion{inbound_onion},
32353242
m_prefer_evict{node_opts.prefer_evict},
32363243
nKeyedNetGroup{nKeyedNetGroupIn},
3244+
m_transport_type{node_opts.transport_type},
32373245
id{idIn},
32383246
nLocalHostNonce{nLocalHostNonceIn},
32393247
m_conn_type{conn_type_in},

src/net.h

+3
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

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

444446
struct BIP324Session {
@@ -544,6 +546,7 @@ class CNode
544546
std::atomic_bool fPauseRecv{false};
545547
std::atomic_bool fPauseSend{false};
546548
std::unique_ptr<BIP324SharedState> m_bip324_shared_state;
549+
TransportProtocolType m_transport_type;
547550

548551
bool IsOutboundOrBlockRelayConn() const {
549552
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)