diff --git a/crates/packet-forward/src/lib.rs b/crates/packet-forward/src/lib.rs index 4f1fd36..3107884 100644 --- a/crates/packet-forward/src/lib.rs +++ b/crates/packet-forward/src/lib.rs @@ -367,7 +367,7 @@ where let override_packet = { let ics20_packet_data = PacketData { receiver: override_receiver, - memo: String::new().into(), + memo: extract_next_memo_from_pfm_packet(&source_transfer_pkt).into(), ..source_transfer_pkt }; @@ -902,3 +902,15 @@ fn emit_event_with_attrs(extras: &mut ModuleExtras, attributes: Vec) -> Option { input_retries.map_or(Some(DEFAULT_FORWARD_RETRIES), NonZeroU8::new) } + +// NB: Assume that `src_packet_data` has been validated as a PFM packet +#[inline] +fn extract_next_memo_from_pfm_packet(src_packet_data: &PacketData) -> String { + let serde_json::Value::Object(mut memo_obj) = + serde_json::from_str(src_packet_data.memo.as_ref()).unwrap() + else { + unreachable!() + }; + memo_obj.remove("forward"); + serde_json::to_string(&memo_obj).unwrap() +} diff --git a/crates/packet-forward/src/tests.rs b/crates/packet-forward/src/tests.rs index e21e63e..430d1b0 100644 --- a/crates/packet-forward/src/tests.rs +++ b/crates/packet-forward/src/tests.rs @@ -1247,3 +1247,22 @@ fn retries_left_for_new_pkt() { Some(DEFAULT_FORWARD_RETRIES) ); } + +#[test] +fn next_middleware_memo_has_no_fwd_key() -> Result<(), crate::MiddlewareError> { + let ack_from_c = + AcknowledgementStatus::success(AckStatusValue::new("Ack from chain C").unwrap()).into(); + no_timeout_packet_flow_inner(&ack_from_c, |_, pfm| { + assert!(pfm.next.overriden_packets_received.iter().all(|packet| { + let packet_data: PacketData = serde_json::from_slice(&packet.data).unwrap(); + + let serde_json::Value::Object(memo_obj) = + serde_json::from_str(packet_data.memo.as_ref()).unwrap() + else { + panic!("Expected JSON object"); + }; + + !memo_obj.contains_key("forward") + })) + }) +} diff --git a/crates/packet-forward/src/tests/utils.rs b/crates/packet-forward/src/tests/utils.rs index 3b63184..cc2cced 100644 --- a/crates/packet-forward/src/tests/utils.rs +++ b/crates/packet-forward/src/tests/utils.rs @@ -73,6 +73,7 @@ pub struct Store { pub refunds_received: Vec<(Packet, PacketData)>, pub refunds_sent: Vec, pub ack_and_events_written: Vec<(Packet, Acknowledgement)>, + pub overriden_packets_received: Vec, } impl Store { @@ -85,6 +86,7 @@ impl Store { refunds_sent: Vec::new(), ack_and_events_written: Vec::new(), failure_injections: HashSet::new(), + overriden_packets_received: Vec::new(), } } @@ -122,6 +124,8 @@ where packet: &Packet, relayer: &Signer, ) -> (ModuleExtras, Option) { + self.overriden_packets_received.push(packet.clone()); + if let Err(err) = self.check_failure_injection(FailurePoint::BeforeNextMiddlewareOnRecvPacket) {