trust-graph-test/src/trust_graph_storage.rs

127 lines
3.6 KiB
Rust
Raw Normal View History

2021-02-11 15:10:18 +03:00
use crate::public_key_hashable::PublicKeyHashable as PK;
2021-01-18 03:57:10 +03:00
use crate::revoke::Revoke;
2021-01-18 18:26:17 +03:00
use crate::trust_graph::Weight;
2021-02-09 13:26:44 +03:00
use crate::trust_graph_storage::InMemoryStorageError::RevokeError;
2021-01-18 18:26:17 +03:00
use crate::trust_node::{Auth, TrustNode};
use fluence_identity::public_key::PublicKey;
2021-01-18 18:26:17 +03:00
use std::collections::HashMap;
2021-02-11 15:10:18 +03:00
use std::fmt::Display;
2021-01-18 18:26:17 +03:00
use std::time::Duration;
2021-01-28 11:57:43 +03:00
use thiserror::Error as ThisError;
2021-02-11 15:10:18 +03:00
pub trait StorageError: std::error::Error + Display {}
2021-01-18 03:57:10 +03:00
pub trait Storage {
2021-02-11 15:10:18 +03:00
type Error: StorageError + 'static;
2021-01-28 11:57:43 +03:00
2021-02-11 15:10:18 +03:00
fn get(&self, pk: &PK) -> Result<Option<TrustNode>, Self::Error>;
fn insert(&mut self, pk: PK, node: TrustNode) -> Result<(), Self::Error>;
2021-01-28 11:57:43 +03:00
2021-02-11 15:10:18 +03:00
fn get_root_weight(&self, pk: &PK) -> Result<Option<Weight>, Self::Error>;
fn add_root_weight(&mut self, pk: PK, weight: Weight) -> Result<(), Self::Error>;
fn root_keys(&self) -> Result<Vec<PK>, Self::Error>;
fn revoke(&mut self, pk: &PK, revoke: Revoke) -> Result<(), Self::Error>;
2021-01-18 18:26:17 +03:00
fn update_auth(
&mut self,
2021-02-11 15:10:18 +03:00
pk: &PK,
2021-01-18 18:26:17 +03:00
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 {
2021-02-11 15:10:18 +03:00
nodes: HashMap<PK, TrustNode>,
root_weights: HashMap<PK, Weight>,
2021-01-18 03:57:10 +03:00
}
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 {
2021-02-09 13:26:44 +03:00
#[error("InMemoryStorageError::RevokeError {0:?}")]
RevokeError(String),
}
2021-01-28 11:57:43 +03:00
2021-02-09 13:26:44 +03:00
impl StorageError for InMemoryStorageError {}
2021-01-28 11:57:43 +03:00
impl Storage for InMemoryStorage {
2021-01-28 11:57:43 +03:00
type Error = InMemoryStorageError;
2021-02-11 15:10:18 +03:00
fn get(&self, pk: &PK) -> Result<Option<TrustNode>, Self::Error> {
2021-01-28 11:57:43 +03:00
Ok(self.nodes.get(pk).cloned())
2021-01-18 03:57:10 +03:00
}
2021-02-11 15:10:18 +03:00
fn insert(&mut self, pk: PK, 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-02-11 15:10:18 +03:00
fn get_root_weight(&self, pk: &PK) -> Result<Option<Weight>, Self::Error> {
2021-01-28 11:57:43 +03:00
Ok(self.root_weights.get(pk).cloned())
2021-01-18 03:57:10 +03:00
}
2021-02-11 15:10:18 +03:00
fn add_root_weight(&mut self, pk: PK, weight: Weight) -> Result<(), Self::Error> {
2021-02-09 13:26:44 +03:00
&self.root_weights.insert(pk, weight);
Ok({})
2021-01-18 03:57:10 +03:00
}
2021-02-11 15:10:18 +03:00
fn root_keys(&self) -> Result<Vec<PK>, Self::Error> {
2021-01-28 11:57:43 +03:00
Ok(self.root_weights.keys().cloned().map(Into::into).collect())
2021-01-18 03:57:10 +03:00
}
2021-02-11 15:10:18 +03:00
fn revoke(&mut self, pk: &PK, 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-02-09 13:26:44 +03:00
None => Err(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,
2021-02-11 15:10:18 +03:00
pk: &PK,
2021-01-18 18:26:17 +03:00
auth: Auth,
issued_for: &PublicKey,
cur_time: Duration,
2021-02-09 14:31:41 +03:00
) -> Result<(), Self::Error> {
2021-01-18 03:57:10 +03:00
match self.nodes.get_mut(&pk) {
Some(trust_node) => {
trust_node.update_auth(auth);
2021-02-09 13:26:44 +03:00
Ok({})
2021-01-18 03:57:10 +03:00
}
None => {
let mut trust_node = TrustNode::new(issued_for.clone(), cur_time);
trust_node.update_auth(auth);
self.nodes.insert(pk.clone(), trust_node);
2021-02-09 13:26:44 +03:00
Ok({})
2021-01-18 03:57:10 +03:00
}
}
}
}