Skip to content

Commit

Permalink
rpc: Expose BIP324 session id via getpeerinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv committed Jan 14, 2023
1 parent 26e7a73 commit 98b0194
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -712,6 +715,7 @@ void CNode::InitV2P2P(const Span<const std::byte> their_ellswift, const Span<con
}
// Both peers must keep around a copy of the garbage terminator for the BIP324 shapable handshake
m_bip324_node_state->keys_derived = true;
m_bip324_node_state->session_id = HexStr(v2_keys.session_id);
}

void CNode::EnsureInitV2Key(bool initiating)
Expand Down
2 changes: 2 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};


Expand Down Expand Up @@ -469,6 +470,7 @@ struct BIP324NodeState {
std::array<std::byte, BIP324_GARBAGE_TERMINATOR_LEN> recv_garbage_terminator;
std::vector<std::byte> garbage_bytes_recd;
bool keys_derived{false};
std::string session_id;
};

void DeriveBIP324Session(ECDHSecret&& ecdh_secret, BIP324Session& session);
Expand Down
4 changes: 4 additions & 0 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}},
}},
},
Expand Down Expand Up @@ -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);
}
Expand Down
11 changes: 9 additions & 2 deletions test/functional/p2p_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 98b0194

Please sign in to comment.