Files
nym/validator-api/src/node_status_api/models.rs
T
Jędrzej Stuczyński 020cad897d Feature/rust rewarding (#750)
* Calculating gas fees

* Ability to set custom fees

* Added extra test

* Removed commented code

* Moved all msg types to common contract crate

* Temporarily disabling get_tx method

* Finishing up nymd client API

* Comment fix

* Remaining fee values

* Some cleanup

* Removed needless borrow

* Fixed imports in contract tests

* Moved error types around

* New ValidatorClient

* Experiment with new type of defaults

* Removed dead module

* Dealt with unwrap

* Migrated mixnode to use new validator client

* Migrated gateway to use new validator client

* Mixnode and gateway adjustments

* More exported defaults

* Clients using new validator client

* Fixed mixnode upgrade

* Moved default values to a new crate

* Changed behaviour of validator client features

* Migrated basic functions of validator api

* Updated config + fixed startup

* Fixed wasm client build

* Integration with the explorer api

* Removed tokio dev dependency

* Needless borrow

* Fixex wasm client build

* Fixed tauri client build

* Needless borrows

* New tables for rewarding

* Updated cosmos-sdk version

* Removed reward-specific node status routes

* New rewarding-specific config entries

* Additional network defaults

* Initial periodic rewards from validator api

* Replaced print with log

* Filtering nodes with uptime > 0

* Additional failure logging statements

* Fixed operation ordering

* Adjusted next rewarding epoch determination

* Modified rewarding behaviour to keep track of rewarding in progress

* Improved error message on config load failure

* Additional log statement

* Adjusted rewarding gas limit calculation

* Made naming slightly more consistent

* Fixed incorrect parentheses placement

* Fixed fee calculation

* Cargo fmt

* Removed failed merge artifacts

* Introduced comment for any future reward modification

* typos

* Helper functions for the future

* Making @mfahampshire 's life easier

* Redesigned epoch + rewarding skipped epochs (if possible)

* Removed old merge artifacts

* Naming consistency

* Constraining arguments

* Removed unnecessary if branch

* Ignore monitor check for current epoch

* Additional checks for current epoch data

* Monitor threshold check

* cargo fmt

* Fixed post-merge issues in transactions.rs
2021-09-24 15:49:21 +01:00

277 lines
7.5 KiB
Rust

// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::node_status_api::utils::NodeUptimes;
use crate::storage::models::NodeStatus;
use rocket::http::{ContentType, Status};
use rocket::response::{self, Responder, Response};
use rocket::Request;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::fmt::{self, Display, Formatter};
use std::io::Cursor;
use time::OffsetDateTime;
// todo: put into some error enum
#[derive(Debug)]
pub struct InvalidUptime;
// value in range 0-100
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub struct Uptime(u8);
impl Uptime {
pub const fn zero() -> Self {
Uptime(0)
}
pub fn from_ratio(numerator: usize, denominator: usize) -> Result<Self, InvalidUptime> {
if denominator == 0 {
return Ok(Self::zero());
}
let uptime = ((numerator as f32 / denominator as f32) * 100.0) as u8;
if uptime > 100 {
Err(InvalidUptime)
} else {
Ok(Uptime(uptime))
}
}
pub fn u8(&self) -> u8 {
self.0
}
}
impl From<Uptime> for u8 {
fn from(uptime: Uptime) -> Self {
uptime.0
}
}
impl TryFrom<u8> for Uptime {
type Error = InvalidUptime;
fn try_from(value: u8) -> Result<Self, Self::Error> {
if value > 100 {
Err(InvalidUptime)
} else {
Ok(Uptime(value))
}
}
}
impl TryFrom<i64> for Uptime {
type Error = InvalidUptime;
fn try_from(value: i64) -> Result<Self, Self::Error> {
if !(0..=100).contains(&value) {
Err(InvalidUptime)
} else {
Ok(Uptime(value as u8))
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct MixnodeStatusReport {
pub(crate) identity: String,
pub(crate) owner: String,
pub(crate) most_recent_ipv4: bool,
pub(crate) most_recent_ipv6: bool,
pub(crate) last_hour_ipv4: Uptime,
pub(crate) last_hour_ipv6: Uptime,
pub(crate) last_day_ipv4: Uptime,
pub(crate) last_day_ipv6: Uptime,
}
impl MixnodeStatusReport {
pub(crate) fn construct_from_last_day_reports(
report_time: OffsetDateTime,
identity: String,
owner: String,
last_day_ipv4: Vec<NodeStatus>,
last_day_ipv6: Vec<NodeStatus>,
last_hour_test_runs: usize,
last_day_test_runs: usize,
) -> Self {
let node_uptimes = NodeUptimes::calculate_from_last_day_reports(
report_time,
last_day_ipv4,
last_day_ipv6,
last_hour_test_runs,
last_day_test_runs,
);
MixnodeStatusReport {
identity,
owner,
most_recent_ipv4: node_uptimes.most_recent_ipv4,
most_recent_ipv6: node_uptimes.most_recent_ipv6,
last_hour_ipv4: node_uptimes.last_hour_ipv4,
last_hour_ipv6: node_uptimes.last_hour_ipv6,
last_day_ipv4: node_uptimes.last_day_ipv4,
last_day_ipv6: node_uptimes.last_day_ipv6,
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct GatewayStatusReport {
pub(crate) identity: String,
pub(crate) owner: String,
pub(crate) most_recent_ipv4: bool,
pub(crate) most_recent_ipv6: bool,
pub(crate) last_hour_ipv4: Uptime,
pub(crate) last_hour_ipv6: Uptime,
pub(crate) last_day_ipv4: Uptime,
pub(crate) last_day_ipv6: Uptime,
}
impl GatewayStatusReport {
pub(crate) fn construct_from_last_day_reports(
report_time: OffsetDateTime,
identity: String,
owner: String,
last_day_ipv4: Vec<NodeStatus>,
last_day_ipv6: Vec<NodeStatus>,
last_hour_test_runs: usize,
last_day_test_runs: usize,
) -> Self {
let node_uptimes = NodeUptimes::calculate_from_last_day_reports(
report_time,
last_day_ipv4,
last_day_ipv6,
last_hour_test_runs,
last_day_test_runs,
);
GatewayStatusReport {
identity,
owner,
most_recent_ipv4: node_uptimes.most_recent_ipv4,
most_recent_ipv6: node_uptimes.most_recent_ipv6,
last_hour_ipv4: node_uptimes.last_hour_ipv4,
last_hour_ipv6: node_uptimes.last_hour_ipv6,
last_day_ipv4: node_uptimes.last_day_ipv4,
last_day_ipv6: node_uptimes.last_day_ipv6,
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct MixnodeUptimeHistory {
pub(crate) identity: String,
pub(crate) owner: String,
pub(crate) history: Vec<HistoricalUptime>,
}
impl MixnodeUptimeHistory {
pub(crate) fn new(identity: String, owner: String, history: Vec<HistoricalUptime>) -> Self {
MixnodeUptimeHistory {
identity,
owner,
history,
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct GatewayUptimeHistory {
pub(crate) identity: String,
pub(crate) owner: String,
pub(crate) history: Vec<HistoricalUptime>,
}
impl GatewayUptimeHistory {
pub(crate) fn new(identity: String, owner: String, history: Vec<HistoricalUptime>) -> Self {
GatewayUptimeHistory {
identity,
owner,
history,
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct HistoricalUptime {
// ISO 8601 date string
// I think this is more than enough, we don't need the uber precision of timezone offsets, etc
pub(crate) date: String,
pub(crate) ipv4_uptime: Uptime,
pub(crate) ipv6_uptime: Uptime,
}
pub(crate) struct ErrorResponse {
error: NodeStatusApiError,
status: Status,
}
impl ErrorResponse {
pub(crate) fn new(error: NodeStatusApiError, status: Status) -> Self {
ErrorResponse { error, status }
}
}
impl<'r, 'o: 'r> Responder<'r, 'o> for ErrorResponse {
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'o> {
let message = format!("{}", self.error);
Response::build()
.header(ContentType::Plain)
.sized_body(message.len(), Cursor::new(message))
.status(self.status)
.ok()
}
}
#[derive(Debug)]
pub enum NodeStatusApiError {
MixnodeReportNotFound(String),
GatewayReportNotFound(String),
MixnodeUptimeHistoryNotFound(String),
GatewayUptimeHistoryNotFound(String),
// I don't think we want to expose errors to the user about what really happened
InternalDatabaseError,
}
impl Display for NodeStatusApiError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
NodeStatusApiError::MixnodeReportNotFound(identity) => write!(
f,
"Could not find status report associated with mixnode {}",
identity
),
NodeStatusApiError::GatewayReportNotFound(identity) => write!(
f,
"Could not find status report associated with gateway {}",
identity
),
NodeStatusApiError::MixnodeUptimeHistoryNotFound(identity) => write!(
f,
"Could not find uptime history associated with mixnode {}",
identity
),
NodeStatusApiError::GatewayUptimeHistoryNotFound(identity) => write!(
f,
"Could not find uptime history associated with gateway {}",
identity
),
NodeStatusApiError::InternalDatabaseError => {
write!(f, "The internal database has experienced an issue")
}
}
}
}