From 43fddabc10bd7c319daa2647bc1f7b3f93043c54 Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Fri, 7 Feb 2025 19:57:29 +0530 Subject: [PATCH] feat: Optimize proposal part handling logic in received_proposal_part --- code/examples/channel/src/state.rs | 36 ++++++++++++++++++------------ 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/code/examples/channel/src/state.rs b/code/examples/channel/src/state.rs index 1d1146254..23ea775dc 100644 --- a/code/examples/channel/src/state.rs +++ b/code/examples/channel/src/state.rs @@ -115,6 +115,28 @@ impl State { ) -> eyre::Result>> { let sequence = part.sequence; + if let Some(proposal) = part + .content + .clone() + .into_data() + .as_ref() + .and_then(|data| data.as_init()) + { + // Check if the proposal is outdated + if proposal.height < self.current_height { + debug!( + height = %self.current_height, + round = %self.current_round, + part.height = %proposal.height, + part.round = %proposal.round, + part.sequence = %sequence, + "Received outdated proposal part, ignoring" + ); + + return Ok(None); + } + } + // Check if we have a full proposal let Some(parts) = self.streams_map.insert(from, part) else { return Ok(None); @@ -141,20 +163,6 @@ impl State { } } - // Check if the proposal is outdated - if parts.height < self.current_height { - debug!( - height = %self.current_height, - round = %self.current_round, - part.height = %parts.height, - part.round = %parts.round, - part.sequence = %sequence, - "Received outdated proposal part, ignoring" - ); - - return Ok(None); - } - // Re-assemble the proposal from its parts let value = assemble_value_from_parts(parts);