From a8fd4d713ec3478bb45d2c2d444bd9cd8ddd62eb Mon Sep 17 00:00:00 2001 From: bishalbikram Date: Tue, 19 Mar 2024 19:38:25 +0545 Subject: [PATCH] chore: add test coverage for cosmwasm ibc core --- .../tests/channel/test_acknowledgement.rs | 159 ++++++++- .../tests/channel/test_channel_closeinit.rs | 21 ++ .../tests/channel/test_close_confirm.rs | 21 ++ .../cw-ibc-core/tests/channel/test_handler.rs | 114 +++++++ .../tests/channel/test_open_ack.rs | 86 +++++ .../tests/channel/test_open_confirm.rs | 63 ++++ .../cw-ibc-core/tests/channel/test_packet.rs | 17 + .../tests/channel/test_receive_packet.rs | 151 ++++++++- .../cw-ibc-core/tests/channel/test_timeout.rs | 316 +++++------------- .../tests/channel/test_timeout_on_close.rs | 135 +++++++- .../cw-ibc-core/tests/test_channel.rs | 57 +++- .../cw-ibc-core/tests/test_connection.rs | 12 + .../cw-ibc-core/tests/test_host.rs | 10 + 13 files changed, 913 insertions(+), 249 deletions(-) diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_acknowledgement.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_acknowledgement.rs index 68562708c..ddfced715 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_acknowledgement.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_acknowledgement.rs @@ -1,6 +1,9 @@ +use super::*; + +use cosmwasm_std::to_vec; use cw_ibc_core::conversions::to_ibc_channel_id; -use super::*; +use common::ibc::core::ics04_channel::commitment::AcknowledgementCommitment; #[test] fn test_acknowledgement_packet_validate_ordered() { @@ -89,3 +92,157 @@ fn test_acknowledgement_packet_validate_fail_missing_channel() { .acknowledgement_packet_validate(deps.as_mut(), info, env, &msg) .unwrap(); } + +#[test] +#[should_panic(expected = "ChannelClosed")] +fn acknowledgement_packet_validate_fail_on_channel_close() { + let msg = get_dummy_raw_msg_acknowledgement(10); + let mut ctx = TestContext::for_acknowledge_packet(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::new(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + if let Some(chann_end) = &mut ctx.channel_end { + chann_end.state = State::Closed + } + + ctx.init_acknowledge_packet(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + contract + .acknowledgement_packet_validate(deps.as_mut(), info, ctx.env, &msg) + .unwrap(); +} + +#[test] +#[should_panic(expected = "InvalidPacketCounterparty")] +fn acknowledgement_packet_validate_fail_on_invalid_packet_counterparty() { + let mut msg = get_dummy_raw_msg_acknowledgement(10); + let mut ctx = TestContext::for_acknowledge_packet(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::new(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + ctx.init_acknowledge_packet(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + if let Some(packet) = &mut msg.packet { + packet.destination_port = "different_port".to_string(); + } + + contract + .acknowledgement_packet_validate(deps.as_mut(), info, ctx.env, &msg) + .unwrap(); +} + +#[test] +#[should_panic( + expected = "IbcPacketError { error: IncorrectPacketCommitment { sequence: Sequence(1) } }" +)] +fn acknowledgement_packet_validate_fail_for_incorrect_packet_commitment() { + let mut msg = get_dummy_raw_msg_acknowledgement(10); + let mut ctx = TestContext::for_acknowledge_packet(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::new(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + ctx.init_acknowledge_packet(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + if let Some(packet) = &mut msg.packet { + packet.timeout_height = Some(RawHeight { + revision_height: 100, + revision_number: 100, + }); + } + + contract + .acknowledgement_packet_validate(deps.as_mut(), info, ctx.env, &msg) + .unwrap(); +} + +#[test] +#[should_panic(expected = "InvalidPacketSequence")] +fn acknowledgement_packet_validate_fail_for_invalid_packet_sequence() { + let msg = get_dummy_raw_msg_acknowledgement(10); + let mut ctx = TestContext::for_acknowledge_packet(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::new(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + if let Some(chann_end) = &mut ctx.channel_end { + chann_end.ordering = Order::Ordered + } + + ctx.init_acknowledge_packet(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + let packet = msg.clone().packet.unwrap(); + let dest_port = to_ibc_port_id(&packet.source_port).unwrap(); + let dest_channel = to_ibc_channel_id(&packet.source_channel).unwrap(); + + contract + .store_next_sequence_ack( + deps.as_mut().storage, + &dest_port, + &dest_channel, + &Sequence::from_str("2").unwrap(), + ) + .unwrap(); + + contract + .acknowledgement_packet_validate(deps.as_mut(), info, ctx.env, &msg) + .unwrap(); +} + +#[test] +#[should_panic(expected = "FrozenClient")] +fn acknowledgement_packet_validate_fail_for_frozen_client() { + let msg = get_dummy_raw_msg_acknowledgement(10); + let mut ctx = TestContext::for_acknowledge_packet(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::new(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + if let Some(client_state) = &mut ctx.client_state { + client_state.frozen_height = 10 + } + + ctx.init_acknowledge_packet(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + contract + .acknowledgement_packet_validate(deps.as_mut(), info, ctx.env, &msg) + .unwrap(); +} + +#[test] +fn test_get_packet_acknowledgement() { + let ctx = TestContext::default(get_mock_env()); + let mut deps = deps(); + let contract = CwIbcCoreContext::new(); + + let sequence = Sequence::from_str("0").unwrap(); + let ack_commitment = AcknowledgementCommitment::from(to_vec("ack").unwrap()); + + contract + .store_packet_acknowledgement( + deps.as_mut().storage, + &ctx.port_id, + &ctx.channel_id, + sequence, + ack_commitment.clone(), + ) + .unwrap(); + + let res = contract + .get_packet_acknowledgement( + deps.as_ref().storage, + &ctx.port_id, + &ctx.channel_id, + sequence, + ) + .unwrap(); + + assert_eq!(ack_commitment, res); +} diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_channel_closeinit.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_channel_closeinit.rs index ff00fa4ff..4992b9b3e 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_channel_closeinit.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_channel_closeinit.rs @@ -228,3 +228,24 @@ fn test_on_chan_close_init_submessage() { channel_close_init_validate.channel().order ); } + +#[test] +#[should_panic(expected = "ClientFrozen")] +fn fail_test_validate_chanel_close_init_for_frozen_client() { + let msg = get_dummy_raw_msg_chan_close_init(); + let mut ctx = TestContext::for_channel_close_init(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::default(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + if let Some(client_state) = &mut ctx.client_state { + client_state.frozen_height = 1 + } + + ctx.init_channel_close_init(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + contract + .validate_channel_close_init(deps.as_mut(), info, &msg) + .unwrap(); +} diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_close_confirm.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_close_confirm.rs index 15b644f9b..baeff443c 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_close_confirm.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_close_confirm.rs @@ -234,3 +234,24 @@ pub fn test_channel_close_confirm_validate_fail_connection_hops() { }; channel_close_confirm_validate(&channel_id, &channel_end).unwrap(); } + +#[test] +#[should_panic(expected = "FrozenClient")] +fn fail_test_validate_channel_close_confirm_for_frozen_client() { + let msg = get_dummy_raw_msg_chan_close_confirm(10); + let mut ctx = TestContext::for_channel_close_confirm(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::default(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + if let Some(client_state) = &mut ctx.client_state { + client_state.frozen_height = 1 + } + + ctx.init_channel_close_confirm(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + contract + .validate_channel_close_confirm(deps.as_mut(), info, &msg) + .unwrap(); +} diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_handler.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_handler.rs index b0226ec48..e58e7f8f3 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_handler.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_handler.rs @@ -1,5 +1,12 @@ use super::*; +use common::ibc::core::ics04_channel::channel::Order; +use common::ibc::core::ics24_host::identifier::PortId; + +use cw_ibc_core::ics03_connection::State as ConnectionState; +use cw_ibc_core::ics04_channel::{open_init, open_try}; +use cw_ibc_core::{context::CwIbcCoreContext, ConnectionEnd}; + #[test] #[should_panic(expected = "UndefinedConnectionCounterparty")] fn test_validate_open_try_channel_fail_missing_counterparty() { @@ -20,3 +27,110 @@ fn test_validate_open_try_channel_fail_missing_counterparty() { .validate_channel_open_try(deps.as_mut(), info, &raw) .unwrap(); } + +#[test] +#[should_panic(expected = "InvalidConnectionHopsLength")] +fn fail_test_validate_open_init_channel_on_invalid_connection_hops_length() { + let mut deps = deps(); + let contract = CwIbcCoreContext::default(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + let mut raw_channel = get_dummy_raw_channel_end(Some(0)); + raw_channel.connection_hops = Vec::new(); + + let msg = RawMsgChannelOpenInit { + port_id: PortId::default().to_string(), + channel: Some(raw_channel), + signer: get_dummy_bech32_account(), + }; + contract + .validate_channel_open_init(deps.as_mut(), info, &msg) + .unwrap(); +} + +#[test] +#[should_panic(expected = "InvalidVersionLengthConnection")] +fn fail_channel_open_init_msg_validate_on_invalid_version_length_of_connection() { + let ctx = TestContext::default(get_mock_env()); + + let conn_end = ConnectionEnd::new( + ctx.connection_end().state.clone(), + ctx.client_id.clone(), + ctx.connection_end().counterparty().clone(), + Vec::new(), + ctx.connection_end().delay_period().clone(), + ); + open_init::channel_open_init_msg_validate(&ctx.channel_end(), conn_end).unwrap(); +} + +#[test] +#[should_panic(expected = "ChannelFeatureNotSupportedByConnection")] +fn fail_channel_open_init_msg_validate_on_channel_feature_not_supported() { + let mut ctx = TestContext::default(get_mock_env()); + if let Some(chann_end) = &mut ctx.channel_end { + chann_end.ordering = Order::None + } + + open_init::channel_open_init_msg_validate(&ctx.channel_end(), ctx.connection_end()).unwrap(); +} + +#[test] +#[should_panic(expected = "ConnectionNotOpen")] +fn fail_channel_open_try_msg_validate_on_invalid_connection_state() { + let mut ctx = TestContext::default(get_mock_env()); + if let Some(conn_end) = &mut ctx.connection_end { + conn_end.state = ConnectionState::Init; + } + + open_try::channel_open_try_msg_validate(&ctx.channel_end(), &ctx.connection_end()).unwrap(); +} + +#[test] +#[should_panic(expected = "ChannelFeatureNotSupportedByConnection")] +fn fail_channel_open_try_msg_validate_on_feature_not_supported() { + let mut ctx = TestContext::default(get_mock_env()); + if let Some(chann_end) = &mut ctx.channel_end { + chann_end.ordering = Order::None + } + + open_try::channel_open_try_msg_validate(&ctx.channel_end(), &ctx.connection_end()).unwrap(); +} + +#[test] +#[should_panic(expected = "ClientFrozen")] +fn fail_test_validate_open_init_channel_for_frozen_client() { + let mut ctx = TestContext::default(get_mock_env()); + let mut deps = deps(); + let contract = CwIbcCoreContext::default(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + if let Some(client_state) = &mut ctx.client_state { + client_state.frozen_height = 1 + } + + ctx.init_channel_open_init(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + let msg = get_dummy_raw_msg_chan_open_init(Some(0)); + contract + .validate_channel_open_init(deps.as_mut(), info, &msg) + .unwrap(); +} + +#[test] +#[should_panic(expected = "InvalidConnectionHopsLength")] +fn fail_test_validate_open_try_channel_on_invalid_connection_hops_length() { + let mut deps = deps(); + let contract = CwIbcCoreContext::default(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + let mut raw_channel = get_dummy_raw_channel_end(Some(0)); + raw_channel.connection_hops = Vec::new(); + + let mut msg = get_dummy_raw_msg_chan_open_try(0); + msg.channel = Some(raw_channel); + + contract + .validate_channel_open_try(deps.as_mut(), info, &msg) + .unwrap(); +} diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_open_ack.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_open_ack.rs index a9994193a..c34e4e72c 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_open_ack.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_open_ack.rs @@ -1,5 +1,6 @@ use super::*; use cosmwasm_std::IbcChannel; +use cw_ibc_core::ics04_channel::open_ack; use cw_ibc_core::{ conversions::to_ibc_channel_id, @@ -190,3 +191,88 @@ fn test_channel_open_try_validate() { let res = channel_open_try_msg_validate(&channel, &connection_end); assert!(res.is_ok()); } + +#[test] +#[should_panic(expected = "UnknownOrderType")] +fn fail_channel_open_ack_msg_validate_on_unknown_order() { + let mut ctx = TestContext::default(get_mock_env()); + if let Some(chann_end) = &mut ctx.channel_end { + chann_end.remote = common::ibc::core::ics04_channel::channel::Counterparty::default(); + chann_end.ordering = Order::None + } + + open_ack::on_chan_open_ack_submessage( + &ctx.channel_end(), + &ctx.port_id, + &ctx.channel_id, + &ctx.connection_id, + ) + .unwrap(); +} + +#[test] +fn test_channel_open_ack_msg_validate_on_ordered_type() { + let mut ctx = TestContext::default(get_mock_env()); + if let Some(chann_end) = &mut ctx.channel_end { + chann_end.remote = common::ibc::core::ics04_channel::channel::Counterparty::default(); + chann_end.ordering = Order::Ordered + } + + let res = Some( + open_ack::on_chan_open_ack_submessage( + &ctx.channel_end(), + &ctx.port_id, + &ctx.channel_id, + &ctx.connection_id, + ) + .unwrap(), + ); + + let res_exist = match res { + Some(_) => true, + None => false, + }; + assert_eq!(res_exist, true); +} + +#[test] +#[should_panic(expected = "FrozenClient")] +fn fail_test_validate_open_try_channel_for_frozen_client() { + let msg = get_dummy_raw_msg_chan_open_try(10); + let mut ctx = TestContext::for_channel_open_try(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::default(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + if let Some(client_state) = &mut ctx.client_state { + client_state.frozen_height = 1 + } + + ctx.init_channel_open_try(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + contract + .validate_channel_open_try(deps.as_mut(), info, &msg) + .unwrap(); +} + +#[test] +#[should_panic(expected = "FrozenClient")] +fn fail_test_validate_channel_open_ack_for_frozen_client() { + let msg = get_dummy_raw_msg_chan_open_ack(10); + let mut ctx = TestContext::for_channel_open_ack(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::default(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + if let Some(client_state) = &mut ctx.client_state { + client_state.frozen_height = 1 + } + + ctx.init_channel_open_ack(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + contract + .validate_channel_open_ack(deps.as_mut(), info, &msg) + .unwrap(); +} diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_open_confirm.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_open_confirm.rs index e957ccde8..6cea7b20b 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_open_confirm.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_open_confirm.rs @@ -1,4 +1,5 @@ use cosmwasm_std::IbcChannel; +use cw_ibc_core::ics04_channel::open_confirm; use cw_ibc_core::{ conversions::to_ibc_channel_id, @@ -139,3 +140,65 @@ pub fn test_on_chan_open_confirm_submessage() { assert_eq!(res.unwrap(), expected); } + +#[test] +#[should_panic(expected = "UnknownOrderType")] +fn fail_channel_open_confirm_msg_validate_on_unknown_order() { + let mut ctx = TestContext::default(get_mock_env()); + if let Some(chann_end) = &mut ctx.channel_end { + chann_end.remote = common::ibc::core::ics04_channel::channel::Counterparty::default(); + chann_end.ordering = Order::None + } + + open_confirm::on_chan_open_confirm_submessage( + &ctx.channel_end(), + &ctx.port_id, + &ctx.channel_id, + ) + .unwrap(); +} + +#[test] +fn test_channel_open_confirm_msg_validate_on_ordered_type() { + let mut ctx = TestContext::default(get_mock_env()); + if let Some(chann_end) = &mut ctx.channel_end { + chann_end.remote = common::ibc::core::ics04_channel::channel::Counterparty::default(); + chann_end.ordering = Order::Ordered + } + + let res = Some( + open_confirm::on_chan_open_confirm_submessage( + &ctx.channel_end(), + &ctx.port_id, + &ctx.channel_id, + ) + .unwrap(), + ); + + let res_exist = match res { + Some(_) => true, + None => false, + }; + assert_eq!(res_exist, true); +} + +#[test] +#[should_panic(expected = "FrozenClient")] +fn fail_test_validate_channel_open_confirm_for_frozen_client() { + let msg = get_dummy_raw_msg_chan_open_confirm(10); + let mut ctx = TestContext::for_channel_open_confirm(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::default(); + let info = create_mock_info("channel-creater", "umlg", 2000); + + if let Some(client_state) = &mut ctx.client_state { + client_state.frozen_height = 1 + } + + ctx.init_channel_open_confirm(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + contract + .validate_channel_open_confirm(deps.as_mut(), info, &msg) + .unwrap(); +} diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_packet.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_packet.rs index cbf0f2858..7970ffe50 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_packet.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_packet.rs @@ -234,3 +234,20 @@ fn test_packet_send_fails_on_timedout_timestamp() { .send_packet(deps.as_mut(), &mock_env(), info, packet) .unwrap(); } + +#[test] +#[should_panic(expected = "Unauthorized")] +fn test_send_packet_fail_for_unauthenticated_capability() { + let packet = get_dummy_raw_packet(110, Timestamp::default().nanoseconds()); + let mut ctx = TestContext::for_send_packet(get_mock_env(), &packet); + let mut deps = deps(); + let contract = CwIbcCoreContext::new(); + let info = create_mock_info("module", "test", 100); + + ctx.init_send_packet(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + contract + .send_packet(deps.as_mut(), &ctx.env, info, packet) + .unwrap(); +} diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_receive_packet.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_receive_packet.rs index fb5e304c3..ee001072d 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_receive_packet.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_receive_packet.rs @@ -1,13 +1,14 @@ use common::ibc::core::ics03_connection::connection::State as ConnectionState; +use common::ibc::core::ics04_channel::commitment::AcknowledgementCommitment; use common::ibc::core::ics04_channel::packet::Receipt; +use common::ibc::core::ics04_channel::timeout::TimeoutHeight; use common::ibc::timestamp::Timestamp; +use cosmwasm_std::to_vec; use cw_common::raw_types::channel::RawMsgRecvPacket; use cw_common::types::Ack; -use cw_ibc_core::conversions::to_ibc_channel_id; - -use cw_ibc_core::conversions::to_ibc_port_id; +use cw_ibc_core::conversions::{to_ibc_channel_id, to_ibc_port_id, to_ibc_timeout_block}; use cw_ibc_core::VALIDATE_ON_PACKET_RECEIVE_ON_MODULE; @@ -269,6 +270,139 @@ fn test_receive_packet_no_op_on_packet_already_received() { .validate_receive_packet(deps.as_mut(), info, env, &msg) .unwrap(); } + +#[test] +fn test_is_packet_already_received_for_none_ordered_channel() { + let msg = get_dummy_raw_msg_recv_packet(10); + let mut ctx = TestContext::for_receive_packet(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::new(); + + ctx.init_receive_packet(deps.as_mut().storage, &contract); + if let Some(channel_end) = &mut ctx.channel_end { + channel_end.ordering = Order::None; + } + + let packet = msg.packet.clone().unwrap(); + let des_port = to_ibc_port_id(&packet.destination_port).unwrap(); + let des_channel = to_ibc_channel_id(&packet.destination_channel).unwrap(); + + let res = contract + .is_packet_already_received( + deps.as_ref(), + &ctx.channel_end(), + &des_port, + &des_channel, + msg.packet.unwrap().sequence.into(), + ) + .unwrap(); + + assert_eq!(res, false) +} + +#[test] +#[should_panic( + expected = "InvalidPacketSequence { given_sequence: Sequence(1), next_sequence: Sequence(0) }" +)] +fn test_is_packet_already_received_fail_for_invalid_packet_sequence() { + let msg = get_dummy_raw_msg_recv_packet(10); + let mut ctx = TestContext::for_receive_packet(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::new(); + + if let Some(channel_end) = &mut ctx.channel_end { + channel_end.ordering = Order::Ordered; + } + + let packet = msg.packet.clone().unwrap(); + let des_port = to_ibc_port_id(&packet.destination_port).unwrap(); + let des_channel = to_ibc_channel_id(&packet.destination_channel).unwrap(); + + contract + .store_next_sequence_recv( + deps.as_mut().storage, + &des_port, + &des_channel, + &Sequence::from(0), + ) + .unwrap(); + + contract + .is_packet_already_received( + deps.as_ref(), + &ctx.channel_end(), + &des_port, + &des_channel, + msg.packet.unwrap().sequence.into(), + ) + .unwrap(); +} + +#[test] +fn test_is_packet_already_received() { + let msg = get_dummy_raw_msg_recv_packet(10); + let mut ctx = TestContext::for_receive_packet(get_mock_env(), &msg); + let mut deps = deps(); + let contract = CwIbcCoreContext::new(); + + if let Some(channel_end) = &mut ctx.channel_end { + channel_end.ordering = Order::Ordered; + } + + let packet = msg.packet.clone().unwrap(); + let des_port = to_ibc_port_id(&packet.destination_port).unwrap(); + let des_channel = to_ibc_channel_id(&packet.destination_channel).unwrap(); + + contract + .store_next_sequence_recv( + deps.as_mut().storage, + &des_port, + &des_channel, + &Sequence::from(1), + ) + .unwrap(); + + let res = contract + .is_packet_already_received( + deps.as_ref(), + &ctx.channel_end(), + &des_port, + &des_channel, + msg.packet.unwrap().sequence.into(), + ) + .unwrap(); + + assert_eq!(res, false) +} + +#[test] +#[should_panic(expected = "{ error: AcknowledgementExists { sequence: Sequence(1) } }")] +fn fail_test_validate_write_acknowledgement() { + let msg = get_dummy_raw_msg_recv_packet(10); + let mut deps = deps(); + let contract = CwIbcCoreContext::new(); + + let packet = msg.packet.unwrap(); + let sequence = packet.sequence.into(); + let dest_port = to_ibc_port_id(&packet.destination_port).unwrap(); + let dest_channel = to_ibc_channel_id(&packet.destination_channel).unwrap(); + let ack_commitment = AcknowledgementCommitment::from(to_vec("0").unwrap()); + + contract + .store_packet_acknowledgement( + deps.as_mut().storage, + &dest_port, + &dest_channel, + sequence, + ack_commitment, + ) + .unwrap(); + + contract + .validate_write_acknowledgement(deps.as_mut().storage, &packet) + .unwrap(); +} + #[test] fn execute_receive_packet() { let contract = CwIbcCoreContext::default(); @@ -407,3 +541,14 @@ fn test_lookup_module_packet() { assert!(res.is_ok()); assert_eq!("contractaddress", res.unwrap()) } + +#[test] +fn test_timeout_height_to_str() { + let contract = CwIbcCoreContext::new(); + + let timeout_height = TimeoutHeight::from(mock_height(1, 1).unwrap()); + let timeout = to_ibc_timeout_block(&timeout_height); + + let res = contract.timeout_height_to_str(timeout); + assert_eq!(res, "1-1".to_string()) +} diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_timeout.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_timeout.rs index 78ea42633..eb3b0f05f 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_timeout.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_timeout.rs @@ -1,3 +1,6 @@ +use common::icon::icon::proto::core::channel::channel::Order; + +use cw_common::types::TimeoutMsgType; use cw_ibc_core::{light_client::light_client::LightClient, VALIDATE_ON_PACKET_TIMEOUT_ON_MODULE}; use super::*; @@ -131,7 +134,9 @@ fn test_timeout_packet_validate_to_light_client() { mock_lightclient_query(test_context.mock_queries, &mut deps); - let res = contract.timeout_packet_validate_to_light_client(deps.as_mut(), info, env, msg); + let res = + contract.timeout_packet_validate(deps.as_mut(), env, info, TimeoutMsgType::Timeout(msg)); + println!("{res:?}"); assert!(res.is_ok()); assert_eq!( @@ -155,129 +160,12 @@ fn test_timeout_packet_fails_if_height_not_expired() { let mut test_context = TestContext::for_packet_timeout(env.clone(), &msg); test_context.init_timeout_packet(deps.as_mut().storage, &contract); - // let packet = &msg.packet.clone().unwrap(); - // let src_port = to_ibc_port_id(&packet.source_port).unwrap(); - // let src_channel = to_ibc_channel_id(&packet.source_channel).unwrap(); - - // let dst_port = to_ibc_port_id(&packet.destination_port).unwrap(); - // let dst_channel = to_ibc_channel_id(&packet.destination_channel).unwrap(); - - // let packet_timeout_height = to_ibc_timeout_height(packet.timeout_height.clone()).unwrap(); - // let packet_timestamp = to_ibc_timestamp(packet.timeout_timestamp).unwrap(); - // let packet_sequence = Sequence::from(packet.sequence); - // let proof_height = to_ibc_height(msg.proof_height.clone()).unwrap(); - // let _next_sequence_recv = Sequence::from(msg.next_sequence_recv); - - // let chan_end_on_a_ordered = ChannelEnd::new( - // State::Open, - // Order::Ordered, - // Counterparty::new(dst_port, Some(dst_channel)), - // vec![IbcConnectionId::default()], - // Version::new("ics20-1".to_string()), - // ); - // contract - // .store_channel_end( - // &mut deps.storage, - // &src_port.clone(), - // &src_channel.clone(), - // &chan_end_on_a_ordered, - // ) - // .unwrap(); - // let conn_prefix = common::ibc::core::ics23_commitment::commitment::CommitmentPrefix::try_from( - // "hello".to_string().as_bytes().to_vec(), - // ); - - // let conn_end_on_a = ConnectionEnd::new( - // ConnectionState::Open, - // ClientId::default(), - // ConnectionCounterparty::new( - // ClientId::default(), - // Some(ConnectionId::default()), - // conn_prefix.unwrap(), - // ), - // get_compatible_versions(), - // ZERO_DURATION, - // ); - // contract - // .store_connection( - // &mut deps.storage, - // &chan_end_on_a_ordered.connection_hops()[0].clone(), - // &conn_end_on_a, - // ) - // .unwrap(); - // let packet_commitment = - // compute_packet_commitment(&packet.data, &packet_timeout_height, &packet_timestamp); - - // contract - // .store_packet_commitment( - // &mut deps.storage, - // &src_port, - // &src_channel, - // packet_sequence, - // packet_commitment, - // ) - // .unwrap(); - - // let client_state: ClientState = get_dummy_client_state(); - - // let client = client_state.to_any().encode_to_vec(); - // contract - // .store_client_state( - // &mut deps.storage, - // &env, - // &IbcClientId::default(), - // client, - // client_state.get_keccak_hash().to_vec(), - // ) - // .unwrap(); - // let _client_type = IbcClientType::new("iconclient".to_string()); - - // let light_client = LightClient::new("lightclient".to_string()); - // contract - // .store_client_implementations( - // &mut deps.storage, - // &IbcClientId::default(), - // light_client.clone(), - // ) - // .unwrap(); - let timestamp_query = LightClient::get_timestamp_at_height_query(&IbcClientId::default(), proof_height).unwrap(); let mut mocks = test_context.mock_queries.clone(); mocks.insert(timestamp_query, to_binary(&0_u64).unwrap()); mock_lightclient_query(mocks, &mut deps); - // contract - // .bind_port(&mut deps.storage, &src_port, "moduleaddress".to_string()) - // .unwrap(); - - // let consenus_state: ConsensusState = common::icon::icon::lightclient::v1::ConsensusState { - // message_root: vec![1, 2, 3, 4], - // next_proof_context_hash: vec![1, 2, 3], - // } - // .try_into() - // .unwrap(); - - // let consenus_state_any = consenus_state.to_any().encode_to_vec(); - // contract - // .store_consensus_state( - // &mut deps.storage, - // &IbcClientId::default(), - // proof_height, - // consenus_state_any, - // consenus_state.get_keccak_hash().to_vec(), - // ) - // .unwrap(); - // let env = get_mock_env(); - // contract - // .ibc_store() - // .expected_time_per_block() - // .save(deps.as_mut().storage, &(env.block.time.seconds())) - // .unwrap(); - // let light_client = LightClient::new("lightclient".to_string()); - // contract - // .store_client_implementations(deps.as_mut().storage, &IbcClientId::default(), light_client) - // .unwrap(); contract .timeout_packet_validate_to_light_client(deps.as_mut(), info, env, msg) .unwrap(); @@ -298,91 +186,6 @@ fn test_timeout_packet_fails_if_timestamp_not_expired() { let mut test_context = TestContext::for_packet_timeout(env.clone(), &msg); test_context.init_timeout_packet(deps.as_mut().storage, &contract); - // let packet = &msg.packet.clone().unwrap(); - // let src_port = to_ibc_port_id(&packet.source_port).unwrap(); - // let src_channel = to_ibc_channel_id(&packet.source_channel).unwrap(); - - // let dst_port = to_ibc_port_id(&packet.destination_port).unwrap(); - // let dst_channel = to_ibc_channel_id(&packet.destination_channel).unwrap(); - - // let packet_timeout_height = to_ibc_timeout_height(packet.timeout_height.clone()).unwrap(); - // let packet_timestamp = to_ibc_timestamp(packet.timeout_timestamp).unwrap(); - // let packet_sequence = Sequence::from(packet.sequence); - // let proof_height = to_ibc_height(msg.proof_height.clone()).unwrap(); - // let _next_sequence_recv = Sequence::from(msg.next_sequence_recv); - - // let chan_end_on_a_ordered = ChannelEnd::new( - // State::Open, - // Order::Ordered, - // Counterparty::new(dst_port, Some(dst_channel)), - // vec![IbcConnectionId::default()], - // Version::new("ics20-1".to_string()), - // ); - // contract - // .store_channel_end( - // &mut deps.storage, - // &src_port.clone(), - // &src_channel.clone(), - // &chan_end_on_a_ordered, - // ) - // .unwrap(); - // let conn_prefix = common::ibc::core::ics23_commitment::commitment::CommitmentPrefix::try_from( - // "hello".to_string().as_bytes().to_vec(), - // ); - - // let conn_end_on_a = ConnectionEnd::new( - // ConnectionState::Open, - // ClientId::default(), - // ConnectionCounterparty::new( - // ClientId::default(), - // Some(ConnectionId::default()), - // conn_prefix.unwrap(), - // ), - // get_compatible_versions(), - // ZERO_DURATION, - // ); - // contract - // .store_connection( - // &mut deps.storage, - // &chan_end_on_a_ordered.connection_hops()[0].clone(), - // &conn_end_on_a, - // ) - // .unwrap(); - // let packet_commitment = - // compute_packet_commitment(&packet.data, &packet_timeout_height, &packet_timestamp); - - // contract - // .store_packet_commitment( - // &mut deps.storage, - // &src_port, - // &src_channel, - // packet_sequence, - // packet_commitment, - // ) - // .unwrap(); - - // let client_state: ClientState = get_dummy_client_state(); - - // let client = client_state.to_any().encode_to_vec(); - // contract - // .store_client_commitment( - // &mut deps.storage, - // &env, - // &IbcClientId::default(), - - // client_state.get_keccak_hash().to_vec(), - // ) - // .unwrap(); - // let _client_type = IbcClientType::new("iconclient".to_string()); - - // let light_client = LightClient::new("lightclient".to_string()); - // contract - // .store_client_implementations( - // &mut deps.storage, - // &IbcClientId::default(), - // light_client.clone(), - // ) - // .unwrap(); let timestamp_query = LightClient::get_timestamp_at_height_query(&IbcClientId::default(), proof_height).unwrap(); let mut mocks = test_context.mock_queries.clone(); @@ -393,38 +196,83 @@ fn test_timeout_packet_fails_if_timestamp_not_expired() { ); mock_lightclient_query(mocks, &mut deps); - // contract - // .bind_port(&mut deps.storage, &src_port, "moduleaddress".to_string()) - // .unwrap(); - - // let consenus_state: ConsensusState = common::icon::icon::lightclient::v1::ConsensusState { - // message_root: vec![1, 2, 3, 4], - // next_proof_context_hash: vec![1, 2, 3], - // } - // .try_into() - // .unwrap(); - - // let consenus_state_any = consenus_state.to_any().encode_to_vec(); - // contract - // .store_consensus_commitment( - // &mut deps.storage, - // &IbcClientId::default(), - // proof_height, - - // consenus_state.get_keccak_hash().to_vec(), - // ) - // .unwrap(); - // let env = get_mock_env(); - // contract - // .ibc_store() - // .expected_time_per_block() - // .save(deps.as_mut().storage, &(env.block.time.seconds())) - // .unwrap(); - // let light_client = LightClient::new("lightclient".to_string()); - // contract - // .store_client_implementations(deps.as_mut().storage, &IbcClientId::default(), light_client) - // .unwrap(); contract .timeout_packet_validate_to_light_client(deps.as_mut(), info, env, msg) .unwrap(); } + +#[test] +#[should_panic(expected = "ChannelClosed")] +fn test_timeout_packet_fails_for_closed_connection() { + let proof_height = 50; + let timeout_height = proof_height - 1; + let timeout_timestamp = 0; + let msg = get_dummy_raw_msg_timeout(proof_height, timeout_height, timeout_timestamp); + + let mut ctx = TestContext::for_packet_timeout(get_mock_env(), &msg); + let contract = CwIbcCoreContext::default(); + let mut deps = deps(); + let info = create_mock_info("channel-creater", "umlg", 20000000); + + if let Some(channel_end) = &mut ctx.channel_end { + channel_end.state = cw_ibc_core::ics04_channel::State::Closed; + } + + ctx.init_timeout_packet(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + contract + .timeout_packet_validate_to_light_client(deps.as_mut(), info, ctx.env, msg) + .unwrap(); +} + +#[test] +#[should_panic(expected = "InvalidPacketSequence")] +fn test_timeout_packet_fails_for_invalid_packet_sequence() { + let proof_height = 50; + let timeout_height = proof_height - 1; + let timeout_timestamp = 0; + let mut msg = get_dummy_raw_msg_timeout(proof_height, timeout_height, timeout_timestamp); + msg.next_sequence_recv = 20; + + let mut ctx = TestContext::for_packet_timeout(get_mock_env(), &msg); + let contract = CwIbcCoreContext::default(); + let mut deps = deps(); + let info = create_mock_info("channel-creater", "umlg", 20000000); + + if let Some(channel_end) = &mut ctx.channel_end { + channel_end.ordering = channel::Order::Ordered; + } + + ctx.init_timeout_packet(deps.as_mut().storage, &contract); + ctx.save_timestamp_at_height(proof_height, 0); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + contract + .timeout_packet_validate_to_light_client(deps.as_mut(), info, ctx.env, msg) + .unwrap(); +} + +#[test] +fn test_timeout_packet_for_ordered_channel() { + let proof_height = 50; + let timeout_height = proof_height - 1; + let timeout_timestamp = 0; + let msg = get_dummy_raw_msg_timeout(proof_height, timeout_height, timeout_timestamp); + + let mut ctx = TestContext::for_packet_timeout(get_mock_env(), &msg); + let contract = CwIbcCoreContext::default(); + let mut deps = deps(); + let info = create_mock_info("channel-creater", "umlg", 20000000); + + if let Some(channel_end) = &mut ctx.channel_end { + channel_end.ordering = channel::Order::Ordered; + } + + ctx.init_timeout_packet(deps.as_mut().storage, &contract); + ctx.save_timestamp_at_height(proof_height, 0); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + let res = contract.timeout_packet_validate_to_light_client(deps.as_mut(), info, ctx.env, msg); + assert!(res.is_ok()) +} diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_timeout_on_close.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_timeout_on_close.rs index 9fff8a5dc..eb7c0a61d 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_timeout_on_close.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_timeout_on_close.rs @@ -1,3 +1,4 @@ +use cw_common::types::TimeoutMsgType; use cw_ibc_core::VALIDATE_ON_PACKET_TIMEOUT_ON_MODULE; use super::*; @@ -17,8 +18,12 @@ fn test_timeout_on_close_packet_validate_to_light_client() { mock_lightclient_query(test_context.mock_queries, &mut deps); - let res = - contract.timeout_on_close_packet_validate_to_light_client(deps.as_mut(), info, env, msg); + let res = contract.timeout_packet_validate( + deps.as_mut(), + env, + info, + TimeoutMsgType::TimeoutOnClose(msg), + ); print!("{:?}", res); assert!(res.is_ok()); assert_eq!( @@ -26,3 +31,129 @@ fn test_timeout_on_close_packet_validate_to_light_client() { VALIDATE_ON_PACKET_TIMEOUT_ON_MODULE ) } + +#[test] +#[should_panic(expected = "InvalidPacketCounterparty")] +fn test_timeout_on_close_packet_validate_to_light_client_fails_on_invalid_channel_counterparty() { + let height = 2; + let timeout_timestamp = 5; + let mut msg = get_dummy_raw_msg_timeout_on_close(height, timeout_timestamp); + + let mut ctx = TestContext::for_packet_timeout_on_close(get_mock_env(), &msg); + let contract = CwIbcCoreContext::default(); + let mut deps = deps(); + let info = create_mock_info("channel-creater", "umlg", 20000000); + + ctx.init_timeout_packet_on_close(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + if let Some(packet) = &mut msg.packet { + packet.destination_port = "different_port".to_string(); + } + + contract + .timeout_on_close_packet_validate_to_light_client(deps.as_mut(), info, ctx.env, msg) + .unwrap(); +} + +#[test] +#[should_panic( + expected = "IbcPacketError { error: IncorrectPacketCommitment { sequence: Sequence(1) } }" +)] +fn test_timeout_on_close_packet_validate_to_light_client_fails_on_incorrect_packet_commitment() { + let height = 2; + let timeout_timestamp = 5; + let mut msg = get_dummy_raw_msg_timeout_on_close(height, timeout_timestamp); + + let mut ctx = TestContext::for_packet_timeout_on_close(get_mock_env(), &msg); + let contract = CwIbcCoreContext::default(); + let mut deps = deps(); + let info = create_mock_info("channel-creater", "umlg", 20000000); + + ctx.init_timeout_packet_on_close(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + if let Some(packet) = &mut msg.packet { + packet.timeout_timestamp = 100; + } + + contract + .timeout_on_close_packet_validate_to_light_client(deps.as_mut(), info, ctx.env, msg) + .unwrap(); +} + +#[test] +#[should_panic(expected = "FrozenClient")] +fn test_timeout_on_close_packet_validate_to_light_client_fails_for_frozen_client() { + let height = 2; + let timeout_timestamp = 5; + let msg = get_dummy_raw_msg_timeout_on_close(height, timeout_timestamp); + + let mut ctx = TestContext::for_packet_timeout_on_close(get_mock_env(), &msg); + let contract = CwIbcCoreContext::default(); + let mut deps = deps(); + let info = create_mock_info("channel-creater", "umlg", 20000000); + + if let Some(client_state) = &mut ctx.client_state { + client_state.frozen_height = 10; + } + + ctx.init_timeout_packet_on_close(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + contract + .timeout_on_close_packet_validate_to_light_client(deps.as_mut(), info, ctx.env, msg) + .unwrap(); +} + +#[test] +#[should_panic(expected = "InvalidPacketSequence")] +fn test_timeout_on_close_packet_validate_to_light_client_fails_on_invalid_packet_sequence() { + let height = 2; + let timeout_timestamp = 5; + let mut msg = get_dummy_raw_msg_timeout_on_close(height, timeout_timestamp); + msg.next_sequence_recv = 2; + + let mut ctx = TestContext::for_packet_timeout_on_close(get_mock_env(), &msg); + let contract = CwIbcCoreContext::default(); + let mut deps = deps(); + let info = create_mock_info("channel-creater", "umlg", 20000000); + + if let Some(channel_end) = &mut ctx.channel_end { + channel_end.ordering = Order::Ordered; + } + + ctx.init_timeout_packet_on_close(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + contract + .timeout_on_close_packet_validate_to_light_client(deps.as_mut(), info, ctx.env, msg) + .unwrap(); +} + +#[test] +fn test_timeout_on_close_packet_validate_to_light_client_for_orderd_channel() { + let height = 2; + let timeout_timestamp = 5; + let msg = get_dummy_raw_msg_timeout_on_close(height, timeout_timestamp); + + let mut ctx = TestContext::for_packet_timeout_on_close(get_mock_env(), &msg); + let contract = CwIbcCoreContext::default(); + let mut deps = deps(); + let info = create_mock_info("channel-creater", "umlg", 20000000); + + if let Some(channel_end) = &mut ctx.channel_end { + channel_end.ordering = Order::Ordered; + } + + ctx.init_timeout_packet_on_close(deps.as_mut().storage, &contract); + mock_lightclient_query(ctx.mock_queries, &mut deps); + + let res = contract.timeout_on_close_packet_validate_to_light_client( + deps.as_mut(), + info, + ctx.env, + msg, + ); + assert!(res.is_ok()) +} diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/test_channel.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/test_channel.rs index 082d5a003..64d2b28fd 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/test_channel.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/test_channel.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::{str::FromStr, time::Duration}; use common::traits::AnyTypes; -use cosmwasm_std::testing::mock_env; +use cosmwasm_std::testing::{mock_dependencies, mock_env}; use cosmwasm_std::{ to_binary, Addr, Event, IbcEndpoint, IbcPacket, IbcPacketReceiveMsg, IbcTimeout, IbcTimeoutBlock, Reply, SubMsgResponse, SubMsgResult, @@ -19,17 +19,13 @@ use common::ibc::{ events::IbcEventType, }; use cw_common::ibc_types::{IbcClientId, IbcConnectionId, IbcPortId}; -use cw_common::raw_types::channel::RawPacket; +use cw_common::raw_types::channel::*; use cw_common::raw_types::to_raw_packet; use cw_ibc_core::conversions::{to_ibc_channel, to_ibc_channel_id, to_ibc_height, to_ibc_port_id}; -use cw_ibc_core::ics04_channel::open_init::{ - create_channel_submesssage, on_chan_open_init_submessage, -}; - use cw_ibc_core::ics04_channel::{ - create_channel_event, create_packet_event, EXECUTE_ON_CHANNEL_OPEN_INIT, + create_channel_event, create_packet_event, open_init, EXECUTE_ON_CHANNEL_OPEN_INIT, EXECUTE_ON_CHANNEL_OPEN_TRY, }; use cw_ibc_core::light_client::light_client::LightClient; @@ -517,7 +513,7 @@ fn test_validate_open_init_channel() { let res = contract.validate_channel_open_init(deps.as_mut(), info.clone(), &raw); - let expected = on_chan_open_init_submessage( + let expected = open_init::on_chan_open_init_submessage( &test_context.channel_end(), &test_context.port_id, &test_context.channel_id, @@ -525,7 +521,7 @@ fn test_validate_open_init_channel() { ); let data = cw_common::ibc_dapp_msg::ExecuteMsg::IbcChannelOpen { msg: expected }; let data = to_binary(&data).unwrap(); - let on_chan_open_init = create_channel_submesssage( + let on_chan_open_init = open_init::create_channel_submesssage( "moduleaddress".to_string(), data, info.funds, @@ -667,3 +663,46 @@ fn test_get_channel_fail() { ctx.get_channel_end(mock_deps.as_ref().storage, &port_id, &channel_id) .unwrap(); } + +#[test] +#[should_panic(expected = "ChannelNotFound")] +fn fail_test_channel_end_not_found() { + let ctx = TestContext::default(get_mock_env()); + let deps = mock_dependencies(); + let contract = CwIbcCoreContext::new(); + + contract + .channel_end(deps.as_ref().storage, &ctx.port_id, &ctx.channel_id) + .unwrap(); +} + +#[test] +#[should_panic(expected = "InvalidEventType")] +fn fail_create_channel_event() { + let ctx = TestContext::default(get_mock_env()); + + create_channel_event( + IbcEventType::AppModule, + &ctx.port_id.to_string(), + &ctx.channel_id.to_string(), + &ctx.channel_end(), + ) + .unwrap(); +} + +#[test] +#[should_panic(expected = "InvalidEventType")] +fn fail_create_packet_event() { + let ctx = TestContext::default(get_mock_env()); + + let packet = get_dummy_raw_packet(10, 1); + + create_packet_event( + IbcEventType::CreateClient, + &packet, + &Order::Unordered, + &ctx.connection_id, + Some(Vec::new()), + ) + .unwrap(); +} diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/test_connection.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/test_connection.rs index 89c80cce9..bec0cd989 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/test_connection.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/test_connection.rs @@ -1,4 +1,5 @@ use cosmwasm_std::Timestamp; +use std::str::FromStr; use std::time::Duration; pub mod setup; @@ -32,6 +33,7 @@ use common::ibc::core::ics23_commitment::commitment::CommitmentPrefix; use common::ibc::core::ics24_host::identifier::ConnectionId; use common::ibc::events::IbcEventType; use cw_common::ibc_types::IbcClientId; +use cw_ibc_core::validations::ensure_consensus_height_valid; use cw_ibc_core::ConnectionEnd; use prost::Message; use setup::*; @@ -905,3 +907,13 @@ fn connection_open_ack_validate_fails_of_connection_mismatch() { .connection_open_ack(deps.as_mut(), info, env, message) .unwrap(); } + +#[test] +#[should_panic(expected = "InvalidConsensusHeight")] +fn test_ensure_consensus_height_valid() { + ensure_consensus_height_valid( + &common::ibc::core::ics02_client::height::Height::from_str("10-10").unwrap(), + &common::ibc::core::ics02_client::height::Height::from_str("11-11").unwrap(), + ) + .unwrap() +} diff --git a/contracts/cosmwasm-vm/cw-ibc-core/tests/test_host.rs b/contracts/cosmwasm-vm/cw-ibc-core/tests/test_host.rs index 4e1806966..ab42ce1a9 100644 --- a/contracts/cosmwasm-vm/cw-ibc-core/tests/test_host.rs +++ b/contracts/cosmwasm-vm/cw-ibc-core/tests/test_host.rs @@ -136,6 +136,16 @@ fn test_set_expected_time_per_block() { assert!(result.is_ok()) } +#[test] +fn test_calc_block_delay_with_zero_deplay_period_time() { + let contract = CwIbcCoreContext::default(); + + let delay_period_time = Duration::from_secs(0); + let res = contract.calc_block_delay(&delay_period_time); + + assert_eq!(res, 0); +} + #[test] fn test_get_expected_time_per_block() { let mut deps = deps();