implement get_weight

This commit is contained in:
DieMyst 2021-01-21 20:35:00 +03:00
parent 467391a8f1
commit b821984e78
4 changed files with 25 additions and 8 deletions

View File

@ -16,7 +16,7 @@ fn test() -> String {
let expires_at = Duration::new(15, 15); let expires_at = Duration::new(15, 15);
let issued_at = Duration::new(5, 5); 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_kp.public().into(), 0);
tg.add_root_weight(root_kp2.public().into(), 1); tg.add_root_weight(root_kp2.public().into(), 1);
tg.add(cert, Duration::new(10, 10)); tg.add(cert, Duration::new(10, 10));

View File

@ -2,6 +2,7 @@
// check if trust is already in list before adding // check if trust is already in list before adding
// if there is an older trust - don't add received trust // if there is an older trust - don't add received trust
use core::convert::TryFrom;
use fce_sqlite_connector; use fce_sqlite_connector;
use fce_sqlite_connector::Value; use fce_sqlite_connector::Value;
use fce_sqlite_connector::{Connection, State}; use fce_sqlite_connector::{Connection, State};
@ -86,8 +87,24 @@ impl Storage for SqliteStorage {
cursor.next().unwrap(); cursor.next().unwrap();
} }
fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<&Weight> { fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<Weight> {
None 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) { fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) {

View File

@ -108,7 +108,7 @@ impl TrustGraph {
P: Borrow<PublicKey>, P: Borrow<PublicKey>,
{ {
if let Some(weight) = self.storage.get_root_weight(pk.borrow().as_ref()) { if let Some(weight) = self.storage.get_root_weight(pk.borrow().as_ref()) {
return Some(*weight); return Some(weight);
} }
let roots: Vec<PublicKey> = self let roots: Vec<PublicKey> = self
@ -142,7 +142,7 @@ impl TrustGraph {
for cert in certs { for cert in certs {
let cert = cert.borrow(); let cert = cert.borrow();
let root_weight = *self let root_weight = self
.storage .storage
.get_root_weight(cert.chain.first()?.issued_for.as_ref()) .get_root_weight(cert.chain.first()?.issued_for.as_ref())
// This panic shouldn't happen // TODO: why? // This panic shouldn't happen // TODO: why?

View File

@ -10,7 +10,7 @@ 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 insert(&mut self, pk: PublicKeyHashable, node: TrustNode);
fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<&Weight>; fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<Weight>;
fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight); fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight);
fn root_keys(&self) -> Vec<PublicKeyHashable>; fn root_keys(&self) -> Vec<PublicKeyHashable>;
fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), String>; fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), String>;
@ -60,8 +60,8 @@ impl Storage for InMemoryStorage {
&self.nodes.insert(pk, node); &self.nodes.insert(pk, node);
} }
fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<&Weight> { fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<Weight> {
self.root_weights.get(pk) self.root_weights.get(pk).cloned()
} }
fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) { fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) {