Skip to content

Commit f97a1a8

Browse files
committed
rpc: Expose BIP324 session id via getpeerinfo
1 parent 21e4054 commit f97a1a8

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

src/net.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,9 @@ void CNode::CopyStats(CNodeStats& stats)
688688

689689
X(m_conn_type);
690690
X(m_transport_type);
691+
if (m_transport_type == TransportProtocolType::V2) {
692+
stats.m_v2_session_id = m_bip324_node_state->session_id;
693+
}
691694
}
692695
#undef X
693696

@@ -713,6 +716,7 @@ void CNode::InitV2P2P(const Span<const std::byte> their_ellswift, const Span<con
713716
}
714717
// Both peers must keep around a copy of the garbage terminator for the BIP324 shapable handshake
715718
m_bip324_node_state->keys_derived = true;
719+
m_bip324_node_state->session_id = HexStr(v2_keys.session_id);
716720
}
717721

718722
void CNode::EnsureInitV2Key(bool initiating)

src/net.h

+2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ class CNodeStats
234234
uint32_t m_mapped_as;
235235
ConnectionType m_conn_type;
236236
TransportProtocolType m_transport_type;
237+
std::string m_v2_session_id;
237238
};
238239

239240

@@ -469,6 +470,7 @@ struct BIP324NodeState {
469470
std::array<std::byte, BIP324_GARBAGE_TERMINATOR_LEN> recv_garbage_terminator;
470471
std::vector<std::byte> garbage_bytes_recd;
471472
bool keys_derived{false};
473+
std::string session_id;
472474
};
473475

474476
void DeriveBIP324Session(ECDHSecret&& ecdh_secret, BIP324Session& session);

src/rpc/net.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ static RPCHelpMan getpeerinfo()
168168
"Please note this output is unlikely to be stable in upcoming releases as we iterate to\n"
169169
"best capture connection behaviors."},
170170
{RPCResult::Type::STR, "transport_protocol_type", "Type of transport protocol: \n" + Join(TRANSPORT_TYPE_DOC, ",\n") + ".\n"},
171+
{RPCResult::Type::STR, "v2_session_id", /* optional */ true, "BIP324 session id for an encrypted v2 connection.\n"},
171172
}},
172173
}},
173174
},
@@ -273,6 +274,9 @@ static RPCHelpMan getpeerinfo()
273274
obj.pushKV("bytesrecv_per_msg", recvPerMsgType);
274275
obj.pushKV("connection_type", ConnectionTypeAsString(stats.m_conn_type));
275276
obj.pushKV("transport_protocol_type", TransportTypeAsString(stats.m_transport_type));
277+
if (stats.m_transport_type == TransportProtocolType::V2) {
278+
obj.pushKV("v2_session_id", stats.m_v2_session_id);
279+
}
276280

277281
ret.push_back(obj);
278282
}

test/functional/p2p_v2.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ 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")
38+
peerinfo_0 = self.nodes[0].getpeerinfo()
39+
peerinfo_1 = self.nodes[1].getpeerinfo()
40+
assert_equal(peerinfo_0[0]["transport_protocol_type"], "v2")
41+
assert_equal(peerinfo_1[0]["transport_protocol_type"], "v2")
42+
assert_equal(len(peerinfo_0[0]["v2_session_id"]), 64)
43+
assert_equal(peerinfo_0[0]["v2_session_id"], peerinfo_1[0]["v2_session_id"])
3944

4045
# V1 nodes can sync with each other
4146
assert_equal(self.nodes[2].getblockcount(), 0)
@@ -49,7 +54,9 @@ def run_test(self):
4954
self.sync_all(self.nodes[2:4])
5055
assert_equal(self.nodes[3].getblockcount(), 8)
5156
assert self.nodes[0].getbestblockhash() != self.nodes[2].getbestblockhash()
52-
assert_equal(self.nodes[2].getpeerinfo()[0]["transport_protocol_type"], "v1")
57+
peerinfo_2 = self.nodes[2].getpeerinfo()
58+
assert_equal(peerinfo_2[0]["transport_protocol_type"], "v1")
59+
assert "v2_session_id" not in peerinfo_2[0]
5360

5461
# V1 nodes can sync with V2 nodes
5562
self.disconnect_nodes(0, 1)

0 commit comments

Comments
 (0)