Endpoints for average mixnode uptime (#1238)

This commit is contained in:
Jon Häggblad
2022-05-04 11:28:41 +02:00
committed by GitHub
parent 8eb3f6f862
commit 139e89643c
9 changed files with 161 additions and 2 deletions
+2
View File
@@ -30,6 +30,8 @@ pub(crate) fn stage_full() -> AdHoc {
routes::get_mixnode_reward_estimation,
routes::get_mixnode_stake_saturation,
routes::get_mixnode_inclusion_probability,
routes::get_mixnode_avg_uptime,
routes::get_mixnode_avg_uptimes,
],
)
})
+55 -1
View File
@@ -8,13 +8,14 @@ use crate::node_status_api::models::{
use crate::storage::ValidatorApiStorage;
use crate::ValidatorCache;
use mixnet_contract_common::reward_params::{NodeRewardParams, RewardParams};
use mixnet_contract_common::Interval;
use rocket::http::Status;
use rocket::serde::json::Json;
use rocket::State;
use rocket_okapi::openapi;
use validator_api_requests::models::{
CoreNodeStatusResponse, InclusionProbabilityResponse, MixnodeStatusResponse,
RewardEstimationResponse, StakeSaturationResponse,
RewardEstimationResponse, StakeSaturationResponse, UptimeResponse,
};
use super::models::Uptime;
@@ -237,3 +238,56 @@ pub(crate) async fn get_mixnode_inclusion_probability(
Json(None)
}
}
async fn average_mixnode_uptime(
identity: &str,
current_epoch: Option<Interval>,
storage: &State<ValidatorApiStorage>,
) -> Result<Uptime, ErrorResponse> {
Ok(if let Some(epoch) = current_epoch {
storage
.get_average_mixnode_uptime_in_the_last_24hrs(identity, epoch.end_unix_timestamp())
.await
.map_err(|err| ErrorResponse::new(err.to_string(), Status::NotFound))?
} else {
Uptime::default()
})
}
#[openapi(tag = "mixnode")]
#[get("/mixnode/<identity>/avg_uptime")]
pub(crate) async fn get_mixnode_avg_uptime(
cache: &State<ValidatorCache>,
storage: &State<ValidatorApiStorage>,
identity: String,
) -> Result<Json<UptimeResponse>, ErrorResponse> {
let current_epoch = cache.current_epoch().await.into_inner();
let uptime = average_mixnode_uptime(&identity, current_epoch, storage).await?;
Ok(Json(UptimeResponse {
identity,
avg_uptime: uptime.u8(),
}))
}
#[openapi(tag = "mixnode")]
#[get("/mixnodes/avg_uptime")]
pub(crate) async fn get_mixnode_avg_uptimes(
cache: &State<ValidatorCache>,
storage: &State<ValidatorApiStorage>,
) -> Result<Json<Vec<UptimeResponse>>, ErrorResponse> {
let mixnodes = cache.mixnodes().await;
let current_epoch = cache.current_epoch().await.into_inner();
let mut response = Vec::new();
for mixnode in mixnodes {
let uptime = average_mixnode_uptime(mixnode.identity(), current_epoch, storage).await?;
response.push(UptimeResponse {
identity: mixnode.identity().to_string(),
avg_uptime: uptime.u8(),
})
}
Ok(Json(response))
}