90e9e3cff8
* feat: unify HTTP client creation and enable domain fronting Enhanced the base nym_http_api_client to reduce fragmentation and enable domain fronting: - Added SerializationFormat enum for explicit JSON/bincode choice (no auto-detection) - Added from_network() method to create clients from NymNetworkDetails with domain fronting - Added with_bincode() builder method for explicit serialization configuration - Set Accept header based on serialization preference - Added deprecation paths for NymApiClient wrapper and nym_api::Client re-export - Enabled domain fronting support via network defaults feature This is part of a broader effort to consolidate HTTP client implementations across the codebase, reducing ~500 lines of wrapper code and providing automatic domain fronting for censorship resistance. * feat: migrate NymApiClient usage to unified HTTP client - Wire up domain fronting configuration in NymNetworkDetails - Implement NymApiClientExt trait for base nym_http_api_client::Client - Migrate direct NymApiClient usage in multiple components: - nym-network-monitor - verloc measurements - connection tester - coconut/ecash client - validator rewarder - Add Copy derive to ApiUrlConst to enable iteration - Update error handling and Display implementations This enables automatic domain fronting for all Nym API calls via the configured CDN front hosts. * fix: resolve all compilation errors after NymApiClient migration - Add missing nym-http-api-client dependencies to multiple crates - Add NymApiClientExt trait imports where needed - Fix type mismatches from NymApiClient to unified Client - Add error conversions for NymAPIError in various error enums - Implement missing trait methods (get_current_rewarded_set, get_all_basic_nodes_with_metadata, get_all_described_nodes) - Fix type conversions for RewardedSetResponse in network monitor - Update all API client instantiation to use new unified HTTP client * feat: complete migration to unified HTTP client and fix all compilation errors - Added missing NymApiClientExt trait methods (get_all_expanded_nodes, change_base_urls) - Fixed all compilation errors across the workspace - Updated nym-node to use unified client instead of deprecated NymApiClient - Fixed type conversions for RewardedSetResponse → EpochRewardedSet - Added nym-http-api-client dependency where needed - Updated all examples and documentation to use new client API * fix: provide all API URLs for automatic failover in endpoint rotation Previously, when rotating API endpoints, only a single URL was provided to the HTTP client, defeating the purpose of having multiple URLs for resilience. Changes: - NymApiTopologyProvider now provides all URLs in rotated order when switching endpoints - NymApisClient similarly provides all URLs starting from the working endpoint - Added clarifying comments for broadcast/exhaustive query methods where single URLs are intentionally used - This enables the HTTP client's built-in failover mechanism while maintaining endpoint rotation behavior The fix ensures that if the primary endpoint fails, the client can automatically failover to alternative endpoints without manual intervention, improving overall network resilience. * Update common/client-core/src/client/base_client/mod.rs Co-authored-by: Jędrzej Stuczyński <jedrzej.stuczynski@gmail.com> * Remove error generics, address PR comments * Explicit warning on missing fronting configuration * Assorted CI fixes * Registry proc-macro * Rename macro * Syn workspace version * Where do we need to put inventory * Ergonomics and call sites, incept the builder * fix: Address critical issues in client configuration registry implementation - Fixed HeaderMapInit parsing bug that would cause compilation errors - Added comprehensive documentation with usage examples and DSL reference - Improved error handling with better error messages for invalid headers - Added test coverage for both macro and registry functionality - Added debug inspection capabilities for registered configurations - Fixed module name conflicts in tests by using separate modules All tests now passing: - 7 macro tests validating DSL parsing and code generation - 4 registry tests verifying configuration collection and application * Use default value for the ports until api is deployed * Feature/improved http error (#6025) * use display impl for urls * feat: attempt to add more details to reqwest errors * temporarily restored GenericRequestFailure variant * another restoration * cleanup * Some debug tooling, and default timeout fix * Fix user-agent override * Fix various wasm things --------- Co-authored-by: Jędrzej Stuczyński <jedrzej.stuczynski@gmail.com> Co-authored-by: Bogdan-Ștefan Neacşu <bogdan@nymtech.net>
259 lines
8.5 KiB
Rust
259 lines
8.5 KiB
Rust
use crate::nym_api::NymApiClientExt;
|
|
use crate::nyxd::contract_traits::MixnetQueryClient;
|
|
use crate::nyxd::error::NyxdError;
|
|
use crate::nyxd::Config as ClientConfig;
|
|
use crate::{QueryHttpRpcNyxdClient, ValidatorClientError};
|
|
use colored::Colorize;
|
|
use core::fmt;
|
|
use itertools::Itertools;
|
|
use nym_network_defaults::NymNetworkDetails;
|
|
use std::collections::HashMap;
|
|
use std::hash::BuildHasher;
|
|
use std::time::Duration;
|
|
use tokio::time::timeout;
|
|
use url::Url;
|
|
|
|
const MAX_URLS_TESTED: usize = 200;
|
|
const CONNECTION_TEST_TIMEOUT_SEC: u64 = 2;
|
|
|
|
/// Run connection tests for all specified nyxd and api urls. These are all run concurrently.
|
|
pub async fn run_validator_connection_test<H: BuildHasher + 'static>(
|
|
nyxd_urls: impl Iterator<Item = (NymNetworkDetails, Url)>,
|
|
api_urls: impl Iterator<Item = (NymNetworkDetails, Url)>,
|
|
mixnet_contract_address: HashMap<NymNetworkDetails, cosmrs::AccountId, H>,
|
|
) -> (
|
|
HashMap<NymNetworkDetails, Vec<(Url, bool)>>,
|
|
HashMap<NymNetworkDetails, Vec<(Url, bool)>>,
|
|
) {
|
|
// Setup all the clients for the connection tests
|
|
let connection_test_clients =
|
|
setup_connection_tests(nyxd_urls, api_urls, mixnet_contract_address);
|
|
|
|
// Run all tests concurrently
|
|
let connection_results = futures::future::join_all(
|
|
connection_test_clients
|
|
.into_iter()
|
|
.take(MAX_URLS_TESTED)
|
|
.map(ClientForConnectionTest::run_connection_check),
|
|
)
|
|
.await;
|
|
|
|
// Seperate and collect results into HashMaps
|
|
(
|
|
extract_and_collect_results_into_map(&connection_results, &UrlType::Nyxd),
|
|
extract_and_collect_results_into_map(&connection_results, &UrlType::NymApi),
|
|
)
|
|
}
|
|
|
|
pub async fn test_nyxd_url_connection(
|
|
network: NymNetworkDetails,
|
|
nyxd_url: Url,
|
|
address: cosmrs::AccountId,
|
|
) -> Result<bool, ValidatorClientError> {
|
|
let config = ClientConfig::try_from_nym_network_details(&network)
|
|
.expect("failed to create valid nyxd client config");
|
|
|
|
let mut nyxd_client = QueryHttpRpcNyxdClient::connect(config, nyxd_url.as_str())?;
|
|
// possibly redundant, but lets just leave it here
|
|
nyxd_client.set_mixnet_contract_address(address);
|
|
match test_nyxd_connection(network, &nyxd_url, &nyxd_client).await {
|
|
ConnectionResult::Nyxd(_, _, res) => Ok(res),
|
|
_ => Ok(false), // ✶ not possible to happens
|
|
}
|
|
}
|
|
|
|
fn setup_connection_tests<H: BuildHasher + 'static>(
|
|
nyxd_urls: impl Iterator<Item = (NymNetworkDetails, Url)>,
|
|
api_urls: impl Iterator<Item = (NymNetworkDetails, Url)>,
|
|
mixnet_contract_address: HashMap<NymNetworkDetails, cosmrs::AccountId, H>,
|
|
) -> impl Iterator<Item = ClientForConnectionTest> {
|
|
let nyxd_connection_test_clients = nyxd_urls.filter_map(move |(network, url)| {
|
|
let address = mixnet_contract_address
|
|
.get(&network)
|
|
.expect("No configured contract address")
|
|
.clone();
|
|
let config = ClientConfig::try_from_nym_network_details(&network)
|
|
.expect("failed to create valid nyxd client config");
|
|
|
|
if let Ok(mut client) = QueryHttpRpcNyxdClient::connect(config, url.as_str()) {
|
|
// possibly redundant, but lets just leave it here
|
|
client.set_mixnet_contract_address(address);
|
|
Some(ClientForConnectionTest::Nyxd(
|
|
network,
|
|
url,
|
|
Box::new(client),
|
|
))
|
|
} else {
|
|
None
|
|
}
|
|
});
|
|
|
|
let api_connection_test_clients = api_urls.filter_map(|(network, url)| {
|
|
match nym_http_api_client::Client::builder(url.clone()).and_then(|b| b.build()) {
|
|
Ok(client) => Some(ClientForConnectionTest::Api(network, url, client)),
|
|
Err(err) => {
|
|
eprintln!(
|
|
"Failed to create API client for {}: {err}",
|
|
network.network_name
|
|
);
|
|
None
|
|
}
|
|
}
|
|
});
|
|
|
|
nyxd_connection_test_clients.chain(api_connection_test_clients)
|
|
}
|
|
|
|
fn extract_and_collect_results_into_map(
|
|
connection_results: &[ConnectionResult],
|
|
url_type: &UrlType,
|
|
) -> HashMap<NymNetworkDetails, Vec<(Url, bool)>> {
|
|
connection_results
|
|
.iter()
|
|
.filter(|c| &c.url_type() == url_type)
|
|
.map(|c| {
|
|
let (network, url, result) = c.result();
|
|
(network.clone(), (url.clone(), *result))
|
|
})
|
|
.into_group_map()
|
|
}
|
|
|
|
async fn test_nyxd_connection(
|
|
network: NymNetworkDetails,
|
|
url: &Url,
|
|
client: &QueryHttpRpcNyxdClient,
|
|
) -> ConnectionResult {
|
|
let result = match timeout(
|
|
Duration::from_secs(CONNECTION_TEST_TIMEOUT_SEC),
|
|
client.get_mixnet_contract_version(),
|
|
)
|
|
.await
|
|
{
|
|
Ok(Err(NyxdError::TendermintErrorRpc(e))) => {
|
|
// If we get a tendermint-rpc error, we classify the node as not contactable
|
|
tracing::warn!("Checking: nyxd url: {url}: {}: {}", "failed".red(), e);
|
|
false
|
|
}
|
|
Ok(Err(NyxdError::AbciError { code, log, .. })) => {
|
|
// We accept the mixnet contract not found as ok from a connection standpoint. This happens
|
|
// for example on a pre-launch network.
|
|
tracing::debug!(
|
|
"Checking: nyxd url: {url}: {}, but with abci error: {code}: {log}",
|
|
"success".green()
|
|
);
|
|
code == 18
|
|
}
|
|
Ok(Err(error @ NyxdError::NoContractAddressAvailable(_))) => {
|
|
tracing::warn!("Checking: nyxd url: {url}: {}: {error}", "failed".red());
|
|
false
|
|
}
|
|
Ok(Err(e)) => {
|
|
// For any other error, we're optimistic and just try anyway.
|
|
tracing::warn!(
|
|
"Checking: nyxd_url: {url}: {}, but with error: {e}",
|
|
"success".green()
|
|
);
|
|
true
|
|
}
|
|
Ok(Ok(_)) => {
|
|
tracing::debug!("Checking: nyxd_url: {url}: {}", "success".green());
|
|
true
|
|
}
|
|
Err(e) => {
|
|
tracing::warn!("Checking: nyxd_url: {url}: {}: {e}", "failed".red());
|
|
false
|
|
}
|
|
};
|
|
ConnectionResult::Nyxd(network, url.clone(), result)
|
|
}
|
|
|
|
async fn test_nym_api_connection(
|
|
network: NymNetworkDetails,
|
|
url: &Url,
|
|
client: &nym_http_api_client::Client,
|
|
) -> ConnectionResult {
|
|
let result = match timeout(
|
|
Duration::from_secs(CONNECTION_TEST_TIMEOUT_SEC),
|
|
client.health(),
|
|
)
|
|
.await
|
|
{
|
|
Ok(Ok(_)) => {
|
|
tracing::debug!("Checking: api_url: {url}: {}", "success".green());
|
|
true
|
|
}
|
|
Ok(Err(e)) => {
|
|
tracing::debug!("Checking: api_url: {url}: {}: {e}", "failed".red());
|
|
false
|
|
}
|
|
Err(e) => {
|
|
tracing::debug!("Checking: api_url: {url}: {}: {e}", "failed".red());
|
|
false
|
|
}
|
|
};
|
|
ConnectionResult::Api(network, url.clone(), result)
|
|
}
|
|
|
|
enum ClientForConnectionTest {
|
|
Nyxd(NymNetworkDetails, Url, Box<QueryHttpRpcNyxdClient>),
|
|
Api(NymNetworkDetails, Url, nym_http_api_client::Client),
|
|
}
|
|
|
|
impl ClientForConnectionTest {
|
|
async fn run_connection_check(self) -> ConnectionResult {
|
|
match self {
|
|
ClientForConnectionTest::Nyxd(network, ref url, ref client) => {
|
|
test_nyxd_connection(network, url, client).await
|
|
}
|
|
ClientForConnectionTest::Api(network, ref url, ref client) => {
|
|
test_nym_api_connection(network, url, client).await
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
enum UrlType {
|
|
Nyxd,
|
|
NymApi,
|
|
}
|
|
|
|
impl fmt::Display for UrlType {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
UrlType::Nyxd => write!(f, "nyxd"),
|
|
UrlType::NymApi => write!(f, "api"),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
enum ConnectionResult {
|
|
Nyxd(NymNetworkDetails, Url, bool),
|
|
Api(NymNetworkDetails, Url, bool),
|
|
}
|
|
|
|
impl ConnectionResult {
|
|
fn result(&self) -> (&NymNetworkDetails, &Url, &bool) {
|
|
match self {
|
|
ConnectionResult::Nyxd(network, url, result)
|
|
| ConnectionResult::Api(network, url, result) => (network, url, result),
|
|
}
|
|
}
|
|
|
|
fn url_type(&self) -> UrlType {
|
|
match self {
|
|
ConnectionResult::Nyxd(..) => UrlType::Nyxd,
|
|
ConnectionResult::Api(..) => UrlType::NymApi,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for ConnectionResult {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
let (_network, url, result) = self.result();
|
|
let url_type = self.url_type();
|
|
write!(f, "{url}: {url_type}: connection is successful: {result}")
|
|
}
|
|
}
|