Skip to content

Commit 26e7a73

Browse files
committed
rpc: Expose transport type via getpeerinfo
1 parent 6824e8f commit 26e7a73

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

688688
X(m_conn_type);
689+
X(m_transport_type);
689690
}
690691
#undef X
691692

@@ -775,7 +776,7 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
775776
// to talk v1 or v2. if the first message is not VERSION, we reinterpret the
776777
// bytes as v2 ellswift
777778
if (gArgs.GetBoolArg("-v2transport", DEFAULT_V2_TRANSPORT) &&
778-
!m_prefer_p2p_v2 && IsInboundConn() &&
779+
m_transport_type == TransportProtocolType::V1 && IsInboundConn() &&
779780
mapRecvBytesPerMsgType.at(NetMsgType::VERSION) == 0 &&
780781
msg.m_type != NetMsgType::VERSION) {
781782
return false;
@@ -1341,6 +1342,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
13411342
.permission_flags = permission_flags,
13421343
.prefer_evict = discouraged,
13431344
.prefer_p2p_v2 = false});
1345+
pnode->m_transport_type = TransportProtocolType::V1;
13441346
pnode->AddRef();
13451347
m_msgproc->InitializeNode(*pnode, nodeServices);
13461348

@@ -1653,6 +1655,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
16531655
PushV2EllSwiftPubkey(pnode);
16541656
// We now know the peer prefers a BIP324 v2 connection
16551657
pnode->m_prefer_p2p_v2 = true;
1658+
pnode->m_transport_type = TransportProtocolType::V2;
16561659
}
16571660

16581661
// Keys are not derived because we don't have the peer ellswift yet, keep buffering.
@@ -1739,6 +1742,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
17391742
pnode->CloseSocketDisconnect();
17401743
} else {
17411744
pnode->m_bip324_shared_state->key_exchange_complete = true;
1745+
pnode->m_transport_type = TransportProtocolType::V2;
17421746
if (!pnode->IsInboundConn()) {
17431747
// Outbound peer has completed key exchange and can start the P2P protocol
17441748
m_msgproc->InitP2P(*pnode, nLocalServices);
@@ -2456,6 +2460,9 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
24562460
// Only the outbound peer knows that both sides support BIP324 transport
24572461
if (pnode->PreferV2Conn()) {
24582462
PushV2EllSwiftPubkey(pnode);
2463+
pnode->m_transport_type = TransportProtocolType::V2;
2464+
} else {
2465+
pnode->m_transport_type = TransportProtocolType::V1;
24592466
}
24602467
m_msgproc->InitializeNode(*pnode, nLocalServices);
24612468
{
@@ -3218,6 +3225,7 @@ CNode::CNode(NodeId idIn,
32183225
m_inbound_onion{inbound_onion},
32193226
m_prefer_evict{node_opts.prefer_evict},
32203227
nKeyedNetGroup{nKeyedNetGroupIn},
3228+
m_transport_type{node_opts.transport_type},
32213229
id{idIn},
32223230
nLocalHostNonce{nLocalHostNonceIn},
32233231
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
},
@@ -271,6 +276,7 @@ static RPCHelpMan getpeerinfo()
271276
}
272277
obj.pushKV("bytesrecv_per_msg", recvPerMsgType);
273278
obj.pushKV("connection_type", ConnectionTypeAsString(stats.m_conn_type));
279+
obj.pushKV("transport_protocol_type", TransportTypeAsString(stats.m_transport_type));
274280

275281
ret.push_back(obj);
276282
}

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
@@ -150,6 +150,7 @@ def test_getpeerinfo(self):
150150
"synced_blocks": -1,
151151
"synced_headers": -1,
152152
"timeoffset": 0,
153+
"transport_protocol_type": "v1",
153154
"version": 0,
154155
},
155156
)

0 commit comments

Comments
 (0)