Skip to content

Commit

Permalink
Change admin account for pausing eth connector
Browse files Browse the repository at this point in the history
Pause flags are now operated by an owner account.
  • Loading branch information
hskang9 committed Feb 3, 2023
1 parent 91441f1 commit 8d58e13
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions engine-tests/src/tests/eth_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,31 +1053,34 @@ fn create_user_account(master_account: &UserAccount) -> UserAccount {
)
}

// admin test
#[test]
fn test_admin_controlled_only_admin_can_pause() {
let (master_account, contract) = init(CUSTODIAN_ADDRESS);
let user_account = create_user_account(&master_account);
let owner_account = create_user_account(&master_account);

// Try to pause from the user - should fail
let res = call_set_paused_flags(&user_account, CONTRACT_ACC, PAUSE_DEPOSIT);
// Try to pause from the admin - should fail
let res = call_set_paused_flags(&contract, CONTRACT_ACC, PAUSE_DEPOSIT);
let promises = res.promise_results();
let p = promises[1].clone();
match p.unwrap().outcome().clone().status {
ExecutionStatus::Failure(_) => {}
_ => panic!("Expected failure as only admin can pause, but user successfully paused"),
}

// Try to pause from the admin - should succeed
let res = call_set_paused_flags(&contract, CONTRACT_ACC, PAUSE_DEPOSIT);
// Try to pause from the owner - should succeed
let res = call_set_paused_flags(&owner_account, CONTRACT_ACC, PAUSE_DEPOSIT);
res.assert_success();
}

// admin test
#[test]
fn test_admin_controlled_admin_can_peform_actions_when_paused() {
let (_master_account, contract) = init(CUSTODIAN_ADDRESS);
let (master_account, contract) = init(CUSTODIAN_ADDRESS);
let owner_account = create_user_account(&master_account);

// 1st deposit call when unpaused - should succeed
let promises = call_deposit_with_proof(&contract, CONTRACT_ACC, PROOF_DATA_NEAR);
let promises = call_deposit_with_proof(&owner_account, CONTRACT_ACC, PROOF_DATA_NEAR);
for p in promises.iter() {
assert!(p.is_some());
let p = p.as_ref().unwrap();
Expand Down Expand Up @@ -1109,21 +1112,21 @@ fn test_admin_controlled_admin_can_peform_actions_when_paused() {
}

// Pause deposit
let res = call_set_paused_flags(&contract, CONTRACT_ACC, PAUSE_DEPOSIT);
let res = call_set_paused_flags(&owner_account, CONTRACT_ACC, PAUSE_DEPOSIT);
res.assert_success();

// 2nd deposit call when paused, but the admin is calling it - should succeed
// NB: We can use `PROOF_DATA_ETH` this will be just a different proof but the same deposit
// method which should be paused
let promises = call_deposit_with_proof(&contract, CONTRACT_ACC, PROOF_DATA_ETH);
let promises = call_deposit_with_proof(&owner_account, CONTRACT_ACC, PROOF_DATA_ETH);
for p in promises.iter() {
assert!(p.is_some());
let p = p.as_ref().unwrap();
p.assert_success()
}

// Pause withdraw
let res = call_set_paused_flags(&contract, CONTRACT_ACC, PAUSE_WITHDRAW);
let res = call_set_paused_flags(&owner_account, CONTRACT_ACC, PAUSE_WITHDRAW);
res.assert_success();

// 2nd withdraw call when paused, but the admin is calling it - should succeed
Expand All @@ -1148,27 +1151,28 @@ fn test_admin_controlled_admin_can_peform_actions_when_paused() {
}
}

// admin test
#[test]
fn test_deposit_pausability() {
let (master_account, contract) = init(CUSTODIAN_ADDRESS);
let user_account = create_user_account(&master_account);
let (master_account, _contract) = init(CUSTODIAN_ADDRESS);
let owner_account = create_user_account(&master_account);

// 1st deposit call - should succeed
let promises = call_deposit_with_proof(&user_account, CONTRACT_ACC, PROOF_DATA_NEAR);
let promises = call_deposit_with_proof(&owner_account, CONTRACT_ACC, PROOF_DATA_NEAR);
for p in promises.iter() {
assert!(p.is_some());
let p = p.as_ref().unwrap();
p.assert_success()
}

// Pause deposit
let res = call_set_paused_flags(&contract, CONTRACT_ACC, PAUSE_DEPOSIT);
let res = call_set_paused_flags(&owner_account, CONTRACT_ACC, PAUSE_DEPOSIT);
res.assert_success();

// 2nd deposit call - should fail
// NB: We can use `PROOF_DATA_ETH` this will be just a different proof but the same deposit
// method which should be paused
let promises = call_deposit_with_proof(&user_account, CONTRACT_ACC, PROOF_DATA_ETH);
let promises = call_deposit_with_proof(&owner_account, CONTRACT_ACC, PROOF_DATA_ETH);
let num_promises = promises.len();
let p = promises[num_promises - 2].clone();
assert_execution_status_failure(
Expand All @@ -1178,29 +1182,30 @@ fn test_deposit_pausability() {
);

// Unpause all
let res = call_set_paused_flags(&contract, CONTRACT_ACC, UNPAUSE_ALL);
let res = call_set_paused_flags(&owner_account, CONTRACT_ACC, UNPAUSE_ALL);
res.assert_success();

// 3rd deposit call - should succeed
let promises = call_deposit_with_proof(&user_account, CONTRACT_ACC, PROOF_DATA_ETH);
let promises = call_deposit_with_proof(&owner_account, CONTRACT_ACC, PROOF_DATA_ETH);
for p in promises.iter() {
assert!(p.is_some());
let p = p.as_ref().unwrap();
p.assert_success()
}
}

// admin test
#[test]
fn test_withdraw_from_near_pausability() {
let (master_account, contract) = init(CUSTODIAN_ADDRESS);
let user_account = create_user_account(&master_account);
let owner_account = create_user_account(&master_account);

call_deposit_eth_to_near(&contract, CONTRACT_ACC);

let withdraw_amount = NEP141Wei::new(100);
let recipient_addr = validate_eth_address(RECIPIENT_ETH_ADDRESS);
// 1st withdraw - should succeed
let res = user_account.call(
let res = owner_account.call(
CONTRACT_ACC.parse().unwrap(),
"withdraw",
&WithdrawCallArgs {
Expand All @@ -1222,11 +1227,11 @@ fn test_withdraw_from_near_pausability() {
}

// Pause withdraw
let res = call_set_paused_flags(&contract, CONTRACT_ACC, PAUSE_WITHDRAW);
let res = call_set_paused_flags(&owner_account, CONTRACT_ACC, PAUSE_WITHDRAW);
res.assert_success();

// 2nd withdraw - should fail
let res = user_account.call(
let res = owner_account.call(
CONTRACT_ACC.parse().unwrap(),
"withdraw",
&WithdrawCallArgs {
Expand All @@ -1247,10 +1252,10 @@ fn test_withdraw_from_near_pausability() {
);

// Unpause all
let res = call_set_paused_flags(&contract, CONTRACT_ACC, UNPAUSE_ALL);
let res = call_set_paused_flags(&owner_account, CONTRACT_ACC, UNPAUSE_ALL);
res.assert_success();

let res = user_account.call(
let res = owner_account.call(
CONTRACT_ACC.parse().unwrap(),
"withdraw",
&WithdrawCallArgs {
Expand Down

0 comments on commit 8d58e13

Please sign in to comment.