diff --git a/src/datastore.rs b/src/datastore.rs index 506e657..5ce1144 100644 --- a/src/datastore.rs +++ b/src/datastore.rs @@ -7,7 +7,7 @@ use std::time::SystemTime; use tabled::{Table, Tabled}; use tokio::sync::Mutex; -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub struct NodeInfo { pub pubkey: VerifyingKey, pub updated_at: SystemTime, @@ -65,11 +65,15 @@ impl Store { pub async fn add_mock_node(&self, ip: String) { let mut csprng = OsRng; let privkey = ed25519_dalek::SigningKey::generate(&mut csprng); - self.add_node(ip, NodeInfo { - pubkey: privkey.verifying_key(), - updated_at: std::time::SystemTime::now(), - online: true, - }).await; + self.update_node( + ip, + NodeInfo { + pubkey: privkey.verifying_key(), + updated_at: std::time::SystemTime::now(), + online: true, + }, + ) + .await; self.add_key(privkey.verifying_key(), privkey).await; } @@ -128,9 +132,16 @@ impl Store { keys.get(pubkey).cloned() } - pub async fn add_node(&self, ip: String, info: NodeInfo) { + /// This returns true if NodeInfo got modified. + /// + /// On a side note, there are two types of people in this worlds: + /// 1. Those that can extrapolate... + pub async fn update_node(&self, ip: String, info: NodeInfo) -> bool { let mut nodes = self.nodes.lock().await; - nodes.insert(ip, info); + match nodes.insert(ip, info.clone()) { + Some(old_info) => old_info.ne(&info), + None => false, + } } pub async fn remove_node(&self, ip: &str) { @@ -147,7 +158,7 @@ impl Store { let mut csprng = OsRng; // TODO: save old private key to disk using SGX Sealing let privkey = ed25519_dalek::SigningKey::generate(&mut csprng); - self.add_node( + self.update_node( "localhost".to_string(), NodeInfo { pubkey: privkey.verifying_key(),