Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fill storage_refunds / pubdata_costs data #2431

Merged
merged 17 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/lib/multivm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ zk_evm_1_4_1.workspace = true
zk_evm_1_4_0.workspace = true
zk_evm_1_3_3.workspace = true
zk_evm_1_3_1.workspace = true
vm2 = { git = "https://github.com/matter-labs/vm2.git", rev = "4931b489dc9e95f88a0b11bd27b110656277c5d2" }
vm2 = { git = "https://github.com/matter-labs/vm2.git", rev = "7fdf80e0c494a1ee1c9d615465eb7d42d3e2c38c" }

circuit_sequencer_api_1_3_3.workspace = true
circuit_sequencer_api_1_4_0.workspace = true
Expand Down
1 change: 1 addition & 0 deletions core/lib/multivm/src/interface/types/outputs/l2_block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use zksync_types::H256;

#[derive(Debug)]
pub struct L2Block {
pub number: u32,
pub timestamp: u64,
Expand Down
20 changes: 19 additions & 1 deletion core/lib/multivm/src/versions/shadow.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{collections::BTreeMap, fmt};
use std::{
collections::{BTreeMap, HashSet},
fmt,
};

use anyhow::Context as _;
use zksync_state::{ImmutableStorageView, ReadStorage, StoragePtr, StorageView};
Expand Down Expand Up @@ -276,6 +279,21 @@ impl DivergenceErrors {
&main.system_logs,
&shadow.system_logs,
);
self.check_match(
"final_state.storage_refunds",
&main.storage_refunds,
&shadow.storage_refunds,
);
self.check_match(
"final_state.pubdata_costs",
&main.pubdata_costs,
&shadow.pubdata_costs,
);
self.check_match(
"final_state.used_contract_hashes",
&main.used_contract_hashes.iter().collect::<HashSet<_>>(),
&shadow.used_contract_hashes.iter().collect::<HashSet<_>>(),
);

let main_deduplicated_logs = Self::gather_logs(&main.deduplicated_storage_logs);
let shadow_deduplicated_logs = Self::gather_logs(&shadow.deduplicated_storage_logs);
Expand Down
21 changes: 12 additions & 9 deletions core/lib/multivm/src/versions/vm_fast/tests/default_aa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ use zksync_utils::u256_to_h256;

use crate::{
interface::{TxExecutionMode, VmExecutionMode, VmInterface},
vm_fast::{
tests::{
tester::{DeployContractsTx, TxType, VmTesterBuilder},
utils::{get_balance, read_test_contract, verify_required_storage},
},
utils::fee::get_batch_base_fee,
vm_fast::tests::{
tester::{DeployContractsTx, TxType, VmTesterBuilder},
utils::{get_balance, read_test_contract, verify_required_storage},
},
vm_latest::utils::fee::get_batch_base_fee,
};

#[test]
Expand Down Expand Up @@ -54,21 +52,26 @@ fn test_default_aa_interaction() {
// The contract should be deployed successfully.
let account_code_key = get_code_key(&address);

let expected_slots = vec![
let expected_slots = [
(u256_to_h256(expected_nonce), account_nonce_key),
(u256_to_h256(U256::from(1u32)), known_codes_key),
(bytecode_hash, account_code_key),
];

verify_required_storage(&vm.vm.state, expected_slots);
verify_required_storage(
&expected_slots,
&mut vm.vm.world.storage,
vm.vm.inner.world_diff.get_storage_state(),
);

let expected_fee = maximal_fee
- U256::from(result.refunds.gas_refunded)
* U256::from(get_batch_base_fee(&vm.vm.batch_env));
let operator_balance = get_balance(
AccountTreeId::new(L2_BASE_TOKEN_ADDRESS),
&vm.fee_account,
vm.vm.state.storage.storage.get_ptr(),
&mut vm.vm.world.storage,
vm.vm.inner.world_diff.get_storage_state(),
);

assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,12 @@ fn test_get_used_contracts() {

assert!(vm
.vm
.get_decommitted_hashes()
.decommitted_hashes()
.contains(&h256_to_u256(tx.bytecode_hash)));

// Note: `Default_AA` will be in the list of used contracts if L2 tx is used
assert_eq!(
vm.vm
.get_decommitted_hashes()
.cloned()
.collect::<HashSet<U256>>(),
vm.vm.decommitted_hashes().collect::<HashSet<U256>>(),
known_bytecodes_without_aa_code(&vm.vm)
);

Expand Down Expand Up @@ -81,7 +78,7 @@ fn test_get_used_contracts() {
let hash = hash_bytecode(&factory_dep);
let hash_to_u256 = h256_to_u256(hash);
assert!(known_bytecodes_without_aa_code(&vm.vm).contains(&hash_to_u256));
assert!(!vm.vm.get_decommitted_hashes().contains(&hash_to_u256));
assert!(!vm.vm.decommitted_hashes().contains(&hash_to_u256));
}
}

Expand Down
18 changes: 8 additions & 10 deletions core/lib/multivm/src/versions/vm_fast/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
mod bootloader;
//mod default_aa;
// TODO - fix this test
// `mod invalid_bytecode;`
//mod block_tip;
mod default_aa;
//mod block_tip; FIXME: requires vm metrics
mod bytecode_publishing;
// mod call_tracer;
// mod circuits;
// mod call_tracer; FIXME: requires tracers
// mod circuits; FIXME: requires tracers / circuit stats
mod code_oracle;
mod gas_limit;
mod get_used_contracts;
mod is_write_initial;
mod l1_tx_execution;
mod l2_blocks;
//mod nonce_holder;
// mod precompiles;
// mod prestate_tracer;
mod nonce_holder;
// mod precompiles; FIXME: requires tracers / circuit stats
// mod prestate_tracer; FIXME: is pre-state tracer still relevant?
mod refunds;
mod require_eip712;
mod rollbacks;
Expand All @@ -24,5 +22,5 @@ mod storage;
mod tester;
mod tracing_execution_error;
mod transfer;
// mod upgrade;
mod upgrade;
mod utils;
50 changes: 21 additions & 29 deletions core/lib/multivm/src/versions/vm_fast/tests/nonce_holder.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use zksync_types::{Execute, Nonce};
use zksync_types::{Execute, ExecuteTransactionCommon, Nonce};

use crate::{
interface::{
ExecutionResult, Halt, TxExecutionMode, TxRevertReason, VmExecutionMode, VmInterface,
VmRevertReason,
},
vm_fast::{
tests::{
tester::{Account, VmTesterBuilder},
utils::read_nonce_holder_tester,
},
transaction_data::TransactionData,
vm_fast::tests::{
tester::{Account, VmTesterBuilder},
utils::read_nonce_holder_tester,
},
};

Expand Down Expand Up @@ -60,21 +57,21 @@ fn test_nonce_holder() {
// it will fail again and again. At the same time we have to keep the same storage, because we want to keep the nonce holder contract state.
// The easiest way in terms of lifetimes is to reuse `vm_builder` to achieve it.
vm.reset_state(true);
let mut transaction_data: TransactionData = account
.get_l2_tx_for_execute_with_nonce(
Execute {
contract_address: account.address,
calldata: vec![12],
value: Default::default(),
factory_deps: None,
},
None,
Nonce(nonce),
)
.into();

transaction_data.signature = vec![test_mode.into()];
vm.vm.push_raw_transaction(transaction_data, 0, 0, true);
let mut transaction = account.get_l2_tx_for_execute_with_nonce(
Execute {
contract_address: account.address,
calldata: vec![12],
value: Default::default(),
factory_deps: vec![],
},
None,
Nonce(nonce),
);
let ExecuteTransactionCommon::L2(tx_data) = &mut transaction.common_data else {
unreachable!();
};
tx_data.signature = vec![test_mode.into()];
vm.vm.push_transaction_inner(transaction, 0, true);
let result = vm.vm.execute(VmExecutionMode::OneTx);

if let Some(msg) = error_message {
Expand All @@ -86,14 +83,9 @@ fn test_nonce_holder() {
let ExecutionResult::Halt { reason } = result.result else {
panic!("Expected revert, got {:?}", result.result);
};
assert_eq!(
reason.to_string(),
expected_error.to_string(),
"{}",
comment
);
assert_eq!(reason.to_string(), expected_error.to_string(), "{comment}");
} else {
assert!(!result.result.is_failed(), "{}", comment);
assert!(!result.result.is_failed(), "{comment}: {result:?}");
}
};
// Test 1: trying to set value under non sequential nonce value.
Expand Down
23 changes: 19 additions & 4 deletions core/lib/multivm/src/versions/vm_fast/tests/tester/vm_tester.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{cell::RefCell, rc::Rc};

use vm2::WorldDiff;
use zksync_contracts::BaseSystemContracts;
use zksync_state::{InMemoryStorage, StoragePtr};
use zksync_test_account::{Account, TxType};
Expand All @@ -9,7 +10,8 @@ use zksync_types::{
get_code_key, get_is_account_key,
helpers::unix_timestamp_ms,
utils::{deployed_address_create, storage_key_for_eth_balance},
Address, L1BatchNumber, L2BlockNumber, L2ChainId, Nonce, ProtocolVersionId, U256,
AccountTreeId, Address, L1BatchNumber, L2BlockNumber, L2ChainId, Nonce, ProtocolVersionId,
StorageKey, U256,
};
use zksync_utils::{bytecode::hash_bytecode, u256_to_h256};

Expand All @@ -26,6 +28,7 @@ pub(crate) struct VmTester {
pub(crate) storage: StoragePtr<InMemoryStorage>,
pub(crate) deployer: Option<Account>,
pub(crate) test_contract: Option<Address>,
pub(crate) fee_account: Address,
pub(crate) rich_accounts: Vec<Account>,
pub(crate) custom_contracts: Vec<ContractsToDeploy>,
}
Expand All @@ -49,6 +52,7 @@ impl VmTester {

pub(crate) fn reset_with_empty_storage(&mut self) {
self.storage = Rc::new(RefCell::new(get_empty_storage()));
self.vm.inner.world_diff = WorldDiff::default();
self.reset_state(false);
}

Expand All @@ -69,9 +73,19 @@ impl VmTester {
// `insert_contracts(&mut self.storage, &self.custom_contracts);`
}

let storage = self.storage.clone();
{
let mut storage = storage.borrow_mut();
// Commit pending storage changes (old VM versions commit them on successful execution)
for (&(address, slot), &value) in self.vm.inner.world_diff.get_storage_state() {
let key = StorageKey::new(AccountTreeId::new(address), u256_to_h256(slot));
storage.set_value(key, u256_to_h256(value));
}
}

montekki marked this conversation as resolved.
Show resolved Hide resolved
let mut l1_batch = self.vm.batch_env.clone();
if use_latest_l2_block {
let last_l2_block = load_last_l2_block(self.storage.clone()).unwrap_or(L2Block {
let last_l2_block = load_last_l2_block(&storage).unwrap_or(L2Block {
number: 0,
timestamp: 0,
hash: L2BlockHasher::legacy_hash(L2BlockNumber(0)),
Expand All @@ -84,12 +98,11 @@ impl VmTester {
};
}

let vm = Vm::new(l1_batch, self.vm.system_env.clone(), self.storage.clone());
let vm = Vm::new(l1_batch, self.vm.system_env.clone(), storage);

if self.test_contract.is_some() {
self.deploy_test_contract();
}

self.vm = vm;
}
}
Expand Down Expand Up @@ -216,13 +229,15 @@ impl VmTesterBuilder {
make_account_rich(storage_ptr.clone(), deployer);
}

let fee_account = l1_batch_env.fee_account;
let vm = Vm::new(l1_batch_env, self.system_env, storage_ptr.clone());

VmTester {
vm,
storage: storage_ptr,
deployer: self.deployer,
test_contract: None,
fee_account,
rich_accounts: self.rich_accounts.clone(),
custom_contracts: self.custom_contracts.clone(),
}
Expand Down
Loading
Loading