find ipsf address by the env variable

This commit is contained in:
vms 2020-06-30 14:45:04 +03:00
parent dd1d1a0951
commit c907865444
5 changed files with 19 additions and 26 deletions

View File

@ -8,7 +8,7 @@
ipfs = "/usr/local/bin/ipfs"
[core_module.wasi]
envs = []
envs = ["IPFS_ADDR=/dns4/relay02.fluence.dev/tcp/15001"]
preopened_files = ["./wasm/artifacts"]
mapped_dirs = { "tmp" = "./wasm/artifacts" }

View File

@ -34,11 +34,11 @@ fn main() {
println!("ipfs node interface is\n{}", ipfs_node.get_interface());
let node_addresses = ipfs_node
.call_module("ipfs_node.wasm", "get_addresses", &[])
let node_address = ipfs_node
.call_module("ipfs_node.wasm", "get_address", &[])
.unwrap();
println!("ipfs node addresses are:\n{:?}", node_addresses);
println!("ipfs node address is:\n{:?}", node_address);
let result = ipfs_node
.call_code(

View File

@ -24,6 +24,7 @@ use crate::result::{RESULT_PTR, RESULT_SIZE};
use crate::path::to_full_path;
const RESULT_FILE_PATH: &str = "/tmp/ipfs_rpc_file";
const IPFS_ADDR_ENV_NAME: &str = "IPFS_ADDR";
pub fn main() {
let msg = "ipfs_node.main: WASI initialization finished";
@ -92,32 +93,24 @@ pub unsafe fn get(hash_ptr: *mut u8, hash_size: usize) {
}
#[no_mangle]
pub unsafe fn get_addresses() {
let msg = "ipfs_node.get_addresses".to_string();
pub unsafe fn get_address() {
let msg = "ipfs_node.get_address".to_string();
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
let cmd = "id -f'<addrs>'";
let result = ipfs(cmd.as_ptr() as _, cmd.len() as _);
let multiaddrs = if result == 0 {
String::from_raw_parts(
*RESULT_PTR.get_mut() as _,
*RESULT_SIZE.get_mut(),
*RESULT_SIZE.get_mut(),
)
} else {
"host ipfs call failed".to_string()
let ipfs_address = match std::env::var(IPFS_ADDR_ENV_NAME) {
Ok(addr) => addr,
Err(e) => format!("getting {} env variable failed with error {:?}", IPFS_ADDR_ENV_NAME, e)
};
let msg = format!(
"ipfs_node.get_addresses: node addresses are {} \n",
multiaddrs
"ipfs_node.get_address: node address is {} \n",
ipfs_address
);
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
*RESULT_PTR.get_mut() = multiaddrs.as_ptr() as _;
*RESULT_SIZE.get_mut() = multiaddrs.len();
std::mem::forget(multiaddrs);
*RESULT_PTR.get_mut() = ipfs_address.as_ptr() as _;
*RESULT_SIZE.get_mut() = ipfs_address.len();
std::mem::forget(ipfs_address);
}
#[link(wasm_import_module = "host")]

View File

@ -25,10 +25,10 @@
;; import ipfs put/get function
(@interface type (func (param string) (result string))) ;; 8
;; export ipfs.get_addresses function
;; export ipfs.get_address function
(@interface type (func (result string))) ;; 9
;; export ipfs.get_addresses function
;; export ipfs.get_address function
(@interface type (func )) ;; 10
(@interface export "allocate" (func 0)) ;; 0
@ -40,7 +40,7 @@
(@interface export "put" (func 5)) ;; 6
(@interface export "get" (func 6)) ;; 7
(@interface export "get_addresses" (func 10)) ;; 8
(@interface export "get_address" (func 10)) ;; 8
;; adapter for export function put
(@interface func (type 7)
@ -74,7 +74,7 @@
call-core 1 ;; call deallocate
)
;; adapter for export function get_addresses
;; adapter for export function get_address
(@interface func (type 9)
call-core 8 ;; call self.get
call-core 3 ;; call get_result_ptr