Skip to content

Commit

Permalink
Accept arbitrary tokens in test fns
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Nov 19, 2024
1 parent bd88f3b commit b42a781
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions crates/packet-forward/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl From<InFlightPacket> for Packet {
mod tests {
use super::*;
use crate::msg;
use crate::tests::utils::{channels, get_dummy_packet_data};
use crate::tests::utils::{channels, get_dummy_coin, get_dummy_packet_data};

#[test]
fn conversion_from_inflight_packet_to_ibc_packet() {
Expand All @@ -85,7 +85,7 @@ mod tests {
packet_src_channel_id: ChannelId::new(channels::AB),
packet_timeout_timestamp: TimeoutTimestamp::Never,
packet_timeout_height: TimeoutHeight::Never,
packet_data: get_dummy_packet_data(100),
packet_data: get_dummy_packet_data(get_dummy_coin(100)),
refund_sequence: 0u64.into(),
retries_remaining: NonZeroU8::new(1),
timeout: msg::Duration::from_dur(dur::Duration::from_secs(600)),
Expand Down
25 changes: 14 additions & 11 deletions crates/packet-forward/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn decode_ics20_msg_forwards_to_next_middleware() {

#[test]
fn decode_ics20_msg_on_valid_ics20_data() {
let expected_packet_data = get_dummy_packet_data(100);
let expected_packet_data = get_dummy_packet_data(get_dummy_coin(100));
let packet = get_dummy_packet_with_data(0, &expected_packet_data);

let got_packet_data = decode_ics20_msg(&packet).unwrap();
Expand All @@ -28,7 +28,7 @@ fn decode_ics20_msg_on_valid_ics20_data() {

#[test]
fn decode_forward_msg_forwards_to_next_middleware_not_json() {
let packet_data = get_dummy_packet_data_with_memo(100, "oh hi mark".to_owned());
let packet_data = get_dummy_packet_data_with_memo(get_dummy_coin(100), "oh hi mark".to_owned());
let packet = get_dummy_packet_with_data(0, &packet_data);

assert!(matches!(
Expand All @@ -39,7 +39,8 @@ fn decode_forward_msg_forwards_to_next_middleware_not_json() {

#[test]
fn decode_forward_msg_forwards_to_next_middleware_not_pfm_msg() {
let packet_data = get_dummy_packet_data_with_memo(100, r#"{"combo": "breaker"}"#.to_owned());
let packet_data =
get_dummy_packet_data_with_memo(get_dummy_coin(100), r#"{"combo": "breaker"}"#.to_owned());
let packet = get_dummy_packet_with_data(0, &packet_data);

assert!(matches!(
Expand All @@ -50,8 +51,10 @@ fn decode_forward_msg_forwards_to_next_middleware_not_pfm_msg() {

#[test]
fn decode_forward_msg_failure() {
let packet_data =
get_dummy_packet_data_with_memo(100, r#"{"forward": {"foot": "best"}}"#.to_owned());
let packet_data = get_dummy_packet_data_with_memo(
get_dummy_coin(100),
r#"{"forward": {"foot": "best"}}"#.to_owned(),
);
let packet = get_dummy_packet_with_data(0, &packet_data);

assert!(matches!(
Expand All @@ -66,7 +69,7 @@ fn decode_forward_msg_success() {
forward: get_dummy_fwd_metadata(),
};
let expected_packet_data = get_dummy_packet_data_with_memo(
100,
get_dummy_coin(100),
serde_json::to_string(&expected_fwd_metadata).unwrap(),
);

Expand All @@ -89,7 +92,7 @@ fn next_inflight_packet_decreases_retries() {
packet_src_channel_id: ChannelId::new(channels::AB),
packet_timeout_timestamp: TimeoutTimestamp::Never,
packet_timeout_height: TimeoutHeight::Never,
packet_data: get_dummy_packet_data(100),
packet_data: get_dummy_packet_data(get_dummy_coin(100)),
refund_sequence: 0u64.into(),
retries_remaining: Some(retries),
timeout: msg::Duration::from_dur(DEFAULT_FORWARD_TIMEOUT),
Expand All @@ -108,7 +111,7 @@ fn next_inflight_packet_decreases_retries() {

#[test]
fn next_inflight_packet_from_packet() {
let packet_data = get_dummy_packet_data(100);
let packet_data = get_dummy_packet_data(get_dummy_coin(100));
let packet = Packet {
data: serde_json::to_vec(&packet_data).unwrap(),
port_id_on_b: PortId::transfer(),
Expand Down Expand Up @@ -136,7 +139,7 @@ fn next_inflight_packet_from_packet() {
packet_src_channel_id: ChannelId::new(channels::AB),
packet_timeout_timestamp: TimeoutTimestamp::Never,
packet_timeout_height: TimeoutHeight::Never,
packet_data: get_dummy_packet_data(100),
packet_data: get_dummy_packet_data(get_dummy_coin(100)),
refund_sequence: 0u64.into(),
retries_remaining: Some(DEFAULT_FORWARD_RETRIES),
timeout: msg::Duration::from_dur(DEFAULT_FORWARD_TIMEOUT),
Expand Down Expand Up @@ -184,7 +187,7 @@ fn events_kept_on_errors() {

let mut extras = ModuleExtras::empty();

let packet_data = get_dummy_packet_data(100);
let packet_data = get_dummy_packet_data(get_dummy_coin(100));
let packet = get_dummy_packet_with_data(0, &packet_data);
let fwd_metadata = get_dummy_fwd_metadata();

Expand Down Expand Up @@ -262,7 +265,7 @@ fn on_recv_packet_execute_happy_flow() -> Result<(), crate::MiddlewareError> {
let mut extras = ModuleExtras::empty();

let packet_data = get_dummy_packet_data_with_fwd_meta(
TRANSFER_AMOUNT,
get_dummy_coin(TRANSFER_AMOUNT),
msg::PacketMetadata {
forward: get_dummy_fwd_metadata(),
},
Expand Down
15 changes: 9 additions & 6 deletions crates/packet-forward/src/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,24 +524,27 @@ pub fn get_dummy_coin(amount: u64) -> Coin<PrefixedDenom> {
}

pub fn get_dummy_packet_data_with_fwd_meta(
transfer_amount: u64,
transfer_coin: Coin<PrefixedDenom>,
meta: msg::PacketMetadata,
) -> PacketData {
get_dummy_packet_data_with_memo(transfer_amount, serde_json::to_string(&meta).unwrap())
get_dummy_packet_data_with_memo(transfer_coin, serde_json::to_string(&meta).unwrap())
}

pub fn get_dummy_packet_data_with_memo(transfer_amount: u64, memo: String) -> PacketData {
pub fn get_dummy_packet_data_with_memo(
transfer_coin: Coin<PrefixedDenom>,
memo: String,
) -> PacketData {
PacketData {
sender: addresses::A.to_string().into(),
// NB: the ICS-20 receiver field is overriden
receiver: addresses::NULL.to_string().into(),
token: get_dummy_coin(transfer_amount),
token: transfer_coin,
memo: memo.into(),
}
}

pub fn get_dummy_packet_data(transfer_amount: u64) -> PacketData {
get_dummy_packet_data_with_memo(transfer_amount, String::new())
pub fn get_dummy_packet_data(transfer_coin: Coin<PrefixedDenom>) -> PacketData {
get_dummy_packet_data_with_memo(transfer_coin, String::new())
}

pub fn get_dummy_packet_with_data(seq: u64, packet_data: &PacketData) -> Packet {
Expand Down

0 comments on commit b42a781

Please sign in to comment.