discv5/handler/
request_call.rs

1pub use crate::node_info::NodeContact;
2use crate::{
3    packet::Packet,
4    rpc::{Request, RequestBody},
5};
6
7use super::HandlerReqId;
8
9/// A request to a node that we are waiting for a response.
10#[derive(Debug)]
11pub(super) struct RequestCall {
12    contact: NodeContact,
13    /// The raw discv5 packet sent.
14    packet: Packet,
15    /// Request id
16    request_id: HandlerReqId,
17    /// The message body. Required if need to re-encrypt and re-send.
18    request: RequestBody,
19    /// Handshakes attempted.
20    handshake_sent: bool,
21    /// The number of times this request has been re-sent.
22    retries: u8,
23    /// If we receive a Nodes Response with a total greater than 1. This keeps track of the
24    /// remaining responses expected.
25    remaining_responses: Option<u64>,
26    /// Signifies if we are initiating the session with a random packet. This is only used to
27    /// determine the connection direction of the session.
28    initiating_session: bool,
29}
30
31impl RequestCall {
32    pub fn new(
33        contact: NodeContact,
34        packet: Packet,
35        request_id: HandlerReqId,
36        request: RequestBody,
37        initiating_session: bool,
38    ) -> Self {
39        RequestCall {
40            contact,
41            packet,
42            request_id,
43            request,
44            handshake_sent: false,
45            retries: 1,
46            remaining_responses: None,
47            initiating_session,
48        }
49    }
50
51    /// Returns the contact associated with this call.
52    pub fn contact(&self) -> &NodeContact {
53        &self.contact
54    }
55
56    /// Returns the id associated with this call.
57    pub fn id(&self) -> &HandlerReqId {
58        &self.request_id
59    }
60
61    /// Returns the associated request for this call.
62    pub fn body(&self) -> &RequestBody {
63        &self.request
64    }
65
66    /// Returns the packet associated with this call.
67    pub fn packet(&self) -> &Packet {
68        &self.packet
69    }
70
71    pub fn encode(&self) -> Vec<u8> {
72        match &self.request_id {
73            HandlerReqId::Internal(id) | HandlerReqId::External(id) => {
74                let request = Request {
75                    id: id.clone(),
76                    body: self.request.clone(),
77                };
78                request.encode()
79            }
80        }
81    }
82
83    /// The number of times this request has been resent.
84    pub fn retries(&self) -> u8 {
85        self.retries
86    }
87
88    /// Increments the number of retries for this call
89    pub fn increment_retries(&mut self) {
90        self.retries += 1;
91    }
92
93    /// Returns whether the handshake has been sent for this call or not.
94    pub fn handshake_sent(&self) -> bool {
95        self.handshake_sent
96    }
97
98    /// Indicates the handshake has been sent.
99    pub fn set_handshake_sent(&mut self) {
100        self.handshake_sent = true;
101    }
102
103    /// Indicates a session has been initiated for this call.
104    pub fn set_initiating_session(&mut self, state: bool) {
105        self.initiating_session = state;
106    }
107
108    /// Returns whether a session is being initiated for this call.
109    pub fn initiating_session(&self) -> bool {
110        self.initiating_session
111    }
112
113    /// Updates the underlying packet for the call.
114    pub fn update_packet(&mut self, packet: Packet) {
115        self.packet = packet;
116    }
117
118    /// Gets a mutable reference to the remaining repsonses.
119    pub fn remaining_responses_mut(&mut self) -> &mut Option<u64> {
120        &mut self.remaining_responses
121    }
122}