diff --git a/README.md b/README.md new file mode 100644 index 0000000..9f339a8 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +## Moscow blockchain hackathon tutorial + +### Installation + +To compile and run these example you need to install + - rust with wasm32-wasi target +```bash +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh +rustup install nightly +rustup target add wasm32-wasi +``` +- Marine tools +```bash +cargo install marine +cargo +nightly install mrepl +``` +- Fluence deploying tools +```bash +npm -g install @fluencelabs/fldist +``` + +### Building + +To build all the examples just run the following command +```bash +./build.sh +``` + +### Deploying and calling from Aqua + +TBD diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..4ac71d6 --- /dev/null +++ b/build.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +( + cd hello_world || exit; + ./build.sh +) + +( + cd url-downloader || exit; + ./build.sh +) + +( + cd ipfs-node || exit; + ./build.sh +) diff --git a/hello_world/Cargo.lock b/hello-world/Cargo.lock similarity index 100% rename from hello_world/Cargo.lock rename to hello-world/Cargo.lock diff --git a/hello_world/Cargo.toml b/hello-world/Cargo.toml similarity index 100% rename from hello_world/Cargo.toml rename to hello-world/Cargo.toml diff --git a/hello_world/Config.toml b/hello-world/Config.toml similarity index 100% rename from hello_world/Config.toml rename to hello-world/Config.toml diff --git a/hello_world/artifacts/greeting.wasm b/hello-world/artifacts/greeting.wasm similarity index 99% rename from hello_world/artifacts/greeting.wasm rename to hello-world/artifacts/greeting.wasm index e5703c5..843ed21 100755 Binary files a/hello_world/artifacts/greeting.wasm and b/hello-world/artifacts/greeting.wasm differ diff --git a/hello_world/build.sh b/hello-world/build.sh similarity index 100% rename from hello_world/build.sh rename to hello-world/build.sh diff --git a/hello_world/src/main.rs b/hello-world/src/main.rs similarity index 100% rename from hello_world/src/main.rs rename to hello-world/src/main.rs diff --git a/ipfs-node/Config.toml b/ipfs-node/Config.toml new file mode 100644 index 0000000..318157a --- /dev/null +++ b/ipfs-node/Config.toml @@ -0,0 +1,17 @@ +modules_dir = "artifacts/" + +[[module]] + name = "ipfs_effector" + max_heap_size = "10 KiB" + logger_enabled = true + + [module.mounted_binaries] + ipfs = "/usr/local/bin/ipfs" + + [module.wasi] + envs = { "IPFS_ADDR" = "/dns4/relay02.fluence.dev/tcp/15001", "timeout" = "1s" } + +[[module]] + name = "ipfs_pure" + max_heap_size = "10 KiB" + logger_enabled = true diff --git a/ipfs-node/artifacts/ipfs_effector.wasm b/ipfs-node/artifacts/ipfs_effector.wasm new file mode 100755 index 0000000..a46d4bb Binary files /dev/null and b/ipfs-node/artifacts/ipfs_effector.wasm differ diff --git a/ipfs-node/artifacts/ipfs_pure.wasm b/ipfs-node/artifacts/ipfs_pure.wasm new file mode 100755 index 0000000..8046d13 Binary files /dev/null and b/ipfs-node/artifacts/ipfs_pure.wasm differ diff --git a/ipfs-node/build.sh b/ipfs-node/build.sh new file mode 100755 index 0000000..5bb1a73 --- /dev/null +++ b/ipfs-node/build.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +( + cd effector || exit; + cargo update --aggressive; + marine build --release; +) + +( + cd pure || exit; + cargo update --aggressive; + marine build --release; +) + +rm artifacts/* || true +mkdir -p artifacts + +cp ./effector/target/wasm32-wasi/release/ipfs_effector.wasm artifacts/ +cp ./pure/target/wasm32-wasi/release/ipfs_pure.wasm artifacts/ diff --git a/ipfs-node/effector/Cargo.lock b/ipfs-node/effector/Cargo.lock new file mode 100644 index 0000000..b7d7150 --- /dev/null +++ b/ipfs-node/effector/Cargo.lock @@ -0,0 +1,275 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + +[[package]] +name = "getrandom" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "ipfs-effector" +version = "0.1.0" +dependencies = [ + "log", + "marine-rs-sdk", +] + +[[package]] +name = "itoa" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" + +[[package]] +name = "libc" +version = "0.2.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "marine-macro" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc907943772cf966ebe2a2d462d09bc79e898ef102ed065ba3d3abcc93fbb71" +dependencies = [ + "marine-macro-impl", + "marine-rs-sdk-main", +] + +[[package]] +name = "marine-macro-impl" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfdbed3a10ae9b22df06bee8cd0023255358935aae8e8daf9d1a006cfaeeb11e" +dependencies = [ + "proc-macro2", + "quote", + "serde", + "serde_json", + "syn", + "uuid", +] + +[[package]] +name = "marine-rs-sdk" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d17d44016675abdf18caf3ac1bdd4437cc17ec0db019da141bb06a9ea991726" +dependencies = [ + "marine-macro", + "marine-rs-sdk-main", + "marine-timestamp-macro", + "polyplets", + "serde", +] + +[[package]] +name = "marine-rs-sdk-main" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ff262801664ce22f9d274504d6e8088c217034aba0ca431eca8d0258f3699c6" +dependencies = [ + "log", + "serde", +] + +[[package]] +name = "marine-timestamp-macro" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85a9f5b1adeff98ac5a14cfc5fa814185a807ba4a0d3849c9f9e9fd868b27d1f" +dependencies = [ + "chrono", + "quote", +] + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "polyplets" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cc56e7803860fa903c27675a17215e1cfb39fb26e111c0b93f6c049f9cf8d8e" +dependencies = [ + "marine-macro", + "marine-rs-sdk-main", + "serde", +] + +[[package]] +name = "proc-macro2" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" + +[[package]] +name = "serde" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d23c1ba4cf0efd44be32017709280b32d1cea5c3f1275c3b6d9e8bc54f758085" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/ipfs-node/effector/Cargo.toml b/ipfs-node/effector/Cargo.toml new file mode 100644 index 0000000..6c53f93 --- /dev/null +++ b/ipfs-node/effector/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "ipfs-effector" +version = "0.1.0" +authors = ["Fluence Labs"] +edition = "2018" +publish = false + +[[bin]] +name = "ipfs_effector" +path = "src/main.rs" + +[dependencies] +marine-rs-sdk ={ version = "0.6.13", features = ["logger"] } +log = "0.4.14" diff --git a/ipfs-node/effector/src/main.rs b/ipfs-node/effector/src/main.rs new file mode 100644 index 0000000..5cb314d --- /dev/null +++ b/ipfs-node/effector/src/main.rs @@ -0,0 +1,101 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#![allow(improper_ctypes)] + +mod path; + +use crate::path::to_full_path; + +use marine_rs_sdk::marine; +use marine_rs_sdk::module_manifest; +use marine_rs_sdk::WasmLoggerBuilder; +use marine_rs_sdk::MountedBinaryResult; + +const RESULT_FILE_PATH: &str = "/tmp/ipfs_rpc_file"; +const IPFS_ADDR_ENV_NAME: &str = "IPFS_ADDR"; +const TIMEOUT_ENV_NAME: &str = "timeout"; + +module_manifest!(); + +pub fn main() { + WasmLoggerBuilder::new() + .with_log_level(log::LevelFilter::Info) + .build() + .unwrap(); +} + +/// Put file from specified path to IPFS and return its hash. +#[marine] +pub fn put(file_path: String) -> String { + log::info!("put called with file path {}", file_path); + + let file_path = to_full_path(file_path); + + let timeout = std::env::var(TIMEOUT_ENV_NAME).unwrap_or_else(|_| "1s".to_string()); + let cmd = vec![ + String::from("add"), + String::from("--timeout"), + timeout, + String::from("-Q"), + file_path, + ]; + + let ipfs_result = ipfs(cmd); + ipfs_result + .into_std() + .unwrap() + .unwrap_or_else(std::convert::identity) +} + +/// Get file by provided hash from IPFS, saves it to a temporary file and returns a path to it. +#[marine] +pub fn get(hash: String) -> String { + log::info!("get called with hash {}", hash); + + let result_file_path = to_full_path(RESULT_FILE_PATH); + + let timeout = std::env::var(TIMEOUT_ENV_NAME).unwrap_or_else(|_| "1s".to_string()); + let cmd = vec![ + String::from("get"), + String::from("--timeout"), + timeout, + String::from("-o"), + result_file_path, + hash, + ]; + + ipfs(cmd); + RESULT_FILE_PATH.to_string() +} + +#[marine] +pub fn get_address() -> String { + 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 + ), + } +} + +#[marine] +#[link(wasm_import_module = "host")] +extern "C" { + /// Execute provided cmd as a parameters of ipfs cli, return result. + pub fn ipfs(cmd: Vec) -> MountedBinaryResult; +} diff --git a/ipfs-node/effector/src/path.rs b/ipfs-node/effector/src/path.rs new file mode 100644 index 0000000..4f7d0d5 --- /dev/null +++ b/ipfs-node/effector/src/path.rs @@ -0,0 +1,52 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +pub(super) fn to_full_path(cmd: S) -> String +where + S: Into, +{ + use std::path::Path; + use std::path::Component; + + let cmd = cmd.into(); + let path = Path::new(&cmd); + + let mut components = path.components(); + let is_absolute = components.next() == Some(Component::RootDir); + + if !is_absolute { + return cmd; + } + + let parent = match components.next() { + Some(Component::Normal(path)) => path.to_str().unwrap(), + _ => return cmd, + }; + + match std::env::var(parent) { + Ok(to_dir) => { + let mut full_path = std::path::PathBuf::from(to_dir); + + // TODO: optimize this + #[allow(clippy::while_let_on_iterator)] + while let Some(component) = components.next() { + full_path.push(component); + } + full_path.to_string_lossy().into_owned() + } + Err(_) => cmd, + } +} diff --git a/ipfs-node/pure/Cargo.lock b/ipfs-node/pure/Cargo.lock new file mode 100644 index 0000000..7e0bc3b --- /dev/null +++ b/ipfs-node/pure/Cargo.lock @@ -0,0 +1,275 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + +[[package]] +name = "getrandom" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "ipfs-pure" +version = "0.1.0" +dependencies = [ + "log", + "marine-rs-sdk", +] + +[[package]] +name = "itoa" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" + +[[package]] +name = "libc" +version = "0.2.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "marine-macro" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc907943772cf966ebe2a2d462d09bc79e898ef102ed065ba3d3abcc93fbb71" +dependencies = [ + "marine-macro-impl", + "marine-rs-sdk-main", +] + +[[package]] +name = "marine-macro-impl" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfdbed3a10ae9b22df06bee8cd0023255358935aae8e8daf9d1a006cfaeeb11e" +dependencies = [ + "proc-macro2", + "quote", + "serde", + "serde_json", + "syn", + "uuid", +] + +[[package]] +name = "marine-rs-sdk" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d17d44016675abdf18caf3ac1bdd4437cc17ec0db019da141bb06a9ea991726" +dependencies = [ + "marine-macro", + "marine-rs-sdk-main", + "marine-timestamp-macro", + "polyplets", + "serde", +] + +[[package]] +name = "marine-rs-sdk-main" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ff262801664ce22f9d274504d6e8088c217034aba0ca431eca8d0258f3699c6" +dependencies = [ + "log", + "serde", +] + +[[package]] +name = "marine-timestamp-macro" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85a9f5b1adeff98ac5a14cfc5fa814185a807ba4a0d3849c9f9e9fd868b27d1f" +dependencies = [ + "chrono", + "quote", +] + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "polyplets" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cc56e7803860fa903c27675a17215e1cfb39fb26e111c0b93f6c049f9cf8d8e" +dependencies = [ + "marine-macro", + "marine-rs-sdk-main", + "serde", +] + +[[package]] +name = "proc-macro2" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" + +[[package]] +name = "serde" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d23c1ba4cf0efd44be32017709280b32d1cea5c3f1275c3b6d9e8bc54f758085" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/ipfs-node/pure/Cargo.toml b/ipfs-node/pure/Cargo.toml new file mode 100644 index 0000000..11db390 --- /dev/null +++ b/ipfs-node/pure/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "ipfs-pure" +version = "0.1.0" +authors = ["Fluence Labs"] +edition = "2018" +publish = false + +[[bin]] +name = "ipfs_pure" +path = "src/main.rs" + +[dependencies] +marine-rs-sdk ={ version = "0.6.13", features = ["logger"] } +log = "0.4.14" diff --git a/ipfs-node/pure/src/main.rs b/ipfs-node/pure/src/main.rs new file mode 100644 index 0000000..7a28624 --- /dev/null +++ b/ipfs-node/pure/src/main.rs @@ -0,0 +1,74 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#![allow(improper_ctypes)] + +use marine_rs_sdk::marine; +use marine_rs_sdk::module_manifest; +use marine_rs_sdk::WasmLoggerBuilder; + +use std::fs; +use std::path::PathBuf; + +const RPC_TMP_FILEPATH: &str = "/tmp/ipfs_rpc_file"; + +module_manifest!(); + +pub fn main() { + WasmLoggerBuilder::new() + .with_log_level(log::LevelFilter::Info) + .build() + .unwrap(); +} + +#[marine] +pub fn invoke() -> String { + "IPFS_RPC wasm example, it allows to:\ninvoke\nput\nget".to_string() +} + +#[marine] +pub fn put(file_content: Vec) -> String { + log::info!("put called with {:?}", file_content); + + let rpc_tmp_filepath = RPC_TMP_FILEPATH.to_string(); + + let r = fs::write(PathBuf::from(rpc_tmp_filepath.clone()), file_content); + if let Err(e) = r { + return format!("file can't be written: {}", e); + } + + ipfs_put(rpc_tmp_filepath) +} + +#[marine] +pub fn get(hash: String) -> Vec { + log::info!("get called with hash: {}", hash); + + let file_path = ipfs_get(hash); + fs::read(file_path).unwrap_or_else(|_| b"error while reading file".to_vec()) +} + +#[marine] +#[link(wasm_import_module = "ipfs_effector")] +extern "C" { + /// Put provided file to ipfs, return ipfs hash of the file. + #[link_name = "put"] + pub fn ipfs_put(file_path: String) -> String; + + /// Get file from ipfs by hash. + #[link_name = "get"] + pub fn ipfs_get(hash: String) -> String; +}