Skip to content

Commit

Permalink
Add StatusResponse::is_network_healthy (#2197)
Browse files Browse the repository at this point in the history
* Add StatusResponse::is_network_healthy

* get_network_health
  • Loading branch information
thibault-martinez authored Mar 22, 2024
1 parent 3091393 commit 4546db7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions bindings/nodejs/lib/types/models/api/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface StatusResponse {
* Tells whether the node is healthy or not.
*/
isHealthy: boolean;
/**
* Tells whether the network is healthy (finalization is not delayed).
*/
isNetworkHealthy: boolean;
/**
* A notion of time that is anchored to the latest accepted block.
*/
Expand Down
2 changes: 2 additions & 0 deletions bindings/python/iota_sdk/types/node_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class StatusResponse:
Attributes:
is_healthy: Tells whether the node is healthy or not.
is_network_healthy: Tells whether the network is healthy (finalization is not delayed).
accepted_tangle_time: A notion of time that is anchored to the latest accepted block.
relative_accepted_tangle_time: The time after Accepted Tangle Time has advanced with the system clock.
confirmed_tangle_time: A notion of time that is anchored to the latest confirmed block.
Expand All @@ -27,6 +28,7 @@ class StatusResponse:
pruning_epoch: The index of the epoch before which the tangle history is pruned.
"""
is_healthy: bool
is_network_healthy: bool
accepted_tangle_time: int = field(metadata=config(
encoder=str
))
Expand Down
3 changes: 2 additions & 1 deletion sdk/examples/how_tos/client/get_health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// Get node health.
let is_healthy = client.get_health(&node_url).await?;
let is_network_healthy = client.get_network_health().await?;

println!("Healthy: {is_healthy}");
println!("Healthy: node {is_healthy}, network {is_network_healthy}");

Ok(())
}
19 changes: 19 additions & 0 deletions sdk/src/client/node_api/core/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ impl ClientInner {

self.get_request(PATH, None, false).await
}

/// Returns whether the network is healthy (finalization is not delayed).
/// GET /api/core/v3/network/health
pub async fn get_network_health(&self) -> Result<bool, ClientError> {
const PATH: &str = "api/core/v3/network/health";

let nodes = self.node_manager.read().await.get_nodes(PATH, None)?;
let client = crate::client::node_manager::http_client::HttpClient::new(DEFAULT_USER_AGENT.to_string());

for node in &nodes {
if let Ok(res) = client.get(node, DEFAULT_API_TIMEOUT).await {
if res.status() == 200 {
return Ok(true);
}
}
}

Ok(false)
}
}

impl Client {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/client/node_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl NodeManager {
NodeManagerBuilder::new()
}

fn get_nodes(&self, path: &str, query: Option<&str>) -> Result<Vec<Node>, ClientError> {
pub(crate) fn get_nodes(&self, path: &str, query: Option<&str>) -> Result<Vec<Node>, ClientError> {
let mut nodes_with_modified_url: Vec<Node> = Vec::new();

// Set primary nodes first, so they will also be used first for requests.
Expand Down
1 change: 1 addition & 0 deletions sdk/src/types/api/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl PermanodeInfoResponse {
#[serde(rename_all = "camelCase")]
pub struct StatusResponse {
pub is_healthy: bool,
pub is_network_healthy: bool,
#[serde(with = "option_string")]
pub accepted_tangle_time: Option<u64>,
#[serde(with = "option_string")]
Expand Down

0 comments on commit 4546db7

Please sign in to comment.