abstract storage

This commit is contained in:
DieMyst 2021-01-18 03:57:10 +03:00
parent f2afc13ea2
commit a294c42dec
8 changed files with 1754 additions and 63 deletions

1593
bin/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

15
bin/Cargo.toml Normal file
View File

@ -0,0 +1,15 @@
[package]
name = "trust-graph-wasm"
version = "0.2.0"
authors = ["Fluence Labs"]
edition = "2018"
description = "trust graph wasm"
license = "Apache-2.0"
[[bin]]
name = "trust-graph"
path = "src/main.rs"
[dependencies]
trust-graph = { path = "../" }
fluence = { version = "0.2.18", features = ["logger"] }

10
bin/main.rs Normal file
View File

@ -0,0 +1,10 @@
use fluence::WasmLoggerBuilder;
pub(crate) type Result<T> = std::result::Result<T, errors::HistoryError>;
pub fn main() {
WasmLoggerBuilder::new()
.with_log_level(log::Level::Info)
.build()
.unwrap();
}

0
bin/service_api.rs Normal file
View File

3
bin/storage_impl.rs Normal file
View File

@ -0,0 +1,3 @@
// store list of trusts
// check if trust is already in list before adding
// if there is an older trust - don't add received trust

View File

@ -35,6 +35,7 @@ mod revoke;
mod trust; mod trust;
mod trust_graph; mod trust_graph;
mod trust_node; mod trust_node;
mod trust_graph_storage;
pub(crate) use libp2p_core::identity::ed25519; pub(crate) use libp2p_core::identity::ed25519;

View File

@ -20,15 +20,14 @@ 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_node::{Auth, TrustNode};
use crate::trust_graph_storage::{Storage, InMemoryStorage};
use std::borrow::Borrow; use std::borrow::Borrow;
use std::collections::hash_map::Entry; use std::collections::{HashSet, VecDeque};
use std::collections::hash_map::Entry::Occupied;
use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt::Debug; use std::fmt::Debug;
use std::time::Duration; use std::time::Duration;
/// for simplicity, we store `n` where Weight = 1/n^2 /// for simplicity, we store `n` where Weight = 1/n^2
type Weight = u32; pub type Weight = u32;
/// Graph to efficiently calculate weights of certificates and get chains of certificates. /// Graph to efficiently calculate weights of certificates and get chains of certificates.
/// TODO serialization/deserialization /// TODO serialization/deserialization
@ -36,33 +35,28 @@ type Weight = u32;
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct TrustGraph { pub struct TrustGraph {
// TODO abstract this into a trait with key access methods storage: InMemoryStorage
// TODO: add docs on fields
nodes: HashMap<PublicKeyHashable, TrustNode>,
root_weights: HashMap<PublicKeyHashable, Weight>,
} }
#[allow(dead_code)] #[allow(dead_code)]
impl TrustGraph { impl TrustGraph {
pub fn new(root_weights: Vec<(PublicKey, Weight)>) -> Self { pub fn new(root_weights: Vec<(PublicKey, Weight)>) -> Self {
let root_weights = root_weights.into_iter()
.map(|(k, w)| (k.into(), w))
.collect();
Self { Self {
nodes: HashMap::new(), storage: InMemoryStorage::new_in_memory(root_weights)
root_weights: root_weights
.into_iter()
.map(|(k, w)| (k.into(), w))
.collect(),
} }
} }
/// Insert new root weights /// Insert new root weight
pub fn add_root_weights<P: Into<PublicKeyHashable>>(&mut self, weights: Vec<(P, Weight)>) { pub fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) {
self.root_weights self.storage.add_root_weight(pk, weight)
.extend(weights.into_iter().map(|(k, w)| (k.into(), w)))
} }
/// Get trust by public key /// Get trust by public key
pub fn get(&self, pk: PublicKey) -> Option<&TrustNode> { pub fn get(&self, pk: PublicKey) -> Option<&TrustNode> {
self.nodes.get(&pk.into()) self.storage.get(&pk.into())
} }
// TODO: remove cur_time from api, leave it for tests only // TODO: remove cur_time from api, leave it for tests only
@ -71,7 +65,7 @@ impl TrustGraph {
where where
C: Borrow<Certificate>, C: Borrow<Certificate>,
{ {
let roots: Vec<PublicKey> = self.root_weights.keys().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)?;
@ -80,14 +74,14 @@ impl TrustGraph {
let root_pk: PublicKeyHashable = root_trust.issued_for.clone().into(); let root_pk: PublicKeyHashable = root_trust.issued_for.clone().into();
// Insert new TrustNode for this root_pk if there wasn't one // Insert new TrustNode for this root_pk if there wasn't one
if self.nodes.get_mut(&root_pk).is_none() { if self.storage.get(&root_pk).is_none() {
let mut trust_node = TrustNode::new(root_trust.issued_for.clone(), cur_time); let mut trust_node = TrustNode::new(root_trust.issued_for.clone(), cur_time);
let root_auth = Auth { let root_auth = Auth {
trust: root_trust.clone(), trust: root_trust.clone(),
issued_by: root_trust.issued_for.clone(), issued_by: root_trust.issued_for.clone(),
}; };
trust_node.update_auth(root_auth); trust_node.update_auth(root_auth);
self.nodes.insert(root_pk, trust_node); self.storage.insert(root_pk, trust_node);
} }
// Insert remaining trusts to the graph // Insert remaining trusts to the graph
@ -100,16 +94,7 @@ impl TrustGraph {
issued_by: previous_trust.issued_for.clone(), issued_by: previous_trust.issued_for.clone(),
}; };
match self.nodes.get_mut(&pk) { self.storage.update_auth(&pk, auth, &root_trust.issued_for, cur_time);
Some(trust_node) => {
trust_node.update_auth(auth);
}
None => {
let mut trust_node = TrustNode::new(root_trust.issued_for.clone(), cur_time);
trust_node.update_auth(auth);
self.nodes.insert(pk, trust_node);
}
}
previous_trust = trust; previous_trust = trust;
} }
@ -122,13 +107,14 @@ impl TrustGraph {
where where
P: Borrow<PublicKey>, P: Borrow<PublicKey>,
{ {
if let Some(weight) = self.root_weights.get(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
.root_weights .storage
.keys() .root_keys()
.iter()
.map(|pk| pk.clone().into()) .map(|pk| pk.clone().into())
.collect(); .collect();
@ -157,8 +143,8 @@ impl TrustGraph {
let cert = cert.borrow(); let cert = cert.borrow();
let root_weight = *self let root_weight = *self
.root_weights .storage
.get(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?
.expect("first trust in chain must be in root_weights"); .expect("first trust in chain must be in root_weights");
@ -200,7 +186,7 @@ impl TrustGraph {
.expect("`cur_chain` always has at least one element"); .expect("`cur_chain` always has at least one element");
let auths: Vec<Auth> = self let auths: Vec<Auth> = self
.nodes .storage
.get(&last.issued_by.clone().into()) .get(&last.issued_by.clone().into())
.expect( .expect(
"there cannot be paths without any nodes after adding verified certificates", "there cannot be paths without any nodes after adding verified certificates",
@ -244,14 +230,15 @@ impl TrustGraph {
P: Borrow<PublicKey>, P: Borrow<PublicKey>,
{ {
// get all auths (edges) for issued public key // get all auths (edges) for issued public key
let issued_for_node = self.nodes.get(issued_for.borrow().as_ref()); let issued_for_node = self.storage.get(issued_for.borrow().as_ref());
let roots = roots.iter().map(|pk| pk.as_ref()); let roots = roots.iter().map(|pk| pk.as_ref());
let roots = self.root_weights.keys().chain(roots).collect(); let keys = self.storage.root_keys();
let roots = keys.iter().chain(roots).collect();
match issued_for_node { match issued_for_node {
Some(node) => self Some(node) => self
.bf_search_paths(node, roots) .bf_search_paths(&node, roots)
.iter() .iter()
.map(|auths| { .map(|auths| {
// TODO: can avoid cloning here by returning &Certificate // TODO: can avoid cloning here by returning &Certificate
@ -279,13 +266,7 @@ impl TrustGraph {
let pk: PublicKeyHashable = revoke.pk.clone().into(); let pk: PublicKeyHashable = revoke.pk.clone().into();
match self.nodes.entry(pk) { self.storage.revoke(&pk, revoke)
Occupied(mut entry) => {
entry.get_mut().update_revoke(revoke);
Ok(())
}
Entry::Vacant(_) => Err("There is no trust with such PublicKey".to_string()),
}
} }
/// Check information about new certificates and about revoked certificates. /// Check information about new certificates and about revoked certificates.
@ -300,6 +281,7 @@ mod tests {
use crate::key_pair::KeyPair; use crate::key_pair::KeyPair;
use crate::misc::current_time; use crate::misc::current_time;
use failure::_core::time::Duration; use failure::_core::time::Duration;
use std::collections::HashMap;
pub fn one_minute() -> Duration { pub fn one_minute() -> Duration {
Duration::new(60, 0) Duration::new(60, 0)
@ -385,7 +367,7 @@ mod tests {
let (root, _, cert) = generate_root_cert(); let (root, _, cert) = generate_root_cert();
let mut graph = TrustGraph::default(); let mut graph = TrustGraph::default();
graph.root_weights.insert(root.key_pair.public().into(), 0); graph.add_root_weight(root.key_pair.public().into(), 0);
let addition = graph.add(cert, current_time()); let addition = graph.add(cert, current_time());
assert_eq!(addition.is_ok(), true); assert_eq!(addition.is_ok(), true);
@ -417,8 +399,8 @@ mod tests {
let mut graph = TrustGraph::default(); let mut graph = TrustGraph::default();
let root1_pk = key_pairs1[0].public_key(); let root1_pk = key_pairs1[0].public_key();
let root2_pk = key_pairs2[0].public_key(); let root2_pk = key_pairs2[0].public_key();
graph.root_weights.insert(root1_pk.into(), 1); graph.add_root_weight(root1_pk.into(), 1);
graph.root_weights.insert(root2_pk.into(), 0); graph.add_root_weight(root2_pk.into(), 0);
graph.add(cert1, cur_time).unwrap(); graph.add(cert1, cur_time).unwrap();
let node2 = graph.get(key_pair2.public_key()).unwrap(); let node2 = graph.get(key_pair2.public_key()).unwrap();
@ -448,7 +430,7 @@ mod tests {
let mut graph = TrustGraph::default(); let mut graph = TrustGraph::default();
let root_pk = key_pairs[0].public_key(); let root_pk = key_pairs[0].public_key();
graph.root_weights.insert(root_pk.into(), 1); graph.add_root_weight(root_pk.into(), 1);
graph.add(cert1, current_time()).unwrap(); graph.add(cert1, current_time()).unwrap();
@ -491,8 +473,8 @@ mod tests {
let mut graph = TrustGraph::default(); let mut graph = TrustGraph::default();
let root1_pk = key_pairs1[0].public_key(); let root1_pk = key_pairs1[0].public_key();
let root2_pk = key_pairs2[0].public_key(); let root2_pk = key_pairs2[0].public_key();
graph.root_weights.insert(root1_pk.into(), 1); graph.add_root_weight(root1_pk.into(), 1);
graph.root_weights.insert(root2_pk.into(), 0); graph.add_root_weight(root2_pk.into(), 0);
let last_pk1 = cert1.chain[9].issued_for.clone(); let last_pk1 = cert1.chain[9].issued_for.clone();
let last_pk2 = cert2.chain[9].issued_for.clone(); let last_pk2 = cert2.chain[9].issued_for.clone();
@ -525,7 +507,7 @@ mod tests {
let mut graph = TrustGraph::default(); let mut graph = TrustGraph::default();
let root1_pk = key_pairs[0].public_key(); let root1_pk = key_pairs[0].public_key();
graph.root_weights.insert(root1_pk.clone().into(), 1); graph.add_root_weight(root1_pk.clone().into(), 1);
graph.add(cert.clone(), current_time()).unwrap(); graph.add(cert.clone(), current_time()).unwrap();
@ -542,14 +524,11 @@ mod tests {
let mut graph = TrustGraph::default(); let mut graph = TrustGraph::default();
// add first and last trusts as roots // add first and last trusts as roots
graph graph
.root_weights .add_root_weight(cert.chain[0].clone().issued_for.into(), 1);
.insert(cert.chain[0].clone().issued_for.into(), 1);
graph graph
.root_weights .add_root_weight(cert.chain[3].clone().issued_for.into(), 1);
.insert(cert.chain[3].clone().issued_for.into(), 1);
graph graph
.root_weights .add_root_weight(cert.chain[5].clone().issued_for.into(), 1);
.insert(cert.chain[5].clone().issued_for.into(), 1);
graph.add(cert.clone(), current_time()).unwrap(); graph.add(cert.clone(), current_time()).unwrap();
@ -590,9 +569,9 @@ mod tests {
let root1_pk = key_pairs1[0].public_key(); let root1_pk = key_pairs1[0].public_key();
let root2_pk = key_pairs2[0].public_key(); let root2_pk = key_pairs2[0].public_key();
let root3_pk = key_pairs3[0].public_key(); let root3_pk = key_pairs3[0].public_key();
graph.root_weights.insert(root1_pk.clone().into(), 1); graph.add_root_weight(root1_pk.clone().into(), 1);
graph.root_weights.insert(root2_pk.clone().into(), 0); graph.add_root_weight(root2_pk.clone().into(), 0);
graph.root_weights.insert(root3_pk.clone().into(), 0); graph.add_root_weight(root3_pk.clone().into(), 0);
graph.add(cert1, current_time()).unwrap(); graph.add(cert1, current_time()).unwrap();
graph.add(cert2, current_time()).unwrap(); graph.add(cert2, current_time()).unwrap();

View File

@ -0,0 +1,90 @@
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 libp2p_core::identity::ed25519::PublicKey;
pub trait Storage {
fn new() -> Self;
fn get(&self, pk: &PublicKeyHashable) -> Option<&TrustNode>;
fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode);
fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<&Weight>;
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);
}
#[derive(Debug, Default)]
pub struct InMemoryStorage {
nodes: HashMap<PublicKeyHashable, TrustNode>,
root_weights: HashMap<PublicKeyHashable, Weight>,
}
impl InMemoryStorage {
pub fn new_in_memory(root_weights: HashMap<PublicKeyHashable, Weight>) -> Self {
Self {
nodes: HashMap::new(),
root_weights: root_weights,
}
}
}
impl Storage for InMemoryStorage {
fn new() -> Self {
InMemoryStorage {
nodes: HashMap::new(),
root_weights: HashMap::new()
}
}
fn get(&self, pk: &PublicKeyHashable) -> Option<&TrustNode> {
self.nodes.get(pk)
}
fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) {
&self.nodes.insert(pk, node);
}
fn get_root_weight(&self, pk: &PublicKeyHashable) -> Option<&Weight> {
self.root_weights.get(pk)
}
fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) {
&self.root_weights.insert(pk, weight);
}
fn root_keys(&self) -> Vec<PublicKeyHashable> {
self.root_weights.keys().cloned().map(Into::into).collect()
}
fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), String> {
match self.nodes.get_mut(&pk) {
Some(trust_node) => {
trust_node.update_revoke(revoke);
Ok(())
}
None => Err("There is no trust with such PublicKey".to_string()),
}
}
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);
}
None => {
let mut trust_node = TrustNode::new(issued_for.clone(), cur_time);
trust_node.update_auth(auth);
self.nodes.insert(pk.clone(), trust_node);
}
}
}
}