Skip to main content

ethrex_p2p/discv4/
server.rs

1use crate::{
2    discovery::lookup::IterativeLookup,
3    discv4::messages::{Message, Packet},
4    utils::node_id,
5};
6use bytes::BytesMut;
7use ethrex_common::{H256, H512};
8use std::{collections::HashMap, net::SocketAddr, time::Instant};
9
10pub const EXPIRATION_SECONDS: u64 = 20;
11
12/// Discv4-specific state held within the unified DiscoveryServer.
13#[derive(Debug, Default)]
14pub struct Discv4State {
15    /// Tracks pending FindNode requests by node_id -> sent_at.
16    /// Used to reject unsolicited Neighbors responses.
17    pub pending_find_node: HashMap<H256, Instant>,
18    /// Currently active iterative lookups, each with its cached signed FindNode message.
19    pub active_lookups: Vec<(IterativeLookup, BytesMut)>,
20}
21
22#[derive(Debug, Clone)]
23pub struct Discv4Message {
24    pub(crate) from: SocketAddr,
25    pub(crate) message: Message,
26    pub(crate) hash: H256,
27    pub(crate) sender_public_key: H512,
28}
29
30impl Discv4Message {
31    pub fn from(packet: Packet, from: SocketAddr) -> Self {
32        Self {
33            from,
34            message: packet.get_message().clone(),
35            hash: packet.get_hash(),
36            sender_public_key: packet.get_public_key(),
37        }
38    }
39
40    pub fn get_node_id(&self) -> H256 {
41        node_id(&self.sender_public_key)
42    }
43}