From 9e21be7dd10c6a86bd51795c7aa1d4521cd3ba14 Mon Sep 17 00:00:00 2001 From: Alex Ostrovski Date: Thu, 17 Oct 2024 13:50:46 +0300 Subject: [PATCH] Eliminate remaining randomness --- .../lib/multivm/src/versions/testonly/block_tip.rs | 6 +++--- core/lib/multivm/src/versions/testonly/circuits.rs | 2 +- .../multivm/src/versions/testonly/code_oracle.rs | 6 +++--- .../src/versions/testonly/get_used_contracts.rs | 6 ++++-- .../lib/multivm/src/versions/testonly/l2_blocks.rs | 10 +++++----- .../multivm/src/versions/testonly/precompiles.rs | 4 ++-- core/lib/multivm/src/versions/testonly/refunds.rs | 2 +- core/lib/multivm/src/versions/testonly/shadow.rs | 4 ++-- core/lib/multivm/src/versions/testonly/storage.rs | 2 +- .../multivm/src/versions/testonly/tester/mod.rs | 7 ++----- .../versions/testonly/tracing_execution_error.rs | 4 ++-- core/lib/multivm/src/versions/testonly/transfer.rs | 8 ++++---- core/lib/multivm/src/versions/testonly/upgrade.rs | 14 +++++++------- core/tests/test_account/src/lib.rs | 6 ++++++ 14 files changed, 43 insertions(+), 38 deletions(-) diff --git a/core/lib/multivm/src/versions/testonly/block_tip.rs b/core/lib/multivm/src/versions/testonly/block_tip.rs index d65be589cc6..7700f347ca6 100644 --- a/core/lib/multivm/src/versions/testonly/block_tip.rs +++ b/core/lib/multivm/src/versions/testonly/block_tip.rs @@ -44,7 +44,7 @@ fn populate_mimic_calls(data: L1MessengerTestData) -> Vec> { let complex_upgrade = get_complex_upgrade_abi(); let l1_messenger = load_sys_contract("L1Messenger"); - let logs_mimic_calls = (0..data.l2_to_l1_logs).map(|_| MimicCallInfo { + let logs_mimic_calls = (0..data.l2_to_l1_logs).map(|i| MimicCallInfo { to: L1_MESSENGER_ADDRESS, who_to_mimic: KNOWN_CODES_STORAGE_ADDRESS, data: l1_messenger @@ -52,8 +52,8 @@ fn populate_mimic_calls(data: L1MessengerTestData) -> Vec> { .unwrap() .encode_input(&[ Token::Bool(false), - Token::FixedBytes(H256::random().0.to_vec()), - Token::FixedBytes(H256::random().0.to_vec()), + Token::FixedBytes(H256::from_low_u64_be(2 * i as u64).0.to_vec()), + Token::FixedBytes(H256::from_low_u64_be(2 * i as u64 + 1).0.to_vec()), ]) .unwrap(), }); diff --git a/core/lib/multivm/src/versions/testonly/circuits.rs b/core/lib/multivm/src/versions/testonly/circuits.rs index cf70fe1ba2c..9503efe9208 100644 --- a/core/lib/multivm/src/versions/testonly/circuits.rs +++ b/core/lib/multivm/src/versions/testonly/circuits.rs @@ -20,7 +20,7 @@ pub(crate) fn test_circuits() { let account = &mut vm.rich_accounts[0]; let tx = account.get_l2_tx_for_execute( Execute { - contract_address: Some(Address::random()), + contract_address: Some(Address::repeat_byte(1)), calldata: Vec::new(), value: U256::from(1u8), factory_deps: vec![], diff --git a/core/lib/multivm/src/versions/testonly/code_oracle.rs b/core/lib/multivm/src/versions/testonly/code_oracle.rs index a8391feda25..b786539329b 100644 --- a/core/lib/multivm/src/versions/testonly/code_oracle.rs +++ b/core/lib/multivm/src/versions/testonly/code_oracle.rs @@ -19,7 +19,7 @@ fn generate_large_bytecode() -> Vec { } pub(crate) fn test_code_oracle() { - let precompiles_contract_address = Address::random(); + let precompiles_contract_address = Address::repeat_byte(1); let precompile_contract_bytecode = read_precompiles_contract(); // Filling the zkevm bytecode @@ -110,7 +110,7 @@ fn find_code_oracle_cost_log( } pub(crate) fn test_code_oracle_big_bytecode() { - let precompiles_contract_address = Address::random(); + let precompiles_contract_address = Address::repeat_byte(1); let precompile_contract_bytecode = read_precompiles_contract(); let big_zkevm_bytecode = generate_large_bytecode(); @@ -168,7 +168,7 @@ pub(crate) fn test_code_oracle_big_bytecode() { } pub(crate) fn test_refunds_in_code_oracle() { - let precompiles_contract_address = Address::random(); + let precompiles_contract_address = Address::repeat_byte(1); let precompile_contract_bytecode = read_precompiles_contract(); let normal_zkevm_bytecode = read_test_contract(); diff --git a/core/lib/multivm/src/versions/testonly/get_used_contracts.rs b/core/lib/multivm/src/versions/testonly/get_used_contracts.rs index faaf507fcf0..fbad94a0eee 100644 --- a/core/lib/multivm/src/versions/testonly/get_used_contracts.rs +++ b/core/lib/multivm/src/versions/testonly/get_used_contracts.rs @@ -24,6 +24,7 @@ pub(crate) fn test_get_used_contracts() { let mut vm = VmTesterBuilder::new() .with_empty_in_memory_storage() .with_execution_mode(TxExecutionMode::VerifyExecute) + .with_rich_accounts(1) .build::(); assert!(known_bytecodes_without_base_system_contracts(&vm.vm).is_empty()); @@ -31,7 +32,7 @@ pub(crate) fn test_get_used_contracts() { // create and push and execute some not-empty factory deps transaction with success status // to check that `get_decommitted_hashes()` updates let contract_code = read_test_contract(); - let mut account = Account::random(); + let account = &mut vm.rich_accounts[0]; let tx = account.get_deploy_tx(&contract_code, None, TxType::L1 { serial_id: 0 }); vm.vm.push_transaction(tx.tx.clone()); let result = vm.vm.execute(VmExecutionMode::OneTx); @@ -58,7 +59,8 @@ pub(crate) fn test_get_used_contracts() { .take(calldata.len() * 1024) .cloned() .collect(); - let account2 = Account::random(); + let account2 = Account::from_seed(u32::MAX); + assert_ne!(account2.address, account.address); let tx2 = account2.get_l1_tx( Execute { contract_address: Some(CONTRACT_DEPLOYER_ADDRESS), diff --git a/core/lib/multivm/src/versions/testonly/l2_blocks.rs b/core/lib/multivm/src/versions/testonly/l2_blocks.rs index 8ea8cc958dc..634a9b34bf6 100644 --- a/core/lib/multivm/src/versions/testonly/l2_blocks.rs +++ b/core/lib/multivm/src/versions/testonly/l2_blocks.rs @@ -7,8 +7,8 @@ use assert_matches::assert_matches; use zksync_system_constants::REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_BYTE; use zksync_types::{ block::{pack_block_info, L2BlockHasher}, - AccountTreeId, Execute, ExecuteTransactionCommon, L1BatchNumber, L1TxCommonData, L2BlockNumber, - ProtocolVersionId, StorageKey, Transaction, H160, H256, SYSTEM_CONTEXT_ADDRESS, + AccountTreeId, Address, Execute, ExecuteTransactionCommon, L1BatchNumber, L1TxCommonData, + L2BlockNumber, ProtocolVersionId, StorageKey, Transaction, H256, SYSTEM_CONTEXT_ADDRESS, SYSTEM_CONTEXT_BLOCK_INFO_POSITION, SYSTEM_CONTEXT_CURRENT_L2_BLOCK_INFO_POSITION, SYSTEM_CONTEXT_CURRENT_TX_ROLLING_HASH_POSITION, U256, }; @@ -29,13 +29,13 @@ use crate::{ fn get_l1_noop() -> Transaction { Transaction { common_data: ExecuteTransactionCommon::L1(L1TxCommonData { - sender: H160::random(), + sender: Address::repeat_byte(1), gas_limit: U256::from(2000000u32), gas_per_pubdata_limit: REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_BYTE.into(), ..Default::default() }), execute: Execute { - contract_address: Some(H160::zero()), + contract_address: Some(Address::repeat_byte(0xc0)), calldata: vec![], value: U256::zero(), factory_deps: vec![], @@ -47,7 +47,7 @@ fn get_l1_noop() -> Transaction { pub(crate) fn test_l2_block_initialization_timestamp() { // This test checks that the L2 block initialization works correctly. - // Here we check that that the first block must have timestamp that is greater or equal to the timestamp + // Here we check that the first block must have timestamp that is greater or equal to the timestamp // of the current batch. let mut vm = VmTesterBuilder::new() diff --git a/core/lib/multivm/src/versions/testonly/precompiles.rs b/core/lib/multivm/src/versions/testonly/precompiles.rs index 3abb7cb3902..270afab0731 100644 --- a/core/lib/multivm/src/versions/testonly/precompiles.rs +++ b/core/lib/multivm/src/versions/testonly/precompiles.rs @@ -11,7 +11,7 @@ use crate::{ pub(crate) fn test_keccak() { // Execute special transaction and check that at least 1000 keccak calls were made. let contract = read_precompiles_contract(); - let address = Address::random(); + let address = Address::repeat_byte(1); let mut vm = VmTesterBuilder::new() .with_empty_in_memory_storage() .with_rich_accounts(1) @@ -47,7 +47,7 @@ pub(crate) fn test_keccak() { pub(crate) fn test_sha256() { // Execute special transaction and check that at least 1000 `sha256` calls were made. let contract = read_precompiles_contract(); - let address = Address::random(); + let address = Address::repeat_byte(1); let mut vm = VmTesterBuilder::new() .with_empty_in_memory_storage() .with_rich_accounts(1) diff --git a/core/lib/multivm/src/versions/testonly/refunds.rs b/core/lib/multivm/src/versions/testonly/refunds.rs index d0934249de8..565607dff10 100644 --- a/core/lib/multivm/src/versions/testonly/refunds.rs +++ b/core/lib/multivm/src/versions/testonly/refunds.rs @@ -158,7 +158,7 @@ pub(crate) fn test_predetermined_refunded_gas() { } pub(crate) fn test_negative_pubdata_for_transaction() { - let expensive_contract_address = Address::random(); + let expensive_contract_address = Address::repeat_byte(1); let (expensive_contract_bytecode, expensive_contract) = read_expensive_contract(); let expensive_function = expensive_contract.function("expensive").unwrap(); let cleanup_function = expensive_contract.function("cleanUp").unwrap(); diff --git a/core/lib/multivm/src/versions/testonly/shadow.rs b/core/lib/multivm/src/versions/testonly/shadow.rs index d962e4411b0..6a7d42b06fc 100644 --- a/core/lib/multivm/src/versions/testonly/shadow.rs +++ b/core/lib/multivm/src/versions/testonly/shadow.rs @@ -70,8 +70,8 @@ impl Harness { fn new(l1_batch_env: &L1BatchEnv) -> Self { Self { - alice: Account::random(), - bob: Account::random(), + alice: Account::from_seed(0), + bob: Account::from_seed(1), storage_contract: ContractToDeploy::new( read_bytecode(Self::STORAGE_CONTRACT_PATH), Self::STORAGE_CONTRACT_ADDRESS, diff --git a/core/lib/multivm/src/versions/testonly/storage.rs b/core/lib/multivm/src/versions/testonly/storage.rs index c4e3b9bf739..4951272a60c 100644 --- a/core/lib/multivm/src/versions/testonly/storage.rs +++ b/core/lib/multivm/src/versions/testonly/storage.rs @@ -10,7 +10,7 @@ fn test_storage(first_tx_calldata: Vec, second_tx_calldata: Ve "etc/contracts-test-data/artifacts-zk/contracts/storage/storage.sol/StorageTester.json", ); - let test_contract_address = Address::random(); + let test_contract_address = Address::repeat_byte(1); // In this test, we aim to test whether a simple account interaction (without any fee logic) // will work. The account will try to deploy a simple contract from integration tests. diff --git a/core/lib/multivm/src/versions/testonly/tester/mod.rs b/core/lib/multivm/src/versions/testonly/tester/mod.rs index 2efccd9b149..4bab9bca610 100644 --- a/core/lib/multivm/src/versions/testonly/tester/mod.rs +++ b/core/lib/multivm/src/versions/testonly/tester/mod.rs @@ -5,7 +5,7 @@ use zksync_test_account::{Account, TxType}; use zksync_types::{ utils::{deployed_address_create, storage_key_for_eth_balance}, writes::StateDiffRecord, - Address, K256PrivateKey, L1BatchNumber, StorageKey, Transaction, H256, U256, + Address, L1BatchNumber, StorageKey, Transaction, H256, U256, }; use zksync_vm_interface::{ CurrentExecutionState, VmExecutionResultAndLogs, VmInterfaceHistoryEnabled, @@ -127,10 +127,7 @@ impl VmTesterBuilder { /// Creates the specified number of pre-funded accounts. pub(crate) fn with_rich_accounts(mut self, number: u32) -> Self { for i in 0..number { - let account_private_key_bytes = H256::from_low_u64_be(u64::from(i) + 1); - let account = - Account::new(K256PrivateKey::from_bytes(account_private_key_bytes).unwrap()); - self.rich_accounts.push(account); + self.rich_accounts.push(Account::from_seed(i)); } self } diff --git a/core/lib/multivm/src/versions/testonly/tracing_execution_error.rs b/core/lib/multivm/src/versions/testonly/tracing_execution_error.rs index 36b458b849d..e87e6eb7c06 100644 --- a/core/lib/multivm/src/versions/testonly/tracing_execution_error.rs +++ b/core/lib/multivm/src/versions/testonly/tracing_execution_error.rs @@ -1,5 +1,5 @@ use zksync_contracts::load_contract; -use zksync_types::{Execute, H160}; +use zksync_types::{Address, Execute}; use super::{ read_error_contract, tester::VmTesterBuilder, ContractToDeploy, TestedVm, BASE_SYSTEM_CONTRACTS, @@ -20,7 +20,7 @@ fn get_execute_error_calldata() -> Vec { } pub(crate) fn test_tracing_of_execution_errors() { - let contract_address = H160::random(); + let contract_address = Address::repeat_byte(1); let mut vm = VmTesterBuilder::new() .with_empty_in_memory_storage() .with_base_system_smart_contracts(BASE_SYSTEM_CONTRACTS.clone()) diff --git a/core/lib/multivm/src/versions/testonly/transfer.rs b/core/lib/multivm/src/versions/testonly/transfer.rs index 647570f2343..051826a64f2 100644 --- a/core/lib/multivm/src/versions/testonly/transfer.rs +++ b/core/lib/multivm/src/versions/testonly/transfer.rs @@ -22,8 +22,8 @@ fn test_send_or_transfer(test_option: TestOptions) { "etc/contracts-test-data/artifacts-zk/contracts/transfer/transfer.sol/TransferTest.json", ); - let test_contract_address = Address::random(); - let recipient_address = Address::random(); + let test_contract_address = Address::repeat_byte(1); + let recipient_address = Address::repeat_byte(2); let (value, calldata) = match test_option { TestOptions::Send(value) => ( @@ -106,8 +106,8 @@ fn test_reentrancy_protection_send_or_transfer(test_option: TestOp "etc/contracts-test-data/artifacts-zk/contracts/transfer/transfer.sol/ReentrantRecipient.json", ); - let test_contract_address = Address::random(); - let reentrant_recipient_address = Address::random(); + let test_contract_address = Address::repeat_byte(1); + let reentrant_recipient_address = Address::repeat_byte(2); let (value, calldata) = match test_option { TestOptions::Send(value) => ( diff --git a/core/lib/multivm/src/versions/testonly/upgrade.rs b/core/lib/multivm/src/versions/testonly/upgrade.rs index 0639dba0dc0..9401cbb4ba8 100644 --- a/core/lib/multivm/src/versions/testonly/upgrade.rs +++ b/core/lib/multivm/src/versions/testonly/upgrade.rs @@ -5,7 +5,7 @@ use zksync_types::{ get_code_key, get_known_code_key, protocol_upgrade::ProtocolUpgradeTxCommonData, Address, Execute, ExecuteTransactionCommon, Transaction, COMPLEX_UPGRADER_ADDRESS, - CONTRACT_DEPLOYER_ADDRESS, CONTRACT_FORCE_DEPLOYER_ADDRESS, H160, H256, + CONTRACT_DEPLOYER_ADDRESS, CONTRACT_FORCE_DEPLOYER_ADDRESS, H256, REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_BYTE, U256, }; use zksync_utils::{bytecode::hash_bytecode, h256_to_u256, u256_to_h256}; @@ -35,7 +35,7 @@ pub(crate) fn test_protocol_upgrade_is_first() { // The bytecode hash to put on an address bytecode_hash, // The address on which to deploy the bytecode hash to - address: H160::random(), + address: Address::repeat_byte(1), // Whether to run the constructor on the force deployment call_constructor: false, // The value with which to initialize a contract @@ -49,7 +49,7 @@ pub(crate) fn test_protocol_upgrade_is_first() { // The bytecode hash to put on an address bytecode_hash, // The address on which to deploy the bytecode hash to - address: H160::random(), + address: Address::repeat_byte(2), // Whether to run the constructor on the force deployment call_constructor: false, // The value with which to initialize a contract @@ -120,7 +120,7 @@ pub(crate) fn test_force_deploy_upgrade() { .with_rich_accounts(1) .build::(); - let address_to_deploy = H160::random(); + let address_to_deploy = Address::repeat_byte(1); // Here we just use some random transaction of protocol upgrade type: let transaction = get_forced_deploy_tx(&[ForceDeployment { // The bytecode hash to put on an address @@ -158,7 +158,7 @@ pub(crate) fn test_complex_upgrader() { let msg_sender_test_hash = hash_bytecode(&read_msg_sender_test()); // Let's assume that the bytecode for the implementation of the complex upgrade // is already deployed in some address in user space - let upgrade_impl = H160::random(); + let upgrade_impl = Address::repeat_byte(1); let account_code_key = get_code_key(&upgrade_impl); storage.set_value(get_known_code_key(&bytecode_hash), u256_to_h256(1.into())); storage.set_value( @@ -175,8 +175,8 @@ pub(crate) fn test_complex_upgrader() { .with_rich_accounts(1) .build::(); - let address_to_deploy1 = H160::random(); - let address_to_deploy2 = H160::random(); + let address_to_deploy1 = Address::repeat_byte(0xfe); + let address_to_deploy2 = Address::repeat_byte(0xff); let transaction = get_complex_upgrade_tx( upgrade_impl, diff --git a/core/tests/test_account/src/lib.rs b/core/tests/test_account/src/lib.rs index 39a36694526..cfb539c0e0f 100644 --- a/core/tests/test_account/src/lib.rs +++ b/core/tests/test_account/src/lib.rs @@ -54,6 +54,12 @@ impl Account { Self::new(K256PrivateKey::random_using(rng)) } + /// Creates an account deterministically from the provided seed. + pub fn from_seed(seed: u32) -> Self { + let private_key_bytes = H256::from_low_u64_be(u64::from(seed) + 1); + Self::new(K256PrivateKey::from_bytes(private_key_bytes).unwrap()) + } + pub fn get_l2_tx_for_execute(&mut self, execute: Execute, fee: Option) -> Transaction { let tx = self.get_l2_tx_for_execute_with_nonce(execute, fee, self.nonce); self.nonce += 1;