diff --git a/etc/system-contracts/bootloader/test_infra/src/main.rs b/etc/system-contracts/bootloader/test_infra/src/main.rs index e49e009d..c91a8198 100644 --- a/etc/system-contracts/bootloader/test_infra/src/main.rs +++ b/etc/system-contracts/bootloader/test_infra/src/main.rs @@ -91,7 +91,7 @@ fn execute_internal_bootloader_test() { hash_bytecode, get_system_smart_contracts_from_dir(env::current_dir().unwrap().join("../../")), )) - .to_rc_ptr(); + .into_rc_ptr(); let mut vm = Vm::new( l1_batch_env.clone(), @@ -124,7 +124,7 @@ fn execute_internal_bootloader_test() { hash_bytecode, get_system_smart_contracts_from_dir(env::current_dir().unwrap().join("../../")), )) - .to_rc_ptr(); + .into_rc_ptr(); // We are passing id of the test in location (0) where we normally put the operator. // This is then picked up by the testing framework. diff --git a/src/deps/storage_view.rs b/src/deps/storage_view.rs index 12e4d269..139ff8c0 100644 --- a/src/deps/storage_view.rs +++ b/src/deps/storage_view.rs @@ -36,6 +36,7 @@ impl StorageView { } } + #[allow(dead_code)] pub fn clean_cache(&mut self) { self.modified_storage_keys = Default::default(); self.read_storage_keys = Default::default(); @@ -54,7 +55,7 @@ impl StorageView { }) } /// Make a Rc RefCell ptr to the storage - pub fn to_rc_ptr(self) -> Rc> { + pub fn into_rc_ptr(self) -> Rc> { Rc::new(RefCell::new(self)) } } @@ -121,51 +122,51 @@ impl WriteStorage for StorageView { } } -// #[cfg(test)] -// mod test { -// use super::*; -// use zksync_types::{AccountTreeId, Address, H256}; - -// #[test] -// fn test_storage_access() { -// let account: AccountTreeId = AccountTreeId::new(Address::from([0xfe; 20])); -// let key = H256::from_low_u64_be(61); -// let value = H256::from_low_u64_be(73); -// let key = StorageKey::new(account, key); - -// let mut raw_storage = InMemoryStorage::default(); -// let mut storage_view = StorageView::new(&raw_storage); - -// let default_value = storage_view.read_value(&key); -// assert_eq!(default_value, H256::zero()); - -// let prev_value = storage_view.set_value(key, value); -// assert_eq!(prev_value, H256::zero()); -// assert_eq!(storage_view.read_value(&key), value); -// assert!(storage_view.is_write_initial(&key)); // key was inserted during the view -// lifetime - -// assert_eq!(storage_view.metrics().storage_invocations_missed, 1); -// // ^ We should only read a value at `key` once, and then used the cached value. - -// raw_storage.set_value(key, value); -// let mut storage_view = StorageView::new(&raw_storage); - -// assert_eq!(storage_view.read_value(&key), value); -// assert!(!storage_view.is_write_initial(&key)); // `key` is present in `raw_storage` - -// let new_value = H256::from_low_u64_be(74); -// storage_view.set_value(key, new_value); -// assert_eq!(storage_view.read_value(&key), new_value); - -// let new_key = StorageKey::new(account, H256::from_low_u64_be(62)); -// storage_view.set_value(new_key, new_value); -// assert_eq!(storage_view.read_value(&new_key), new_value); -// assert!(storage_view.is_write_initial(&new_key)); - -// let metrics = storage_view.metrics(); -// assert_eq!(metrics.storage_invocations_missed, 2); -// assert_eq!(metrics.get_value_storage_invocations, 3); -// assert_eq!(metrics.set_value_storage_invocations, 2); -// } -// } +#[cfg(test)] +mod test { + use super::*; + use crate::deps::InMemoryStorage; + use zksync_types::{AccountTreeId, Address, H256}; + + #[test] + fn test_storage_access() { + let account: AccountTreeId = AccountTreeId::new(Address::from([0xfe; 20])); + let key = H256::from_low_u64_be(61); + let value = H256::from_low_u64_be(73); + let key = StorageKey::new(account, key); + + let mut raw_storage = InMemoryStorage::default(); + let mut storage_view = StorageView::new(&raw_storage); + + let default_value = storage_view.read_value(&key); + assert_eq!(default_value, H256::zero()); + + let prev_value = storage_view.set_value(key, value); + assert_eq!(prev_value, H256::zero()); + assert_eq!(storage_view.read_value(&key), value); + assert!(storage_view.is_write_initial(&key)); // key was inserted during the view lifetime + + assert_eq!(storage_view.metrics().storage_invocations_missed, 1); + // ^ We should only read a value at `key` once, and then used the cached value. + + raw_storage.set_value(key, value); + let mut storage_view = StorageView::new(&raw_storage); + + assert_eq!(storage_view.read_value(&key), value); + assert!(!storage_view.is_write_initial(&key)); // `key` is present in `raw_storage` + + let new_value = H256::from_low_u64_be(74); + storage_view.set_value(key, new_value); + assert_eq!(storage_view.read_value(&key), new_value); + + let new_key = StorageKey::new(account, H256::from_low_u64_be(62)); + storage_view.set_value(new_key, new_value); + assert_eq!(storage_view.read_value(&new_key), new_value); + assert!(storage_view.is_write_initial(&new_key)); + + let metrics = storage_view.metrics(); + assert_eq!(metrics.storage_invocations_missed, 2); + assert_eq!(metrics.get_value_storage_invocations, 3); + assert_eq!(metrics.set_value_storage_invocations, 2); + } +} diff --git a/src/namespaces/debug.rs b/src/namespaces/debug.rs index 2ad49d7a..421154a5 100644 --- a/src/namespaces/debug.rs +++ b/src/namespaces/debug.rs @@ -170,7 +170,7 @@ impl DebugNamespaceT }; let execution_mode = multivm::interface::TxExecutionMode::EthCall; - let storage = StorageView::new(&inner.fork_storage).to_rc_ptr(); + let storage = StorageView::new(&inner.fork_storage).into_rc_ptr(); let bootloader_code = inner.system_contracts.contracts_for_l2_call(); diff --git a/src/node/debug.rs b/src/node/debug.rs index 70dc7e2d..7bf11179 100644 --- a/src/node/debug.rs +++ b/src/node/debug.rs @@ -162,7 +162,7 @@ impl DebugNames }; let execution_mode = multivm::interface::TxExecutionMode::EthCall; - let storage = StorageView::new(&inner.fork_storage).to_rc_ptr(); + let storage = StorageView::new(&inner.fork_storage).into_rc_ptr(); let bootloader_code = inner.system_contracts.contracts_for_l2_call(); diff --git a/src/node/in_memory.rs b/src/node/in_memory.rs index 1a66a9df..37b4106b 100644 --- a/src/node/in_memory.rs +++ b/src/node/in_memory.rs @@ -341,15 +341,13 @@ impl InMemoryNodeInner { max_virtual_blocks_to_create: 1, } } else { + // This is the scenario of either the first L2 block ever L2BlockEnv { number: 1, timestamp: 1, prev_block_hash: MiniblockHasher::legacy_hash(MiniblockNumber(0)), max_virtual_blocks_to_create: 1, } - // This is the scenario of either the first L2 block ever or - // the first block after the upgrade for support of L2 blocks. - // legacy_miniblock_hash(MiniblockNumber(self.current_miniblock as u32)) }; let block_ctx = if let Some((batch_number, batch_timestamp)) = load_last_l1_batch(storage) { BlockContext::from_current( @@ -502,7 +500,7 @@ impl InMemoryNodeInner { let gas_for_bytecodes_pubdata: u32 = pubdata_for_factory_deps * (gas_per_pubdata_byte as u32); - let storage = storage_view.to_rc_ptr(); + let storage = storage_view.into_rc_ptr(); let execution_mode = TxExecutionMode::EstimateFee; let (mut batch_env, _) = self.create_l1_batch_env(storage.clone()); @@ -708,7 +706,7 @@ impl InMemoryNodeInner { ); l2_tx.common_data.fee.gas_limit = gas_limit_with_overhead.into(); - let storage = StorageView::new(fork_storage).to_rc_ptr(); + let storage = StorageView::new(fork_storage).into_rc_ptr(); // The nonce needs to be updated let nonce = l2_tx.nonce(); @@ -1016,7 +1014,7 @@ impl InMemoryNode { .write() .map_err(|e| format!("Failed to acquire write lock: {}", e))?; - let storage = StorageView::new(&inner.fork_storage).to_rc_ptr(); + let storage = StorageView::new(&inner.fork_storage).into_rc_ptr(); let bootloader_code = inner.system_contracts.contracts_for_l2_call(); @@ -1300,11 +1298,10 @@ impl InMemoryNode { .write() .map_err(|e| format!("Failed to acquire write lock: {}", e))?; - let storage = StorageView::new(inner.fork_storage.clone()).to_rc_ptr(); + let storage = StorageView::new(inner.fork_storage.clone()).into_rc_ptr(); storage.borrow_mut().modified_storage_keys = modified_storage_keys; let (batch_env, block_ctx) = inner.create_l1_batch_env(storage.clone()); - dbg!(&batch_env); let bootloader_code = { if inner @@ -1472,16 +1469,12 @@ impl InMemoryNode { ..Default::default() }; - tracing::info!(""); - let bytecodes: HashMap> = vm .get_last_tx_compressed_bytecodes() .iter() .map(|b| bytecode_to_factory_dep(b.original.clone())) .collect(); - // vm.execute(VmExecutionMode::Bootloader); - let modified_keys = storage.borrow().modified_storage_keys().clone(); Ok(( diff --git a/src/utils.rs b/src/utils.rs index 8e534011..197ff03d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -107,7 +107,7 @@ pub fn mine_empty_blocks( for i in 0..num_blocks { // roll the vm let (keys, bytecodes, block_ctx) = { - let storage = StorageView::new(&node.fork_storage).to_rc_ptr(); + let storage = StorageView::new(&node.fork_storage).into_rc_ptr(); // system_contract.contracts_for_l2_call() will give playground contracts // we need these to use the unsafeOverrideBlock method in SystemContext.sol