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};
|
2021-01-19 18:28:13 +03:00
|
|
|
use fluence_identity::key_pair::PublicKey;
|
2021-01-18 18:26:17 +03:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::time::Duration;
|
2021-01-18 03:57:10 +03:00
|
|
|
|
|
|
|
pub trait Storage {
|
|
|
|
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>;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct InMemoryStorage {
|
|
|
|
nodes: HashMap<PublicKeyHashable, TrustNode>,
|
|
|
|
root_weights: HashMap<PublicKeyHashable, Weight>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InMemoryStorage {
|
2021-01-18 04:29:16 +03:00
|
|
|
#[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()
|
2021-01-18 04:29:16 +03:00
|
|
|
.map(|(k, w)| (k.into(), w))
|
|
|
|
.collect();
|
2021-01-18 03:57:10 +03:00
|
|
|
Self {
|
|
|
|
nodes: HashMap::new(),
|
|
|
|
root_weights: root_weights,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 04:29:16 +03:00
|
|
|
#[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 04:29:16 +03:00
|
|
|
}
|
2021-01-18 03:57:10 +03:00
|
|
|
|
2021-01-18 04:29:16 +03:00
|
|
|
impl Storage for InMemoryStorage {
|
2021-01-18 03:57:10 +03:00
|
|
|
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()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|