Skip to content

Commit

Permalink
Merge pull request #2283 from CosmWasm/mergify/bp/release/2.2/pr-2282
Browse files Browse the repository at this point in the history
Add unit to config field names (backport #2282)
  • Loading branch information
chipshort authored Oct 12, 2024
2 parents f13b0a7 + e376076 commit f5744be
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/check/tests/cosmwasm_check_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn wasm_limits_string_check() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("cosmwasm-check")?;

let mut limits = WasmLimits::default();
limits.initial_memory_limit = Some(10);
limits.initial_memory_limit_pages = Some(10);

cmd.arg("--wasm-limits")
.arg(to_json_string(&limits).unwrap())
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ fn bench_combined(c: &mut Criterion) {

group.bench_function("get instance from fs cache and execute", |b| {
let mut non_memcache = options.clone();
non_memcache.memory_cache_size = Size::kibi(0);
non_memcache.memory_cache_size_bytes = Size::kibi(0);

let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(non_memcache).unwrap() };
Expand Down
36 changes: 18 additions & 18 deletions packages/vm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ where
CacheOptions {
base_dir,
available_capabilities,
memory_cache_size,
instance_memory_limit,
memory_cache_size_bytes,
instance_memory_limit_bytes,
},
wasm_limits,
} = config;
Expand All @@ -165,11 +165,11 @@ where
inner: Mutex::new(CacheInner {
wasm_path,
pinned_memory_cache: PinnedMemoryCache::new(),
memory_cache: InMemoryCache::new(memory_cache_size),
memory_cache: InMemoryCache::new(memory_cache_size_bytes),
fs_cache,
stats: Stats::default(),
}),
instance_memory_limit,
instance_memory_limit: instance_memory_limit_bytes,
type_storage: PhantomData::<S>,
type_api: PhantomData::<A>,
type_querier: PhantomData::<Q>,
Expand Down Expand Up @@ -614,8 +614,8 @@ mod tests {
CacheOptions {
base_dir: TempDir::new().unwrap().into_path(),
available_capabilities: default_capabilities(),
memory_cache_size: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit: TESTING_MEMORY_LIMIT,
memory_cache_size_bytes: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit_bytes: TESTING_MEMORY_LIMIT,
}
}

Expand All @@ -625,8 +625,8 @@ mod tests {
CacheOptions {
base_dir: TempDir::new().unwrap().into_path(),
available_capabilities: capabilities,
memory_cache_size: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit: TESTING_MEMORY_LIMIT,
memory_cache_size_bytes: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit_bytes: TESTING_MEMORY_LIMIT,
}
}

Expand Down Expand Up @@ -755,8 +755,8 @@ mod tests {
let options1 = CacheOptions {
base_dir: tmp_dir.path().to_path_buf(),
available_capabilities: default_capabilities(),
memory_cache_size: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit: TESTING_MEMORY_LIMIT,
memory_cache_size_bytes: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit_bytes: TESTING_MEMORY_LIMIT,
};
let cache1: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options1).unwrap() };
Expand All @@ -767,8 +767,8 @@ mod tests {
let options2 = CacheOptions {
base_dir: tmp_dir.path().to_path_buf(),
available_capabilities: default_capabilities(),
memory_cache_size: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit: TESTING_MEMORY_LIMIT,
memory_cache_size_bytes: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit_bytes: TESTING_MEMORY_LIMIT,
};
let cache2: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options2).unwrap() };
Expand Down Expand Up @@ -800,8 +800,8 @@ mod tests {
let options = CacheOptions {
base_dir: tmp_dir.path().to_path_buf(),
available_capabilities: default_capabilities(),
memory_cache_size: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit: TESTING_MEMORY_LIMIT,
memory_cache_size_bytes: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit_bytes: TESTING_MEMORY_LIMIT,
};
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options).unwrap() };
Expand Down Expand Up @@ -1606,8 +1606,8 @@ mod tests {
let options = CacheOptions {
base_dir: tmp_dir.path().to_path_buf(),
available_capabilities: default_capabilities(),
memory_cache_size: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit: TESTING_MEMORY_LIMIT,
memory_cache_size_bytes: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit_bytes: TESTING_MEMORY_LIMIT,
};
let cache: Cache<MockApi, MockStorage, MockQuerier> =
unsafe { Cache::new(options).unwrap() };
Expand Down Expand Up @@ -1639,8 +1639,8 @@ mod tests {
cache: CacheOptions {
base_dir: tmp_dir.path().to_path_buf(),
available_capabilities: default_capabilities(),
memory_cache_size: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit: TESTING_MEMORY_LIMIT,
memory_cache_size_bytes: TESTING_MEMORY_CACHE_SIZE,
instance_memory_limit_bytes: TESTING_MEMORY_LIMIT,
},
};

Expand Down
6 changes: 3 additions & 3 deletions packages/vm/src/compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn check_wasm_tables(module: &ParsedWasm, wasm_limits: &WasmLimits) -> VmResult<
1 => {
let limits = &module.tables[0];
if let Some(maximum) = limits.maximum {
if maximum > wasm_limits.table_size_limit() {
if maximum > wasm_limits.table_size_limit_elements() {
return Err(VmError::static_validation_err(
"Wasm contract's first table section has a too large max limit",
));
Expand All @@ -148,10 +148,10 @@ fn check_wasm_memories(module: &ParsedWasm, limits: &WasmLimits) -> VmResult<()>
}
let memory = &module.memories[0];

if memory.initial > limits.initial_memory_limit() as u64 {
if memory.initial > limits.initial_memory_limit_pages() as u64 {
return Err(VmError::static_validation_err(format!(
"Wasm contract memory's minimum must not exceed {} pages.",
limits.initial_memory_limit()
limits.initial_memory_limit_pages()
)));
}

Expand Down
26 changes: 14 additions & 12 deletions packages/vm/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct WasmLimits {
///
/// Every Wasm memory has an initial size and an optional maximum size,
/// both measured in Wasm pages. This limit applies to the initial size.
pub initial_memory_limit: Option<u32>,
pub initial_memory_limit_pages: Option<u32>,
/// The upper limit for the `max` value of each table. CosmWasm contracts have
/// initial=max for 1 table. See
///
Expand All @@ -64,7 +64,7 @@ pub struct WasmLimits {
/// - table[0] type=funcref initial=161 max=161
/// ```
///
pub table_size_limit: Option<u32>,
pub table_size_limit_elements: Option<u32>,
/// If the contract has more than this amount of imports, it will be rejected
/// during static validation before even looking into the imports.
pub max_imports: Option<usize>,
Expand All @@ -88,12 +88,14 @@ pub struct WasmLimits {
}

impl WasmLimits {
pub fn initial_memory_limit(&self) -> u32 {
self.initial_memory_limit.unwrap_or(DEFAULT_MEMORY_LIMIT)
pub fn initial_memory_limit_pages(&self) -> u32 {
self.initial_memory_limit_pages
.unwrap_or(DEFAULT_MEMORY_LIMIT)
}

pub fn table_size_limit(&self) -> u32 {
self.table_size_limit.unwrap_or(DEFAULT_TABLE_SIZE_LIMIT)
pub fn table_size_limit_elements(&self) -> u32 {
self.table_size_limit_elements
.unwrap_or(DEFAULT_TABLE_SIZE_LIMIT)
}

pub fn max_imports(&self) -> usize {
Expand Down Expand Up @@ -130,24 +132,24 @@ pub struct CacheOptions {
pub base_dir: PathBuf,
pub available_capabilities: HashSet<String>,
/// Memory limit for the cache, in bytes.
pub memory_cache_size: Size,
pub memory_cache_size_bytes: Size,
/// Memory limit for instances, in bytes. Use a value that is divisible by the Wasm page size 65536,
/// e.g. full MiBs.
pub instance_memory_limit: Size,
pub instance_memory_limit_bytes: Size,
}

impl CacheOptions {
pub fn new(
base_dir: impl Into<PathBuf>,
available_capabilities: impl Into<HashSet<String>>,
memory_cache_size: Size,
instance_memory_limit: Size,
memory_cache_size_bytes: Size,
instance_memory_limit_bytes: Size,
) -> Self {
Self {
base_dir: base_dir.into(),
available_capabilities: available_capabilities.into(),
memory_cache_size,
instance_memory_limit,
memory_cache_size_bytes,
instance_memory_limit_bytes,
}
}
}

0 comments on commit f5744be

Please sign in to comment.