Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(connection): add option to unset ISM and hook #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/connection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
155 changes: 141 additions & 14 deletions packages/connection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,52 @@ pub fn handle<C: CustomQuery>(
))
}
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),
))
}
}
}
}
}
Expand Down Expand Up @@ -98,3 +128,100 @@ pub fn get_ism(storage: &dyn Storage) -> StdResult<Option<Addr>> {
pub fn get_hook(storage: &dyn Storage) -> StdResult<Option<Addr>> {
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());
}
}
4 changes: 2 additions & 2 deletions packages/interface/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use cosmwasm_schema::{cw_serde, QueryResponses};
pub enum ConnectionMsg {
SetMailbox { mailbox: String },

SetHook { hook: String },
SetHook { hook: Option<String> },

SetIsm { ism: String },
SetIsm { ism: Option<String> },
}

#[cw_serde]
Expand Down