Skip to content

Commit

Permalink
use timeout from config
Browse files Browse the repository at this point in the history
  • Loading branch information
justprosh committed Jun 30, 2021
1 parent ac7ed98 commit 10c490e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 35 deletions.
30 changes: 13 additions & 17 deletions effector/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ use marine_rs_sdk::WasmLoggerBuilder;

use eyre::Result;

const TIMEOUT_ENV_NAME: &str = "timeout";

module_manifest!();

fn unwrap_mounted_binary_result(result: MountedBinaryResult) -> Result<String> {
result.into_std().ok_or(eyre::eyre!("stdout or stderr contains non valid UTF8 string"))?.map_err(|e| eyre::eyre!("ipfs cli call failed: {}", e))
}

#[inline]
fn get_timeout_string(timeout: u64) -> String { format!("{}s", timeout) }

pub fn main() {
WasmLoggerBuilder::new()
.with_log_level(log::LevelFilter::Info)
Expand All @@ -42,15 +43,14 @@ pub fn main() {
}

#[marine]
pub fn connect(multiaddr: String) -> IpfsResult {
pub fn connect(multiaddr: String, timeout_sec: u64) -> IpfsResult {
log::info!("connect called with multiaddr {}", multiaddr);

let timeout = std::env::var(TIMEOUT_ENV_NAME).unwrap_or_else(|_| "1s".to_string());
let cmd = vec![
String::from("swarm"),
String::from("connect"),
String::from("--timeout"),
timeout,
get_timeout_string(timeout_sec),
multiaddr
];

Expand All @@ -59,14 +59,13 @@ pub fn connect(multiaddr: String) -> IpfsResult {

/// Put file from specified path to IPFS and return its hash.
#[marine]
pub fn put(file_path: String) -> IpfsResult {
pub fn put(file_path: String, timeout_sec: u64) -> IpfsResult {
log::info!("put called with file 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,
get_timeout_string(timeout_sec),
String::from("-Q"),
];

Expand All @@ -75,14 +74,13 @@ pub fn put(file_path: String) -> IpfsResult {

/// 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, file_path: String) -> IpfsResult {
pub fn get(hash: String, file_path: String, timeout_sec: u64) -> IpfsResult {
log::info!("get called with hash {}", hash);

let timeout = std::env::var(TIMEOUT_ENV_NAME).unwrap_or_else(|_| "1s".to_string());
let cmd = vec![
String::from("get"),
String::from("--timeout"),
timeout,
get_timeout_string(timeout_sec),
String::from("-o"),
file_path,
hash,
Expand All @@ -92,13 +90,12 @@ pub fn get(hash: String, file_path: String) -> IpfsResult {
}

#[marine]
pub fn get_peer_id() -> IpfsResult {
let timeout = std::env::var(TIMEOUT_ENV_NAME).unwrap_or_else(|_| "1s".to_string());
pub fn get_peer_id(timeout_sec: u64) -> IpfsResult {
let result: Result<String> = try {
let cmd = vec![
String::from("id"),
String::from("--timeout"),
timeout,
get_timeout_string(timeout_sec),
];

let result: serde_json::Value = serde_json::from_str(&unwrap_mounted_binary_result(ipfs(cmd))?)?;
Expand All @@ -109,13 +106,12 @@ pub fn get_peer_id() -> IpfsResult {
}

#[marine]
pub fn set_external_multiaddr(multiaddr: String) -> IpfsResult {
let timeout = std::env::var(TIMEOUT_ENV_NAME).unwrap_or_else(|_| "1s".to_string());
pub fn set_external_multiaddr(multiaddr: String, timeout_sec: u64) -> IpfsResult {

let cmd = vec![
String::from("config"),
String::from("--timeout"),
timeout,
get_timeout_string(timeout_sec),
String::from("Addresses.Announce"),
format!("[\"{}\"]", multiaddr),
String::from("--json"),
Expand Down
4 changes: 3 additions & 1 deletion pure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ path = "src/main.rs"
marine-rs-sdk = { version = "0.6.10", features = ["logger"] }
log = "0.4.14"
eyre = "0.6.5"
toml = "0.5.8"
serde = "1.0.118"

types = { path = "../types" }
types = { path = "../types" }
75 changes: 58 additions & 17 deletions pure/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,52 @@ use marine_rs_sdk::module_manifest;
use marine_rs_sdk::WasmLoggerBuilder;

use std::fs;
use serde::{Deserialize, Serialize};

const MULTIADDR_FILE_PATH: &str = "/tmp/multiaddr_config";

const CONFIG_FILE_PATH: &str = "/tmp/multiaddr_config";
const DEFAULT_TIMEOUT_SEC: u64 = 1u64;
module_manifest!();

#[derive(Deserialize, Serialize)]
pub struct Config {
pub timeout: u64,
pub multiaddr: Option<String>
}

fn save_multiaddr(multiaddr: String) {
let mut config = load_config();
config.multiaddr = Some(multiaddr);
write_config(config);
}

fn load_multiaddr() -> eyre::Result<String> {
Ok(fs::read_to_string(MULTIADDR_FILE_PATH)?)
load_config().multiaddr.ok_or(eyre::eyre!("multiaddr is not set"))
}

pub fn write_config(config: Config) {
fs::write(CONFIG_FILE_PATH, toml::to_string(&config).unwrap()).unwrap();
}

fn save_multiaddr(multiaddr: String) -> eyre::Result<()> {
Ok(fs::write(MULTIADDR_FILE_PATH, multiaddr)?)
pub fn load_config() -> Config {
let file_content = fs::read_to_string(CONFIG_FILE_PATH).unwrap();
let config: Config = toml::from_str(&file_content).unwrap();
config
}

pub(crate) fn create_config() {
if fs::metadata(CONFIG_FILE_PATH).is_err() {
write_config(Config {
timeout: DEFAULT_TIMEOUT_SEC,
multiaddr: None
});
}
}
pub fn main() {
WasmLoggerBuilder::new()
.with_log_level(log::LevelFilter::Info)
.build()
.unwrap();
create_config();
}

#[marine]
Expand All @@ -51,69 +79,82 @@ pub fn invoke() -> String {
#[marine]
pub fn put(file_path: String) -> IpfsResult {
log::info!("put called with {:?}", file_path);

ipfs_put(file_path)
let timeout = load_config().timeout;
ipfs_put(file_path, timeout)
}

#[marine]
pub fn get_from(hash: String, multiaddr: String) -> IpfsResult {
log::info!("get called with hash: {}", hash);
let timeout = load_config().timeout;

let particle_id = marine_rs_sdk::get_call_parameters().particle_id;
let connect_result = ipfs_connect(multiaddr);
let connect_result = ipfs_connect(multiaddr, timeout.clone());

if !connect_result.success {
return connect_result;
}

let particle_vault_path = format!("/tmp/vault/{}", particle_id);
let file_path = format!("{}/{}", particle_vault_path, hash);
ipfs_get(hash, file_path)
ipfs_get(hash, file_path, timeout)
}

#[marine]
pub fn get_multiaddr() -> IpfsResult {
load_multiaddr().into()
}

#[marine]
pub fn set_multiaddr(multiaddr: String) -> IpfsResult {
let call_parameters = marine_rs_sdk::get_call_parameters();
if load_multiaddr().is_ok() || call_parameters.init_peer_id != call_parameters.service_creator_peer_id {
return eyre::Result::<()>::Err(eyre::eyre!("only service creator can set multiaddr only once")).into();
}

let set_result = ipfs_set_external_multiaddr(multiaddr.clone());
let timeout = load_config().timeout;
let set_result = ipfs_set_external_multiaddr(multiaddr.clone(), timeout.clone());
if !set_result.success {
return set_result;
}

let peer_id_result = ipfs_get_peer_id();
let peer_id_result = ipfs_get_peer_id(timeout);
if !peer_id_result.success {
return peer_id_result;
}

// trim trailing /
let multiaddr = if multiaddr.ends_with("/") { multiaddr[..multiaddr.len() - 1].to_string() } else { multiaddr.clone() };
save_multiaddr(format!("{}/{}", multiaddr, peer_id_result.result)).into()
save_multiaddr(format!("{}/{}", multiaddr, peer_id_result.result));
Ok(()).into()
}

#[marine]
pub fn set_timeout(timeout_sec: u64) {
let mut config = load_config();
config.timeout = timeout_sec;
write_config(config);
}


#[marine]
#[link(wasm_import_module = "ipfs_effector")]
extern "C" {
#[link_name = "connect"]
pub fn ipfs_connect(multiaddr: String) -> IpfsResult;
pub fn ipfs_connect(multiaddr: String, timeout_sec: u64) -> IpfsResult;


/// Put provided file to ipfs, return ipfs hash of the file.
#[link_name = "put"]
pub fn ipfs_put(file_path: String) -> IpfsResult;
pub fn ipfs_put(file_path: String, timeout_sec: u64) -> IpfsResult;

/// Get file from ipfs by hash.
#[link_name = "get"]
pub fn ipfs_get(hash: String, file_path: String) -> IpfsResult;
pub fn ipfs_get(hash: String, file_path: String, timeout_sec: u64) -> IpfsResult;

#[link_name = "get_peer_id"]
pub fn ipfs_get_peer_id() -> IpfsResult;
pub fn ipfs_get_peer_id(timeout_sec: u64) -> IpfsResult;

#[link_name = "set_external_multiaddr"]
pub fn ipfs_set_external_multiaddr(multiaddr: String) -> IpfsResult;
pub fn ipfs_set_external_multiaddr(multiaddr: String, timeout_sec: u64) -> IpfsResult;
}
1 change: 1 addition & 0 deletions types/src/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use marine_rs_sdk::marine;
use eyre::Result;

Expand Down

0 comments on commit 10c490e

Please sign in to comment.