Generalize cache, and cache gateways (#666)

* Generalize cache, and cache gateways

* Allow dead code
This commit is contained in:
Drazen Urch
2021-07-01 17:24:23 +02:00
committed by GitHub
parent 57df77a995
commit 2e6a32b298
2 changed files with 65 additions and 33 deletions
+52 -25
View File
@@ -1,41 +1,68 @@
use anyhow::Result;
use mixnet_contract::MixNodeBond;
use mixnet_contract::{GatewayBond, MixNodeBond};
use std::time::Instant;
use validator_client::Client;
pub struct MixNodeCache {
value: Vec<MixNodeBond>,
#[allow(dead_code)]
as_at: Instant,
pub struct ValidatorCache {
mixnodes: Cache<Vec<MixNodeBond>>,
gateways: Cache<Vec<GatewayBond>>,
validator_client: Client,
}
impl MixNodeCache {
pub fn init(
value: Vec<MixNodeBond>,
validators_rest_uris: Vec<String>,
mixnet_contract: String,
) -> Self {
#[derive(Default)]
struct Cache<T> {
value: T,
#[allow(dead_code)]
as_at: Option<Instant>,
}
impl<T: Clone> Cache<T> {
fn set(&mut self, value: T) {
self.value = value;
self.as_at = Some(Instant::now())
}
pub fn get(&self) -> T {
self.value.clone()
}
}
impl ValidatorCache {
pub fn init(validators_rest_uris: Vec<String>, mixnet_contract: String) -> Self {
let config = validator_client::Config::new(validators_rest_uris, mixnet_contract);
let validator_client = validator_client::Client::new(config);
MixNodeCache {
value,
as_at: Instant::now(),
ValidatorCache {
mixnodes: Cache::default(),
gateways: Cache::default(),
validator_client,
}
}
fn set_value(&mut self, value: Vec<MixNodeBond>) {
self.value = value;
self.as_at = Instant::now()
}
pub fn value(&self) -> Vec<MixNodeBond> {
self.value.clone()
}
pub async fn cache(&mut self) -> Result<()> {
let mixnodes = self.validator_client.get_mix_nodes().await?;
self.set_value(mixnodes);
// We need to make validator_api non mut first
// tokio::join!(self.cache_mixnodes(), self.cache_gateways());
self.cache_mixnodes().await?;
self.cache_gateways().await?;
Ok(())
}
async fn cache_mixnodes(&mut self) -> Result<()> {
let mixnodes = self.validator_client.get_mix_nodes().await?;
self.mixnodes.set(mixnodes);
Ok(())
}
async fn cache_gateways(&mut self) -> Result<()> {
let gateways = self.validator_client.get_gateways().await?;
self.gateways.set(gateways);
Ok(())
}
pub fn mixnodes(&self) -> Vec<MixNodeBond> {
self.mixnodes.get()
}
pub fn gateways(&self) -> Vec<GatewayBond> {
self.gateways.get()
}
}
+13 -8
View File
@@ -21,12 +21,12 @@ use crate::monitor::summary_producer::SummaryProducer;
use crate::tested_network::good_topology::parse_topology_file;
use crate::tested_network::TestedNetwork;
use anyhow::Result;
use cache::MixNodeCache;
use cache::ValidatorCache;
use clap::{App, Arg, ArgMatches};
use crypto::asymmetric::{encryption, identity};
use futures::channel::mpsc;
use log::info;
use mixnet_contract::MixNodeBond;
use mixnet_contract::{GatewayBond, MixNodeBond};
use nymsphinx::addressing::clients::Recipient;
use std::sync::Arc;
use std::time::Duration;
@@ -265,9 +265,15 @@ fn check_if_up_to_date(v4_topology: &NymTopology, v6_topology: &NymTopology) {
}
#[get("/mixnodes")]
async fn get_mixnodes(mixnode_cache: &State<Arc<RwLock<MixNodeCache>>>) -> Json<Vec<MixNodeBond>> {
let mixnodes = mixnode_cache.read().await;
Json(mixnodes.value())
async fn get_mixnodes(cache: &State<Arc<RwLock<ValidatorCache>>>) -> Json<Vec<MixNodeBond>> {
let cache = cache.read().await;
Json(cache.mixnodes())
}
#[get("/gateways")]
async fn get_gateways(cache: &State<Arc<RwLock<ValidatorCache>>>) -> Json<Vec<GatewayBond>> {
let cache = cache.read().await;
Json(cache.gateways())
}
#[tokio::main]
@@ -380,8 +386,7 @@ async fn main() -> Result<()> {
tested_network,
);
let mixnode_cache = Arc::new(RwLock::new(MixNodeCache::init(
vec![],
let mixnode_cache = Arc::new(RwLock::new(ValidatorCache::init(
validators_rest_uris,
mixnet_contract,
)));
@@ -423,7 +428,7 @@ async fn main() -> Result<()> {
rocket::build()
.attach(cors)
.mount("/", routes![get_mixnodes])
.mount("/v1", routes![get_mixnodes, get_gateways])
.manage(mixnode_cache)
.ignite()
.await?