diff --git a/contracts/voting-verifier/tests/mock.rs b/contracts/voting-verifier/tests/mock.rs deleted file mode 100644 index 01cca445c..000000000 --- a/contracts/voting-verifier/tests/mock.rs +++ /dev/null @@ -1,124 +0,0 @@ -use cosmwasm_schema::cw_serde; -use cosmwasm_std::{ - to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult, Uint128, -}; -use cw_multi_test::{App, ContractWrapper, Executor}; - -use service_registry::{ - msg::{ExecuteMsg, InstantiateMsg}, - state::{AuthorizationState, BondingState, Worker}, - ContractError, -}; - -pub fn mock_service_registry_execute( - _deps: DepsMut, - _env: Env, - _info: MessageInfo, - _msg: ExecuteMsg, -) -> Result { - Ok(Response::new()) -} - -#[cw_serde] -pub enum MockServiceRegistryQueryMsg { - GetActiveWorkers { - service_name: String, - chain_name: String, - }, -} -pub fn mock_service_registry_query( - _deps: Deps, - _env: Env, - msg: MockServiceRegistryQueryMsg, -) -> StdResult { - match msg { - MockServiceRegistryQueryMsg::GetActiveWorkers { - service_name, - chain_name: _, - } => { - let res = vec![ - Worker { - address: Addr::unchecked("addr1"), - bonding_state: BondingState::Bonded { - amount: Uint128::from(100u128), - }, - authorization_state: AuthorizationState::Authorized, - service_name: service_name.clone(), - }, - Worker { - address: Addr::unchecked("addr2"), - bonding_state: BondingState::Bonded { - amount: Uint128::from(100u128), - }, - authorization_state: AuthorizationState::Authorized, - service_name: service_name.clone(), - }, - ]; - Ok(to_binary(&res)?) - } - } -} - -pub fn make_mock_service_registry(app: &mut App) -> Addr { - let code = ContractWrapper::new( - mock_service_registry_execute, - |_, _, _, _: InstantiateMsg| Ok::(Response::new()), - mock_service_registry_query, - ); - let code_id = app.store_code(Box::new(code)); - - let contract_address = app - .instantiate_contract( - code_id, - Addr::unchecked("sender"), - &InstantiateMsg { - governance_account: Addr::unchecked("governance").into(), - }, - &[], - "Contract", - None, - ) - .unwrap(); - contract_address -} - -#[cw_serde] -pub struct MockRewardsInstantiateMsg; - -pub fn mock_rewards_execute( - _deps: DepsMut, - _env: Env, - _info: MessageInfo, - _msg: rewards::msg::ExecuteMsg, -) -> Result { - Ok(Response::new()) -} - -pub fn mock_rewards_query( - _deps: Deps, - _env: Env, - _msg: rewards::msg::QueryMsg, -) -> StdResult { - Ok(to_binary("")?) -} - -pub fn make_mock_rewards(app: &mut App) -> Addr { - let code = ContractWrapper::new( - mock_rewards_execute, - |_, _, _, _: MockRewardsInstantiateMsg| Ok::(Response::new()), - mock_rewards_query, - ); - let code_id = app.store_code(Box::new(code)); - - let contract_address = app - .instantiate_contract( - code_id, - Addr::unchecked("sender"), - &MockRewardsInstantiateMsg, - &[], - "Contract", - None, - ) - .unwrap(); - contract_address -} diff --git a/contracts/voting-verifier/tests/tests.rs b/contracts/voting-verifier/tests/tests.rs deleted file mode 100644 index a51710e2c..000000000 --- a/contracts/voting-verifier/tests/tests.rs +++ /dev/null @@ -1,358 +0,0 @@ -use axelar_wasm_std::voting::Vote; -use cosmwasm_std::{from_binary, Addr, Uint64}; -use cw_multi_test::{App, Executor}; - -use axelar_wasm_std::operators::Operators; -use axelar_wasm_std::{nonempty, VerificationStatus}; -use connection_router_api::{ChainName, CrossChainId, Message, ID_SEPARATOR}; -use integration_tests::contract::Contract; -use mock::make_mock_rewards; -use service_registry::state::Worker; -use voting_verifier::events::TxEventConfirmation; -use voting_verifier::{error::ContractError, msg}; - -use crate::mock::make_mock_service_registry; -use crate::test_utils::VotingVerifierContract; - -pub mod mock; -mod test_utils; - -const SENDER: &str = "sender"; -const POLL_BLOCK_EXPIRY: u64 = 100; -fn source_chain() -> ChainName { - "source_chain".parse().unwrap() -} - -struct TestFixture { - app: App, - service_registry_address: Addr, - voting_verifier: VotingVerifierContract, -} - -fn setup() -> TestFixture { - let mut app = App::default(); - let service_registry_address = make_mock_service_registry(&mut app); - let rewards_address: String = make_mock_rewards(&mut app).into(); - let voting_verifier = VotingVerifierContract::instantiate_contract( - &mut app, - service_registry_address.as_ref().parse().unwrap(), - rewards_address.clone(), - ); - TestFixture { - app, - service_registry_address, - voting_verifier, - } -} - -fn message_id(id: &str, index: u64) -> nonempty::String { - format!("{}{}{}", id, ID_SEPARATOR, index) - .try_into() - .unwrap() -} - -fn messages(len: u64) -> Vec { - (0..len) - .map(|i| Message { - cc_id: CrossChainId { - chain: source_chain(), - id: format!("id:{i}").parse().unwrap(), - }, - source_address: format!("source_address{i}").parse().unwrap(), - destination_chain: format!("destination_chain{i}").parse().unwrap(), - destination_address: format!("destination_address{i}").parse().unwrap(), - payload_hash: [0; 32], - }) - .collect() -} - -#[test] -fn should_confirm_worker_set() { - let mut fixture = setup(); - - let workers: Vec = fixture - .app - .wrap() - .query_wasm_smart( - fixture.service_registry_address, - &service_registry::msg::QueryMsg::GetActiveWorkers { - service_name: "service_name".to_string(), - chain_name: source_chain(), - }, - ) - .unwrap(); - - let operators = Operators::new(vec![(vec![0, 1, 0, 1].into(), 1u64.into())], 1u64.into()); - let msg = msg::ExecuteMsg::VerifyWorkerSet { - message_id: message_id("id", 0), - new_operators: operators.clone(), - }; - let res = fixture - .voting_verifier - .execute(&mut fixture.app, Addr::unchecked(SENDER), &msg); - assert!(res.is_ok()); - - let msg = msg::ExecuteMsg::Vote { - poll_id: 1u64.into(), - votes: vec![Vote::SucceededOnChain], - }; - for worker in workers { - let res = fixture - .voting_verifier - .execute(&mut fixture.app, worker.address.clone(), &msg); - assert!(res.is_ok()); - } - - fixture - .app - .update_block(|block| block.height += POLL_BLOCK_EXPIRY); - - let res = fixture.voting_verifier.execute( - &mut fixture.app, - Addr::unchecked(SENDER), - &msg::ExecuteMsg::EndPoll { - poll_id: 1u64.into(), - }, - ); - assert!(res.is_ok()); - - let res: VerificationStatus = fixture.voting_verifier.query( - &fixture.app, - &msg::QueryMsg::GetWorkerSetStatus { - new_operators: operators.clone(), - }, - ); - assert_eq!(res, VerificationStatus::SucceededOnChain); -} - -#[test] -fn should_not_confirm_worker_set() { - let mut fixture = setup(); - - let workers: Vec = fixture - .app - .wrap() - .query_wasm_smart( - fixture.service_registry_address, - &service_registry::msg::QueryMsg::GetActiveWorkers { - service_name: "service_name".to_string(), - chain_name: source_chain(), - }, - ) - .unwrap(); - - let operators = Operators::new(vec![(vec![0, 1, 0, 1].into(), 1u64.into())], 1u64.into()); - let res = fixture.voting_verifier.execute( - &mut fixture.app, - Addr::unchecked(SENDER), - &msg::ExecuteMsg::VerifyWorkerSet { - message_id: message_id("id", 0), - new_operators: operators.clone(), - }, - ); - assert!(res.is_ok()); - - for worker in workers { - let res = fixture.voting_verifier.execute( - &mut fixture.app, - worker.address.clone(), - &msg::ExecuteMsg::Vote { - poll_id: 1u64.into(), - votes: vec![Vote::NotFound], - }, - ); - assert!(res.is_ok()); - } - - fixture - .app - .update_block(|block| block.height += POLL_BLOCK_EXPIRY); - - let res = fixture.voting_verifier.execute( - &mut fixture.app, - Addr::unchecked(SENDER), - &msg::ExecuteMsg::EndPoll { - poll_id: 1u64.into(), - }, - ); - assert!(res.is_ok()); - - let res: VerificationStatus = fixture.voting_verifier.query( - &fixture.app, - &msg::QueryMsg::GetWorkerSetStatus { - new_operators: operators.clone(), - }, - ); - assert_eq!(res, VerificationStatus::NotFound); -} - -#[test] -fn should_confirm_worker_set_after_failed() { - let mut fixture = setup(); - - let workers: Vec = fixture - .app - .wrap() - .query_wasm_smart( - fixture.service_registry_address, - &service_registry::msg::QueryMsg::GetActiveWorkers { - service_name: "service_name".to_string(), - chain_name: source_chain(), - }, - ) - .unwrap(); - - let operators = Operators::new(vec![(vec![0, 1, 0, 1].into(), 1u64.into())], 1u64.into()); - let res = fixture.voting_verifier.execute( - &mut fixture.app, - Addr::unchecked(SENDER), - &msg::ExecuteMsg::VerifyWorkerSet { - message_id: message_id("id", 0), - new_operators: operators.clone(), - }, - ); - assert!(res.is_ok()); - - for worker in &workers { - let res = fixture.voting_verifier.execute( - &mut fixture.app, - worker.address.clone(), - &msg::ExecuteMsg::Vote { - poll_id: 1u64.into(), - votes: vec![Vote::NotFound], - }, - ); - assert!(res.is_ok()); - } - - fixture - .app - .update_block(|block| block.height += POLL_BLOCK_EXPIRY); - - let res = fixture.voting_verifier.execute( - &mut fixture.app, - Addr::unchecked(SENDER), - &msg::ExecuteMsg::EndPoll { - poll_id: 1u64.into(), - }, - ); - assert!(res.is_ok()); - - let res: VerificationStatus = fixture.voting_verifier.query( - &fixture.app, - &msg::QueryMsg::GetWorkerSetStatus { - new_operators: operators.clone(), - }, - ); - assert_eq!(res, VerificationStatus::NotFound); - - let res = fixture.voting_verifier.execute( - &mut fixture.app, - Addr::unchecked(SENDER), - &msg::ExecuteMsg::VerifyWorkerSet { - message_id: message_id("id", 0), - new_operators: operators.clone(), - }, - ); - assert!(res.is_ok()); - - for worker in workers { - let res = fixture.voting_verifier.execute( - &mut fixture.app, - worker.address.clone(), - &msg::ExecuteMsg::Vote { - poll_id: 2u64.into(), - votes: vec![Vote::SucceededOnChain], - }, - ); - assert!(res.is_ok()); - } - - fixture - .app - .update_block(|block| block.height += POLL_BLOCK_EXPIRY); - - let res = fixture.voting_verifier.execute( - &mut fixture.app, - Addr::unchecked(SENDER), - &msg::ExecuteMsg::EndPoll { - poll_id: 2u64.into(), - }, - ); - assert!(res.is_ok()); - - let res: VerificationStatus = fixture.voting_verifier.query( - &fixture.app, - &msg::QueryMsg::GetWorkerSetStatus { - new_operators: operators.clone(), - }, - ); - assert_eq!(res, VerificationStatus::SucceededOnChain); -} - -#[test] -fn should_not_confirm_twice() { - let mut fixture = setup(); - - let workers: Vec = fixture - .app - .wrap() - .query_wasm_smart( - fixture.service_registry_address, - &service_registry::msg::QueryMsg::GetActiveWorkers { - service_name: "service_name".to_string(), - chain_name: source_chain(), - }, - ) - .unwrap(); - - let operators = Operators::new(vec![(vec![0, 1, 0, 1].into(), 1u64.into())], 1u64.into()); - let res = fixture.voting_verifier.execute( - &mut fixture.app, - Addr::unchecked(SENDER), - &msg::ExecuteMsg::VerifyWorkerSet { - message_id: message_id("id", 0), - new_operators: operators.clone(), - }, - ); - assert!(res.is_ok()); - - for worker in workers { - let res = fixture.voting_verifier.execute( - &mut fixture.app, - worker.address.clone(), - &msg::ExecuteMsg::Vote { - poll_id: 1u64.into(), - votes: vec![Vote::SucceededOnChain], - }, - ); - assert!(res.is_ok()); - } - - fixture - .app - .update_block(|block| block.height += POLL_BLOCK_EXPIRY); - - let res = fixture.voting_verifier.execute( - &mut fixture.app, - Addr::unchecked(SENDER), - &msg::ExecuteMsg::EndPoll { - poll_id: 1u64.into(), - }, - ); - assert!(res.is_ok()); - - // try again, should fail - let err = fixture - .voting_verifier - .execute( - &mut fixture.app, - Addr::unchecked(SENDER), - &msg::ExecuteMsg::VerifyWorkerSet { - message_id: message_id("id", 0), - new_operators: operators.clone(), - }, - ) - .unwrap_err(); - test_utils::are_contract_err_strings_equal(err, ContractError::WorkerSetAlreadyConfirmed); -}