Additional, more informative routes (#1204)

* Have reward set updater run its own timer (#1200)

* Have reward set updater run its own timer

* Filter rocket log spam

* Take last day of uptime for rewarding (#1202)

* Take last day of uptime for rewarding

* Rejigger calculations

* Blacklist based on last 24 hr

* Cleanup

* Clippy

* Additional, more informative routes

* Improve blacklist updates

* Fix rewards estimation
This commit is contained in:
Drazen Urch
2022-04-12 11:55:32 +02:00
committed by GitHub
parent 25e1bfa345
commit d3372bfc85
21 changed files with 486 additions and 343 deletions
+43 -42
View File
@@ -17,7 +17,7 @@ use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{Notify, RwLock};
use tokio::sync::RwLock;
use tokio::time;
use validator_api_requests::models::MixnodeStatus;
use validator_client::nymd::CosmWasmClient;
@@ -28,7 +28,6 @@ pub struct ValidatorCacheRefresher<C> {
nymd_client: Client<C>,
cache: ValidatorCache,
caching_interval: Duration,
update_rewarded_set_notify: Option<Arc<Notify>>,
}
#[derive(Clone)]
@@ -89,13 +88,11 @@ impl<C> ValidatorCacheRefresher<C> {
nymd_client: Client<C>,
caching_interval: Duration,
cache: ValidatorCache,
update_rewarded_set_notify: Option<Arc<Notify>>,
) -> Self {
ValidatorCacheRefresher {
nymd_client,
cache,
caching_interval,
update_rewarded_set_notify,
}
}
@@ -136,6 +133,7 @@ impl<C> ValidatorCacheRefresher<C> {
self.collect_rewarded_and_active_set_details(&mixnodes, rewarded_set_identities);
let epoch_rewarding_params = self.nymd_client.get_current_epoch_reward_params().await?;
let current_epoch = self.nymd_client.get_current_epoch().await?;
info!(
"Updating validator cache. There are {} mixnodes and {} gateways",
@@ -150,23 +148,10 @@ impl<C> ValidatorCacheRefresher<C> {
rewarded_set,
active_set,
epoch_rewarding_params,
current_epoch,
)
.await;
if let Some(notify) = &self.update_rewarded_set_notify {
let update_details = self
.nymd_client
.get_current_rewarded_set_update_details()
.await?;
if update_details.last_refreshed_block + (update_details.refresh_rate_blocks as u64)
< update_details.current_height
{
// there's only ever a single waiter -> the set updater
notify.notify_one()
}
}
Ok(())
}
@@ -209,6 +194,8 @@ impl ValidatorCache {
routes::get_rewarded_set,
routes::get_blacklisted_mixnodes,
routes::get_blacklisted_gateways,
routes::get_epoch_reward_params,
routes::get_current_epoch
],
)
})
@@ -221,6 +208,7 @@ impl ValidatorCache {
rewarded_set: Vec<MixNodeBond>,
active_set: Vec<MixNodeBond>,
epoch_rewarding_params: EpochRewardParams,
current_epoch: Interval,
) {
let mut inner = self.inner.write().await;
@@ -229,6 +217,7 @@ impl ValidatorCache {
inner.rewarded_set.update(rewarded_set);
inner.active_set.update(active_set);
inner.current_reward_params.update(epoch_rewarding_params);
inner.current_epoch.update(Some(current_epoch));
}
pub async fn mixnodes_blacklist(&self) -> Cache<HashSet<IdentityKey>> {
@@ -239,40 +228,52 @@ impl ValidatorCache {
self.inner.read().await.gateways_blacklist.clone()
}
pub async fn insert_mixnodes_blacklist(&mut self, mix_identity: IdentityKey) {
pub async fn update_mixnodes_blacklist(
&self,
add: HashSet<IdentityKey>,
remove: HashSet<IdentityKey>,
) {
let blacklist = self.mixnodes_blacklist().await.value;
let mut blacklist = blacklist
.union(&add)
.cloned()
.collect::<HashSet<IdentityKey>>();
let to_remove = blacklist
.intersection(&remove)
.cloned()
.collect::<HashSet<IdentityKey>>();
for key in to_remove {
blacklist.remove(&key);
}
self.inner
.write()
.await
.mixnodes_blacklist
.value
.insert(mix_identity);
.update(blacklist);
}
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) {
pub async fn update_gateways_blacklist(
&self,
add: HashSet<IdentityKey>,
remove: HashSet<IdentityKey>,
) {
let blacklist = self.gateways_blacklist().await.value;
let mut blacklist = blacklist
.union(&add)
.cloned()
.collect::<HashSet<IdentityKey>>();
let to_remove = blacklist
.intersection(&remove)
.cloned()
.collect::<HashSet<IdentityKey>>();
for key in to_remove {
blacklist.remove(&key);
}
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);
.update(blacklist);
}
pub async fn mixnodes(&self) -> Vec<MixNodeBond> {
+18 -11
View File
@@ -2,41 +2,48 @@
// SPDX-License-Identifier: Apache-2.0
use crate::contract_cache::ValidatorCache;
use mixnet_contract_common::{GatewayBond, MixNodeBond};
use mixnet_contract_common::reward_params::EpochRewardParams;
use mixnet_contract_common::{GatewayBond, Interval, 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>> {
pub async fn get_mixnodes(cache: &State<ValidatorCache>) -> Json<Vec<MixNodeBond>> {
Json(cache.mixnodes().await)
}
#[get("/gateways")]
pub(crate) async fn get_gateways(cache: &State<ValidatorCache>) -> Json<Vec<GatewayBond>> {
pub async fn get_gateways(cache: &State<ValidatorCache>) -> Json<Vec<GatewayBond>> {
Json(cache.gateways().await)
}
#[get("/mixnodes/rewarded")]
pub(crate) async fn get_rewarded_set(cache: &State<ValidatorCache>) -> Json<Vec<MixNodeBond>> {
pub async fn get_rewarded_set(cache: &State<ValidatorCache>) -> Json<Vec<MixNodeBond>> {
Json(cache.rewarded_set().await.value)
}
#[get("/mixnodes/active")]
pub(crate) async fn get_active_set(cache: &State<ValidatorCache>) -> Json<Vec<MixNodeBond>> {
pub 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>> {
pub 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>> {
pub async fn get_blacklisted_gateways(cache: &State<ValidatorCache>) -> Json<HashSet<String>> {
Json(cache.gateways_blacklist().await.value)
}
#[get("/epoch/reward_params")]
pub async fn get_epoch_reward_params(cache: &State<ValidatorCache>) -> Json<EpochRewardParams> {
Json(cache.epoch_reward_params().await.value)
}
#[get("/epoch/current")]
pub async fn get_current_epoch(cache: &State<ValidatorCache>) -> Json<Option<Interval>> {
Json(cache.current_epoch().await.value)
}