From 98b019466c6cb8edca68541ba434d76404f53b38 Mon Sep 17 00:00:00 2001 From: dhruv <856960+dhruv@users.noreply.github.com> Date: Thu, 20 Oct 2022 14:32:31 -0700 Subject: [PATCH] rpc: Expose BIP324 session id via getpeerinfo --- src/net.cpp | 4 ++++ src/net.h | 2 ++ src/rpc/net.cpp | 4 ++++ test/functional/p2p_v2.py | 11 +++++++++-- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 30fd930bf..087b1054d 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -687,6 +687,9 @@ void CNode::CopyStats(CNodeStats& stats) X(m_conn_type); X(m_transport_type); + if (m_transport_type == TransportProtocolType::V2) { + stats.m_v2_session_id = m_bip324_node_state->session_id; + } } #undef X @@ -712,6 +715,7 @@ void CNode::InitV2P2P(const Span their_ellswift, const Spankeys_derived = true; + m_bip324_node_state->session_id = HexStr(v2_keys.session_id); } void CNode::EnsureInitV2Key(bool initiating) diff --git a/src/net.h b/src/net.h index b0e99cfe8..e63ed9faa 100644 --- a/src/net.h +++ b/src/net.h @@ -234,6 +234,7 @@ class CNodeStats uint32_t m_mapped_as; ConnectionType m_conn_type; TransportProtocolType m_transport_type; + std::string m_v2_session_id; }; @@ -469,6 +470,7 @@ struct BIP324NodeState { std::array recv_garbage_terminator; std::vector garbage_bytes_recd; bool keys_derived{false}; + std::string session_id; }; void DeriveBIP324Session(ECDHSecret&& ecdh_secret, BIP324Session& session); diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 6367884b0..e4ebcf3ce 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -168,6 +168,7 @@ static RPCHelpMan getpeerinfo() "Please note this output is unlikely to be stable in upcoming releases as we iterate to\n" "best capture connection behaviors."}, {RPCResult::Type::STR, "transport_protocol_type", "Type of transport protocol: \n" + Join(TRANSPORT_TYPE_DOC, ",\n") + ".\n"}, + {RPCResult::Type::STR, "v2_session_id", /* optional */ true, "BIP324 session id for an encrypted v2 connection.\n"}, }}, }}, }, @@ -277,6 +278,9 @@ static RPCHelpMan getpeerinfo() obj.pushKV("bytesrecv_per_msg", recvPerMsgType); obj.pushKV("connection_type", ConnectionTypeAsString(stats.m_conn_type)); obj.pushKV("transport_protocol_type", TransportTypeAsString(stats.m_transport_type)); + if (stats.m_transport_type == TransportProtocolType::V2) { + obj.pushKV("v2_session_id", stats.m_v2_session_id); + } ret.push_back(obj); } diff --git a/test/functional/p2p_v2.py b/test/functional/p2p_v2.py index 1548d518d..9b757def2 100755 --- a/test/functional/p2p_v2.py +++ b/test/functional/p2p_v2.py @@ -35,7 +35,12 @@ def run_test(self): # sync_all() verifies that the block tips match self.sync_all(self.nodes[0:2]) assert_equal(self.nodes[1].getblockcount(), 5) - assert_equal(self.nodes[1].getpeerinfo()[0]["transport_protocol_type"], "v2") + peerinfo_0 = self.nodes[0].getpeerinfo() + peerinfo_1 = self.nodes[1].getpeerinfo() + assert_equal(peerinfo_0[0]["transport_protocol_type"], "v2") + assert_equal(peerinfo_1[0]["transport_protocol_type"], "v2") + assert_equal(len(peerinfo_0[0]["v2_session_id"]), 64) + assert_equal(peerinfo_0[0]["v2_session_id"], peerinfo_1[0]["v2_session_id"]) # V1 nodes can sync with each other assert_equal(self.nodes[2].getblockcount(), 0) @@ -49,7 +54,9 @@ def run_test(self): self.sync_all(self.nodes[2:4]) assert_equal(self.nodes[3].getblockcount(), 8) assert self.nodes[0].getbestblockhash() != self.nodes[2].getbestblockhash() - assert_equal(self.nodes[2].getpeerinfo()[0]["transport_protocol_type"], "v1") + peerinfo_2 = self.nodes[2].getpeerinfo() + assert_equal(peerinfo_2[0]["transport_protocol_type"], "v1") + assert "v2_session_id" not in peerinfo_2[0] # V1 nodes can sync with V2 nodes self.disconnect_nodes(0, 1)