Skip to content

Commit

Permalink
Eliminate randomness in VmTester
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Oct 17, 2024
1 parent 3612a46 commit 2fd46ee
Show file tree
Hide file tree
Showing 28 changed files with 117 additions and 104 deletions.
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/testonly/block_tip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn execute_test<VM: TestedVm>(test_data: L1MessengerTestData) -> TestStatistics
let mut vm = VmTesterBuilder::new()
.with_storage(storage)
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.with_l1_batch_env(batch_env)
.build::<VM>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) fn test_bytecode_publishing<VM: TestedVm>() {
let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.build::<VM>();

let counter = read_test_contract();
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/testonly/circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
pub(crate) fn test_circuits<VM: TestedVm>() {
let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.with_bootloader_gas_limit(BATCH_COMPUTATIONAL_GAS_LIMIT)
.with_execution_mode(TxExecutionMode::VerifyExecute)
.build::<VM>();
Expand Down
6 changes: 3 additions & 3 deletions core/lib/multivm/src/versions/testonly/code_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub(crate) fn test_code_oracle<VM: TestedVm>() {
let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.with_custom_contracts(vec![ContractToDeploy::new(
precompile_contract_bytecode,
precompiles_contract_address,
Expand Down Expand Up @@ -128,7 +128,7 @@ pub(crate) fn test_code_oracle_big_bytecode<VM: TestedVm>() {
let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.with_custom_contracts(vec![ContractToDeploy::new(
precompile_contract_bytecode,
precompiles_contract_address,
Expand Down Expand Up @@ -190,7 +190,7 @@ pub(crate) fn test_refunds_in_code_oracle<VM: TestedVm>() {
for decommit in [false, true] {
let mut vm = VmTesterBuilder::new()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.with_custom_contracts(vec![ContractToDeploy::new(
precompile_contract_bytecode.clone(),
precompiles_contract_address,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/testonly/default_aa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) fn test_default_aa_interaction<VM: TestedVm>() {
let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.build::<VM>();

let counter = read_test_contract();
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/testonly/gas_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) fn test_tx_gas_limit_offset<VM: TestedVm>() {
let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.build::<VM>();

let gas_limit = 9999.into();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn execute_proxy_counter<VM: TestedVm>(
counter_address,
)])
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.build::<VM>();

let (proxy_counter_bytecode, proxy_counter_abi) = read_proxy_counter_contract();
Expand Down
7 changes: 3 additions & 4 deletions core/lib/multivm/src/versions/testonly/is_write_initial.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use zksync_test_account::{Account, TxType};
use zksync_test_account::TxType;
use zksync_types::get_nonce_key;

use super::{read_test_contract, tester::VmTesterBuilder, TestedVm};
Expand All @@ -8,13 +8,12 @@ pub(crate) fn test_is_write_initial_behaviour<VM: TestedVm>() {
// In this test, we check result of `is_write_initial` at different stages.
// The main idea is to check that `is_write_initial` storage uses the correct cache for initial_writes and doesn't
// messed up it with the repeated writes during the one batch execution.

let mut account = Account::random();
let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_rich_accounts(vec![account.clone()])
.with_rich_accounts(1)
.build::<VM>();
let account = &mut vm.rich_accounts[0];

let nonce_key = get_nonce_key(&account.address);
// Check that the next write to the nonce key will be initial.
Expand Down
4 changes: 2 additions & 2 deletions core/lib/multivm/src/versions/testonly/l1_tx_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) fn test_l1_tx_execution<VM: TestedVm>() {
.with_empty_in_memory_storage()
.with_base_system_smart_contracts(BASE_SYSTEM_CONTRACTS.clone())
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.build::<VM>();

let contract_code = read_test_contract();
Expand Down Expand Up @@ -143,7 +143,7 @@ pub(crate) fn test_l1_tx_execution_high_gas_limit<VM: TestedVm>() {
.with_empty_in_memory_storage()
.with_base_system_smart_contracts(BASE_SYSTEM_CONTRACTS.clone())
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.build::<VM>();

let account = &mut vm.rich_accounts[0];
Expand Down
10 changes: 5 additions & 5 deletions core/lib/multivm/src/versions/testonly/l2_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(crate) fn test_l2_block_initialization_timestamp<VM: TestedVm>() {
let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.build::<VM>();

// Override the timestamp of the current L2 block to be 0.
Expand Down Expand Up @@ -91,7 +91,7 @@ pub(crate) fn test_l2_block_initialization_number_non_zero<VM: TestedVm>() {
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_l1_batch_env(l1_batch)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.build::<VM>();

let l1_tx = get_l1_noop();
Expand Down Expand Up @@ -123,7 +123,7 @@ fn test_same_l2_block<VM: TestedVm>(
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_l1_batch_env(l1_batch)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.build::<VM>();

let l1_tx = get_l1_noop();
Expand Down Expand Up @@ -196,7 +196,7 @@ fn test_new_l2_block<VM: TestedVm>(
.with_empty_in_memory_storage()
.with_l1_batch_env(l1_batch)
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.build::<VM>();

let l1_tx = get_l1_noop();
Expand Down Expand Up @@ -295,7 +295,7 @@ fn test_first_in_batch<VM: TestedVm>(
.with_empty_in_memory_storage()
.with_l1_batch_env(l1_batch)
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.with_rich_accounts(1)
.build::<VM>();
let l1_tx = get_l1_noop();

Expand Down
26 changes: 19 additions & 7 deletions core/lib/multivm/src/versions/testonly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ use zksync_contracts::{
load_contract, read_bootloader_code, read_bytecode, read_zbin_bytecode, BaseSystemContracts,
SystemContractCode,
};
use zksync_test_account::Account;
use zksync_types::{
block::L2BlockHasher, fee_model::BatchFeeInput, get_code_key, get_is_account_key,
helpers::unix_timestamp_ms, utils::storage_key_for_eth_balance, Address, L1BatchNumber,
L2BlockNumber, L2ChainId, ProtocolVersionId, U256,
utils::storage_key_for_eth_balance, Address, L1BatchNumber, L2BlockNumber, L2ChainId,
ProtocolVersionId, U256,
};
use zksync_utils::{bytecode::hash_bytecode, bytes_to_be_words, u256_to_h256};
use zksync_vm_interface::{L1BatchEnv, L2BlockEnv, SystemEnv, TxExecutionMode};
Expand Down Expand Up @@ -145,7 +144,7 @@ pub(super) fn default_system_env() -> SystemEnv {
}

pub(super) fn default_l1_batch(number: L1BatchNumber) -> L1BatchEnv {
let timestamp = unix_timestamp_ms();
let timestamp = number.0.into();
L1BatchEnv {
previous_batch_hash: None,
number,
Expand All @@ -154,7 +153,7 @@ pub(super) fn default_l1_batch(number: L1BatchNumber) -> L1BatchEnv {
50_000_000_000, // 50 gwei
250_000_000, // 0.25 gwei
),
fee_account: Address::random(),
fee_account: Address::repeat_byte(1),
enforced_base_fee: None,
first_l2_block: L2BlockEnv {
number: 1,
Expand All @@ -165,8 +164,8 @@ pub(super) fn default_l1_batch(number: L1BatchNumber) -> L1BatchEnv {
}
}

pub(super) fn make_account_rich(storage: &mut InMemoryStorage, account: &Account) {
let key = storage_key_for_eth_balance(&account.address);
pub(super) fn make_address_rich(storage: &mut InMemoryStorage, address: Address) {
let key = storage_key_for_eth_balance(&address);
storage.set_value(key, u256_to_h256(U256::from(10_u64.pow(19))));
}

Expand All @@ -175,6 +174,7 @@ pub(super) struct ContractToDeploy {
bytecode: Vec<u8>,
address: Address,
is_account: bool,
is_funded: bool,
}

impl ContractToDeploy {
Expand All @@ -183,6 +183,7 @@ impl ContractToDeploy {
bytecode,
address,
is_account: false,
is_funded: false,
}
}

Expand All @@ -191,9 +192,16 @@ impl ContractToDeploy {
bytecode,
address,
is_account: true,
is_funded: false,
}
}

#[must_use]
pub fn funded(mut self) -> Self {
self.is_funded = true;
self
}

pub fn insert(&self, storage: &mut InMemoryStorage) {
let deployer_code_key = get_code_key(&self.address);
storage.set_value(deployer_code_key, hash_bytecode(&self.bytecode));
Expand All @@ -202,6 +210,10 @@ impl ContractToDeploy {
storage.set_value(is_account_key, u256_to_h256(1_u32.into()));
}
storage.store_factory_dep(hash_bytecode(&self.bytecode), self.bytecode.clone());

if self.is_funded {
make_address_rich(storage, self.address);
}
}

/// Inserts the contracts into the test environment, bypassing the deployer system contract.
Expand Down
34 changes: 18 additions & 16 deletions core/lib/multivm/src/versions/testonly/nonce_holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,24 @@ fn run_nonce_test(
}

pub(crate) fn test_nonce_holder<VM: TestedVm>() {
let mut account = Account::random();
let hex_addr = hex::encode(account.address.to_fixed_bytes());
let mut vm = VmTesterBuilder::new()
let builder = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_rich_accounts(1);
let account_address = builder.rich_account(0).address;
let mut vm = builder
.with_custom_contracts(vec![ContractToDeploy::account(
read_nonce_holder_tester(),
account.address,
account_address,
)])
.with_rich_accounts(vec![account.clone()])
.build::<VM>();
let account = &mut vm.rich_accounts[0];
let hex_addr = hex::encode(account.address.to_fixed_bytes());

// Test 1: trying to set value under non sequential nonce value.
run_nonce_test(
&mut vm.vm,
&mut account,
account,
1u32,
NonceHolderTestMode::SetValueUnderNonce,
Some("Error function_selector = 0x13595475, data = 0x13595475".to_string()),
Expand All @@ -98,7 +100,7 @@ pub(crate) fn test_nonce_holder<VM: TestedVm>() {
// Test 2: increase min nonce by 1 with sequential nonce ordering:
run_nonce_test(
&mut vm.vm,
&mut account,
account,
0u32,
NonceHolderTestMode::IncreaseMinNonceBy1,
None,
Expand All @@ -108,7 +110,7 @@ pub(crate) fn test_nonce_holder<VM: TestedVm>() {
// Test 3: correctly set value under nonce with sequential nonce ordering:
run_nonce_test(
&mut vm.vm,
&mut account,
account,
1u32,
NonceHolderTestMode::SetValueUnderNonce,
None,
Expand All @@ -118,7 +120,7 @@ pub(crate) fn test_nonce_holder<VM: TestedVm>() {
// Test 5: migrate to the arbitrary nonce ordering:
run_nonce_test(
&mut vm.vm,
&mut account,
account,
2u32,
NonceHolderTestMode::SwitchToArbitraryOrdering,
None,
Expand All @@ -128,7 +130,7 @@ pub(crate) fn test_nonce_holder<VM: TestedVm>() {
// Test 6: increase min nonce by 5
run_nonce_test(
&mut vm.vm,
&mut account,
account,
6u32,
NonceHolderTestMode::IncreaseMinNonceBy5,
None,
Expand All @@ -139,7 +141,7 @@ pub(crate) fn test_nonce_holder<VM: TestedVm>() {
// tx with nonce 10 should not be allowed
run_nonce_test(
&mut vm.vm,
&mut account,
account,
10u32,
NonceHolderTestMode::IncreaseMinNonceBy5,
Some(format!("Error function_selector = 0xe90aded4, data = 0xe90aded4000000000000000000000000{hex_addr}000000000000000000000000000000000000000000000000000000000000000a")),
Expand All @@ -149,7 +151,7 @@ pub(crate) fn test_nonce_holder<VM: TestedVm>() {
// Test 8: we should be able to use nonce 13
run_nonce_test(
&mut vm.vm,
&mut account,
account,
13u32,
NonceHolderTestMode::SetValueUnderNonce,
None,
Expand All @@ -159,7 +161,7 @@ pub(crate) fn test_nonce_holder<VM: TestedVm>() {
// Test 9: we should not be able to reuse nonce 13
run_nonce_test(
&mut vm.vm,
&mut account,
account,
13u32,
NonceHolderTestMode::IncreaseMinNonceBy5,
Some(format!("Error function_selector = 0xe90aded4, data = 0xe90aded4000000000000000000000000{hex_addr}000000000000000000000000000000000000000000000000000000000000000d")),
Expand All @@ -169,7 +171,7 @@ pub(crate) fn test_nonce_holder<VM: TestedVm>() {
// Test 10: we should be able to simply use nonce 14, while bumping the minimal nonce by 5
run_nonce_test(
&mut vm.vm,
&mut account,
account,
14u32,
NonceHolderTestMode::IncreaseMinNonceBy5,
None,
Expand All @@ -179,7 +181,7 @@ pub(crate) fn test_nonce_holder<VM: TestedVm>() {
// Test 11: Do not allow bumping nonce by too much
run_nonce_test(
&mut vm.vm,
&mut account,
account,
16u32,
NonceHolderTestMode::IncreaseMinNonceTooMuch,
Some("Error function_selector = 0x45ac24a6, data = 0x45ac24a600000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000040000000000000000000000".to_string()),
Expand All @@ -189,7 +191,7 @@ pub(crate) fn test_nonce_holder<VM: TestedVm>() {
// Test 12: Do not allow not setting a nonce as used
run_nonce_test(
&mut vm.vm,
&mut account,
account,
16u32,
NonceHolderTestMode::LeaveNonceUnused,
Some(format!("Error function_selector = 0x1f2f8478, data = 0x1f2f8478000000000000000000000000{hex_addr}0000000000000000000000000000000000000000000000000000000000000010")),
Expand Down
Loading

0 comments on commit 2fd46ee

Please sign in to comment.