Skip to content

Commit

Permalink
Add a failure test
Browse files Browse the repository at this point in the history
  • Loading branch information
ly0va committed May 31, 2021
1 parent a10361a commit 4a42f79
Showing 1 changed file with 122 additions and 55 deletions.
177 changes: 122 additions & 55 deletions core/lib/state/src/tests/operations/swap.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::tests::{AccountState::*, PlasmaTestBuilder};
use num::{BigUint, Zero};
use web3::types::H160;
use zksync_crypto::PrivateKey;
use zksync_types::{
Account, AccountId, AccountUpdate, Nonce, Order, SignedZkSyncTx, Swap, TokenId, ZkSyncTx,
};
use zksync_types::{Account, AccountId, AccountUpdate, Order, Swap, TokenId};

type TestAccount = (AccountId, Account, PrivateKey);

Expand All @@ -23,8 +20,15 @@ struct TestSwap {
test_accounts: Vec<TestAccount>,
}

enum TestResult {
Success(Vec<(u64, u64)>),
Failure(&'static str),
}

use TestResult::*;

impl TestSwap {
fn test_success(&self, mut tb: PlasmaTestBuilder) {
fn test_success(&self, mut tb: PlasmaTestBuilder, outcome: TestResult) {
let (account_0_id, account_0, account_0_sk) = &self.test_accounts[self.accounts.0];
let (account_1_id, account_1, account_1_sk) = &self.test_accounts[self.accounts.1];
let (recipient_0_id, recipient_0, _) = &self.test_accounts[self.recipients.0];
Expand Down Expand Up @@ -56,7 +60,7 @@ impl TestSwap {

tb.set_balance(*account_0_id, token_0, balances.0.clone());
tb.set_balance(*account_1_id, token_1, balances.1.clone());
tb.set_balance(*submitter_id, fee_token, balances.2.clone());
tb.set_balance(*submitter_id, fee_token, balances.2);

let order_0 = Order::new_signed(
*account_0_id,
Expand All @@ -68,7 +72,7 @@ impl TestSwap {
BigUint::from(self.first_price.0),
BigUint::from(self.first_price.1),
),
amount_0.clone(),
amount_0,
Default::default(),
&&account_0_sk,
)
Expand All @@ -84,7 +88,7 @@ impl TestSwap {
BigUint::from(self.second_price.0),
BigUint::from(self.second_price.1),
),
amount_1.clone(),
amount_1,
Default::default(),
&account_1_sk,
)
Expand All @@ -96,57 +100,90 @@ impl TestSwap {
submitter.nonce,
(order_0, order_1),
(BigUint::from(self.amounts.0), BigUint::from(self.amounts.1)),
fee.clone(),
fee,
fee_token,
&submitter_sk,
)
.expect("swap creation failed");

tb.test_tx_success(
swap.into(),
&[
(
*account_0_id,
AccountUpdate::UpdateBalance {
old_nonce: account_0.nonce,
new_nonce: account_0.nonce + 1,
balance_update: (token_0, balances.0.clone(), balances.0 - &amount_0),
},
),
(
*recipient_1_id,
AccountUpdate::UpdateBalance {
old_nonce: recipient_1.nonce,
new_nonce: recipient_1.nonce,
balance_update: (token_0, BigUint::zero(), amount_0),
},
),
(
*account_1_id,
AccountUpdate::UpdateBalance {
old_nonce: account_1.nonce,
new_nonce: account_1.nonce + 1,
balance_update: (token_1, balances.1.clone(), balances.1 - &amount_1),
},
),
(
*recipient_0_id,
AccountUpdate::UpdateBalance {
old_nonce: recipient_0.nonce,
new_nonce: recipient_0.nonce,
balance_update: (token_1, BigUint::zero(), amount_1),
},
),
(
*submitter_id,
AccountUpdate::UpdateBalance {
old_nonce: submitter.nonce,
new_nonce: submitter.nonce + 1,
balance_update: (fee_token, balances.2.clone(), balances.2 - &fee),
},
),
],
)
match outcome {
Success(balance_changes) => {
let balance_changes: Vec<_> = balance_changes
.iter()
.map(|(a, b)| (BigUint::from(*a), BigUint::from(*b)))
.collect();

tb.test_tx_success(
swap.into(),
&[
(
*account_0_id,
AccountUpdate::UpdateBalance {
old_nonce: account_0.nonce,
new_nonce: account_0.nonce + 1,
balance_update: (
token_0,
balance_changes[0].0.clone(),
balance_changes[0].1.clone(),
),
},
),
(
*recipient_1_id,
AccountUpdate::UpdateBalance {
old_nonce: recipient_1.nonce,
new_nonce: recipient_1.nonce,
balance_update: (
token_0,
balance_changes[1].0.clone(),
balance_changes[1].1.clone(),
),
},
),
(
*account_1_id,
AccountUpdate::UpdateBalance {
old_nonce: account_1.nonce,
new_nonce: account_1.nonce + 1,
balance_update: (
token_1,
balance_changes[2].0.clone(),
balance_changes[2].1.clone(),
),
},
),
(
*recipient_0_id,
AccountUpdate::UpdateBalance {
old_nonce: recipient_0.nonce,
new_nonce: recipient_0.nonce,
balance_update: (
token_1,
balance_changes[3].0.clone(),
balance_changes[3].1.clone(),
),
},
),
(
*submitter_id,
AccountUpdate::UpdateBalance {
old_nonce: submitter.nonce,
new_nonce: submitter.nonce + 1,
balance_update: (
fee_token,
balance_changes[4].0.clone(),
balance_changes[4].1.clone(),
),
},
),
],
);
}

Failure(message) => {
tb.test_tx_fail(swap.into(), message);
}
}
}
}

Expand Down Expand Up @@ -175,5 +212,35 @@ fn swap_success() {
],
};

test_swap.test_success(tb);
test_swap.test_success(
tb,
Success(vec![(100, 50), (0, 50), (200, 100), (0, 100), (50, 25)]),
);
}

#[test]
fn self_swap_fail() {
let mut tb = PlasmaTestBuilder::new();

let test_swap = TestSwap {
accounts: (0, 0),
recipients: (1, 2),
submitter: 3,
tokens: (18, 19),
fee_token: 0,
amounts: (50, 100),
fee: 25,
balances: (100, 200, 50),
first_price: (1, 2),
second_price: (2, 1),
is_limit_order: (false, false),
test_accounts: vec![
tb.add_account(Unlocked),
tb.add_account(Unlocked),
tb.add_account(Unlocked),
tb.add_account(Unlocked),
],
};

test_swap.test_success(tb, Failure("Self-swap is not allowed"));
}

0 comments on commit 4a42f79

Please sign in to comment.