From 4ed1e3eaadd42b860fb6b6fc51e3f8a755c4608e Mon Sep 17 00:00:00 2001 From: vms Date: Wed, 10 Jun 2020 18:00:07 +0300 Subject: [PATCH] adjust ipfs rpc module to use wasi --- examples/ipfs_node/src/imports.rs | 2 +- examples/ipfs_node/src/main.rs | 10 +-- examples/ipfs_node/src/node.rs | 27 ++++---- .../ipfs_node/src/node_public_interface.rs | 6 +- examples/ipfs_node/wasm/ipfs_rpc/src/lib.rs | 61 +++++++++++++++---- 5 files changed, 76 insertions(+), 30 deletions(-) diff --git a/examples/ipfs_node/src/imports.rs b/examples/ipfs_node/src/imports.rs index 6e709eec..c81b6203 100644 --- a/examples/ipfs_node/src/imports.rs +++ b/examples/ipfs_node/src/imports.rs @@ -45,7 +45,7 @@ pub(super) fn create_host_import_func(_host_cmd: String) -> DynamicFunc<'static> let wasm_ptr = WasmPtr::::new(array_ptr as _); match wasm_ptr.get_utf8_string(ctx.memory(0), array_size as _) { Some(msg) => print!("{}", msg), - None => print!("callback: incorrect UTF8 string's been supplied to logger"), + None => println!("callback: incorrect UTF8 string's been supplied to logger"), } vec![Value::I32(0x1337)] diff --git a/examples/ipfs_node/src/main.rs b/examples/ipfs_node/src/main.rs index 1ef0854b..bdc77b13 100644 --- a/examples/ipfs_node/src/main.rs +++ b/examples/ipfs_node/src/main.rs @@ -20,6 +20,7 @@ mod config; mod imports; mod node_public_interface; +use fce::IValue; use node::IpfsNode; use std::path::PathBuf; @@ -34,16 +35,17 @@ const IPFS_RPC: &str = "/Users/mike/dev/work/fluence/wasm/fce/bin/wasm_ipfs_rpc_ fn main() { let ipfs_rpc = std::fs::read(IPFS_RPC).unwrap(); - let mut ipfs_node = crate::IpfsNode::new( + let mut ipfs_node = IpfsNode::new( PathBuf::from(IPFS_MODULES_DIR), PathBuf::from(IPFS_MODULES_CONFIG_PATH), ) .unwrap(); + println!("ipfs node interface is\n{}", ipfs_node.get_interface()); - println!("ipfs node interface is {}", ipfs_node.get_interface()); - - let result = ipfs_node.rpc_call(&ipfs_rpc, &[]).unwrap(); + let result = ipfs_node + .rpc_call(&ipfs_rpc, &[IValue::String("asdsad".to_string())]) + .unwrap(); println!("execution result {:?}", result); } diff --git a/examples/ipfs_node/src/node.rs b/examples/ipfs_node/src/node.rs index e880bcd6..9da20999 100644 --- a/examples/ipfs_node/src/node.rs +++ b/examples/ipfs_node/src/node.rs @@ -37,14 +37,19 @@ pub struct IpfsNode { } impl IpfsNode { - pub fn new>(core_modules_dir: P, config_file_path: P) -> Result { + pub fn new>( + core_modules_dir: P, + config_file_path: P, + ) -> Result { let mut wasm_process = FCE::new(); let mut module_names = Vec::new(); - let mut core_modules_config = crate::config::parse_config_from_file(config_file_path.into())?; + let mut core_modules_config = + crate::config::parse_config_from_file(config_file_path.into())?; for entry in fs::read_dir(core_modules_dir.into())? { let path = entry?.path(); if path.is_dir() { + // just pass directories continue; } @@ -56,13 +61,15 @@ impl IpfsNode { let module_bytes = fs::read(path.clone())?; - let core_module_config = - Self::make_wasm_process_config(core_modules_config.modules_config.remove(&module_name))?; + let core_module_config = Self::make_wasm_process_config( + core_modules_config.modules_config.remove(&module_name), + )?; wasm_process.load_module(module_name.clone(), &module_bytes, core_module_config)?; module_names.push(module_name); } - let rpc_module_config = Self::make_wasm_process_config(core_modules_config.rpc_module_config)?; + let rpc_module_config = + Self::make_wasm_process_config(core_modules_config.rpc_module_config)?; Ok(Self { process: wasm_process, @@ -77,11 +84,7 @@ impl IpfsNode { self.process .load_module(rpc_module_name, wasm_rpc, self.rpc_module_config.clone())?; - let call_result = self.process.call( - rpc_module_name, - "invoke", - args - )?; + let call_result = self.process.call(rpc_module_name, "invoke", args)?; self.process.unload_module(rpc_module_name)?; Ok(call_result) @@ -101,7 +104,9 @@ impl IpfsNode { NodePublicInterface { modules } } - fn make_wasm_process_config(config: Option) -> Result { + fn make_wasm_process_config( + config: Option, + ) -> Result { use crate::imports::create_host_import_func; use crate::imports::log_utf8_string; use wasmer_core::import::Namespace; diff --git a/examples/ipfs_node/src/node_public_interface.rs b/examples/ipfs_node/src/node_public_interface.rs index 283c5c7b..010422d1 100644 --- a/examples/ipfs_node/src/node_public_interface.rs +++ b/examples/ipfs_node/src/node_public_interface.rs @@ -42,7 +42,11 @@ impl<'a> fmt::Display for NodeModulePublicInterface<'a> { write!(f, "{}\n", self.name)?; for function in self.functions.iter() { - write!(f, " pub fn {}({:?}) -> {:?}\n", function.name, function.inputs, function.outputs)?; + write!( + f, + " pub fn {}({:?}) -> {:?}\n", + function.name, function.inputs, function.outputs + )?; } Ok(()) diff --git a/examples/ipfs_node/wasm/ipfs_rpc/src/lib.rs b/examples/ipfs_node/wasm/ipfs_rpc/src/lib.rs index 5c20fbd5..f6636e25 100644 --- a/examples/ipfs_node/wasm/ipfs_rpc/src/lib.rs +++ b/examples/ipfs_node/wasm/ipfs_rpc/src/lib.rs @@ -19,16 +19,48 @@ mod mem; mod result; use crate::result::{RESULT_PTR, RESULT_SIZE}; +use std::fs; +use std::path::PathBuf; #[no_mangle] -pub unsafe fn invoke(file_content_ptr: *mut u8, file_content_size: usize) { - let file_content = - String::from_raw_parts(file_content_ptr, file_content_size, file_content_size); - let msg = format!("from Wasm rpc: file_content is {}\n", file_content); - log_utf8_string(msg.as_ptr() as _, msg.len() as _); +pub unsafe fn get(hash_ptr: *mut u8, hash_size: usize) { + let hash = String::from_raw_parts(hash_ptr, hash_size, hash_size); + + let msg = format!("from Wasm rpc: getting file with hash {}\n", hash); log_utf8_string(msg.as_ptr() as _, msg.len() as _); - put(file_content.as_ptr() as _, file_content.len() as _); + ipfs_get(hash.as_ptr() as _, hash.len() as _); + + let file_path = String::from_raw_parts( + *RESULT_PTR.get_mut() as _, + *RESULT_SIZE.get_mut(), + *RESULT_SIZE.get_mut(), + ); + + let msg = format!("from Wasm rpc: reading file from {}\n", file_path); + log_utf8_string(msg.as_ptr() as _, msg.len() as _); + + let file_content = fs::read(file_path).unwrap_or_else(|_| b"error while reading file".to_vec()); + + *RESULT_PTR.get_mut() = file_content.as_ptr() as _; + *RESULT_SIZE.get_mut() = file_content.len(); + std::mem::forget(file_content); +} + +#[no_mangle] +pub unsafe fn put(file_content_ptr: *mut u8, file_content_size: usize) { + let file_content = + String::from_raw_parts(file_content_ptr, file_content_size, file_content_size); + + let msg = format!("from Wasm rpc: file_content is {}\n", file_content); + log_utf8_string(msg.as_ptr() as _, msg.len() as _); + + let rpc_tmp_filepath = "/tmp/ipfs_rpc.tmp".to_string(); + + let _ = fs::write(PathBuf::from(rpc_tmp_filepath.clone()), file_content); + + ipfs_put(rpc_tmp_filepath.as_ptr() as _, rpc_tmp_filepath.len() as _); + std::mem::forget(rpc_tmp_filepath); let hash = String::from_raw_parts( *RESULT_PTR.get_mut() as _, @@ -36,10 +68,12 @@ pub unsafe fn invoke(file_content_ptr: *mut u8, file_content_size: usize) { *RESULT_SIZE.get_mut(), ); - let result_msg = format!("result from Wasm rpc: {}\n", hash); - *RESULT_PTR.get_mut() = result_msg.as_ptr() as _; - *RESULT_SIZE.get_mut() = result_msg.len(); - std::mem::forget(result_msg); + let msg = format!("from Wasm rpc: file add with hash {}\n", hash); + log_utf8_string(msg.as_ptr() as _, msg.len() as _); + + *RESULT_PTR.get_mut() = hash.as_ptr() as _; + *RESULT_SIZE.get_mut() = hash.len(); + std::mem::forget(hash); } #[link(wasm_import_module = "host")] @@ -51,9 +85,10 @@ extern "C" { #[link(wasm_import_module = "ipfs_node.wasm")] extern "C" { /// Put a file to ipfs, returns ipfs hash of the file. - fn put(ptr: i32, size: i32); + #[link_name = "put"] + fn ipfs_put(ptr: i32, size: i32); - #[allow(unused)] /// Get file from ipfs by hash. - fn get(ptr: i32, size: i32); + #[link_name = "get"] + fn ipfs_get(ptr: i32, size: i32); }