From 94c0c5ce379d43f23e37ca1b565a8b94b5f16c92 Mon Sep 17 00:00:00 2001 From: gluax <16431709+gluax@users.noreply.github.com> Date: Tue, 7 May 2024 11:18:33 -0600 Subject: [PATCH] fix: deposit stake withdraw test --- packages/integration-tests/src/data_result.rs | 12 ++--- packages/integration-tests/src/sign.rs | 2 +- packages/integration-tests/src/staking.rs | 44 ++++++++++--------- packages/integration-tests/src/utils.rs | 4 +- packages/proxy/src/msg.rs | 2 +- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/packages/integration-tests/src/data_result.rs b/packages/integration-tests/src/data_result.rs index e7e95573..329ffeea 100644 --- a/packages/integration-tests/src/data_result.rs +++ b/packages/integration-tests/src/data_result.rs @@ -20,9 +20,9 @@ use proxy_contract::msg::{ProxyExecuteMsg, ProxyQueryMsg}; fn commit_reveal_result() { let (mut app, proxy_contract) = proper_instantiate(); - let mut exec_1 = TestExecutor::new("exec_1"); - let mut exec_2 = TestExecutor::new("exec_2"); - let mut exec_3 = TestExecutor::new("exec_3"); + let exec_1 = TestExecutor::new("exec_1"); + let exec_2 = TestExecutor::new("exec_2"); + let exec_3 = TestExecutor::new("exec_3"); // executor 1 should be ineligible to register let msg = ProxyQueryMsg::IsDataRequestExecutorEligible { @@ -41,9 +41,9 @@ fn commit_reveal_result() { // register executors let memo = Some("address".to_string()); - helper_reg_dr_executor(&mut app, proxy_contract.clone(), &mut exec_1, memo.clone()).unwrap(); - helper_reg_dr_executor(&mut app, proxy_contract.clone(), &mut exec_2, memo.clone()).unwrap(); - helper_reg_dr_executor(&mut app, proxy_contract.clone(), &mut exec_3, memo.clone()).unwrap(); + helper_reg_dr_executor(&mut app, proxy_contract.clone(), &exec_1, memo.clone()).unwrap(); + helper_reg_dr_executor(&mut app, proxy_contract.clone(), &exec_2, memo.clone()).unwrap(); + helper_reg_dr_executor(&mut app, proxy_contract.clone(), &exec_3, memo.clone()).unwrap(); // check if executors are eligible register // executor 1 should be eligible to register diff --git a/packages/integration-tests/src/sign.rs b/packages/integration-tests/src/sign.rs index b4778b8c..51c59834 100644 --- a/packages/integration-tests/src/sign.rs +++ b/packages/integration-tests/src/sign.rs @@ -5,7 +5,7 @@ use sha3::{Digest, Keccak256}; #[test] pub fn recover_pub_key_from_sig() { - let mut executor = TestExecutor::new("test"); + let executor = TestExecutor::new("test"); let mut hasher = Keccak256::new(); hasher.update("hello world".as_bytes()); diff --git a/packages/integration-tests/src/staking.rs b/packages/integration-tests/src/staking.rs index 75f7feb9..83056e22 100644 --- a/packages/integration-tests/src/staking.rs +++ b/packages/integration-tests/src/staking.rs @@ -1,6 +1,6 @@ -use crate::tests::utils::{proper_instantiate, send_tokens, EXECUTOR_1, NATIVE_DENOM, USER}; +use crate::tests::utils::{proper_instantiate, send_tokens, TestExecutor, NATIVE_DENOM, USER}; -use common::{msg::GetDataRequestExecutorResponse, state::DataRequestExecutor, types::Signature}; +use common::{msg::GetDataRequestExecutorResponse, state::DataRequestExecutor}; use cosmwasm_std::Addr; use cw_multi_test::Executor; @@ -11,23 +11,25 @@ use proxy_contract::msg::{ProxyExecuteMsg, ProxyQueryMsg}; fn deposit_stake_withdraw() { let (mut app, proxy_contract) = proper_instantiate(); + let exec = TestExecutor::new("foo"); + // send tokens from USER to executor1 so it can register - send_tokens(&mut app, USER, EXECUTOR_1, 3); + send_tokens(&mut app, USER, exec.name, 3); let msg = ProxyExecuteMsg::RegisterDataRequestExecutor { memo: Some("address".to_string()), - public_key: vec![], - signature: Signature::new([0; 65]), + public_key: exec.public_key.clone(), + signature: exec.sign(["register_data_request_executor".as_bytes().to_vec()]), }; let cosmos_msg = proxy_contract .call_with_deposit(msg, INITIAL_MINIMUM_STAKE_TO_REGISTER) .unwrap(); // register executor - app.execute(Addr::unchecked(EXECUTOR_1), cosmos_msg.clone()) + app.execute(Addr::unchecked(exec.name), cosmos_msg.clone()) .unwrap(); let msg = ProxyQueryMsg::GetDataRequestExecutor { - executor: Addr::unchecked(EXECUTOR_1), + executor: exec.public_key.clone(), }; let res: GetDataRequestExecutorResponse = app .wrap() @@ -47,15 +49,15 @@ fn deposit_stake_withdraw() { // deposit 2 more let msg = ProxyExecuteMsg::DepositAndStake { - public_key: vec![], - signature: Signature::new([0; 65]), + public_key: exec.public_key.clone(), + signature: exec.sign(["deposit_and_stake".as_bytes().to_vec()]), }; let cosmos_msg = proxy_contract.call_with_deposit(msg, 2).unwrap(); - app.execute(Addr::unchecked(EXECUTOR_1), cosmos_msg.clone()) + app.execute(Addr::unchecked(exec.name), cosmos_msg.clone()) .unwrap(); let msg = ProxyQueryMsg::GetDataRequestExecutor { - executor: Addr::unchecked(EXECUTOR_1), + executor: exec.public_key.clone(), }; let res: GetDataRequestExecutorResponse = app .wrap() @@ -76,15 +78,15 @@ fn deposit_stake_withdraw() { // unstake 2 let msg = ProxyExecuteMsg::Unstake { amount: 2, - public_key: vec![], - signature: Signature::new([0; 65]), + public_key: exec.public_key.clone(), + signature: exec.sign(["unstake".as_bytes().to_vec()]), }; let cosmos_msg = proxy_contract.call(msg).unwrap(); - app.execute(Addr::unchecked(EXECUTOR_1), cosmos_msg.clone()) + app.execute(Addr::unchecked(exec.name), cosmos_msg.clone()) .unwrap(); let msg = ProxyQueryMsg::GetDataRequestExecutor { - executor: Addr::unchecked(EXECUTOR_1), + executor: exec.public_key.clone(), }; let res: GetDataRequestExecutorResponse = app .wrap() @@ -104,7 +106,7 @@ fn deposit_stake_withdraw() { let balance_before = app .wrap() - .query_balance(EXECUTOR_1, NATIVE_DENOM) + .query_balance(exec.name, NATIVE_DENOM) .unwrap() .amount .u128(); @@ -113,15 +115,15 @@ fn deposit_stake_withdraw() { // withdraw 2 let msg = ProxyExecuteMsg::Withdraw { amount: 2, - public_key: vec![], - signature: Signature::new([0; 65]), + public_key: exec.public_key.clone(), + signature: exec.sign(["withdraw".as_bytes().to_vec()]), }; let cosmos_msg = proxy_contract.call(msg).unwrap(); - app.execute(Addr::unchecked(EXECUTOR_1), cosmos_msg.clone()) + app.execute(Addr::unchecked(exec.name), cosmos_msg.clone()) .unwrap(); let msg = ProxyQueryMsg::GetDataRequestExecutor { - executor: Addr::unchecked(EXECUTOR_1), + executor: exec.public_key.clone(), }; let res: GetDataRequestExecutorResponse = app .wrap() @@ -141,7 +143,7 @@ fn deposit_stake_withdraw() { let balance_after = app .wrap() - .query_balance(EXECUTOR_1, NATIVE_DENOM) + .query_balance(exec.name, NATIVE_DENOM) .unwrap() .amount .u128(); diff --git a/packages/integration-tests/src/utils.rs b/packages/integration-tests/src/utils.rs index 16c61633..416cbcd5 100644 --- a/packages/integration-tests/src/utils.rs +++ b/packages/integration-tests/src/utils.rs @@ -53,7 +53,7 @@ impl TestExecutor { hasher.finalize().into() } - pub fn sign(&mut self, msg: I) -> Signature + pub fn sign(&self, msg: I) -> Signature where I: IntoIterator>, { @@ -291,7 +291,7 @@ pub fn reveal_hash(reveal: &RevealBody, salt: Option<&'static str>) -> (Hash, Ve pub fn helper_reg_dr_executor( app: &mut App, proxy_contract: CwTemplateContract, - executor: &mut TestExecutor, + executor: &TestExecutor, memo: Option, ) -> Result { let contract_call_bytes = "register_data_request_executor".as_bytes().to_vec(); diff --git a/packages/proxy/src/msg.rs b/packages/proxy/src/msg.rs index 588076ed..b37cd005 100644 --- a/packages/proxy/src/msg.rs +++ b/packages/proxy/src/msg.rs @@ -114,7 +114,7 @@ pub enum ProxyQueryMsg { // Staking #[returns(GetDataRequestExecutorResponse)] - GetDataRequestExecutor { executor: Addr }, + GetDataRequestExecutor { executor: Secpk256k1PublicKey }, #[returns(IsDataRequestExecutorEligibleResponse)] IsDataRequestExecutorEligible { executor: Secpk256k1PublicKey }, #[returns(GetStakingConfigResponse)]