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
232 lines
7.6 KiB
Rust
232 lines
7.6 KiB
Rust
// Copyright 2022-2024 - Nym Technologies SA <contact@nymtech.net>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//! Collection of initialization steps used by client implementations
|
|
|
|
use crate::client::base_client::storage::helpers::{
|
|
has_gateway_details, load_active_gateway_details, load_client_keys, load_gateway_details,
|
|
store_gateway_details,
|
|
};
|
|
use crate::client::key_manager::persistence::KeyStore;
|
|
use crate::client::key_manager::ClientKeys;
|
|
use crate::error::ClientCoreError;
|
|
use crate::init::helpers::{
|
|
choose_gateway_by_latency, get_specified_gateway, uniformly_random_gateway,
|
|
};
|
|
use crate::init::types::{
|
|
GatewaySelectionSpecification, GatewaySetup, InitialisationResult, SelectedGateway,
|
|
};
|
|
use nym_client_core_gateways_storage::GatewaysDetailsStore;
|
|
use nym_client_core_gateways_storage::{GatewayDetails, GatewayRegistration};
|
|
use nym_gateway_client::client::InitGatewayClient;
|
|
use nym_topology::gateway;
|
|
use rand::rngs::OsRng;
|
|
use rand::{CryptoRng, RngCore};
|
|
use serde::Serialize;
|
|
|
|
pub mod helpers;
|
|
pub mod types;
|
|
|
|
// helpers for error wrapping
|
|
|
|
pub async fn generate_new_client_keys<K, R>(
|
|
rng: &mut R,
|
|
key_store: &K,
|
|
) -> Result<(), ClientCoreError>
|
|
where
|
|
R: RngCore + CryptoRng,
|
|
K: KeyStore,
|
|
K::StorageError: Send + Sync + 'static,
|
|
{
|
|
ClientKeys::generate_new(rng)
|
|
.persist_keys(key_store)
|
|
.await
|
|
.map_err(|source| ClientCoreError::KeyStoreError {
|
|
source: Box::new(source),
|
|
})
|
|
}
|
|
|
|
async fn setup_new_gateway<K, D>(
|
|
key_store: &K,
|
|
details_store: &D,
|
|
selection_specification: GatewaySelectionSpecification,
|
|
available_gateways: Vec<gateway::LegacyNode>,
|
|
) -> Result<InitialisationResult, ClientCoreError>
|
|
where
|
|
K: KeyStore,
|
|
D: GatewaysDetailsStore,
|
|
K::StorageError: Send + Sync + 'static,
|
|
D::StorageError: Send + Sync + 'static,
|
|
{
|
|
log::trace!("Setting up new gateway");
|
|
|
|
// if we're setting up new gateway, we must have had generated long-term client keys before
|
|
let client_keys = load_client_keys(key_store).await?;
|
|
|
|
let mut rng = OsRng;
|
|
|
|
let selected_gateway = match selection_specification {
|
|
GatewaySelectionSpecification::UniformRemote { must_use_tls } => {
|
|
let gateway = uniformly_random_gateway(&mut rng, &available_gateways, must_use_tls)?;
|
|
SelectedGateway::from_topology_node(gateway, must_use_tls)?
|
|
}
|
|
GatewaySelectionSpecification::RemoteByLatency { must_use_tls } => {
|
|
let gateway =
|
|
choose_gateway_by_latency(&mut rng, &available_gateways, must_use_tls).await?;
|
|
SelectedGateway::from_topology_node(gateway, must_use_tls)?
|
|
}
|
|
GatewaySelectionSpecification::Specified {
|
|
must_use_tls,
|
|
identity,
|
|
} => {
|
|
let gateway = get_specified_gateway(&identity, &available_gateways, must_use_tls)?;
|
|
SelectedGateway::from_topology_node(gateway, must_use_tls)?
|
|
}
|
|
GatewaySelectionSpecification::Custom {
|
|
gateway_identity,
|
|
additional_data,
|
|
} => SelectedGateway::custom(gateway_identity, additional_data)?,
|
|
};
|
|
|
|
// check if we already have details associated with this particular gateway
|
|
// and if so, see if we can overwrite it
|
|
let selected_id = selected_gateway.gateway_id().to_base58_string();
|
|
if has_gateway_details(details_store, &selected_id).await? {
|
|
return Err(ClientCoreError::AlreadyRegistered {
|
|
gateway_id: selected_id,
|
|
});
|
|
}
|
|
|
|
let (gateway_details, authenticated_ephemeral_client) = match selected_gateway {
|
|
SelectedGateway::Remote {
|
|
gateway_id,
|
|
gateway_owner_address,
|
|
gateway_listener,
|
|
} => {
|
|
// if we're using a 'normal' gateway setup, do register
|
|
let our_identity = client_keys.identity_keypair();
|
|
|
|
let registration =
|
|
helpers::register_with_gateway(gateway_id, gateway_listener.clone(), our_identity)
|
|
.await?;
|
|
(
|
|
GatewayDetails::new_remote(
|
|
gateway_id,
|
|
registration.shared_keys,
|
|
gateway_owner_address,
|
|
gateway_listener,
|
|
),
|
|
Some(registration.authenticated_ephemeral_client),
|
|
)
|
|
}
|
|
SelectedGateway::Custom {
|
|
gateway_id,
|
|
additional_data,
|
|
} => (
|
|
GatewayDetails::new_custom(gateway_id, additional_data),
|
|
None,
|
|
),
|
|
};
|
|
|
|
let gateway_registration = gateway_details.into();
|
|
|
|
// persist gateway details
|
|
store_gateway_details(details_store, &gateway_registration).await?;
|
|
|
|
Ok(InitialisationResult {
|
|
gateway_registration,
|
|
client_keys,
|
|
authenticated_ephemeral_client,
|
|
})
|
|
}
|
|
|
|
async fn use_loaded_gateway_details<K, D>(
|
|
key_store: &K,
|
|
details_store: &D,
|
|
gateway_id: Option<String>,
|
|
) -> Result<InitialisationResult, ClientCoreError>
|
|
where
|
|
K: KeyStore,
|
|
D: GatewaysDetailsStore,
|
|
K::StorageError: Send + Sync + 'static,
|
|
D::StorageError: Send + Sync + 'static,
|
|
{
|
|
let loaded_details = if let Some(gateway_id) = gateway_id {
|
|
load_gateway_details(details_store, &gateway_id).await?
|
|
} else {
|
|
load_active_gateway_details(details_store)
|
|
.await?
|
|
.registration
|
|
.ok_or(ClientCoreError::NoActiveGatewaySet)?
|
|
};
|
|
|
|
let loaded_keys = load_client_keys(key_store).await?;
|
|
|
|
// no need to persist anything as we got everything from the storage
|
|
Ok(InitialisationResult::new_loaded(
|
|
loaded_details,
|
|
loaded_keys,
|
|
))
|
|
}
|
|
|
|
fn reuse_gateway_connection(
|
|
authenticated_ephemeral_client: InitGatewayClient,
|
|
gateway_registration: GatewayRegistration,
|
|
client_keys: ClientKeys,
|
|
) -> InitialisationResult {
|
|
InitialisationResult {
|
|
gateway_registration,
|
|
client_keys,
|
|
authenticated_ephemeral_client: Some(authenticated_ephemeral_client),
|
|
}
|
|
}
|
|
|
|
pub async fn setup_gateway<K, D>(
|
|
setup: GatewaySetup,
|
|
key_store: &K,
|
|
details_store: &D,
|
|
) -> Result<InitialisationResult, ClientCoreError>
|
|
where
|
|
K: KeyStore,
|
|
D: GatewaysDetailsStore,
|
|
K::StorageError: Send + Sync + 'static,
|
|
D::StorageError: Send + Sync + 'static,
|
|
{
|
|
log::debug!("Setting up gateway");
|
|
match setup {
|
|
GatewaySetup::MustLoad { gateway_id } => {
|
|
log::debug!("GatewaySetup::MustLoad with id: {gateway_id:?}");
|
|
use_loaded_gateway_details(key_store, details_store, gateway_id).await
|
|
}
|
|
GatewaySetup::New {
|
|
specification,
|
|
available_gateways,
|
|
} => {
|
|
log::debug!("GatewaySetup::New with spec: {specification:?}");
|
|
setup_new_gateway(key_store, details_store, specification, available_gateways).await
|
|
}
|
|
GatewaySetup::ReuseConnection {
|
|
authenticated_ephemeral_client,
|
|
gateway_details,
|
|
client_keys: managed_keys,
|
|
} => {
|
|
log::debug!("GatewaySetup::ReuseConnection");
|
|
Ok(reuse_gateway_connection(
|
|
authenticated_ephemeral_client,
|
|
*gateway_details,
|
|
managed_keys,
|
|
))
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn output_to_json<T: Serialize>(init_results: &T, output_file: &str) {
|
|
match std::fs::File::create(output_file) {
|
|
Ok(file) => match serde_json::to_writer_pretty(file, init_results) {
|
|
Ok(_) => println!("Saved: {output_file}"),
|
|
Err(err) => eprintln!("Could not save {output_file}: {err}"),
|
|
},
|
|
Err(err) => eprintln!("Could not save {output_file}: {err}"),
|
|
}
|
|
}
|