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

Test recompiling modules from Wasm and fix missing stats increment #1755

Merged
merged 2 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 60 additions & 1 deletion packages/vm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -451,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;

Expand Down Expand Up @@ -775,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() };
Expand Down Expand Up @@ -1219,6 +1250,34 @@ mod tests {
cache.unpin(&non_id).unwrap();
}

#[test]
fn pin_recompiles_module() {
let options = make_testing_options();
let cache: Cache<MockApi, MockStorage, MockQuerier> =
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();
Expand Down
Loading