From 3ff3df7424fc1afef98ac9a0a90aa8b3e131c520 Mon Sep 17 00:00:00 2001 From: poisonphang <17688291+PoisonPhang@users.noreply.github.com> Date: Mon, 6 Jan 2025 09:51:03 -0600 Subject: [PATCH 01/11] chore: wip --- cosmwasm/ibc-union/core/Cargo.toml | 2 + .../ibc-union/core/msg/src/lightclient.rs | 2 +- cosmwasm/ibc-union/core/src/contract.rs | 141 ++++++++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) diff --git a/cosmwasm/ibc-union/core/Cargo.toml b/cosmwasm/ibc-union/core/Cargo.toml index d058278053..21caa9ecdd 100644 --- a/cosmwasm/ibc-union/core/Cargo.toml +++ b/cosmwasm/ibc-union/core/Cargo.toml @@ -17,8 +17,10 @@ library = [] [dependencies] alloy = { workspace = true, features = ["sol-types"] } +cosmwasm-schema = { workspace = true } cosmwasm-std = { workspace = true, features = ["abort"] } cw-storage-plus = { workspace = true } +ethabi = { workspace = true } hex = { workspace = true } ibc-solidity = { workspace = true, features = ["serde"] } ibc-union-msg = { workspace = true } diff --git a/cosmwasm/ibc-union/core/msg/src/lightclient.rs b/cosmwasm/ibc-union/core/msg/src/lightclient.rs index 806424455d..ac2abf3cf6 100644 --- a/cosmwasm/ibc-union/core/msg/src/lightclient.rs +++ b/cosmwasm/ibc-union/core/msg/src/lightclient.rs @@ -22,7 +22,7 @@ pub struct MisbehaviourResponse { pub client_state: Bytes, } -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(serde::Serialize, serde::Deserialize, Debug)] #[serde(deny_unknown_fields, rename_all = "snake_case")] pub struct VerifyCreationResponse { pub latest_height: u64, diff --git a/cosmwasm/ibc-union/core/src/contract.rs b/cosmwasm/ibc-union/core/src/contract.rs index 0fcb4adf55..04558be43e 100644 --- a/cosmwasm/ibc-union/core/src/contract.rs +++ b/cosmwasm/ibc-union/core/src/contract.rs @@ -1850,9 +1850,150 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result Addr { + let mock_api = MockApi::default(); + mock_api.addr_make(sender) + } + + fn new_client_registered_event(client_type: &str, client_address: &Addr) -> Event { + Event::new(events::client::REGISTER) + .add_attribute(events::attribute::CLIENT_TYPE, client_type) + .add_attribute(events::attribute::CLIENT_ADDRESS, client_address) + } + + fn new_client_created_event(client_type: &str, client_id: u32) -> Event { + Event::new(events::client::CREATE).add_attributes([ + (events::attribute::CLIENT_TYPE, client_type), + (events::attribute::CLIENT_ID, &client_id.to_string()), + ]) + } + + fn register_client(deps: DepsMut) -> Result { + let register_msg = ExecuteMsg::RegisterClient(MsgRegisterClient { + client_type: CLIENT_TYPE.into(), + client_address: mock_addr(CLIENT_ADDRESS).to_string(), + }); + let sender = mock_addr(SENDER); + + execute(deps, mock_env(), message_info(&sender, &[]), register_msg) + } + + fn create_client(mut deps: DepsMut) -> Result { + let _ = register_client(deps.branch())?; + + let execute_msg = ExecuteMsg::CreateClient(MsgCreateClient { + client_type: CLIENT_TYPE.into(), + client_state_bytes: vec![1, 2, 3].into(), + consensus_state_bytes: vec![1, 2, 3].into(), + relayer: mock_addr(RELAYER).into_string(), + }); + let sender = mock_addr(SENDER); + + execute(deps, mock_env(), message_info(&sender, &[]), execute_msg) + } + + fn wasm_query_handler StdResult + 'static>( + querier: F, + ) -> impl Fn(&WasmQuery) -> QuerierResult + 'static { + move |msg| match msg { + WasmQuery::Smart { msg, .. } => { + let msg: LightClientQueryMsg = from_json(msg).unwrap(); + let res = querier(msg).unwrap(); + QuerierResult::Ok(cosmwasm_std::ContractResult::Ok(res)) + } + _ => panic!( + "Only smart queries should be possible now. Adjust this based on your needs." + ), + } + } + + #[test] + fn register_client_ok() { + let mut deps = mock_dependencies(); + let res = register_client(deps.as_mut()).unwrap(); + + assert!(res + .events + .into_iter() + .any(|e| e == new_client_registered_event(CLIENT_TYPE, &mock_addr(CLIENT_ADDRESS)))); + + assert_eq!( + CLIENT_REGISTRY + .load(&deps.storage, CLIENT_TYPE) + .unwrap() + .as_str(), + mock_addr(CLIENT_ADDRESS).to_string() + ); + } + + #[test] + fn register_client_fails_when_duplicate() { + let mut deps = mock_dependencies(); + register_client(deps.as_mut()).unwrap(); + assert_eq!( + register_client(deps.as_mut()), + Err(ContractError::ClientTypeAlreadyExists) + ); + } + + #[test] + fn create_client_ok() { + let mut deps = mock_dependencies(); + let sender = mock_addr(SENDER); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&sender, &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + msg => panic!("should not be called: {:?}", msg), + })); + let res = create_client(deps.as_mut()).unwrap(); + + assert!(res + .events + .into_iter() + .any(|e| e == new_client_created_event(CLIENT_TYPE, 1))); + + assert_eq!(CLIENT_TYPES.load(&deps.storage, 1).unwrap(), CLIENT_TYPE); + assert_eq!( + CLIENT_IMPLS.load(&deps.storage, 1).unwrap().as_str(), + mock_addr(CLIENT_ADDRESS).to_string() + ); + assert_eq!(CLIENT_STATES.load(&deps.storage, 1).unwrap(), vec![1, 2, 3]); + assert_eq!( + CLIENT_CONSENSUS_STATES.load(&deps.storage, (1, 1)).unwrap(), + vec![1, 2, 3] + ); + // assert_eq!( + // deps.storage + // .get(unionlabs::ics24::ethabi::client_state_key(1).as_ref()) + // .unwrap(), + // commit(vec![1, 2, 3]).into_bytes().to_vec() + // ); + } + + #[test] + fn create_client_commitments_saved() {} + #[test] fn channel_value() { let channel = Channel { From db7feb2a55096ee90f36e8b1d0d1484820cb8528 Mon Sep 17 00:00:00 2001 From: PoisonPhang <17688291+PoisonPhang@users.noreply.github.com> Date: Mon, 6 Jan 2025 13:41:00 -0600 Subject: [PATCH 02/11] feat(union-ibc): test create client commit store --- cosmwasm/ibc-union/core/src/contract.rs | 71 +++++++++++++++++++------ 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/cosmwasm/ibc-union/core/src/contract.rs b/cosmwasm/ibc-union/core/src/contract.rs index 04558be43e..591c83edb9 100644 --- a/cosmwasm/ibc-union/core/src/contract.rs +++ b/cosmwasm/ibc-union/core/src/contract.rs @@ -1849,13 +1849,15 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result to_json_binary(&1), msg => panic!("should not be called: {:?}", msg), })); - let res = create_client(deps.as_mut()).unwrap(); + + assert!(create_client(deps.as_mut()).is_ok()) + } + + #[test] + fn create_client_commitments_saved() { + let mut deps = mock_dependencies(); + let sender = mock_addr(SENDER); + + instantiate( + deps.as_mut(), + mock_env(), + message_info(&sender, &[]), + InitMsg {}, + ) + .expect("instantiate ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + msg => panic!("should not be called: {:?}", msg), + })); + + let res = create_client(deps.as_mut()).expect("create client ok"); + let client_id: u32 = res + .events + .iter() + .find(|event| event.ty.eq(events::client::CREATE)) + .expect("create client event exists") + .attributes + .iter() + .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) + .expect("client type attribute exists") + .value + .parse() + .expect("client type string is u32"); assert!(res .events .into_iter() - .any(|e| e == new_client_created_event(CLIENT_TYPE, 1))); + .any(|e| e == new_client_created_event(CLIENT_TYPE, client_id))); - assert_eq!(CLIENT_TYPES.load(&deps.storage, 1).unwrap(), CLIENT_TYPE); assert_eq!( - CLIENT_IMPLS.load(&deps.storage, 1).unwrap().as_str(), + CLIENT_TYPES.load(&deps.storage, client_id).unwrap(), + CLIENT_TYPE + ); + assert_eq!( + CLIENT_IMPLS + .load(&deps.storage, client_id) + .unwrap() + .as_str(), mock_addr(CLIENT_ADDRESS).to_string() ); - assert_eq!(CLIENT_STATES.load(&deps.storage, 1).unwrap(), vec![1, 2, 3]); assert_eq!( - CLIENT_CONSENSUS_STATES.load(&deps.storage, (1, 1)).unwrap(), + CLIENT_STATES.load(&deps.storage, client_id).unwrap(), + vec![1, 2, 3] + ); + assert_eq!( + CLIENT_CONSENSUS_STATES + .load(&deps.storage, (client_id, 1)) + .unwrap(), vec![1, 2, 3] ); - // assert_eq!( - // deps.storage - // .get(unionlabs::ics24::ethabi::client_state_key(1).as_ref()) - // .unwrap(), - // commit(vec![1, 2, 3]).into_bytes().to_vec() - // ); } - #[test] - fn create_client_commitments_saved() {} - #[test] fn channel_value() { let channel = Channel { From 59c40ed1ad0885f791c2ed17231481a6dccaee0e Mon Sep 17 00:00:00 2001 From: PoisonPhang <17688291+PoisonPhang@users.noreply.github.com> Date: Mon, 6 Jan 2025 15:42:56 -0600 Subject: [PATCH 03/11] feat(union-ibc): test update client --- cosmwasm/ibc-union/core/src/contract.rs | 122 ++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/cosmwasm/ibc-union/core/src/contract.rs b/cosmwasm/ibc-union/core/src/contract.rs index 591c83edb9..94a3a086c4 100644 --- a/cosmwasm/ibc-union/core/src/contract.rs +++ b/cosmwasm/ibc-union/core/src/contract.rs @@ -2033,6 +2033,128 @@ mod tests { ); } + #[test] + fn update_client_ok() { + let mut deps = mock_dependencies(); + let sender = mock_addr(SENDER); + + instantiate( + deps.as_mut(), + mock_env(), + message_info(&sender, &[]), + InitMsg {}, + ) + .expect("instantiate ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyClientMessage { .. } => { + to_json_binary(&VerifyClientMessageUpdate { + height: 2, + consensus_state: vec![3, 2, 1].into(), + client_state: vec![3, 2, 1].into(), + }) + } + msg => panic!("should not be called: {:?}", msg), + })); + + let res = create_client(deps.as_mut()).expect("create client ok"); + let client_id: u32 = res + .events + .iter() + .find(|event| event.ty.eq(events::client::CREATE)) + .expect("create client event exists") + .attributes + .iter() + .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) + .expect("client type attribute exists") + .value + .parse() + .expect("client type string is u32"); + + let msg = ExecuteMsg::UpdateClient(MsgUpdateClient { + client_id, + client_message: vec![3, 2, 1].into(), + relayer: mock_addr(RELAYER).into_string(), + }); + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + msg + ) + .is_ok()) + } + + #[test] + fn update_client_ko() { + todo!() + } + + #[test] + fn update_client_commitments_saved() { + let mut deps = mock_dependencies(); + let sender = mock_addr(SENDER); + + instantiate( + deps.as_mut(), + mock_env(), + message_info(&sender, &[]), + InitMsg {}, + ) + .expect("instantiate ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyClientMessage { .. } => { + to_json_binary(&VerifyClientMessageUpdate { + height: 2, + consensus_state: vec![3, 2, 1].into(), + client_state: vec![3, 2, 1].into(), + }) + } + msg => panic!("should not be called: {:?}", msg), + })); + + let res = create_client(deps.as_mut()).expect("create client ok"); + let client_id: u32 = res + .events + .iter() + .find(|event| event.ty.eq(events::client::CREATE)) + .expect("create client event exists") + .attributes + .iter() + .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) + .expect("client type attribute exists") + .value + .parse() + .expect("client type string is u32"); + + let msg = ExecuteMsg::UpdateClient(MsgUpdateClient { + client_id, + client_message: vec![3, 2, 1].into(), + relayer: mock_addr(RELAYER).into_string(), + }); + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + msg, + ) + .expect("update client ok"); + + assert_eq!( + CLIENT_STATES.load(&deps.storage, client_id).unwrap(), + vec![3, 2, 1] + ); + assert_eq!( + CLIENT_CONSENSUS_STATES + .load(&deps.storage, (client_id, 2)) + .unwrap(), + vec![3, 2, 1] + ); + } + #[test] fn channel_value() { let channel = Channel { From 1434e00270201830e1c80ed92ba12e0ed4893c35 Mon Sep 17 00:00:00 2001 From: PoisonPhang <17688291+PoisonPhang@users.noreply.github.com> Date: Mon, 6 Jan 2025 15:59:22 -0600 Subject: [PATCH 04/11] feat(union-ibc): test update client ko --- cosmwasm/ibc-union/core/src/contract.rs | 302 ---------------- cosmwasm/ibc-union/core/src/lib.rs | 19 +- cosmwasm/ibc-union/core/src/tests.rs | 12 + cosmwasm/ibc-union/core/src/tests/client.rs | 27 ++ .../core/src/tests/client/cometbls.rs | 1 + .../ibc-union/core/src/tests/client/ibc.rs | 336 ++++++++++++++++++ .../light-clients/cometbls/Cargo.toml | 1 + .../light-clients/cometbls/src/lib.rs | 3 + .../light-clients/cometbls/src/tests.rs | 26 ++ .../cometbls/src/tests/client.rs | 257 ++++++++++++++ 10 files changed, 666 insertions(+), 318 deletions(-) create mode 100644 cosmwasm/ibc-union/core/src/tests.rs create mode 100644 cosmwasm/ibc-union/core/src/tests/client.rs create mode 100644 cosmwasm/ibc-union/core/src/tests/client/cometbls.rs create mode 100644 cosmwasm/ibc-union/core/src/tests/client/ibc.rs create mode 100644 cosmwasm/ibc-union/light-clients/cometbls/src/tests.rs create mode 100644 cosmwasm/ibc-union/light-clients/cometbls/src/tests/client.rs diff --git a/cosmwasm/ibc-union/core/src/contract.rs b/cosmwasm/ibc-union/core/src/contract.rs index 94a3a086c4..0fcb4adf55 100644 --- a/cosmwasm/ibc-union/core/src/contract.rs +++ b/cosmwasm/ibc-union/core/src/contract.rs @@ -1849,312 +1849,10 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result Addr { - let mock_api = MockApi::default(); - mock_api.addr_make(sender) - } - - fn new_client_registered_event(client_type: &str, client_address: &Addr) -> Event { - Event::new(events::client::REGISTER) - .add_attribute(events::attribute::CLIENT_TYPE, client_type) - .add_attribute(events::attribute::CLIENT_ADDRESS, client_address) - } - - fn new_client_created_event(client_type: &str, client_id: u32) -> Event { - Event::new(events::client::CREATE).add_attributes([ - (events::attribute::CLIENT_TYPE, client_type), - (events::attribute::CLIENT_ID, &client_id.to_string()), - ]) - } - - fn register_client(deps: DepsMut) -> Result { - let register_msg = ExecuteMsg::RegisterClient(MsgRegisterClient { - client_type: CLIENT_TYPE.into(), - client_address: mock_addr(CLIENT_ADDRESS).to_string(), - }); - let sender = mock_addr(SENDER); - - execute(deps, mock_env(), message_info(&sender, &[]), register_msg) - } - - fn create_client(mut deps: DepsMut) -> Result { - let _ = register_client(deps.branch())?; - - let execute_msg = ExecuteMsg::CreateClient(MsgCreateClient { - client_type: CLIENT_TYPE.into(), - client_state_bytes: vec![1, 2, 3].into(), - consensus_state_bytes: vec![1, 2, 3].into(), - relayer: mock_addr(RELAYER).into_string(), - }); - let sender = mock_addr(SENDER); - - execute(deps, mock_env(), message_info(&sender, &[]), execute_msg) - } - - fn wasm_query_handler StdResult + 'static>( - querier: F, - ) -> impl Fn(&WasmQuery) -> QuerierResult + 'static { - move |msg| match msg { - WasmQuery::Smart { msg, .. } => { - let msg: LightClientQueryMsg = from_json(msg).unwrap(); - let res = querier(msg).unwrap(); - QuerierResult::Ok(cosmwasm_std::ContractResult::Ok(res)) - } - _ => panic!( - "Only smart queries should be possible now. Adjust this based on your needs." - ), - } - } - - #[test] - fn register_client_ok() { - let mut deps = mock_dependencies(); - let res = register_client(deps.as_mut()).unwrap(); - - assert!(res - .events - .into_iter() - .any(|e| e == new_client_registered_event(CLIENT_TYPE, &mock_addr(CLIENT_ADDRESS)))); - - assert_eq!( - CLIENT_REGISTRY - .load(&deps.storage, CLIENT_TYPE) - .unwrap() - .as_str(), - mock_addr(CLIENT_ADDRESS).to_string() - ); - } - - #[test] - fn register_client_fails_when_duplicate() { - let mut deps = mock_dependencies(); - register_client(deps.as_mut()).unwrap(); - assert_eq!( - register_client(deps.as_mut()), - Err(ContractError::ClientTypeAlreadyExists) - ); - } - - #[test] - fn create_client_ok() { - let mut deps = mock_dependencies(); - let sender = mock_addr(SENDER); - - instantiate( - deps.as_mut(), - mock_env(), - message_info(&sender, &[]), - InitMsg {}, - ) - .unwrap(); - deps.querier - .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), - msg => panic!("should not be called: {:?}", msg), - })); - - assert!(create_client(deps.as_mut()).is_ok()) - } - - #[test] - fn create_client_commitments_saved() { - let mut deps = mock_dependencies(); - let sender = mock_addr(SENDER); - - instantiate( - deps.as_mut(), - mock_env(), - message_info(&sender, &[]), - InitMsg {}, - ) - .expect("instantiate ok"); - deps.querier - .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), - msg => panic!("should not be called: {:?}", msg), - })); - - let res = create_client(deps.as_mut()).expect("create client ok"); - let client_id: u32 = res - .events - .iter() - .find(|event| event.ty.eq(events::client::CREATE)) - .expect("create client event exists") - .attributes - .iter() - .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) - .expect("client type attribute exists") - .value - .parse() - .expect("client type string is u32"); - - assert!(res - .events - .into_iter() - .any(|e| e == new_client_created_event(CLIENT_TYPE, client_id))); - - assert_eq!( - CLIENT_TYPES.load(&deps.storage, client_id).unwrap(), - CLIENT_TYPE - ); - assert_eq!( - CLIENT_IMPLS - .load(&deps.storage, client_id) - .unwrap() - .as_str(), - mock_addr(CLIENT_ADDRESS).to_string() - ); - assert_eq!( - CLIENT_STATES.load(&deps.storage, client_id).unwrap(), - vec![1, 2, 3] - ); - assert_eq!( - CLIENT_CONSENSUS_STATES - .load(&deps.storage, (client_id, 1)) - .unwrap(), - vec![1, 2, 3] - ); - } - - #[test] - fn update_client_ok() { - let mut deps = mock_dependencies(); - let sender = mock_addr(SENDER); - - instantiate( - deps.as_mut(), - mock_env(), - message_info(&sender, &[]), - InitMsg {}, - ) - .expect("instantiate ok"); - deps.querier - .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), - LightClientQueryMsg::VerifyClientMessage { .. } => { - to_json_binary(&VerifyClientMessageUpdate { - height: 2, - consensus_state: vec![3, 2, 1].into(), - client_state: vec![3, 2, 1].into(), - }) - } - msg => panic!("should not be called: {:?}", msg), - })); - - let res = create_client(deps.as_mut()).expect("create client ok"); - let client_id: u32 = res - .events - .iter() - .find(|event| event.ty.eq(events::client::CREATE)) - .expect("create client event exists") - .attributes - .iter() - .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) - .expect("client type attribute exists") - .value - .parse() - .expect("client type string is u32"); - - let msg = ExecuteMsg::UpdateClient(MsgUpdateClient { - client_id, - client_message: vec![3, 2, 1].into(), - relayer: mock_addr(RELAYER).into_string(), - }); - assert!(execute( - deps.as_mut(), - mock_env(), - message_info(&mock_addr(SENDER), &[]), - msg - ) - .is_ok()) - } - - #[test] - fn update_client_ko() { - todo!() - } - - #[test] - fn update_client_commitments_saved() { - let mut deps = mock_dependencies(); - let sender = mock_addr(SENDER); - - instantiate( - deps.as_mut(), - mock_env(), - message_info(&sender, &[]), - InitMsg {}, - ) - .expect("instantiate ok"); - deps.querier - .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), - LightClientQueryMsg::VerifyClientMessage { .. } => { - to_json_binary(&VerifyClientMessageUpdate { - height: 2, - consensus_state: vec![3, 2, 1].into(), - client_state: vec![3, 2, 1].into(), - }) - } - msg => panic!("should not be called: {:?}", msg), - })); - - let res = create_client(deps.as_mut()).expect("create client ok"); - let client_id: u32 = res - .events - .iter() - .find(|event| event.ty.eq(events::client::CREATE)) - .expect("create client event exists") - .attributes - .iter() - .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) - .expect("client type attribute exists") - .value - .parse() - .expect("client type string is u32"); - - let msg = ExecuteMsg::UpdateClient(MsgUpdateClient { - client_id, - client_message: vec![3, 2, 1].into(), - relayer: mock_addr(RELAYER).into_string(), - }); - execute( - deps.as_mut(), - mock_env(), - message_info(&mock_addr(SENDER), &[]), - msg, - ) - .expect("update client ok"); - - assert_eq!( - CLIENT_STATES.load(&deps.storage, client_id).unwrap(), - vec![3, 2, 1] - ); - assert_eq!( - CLIENT_CONSENSUS_STATES - .load(&deps.storage, (client_id, 2)) - .unwrap(), - vec![3, 2, 1] - ); - } - #[test] fn channel_value() { let channel = Channel { diff --git a/cosmwasm/ibc-union/core/src/lib.rs b/cosmwasm/ibc-union/core/src/lib.rs index b51210bf2f..2c95cd4ac7 100644 --- a/cosmwasm/ibc-union/core/src/lib.rs +++ b/cosmwasm/ibc-union/core/src/lib.rs @@ -3,6 +3,9 @@ pub mod contract; pub mod state; +#[cfg(test)] +mod tests; + use cosmwasm_std::{Addr, StdError}; use ibc_solidity::{ChannelState, ConnectionState}; use thiserror::Error; @@ -124,19 +127,3 @@ impl ContractErrorKind { err.strip_prefix("IBC_UNION_ERR_")?.parse().ok() } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn display() { - assert_eq!( - ContractErrorKind::ArithmeticOverflow, - ContractErrorKind::parse_from_error_message( - &ContractError::ArithmeticOverflow.to_string() - ) - .unwrap() - ) - } -} diff --git a/cosmwasm/ibc-union/core/src/tests.rs b/cosmwasm/ibc-union/core/src/tests.rs new file mode 100644 index 0000000000..06ec0b504d --- /dev/null +++ b/cosmwasm/ibc-union/core/src/tests.rs @@ -0,0 +1,12 @@ +mod client; + +use super::*; + +#[test] +fn display() { + assert_eq!( + ContractErrorKind::ArithmeticOverflow, + ContractErrorKind::parse_from_error_message(&ContractError::ArithmeticOverflow.to_string()) + .unwrap() + ) +} diff --git a/cosmwasm/ibc-union/core/src/tests/client.rs b/cosmwasm/ibc-union/core/src/tests/client.rs new file mode 100644 index 0000000000..742f56aa5d --- /dev/null +++ b/cosmwasm/ibc-union/core/src/tests/client.rs @@ -0,0 +1,27 @@ +use cosmwasm_std::{ + from_json, testing::MockApi, Addr, Binary, QuerierResult, StdResult, WasmQuery, +}; +use union_ibc_msg::lightclient::QueryMsg as LightClientQueryMsg; + +mod cometbls; +mod ibc; + +/// Creates a mock address from a given string. Prefixed with the default +/// `MockApi` prefix. +fn mock_addr(sender: &str) -> Addr { + let mock_api = MockApi::default(); + mock_api.addr_make(sender) +} + +fn wasm_query_handler StdResult + 'static>( + querier: F, +) -> impl Fn(&WasmQuery) -> QuerierResult + 'static { + move |msg| match msg { + WasmQuery::Smart { msg, .. } => { + let msg: LightClientQueryMsg = from_json(msg).unwrap(); + let res = querier(msg).unwrap(); + QuerierResult::Ok(cosmwasm_std::ContractResult::Ok(res)) + } + _ => panic!("Only smart queries should be possible now. Adjust this based on your needs."), + } +} diff --git a/cosmwasm/ibc-union/core/src/tests/client/cometbls.rs b/cosmwasm/ibc-union/core/src/tests/client/cometbls.rs new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/cosmwasm/ibc-union/core/src/tests/client/cometbls.rs @@ -0,0 +1 @@ + diff --git a/cosmwasm/ibc-union/core/src/tests/client/ibc.rs b/cosmwasm/ibc-union/core/src/tests/client/ibc.rs new file mode 100644 index 0000000000..d705f6bdc2 --- /dev/null +++ b/cosmwasm/ibc-union/core/src/tests/client/ibc.rs @@ -0,0 +1,336 @@ +use cosmwasm_std::{ + testing::{message_info, mock_dependencies, mock_env}, + to_json_binary, Addr, DepsMut, Event, Response, +}; +use union_ibc_msg::{ + lightclient::{QueryMsg as LightClientQueryMsg, VerifyClientMessageUpdate}, + msg::{ExecuteMsg, InitMsg, MsgCreateClient, MsgRegisterClient, MsgUpdateClient}, +}; + +use super::*; +use crate::{ + contract::{events, execute, instantiate}, + ContractError, +}; + +const CLIENT_TYPE: &str = "union"; +const CLIENT_ADDRESS: &str = "unionclient"; +const SENDER: &str = "unionsender"; +const RELAYER: &str = "unionrelayer"; + +fn new_client_registered_event(client_type: &str, client_address: &Addr) -> Event { + Event::new(events::client::REGISTER) + .add_attribute(events::attribute::CLIENT_TYPE, client_type) + .add_attribute(events::attribute::CLIENT_ADDRESS, client_address) +} + +fn new_client_created_event(client_type: &str, client_id: u32) -> Event { + Event::new(events::client::CREATE).add_attributes([ + (events::attribute::CLIENT_TYPE, client_type), + (events::attribute::CLIENT_ID, &client_id.to_string()), + ]) +} + +fn register_client(deps: DepsMut) -> Result { + let register_msg = ExecuteMsg::RegisterClient(MsgRegisterClient { + client_type: CLIENT_TYPE.into(), + client_address: mock_addr(CLIENT_ADDRESS).to_string(), + }); + let sender = mock_addr(SENDER); + + execute(deps, mock_env(), message_info(&sender, &[]), register_msg) +} + +fn create_client(mut deps: DepsMut) -> Result { + let _ = register_client(deps.branch())?; + + let execute_msg = ExecuteMsg::CreateClient(MsgCreateClient { + client_type: CLIENT_TYPE.into(), + client_state_bytes: vec![1, 2, 3].into(), + consensus_state_bytes: vec![1, 2, 3].into(), + relayer: mock_addr(RELAYER).into_string(), + }); + let sender = mock_addr(SENDER); + + execute(deps, mock_env(), message_info(&sender, &[]), execute_msg) +} + +#[test] +fn register_client_ok() { + let mut deps = mock_dependencies(); + let res = register_client(deps.as_mut()).unwrap(); + + assert!(res + .events + .into_iter() + .any(|e| e == new_client_registered_event(CLIENT_TYPE, &mock_addr(CLIENT_ADDRESS)))); + + assert_eq!( + crate::state::CLIENT_REGISTRY + .load(&deps.storage, CLIENT_TYPE) + .unwrap() + .as_str(), + mock_addr(CLIENT_ADDRESS).to_string() + ); +} + +#[test] +fn register_client_fails_when_duplicate() { + let mut deps = mock_dependencies(); + register_client(deps.as_mut()).unwrap(); + assert_eq!( + register_client(deps.as_mut()), + Err(ContractError::ClientTypeAlreadyExists) + ); +} + +#[test] +fn create_client_ok() { + let mut deps = mock_dependencies(); + let sender = mock_addr(SENDER); + + instantiate( + deps.as_mut(), + mock_env(), + message_info(&sender, &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + msg => panic!("should not be called: {:?}", msg), + })); + + assert!(create_client(deps.as_mut()).is_ok()) +} + +#[test] +fn create_client_commitments_saved() { + let mut deps = mock_dependencies(); + let sender = mock_addr(SENDER); + + instantiate( + deps.as_mut(), + mock_env(), + message_info(&sender, &[]), + InitMsg {}, + ) + .expect("instantiate ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + msg => panic!("should not be called: {:?}", msg), + })); + + let res = create_client(deps.as_mut()).expect("create client ok"); + let client_id: u32 = res + .events + .iter() + .find(|event| event.ty.eq(events::client::CREATE)) + .expect("create client event exists") + .attributes + .iter() + .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) + .expect("client type attribute exists") + .value + .parse() + .expect("client type string is u32"); + + assert!(res + .events + .into_iter() + .any(|e| e == new_client_created_event(CLIENT_TYPE, client_id))); + + assert_eq!( + crate::state::CLIENT_TYPES + .load(&deps.storage, client_id) + .unwrap(), + CLIENT_TYPE + ); + assert_eq!( + crate::state::CLIENT_IMPLS + .load(&deps.storage, client_id) + .unwrap() + .as_str(), + mock_addr(CLIENT_ADDRESS).to_string() + ); + assert_eq!( + crate::state::CLIENT_STATES + .load(&deps.storage, client_id) + .unwrap(), + vec![1, 2, 3] + ); + assert_eq!( + crate::state::CLIENT_CONSENSUS_STATES + .load(&deps.storage, (client_id, 1)) + .unwrap(), + vec![1, 2, 3] + ); +} + +#[test] +fn update_client_ok() { + let mut deps = mock_dependencies(); + let sender = mock_addr(SENDER); + + instantiate( + deps.as_mut(), + mock_env(), + message_info(&sender, &[]), + InitMsg {}, + ) + .expect("instantiate ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyClientMessage { .. } => { + to_json_binary(&VerifyClientMessageUpdate { + height: 2, + consensus_state: vec![3, 2, 1].into(), + client_state: vec![3, 2, 1].into(), + }) + } + msg => panic!("should not be called: {:?}", msg), + })); + + let res = create_client(deps.as_mut()).expect("create client ok"); + let client_id: u32 = res + .events + .iter() + .find(|event| event.ty.eq(events::client::CREATE)) + .expect("create client event exists") + .attributes + .iter() + .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) + .expect("client type attribute exists") + .value + .parse() + .expect("client type string is u32"); + + let msg = ExecuteMsg::UpdateClient(MsgUpdateClient { + client_id, + client_message: vec![3, 2, 1].into(), + relayer: mock_addr(RELAYER).into_string(), + }); + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + msg + ) + .is_ok()) +} + +#[test] +fn update_client_ko() { + let mut deps = mock_dependencies(); + let sender = mock_addr(SENDER); + + instantiate( + deps.as_mut(), + mock_env(), + message_info(&sender, &[]), + InitMsg {}, + ) + .expect("instantiate ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyClientMessage { .. } => to_json_binary(&0), + msg => panic!("should not be called: {:?}", msg), + })); + + let res = create_client(deps.as_mut()).expect("create client ok"); + let client_id: u32 = res + .events + .iter() + .find(|event| event.ty.eq(events::client::CREATE)) + .expect("create client event exists") + .attributes + .iter() + .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) + .expect("client type attribute exists") + .value + .parse() + .expect("client type string is u32"); + + let msg = ExecuteMsg::UpdateClient(MsgUpdateClient { + client_id, + client_message: vec![3, 2, 1].into(), + relayer: mock_addr(RELAYER).into_string(), + }); + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + msg + ) + .is_err()) +} + +#[test] +fn update_client_commitments_saved() { + let mut deps = mock_dependencies(); + let sender = mock_addr(SENDER); + + instantiate( + deps.as_mut(), + mock_env(), + message_info(&sender, &[]), + InitMsg {}, + ) + .expect("instantiate ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyClientMessage { .. } => { + to_json_binary(&VerifyClientMessageUpdate { + height: 2, + consensus_state: vec![3, 2, 1].into(), + client_state: vec![3, 2, 1].into(), + }) + } + msg => panic!("should not be called: {:?}", msg), + })); + + let res = create_client(deps.as_mut()).expect("create client ok"); + let client_id: u32 = res + .events + .iter() + .find(|event| event.ty.eq(events::client::CREATE)) + .expect("create client event exists") + .attributes + .iter() + .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) + .expect("client type attribute exists") + .value + .parse() + .expect("client type string is u32"); + + let msg = ExecuteMsg::UpdateClient(MsgUpdateClient { + client_id, + client_message: vec![3, 2, 1].into(), + relayer: mock_addr(RELAYER).into_string(), + }); + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + msg, + ) + .expect("update client ok"); + + assert_eq!( + crate::state::CLIENT_STATES + .load(&deps.storage, client_id) + .unwrap(), + vec![3, 2, 1] + ); + assert_eq!( + crate::state::CLIENT_CONSENSUS_STATES + .load(&deps.storage, (client_id, 2)) + .unwrap(), + vec![3, 2, 1] + ); +} diff --git a/cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml b/cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml index b331b3a87e..f58768671a 100644 --- a/cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml +++ b/cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml @@ -31,6 +31,7 @@ base64 = { workspace = true } hex-literal = { workspace = true } lazy_static = "1.4.0" serde_json = { workspace = true } +union-ibc = { workspace = true } [features] diff --git a/cosmwasm/ibc-union/light-clients/cometbls/src/lib.rs b/cosmwasm/ibc-union/light-clients/cometbls/src/lib.rs index 3336ca5909..57b1ccb9a8 100644 --- a/cosmwasm/ibc-union/light-clients/cometbls/src/lib.rs +++ b/cosmwasm/ibc-union/light-clients/cometbls/src/lib.rs @@ -3,3 +3,6 @@ pub mod contract; pub mod errors; pub mod storage; pub mod zkp_verifier; + +#[cfg(test)] +mod tests; diff --git a/cosmwasm/ibc-union/light-clients/cometbls/src/tests.rs b/cosmwasm/ibc-union/light-clients/cometbls/src/tests.rs new file mode 100644 index 0000000000..971d27e257 --- /dev/null +++ b/cosmwasm/ibc-union/light-clients/cometbls/src/tests.rs @@ -0,0 +1,26 @@ +use cosmwasm_std::{ + from_json, testing::MockApi, Addr, Binary, QuerierResult, StdResult, WasmQuery, +}; +use union_ibc_msg::lightclient::QueryMsg as LightClientQueryMsg; + +mod client; + +/// Creates a mock address from a given string. Prefixed with the default +/// `MockApi` prefix. +fn mock_addr(sender: &str) -> Addr { + let mock_api = MockApi::default(); + mock_api.addr_make(sender) +} + +fn wasm_query_handler StdResult + 'static>( + querier: F, +) -> impl Fn(&WasmQuery) -> QuerierResult + 'static { + move |msg| match msg { + WasmQuery::Smart { msg, .. } => { + let msg: LightClientQueryMsg = from_json(msg).unwrap(); + let res = querier(msg).unwrap(); + QuerierResult::Ok(cosmwasm_std::ContractResult::Ok(res)) + } + _ => panic!("Only smart queries should be possible now. Adjust this based on your needs."), + } +} diff --git a/cosmwasm/ibc-union/light-clients/cometbls/src/tests/client.rs b/cosmwasm/ibc-union/light-clients/cometbls/src/tests/client.rs new file mode 100644 index 0000000000..4af608d01f --- /dev/null +++ b/cosmwasm/ibc-union/light-clients/cometbls/src/tests/client.rs @@ -0,0 +1,257 @@ +use std::io::Read; + +use cometbls_light_client_types::{ChainId, ClientState, ConsensusState}; +use cosmwasm_std::{ + testing::{message_info, mock_dependencies, mock_env}, + to_json_binary, +}; +use union_ibc::contract::{ + events::{self, attribute}, + execute, instantiate, +}; +use union_ibc_msg::{ + lightclient::QueryMsg as LightClientQueryMsg, + msg::{ExecuteMsg, InitMsg, MsgCreateClient, MsgRegisterClient}, +}; +use unionlabs::{ + bytes::Bytes, + encoding::{Decode, EncodeAs, EthAbi}, + ethereum::keccak256, + hash::hash_v2::{Base64, HexUnprefixed}, + ibc::core::{client::height::Height, commitment::merkle_root::MerkleRoot}, +}; + +use super::*; + +const SENDER: &str = "admin_sender"; +const CLIENT_TYPE: &str = "cometbls"; +const RELAYER: &str = "relayer"; + +// Client state starting values +const CHAIN_ID: &str = "test-chain"; +const TRUSTING_PERIOD: u64 = 86400; +const MAX_CLOCK_DRIFT: u64 = 300; +const FROZEN_HEIGHT: Height = Height::new(0); +const LATEST_HEIGHT: Height = Height::new(100); +const CONTRACT_ADDRESS_SEED: &str = "test-contract"; + +// Consensus state starting values +const TIMESTAMP: u64 = 1337; +const APP_HASH_SEED: &str = "app"; +const NEXT_VALIDATOR_HASH_SEED: &str = "validators"; + +fn misbehaviour_common(trusting_period: u64) -> u32 { + let mut deps = mock_dependencies(); + + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + msg => panic!("should not be called: {:?}", msg), + })); + + // Encode client state + let client_state = ClientState { + chain_id: ChainId::from_string(CHAIN_ID).expect("is valid chain ID"), + trusting_period, + max_clock_drift: trusting_period, + frozen_height: FROZEN_HEIGHT, + latest_height: Height::new(99), + contract_address: keccak256(CONTRACT_ADDRESS_SEED), + }; + let client_state_bytes = Bytes::from(client_state.clone().encode_as::()); + + // Encode consensus state + let consensus_state = ConsensusState { + timestamp: TIMESTAMP, + app_hash: MerkleRoot { + hash: keccak256(APP_HASH_SEED).into_encoding::(), + }, + next_validators_hash: keccak256(NEXT_VALIDATOR_HASH_SEED).into_encoding::(), + }; + let consensus_state_bytes = Bytes::from(consensus_state.clone().encode_as::()); + + // Register client type + let msg = MsgRegisterClient { + client_type: CLIENT_TYPE.to_owned(), + client_address: mock_addr(CLIENT_TYPE).into_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::RegisterClient(msg), + ) + .expect("register client ok"); + + // Create client + let msg = MsgCreateClient { + client_type: CLIENT_TYPE.to_owned(), + client_state_bytes, + consensus_state_bytes: consensus_state_bytes.clone(), + relayer: mock_addr(RELAYER).into_string(), + }; + let res = execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::CreateClient(msg), + ) + .expect("create client ok"); + let client_id: u32 = res + .events + .iter() + .find(|event| event.ty.eq(events::client::CREATE)) + .expect("create client event exists") + .attributes + .iter() + .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) + .expect("client type attribute exists") + .value + .parse() + .expect("client type string is u32"); + + dbg!(client_id); + + let client_state = ClientState { + latest_height: Height::new(100), + ..client_state + }; + let client_state_bytes = Bytes::from(client_state.clone().encode_as::()); + // Create client + let msg = MsgCreateClient { + client_type: CLIENT_TYPE.to_owned(), + client_state_bytes, + consensus_state_bytes, + relayer: mock_addr(RELAYER).into_string(), + }; + let res = execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::CreateClient(msg), + ) + .expect("create client ok"); + let client_id = res + .events + .iter() + .find(|event| event.ty.eq(events::client::CREATE)) + .expect("create client event exists") + .attributes + .iter() + .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) + .expect("client type attribute exists") + .value + .parse() + .expect("client type string is u32"); + + client_id +} + +#[test] +fn create_client_success() { + let mut deps = mock_dependencies(); + + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + msg => panic!("should not be called: {:?}", msg), + })); + + // Encode client state + let client_state = ClientState { + chain_id: ChainId::from_string(CHAIN_ID).expect("is valid chain ID"), + trusting_period: TRUSTING_PERIOD, + max_clock_drift: MAX_CLOCK_DRIFT, + frozen_height: FROZEN_HEIGHT, + latest_height: LATEST_HEIGHT, + contract_address: keccak256(CONTRACT_ADDRESS_SEED), + }; + let client_state_bytes = Bytes::from(client_state.clone().encode_as::()); + + // Encode consensus state + let consensus_state = ConsensusState { + timestamp: TIMESTAMP, + app_hash: MerkleRoot { + hash: keccak256(APP_HASH_SEED).into_encoding::(), + }, + next_validators_hash: keccak256(NEXT_VALIDATOR_HASH_SEED).into_encoding::(), + }; + let consensus_state_bytes = Bytes::from(consensus_state.clone().encode_as::()); + + // Register client type + let msg = MsgRegisterClient { + client_type: CLIENT_TYPE.to_owned(), + client_address: mock_addr(CLIENT_TYPE).into_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::RegisterClient(msg), + ) + .expect("register client ok"); + + // Create client + let msg = MsgCreateClient { + client_type: CLIENT_TYPE.to_owned(), + client_state_bytes, + consensus_state_bytes, + relayer: mock_addr(RELAYER).into_string(), + }; + let res = execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::CreateClient(msg), + ) + .expect("create client ok"); + let client_id: u32 = res + .events + .iter() + .find(|event| event.ty.eq(events::client::CREATE)) + .expect("create client event exists") + .attributes + .iter() + .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) + .expect("client type attribute exists") + .value + .parse() + .expect("client type string is u32"); + + // Verify client state was stored + let stored_client_state = >::decode( + &union_ibc::state::CLIENT_STATES + .load(&deps.storage, client_id) + .unwrap(), + ) + .unwrap(); + assert_eq!(stored_client_state, client_state); + + // Verify consensus state was stored + let stored_consensus_state = >::decode( + &union_ibc::state::CLIENT_CONSENSUS_STATES + .load(&deps.storage, (client_id, 1)) + .unwrap(), + ) + .unwrap(); + assert_eq!(stored_consensus_state, consensus_state); +} + +#[test] +fn misbehaviour_freezes_client() { + todo!("Mock CometBLS client and zkp verifier") +} From 59d517e5f448b451be3712b01419ed743bc9238c Mon Sep 17 00:00:00 2001 From: PoisonPhang <17688291+PoisonPhang@users.noreply.github.com> Date: Wed, 8 Jan 2025 20:50:36 -0600 Subject: [PATCH 05/11] feat(union-ibc): tests for 03-connection --- cosmwasm/ibc-union/core/src/tests.rs | 69 ++- cosmwasm/ibc-union/core/src/tests/client.rs | 26 +- .../ibc-union/core/src/tests/client/ibc.rs | 54 +-- .../ibc-union/core/src/tests/connection.rs | 3 + .../core/src/tests/connection/ibc.rs | 444 ++++++++++++++++++ .../light-clients/cometbls/Cargo.toml | 2 +- 6 files changed, 537 insertions(+), 61 deletions(-) create mode 100644 cosmwasm/ibc-union/core/src/tests/connection.rs create mode 100644 cosmwasm/ibc-union/core/src/tests/connection/ibc.rs diff --git a/cosmwasm/ibc-union/core/src/tests.rs b/cosmwasm/ibc-union/core/src/tests.rs index 06ec0b504d..6eec172c6a 100644 --- a/cosmwasm/ibc-union/core/src/tests.rs +++ b/cosmwasm/ibc-union/core/src/tests.rs @@ -1,7 +1,74 @@ -mod client; +use contract::execute; +use cosmwasm_std::{ + from_json, + testing::{message_info, mock_env, MockApi}, + Addr, Binary, DepsMut, QuerierResult, Response, StdResult, WasmQuery, +}; +use union_ibc_msg::{ + lightclient::QueryMsg as LightClientQueryMsg, + msg::{ExecuteMsg, MsgCreateClient, MsgRegisterClient}, +}; use super::*; +mod client; +mod connection; + +/// Creates a mock address from a given string. +/// Addresses are prefixed with the default [`MockApi`] prefix. +fn mock_addr(sender: impl Into) -> Addr { + let mock_api = MockApi::default(); + mock_api.addr_make(&Into::::into(sender)) +} + +fn wasm_query_handler StdResult + 'static>( + querier: F, +) -> impl Fn(&WasmQuery) -> QuerierResult + 'static { + move |msg| match msg { + WasmQuery::Smart { msg, .. } => { + let msg: LightClientQueryMsg = from_json(msg).unwrap(); + let res = querier(msg).unwrap(); + QuerierResult::Ok(cosmwasm_std::ContractResult::Ok(res)) + } + _ => panic!("Only smart queries should be possible now. Adjust this based on your needs."), + } +} + +/// Creates a mock client. +/// Uses [`mock_addr`] to convert address seeds to addresses +/// Addresses are prefixed with the default [`MockApi`] prefix. +fn register_client( + deps: DepsMut, + client_type: impl Into, + client_address_seed: impl Into, + sender_address_seed: impl Into, +) -> Result { + let register_msg = ExecuteMsg::RegisterClient(MsgRegisterClient { + client_type: client_type.into(), + client_address: mock_addr(client_address_seed).into_string(), + }); + + let sender = mock_addr(sender_address_seed); + execute(deps, mock_env(), message_info(&sender, &[]), register_msg) +} + +fn create_client( + deps: DepsMut, + client_type: impl Into + Clone, + sender_address_seed: impl Into + Clone, + relayer_address_seed: impl Into + Clone, +) -> Result { + let execute_msg = ExecuteMsg::CreateClient(MsgCreateClient { + client_type: client_type.into(), + client_state_bytes: vec![1, 2, 3].into(), + consensus_state_bytes: vec![1, 2, 3].into(), + relayer: mock_addr(relayer_address_seed).into_string(), + }); + + let sender = mock_addr(sender_address_seed); + execute(deps, mock_env(), message_info(&sender, &[]), execute_msg) +} + #[test] fn display() { assert_eq!( diff --git a/cosmwasm/ibc-union/core/src/tests/client.rs b/cosmwasm/ibc-union/core/src/tests/client.rs index 742f56aa5d..7a5b3623f4 100644 --- a/cosmwasm/ibc-union/core/src/tests/client.rs +++ b/cosmwasm/ibc-union/core/src/tests/client.rs @@ -1,27 +1,3 @@ -use cosmwasm_std::{ - from_json, testing::MockApi, Addr, Binary, QuerierResult, StdResult, WasmQuery, -}; -use union_ibc_msg::lightclient::QueryMsg as LightClientQueryMsg; +use super::*; -mod cometbls; mod ibc; - -/// Creates a mock address from a given string. Prefixed with the default -/// `MockApi` prefix. -fn mock_addr(sender: &str) -> Addr { - let mock_api = MockApi::default(); - mock_api.addr_make(sender) -} - -fn wasm_query_handler StdResult + 'static>( - querier: F, -) -> impl Fn(&WasmQuery) -> QuerierResult + 'static { - move |msg| match msg { - WasmQuery::Smart { msg, .. } => { - let msg: LightClientQueryMsg = from_json(msg).unwrap(); - let res = querier(msg).unwrap(); - QuerierResult::Ok(cosmwasm_std::ContractResult::Ok(res)) - } - _ => panic!("Only smart queries should be possible now. Adjust this based on your needs."), - } -} diff --git a/cosmwasm/ibc-union/core/src/tests/client/ibc.rs b/cosmwasm/ibc-union/core/src/tests/client/ibc.rs index d705f6bdc2..6b017f5fa0 100644 --- a/cosmwasm/ibc-union/core/src/tests/client/ibc.rs +++ b/cosmwasm/ibc-union/core/src/tests/client/ibc.rs @@ -1,10 +1,10 @@ use cosmwasm_std::{ testing::{message_info, mock_dependencies, mock_env}, - to_json_binary, Addr, DepsMut, Event, Response, + to_json_binary, Addr, Event, }; use union_ibc_msg::{ lightclient::{QueryMsg as LightClientQueryMsg, VerifyClientMessageUpdate}, - msg::{ExecuteMsg, InitMsg, MsgCreateClient, MsgRegisterClient, MsgUpdateClient}, + msg::{ExecuteMsg, InitMsg, MsgUpdateClient}, }; use super::*; @@ -31,34 +31,10 @@ fn new_client_created_event(client_type: &str, client_id: u32) -> Event { ]) } -fn register_client(deps: DepsMut) -> Result { - let register_msg = ExecuteMsg::RegisterClient(MsgRegisterClient { - client_type: CLIENT_TYPE.into(), - client_address: mock_addr(CLIENT_ADDRESS).to_string(), - }); - let sender = mock_addr(SENDER); - - execute(deps, mock_env(), message_info(&sender, &[]), register_msg) -} - -fn create_client(mut deps: DepsMut) -> Result { - let _ = register_client(deps.branch())?; - - let execute_msg = ExecuteMsg::CreateClient(MsgCreateClient { - client_type: CLIENT_TYPE.into(), - client_state_bytes: vec![1, 2, 3].into(), - consensus_state_bytes: vec![1, 2, 3].into(), - relayer: mock_addr(RELAYER).into_string(), - }); - let sender = mock_addr(SENDER); - - execute(deps, mock_env(), message_info(&sender, &[]), execute_msg) -} - #[test] fn register_client_ok() { let mut deps = mock_dependencies(); - let res = register_client(deps.as_mut()).unwrap(); + let res = register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER).unwrap(); assert!(res .events @@ -77,9 +53,9 @@ fn register_client_ok() { #[test] fn register_client_fails_when_duplicate() { let mut deps = mock_dependencies(); - register_client(deps.as_mut()).unwrap(); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER).unwrap(); assert_eq!( - register_client(deps.as_mut()), + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER), Err(ContractError::ClientTypeAlreadyExists) ); } @@ -102,7 +78,9 @@ fn create_client_ok() { msg => panic!("should not be called: {:?}", msg), })); - assert!(create_client(deps.as_mut()).is_ok()) + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + assert!(create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).is_ok()) } #[test] @@ -123,7 +101,9 @@ fn create_client_commitments_saved() { msg => panic!("should not be called: {:?}", msg), })); - let res = create_client(deps.as_mut()).expect("create client ok"); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + let res = create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); let client_id: u32 = res .events .iter() @@ -194,7 +174,9 @@ fn update_client_ok() { msg => panic!("should not be called: {:?}", msg), })); - let res = create_client(deps.as_mut()).expect("create client ok"); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + let res = create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); let client_id: u32 = res .events .iter() @@ -241,7 +223,9 @@ fn update_client_ko() { msg => panic!("should not be called: {:?}", msg), })); - let res = create_client(deps.as_mut()).expect("create client ok"); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + let res = create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); let client_id: u32 = res .events .iter() @@ -294,7 +278,9 @@ fn update_client_commitments_saved() { msg => panic!("should not be called: {:?}", msg), })); - let res = create_client(deps.as_mut()).expect("create client ok"); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + let res = create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); let client_id: u32 = res .events .iter() diff --git a/cosmwasm/ibc-union/core/src/tests/connection.rs b/cosmwasm/ibc-union/core/src/tests/connection.rs new file mode 100644 index 0000000000..7a5b3623f4 --- /dev/null +++ b/cosmwasm/ibc-union/core/src/tests/connection.rs @@ -0,0 +1,3 @@ +use super::*; + +mod ibc; diff --git a/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs b/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs new file mode 100644 index 0000000000..350d032f2c --- /dev/null +++ b/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs @@ -0,0 +1,444 @@ +use std::marker::PhantomData; + +use contract::{events::attribute::CLIENT_ID, instantiate}; +use cosmwasm_std::{testing::mock_dependencies, to_json_binary}; +use ibc_solidity::Connection; +use union_ibc_msg::msg::{ + ExecuteMsg, InitMsg, MsgConnectionOpenAck, MsgConnectionOpenConfirm, MsgConnectionOpenInit, + MsgConnectionOpenTry, MsgRegisterClient, +}; +use unionlabs::ibc::core::client::height::Height; + +use super::*; + +const CLIENT_TYPE: &str = "union"; +const CLIENT_ADDRESS: &str = "unionclient"; +const SENDER: &str = "unionsender"; +const RELAYER: &str = "unionrelayer"; + +fn connection_open_init( + deps: DepsMut, + client_id: impl Into, + counterparty_client_id: impl Into, + sender_address_seed: impl Into, + relayer_address_seed: impl Into, +) -> Result { + let msg = MsgConnectionOpenInit { + client_id: client_id.into(), + counterparty_client_id: counterparty_client_id.into(), + relayer: mock_addr(relayer_address_seed).into_string(), + }; + execute( + deps, + mock_env(), + message_info(&mock_addr(sender_address_seed), &[]), + ExecuteMsg::ConnectionOpenInit(msg), + ) +} + +fn connection_open_try( + deps: DepsMut, + counterparty_client_id: impl Into, + counterparty_connection_id: impl Into, + client_id: impl Into, + proof_height: impl Into, + relayer_address_seed: impl Into, +) -> Result { + let msg = MsgConnectionOpenTry { + counterparty_client_id: counterparty_client_id.into(), + counterparty_connection_id: counterparty_connection_id.into(), + client_id: client_id.into(), + proof_init: vec![1, 2, 3].into(), + proof_height: proof_height.into(), + relayer: mock_addr(relayer_address_seed).into_string(), + }; + + execute( + deps, + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ConnectionOpenTry(msg), + ) +} + +#[test] +fn connection_open_init_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + let msg = MsgConnectionOpenInit { + client_id: 1, + counterparty_client_id: 2, + relayer: mock_addr(RELAYER).into_string(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ConnectionOpenInit(msg), + ) + .is_ok()) +} + +#[test] +fn connection_open_init_commitment_saved() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + connection_open_init(deps.as_mut(), 1_u32, 2_u32, SENDER, RELAYER) + .expect("open connection init is ok"); + + assert_eq!( + crate::state::CONNECTIONS.load(&deps.storage, 1).unwrap(), + Connection { + state: ConnectionState::Init, + client_id: 1, + counterparty_client_id: 2, + counterparty_connection_id: 0 + } + ); +} + +#[test] +fn connection_open_try_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + let msg = MsgConnectionOpenTry { + counterparty_client_id: 2, + counterparty_connection_id: 1, + client_id: 1, + proof_init: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ConnectionOpenTry(msg), + ) + .is_ok()); +} + +#[test] +fn connection_open_try_client_not_found() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + + let msg = MsgConnectionOpenTry { + counterparty_client_id: 2, + counterparty_connection_id: 1, + client_id: 1, + proof_init: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ConnectionOpenTry(msg), + ) + .is_err_and(|err| { + match err { + ContractError::Std(err) => matches!(err, StdError::NotFound { .. }), + _ => false, + } + })); +} + +// #[test] +// fn connection_open_try_invalid_proof() { +// todo!() +// } + +#[test] +fn connection_open_try_commitment_saved() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + let msg = MsgConnectionOpenTry { + counterparty_client_id: 2, + counterparty_connection_id: 1, + client_id: 1, + proof_init: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ConnectionOpenTry(msg), + ) + .expect("connection open try is ok"); + + assert_eq!( + crate::state::CONNECTIONS.load(&deps.storage, 1).unwrap(), + Connection { + state: ConnectionState::TryOpen, + client_id: 1, + counterparty_client_id: 2, + counterparty_connection_id: 1 + } + ); +} + +#[test] +fn connection_open_ack_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + connection_open_init(deps.as_mut(), 1_u32, 2_u32, SENDER, RELAYER) + .expect("connection open init is ok"); + + let msg = MsgConnectionOpenAck { + connection_id: 1, + counterparty_connection_id: 1, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + assert!(dbg!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ConnectionOpenAck(msg), + )) + .is_ok()) +} + +// #[test] +// fn connection_open_ack_invalid_proof() { +// todo!() +// } + +#[test] +fn connection_open_ack_commitment_saved() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + connection_open_init(deps.as_mut(), 1_u32, 2_u32, SENDER, RELAYER) + .expect("connection open init is ok"); + + let msg = MsgConnectionOpenAck { + connection_id: 1, + counterparty_connection_id: 1, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ConnectionOpenAck(msg), + ) + .expect("connection open ack is ok"); + + assert_eq!( + crate::state::CONNECTIONS.load(&deps.storage, 1).unwrap(), + Connection { + state: ConnectionState::Open, + client_id: 1, + counterparty_client_id: 2, + counterparty_connection_id: 1 + } + ); +} + +#[test] +fn connection_open_confirm_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER) + .expect("connection open try is ok"); + + let msg = MsgConnectionOpenConfirm { + connection_id: 1, + proof_ack: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + assert!(dbg!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ConnectionOpenConfirm(msg), + )) + .is_ok()); +} + +// #[test] +// fn connection_open_confirm_invalid_proof() { +// todo!() +// } + +#[test] +fn connection_open_try_confirm_commitment_saved() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER) + .expect("connection open try is ok"); + + let msg = MsgConnectionOpenConfirm { + connection_id: 1, + proof_ack: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ConnectionOpenConfirm(msg), + ) + .expect("connection_open_confirm is ok"); + + assert_eq!( + crate::state::CONNECTIONS.load(&deps.storage, 1).unwrap(), + Connection { + state: ConnectionState::Open, + client_id: 1, + counterparty_client_id: 2, + counterparty_connection_id: 1 + } + ); +} diff --git a/cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml b/cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml index f58768671a..73458f5092 100644 --- a/cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml +++ b/cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml @@ -31,7 +31,7 @@ base64 = { workspace = true } hex-literal = { workspace = true } lazy_static = "1.4.0" serde_json = { workspace = true } -union-ibc = { workspace = true } +union-ibc = { workspace = true } [features] From 439f3221361059ad911cc4446919a2951c306591 Mon Sep 17 00:00:00 2001 From: PoisonPhang <17688291+PoisonPhang@users.noreply.github.com> Date: Thu, 9 Jan 2025 20:23:46 -0600 Subject: [PATCH 06/11] feat(union-ibc): 04-channel tests --- cosmwasm/ibc-union/core/src/tests.rs | 1 + cosmwasm/ibc-union/core/src/tests/channel.rs | 4 + .../core/src/tests/channel/ibc_channel.rs | 764 ++++++++++++++++++ .../core/src/tests/channel/ibc_packet.rs | 0 4 files changed, 769 insertions(+) create mode 100644 cosmwasm/ibc-union/core/src/tests/channel.rs create mode 100644 cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs create mode 100644 cosmwasm/ibc-union/core/src/tests/channel/ibc_packet.rs diff --git a/cosmwasm/ibc-union/core/src/tests.rs b/cosmwasm/ibc-union/core/src/tests.rs index 6eec172c6a..dcdcbeaa20 100644 --- a/cosmwasm/ibc-union/core/src/tests.rs +++ b/cosmwasm/ibc-union/core/src/tests.rs @@ -11,6 +11,7 @@ use union_ibc_msg::{ use super::*; +mod channel; mod client; mod connection; diff --git a/cosmwasm/ibc-union/core/src/tests/channel.rs b/cosmwasm/ibc-union/core/src/tests/channel.rs new file mode 100644 index 0000000000..7d2a3279d2 --- /dev/null +++ b/cosmwasm/ibc-union/core/src/tests/channel.rs @@ -0,0 +1,4 @@ +use super::*; + +mod ibc_channel; +mod ibc_packet; diff --git a/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs b/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs new file mode 100644 index 0000000000..7e87f6ef97 --- /dev/null +++ b/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs @@ -0,0 +1,764 @@ +use contract::instantiate; +use cosmwasm_std::{testing::mock_dependencies, to_json_binary}; +use ibc_solidity::Channel; +use union_ibc_msg::msg::{ + InitMsg, MsgChannelOpenAck, MsgChannelOpenConfirm, MsgChannelOpenInit, MsgChannelOpenTry, + MsgConnectionOpenConfirm, MsgConnectionOpenTry, +}; + +use super::*; + +const CLIENT_TYPE: &str = "union"; +const CLIENT_ADDRESS: &str = "unionclient"; +const SENDER: &str = "unionsender"; +const RELAYER: &str = "unionrelayer"; +const VERSION: &str = "version"; + +fn connection_open_try( + deps: DepsMut, + counterparty_client_id: impl Into, + counterparty_connection_id: impl Into, + client_id: impl Into, + proof_height: impl Into, + relayer_address_seed: impl Into, + sender_address_seed: impl Into, +) -> Result { + let msg = MsgConnectionOpenTry { + counterparty_client_id: counterparty_client_id.into(), + counterparty_connection_id: counterparty_connection_id.into(), + client_id: client_id.into(), + proof_init: vec![1, 2, 3].into(), + proof_height: proof_height.into(), + relayer: mock_addr(relayer_address_seed).into_string(), + }; + execute( + deps, + mock_env(), + message_info(&mock_addr(sender_address_seed), &[]), + ExecuteMsg::ConnectionOpenTry(msg), + ) +} + +fn connection_open_confirm( + deps: DepsMut, + connection_id: impl Into, + proof_height: impl Into, + relayer_address_seed: impl Into, + sender_address_seed: impl Into, +) -> Result { + let msg = MsgConnectionOpenConfirm { + connection_id: connection_id.into(), + proof_ack: vec![1, 2, 3].into(), + proof_height: proof_height.into(), + relayer: mock_addr(relayer_address_seed).into_string(), + }; + execute( + deps, + mock_env(), + message_info(&mock_addr(sender_address_seed), &[]), + ExecuteMsg::ConnectionOpenConfirm(msg), + ) +} + +fn channel_open_init(deps: DepsMut) -> Result { + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps, + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) +} + +#[test] +fn channel_open_init_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .is_ok()); +} + +#[test] +fn channel_open_init_channel_claimed() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + + assert_eq!( + crate::state::CHANNEL_OWNER.load(&deps.storage, 1).unwrap(), + mock_addr(SENDER) + ); +} + +#[test] +fn channel_open_init_commitment_saved() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + + assert_eq!( + crate::state::CHANNELS.load(&deps.storage, 1).unwrap(), + Channel { + state: ChannelState::Init, + connection_id: 1, + counterparty_channel_id: 0, + counterparty_port_id: vec![1].into(), + version: VERSION.to_owned() + } + ); +} + +#[test] +fn channel_open_try_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + + let msg = MsgChannelOpenTry { + port_id: mock_addr(SENDER).into_string(), + channel: Channel { + state: ChannelState::TryOpen, + connection_id: 1, + counterparty_channel_id: 0, + counterparty_port_id: vec![1].into(), + version: VERSION.to_owned(), + }, + counterparty_version: VERSION.to_owned(), + proof_init: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenTry(msg), + ) + .is_ok()) +} +#[test] +fn channel_open_try_invalid_state() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + + let msg = MsgChannelOpenTry { + port_id: mock_addr(SENDER).into_string(), + channel: Channel { + state: ChannelState::Unspecified, + connection_id: 1, + counterparty_channel_id: 0, + counterparty_port_id: vec![1].into(), + version: VERSION.to_owned(), + }, + counterparty_version: VERSION.to_owned(), + proof_init: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenTry(msg), + ) + .is_err_and(|err| { + matches!( + err, + ContractError::ChannelInvalidState { + got: ChannelState::Unspecified, + expected: ChannelState::TryOpen + } + ) + })) +} + +#[test] +fn channel_open_try_channel_claimed() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + + let msg = MsgChannelOpenTry { + port_id: mock_addr(SENDER).into_string(), + channel: Channel { + state: ChannelState::TryOpen, + connection_id: 1, + counterparty_channel_id: 0, + counterparty_port_id: vec![1].into(), + version: VERSION.to_owned(), + }, + counterparty_version: VERSION.to_owned(), + proof_init: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenTry(msg), + ) + .expect("channel open try is ok"); + + assert_eq!( + crate::state::CHANNEL_OWNER.load(&deps.storage, 1).unwrap(), + mock_addr(SENDER) + ); +} + +#[test] +fn channel_open_try_commitment_saved() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + + let msg = MsgChannelOpenTry { + port_id: mock_addr(SENDER).into_string(), + channel: Channel { + state: ChannelState::TryOpen, + connection_id: 1, + counterparty_channel_id: 0, + counterparty_port_id: vec![1].into(), + version: VERSION.to_owned(), + }, + counterparty_version: VERSION.to_owned(), + proof_init: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenTry(msg), + ) + .expect("channel open try is ok"); + + assert_eq!( + crate::state::CHANNELS.load(&deps.storage, 1).unwrap(), + Channel { + state: ChannelState::TryOpen, + connection_id: 1, + counterparty_channel_id: 0, + counterparty_port_id: vec![1].into(), + version: VERSION.to_owned(), + } + ); +} + +#[test] +fn channel_open_ack_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is ok"); + + let msg = MsgChannelOpenAck { + channel_id: 1, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg) + ) + .is_ok()) +} + +#[test] +fn channel_open_ack_not_found() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + + let msg = MsgChannelOpenAck { + channel_id: 1, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg) + ) + .is_err_and(|err| { + match err { + ContractError::Std(err) => { + matches!(err, StdError::NotFound { .. }) + } + _ => false, + } + })) +} + +#[test] +fn channel_open_ack_commitment_saved() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is ok"); + + let msg = MsgChannelOpenAck { + channel_id: 1, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + assert_eq!( + crate::state::CHANNELS.load(&deps.storage, 1).unwrap(), + Channel { + state: ChannelState::Open, + connection_id: 1, + counterparty_channel_id: 0, + counterparty_port_id: vec![1].into(), + version: VERSION.to_owned() + } + ); +} + +#[test] +fn channel_open_confirm_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + + let msg = MsgChannelOpenTry { + port_id: mock_addr(SENDER).into_string(), + channel: Channel { + state: ChannelState::TryOpen, + connection_id: 1, + counterparty_channel_id: 0, + counterparty_port_id: vec![1].into(), + version: VERSION.to_owned(), + }, + counterparty_version: VERSION.to_owned(), + proof_init: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenTry(msg), + ) + .expect("channel open try is ok"); + + let msg = MsgChannelOpenConfirm { + channel_id: 1, + proof_ack: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenConfirm(msg), + ) + .is_ok()) +} + +#[test] +fn channel_open_confirm_not_found() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + + let msg = MsgChannelOpenConfirm { + channel_id: 1, + proof_ack: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenConfirm(msg), + ) + .is_err_and(|err| { + match err { + ContractError::Std(err) => { + matches!(err, StdError::NotFound { .. }) + } + _ => false, + } + })) +} + +#[test] +fn channel_open_confirm_commitment_saved() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .unwrap(); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) + .expect("register client ok"); + create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + + connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) + .expect("connection open try is ok"); + connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) + .expect("connection open confirm is ok"); + + let msg = MsgChannelOpenTry { + port_id: mock_addr(SENDER).into_string(), + channel: Channel { + state: ChannelState::TryOpen, + connection_id: 1, + counterparty_channel_id: 0, + counterparty_port_id: vec![1].into(), + version: VERSION.to_owned(), + }, + counterparty_version: VERSION.to_owned(), + proof_init: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenTry(msg), + ) + .expect("channel open try is ok"); + + let msg = MsgChannelOpenConfirm { + channel_id: 1, + proof_ack: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenConfirm(msg), + ) + .expect("channel open confirm is ok"); + + assert_eq!( + crate::state::CHANNELS.load(&deps.storage, 1).unwrap(), + Channel { + state: ChannelState::Open, + connection_id: 1, + counterparty_channel_id: 0, + counterparty_port_id: vec![1].into(), + version: VERSION.to_owned() + } + ); +} diff --git a/cosmwasm/ibc-union/core/src/tests/channel/ibc_packet.rs b/cosmwasm/ibc-union/core/src/tests/channel/ibc_packet.rs new file mode 100644 index 0000000000..e69de29bb2 From 4019c474e2644ed4c308b1e49a7a444be0fc1a98 Mon Sep 17 00:00:00 2001 From: PoisonPhang <17688291+PoisonPhang@users.noreply.github.com> Date: Thu, 9 Jan 2025 20:52:27 -0600 Subject: [PATCH 07/11] chore(union-ibc): helpful helper functions --- cosmwasm/ibc-union/core/src/tests.rs | 71 +++++++---- .../core/src/tests/channel/ibc_channel.rs | 65 ++++------ .../ibc-union/core/src/tests/client/ibc.rs | 31 ++--- .../core/src/tests/connection/ibc.rs | 115 ++++-------------- 4 files changed, 113 insertions(+), 169 deletions(-) diff --git a/cosmwasm/ibc-union/core/src/tests.rs b/cosmwasm/ibc-union/core/src/tests.rs index dcdcbeaa20..179fec075b 100644 --- a/cosmwasm/ibc-union/core/src/tests.rs +++ b/cosmwasm/ibc-union/core/src/tests.rs @@ -6,7 +6,9 @@ use cosmwasm_std::{ }; use union_ibc_msg::{ lightclient::QueryMsg as LightClientQueryMsg, - msg::{ExecuteMsg, MsgCreateClient, MsgRegisterClient}, + msg::{ + ExecuteMsg, MsgConnectionOpenInit, MsgConnectionOpenTry, MsgCreateClient, MsgRegisterClient, + }, }; use super::*; @@ -15,11 +17,16 @@ mod channel; mod client; mod connection; +const CLIENT_TYPE: &str = "union"; +const CLIENT_ADDRESS: &str = "unionclient"; +const SENDER: &str = "unionsender"; +const RELAYER: &str = "unionrelayer"; + /// Creates a mock address from a given string. /// Addresses are prefixed with the default [`MockApi`] prefix. -fn mock_addr(sender: impl Into) -> Addr { +fn mock_addr(address_seed: impl Into) -> Addr { let mock_api = MockApi::default(); - mock_api.addr_make(&Into::::into(sender)) + mock_api.addr_make(&Into::::into(address_seed)) } fn wasm_query_handler StdResult + 'static>( @@ -38,38 +45,60 @@ fn wasm_query_handler StdResult + 'static> /// Creates a mock client. /// Uses [`mock_addr`] to convert address seeds to addresses /// Addresses are prefixed with the default [`MockApi`] prefix. -fn register_client( - deps: DepsMut, - client_type: impl Into, - client_address_seed: impl Into, - sender_address_seed: impl Into, -) -> Result { +fn register_client(deps: DepsMut) -> Result { let register_msg = ExecuteMsg::RegisterClient(MsgRegisterClient { - client_type: client_type.into(), - client_address: mock_addr(client_address_seed).into_string(), + client_type: CLIENT_TYPE.to_owned(), + client_address: mock_addr(CLIENT_ADDRESS).into_string(), }); - let sender = mock_addr(sender_address_seed); + let sender = mock_addr(SENDER); execute(deps, mock_env(), message_info(&sender, &[]), register_msg) } -fn create_client( - deps: DepsMut, - client_type: impl Into + Clone, - sender_address_seed: impl Into + Clone, - relayer_address_seed: impl Into + Clone, -) -> Result { +fn create_client(deps: DepsMut) -> Result { let execute_msg = ExecuteMsg::CreateClient(MsgCreateClient { - client_type: client_type.into(), + client_type: CLIENT_TYPE.to_owned(), client_state_bytes: vec![1, 2, 3].into(), consensus_state_bytes: vec![1, 2, 3].into(), - relayer: mock_addr(relayer_address_seed).into_string(), + relayer: mock_addr(RELAYER).into_string(), }); - let sender = mock_addr(sender_address_seed); + let sender = mock_addr(SENDER); execute(deps, mock_env(), message_info(&sender, &[]), execute_msg) } +fn connection_open_init(deps: DepsMut) -> Result { + let msg = MsgConnectionOpenInit { + client_id: 1, + counterparty_client_id: 2, + relayer: mock_addr(RELAYER).into_string(), + }; + execute( + deps, + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ConnectionOpenInit(msg), + ) +} + +fn connection_open_try(deps: DepsMut) -> Result { + let msg = MsgConnectionOpenTry { + counterparty_client_id: 2, + counterparty_connection_id: 1, + client_id: 1, + proof_init: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + execute( + deps, + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ConnectionOpenTry(msg), + ) +} + #[test] fn display() { assert_eq!( diff --git a/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs b/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs index 7e87f6ef97..6c7c7a52f8 100644 --- a/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs +++ b/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs @@ -92,9 +92,8 @@ fn channel_open_init_ok() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); @@ -133,9 +132,8 @@ fn channel_open_init_channel_claimed() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); @@ -165,9 +163,8 @@ fn channel_open_init_commitment_saved() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); @@ -203,9 +200,8 @@ fn channel_open_try_ok() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); @@ -251,9 +247,8 @@ fn channel_open_try_invalid_state() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); @@ -308,9 +303,8 @@ fn channel_open_try_channel_claimed() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); @@ -361,9 +355,8 @@ fn channel_open_try_commitment_saved() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); @@ -420,9 +413,8 @@ fn channel_open_ack_ok() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); @@ -478,9 +470,8 @@ fn channel_open_ack_not_found() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); @@ -528,9 +519,8 @@ fn channel_open_ack_commitment_saved() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); @@ -597,9 +587,8 @@ fn channel_open_confirm_ok() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); @@ -659,9 +648,8 @@ fn channel_open_confirm_not_found() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); @@ -706,9 +694,8 @@ fn channel_open_confirm_commitment_saved() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) .expect("connection open try is ok"); diff --git a/cosmwasm/ibc-union/core/src/tests/client/ibc.rs b/cosmwasm/ibc-union/core/src/tests/client/ibc.rs index 6b017f5fa0..44785a7153 100644 --- a/cosmwasm/ibc-union/core/src/tests/client/ibc.rs +++ b/cosmwasm/ibc-union/core/src/tests/client/ibc.rs @@ -34,7 +34,7 @@ fn new_client_created_event(client_type: &str, client_id: u32) -> Event { #[test] fn register_client_ok() { let mut deps = mock_dependencies(); - let res = register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER).unwrap(); + let res = register_client(deps.as_mut()).unwrap(); assert!(res .events @@ -53,9 +53,9 @@ fn register_client_ok() { #[test] fn register_client_fails_when_duplicate() { let mut deps = mock_dependencies(); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER).unwrap(); + register_client(deps.as_mut()).unwrap(); assert_eq!( - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER), + register_client(deps.as_mut()), Err(ContractError::ClientTypeAlreadyExists) ); } @@ -78,9 +78,8 @@ fn create_client_ok() { msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - assert!(create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).is_ok()) + register_client(deps.as_mut()).expect("register client ok"); + assert!(create_client(deps.as_mut()).is_ok()) } #[test] @@ -101,9 +100,8 @@ fn create_client_commitments_saved() { msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - let res = create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + let res = create_client(deps.as_mut()).expect("create client ok"); let client_id: u32 = res .events .iter() @@ -174,9 +172,8 @@ fn update_client_ok() { msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - let res = create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + let res = create_client(deps.as_mut()).expect("create client ok"); let client_id: u32 = res .events .iter() @@ -223,9 +220,8 @@ fn update_client_ko() { msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - let res = create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + let res = create_client(deps.as_mut()).expect("create client ok"); let client_id: u32 = res .events .iter() @@ -278,9 +274,8 @@ fn update_client_commitments_saved() { msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - let res = create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + let res = create_client(deps.as_mut()).expect("create client ok"); let client_id: u32 = res .events .iter() diff --git a/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs b/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs index 350d032f2c..fbca63a93f 100644 --- a/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs +++ b/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs @@ -1,66 +1,13 @@ -use std::marker::PhantomData; - -use contract::{events::attribute::CLIENT_ID, instantiate}; +use contract::instantiate; use cosmwasm_std::{testing::mock_dependencies, to_json_binary}; use ibc_solidity::Connection; use union_ibc_msg::msg::{ ExecuteMsg, InitMsg, MsgConnectionOpenAck, MsgConnectionOpenConfirm, MsgConnectionOpenInit, - MsgConnectionOpenTry, MsgRegisterClient, + MsgConnectionOpenTry, }; -use unionlabs::ibc::core::client::height::Height; use super::*; -const CLIENT_TYPE: &str = "union"; -const CLIENT_ADDRESS: &str = "unionclient"; -const SENDER: &str = "unionsender"; -const RELAYER: &str = "unionrelayer"; - -fn connection_open_init( - deps: DepsMut, - client_id: impl Into, - counterparty_client_id: impl Into, - sender_address_seed: impl Into, - relayer_address_seed: impl Into, -) -> Result { - let msg = MsgConnectionOpenInit { - client_id: client_id.into(), - counterparty_client_id: counterparty_client_id.into(), - relayer: mock_addr(relayer_address_seed).into_string(), - }; - execute( - deps, - mock_env(), - message_info(&mock_addr(sender_address_seed), &[]), - ExecuteMsg::ConnectionOpenInit(msg), - ) -} - -fn connection_open_try( - deps: DepsMut, - counterparty_client_id: impl Into, - counterparty_connection_id: impl Into, - client_id: impl Into, - proof_height: impl Into, - relayer_address_seed: impl Into, -) -> Result { - let msg = MsgConnectionOpenTry { - counterparty_client_id: counterparty_client_id.into(), - counterparty_connection_id: counterparty_connection_id.into(), - client_id: client_id.into(), - proof_init: vec![1, 2, 3].into(), - proof_height: proof_height.into(), - relayer: mock_addr(relayer_address_seed).into_string(), - }; - - execute( - deps, - mock_env(), - message_info(&mock_addr(SENDER), &[]), - ExecuteMsg::ConnectionOpenTry(msg), - ) -} - #[test] fn connection_open_init_ok() { let mut deps = mock_dependencies(); @@ -76,9 +23,8 @@ fn connection_open_init_ok() { LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); let msg = MsgConnectionOpenInit { client_id: 1, @@ -109,11 +55,9 @@ fn connection_open_init_commitment_saved() { LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); - connection_open_init(deps.as_mut(), 1_u32, 2_u32, SENDER, RELAYER) - .expect("open connection init is ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + connection_open_init(deps.as_mut()).expect("open connection init is ok"); assert_eq!( crate::state::CONNECTIONS.load(&deps.storage, 1).unwrap(), @@ -142,9 +86,8 @@ fn connection_open_try_ok() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); let msg = MsgConnectionOpenTry { counterparty_client_id: 2, @@ -180,8 +123,7 @@ fn connection_open_try_client_not_found() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); + register_client(deps.as_mut()).expect("register client ok"); let msg = MsgConnectionOpenTry { counterparty_client_id: 2, @@ -227,9 +169,8 @@ fn connection_open_try_commitment_saved() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); let msg = MsgConnectionOpenTry { counterparty_client_id: 2, @@ -275,11 +216,9 @@ fn connection_open_ack_ok() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); - connection_open_init(deps.as_mut(), 1_u32, 2_u32, SENDER, RELAYER) - .expect("connection open init is ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + connection_open_init(deps.as_mut()).expect("connection open init is ok"); let msg = MsgConnectionOpenAck { connection_id: 1, @@ -319,11 +258,9 @@ fn connection_open_ack_commitment_saved() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); - connection_open_init(deps.as_mut(), 1_u32, 2_u32, SENDER, RELAYER) - .expect("connection open init is ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + connection_open_init(deps.as_mut()).expect("connection open init is ok"); let msg = MsgConnectionOpenAck { connection_id: 1, @@ -368,11 +305,9 @@ fn connection_open_confirm_ok() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER) - .expect("connection open try is ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); let msg = MsgConnectionOpenConfirm { connection_id: 1, @@ -411,11 +346,9 @@ fn connection_open_try_confirm_commitment_saved() { LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); - register_client(deps.as_mut(), CLIENT_TYPE, CLIENT_ADDRESS, SENDER) - .expect("register client ok"); - create_client(deps.as_mut(), CLIENT_TYPE, SENDER, RELAYER).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER) - .expect("connection open try is ok"); + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); let msg = MsgConnectionOpenConfirm { connection_id: 1, From 7aa3fd32018d9db042cd14d59aa355c485dc06b1 Mon Sep 17 00:00:00 2001 From: PoisonPhang <17688291+PoisonPhang@users.noreply.github.com> Date: Fri, 10 Jan 2025 17:18:58 -0600 Subject: [PATCH 08/11] feat(ibc-union): 04-channel send packet tests --- cosmwasm/ibc-union/core/src/tests.rs | 54 +++++- .../core/src/tests/channel/ibc_channel.rs | 143 +++----------- .../core/src/tests/connection/ibc.rs | 19 +- .../core/src/tests/channel/ibc_packet.rs | 178 ++++++++++++++++++ 4 files changed, 260 insertions(+), 134 deletions(-) create mode 100644 cosmwasm/union-ibc/core/src/tests/channel/ibc_packet.rs diff --git a/cosmwasm/ibc-union/core/src/tests.rs b/cosmwasm/ibc-union/core/src/tests.rs index 179fec075b..6da3b895c3 100644 --- a/cosmwasm/ibc-union/core/src/tests.rs +++ b/cosmwasm/ibc-union/core/src/tests.rs @@ -7,7 +7,8 @@ use cosmwasm_std::{ use union_ibc_msg::{ lightclient::QueryMsg as LightClientQueryMsg, msg::{ - ExecuteMsg, MsgConnectionOpenInit, MsgConnectionOpenTry, MsgCreateClient, MsgRegisterClient, + ExecuteMsg, MsgChannelOpenAck, MsgChannelOpenInit, MsgConnectionOpenConfirm, + MsgConnectionOpenInit, MsgConnectionOpenTry, MsgCreateClient, MsgRegisterClient, }, }; @@ -21,6 +22,7 @@ const CLIENT_TYPE: &str = "union"; const CLIENT_ADDRESS: &str = "unionclient"; const SENDER: &str = "unionsender"; const RELAYER: &str = "unionrelayer"; +const VERSION: &str = "version"; /// Creates a mock address from a given string. /// Addresses are prefixed with the default [`MockApi`] prefix. @@ -99,6 +101,56 @@ fn connection_open_try(deps: DepsMut) -> Result { ) } +fn connection_open_confirm(deps: DepsMut) -> Result { + let msg = MsgConnectionOpenConfirm { + connection_id: 1, + proof_ack: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + execute( + deps, + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ConnectionOpenConfirm(msg), + ) +} + +fn channel_open_init(deps: DepsMut) -> Result { + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps, + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) +} + +fn channel_open_ack(deps: DepsMut) -> Result { + let msg = MsgChannelOpenAck { + channel_id: 1, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + + execute( + deps, + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) +} + #[test] fn display() { assert_eq!( diff --git a/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs b/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs index 6c7c7a52f8..914744f203 100644 --- a/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs +++ b/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs @@ -3,79 +3,14 @@ use cosmwasm_std::{testing::mock_dependencies, to_json_binary}; use ibc_solidity::Channel; use union_ibc_msg::msg::{ InitMsg, MsgChannelOpenAck, MsgChannelOpenConfirm, MsgChannelOpenInit, MsgChannelOpenTry, - MsgConnectionOpenConfirm, MsgConnectionOpenTry, }; use super::*; -const CLIENT_TYPE: &str = "union"; -const CLIENT_ADDRESS: &str = "unionclient"; const SENDER: &str = "unionsender"; const RELAYER: &str = "unionrelayer"; const VERSION: &str = "version"; -fn connection_open_try( - deps: DepsMut, - counterparty_client_id: impl Into, - counterparty_connection_id: impl Into, - client_id: impl Into, - proof_height: impl Into, - relayer_address_seed: impl Into, - sender_address_seed: impl Into, -) -> Result { - let msg = MsgConnectionOpenTry { - counterparty_client_id: counterparty_client_id.into(), - counterparty_connection_id: counterparty_connection_id.into(), - client_id: client_id.into(), - proof_init: vec![1, 2, 3].into(), - proof_height: proof_height.into(), - relayer: mock_addr(relayer_address_seed).into_string(), - }; - execute( - deps, - mock_env(), - message_info(&mock_addr(sender_address_seed), &[]), - ExecuteMsg::ConnectionOpenTry(msg), - ) -} - -fn connection_open_confirm( - deps: DepsMut, - connection_id: impl Into, - proof_height: impl Into, - relayer_address_seed: impl Into, - sender_address_seed: impl Into, -) -> Result { - let msg = MsgConnectionOpenConfirm { - connection_id: connection_id.into(), - proof_ack: vec![1, 2, 3].into(), - proof_height: proof_height.into(), - relayer: mock_addr(relayer_address_seed).into_string(), - }; - execute( - deps, - mock_env(), - message_info(&mock_addr(sender_address_seed), &[]), - ExecuteMsg::ConnectionOpenConfirm(msg), - ) -} - -fn channel_open_init(deps: DepsMut) -> Result { - let msg = MsgChannelOpenInit { - port_id: mock_addr(SENDER).to_string(), - counterparty_port_id: vec![1].into(), - connection_id: 1, - version: VERSION.to_owned(), - relayer: mock_addr(RELAYER).to_string(), - }; - execute( - deps, - mock_env(), - message_info(&mock_addr(SENDER), &[]), - ExecuteMsg::ChannelOpenInit(msg), - ) -} - #[test] fn channel_open_init_ok() { let mut deps = mock_dependencies(); @@ -95,10 +30,8 @@ fn channel_open_init_ok() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); let msg = MsgChannelOpenInit { port_id: mock_addr(SENDER).to_string(), @@ -135,10 +68,8 @@ fn channel_open_init_channel_claimed() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); channel_open_init(deps.as_mut()).expect("channel open init is ok"); assert_eq!( @@ -166,10 +97,8 @@ fn channel_open_init_commitment_saved() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); channel_open_init(deps.as_mut()).expect("channel open init is ok"); assert_eq!( @@ -203,10 +132,8 @@ fn channel_open_try_ok() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); let msg = MsgChannelOpenTry { port_id: mock_addr(SENDER).into_string(), @@ -250,10 +177,8 @@ fn channel_open_try_invalid_state() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); let msg = MsgChannelOpenTry { port_id: mock_addr(SENDER).into_string(), @@ -306,10 +231,8 @@ fn channel_open_try_channel_claimed() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); let msg = MsgChannelOpenTry { port_id: mock_addr(SENDER).into_string(), @@ -358,10 +281,8 @@ fn channel_open_try_commitment_saved() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); let msg = MsgChannelOpenTry { port_id: mock_addr(SENDER).into_string(), @@ -416,10 +337,8 @@ fn channel_open_ack_ok() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); let msg = MsgChannelOpenInit { port_id: mock_addr(SENDER).to_string(), @@ -473,10 +392,8 @@ fn channel_open_ack_not_found() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); let msg = MsgChannelOpenAck { channel_id: 1, @@ -522,10 +439,8 @@ fn channel_open_ack_commitment_saved() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); let msg = MsgChannelOpenInit { port_id: mock_addr(SENDER).to_string(), @@ -590,10 +505,8 @@ fn channel_open_confirm_ok() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); let msg = MsgChannelOpenTry { port_id: mock_addr(SENDER).into_string(), @@ -651,10 +564,8 @@ fn channel_open_confirm_not_found() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); let msg = MsgChannelOpenConfirm { channel_id: 1, @@ -697,10 +608,8 @@ fn channel_open_confirm_commitment_saved() { register_client(deps.as_mut()).expect("register client ok"); create_client(deps.as_mut()).expect("create client ok"); - connection_open_try(deps.as_mut(), 2_u32, 1_u32, 1_u32, 1_u64, RELAYER, SENDER) - .expect("connection open try is ok"); - connection_open_confirm(deps.as_mut(), 1_u32, 1_u32, RELAYER, SENDER) - .expect("connection open confirm is ok"); + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); let msg = MsgChannelOpenTry { port_id: mock_addr(SENDER).into_string(), diff --git a/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs b/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs index fbca63a93f..7f577a0fe1 100644 --- a/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs +++ b/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs @@ -316,12 +316,12 @@ fn connection_open_confirm_ok() { relayer: mock_addr(RELAYER).into_string(), }; - assert!(dbg!(execute( + assert!(execute( deps.as_mut(), mock_env(), message_info(&mock_addr(SENDER), &[]), ExecuteMsg::ConnectionOpenConfirm(msg), - )) + ) .is_ok()); } @@ -350,20 +350,7 @@ fn connection_open_try_confirm_commitment_saved() { create_client(deps.as_mut()).expect("create client ok"); connection_open_try(deps.as_mut()).expect("connection open try is ok"); - let msg = MsgConnectionOpenConfirm { - connection_id: 1, - proof_ack: vec![1, 2, 3].into(), - proof_height: 1, - relayer: mock_addr(RELAYER).into_string(), - }; - - execute( - deps.as_mut(), - mock_env(), - message_info(&mock_addr(SENDER), &[]), - ExecuteMsg::ConnectionOpenConfirm(msg), - ) - .expect("connection_open_confirm is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); assert_eq!( crate::state::CONNECTIONS.load(&deps.storage, 1).unwrap(), diff --git a/cosmwasm/union-ibc/core/src/tests/channel/ibc_packet.rs b/cosmwasm/union-ibc/core/src/tests/channel/ibc_packet.rs new file mode 100644 index 0000000000..062311039f --- /dev/null +++ b/cosmwasm/union-ibc/core/src/tests/channel/ibc_packet.rs @@ -0,0 +1,178 @@ +use contract::instantiate; +use cosmwasm_std::{testing::mock_dependencies, to_json_binary}; +use union_ibc_msg::msg::{InitMsg, MsgSendPacket}; + +use super::*; + +#[test] +fn send_packet_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgSendPacket { + source_channel: 1, + timeout_height: 10, + timeout_timestamp: 1000000, + data: vec![0, 1, 2].into(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketSend(msg) + ) + .is_ok()) +} + +#[test] +fn send_packet_missing_timeout() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgSendPacket { + source_channel: 1, + timeout_height: 0, + timeout_timestamp: 0, + data: vec![0, 1, 2].into(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketSend(msg), + ) + .is_err_and(|err| { matches!(err, ContractError::TimeoutMustBeSet) })) +} + +#[test] +fn send_packet_channel_does_not_exist() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgSendPacket { + source_channel: 3, + timeout_height: 10, + timeout_timestamp: 1000000, + data: vec![0, 1, 2].into(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketSend(msg), + ) + .is_err_and(|err| { + match err { + ContractError::Std(err) => matches!(err, StdError::NotFound { .. }), + _ => false, + } + })) +} + +#[test] +fn send_packet_module_is_not_channel_owner() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgSendPacket { + source_channel: 1, + timeout_height: 10, + timeout_timestamp: 1000000, + data: vec![0, 1, 2].into(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr("not module"), &[]), + ExecuteMsg::PacketSend(msg), + ) + .is_err_and(|err| { matches!(err, ContractError::Unauthorized { .. }) })) +} From 0645fad2674cce051b642dee7beb8cbc2d52e6a0 Mon Sep 17 00:00:00 2001 From: PoisonPhang <17688291+PoisonPhang@users.noreply.github.com> Date: Tue, 14 Jan 2025 21:16:05 -0600 Subject: [PATCH 09/11] feat(ibc-union): 04-packet tests --- .../core/src/tests/channel/ibc_packet.rs | 1892 ++++++++++++++++- 1 file changed, 1891 insertions(+), 1 deletion(-) diff --git a/cosmwasm/union-ibc/core/src/tests/channel/ibc_packet.rs b/cosmwasm/union-ibc/core/src/tests/channel/ibc_packet.rs index 062311039f..d83e4b810c 100644 --- a/cosmwasm/union-ibc/core/src/tests/channel/ibc_packet.rs +++ b/cosmwasm/union-ibc/core/src/tests/channel/ibc_packet.rs @@ -1,6 +1,11 @@ use contract::instantiate; use cosmwasm_std::{testing::mock_dependencies, to_json_binary}; -use union_ibc_msg::msg::{InitMsg, MsgSendPacket}; +use ibc_solidity::Packet; +use ibc_union_spec::COMMITMENT_MAGIC; +use union_ibc_msg::msg::{ + InitMsg, MsgBatchAcks, MsgBatchSend, MsgIntentPacketRecv, MsgPacketAcknowledgement, + MsgPacketRecv, MsgPacketTimeout, MsgSendPacket, MsgWriteAcknowledgement, +}; use super::*; @@ -176,3 +181,1888 @@ fn send_packet_module_is_not_channel_owner() { ) .is_err_and(|err| { matches!(err, ContractError::Unauthorized { .. }) })) } + +#[test] +fn recv_packet_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgPacketRecv { + packets: vec![Packet { + source_channel: 2, + destination_channel: 1, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }], + relayer_msgs: vec![vec![1].into()], + relayer: mock_addr(RELAYER).to_string(), + proof: vec![1, 2, 3].into(), + proof_height: 1, + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketRecv(msg), + ) + .is_ok()) +} + +#[test] +fn recv_packet_invalid_channel_state() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgPacketRecv { + packets: vec![Packet { + source_channel: 2, + destination_channel: 5, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }], + relayer_msgs: vec![vec![1].into()], + relayer: mock_addr(RELAYER).to_string(), + proof: vec![1, 2, 3].into(), + proof_height: 1, + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketRecv(msg), + ) + .is_err_and(|err| { + match err { + ContractError::Std(err) => { + matches!(err, StdError::NotFound { .. }) + } + _ => false, + } + })) +} + +#[test] +fn recv_packet_timeout_timestamp() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgPacketRecv { + packets: vec![Packet { + source_channel: 2, + destination_channel: 1, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 100, + }], + relayer_msgs: vec![vec![1].into()], + relayer: mock_addr(RELAYER).to_string(), + proof: vec![1, 2, 3].into(), + proof_height: 1, + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketRecv(msg), + ) + .is_err_and(|err| { matches!(err, ContractError::ReceivedTimedOutPacketTimestamp { .. }) })) +} + +#[test] +fn recv_packet_timeout_height() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgPacketRecv { + packets: vec![Packet { + source_channel: 2, + destination_channel: 1, + data: vec![1, 2, 3].into(), + timeout_height: 1, + timeout_timestamp: 2000000000000000000, + }], + relayer_msgs: vec![vec![1].into()], + relayer: mock_addr(RELAYER).to_string(), + proof: vec![1, 2, 3].into(), + proof_height: 1, + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketRecv(msg), + ) + .is_err_and(|err| { matches!(err, ContractError::ReceivedTimedOutPacketHeight { .. }) })) +} + +#[test] +fn recv_intent_packet_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgIntentPacketRecv { + packets: vec![Packet { + source_channel: 2, + destination_channel: 1, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }], + market_maker_msgs: vec![vec![1, 2, 3].into()], + market_maker: mock_addr("marketmaker").into_string(), + empty_proof: vec![].into(), + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::IntentPacketRecv(msg) + ) + .is_ok()) +} + +#[test] +fn recv_intent_packet_timeout_timestamp() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgIntentPacketRecv { + packets: vec![Packet { + source_channel: 2, + destination_channel: 1, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 100, + }], + market_maker_msgs: vec![vec![1, 2, 3].into()], + market_maker: mock_addr("marketmaker").into_string(), + empty_proof: vec![].into(), + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::IntentPacketRecv(msg), + ) + .is_err_and(|err| { matches!(err, ContractError::ReceivedTimedOutPacketTimestamp { .. }) })) +} + +#[test] +fn recv_intent_packet_timeout_height() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgIntentPacketRecv { + packets: vec![Packet { + source_channel: 2, + destination_channel: 1, + data: vec![1, 2, 3].into(), + timeout_height: 1, + timeout_timestamp: 2000000000000000000, + }], + market_maker_msgs: vec![vec![1, 2, 3].into()], + market_maker: mock_addr("marketmaker").into_string(), + empty_proof: vec![].into(), + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::IntentPacketRecv(msg), + ) + .is_err_and(|err| { matches!(err, ContractError::ReceivedTimedOutPacketHeight { .. }) })) +} + +#[test] +fn acknowledge_packet_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is okay"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + let msg = MsgChannelOpenAck { + channel_id: 2, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + let msg = MsgSendPacket { + source_channel: 1, + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + data: vec![1, 2, 3].into(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketSend(msg), + ) + .expect("send packet ok"); + + let msg = MsgPacketAcknowledgement { + packets: vec![Packet { + source_channel: 1, + destination_channel: 0, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }], + acknowledgements: vec![vec![1, 2, 3].into()], + proof: vec![1].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketAck(msg) + ) + .is_ok()) +} + +#[test] +fn acknowledge_packet_tampered() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is okay"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + let msg = MsgChannelOpenAck { + channel_id: 2, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + let msg = MsgSendPacket { + source_channel: 1, + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + data: vec![1, 2, 3].into(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketSend(msg), + ) + .expect("send packet ok"); + + let msg = MsgPacketAcknowledgement { + packets: vec![Packet { + source_channel: 1, + destination_channel: 0, + data: vec![4, 1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }], + acknowledgements: vec![vec![1, 2, 3].into()], + proof: vec![1].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketAck(msg) + ) + .is_err_and(|err| matches!(err, ContractError::PacketCommitmentNotFound))) +} + +#[test] +fn acknowledge_packet_not_sent() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is okay"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + let msg = MsgChannelOpenAck { + channel_id: 2, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + let msg = MsgPacketAcknowledgement { + packets: vec![Packet { + source_channel: 1, + destination_channel: 0, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }], + acknowledgements: vec![vec![1, 2, 3].into()], + proof: vec![1].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).into_string(), + }; + + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketAck(msg) + ) + .is_err_and(|err| matches!(err, ContractError::PacketCommitmentNotFound))) +} + +#[test] +fn timeout_packet_timestamp_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgSendPacket { + source_channel: 1, + timeout_height: 10, + timeout_timestamp: 100, + data: vec![1, 2, 3].into(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketSend(msg), + ) + .expect("send packet ok"); + + let msg = MsgPacketTimeout { + packet: Packet { + source_channel: 1, + destination_channel: 0, + data: vec![1, 2, 3].into(), + timeout_height: 10, + timeout_timestamp: 100, + }, + proof: vec![1].into(), + proof_height: 11, + relayer: mock_addr(RELAYER).into_string(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketTimeout(msg) + ) + .is_ok()) +} + +#[test] +fn timeout_packet_timestamp_timestamp_not_reached() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgSendPacket { + source_channel: 1, + timeout_height: 10, + timeout_timestamp: 200000, + data: vec![1, 2, 3].into(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketSend(msg), + ) + .expect("send packet ok"); + + let msg = MsgPacketTimeout { + packet: Packet { + source_channel: 1, + destination_channel: 0, + data: vec![1, 2, 3].into(), + timeout_height: 10, + timeout_timestamp: 200000, + }, + proof: vec![1].into(), + proof_height: 11, + relayer: mock_addr(RELAYER).into_string(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketTimeout(msg) + ) + .is_err_and(|err| { matches!(err, ContractError::TimeoutTimestampNotReached) })) +} + +#[test] +fn timeout_packet_height_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgSendPacket { + source_channel: 1, + timeout_height: 10, + timeout_timestamp: 0, + data: vec![1, 2, 3].into(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketSend(msg), + ) + .expect("send packet ok"); + + let msg = MsgPacketTimeout { + packet: Packet { + source_channel: 1, + destination_channel: 0, + data: vec![1, 2, 3].into(), + timeout_height: 10, + timeout_timestamp: 0, + }, + proof: vec![1].into(), + proof_height: 11, + relayer: mock_addr(RELAYER).into_string(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketTimeout(msg) + ) + .is_ok()) +} + +#[test] +fn timeout_packet_height_not_reached() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + + let msg = MsgSendPacket { + source_channel: 1, + timeout_height: 10, + timeout_timestamp: 0, + data: vec![1, 2, 3].into(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketSend(msg), + ) + .expect("send packet ok"); + + let msg = MsgPacketTimeout { + packet: Packet { + source_channel: 1, + destination_channel: 0, + data: vec![1, 2, 3].into(), + timeout_height: 10, + timeout_timestamp: 0, + }, + proof: vec![1].into(), + proof_height: 9, + relayer: mock_addr(RELAYER).into_string(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketTimeout(msg) + ) + .is_err_and(|err| { matches!(err, ContractError::TimeoutHeightNotReached) })) +} + +#[test] +fn write_acknowledgement_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is okay"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + let msg = MsgChannelOpenAck { + channel_id: 2, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + let msg = MsgPacketRecv { + packets: vec![Packet { + source_channel: 1, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }], + relayer_msgs: vec![vec![1].into()], + relayer: mock_addr(RELAYER).to_string(), + proof: vec![1, 2, 3].into(), + proof_height: 1, + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketRecv(msg), + ) + .expect("recv packet ok"); + + let msg = MsgWriteAcknowledgement { + channel_id: 2, + packet: Packet { + source_channel: 1, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }, + acknowledgement: vec![1].into(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::WriteAcknowledgement(msg), + ) + .is_ok()) +} + +#[test] +fn write_acknowledgement_module_is_not_channel_owner() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + let msg = MsgChannelOpenInit { + port_id: mock_addr("malicious").to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr("malicious"), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is okay"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + let msg = MsgChannelOpenAck { + channel_id: 2, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr("malicious"), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + let msg = MsgPacketRecv { + packets: vec![Packet { + source_channel: 1, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }], + relayer_msgs: vec![vec![1].into()], + relayer: mock_addr(RELAYER).to_string(), + proof: vec![1, 2, 3].into(), + proof_height: 1, + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketRecv(msg), + ) + .expect("recv packet ok"); + + let msg = MsgWriteAcknowledgement { + channel_id: 2, + packet: Packet { + source_channel: 1, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }, + acknowledgement: vec![1].into(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::WriteAcknowledgement(msg), + ) + .is_err_and(|err| { matches!(err, ContractError::Unauthorized { .. }) })) +} + +#[test] +fn write_acknowledgement_packet_not_received() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is okay"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + let msg = MsgChannelOpenAck { + channel_id: 2, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + let msg = MsgWriteAcknowledgement { + channel_id: 2, + packet: Packet { + source_channel: 1, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }, + acknowledgement: vec![1].into(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::WriteAcknowledgement(msg), + ) + .is_err_and(|err| { matches!(err, ContractError::PacketNotReceived) })) +} + +#[test] +fn write_acknowledgement_already_exists() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is okay"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + let msg = MsgChannelOpenAck { + channel_id: 2, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + let msg = MsgPacketRecv { + packets: vec![Packet { + source_channel: 1, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }], + relayer_msgs: vec![vec![1].into()], + relayer: mock_addr(RELAYER).to_string(), + proof: vec![1, 2, 3].into(), + proof_height: 1, + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketRecv(msg), + ) + .expect("recv packet ok"); + + let msg = MsgWriteAcknowledgement { + channel_id: 2, + packet: Packet { + source_channel: 1, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }, + acknowledgement: vec![1].into(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::WriteAcknowledgement(msg), + ) + .is_ok()); + let msg = MsgWriteAcknowledgement { + channel_id: 2, + packet: Packet { + source_channel: 1, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 2000000000000000000, + }, + acknowledgement: vec![1].into(), + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::WriteAcknowledgement(msg), + ) + .is_err_and(|err| { matches!(err, ContractError::AlreadyAcknowledged) })) +} + +#[test] +fn batch_send_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is okay"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + let msg = MsgChannelOpenAck { + channel_id: 2, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + let msg = MsgSendPacket { + source_channel: 2, + timeout_height: 10, + timeout_timestamp: 0, + data: vec![1, 2, 3].into(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketSend(msg), + ) + .expect("send packet is ok"); + let msg = MsgSendPacket { + source_channel: 2, + timeout_height: 10, + timeout_timestamp: 0, + data: vec![4, 5, 6].into(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketSend(msg), + ) + .expect("send packet is ok"); + + let msg = MsgBatchSend { + source_channel: 2, + packets: vec![ + Packet { + source_channel: 2, + destination_channel: 0, + data: vec![4, 5, 6].into(), + timeout_height: 10, + timeout_timestamp: 0, + }, + Packet { + source_channel: 2, + destination_channel: 0, + data: vec![1, 2, 3].into(), + timeout_height: 10, + timeout_timestamp: 0, + }, + ], + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::BatchSend(msg) + ) + .is_ok()) +} + +#[test] +fn batch_send_packet_not_sent() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is okay"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + let msg = MsgChannelOpenAck { + channel_id: 2, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + let msg = MsgBatchSend { + source_channel: 2, + packets: vec![ + Packet { + source_channel: 2, + destination_channel: 0, + data: vec![4, 5, 6].into(), + timeout_height: 10, + timeout_timestamp: 0, + }, + Packet { + source_channel: 2, + destination_channel: 0, + data: vec![1, 2, 3].into(), + timeout_height: 10, + timeout_timestamp: 0, + }, + ], + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::BatchSend(msg) + ) + .is_err_and(|err| { matches!(err, ContractError::PacketCommitmentNotFound) })) +} + +#[test] +fn batch_acks_ok() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is okay"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + let msg = MsgChannelOpenAck { + channel_id: 2, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + let msg = MsgPacketRecv { + packets: vec![Packet { + source_channel: 0, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }], + relayer_msgs: vec![vec![1].into()], + relayer: mock_addr(RELAYER).to_string(), + proof: vec![1, 2, 3].into(), + proof_height: 1, + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketRecv(msg), + ) + .expect("recv packet ok"); + let msg = MsgWriteAcknowledgement { + channel_id: 2, + packet: Packet { + source_channel: 0, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }, + acknowledgement: vec![1].into(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::WriteAcknowledgement(msg), + ) + .expect("write ack is ok"); + let msg = MsgPacketRecv { + packets: vec![Packet { + source_channel: 0, + destination_channel: 2, + data: vec![3, 4, 5].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }], + relayer_msgs: vec![vec![1].into()], + relayer: mock_addr(RELAYER).to_string(), + proof: vec![1, 2, 3].into(), + proof_height: 1, + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketRecv(msg), + ) + .expect("recv packet ok"); + let msg = MsgWriteAcknowledgement { + channel_id: 2, + packet: Packet { + source_channel: 0, + destination_channel: 2, + data: vec![3, 4, 5].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }, + acknowledgement: vec![1].into(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::WriteAcknowledgement(msg), + ) + .expect("write ack is ok"); + + let msg = MsgBatchAcks { + source_channel: 2, + packets: vec![ + Packet { + source_channel: 0, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }, + Packet { + source_channel: 0, + destination_channel: 2, + data: vec![3, 4, 5].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }, + ], + acks: vec![vec![1].into(), vec![1].into()], + }; + assert!(dbg!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::BatchAcks(msg) + )) + .is_ok()) +} + +#[test] +fn batch_acks_packet_not_received() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is okay"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + let msg = MsgChannelOpenAck { + channel_id: 2, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + let msg = MsgBatchAcks { + source_channel: 2, + packets: vec![ + Packet { + source_channel: 0, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }, + Packet { + source_channel: 0, + destination_channel: 2, + data: vec![3, 4, 5].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }, + ], + acks: vec![vec![1].into(), vec![1].into()], + }; + assert!(dbg!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::BatchAcks(msg) + )) + .is_err_and(|err| { matches!(err, ContractError::PacketCommitmentNotFound) })) +} + +#[test] +fn batch_acks_tampered_packet() { + let mut deps = mock_dependencies(); + instantiate( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + InitMsg {}, + ) + .expect("instantiate is ok"); + deps.querier + .update_wasm(wasm_query_handler(|msg| match msg { + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::VerifyNonMembership { .. } => to_json_binary(&()), + LightClientQueryMsg::GetTimestamp { .. } => to_json_binary(&100000), + msg => panic!("should not be called: {:?}", msg), + })); + + // Create client + register_client(deps.as_mut()).expect("register client ok"); + create_client(deps.as_mut()).expect("create client ok"); + // Create connection + connection_open_try(deps.as_mut()).expect("connection open try is ok"); + connection_open_confirm(deps.as_mut()).expect("connection open confirm is ok"); + // Create channel + channel_open_init(deps.as_mut()).expect("channel open init is ok"); + let msg = MsgChannelOpenInit { + port_id: mock_addr(SENDER).to_string(), + counterparty_port_id: vec![1].into(), + connection_id: 1, + version: VERSION.to_owned(), + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenInit(msg), + ) + .expect("channel open init is okay"); + channel_open_ack(deps.as_mut()).expect("channel open ack is ok"); + let msg = MsgChannelOpenAck { + channel_id: 2, + counterparty_version: VERSION.to_owned(), + counterparty_channel_id: 0, + proof_try: vec![1, 2, 3].into(), + proof_height: 1, + relayer: mock_addr(RELAYER).to_string(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::ChannelOpenAck(msg), + ) + .expect("channel open ack is ok"); + + let msg = MsgPacketRecv { + packets: vec![Packet { + source_channel: 0, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }], + relayer_msgs: vec![vec![1].into()], + relayer: mock_addr(RELAYER).to_string(), + proof: vec![1, 2, 3].into(), + proof_height: 1, + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketRecv(msg), + ) + .expect("recv packet ok"); + let msg = MsgWriteAcknowledgement { + channel_id: 2, + packet: Packet { + source_channel: 0, + destination_channel: 2, + data: vec![1, 2, 3].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }, + acknowledgement: vec![1].into(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::WriteAcknowledgement(msg), + ) + .expect("write ack is ok"); + let msg = MsgPacketRecv { + packets: vec![Packet { + source_channel: 0, + destination_channel: 2, + data: vec![3, 4, 5].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }], + relayer_msgs: vec![vec![1].into()], + relayer: mock_addr(RELAYER).to_string(), + proof: vec![1, 2, 3].into(), + proof_height: 1, + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::PacketRecv(msg), + ) + .expect("recv packet ok"); + let msg = MsgWriteAcknowledgement { + channel_id: 2, + packet: Packet { + source_channel: 0, + destination_channel: 2, + data: vec![3, 4, 5].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }, + acknowledgement: vec![1].into(), + }; + execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::WriteAcknowledgement(msg), + ) + .expect("write ack is ok"); + + let msg = MsgBatchAcks { + source_channel: 2, + packets: vec![ + Packet { + source_channel: 0, + destination_channel: 2, + data: vec![10, 20, 30].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }, + Packet { + source_channel: 0, + destination_channel: 2, + data: vec![30, 40, 50].into(), + timeout_height: 100000, + timeout_timestamp: 0, + }, + ], + acks: vec![vec![1].into(), vec![1].into()], + }; + assert!(execute( + deps.as_mut(), + mock_env(), + message_info(&mock_addr(SENDER), &[]), + ExecuteMsg::BatchAcks(msg) + ) + .is_err_and(|err| { matches!(err, ContractError::PacketCommitmentNotFound) })) +} From ff341d362dc8d41c99fe325c6fc0bf35ef01f7ad Mon Sep 17 00:00:00 2001 From: PoisonPhang <17688291+PoisonPhang@users.noreply.github.com> Date: Wed, 15 Jan 2025 11:35:07 -0600 Subject: [PATCH 10/11] chore: remove cometbls tests --- .../light-clients/cometbls/Cargo.toml | 1 - .../light-clients/cometbls/src/lib.rs | 3 - .../light-clients/cometbls/src/tests.rs | 26 -- .../cometbls/src/tests/client.rs | 257 ------------------ 4 files changed, 287 deletions(-) delete mode 100644 cosmwasm/ibc-union/light-clients/cometbls/src/tests.rs delete mode 100644 cosmwasm/ibc-union/light-clients/cometbls/src/tests/client.rs diff --git a/cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml b/cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml index 73458f5092..b331b3a87e 100644 --- a/cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml +++ b/cosmwasm/ibc-union/light-clients/cometbls/Cargo.toml @@ -31,7 +31,6 @@ base64 = { workspace = true } hex-literal = { workspace = true } lazy_static = "1.4.0" serde_json = { workspace = true } -union-ibc = { workspace = true } [features] diff --git a/cosmwasm/ibc-union/light-clients/cometbls/src/lib.rs b/cosmwasm/ibc-union/light-clients/cometbls/src/lib.rs index 57b1ccb9a8..3336ca5909 100644 --- a/cosmwasm/ibc-union/light-clients/cometbls/src/lib.rs +++ b/cosmwasm/ibc-union/light-clients/cometbls/src/lib.rs @@ -3,6 +3,3 @@ pub mod contract; pub mod errors; pub mod storage; pub mod zkp_verifier; - -#[cfg(test)] -mod tests; diff --git a/cosmwasm/ibc-union/light-clients/cometbls/src/tests.rs b/cosmwasm/ibc-union/light-clients/cometbls/src/tests.rs deleted file mode 100644 index 971d27e257..0000000000 --- a/cosmwasm/ibc-union/light-clients/cometbls/src/tests.rs +++ /dev/null @@ -1,26 +0,0 @@ -use cosmwasm_std::{ - from_json, testing::MockApi, Addr, Binary, QuerierResult, StdResult, WasmQuery, -}; -use union_ibc_msg::lightclient::QueryMsg as LightClientQueryMsg; - -mod client; - -/// Creates a mock address from a given string. Prefixed with the default -/// `MockApi` prefix. -fn mock_addr(sender: &str) -> Addr { - let mock_api = MockApi::default(); - mock_api.addr_make(sender) -} - -fn wasm_query_handler StdResult + 'static>( - querier: F, -) -> impl Fn(&WasmQuery) -> QuerierResult + 'static { - move |msg| match msg { - WasmQuery::Smart { msg, .. } => { - let msg: LightClientQueryMsg = from_json(msg).unwrap(); - let res = querier(msg).unwrap(); - QuerierResult::Ok(cosmwasm_std::ContractResult::Ok(res)) - } - _ => panic!("Only smart queries should be possible now. Adjust this based on your needs."), - } -} diff --git a/cosmwasm/ibc-union/light-clients/cometbls/src/tests/client.rs b/cosmwasm/ibc-union/light-clients/cometbls/src/tests/client.rs deleted file mode 100644 index 4af608d01f..0000000000 --- a/cosmwasm/ibc-union/light-clients/cometbls/src/tests/client.rs +++ /dev/null @@ -1,257 +0,0 @@ -use std::io::Read; - -use cometbls_light_client_types::{ChainId, ClientState, ConsensusState}; -use cosmwasm_std::{ - testing::{message_info, mock_dependencies, mock_env}, - to_json_binary, -}; -use union_ibc::contract::{ - events::{self, attribute}, - execute, instantiate, -}; -use union_ibc_msg::{ - lightclient::QueryMsg as LightClientQueryMsg, - msg::{ExecuteMsg, InitMsg, MsgCreateClient, MsgRegisterClient}, -}; -use unionlabs::{ - bytes::Bytes, - encoding::{Decode, EncodeAs, EthAbi}, - ethereum::keccak256, - hash::hash_v2::{Base64, HexUnprefixed}, - ibc::core::{client::height::Height, commitment::merkle_root::MerkleRoot}, -}; - -use super::*; - -const SENDER: &str = "admin_sender"; -const CLIENT_TYPE: &str = "cometbls"; -const RELAYER: &str = "relayer"; - -// Client state starting values -const CHAIN_ID: &str = "test-chain"; -const TRUSTING_PERIOD: u64 = 86400; -const MAX_CLOCK_DRIFT: u64 = 300; -const FROZEN_HEIGHT: Height = Height::new(0); -const LATEST_HEIGHT: Height = Height::new(100); -const CONTRACT_ADDRESS_SEED: &str = "test-contract"; - -// Consensus state starting values -const TIMESTAMP: u64 = 1337; -const APP_HASH_SEED: &str = "app"; -const NEXT_VALIDATOR_HASH_SEED: &str = "validators"; - -fn misbehaviour_common(trusting_period: u64) -> u32 { - let mut deps = mock_dependencies(); - - instantiate( - deps.as_mut(), - mock_env(), - message_info(&mock_addr(SENDER), &[]), - InitMsg {}, - ) - .expect("instantiate ok"); - deps.querier - .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), - msg => panic!("should not be called: {:?}", msg), - })); - - // Encode client state - let client_state = ClientState { - chain_id: ChainId::from_string(CHAIN_ID).expect("is valid chain ID"), - trusting_period, - max_clock_drift: trusting_period, - frozen_height: FROZEN_HEIGHT, - latest_height: Height::new(99), - contract_address: keccak256(CONTRACT_ADDRESS_SEED), - }; - let client_state_bytes = Bytes::from(client_state.clone().encode_as::()); - - // Encode consensus state - let consensus_state = ConsensusState { - timestamp: TIMESTAMP, - app_hash: MerkleRoot { - hash: keccak256(APP_HASH_SEED).into_encoding::(), - }, - next_validators_hash: keccak256(NEXT_VALIDATOR_HASH_SEED).into_encoding::(), - }; - let consensus_state_bytes = Bytes::from(consensus_state.clone().encode_as::()); - - // Register client type - let msg = MsgRegisterClient { - client_type: CLIENT_TYPE.to_owned(), - client_address: mock_addr(CLIENT_TYPE).into_string(), - }; - execute( - deps.as_mut(), - mock_env(), - message_info(&mock_addr(SENDER), &[]), - ExecuteMsg::RegisterClient(msg), - ) - .expect("register client ok"); - - // Create client - let msg = MsgCreateClient { - client_type: CLIENT_TYPE.to_owned(), - client_state_bytes, - consensus_state_bytes: consensus_state_bytes.clone(), - relayer: mock_addr(RELAYER).into_string(), - }; - let res = execute( - deps.as_mut(), - mock_env(), - message_info(&mock_addr(SENDER), &[]), - ExecuteMsg::CreateClient(msg), - ) - .expect("create client ok"); - let client_id: u32 = res - .events - .iter() - .find(|event| event.ty.eq(events::client::CREATE)) - .expect("create client event exists") - .attributes - .iter() - .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) - .expect("client type attribute exists") - .value - .parse() - .expect("client type string is u32"); - - dbg!(client_id); - - let client_state = ClientState { - latest_height: Height::new(100), - ..client_state - }; - let client_state_bytes = Bytes::from(client_state.clone().encode_as::()); - // Create client - let msg = MsgCreateClient { - client_type: CLIENT_TYPE.to_owned(), - client_state_bytes, - consensus_state_bytes, - relayer: mock_addr(RELAYER).into_string(), - }; - let res = execute( - deps.as_mut(), - mock_env(), - message_info(&mock_addr(SENDER), &[]), - ExecuteMsg::CreateClient(msg), - ) - .expect("create client ok"); - let client_id = res - .events - .iter() - .find(|event| event.ty.eq(events::client::CREATE)) - .expect("create client event exists") - .attributes - .iter() - .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) - .expect("client type attribute exists") - .value - .parse() - .expect("client type string is u32"); - - client_id -} - -#[test] -fn create_client_success() { - let mut deps = mock_dependencies(); - - instantiate( - deps.as_mut(), - mock_env(), - message_info(&mock_addr(SENDER), &[]), - InitMsg {}, - ) - .expect("instantiate ok"); - deps.querier - .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), - msg => panic!("should not be called: {:?}", msg), - })); - - // Encode client state - let client_state = ClientState { - chain_id: ChainId::from_string(CHAIN_ID).expect("is valid chain ID"), - trusting_period: TRUSTING_PERIOD, - max_clock_drift: MAX_CLOCK_DRIFT, - frozen_height: FROZEN_HEIGHT, - latest_height: LATEST_HEIGHT, - contract_address: keccak256(CONTRACT_ADDRESS_SEED), - }; - let client_state_bytes = Bytes::from(client_state.clone().encode_as::()); - - // Encode consensus state - let consensus_state = ConsensusState { - timestamp: TIMESTAMP, - app_hash: MerkleRoot { - hash: keccak256(APP_HASH_SEED).into_encoding::(), - }, - next_validators_hash: keccak256(NEXT_VALIDATOR_HASH_SEED).into_encoding::(), - }; - let consensus_state_bytes = Bytes::from(consensus_state.clone().encode_as::()); - - // Register client type - let msg = MsgRegisterClient { - client_type: CLIENT_TYPE.to_owned(), - client_address: mock_addr(CLIENT_TYPE).into_string(), - }; - execute( - deps.as_mut(), - mock_env(), - message_info(&mock_addr(SENDER), &[]), - ExecuteMsg::RegisterClient(msg), - ) - .expect("register client ok"); - - // Create client - let msg = MsgCreateClient { - client_type: CLIENT_TYPE.to_owned(), - client_state_bytes, - consensus_state_bytes, - relayer: mock_addr(RELAYER).into_string(), - }; - let res = execute( - deps.as_mut(), - mock_env(), - message_info(&mock_addr(SENDER), &[]), - ExecuteMsg::CreateClient(msg), - ) - .expect("create client ok"); - let client_id: u32 = res - .events - .iter() - .find(|event| event.ty.eq(events::client::CREATE)) - .expect("create client event exists") - .attributes - .iter() - .find(|attribute| attribute.key.eq(events::attribute::CLIENT_ID)) - .expect("client type attribute exists") - .value - .parse() - .expect("client type string is u32"); - - // Verify client state was stored - let stored_client_state = >::decode( - &union_ibc::state::CLIENT_STATES - .load(&deps.storage, client_id) - .unwrap(), - ) - .unwrap(); - assert_eq!(stored_client_state, client_state); - - // Verify consensus state was stored - let stored_consensus_state = >::decode( - &union_ibc::state::CLIENT_CONSENSUS_STATES - .load(&deps.storage, (client_id, 1)) - .unwrap(), - ) - .unwrap(); - assert_eq!(stored_consensus_state, consensus_state); -} - -#[test] -fn misbehaviour_freezes_client() { - todo!("Mock CometBLS client and zkp verifier") -} From 36f2d1fe7b7ae23780cd51d79348fab800bd5f3a Mon Sep 17 00:00:00 2001 From: PoisonPhang <17688291+PoisonPhang@users.noreply.github.com> Date: Wed, 15 Jan 2025 11:35:37 -0600 Subject: [PATCH 11/11] chore(union-ibc): rebase claenup --- Cargo.lock | 2 + .../ibc-union/core/msg/src/lightclient.rs | 2 +- cosmwasm/ibc-union/core/src/tests.rs | 24 +------ .../core/src/tests/channel/ibc_channel.rs | 72 +++++++++++++++---- .../core/src/tests/channel/ibc_packet.rs | 1 + .../ibc-union/core/src/tests/client/ibc.rs | 43 ++++++----- .../core/src/tests/connection/ibc.rs | 54 ++++++++++---- .../core/src/tests/channel/ibc_packet.rs | 4 +- 8 files changed, 132 insertions(+), 70 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 78317fcf42..2b050e6e9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6128,8 +6128,10 @@ name = "ibc-union" version = "1.0.0" dependencies = [ "alloy", + "cosmwasm-schema 2.1.4", "cosmwasm-std 2.1.4", "cw-storage-plus 2.0.0", + "ethabi", "hex", "ibc-solidity", "ibc-union-msg", diff --git a/cosmwasm/ibc-union/core/msg/src/lightclient.rs b/cosmwasm/ibc-union/core/msg/src/lightclient.rs index ac2abf3cf6..645e527f5b 100644 --- a/cosmwasm/ibc-union/core/msg/src/lightclient.rs +++ b/cosmwasm/ibc-union/core/msg/src/lightclient.rs @@ -29,7 +29,7 @@ pub struct VerifyCreationResponse { pub counterparty_chain_id: String, } -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(Debug, serde::Serialize, serde::Deserialize)] #[serde(deny_unknown_fields, rename_all = "snake_case")] pub enum QueryMsg { GetTimestamp { diff --git a/cosmwasm/ibc-union/core/src/tests.rs b/cosmwasm/ibc-union/core/src/tests.rs index 6da3b895c3..1ef4f8a5a8 100644 --- a/cosmwasm/ibc-union/core/src/tests.rs +++ b/cosmwasm/ibc-union/core/src/tests.rs @@ -4,11 +4,11 @@ use cosmwasm_std::{ testing::{message_info, mock_env, MockApi}, Addr, Binary, DepsMut, QuerierResult, Response, StdResult, WasmQuery, }; -use union_ibc_msg::{ +use ibc_union_msg::{ lightclient::QueryMsg as LightClientQueryMsg, msg::{ - ExecuteMsg, MsgChannelOpenAck, MsgChannelOpenInit, MsgConnectionOpenConfirm, - MsgConnectionOpenInit, MsgConnectionOpenTry, MsgCreateClient, MsgRegisterClient, + ExecuteMsg, MsgChannelOpenInit, MsgConnectionOpenConfirm, MsgConnectionOpenInit, + MsgConnectionOpenTry, MsgCreateClient, MsgRegisterClient, }, }; @@ -133,24 +133,6 @@ fn channel_open_init(deps: DepsMut) -> Result { ) } -fn channel_open_ack(deps: DepsMut) -> Result { - let msg = MsgChannelOpenAck { - channel_id: 1, - counterparty_version: VERSION.to_owned(), - counterparty_channel_id: 0, - proof_try: vec![1, 2, 3].into(), - proof_height: 1, - relayer: mock_addr(RELAYER).to_string(), - }; - - execute( - deps, - mock_env(), - message_info(&mock_addr(SENDER), &[]), - ExecuteMsg::ChannelOpenAck(msg), - ) -} - #[test] fn display() { assert_eq!( diff --git a/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs b/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs index 914744f203..5d2f3e20c1 100644 --- a/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs +++ b/cosmwasm/ibc-union/core/src/tests/channel/ibc_channel.rs @@ -1,8 +1,11 @@ use contract::instantiate; use cosmwasm_std::{testing::mock_dependencies, to_json_binary}; use ibc_solidity::Channel; -use union_ibc_msg::msg::{ - InitMsg, MsgChannelOpenAck, MsgChannelOpenConfirm, MsgChannelOpenInit, MsgChannelOpenTry, +use ibc_union_msg::{ + lightclient::VerifyCreationResponse, + msg::{ + InitMsg, MsgChannelOpenAck, MsgChannelOpenConfirm, MsgChannelOpenInit, MsgChannelOpenTry, + }, }; use super::*; @@ -23,7 +26,10 @@ fn channel_open_init_ok() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -61,7 +67,10 @@ fn channel_open_init_channel_claimed() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -90,7 +99,10 @@ fn channel_open_init_commitment_saved() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -125,7 +137,10 @@ fn channel_open_try_ok() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -170,7 +185,10 @@ fn channel_open_try_invalid_state() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -224,7 +242,10 @@ fn channel_open_try_channel_claimed() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -274,7 +295,10 @@ fn channel_open_try_commitment_saved() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -330,7 +354,10 @@ fn channel_open_ack_ok() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -385,7 +412,10 @@ fn channel_open_ack_not_found() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -432,7 +462,10 @@ fn channel_open_ack_commitment_saved() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -498,7 +531,10 @@ fn channel_open_confirm_ok() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -557,7 +593,10 @@ fn channel_open_confirm_not_found() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -601,7 +640,10 @@ fn channel_open_confirm_commitment_saved() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); diff --git a/cosmwasm/ibc-union/core/src/tests/channel/ibc_packet.rs b/cosmwasm/ibc-union/core/src/tests/channel/ibc_packet.rs index e69de29bb2..8b13789179 100644 --- a/cosmwasm/ibc-union/core/src/tests/channel/ibc_packet.rs +++ b/cosmwasm/ibc-union/core/src/tests/channel/ibc_packet.rs @@ -0,0 +1 @@ + diff --git a/cosmwasm/ibc-union/core/src/tests/client/ibc.rs b/cosmwasm/ibc-union/core/src/tests/client/ibc.rs index 44785a7153..07dbdade95 100644 --- a/cosmwasm/ibc-union/core/src/tests/client/ibc.rs +++ b/cosmwasm/ibc-union/core/src/tests/client/ibc.rs @@ -2,8 +2,10 @@ use cosmwasm_std::{ testing::{message_info, mock_dependencies, mock_env}, to_json_binary, Addr, Event, }; -use union_ibc_msg::{ - lightclient::{QueryMsg as LightClientQueryMsg, VerifyClientMessageUpdate}, +use ibc_union_msg::{ + lightclient::{ + QueryMsg as LightClientQueryMsg, VerifyClientMessageUpdate, VerifyCreationResponse, + }, msg::{ExecuteMsg, InitMsg, MsgUpdateClient}, }; @@ -24,13 +26,6 @@ fn new_client_registered_event(client_type: &str, client_address: &Addr) -> Even .add_attribute(events::attribute::CLIENT_ADDRESS, client_address) } -fn new_client_created_event(client_type: &str, client_id: u32) -> Event { - Event::new(events::client::CREATE).add_attributes([ - (events::attribute::CLIENT_TYPE, client_type), - (events::attribute::CLIENT_ID, &client_id.to_string()), - ]) -} - #[test] fn register_client_ok() { let mut deps = mock_dependencies(); @@ -74,7 +69,10 @@ fn create_client_ok() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), msg => panic!("should not be called: {:?}", msg), })); @@ -96,7 +94,10 @@ fn create_client_commitments_saved() { .expect("instantiate ok"); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), msg => panic!("should not be called: {:?}", msg), })); @@ -115,11 +116,6 @@ fn create_client_commitments_saved() { .parse() .expect("client type string is u32"); - assert!(res - .events - .into_iter() - .any(|e| e == new_client_created_event(CLIENT_TYPE, client_id))); - assert_eq!( crate::state::CLIENT_TYPES .load(&deps.storage, client_id) @@ -161,7 +157,10 @@ fn update_client_ok() { .expect("instantiate ok"); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyClientMessage { .. } => { to_json_binary(&VerifyClientMessageUpdate { height: 2, @@ -215,7 +214,10 @@ fn update_client_ko() { .expect("instantiate ok"); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyClientMessage { .. } => to_json_binary(&0), msg => panic!("should not be called: {:?}", msg), })); @@ -263,7 +265,10 @@ fn update_client_commitments_saved() { .expect("instantiate ok"); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyClientMessage { .. } => { to_json_binary(&VerifyClientMessageUpdate { height: 2, diff --git a/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs b/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs index 7f577a0fe1..00773dc051 100644 --- a/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs +++ b/cosmwasm/ibc-union/core/src/tests/connection/ibc.rs @@ -1,9 +1,12 @@ use contract::instantiate; use cosmwasm_std::{testing::mock_dependencies, to_json_binary}; use ibc_solidity::Connection; -use union_ibc_msg::msg::{ - ExecuteMsg, InitMsg, MsgConnectionOpenAck, MsgConnectionOpenConfirm, MsgConnectionOpenInit, - MsgConnectionOpenTry, +use ibc_union_msg::{ + lightclient::VerifyCreationResponse, + msg::{ + ExecuteMsg, InitMsg, MsgConnectionOpenAck, MsgConnectionOpenConfirm, MsgConnectionOpenInit, + MsgConnectionOpenTry, + }, }; use super::*; @@ -20,7 +23,10 @@ fn connection_open_init_ok() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), msg => panic!("should not be called: {:?}", msg), })); register_client(deps.as_mut()).expect("register client ok"); @@ -52,7 +58,10 @@ fn connection_open_init_commitment_saved() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), msg => panic!("should not be called: {:?}", msg), })); register_client(deps.as_mut()).expect("register client ok"); @@ -82,7 +91,10 @@ fn connection_open_try_ok() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -119,7 +131,10 @@ fn connection_open_try_client_not_found() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -165,7 +180,10 @@ fn connection_open_try_commitment_saved() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -212,7 +230,10 @@ fn connection_open_ack_ok() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -254,7 +275,10 @@ fn connection_open_ack_commitment_saved() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -301,7 +325,10 @@ fn connection_open_confirm_ok() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); @@ -342,7 +369,10 @@ fn connection_open_try_confirm_commitment_saved() { .unwrap(); deps.querier .update_wasm(wasm_query_handler(|msg| match msg { - LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&1), + LightClientQueryMsg::VerifyCreation { .. } => to_json_binary(&VerifyCreationResponse { + latest_height: 1, + counterparty_chain_id: "testchain".to_owned(), + }), LightClientQueryMsg::VerifyMembership { .. } => to_json_binary(&()), msg => panic!("should not be called: {:?}", msg), })); diff --git a/cosmwasm/union-ibc/core/src/tests/channel/ibc_packet.rs b/cosmwasm/union-ibc/core/src/tests/channel/ibc_packet.rs index d83e4b810c..1a8c6cff34 100644 --- a/cosmwasm/union-ibc/core/src/tests/channel/ibc_packet.rs +++ b/cosmwasm/union-ibc/core/src/tests/channel/ibc_packet.rs @@ -1,11 +1,11 @@ use contract::instantiate; use cosmwasm_std::{testing::mock_dependencies, to_json_binary}; use ibc_solidity::Packet; -use ibc_union_spec::COMMITMENT_MAGIC; -use union_ibc_msg::msg::{ +use ibc_union_msg::msg::{ InitMsg, MsgBatchAcks, MsgBatchSend, MsgIntentPacketRecv, MsgPacketAcknowledgement, MsgPacketRecv, MsgPacketTimeout, MsgSendPacket, MsgWriteAcknowledgement, }; +use ibc_union_spec::COMMITMENT_MAGIC; use super::*;