trust-graph-test/src/trust_graph_storage.rs

126 lines
3.7 KiB
Rust
Raw Normal View History

2021-01-18 03:57:10 +03:00
use crate::public_key_hashable::PublicKeyHashable;
use crate::revoke::Revoke;
2021-01-18 18:26:17 +03:00
use crate::trust_graph::Weight;
use crate::trust_node::{Auth, TrustNode};
use fluence_identity::public_key::PublicKey;
2021-01-18 18:26:17 +03:00
use std::collections::HashMap;
use std::time::Duration;
2021-01-28 11:57:43 +03:00
use thiserror::Error as ThisError;
use crate::trust_graph_storage::InMemoryStorageError::RevokeError;
pub trait StorageError {}
2021-01-18 03:57:10 +03:00
pub trait Storage {
2021-01-28 11:57:43 +03:00
type Error: StorageError + Into<String>;
fn get(&self, pk: &PublicKeyHashable) -> Result<Option<TrustNode>, Self::Error>;
fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) -> Result<(), Self::Error>;
fn get_root_weight(&self, pk: &PublicKeyHashable) -> Result<Option<Weight>, Self::Error>;
fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) -> Result<(), Self::Error>;
fn root_keys(&self) -> Result<Vec<PublicKeyHashable>, Self::Error>;
fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), Self::Error>;
2021-01-18 18:26:17 +03:00
fn update_auth(
&mut self,
pk: &PublicKeyHashable,
auth: Auth,
issued_for: &PublicKey,
cur_time: Duration,
2021-01-28 11:57:43 +03:00
) -> Result<(), Self::Error>;
2021-01-18 03:57:10 +03:00
}
#[derive(Debug, Default)]
pub struct InMemoryStorage {
nodes: HashMap<PublicKeyHashable, TrustNode>,
root_weights: HashMap<PublicKeyHashable, Weight>,
}
impl InMemoryStorage {
#[allow(dead_code)]
pub fn new_in_memory(root_weights: Vec<(PublicKey, Weight)>) -> Self {
2021-01-18 18:26:17 +03:00
let root_weights = root_weights
.into_iter()
.map(|(k, w)| (k.into(), w))
.collect();
2021-01-18 03:57:10 +03:00
Self {
nodes: HashMap::new(),
root_weights: root_weights,
}
}
#[allow(dead_code)]
pub fn new() -> Self {
2021-01-18 03:57:10 +03:00
InMemoryStorage {
nodes: HashMap::new(),
2021-01-18 18:26:17 +03:00
root_weights: HashMap::new(),
2021-01-18 03:57:10 +03:00
}
}
}
2021-01-18 03:57:10 +03:00
2021-01-28 11:57:43 +03:00
#[derive(ThisError, Debug)]
pub enum InMemoryStorageError {
#[error("{0:?}")]
RevokeError(String)
}
impl StorageError for InMemoryStorage {}
impl Storage for InMemoryStorage {
2021-01-28 11:57:43 +03:00
type Error = InMemoryStorageError;
fn get(&self, pk: &PublicKeyHashable) -> Result<Option<TrustNode>, Self::Error> {
Ok(self.nodes.get(pk).cloned())
2021-01-18 03:57:10 +03:00
}
2021-01-28 11:57:43 +03:00
fn insert(&mut self, pk: PublicKeyHashable, node: TrustNode) -> Result<(), Self::Error> {
2021-01-18 03:57:10 +03:00
&self.nodes.insert(pk, node);
2021-01-28 11:57:43 +03:00
Ok(())
2021-01-18 03:57:10 +03:00
}
2021-01-28 11:57:43 +03:00
fn get_root_weight(&self, pk: &PublicKeyHashable) -> Result<Option<Weight>, Self::Error> {
Ok(self.root_weights.get(pk).cloned())
2021-01-18 03:57:10 +03:00
}
2021-01-28 11:57:43 +03:00
fn add_root_weight(&mut self, pk: PublicKeyHashable, weight: Weight) -> Result<(), Self::Error> {
Ok(&self.root_weights.insert(pk, weight));
2021-01-18 03:57:10 +03:00
}
2021-01-28 11:57:43 +03:00
fn root_keys(&self) -> Result<Vec<PublicKeyHashable>, Self::Error> {
Ok(self.root_weights.keys().cloned().map(Into::into).collect())
2021-01-18 03:57:10 +03:00
}
2021-01-28 11:57:43 +03:00
fn revoke(&mut self, pk: &PublicKeyHashable, revoke: Revoke) -> Result<(), Self::Error> {
2021-01-18 03:57:10 +03:00
match self.nodes.get_mut(&pk) {
Some(trust_node) => {
trust_node.update_revoke(revoke);
Ok(())
}
2021-01-28 11:57:43 +03:00
None => RevokeError("There is no trust with such PublicKey".to_string()),
2021-01-18 03:57:10 +03:00
}
}
2021-01-18 18:26:17 +03:00
fn update_auth(
&mut self,
pk: &PublicKeyHashable,
auth: Auth,
issued_for: &PublicKey,
cur_time: Duration,
) {
2021-01-18 03:57:10 +03:00
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);
}
}
}
}