reth_network/session/
active.rs

1//! Represents an established session.
2
3use core::sync::atomic::Ordering;
4use std::{
5    collections::VecDeque,
6    future::Future,
7    net::SocketAddr,
8    pin::Pin,
9    sync::{atomic::AtomicU64, Arc},
10    task::{ready, Context, Poll},
11    time::{Duration, Instant},
12};
13
14use crate::{
15    message::{NewBlockMessage, PeerMessage, PeerResponse, PeerResponseResult},
16    session::{
17        conn::EthRlpxConnection,
18        handle::{ActiveSessionMessage, SessionCommand},
19        SessionId,
20    },
21};
22use alloy_primitives::Sealable;
23use futures::{stream::Fuse, SinkExt, StreamExt};
24use metrics::Gauge;
25use reth_eth_wire::{
26    errors::{EthHandshakeError, EthStreamError},
27    message::{EthBroadcastMessage, RequestPair},
28    Capabilities, DisconnectP2P, DisconnectReason, EthMessage, NetworkPrimitives,
29};
30use reth_eth_wire_types::RawCapabilityMessage;
31use reth_metrics::common::mpsc::MeteredPollSender;
32use reth_network_api::PeerRequest;
33use reth_network_p2p::error::RequestError;
34use reth_network_peers::PeerId;
35use reth_network_types::session::config::INITIAL_REQUEST_TIMEOUT;
36use reth_primitives_traits::Block;
37use rustc_hash::FxHashMap;
38use tokio::{
39    sync::{mpsc::error::TrySendError, oneshot},
40    time::Interval,
41};
42use tokio_stream::wrappers::ReceiverStream;
43use tokio_util::sync::PollSender;
44use tracing::{debug, trace};
45
46// Constants for timeout updating.
47
48/// Minimum timeout value
49const MINIMUM_TIMEOUT: Duration = Duration::from_secs(2);
50/// Maximum timeout value
51const MAXIMUM_TIMEOUT: Duration = INITIAL_REQUEST_TIMEOUT;
52/// How much the new measurements affect the current timeout (X percent)
53const SAMPLE_IMPACT: f64 = 0.1;
54/// Amount of RTTs before timeout
55const TIMEOUT_SCALING: u32 = 3;
56
57/// Restricts the number of queued outgoing messages for larger responses:
58///  - Block Bodies
59///  - Receipts
60///  - Headers
61///  - `PooledTransactions`
62///
63/// With proper softlimits in place (2MB) this targets 10MB (4+1 * 2MB) of outgoing response data.
64///
65/// This parameter serves as backpressure for reading additional requests from the remote.
66/// Once we've queued up more responses than this, the session should prioritize message flushing
67/// before reading any more messages from the remote peer, throttling the peer.
68const MAX_QUEUED_OUTGOING_RESPONSES: usize = 4;
69
70/// The type that advances an established session by listening for incoming messages (from local
71/// node or read from connection) and emitting events back to the
72/// [`SessionManager`](super::SessionManager).
73///
74/// It listens for
75///    - incoming commands from the [`SessionManager`](super::SessionManager)
76///    - incoming _internal_ requests/broadcasts via the request/command channel
77///    - incoming requests/broadcasts _from remote_ via the connection
78///    - responses for handled ETH requests received from the remote peer.
79#[expect(dead_code)]
80pub(crate) struct ActiveSession<N: NetworkPrimitives> {
81    /// Keeps track of request ids.
82    pub(crate) next_id: u64,
83    /// The underlying connection.
84    pub(crate) conn: EthRlpxConnection<N>,
85    /// Identifier of the node we're connected to.
86    pub(crate) remote_peer_id: PeerId,
87    /// The address we're connected to.
88    pub(crate) remote_addr: SocketAddr,
89    /// All capabilities the peer announced
90    pub(crate) remote_capabilities: Arc<Capabilities>,
91    /// Internal identifier of this session
92    pub(crate) session_id: SessionId,
93    /// Incoming commands from the manager
94    pub(crate) commands_rx: ReceiverStream<SessionCommand<N>>,
95    /// Sink to send messages to the [`SessionManager`](super::SessionManager).
96    pub(crate) to_session_manager: MeteredPollSender<ActiveSessionMessage<N>>,
97    /// A message that needs to be delivered to the session manager
98    pub(crate) pending_message_to_session: Option<ActiveSessionMessage<N>>,
99    /// Incoming internal requests which are delegated to the remote peer.
100    pub(crate) internal_request_rx: Fuse<ReceiverStream<PeerRequest<N>>>,
101    /// All requests sent to the remote peer we're waiting on a response
102    pub(crate) inflight_requests: FxHashMap<u64, InflightRequest<PeerRequest<N>>>,
103    /// All requests that were sent by the remote peer and we're waiting on an internal response
104    pub(crate) received_requests_from_remote: Vec<ReceivedRequest<N>>,
105    /// Buffered messages that should be handled and sent to the peer.
106    pub(crate) queued_outgoing: QueuedOutgoingMessages<N>,
107    /// The maximum time we wait for a response from a peer.
108    pub(crate) internal_request_timeout: Arc<AtomicU64>,
109    /// Interval when to check for timed out requests.
110    pub(crate) internal_request_timeout_interval: Interval,
111    /// If an [`ActiveSession`] does not receive a response at all within this duration then it is
112    /// considered a protocol violation and the session will initiate a drop.
113    pub(crate) protocol_breach_request_timeout: Duration,
114    /// Used to reserve a slot to guarantee that the termination message is delivered
115    pub(crate) terminate_message:
116        Option<(PollSender<ActiveSessionMessage<N>>, ActiveSessionMessage<N>)>,
117}
118
119impl<N: NetworkPrimitives> ActiveSession<N> {
120    /// Returns `true` if the session is currently in the process of disconnecting
121    fn is_disconnecting(&self) -> bool {
122        self.conn.inner().is_disconnecting()
123    }
124
125    /// Returns the next request id
126    const fn next_id(&mut self) -> u64 {
127        let id = self.next_id;
128        self.next_id += 1;
129        id
130    }
131
132    /// Shrinks the capacity of the internal buffers.
133    pub fn shrink_to_fit(&mut self) {
134        self.received_requests_from_remote.shrink_to_fit();
135        self.queued_outgoing.shrink_to_fit();
136    }
137
138    /// Returns how many responses we've currently queued up.
139    fn queued_response_count(&self) -> usize {
140        self.queued_outgoing.messages.iter().filter(|m| m.is_response()).count()
141    }
142
143    /// Handle a message read from the connection.
144    ///
145    /// Returns an error if the message is considered to be in violation of the protocol.
146    fn on_incoming_message(&mut self, msg: EthMessage<N>) -> OnIncomingMessageOutcome<N> {
147        /// A macro that handles an incoming request
148        /// This creates a new channel and tries to send the sender half to the session while
149        /// storing the receiver half internally so the pending response can be polled.
150        macro_rules! on_request {
151            ($req:ident, $resp_item:ident, $req_item:ident) => {{
152                let RequestPair { request_id, message: request } = $req;
153                let (tx, response) = oneshot::channel();
154                let received = ReceivedRequest {
155                    request_id,
156                    rx: PeerResponse::$resp_item { response },
157                    received: Instant::now(),
158                };
159                self.received_requests_from_remote.push(received);
160                self.try_emit_request(PeerMessage::EthRequest(PeerRequest::$req_item {
161                    request,
162                    response: tx,
163                }))
164                .into()
165            }};
166        }
167
168        /// Processes a response received from the peer
169        macro_rules! on_response {
170            ($resp:ident, $item:ident) => {{
171                let RequestPair { request_id, message } = $resp;
172                if let Some(req) = self.inflight_requests.remove(&request_id) {
173                    match req.request {
174                        RequestState::Waiting(PeerRequest::$item { response, .. }) => {
175                            let _ = response.send(Ok(message));
176                            self.update_request_timeout(req.timestamp, Instant::now());
177                        }
178                        RequestState::Waiting(request) => {
179                            request.send_bad_response();
180                        }
181                        RequestState::TimedOut => {
182                            // request was already timed out internally
183                            self.update_request_timeout(req.timestamp, Instant::now());
184                        }
185                    }
186                } else {
187                    // we received a response to a request we never sent
188                    self.on_bad_message();
189                }
190
191                OnIncomingMessageOutcome::Ok
192            }};
193        }
194
195        match msg {
196            message @ EthMessage::Status(_) => OnIncomingMessageOutcome::BadMessage {
197                error: EthStreamError::EthHandshakeError(EthHandshakeError::StatusNotInHandshake),
198                message,
199            },
200            EthMessage::NewBlockHashes(msg) => {
201                self.try_emit_broadcast(PeerMessage::NewBlockHashes(msg)).into()
202            }
203            EthMessage::NewBlock(msg) => {
204                let block =
205                    NewBlockMessage { hash: msg.block.header().hash_slow(), block: Arc::new(*msg) };
206                self.try_emit_broadcast(PeerMessage::NewBlock(block)).into()
207            }
208            EthMessage::Transactions(msg) => {
209                self.try_emit_broadcast(PeerMessage::ReceivedTransaction(msg)).into()
210            }
211            EthMessage::NewPooledTransactionHashes66(msg) => {
212                self.try_emit_broadcast(PeerMessage::PooledTransactions(msg.into())).into()
213            }
214            EthMessage::NewPooledTransactionHashes68(msg) => {
215                if msg.hashes.len() != msg.types.len() || msg.hashes.len() != msg.sizes.len() {
216                    return OnIncomingMessageOutcome::BadMessage {
217                        error: EthStreamError::TransactionHashesInvalidLenOfFields {
218                            hashes_len: msg.hashes.len(),
219                            types_len: msg.types.len(),
220                            sizes_len: msg.sizes.len(),
221                        },
222                        message: EthMessage::NewPooledTransactionHashes68(msg),
223                    }
224                }
225                self.try_emit_broadcast(PeerMessage::PooledTransactions(msg.into())).into()
226            }
227            EthMessage::GetBlockHeaders(req) => {
228                on_request!(req, BlockHeaders, GetBlockHeaders)
229            }
230            EthMessage::BlockHeaders(resp) => {
231                on_response!(resp, GetBlockHeaders)
232            }
233            EthMessage::GetBlockBodies(req) => {
234                on_request!(req, BlockBodies, GetBlockBodies)
235            }
236            EthMessage::BlockBodies(resp) => {
237                on_response!(resp, GetBlockBodies)
238            }
239            EthMessage::GetPooledTransactions(req) => {
240                on_request!(req, PooledTransactions, GetPooledTransactions)
241            }
242            EthMessage::PooledTransactions(resp) => {
243                on_response!(resp, GetPooledTransactions)
244            }
245            EthMessage::GetNodeData(req) => {
246                on_request!(req, NodeData, GetNodeData)
247            }
248            EthMessage::NodeData(resp) => {
249                on_response!(resp, GetNodeData)
250            }
251            EthMessage::GetReceipts(req) => {
252                on_request!(req, Receipts, GetReceipts)
253            }
254            EthMessage::Receipts(resp) => {
255                on_response!(resp, GetReceipts)
256            }
257            EthMessage::Other(bytes) => self.try_emit_broadcast(PeerMessage::Other(bytes)).into(),
258        }
259    }
260
261    /// Handle an internal peer request that will be sent to the remote.
262    fn on_internal_peer_request(&mut self, request: PeerRequest<N>, deadline: Instant) {
263        let request_id = self.next_id();
264        let msg = request.create_request_message(request_id);
265        self.queued_outgoing.push_back(msg.into());
266        let req = InflightRequest {
267            request: RequestState::Waiting(request),
268            timestamp: Instant::now(),
269            deadline,
270        };
271        self.inflight_requests.insert(request_id, req);
272    }
273
274    /// Handle a message received from the internal network
275    fn on_internal_peer_message(&mut self, msg: PeerMessage<N>) {
276        match msg {
277            PeerMessage::NewBlockHashes(msg) => {
278                self.queued_outgoing.push_back(EthMessage::NewBlockHashes(msg).into());
279            }
280            PeerMessage::NewBlock(msg) => {
281                self.queued_outgoing.push_back(EthBroadcastMessage::NewBlock(msg.block).into());
282            }
283            PeerMessage::PooledTransactions(msg) => {
284                if msg.is_valid_for_version(self.conn.version()) {
285                    self.queued_outgoing.push_back(EthMessage::from(msg).into());
286                }
287            }
288            PeerMessage::EthRequest(req) => {
289                let deadline = self.request_deadline();
290                self.on_internal_peer_request(req, deadline);
291            }
292            PeerMessage::SendTransactions(msg) => {
293                self.queued_outgoing.push_back(EthBroadcastMessage::Transactions(msg).into());
294            }
295            PeerMessage::ReceivedTransaction(_) => {
296                unreachable!("Not emitted by network")
297            }
298            PeerMessage::Other(other) => {
299                self.queued_outgoing.push_back(OutgoingMessage::Raw(other));
300            }
301        }
302    }
303
304    /// Returns the deadline timestamp at which the request times out
305    fn request_deadline(&self) -> Instant {
306        Instant::now() +
307            Duration::from_millis(self.internal_request_timeout.load(Ordering::Relaxed))
308    }
309
310    /// Handle a Response to the peer
311    ///
312    /// This will queue the response to be sent to the peer
313    fn handle_outgoing_response(&mut self, id: u64, resp: PeerResponseResult<N>) {
314        match resp.try_into_message(id) {
315            Ok(msg) => {
316                self.queued_outgoing.push_back(msg.into());
317            }
318            Err(err) => {
319                debug!(target: "net", %err, "Failed to respond to received request");
320            }
321        }
322    }
323
324    /// Send a message back to the [`SessionManager`](super::SessionManager).
325    ///
326    /// Returns the message if the bounded channel is currently unable to handle this message.
327    #[expect(clippy::result_large_err)]
328    fn try_emit_broadcast(&self, message: PeerMessage<N>) -> Result<(), ActiveSessionMessage<N>> {
329        let Some(sender) = self.to_session_manager.inner().get_ref() else { return Ok(()) };
330
331        match sender
332            .try_send(ActiveSessionMessage::ValidMessage { peer_id: self.remote_peer_id, message })
333        {
334            Ok(_) => Ok(()),
335            Err(err) => {
336                trace!(
337                    target: "net",
338                    %err,
339                    "no capacity for incoming broadcast",
340                );
341                match err {
342                    TrySendError::Full(msg) => Err(msg),
343                    TrySendError::Closed(_) => Ok(()),
344                }
345            }
346        }
347    }
348
349    /// Send a message back to the [`SessionManager`](super::SessionManager)
350    /// covering both broadcasts and incoming requests.
351    ///
352    /// Returns the message if the bounded channel is currently unable to handle this message.
353    #[expect(clippy::result_large_err)]
354    fn try_emit_request(&self, message: PeerMessage<N>) -> Result<(), ActiveSessionMessage<N>> {
355        let Some(sender) = self.to_session_manager.inner().get_ref() else { return Ok(()) };
356
357        match sender
358            .try_send(ActiveSessionMessage::ValidMessage { peer_id: self.remote_peer_id, message })
359        {
360            Ok(_) => Ok(()),
361            Err(err) => {
362                trace!(
363                    target: "net",
364                    %err,
365                    "no capacity for incoming request",
366                );
367                match err {
368                    TrySendError::Full(msg) => Err(msg),
369                    TrySendError::Closed(_) => {
370                        // Note: this would mean the `SessionManager` was dropped, which is already
371                        // handled by checking if the command receiver channel has been closed.
372                        Ok(())
373                    }
374                }
375            }
376        }
377    }
378
379    /// Notify the manager that the peer sent a bad message
380    fn on_bad_message(&self) {
381        let Some(sender) = self.to_session_manager.inner().get_ref() else { return };
382        let _ = sender.try_send(ActiveSessionMessage::BadMessage { peer_id: self.remote_peer_id });
383    }
384
385    /// Report back that this session has been closed.
386    fn emit_disconnect(&mut self, cx: &mut Context<'_>) -> Poll<()> {
387        trace!(target: "net::session", remote_peer_id=?self.remote_peer_id, "emitting disconnect");
388        let msg = ActiveSessionMessage::Disconnected {
389            peer_id: self.remote_peer_id,
390            remote_addr: self.remote_addr,
391        };
392
393        self.terminate_message = Some((self.to_session_manager.inner().clone(), msg));
394        self.poll_terminate_message(cx).expect("message is set")
395    }
396
397    /// Report back that this session has been closed due to an error
398    fn close_on_error(&mut self, error: EthStreamError, cx: &mut Context<'_>) -> Poll<()> {
399        let msg = ActiveSessionMessage::ClosedOnConnectionError {
400            peer_id: self.remote_peer_id,
401            remote_addr: self.remote_addr,
402            error,
403        };
404        self.terminate_message = Some((self.to_session_manager.inner().clone(), msg));
405        self.poll_terminate_message(cx).expect("message is set")
406    }
407
408    /// Starts the disconnect process
409    fn start_disconnect(&mut self, reason: DisconnectReason) -> Result<(), EthStreamError> {
410        Ok(self.conn.inner_mut().start_disconnect(reason)?)
411    }
412
413    /// Flushes the disconnect message and emits the corresponding message
414    fn poll_disconnect(&mut self, cx: &mut Context<'_>) -> Poll<()> {
415        debug_assert!(self.is_disconnecting(), "not disconnecting");
416
417        // try to close the flush out the remaining Disconnect message
418        let _ = ready!(self.conn.poll_close_unpin(cx));
419        self.emit_disconnect(cx)
420    }
421
422    /// Attempts to disconnect by sending the given disconnect reason
423    fn try_disconnect(&mut self, reason: DisconnectReason, cx: &mut Context<'_>) -> Poll<()> {
424        match self.start_disconnect(reason) {
425            Ok(()) => {
426                // we're done
427                self.poll_disconnect(cx)
428            }
429            Err(err) => {
430                debug!(target: "net::session", %err, remote_peer_id=?self.remote_peer_id, "could not send disconnect");
431                self.close_on_error(err, cx)
432            }
433        }
434    }
435
436    /// Checks for _internally_ timed out requests.
437    ///
438    /// If a requests misses its deadline, then it is timed out internally.
439    /// If a request misses the `protocol_breach_request_timeout` then this session is considered in
440    /// protocol violation and will close.
441    ///
442    /// Returns `true` if a peer missed the `protocol_breach_request_timeout`, in which case the
443    /// session should be terminated.
444    #[must_use]
445    fn check_timed_out_requests(&mut self, now: Instant) -> bool {
446        for (id, req) in &mut self.inflight_requests {
447            if req.is_timed_out(now) {
448                if req.is_waiting() {
449                    debug!(target: "net::session", ?id, remote_peer_id=?self.remote_peer_id, "timed out outgoing request");
450                    req.timeout();
451                } else if now - req.timestamp > self.protocol_breach_request_timeout {
452                    return true
453                }
454            }
455        }
456
457        false
458    }
459
460    /// Updates the request timeout with a request's timestamps
461    fn update_request_timeout(&mut self, sent: Instant, received: Instant) {
462        let elapsed = received.saturating_duration_since(sent);
463
464        let current = Duration::from_millis(self.internal_request_timeout.load(Ordering::Relaxed));
465        let request_timeout = calculate_new_timeout(current, elapsed);
466        self.internal_request_timeout.store(request_timeout.as_millis() as u64, Ordering::Relaxed);
467        self.internal_request_timeout_interval = tokio::time::interval(request_timeout);
468    }
469
470    /// If a termination message is queued this will try to send it
471    fn poll_terminate_message(&mut self, cx: &mut Context<'_>) -> Option<Poll<()>> {
472        let (mut tx, msg) = self.terminate_message.take()?;
473        match tx.poll_reserve(cx) {
474            Poll::Pending => {
475                self.terminate_message = Some((tx, msg));
476                return Some(Poll::Pending)
477            }
478            Poll::Ready(Ok(())) => {
479                let _ = tx.send_item(msg);
480            }
481            Poll::Ready(Err(_)) => {
482                // channel closed
483            }
484        }
485        // terminate the task
486        Some(Poll::Ready(()))
487    }
488}
489
490impl<N: NetworkPrimitives> Future for ActiveSession<N> {
491    type Output = ();
492
493    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
494        let this = self.get_mut();
495
496        // if the session is terminate we have to send the termination message before we can close
497        if let Some(terminate) = this.poll_terminate_message(cx) {
498            return terminate
499        }
500
501        if this.is_disconnecting() {
502            return this.poll_disconnect(cx)
503        }
504
505        // The receive loop can be CPU intensive since it involves message decoding which could take
506        // up a lot of resources and increase latencies for other sessions if not yielded manually.
507        // If the budget is exhausted we manually yield back control to the (coop) scheduler. This
508        // manual yield point should prevent situations where polling appears to be frozen. See also <https://tokio.rs/blog/2020-04-preemption>
509        // And tokio's docs on cooperative scheduling <https://docs.rs/tokio/latest/tokio/task/#cooperative-scheduling>
510        let mut budget = 4;
511
512        // The main poll loop that drives the session
513        'main: loop {
514            let mut progress = false;
515
516            // we prioritize incoming commands sent from the session manager
517            loop {
518                match this.commands_rx.poll_next_unpin(cx) {
519                    Poll::Pending => break,
520                    Poll::Ready(None) => {
521                        // this is only possible when the manager was dropped, in which case we also
522                        // terminate this session
523                        return Poll::Ready(())
524                    }
525                    Poll::Ready(Some(cmd)) => {
526                        progress = true;
527                        match cmd {
528                            SessionCommand::Disconnect { reason } => {
529                                debug!(
530                                    target: "net::session",
531                                    ?reason,
532                                    remote_peer_id=?this.remote_peer_id,
533                                    "Received disconnect command for session"
534                                );
535                                let reason =
536                                    reason.unwrap_or(DisconnectReason::DisconnectRequested);
537
538                                return this.try_disconnect(reason, cx)
539                            }
540                            SessionCommand::Message(msg) => {
541                                this.on_internal_peer_message(msg);
542                            }
543                        }
544                    }
545                }
546            }
547
548            let deadline = this.request_deadline();
549
550            while let Poll::Ready(Some(req)) = this.internal_request_rx.poll_next_unpin(cx) {
551                progress = true;
552                this.on_internal_peer_request(req, deadline);
553            }
554
555            // Advance all active requests.
556            // We remove each request one by one and add them back.
557            for idx in (0..this.received_requests_from_remote.len()).rev() {
558                let mut req = this.received_requests_from_remote.swap_remove(idx);
559                match req.rx.poll(cx) {
560                    Poll::Pending => {
561                        // not ready yet
562                        this.received_requests_from_remote.push(req);
563                    }
564                    Poll::Ready(resp) => {
565                        this.handle_outgoing_response(req.request_id, resp);
566                    }
567                }
568            }
569
570            // Send messages by advancing the sink and queuing in buffered messages
571            while this.conn.poll_ready_unpin(cx).is_ready() {
572                if let Some(msg) = this.queued_outgoing.pop_front() {
573                    progress = true;
574                    let res = match msg {
575                        OutgoingMessage::Eth(msg) => this.conn.start_send_unpin(msg),
576                        OutgoingMessage::Broadcast(msg) => this.conn.start_send_broadcast(msg),
577                        OutgoingMessage::Raw(msg) => this.conn.start_send_raw(msg),
578                    };
579                    if let Err(err) = res {
580                        debug!(target: "net::session", %err, remote_peer_id=?this.remote_peer_id, "failed to send message");
581                        // notify the manager
582                        return this.close_on_error(err, cx)
583                    }
584                } else {
585                    // no more messages to send over the wire
586                    break
587                }
588            }
589
590            // read incoming messages from the wire
591            'receive: loop {
592                // ensure we still have enough budget for another iteration
593                budget -= 1;
594                if budget == 0 {
595                    // make sure we're woken up again
596                    cx.waker().wake_by_ref();
597                    break 'main
598                }
599
600                // try to resend the pending message that we could not send because the channel was
601                // full. [`PollSender`] will ensure that we're woken up again when the channel is
602                // ready to receive the message, and will only error if the channel is closed.
603                if let Some(msg) = this.pending_message_to_session.take() {
604                    match this.to_session_manager.poll_reserve(cx) {
605                        Poll::Ready(Ok(_)) => {
606                            let _ = this.to_session_manager.send_item(msg);
607                        }
608                        Poll::Ready(Err(_)) => return Poll::Ready(()),
609                        Poll::Pending => {
610                            this.pending_message_to_session = Some(msg);
611                            break 'receive
612                        }
613                    };
614                }
615
616                // check whether we should throttle incoming messages
617                if this.received_requests_from_remote.len() > MAX_QUEUED_OUTGOING_RESPONSES {
618                    // we're currently waiting for the responses to the peer's requests which aren't
619                    // queued as outgoing yet
620                    //
621                    // Note: we don't need to register the waker here because we polled the requests
622                    // above
623                    break 'receive
624                }
625
626                // we also need to check if we have multiple responses queued up
627                if this.queued_outgoing.messages.len() > MAX_QUEUED_OUTGOING_RESPONSES &&
628                    this.queued_response_count() > MAX_QUEUED_OUTGOING_RESPONSES
629                {
630                    // if we've queued up more responses than allowed, we don't poll for new
631                    // messages and break the receive loop early
632                    //
633                    // Note: we don't need to register the waker here because we still have
634                    // queued messages and the sink impl registered the waker because we've
635                    // already advanced it to `Pending` earlier
636                    break 'receive
637                }
638
639                match this.conn.poll_next_unpin(cx) {
640                    Poll::Pending => break,
641                    Poll::Ready(None) => {
642                        if this.is_disconnecting() {
643                            break
644                        }
645                        debug!(target: "net::session", remote_peer_id=?this.remote_peer_id, "eth stream completed");
646                        return this.emit_disconnect(cx)
647                    }
648                    Poll::Ready(Some(res)) => {
649                        match res {
650                            Ok(msg) => {
651                                trace!(target: "net::session", msg_id=?msg.message_id(), remote_peer_id=?this.remote_peer_id, "received eth message");
652                                // decode and handle message
653                                match this.on_incoming_message(msg) {
654                                    OnIncomingMessageOutcome::Ok => {
655                                        // handled successfully
656                                        progress = true;
657                                    }
658                                    OnIncomingMessageOutcome::BadMessage { error, message } => {
659                                        debug!(target: "net::session", %error, msg=?message, remote_peer_id=?this.remote_peer_id, "received invalid protocol message");
660                                        return this.close_on_error(error, cx)
661                                    }
662                                    OnIncomingMessageOutcome::NoCapacity(msg) => {
663                                        // failed to send due to lack of capacity
664                                        this.pending_message_to_session = Some(msg);
665                                    }
666                                }
667                            }
668                            Err(err) => {
669                                debug!(target: "net::session", %err, remote_peer_id=?this.remote_peer_id, "failed to receive message");
670                                return this.close_on_error(err, cx)
671                            }
672                        }
673                    }
674                }
675            }
676
677            if !progress {
678                break 'main
679            }
680        }
681
682        while this.internal_request_timeout_interval.poll_tick(cx).is_ready() {
683            // check for timed out requests
684            if this.check_timed_out_requests(Instant::now()) {
685                if let Poll::Ready(Ok(_)) = this.to_session_manager.poll_reserve(cx) {
686                    let msg = ActiveSessionMessage::ProtocolBreach { peer_id: this.remote_peer_id };
687                    this.pending_message_to_session = Some(msg);
688                }
689            }
690        }
691
692        this.shrink_to_fit();
693
694        Poll::Pending
695    }
696}
697
698/// Tracks a request received from the peer
699pub(crate) struct ReceivedRequest<N: NetworkPrimitives> {
700    /// Protocol Identifier
701    request_id: u64,
702    /// Receiver half of the channel that's supposed to receive the proper response.
703    rx: PeerResponse<N>,
704    /// Timestamp when we read this msg from the wire.
705    #[expect(dead_code)]
706    received: Instant,
707}
708
709/// A request that waits for a response from the peer
710pub(crate) struct InflightRequest<R> {
711    /// Request we sent to peer and the internal response channel
712    request: RequestState<R>,
713    /// Instant when the request was sent
714    timestamp: Instant,
715    /// Time limit for the response
716    deadline: Instant,
717}
718
719// === impl InflightRequest ===
720
721impl<N: NetworkPrimitives> InflightRequest<PeerRequest<N>> {
722    /// Returns true if the request is timedout
723    #[inline]
724    fn is_timed_out(&self, now: Instant) -> bool {
725        now > self.deadline
726    }
727
728    /// Returns true if we're still waiting for a response
729    #[inline]
730    const fn is_waiting(&self) -> bool {
731        matches!(self.request, RequestState::Waiting(_))
732    }
733
734    /// This will timeout the request by sending an error response to the internal channel
735    fn timeout(&mut self) {
736        let mut req = RequestState::TimedOut;
737        std::mem::swap(&mut self.request, &mut req);
738
739        if let RequestState::Waiting(req) = req {
740            req.send_err_response(RequestError::Timeout);
741        }
742    }
743}
744
745/// All outcome variants when handling an incoming message
746enum OnIncomingMessageOutcome<N: NetworkPrimitives> {
747    /// Message successfully handled.
748    Ok,
749    /// Message is considered to be in violation of the protocol
750    BadMessage { error: EthStreamError, message: EthMessage<N> },
751    /// Currently no capacity to handle the message
752    NoCapacity(ActiveSessionMessage<N>),
753}
754
755impl<N: NetworkPrimitives> From<Result<(), ActiveSessionMessage<N>>>
756    for OnIncomingMessageOutcome<N>
757{
758    fn from(res: Result<(), ActiveSessionMessage<N>>) -> Self {
759        match res {
760            Ok(_) => Self::Ok,
761            Err(msg) => Self::NoCapacity(msg),
762        }
763    }
764}
765
766enum RequestState<R> {
767    /// Waiting for the response
768    Waiting(R),
769    /// Request already timed out
770    TimedOut,
771}
772
773/// Outgoing messages that can be sent over the wire.
774pub(crate) enum OutgoingMessage<N: NetworkPrimitives> {
775    /// A message that is owned.
776    Eth(EthMessage<N>),
777    /// A message that may be shared by multiple sessions.
778    Broadcast(EthBroadcastMessage<N>),
779    /// A raw capability message
780    Raw(RawCapabilityMessage),
781}
782
783impl<N: NetworkPrimitives> OutgoingMessage<N> {
784    /// Returns true if this is a response.
785    const fn is_response(&self) -> bool {
786        match self {
787            Self::Eth(msg) => msg.is_response(),
788            _ => false,
789        }
790    }
791}
792
793impl<N: NetworkPrimitives> From<EthMessage<N>> for OutgoingMessage<N> {
794    fn from(value: EthMessage<N>) -> Self {
795        Self::Eth(value)
796    }
797}
798
799impl<N: NetworkPrimitives> From<EthBroadcastMessage<N>> for OutgoingMessage<N> {
800    fn from(value: EthBroadcastMessage<N>) -> Self {
801        Self::Broadcast(value)
802    }
803}
804
805/// Calculates a new timeout using an updated estimation of the RTT
806#[inline]
807fn calculate_new_timeout(current_timeout: Duration, estimated_rtt: Duration) -> Duration {
808    let new_timeout = estimated_rtt.mul_f64(SAMPLE_IMPACT) * TIMEOUT_SCALING;
809
810    // this dampens sudden changes by taking a weighted mean of the old and new values
811    let smoothened_timeout = current_timeout.mul_f64(1.0 - SAMPLE_IMPACT) + new_timeout;
812
813    smoothened_timeout.clamp(MINIMUM_TIMEOUT, MAXIMUM_TIMEOUT)
814}
815
816/// A helper struct that wraps the queue of outgoing messages and a metric to track their count
817pub(crate) struct QueuedOutgoingMessages<N: NetworkPrimitives> {
818    messages: VecDeque<OutgoingMessage<N>>,
819    count: Gauge,
820}
821
822impl<N: NetworkPrimitives> QueuedOutgoingMessages<N> {
823    pub(crate) const fn new(metric: Gauge) -> Self {
824        Self { messages: VecDeque::new(), count: metric }
825    }
826
827    pub(crate) fn push_back(&mut self, message: OutgoingMessage<N>) {
828        self.messages.push_back(message);
829        self.count.increment(1);
830    }
831
832    pub(crate) fn pop_front(&mut self) -> Option<OutgoingMessage<N>> {
833        self.messages.pop_front().inspect(|_| self.count.decrement(1))
834    }
835
836    pub(crate) fn shrink_to_fit(&mut self) {
837        self.messages.shrink_to_fit();
838    }
839}
840
841#[cfg(test)]
842mod tests {
843    use super::*;
844    use crate::session::{handle::PendingSessionEvent, start_pending_incoming_session};
845    use alloy_eips::eip2124::ForkFilter;
846    use reth_chainspec::MAINNET;
847    use reth_ecies::stream::ECIESStream;
848    use reth_eth_wire::{
849        handshake::EthHandshake, EthNetworkPrimitives, EthStream, GetBlockBodies,
850        HelloMessageWithProtocols, P2PStream, Status, StatusBuilder, UnauthedEthStream,
851        UnauthedP2PStream,
852    };
853    use reth_ethereum_forks::EthereumHardfork;
854    use reth_network_peers::pk2id;
855    use reth_network_types::session::config::PROTOCOL_BREACH_REQUEST_TIMEOUT;
856    use secp256k1::{SecretKey, SECP256K1};
857    use tokio::{
858        net::{TcpListener, TcpStream},
859        sync::mpsc,
860    };
861
862    /// Returns a testing `HelloMessage` and new secretkey
863    fn eth_hello(server_key: &SecretKey) -> HelloMessageWithProtocols {
864        HelloMessageWithProtocols::builder(pk2id(&server_key.public_key(SECP256K1))).build()
865    }
866
867    struct SessionBuilder<N: NetworkPrimitives = EthNetworkPrimitives> {
868        _remote_capabilities: Arc<Capabilities>,
869        active_session_tx: mpsc::Sender<ActiveSessionMessage<N>>,
870        active_session_rx: ReceiverStream<ActiveSessionMessage<N>>,
871        to_sessions: Vec<mpsc::Sender<SessionCommand<N>>>,
872        secret_key: SecretKey,
873        local_peer_id: PeerId,
874        hello: HelloMessageWithProtocols,
875        status: Status,
876        fork_filter: ForkFilter,
877        next_id: usize,
878    }
879
880    impl<N: NetworkPrimitives> SessionBuilder<N> {
881        fn next_id(&mut self) -> SessionId {
882            let id = self.next_id;
883            self.next_id += 1;
884            SessionId(id)
885        }
886
887        /// Connects a new Eth stream and executes the given closure with that established stream
888        fn with_client_stream<F, O>(
889            &self,
890            local_addr: SocketAddr,
891            f: F,
892        ) -> Pin<Box<dyn Future<Output = ()> + Send>>
893        where
894            F: FnOnce(EthStream<P2PStream<ECIESStream<TcpStream>>, N>) -> O + Send + 'static,
895            O: Future<Output = ()> + Send + Sync,
896        {
897            let status = self.status;
898            let fork_filter = self.fork_filter.clone();
899            let local_peer_id = self.local_peer_id;
900            let mut hello = self.hello.clone();
901            let key = SecretKey::new(&mut rand_08::thread_rng());
902            hello.id = pk2id(&key.public_key(SECP256K1));
903            Box::pin(async move {
904                let outgoing = TcpStream::connect(local_addr).await.unwrap();
905                let sink = ECIESStream::connect(outgoing, key, local_peer_id).await.unwrap();
906
907                let (p2p_stream, _) = UnauthedP2PStream::new(sink).handshake(hello).await.unwrap();
908
909                let (client_stream, _) = UnauthedEthStream::new(p2p_stream)
910                    .handshake(status, fork_filter)
911                    .await
912                    .unwrap();
913                f(client_stream).await
914            })
915        }
916
917        async fn connect_incoming(&mut self, stream: TcpStream) -> ActiveSession<N> {
918            let remote_addr = stream.local_addr().unwrap();
919            let session_id = self.next_id();
920            let (_disconnect_tx, disconnect_rx) = oneshot::channel();
921            let (pending_sessions_tx, pending_sessions_rx) = mpsc::channel(1);
922
923            tokio::task::spawn(start_pending_incoming_session(
924                Arc::new(EthHandshake::default()),
925                disconnect_rx,
926                session_id,
927                stream,
928                pending_sessions_tx,
929                remote_addr,
930                self.secret_key,
931                self.hello.clone(),
932                self.status,
933                self.fork_filter.clone(),
934                Default::default(),
935            ));
936
937            let mut stream = ReceiverStream::new(pending_sessions_rx);
938
939            match stream.next().await.unwrap() {
940                PendingSessionEvent::Established {
941                    session_id,
942                    remote_addr,
943                    peer_id,
944                    capabilities,
945                    conn,
946                    ..
947                } => {
948                    let (_to_session_tx, messages_rx) = mpsc::channel(10);
949                    let (commands_to_session, commands_rx) = mpsc::channel(10);
950                    let poll_sender = PollSender::new(self.active_session_tx.clone());
951
952                    self.to_sessions.push(commands_to_session);
953
954                    ActiveSession {
955                        next_id: 0,
956                        remote_peer_id: peer_id,
957                        remote_addr,
958                        remote_capabilities: Arc::clone(&capabilities),
959                        session_id,
960                        commands_rx: ReceiverStream::new(commands_rx),
961                        to_session_manager: MeteredPollSender::new(
962                            poll_sender,
963                            "network_active_session",
964                        ),
965                        pending_message_to_session: None,
966                        internal_request_rx: ReceiverStream::new(messages_rx).fuse(),
967                        inflight_requests: Default::default(),
968                        conn,
969                        queued_outgoing: QueuedOutgoingMessages::new(Gauge::noop()),
970                        received_requests_from_remote: Default::default(),
971                        internal_request_timeout_interval: tokio::time::interval(
972                            INITIAL_REQUEST_TIMEOUT,
973                        ),
974                        internal_request_timeout: Arc::new(AtomicU64::new(
975                            INITIAL_REQUEST_TIMEOUT.as_millis() as u64,
976                        )),
977                        protocol_breach_request_timeout: PROTOCOL_BREACH_REQUEST_TIMEOUT,
978                        terminate_message: None,
979                    }
980                }
981                ev => {
982                    panic!("unexpected message {ev:?}")
983                }
984            }
985        }
986    }
987
988    impl Default for SessionBuilder {
989        fn default() -> Self {
990            let (active_session_tx, active_session_rx) = mpsc::channel(100);
991
992            let (secret_key, pk) = SECP256K1.generate_keypair(&mut rand_08::thread_rng());
993            let local_peer_id = pk2id(&pk);
994
995            Self {
996                next_id: 0,
997                _remote_capabilities: Arc::new(Capabilities::from(vec![])),
998                active_session_tx,
999                active_session_rx: ReceiverStream::new(active_session_rx),
1000                to_sessions: vec![],
1001                hello: eth_hello(&secret_key),
1002                secret_key,
1003                local_peer_id,
1004                status: StatusBuilder::default().build(),
1005                fork_filter: MAINNET
1006                    .hardfork_fork_filter(EthereumHardfork::Frontier)
1007                    .expect("The Frontier fork filter should exist on mainnet"),
1008            }
1009        }
1010    }
1011
1012    #[tokio::test(flavor = "multi_thread")]
1013    async fn test_disconnect() {
1014        let mut builder = SessionBuilder::default();
1015
1016        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
1017        let local_addr = listener.local_addr().unwrap();
1018
1019        let expected_disconnect = DisconnectReason::UselessPeer;
1020
1021        let fut = builder.with_client_stream(local_addr, move |mut client_stream| async move {
1022            let msg = client_stream.next().await.unwrap().unwrap_err();
1023            assert_eq!(msg.as_disconnected().unwrap(), expected_disconnect);
1024        });
1025
1026        tokio::task::spawn(async move {
1027            let (incoming, _) = listener.accept().await.unwrap();
1028            let mut session = builder.connect_incoming(incoming).await;
1029
1030            session.start_disconnect(expected_disconnect).unwrap();
1031            session.await
1032        });
1033
1034        fut.await;
1035    }
1036
1037    #[tokio::test(flavor = "multi_thread")]
1038    async fn handle_dropped_stream() {
1039        let mut builder = SessionBuilder::default();
1040
1041        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
1042        let local_addr = listener.local_addr().unwrap();
1043
1044        let fut = builder.with_client_stream(local_addr, move |client_stream| async move {
1045            drop(client_stream);
1046            tokio::time::sleep(Duration::from_secs(1)).await
1047        });
1048
1049        let (tx, rx) = oneshot::channel();
1050
1051        tokio::task::spawn(async move {
1052            let (incoming, _) = listener.accept().await.unwrap();
1053            let session = builder.connect_incoming(incoming).await;
1054            session.await;
1055
1056            tx.send(()).unwrap();
1057        });
1058
1059        tokio::task::spawn(fut);
1060
1061        rx.await.unwrap();
1062    }
1063
1064    #[tokio::test(flavor = "multi_thread")]
1065    async fn test_send_many_messages() {
1066        reth_tracing::init_test_tracing();
1067        let mut builder = SessionBuilder::default();
1068
1069        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
1070        let local_addr = listener.local_addr().unwrap();
1071
1072        let num_messages = 100;
1073
1074        let fut = builder.with_client_stream(local_addr, move |mut client_stream| async move {
1075            for _ in 0..num_messages {
1076                client_stream
1077                    .send(EthMessage::NewPooledTransactionHashes66(Vec::new().into()))
1078                    .await
1079                    .unwrap();
1080            }
1081        });
1082
1083        let (tx, rx) = oneshot::channel();
1084
1085        tokio::task::spawn(async move {
1086            let (incoming, _) = listener.accept().await.unwrap();
1087            let session = builder.connect_incoming(incoming).await;
1088            session.await;
1089
1090            tx.send(()).unwrap();
1091        });
1092
1093        tokio::task::spawn(fut);
1094
1095        rx.await.unwrap();
1096    }
1097
1098    #[tokio::test(flavor = "multi_thread")]
1099    async fn test_request_timeout() {
1100        reth_tracing::init_test_tracing();
1101
1102        let mut builder = SessionBuilder::default();
1103
1104        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
1105        let local_addr = listener.local_addr().unwrap();
1106
1107        let request_timeout = Duration::from_millis(100);
1108        let drop_timeout = Duration::from_millis(1500);
1109
1110        let fut = builder.with_client_stream(local_addr, move |client_stream| async move {
1111            let _client_stream = client_stream;
1112            tokio::time::sleep(drop_timeout * 60).await;
1113        });
1114        tokio::task::spawn(fut);
1115
1116        let (incoming, _) = listener.accept().await.unwrap();
1117        let mut session = builder.connect_incoming(incoming).await;
1118        session
1119            .internal_request_timeout
1120            .store(request_timeout.as_millis() as u64, Ordering::Relaxed);
1121        session.protocol_breach_request_timeout = drop_timeout;
1122        session.internal_request_timeout_interval =
1123            tokio::time::interval_at(tokio::time::Instant::now(), request_timeout);
1124        let (tx, rx) = oneshot::channel();
1125        let req = PeerRequest::GetBlockBodies { request: GetBlockBodies(vec![]), response: tx };
1126        session.on_internal_peer_request(req, Instant::now());
1127        tokio::spawn(session);
1128
1129        let err = rx.await.unwrap().unwrap_err();
1130        assert_eq!(err, RequestError::Timeout);
1131
1132        // wait for protocol breach error
1133        let msg = builder.active_session_rx.next().await.unwrap();
1134        match msg {
1135            ActiveSessionMessage::ProtocolBreach { .. } => {}
1136            ev => unreachable!("{ev:?}"),
1137        }
1138    }
1139
1140    #[tokio::test(flavor = "multi_thread")]
1141    async fn test_keep_alive() {
1142        let mut builder = SessionBuilder::default();
1143
1144        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
1145        let local_addr = listener.local_addr().unwrap();
1146
1147        let fut = builder.with_client_stream(local_addr, move |mut client_stream| async move {
1148            let _ = tokio::time::timeout(Duration::from_secs(5), client_stream.next()).await;
1149            client_stream.into_inner().disconnect(DisconnectReason::UselessPeer).await.unwrap();
1150        });
1151
1152        let (tx, rx) = oneshot::channel();
1153
1154        tokio::task::spawn(async move {
1155            let (incoming, _) = listener.accept().await.unwrap();
1156            let session = builder.connect_incoming(incoming).await;
1157            session.await;
1158
1159            tx.send(()).unwrap();
1160        });
1161
1162        tokio::task::spawn(fut);
1163
1164        rx.await.unwrap();
1165    }
1166
1167    // This tests that incoming messages are delivered when there's capacity.
1168    #[tokio::test(flavor = "multi_thread")]
1169    async fn test_send_at_capacity() {
1170        let mut builder = SessionBuilder::default();
1171
1172        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
1173        let local_addr = listener.local_addr().unwrap();
1174
1175        let fut = builder.with_client_stream(local_addr, move |mut client_stream| async move {
1176            client_stream
1177                .send(EthMessage::NewPooledTransactionHashes68(Default::default()))
1178                .await
1179                .unwrap();
1180            let _ = tokio::time::timeout(Duration::from_secs(100), client_stream.next()).await;
1181        });
1182        tokio::task::spawn(fut);
1183
1184        let (incoming, _) = listener.accept().await.unwrap();
1185        let session = builder.connect_incoming(incoming).await;
1186
1187        // fill the entire message buffer with an unrelated message
1188        let mut num_fill_messages = 0;
1189        loop {
1190            if builder
1191                .active_session_tx
1192                .try_send(ActiveSessionMessage::ProtocolBreach { peer_id: PeerId::random() })
1193                .is_err()
1194            {
1195                break
1196            }
1197            num_fill_messages += 1;
1198        }
1199
1200        tokio::task::spawn(async move {
1201            session.await;
1202        });
1203
1204        tokio::time::sleep(Duration::from_millis(100)).await;
1205
1206        for _ in 0..num_fill_messages {
1207            let message = builder.active_session_rx.next().await.unwrap();
1208            match message {
1209                ActiveSessionMessage::ProtocolBreach { .. } => {}
1210                ev => unreachable!("{ev:?}"),
1211            }
1212        }
1213
1214        let message = builder.active_session_rx.next().await.unwrap();
1215        match message {
1216            ActiveSessionMessage::ValidMessage {
1217                message: PeerMessage::PooledTransactions(_),
1218                ..
1219            } => {}
1220            _ => unreachable!(),
1221        }
1222    }
1223
1224    #[test]
1225    fn timeout_calculation_sanity_tests() {
1226        let rtt = Duration::from_secs(5);
1227        // timeout for an RTT of `rtt`
1228        let timeout = rtt * TIMEOUT_SCALING;
1229
1230        // if rtt hasn't changed, timeout shouldn't change
1231        assert_eq!(calculate_new_timeout(timeout, rtt), timeout);
1232
1233        // if rtt changed, the new timeout should change less than it
1234        assert!(calculate_new_timeout(timeout, rtt / 2) < timeout);
1235        assert!(calculate_new_timeout(timeout, rtt / 2) > timeout / 2);
1236        assert!(calculate_new_timeout(timeout, rtt * 2) > timeout);
1237        assert!(calculate_new_timeout(timeout, rtt * 2) < timeout * 2);
1238    }
1239}