From 062be1ab4768a2f481ce08fb95d0e417cb8b7251 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 29 Jun 2023 15:05:18 +0200 Subject: [PATCH 1/2] Add missing stats call --- CHANGELOG.md | 4 ++++ packages/vm/src/cache.rs | 1 + 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4aafbc2baf..66ff512359 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to ## [Unreleased] +### Fixed + +- cosmwasm-vm: Add missing cache stats increment when calling `pin`. + ### Added - cosmwasm-std: Implement `BankQuery::AllDenomMetadata` to allow querying all diff --git a/packages/vm/src/cache.rs b/packages/vm/src/cache.rs index 85c145e15d..4f00e7430a 100644 --- a/packages/vm/src/cache.rs +++ b/packages/vm/src/cache.rs @@ -272,6 +272,7 @@ where // Re-compile from original Wasm bytecode let code = self.load_wasm_with_path(&cache.wasm_path, checksum)?; + cache.stats.misses = cache.stats.misses.saturating_add(1); let module = compile(&code, Some(cache.instance_memory_limit), &[])?; // Store into the fs cache too cache.fs_cache.store(checksum, &module)?; From 053b5c9a91bdcfcbac7a2bab257e7d0b2cdb000e Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 29 Jun 2023 15:41:04 +0200 Subject: [PATCH 2/2] Test recompiling modules from Wasm --- packages/vm/src/cache.rs | 60 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/vm/src/cache.rs b/packages/vm/src/cache.rs index 4f00e7430a..dbc809ef09 100644 --- a/packages/vm/src/cache.rs +++ b/packages/vm/src/cache.rs @@ -452,7 +452,7 @@ mod tests { use crate::errors::VmError; use crate::testing::{mock_backend, mock_env, mock_info, MockApi, MockQuerier, MockStorage}; use cosmwasm_std::{coins, Empty}; - use std::fs::{create_dir_all, OpenOptions}; + use std::fs::{create_dir_all, remove_dir_all, OpenOptions}; use std::io::Write; use tempfile::TempDir; @@ -776,6 +776,36 @@ mod tests { assert_eq!(cache.stats().misses, 0); } + #[test] + fn get_instance_recompiles_module() { + let options = make_testing_options(); + let cache = unsafe { Cache::new(options.clone()).unwrap() }; + let checksum = cache.save_wasm(CONTRACT).unwrap(); + + // Remove compiled module from disk + remove_dir_all(options.base_dir.join(CACHE_DIR).join(MODULES_DIR)).unwrap(); + + // The first get_instance recompiles the Wasm (miss) + let backend = mock_backend(&[]); + let _instance = cache + .get_instance(&checksum, backend, TESTING_OPTIONS) + .unwrap(); + assert_eq!(cache.stats().hits_pinned_memory_cache, 0); + assert_eq!(cache.stats().hits_memory_cache, 0); + assert_eq!(cache.stats().hits_fs_cache, 0); + assert_eq!(cache.stats().misses, 1); + + // The second get_instance finds the module in cache (hit) + let backend = mock_backend(&[]); + let _instance = cache + .get_instance(&checksum, backend, TESTING_OPTIONS) + .unwrap(); + assert_eq!(cache.stats().hits_pinned_memory_cache, 0); + assert_eq!(cache.stats().hits_memory_cache, 1); + assert_eq!(cache.stats().hits_fs_cache, 0); + assert_eq!(cache.stats().misses, 1); + } + #[test] fn call_instantiate_on_cached_contract() { let cache = unsafe { Cache::new(make_testing_options()).unwrap() }; @@ -1220,6 +1250,34 @@ mod tests { cache.unpin(&non_id).unwrap(); } + #[test] + fn pin_recompiles_module() { + let options = make_testing_options(); + let cache: Cache = + unsafe { Cache::new(options.clone()).unwrap() }; + let checksum = cache.save_wasm(CONTRACT).unwrap(); + + // Remove compiled module from disk + remove_dir_all(options.base_dir.join(CACHE_DIR).join(MODULES_DIR)).unwrap(); + + // Pin misses, forcing a re-compile of the module + cache.pin(&checksum).unwrap(); + assert_eq!(cache.stats().hits_pinned_memory_cache, 0); + assert_eq!(cache.stats().hits_memory_cache, 0); + assert_eq!(cache.stats().hits_fs_cache, 0); + assert_eq!(cache.stats().misses, 1); + + // After the compilation in pin, the module can be used from pinned memory cache + let backend = mock_backend(&[]); + let _ = cache + .get_instance(&checksum, backend, TESTING_OPTIONS) + .unwrap(); + assert_eq!(cache.stats().hits_pinned_memory_cache, 1); + assert_eq!(cache.stats().hits_memory_cache, 0); + assert_eq!(cache.stats().hits_fs_cache, 0); + assert_eq!(cache.stats().misses, 1); + } + #[test] fn loading_without_extension_works() { let tmp_dir = TempDir::new().unwrap();