From afab376cccb109c85fbabd839ad607905c24662d Mon Sep 17 00:00:00 2001 From: DieMyst Date: Wed, 20 Jan 2021 17:21:02 +0300 Subject: [PATCH] API changes, test function, sqlite requests --- bin/src/service_api.rs | 25 +++++++++++++++++-- bin/src/storage_impl.rs | 50 ++++++++++++++++++++++++++------------ src/trust_graph.rs | 2 +- src/trust_graph_storage.rs | 6 ++--- src/trust_node.rs | 2 +- 5 files changed, 63 insertions(+), 22 deletions(-) diff --git a/bin/src/service_api.rs b/bin/src/service_api.rs index 471461e..61969ea 100644 --- a/bin/src/service_api.rs +++ b/bin/src/service_api.rs @@ -1,6 +1,27 @@ +use crate::storage_impl::get_data; use fluence::{fce, CallParameters}; +use fluence_identity::KeyPair; +use std::ops::Deref; +use std::time::Duration; +use trust_graph::Certificate; #[fce] -fn test(a: String) -> String { - a +fn test() -> String { + let mut tg = get_data().lock(); + + let root_kp = KeyPair::generate(); + let second_kp = KeyPair::generate(); + + let expires_at = Duration::new(15, 15); + let issued_at = Duration::new(5, 5); + + let mut cert = Certificate::issue_root(&root_kp, second_kp.public_key(), expires_at, issued_at); + tg.add_root_weight(root_kp.public().into(), 0); + tg.add(cert, Duration::new(10, 10)); + + let a = tg.get(second_kp.public_key()); + let str = format!("{:?}", a); + log::info!("{}", &str); + + str } diff --git a/bin/src/storage_impl.rs b/bin/src/storage_impl.rs index 19d7f21..c114fd3 100644 --- a/bin/src/storage_impl.rs +++ b/bin/src/storage_impl.rs @@ -13,11 +13,19 @@ use trust_graph::{Auth, PublicKeyHashable, Revoke, Storage, TrustGraph, TrustNod static INSTANCE: OnceCell> = OnceCell::new(); -fn get_data() -> &'static Mutex { +pub 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 db_path = "/tmp/users.sqlite"; let connection = fce_sqlite_connector::open(db_path).unwrap(); - Mutex::new(TrustGraph::new(Box::new(SqliteStorage {connection}))) + + let init_sql = "CREATE TABLE IF NOT EXISTS trustnodes(\ + public_key TEXT PRIMARY KEY,\ + trustnode TEXT NOT NULL,\ + );"; + + connection.execute(init_sql).expect("cannot connect to db"); + + Mutex::new(TrustGraph::new(Box::new(SqliteStorage { connection }))) }) } @@ -25,20 +33,32 @@ struct SqliteStorage { connection: Connection, } -impl SqliteStorage { - pub fn init(&self) { - let init_sql = "CREATE TABLE IF NOT EXISTS trustnodes(\ - public_key TEXT PRIMARY KEY,\ - trustnode TEXT NOT NULL,\ - );"; - - self.connection.execute(init_sql).unwrap(); - } -} +impl SqliteStorage {} impl Storage for SqliteStorage { - fn get(&self, pk: &PublicKeyHashable) -> Option<&TrustNode> { - None + fn get(&self, pk: &PublicKeyHashable) -> Option { + let mut cursor = self + .connection + .prepare("SELECT trustnode FROM trustnodes WHERE public_key = ?") + .expect("unexpected: 'get' request should be correct") + .cursor(); + + cursor + .bind(&[Value::String(format!("{}", pk))]) + .expect("unexpected: 'public_key' field should be string"); + + match cursor.next().unwrap() { + Some(r) => { + let tn_str = r[0] + .as_string() + .expect("unexpected: 'trustnode' in a table should be as string"); + let trust_node: TrustNode = serde_json::from_str(tn_str) + .expect("unexpected: 'trustnode' should be as correct json"); + Some(trust_node) + } + + None => None, + } } fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) { diff --git a/src/trust_graph.rs b/src/trust_graph.rs index 9136e49..86345c0 100644 --- a/src/trust_graph.rs +++ b/src/trust_graph.rs @@ -48,7 +48,7 @@ impl TrustGraph { } /// Get trust by public key - pub fn get(&self, pk: PublicKey) -> Option<&TrustNode> { + pub fn get(&self, pk: PublicKey) -> Option { self.storage.get(&pk.into()) } diff --git a/src/trust_graph_storage.rs b/src/trust_graph_storage.rs index c778a54..f62a4d1 100644 --- a/src/trust_graph_storage.rs +++ b/src/trust_graph_storage.rs @@ -7,7 +7,7 @@ use std::collections::HashMap; use std::time::Duration; pub trait Storage { - fn get(&self, pk: &PublicKeyHashable) -> Option<&TrustNode>; + fn get(&self, pk: &PublicKeyHashable) -> Option; fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode); fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<&Weight>; @@ -52,8 +52,8 @@ impl InMemoryStorage { } impl Storage for InMemoryStorage { - fn get(&self, pk: &PublicKeyHashable) -> Option<&TrustNode> { - self.nodes.get(pk) + fn get(&self, pk: &PublicKeyHashable) -> Option { + self.nodes.get(pk).cloned() } fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) { diff --git a/src/trust_node.rs b/src/trust_node.rs index c785df3..a79d491 100644 --- a/src/trust_node.rs +++ b/src/trust_node.rs @@ -59,7 +59,7 @@ pub struct Auth { /// An element of trust graph that store relations (trust or revoke) /// that given by some owners of public keys. #[serde_as] -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct TrustNode { /// identity key of this element pub pk: PublicKey,