reth_discv4/node.rs
1use alloy_primitives::keccak256;
2use reth_network_peers::{NodeRecord, PeerId};
3
4/// The key type for the table.
5#[derive(Debug, Copy, Clone, Eq, PartialEq)]
6pub(crate) struct NodeKey(pub(crate) PeerId);
7
8impl From<PeerId> for NodeKey {
9 fn from(value: PeerId) -> Self {
10 Self(value)
11 }
12}
13
14impl From<NodeKey> for discv5::Key<NodeKey> {
15 fn from(value: NodeKey) -> Self {
16 let hash = keccak256(value.0.as_slice());
17 Self::new_raw(value, hash.0.into())
18 }
19}
20
21impl From<&NodeRecord> for NodeKey {
22 fn from(node: &NodeRecord) -> Self {
23 Self(node.id)
24 }
25}
26
27/// Converts a `PeerId` into the required `Key` type for the table
28#[inline]
29pub(crate) fn kad_key(node: PeerId) -> discv5::Key<NodeKey> {
30 discv5::kbucket::Key::from(NodeKey::from(node))
31}