From 0b1b0a82a7ecc7bf840d727dcb5a593b151f5591 Mon Sep 17 00:00:00 2001 From: DieMyst Date: Wed, 20 Jan 2021 00:17:24 +0300 Subject: [PATCH] init storage for wasm, add sqlite to project WIP --- bin/Cargo.lock | 21 +++++++++++++ bin/Cargo.toml | 8 ++++- bin/src/main.rs | 3 ++ bin/src/service_api.rs | 6 ++++ bin/src/storage_impl.rs | 66 ++++++++++++++++++++++++++++++++++++++++- src/lib.rs | 5 +++- src/trust_graph.rs | 4 +-- 7 files changed, 108 insertions(+), 5 deletions(-) diff --git a/bin/Cargo.lock b/bin/Cargo.lock index e30326b..7ec2f33 100644 --- a/bin/Cargo.lock +++ b/bin/Cargo.lock @@ -102,6 +102,12 @@ dependencies = [ "byte-tools", ] +[[package]] +name = "boolinator" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfa8873f51c92e232f9bac4065cddef41b714152812bfc5f7672ba16d6ef8cd9" + [[package]] name = "bs58" version = "0.3.1" @@ -283,6 +289,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +[[package]] +name = "fce-sqlite-connector" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8165090ee52453a5c14bd42212bfc6516860c4aaa2315fd568c0198b46b52901" +dependencies = [ + "fluence", +] + [[package]] name = "fixedbitset" version = "0.2.0" @@ -1396,8 +1411,14 @@ dependencies = [ name = "trust-graph-wasm" version = "0.2.0" dependencies = [ + "anyhow", + "boolinator", + "fce-sqlite-connector", "fluence", + "fluence-identity", "log", + "once_cell", + "parking_lot", "trust-graph", ] diff --git a/bin/Cargo.toml b/bin/Cargo.toml index fac534a..91e8474 100644 --- a/bin/Cargo.toml +++ b/bin/Cargo.toml @@ -12,5 +12,11 @@ path = "src/main.rs" [dependencies] trust-graph = { path = "../" } +fluence-identity = { path = "../identity" } log = "0.4.8" -fluence = { version = "0.2.18", features = ["logger"] } \ No newline at end of file +fluence = { version = "0.2.18", features = ["logger"] } +anyhow = "1.0.31" +boolinator = "2.4.0" +once_cell = "1.4.1" +parking_lot = "0.11.1" +fce-sqlite-connector = "0.1.3" \ No newline at end of file diff --git a/bin/src/main.rs b/bin/src/main.rs index 1e07879..cf92a55 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -1,5 +1,8 @@ use fluence::WasmLoggerBuilder; +mod storage_impl; +mod service_api; + pub fn main() { WasmLoggerBuilder::new() .with_log_level(log::Level::Info) diff --git a/bin/src/service_api.rs b/bin/src/service_api.rs index e69de29..22a0823 100644 --- a/bin/src/service_api.rs +++ b/bin/src/service_api.rs @@ -0,0 +1,6 @@ +use fluence::{fce, CallParameters}; + +#[fce] +fn test(a: String) -> String { + a +} \ No newline at end of file diff --git a/bin/src/storage_impl.rs b/bin/src/storage_impl.rs index 8f6d696..b7bde9c 100644 --- a/bin/src/storage_impl.rs +++ b/bin/src/storage_impl.rs @@ -1,3 +1,67 @@ // store list of trusts // check if trust is already in list before adding -// if there is an older trust - don't add received trust \ No newline at end of file +// if there is an older trust - don't add received trust + +use trust_graph::{Storage, TrustGraph, PublicKeyHashable, TrustNode, Weight, Auth, Revoke}; +use fluence_identity::public_key::PublicKey; +use once_cell::sync::OnceCell; +use parking_lot::Mutex; +use std::time::Duration; +use fce_sqlite_connector; +use fce_sqlite_connector::{State, Connection}; + +static INSTANCE: OnceCell> = OnceCell::new(); + +fn get_data() -> &'static Mutex { + INSTANCE.get_or_init(|| { + let db_path = "/var/folders/ww/v__xg0cj17x7h7sf3bgwpx8h0000gn/T/4589ab6f-5440-4933-ace5-a62714784142/tmp/users.sqlite"; + let connection = fce_sqlite_connector::open(db_path).unwrap(); + Mutex::new(TrustGraph::new(Box::new(SqliteStorage {connection}))) + }) +} + +struct SqliteStorage { + connection: Connection, +} + +impl SqliteStorage { + pub fn init(&self) { + let init_sql = "CREATE TABLE IF NOT EXISTS trusts(\ + peer_id TEXT PRIMARY KEY,\ + relay TEXT NOT NULL,\ + sig TEXT NOT NULL,\ + name TEXT NOT NULL\ + );"; + } +} + +impl Storage for SqliteStorage { + fn get(&self, pk: &PublicKeyHashable) -> Option<&TrustNode> { + None + } + fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) { + + } + + fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<&Weight> { + None + } + fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) { + + } + fn root_keys(&self) -> Vec { + vec![] + } + fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), String> { + Err("not implemented".to_string()) + } + fn update_auth( + &mut self, + pk: &PublicKeyHashable, + auth: Auth, + issued_for: &PublicKey, + cur_time: Duration, + ) { + + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 9672dfc..766831a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,4 +40,7 @@ pub use crate::certificate::Certificate; pub use crate::misc::current_time; pub use crate::public_key_hashable::PublicKeyHashable; pub use crate::trust::Trust; -pub use crate::trust_graph::TrustGraph; +pub use crate::revoke::Revoke; +pub use crate::trust_node::{TrustNode, Auth}; +pub use crate::trust_graph::{TrustGraph, Weight}; +pub use crate::trust_graph_storage::Storage; diff --git a/src/trust_graph.rs b/src/trust_graph.rs index 578ac46..9136e49 100644 --- a/src/trust_graph.rs +++ b/src/trust_graph.rs @@ -33,12 +33,12 @@ pub type Weight = u32; /// TODO export a certificate from graph #[allow(dead_code)] pub struct TrustGraph { - storage: Box, + storage: Box, } #[allow(dead_code)] impl TrustGraph { - pub fn new(storage: Box) -> Self { + pub fn new(storage: Box) -> Self { Self { storage: storage } }