Skip to content

Commit

Permalink
Keep other JSON keys after processing PFM
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Dec 6, 2024
1 parent 1da0cbc commit 505404a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
19 changes: 17 additions & 2 deletions crates/packet-forward/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
clippy::shadow_unrelated,
clippy::useless_attribute,
clippy::zero_repeat_side_effects,
clippy::builtin_type_shadow
clippy::builtin_type_shadow,
clippy::unreachable
)
)]

Expand Down Expand Up @@ -367,7 +368,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
};

Expand Down Expand Up @@ -902,3 +903,17 @@ fn emit_event_with_attrs(extras: &mut ModuleExtras, attributes: Vec<ModuleEventA
fn get_retries_left_for_new_pkt(input_retries: Option<u8>) -> Option<NonZeroU8> {
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 {
#[allow(clippy::unwrap_used, clippy::unreachable)]
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");
#[allow(clippy::unwrap_used)]
serde_json::to_string(&memo_obj).unwrap()
}
19 changes: 19 additions & 0 deletions crates/packet-forward/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}))
})
}
4 changes: 4 additions & 0 deletions crates/packet-forward/src/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub struct Store<M> {
pub refunds_received: Vec<(Packet, PacketData)>,
pub refunds_sent: Vec<InFlightPacket>,
pub ack_and_events_written: Vec<(Packet, Acknowledgement)>,
pub overriden_packets_received: Vec<Packet>,
}

impl<M> Store<M> {
Expand All @@ -85,6 +86,7 @@ impl<M> Store<M> {
refunds_sent: Vec::new(),
ack_and_events_written: Vec::new(),
failure_injections: HashSet::new(),
overriden_packets_received: Vec::new(),
}
}

Expand Down Expand Up @@ -122,6 +124,8 @@ where
packet: &Packet,
relayer: &Signer,
) -> (ModuleExtras, Option<Acknowledgement>) {
self.overriden_packets_received.push(packet.clone());

if let Err(err) =
self.check_failure_injection(FailurePoint::BeforeNextMiddlewareOnRecvPacket)
{
Expand Down

0 comments on commit 505404a

Please sign in to comment.