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

Allow threshold == set size and add tests #3

Merged
merged 1 commit into from
Jan 16, 2024
Merged
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
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
Loading