Files
reth/crates/net/discv4/src/node.rs
Tomás dcd3923d19 Add serde support for NodeRecord primitive type (#617)
* Add serde support to NodeRecord

* Move NodeRecord to primitives

along with NodeKey and Octets

* Reexport NodeRecord from discv4

* Move NodeKey and kad_key back to discv4::node

Also, move NodeRecord::key functionality to a helper function: discv4::node::record_key.
This avoids the discv5 dependency in the primitives crate.

* Fix NodeRecord (de)serializing

The default derive macros work with a dictionary like display.
Changed that to serde_with macros, that use Display and FromStr traits.

* Add some tests for NodeRecord (de)serializing

* Hide NodeKey struct

* Move Octets after NodeRecord

* Replace record_key with From trait

* Fix clippy error

unnecessary into()
2022-12-27 18:03:54 +01:00

33 lines
866 B
Rust

use generic_array::GenericArray;
use reth_primitives::{keccak256, NodeRecord, PeerId};
/// The key type for the table.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub(crate) struct NodeKey(pub(crate) PeerId);
impl From<PeerId> for NodeKey {
fn from(value: PeerId) -> Self {
NodeKey(value)
}
}
impl From<NodeKey> for discv5::Key<NodeKey> {
fn from(value: NodeKey) -> Self {
let hash = keccak256(value.0.as_bytes());
let hash = *GenericArray::from_slice(hash.as_bytes());
discv5::Key::new_raw(value, hash)
}
}
impl From<&NodeRecord> for NodeKey {
fn from(node: &NodeRecord) -> Self {
NodeKey(node.id)
}
}
/// Converts a `PeerId` into the required `Key` type for the table
#[inline]
pub(crate) fn kad_key(node: PeerId) -> discv5::Key<NodeKey> {
discv5::kbucket::Key::from(NodeKey::from(node))
}