Add global blacklist to validator-cache (#1168)
This commit is contained in:
@@ -13,6 +13,7 @@ use mixnet_contract_common::{
|
||||
use rocket::fairing::AdHoc;
|
||||
use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
@@ -40,6 +41,9 @@ struct ValidatorCacheInner {
|
||||
mixnodes: Cache<Vec<MixNodeBond>>,
|
||||
gateways: Cache<Vec<GatewayBond>>,
|
||||
|
||||
mixnodes_blacklist: Cache<HashSet<IdentityKey>>,
|
||||
gateways_blacklist: Cache<HashSet<IdentityKey>>,
|
||||
|
||||
rewarded_set: Cache<Vec<MixNodeBond>>,
|
||||
active_set: Cache<Vec<MixNodeBond>>,
|
||||
|
||||
@@ -203,6 +207,8 @@ impl ValidatorCache {
|
||||
routes::get_gateways,
|
||||
routes::get_active_set,
|
||||
routes::get_rewarded_set,
|
||||
routes::get_blacklisted_mixnodes,
|
||||
routes::get_blacklisted_gateways,
|
||||
],
|
||||
)
|
||||
})
|
||||
@@ -225,12 +231,82 @@ impl ValidatorCache {
|
||||
inner.current_reward_params.update(epoch_rewarding_params);
|
||||
}
|
||||
|
||||
pub async fn mixnodes(&self) -> Cache<Vec<MixNodeBond>> {
|
||||
self.inner.read().await.mixnodes.clone()
|
||||
pub async fn mixnodes_blacklist(&self) -> Cache<HashSet<IdentityKey>> {
|
||||
self.inner.read().await.mixnodes_blacklist.clone()
|
||||
}
|
||||
|
||||
pub async fn gateways(&self) -> Cache<Vec<GatewayBond>> {
|
||||
self.inner.read().await.gateways.clone()
|
||||
pub async fn gateways_blacklist(&self) -> Cache<HashSet<IdentityKey>> {
|
||||
self.inner.read().await.gateways_blacklist.clone()
|
||||
}
|
||||
|
||||
pub async fn insert_mixnodes_blacklist(&mut self, mix_identity: IdentityKey) {
|
||||
self.inner
|
||||
.write()
|
||||
.await
|
||||
.mixnodes_blacklist
|
||||
.value
|
||||
.insert(mix_identity);
|
||||
}
|
||||
|
||||
pub async fn remove_mixnodes_blacklist(&mut self, mix_identity: &str) {
|
||||
self.inner
|
||||
.write()
|
||||
.await
|
||||
.mixnodes_blacklist
|
||||
.value
|
||||
.remove(mix_identity);
|
||||
}
|
||||
|
||||
pub async fn insert_gateways_blacklist(&mut self, gateway_identity: IdentityKey) {
|
||||
self.inner
|
||||
.write()
|
||||
.await
|
||||
.gateways_blacklist
|
||||
.value
|
||||
.insert(gateway_identity);
|
||||
}
|
||||
|
||||
pub async fn remove_gateways_blacklist(&mut self, gateway_identity: &str) {
|
||||
self.inner
|
||||
.write()
|
||||
.await
|
||||
.gateways_blacklist
|
||||
.value
|
||||
.remove(gateway_identity);
|
||||
}
|
||||
|
||||
pub async fn mixnodes(&self) -> Vec<MixNodeBond> {
|
||||
let blacklist = self.mixnodes_blacklist().await.value;
|
||||
self.inner
|
||||
.read()
|
||||
.await
|
||||
.mixnodes
|
||||
.value
|
||||
.iter()
|
||||
.filter(|mix| !blacklist.contains(mix.identity()))
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn mixnodes_all(&self) -> Vec<MixNodeBond> {
|
||||
self.inner.read().await.mixnodes.value.clone()
|
||||
}
|
||||
|
||||
pub async fn gateways(&self) -> Vec<GatewayBond> {
|
||||
let blacklist = self.gateways_blacklist().await.value;
|
||||
self.inner
|
||||
.read()
|
||||
.await
|
||||
.gateways
|
||||
.value
|
||||
.iter()
|
||||
.filter(|gateway| !blacklist.contains(gateway.identity()))
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn gateways_all(&self) -> Vec<GatewayBond> {
|
||||
self.inner.read().await.gateways.value.clone()
|
||||
}
|
||||
|
||||
pub async fn rewarded_set(&self) -> Cache<Vec<MixNodeBond>> {
|
||||
@@ -312,6 +388,8 @@ impl ValidatorCacheInner {
|
||||
rewarded_set: Cache::default(),
|
||||
active_set: Cache::default(),
|
||||
current_reward_params: Cache::new(EpochRewardParams::new_empty()),
|
||||
mixnodes_blacklist: Cache::default(),
|
||||
gateways_blacklist: Cache::default(),
|
||||
// setting it to a dummy value on creation is fine, as nothing will be able to ready from it
|
||||
// since 'initialised' flag won't be set
|
||||
current_epoch: Cache::new(None),
|
||||
|
||||
@@ -5,15 +5,16 @@ use crate::contract_cache::ValidatorCache;
|
||||
use mixnet_contract_common::{GatewayBond, MixNodeBond};
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::State;
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[get("/mixnodes")]
|
||||
pub(crate) async fn get_mixnodes(cache: &State<ValidatorCache>) -> Json<Vec<MixNodeBond>> {
|
||||
Json(cache.mixnodes().await.value)
|
||||
Json(cache.mixnodes().await)
|
||||
}
|
||||
|
||||
#[get("/gateways")]
|
||||
pub(crate) async fn get_gateways(cache: &State<ValidatorCache>) -> Json<Vec<GatewayBond>> {
|
||||
Json(cache.gateways().await.value)
|
||||
Json(cache.gateways().await)
|
||||
}
|
||||
|
||||
#[get("/mixnodes/rewarded")]
|
||||
@@ -25,3 +26,17 @@ pub(crate) async fn get_rewarded_set(cache: &State<ValidatorCache>) -> Json<Vec<
|
||||
pub(crate) async fn get_active_set(cache: &State<ValidatorCache>) -> Json<Vec<MixNodeBond>> {
|
||||
Json(cache.active_set().await.value)
|
||||
}
|
||||
|
||||
#[get("/mixnodes/blacklisted")]
|
||||
pub(crate) async fn get_blacklisted_mixnodes(
|
||||
cache: &State<ValidatorCache>,
|
||||
) -> Json<HashSet<String>> {
|
||||
Json(cache.mixnodes_blacklist().await.value)
|
||||
}
|
||||
|
||||
#[get("/gateways/blacklisted")]
|
||||
pub(crate) async fn get_blacklisted_gateways(
|
||||
cache: &State<ValidatorCache>,
|
||||
) -> Json<HashSet<String>> {
|
||||
Json(cache.gateways_blacklist().await.value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user