mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-02-19 03:04:27 -05:00
* 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()
33 lines
866 B
Rust
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))
|
|
}
|