reth_discv4/
node.rs

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