Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix: remove inflights of banned peers #203

Merged
merged 1 commit into from
Jul 30, 2024

Conversation

lla-dane
Copy link
Contributor

@lla-dane lla-dane commented Jul 28, 2024

When we ban a peer, we also have to remove the inflight requests related to that peer and send those to a random_peer.

@lla-dane
Copy link
Contributor Author

@Davidson-Souza, please take a look.

@Davidson-Souza
Copy link
Collaborator

I think we can migrate that code to hadle_disconnect and every time we get a disconnection from a peer that has inflight we redo the requests, like this:

+        let inflight = self
+            .inflight
+            .clone()
+            .into_iter()
+            .filter(|(_k, v)| v.0 == peer)
+            .collect::<Vec<_>>();
+
+        for req in inflight {
+            self.inflight.remove(&req.0);
+            self.redo_inflight_request(req.0.clone()).await?;
+        }
+
         self.peer_ids.retain(|&id| id != peer);
         for (_, v) in self.peer_by_service.iter_mut() {
             v.retain(|&id| id != peer);
@@ -265,6 +281,66 @@ where
         );
         Ok(())
     }
+
+    pub(crate) async fn redo_inflight_request(
+        &mut self,
+        req: InflightRequests,
+    ) -> Result<(), WireError> {
+        match req {
+            InflightRequests::Blocks(block) => {
+                let peer = self
+                    .send_to_random_peer(
+                        NodeRequest::GetBlock((vec![block], true)),
+                        ServiceFlags::UTREEXO,
+                    )
+                    .await?;
+                self.inflight
+                    .insert(InflightRequests::Blocks(block), (peer, Instant::now()));
+            }
+            InflightRequests::Headers => {
+                let peer = self
+                    .send_to_random_peer(NodeRequest::GetHeaders(vec![]), ServiceFlags::UTREEXO)
+                    .await?;
+                self.inflight
+                    .insert(InflightRequests::Headers, (peer, Instant::now()));
+            }
+            InflightRequests::UtreexoState(_) => {
+                let peer = self
+                    .send_to_random_peer(
+                        NodeRequest::GetUtreexoState((self.chain.get_block_hash(0).unwrap(), 0)),
+                        ServiceFlags::UTREEXO,
+                    )
+                    .await?;
+                self.inflight
+                    .insert(InflightRequests::UtreexoState(peer), (peer, Instant::now()));
+            }
+            InflightRequests::RescanBlock(block) => {
+                let peer = self
+                    .send_to_random_peer(
+                        NodeRequest::GetBlock((vec![block], false)),
+                        ServiceFlags::UTREEXO,
+                    )
+                    .await?;
+                self.inflight
+                    .insert(InflightRequests::RescanBlock(block), (peer, Instant::now()));
+            }
+            InflightRequests::GetFilters => {
+                let peer = self
+                    .send_to_random_peer(
+                        NodeRequest::GetFilter((self.chain.get_block_hash(0).unwrap(), 0)),
+                        ServiceFlags::COMPACT_FILTERS,
+                    )
+                    .await?;
+                self.inflight
+                    .insert(InflightRequests::GetFilters, (peer, Instant::now()));
+            }
+            InflightRequests::Connect(_) | InflightRequests::UserRequest(_) => {
+                // We don't need to do anything here
+            }
+        }
+        Ok(())
+    }
+

@lla-dane
Copy link
Contributor Author

Sure, I will update the code

@lla-dane
Copy link
Contributor Author

@Davidson-Souza, please take a look.

@Davidson-Souza Davidson-Souza merged commit ecc3cbc into vinteumorg:master Jul 30, 2024
5 checks passed
@lla-dane lla-dane deleted the bug/sync-node branch July 30, 2024 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants