diff --git a/services/history-inmemory/Cargo.lock b/services/history-inmemory/Cargo.lock index 67f6156..098ce8e 100644 --- a/services/history-inmemory/Cargo.lock +++ b/services/history-inmemory/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "anyhow" version = "1.0.38" @@ -89,6 +91,8 @@ dependencies = [ "log", "once_cell", "parking_lot", + "serde", + "serde_json", ] [[package]] @@ -108,9 +112,9 @@ checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" [[package]] name = "libc" -version = "0.2.86" +version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" +checksum = "265d751d31d6780a3f956bb5b8022feba2d94eeee5a84ba64f4212eedca42213" [[package]] name = "lock_api" @@ -132,9 +136,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.6.0" +version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ad167a2f54e832b82dbe003a046280dceffe5227b5f79e08e363a29638cfddd" +checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" [[package]] name = "parking_lot" @@ -222,9 +226,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.62" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea1c6153794552ea7cf7cf63b1231a25de00ec90db326ba6264440fa08e31486" +checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" dependencies = [ "itoa", "ryu", @@ -239,9 +243,9 @@ checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" [[package]] name = "syn" -version = "1.0.60" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" +checksum = "ed22b90a0e734a23a7610f4283ac9e5acfb96cbb30dfefa540d66f866f1c09c5" dependencies = [ "proc-macro2", "quote", diff --git a/services/history-inmemory/Cargo.toml b/services/history-inmemory/Cargo.toml index d2cb27a..2527194 100644 --- a/services/history-inmemory/Cargo.toml +++ b/services/history-inmemory/Cargo.toml @@ -16,3 +16,5 @@ boolinator = "2.4.0" log = "0.4.8" once_cell = "1.4.1" parking_lot = "0.11.1" +serde_json = "1.0.64" +serde = "1.0.118" diff --git a/services/history-inmemory/src/errors.rs b/services/history-inmemory/src/errors.rs index 9027d10..252ebc7 100644 --- a/services/history-inmemory/src/errors.rs +++ b/services/history-inmemory/src/errors.rs @@ -26,6 +26,9 @@ pub enum HistoryError { InternalError(String), InvalidArgument(String), Unauthorized(String), + IOError(String), + SerializeError(String), + DeserializeError(String), } impl Error for HistoryError {} @@ -36,6 +39,9 @@ impl std::fmt::Display for HistoryError { Self::InternalError(err_msg) => writeln!(f, "{}", err_msg), Self::InvalidArgument(err_msg) => writeln!(f, "{}", err_msg), Self::Unauthorized(err_msg) => writeln!(f, "{}", err_msg), + Self::IOError(err_msg) => writeln!(f, "{}", err_msg), + Self::SerializeError(err_msg) => writeln!(f, "{}", err_msg), + Self::DeserializeError(err_msg) => writeln!(f, "{}", err_msg), } } } @@ -51,6 +57,9 @@ fn to_error_core(err: &HistoryError) -> i32 { HistoryError::Unauthorized(_) => 1, HistoryError::InternalError(_) => 2, HistoryError::InvalidArgument(_) => 3, + HistoryError::IOError(_) => 4, + HistoryError::SerializeError(_) => 5, + HistoryError::DeserializeError(_) => 6, } } diff --git a/services/history-inmemory/src/service_api.rs b/services/history-inmemory/src/service_api.rs index f0f4642..8a41716 100644 --- a/services/history-inmemory/src/service_api.rs +++ b/services/history-inmemory/src/service_api.rs @@ -65,7 +65,7 @@ pub fn set_tetraplet( ) -> EmptyResult { fn set_impl(peer_id: String, service_id: String, fn_name: String, path: String) -> Result<()> { is_owner()?; - Ok(store_tetraplet(peer_id, service_id, fn_name, path)) + Ok(store_tetraplet(peer_id, service_id, fn_name, path)?) } set_impl(peer_id, service_id, fn_name, path).into() @@ -88,7 +88,7 @@ pub fn is_authenticated(auth: bool, index: u64) -> Result<()> { use crate::errors::HistoryError::Unauthorized; use boolinator::Boolinator; - match get_tetraplet() { + match get_tetraplet()? { None => Err(Unauthorized("Set tetraplet before usage".to_string())), Some(t) => { let call_parameters: CallParameters = fluence::get_call_parameters(); diff --git a/services/history-inmemory/src/storage_api.rs b/services/history-inmemory/src/storage_api.rs index ab924d9..ef930c2 100644 --- a/services/history-inmemory/src/storage_api.rs +++ b/services/history-inmemory/src/storage_api.rs @@ -17,11 +17,15 @@ use crate::history_entry::HistoryEntry; use crate::Result; +use crate::errors::HistoryError; use crate::utils::{u64_to_usize, usize_to_u64}; use once_cell::sync::OnceCell; use parking_lot::Mutex; +use serde::{Deserialize, Serialize}; +use std::fs; +use std::path::Path; -#[derive(Clone, Debug, Default, Eq, PartialEq)] +#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] pub struct Tetraplet { pub peer_pk: String, pub service_id: String, @@ -36,6 +40,7 @@ pub struct Data { } static INSTANCE: OnceCell> = OnceCell::new(); +const TETRAPLET_PATH: &str = "/history/tetraplet.txt"; fn get_data() -> &'static Mutex { INSTANCE.get_or_init(|| <_>::default()) @@ -73,7 +78,12 @@ pub fn get_all_entries() -> Result> { Ok(data.entries.to_vec()) } -pub fn store_tetraplet(peer_id: String, service_id: String, fn_name: String, path: String) { +pub fn store_tetraplet( + peer_id: String, + service_id: String, + fn_name: String, + path: String, +) -> Result<()> { let mut data = get_data().lock(); let tetraplet = Tetraplet { @@ -83,10 +93,36 @@ pub fn store_tetraplet(peer_id: String, service_id: String, fn_name: String, pat json_path: path, }; - data.tetraplet = Some(tetraplet) + let tetraplet_str = serde_json::to_string(&tetraplet).map_err(|err| { + HistoryError::SerializeError(format!("Cannot serialize tetraplet to a file: {}", err)) + })?; + fs::write(Path::new(TETRAPLET_PATH), tetraplet_str) + .map_err(|err| HistoryError::IOError(format!("Cannot write to a file: {}", err)))?; + + data.tetraplet = Some(tetraplet); + + Ok(()) } -pub fn get_tetraplet() -> Option { +pub fn get_tetraplet() -> Result> { let data = get_data().lock(); - data.tetraplet.clone() + let t_op = data.tetraplet.clone(); + if let None = t_op { + if Path::new(TETRAPLET_PATH).exists() { + let tetraplet = fs::read_to_string(TETRAPLET_PATH).map_err(|err| { + HistoryError::IOError(format!("Cannot read from a file: {}", err)) + })?; + let tetraplet: Tetraplet = serde_json::from_str(&tetraplet).map_err(|err| { + HistoryError::DeserializeError(format!( + "Cannot deserialize tetraplet from a file: {}", + err + )) + })?; + Ok(Some(tetraplet)) + } else { + Ok(None) + } + } else { + Ok(t_op) + } } diff --git a/services/history_Config.toml b/services/history_Config.toml index 2eaa429..53b31d8 100644 --- a/services/history_Config.toml +++ b/services/history_Config.toml @@ -4,3 +4,8 @@ modules_dir = "artifacts" name = "history" mem_pages_count = 100 logger_enabled = true + + [module.wasi] + preopened_files = ["./history"] + # this is where files will be stored + mapped_dirs = { "history" = "./history" } \ No newline at end of file diff --git a/services/user-list-inmemory/Cargo.lock b/services/user-list-inmemory/Cargo.lock index cbdc366..7fa6387 100644 --- a/services/user-list-inmemory/Cargo.lock +++ b/services/user-list-inmemory/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "anyhow" version = "1.0.38" @@ -96,9 +98,9 @@ checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" [[package]] name = "libc" -version = "0.2.86" +version = "0.2.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" +checksum = "265d751d31d6780a3f956bb5b8022feba2d94eeee5a84ba64f4212eedca42213" [[package]] name = "lock_api" @@ -120,9 +122,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.6.0" +version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ad167a2f54e832b82dbe003a046280dceffe5227b5f79e08e363a29638cfddd" +checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" [[package]] name = "parking_lot" @@ -210,9 +212,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.62" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea1c6153794552ea7cf7cf63b1231a25de00ec90db326ba6264440fa08e31486" +checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" dependencies = [ "itoa", "ryu", @@ -227,9 +229,9 @@ checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" [[package]] name = "syn" -version = "1.0.60" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" +checksum = "ed22b90a0e734a23a7610f4283ac9e5acfb96cbb30dfefa540d66f866f1c09c5" dependencies = [ "proc-macro2", "quote",