From c5d5bda4e31dfce7b96d3e6d6efed8b9526da17e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Leegwater=20Sim=C3=B5es?= Date: Tue, 7 May 2024 17:36:26 +0200 Subject: [PATCH] Drop `wasmer` in favor of `wasmtime` for tests Since we've transitioned to `wasmtime` for the entirety of our stack, we should also use it for the tests in this crate. This commit does exactly that, doing away with the `wasmer` dependency, and replacing it with `wasmtime`. This also addresses some - if not all - of the problems we've been having with running the tests. They now pass when compiling them using `release` compile option. However, they still fail on `debug`. See-also: #105 --- Cargo.toml | 2 +- tests/wallet.rs | 83 +++++++++++++++++++++++++------------------------ 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 604c5f1..a4f1d55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,7 +71,7 @@ rusk-abi = { version = "0.12.0-rc.0", default-features = false } [dev-dependencies] rand = "^0.8" -wasmer = "=3.1" +wasmtime = "20" [build-dependencies] schemafy_lib = "0.6" diff --git a/tests/wallet.rs b/tests/wallet.rs index 9cf60fe..f785782 100644 --- a/tests/wallet.rs +++ b/tests/wallet.rs @@ -19,7 +19,7 @@ use phoenix_core::Crossover; use rusk_abi::ContractId; use serde::{Deserialize, Serialize}; use serde_json::json; -use wasmer::{imports, Instance, Module, Store, Value}; +use wasmtime::{Engine, Instance, Module, Store, Val}; #[test] fn seed_works() { @@ -356,7 +356,7 @@ mod node { } pub struct Wallet { - pub store: Store, + pub store: Store<()>, pub module: Module, pub instance: Instance, } @@ -386,21 +386,19 @@ impl<'a> CallResult<'a> { self.wallet .instance - .exports - .get_memory("memory") - .unwrap() - .view(&self.wallet.store) - .read(self.val as u64, &mut bytes) + .get_memory(&mut self.wallet.store, "memory") + .expect("There should be one memory") + .read(&mut self.wallet.store, self.val as usize, &mut bytes) .unwrap(); self.wallet .instance - .exports - .get_function("free_mem") - .unwrap() + .get_func(&mut self.wallet.store, "free_mem") + .expect("free_mem should exist") .call( &mut self.wallet.store, - &[Value::I32(self.val as i32), Value::I32(self.aux as i32)], + &[Val::I32(self.val as i32), Val::I32(self.aux as i32)], + &mut [], ) .unwrap(); @@ -429,35 +427,39 @@ impl Wallet { T: Serialize, { let bytes = serde_json::to_string(&args).unwrap(); - let len = Value::I32(bytes.len() as i32); - let ptr = self + + let len_params = [Val::I32(bytes.len() as i32)]; + let mut ptr_results = [Val::I32(0)]; + + let allocate = self .instance - .exports - .get_function("allocate") - .unwrap() - .call(&mut self.store, &[len.clone()]) - .unwrap()[0] - .unwrap_i32(); + .get_func(&mut self.store, "allocate") + .expect("allocate should exist"); - self.instance - .exports - .get_memory("memory") - .unwrap() - .view(&self.store) - .write(ptr as u64, bytes.as_bytes()) + allocate + .call(&mut self.store, &len_params, &mut ptr_results) .unwrap(); - let ptr = Value::I32(ptr); - let result = self - .instance - .exports - .get_function(f) - .unwrap() - .call(&mut self.store, &[ptr, len]) - .unwrap()[0] - .unwrap_i64(); + self.instance + .get_memory(&mut self.store, "memory") + .expect("There should be one memory") + .write( + &mut self.store, + ptr_results[0].unwrap_i32() as usize, + bytes.as_bytes(), + ) + .expect("Writing to memory should succeed"); + + let params = [ptr_results[0].clone(), len_params[0].clone()]; + let mut results = [Val::I64(0)]; - CallResult::new(self, result) + self.instance + .get_func(&mut self.store, f) + .expect("allocate should exist") + .call(&mut self.store, ¶ms, &mut results) + .unwrap(); + + CallResult::new(self, results[0].unwrap_i64()) } } @@ -465,13 +467,14 @@ impl Default for Wallet { fn default() -> Self { const WALLET: &[u8] = include_bytes!("../assets/dusk_wallet_core.wasm"); - let mut store = Store::default(); + let engine = Engine::default(); + let mut store = Store::new(&engine, ()); + let module = - Module::new(&store, WALLET).expect("failed to create wasm module"); + Module::new(&engine, WALLET).expect("failed to create wasm module"); - let import_object = imports! {}; - let instance = Instance::new(&mut store, &module, &import_object) - .expect("failed to instanciate the wasm module"); + let instance = Instance::new(&mut store, &module, &[]) + .expect("failed to instantiate the wasm module"); Self { store,