From 24501f721da1cdaecae1351b390fc4e998976943 Mon Sep 17 00:00:00 2001 From: sh-cha Date: Fri, 1 Nov 2024 16:38:18 +0900 Subject: [PATCH] ignore cache insertion error to avoid non-deterministic state --- crates/storage/src/module_cache.rs | 27 +++++++++++++-------------- crates/storage/src/script_cache.rs | 21 +++++++++------------ 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/crates/storage/src/module_cache.rs b/crates/storage/src/module_cache.rs index 3efdf5f1..62f056a8 100644 --- a/crates/storage/src/module_cache.rs +++ b/crates/storage/src/module_cache.rs @@ -3,7 +3,7 @@ use std::{hash::RandomState, num::NonZeroUsize, sync::Arc}; use bytes::Bytes; use clru::{CLruCache, CLruCacheConfig}; use move_binary_format::{ - errors::{Location, PartialVMError, VMError, VMResult}, + errors::{Location, PartialVMError, VMResult}, CompiledModule, }; use move_core_types::{language_storage::ModuleId, vm_status::StatusCode}; @@ -17,12 +17,6 @@ use crate::{ state_view::Checksum, }; -fn handle_cache_error(module_id: ModuleId) -> VMError { - PartialVMError::new(StatusCode::MEMORY_LIMIT_EXCEEDED) - .with_message("Module storage cache eviction error".to_string()) - .finish(Location::Module(module_id)) -} - /// Extension for modules stored in [InitialModuleCache] to also capture information about bytes /// and hash. #[derive(PartialEq, Eq, Debug)] @@ -97,7 +91,10 @@ impl InitiaModuleCache { )); module_cache .put_with_weight(key, ModuleWrapper::new(module, allocated_size)) - .map_err(|_| handle_cache_error(module_id))?; + .unwrap_or_else(|_| { + eprintln!("WARNING: failed to insert module {:?} into cache; cache capacity might be too small", module_id.short_str_lossless().to_string()); + None + }); Ok(()) } } @@ -121,7 +118,10 @@ impl InitiaModuleCache { let module = Arc::new(ModuleCode::from_verified(verified_code, extension, version)); module_cache .put_with_weight(key, ModuleWrapper::new(module.clone(), allocated_size)) - .map_err(|_| handle_cache_error(module_id))?; + .unwrap_or_else(|_| { + eprintln!("WARNING: failed to insert module {:?} into cache; cache capacity might be too small", module_id.short_str_lossless().to_string()); + None + }); Ok(module) } } @@ -158,11 +158,10 @@ impl InitiaModuleCache { let code_wrapper = ModuleWrapper::new(Arc::new(code), allocated_size); module_cache .put_with_weight(*checksum, code_wrapper.clone()) - .map_err(|_| { - PartialVMError::new(StatusCode::MEMORY_LIMIT_EXCEEDED) - .with_message("Module storage cache eviction error".to_string()) - .finish(Location::Module(id.clone())) - })?; + .unwrap_or_else(|_| { + eprintln!("WARNING: failed to insert module {:?} into cache; cache capacity might be too small", id.short_str_lossless().to_string()); + None + }); Some(code_wrapper) } None => None, diff --git a/crates/storage/src/script_cache.rs b/crates/storage/src/script_cache.rs index b7ee58c7..61b7b08a 100644 --- a/crates/storage/src/script_cache.rs +++ b/crates/storage/src/script_cache.rs @@ -2,10 +2,9 @@ use std::{hash::RandomState, num::NonZeroUsize, sync::Arc}; use clru::{CLruCache, CLruCacheConfig}; use move_binary_format::{ - errors::{Location, PartialVMError, VMResult}, + errors::VMResult, file_format::CompiledScript, }; -use move_core_types::vm_status::StatusCode; use move_vm_runtime::Script; use move_vm_types::code::Code; use parking_lot::Mutex; @@ -49,11 +48,10 @@ impl InitiaScriptCache { // error occurs when the new script has a weight greater than the cache capacity script_cache .put_with_weight(key, ScriptWrapper::new(new_script, allocated_size)) - .map_err(|_| { - PartialVMError::new(StatusCode::MEMORY_LIMIT_EXCEEDED) - .with_message("Script storage cache eviction error".to_string()) - .finish(Location::Script) - })?; + .unwrap_or_else(|_| { + eprintln!("WARNING: failed to insert script into cache; cache capacity might be too small"); + None + }); Ok(deserialized_script) } @@ -88,11 +86,10 @@ impl InitiaScriptCache { if new_script.is_some() { script_cache .put_with_weight(key, ScriptWrapper::new(new_script.unwrap(), allocated_size)) - .map_err(|_| { - PartialVMError::new(StatusCode::MEMORY_LIMIT_EXCEEDED) - .with_message("Script storage cache eviction error".to_string()) - .finish(Location::Script) - })?; + .unwrap_or_else(|_| { + eprintln!("WARNING: failed to insert script into cache; cache capacity might be too small"); + None + }); } Ok(verified_script) }