Skip to content

Commit 5e3d212

Browse files
committed
Use serialize_to_file/deserialize_from_file from Wasmer
1 parent 1f08608 commit 5e3d212

File tree

8 files changed

+22
-42
lines changed

8 files changed

+22
-42
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/burner/Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/hackatom/Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/queue/Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/reflect/Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/staking/Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/vm/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ clru = "0.2.0"
3535
# Uses the path when built locally; uses the given version from crates.io when published
3636
cosmwasm-std = { path = "../std", version = "0.12.1" }
3737
hex = "0.4"
38-
memmap = "0.7"
3938
parity-wasm = "0.42"
4039
schemars = "0.7"
4140
serde = { version = "1.0.103", default-features = false, features = ["derive", "alloc"] }

packages/vm/src/modules/file_system_cache.rs

+22-35
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
// copied from https://github.com/wasmerio/wasmer/blob/0.8.0/lib/runtime/src/cache.rs
2-
// with some minor modifications
1+
use std::fs;
2+
use std::io;
3+
use std::path::PathBuf;
34

4-
use memmap::Mmap;
5-
use std::{
6-
fs::{self, File},
7-
io::{self, ErrorKind, Write},
8-
path::PathBuf,
9-
};
10-
11-
use wasmer::Module;
5+
use wasmer::{DeserializeError, Module};
126

137
use crate::checksum::Checksum;
148
use crate::errors::{VmError, VmResult};
@@ -76,40 +70,33 @@ impl FileSystemCache {
7670
.join(MODULE_SERIALIZATION_VERSION)
7771
.join(filename);
7872

79-
let file = match File::open(file_path) {
80-
Ok(file) => file,
81-
Err(err) => match err.kind() {
82-
ErrorKind::NotFound => return Ok(None),
83-
_ => {
84-
return Err(VmError::cache_err(format!(
85-
"Error opening module file: {}",
86-
err
87-
)))
88-
}
89-
},
90-
};
91-
92-
let mmap = unsafe { Mmap::map(&file) }
93-
.map_err(|e| VmError::cache_err(format!("Mmap error: {}", e)))?;
94-
9573
let store = make_store_headless(Some(memory_limit));
96-
let module = unsafe { Module::deserialize(&store, &mmap[..]) }?;
97-
Ok(Some(module))
74+
let result = unsafe { Module::deserialize_from_file(&store, &file_path) };
75+
match result {
76+
Ok(module) => Ok(Some(module)),
77+
Err(DeserializeError::Io(err)) => match err.kind() {
78+
io::ErrorKind::NotFound => Ok(None),
79+
_ => Err(VmError::cache_err(format!(
80+
"Error opening module file: {}",
81+
err
82+
))),
83+
},
84+
Err(err) => Err(VmError::cache_err(format!(
85+
"Error deserializing module: {}",
86+
err
87+
))),
88+
}
9889
}
9990

10091
pub fn store(&mut self, checksum: &Checksum, module: &Module) -> VmResult<()> {
10192
let modules_dir = self.path.clone().join(MODULE_SERIALIZATION_VERSION);
10293
fs::create_dir_all(&modules_dir)
10394
.map_err(|e| VmError::cache_err(format!("Error creating direcory: {}", e)))?;
104-
105-
let buffer = module.serialize()?;
106-
10795
let filename = checksum.to_hex();
108-
let mut file = File::create(modules_dir.join(filename))
109-
.map_err(|e| VmError::cache_err(format!("Error creating module file: {}", e)))?;
110-
file.write_all(&buffer)
96+
let path = modules_dir.join(filename);
97+
module
98+
.serialize_to_file(path)
11199
.map_err(|e| VmError::cache_err(format!("Error writing module to disk: {}", e)))?;
112-
113100
Ok(())
114101
}
115102
}

0 commit comments

Comments
 (0)