51b511b27e
WIP; rebasing Another branch squash Squashing the v3 branch changing min pledge amounts logic for adding new nymnode into the contract converting mixnode/gateway bonding into nym-node bonding logic for migrating gateways into nymnodes ibid for mixnodes further nym-node work + fixed most existing unit tests forbid nymnode migration with pending cost params changes preassign nodeid for gateways changing role assignment and epoch progression changing role assignment and epoch progression optional custom http port logic for unbonding a nym-node updating Delegation struct logic for increasing pledge of either mixnode or nymnode logic for decreasing pledge of either mixnode or a nym node logic for changing cost params of either mixnode or a nym node wip initialise nymnodes storage fixing transaction tests fixed naive family tests reward-compatibility related works resolving delegation events introduced rewarded set metadata another iteration of restoring old tests updated rewarding part of nym-api parking the branch unparking the branch wip purged families added 'ExitGateway' role passing explicit work factor for rewarding function remove legacy layers storage wip: node description queries added announced ports to self-described api step1 in gruelling journey of adding node_id to gateways ensure epoch work never goes above 1.0 changed active set to contain role distribution [theoretically] sending rewarding messages for the new rewarded set [theoretically] assigning new rewarded set reimplementing more nym-api features remove legacy types re-implement legacy network monitor restoring further routes + minor refactor of NodeStatusCache skimmed routes now return legacy nodes alongside nym-nodes seemingly restored all functionalities in nym-api removing more legacy things from the contract initial contract cleanup added nym-api endpoints to return generic annotations regardless of type updated simulator to use new rewarding parameters more contract cleanup made existing mixnet contract tests compile extra validation of nym-node bonding parameters fixed additional compilation issues fixed nym-api v3 database migration failure added additional nym-node contract queries updated the schema made additional delegation/rewards queries compatible with both legacy mixnodes and nym-nodes fixing existing unit tests in mixnet contract wip resolved first batch of 500 compiler errors re-deprecating routes making wallet's rust backend compile fixed non-determinism in contract + nym-api build fixes to the build populating cotracts-cache with nym-nodes data more missing nymnodes queries temp mixnet contract methods + restored result submission in nym-api allow deprecated routes submitting correct results for mixnode results removed deprecated re-export of AxumAppState and removed smurf naming moved axum modules into support::http cleaning up nym-api warnings determine entry gateways before exits exposed transaction to update nym-node config missing memo for updating node config new routes added routes to swagger and fixed relative paths fixed some macro derivations added nym-node commands to nym-cli
198 lines
6.7 KiB
Rust
198 lines
6.7 KiB
Rust
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
use crate::mix_node::delegations::{
|
|
get_single_mixnode_delegations, get_single_mixnode_delegations_summed,
|
|
};
|
|
use crate::mix_node::econ_stats::retrieve_mixnode_econ_stats;
|
|
use crate::mix_node::models::{
|
|
EconomicDynamicsStats, NodeDescription, NodeStats, SummedDelegations,
|
|
};
|
|
use crate::state::ExplorerApiStateContext;
|
|
use nym_explorer_api_requests::PrettyDetailedMixNodeBond;
|
|
use nym_mixnet_contract_common::{Delegation, NodeId};
|
|
use reqwest::Error as ReqwestError;
|
|
use rocket::response::status::NotFound;
|
|
use rocket::serde::json::Json;
|
|
use rocket::{Route, State};
|
|
use rocket_okapi::okapi::openapi3::OpenApi;
|
|
use rocket_okapi::openapi_get_routes_spec;
|
|
use rocket_okapi::settings::OpenApiSettings;
|
|
|
|
pub fn mix_node_make_default_routes(settings: &OpenApiSettings) -> (Vec<Route>, OpenApi) {
|
|
openapi_get_routes_spec![
|
|
settings: get_delegations,
|
|
get_delegations_summed,
|
|
get_by_id,
|
|
get_description,
|
|
get_stats,
|
|
get_economic_dynamics_stats,
|
|
]
|
|
}
|
|
|
|
async fn get_mix_node_description(host: &str, port: u16) -> Result<NodeDescription, ReqwestError> {
|
|
reqwest::get(format!("http://{host}:{port}/description"))
|
|
.await?
|
|
.json::<NodeDescription>()
|
|
.await
|
|
}
|
|
|
|
async fn get_mix_node_stats(host: &str, port: u16) -> Result<NodeStats, ReqwestError> {
|
|
reqwest::get(format!("http://{host}:{port}/stats"))
|
|
.await?
|
|
.json::<NodeStats>()
|
|
.await
|
|
}
|
|
|
|
#[openapi(tag = "mix_nodes")]
|
|
#[get("/<mix_id>")]
|
|
pub(crate) async fn get_by_id(
|
|
mix_id: NodeId,
|
|
state: &State<ExplorerApiStateContext>,
|
|
) -> Result<Json<PrettyDetailedMixNodeBond>, NotFound<String>> {
|
|
match state.inner.mixnodes.get_detailed_mixnode(mix_id).await {
|
|
Some(mixnode) => Ok(Json(mixnode)),
|
|
None => Err(NotFound("Mixnode not found".to_string())),
|
|
}
|
|
}
|
|
|
|
#[openapi(tag = "mix_node")]
|
|
#[get("/<mix_id>/delegations")]
|
|
pub(crate) async fn get_delegations(
|
|
mix_id: NodeId,
|
|
state: &State<ExplorerApiStateContext>,
|
|
) -> Json<Vec<Delegation>> {
|
|
Json(get_single_mixnode_delegations(&state.inner.validator_client, mix_id).await)
|
|
}
|
|
|
|
#[openapi(tag = "mix_node")]
|
|
#[get("/<mix_id>/delegations/summed")]
|
|
pub(crate) async fn get_delegations_summed(
|
|
mix_id: NodeId,
|
|
state: &State<ExplorerApiStateContext>,
|
|
) -> Json<Vec<SummedDelegations>> {
|
|
Json(get_single_mixnode_delegations_summed(&state.inner.validator_client, mix_id).await)
|
|
}
|
|
|
|
#[openapi(tag = "mix_node")]
|
|
#[get("/<mix_id>/description")]
|
|
pub(crate) async fn get_description(
|
|
mix_id: NodeId,
|
|
state: &State<ExplorerApiStateContext>,
|
|
) -> Option<Json<NodeDescription>> {
|
|
match state.inner.mixnode.clone().get_description(mix_id).await {
|
|
Some(cache_value) => {
|
|
trace!("Returning cached value for {}", mix_id);
|
|
Some(Json(cache_value))
|
|
}
|
|
None => {
|
|
trace!("No valid cache value for {}", mix_id);
|
|
match state.inner.get_mix_node(mix_id).await {
|
|
Some(bond) => {
|
|
match get_mix_node_description(
|
|
&bond.mix_node().host,
|
|
bond.mix_node().http_api_port,
|
|
)
|
|
.await
|
|
{
|
|
Ok(response) => {
|
|
// cache the response and return as the HTTP response
|
|
state
|
|
.inner
|
|
.mixnode
|
|
.set_description(mix_id, response.clone())
|
|
.await;
|
|
Some(Json(response))
|
|
}
|
|
Err(e) => {
|
|
error!(
|
|
"Unable to get description for {} on {}:{} -> {}",
|
|
mix_id,
|
|
bond.mix_node().host,
|
|
bond.mix_node().http_api_port,
|
|
e
|
|
);
|
|
Option::None
|
|
}
|
|
}
|
|
}
|
|
None => Option::None,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[openapi(tag = "mix_node")]
|
|
#[get("/<mix_id>/stats")]
|
|
pub(crate) async fn get_stats(
|
|
mix_id: NodeId,
|
|
state: &State<ExplorerApiStateContext>,
|
|
) -> Option<Json<NodeStats>> {
|
|
match state.inner.mixnode.get_node_stats(mix_id).await {
|
|
Some(cache_value) => {
|
|
trace!("Returning cached value for {}", mix_id);
|
|
Some(Json(cache_value))
|
|
}
|
|
None => {
|
|
trace!("No valid cache value for {}", mix_id);
|
|
match state.inner.get_mix_node(mix_id).await {
|
|
Some(bond) => {
|
|
match get_mix_node_stats(&bond.mix_node().host, bond.mix_node().http_api_port)
|
|
.await
|
|
{
|
|
Ok(response) => {
|
|
// cache the response and return as the HTTP response
|
|
state
|
|
.inner
|
|
.mixnode
|
|
.set_node_stats(mix_id, response.clone())
|
|
.await;
|
|
Some(Json(response))
|
|
}
|
|
Err(e) => {
|
|
error!(
|
|
"Unable to get description for {} on {}:{} -> {}",
|
|
mix_id,
|
|
bond.mix_node().host,
|
|
bond.mix_node().http_api_port,
|
|
e
|
|
);
|
|
Option::None
|
|
}
|
|
}
|
|
}
|
|
None => Option::None,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[openapi(tag = "mix_node")]
|
|
#[get("/<mix_id>/economic-dynamics-stats")]
|
|
pub(crate) async fn get_economic_dynamics_stats(
|
|
mix_id: NodeId,
|
|
state: &State<ExplorerApiStateContext>,
|
|
) -> Option<Json<EconomicDynamicsStats>> {
|
|
match state.inner.mixnode.get_econ_stats(mix_id).await {
|
|
Some(cache_value) => {
|
|
trace!("Returning cached value for {}", mix_id);
|
|
Some(Json(cache_value))
|
|
}
|
|
None => {
|
|
trace!("No valid cache value for {}", mix_id);
|
|
|
|
// get fresh value from the validator API
|
|
let econ_stats =
|
|
retrieve_mixnode_econ_stats(&state.inner.validator_client, mix_id).await?;
|
|
|
|
// update cache
|
|
state
|
|
.inner
|
|
.mixnode
|
|
.set_econ_stats(mix_id, econ_stats.clone())
|
|
.await;
|
|
Some(Json(econ_stats))
|
|
}
|
|
}
|
|
}
|