validator-api: use response type for history and report endpoints (#1711)

This commit is contained in:
Jon Häggblad
2022-10-26 17:26:17 +02:00
committed by GitHub
parent f0aa2feb76
commit 56cf181770
7 changed files with 197 additions and 19 deletions
+7 -4
View File
@@ -3,7 +3,7 @@
use crate::contract_cache::reward_estimate::compute_reward_estimate;
use crate::contract_cache::Cache;
use crate::node_status_api::models::{ErrorResponse, MixnodeStatusReport, MixnodeUptimeHistory};
use crate::node_status_api::models::ErrorResponse;
use crate::storage::ValidatorApiStorage;
use crate::{NodeStatusCache, ValidatorCache};
use cosmwasm_std::Decimal;
@@ -13,26 +13,29 @@ use rocket::http::Status;
use rocket::State;
use validator_api_requests::models::{
ComputeRewardEstParam, InclusionProbabilityResponse, MixnodeCoreStatusResponse,
MixnodeStatusResponse, RewardEstimationResponse, StakeSaturationResponse, UptimeResponse,
MixnodeStatusReportResponse, MixnodeStatusResponse, MixnodeUptimeHistoryResponse,
RewardEstimationResponse, StakeSaturationResponse, UptimeResponse,
};
pub(crate) async fn _mixnode_report(
storage: &ValidatorApiStorage,
mix_id: MixId,
) -> Result<MixnodeStatusReport, ErrorResponse> {
) -> Result<MixnodeStatusReportResponse, ErrorResponse> {
storage
.construct_mixnode_report(mix_id)
.await
.map(MixnodeStatusReportResponse::from)
.map_err(|err| ErrorResponse::new(err.to_string(), Status::NotFound))
}
pub(crate) async fn _mixnode_uptime_history(
storage: &ValidatorApiStorage,
mix_id: MixId,
) -> Result<MixnodeUptimeHistory, ErrorResponse> {
) -> Result<MixnodeUptimeHistoryResponse, ErrorResponse> {
storage
.get_mixnode_uptime_history(mix_id)
.await
.map(MixnodeUptimeHistoryResponse::from)
.map_err(|err| ErrorResponse::new(err.to_string(), Status::NotFound))
}
@@ -21,6 +21,10 @@ use std::convert::TryFrom;
use std::fmt::{self, Display, Formatter};
use std::io::Cursor;
use time::OffsetDateTime;
use validator_api_requests::models::{
GatewayStatusReportResponse, GatewayUptimeHistoryResponse, HistoricalUptimeResponse,
MixnodeStatusReportResponse, MixnodeUptimeHistoryResponse,
};
// todo: put into some error enum
#[derive(Debug)]
@@ -153,6 +157,19 @@ impl MixnodeStatusReport {
}
}
impl From<MixnodeStatusReport> for MixnodeStatusReportResponse {
fn from(status: MixnodeStatusReport) -> Self {
MixnodeStatusReportResponse {
mix_id: status.mix_id,
identity: status.identity,
owner: status.owner,
most_recent: status.most_recent.0,
last_hour: status.last_hour.0,
last_day: status.last_day.0,
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
pub struct GatewayStatusReport {
pub(crate) identity: String,
@@ -190,6 +207,18 @@ impl GatewayStatusReport {
}
}
impl From<GatewayStatusReport> for GatewayStatusReportResponse {
fn from(status: GatewayStatusReport) -> Self {
GatewayStatusReportResponse {
identity: status.identity,
owner: status.owner,
most_recent: status.most_recent.0,
last_hour: status.last_hour.0,
last_day: status.last_day.0,
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
pub struct MixnodeUptimeHistory {
pub(crate) mix_id: MixId,
@@ -215,6 +244,17 @@ impl MixnodeUptimeHistory {
}
}
impl From<MixnodeUptimeHistory> for MixnodeUptimeHistoryResponse {
fn from(history: MixnodeUptimeHistory) -> Self {
MixnodeUptimeHistoryResponse {
mix_id: history.mix_id,
identity: history.identity,
owner: history.owner,
history: history.history.into_iter().map(Into::into).collect(),
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
pub struct GatewayUptimeHistory {
pub(crate) identity: String,
@@ -233,6 +273,16 @@ impl GatewayUptimeHistory {
}
}
impl From<GatewayUptimeHistory> for GatewayUptimeHistoryResponse {
fn from(history: GatewayUptimeHistory) -> Self {
GatewayUptimeHistoryResponse {
identity: history.identity,
owner: history.owner,
history: history.history.into_iter().map(Into::into).collect(),
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
pub struct HistoricalUptime {
// ISO 8601 date string
@@ -242,6 +292,15 @@ pub struct HistoricalUptime {
pub(crate) uptime: Uptime,
}
impl From<HistoricalUptime> for HistoricalUptimeResponse {
fn from(uptime: HistoricalUptime) -> Self {
HistoricalUptimeResponse {
date: uptime.date,
uptime: uptime.uptime.0,
}
}
}
pub(crate) struct ErrorResponse {
error_message: String,
status: Status,
+11 -10
View File
@@ -8,10 +8,7 @@ use crate::node_status_api::helpers::{
_get_mixnode_stake_saturation, _get_mixnode_status, _mixnode_core_status_count,
_mixnode_report, _mixnode_uptime_history,
};
use crate::node_status_api::models::{
ErrorResponse, GatewayStatusReport, GatewayUptimeHistory, MixnodeStatusReport,
MixnodeUptimeHistory,
};
use crate::node_status_api::models::ErrorResponse;
use crate::storage::ValidatorApiStorage;
use crate::ValidatorCache;
use mixnet_contract_common::MixId;
@@ -21,8 +18,10 @@ use rocket::State;
use rocket_okapi::openapi;
use validator_api_requests::models::{
AllInclusionProbabilitiesResponse, ComputeRewardEstParam, GatewayCoreStatusResponse,
InclusionProbabilityResponse, MixnodeCoreStatusResponse, MixnodeStatusResponse,
RewardEstimationResponse, StakeSaturationResponse, UptimeResponse,
GatewayStatusReportResponse, GatewayUptimeHistoryResponse, InclusionProbabilityResponse,
MixnodeCoreStatusResponse, MixnodeStatusReportResponse, MixnodeStatusResponse,
MixnodeUptimeHistoryResponse, RewardEstimationResponse, StakeSaturationResponse,
UptimeResponse,
};
#[openapi(tag = "status")]
@@ -30,10 +29,11 @@ use validator_api_requests::models::{
pub(crate) async fn gateway_report(
storage: &State<ValidatorApiStorage>,
identity: &str,
) -> Result<Json<GatewayStatusReport>, ErrorResponse> {
) -> Result<Json<GatewayStatusReportResponse>, ErrorResponse> {
storage
.construct_gateway_report(identity)
.await
.map(GatewayStatusReportResponse::from)
.map(Json)
.map_err(|err| ErrorResponse::new(err.to_string(), Status::NotFound))
}
@@ -43,10 +43,11 @@ pub(crate) async fn gateway_report(
pub(crate) async fn gateway_uptime_history(
storage: &State<ValidatorApiStorage>,
identity: &str,
) -> Result<Json<GatewayUptimeHistory>, ErrorResponse> {
) -> Result<Json<GatewayUptimeHistoryResponse>, ErrorResponse> {
storage
.get_gateway_uptime_history(identity)
.await
.map(GatewayUptimeHistoryResponse::from)
.map(Json)
.map_err(|err| ErrorResponse::new(err.to_string(), Status::NotFound))
}
@@ -74,7 +75,7 @@ pub(crate) async fn gateway_core_status_count(
pub(crate) async fn mixnode_report(
storage: &State<ValidatorApiStorage>,
mix_id: MixId,
) -> Result<Json<MixnodeStatusReport>, ErrorResponse> {
) -> Result<Json<MixnodeStatusReportResponse>, ErrorResponse> {
Ok(Json(_mixnode_report(storage, mix_id).await?))
}
@@ -83,7 +84,7 @@ pub(crate) async fn mixnode_report(
pub(crate) async fn mixnode_uptime_history(
storage: &State<ValidatorApiStorage>,
mix_id: MixId,
) -> Result<Json<MixnodeUptimeHistory>, ErrorResponse> {
) -> Result<Json<MixnodeUptimeHistoryResponse>, ErrorResponse> {
Ok(Json(_mixnode_uptime_history(storage, mix_id).await?))
}