adjust ipfs rpc module to use wasi

This commit is contained in:
vms 2020-06-10 18:00:07 +03:00
parent bbe4612a40
commit 4ed1e3eaad
5 changed files with 76 additions and 30 deletions

View File

@ -45,7 +45,7 @@ pub(super) fn create_host_import_func(_host_cmd: String) -> DynamicFunc<'static>
let wasm_ptr = WasmPtr::<u8, Array>::new(array_ptr as _); let wasm_ptr = WasmPtr::<u8, Array>::new(array_ptr as _);
match wasm_ptr.get_utf8_string(ctx.memory(0), array_size as _) { match wasm_ptr.get_utf8_string(ctx.memory(0), array_size as _) {
Some(msg) => print!("{}", msg), 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)] vec![Value::I32(0x1337)]

View File

@ -20,6 +20,7 @@ mod config;
mod imports; mod imports;
mod node_public_interface; mod node_public_interface;
use fce::IValue;
use node::IpfsNode; use node::IpfsNode;
use std::path::PathBuf; 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() { fn main() {
let ipfs_rpc = std::fs::read(IPFS_RPC).unwrap(); 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_DIR),
PathBuf::from(IPFS_MODULES_CONFIG_PATH), PathBuf::from(IPFS_MODULES_CONFIG_PATH),
) )
.unwrap(); .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, &[IValue::String("asdsad".to_string())])
let result = ipfs_node.rpc_call(&ipfs_rpc, &[]).unwrap(); .unwrap();
println!("execution result {:?}", result); println!("execution result {:?}", result);
} }

View File

@ -37,14 +37,19 @@ pub struct IpfsNode {
} }
impl IpfsNode { impl IpfsNode {
pub fn new<P: Into<PathBuf>>(core_modules_dir: P, config_file_path: P) -> Result<Self, NodeError> { pub fn new<P: Into<PathBuf>>(
core_modules_dir: P,
config_file_path: P,
) -> Result<Self, NodeError> {
let mut wasm_process = FCE::new(); let mut wasm_process = FCE::new();
let mut module_names = Vec::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())? { for entry in fs::read_dir(core_modules_dir.into())? {
let path = entry?.path(); let path = entry?.path();
if path.is_dir() { if path.is_dir() {
// just pass directories
continue; continue;
} }
@ -56,13 +61,15 @@ impl IpfsNode {
let module_bytes = fs::read(path.clone())?; let module_bytes = fs::read(path.clone())?;
let core_module_config = let core_module_config = Self::make_wasm_process_config(
Self::make_wasm_process_config(core_modules_config.modules_config.remove(&module_name))?; core_modules_config.modules_config.remove(&module_name),
)?;
wasm_process.load_module(module_name.clone(), &module_bytes, core_module_config)?; wasm_process.load_module(module_name.clone(), &module_bytes, core_module_config)?;
module_names.push(module_name); 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 { Ok(Self {
process: wasm_process, process: wasm_process,
@ -77,11 +84,7 @@ impl IpfsNode {
self.process self.process
.load_module(rpc_module_name, wasm_rpc, self.rpc_module_config.clone())?; .load_module(rpc_module_name, wasm_rpc, self.rpc_module_config.clone())?;
let call_result = self.process.call( let call_result = self.process.call(rpc_module_name, "invoke", args)?;
rpc_module_name,
"invoke",
args
)?;
self.process.unload_module(rpc_module_name)?; self.process.unload_module(rpc_module_name)?;
Ok(call_result) Ok(call_result)
@ -101,7 +104,9 @@ impl IpfsNode {
NodePublicInterface { modules } NodePublicInterface { modules }
} }
fn make_wasm_process_config(config: Option<ModuleConfig>) -> Result<FCEModuleConfig, NodeError> { fn make_wasm_process_config(
config: Option<ModuleConfig>,
) -> Result<FCEModuleConfig, NodeError> {
use crate::imports::create_host_import_func; use crate::imports::create_host_import_func;
use crate::imports::log_utf8_string; use crate::imports::log_utf8_string;
use wasmer_core::import::Namespace; use wasmer_core::import::Namespace;

View File

@ -42,7 +42,11 @@ impl<'a> fmt::Display for NodeModulePublicInterface<'a> {
write!(f, "{}\n", self.name)?; write!(f, "{}\n", self.name)?;
for function in self.functions.iter() { 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(()) Ok(())

View File

@ -19,16 +19,48 @@ mod mem;
mod result; mod result;
use crate::result::{RESULT_PTR, RESULT_SIZE}; use crate::result::{RESULT_PTR, RESULT_SIZE};
use std::fs;
use std::path::PathBuf;
#[no_mangle] #[no_mangle]
pub unsafe fn invoke(file_content_ptr: *mut u8, file_content_size: usize) { pub unsafe fn get(hash_ptr: *mut u8, hash_size: usize) {
let file_content = let hash = String::from_raw_parts(hash_ptr, hash_size, hash_size);
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); let msg = format!("from Wasm rpc: getting file with hash {}\n", hash);
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
log_utf8_string(msg.as_ptr() as _, msg.len() as _); 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( let hash = String::from_raw_parts(
*RESULT_PTR.get_mut() as _, *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(), *RESULT_SIZE.get_mut(),
); );
let result_msg = format!("result from Wasm rpc: {}\n", hash); let msg = format!("from Wasm rpc: file add with hash {}\n", hash);
*RESULT_PTR.get_mut() = result_msg.as_ptr() as _; log_utf8_string(msg.as_ptr() as _, msg.len() as _);
*RESULT_SIZE.get_mut() = result_msg.len();
std::mem::forget(result_msg); *RESULT_PTR.get_mut() = hash.as_ptr() as _;
*RESULT_SIZE.get_mut() = hash.len();
std::mem::forget(hash);
} }
#[link(wasm_import_module = "host")] #[link(wasm_import_module = "host")]
@ -51,9 +85,10 @@ extern "C" {
#[link(wasm_import_module = "ipfs_node.wasm")] #[link(wasm_import_module = "ipfs_node.wasm")]
extern "C" { extern "C" {
/// Put a file to ipfs, returns ipfs hash of the file. /// 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. /// Get file from ipfs by hash.
fn get(ptr: i32, size: i32); #[link_name = "get"]
fn ipfs_get(ptr: i32, size: i32);
} }