From 49b8ea771eba20d446aa1bb5a4b2bb4b3b0f405a Mon Sep 17 00:00:00 2001 From: behzad nouri Date: Mon, 27 Mar 2023 23:19:55 +0000 Subject: [PATCH] simplifies ServeRepair::run_orphan (#30908) --- core/src/serve_repair.rs | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/core/src/serve_repair.rs b/core/src/serve_repair.rs index 72e3aa8903dab2..121d5c4f1992ef 100644 --- a/core/src/serve_repair.rs +++ b/core/src/serve_repair.rs @@ -1277,40 +1277,29 @@ impl ServeRepair { recycler: &PacketBatchRecycler, from_addr: &SocketAddr, blockstore: &Blockstore, - mut slot: Slot, + slot: Slot, max_responses: usize, nonce: Nonce, ) -> Option { let mut res = PacketBatch::new_unpinned_with_recycler(recycler.clone(), max_responses, "run_orphan"); // Try to find the next "n" parent slots of the input slot - while let Ok(Some(meta)) = blockstore.meta(slot) { - if meta.received == 0 { - break; - } - let packet = repair_response::repair_response_packet( + let packets = std::iter::successors(blockstore.meta(slot).ok()?, |meta| { + blockstore.meta(meta.parent_slot?).ok()? + }) + .map_while(|meta| { + repair_response::repair_response_packet( blockstore, - slot, - meta.received - 1, + meta.slot, + meta.received.checked_sub(1u64)?, from_addr, nonce, - ); - if let Some(packet) = packet { - res.push(packet); - } else { - break; - } - - if meta.parent_slot.is_some() && res.len() < max_responses { - slot = meta.parent_slot.unwrap(); - } else { - break; - } - } - if res.is_empty() { - return None; + ) + }); + for packet in packets.take(max_responses) { + res.push(packet); } - Some(res) + (!res.is_empty()).then_some(res) } fn run_ancestor_hashes(