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 trust;
mod trust_graph;
mod trust_node;
mod trust_graph_storage;
mod trust_node;
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::revoke::Revoke;
use crate::trust::Trust;
use crate::trust_node::{Auth, TrustNode};
use crate::trust_graph_storage::Storage;
use crate::trust_node::{Auth, TrustNode};
use std::borrow::Borrow;
use std::collections::{HashSet, VecDeque};
use std::time::Duration;
@ -33,15 +33,13 @@ pub type Weight = u32;
/// TODO export a certificate from graph
#[allow(dead_code)]
pub struct TrustGraph {
storage: Box<dyn Storage>
storage: Box<dyn Storage>,
}
#[allow(dead_code)]
impl TrustGraph {
pub fn new(storage: Box<dyn Storage>) -> Self {
Self {
storage: storage
}
Self { storage: storage }
}
/// Insert new root weight
@ -60,7 +58,13 @@ impl TrustGraph {
where
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
Certificate::verify(cert.borrow(), roots.as_slice(), cur_time)?;
@ -89,7 +93,8 @@ impl TrustGraph {
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;
}
@ -275,9 +280,9 @@ mod tests {
use super::*;
use crate::key_pair::KeyPair;
use crate::misc::current_time;
use crate::trust_graph_storage::InMemoryStorage;
use failure::_core::time::Duration;
use std::collections::HashMap;
use crate::trust_graph_storage::InMemoryStorage;
pub fn one_minute() -> Duration {
Duration::new(60, 0)
@ -526,12 +531,9 @@ mod tests {
let st = Box::new(InMemoryStorage::new());
let mut graph = TrustGraph::new(st);
// add first and last trusts as roots
graph
.add_root_weight(cert.chain[0].clone().issued_for.into(), 1);
graph
.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_root_weight(cert.chain[0].clone().issued_for.into(), 1);
graph.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();

View File

@ -1,10 +1,10 @@
use crate::trust_node::{Auth, TrustNode};
use crate::public_key_hashable::PublicKeyHashable;
use crate::trust_graph::Weight;
use std::collections::{HashMap};
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 std::collections::HashMap;
use std::time::Duration;
pub trait Storage {
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 root_keys(&self) -> Vec<PublicKeyHashable>;
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)]
@ -26,7 +32,8 @@ pub struct InMemoryStorage {
impl InMemoryStorage {
#[allow(dead_code)]
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))
.collect();
Self {
@ -39,7 +46,7 @@ impl InMemoryStorage {
pub fn new() -> Self {
InMemoryStorage {
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) {
Some(trust_node) => {
trust_node.update_auth(auth);