Skip to content

Commit 1af0216

Browse files
committed
Rewrite imports
1 parent f148283 commit 1af0216

File tree

7 files changed

+50
-62
lines changed

7 files changed

+50
-62
lines changed

packages/vm/src/cache.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::{error::Error, checksum::Checksum};
1+
use crate::checksum::Checksum;
2+
use crate::error::Error;
23

34
use clru::CLruCache;
45
use wasmer::{Instance, Module, Store};

packages/vm/src/calls.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::cache::Cache;
2+
use crate::error::Error;
23
use crate::imports::create_import_object;
34
use crate::store::make_store;
4-
use crate::vm::{self, Environment};
5+
use crate::vm::{Env, Environment};
56

6-
pub use crate::error::Error;
7-
pub use std::ptr::NonNull;
7+
use std::ptr::NonNull;
88
use wasmer_middlewares::metering::{get_remaining_points, MeteringPoints};
99

1010
pub fn run<E>(
@@ -15,7 +15,7 @@ pub fn run<E>(
1515
env: E,
1616
) -> Result<u64, Error>
1717
where
18-
E: vm::Env + 'static,
18+
E: Env + 'static,
1919
{
2020
let owasm_env = Environment::new(env);
2121
let store = make_store();
@@ -64,7 +64,7 @@ mod test {
6464

6565
pub struct MockEnv {}
6666

67-
impl vm::Env for MockEnv {
67+
impl Env for MockEnv {
6868
fn get_span_size(&self) -> i64 {
6969
300
7070
}

packages/vm/src/checksum.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use std::convert::TryFrom;
1+
use crate::Error;
22

33
use sha2::{Digest, Sha256};
4-
5-
use crate::Error;
4+
use std::convert::TryFrom;
65

76
/// This is a copy of checksum.rs from https://github.com/CosmWasm/cosmwasm/blob/6082e8a35a193f1365cb3367f77bd87c593a7ae4/packages/vm/src/checksum.rs
8-
///
7+
///
98
/// A SHA-256 checksum of a Wasm blob, used to identify a Wasm code.
109
/// This must remain stable since this checksum is stored in the blockchain state.
1110
///

packages/vm/src/compile.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use crate::Error;
2+
13
use wasm_instrument::parity_wasm::{
24
builder,
3-
elements::{self, External, MemoryType, Module},
5+
elements::{deserialize_buffer, serialize, External, MemoryType, Module},
46
};
57
use wasmer::wasmparser;
68

7-
use crate::Error;
8-
99
// inspired by https://github.com/CosmWasm/cosmwasm/issues/81
1010
// 512 pages = 32mb
1111
static MEMORY_LIMIT: u32 = 512; // in pages
@@ -32,14 +32,14 @@ pub fn compile(code: &[u8]) -> Result<Vec<u8>, Error> {
3232
wasmparser::validate(code).map_err(|_| Error::ValidationError)?;
3333

3434
// Start the compiling chains.
35-
let module = elements::deserialize_buffer(code).map_err(|_| Error::DeserializationError)?;
35+
let module = deserialize_buffer(code).map_err(|_| Error::DeserializationError)?;
3636
check_wasm_exports(&module)?;
3737
check_wasm_imports(&module)?;
3838
let module = inject_memory(module)?;
3939
let module = inject_stack_height(module)?;
4040

4141
// Serialize the final Wasm code back to bytes.
42-
elements::serialize(module).map_err(|_| Error::SerializationError)
42+
serialize(module).map_err(|_| Error::SerializationError)
4343
}
4444

4545
fn check_wasm_exports(module: &Module) -> Result<(), Error> {
@@ -114,14 +114,11 @@ fn inject_stack_height(module: Module) -> Result<Module, Error> {
114114
#[cfg(test)]
115115
mod test {
116116
use super::*;
117-
use assert_matches::assert_matches;
118-
use std::{
119-
io::{Read, Write},
120-
process::Command,
121-
};
122117

118+
use assert_matches::assert_matches;
119+
use std::io::{Read, Write};
120+
use std::process::Command;
123121
use tempfile::NamedTempFile;
124-
use wasm_instrument::parity_wasm::elements::{self, Module};
125122

126123
fn wat2wasm(wat: impl AsRef<[u8]>) -> Vec<u8> {
127124
let mut input_file = NamedTempFile::new().unwrap();
@@ -141,7 +138,7 @@ mod test {
141138
}
142139

143140
fn get_module_from_wasm(code: &[u8]) -> Module {
144-
match elements::deserialize_buffer(code) {
141+
match deserialize_buffer(code) {
145142
Ok(deserialized) => deserialized,
146143
Err(_) => panic!("Cannot deserialized"),
147144
}
@@ -219,7 +216,7 @@ mod test {
219216
"#,
220217
);
221218
let module = inject_stack_height(get_module_from_wasm(&wasm)).unwrap();
222-
let wasm = elements::serialize(module).unwrap();
219+
let wasm = serialize(module).unwrap();
223220
let expected = wat2wasm(
224221
r#"(module
225222
(type (;0;) (func))

packages/vm/src/imports.rs

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use wasmer::{imports, Function, ImportObject, Store};
1+
use crate::error::Error;
2+
use crate::vm::{Env, Environment};
23

3-
use crate::{
4-
vm::{self, Environment},
5-
Error,
6-
};
4+
use wasmer::{imports, Function, ImportObject, Store};
75

86
// use owasm_crypto::ecvrf;
97

@@ -16,21 +14,21 @@ fn require_mem_range(max_range: usize, require_range: usize) -> Result<(), Error
1614

1715
fn do_gas<E>(env: &Environment<E>, _gas: u32) -> Result<(), Error>
1816
where
19-
E: vm::Env + 'static,
17+
E: Env + 'static,
2018
{
2119
env.decrease_gas_left(12500000)
2220
}
2321

2422
fn do_get_span_size<E>(env: &Environment<E>) -> i64
2523
where
26-
E: vm::Env + 'static,
24+
E: Env + 'static,
2725
{
2826
env.with_vm(|vm| vm.env.get_span_size())
2927
}
3028

3129
fn do_read_calldata<E>(env: &Environment<E>, ptr: i64) -> Result<i64, Error>
3230
where
33-
E: vm::Env + 'static,
31+
E: Env + 'static,
3432
{
3533
env.with_mut_vm(|vm| -> Result<i64, Error> {
3634
let span_size = vm.env.get_span_size();
@@ -50,7 +48,7 @@ where
5048

5149
fn do_set_return_data<E>(env: &Environment<E>, ptr: i64, len: i64) -> Result<(), Error>
5250
where
53-
E: vm::Env + 'static,
51+
E: Env + 'static,
5452
{
5553
env.with_mut_vm(|vm| {
5654
let span_size = vm.env.get_span_size();
@@ -72,35 +70,35 @@ where
7270

7371
fn do_get_ask_count<E>(env: &Environment<E>) -> i64
7472
where
75-
E: vm::Env + 'static,
73+
E: Env + 'static,
7674
{
7775
env.with_vm(|vm| vm.env.get_ask_count())
7876
}
7977

8078
fn do_get_min_count<E>(env: &Environment<E>) -> i64
8179
where
82-
E: vm::Env + 'static,
80+
E: Env + 'static,
8381
{
8482
env.with_vm(|vm| vm.env.get_min_count())
8583
}
8684

8785
fn do_get_prepare_time<E>(env: &Environment<E>) -> i64
8886
where
89-
E: vm::Env + 'static,
87+
E: Env + 'static,
9088
{
9189
env.with_vm(|vm| vm.env.get_prepare_time())
9290
}
9391

9492
fn do_get_execute_time<E>(env: &Environment<E>) -> Result<i64, Error>
9593
where
96-
E: vm::Env + 'static,
94+
E: Env + 'static,
9795
{
9896
env.with_vm(|vm| vm.env.get_execute_time())
9997
}
10098

10199
fn do_get_ans_count<E>(env: &Environment<E>) -> Result<i64, Error>
102100
where
103-
E: vm::Env + 'static,
101+
E: Env + 'static,
104102
{
105103
env.with_vm(|vm| vm.env.get_ans_count())
106104
}
@@ -113,7 +111,7 @@ fn do_ask_external_data<E>(
113111
len: i64,
114112
) -> Result<(), Error>
115113
where
116-
E: vm::Env + 'static,
114+
E: Env + 'static,
117115
{
118116
env.with_mut_vm(|vm| {
119117
let span_size = vm.env.get_span_size();
@@ -135,7 +133,7 @@ where
135133

136134
fn do_get_external_data_status<E>(env: &Environment<E>, eid: i64, vid: i64) -> Result<i64, Error>
137135
where
138-
E: vm::Env + 'static,
136+
E: Env + 'static,
139137
{
140138
env.with_vm(|vm| vm.env.get_external_data_status(eid, vid))
141139
}
@@ -147,7 +145,7 @@ fn do_read_external_data<E>(
147145
ptr: i64,
148146
) -> Result<i64, Error>
149147
where
150-
E: vm::Env + 'static,
148+
E: Env + 'static,
151149
{
152150
env.with_mut_vm(|vm| -> Result<i64, Error> {
153151
let span_size = vm.env.get_span_size();
@@ -175,7 +173,7 @@ where
175173
// alpha_len: i64,
176174
// ) -> Result<u32, Error>
177175
// where
178-
// E: vm::Env + 'static,
176+
// E: Env + 'static,
179177
// {
180178
// env.with_mut_vm(|vm| -> Result<u32, Error> {
181179
// // consume gas relatively to the function running time (~12ms)
@@ -189,7 +187,7 @@ where
189187

190188
pub fn create_import_object<E>(store: &Store, owasm_env: Environment<E>) -> ImportObject
191189
where
192-
E: vm::Env + 'static,
190+
E: Env + 'static,
193191
{
194192
imports! {
195193
"env" => {
@@ -213,30 +211,23 @@ where
213211
#[cfg(test)]
214212
mod test {
215213
use super::*;
216-
use std::{
217-
io::{Read, Write},
218-
process::Command,
219-
ptr::NonNull,
220-
};
221214

222-
use tempfile::NamedTempFile;
223-
224-
use crate::{
225-
cache::{Cache, CacheOptions},
226-
compile,
227-
store::make_store,
228-
vm::{self, Environment},
229-
Error,
230-
};
215+
use crate::cache::{Cache, CacheOptions};
216+
use crate::compile::compile;
217+
use crate::store::make_store;
231218

219+
use std::io::{Read, Write};
220+
use std::process::Command;
221+
use std::ptr::NonNull;
222+
use tempfile::NamedTempFile;
223+
use wasmer::ExternType::Function;
232224
use wasmer::FunctionType;
233-
use wasmer::ValType::I32;
234-
use wasmer::ValType::I64;
235-
use wasmer::{ExternType::Function, Instance};
225+
use wasmer::Instance;
226+
use wasmer::ValType::{I32, I64};
236227

237228
pub struct MockEnv {}
238229

239-
impl vm::Env for MockEnv {
230+
impl Env for MockEnv {
240231
fn get_span_size(&self) -> i64 {
241232
300
242233
}

packages/vm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub mod cache;
22
pub mod calls;
33
mod checksum;
4-
pub mod compile;
4+
mod compile;
55
pub mod error;
66
mod imports;
77
mod store;

packages/vm/src/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::error::Error;
2+
23
use std::borrow::Borrow;
34
use std::ptr::NonNull;
45
use std::sync::{Arc, Mutex, RwLock};
5-
66
use wasmer::{Instance, Memory, WasmerEnv};
77
use wasmer_middlewares::metering::{get_remaining_points, set_remaining_points, MeteringPoints};
88

0 commit comments

Comments
 (0)