Skip to main content

reth_discv4/
lib.rs

1//! Discovery v4 implementation: <https://github.com/ethereum/devp2p/blob/master/discv4.md>
2//!
3//! Discv4 employs a kademlia-like routing table to store and manage discovered peers and topics.
4//! The protocol allows for external IP discovery in NAT environments through regular PING/PONG's
5//! with discovered nodes. Nodes return the external IP address that they have received and a simple
6//! majority is chosen as our external IP address. If an external IP address is updated, this is
7//! produced as an event to notify the swarm (if one is used for this behaviour).
8//!
9//! This implementation consists of a [`Discv4`] and [`Discv4Service`] pair. The service manages the
10//! state and drives the UDP socket. The (optional) [`Discv4`] serves as the frontend to interact
11//! with the service via a channel. Whenever the underlying table changes service produces a
12//! [`DiscoveryUpdate`] that listeners will receive.
13//!
14//! ## Feature Flags
15//!
16//! - `serde` (default): Enable serde support
17//! - `test-utils`: Export utilities for testing
18
19#![doc(
20    html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
21    html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
22    issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
23)]
24#![cfg_attr(not(test), warn(unused_crate_dependencies))]
25#![cfg_attr(docsrs, feature(doc_cfg))]
26
27use crate::{
28    error::{DecodePacketError, Discv4Error},
29    proto::{FindNode, Message, Neighbours, Packet, Ping, Pong},
30};
31use alloy_primitives::{bytes::Bytes, hex, B256};
32use discv5::{
33    kbucket,
34    kbucket::{
35        BucketInsertResult, Distance, Entry as BucketEntry, InsertResult, KBucketsTable,
36        NodeStatus, MAX_NODES_PER_BUCKET,
37    },
38    ConnectionDirection, ConnectionState,
39};
40use enr::Enr;
41use itertools::Itertools;
42use parking_lot::Mutex;
43use proto::{EnrRequest, EnrResponse};
44use reth_ethereum_forks::ForkId;
45use reth_network_peers::{pk2id, PeerId};
46use secp256k1::SecretKey;
47use std::{
48    cell::RefCell,
49    collections::{btree_map, hash_map::Entry, BTreeMap, HashMap, VecDeque},
50    fmt, io,
51    net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4},
52    pin::Pin,
53    rc::Rc,
54    sync::Arc,
55    task::{ready, Context, Poll},
56    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
57};
58use tokio::{
59    net::UdpSocket,
60    sync::{mpsc, mpsc::error::TrySendError, oneshot, oneshot::Sender as OneshotSender},
61    task::{JoinHandle, JoinSet},
62    time::Interval,
63};
64use tokio_stream::{wrappers::ReceiverStream, Stream, StreamExt};
65use tracing::{debug, trace};
66
67pub mod error;
68pub mod proto;
69
70mod config;
71pub use config::{Discv4Config, Discv4ConfigBuilder};
72
73mod node;
74use node::{kad_key, NodeKey};
75
76mod table;
77
78// reexport NodeRecord primitive
79pub use reth_network_peers::NodeRecord;
80
81#[cfg(any(test, feature = "test-utils"))]
82pub mod test_utils;
83
84use crate::table::PongTable;
85use reth_net_nat::ResolveNatInterval;
86/// reexport to get public ip.
87pub use reth_net_nat::{external_ip, NatResolver};
88
89/// The default address for discv4 via UDP
90///
91/// Note: the default TCP address is the same.
92pub const DEFAULT_DISCOVERY_ADDR: IpAddr = IpAddr::V4(Ipv4Addr::UNSPECIFIED);
93
94/// The default port for discv4 via UDP
95///
96/// Note: the default TCP port is the same.
97pub const DEFAULT_DISCOVERY_PORT: u16 = 30303;
98
99/// The default address for discv4 via UDP: "0.0.0.0:30303"
100///
101/// Note: The default TCP address is the same.
102pub const DEFAULT_DISCOVERY_ADDRESS: SocketAddr =
103    SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, DEFAULT_DISCOVERY_PORT));
104
105/// The maximum size of any packet is 1280 bytes.
106const MAX_PACKET_SIZE: usize = 1280;
107
108/// Length of the UDP datagram packet-header: Hash(32b) + Signature(65b) + Packet Type(1b)
109const MIN_PACKET_SIZE: usize = 32 + 65 + 1;
110
111/// Concurrency factor for `FindNode` requests to pick `ALPHA` closest nodes, <https://github.com/ethereum/devp2p/blob/master/discv4.md#recursive-lookup>
112const ALPHA: usize = 3;
113
114/// Maximum number of nodes to ping at concurrently.
115///
116/// This corresponds to 2 full `Neighbours` responses with 16 _new_ nodes. This will apply some
117/// backpressure in recursive lookups.
118const MAX_NODES_PING: usize = 2 * MAX_NODES_PER_BUCKET;
119
120/// Maximum number of pings to keep queued.
121///
122/// If we are currently sending too many pings, any new pings will be queued. To prevent unbounded
123/// growth of the queue, the queue has a maximum capacity, after which any additional pings will be
124/// discarded.
125///
126/// This corresponds to 2 full `Neighbours` responses with 16 new nodes.
127const MAX_QUEUED_PINGS: usize = 2 * MAX_NODES_PER_BUCKET;
128
129/// The size of the datagram is limited [`MAX_PACKET_SIZE`], 16 nodes, as the discv4 specifies don't
130/// fit in one datagram. The safe number of nodes that always fit in a datagram is 12, with worst
131/// case all of them being IPv6 nodes. This is calculated by `(MAX_PACKET_SIZE - (header + expire +
132/// rlp overhead) / size(rlp(Node_IPv6))`
133/// Even in the best case where all nodes are IPv4, only 14 nodes fit into one packet.
134const SAFE_MAX_DATAGRAM_NEIGHBOUR_RECORDS: usize = (MAX_PACKET_SIZE - 109) / 91;
135
136/// The timeout used to identify expired nodes, 24h
137///
138/// Mirrors geth's `bondExpiration` of 24h
139const ENDPOINT_PROOF_EXPIRATION: Duration = Duration::from_secs(24 * 60 * 60);
140
141/// Duration used to expire nodes from the routing table 1hr
142const EXPIRE_DURATION: Duration = Duration::from_secs(60 * 60);
143
144// Restricts how many udp messages can be processed in a single [Discv4Service::poll] call.
145//
146// This will act as a manual yield point when draining the socket messages where the most CPU
147// expensive part is handling outgoing messages: encoding and hashing the packet
148const UDP_MESSAGE_POLL_LOOP_BUDGET: i32 = 4;
149
150type EgressSender = mpsc::Sender<(Bytes, SocketAddr)>;
151type EgressReceiver = mpsc::Receiver<(Bytes, SocketAddr)>;
152
153pub(crate) type IngressSender = mpsc::Sender<IngressEvent>;
154pub(crate) type IngressReceiver = mpsc::Receiver<IngressEvent>;
155
156type NodeRecordSender = OneshotSender<Vec<NodeRecord>>;
157
158/// The Discv4 frontend.
159///
160/// This is a cloneable type that communicates with the [`Discv4Service`] by sending commands over a
161/// shared channel.
162///
163/// See also [`Discv4::spawn`]
164#[derive(Debug, Clone)]
165pub struct Discv4 {
166    /// The address of the udp socket
167    local_addr: SocketAddr,
168    /// channel to send commands over to the service
169    to_service: mpsc::UnboundedSender<Discv4Command>,
170    /// Tracks the local node record.
171    ///
172    /// This includes the currently tracked external IP address of the node.
173    node_record: Arc<Mutex<NodeRecord>>,
174}
175
176impl Discv4 {
177    /// Same as [`Self::bind`] but also spawns the service onto a new task.
178    ///
179    /// See also: [`Discv4Service::spawn()`]
180    pub async fn spawn(
181        local_address: SocketAddr,
182        local_enr: NodeRecord,
183        secret_key: SecretKey,
184        config: Discv4Config,
185    ) -> io::Result<Self> {
186        let (discv4, service) = Self::bind(local_address, local_enr, secret_key, config).await?;
187
188        service.spawn();
189
190        Ok(discv4)
191    }
192
193    /// Returns a new instance with the given channel directly
194    ///
195    /// NOTE: this is only intended for test setups.
196    #[cfg(feature = "test-utils")]
197    pub fn noop() -> Self {
198        let (to_service, _rx) = mpsc::unbounded_channel();
199        let local_addr =
200            (IpAddr::from(std::net::Ipv4Addr::UNSPECIFIED), DEFAULT_DISCOVERY_PORT).into();
201        Self {
202            local_addr,
203            to_service,
204            node_record: Arc::new(Mutex::new(NodeRecord::new(
205                "127.0.0.1:3030".parse().unwrap(),
206                PeerId::random(),
207            ))),
208        }
209    }
210
211    /// Binds a new `UdpSocket` and creates the service
212    ///
213    /// ```
214    /// use reth_discv4::{Discv4, Discv4Config};
215    /// use reth_network_peers::{pk2id, NodeRecord, PeerId};
216    /// use secp256k1::SECP256K1;
217    /// use std::{net::SocketAddr, str::FromStr};
218    /// # async fn t() -> std:: io::Result<()> {
219    ///
220    /// // generate a (random) keypair
221    /// let (secret_key, pk) = SECP256K1.generate_keypair(&mut rand_08::thread_rng());
222    /// let id = pk2id(&pk);
223    ///
224    /// let socket = SocketAddr::from_str("0.0.0.0:0").unwrap();
225    /// let local_enr =
226    ///     NodeRecord { address: socket.ip(), tcp_port: socket.port(), udp_port: socket.port(), id };
227    /// let config = Discv4Config::default();
228    ///
229    /// let (discv4, mut service) = Discv4::bind(socket, local_enr, secret_key, config).await.unwrap();
230    ///
231    /// // get an update strea
232    /// let updates = service.update_stream();
233    ///
234    /// let _handle = service.spawn();
235    ///
236    /// // lookup the local node in the DHT
237    /// let _discovered = discv4.lookup_self().await.unwrap();
238    ///
239    /// # Ok(())
240    /// # }
241    /// ```
242    pub async fn bind(
243        local_address: SocketAddr,
244        local_node_record: NodeRecord,
245        secret_key: SecretKey,
246        config: Discv4Config,
247    ) -> io::Result<(Self, Discv4Service)> {
248        let socket = Arc::new(UdpSocket::bind(local_address).await?);
249        trace!(target: "discv4", local_addr=?socket.local_addr(), "opened UDP socket");
250        let (tx, rx) = mpsc::channel(config.udp_ingress_message_buffer);
251
252        Self::bind_with_socket(socket, Some(tx), rx, local_node_record, secret_key, config)
253    }
254
255    /// Creates a new `Discv4` instance using a pre-bound shared socket. No receive loop is
256    /// spawned; instead returns an [`IngressHandler`] that should be used to forward raw packets
257    /// received by the socket owner (e.g. discv5 unrecognized frames).
258    pub fn bind_shared(
259        socket: Arc<UdpSocket>,
260        local_node_record: NodeRecord,
261        secret_key: SecretKey,
262        config: Discv4Config,
263    ) -> io::Result<(Self, Discv4Service, IngressHandler)> {
264        let (tx, rx) = mpsc::channel(config.udp_ingress_message_buffer);
265        let local_id = local_node_record.id;
266        let (discv4, service) =
267            Self::bind_with_socket(socket, None, rx, local_node_record, secret_key, config)?;
268
269        let handler = IngressHandler::new(tx, local_id);
270
271        Ok((discv4, service, handler))
272    }
273
274    fn bind_with_socket(
275        socket: Arc<UdpSocket>,
276        ingress_tx: Option<IngressSender>,
277        ingress_rx: IngressReceiver,
278        mut local_node_record: NodeRecord,
279        secret_key: SecretKey,
280        config: Discv4Config,
281    ) -> io::Result<(Self, Discv4Service)> {
282        let local_addr = socket.local_addr()?;
283        local_node_record.udp_port = local_addr.port();
284
285        let mut service = Discv4Service::new(
286            socket,
287            ingress_tx,
288            ingress_rx,
289            local_addr,
290            local_node_record,
291            secret_key,
292            config,
293        );
294
295        // resolve the external address immediately
296        service.resolve_external_ip();
297
298        let discv4 = service.handle();
299        Ok((discv4, service))
300    }
301
302    /// Returns the address of the UDP socket.
303    pub const fn local_addr(&self) -> SocketAddr {
304        self.local_addr
305    }
306
307    /// Returns the [`NodeRecord`] of the local node.
308    ///
309    /// This includes the currently tracked external IP address of the node.
310    pub fn node_record(&self) -> NodeRecord {
311        *self.node_record.lock()
312    }
313
314    /// Returns the currently tracked external IP of the node.
315    pub fn external_ip(&self) -> IpAddr {
316        self.node_record.lock().address
317    }
318
319    /// Sets the [Interval] used for periodically looking up targets over the network
320    pub fn set_lookup_interval(&self, duration: Duration) {
321        self.send_to_service(Discv4Command::SetLookupInterval(duration))
322    }
323
324    /// Starts a `FindNode` recursive lookup that locates the closest nodes to the given node id. See also: <https://github.com/ethereum/devp2p/blob/master/discv4.md#recursive-lookup>
325    ///
326    /// The lookup initiator starts by picking α closest nodes to the target it knows of. The
327    /// initiator then sends concurrent `FindNode` packets to those nodes. α is a system-wide
328    /// concurrency parameter, such as 3. In the recursive step, the initiator resends `FindNode` to
329    /// nodes it has learned about from previous queries. Of the k nodes the initiator has heard of
330    /// closest to the target, it picks α that it has not yet queried and resends `FindNode` to
331    /// them. Nodes that fail to respond quickly are removed from consideration until and unless
332    /// they do respond.
333    //
334    // If a round of FindNode queries fails to return a node any closer than the closest already
335    // seen, the initiator resends the find node to all of the k closest nodes it has not already
336    // queried. The lookup terminates when the initiator has queried and gotten responses from the k
337    // closest nodes it has seen.
338    pub async fn lookup_self(&self) -> Result<Vec<NodeRecord>, Discv4Error> {
339        self.lookup_node(None).await
340    }
341
342    /// Looks up the given node id.
343    ///
344    /// Returning the closest nodes to the given node id.
345    pub async fn lookup(&self, node_id: PeerId) -> Result<Vec<NodeRecord>, Discv4Error> {
346        self.lookup_node(Some(node_id)).await
347    }
348
349    /// Performs a random lookup for node records.
350    pub async fn lookup_random(&self) -> Result<Vec<NodeRecord>, Discv4Error> {
351        let target = PeerId::random();
352        self.lookup_node(Some(target)).await
353    }
354
355    /// Sends a message to the service to lookup the closest nodes
356    pub fn send_lookup(&self, node_id: PeerId) {
357        let cmd = Discv4Command::Lookup { node_id: Some(node_id), tx: None };
358        self.send_to_service(cmd);
359    }
360
361    async fn lookup_node(&self, node_id: Option<PeerId>) -> Result<Vec<NodeRecord>, Discv4Error> {
362        let (tx, rx) = oneshot::channel();
363        let cmd = Discv4Command::Lookup { node_id, tx: Some(tx) };
364        self.to_service.send(cmd)?;
365        Ok(rx.await?)
366    }
367
368    /// Triggers a new self lookup without expecting a response
369    pub fn send_lookup_self(&self) {
370        let cmd = Discv4Command::Lookup { node_id: None, tx: None };
371        self.send_to_service(cmd);
372    }
373
374    /// Removes the peer from the table, if it exists.
375    pub fn remove_peer(&self, node_id: PeerId) {
376        let cmd = Discv4Command::Remove(node_id);
377        self.send_to_service(cmd);
378    }
379
380    /// Adds the node to the table, if it is not already present.
381    pub fn add_node(&self, node_record: NodeRecord) {
382        let cmd = Discv4Command::Add(node_record);
383        self.send_to_service(cmd);
384    }
385
386    /// Adds the node as a bootnode.
387    ///
388    /// This registers the node in the configured bootstrap set and inserts it into the routing
389    /// table, pinging it to establish the endpoint proof, same as the nodes provided at startup.
390    pub fn add_boot_node(&self, node_record: NodeRecord) {
391        let cmd = Discv4Command::AddBootNode(node_record);
392        self.send_to_service(cmd);
393    }
394
395    /// Adds the peer and id to the ban list.
396    ///
397    /// This will prevent any future inclusion in the table
398    pub fn ban(&self, node_id: PeerId, ip: IpAddr) {
399        let cmd = Discv4Command::Ban(node_id, ip);
400        self.send_to_service(cmd);
401    }
402
403    /// Adds the ip to the ban list.
404    ///
405    /// This will prevent any future inclusion in the table
406    pub fn ban_ip(&self, ip: IpAddr) {
407        let cmd = Discv4Command::BanIp(ip);
408        self.send_to_service(cmd);
409    }
410
411    /// Adds the peer to the ban list.
412    ///
413    /// This will prevent any future inclusion in the table
414    pub fn ban_node(&self, node_id: PeerId) {
415        let cmd = Discv4Command::BanPeer(node_id);
416        self.send_to_service(cmd);
417    }
418
419    /// Sets the tcp port
420    ///
421    /// This will update our [`NodeRecord`]'s tcp port.
422    pub fn set_tcp_port(&self, port: u16) {
423        let cmd = Discv4Command::SetTcpPort(port);
424        self.send_to_service(cmd);
425    }
426
427    /// Sets the pair in the EIP-868 [`Enr`] of the node.
428    ///
429    /// If the key already exists, this will update it.
430    ///
431    /// CAUTION: The value **must** be rlp encoded
432    pub fn set_eip868_rlp_pair(&self, key: Vec<u8>, rlp: Bytes) {
433        let cmd = Discv4Command::SetEIP868RLPPair { key, rlp };
434        self.send_to_service(cmd);
435    }
436
437    /// Sets the pair in the EIP-868 [`Enr`] of the node.
438    ///
439    /// If the key already exists, this will update it.
440    pub fn set_eip868_rlp(&self, key: Vec<u8>, value: impl alloy_rlp::Encodable) {
441        self.set_eip868_rlp_pair(key, Bytes::from(alloy_rlp::encode(&value)))
442    }
443
444    #[inline]
445    fn send_to_service(&self, cmd: Discv4Command) {
446        let _ = self.to_service.send(cmd).map_err(|err| {
447            debug!(
448                target: "discv4",
449                %err,
450                "channel capacity reached, dropping command",
451            )
452        });
453    }
454
455    /// Returns the receiver half of new listener channel that streams [`DiscoveryUpdate`]s.
456    pub async fn update_stream(&self) -> Result<ReceiverStream<DiscoveryUpdate>, Discv4Error> {
457        let (tx, rx) = oneshot::channel();
458        let cmd = Discv4Command::Updates(tx);
459        self.to_service.send(cmd)?;
460        Ok(rx.await?)
461    }
462
463    /// Terminates the spawned [`Discv4Service`].
464    pub fn terminate(&self) {
465        self.send_to_service(Discv4Command::Terminated);
466    }
467}
468
469/// Manages discv4 peer discovery over UDP.
470///
471/// This is a [Stream] to handles incoming and outgoing discv4 messages and emits updates via:
472/// [`Discv4Service::update_stream`].
473///
474/// This type maintains the discv Kademlia routing table and is responsible for performing lookups.
475///
476/// ## Lookups
477///
478/// See also [Recursive Lookups](https://github.com/ethereum/devp2p/blob/master/discv4.md#recursive-lookup).
479/// Lookups are either triggered periodically or performaned on demand: [`Discv4::lookup`]
480/// Newly discovered nodes are emitted as [`DiscoveryUpdate::Added`] event to all subscribers:
481/// [`Discv4Service::update_stream`].
482#[must_use = "Stream does nothing unless polled"]
483pub struct Discv4Service {
484    /// Local address of the UDP socket.
485    local_address: SocketAddr,
486    /// The local ENR for EIP-868 <https://eips.ethereum.org/EIPS/eip-868>
487    local_eip_868_enr: Enr<SecretKey>,
488    /// Local ENR of the server.
489    local_node_record: NodeRecord,
490    /// Keeps track of the node record of the local node.
491    shared_node_record: Arc<Mutex<NodeRecord>>,
492    /// The secret key used to sign payloads
493    secret_key: SecretKey,
494    /// The UDP socket for sending and receiving messages.
495    _socket: Arc<UdpSocket>,
496    /// The spawned UDP tasks.
497    ///
498    /// Note: If dropped, the spawned send+receive tasks are aborted.
499    _tasks: JoinSet<()>,
500    /// The routing table.
501    kbuckets: KBucketsTable<NodeKey, NodeEntry>,
502    /// Receiver for incoming messages
503    ///
504    /// Receives incoming messages from the UDP task.
505    ingress: IngressReceiver,
506    /// Sender for sending outgoing messages
507    ///
508    /// Sends outgoing messages to the UDP task.
509    egress: EgressSender,
510    /// Buffered pending pings to apply backpressure.
511    ///
512    /// Lookups behave like bursts of requests: Endpoint proof followed by `FindNode` request. [Recursive lookups](https://github.com/ethereum/devp2p/blob/master/discv4.md#recursive-lookup) can trigger multiple followup Pings+FindNode requests.
513    /// A cap on concurrent `Ping` prevents escalation where: A large number of new nodes
514    /// discovered via `FindNode` in a recursive lookup triggers a large number of `Ping`s, and
515    /// followup `FindNode` requests.... Buffering them effectively prevents high `Ping` peaks.
516    queued_pings: VecDeque<(NodeRecord, PingReason)>,
517    /// Currently active pings to specific nodes.
518    pending_pings: HashMap<PeerId, PingRequest>,
519    /// Currently active endpoint proof verification lookups to specific nodes.
520    ///
521    /// Entries here means we've proven the peer's endpoint but haven't completed our end of the
522    /// endpoint proof
523    pending_lookup: HashMap<PeerId, (Instant, LookupContext)>,
524    /// Currently active `FindNode` requests
525    pending_find_nodes: HashMap<PeerId, FindNodeRequest>,
526    /// Currently active ENR requests
527    pending_enr_requests: HashMap<PeerId, EnrRequestState>,
528    /// Copy of the sender half of the commands channel for [Discv4]
529    to_service: mpsc::UnboundedSender<Discv4Command>,
530    /// Receiver half of the commands channel for [Discv4]
531    commands_rx: mpsc::UnboundedReceiver<Discv4Command>,
532    /// All subscribers for table updates
533    update_listeners: Vec<mpsc::Sender<DiscoveryUpdate>>,
534    /// The interval when to trigger random lookups
535    lookup_interval: Interval,
536    /// Used to rotate targets to lookup
537    lookup_rotator: LookupTargetRotator,
538    /// Whether we still need to reset the lookup interval on the first bootnode pong.
539    pending_lookup_reset: bool,
540    /// Interval when to recheck active requests
541    evict_expired_requests_interval: Interval,
542    /// Interval when to resend pings.
543    ping_interval: Interval,
544    /// The interval at which to attempt resolving external IP again.
545    resolve_external_ip_interval: Option<ResolveNatInterval>,
546    /// How this services is configured
547    config: Discv4Config,
548    /// Buffered events populated during poll.
549    queued_events: VecDeque<Discv4Event>,
550    /// Keeps track of nodes from which we have received a `Pong` message.
551    received_pongs: PongTable,
552    /// Interval used to expire additionally tracked nodes
553    expire_interval: Interval,
554    /// Cached signed `FindNode` packet to avoid redundant ECDSA signing during lookups.
555    cached_find_node: Option<CachedFindNode>,
556}
557
558impl Discv4Service {
559    /// Create a new instance for a bound [`UdpSocket`].
560    ///
561    /// If `ingress_tx` is `Some`, the receive loop is spawned to read from the socket. If `None`,
562    /// the caller feeds packets into `ingress_rx` externally (shared socket mode).
563    pub(crate) fn new(
564        socket: Arc<UdpSocket>,
565        ingress_tx: Option<IngressSender>,
566        ingress_rx: IngressReceiver,
567        local_address: SocketAddr,
568        local_node_record: NodeRecord,
569        secret_key: SecretKey,
570        config: Discv4Config,
571    ) -> Self {
572        let (egress_tx, egress_rx) = mpsc::channel(config.udp_egress_message_buffer);
573        let mut tasks = JoinSet::<()>::new();
574
575        if let Some(ingress_tx) = ingress_tx {
576            let udp = Arc::clone(&socket);
577            tasks.spawn(receive_loop(udp, ingress_tx, local_node_record.id));
578        }
579
580        let udp = Arc::clone(&socket);
581        tasks.spawn(send_loop(udp, egress_rx));
582
583        let kbuckets = KBucketsTable::new(
584            NodeKey::from(&local_node_record).into(),
585            Duration::from_secs(60),
586            MAX_NODES_PER_BUCKET,
587            None,
588            None,
589        );
590
591        let self_lookup_interval = tokio::time::interval(config.lookup_interval);
592
593        // Wait `ping_interval` and then start pinging every `ping_interval` because we want to wait
594        // for
595        let ping_interval = tokio::time::interval_at(
596            tokio::time::Instant::now() + config.ping_interval,
597            config.ping_interval,
598        );
599
600        let evict_expired_requests_interval = tokio::time::interval_at(
601            tokio::time::Instant::now() + config.request_timeout,
602            config.request_timeout,
603        );
604
605        let lookup_rotator = if config.enable_dht_random_walk {
606            LookupTargetRotator::default()
607        } else {
608            LookupTargetRotator::local_only()
609        };
610
611        // for EIP-868 construct an ENR
612        let local_eip_868_enr = {
613            let mut builder = Enr::builder();
614            builder.ip(local_node_record.address);
615            if local_node_record.address.is_ipv4() {
616                builder.udp4(local_node_record.udp_port);
617                builder.tcp4(local_node_record.tcp_port);
618            } else {
619                builder.udp6(local_node_record.udp_port);
620                builder.tcp6(local_node_record.tcp_port);
621            }
622
623            for (key, val) in &config.additional_eip868_rlp_pairs {
624                builder.add_value_rlp(key, val.clone());
625            }
626            builder.build(&secret_key).expect("v4 is set")
627        };
628
629        let (to_service, commands_rx) = mpsc::unbounded_channel();
630
631        let shared_node_record = Arc::new(Mutex::new(local_node_record));
632
633        Self {
634            local_address,
635            local_eip_868_enr,
636            local_node_record,
637            shared_node_record,
638            _socket: socket,
639            kbuckets,
640            secret_key,
641            _tasks: tasks,
642            ingress: ingress_rx,
643            egress: egress_tx,
644            queued_pings: VecDeque::with_capacity(MAX_QUEUED_PINGS),
645            pending_pings: Default::default(),
646            pending_lookup: Default::default(),
647            pending_find_nodes: Default::default(),
648            pending_enr_requests: Default::default(),
649            commands_rx,
650            to_service,
651            update_listeners: Vec::with_capacity(1),
652            lookup_interval: self_lookup_interval,
653            ping_interval,
654            evict_expired_requests_interval,
655            lookup_rotator,
656            pending_lookup_reset: config.enable_lookup,
657            resolve_external_ip_interval: config.resolve_external_ip_interval(),
658            config,
659            queued_events: Default::default(),
660            received_pongs: Default::default(),
661            expire_interval: tokio::time::interval(EXPIRE_DURATION),
662            cached_find_node: None,
663        }
664    }
665
666    /// Returns the frontend handle that can communicate with the service via commands.
667    pub fn handle(&self) -> Discv4 {
668        Discv4 {
669            local_addr: self.local_address,
670            to_service: self.to_service.clone(),
671            node_record: self.shared_node_record.clone(),
672        }
673    }
674
675    /// Returns the current enr sequence of the local record.
676    fn enr_seq(&self) -> Option<u64> {
677        self.config.enable_eip868.then(|| self.local_eip_868_enr.seq())
678    }
679
680    /// Sets the [Interval] used for periodically looking up targets over the network
681    pub fn set_lookup_interval(&mut self, duration: Duration) {
682        self.lookup_interval = tokio::time::interval(duration);
683    }
684
685    /// Sets the external Ip to the configured external IP if [`NatResolver::ExternalIp`] or
686    /// [`NatResolver::ExternalAddr`]. In the case of [`NatResolver::ExternalAddr`], it will return
687    /// the first IP address found for the domain associated with the discv4 UDP port.
688    fn resolve_external_ip(&mut self) {
689        if let Some(r) = &self.resolve_external_ip_interval &&
690            let Some(external_ip) =
691                r.resolver().clone().as_external_ip(self.local_node_record.udp_port)
692        {
693            self.set_external_ip_addr(external_ip);
694        }
695    }
696
697    /// Sets the given ip address as the node's external IP in the node record announced in
698    /// discovery
699    pub fn set_external_ip_addr(&mut self, external_ip: IpAddr) {
700        if self.local_node_record.address != external_ip {
701            debug!(target: "discv4", ?external_ip, "Updating external ip");
702            self.local_node_record.address = external_ip;
703            let _ = self.local_eip_868_enr.set_ip(external_ip, &self.secret_key);
704            let mut lock = self.shared_node_record.lock();
705            *lock = self.local_node_record;
706            debug!(target: "discv4", enr=?self.local_eip_868_enr, "Updated local ENR");
707        }
708    }
709
710    /// Returns the [`PeerId`] that identifies this node
711    pub const fn local_peer_id(&self) -> &PeerId {
712        &self.local_node_record.id
713    }
714
715    /// Returns the address of the UDP socket
716    pub const fn local_addr(&self) -> SocketAddr {
717        self.local_address
718    }
719
720    /// Returns the ENR of this service.
721    ///
722    /// Note: this will include the external address if resolved.
723    pub const fn local_enr(&self) -> NodeRecord {
724        self.local_node_record
725    }
726
727    /// Returns mutable reference to ENR for testing.
728    #[cfg(test)]
729    pub const fn local_enr_mut(&mut self) -> &mut NodeRecord {
730        &mut self.local_node_record
731    }
732
733    /// Returns true if the given `PeerId` is currently in the bucket
734    pub fn contains_node(&self, id: PeerId) -> bool {
735        let key = kad_key(id);
736        self.kbuckets.get_index(&key).is_some()
737    }
738
739    /// Bootstraps the local node to join the DHT.
740    ///
741    /// Bootstrapping is a multi-step operation that starts with a lookup of the local node's
742    /// own ID in the DHT. This introduces the local node to the other nodes
743    /// in the DHT and populates its routing table with the closest proven neighbours.
744    ///
745    /// This inserts the configured bootnodes into the routing table and pings them. Once the
746    /// endpoint proof succeeds (pong received), a [`DiscoveryUpdate::Added`] event is emitted,
747    /// same as with [`Self::add_node`].
748    ///
749    /// **Note:** This is a noop if there are no bootnodes.
750    pub fn bootstrap(&mut self) {
751        for record in self.config.bootstrap_nodes.clone() {
752            debug!(target: "discv4", ?record, "pinging boot node");
753            let key = kad_key(record.id);
754            let entry = NodeEntry::new(record);
755
756            // insert the boot node in the table
757            match self.kbuckets.insert_or_update(
758                &key,
759                entry,
760                NodeStatus {
761                    state: ConnectionState::Disconnected,
762                    direction: ConnectionDirection::Outgoing,
763                },
764            ) {
765                InsertResult::Failed(_) => {}
766                _ => {
767                    self.try_ping(record, PingReason::InitialInsert);
768                }
769            }
770        }
771    }
772
773    /// Adds the node to the bootstrap set and to the routing table.
774    ///
775    /// Behaves like [`Self::add_node`] but also registers the node in the configured bootstrap
776    /// set so it is used for subsequent bootstrap attempts.
777    pub fn add_boot_node(&mut self, record: NodeRecord) -> bool {
778        self.config.bootstrap_nodes.insert(record);
779        self.add_node(record)
780    }
781
782    /// Spawns this services onto a new task
783    ///
784    /// Note: requires a running tokio runtime
785    pub fn spawn(mut self) -> JoinHandle<()> {
786        tokio::task::spawn(async move {
787            self.bootstrap();
788
789            while let Some(event) = self.next().await {
790                trace!(target: "discv4", ?event, "processed");
791            }
792            trace!(target: "discv4", "service terminated");
793        })
794    }
795
796    /// Creates a new bounded channel for [`DiscoveryUpdate`]s.
797    pub fn update_stream(&mut self) -> ReceiverStream<DiscoveryUpdate> {
798        let (tx, rx) = mpsc::channel(512);
799        self.update_listeners.push(tx);
800        ReceiverStream::new(rx)
801    }
802
803    /// Looks up the local node in the DHT.
804    pub fn lookup_self(&mut self) {
805        self.lookup(self.local_node_record.id)
806    }
807
808    /// Looks up the given node in the DHT
809    ///
810    /// A `FindNode` packet requests information about nodes close to target. The target is a
811    /// 64-byte secp256k1 public key. When `FindNode` is received, the recipient should reply
812    /// with Neighbors packets containing the closest 16 nodes to target found in its local
813    /// table.
814    //
815    // To guard against traffic amplification attacks, Neighbors replies should only be sent if the
816    // sender of FindNode has been verified by the endpoint proof procedure.
817    pub fn lookup(&mut self, target: PeerId) {
818        self.lookup_with(target, None)
819    }
820
821    /// Starts the recursive lookup process for the given target, <https://github.com/ethereum/devp2p/blob/master/discv4.md#recursive-lookup>.
822    ///
823    /// At first the `ALPHA` (==3, defined concurrency factor) nodes that are closest to the target
824    /// in the underlying DHT are selected to seed the lookup via `FindNode` requests. In the
825    /// recursive step, the initiator resends `FindNode` to nodes it has learned about from previous
826    /// queries.
827    ///
828    /// This takes an optional Sender through which all successfully discovered nodes are sent once
829    /// the request has finished.
830    fn lookup_with(&mut self, target: PeerId, tx: Option<NodeRecordSender>) {
831        trace!(target: "discv4", ?target, "Starting lookup");
832        let target_key = kad_key(target);
833
834        // Start a lookup context with the 16 (MAX_NODES_PER_BUCKET) closest nodes to which we have
835        // a valid endpoint proof
836        let ctx = LookupContext::new(
837            target_key.clone(),
838            self.kbuckets
839                .closest_values(&target_key)
840                .filter(|node| {
841                    node.value.has_endpoint_proof &&
842                        !self.pending_find_nodes.contains_key(&node.key.preimage().0)
843                })
844                .take(MAX_NODES_PER_BUCKET)
845                .map(|n| (target_key.distance(&n.key), n.value.record)),
846            tx,
847        );
848
849        // From those 16, pick the 3 closest to start the concurrent lookup.
850        let closest = ctx.closest(ALPHA);
851
852        if closest.is_empty() && self.pending_find_nodes.is_empty() {
853            // no closest nodes, and no lookup in progress: table is empty.
854            // This could happen if all records were deleted from the table due to missed pongs
855            // (e.g. connectivity problems over a long period of time, or issues during initial
856            // bootstrapping) so we attempt to bootstrap again
857            self.bootstrap();
858            return
859        }
860
861        trace!(target: "discv4", ?target, num = closest.len(), "Start lookup closest nodes");
862
863        for node in closest {
864            // here we still want to check against previous request failures and if necessary
865            // re-establish a new endpoint proof because it can be the case that the other node lost
866            // our entry and no longer has an endpoint proof on their end
867            self.find_node_checked(&node, ctx.clone());
868        }
869    }
870
871    /// Sends a new `FindNode` packet to the node with `target` as the lookup target.
872    ///
873    /// CAUTION: This expects there's a valid Endpoint proof to the given `node`.
874    fn find_node(&mut self, node: &NodeRecord, ctx: LookupContext) {
875        trace!(target: "discv4", ?node, lookup=?ctx.target(), "Sending FindNode");
876        ctx.mark_queried(node.id);
877        let (payload, hash) = self.find_node_packet(ctx.target());
878        let to = node.udp_addr();
879        trace!(target: "discv4", ?to, ?hash, "sending FindNode packet");
880        let _ = self.egress.try_send((payload, to)).map_err(|err| {
881            debug!(target: "discv4", %err, "dropped outgoing packet");
882        });
883        self.pending_find_nodes.insert(node.id, FindNodeRequest::new(ctx));
884    }
885
886    /// Sends a new `FindNode` packet to the node with `target` as the lookup target but checks
887    /// whether we should send a new ping first to renew the endpoint proof by checking the
888    /// previously failed findNode requests. It could be that the node is no longer reachable or
889    /// lost our entry.
890    fn find_node_checked(&mut self, node: &NodeRecord, ctx: LookupContext) {
891        let max_failures = self.config.max_find_node_failures;
892        let needs_ping = self
893            .on_entry(node.id, |entry| entry.exceeds_find_node_failures(max_failures))
894            .unwrap_or(true);
895        if needs_ping {
896            self.try_ping(*node, PingReason::Lookup(*node, ctx))
897        } else {
898            self.find_node(node, ctx)
899        }
900    }
901
902    /// Notifies all listeners.
903    ///
904    /// Removes all listeners that are closed.
905    fn notify(&mut self, update: DiscoveryUpdate) {
906        self.update_listeners.retain_mut(|listener| match listener.try_send(update.clone()) {
907            Ok(()) => true,
908            Err(err) => match err {
909                TrySendError::Full(_) => true,
910                TrySendError::Closed(_) => false,
911            },
912        });
913    }
914
915    /// Adds the ip to the ban list indefinitely
916    pub fn ban_ip(&mut self, ip: IpAddr) {
917        self.config.ban_list.ban_ip(ip);
918    }
919
920    /// Adds the peer to the ban list indefinitely.
921    pub fn ban_node(&mut self, node_id: PeerId) {
922        self.remove_node(node_id);
923        self.config.ban_list.ban_peer(node_id);
924    }
925
926    /// Adds the ip to the ban list until the given timestamp.
927    pub fn ban_ip_until(&mut self, ip: IpAddr, until: Instant) {
928        self.config.ban_list.ban_ip_until(ip, until);
929    }
930
931    /// Adds the peer to the ban list and bans it until the given timestamp
932    pub fn ban_node_until(&mut self, node_id: PeerId, until: Instant) {
933        self.remove_node(node_id);
934        self.config.ban_list.ban_peer_until(node_id, until);
935    }
936
937    /// Removes a `node_id` from the routing table.
938    ///
939    /// This allows applications, for whatever reason, to remove nodes from the local routing
940    /// table. Returns `true` if the node was in the table and `false` otherwise.
941    pub fn remove_node(&mut self, node_id: PeerId) -> bool {
942        let key = kad_key(node_id);
943        self.remove_key(node_id, key)
944    }
945
946    /// Removes a `node_id` from the routing table but only if there are enough other nodes in the
947    /// bucket (bucket must be at least half full)
948    ///
949    /// Returns `true` if the node was removed
950    pub fn soft_remove_node(&mut self, node_id: PeerId) -> bool {
951        let key = kad_key(node_id);
952        let Some(bucket) = self.kbuckets.get_bucket(&key) else { return false };
953        if bucket.num_entries() < MAX_NODES_PER_BUCKET / 2 {
954            // skip half empty bucket
955            return false
956        }
957        self.remove_key(node_id, key)
958    }
959
960    fn remove_key(&mut self, node_id: PeerId, key: discv5::Key<NodeKey>) -> bool {
961        let removed = self.kbuckets.remove(&key);
962        if removed {
963            trace!(target: "discv4", ?node_id, "removed node");
964            self.notify(DiscoveryUpdate::Removed(node_id));
965        }
966        removed
967    }
968
969    /// Gets the number of entries that are considered connected.
970    pub fn num_connected(&self) -> usize {
971        self.kbuckets.buckets_iter().fold(0, |count, bucket| count + bucket.num_connected())
972    }
973
974    /// Check if the peer has an active bond.
975    fn has_bond(&self, remote_id: PeerId, remote_ip: IpAddr) -> bool {
976        if let Some(timestamp) = self.received_pongs.last_pong(remote_id, remote_ip) &&
977            timestamp.elapsed() < self.config.bond_expiration
978        {
979            return true
980        }
981        false
982    }
983
984    /// Applies a closure on the pending or present [`NodeEntry`].
985    fn on_entry<F, R>(&mut self, peer_id: PeerId, f: F) -> Option<R>
986    where
987        F: FnOnce(&NodeEntry) -> R,
988    {
989        let key = kad_key(peer_id);
990        match self.kbuckets.entry(&key) {
991            BucketEntry::Present(entry, _) => Some(f(entry.value())),
992            BucketEntry::Pending(entry, _) => Some(f(entry.value())),
993            _ => None,
994        }
995    }
996
997    /// Update the entry on RE-ping.
998    ///
999    /// Invoked when we received the Pong to our [`PingReason::RePing`] ping.
1000    ///
1001    /// On re-ping we check for a changed `enr_seq` if eip868 is enabled and when it changed we sent
1002    /// a followup request to retrieve the updated ENR
1003    fn update_on_reping(&mut self, record: NodeRecord, mut last_enr_seq: Option<u64>) {
1004        if record.id == self.local_node_record.id {
1005            return
1006        }
1007
1008        // If EIP868 extension is disabled then we want to ignore this
1009        if !self.config.enable_eip868 {
1010            last_enr_seq = None;
1011        }
1012
1013        let key = kad_key(record.id);
1014        let old_enr = match self.kbuckets.entry(&key) {
1015            kbucket::Entry::Present(mut entry, _) => {
1016                entry.value_mut().update_with_enr(last_enr_seq)
1017            }
1018            kbucket::Entry::Pending(mut entry, _) => {
1019                entry.value_mut().update_with_enr(last_enr_seq)
1020            }
1021            _ => return,
1022        };
1023
1024        // Check if ENR was updated
1025        match (last_enr_seq, old_enr) {
1026            (Some(new), Some(old)) if new > old => {
1027                self.send_enr_request(record);
1028            }
1029            (Some(_), None) => {
1030                // got an ENR
1031                self.send_enr_request(record);
1032            }
1033            _ => {}
1034        };
1035    }
1036
1037    /// Callback invoked when we receive a pong from the peer.
1038    fn update_on_pong(&mut self, record: NodeRecord, mut last_enr_seq: Option<u64>) {
1039        if record.id == *self.local_peer_id() {
1040            return
1041        }
1042
1043        // If EIP868 extension is disabled then we want to ignore this
1044        if !self.config.enable_eip868 {
1045            last_enr_seq = None;
1046        }
1047
1048        // if the peer included a enr seq in the pong then we can try to request the ENR of that
1049        // node
1050        let has_enr_seq = last_enr_seq.is_some();
1051
1052        let key = kad_key(record.id);
1053        match self.kbuckets.entry(&key) {
1054            kbucket::Entry::Present(mut entry, old_status) => {
1055                // endpoint is now proven
1056                entry.value_mut().establish_proof();
1057                entry.value_mut().update_with_enr(last_enr_seq);
1058
1059                if !old_status.is_connected() {
1060                    let _ = entry.update(ConnectionState::Connected, Some(old_status.direction));
1061                    trace!(target: "discv4", ?record, "added after successful endpoint proof");
1062                    self.notify(DiscoveryUpdate::Added(record));
1063
1064                    if has_enr_seq {
1065                        // request the ENR of the node
1066                        self.send_enr_request(record);
1067                    }
1068                }
1069            }
1070            kbucket::Entry::Pending(mut entry, mut status) => {
1071                // endpoint is now proven
1072                entry.value_mut().establish_proof();
1073                entry.value_mut().update_with_enr(last_enr_seq);
1074
1075                if !status.is_connected() {
1076                    status.state = ConnectionState::Connected;
1077                    let _ = entry.update(status);
1078                    trace!(target: "discv4", ?record, "added after successful endpoint proof");
1079                    self.notify(DiscoveryUpdate::Added(record));
1080
1081                    if has_enr_seq {
1082                        // request the ENR of the node
1083                        self.send_enr_request(record);
1084                    }
1085                }
1086            }
1087            _ => {}
1088        };
1089    }
1090
1091    /// Adds all nodes
1092    ///
1093    /// See [`Self::add_node`]
1094    pub fn add_all_nodes(&mut self, records: impl IntoIterator<Item = NodeRecord>) {
1095        for record in records {
1096            self.add_node(record);
1097        }
1098    }
1099
1100    /// If the node's not in the table yet, this will add it to the table and start the endpoint
1101    /// proof by sending a ping to the node.
1102    ///
1103    /// Returns `true` if the record was added successfully, and `false` if the node is either
1104    /// already in the table or the record's bucket is full.
1105    pub fn add_node(&mut self, record: NodeRecord) -> bool {
1106        let key = kad_key(record.id);
1107        match self.kbuckets.entry(&key) {
1108            kbucket::Entry::Absent(entry) => {
1109                let node = NodeEntry::new(record);
1110                match entry.insert(
1111                    node,
1112                    NodeStatus {
1113                        direction: ConnectionDirection::Outgoing,
1114                        state: ConnectionState::Disconnected,
1115                    },
1116                ) {
1117                    BucketInsertResult::Inserted | BucketInsertResult::Pending { .. } => {
1118                        trace!(target: "discv4", ?record, "inserted new record");
1119                    }
1120                    _ => return false,
1121                }
1122            }
1123            _ => return false,
1124        }
1125
1126        // send the initial ping to the _new_ node
1127        self.try_ping(record, PingReason::InitialInsert);
1128        true
1129    }
1130
1131    /// Encodes the packet, sends it and returns the hash.
1132    pub(crate) fn send_packet(&self, msg: Message, to: SocketAddr) -> B256 {
1133        let (payload, hash) = msg.encode(&self.secret_key);
1134        trace!(target: "discv4", r#type=?msg.msg_type(), ?to, ?hash, "sending packet");
1135        let _ = self.egress.try_send((payload, to)).map_err(|err| {
1136            debug!(
1137                target: "discv4",
1138                %err,
1139                "dropped outgoing packet",
1140            );
1141        });
1142        hash
1143    }
1144
1145    /// Returns a signed `FindNode` packet for `target`, reusing a cached payload when possible.
1146    fn find_node_packet(&mut self, target: PeerId) -> (Bytes, B256) {
1147        let expire = self.find_node_expiration();
1148        let cache_ttl = self.config.request_timeout / 4;
1149        CachedFindNode::get_or_sign(
1150            &mut self.cached_find_node,
1151            target,
1152            cache_ttl,
1153            &self.secret_key,
1154            expire,
1155        )
1156    }
1157
1158    /// Message handler for an incoming `Ping`
1159    fn on_ping(&mut self, ping: Ping, remote_addr: SocketAddr, remote_id: PeerId, hash: B256) {
1160        if self.is_expired(ping.expire) {
1161            // ping's expiration timestamp is in the past
1162            return
1163        }
1164
1165        // create the record
1166        let record = NodeRecord {
1167            address: remote_addr.ip(),
1168            udp_port: remote_addr.port(),
1169            tcp_port: ping.from.tcp_port,
1170            id: remote_id,
1171        }
1172        .into_ipv4_mapped();
1173
1174        let key = kad_key(record.id);
1175
1176        // See also <https://github.com/ethereum/devp2p/blob/master/discv4.md#ping-packet-0x01>:
1177        // > If no communication with the sender of this ping has occurred within the last 12h, a
1178        // > ping should be sent in addition to pong in order to receive an endpoint proof.
1179        //
1180        // Note: we only mark if the node is absent because the `last 12h` condition is handled by
1181        // the ping interval
1182        let mut is_new_insert = false;
1183        let mut needs_bond = false;
1184        let mut is_proven = false;
1185
1186        let old_enr = match self.kbuckets.entry(&key) {
1187            kbucket::Entry::Present(mut entry, _) => {
1188                if entry.value().is_expired() {
1189                    // If no communication with the sender has occurred within the last 12h, a ping
1190                    // should be sent in addition to pong in order to receive an endpoint proof.
1191                    needs_bond = true;
1192                } else {
1193                    is_proven = entry.value().has_endpoint_proof;
1194                }
1195                entry.value_mut().update_with_enr(ping.enr_sq)
1196            }
1197            kbucket::Entry::Pending(mut entry, _) => {
1198                if entry.value().is_expired() {
1199                    // If no communication with the sender has occurred within the last 12h, a ping
1200                    // should be sent in addition to pong in order to receive an endpoint proof.
1201                    needs_bond = true;
1202                } else {
1203                    is_proven = entry.value().has_endpoint_proof;
1204                }
1205                entry.value_mut().update_with_enr(ping.enr_sq)
1206            }
1207            kbucket::Entry::Absent(entry) => {
1208                let mut node = NodeEntry::new(record);
1209                node.last_enr_seq = ping.enr_sq;
1210
1211                match entry.insert(
1212                    node,
1213                    NodeStatus {
1214                        direction: ConnectionDirection::Incoming,
1215                        // mark as disconnected until endpoint proof established on pong
1216                        state: ConnectionState::Disconnected,
1217                    },
1218                ) {
1219                    BucketInsertResult::Inserted | BucketInsertResult::Pending { .. } => {
1220                        // mark as new insert if insert was successful
1221                        is_new_insert = true;
1222                    }
1223                    BucketInsertResult::Full => {
1224                        // we received a ping but the corresponding bucket for the peer is already
1225                        // full, we can't add any additional peers to that bucket, but we still want
1226                        // to emit an event that we discovered the node
1227                        trace!(target: "discv4", ?record, "discovered new record but bucket is full");
1228                        self.notify(DiscoveryUpdate::DiscoveredAtCapacity(record));
1229                        needs_bond = true;
1230                    }
1231                    BucketInsertResult::TooManyIncoming | BucketInsertResult::NodeExists => {
1232                        needs_bond = true;
1233                        // insert unsuccessful but we still want to send the pong
1234                    }
1235                    BucketInsertResult::FailedFilter => return,
1236                }
1237
1238                None
1239            }
1240            kbucket::Entry::SelfEntry => return,
1241        };
1242
1243        // send the pong first, but the PONG and optionally PING don't need to be send in a
1244        // particular order
1245        let pong = Message::Pong(Pong {
1246            // we use the actual address of the peer
1247            to: record.into(),
1248            echo: hash,
1249            expire: ping.expire,
1250            enr_sq: self.enr_seq(),
1251        });
1252        self.send_packet(pong, remote_addr);
1253
1254        // if node was absent also send a ping to establish the endpoint proof from our end
1255        if is_new_insert {
1256            self.try_ping(record, PingReason::InitialInsert);
1257        } else if needs_bond {
1258            self.try_ping(record, PingReason::EstablishBond);
1259        } else if is_proven {
1260            // if node has been proven, this means we've received a pong and verified its endpoint
1261            // proof. We've also sent a pong above to verify our endpoint proof, so we can now
1262            // send our find_nodes request if PingReason::Lookup
1263            if let Some((_, ctx)) = self.pending_lookup.remove(&record.id) {
1264                if self.pending_find_nodes.contains_key(&record.id) {
1265                    // there's already another pending request, unmark it so the next round can
1266                    // try to send it
1267                    ctx.unmark_queried(record.id);
1268                } else {
1269                    // we just received a ping from that peer so we can send a find node request
1270                    // directly
1271                    self.find_node(&record, ctx);
1272                }
1273            }
1274        } else {
1275            // Request ENR if included in the ping
1276            match (ping.enr_sq, old_enr) {
1277                (Some(new), Some(old)) if new > old => {
1278                    self.send_enr_request(record);
1279                }
1280                (Some(_), None) => {
1281                    self.send_enr_request(record);
1282                }
1283                _ => {}
1284            };
1285        }
1286    }
1287
1288    // Guarding function for [`Self::send_ping`] that applies pre-checks
1289    fn try_ping(&mut self, node: NodeRecord, reason: PingReason) {
1290        if node.id == *self.local_peer_id() {
1291            // don't ping ourselves
1292            return
1293        }
1294
1295        if self.pending_pings.contains_key(&node.id) ||
1296            self.pending_find_nodes.contains_key(&node.id)
1297        {
1298            return
1299        }
1300
1301        if self.queued_pings.iter().any(|(n, _)| n.id == node.id) {
1302            return
1303        }
1304
1305        if self.pending_pings.len() < MAX_NODES_PING {
1306            self.send_ping(node, reason);
1307        } else if self.queued_pings.len() < MAX_QUEUED_PINGS {
1308            self.queued_pings.push_back((node, reason));
1309        }
1310    }
1311
1312    /// Sends a ping message to the node's UDP address.
1313    ///
1314    /// Returns the echo hash of the ping message.
1315    pub(crate) fn send_ping(&mut self, node: NodeRecord, reason: PingReason) -> B256 {
1316        let remote_addr = node.udp_addr();
1317        let id = node.id;
1318        let ping = Ping {
1319            from: self.local_node_record.into(),
1320            to: node.into(),
1321            expire: self.ping_expiration(),
1322            enr_sq: self.enr_seq(),
1323        };
1324        trace!(target: "discv4", ?ping, "sending ping");
1325        let echo_hash = self.send_packet(Message::Ping(ping), remote_addr);
1326
1327        self.pending_pings
1328            .insert(id, PingRequest { sent_at: Instant::now(), node, echo_hash, reason });
1329        echo_hash
1330    }
1331
1332    /// Sends an enr request message to the node's UDP address.
1333    ///
1334    /// Returns the echo hash of the ping message.
1335    pub(crate) fn send_enr_request(&mut self, node: NodeRecord) {
1336        if !self.config.enable_eip868 {
1337            return
1338        }
1339        let remote_addr = node.udp_addr();
1340        let enr_request = EnrRequest { expire: self.enr_request_expiration() };
1341
1342        trace!(target: "discv4", ?enr_request, "sending enr request");
1343        let echo_hash = self.send_packet(Message::EnrRequest(enr_request), remote_addr);
1344
1345        self.pending_enr_requests
1346            .insert(node.id, EnrRequestState { sent_at: Instant::now(), echo_hash });
1347    }
1348
1349    /// Message handler for an incoming `Pong`.
1350    fn on_pong(&mut self, pong: Pong, remote_addr: SocketAddr, remote_id: PeerId) {
1351        if self.is_expired(pong.expire) {
1352            return
1353        }
1354
1355        let PingRequest { node, reason, .. } = match self.pending_pings.entry(remote_id) {
1356            Entry::Occupied(entry) => {
1357                {
1358                    let request = entry.get();
1359                    if request.echo_hash != pong.echo {
1360                        trace!(target: "discv4", from=?remote_addr, expected=?request.echo_hash, echo_hash=?pong.echo,"Got unexpected Pong");
1361                        return
1362                    }
1363                }
1364                entry.remove()
1365            }
1366            Entry::Vacant(_) => return,
1367        };
1368
1369        // keep track of the pong
1370        self.received_pongs.on_pong(remote_id, remote_addr.ip());
1371
1372        match reason {
1373            PingReason::InitialInsert => {
1374                self.update_on_pong(node, pong.enr_sq);
1375                // Reset the lookup interval so the next poll_tick fires immediately,
1376                // rather than waiting the full ~20s for the first lookup.
1377                if self.pending_lookup_reset && self.config.bootstrap_nodes.contains(&node) {
1378                    self.pending_lookup_reset = false;
1379                    self.lookup_interval.reset();
1380                }
1381            }
1382            PingReason::EstablishBond => {
1383                // no initial lookup needed here since the node was already in the table.
1384                self.update_on_pong(node, pong.enr_sq);
1385            }
1386            PingReason::RePing => {
1387                self.update_on_reping(node, pong.enr_sq);
1388            }
1389            PingReason::Lookup(node, ctx) => {
1390                self.update_on_pong(node, pong.enr_sq);
1391                // insert node and assoc. lookup_context into the pending_lookup table to complete
1392                // our side of the endpoint proof verification.
1393                // Start the lookup timer here - and evict accordingly. Note that this is a separate
1394                // timer than the ping_request timer.
1395                self.pending_lookup.insert(node.id, (Instant::now(), ctx));
1396            }
1397        }
1398    }
1399
1400    /// Handler for an incoming `FindNode` message
1401    fn on_find_node(&mut self, msg: FindNode, remote_addr: SocketAddr, node_id: PeerId) {
1402        if self.is_expired(msg.expire) {
1403            // expiration timestamp is in the past
1404            return
1405        }
1406        if node_id == *self.local_peer_id() {
1407            // ignore find node requests to ourselves
1408            return
1409        }
1410
1411        if self.has_bond(node_id, remote_addr.ip()) {
1412            self.respond_closest(msg.id, remote_addr)
1413        }
1414    }
1415
1416    /// Handler for incoming `EnrResponse` message
1417    fn on_enr_response(&mut self, msg: EnrResponse, remote_addr: SocketAddr, id: PeerId) {
1418        trace!(target: "discv4", ?remote_addr, ?msg, "received ENR response");
1419        if let Some(resp) = self.pending_enr_requests.remove(&id) {
1420            // ensure the ENR's public key matches the expected node id
1421            let enr_id = pk2id(&msg.enr.public_key());
1422            if id != enr_id {
1423                return
1424            }
1425
1426            if resp.echo_hash == msg.request_hash {
1427                let key = kad_key(id);
1428                let fork_id = msg.eth_fork_id();
1429                let (record, old_fork_id) = match self.kbuckets.entry(&key) {
1430                    kbucket::Entry::Present(mut entry, _) => {
1431                        let id = entry.value_mut().update_with_fork_id(fork_id);
1432                        (entry.value().record, id)
1433                    }
1434                    kbucket::Entry::Pending(mut entry, _) => {
1435                        let id = entry.value_mut().update_with_fork_id(fork_id);
1436                        (entry.value().record, id)
1437                    }
1438                    _ => return,
1439                };
1440                match (fork_id, old_fork_id) {
1441                    (Some(new), Some(old)) if new != old => {
1442                        self.notify(DiscoveryUpdate::EnrForkId(record, new))
1443                    }
1444                    (Some(new), None) => self.notify(DiscoveryUpdate::EnrForkId(record, new)),
1445                    _ => {}
1446                }
1447            }
1448        }
1449    }
1450
1451    /// Handler for incoming `EnrRequest` message
1452    fn on_enr_request(
1453        &self,
1454        msg: EnrRequest,
1455        remote_addr: SocketAddr,
1456        id: PeerId,
1457        request_hash: B256,
1458    ) {
1459        if !self.config.enable_eip868 || self.is_expired(msg.expire) {
1460            return
1461        }
1462
1463        if self.has_bond(id, remote_addr.ip()) {
1464            self.send_packet(
1465                Message::EnrResponse(EnrResponse {
1466                    request_hash,
1467                    enr: self.local_eip_868_enr.clone(),
1468                }),
1469                remote_addr,
1470            );
1471        }
1472    }
1473
1474    /// Handler for incoming `Neighbours` messages that are handled if they're responses to
1475    /// `FindNode` requests.
1476    fn on_neighbours(&mut self, msg: Neighbours, remote_addr: SocketAddr, node_id: PeerId) {
1477        if self.is_expired(msg.expire) {
1478            // response is expired
1479            return
1480        }
1481        // check if this request was expected
1482        let ctx = match self.pending_find_nodes.entry(node_id) {
1483            Entry::Occupied(mut entry) => {
1484                {
1485                    let request = entry.get_mut();
1486                    // Mark the request as answered
1487                    request.answered = true;
1488                    let total = request.response_count + msg.nodes.len();
1489
1490                    // Neighbours response is exactly 1 bucket (16 entries).
1491                    if total <= MAX_NODES_PER_BUCKET {
1492                        request.response_count = total;
1493                    } else {
1494                        trace!(target: "discv4", total, from=?remote_addr, "Received neighbors packet entries exceeds max nodes per bucket");
1495                        return
1496                    }
1497                };
1498
1499                if entry.get().response_count == MAX_NODES_PER_BUCKET {
1500                    // node responding with a full bucket of records
1501                    let ctx = entry.remove().lookup_context;
1502                    ctx.mark_responded(node_id);
1503                    ctx
1504                } else {
1505                    entry.get().lookup_context.clone()
1506                }
1507            }
1508            Entry::Vacant(_) => {
1509                // received neighbours response without requesting it
1510                trace!(target: "discv4", from=?remote_addr, "Received unsolicited Neighbours");
1511                return
1512            }
1513        };
1514
1515        // log the peers we discovered
1516        trace!(target: "discv4",
1517            target=format!("{:#?}", node_id),
1518            peers_count=msg.nodes.len(),
1519            peers=format!("[{:#}]", msg.nodes.iter()
1520                .map(|node_rec| node_rec.id
1521            ).format(", ")),
1522            "Received peers from Neighbours packet"
1523        );
1524
1525        // This is the recursive lookup step where we initiate new FindNode requests for new nodes
1526        // that were discovered.
1527        for node in msg.nodes.into_iter().map(NodeRecord::into_ipv4_mapped) {
1528            // prevent banned peers from being added to the context
1529            if self.config.ban_list.is_banned(&node.id, &node.address) {
1530                trace!(target: "discv4", peer_id=?node.id, ip=?node.address, "ignoring banned record");
1531                continue
1532            }
1533
1534            ctx.add_node(node);
1535        }
1536
1537        // get the next closest nodes, not yet queried nodes and start over.
1538        let closest =
1539            ctx.filter_closest(ALPHA, |node| !self.pending_find_nodes.contains_key(&node.id));
1540
1541        for closest in closest {
1542            let key = kad_key(closest.id);
1543            match self.kbuckets.entry(&key) {
1544                BucketEntry::Absent(entry) => {
1545                    // the node's endpoint is not proven yet, so we need to ping it first, on
1546                    // success, we will add the node to the pending_lookup table, and wait to send
1547                    // back a Pong before initiating a FindNode request.
1548                    // In order to prevent that this node is selected again on subsequent responses,
1549                    // while the ping is still active, we always mark it as queried.
1550                    ctx.mark_queried(closest.id);
1551                    let node = NodeEntry::new(closest);
1552                    match entry.insert(
1553                        node,
1554                        NodeStatus {
1555                            direction: ConnectionDirection::Outgoing,
1556                            state: ConnectionState::Disconnected,
1557                        },
1558                    ) {
1559                        BucketInsertResult::Inserted | BucketInsertResult::Pending { .. } => {
1560                            // only ping if the node was added to the table
1561                            self.try_ping(closest, PingReason::Lookup(closest, ctx.clone()))
1562                        }
1563                        BucketInsertResult::Full => {
1564                            // new node but the node's bucket is already full
1565                            self.notify(DiscoveryUpdate::DiscoveredAtCapacity(closest))
1566                        }
1567                        _ => {}
1568                    }
1569                }
1570                BucketEntry::SelfEntry => {
1571                    // we received our own node entry
1572                }
1573                BucketEntry::Present(entry, _) => {
1574                    if entry.value().has_endpoint_proof {
1575                        if entry
1576                            .value()
1577                            .exceeds_find_node_failures(self.config.max_find_node_failures)
1578                        {
1579                            self.try_ping(closest, PingReason::Lookup(closest, ctx.clone()))
1580                        } else {
1581                            self.find_node(&closest, ctx.clone());
1582                        }
1583                    }
1584                }
1585                BucketEntry::Pending(entry, _) => {
1586                    if entry.value().has_endpoint_proof {
1587                        if entry
1588                            .value()
1589                            .exceeds_find_node_failures(self.config.max_find_node_failures)
1590                        {
1591                            self.try_ping(closest, PingReason::Lookup(closest, ctx.clone()))
1592                        } else {
1593                            self.find_node(&closest, ctx.clone());
1594                        }
1595                    }
1596                }
1597            }
1598        }
1599    }
1600
1601    /// Sends a Neighbours packet for `target` to the given addr
1602    fn respond_closest(&mut self, target: PeerId, to: SocketAddr) {
1603        let key = kad_key(target);
1604        let expire = self.send_neighbours_expiration();
1605
1606        let enforce_eip868 = self.config.enable_eip868 && self.config.enforce_eip868_neighbours;
1607
1608        // get the MAX_NODES_PER_BUCKET closest nodes to the target, optionally filtering out
1609        // entries that have no EIP-868 fork ID
1610        let closest_nodes = self
1611            .kbuckets
1612            .closest_values(&key)
1613            .filter(|entry| !enforce_eip868 || entry.value.fork_id.is_some())
1614            .take(MAX_NODES_PER_BUCKET)
1615            .collect::<Vec<_>>();
1616
1617        if closest_nodes.is_empty() {
1618            // always respond so the requester does not treat this as a timeout
1619            let msg = Message::Neighbours(Neighbours { nodes: Vec::new(), expire });
1620            self.send_packet(msg, to);
1621            return;
1622        }
1623
1624        for nodes in closest_nodes.chunks(SAFE_MAX_DATAGRAM_NEIGHBOUR_RECORDS) {
1625            let nodes = nodes.iter().map(|node| node.value.record).collect::<Vec<NodeRecord>>();
1626            trace!(target: "discv4", len = nodes.len(), to=?to,"Sent neighbours packet");
1627            let msg = Message::Neighbours(Neighbours { nodes, expire });
1628            self.send_packet(msg, to);
1629        }
1630    }
1631
1632    fn evict_expired_requests(&mut self, now: Instant) {
1633        self.pending_enr_requests.retain(|_node_id, enr_request| {
1634            now.duration_since(enr_request.sent_at) < self.config.enr_expiration
1635        });
1636
1637        let mut failed_pings = Vec::new();
1638        self.pending_pings.retain(|node_id, ping_request| {
1639            if now.duration_since(ping_request.sent_at) > self.config.ping_expiration {
1640                failed_pings.push(*node_id);
1641                return false
1642            }
1643            true
1644        });
1645
1646        if !failed_pings.is_empty() {
1647            // remove nodes that failed to pong
1648            trace!(target: "discv4", num=%failed_pings.len(), "evicting nodes due to failed pong");
1649            for node_id in failed_pings {
1650                self.remove_node(node_id);
1651            }
1652        }
1653
1654        let mut failed_lookups = Vec::new();
1655        self.pending_lookup.retain(|node_id, (lookup_sent_at, _)| {
1656            if now.duration_since(*lookup_sent_at) > self.config.request_timeout {
1657                failed_lookups.push(*node_id);
1658                return false
1659            }
1660            true
1661        });
1662
1663        if !failed_lookups.is_empty() {
1664            // remove nodes that failed the e2e lookup process, so we can restart it
1665            trace!(target: "discv4", num=%failed_lookups.len(), "evicting nodes due to failed lookup");
1666            for node_id in failed_lookups {
1667                self.remove_node(node_id);
1668            }
1669        }
1670
1671        self.evict_failed_find_nodes(now);
1672    }
1673
1674    /// Handles failed responses to `FindNode`
1675    fn evict_failed_find_nodes(&mut self, now: Instant) {
1676        let mut failed_find_nodes = Vec::new();
1677        self.pending_find_nodes.retain(|node_id, find_node_request| {
1678            if now.duration_since(find_node_request.sent_at) > self.config.neighbours_expiration {
1679                if !find_node_request.answered {
1680                    // node actually responded but with fewer entries than expected, but we don't
1681                    // treat this as an hard error since it responded.
1682                    failed_find_nodes.push(*node_id);
1683                }
1684                return false
1685            }
1686            true
1687        });
1688
1689        if failed_find_nodes.is_empty() {
1690            return
1691        }
1692
1693        trace!(target: "discv4", num=%failed_find_nodes.len(), "processing failed find nodes");
1694
1695        for node_id in failed_find_nodes {
1696            let key = kad_key(node_id);
1697            let failures = match self.kbuckets.entry(&key) {
1698                kbucket::Entry::Present(mut entry, _) => {
1699                    entry.value_mut().inc_failed_request();
1700                    entry.value().find_node_failures
1701                }
1702                kbucket::Entry::Pending(mut entry, _) => {
1703                    entry.value_mut().inc_failed_request();
1704                    entry.value().find_node_failures
1705                }
1706                _ => continue,
1707            };
1708
1709            // if the node failed to respond anything useful multiple times, remove the node from
1710            // the table, but only if there are enough other nodes in the bucket (bucket must be at
1711            // least half full)
1712            if failures > self.config.max_find_node_failures {
1713                self.soft_remove_node(node_id);
1714            }
1715        }
1716    }
1717
1718    /// Re-pings all nodes which endpoint proofs are considered expired: [`NodeEntry::is_expired`]
1719    ///
1720    /// This will send a `Ping` to the nodes, if a node fails to respond with a `Pong` to renew the
1721    /// endpoint proof it will be removed from the table.
1722    fn re_ping_oldest(&mut self) {
1723        let mut nodes = self
1724            .kbuckets
1725            .iter_ref()
1726            .filter(|entry| entry.node.value.is_expired())
1727            .map(|n| n.node.value)
1728            .collect::<Vec<_>>();
1729        nodes.sort_unstable_by_key(|a| a.last_seen);
1730        let to_ping = nodes.into_iter().map(|n| n.record).take(MAX_NODES_PING).collect::<Vec<_>>();
1731        for node in to_ping {
1732            self.try_ping(node, PingReason::RePing)
1733        }
1734    }
1735
1736    /// Returns true if the expiration timestamp is in the past.
1737    fn is_expired(&self, expiration: u64) -> bool {
1738        self.ensure_not_expired(expiration).is_err()
1739    }
1740
1741    /// Validate that given timestamp is not expired.
1742    ///
1743    /// Note: this accepts the timestamp as u64 because this is used by the wire protocol, but the
1744    /// UNIX timestamp (number of non-leap seconds since January 1, 1970 0:00:00 UTC) is supposed to
1745    /// be an i64.
1746    ///
1747    /// Returns an error if:
1748    ///  - invalid UNIX timestamp (larger than `i64::MAX`)
1749    ///  - timestamp is expired (lower than current local UNIX timestamp)
1750    fn ensure_not_expired(&self, timestamp: u64) -> Result<(), ()> {
1751        // ensure the timestamp is a valid UNIX timestamp
1752        let _ = i64::try_from(timestamp).map_err(drop)?;
1753
1754        let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
1755        if self.config.enforce_expiration_timestamps && timestamp < now {
1756            trace!(target: "discv4", "Expired packet");
1757            return Err(())
1758        }
1759        Ok(())
1760    }
1761
1762    /// Pops buffered ping requests and sends them.
1763    fn ping_buffered(&mut self) {
1764        while self.pending_pings.len() < MAX_NODES_PING {
1765            match self.queued_pings.pop_front() {
1766                Some((next, reason)) => self.try_ping(next, reason),
1767                None => break,
1768            }
1769        }
1770    }
1771
1772    fn ping_expiration(&self) -> u64 {
1773        (SystemTime::now().duration_since(UNIX_EPOCH).unwrap() + self.config.ping_expiration)
1774            .as_secs()
1775    }
1776
1777    fn find_node_expiration(&self) -> u64 {
1778        (SystemTime::now().duration_since(UNIX_EPOCH).unwrap() + self.config.request_timeout)
1779            .as_secs()
1780    }
1781
1782    fn enr_request_expiration(&self) -> u64 {
1783        (SystemTime::now().duration_since(UNIX_EPOCH).unwrap() + self.config.enr_expiration)
1784            .as_secs()
1785    }
1786
1787    fn send_neighbours_expiration(&self) -> u64 {
1788        (SystemTime::now().duration_since(UNIX_EPOCH).unwrap() + self.config.neighbours_expiration)
1789            .as_secs()
1790    }
1791
1792    /// Polls the socket and advances the state.
1793    ///
1794    /// To prevent traffic amplification attacks, implementations must verify that the sender of a
1795    /// query participates in the discovery protocol. The sender of a packet is considered verified
1796    /// if it has sent a valid Pong response with matching ping hash within the last 12 hours.
1797    pub(crate) fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Discv4Event> {
1798        loop {
1799            // drain buffered events first
1800            if let Some(event) = self.queued_events.pop_front() {
1801                return Poll::Ready(event)
1802            }
1803
1804            // trigger self lookup
1805            if self.config.enable_lookup {
1806                while self.lookup_interval.poll_tick(cx).is_ready() {
1807                    let target = self.lookup_rotator.next(&self.local_node_record.id);
1808                    self.lookup_with(target, None);
1809                }
1810            }
1811
1812            // re-ping some peers
1813            while self.ping_interval.poll_tick(cx).is_ready() {
1814                self.re_ping_oldest();
1815            }
1816
1817            if let Some(Poll::Ready(Some(ip))) =
1818                self.resolve_external_ip_interval.as_mut().map(|r| r.poll_tick(cx))
1819            {
1820                self.set_external_ip_addr(ip);
1821            }
1822
1823            // drain all incoming `Discv4` commands, this channel can never close
1824            while let Poll::Ready(Some(cmd)) = self.commands_rx.poll_recv(cx) {
1825                match cmd {
1826                    Discv4Command::Add(enr) => {
1827                        self.add_node(enr);
1828                    }
1829                    Discv4Command::AddBootNode(record) => {
1830                        self.add_boot_node(record);
1831                    }
1832                    Discv4Command::Lookup { node_id, tx } => {
1833                        let node_id = node_id.unwrap_or(self.local_node_record.id);
1834                        self.lookup_with(node_id, tx);
1835                    }
1836                    Discv4Command::SetLookupInterval(duration) => {
1837                        self.set_lookup_interval(duration);
1838                    }
1839                    Discv4Command::Updates(tx) => {
1840                        let rx = self.update_stream();
1841                        let _ = tx.send(rx);
1842                    }
1843                    Discv4Command::BanPeer(node_id) => self.ban_node(node_id),
1844                    Discv4Command::Remove(node_id) => {
1845                        self.remove_node(node_id);
1846                    }
1847                    Discv4Command::Ban(node_id, ip) => {
1848                        self.ban_node(node_id);
1849                        self.ban_ip(ip);
1850                    }
1851                    Discv4Command::BanIp(ip) => {
1852                        self.ban_ip(ip);
1853                    }
1854                    Discv4Command::SetEIP868RLPPair { key, rlp } => {
1855                        debug!(target: "discv4", key=%String::from_utf8_lossy(&key), "Update EIP-868 extension pair");
1856
1857                        let _ = self.local_eip_868_enr.insert_raw_rlp(key, rlp, &self.secret_key);
1858                    }
1859                    Discv4Command::SetTcpPort(port) => {
1860                        debug!(target: "discv4", %port, "Update tcp port");
1861                        self.local_node_record.tcp_port = port;
1862                        if self.local_node_record.address.is_ipv4() {
1863                            let _ = self.local_eip_868_enr.set_tcp4(port, &self.secret_key);
1864                        } else {
1865                            let _ = self.local_eip_868_enr.set_tcp6(port, &self.secret_key);
1866                        }
1867                    }
1868
1869                    Discv4Command::Terminated => {
1870                        // terminate the service
1871                        self.queued_events.push_back(Discv4Event::Terminated);
1872                    }
1873                }
1874            }
1875
1876            // restricts how many messages we process in a single poll before yielding back control
1877            let mut udp_message_budget = UDP_MESSAGE_POLL_LOOP_BUDGET;
1878
1879            // process all incoming datagrams
1880            while let Poll::Ready(Some(event)) = self.ingress.poll_recv(cx) {
1881                match event {
1882                    IngressEvent::RecvError(err) => {
1883                        debug!(target: "discv4", %err, "failed to read datagram");
1884                    }
1885                    IngressEvent::BadPacket(from, err, data) => {
1886                        trace!(target: "discv4", ?from, %err, packet=?hex::encode(&data), "bad packet");
1887                    }
1888                    IngressEvent::Packet(remote_addr, Packet { msg, node_id, hash }) => {
1889                        trace!(target: "discv4", r#type=?msg.msg_type(), from=?remote_addr,"received packet");
1890                        let event = match msg {
1891                            Message::Ping(ping) => {
1892                                self.on_ping(ping, remote_addr, node_id, hash);
1893                                Discv4Event::Ping
1894                            }
1895                            Message::Pong(pong) => {
1896                                self.on_pong(pong, remote_addr, node_id);
1897                                Discv4Event::Pong
1898                            }
1899                            Message::FindNode(msg) => {
1900                                self.on_find_node(msg, remote_addr, node_id);
1901                                Discv4Event::FindNode
1902                            }
1903                            Message::Neighbours(msg) => {
1904                                self.on_neighbours(msg, remote_addr, node_id);
1905                                Discv4Event::Neighbours
1906                            }
1907                            Message::EnrRequest(msg) => {
1908                                self.on_enr_request(msg, remote_addr, node_id, hash);
1909                                Discv4Event::EnrRequest
1910                            }
1911                            Message::EnrResponse(msg) => {
1912                                self.on_enr_response(msg, remote_addr, node_id);
1913                                Discv4Event::EnrResponse
1914                            }
1915                        };
1916
1917                        self.queued_events.push_back(event);
1918                    }
1919                }
1920
1921                udp_message_budget -= 1;
1922                if udp_message_budget < 0 {
1923                    trace!(target: "discv4", budget=UDP_MESSAGE_POLL_LOOP_BUDGET, "exhausted message poll budget");
1924                    if self.queued_events.is_empty() {
1925                        // we've exceeded the message budget and have no events to process
1926                        // this will make sure we're woken up again
1927                        cx.waker().wake_by_ref();
1928                    }
1929                    break
1930                }
1931            }
1932
1933            // try resending buffered pings
1934            self.ping_buffered();
1935
1936            // evict expired requests
1937            while self.evict_expired_requests_interval.poll_tick(cx).is_ready() {
1938                self.evict_expired_requests(Instant::now());
1939            }
1940
1941            // evict expired nodes
1942            while self.expire_interval.poll_tick(cx).is_ready() {
1943                self.received_pongs.evict_expired(Instant::now(), EXPIRE_DURATION);
1944            }
1945
1946            if self.queued_events.is_empty() {
1947                return Poll::Pending
1948            }
1949        }
1950    }
1951}
1952
1953/// Endless future impl
1954impl Stream for Discv4Service {
1955    type Item = Discv4Event;
1956
1957    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
1958        // Poll the internal poll method
1959        match ready!(self.get_mut().poll(cx)) {
1960            // if the service is terminated, return None to terminate the stream
1961            Discv4Event::Terminated => Poll::Ready(None),
1962            // For any other event, return Poll::Ready(Some(event))
1963            ev => Poll::Ready(Some(ev)),
1964        }
1965    }
1966}
1967
1968impl fmt::Debug for Discv4Service {
1969    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1970        f.debug_struct("Discv4Service")
1971            .field("local_address", &self.local_address)
1972            .field("local_peer_id", &self.local_peer_id())
1973            .field("local_node_record", &self.local_node_record)
1974            .field("queued_pings", &self.queued_pings)
1975            .field("pending_lookup", &self.pending_lookup)
1976            .field("pending_find_nodes", &self.pending_find_nodes)
1977            .field("lookup_interval", &self.lookup_interval)
1978            .finish_non_exhaustive()
1979    }
1980}
1981
1982/// The Event type the Service stream produces.
1983///
1984/// This is mainly used for testing purposes and represents messages the service processed
1985#[derive(Debug, Eq, PartialEq)]
1986pub enum Discv4Event {
1987    /// A `Ping` message was handled.
1988    Ping,
1989    /// A `Pong` message was handled.
1990    Pong,
1991    /// A `FindNode` message was handled.
1992    FindNode,
1993    /// A `Neighbours` message was handled.
1994    Neighbours,
1995    /// A `EnrRequest` message was handled.
1996    EnrRequest,
1997    /// A `EnrResponse` message was handled.
1998    EnrResponse,
1999    /// Service is being terminated
2000    Terminated,
2001}
2002
2003/// Continuously reads new messages from the channel and writes them to the socket
2004pub(crate) async fn send_loop(udp: Arc<UdpSocket>, rx: EgressReceiver) {
2005    let mut stream = ReceiverStream::new(rx);
2006    while let Some((payload, to)) = stream.next().await {
2007        match udp.send_to(&payload, to).await {
2008            Ok(size) => {
2009                trace!(target: "discv4", ?to, ?size,"sent payload");
2010            }
2011            Err(err) => {
2012                debug!(target: "discv4", ?to, %err,"Failed to send datagram.");
2013            }
2014        }
2015    }
2016}
2017
2018/// Rate limits the number of incoming packets from individual IPs to 1 packet/second
2019const MAX_INCOMING_PACKETS_PER_MINUTE_BY_IP: usize = 60usize;
2020
2021/// Continuously awaits new incoming messages and sends them back through the channel.
2022///
2023/// The receive loop enforces primitive rate limiting for IPs to prevent message spams from
2024/// individual IPs.
2025pub(crate) async fn receive_loop(udp: Arc<UdpSocket>, tx: IngressSender, local_id: PeerId) {
2026    let mut handler = IngressHandler::new(tx, local_id);
2027    let mut buf = [0; MAX_PACKET_SIZE];
2028    loop {
2029        let res = udp.recv_from(&mut buf).await;
2030        match res {
2031            Err(err) => {
2032                debug!(target: "discv4", %err, "Failed to read datagram.");
2033                handler.send(IngressEvent::RecvError(err)).await;
2034            }
2035            Ok((read, remote_addr)) => {
2036                handler.handle_packet(&buf[..read], remote_addr).await;
2037            }
2038        }
2039    }
2040}
2041
2042/// Handles decoding, rate-limiting, and deduplication of incoming discv4 packets.
2043///
2044/// Used by both the standalone receive loop and the shared-port mode via
2045/// [`Discv4::bind_shared`].
2046#[derive(Debug)]
2047pub struct IngressHandler {
2048    tx: IngressSender,
2049    local_id: PeerId,
2050    tick: usize,
2051    tick_interval: Duration,
2052    cache: ReceiveCache,
2053    last_tick: Instant,
2054}
2055
2056impl IngressHandler {
2057    fn new(tx: IngressSender, local_id: PeerId) -> Self {
2058        let tick = MAX_INCOMING_PACKETS_PER_MINUTE_BY_IP / 2;
2059        Self {
2060            tx,
2061            local_id,
2062            tick,
2063            tick_interval: Duration::from_secs(tick as u64),
2064            cache: ReceiveCache::default(),
2065            last_tick: Instant::now(),
2066        }
2067    }
2068
2069    async fn send(&self, event: IngressEvent) {
2070        let _ = self.tx.send(event).await.map_err(|err| {
2071            debug!(target: "discv4", %err, "failed send incoming packet");
2072        });
2073    }
2074
2075    /// Handles an incoming raw packet: decodes, rate-limits, deduplicates, and forwards to the
2076    /// discv4 service. Used in shared-port mode to process unrecognized frames from discv5.
2077    pub async fn handle_packet(&mut self, data: &[u8], src: SocketAddr) {
2078        if self.last_tick.elapsed() >= self.tick_interval {
2079            self.cache.tick_ips(self.tick);
2080            self.last_tick = Instant::now();
2081        }
2082
2083        // rate limit incoming packets by IP
2084        if self.cache.inc_ip(src.ip()) > MAX_INCOMING_PACKETS_PER_MINUTE_BY_IP {
2085            trace!(target: "discv4", ?src, "Too many incoming packets from IP.");
2086            return
2087        }
2088
2089        // A packet starts with the hash of everything that follows it, and `Message::decode`
2090        // rejects any packet whose contents do not hash to it. A repeat of a packet we already
2091        // accepted can therefore be recognised from those 32 bytes alone, without decoding.
2092        // Decoding runs an ECDSA recovery, so checking here keeps a replayed packet from costing
2093        // a signature verification.
2094        if data.len() >= MIN_PACKET_SIZE &&
2095            self.cache.contains_packet(B256::from_slice(&data[..32]))
2096        {
2097            trace!(target: "discv4", ?src, "Received duplicate packet.");
2098            return
2099        }
2100
2101        let event = match Message::decode(data) {
2102            Ok(packet) => {
2103                if packet.node_id == self.local_id {
2104                    debug!(target: "discv4", ?src, "Received own packet.");
2105                    return
2106                }
2107
2108                // Only packets that decoded are remembered, so a peer cannot suppress a packet we
2109                // have not seen yet by guessing its hash.
2110                self.cache.insert_packet(packet.hash);
2111
2112                IngressEvent::Packet(src, packet)
2113            }
2114            Err(err) => {
2115                trace!(target: "discv4", %err, "Failed to decode packet");
2116                IngressEvent::BadPacket(src, err, data.to_vec())
2117            }
2118        };
2119
2120        self.send(event).await;
2121    }
2122}
2123
2124/// A cache for received packets and their source address.
2125///
2126/// This is used to discard duplicated packets and rate limit messages from the same source.
2127#[derive(Debug)]
2128struct ReceiveCache {
2129    /// keeps track of how many messages we've received from a given IP address since the last
2130    /// tick.
2131    ///
2132    /// This is used to count the number of messages received from a given IP address within an
2133    /// interval.
2134    ip_messages: HashMap<IpAddr, usize>,
2135    // keeps track of unique packet hashes
2136    unique_packets: schnellru::LruMap<B256, ()>,
2137}
2138
2139impl ReceiveCache {
2140    /// Updates the counter for each IP address and removes IPs that have exceeded the limit.
2141    ///
2142    /// This will decrement the counter for each IP address and remove IPs that have reached 0.
2143    fn tick_ips(&mut self, tick: usize) {
2144        self.ip_messages.retain(|_, count| {
2145            if let Some(reset) = count.checked_sub(tick) {
2146                *count = reset;
2147                true
2148            } else {
2149                false
2150            }
2151        });
2152    }
2153
2154    /// Increases the counter for the given IP address and returns the new count.
2155    fn inc_ip(&mut self, ip: IpAddr) -> usize {
2156        let ctn = self.ip_messages.entry(ip).or_default();
2157        *ctn = ctn.saturating_add(1);
2158        *ctn
2159    }
2160
2161    /// Returns true if we previously received the packet.
2162    ///
2163    /// A hit refreshes the entry so that a packet being replayed repeatedly stays cached.
2164    fn contains_packet(&mut self, hash: B256) -> bool {
2165        self.unique_packets.get(&hash).is_some()
2166    }
2167
2168    /// Remembers a packet we accepted.
2169    fn insert_packet(&mut self, hash: B256) {
2170        self.unique_packets.insert(hash, ());
2171    }
2172}
2173
2174impl Default for ReceiveCache {
2175    fn default() -> Self {
2176        Self {
2177            ip_messages: Default::default(),
2178            unique_packets: schnellru::LruMap::new(schnellru::ByLength::new(32)),
2179        }
2180    }
2181}
2182
2183/// The commands sent from the frontend [Discv4] to the service [`Discv4Service`].
2184enum Discv4Command {
2185    Add(NodeRecord),
2186    AddBootNode(NodeRecord),
2187    SetTcpPort(u16),
2188    SetEIP868RLPPair { key: Vec<u8>, rlp: Bytes },
2189    Ban(PeerId, IpAddr),
2190    BanPeer(PeerId),
2191    BanIp(IpAddr),
2192    Remove(PeerId),
2193    Lookup { node_id: Option<PeerId>, tx: Option<NodeRecordSender> },
2194    SetLookupInterval(Duration),
2195    Updates(OneshotSender<ReceiverStream<DiscoveryUpdate>>),
2196    Terminated,
2197}
2198
2199/// Event type receiver produces
2200#[derive(Debug)]
2201pub(crate) enum IngressEvent {
2202    /// Encountered an error when reading a datagram message.
2203    RecvError(io::Error),
2204    /// Received a bad message
2205    BadPacket(SocketAddr, DecodePacketError, Vec<u8>),
2206    /// Received a datagram from an address.
2207    Packet(SocketAddr, Packet),
2208}
2209
2210/// Tracks a sent ping
2211#[derive(Debug)]
2212struct PingRequest {
2213    // Timestamp when the request was sent.
2214    sent_at: Instant,
2215    // Node to which the request was sent.
2216    node: NodeRecord,
2217    // Hash sent in the Ping request
2218    echo_hash: B256,
2219    /// Why this ping was sent.
2220    reason: PingReason,
2221}
2222
2223/// Rotates the `PeerId` that is periodically looked up.
2224///
2225/// By selecting different targets, the lookups will be seeded with different ALPHA seed nodes.
2226#[derive(Debug)]
2227struct LookupTargetRotator {
2228    interval: usize,
2229    counter: usize,
2230}
2231
2232// === impl LookupTargetRotator ===
2233
2234impl LookupTargetRotator {
2235    /// Returns a rotator that always returns the local target.
2236    const fn local_only() -> Self {
2237        Self { interval: 1, counter: 0 }
2238    }
2239}
2240
2241impl Default for LookupTargetRotator {
2242    fn default() -> Self {
2243        Self {
2244            // every 4th lookup is our own node
2245            interval: 4,
2246            counter: 3,
2247        }
2248    }
2249}
2250
2251impl LookupTargetRotator {
2252    /// This will return the next node id to lookup
2253    fn next(&mut self, local: &PeerId) -> PeerId {
2254        self.counter += 1;
2255        self.counter %= self.interval;
2256        if self.counter == 0 {
2257            return *local
2258        }
2259        PeerId::random()
2260    }
2261}
2262
2263/// Tracks lookups across multiple `FindNode` requests.
2264///
2265/// If this type is dropped by all Clones, it will send all the discovered nodes to the listener, if
2266/// one is present.
2267#[derive(Clone, Debug)]
2268struct LookupContext {
2269    inner: Rc<LookupContextInner>,
2270}
2271
2272impl LookupContext {
2273    /// Create new context for a recursive lookup
2274    fn new(
2275        target: discv5::Key<NodeKey>,
2276        nearest_nodes: impl IntoIterator<Item = (Distance, NodeRecord)>,
2277        listener: Option<NodeRecordSender>,
2278    ) -> Self {
2279        let closest_nodes = nearest_nodes
2280            .into_iter()
2281            .map(|(distance, record)| {
2282                (distance, QueryNode { record, queried: false, responded: false })
2283            })
2284            .collect();
2285
2286        let inner = Rc::new(LookupContextInner {
2287            target,
2288            closest_nodes: RefCell::new(closest_nodes),
2289            listener,
2290        });
2291        Self { inner }
2292    }
2293
2294    /// Returns the target of this lookup
2295    fn target(&self) -> PeerId {
2296        self.inner.target.preimage().0
2297    }
2298
2299    fn closest(&self, num: usize) -> Vec<NodeRecord> {
2300        self.inner
2301            .closest_nodes
2302            .borrow()
2303            .iter()
2304            .filter(|(_, node)| !node.queried)
2305            .map(|(_, n)| n.record)
2306            .take(num)
2307            .collect()
2308    }
2309
2310    /// Returns the closest nodes that have not been queried yet.
2311    fn filter_closest<P>(&self, num: usize, filter: P) -> Vec<NodeRecord>
2312    where
2313        P: FnMut(&NodeRecord) -> bool,
2314    {
2315        self.inner
2316            .closest_nodes
2317            .borrow()
2318            .iter()
2319            .filter(|(_, node)| !node.queried)
2320            .map(|(_, n)| n.record)
2321            .filter(filter)
2322            .take(num)
2323            .collect()
2324    }
2325
2326    /// Inserts the node if it's missing
2327    fn add_node(&self, record: NodeRecord) {
2328        let distance = self.inner.target.distance(&kad_key(record.id));
2329        let mut closest = self.inner.closest_nodes.borrow_mut();
2330        if let btree_map::Entry::Vacant(entry) = closest.entry(distance) {
2331            entry.insert(QueryNode { record, queried: false, responded: false });
2332        }
2333    }
2334
2335    fn set_queried(&self, id: PeerId, val: bool) {
2336        if let Some((_, node)) =
2337            self.inner.closest_nodes.borrow_mut().iter_mut().find(|(_, node)| node.record.id == id)
2338        {
2339            node.queried = val;
2340        }
2341    }
2342
2343    /// Marks the node as queried
2344    fn mark_queried(&self, id: PeerId) {
2345        self.set_queried(id, true)
2346    }
2347
2348    /// Marks the node as not queried
2349    fn unmark_queried(&self, id: PeerId) {
2350        self.set_queried(id, false)
2351    }
2352
2353    /// Marks the node as responded
2354    fn mark_responded(&self, id: PeerId) {
2355        if let Some((_, node)) =
2356            self.inner.closest_nodes.borrow_mut().iter_mut().find(|(_, node)| node.record.id == id)
2357        {
2358            node.responded = true;
2359        }
2360    }
2361}
2362
2363// SAFETY: The [`Discv4Service`] is intended to be spawned as task which requires `Send`.
2364// The `LookupContext` is shared by all active `FindNode` requests that are part of the lookup step.
2365// Which can modify the context. The shared context is only ever accessed mutably when a `Neighbour`
2366// response is processed and all Clones are stored inside [`Discv4Service`], in other words it is
2367// guaranteed that there's only 1 owner ([`Discv4Service`]) of all possible [`Rc`] clones of
2368// [`LookupContext`].
2369unsafe impl Send for LookupContext {}
2370#[derive(Debug)]
2371struct LookupContextInner {
2372    /// The target to lookup.
2373    target: discv5::Key<NodeKey>,
2374    /// The closest nodes
2375    closest_nodes: RefCell<BTreeMap<Distance, QueryNode>>,
2376    /// A listener for all the nodes retrieved in this lookup
2377    ///
2378    /// This is present if the lookup was triggered manually via [Discv4] and we want to return all
2379    /// the nodes once the lookup finishes.
2380    listener: Option<NodeRecordSender>,
2381}
2382
2383impl Drop for LookupContextInner {
2384    fn drop(&mut self) {
2385        if let Some(tx) = self.listener.take() {
2386            // there's only 1 instance shared across `FindNode` requests, if this is dropped then
2387            // all requests finished, and we can send all results back
2388            let nodes = self
2389                .closest_nodes
2390                .take()
2391                .into_values()
2392                .filter(|node| node.responded)
2393                .map(|node| node.record)
2394                .collect();
2395            let _ = tx.send(nodes);
2396        }
2397    }
2398}
2399
2400/// Tracks the state of a recursive lookup step
2401#[derive(Debug, Clone, Copy)]
2402struct QueryNode {
2403    record: NodeRecord,
2404    queried: bool,
2405    responded: bool,
2406}
2407
2408#[derive(Debug)]
2409struct FindNodeRequest {
2410    // Timestamp when the request was sent.
2411    sent_at: Instant,
2412    // Number of items sent by the node
2413    response_count: usize,
2414    // Whether the request has been answered yet.
2415    answered: bool,
2416    /// Response buffer
2417    lookup_context: LookupContext,
2418}
2419
2420// === impl FindNodeRequest ===
2421
2422impl FindNodeRequest {
2423    fn new(resp: LookupContext) -> Self {
2424        Self { sent_at: Instant::now(), response_count: 0, answered: false, lookup_context: resp }
2425    }
2426}
2427
2428/// Cached signed `FindNode` packet to avoid redundant ECDSA signing during Kademlia lookups.
2429#[derive(Debug)]
2430struct CachedFindNode {
2431    target: PeerId,
2432    payload: Bytes,
2433    hash: B256,
2434    cached_at: Instant,
2435}
2436
2437impl CachedFindNode {
2438    /// Returns the cached `(payload, hash)` if the target matches and the cache is still fresh,
2439    /// or signs a new packet, updates the cache, and returns it.
2440    fn get_or_sign(
2441        cache: &mut Option<Self>,
2442        target: PeerId,
2443        ttl: Duration,
2444        secret_key: &secp256k1::SecretKey,
2445        expire: u64,
2446    ) -> (Bytes, B256) {
2447        if let Some(c) = cache.as_ref() &&
2448            c.target == target &&
2449            c.cached_at.elapsed() < ttl
2450        {
2451            return (c.payload.clone(), c.hash);
2452        }
2453
2454        let msg = Message::FindNode(FindNode { id: target, expire });
2455        let (payload, hash) = msg.encode(secret_key);
2456
2457        *cache = Some(Self { target, payload: payload.clone(), hash, cached_at: Instant::now() });
2458
2459        (payload, hash)
2460    }
2461}
2462
2463#[derive(Debug)]
2464struct EnrRequestState {
2465    // Timestamp when the request was sent.
2466    sent_at: Instant,
2467    // Hash sent in the Ping request
2468    echo_hash: B256,
2469}
2470
2471/// Stored node info.
2472#[derive(Debug, Clone, Eq, PartialEq)]
2473struct NodeEntry {
2474    /// Node record info.
2475    record: NodeRecord,
2476    /// Timestamp of last pong.
2477    last_seen: Instant,
2478    /// Last enr seq we retrieved via a ENR request.
2479    last_enr_seq: Option<u64>,
2480    /// `ForkId` if retrieved via ENR requests.
2481    fork_id: Option<ForkId>,
2482    /// Counter for failed _consecutive_ findNode requests.
2483    find_node_failures: u8,
2484    /// Whether the endpoint of the peer is proven.
2485    has_endpoint_proof: bool,
2486}
2487
2488// === impl NodeEntry ===
2489
2490impl NodeEntry {
2491    /// Creates a new, unpopulated entry
2492    fn new(record: NodeRecord) -> Self {
2493        Self {
2494            record,
2495            last_seen: Instant::now(),
2496            last_enr_seq: None,
2497            fork_id: None,
2498            find_node_failures: 0,
2499            has_endpoint_proof: false,
2500        }
2501    }
2502
2503    #[cfg(test)]
2504    fn new_proven(record: NodeRecord) -> Self {
2505        let mut node = Self::new(record);
2506        node.has_endpoint_proof = true;
2507        node
2508    }
2509
2510    /// Marks the entry with an established proof and resets the consecutive failure counter.
2511    const fn establish_proof(&mut self) {
2512        self.has_endpoint_proof = true;
2513        self.find_node_failures = 0;
2514    }
2515
2516    /// Returns true if the tracked find node failures exceed the max amount
2517    const fn exceeds_find_node_failures(&self, max_failures: u8) -> bool {
2518        self.find_node_failures >= max_failures
2519    }
2520
2521    /// Updates the last timestamp and sets the enr seq
2522    fn update_with_enr(&mut self, last_enr_seq: Option<u64>) -> Option<u64> {
2523        self.update_now(|s| std::mem::replace(&mut s.last_enr_seq, last_enr_seq))
2524    }
2525
2526    /// Increases the failed request counter
2527    const fn inc_failed_request(&mut self) {
2528        self.find_node_failures += 1;
2529    }
2530
2531    /// Updates the last timestamp and sets the enr seq
2532    fn update_with_fork_id(&mut self, fork_id: Option<ForkId>) -> Option<ForkId> {
2533        self.update_now(|s| std::mem::replace(&mut s.fork_id, fork_id))
2534    }
2535
2536    /// Updates the `last_seen` timestamp and calls the closure
2537    fn update_now<F, R>(&mut self, f: F) -> R
2538    where
2539        F: FnOnce(&mut Self) -> R,
2540    {
2541        self.last_seen = Instant::now();
2542        f(self)
2543    }
2544}
2545
2546// === impl NodeEntry ===
2547
2548impl NodeEntry {
2549    /// Returns true if the node should be re-pinged.
2550    fn is_expired(&self) -> bool {
2551        self.last_seen.elapsed() > (ENDPOINT_PROOF_EXPIRATION / 2)
2552    }
2553}
2554
2555/// Represents why a ping is issued
2556#[derive(Debug)]
2557enum PingReason {
2558    /// Initial ping to a previously unknown peer that was inserted into the table.
2559    InitialInsert,
2560    /// A ping to a peer to establish a bond (endpoint proof).
2561    EstablishBond,
2562    /// Re-ping a peer.
2563    RePing,
2564    /// Part of a lookup to ensure endpoint is proven before we can send a `FindNode` request.
2565    Lookup(NodeRecord, LookupContext),
2566}
2567
2568/// Represents node related updates state changes in the underlying node table
2569#[derive(Debug, Clone)]
2570pub enum DiscoveryUpdate {
2571    /// A new node was discovered _and_ added to the table.
2572    Added(NodeRecord),
2573    /// A new node was discovered but _not_ added to the table because it is currently full.
2574    DiscoveredAtCapacity(NodeRecord),
2575    /// Received a [`ForkId`] via EIP-868 for the given [`NodeRecord`].
2576    EnrForkId(NodeRecord, ForkId),
2577    /// Node that was removed from the table
2578    Removed(PeerId),
2579    /// A series of updates
2580    Batch(Vec<Self>),
2581}
2582
2583#[cfg(test)]
2584mod tests {
2585    use super::*;
2586    use crate::test_utils::{create_discv4, create_discv4_with_config, rng_endpoint, rng_record};
2587    use alloy_primitives::hex;
2588    use alloy_rlp::{Decodable, Encodable};
2589    use rand_08::Rng;
2590    use reth_ethereum_forks::{EnrForkIdEntry, ForkHash};
2591    use reth_network_peers::mainnet_nodes;
2592    use secp256k1::SECP256K1;
2593    use std::future::poll_fn;
2594
2595    #[tokio::test]
2596    async fn test_duplicate_packet_rejected_without_decoding() {
2597        let secret_key = SecretKey::new(&mut rand_08::thread_rng());
2598        let local_id = pk2id(&secret_key.public_key(SECP256K1));
2599        let (tx, mut rx) = mpsc::channel(16);
2600        let mut handler = IngressHandler::new(tx, local_id);
2601
2602        let remote_key = SecretKey::new(&mut rand_08::thread_rng());
2603        let msg = Message::Ping(Ping {
2604            from: rng_endpoint(&mut rand_08::thread_rng()),
2605            to: rng_endpoint(&mut rand_08::thread_rng()),
2606            expire: u64::MAX,
2607            enr_sq: None,
2608        });
2609        let (packet, hash) = msg.encode(&remote_key);
2610        let src = "10.0.0.1:30303".parse().unwrap();
2611
2612        handler.handle_packet(&packet, src).await;
2613        assert!(matches!(rx.try_recv(), Ok(IngressEvent::Packet(_, _))));
2614
2615        // the replay is dropped on the hash prefix alone
2616        handler.handle_packet(&packet, src).await;
2617        assert!(rx.try_recv().is_err());
2618        assert!(handler.cache.contains_packet(hash));
2619    }
2620
2621    #[tokio::test]
2622    async fn test_undecodable_packet_does_not_poison_cache() {
2623        let secret_key = SecretKey::new(&mut rand_08::thread_rng());
2624        let local_id = pk2id(&secret_key.public_key(SECP256K1));
2625        let (tx, mut rx) = mpsc::channel(16);
2626        let mut handler = IngressHandler::new(tx, local_id);
2627
2628        let remote_key = SecretKey::new(&mut rand_08::thread_rng());
2629        let msg = Message::Ping(Ping {
2630            from: rng_endpoint(&mut rand_08::thread_rng()),
2631            to: rng_endpoint(&mut rand_08::thread_rng()),
2632            expire: u64::MAX,
2633            enr_sq: None,
2634        });
2635        let (packet, hash) = msg.encode(&remote_key);
2636        let src = "10.0.0.1:30303".parse().unwrap();
2637
2638        // a packet carrying the right hash prefix but a corrupt body must not be remembered,
2639        // otherwise it could be used to suppress the real packet
2640        let mut forged = packet.to_vec();
2641        let last = forged.len() - 1;
2642        forged[last] ^= 0xff;
2643        handler.handle_packet(&forged, src).await;
2644        assert!(matches!(rx.try_recv(), Ok(IngressEvent::BadPacket(..))));
2645        assert!(!handler.cache.contains_packet(hash));
2646
2647        // so the genuine packet still gets through
2648        handler.handle_packet(&packet, src).await;
2649        assert!(matches!(rx.try_recv(), Ok(IngressEvent::Packet(_, _))));
2650    }
2651
2652    #[tokio::test]
2653    async fn test_configured_enr_forkid_entry() {
2654        let fork: ForkId = ForkId { hash: ForkHash([220, 233, 108, 45]), next: 0u64 };
2655        let mut disc_conf = Discv4Config::default();
2656        disc_conf.add_eip868_pair("eth", EnrForkIdEntry::from(fork));
2657        let (_discv4, service) = create_discv4_with_config(disc_conf).await;
2658        let eth = service.local_eip_868_enr.get_raw_rlp(b"eth").unwrap();
2659        let fork_entry_id = EnrForkIdEntry::decode(&mut &eth[..]).unwrap();
2660
2661        let raw: [u8; 8] = [0xc7, 0xc6, 0x84, 0xdc, 0xe9, 0x6c, 0x2d, 0x80];
2662        let decoded = EnrForkIdEntry::decode(&mut &raw[..]).unwrap();
2663        let expected = EnrForkIdEntry {
2664            fork_id: ForkId { hash: ForkHash([0xdc, 0xe9, 0x6c, 0x2d]), next: 0 },
2665        };
2666        assert_eq!(expected, fork_entry_id);
2667        assert_eq!(expected, decoded);
2668    }
2669
2670    #[test]
2671    fn test_enr_forkid_entry_decode() {
2672        let raw: [u8; 8] = [0xc7, 0xc6, 0x84, 0xdc, 0xe9, 0x6c, 0x2d, 0x80];
2673        let decoded = EnrForkIdEntry::decode(&mut &raw[..]).unwrap();
2674        let expected = EnrForkIdEntry {
2675            fork_id: ForkId { hash: ForkHash([0xdc, 0xe9, 0x6c, 0x2d]), next: 0 },
2676        };
2677        assert_eq!(expected, decoded);
2678    }
2679
2680    #[test]
2681    fn test_enr_forkid_entry_encode() {
2682        let original = EnrForkIdEntry {
2683            fork_id: ForkId { hash: ForkHash([0xdc, 0xe9, 0x6c, 0x2d]), next: 0 },
2684        };
2685        let expected: [u8; 8] = [0xc7, 0xc6, 0x84, 0xdc, 0xe9, 0x6c, 0x2d, 0x80];
2686        let mut encoded = Vec::with_capacity(expected.len());
2687        original.encode(&mut encoded);
2688        assert_eq!(&expected[..], encoded.as_slice());
2689    }
2690
2691    #[test]
2692    fn test_local_rotator() {
2693        let id = PeerId::random();
2694        let mut rotator = LookupTargetRotator::local_only();
2695        assert_eq!(rotator.next(&id), id);
2696        assert_eq!(rotator.next(&id), id);
2697    }
2698
2699    #[test]
2700    fn test_rotator() {
2701        let id = PeerId::random();
2702        let mut rotator = LookupTargetRotator::default();
2703        assert_eq!(rotator.next(&id), id);
2704        assert_ne!(rotator.next(&id), id);
2705        assert_ne!(rotator.next(&id), id);
2706        assert_ne!(rotator.next(&id), id);
2707        assert_eq!(rotator.next(&id), id);
2708    }
2709
2710    #[tokio::test]
2711    async fn test_pending_ping() {
2712        let (_, mut service) = create_discv4().await;
2713
2714        let local_addr = service.local_addr();
2715
2716        let mut num_inserted = 0;
2717        loop {
2718            let node = NodeRecord::new(local_addr, PeerId::random());
2719            if service.add_node(node) {
2720                num_inserted += 1;
2721                assert!(service.pending_pings.contains_key(&node.id));
2722                assert_eq!(service.pending_pings.len(), num_inserted);
2723                if num_inserted == MAX_NODES_PING {
2724                    break
2725                }
2726            }
2727        }
2728
2729        // `pending_pings` is full, insert into `queued_pings`.
2730        num_inserted = 0;
2731        for _ in 0..MAX_NODES_PING {
2732            let node = NodeRecord::new(local_addr, PeerId::random());
2733            if service.add_node(node) {
2734                num_inserted += 1;
2735                assert!(!service.pending_pings.contains_key(&node.id));
2736                assert_eq!(service.pending_pings.len(), MAX_NODES_PING);
2737                assert_eq!(service.queued_pings.len(), num_inserted);
2738            }
2739        }
2740    }
2741
2742    // Bootstraps with mainnet boot nodes
2743    #[tokio::test(flavor = "multi_thread")]
2744    #[ignore]
2745    async fn test_mainnet_lookup() {
2746        reth_tracing::init_test_tracing();
2747        let fork_id = ForkId { hash: ForkHash(hex!("743f3d89")), next: 16191202 };
2748
2749        let all_nodes = mainnet_nodes();
2750        let config = Discv4Config::builder()
2751            .add_boot_nodes(all_nodes)
2752            .lookup_interval(Duration::from_secs(1))
2753            .add_eip868_pair("eth", fork_id)
2754            .build();
2755        let (_discv4, mut service) = create_discv4_with_config(config).await;
2756
2757        let mut updates = service.update_stream();
2758
2759        let _handle = service.spawn();
2760
2761        let mut table = HashMap::new();
2762        while let Some(update) = updates.next().await {
2763            match update {
2764                DiscoveryUpdate::EnrForkId(record, fork_id) => {
2765                    println!("{record:?}, {fork_id:?}");
2766                }
2767                DiscoveryUpdate::Added(record) => {
2768                    table.insert(record.id, record);
2769                }
2770                DiscoveryUpdate::Removed(id) => {
2771                    table.remove(&id);
2772                }
2773                _ => {}
2774            }
2775            println!("total peers {}", table.len());
2776        }
2777    }
2778
2779    #[tokio::test]
2780    async fn test_mapped_ipv4() {
2781        reth_tracing::init_test_tracing();
2782        let mut rng = rand_08::thread_rng();
2783        let config = Discv4Config::builder().build();
2784        let (_discv4, mut service) = create_discv4_with_config(config).await;
2785
2786        let v4: Ipv4Addr = "0.0.0.0".parse().unwrap();
2787        let v6 = v4.to_ipv6_mapped();
2788        let addr: SocketAddr = (v6, DEFAULT_DISCOVERY_PORT).into();
2789
2790        let ping = Ping {
2791            from: rng_endpoint(&mut rng),
2792            to: rng_endpoint(&mut rng),
2793            expire: service.ping_expiration(),
2794            enr_sq: Some(rng.r#gen()),
2795        };
2796
2797        let id = PeerId::random();
2798        service.on_ping(ping, addr, id, B256::random());
2799
2800        let key = kad_key(id);
2801        match service.kbuckets.entry(&key) {
2802            kbucket::Entry::Present(entry, _) => {
2803                let node_addr = entry.value().record.address;
2804                assert!(node_addr.is_ipv4());
2805                assert_eq!(node_addr, IpAddr::from(v4));
2806            }
2807            _ => unreachable!(),
2808        };
2809    }
2810
2811    #[tokio::test]
2812    async fn test_respect_ping_expiration() {
2813        reth_tracing::init_test_tracing();
2814        let mut rng = rand_08::thread_rng();
2815        let config = Discv4Config::builder().build();
2816        let (_discv4, mut service) = create_discv4_with_config(config).await;
2817
2818        let v4: Ipv4Addr = "0.0.0.0".parse().unwrap();
2819        let v6 = v4.to_ipv6_mapped();
2820        let addr: SocketAddr = (v6, DEFAULT_DISCOVERY_PORT).into();
2821
2822        let ping = Ping {
2823            from: rng_endpoint(&mut rng),
2824            to: rng_endpoint(&mut rng),
2825            expire: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() - 1,
2826            enr_sq: Some(rng.r#gen()),
2827        };
2828
2829        let id = PeerId::random();
2830        service.on_ping(ping, addr, id, B256::random());
2831
2832        let key = kad_key(id);
2833        match service.kbuckets.entry(&key) {
2834            kbucket::Entry::Absent(_) => {}
2835            _ => unreachable!(),
2836        };
2837    }
2838
2839    #[tokio::test]
2840    async fn test_single_lookups() {
2841        reth_tracing::init_test_tracing();
2842
2843        let config = Discv4Config::builder().build();
2844        let (_discv4, mut service) = create_discv4_with_config(config.clone()).await;
2845
2846        let id = PeerId::random();
2847        let key = kad_key(id);
2848        let record = NodeRecord::new("0.0.0.0:0".parse().unwrap(), id);
2849
2850        let _ = service.kbuckets.insert_or_update(
2851            &key,
2852            NodeEntry::new_proven(record),
2853            NodeStatus {
2854                direction: ConnectionDirection::Incoming,
2855                state: ConnectionState::Connected,
2856            },
2857        );
2858
2859        service.lookup_self();
2860        assert_eq!(service.pending_find_nodes.len(), 1);
2861
2862        poll_fn(|cx| {
2863            let _ = service.poll(cx);
2864            assert_eq!(service.pending_find_nodes.len(), 1);
2865
2866            Poll::Ready(())
2867        })
2868        .await;
2869    }
2870
2871    #[tokio::test]
2872    async fn test_on_neighbours_recursive_lookup() {
2873        reth_tracing::init_test_tracing();
2874
2875        let config = Discv4Config::builder().build();
2876        let (_discv4, mut service) = create_discv4_with_config(config.clone()).await;
2877        let (_discv4, mut service2) = create_discv4_with_config(config).await;
2878
2879        let id = PeerId::random();
2880        let key = kad_key(id);
2881        let record = NodeRecord::new("0.0.0.0:0".parse().unwrap(), id);
2882
2883        let _ = service.kbuckets.insert_or_update(
2884            &key,
2885            NodeEntry::new_proven(record),
2886            NodeStatus {
2887                direction: ConnectionDirection::Incoming,
2888                state: ConnectionState::Connected,
2889            },
2890        );
2891        // Needed in this test to populate self.pending_find_nodes for as a prereq to a valid
2892        // on_neighbours request
2893        service.lookup_self();
2894        assert_eq!(service.pending_find_nodes.len(), 1);
2895
2896        poll_fn(|cx| {
2897            let _ = service.poll(cx);
2898            assert_eq!(service.pending_find_nodes.len(), 1);
2899
2900            Poll::Ready(())
2901        })
2902        .await;
2903
2904        let expiry = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs() +
2905            10000000000000;
2906        let msg = Neighbours { nodes: vec![service2.local_node_record], expire: expiry };
2907        service.on_neighbours(msg, record.tcp_addr(), id);
2908        // wait for the processed ping
2909        let event = poll_fn(|cx| service2.poll(cx)).await;
2910        assert_eq!(event, Discv4Event::Ping);
2911        // assert that no find_node req has been added here on top of the initial one, since both
2912        // sides of the endpoint proof is not completed here
2913        assert_eq!(service.pending_find_nodes.len(), 1);
2914        // we now wait for PONG
2915        let event = poll_fn(|cx| service.poll(cx)).await;
2916        assert_eq!(event, Discv4Event::Pong);
2917        // Ideally we want to assert against service.pending_lookup.len() here - but because the
2918        // service2 sends Pong and Ping consecutivley on_ping(), the pending_lookup table gets
2919        // drained almost immediately - and no way to grab the handle to its intermediary state here
2920        // :(
2921        let event = poll_fn(|cx| service.poll(cx)).await;
2922        assert_eq!(event, Discv4Event::Ping);
2923        // assert that we've added the find_node req here after both sides of the endpoint proof is
2924        // done
2925        assert_eq!(service.pending_find_nodes.len(), 2);
2926    }
2927
2928    #[tokio::test]
2929    async fn test_no_local_in_closest() {
2930        reth_tracing::init_test_tracing();
2931
2932        let config = Discv4Config::builder().build();
2933        let (_discv4, mut service) = create_discv4_with_config(config).await;
2934
2935        let target_key = kad_key(PeerId::random());
2936
2937        let id = PeerId::random();
2938        let key = kad_key(id);
2939        let record = NodeRecord::new("0.0.0.0:0".parse().unwrap(), id);
2940
2941        let _ = service.kbuckets.insert_or_update(
2942            &key,
2943            NodeEntry::new(record),
2944            NodeStatus {
2945                direction: ConnectionDirection::Incoming,
2946                state: ConnectionState::Connected,
2947            },
2948        );
2949
2950        let closest = service
2951            .kbuckets
2952            .closest_values(&target_key)
2953            .map(|n| n.value.record)
2954            .take(MAX_NODES_PER_BUCKET)
2955            .collect::<Vec<_>>();
2956
2957        assert_eq!(closest.len(), 1);
2958        assert!(!closest.iter().any(|r| r.id == *service.local_peer_id()));
2959    }
2960
2961    #[tokio::test]
2962    async fn test_random_lookup() {
2963        reth_tracing::init_test_tracing();
2964
2965        let config = Discv4Config::builder().build();
2966        let (_discv4, mut service) = create_discv4_with_config(config).await;
2967
2968        let target = PeerId::random();
2969
2970        let id = PeerId::random();
2971        let key = kad_key(id);
2972        let record = NodeRecord::new("0.0.0.0:0".parse().unwrap(), id);
2973
2974        let _ = service.kbuckets.insert_or_update(
2975            &key,
2976            NodeEntry::new_proven(record),
2977            NodeStatus {
2978                direction: ConnectionDirection::Incoming,
2979                state: ConnectionState::Connected,
2980            },
2981        );
2982
2983        service.lookup(target);
2984        assert_eq!(service.pending_find_nodes.len(), 1);
2985
2986        let ctx = service.pending_find_nodes.values().next().unwrap().lookup_context.clone();
2987
2988        assert_eq!(ctx.target(), target);
2989        assert_eq!(ctx.inner.closest_nodes.borrow().len(), 1);
2990
2991        ctx.add_node(record);
2992        assert_eq!(ctx.inner.closest_nodes.borrow().len(), 1);
2993    }
2994
2995    #[tokio::test]
2996    async fn test_reping_on_find_node_failures() {
2997        reth_tracing::init_test_tracing();
2998
2999        let config = Discv4Config::builder().build();
3000        let (_discv4, mut service) = create_discv4_with_config(config).await;
3001
3002        let target = PeerId::random();
3003
3004        let id = PeerId::random();
3005        let key = kad_key(id);
3006        let record = NodeRecord::new("0.0.0.0:0".parse().unwrap(), id);
3007
3008        let mut entry = NodeEntry::new_proven(record);
3009        entry.find_node_failures = u8::MAX;
3010        let _ = service.kbuckets.insert_or_update(
3011            &key,
3012            entry,
3013            NodeStatus {
3014                direction: ConnectionDirection::Incoming,
3015                state: ConnectionState::Connected,
3016            },
3017        );
3018
3019        service.lookup(target);
3020        assert_eq!(service.pending_find_nodes.len(), 0);
3021        assert_eq!(service.pending_pings.len(), 1);
3022
3023        service.update_on_pong(record, None);
3024
3025        service
3026            .on_entry(record.id, |entry| {
3027                // reset on pong
3028                assert_eq!(entry.find_node_failures, 0);
3029                assert!(entry.has_endpoint_proof);
3030            })
3031            .unwrap();
3032    }
3033
3034    #[tokio::test]
3035    async fn test_service_commands() {
3036        reth_tracing::init_test_tracing();
3037
3038        let config = Discv4Config::builder().build();
3039        let (discv4, mut service) = create_discv4_with_config(config).await;
3040
3041        service.lookup_self();
3042
3043        let _handle = service.spawn();
3044        discv4.send_lookup_self();
3045        let _ = discv4.lookup_self().await;
3046    }
3047
3048    #[tokio::test]
3049    async fn test_requests_timeout() {
3050        reth_tracing::init_test_tracing();
3051        let fork_id = ForkId { hash: ForkHash(hex!("743f3d89")), next: 16191202 };
3052
3053        let config = Discv4Config::builder()
3054            .request_timeout(Duration::from_millis(200))
3055            .ping_expiration(Duration::from_millis(200))
3056            .lookup_neighbours_expiration(Duration::from_millis(200))
3057            .add_eip868_pair("eth", fork_id)
3058            .build();
3059        let (_disv4, mut service) = create_discv4_with_config(config).await;
3060
3061        let id = PeerId::random();
3062        let key = kad_key(id);
3063        let record = NodeRecord::new("0.0.0.0:0".parse().unwrap(), id);
3064
3065        let _ = service.kbuckets.insert_or_update(
3066            &key,
3067            NodeEntry::new_proven(record),
3068            NodeStatus {
3069                direction: ConnectionDirection::Incoming,
3070                state: ConnectionState::Connected,
3071            },
3072        );
3073
3074        service.lookup_self();
3075        assert_eq!(service.pending_find_nodes.len(), 1);
3076
3077        let ctx = service.pending_find_nodes.values().next().unwrap().lookup_context.clone();
3078
3079        service.pending_lookup.insert(record.id, (Instant::now(), ctx));
3080
3081        assert_eq!(service.pending_lookup.len(), 1);
3082
3083        let ping = Ping {
3084            from: service.local_node_record.into(),
3085            to: record.into(),
3086            expire: service.ping_expiration(),
3087            enr_sq: service.enr_seq(),
3088        };
3089        let echo_hash = service.send_packet(Message::Ping(ping), record.udp_addr());
3090        let ping_request = PingRequest {
3091            sent_at: Instant::now(),
3092            node: record,
3093            echo_hash,
3094            reason: PingReason::InitialInsert,
3095        };
3096        service.pending_pings.insert(record.id, ping_request);
3097
3098        assert_eq!(service.pending_pings.len(), 1);
3099
3100        tokio::time::sleep(Duration::from_secs(1)).await;
3101
3102        poll_fn(|cx| {
3103            let _ = service.poll(cx);
3104
3105            assert_eq!(service.pending_find_nodes.len(), 0);
3106            assert_eq!(service.pending_lookup.len(), 0);
3107            assert_eq!(service.pending_pings.len(), 0);
3108
3109            Poll::Ready(())
3110        })
3111        .await;
3112    }
3113
3114    // sends a PING packet with wrong 'to' field and expects a PONG response.
3115    #[tokio::test(flavor = "multi_thread")]
3116    async fn test_check_wrong_to() {
3117        reth_tracing::init_test_tracing();
3118
3119        let config = Discv4Config::builder().external_ip_resolver(None).build();
3120        let (_discv4, mut service_1) = create_discv4_with_config(config.clone()).await;
3121        let (_discv4, mut service_2) = create_discv4_with_config(config).await;
3122
3123        // ping node 2 with wrong to field
3124        let mut ping = Ping {
3125            from: service_1.local_node_record.into(),
3126            to: service_2.local_node_record.into(),
3127            expire: service_1.ping_expiration(),
3128            enr_sq: service_1.enr_seq(),
3129        };
3130        ping.to.address = "192.0.2.0".parse().unwrap();
3131
3132        let echo_hash = service_1.send_packet(Message::Ping(ping), service_2.local_addr());
3133        let ping_request = PingRequest {
3134            sent_at: Instant::now(),
3135            node: service_2.local_node_record,
3136            echo_hash,
3137            reason: PingReason::InitialInsert,
3138        };
3139        service_1.pending_pings.insert(*service_2.local_peer_id(), ping_request);
3140
3141        // wait for the processed ping
3142        let event = poll_fn(|cx| service_2.poll(cx)).await;
3143        assert_eq!(event, Discv4Event::Ping);
3144
3145        // we now wait for PONG
3146        let event = poll_fn(|cx| service_1.poll(cx)).await;
3147        assert_eq!(event, Discv4Event::Pong);
3148        // followed by a ping
3149        let event = poll_fn(|cx| service_1.poll(cx)).await;
3150        assert_eq!(event, Discv4Event::Ping);
3151    }
3152
3153    #[tokio::test(flavor = "multi_thread")]
3154    async fn test_check_ping_pong() {
3155        reth_tracing::init_test_tracing();
3156
3157        let config = Discv4Config::builder().external_ip_resolver(None).build();
3158        let (_discv4, mut service_1) = create_discv4_with_config(config.clone()).await;
3159        let (_discv4, mut service_2) = create_discv4_with_config(config).await;
3160
3161        // send ping from 1 -> 2
3162        service_1.add_node(service_2.local_node_record);
3163
3164        // wait for the processed ping
3165        let event = poll_fn(|cx| service_2.poll(cx)).await;
3166        assert_eq!(event, Discv4Event::Ping);
3167
3168        // node is now in the table but not connected yet
3169        let key1 = kad_key(*service_1.local_peer_id());
3170        match service_2.kbuckets.entry(&key1) {
3171            kbucket::Entry::Present(_entry, status) => {
3172                assert!(!status.is_connected());
3173            }
3174            _ => unreachable!(),
3175        }
3176
3177        // we now wait for PONG
3178        let event = poll_fn(|cx| service_1.poll(cx)).await;
3179        assert_eq!(event, Discv4Event::Pong);
3180
3181        // endpoint is proven
3182        let key2 = kad_key(*service_2.local_peer_id());
3183        match service_1.kbuckets.entry(&key2) {
3184            kbucket::Entry::Present(_entry, status) => {
3185                assert!(status.is_connected());
3186            }
3187            _ => unreachable!(),
3188        }
3189
3190        // we now wait for the PING initiated by 2
3191        let event = poll_fn(|cx| service_1.poll(cx)).await;
3192        assert_eq!(event, Discv4Event::Ping);
3193
3194        // Drain events from service_2 until we see the Pong. Intermediate EnrRequest and
3195        // FindNode events are expected: ENR requests come from the ping handshake, and FindNode
3196        // arrives because service_1 resets its lookup interval on the first bootnode pong.
3197        tokio::time::timeout(Duration::from_secs(5), async {
3198            loop {
3199                let event = poll_fn(|cx| service_2.poll(cx)).await;
3200                match event {
3201                    Discv4Event::Pong => break,
3202                    Discv4Event::EnrRequest | Discv4Event::FindNode => {}
3203                    ev => unreachable!("{ev:?}"),
3204                }
3205            }
3206        })
3207        .await
3208        .expect("timed out waiting for Pong from service_2");
3209
3210        // endpoint is proven
3211        match service_2.kbuckets.entry(&key1) {
3212            kbucket::Entry::Present(_entry, status) => {
3213                assert!(status.is_connected());
3214            }
3215            ev => unreachable!("{ev:?}"),
3216        }
3217    }
3218
3219    #[test]
3220    fn test_insert() {
3221        let local_node_record = rng_record(&mut rand_08::thread_rng());
3222        let mut kbuckets: KBucketsTable<NodeKey, NodeEntry> = KBucketsTable::new(
3223            NodeKey::from(&local_node_record).into(),
3224            Duration::from_secs(60),
3225            MAX_NODES_PER_BUCKET,
3226            None,
3227            None,
3228        );
3229
3230        let new_record = rng_record(&mut rand_08::thread_rng());
3231        let key = kad_key(new_record.id);
3232        match kbuckets.entry(&key) {
3233            kbucket::Entry::Absent(entry) => {
3234                let node = NodeEntry::new(new_record);
3235                let _ = entry.insert(
3236                    node,
3237                    NodeStatus {
3238                        direction: ConnectionDirection::Outgoing,
3239                        state: ConnectionState::Disconnected,
3240                    },
3241                );
3242            }
3243            _ => {
3244                unreachable!()
3245            }
3246        };
3247        match kbuckets.entry(&key) {
3248            kbucket::Entry::Present(_, _) => {}
3249            _ => {
3250                unreachable!()
3251            }
3252        }
3253    }
3254
3255    #[tokio::test]
3256    async fn test_bootnode_not_in_update_stream() {
3257        reth_tracing::init_test_tracing();
3258        let (_, service_1) = create_discv4().await;
3259        let peerid_1 = *service_1.local_peer_id();
3260
3261        let config = Discv4Config::builder().add_boot_node(service_1.local_node_record).build();
3262        service_1.spawn();
3263
3264        let (_, mut service_2) = create_discv4_with_config(config).await;
3265
3266        let mut updates = service_2.update_stream();
3267
3268        service_2.spawn();
3269
3270        // Poll for events for a reasonable time
3271        let mut bootnode_appeared = false;
3272        let timeout = tokio::time::sleep(Duration::from_secs(1));
3273        tokio::pin!(timeout);
3274
3275        loop {
3276            tokio::select! {
3277                Some(update) = updates.next() => {
3278                    if let DiscoveryUpdate::Added(record) = update
3279                        && record.id == peerid_1 {
3280                            bootnode_appeared = true;
3281                            break;
3282                        }
3283                }
3284                _ = &mut timeout => break,
3285            }
3286        }
3287
3288        // Assert bootnode did not appear in update stream
3289        assert!(bootnode_appeared, "Bootnode should appear in update stream");
3290    }
3291
3292    fn insert_proven_node(service: &mut Discv4Service, record: NodeRecord) {
3293        let key = kad_key(record.id);
3294        let _ = service.kbuckets.insert_or_update(
3295            &key,
3296            NodeEntry::new_proven(record),
3297            NodeStatus {
3298                direction: ConnectionDirection::Incoming,
3299                state: ConnectionState::Connected,
3300            },
3301        );
3302    }
3303
3304    fn insert_initial_ping(service: &mut Discv4Service, record: NodeRecord) -> B256 {
3305        let echo_hash = B256::random();
3306        service.pending_pings.insert(
3307            record.id,
3308            PingRequest {
3309                sent_at: Instant::now(),
3310                node: record,
3311                echo_hash,
3312                reason: PingReason::InitialInsert,
3313            },
3314        );
3315        echo_hash
3316    }
3317
3318    fn make_pong(service: &Discv4Service, echo_hash: B256) -> Pong {
3319        Pong {
3320            to: rng_endpoint(&mut rand_08::thread_rng()),
3321            echo: echo_hash,
3322            expire: service.ping_expiration(),
3323            enr_sq: None,
3324        }
3325    }
3326
3327    #[tokio::test]
3328    async fn test_lookup_reset_on_first_bootnode_pong() {
3329        let record = NodeRecord::new("0.0.0.0:0".parse().unwrap(), PeerId::random());
3330        let config = Discv4Config::builder().add_boot_node(record).build();
3331        let (_discv4, mut service) = create_discv4_with_config(config).await;
3332
3333        // 1. initial state
3334        assert!(service.pending_lookup_reset);
3335
3336        // 2. setup: proven bootnode + pending InitialInsert ping
3337        insert_proven_node(&mut service, record);
3338        let echo_hash = insert_initial_ping(&mut service, record);
3339
3340        // 3. input: pong arrives
3341        service.on_pong(make_pong(&service, echo_hash), record.udp_addr(), record.id);
3342
3343        // 4. flag should be consumed — interval was reset
3344        assert!(!service.pending_lookup_reset, "flag should be consumed");
3345    }
3346
3347    #[tokio::test]
3348    async fn test_lookup_reset_fires_only_once() {
3349        let records: Vec<_> = (0..2)
3350            .map(|_| NodeRecord::new("0.0.0.0:0".parse().unwrap(), PeerId::random()))
3351            .collect();
3352        let config = Discv4Config::builder().add_boot_nodes(records.clone()).build();
3353        let (_discv4, mut service) = create_discv4_with_config(config).await;
3354
3355        // 1. setup: two proven bootnodes with pending InitialInsert pings
3356        for &r in &records {
3357            insert_proven_node(&mut service, r);
3358        }
3359        let hashes: Vec<_> =
3360            records.iter().map(|r| insert_initial_ping(&mut service, *r)).collect();
3361
3362        // 2. first pong -> consumes the flag (resets the interval)
3363        service.on_pong(make_pong(&service, hashes[0]), records[0].udp_addr(), records[0].id);
3364        assert!(!service.pending_lookup_reset);
3365
3366        // 3. second pong -> flag already consumed, no second reset
3367        service.on_pong(make_pong(&service, hashes[1]), records[1].udp_addr(), records[1].id);
3368        assert!(!service.pending_lookup_reset);
3369    }
3370
3371    #[tokio::test]
3372    async fn test_lookup_reset_not_triggered_by_non_bootnode() {
3373        let bootnode = NodeRecord::new("0.0.0.0:0".parse().unwrap(), PeerId::random());
3374        let config = Discv4Config::builder().add_boot_node(bootnode).build();
3375        let (_discv4, mut service) = create_discv4_with_config(config).await;
3376
3377        assert!(service.pending_lookup_reset);
3378
3379        // a non-bootnode pong should not consume the flag
3380        let stranger = NodeRecord::new("0.0.0.0:0".parse().unwrap(), PeerId::random());
3381        insert_proven_node(&mut service, stranger);
3382        let echo_hash = insert_initial_ping(&mut service, stranger);
3383        service.on_pong(make_pong(&service, echo_hash), stranger.udp_addr(), stranger.id);
3384
3385        assert!(service.pending_lookup_reset, "flag should not be consumed by non-bootnode");
3386    }
3387
3388    #[tokio::test]
3389    async fn test_lookup_reset_disabled_when_lookup_disabled() {
3390        let config = Discv4Config::builder().enable_lookup(false).build();
3391        let (_discv4, service) = create_discv4_with_config(config).await;
3392
3393        // flag should be false when lookups are disabled
3394        assert!(!service.pending_lookup_reset);
3395    }
3396}