diff --git a/bin/src/service_api.rs b/bin/src/service_api.rs index 6bb4548..90df4c8 100644 --- a/bin/src/service_api.rs +++ b/bin/src/service_api.rs @@ -16,7 +16,7 @@ fn test() -> String { 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); + let 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_root_weight(root_kp2.public().into(), 1); tg.add(cert, Duration::new(10, 10)); diff --git a/bin/src/storage_impl.rs b/bin/src/storage_impl.rs index 5c7d2d4..66b0527 100644 --- a/bin/src/storage_impl.rs +++ b/bin/src/storage_impl.rs @@ -2,6 +2,7 @@ // check if trust is already in list before adding // if there is an older trust - don't add received trust +use core::convert::TryFrom; use fce_sqlite_connector; use fce_sqlite_connector::Value; use fce_sqlite_connector::{Connection, State}; @@ -86,8 +87,24 @@ impl Storage for SqliteStorage { cursor.next().unwrap(); } - fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<&Weight> { - None + fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option { + let mut cursor = self + .connection + .prepare("SELECT public_key,weight FROM roots WHERE public_key = ?") + .unwrap() + .cursor(); + + cursor.bind(&[Value::String(format!("{}", pk))]).unwrap(); + + if let Some(row) = cursor.next().unwrap() { + log::info!("row: {:?}", row); + + let w = u32::try_from(row[1].as_integer().unwrap()).unwrap(); + + Some(w) + } else { + None + } } fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) { diff --git a/src/trust_graph.rs b/src/trust_graph.rs index 86345c0..7f40878 100644 --- a/src/trust_graph.rs +++ b/src/trust_graph.rs @@ -108,7 +108,7 @@ impl TrustGraph { P: Borrow, { if let Some(weight) = self.storage.get_root_weight(pk.borrow().as_ref()) { - return Some(*weight); + return Some(weight); } let roots: Vec = self @@ -142,7 +142,7 @@ impl TrustGraph { for cert in certs { let cert = cert.borrow(); - let root_weight = *self + let root_weight = self .storage .get_root_weight(cert.chain.first()?.issued_for.as_ref()) // This panic shouldn't happen // TODO: why? diff --git a/src/trust_graph_storage.rs b/src/trust_graph_storage.rs index f62a4d1..2f8d566 100644 --- a/src/trust_graph_storage.rs +++ b/src/trust_graph_storage.rs @@ -10,7 +10,7 @@ pub trait Storage { fn get(&self, pk: &PublicKeyHashable) -> Option; fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode); - fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<&Weight>; + fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option; fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight); fn root_keys(&self) -> Vec; fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), String>; @@ -60,8 +60,8 @@ impl Storage for InMemoryStorage { &self.nodes.insert(pk, node); } - fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<&Weight> { - self.root_weights.get(pk) + fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option { + self.root_weights.get(pk).cloned() } fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) {