Skip to content

Commit

Permalink
Add more tests and debug info to the state crate
Browse files Browse the repository at this point in the history
  • Loading branch information
popzxc committed Jan 27, 2021
1 parent 9349df9 commit 1d5bd56
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 6 deletions.
13 changes: 9 additions & 4 deletions core/lib/state/src/handler/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,15 @@ impl ZkSyncState {
"TransferToNew to account id is bigger than max supported"
);

assert!(
self.get_account(op.to).is_none(),
"Transfer to new account exists"
);
if let Some(account) = self.get_account(op.to) {
vlog::error!(
"Attempt to execute transfer to new account for an existing account. Account: {:#?}; Transfer: {:#?}",
account,
op
);
panic!("Transfer to new account exists");
}

let mut to_account = {
let (acc, upd) = Account::create_account(op.to, op.tx.to);
updates.extend(upd.into_iter());
Expand Down
57 changes: 56 additions & 1 deletion core/lib/state/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,10 @@ mod tests {
use super::*;
use crate::tests::{AccountState::*, PlasmaTestBuilder};
use zksync_crypto::rand::{Rng, SeedableRng, XorShiftRng};
use zksync_types::{tx::Withdraw, Nonce};
use zksync_types::{
tx::{Transfer, Withdraw},
Nonce,
};

/// Checks if execute_txs_batch fails if it doesn't have enough balance.
#[test]
Expand Down Expand Up @@ -465,6 +468,58 @@ mod tests {
);
}

#[test]
fn execute_txs_batch_fail_transfers() {
let token_id = TokenId(0);
let amount = BigUint::from(100u32);
let fee = BigUint::from(10u32);

let mut tb = PlasmaTestBuilder::new();

let (account_id, account, sk) = tb.add_account(Unlocked);
tb.set_balance(account_id, token_id, &amount + &fee);

let new_address_1 = Address::random();
let new_address_2 = Address::random();

let transfer_1 = Transfer::new_signed(
account_id,
account.address,
new_address_1,
token_id,
amount.clone(),
fee.clone(),
account.nonce,
&sk,
)
.unwrap();

let transfer_2 = Transfer::new_signed(
account_id,
account.address,
new_address_2,
token_id,
amount,
fee,
account.nonce + 1,
&sk,
)
.unwrap();

let signed_zk_sync_tx1 = SignedZkSyncTx {
tx: ZkSyncTx::Transfer(Box::new(transfer_1)),
eth_sign_data: None,
};
let signed_zk_sync_tx2 = SignedZkSyncTx {
tx: ZkSyncTx::Transfer(Box::new(transfer_2)),
eth_sign_data: None,
};
tb.test_txs_batch_fail(
&[signed_zk_sync_tx1, signed_zk_sync_tx2],
"Batch execution failed, since tx #2 of batch failed with a reason: Not enough balance",
);
}

/// Checks if execute_txs_batch executes normally with valid operations.
#[test]
fn execute_txs_batch_success() {
Expand Down
156 changes: 155 additions & 1 deletion core/lib/state/src/tests/operations/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::tests::{AccountState::*, PlasmaTestBuilder};
use num::{BigUint, Zero};
use web3::types::H160;
use zksync_types::{AccountId, AccountUpdate, Nonce, TokenId, Transfer};
use zksync_types::{AccountId, AccountUpdate, Nonce, SignedZkSyncTx, TokenId, Transfer, ZkSyncTx};

/// Check Transfer operation to existing account
#[test]
Expand Down Expand Up @@ -138,6 +138,36 @@ fn to_new() {
)
}

/// Check Transfer operation to new account
#[test]
fn to_new_failure() {
let token_id = TokenId(0);
let amount = BigUint::from(100u32);
let fee = BigUint::from(10u32);

let mut tb = PlasmaTestBuilder::new();

let (account_id, account, sk) = tb.add_account(Unlocked);
tb.set_balance(account_id, token_id, &amount + &fee);
let too_big_amount = amount * 2u64;

let new_address = H160::random();

let transfer = Transfer::new_signed(
account_id,
account.address,
new_address,
token_id,
too_big_amount,
fee,
account.nonce,
&sk,
)
.unwrap();

tb.test_tx_fail(transfer.into(), "Not enough balance")
}

/// Check Transfer operation from account to itself
#[test]
fn to_self() {
Expand Down Expand Up @@ -230,3 +260,127 @@ fn invalid_account_id() {

tb.test_tx_fail(transfer.into(), "Transfer account id is incorrect")
}

#[test]
fn execute_txs_batch_success_transfers() {
let token_id = TokenId(0);
let amount = BigUint::from(100u32);
let fee = BigUint::from(10u32);
let small_amount = BigUint::from(10u32);

let balance_from = &fee + &amount;
let balance_from_after_first = &balance_from - &small_amount - &fee;
let balance_from_final = &balance_from_after_first - &small_amount - &fee;

let balance_to = 0u64.into();
let balance_to_after_first = &balance_to + &small_amount;
let balance_to_final = &balance_to_after_first + &small_amount;

let mut tb = PlasmaTestBuilder::new();

let (account_id, account, sk) = tb.add_account(Unlocked);
tb.set_balance(account_id, token_id, &amount + &fee);

let new_address = H160::random();

let transfer_1 = Transfer::new_signed(
account_id,
account.address,
new_address,
token_id,
small_amount.clone(),
fee.clone(),
account.nonce,
&sk,
)
.unwrap();

let transfer_2 = Transfer::new_signed(
account_id,
account.address,
new_address,
token_id,
small_amount,
fee.clone(),
account.nonce + 1,
&sk,
)
.unwrap();

let transfer_bad = Transfer::new_signed(
account_id,
account.address,
new_address,
token_id,
amount * 2u64,
fee,
account.nonce + 1,
&sk,
)
.unwrap();

let signed_zk_sync_tx1 = SignedZkSyncTx {
tx: ZkSyncTx::Transfer(Box::new(transfer_1)),
eth_sign_data: None,
};
let signed_zk_sync_tx2 = SignedZkSyncTx {
tx: ZkSyncTx::Transfer(Box::new(transfer_2)),
eth_sign_data: None,
};
let signed_zk_sync_tx_bad = SignedZkSyncTx {
tx: ZkSyncTx::Transfer(Box::new(transfer_bad)),
eth_sign_data: None,
};

tb.test_txs_batch_fail(
&[signed_zk_sync_tx1.clone(), signed_zk_sync_tx_bad],
"Batch execution failed, since tx #2 of batch failed with a reason: Not enough balance",
);

let new_id = tb.state.get_free_account_id();

tb.test_txs_batch_success(
&[signed_zk_sync_tx1, signed_zk_sync_tx2],
&[
(
new_id,
AccountUpdate::Create {
address: new_address,
nonce: Nonce(0),
},
),
(
account_id,
AccountUpdate::UpdateBalance {
old_nonce: account.nonce,
new_nonce: account.nonce + 1,
balance_update: (token_id, balance_from, balance_from_after_first.clone()),
},
),
(
new_id,
AccountUpdate::UpdateBalance {
old_nonce: Nonce(0),
new_nonce: Nonce(0),
balance_update: (token_id, balance_to, balance_to_after_first.clone()),
},
),
(
account_id,
AccountUpdate::UpdateBalance {
old_nonce: account.nonce + 1,
new_nonce: account.nonce + 2,
balance_update: (token_id, balance_from_after_first, balance_from_final),
},
),
(
new_id,
AccountUpdate::UpdateBalance {
old_nonce: Nonce(0),
new_nonce: Nonce(0),
balance_update: (token_id, balance_to_after_first, balance_to_final),
},
),
],
);
}

0 comments on commit 1d5bd56

Please sign in to comment.