Skip to content

Commit 9a526c0

Browse files
committed
fix: don't drop inflight requests before completion
1 parent 2b71875 commit 9a526c0

1 file changed

Lines changed: 25 additions & 13 deletions

File tree

crates/floresta-wire/src/p2p_wire/node/peer_man.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use crate::address_man::AddressState;
2929
use crate::address_man::LocalAddress;
3030
use crate::block_proof::Bitmap;
3131
use crate::node::running_ctx::RunningNode;
32+
use crate::node::try_and_log;
3233
use crate::node_context::NodeContext;
3334
use crate::node_context::PeerId;
3435
use crate::node_interface::NodeResponse;
@@ -454,7 +455,12 @@ where
454455

455456
for req in inflight {
456457
self.inflight.remove(&req.0);
457-
self.redo_inflight_request(req.0.clone())?;
458+
459+
if let Err(e) = self.redo_inflight_request(&req.0) {
460+
// CRITICAL: never drop the request, so we retry it later
461+
self.inflight.insert(req.0, req.1);
462+
return Err(e);
463+
}
458464
}
459465

460466
#[cfg(feature = "metrics")]
@@ -520,7 +526,7 @@ where
520526
.collect::<Vec<_>>();
521527

522528
for req in timed_out {
523-
let Some((peer, _)) = self.inflight.remove(&req) else {
529+
let Some((peer, time)) = self.inflight.remove(&req) else {
524530
continue;
525531
};
526532

@@ -542,8 +548,14 @@ where
542548
}
543549

544550
debug!("Request timed out: {req:?}");
545-
self.increase_banscore(peer, 1)?;
546-
self.redo_inflight_request(req)?;
551+
// Increase the banscore and try banning the peer if needed, then re-request
552+
try_and_log!(self.increase_banscore(peer, 1));
553+
554+
if let Err(e) = self.redo_inflight_request(&req) {
555+
// CRITICAL: never drop the request, so we retry it later
556+
self.inflight.insert(req, (peer, time));
557+
return Err(e);
558+
}
547559
}
548560

549561
Ok(())
@@ -572,39 +584,39 @@ where
572584
Ok(())
573585
}
574586

575-
pub(crate) fn redo_inflight_request(&mut self, req: InflightRequests) -> Result<(), WireError> {
587+
pub(crate) fn redo_inflight_request(
588+
&mut self,
589+
req: &InflightRequests,
590+
) -> Result<(), WireError> {
576591
match req {
577592
InflightRequests::UtreexoProof(block_hash) => {
578593
if !self.has_utreexo_peers() {
579594
return Ok(());
580595
}
581596

582-
if !self.blocks.contains_key(&block_hash) {
597+
if !self.blocks.contains_key(block_hash) {
583598
// If we don't have the block anymore, we can't ask for the proof
584599
return Ok(());
585600
}
586601

587-
if self
588-
.inflight
589-
.contains_key(&InflightRequests::UtreexoProof(block_hash))
590-
{
602+
if self.inflight.contains_key(req) {
591603
// If we already have an inflight request for this block, we don't need to redo it
592604
return Ok(());
593605
}
594606

595607
let peer = self.send_to_fast_peer(
596-
NodeRequest::GetBlockProof((block_hash, Bitmap::new(), Bitmap::new())),
608+
NodeRequest::GetBlockProof((*block_hash, Bitmap::new(), Bitmap::new())),
597609
service_flags::UTREEXO.into(),
598610
)?;
599611

600612
self.inflight.insert(
601-
InflightRequests::UtreexoProof(block_hash),
613+
InflightRequests::UtreexoProof(*block_hash),
602614
(peer, Instant::now()),
603615
);
604616
}
605617

606618
InflightRequests::Blocks(block) => {
607-
self.request_blocks(vec![block])?;
619+
self.request_blocks(vec![*block])?;
608620
}
609621

610622
InflightRequests::Headers => {

0 commit comments

Comments
 (0)