From 1135b5310cd16dc3a0f220505abbee50344136b0 Mon Sep 17 00:00:00 2001 From: -f Date: Fri, 6 Dec 2024 16:26:48 +0530 Subject: [PATCH 1/5] add none to set_ism and set_hook --- packages/connection/Cargo.toml | 2 + packages/connection/src/lib.rs | 155 ++++++++++++++++++++++++--- packages/interface/src/connection.rs | 4 +- 3 files changed, 145 insertions(+), 16 deletions(-) diff --git a/packages/connection/Cargo.toml b/packages/connection/Cargo.toml index 4eaf5e0b..8a824658 100644 --- a/packages/connection/Cargo.toml +++ b/packages/connection/Cargo.toml @@ -30,6 +30,8 @@ serde.workspace = true serde-json-wasm.workspace = true thiserror.workspace = true cosmwasm-schema.workspace = true +rstest.workspace = true +ibcx-test-utils.workspace = true hpl-ownable.workspace = true hpl-interface.workspace = true diff --git a/packages/connection/src/lib.rs b/packages/connection/src/lib.rs index 91c7bdbd..ab97ffe5 100644 --- a/packages/connection/src/lib.rs +++ b/packages/connection/src/lib.rs @@ -49,22 +49,52 @@ pub fn handle( )) } SetIsm { ism } => { - let ism_addr = deps.api.addr_validate(&ism)?; - - ISM.save(deps.storage, &ism_addr)?; - - Ok(event_to_resp( - new_event("set_ism").add_attribute("ism", ism), - )) + match ism { + Some(ism) => { + let ism_addr = deps.api.addr_validate(&ism)?; + + ISM.save(deps.storage, &ism_addr)?; + + Ok(event_to_resp( + new_event("set_ism").add_attribute("ism", ism), + )) + } + None => { + let removed_ism = get_ism(deps.storage)? + .map(|addr| addr.to_string()) + .unwrap_or_else(|| "none".to_string()); + + ISM.remove(deps.storage); + + Ok(event_to_resp( + new_event("unset_ism").add_attribute("ism", removed_ism), + )) + } + } } SetHook { hook } => { - let hook_addr = deps.api.addr_validate(&hook)?; - - HOOK.save(deps.storage, &hook_addr)?; - - Ok(event_to_resp( - new_event("set_hook").add_attribute("hook", hook), - )) + match hook { + Some(hook) => { + let hook_addr = deps.api.addr_validate(&hook)?; + + HOOK.save(deps.storage, &hook_addr)?; + + Ok(event_to_resp( + new_event("set_hook").add_attribute("hook", hook), + )) + } + None => { + let removed_hook = get_hook(deps.storage)? + .map(|addr| addr.to_string()) + .unwrap_or_else(|| "none".to_string()); + + HOOK.remove(deps.storage); + + Ok(event_to_resp( + new_event("unset_hook").add_attribute("hook", removed_hook), + )) + } + } } } } @@ -98,3 +128,100 @@ pub fn get_ism(storage: &dyn Storage) -> StdResult> { pub fn get_hook(storage: &dyn Storage) -> StdResult> { HOOK.may_load(storage) } + +#[cfg(test)] +mod tests { + use super::*; + use cosmwasm_std::{ + testing::{mock_dependencies, mock_env, mock_info}, + Addr, + }; + use ibcx_test_utils::addr; + use rstest::rstest; + + const OWNER: &str = "owner"; + const NOT_OWNER: &str = "not_owner"; + + #[rstest] + #[case(addr(OWNER), addr("new_ism_address"))] + #[should_panic(expected = "unauthorized")] + #[case(addr(NOT_OWNER), addr("new_ism_address"))] + fn test_set_and_unset_ism(#[case] sender: Addr, #[case] new_ism_addr: Addr) { + let mut deps = mock_dependencies(); + + hpl_ownable::initialize(deps.as_mut().storage, &Addr::unchecked(OWNER)).unwrap(); + + assert!(get_ism(&deps.storage).unwrap().is_none()); + + let msg = ConnectionMsg::SetIsm { + ism: Some(new_ism_addr.to_string()), + }; + let info = mock_info(sender.as_str(), &[]); + + let res = handle(deps.as_mut(), mock_env(), info, msg).unwrap(); + assert_eq!( + res, + event_to_resp( + new_event("set_ism").add_attribute("ism", new_ism_addr.to_string()) + ) + ); + + let ism = get_ism(&deps.storage).unwrap().unwrap(); + assert_eq!(ism, new_ism_addr); + + let unset_msg = ConnectionMsg::SetIsm { ism: None }; + let unset_info = mock_info(sender.as_str(), &[]); + + let res = handle(deps.as_mut(), mock_env(), unset_info, unset_msg).unwrap(); + assert_eq!( + res, + event_to_resp( + new_event("unset_ism").add_attribute("ism", new_ism_addr.to_string()) + ) + ); + + let ism = get_ism(&deps.storage).unwrap(); + assert!(ism.is_none()); + } + + #[rstest] + #[case(addr(OWNER), addr("new_hook_address"))] + #[should_panic(expected = "unauthorized")] + #[case(addr(NOT_OWNER), addr("new_hook_address"))] + fn test_set_and_unset_hook(#[case] sender: Addr, #[case] new_hook_addr: Addr) { + let mut deps = mock_dependencies(); + + hpl_ownable::initialize(deps.as_mut().storage, &Addr::unchecked(OWNER)).unwrap(); + assert!(get_hook(&deps.storage).unwrap().is_none()); + + let msg = ConnectionMsg::SetHook { + hook: Some(new_hook_addr.to_string()), + }; + let info = mock_info(sender.as_str(), &[]); + + let res = handle(deps.as_mut(), mock_env(), info, msg).unwrap(); + assert_eq!( + res, + event_to_resp( + new_event("set_hook").add_attribute("hook", new_hook_addr.to_string()) + ) + ); + + let hook = get_hook(&deps.storage).unwrap().unwrap(); + assert_eq!(hook, new_hook_addr); + + let unset_msg = ConnectionMsg::SetHook { hook: None }; + let unset_info = mock_info(sender.as_str(), &[]); + + let res = handle(deps.as_mut(), mock_env(), unset_info, unset_msg).unwrap(); + assert_eq!( + res, + event_to_resp( + new_event("unset_hook").add_attribute("hook", new_hook_addr.to_string()) + ) + ); + + let hook = get_hook(&deps.storage).unwrap(); + assert!(hook.is_none()); + } +} diff --git a/packages/interface/src/connection.rs b/packages/interface/src/connection.rs index 7e655037..fdca8a9b 100644 --- a/packages/interface/src/connection.rs +++ b/packages/interface/src/connection.rs @@ -4,9 +4,9 @@ use cosmwasm_schema::{cw_serde, QueryResponses}; pub enum ConnectionMsg { SetMailbox { mailbox: String }, - SetHook { hook: String }, + SetHook { hook: Option }, - SetIsm { ism: String }, + SetIsm { ism: Option }, } #[cw_serde] From 98a000dbbac0bdce55d4239fdb562f6856cd0de0 Mon Sep 17 00:00:00 2001 From: Kunal Arora <55632507+aroralanuk@users.noreply.github.com> Date: Thu, 26 Dec 2024 22:18:19 +0530 Subject: [PATCH 2/5] Update packages/connection/src/lib.rs Co-authored-by: Yorke Rhodes --- packages/connection/src/lib.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/connection/src/lib.rs b/packages/connection/src/lib.rs index ab97ffe5..b9608c01 100644 --- a/packages/connection/src/lib.rs +++ b/packages/connection/src/lib.rs @@ -60,14 +60,10 @@ pub fn handle( )) } None => { - let removed_ism = get_ism(deps.storage)? - .map(|addr| addr.to_string()) - .unwrap_or_else(|| "none".to_string()); - ISM.remove(deps.storage); Ok(event_to_resp( - new_event("unset_ism").add_attribute("ism", removed_ism), + new_event("unset_ism") )) } } From 26f918a4c4d4f11d3d660fe4ed35e72f66e73c4c Mon Sep 17 00:00:00 2001 From: Kunal Arora <55632507+aroralanuk@users.noreply.github.com> Date: Thu, 26 Dec 2024 22:18:26 +0530 Subject: [PATCH 3/5] Update packages/connection/src/lib.rs Co-authored-by: Yorke Rhodes --- packages/connection/src/lib.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/connection/src/lib.rs b/packages/connection/src/lib.rs index b9608c01..b9ad9f33 100644 --- a/packages/connection/src/lib.rs +++ b/packages/connection/src/lib.rs @@ -80,14 +80,10 @@ pub fn handle( )) } None => { - let removed_hook = get_hook(deps.storage)? - .map(|addr| addr.to_string()) - .unwrap_or_else(|| "none".to_string()); - HOOK.remove(deps.storage); Ok(event_to_resp( - new_event("unset_hook").add_attribute("hook", removed_hook), + new_event("unset_hook") )) } } From eed4de30f822ad61e31e989b8ff1ca740c2ccaf4 Mon Sep 17 00:00:00 2001 From: -f Date: Thu, 26 Dec 2024 22:34:01 +0530 Subject: [PATCH 4/5] rm unset return test req --- packages/connection/src/lib.rs | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/packages/connection/src/lib.rs b/packages/connection/src/lib.rs index b9ad9f33..a30d2f4f 100644 --- a/packages/connection/src/lib.rs +++ b/packages/connection/src/lib.rs @@ -191,13 +191,7 @@ mod tests { }; let info = mock_info(sender.as_str(), &[]); - let res = handle(deps.as_mut(), mock_env(), info, msg).unwrap(); - assert_eq!( - res, - event_to_resp( - new_event("set_hook").add_attribute("hook", new_hook_addr.to_string()) - ) - ); + handle(deps.as_mut(), mock_env(), info, msg).unwrap(); let hook = get_hook(&deps.storage).unwrap().unwrap(); assert_eq!(hook, new_hook_addr); @@ -205,13 +199,7 @@ mod tests { let unset_msg = ConnectionMsg::SetHook { hook: None }; let unset_info = mock_info(sender.as_str(), &[]); - let res = handle(deps.as_mut(), mock_env(), unset_info, unset_msg).unwrap(); - assert_eq!( - res, - event_to_resp( - new_event("unset_hook").add_attribute("hook", new_hook_addr.to_string()) - ) - ); + handle(deps.as_mut(), mock_env(), unset_info, unset_msg).unwrap(); let hook = get_hook(&deps.storage).unwrap(); assert!(hook.is_none()); From 3270aab88c7da2ec4dc61ca67c505e10d8afaa14 Mon Sep 17 00:00:00 2001 From: -f Date: Thu, 26 Dec 2024 22:35:35 +0530 Subject: [PATCH 5/5] rm unset ism req --- packages/connection/src/lib.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/connection/src/lib.rs b/packages/connection/src/lib.rs index a30d2f4f..ccb95dc0 100644 --- a/packages/connection/src/lib.rs +++ b/packages/connection/src/lib.rs @@ -164,13 +164,7 @@ mod tests { let unset_msg = ConnectionMsg::SetIsm { ism: None }; let unset_info = mock_info(sender.as_str(), &[]); - let res = handle(deps.as_mut(), mock_env(), unset_info, unset_msg).unwrap(); - assert_eq!( - res, - event_to_resp( - new_event("unset_ism").add_attribute("ism", new_ism_addr.to_string()) - ) - ); + handle(deps.as_mut(), mock_env(), unset_info, unset_msg).unwrap(); let ism = get_ism(&deps.storage).unwrap(); assert!(ism.is_none()); @@ -191,7 +185,13 @@ mod tests { }; let info = mock_info(sender.as_str(), &[]); - handle(deps.as_mut(), mock_env(), info, msg).unwrap(); + let res = handle(deps.as_mut(), mock_env(), info, msg).unwrap(); + assert_eq!( + res, + event_to_resp( + new_event("set_hook").add_attribute("hook", new_hook_addr.to_string()) + ) + ); let hook = get_hook(&deps.storage).unwrap().unwrap(); assert_eq!(hook, new_hook_addr); @@ -200,7 +200,7 @@ mod tests { let unset_info = mock_info(sender.as_str(), &[]); handle(deps.as_mut(), mock_env(), unset_info, unset_msg).unwrap(); - + let hook = get_hook(&deps.storage).unwrap(); assert!(hook.is_none()); }