1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use super::*;
use delay_map::HashMapDelay;
use more_asserts::debug_unreachable;
use std::collections::hash_map::Entry;

pub(super) struct ActiveRequests {
    /// A list of raw messages we are awaiting a response from the remote.
    active_requests_mapping: HashMap<NodeAddress, Vec<RequestCall>>,
    // WHOAREYOU messages do not include the source node id. We therefore maintain another
    // mapping of active_requests via message_nonce. This allows us to match WHOAREYOU
    // requests with active requests sent.
    /// A mapping of all active raw requests message nonces to their NodeAddress.
    active_requests_nonce_mapping: HashMapDelay<MessageNonce, NodeAddress>,
}

impl ActiveRequests {
    pub fn new(request_timeout: Duration) -> Self {
        ActiveRequests {
            active_requests_mapping: HashMap::new(),
            active_requests_nonce_mapping: HashMapDelay::new(request_timeout),
        }
    }

    /// Insert a new request into the active requests mapping.
    pub fn insert(&mut self, node_address: NodeAddress, request_call: RequestCall) {
        let nonce = *request_call.packet().message_nonce();
        self.active_requests_mapping
            .entry(node_address.clone())
            .or_default()
            .push(request_call);
        self.active_requests_nonce_mapping
            .insert(nonce, node_address);
    }

    /// Update the underlying packet for the request via message nonce.
    pub fn update_packet(&mut self, old_nonce: MessageNonce, new_packet: Packet) {
        let node_address =
            if let Some(node_address) = self.active_requests_nonce_mapping.remove(&old_nonce) {
                node_address
            } else {
                debug_unreachable!("expected to find nonce in active_requests_nonce_mapping");
                error!("expected to find nonce in active_requests_nonce_mapping");
                return;
            };

        self.active_requests_nonce_mapping
            .insert(new_packet.header.message_nonce, node_address.clone());

        match self.active_requests_mapping.entry(node_address) {
            Entry::Occupied(mut requests) => {
                let maybe_request_call = requests
                    .get_mut()
                    .iter_mut()
                    .find(|req| req.packet().message_nonce() == &old_nonce);

                if let Some(request_call) = maybe_request_call {
                    request_call.update_packet(new_packet);
                } else {
                    debug_unreachable!("expected to find request call in active_requests_mapping");
                    error!("expected to find request call in active_requests_mapping");
                }
            }
            Entry::Vacant(_) => {
                debug_unreachable!("expected to find node address in active_requests_mapping");
                error!("expected to find node address in active_requests_mapping");
            }
        }
    }

    pub fn get(&self, node_address: &NodeAddress) -> Option<&Vec<RequestCall>> {
        self.active_requests_mapping.get(node_address)
    }

    /// Remove a single request identified by its nonce.
    pub fn remove_by_nonce(&mut self, nonce: &MessageNonce) -> Option<(NodeAddress, RequestCall)> {
        let node_address = self.active_requests_nonce_mapping.remove(nonce)?;
        match self.active_requests_mapping.entry(node_address.clone()) {
            Entry::Vacant(_) => {
                debug_unreachable!("expected to find node address in active_requests_mapping");
                error!("expected to find node address in active_requests_mapping");
                None
            }
            Entry::Occupied(mut requests) => {
                let result = requests
                    .get()
                    .iter()
                    .position(|req| req.packet().message_nonce() == nonce)
                    .map(|index| (node_address, requests.get_mut().remove(index)));
                if requests.get().is_empty() {
                    requests.remove();
                }
                result
            }
        }
    }

    /// Remove all requests associated with a node.
    pub fn remove_requests(&mut self, node_address: &NodeAddress) -> Option<Vec<RequestCall>> {
        let requests = self.active_requests_mapping.remove(node_address)?;
        // Account for node addresses in `active_requests_nonce_mapping` with an empty list
        if requests.is_empty() {
            debug_unreachable!("expected to find requests in active_requests_mapping");
            return None;
        }
        for req in &requests {
            if self
                .active_requests_nonce_mapping
                .remove(req.packet().message_nonce())
                .is_none()
            {
                debug_unreachable!("expected to find req with nonce");
                error!("expected to find req with nonce");
            }
        }
        Some(requests)
    }

    /// Remove a single request identified by its id.
    pub fn remove_request(
        &mut self,
        node_address: &NodeAddress,
        id: &RequestId,
    ) -> Option<RequestCall> {
        match self.active_requests_mapping.entry(node_address.clone()) {
            Entry::Vacant(_) => None,
            Entry::Occupied(mut requests) => {
                let index = requests.get().iter().position(|req| {
                    let req_id: RequestId = req.id().into();
                    &req_id == id
                })?;
                let request_call = requests.get_mut().remove(index);
                if requests.get().is_empty() {
                    requests.remove();
                }
                // Remove the associated nonce mapping.
                self.active_requests_nonce_mapping
                    .remove(request_call.packet().message_nonce());
                Some(request_call)
            }
        }
    }

    /// Checks that `active_requests_mapping` and `active_requests_nonce_mapping` are in sync.
    // this function is only available in tests
    #[cfg(test)]
    // this makes is so that if there is a panic, the error is printed in the caller of this
    // function.
    #[track_caller]
    pub fn check_invariant(&self) {
        // First check that for every `MessageNonce` there is an associated `NodeAddress`.
        for (nonce, address) in self.active_requests_nonce_mapping.iter() {
            if !self.active_requests_mapping.contains_key(address) {
                panic!("Nonce {:?} maps to address {}, which does not exist in `active_requests_mapping`", nonce, address);
            }
        }

        for (address, requests) in self.active_requests_mapping.iter() {
            for req in requests {
                let nonce = req.packet().message_nonce();
                if !self.active_requests_nonce_mapping.contains_key(nonce) {
                    panic!("Address {} maps to request with nonce {:?}, which does not exist in `active_requests_nonce_mapping`", address, nonce);
                }
            }
        }
    }
}

impl Stream for ActiveRequests {
    type Item = Result<(NodeAddress, RequestCall), String>;
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match self.active_requests_nonce_mapping.poll_next_unpin(cx) {
            Poll::Ready(Some(Ok((nonce, node_address)))) => {
                match self.active_requests_mapping.entry(node_address.clone()) {
                    Entry::Vacant(_) => Poll::Ready(None),
                    Entry::Occupied(mut requests) => {
                        match requests
                            .get()
                            .iter()
                            .position(|req| req.packet().message_nonce() == &nonce)
                        {
                            Some(index) => {
                                let result = (node_address, requests.get_mut().remove(index));
                                if requests.get().is_empty() {
                                    requests.remove();
                                }
                                Poll::Ready(Some(Ok(result)))
                            }
                            None => Poll::Ready(None),
                        }
                    }
                }
            }
            Poll::Ready(Some(Err(err))) => Poll::Ready(Some(Err(err))),
            Poll::Ready(None) => Poll::Ready(None),
            Poll::Pending => Poll::Pending,
        }
    }
}