Skip to content

Commit

Permalink
Add custom account and enforced deployer account address
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosNicolau committed Sep 2, 2024
1 parent d6ba396 commit b23b3ff
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use zksync_types::Transaction;

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct ParallelTransaction {
pub tx: Transaction,
pub refund: u64,
Expand Down
53 changes: 22 additions & 31 deletions core/lib/multivm/src/versions/era_vm/tests/parallel_execution.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use zksync_types::Transaction;
use zksync_test_account::Account;
use zksync_types::{K256PrivateKey, Transaction};

use super::tester::VmTester;
use crate::{
Expand All @@ -7,23 +8,17 @@ use crate::{
};

fn prepare_test(is_parallel: bool) -> (VmTester, [Transaction; 3]) {
let bytes = [1; 32];
let account = Account::new(K256PrivateKey::from_bytes(bytes.into()).unwrap());
dbg!(&account);
let mut vm_tester = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_deployer()
.with_random_rich_accounts(1)
.with_custom_account(account)
.build();

if is_parallel {
let mut vm_tester_2 = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_deployer()
.with_random_rich_accounts(1)
.build();
vm_tester_2.deploy_test_contract();

vm_tester.test_contract = vm_tester_2.test_contract;

vm_tester.deploy_test_contract_parallel()
vm_tester.deploy_test_contract();
} else {
vm_tester.deploy_test_contract();
}
Expand Down Expand Up @@ -59,32 +54,28 @@ fn prepare_test(is_parallel: bool) -> (VmTester, [Transaction; 3]) {

#[test]
fn parallel_execution() {
// let normal_execution = {
// let (mut vm, txs) = prepare_test(false);
// let vm = &mut vm.vm;
// for tx in &txs {
// vm.push_transaction_inner(tx.clone(), 0, true);
// }
// vm.execute(VmExecutionMode::Batch)
// };
let normal_execution = {
let (mut vm, txs) = prepare_test(false);
let vm = &mut vm.vm;
for tx in &txs {
vm.push_transaction_inner(tx.clone(), 0, true);
}
vm.execute(VmExecutionMode::Batch)
};

let parallel_execution = {
let (mut vm, txs) = prepare_test(true);
let vm = &mut vm.vm;
for tx in txs {
vm.push_parallel_transaction(tx, 0, true);
}
vm.execute_parallel()
vm.execute_parallel(VmExecutionMode::Batch)
};
println!("RESULT {:?}", parallel_execution.result);

// we don't assert if statistics are equal since that would require
// sharing the gas in parallel,
assert!(1 == 2);
// assert_eq!(
// normal_execution.logs.storage_logs,
// parallel_execution.logs.storage_logs
// );
// assert_eq!(normal_execution.result, parallel_execution.result);
// assert_eq!(normal_execution.refunds, parallel_execution.refunds);
assert_eq!(
normal_execution.logs.storage_logs,
parallel_execution.logs.storage_logs
);
assert_eq!(normal_execution.result, parallel_execution.result);
assert_eq!(normal_execution.refunds, parallel_execution.refunds);
}
41 changes: 26 additions & 15 deletions core/lib/multivm/src/versions/era_vm/tests/tester/vm_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use zksync_types::{
block::L2BlockHasher,
fee_model::BatchFeeInput,
get_code_key, get_is_account_key,
helpers::unix_timestamp_ms,
utils::{deployed_address_create, storage_key_for_eth_balance},
AccountTreeId, Address, L1BatchNumber, L2BlockNumber, L2ChainId, Nonce, ProtocolVersionId,
StorageKey, H160, U256,
AccountTreeId, Address, K256PrivateKey, L1BatchNumber, L2BlockNumber, L2ChainId, Nonce,
ProtocolVersionId, StorageKey, U256,
};
use zksync_utils::{bytecode::hash_bytecode, u256_to_h256};

Expand Down Expand Up @@ -53,16 +52,21 @@ impl VmTester {
self.test_contract = Some(deployed_address);
}

pub(crate) fn deploy_test_contract_parallel(&mut self) {
let contract = read_test_contract();
let tx = self
.deployer
.as_mut()
.expect("You have to initialize builder with deployer")
.get_deploy_tx(&contract, None, TxType::L2)
.tx;
self.vm.push_parallel_transaction(tx, 0, true);
}
// pub(crate) fn deploy_test_contract_parallel(&mut self) {
// let contract = read_test_contract();
// let tx = self
// .deployer
// .as_mut()
// .expect("You have to initialize builder with deployer")
// .get_deploy_tx(&contract, None, TxType::L2)
// .tx;
// let nonce = tx.nonce().unwrap().0.into();
// self.vm.push_parallel_transaction(tx, 0, true);
// self.vm.execute_parallel(VmExecutionMode::OneTx);
// let deployed_address =
// deployed_address_create(self.deployer.as_ref().unwrap().address, nonce);
// self.test_contract = Some(deployed_address);
// }

pub(crate) fn reset_with_empty_storage(&mut self) {
self.storage = Rc::new(RefCell::new(get_empty_storage()));
Expand Down Expand Up @@ -217,13 +221,20 @@ impl VmTesterBuilder {
self
}

pub fn with_custom_account(mut self, account: Account) -> Self {
self.rich_accounts.push(account);
self
}

pub(crate) fn with_rich_accounts(mut self, accounts: Vec<Account>) -> Self {
self.rich_accounts.extend(accounts);
self
}

pub(crate) fn with_deployer(mut self) -> Self {
let deployer = Account::random();
let bytes = [2; 32];
let account = Account::new(K256PrivateKey::from_bytes(bytes.into()).unwrap());
let deployer = account;
self.deployer = Some(deployer);
self
}
Expand Down Expand Up @@ -264,7 +275,7 @@ impl VmTesterBuilder {
}

pub(crate) fn default_l1_batch(number: L1BatchNumber) -> L1BatchEnv {
let timestamp = unix_timestamp_ms();
let timestamp = 0xabcd;
L1BatchEnv {
previous_batch_hash: None,
number,
Expand Down

0 comments on commit b23b3ff

Please sign in to comment.