Skip to content

Commit

Permalink
Eliminate remaining randomness
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Oct 17, 2024
1 parent 2fd46ee commit 9e21be7
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 38 deletions.
6 changes: 3 additions & 3 deletions core/lib/multivm/src/versions/testonly/block_tip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ fn populate_mimic_calls(data: L1MessengerTestData) -> Vec<Vec<u8>> {
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
.function("sendL2ToL1Log")
.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(),
});
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 @@ -20,7 +20,7 @@ pub(crate) fn test_circuits<VM: TestedVm>() {
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![],
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 @@ -19,7 +19,7 @@ fn generate_large_bytecode() -> Vec<u8> {
}

pub(crate) fn test_code_oracle<VM: TestedVm>() {
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
Expand Down Expand Up @@ -110,7 +110,7 @@ fn find_code_oracle_cost_log(
}

pub(crate) fn test_code_oracle_big_bytecode<VM: TestedVm>() {
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();
Expand Down Expand Up @@ -168,7 +168,7 @@ pub(crate) fn test_code_oracle_big_bytecode<VM: TestedVm>() {
}

pub(crate) fn test_refunds_in_code_oracle<VM: TestedVm>() {
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();
Expand Down
6 changes: 4 additions & 2 deletions core/lib/multivm/src/versions/testonly/get_used_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ pub(crate) fn test_get_used_contracts<VM: TestedVm>() {
let mut vm = VmTesterBuilder::new()
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_rich_accounts(1)
.build::<VM>();

assert!(known_bytecodes_without_base_system_contracts(&vm.vm).is_empty());

// 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);
Expand All @@ -58,7 +59,8 @@ pub(crate) fn test_get_used_contracts<VM: TestedVm>() {
.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),
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 @@ -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,
};
Expand All @@ -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![],
Expand All @@ -47,7 +47,7 @@ fn get_l1_noop() -> Transaction {

pub(crate) fn test_l2_block_initialization_timestamp<VM: TestedVm>() {
// 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()
Expand Down
4 changes: 2 additions & 2 deletions core/lib/multivm/src/versions/testonly/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
pub(crate) fn test_keccak<VM: TestedVm>() {
// 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)
Expand Down Expand Up @@ -47,7 +47,7 @@ pub(crate) fn test_keccak<VM: TestedVm>() {
pub(crate) fn test_sha256<VM: TestedVm>() {
// 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)
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/testonly/refunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub(crate) fn test_predetermined_refunded_gas<VM: TestedVm>() {
}

pub(crate) fn test_negative_pubdata_for_transaction<VM: TestedVm>() {
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();
Expand Down
4 changes: 2 additions & 2 deletions core/lib/multivm/src/versions/testonly/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/multivm/src/versions/testonly/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn test_storage<VM: TestedVm>(first_tx_calldata: Vec<u8>, 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.
Expand Down
7 changes: 2 additions & 5 deletions core/lib/multivm/src/versions/testonly/tester/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -20,7 +20,7 @@ fn get_execute_error_calldata() -> Vec<u8> {
}

pub(crate) fn test_tracing_of_execution_errors<VM: TestedVm>() {
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())
Expand Down
8 changes: 4 additions & 4 deletions core/lib/multivm/src/versions/testonly/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ fn test_send_or_transfer<VM: TestedVm>(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) => (
Expand Down Expand Up @@ -106,8 +106,8 @@ fn test_reentrancy_protection_send_or_transfer<VM: TestedVm>(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) => (
Expand Down
14 changes: 7 additions & 7 deletions core/lib/multivm/src/versions/testonly/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -35,7 +35,7 @@ pub(crate) fn test_protocol_upgrade_is_first<VM: TestedVm>() {
// 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
Expand All @@ -49,7 +49,7 @@ pub(crate) fn test_protocol_upgrade_is_first<VM: TestedVm>() {
// 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
Expand Down Expand Up @@ -120,7 +120,7 @@ pub(crate) fn test_force_deploy_upgrade<VM: TestedVm>() {
.with_rich_accounts(1)
.build::<VM>();

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
Expand Down Expand Up @@ -158,7 +158,7 @@ pub(crate) fn test_complex_upgrader<VM: TestedVm>() {
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(
Expand All @@ -175,8 +175,8 @@ pub(crate) fn test_complex_upgrader<VM: TestedVm>() {
.with_rich_accounts(1)
.build::<VM>();

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,
Expand Down
6 changes: 6 additions & 0 deletions core/tests/test_account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Fee>) -> Transaction {
let tx = self.get_l2_tx_for_execute_with_nonce(execute, fee, self.nonce);
self.nonce += 1;
Expand Down

0 comments on commit 9e21be7

Please sign in to comment.