add get/set_multiaddr methods

This commit is contained in:
Alexey Proshutinskiy 2021-06-24 16:13:29 +03:00
parent 9b119358b9
commit 140530b791
5 changed files with 73 additions and 13 deletions

View File

@ -11,4 +11,4 @@ path = "src/main.rs"
[dependencies]
marine-rs-sdk = { version = "0.6.10", features = ["logger"] }
log = "0.4.14"
log = "0.4.14"

View File

@ -26,7 +26,6 @@ use marine_rs_sdk::MountedBinaryResult;
use marine_rs_sdk::WasmLoggerBuilder;
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!();
@ -82,16 +81,7 @@ pub fn get(hash: String) -> String {
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")]

View File

@ -11,4 +11,5 @@ path = "src/main.rs"
[dependencies]
marine-rs-sdk = { version = "0.6.10", features = ["logger"] }
log = "0.4.14"
log = "0.4.14"
eyre = "0.6.5"

View File

@ -16,6 +16,10 @@
#![allow(improper_ctypes)]
mod results;
use crate::results::IpfsResult;
use marine_rs_sdk::marine;
use marine_rs_sdk::module_manifest;
use marine_rs_sdk::WasmLoggerBuilder;
@ -23,10 +27,19 @@ use marine_rs_sdk::WasmLoggerBuilder;
use std::fs;
use std::path::PathBuf;
const MULTIADDR_FILE_PATH: &str = "/tmp/multiaddr_config";
const RPC_TMP_FILEPATH: &str = "/tmp/ipfs_rpc_file";
module_manifest!();
fn load_multiaddr() -> eyre::Result<String> {
Ok(fs::read_to_string(MULTIADDR_FILE_PATH)?)
}
fn save_multiaddr(multiaddr: String) -> eyre::Result<()> {
Ok(fs::write(MULTIADDR_FILE_PATH, multiaddr)?)
}
pub fn main() {
WasmLoggerBuilder::new()
.with_log_level(log::LevelFilter::Info)
@ -61,6 +74,21 @@ pub fn get(hash: String) -> Vec<u8> {
fs::read(file_path).unwrap_or_else(|_| b"error while reading file".to_vec())
}
#[marine]
pub fn get_multiaddr() -> IpfsResult {
load_multiaddr().into()
}
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();
}
save_multiaddr(multiaddr).into()
}
#[marine]
#[link(wasm_import_module = "ipfs_effector")]
extern "C" {

41
pure/src/results.rs Normal file
View File

@ -0,0 +1,41 @@
/*
* Copyright 2021 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.
*/
use marine_rs_sdk::marine;
#[marine]
pub struct IpfsResult {
pub success: bool,
pub result: String,
}
impl From<eyre::Result<String>> for IpfsResult {
fn from(result: eyre::Result<String>) -> Self {
match result {
Ok(result) => Self { success: true, result },
Err(err) => Self { success: false, result: err.to_string() }
}
}
}
impl From<eyre::Result<()>> for IpfsResult {
fn from(result: eyre::Result<()>) -> Self {
match result {
Ok(_) => Self { success: true, result: "".to_string() },
Err(err) => Self { success: false, result: err.to_string() }
}
}
}