Skip to content

Commit 2222284

Browse files
committed
rpc: Expose BIP324 session id via getpeerinfo
1 parent e29128b commit 2222284

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

src/net.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ void CNode::CopyStats(CNodeStats& stats)
689689

690690
X(m_conn_type);
691691
X(m_transport_type);
692+
X(m_v2_session_id);
692693
}
693694
#undef X
694695

@@ -712,6 +713,7 @@ void CNode::InitV2P2P(const Span<const std::byte> their_ellswift, const Span<con
712713
}
713714
// Both peers must keep around a copy of the garbage terminator for the BIP324 shapable handshake
714715
v2_keys_derived = true;
716+
m_v2_session_id = HexStr(v2_keys.session_id);
715717
}
716718

717719
void CNode::EnsureInitV2Key(bool initiating)

src/net.h

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

@@ -656,7 +657,7 @@ class CNode
656657
bool inbound_onion,
657658
CNodeOptions&& node_opts = {},
658659
bool prefer_p2p_v2 = false,
659-
TransportProtocolType transport_type_in = TransportProtocolType::V1);
660+
TransportProtocolType transport_type_in = TransportProtocolType::V1);
660661
CNode(const CNode&) = delete;
661662
CNode& operator=(const CNode&) = delete;
662663

@@ -759,6 +760,7 @@ class CNode
759760

760761
// Peer prefers a BIP324(v2) p2p transport
761762
bool m_prefer_p2p_v2;
763+
std::string m_v2_session_id;
762764
};
763765

764766
/**

src/rpc/net.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ static RPCHelpMan getpeerinfo()
169169
"Please note this output is unlikely to be stable in upcoming releases as we iterate to\n"
170170
"best capture connection behaviors."},
171171
{RPCResult::Type::STR, "transport_protocol_type", "Type of transport protocol: \n" + Join(TRANSPORT_TYPE_DOC, ",\n") + ".\n"},
172+
{RPCResult::Type::STR, "v2_session_id", /* optional */ true, "BIP324 session id for an encrypted v2 connection.\n"},
172173
}},
173174
}},
174175
},
@@ -270,6 +271,9 @@ static RPCHelpMan getpeerinfo()
270271
obj.pushKV("bytesrecv_per_msg", recvPerMsgType);
271272
obj.pushKV("connection_type", ConnectionTypeAsString(stats.m_conn_type));
272273
obj.pushKV("transport_protocol_type", TransportTypeAsString(stats.m_transport_type));
274+
if (stats.m_transport_type == TransportProtocolType::V2) {
275+
obj.pushKV("v2_session_id", stats.m_v2_session_id);
276+
}
273277

274278
ret.push_back(obj);
275279
}

test/functional/p2p_v2.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ def run_test(self):
3131
# sync_all() verifies that the block tips match
3232
self.sync_all(self.nodes[0:2])
3333
assert_equal(self.nodes[1].getblockcount(), 5)
34-
assert_equal(self.nodes[1].getpeerinfo()[0]["transport_protocol_type"], "v2")
34+
peerinfo_0 = self.nodes[0].getpeerinfo()
35+
peerinfo_1 = self.nodes[1].getpeerinfo()
36+
assert_equal(peerinfo_0[0]["transport_protocol_type"], "v2")
37+
assert_equal(peerinfo_1[0]["transport_protocol_type"], "v2")
38+
assert_equal(len(peerinfo_0[0]["v2_session_id"]), 64)
39+
assert_equal(peerinfo_0[0]["v2_session_id"], peerinfo_1[0]["v2_session_id"])
3540

3641
# V1 nodes can sync with each other
3742
assert_equal(self.nodes[2].getblockcount(), 0)
@@ -43,7 +48,9 @@ def run_test(self):
4348
self.sync_all(self.nodes[2:4])
4449
assert_equal(self.nodes[3].getblockcount(), 8)
4550
assert self.nodes[0].getbestblockhash() != self.nodes[2].getbestblockhash()
46-
assert_equal(self.nodes[2].getpeerinfo()[0]["transport_protocol_type"], "v1")
51+
peerinfo_2 = self.nodes[2].getpeerinfo()
52+
assert_equal(peerinfo_2[0]["transport_protocol_type"], "v1")
53+
assert "v2_session_id" not in peerinfo_2[0]
4754

4855
# V1 nodes can sync with V2 nodes
4956
self.disconnect_nodes(0, 1)

0 commit comments

Comments
 (0)