Skip to content

Commit

Permalink
Bucket inclusion probabilities (#1224)
Browse files Browse the repository at this point in the history
* Bucket inclusion probabilities

* Make display equal variant name for TS
  • Loading branch information
durch authored Apr 26, 2022
1 parent c250492 commit 632612e
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ members = [
"clients/native",
"clients/native/websocket-requests",
"clients/socks5",
# "clients/tauri-client/src-tauri",
"common/client-libs/gateway-client",
"common/client-libs/mixnet-client",
"common/client-libs/validator-client",
Expand Down
6 changes: 5 additions & 1 deletion explorer-api/src/mix_node/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ pub(crate) async fn get_economic_dynamics_stats(
retrieve_mixnode_econ_stats(&state.inner.validator_client, pubkey).await?;

// update cache
state.inner.mixnode.set_econ_stats(pubkey, econ_stats).await;
state
.inner
.mixnode
.set_econ_stats(pubkey, econ_stats.clone())
.await;
Some(Json(econ_stats))
}
}
Expand Down
7 changes: 4 additions & 3 deletions explorer-api/src/mix_node/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::Serialize;
use std::sync::Arc;
use std::time::SystemTime;
use tokio::sync::RwLock;
use validator_client::models::SelectionChance;

#[derive(Clone, Debug, Serialize, JsonSchema, PartialEq)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -122,12 +123,12 @@ pub(crate) struct NodeStats {
packets_explicitly_dropped_since_last_update: u64,
}

#[derive(Serialize, Clone, Copy, Deserialize, JsonSchema)]
#[derive(Clone, Serialize, Deserialize, JsonSchema)]
pub(crate) struct EconomicDynamicsStats {
pub(crate) stake_saturation: f32,

pub(crate) active_set_inclusion_probability: f32,
pub(crate) reserve_set_inclusion_probability: f32,
pub(crate) active_set_inclusion_probability: SelectionChance,
pub(crate) reserve_set_inclusion_probability: SelectionChance,

pub(crate) estimated_total_node_reward: u64,
pub(crate) estimated_operator_reward: u64,
Expand Down
7 changes: 3 additions & 4 deletions nym-wallet/src/types/rust/inclusionprobabilityresponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export interface InclusionProbabilityResponse {
in_active: number;
in_reserve: number;
}
import type { SelectionChance } from "./selectionchance";

export interface InclusionProbabilityResponse { in_active: SelectionChance, in_reserve: SelectionChance, }
2 changes: 2 additions & 0 deletions nym-wallet/src/types/rust/selectionchance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export type SelectionChance = "VeryHigh" | "High" | "Moderate" | "Low" | "VeryLow";
12 changes: 2 additions & 10 deletions validator-api/src/node_status_api/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,8 @@ pub(crate) async fn get_mixnode_inclusion_probability(
// (rewarded_set_size - active_set_size) * prob_one_draw * (1. - prob_active_set);

Json(Some(InclusionProbabilityResponse {
in_active: if prob_active_set > 1. {
1.
} else {
prob_active_set
} as f32,
in_reserve: if prob_reserve_set > 1. {
1.
} else {
prob_reserve_set
} as f32,
in_active: prob_active_set.into(),
in_reserve: prob_reserve_set.into(),
}))
} else {
Json(None)
Expand Down
1 change: 1 addition & 0 deletions validator-api/validator-api-requests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
serde = "1.0"
mixnet-contract-common = { path= ".../../../../common/cosmwasm-smart-contracts/mixnet-contract" }
schemars = { version = "0.8", features = ["preserve_order"] }

[dev-dependencies]
ts-rs = "6.1.2"
50 changes: 46 additions & 4 deletions validator-api/validator-api-requests/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use mixnet_contract_common::reward_params::RewardParams;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;

Expand Down Expand Up @@ -76,7 +77,48 @@ pub struct StakeSaturationResponse {
pub as_at: i64,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[cfg_attr(test, derive(ts_rs::TS))]
#[cfg_attr(
test,
ts(
export,
export_to = "../../nym-wallet/src/types/rust/selectionchance.ts"
)
)]
pub enum SelectionChance {
VeryHigh,
High,
Moderate,
Low,
VeryLow,
}

impl From<f64> for SelectionChance {
fn from(p: f64) -> SelectionChance {
match p {
p if p >= 1. => SelectionChance::VeryHigh,
p if p > 0.9 => SelectionChance::High,
p if p > 0.7 => SelectionChance::Moderate,
p if p > 0.5 => SelectionChance::Low,
_ => SelectionChance::VeryLow,
}
}
}

impl fmt::Display for SelectionChance {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SelectionChance::VeryHigh => write!(f, "VeryHigh"),
SelectionChance::High => write!(f, "High"),
SelectionChance::Moderate => write!(f, "Moderate"),
SelectionChance::Low => write!(f, "Low"),
SelectionChance::VeryLow => write!(f, "VeryLow"),
}
}
}

#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(test, derive(ts_rs::TS))]
#[cfg_attr(
test,
Expand All @@ -86,15 +128,15 @@ pub struct StakeSaturationResponse {
)
)]
pub struct InclusionProbabilityResponse {
pub in_active: f32,
pub in_reserve: f32,
pub in_active: SelectionChance,
pub in_reserve: SelectionChance,
}

impl fmt::Display for InclusionProbabilityResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"in_active: {:.5}, in_reserve: {:.5}",
"in_active: {}, in_reserve: {}",
self.in_active, self.in_reserve
)
}
Expand Down

0 comments on commit 632612e

Please sign in to comment.