Skip to content

Commit

Permalink
Merge pull request #3 from hyperlane-xyz/nambrot/fix-multisig-ism
Browse files Browse the repository at this point in the history
Allow threshold == set size and add tests
  • Loading branch information
yorhodes authored Jan 16, 2024
2 parents 7973533 + 8a9aa1b commit 26f040f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 320 deletions.
76 changes: 75 additions & 1 deletion contracts/isms/multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn execute(
ContractError::invalid_addr("length should be 20")
);
ensure!(
validators.len() > threshold as usize && threshold > 0,
validators.len() >= threshold as usize && threshold > 0,
ContractError::invalid_args(&format!(
"threshold not in range. 0 < <= {}",
validators.len(),
Expand Down Expand Up @@ -137,3 +137,77 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<QueryResponse, Contr
pub fn migrate(_deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
Ok(Response::new())
}

#[cfg(test)]
mod test {
use cosmwasm_std::{
testing::mock_dependencies, HexBinary,
};
use hpl_interface::{
build_test_executor, build_test_querier,
ism::multisig::ExecuteMsg,
};
use ibcx_test_utils::{addr, hex};
use rstest::rstest;

use crate::state::VALIDATORS;

build_test_executor!(crate::contract::execute);
build_test_querier!(crate::contract::query);

#[rstest]
#[case("owner", vec![hex(&"deadbeef".repeat(5))])]
#[should_panic(expected = "unauthorized")]
#[case("someone", vec![hex(&"deadbeef".repeat(5))])]
fn test_enroll(#[case] sender: &str, #[case] validators: Vec<HexBinary>) {
let mut deps = mock_dependencies();

hpl_ownable::initialize(deps.as_mut().storage, &addr("owner")).unwrap();

test_execute(
deps.as_mut(),
&addr(sender),
ExecuteMsg::SetValidators {
domain: 1,
threshold: 1,
validators: validators.clone(),
},
vec![],
);

assert_eq!(
VALIDATORS.load(deps.as_ref().storage, 1).unwrap(),
validators
);
}

#[rstest]
#[case("owner")]
#[should_panic(expected = "unauthorized")]
#[case("someone")]
fn test_unenroll(#[case] sender: &str) {
let mut deps = mock_dependencies();

hpl_ownable::initialize(deps.as_mut().storage, &addr("owner")).unwrap();

test_execute(
deps.as_mut(),
&addr("owner"),
ExecuteMsg::SetValidators {
domain: 1,
threshold: 1,
validators: vec![hex(&"deadbeef".repeat(5))],
},
vec![],
);

test_execute(
deps.as_mut(),
&addr(sender),
ExecuteMsg::UnsetDomain { domain: 1 } ,
vec![],
);

assert!(!VALIDATORS.has(deps.as_ref().storage, 1));
}
}
Loading

0 comments on commit 26f040f

Please sign in to comment.