This commit is contained in:
DieMyst 2021-01-18 18:26:17 +03:00
parent 2ec1114ec2
commit 9e74777fe5
3 changed files with 38 additions and 23 deletions

View File

@ -34,8 +34,8 @@ mod public_key_hashable;
mod revoke; mod revoke;
mod trust; mod trust;
mod trust_graph; mod trust_graph;
mod trust_node;
mod trust_graph_storage; mod trust_graph_storage;
mod trust_node;
pub(crate) use libp2p_core::identity::ed25519; pub(crate) use libp2p_core::identity::ed25519;

View File

@ -19,8 +19,8 @@ use crate::ed25519::PublicKey;
use crate::public_key_hashable::PublicKeyHashable; use crate::public_key_hashable::PublicKeyHashable;
use crate::revoke::Revoke; use crate::revoke::Revoke;
use crate::trust::Trust; use crate::trust::Trust;
use crate::trust_node::{Auth, TrustNode};
use crate::trust_graph_storage::Storage; use crate::trust_graph_storage::Storage;
use crate::trust_node::{Auth, TrustNode};
use std::borrow::Borrow; use std::borrow::Borrow;
use std::collections::{HashSet, VecDeque}; use std::collections::{HashSet, VecDeque};
use std::time::Duration; use std::time::Duration;
@ -33,15 +33,13 @@ pub type Weight = u32;
/// TODO export a certificate from graph /// TODO export a certificate from graph
#[allow(dead_code)] #[allow(dead_code)]
pub struct TrustGraph { pub struct TrustGraph {
storage: Box<dyn Storage> storage: Box<dyn Storage>,
} }
#[allow(dead_code)] #[allow(dead_code)]
impl TrustGraph { impl TrustGraph {
pub fn new(storage: Box<dyn Storage>) -> Self { pub fn new(storage: Box<dyn Storage>) -> Self {
Self { Self { storage: storage }
storage: storage
}
} }
/// Insert new root weight /// Insert new root weight
@ -60,7 +58,13 @@ impl TrustGraph {
where where
C: Borrow<Certificate>, C: Borrow<Certificate>,
{ {
let roots: Vec<PublicKey> = self.storage.root_keys().iter().cloned().map(Into::into).collect(); let roots: Vec<PublicKey> = self
.storage
.root_keys()
.iter()
.cloned()
.map(Into::into)
.collect();
// Check that certificate is valid and converges to one of the known roots // Check that certificate is valid and converges to one of the known roots
Certificate::verify(cert.borrow(), roots.as_slice(), cur_time)?; Certificate::verify(cert.borrow(), roots.as_slice(), cur_time)?;
@ -89,7 +93,8 @@ impl TrustGraph {
issued_by: previous_trust.issued_for.clone(), issued_by: previous_trust.issued_for.clone(),
}; };
self.storage.update_auth(&pk, auth, &root_trust.issued_for, cur_time); self.storage
.update_auth(&pk, auth, &root_trust.issued_for, cur_time);
previous_trust = trust; previous_trust = trust;
} }
@ -275,9 +280,9 @@ mod tests {
use super::*; use super::*;
use crate::key_pair::KeyPair; use crate::key_pair::KeyPair;
use crate::misc::current_time; use crate::misc::current_time;
use crate::trust_graph_storage::InMemoryStorage;
use failure::_core::time::Duration; use failure::_core::time::Duration;
use std::collections::HashMap; use std::collections::HashMap;
use crate::trust_graph_storage::InMemoryStorage;
pub fn one_minute() -> Duration { pub fn one_minute() -> Duration {
Duration::new(60, 0) Duration::new(60, 0)
@ -526,12 +531,9 @@ mod tests {
let st = Box::new(InMemoryStorage::new()); let st = Box::new(InMemoryStorage::new());
let mut graph = TrustGraph::new(st); let mut graph = TrustGraph::new(st);
// add first and last trusts as roots // add first and last trusts as roots
graph graph.add_root_weight(cert.chain[0].clone().issued_for.into(), 1);
.add_root_weight(cert.chain[0].clone().issued_for.into(), 1); graph.add_root_weight(cert.chain[3].clone().issued_for.into(), 1);
graph graph.add_root_weight(cert.chain[5].clone().issued_for.into(), 1);
.add_root_weight(cert.chain[3].clone().issued_for.into(), 1);
graph
.add_root_weight(cert.chain[5].clone().issued_for.into(), 1);
graph.add(cert.clone(), current_time()).unwrap(); graph.add(cert.clone(), current_time()).unwrap();

View File

@ -1,10 +1,10 @@
use crate::trust_node::{Auth, TrustNode};
use crate::public_key_hashable::PublicKeyHashable; use crate::public_key_hashable::PublicKeyHashable;
use crate::trust_graph::Weight;
use std::collections::{HashMap};
use crate::revoke::Revoke; use crate::revoke::Revoke;
use std::time::Duration; use crate::trust_graph::Weight;
use crate::trust_node::{Auth, TrustNode};
use libp2p_core::identity::ed25519::PublicKey; use libp2p_core::identity::ed25519::PublicKey;
use std::collections::HashMap;
use std::time::Duration;
pub trait Storage { pub trait Storage {
fn get(&self, pk: &PublicKeyHashable) -> Option<&TrustNode>; fn get(&self, pk: &PublicKeyHashable) -> Option<&TrustNode>;
@ -14,7 +14,13 @@ pub trait Storage {
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>;
fn update_auth(&mut self, pk: &PublicKeyHashable, auth: Auth, issued_for: &PublicKey, cur_time: Duration); fn update_auth(
&mut self,
pk: &PublicKeyHashable,
auth: Auth,
issued_for: &PublicKey,
cur_time: Duration,
);
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -26,7 +32,8 @@ pub struct InMemoryStorage {
impl InMemoryStorage { impl InMemoryStorage {
#[allow(dead_code)] #[allow(dead_code)]
pub fn new_in_memory(root_weights: Vec<(PublicKey, Weight)>) -> Self { pub fn new_in_memory(root_weights: Vec<(PublicKey, Weight)>) -> Self {
let root_weights = root_weights.into_iter() let root_weights = root_weights
.into_iter()
.map(|(k, w)| (k.into(), w)) .map(|(k, w)| (k.into(), w))
.collect(); .collect();
Self { Self {
@ -39,7 +46,7 @@ impl InMemoryStorage {
pub fn new() -> Self { pub fn new() -> Self {
InMemoryStorage { InMemoryStorage {
nodes: HashMap::new(), nodes: HashMap::new(),
root_weights: HashMap::new() root_weights: HashMap::new(),
} }
} }
} }
@ -75,7 +82,13 @@ impl Storage for InMemoryStorage {
} }
} }
fn update_auth(&mut self, pk: &PublicKeyHashable, auth: Auth, issued_for: &PublicKey, cur_time: Duration) { fn update_auth(
&mut self,
pk: &PublicKeyHashable,
auth: Auth,
issued_for: &PublicKey,
cur_time: Duration,
) {
match self.nodes.get_mut(&pk) { match self.nodes.get_mut(&pk) {
Some(trust_node) => { Some(trust_node) => {
trust_node.update_auth(auth); trust_node.update_auth(auth);