mirror of
https://github.com/fluencelabs/trust-graph
synced 2024-12-04 15:20:19 +00:00
API changes, test function, sqlite requests
This commit is contained in:
parent
3249aa376c
commit
afab376ccc
@ -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
|
||||
}
|
||||
|
@ -13,11 +13,19 @@ use trust_graph::{Auth, PublicKeyHashable, Revoke, Storage, TrustGraph, TrustNod
|
||||
|
||||
static INSTANCE: OnceCell<Mutex<TrustGraph>> = OnceCell::new();
|
||||
|
||||
fn get_data() -> &'static Mutex<TrustGraph> {
|
||||
pub fn get_data() -> &'static Mutex<TrustGraph> {
|
||||
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<TrustNode> {
|
||||
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) {
|
||||
|
@ -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<TrustNode> {
|
||||
self.storage.get(&pk.into())
|
||||
}
|
||||
|
||||
|
@ -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<TrustNode>;
|
||||
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<TrustNode> {
|
||||
self.nodes.get(pk).cloned()
|
||||
}
|
||||
|
||||
fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) {
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user