From 1da950dfd49a4ad3b204f02541c5ddeed7f3f45f Mon Sep 17 00:00:00 2001 From: francis2tm Date: Wed, 11 Dec 2024 23:09:36 +0000 Subject: [PATCH 1/7] chore: change endpoint names --- atoma-daemon/docs/openapi.yml | 61 +++++++++++++------ .../src/handlers/almost_filled_stacks.rs | 9 +++ .../src/handlers/attestation_disputes.rs | 10 +++ atoma-daemon/src/handlers/nodes.rs | 16 +++++ atoma-daemon/src/handlers/stacks.rs | 10 +++ atoma-daemon/src/handlers/subscriptions.rs | 5 ++ atoma-daemon/src/handlers/tasks.rs | 2 + .../src/handlers/chat_completions.rs | 3 +- atoma-service/src/handlers/embeddings.rs | 3 +- .../src/handlers/image_generations.rs | 3 +- atoma-service/src/server.rs | 4 +- 11 files changed, 101 insertions(+), 25 deletions(-) diff --git a/atoma-daemon/docs/openapi.yml b/atoma-daemon/docs/openapi.yml index 6cafd04d..fef6df95 100644 --- a/atoma-daemon/docs/openapi.yml +++ b/atoma-daemon/docs/openapi.yml @@ -13,7 +13,8 @@ paths: get: tags: - Almost filled stacks - summary: Retrieves all stacks that are filled above a specified fraction threshold for all registered nodes. + summary: List almost filled stacks + description: Retrieves all stacks that are filled above a specified fraction threshold for all registered nodes. operationId: almost_filled_stacks_fraction_get parameters: - name: fraction @@ -38,8 +39,10 @@ paths: get: tags: - Almost filled stacks - summary: Retrieves all stacks that are filled above a specified fraction threshold for a specific node. + summary: List almost filled stacks for a specific node description: |- + Retrieves all stacks that are filled above a specified fraction threshold for a specific node. + # Arguments * `daemon_state` - The shared state containing the state manager * `node_small_id` - The small ID of the node whose stacks should be retrieved @@ -83,7 +86,8 @@ paths: get: tags: - Attestation disputes - summary: Retrieves all attestation disputes against the currently registered nodes. + summary: List attestation disputes against currently registered nodes + description: Retrieves all attestation disputes against the currently registered nodes. operationId: attestation_disputes_against_list responses: '200': @@ -100,7 +104,8 @@ paths: get: tags: - Attestation disputes - summary: Retrieves all attestation disputes against a specific node. + summary: List attestation disputes against a specific node + description: Retrieves all attestation disputes against a specific node. operationId: attestation_disputes_against_get parameters: - name: id @@ -125,7 +130,8 @@ paths: get: tags: - Attestation disputes - summary: Retrieves all attestation disputes initiated by the currently registered nodes. + summary: List attestation disputes initiated by currently registered nodes + description: Retrieves all attestation disputes initiated by the currently registered nodes. operationId: attestation_disputes_own_list responses: '200': @@ -142,7 +148,8 @@ paths: get: tags: - Attestation disputes - summary: Retrieves all attestation disputes initiated by a specific node. + summary: List attestation disputes initiated by a specific node + description: Retrieves all attestation disputes initiated by a specific node. operationId: attestation_disputes_own_get parameters: - name: id @@ -167,7 +174,8 @@ paths: post: tags: - Nodes - summary: Claims funds for completed stacks. + summary: Create claim funds transaction + description: Claims funds for completed stacks. operationId: nodes_claim_funds requestBody: content: @@ -188,7 +196,8 @@ paths: post: tags: - Nodes - summary: Subscribes a node to a specific model. + summary: Create model subscription transaction + description: Subscribes a node to a specific model. operationId: nodes_model_subscribe requestBody: content: @@ -209,7 +218,8 @@ paths: post: tags: - Nodes - summary: Registers a new node in the system. + summary: Create node registration transaction + description: Registers a new node in the system. operationId: nodes_register requestBody: content: @@ -230,7 +240,8 @@ paths: post: tags: - Nodes - summary: Submits attestations for stack settlement. + summary: Create attestation proof transaction + description: Submits attestations for stack settlement. operationId: nodes_submit_attestations requestBody: content: @@ -251,7 +262,8 @@ paths: post: tags: - Nodes - summary: Subscribes a node to a specific task. + summary: Create task subscription transaction + description: Subscribes a node to a specific task. operationId: nodes_task_subscribe requestBody: content: @@ -293,7 +305,8 @@ paths: post: tags: - Nodes - summary: Updates an existing task subscription for a node. + summary: Modify task subscription + description: Updates an existing task subscription for a node. operationId: nodes_task_update_subscription requestBody: content: @@ -314,7 +327,8 @@ paths: post: tags: - Nodes - summary: Attempts to settle stacks for a node. + summary: Create try settle stacks transaction + description: Attempts to settle stacks for a node. operationId: nodes_try_settle_stacks requestBody: content: @@ -335,7 +349,8 @@ paths: get: tags: - Stacks - summary: Retrieves all claimed stacks for the currently registered node badges. + summary: List all claimed stacks for currently registered nodes + description: Retrieves all claimed stacks for the currently registered node badges. operationId: claimed_stacks_list responses: '200': @@ -352,7 +367,8 @@ paths: get: tags: - Stacks - summary: Retrieves all claimed stacks for a specific node identified by its small ID. + summary: List all claimed stacks for a specific node + description: Retrieves all claimed stacks for a specific node identified by its small ID. operationId: claimed_stacks_get parameters: - name: id @@ -377,7 +393,8 @@ paths: get: tags: - Stacks - summary: Retrieves all stacks associated with the currently registered node badges. + summary: List all stacks for currently registered nodes + description: Retrieves all stacks associated with the currently registered node badges. operationId: stacks_list responses: '200': @@ -394,7 +411,8 @@ paths: get: tags: - Stacks - summary: Retrieves all stacks for a specific node identified by its small ID. + summary: List all stacks for a specific node + description: Retrieves all stacks for a specific node identified by its small ID. operationId: stacks_get parameters: - name: id @@ -419,7 +437,8 @@ paths: get: tags: - Subscriptions - summary: Retrieves all node subscriptions for the currently registered node badges. + summary: List all subscriptions for currently registered nodes + description: Retrieves all node subscriptions for the currently registered node badges. operationId: subscriptions_list responses: '200': @@ -438,7 +457,8 @@ paths: get: tags: - Subscriptions - summary: Retrieves all subscriptions for a specific node identified by its small ID. + summary: List all subscriptions for a specific node + description: Retrieves all subscriptions for a specific node identified by its small ID. operationId: subscriptions_get parameters: - name: id @@ -463,7 +483,8 @@ paths: get: tags: - Tasks - summary: Retrieves all tasks from the state manager. + summary: List all tasks + description: Retrieves all tasks from the state manager. operationId: tasks_list responses: '200': diff --git a/atoma-daemon/src/handlers/almost_filled_stacks.rs b/atoma-daemon/src/handlers/almost_filled_stacks.rs index 90a42f1e..a13a043f 100644 --- a/atoma-daemon/src/handlers/almost_filled_stacks.rs +++ b/atoma-daemon/src/handlers/almost_filled_stacks.rs @@ -19,6 +19,11 @@ pub const ALMOST_FILLED_STACKS_PATH: &str = "/almost_filled_stacks"; )] pub(crate) struct AlmostFilledStacksOpenApi; +//TODO: we should implement the following endpoints: +// - GET /stacks - List all stacks. A filter `fraction` can be applied +// - GET /stacks/nodes/:id - List all stacks for a specific node. A filter `fraction` can be applied +// Move into stacks.rs + /// Router for handling almost filled stacks endpoints /// /// Creates routes for: @@ -36,6 +41,8 @@ pub fn almost_filled_stacks_router() -> Router { ) } +/// List almost filled stacks +/// /// Retrieves all stacks that are filled above a specified fraction threshold for all registered nodes. #[utoipa::path( get, @@ -71,6 +78,8 @@ pub async fn almost_filled_stacks_fraction_get( )) } +/// List almost filled stacks for a specific node +/// /// Retrieves all stacks that are filled above a specified fraction threshold for a specific node. /// /// # Arguments diff --git a/atoma-daemon/src/handlers/attestation_disputes.rs b/atoma-daemon/src/handlers/attestation_disputes.rs index 0f67f350..a2b09acb 100644 --- a/atoma-daemon/src/handlers/attestation_disputes.rs +++ b/atoma-daemon/src/handlers/attestation_disputes.rs @@ -24,6 +24,8 @@ pub const ATTESTATION_DISPUTES_PATH: &str = "/attestation_disputes"; )] pub(crate) struct AttestationDisputesOpenApi; +//TODO: this endpoint can be merged into one (I think) through filters + /// Router for handling attestation disputes endpoints /// /// Creates routes for: @@ -51,6 +53,8 @@ pub fn attestation_disputes_router() -> Router { ) } +/// List attestation disputes against currently registered nodes +/// /// Retrieves all attestation disputes against the currently registered nodes. #[utoipa::path( get, @@ -81,6 +85,8 @@ pub async fn attestation_disputes_against_list( )) } +/// List attestation disputes against a specific node +/// /// Retrieves all attestation disputes against a specific node. #[utoipa::path( get, @@ -109,6 +115,8 @@ pub async fn attestation_disputes_against_get( )) } +/// List attestation disputes initiated by currently registered nodes +/// /// Retrieves all attestation disputes initiated by the currently registered nodes. #[utoipa::path( get, @@ -139,6 +147,8 @@ pub async fn attestation_disputes_own_list( )) } +/// List attestation disputes initiated by a specific node +/// /// Retrieves all attestation disputes initiated by a specific node. #[utoipa::path( get, diff --git a/atoma-daemon/src/handlers/nodes.rs b/atoma-daemon/src/handlers/nodes.rs index 47bd3d9c..a5820c09 100644 --- a/atoma-daemon/src/handlers/nodes.rs +++ b/atoma-daemon/src/handlers/nodes.rs @@ -90,6 +90,8 @@ pub fn nodes_router() -> Router { ) } +/// Create node registration transaction +/// /// Registers a new node in the system. #[utoipa::path( post, @@ -121,6 +123,8 @@ pub async fn nodes_register( Ok(Json(NodeRegistrationResponse { tx_digest })) } +/// Create model subscription transaction +/// /// Subscribes a node to a specific model. #[utoipa::path( post, @@ -163,6 +167,8 @@ pub async fn nodes_model_subscribe( Ok(Json(NodeModelSubscriptionResponse { tx_digest })) } +/// Create task subscription transaction +/// /// Subscribes a node to a specific task. #[utoipa::path( post, @@ -207,6 +213,8 @@ pub async fn nodes_task_subscribe( Ok(Json(NodeTaskSubscriptionResponse { tx_digest })) } +/// Modify task subscription +/// /// Updates an existing task subscription for a node. #[utoipa::path( post, @@ -251,6 +259,8 @@ pub async fn nodes_task_update_subscription( Ok(Json(NodeTaskUpdateSubscriptionResponse { tx_digest })) } +//TODO: change to delete + /// Unsubscribes a node from a specific task. #[utoipa::path( post, @@ -291,6 +301,8 @@ pub async fn nodes_task_unsubscribe( Ok(Json(NodeTaskUnsubscriptionResponse { tx_digest })) } +/// Create try settle stacks transaction +/// /// Attempts to settle stacks for a node. #[utoipa::path( post, @@ -354,6 +366,8 @@ pub async fn nodes_try_settle_stacks( Ok(Json(NodeTrySettleStacksResponse { tx_digests })) } +/// Create attestation proof transaction +/// /// Submits attestations for stack settlement. #[utoipa::path( post, @@ -465,6 +479,8 @@ pub async fn nodes_submit_attestations( Ok(Json(NodeAttestationProofResponse { tx_digests })) } +/// Create claim funds transaction +/// /// Claims funds for completed stacks. #[utoipa::path( post, diff --git a/atoma-daemon/src/handlers/stacks.rs b/atoma-daemon/src/handlers/stacks.rs index 10a8ca06..4ed93d9c 100644 --- a/atoma-daemon/src/handlers/stacks.rs +++ b/atoma-daemon/src/handlers/stacks.rs @@ -19,6 +19,8 @@ pub const STACKS_PATH: &str = "/stacks"; )] pub(crate) struct StacksOpenApi; +//TODO: all of these endpoints should be deleted and replaced with the ones I suggested in the TODO of almost_filled_stacks.rs. If necessary, we can add additional filters to the endpoints to accomplish the functionality present in these endpoints. + /// Router for handling stack-related endpoints /// /// This function sets up the routing for various stack-related operations, @@ -33,6 +35,8 @@ pub fn stacks_router() -> Router { .route("/claimed_stacks/:id", get(claimed_stacks_get)) } +/// List all stacks for currently registered nodes +/// /// Retrieves all stacks associated with the currently registered node badges. #[utoipa::path( get, @@ -63,6 +67,8 @@ pub async fn stacks_list( )) } +/// List all stacks for a specific node +/// /// Retrieves all stacks for a specific node identified by its small ID. #[utoipa::path( get, @@ -91,6 +97,8 @@ pub async fn stacks_get( )) } +/// List all claimed stacks for currently registered nodes +/// /// Retrieves all claimed stacks for the currently registered node badges. #[utoipa::path( get, @@ -121,6 +129,8 @@ pub async fn claimed_stacks_list( )) } +/// List all claimed stacks for a specific node +/// /// Retrieves all claimed stacks for a specific node identified by its small ID. #[utoipa::path( get, diff --git a/atoma-daemon/src/handlers/subscriptions.rs b/atoma-daemon/src/handlers/subscriptions.rs index 90b153b9..74981ee0 100644 --- a/atoma-daemon/src/handlers/subscriptions.rs +++ b/atoma-daemon/src/handlers/subscriptions.rs @@ -31,6 +31,8 @@ pub fn subscriptions_router() -> Router { .route(&format!("{SUBSCRIPTIONS_PATH}/:id"), get(subscriptions_get)) } +/// List all subscriptions for currently registered nodes +/// /// Retrieves all node subscriptions for the currently registered node badges. #[utoipa::path( get, @@ -64,6 +66,9 @@ pub async fn subscriptions_list( Ok(Json(all_node_subscriptions)) } +//TODO: this endpoint should be deleted and add filter on subscriptions_list. +/// List all subscriptions for a specific node +/// /// Retrieves all subscriptions for a specific node identified by its small ID. #[utoipa::path( get, diff --git a/atoma-daemon/src/handlers/tasks.rs b/atoma-daemon/src/handlers/tasks.rs index bf63f550..6896aa42 100644 --- a/atoma-daemon/src/handlers/tasks.rs +++ b/atoma-daemon/src/handlers/tasks.rs @@ -20,6 +20,8 @@ pub fn tasks_router() -> Router { Router::new().route(TASKS_PATH, get(tasks_list)) } +/// List all tasks +/// /// Retrieves all tasks from the state manager. #[utoipa::path( get, diff --git a/atoma-service/src/handlers/chat_completions.rs b/atoma-service/src/handlers/chat_completions.rs index bc949694..a66d6518 100644 --- a/atoma-service/src/handlers/chat_completions.rs +++ b/atoma-service/src/handlers/chat_completions.rs @@ -87,7 +87,8 @@ const STREAM_KEEP_ALIVE_INTERVAL_IN_SECONDS: u64 = 15; )] pub(crate) struct ChatCompletionsOpenApi; -/// Handles chat completion requests by forwarding them to the inference service and managing token usage. +/// Create chat completion +/// /// /// This handler performs several key operations: /// 1. Forwards the chat completion request to the inference service diff --git a/atoma-service/src/handlers/embeddings.rs b/atoma-service/src/handlers/embeddings.rs index 7243f51c..03f47f3c 100644 --- a/atoma-service/src/handlers/embeddings.rs +++ b/atoma-service/src/handlers/embeddings.rs @@ -28,7 +28,8 @@ pub const EMBEDDINGS_PATH: &str = "/v1/embeddings"; #[openapi(paths(embeddings_handler))] pub(crate) struct EmbeddingsOpenApi; -/// Handles embedding requests by proxying them to the embeddings service. +/// Create embeddings +/// /// /// This handler simply forwards the request to the embeddings service and returns the response. /// diff --git a/atoma-service/src/handlers/image_generations.rs b/atoma-service/src/handlers/image_generations.rs index cc0ac151..11d336ac 100644 --- a/atoma-service/src/handlers/image_generations.rs +++ b/atoma-service/src/handlers/image_generations.rs @@ -26,7 +26,8 @@ pub const IMAGE_GENERATIONS_PATH: &str = "/v1/images/generations"; #[openapi(paths(image_generations_handler))] pub(crate) struct ImageGenerationsOpenApi; -/// Handles image generation requests by proxying them to the image generations service. +/// Create image generation +/// /// /// This handler simply forwards the request to the image generations service and returns the response. /// diff --git a/atoma-service/src/server.rs b/atoma-service/src/server.rs index e4dd6044..02572bef 100644 --- a/atoma-service/src/server.rs +++ b/atoma-service/src/server.rs @@ -290,7 +290,7 @@ pub async fn run_server( #[openapi(paths(health))] pub(crate) struct HealthOpenApi; -/// Handles the health check endpoint. +/// Health /// /// This function is used to verify that the server is running and responsive. /// It's typically used by load balancers or monitoring systems to check the @@ -330,7 +330,7 @@ async fn health() -> impl IntoResponse { #[openapi(paths(metrics_handler))] pub(crate) struct MetricsOpenApi; -/// Handles the metrics endpoint. +/// Metrics /// /// This function is used to return the metrics for the service. /// From c7d2411f25622af0e45b426d4f4f7751c348dc56 Mon Sep 17 00:00:00 2001 From: francis2tm Date: Thu, 12 Dec 2024 12:57:30 +0000 Subject: [PATCH 2/7] wip --- .../src/handlers/almost_filled_stacks.rs | 122 ------------ atoma-daemon/src/handlers/stacks.rs | 176 +++++++----------- 2 files changed, 68 insertions(+), 230 deletions(-) diff --git a/atoma-daemon/src/handlers/almost_filled_stacks.rs b/atoma-daemon/src/handlers/almost_filled_stacks.rs index a13a043f..8b137891 100644 --- a/atoma-daemon/src/handlers/almost_filled_stacks.rs +++ b/atoma-daemon/src/handlers/almost_filled_stacks.rs @@ -1,123 +1 @@ -use atoma_state::types::Stack; -use axum::{ - extract::{Path, State}, - http::StatusCode, - routing::get, - Json, Router, -}; -use tracing::error; -use utoipa::OpenApi; -use crate::DaemonState; - -pub const ALMOST_FILLED_STACKS_PATH: &str = "/almost_filled_stacks"; - -#[derive(OpenApi)] -#[openapi( - paths(almost_filled_stacks_fraction_get, almost_filled_stacks_get), - components(schemas(Stack)) -)] -pub(crate) struct AlmostFilledStacksOpenApi; - -//TODO: we should implement the following endpoints: -// - GET /stacks - List all stacks. A filter `fraction` can be applied -// - GET /stacks/nodes/:id - List all stacks for a specific node. A filter `fraction` can be applied -// Move into stacks.rs - -/// Router for handling almost filled stacks endpoints -/// -/// Creates routes for: -/// - GET /almost_filled_stacks - Get stacks above threshold for all nodes -/// - GET /almost_filled_stacks/:id - Get stacks above threshold for specific node -pub fn almost_filled_stacks_router() -> Router { - Router::new() - .route( - ALMOST_FILLED_STACKS_PATH, - get(almost_filled_stacks_fraction_get), - ) - .route( - &format!("{ALMOST_FILLED_STACKS_PATH}/:id"), - get(almost_filled_stacks_get), - ) -} - -/// List almost filled stacks -/// -/// Retrieves all stacks that are filled above a specified fraction threshold for all registered nodes. -#[utoipa::path( - get, - path = "/{fraction}", - params( - ("fraction" = f64, Path, description = "The fraction threshold (0.0 to 100.0) to filter stacks") - ), - responses( - (status = OK, description = "Stack objects that are filled above the specified fraction threshold", body = Vec), - (status = INTERNAL_SERVER_ERROR, description = "Internal server error") - ) -)] -pub async fn almost_filled_stacks_fraction_get( - State(daemon_state): State, - Path(fraction): Path, -) -> Result>, StatusCode> { - Ok(Json( - daemon_state - .atoma_state - .get_almost_filled_stacks( - &daemon_state - .node_badges - .iter() - .map(|(_, small_id)| *small_id as i64) - .collect::>(), - fraction, - ) - .await - .map_err(|_| { - error!("Failed to get all almost filled stacks"); - StatusCode::INTERNAL_SERVER_ERROR - })?, - )) -} - -/// List almost filled stacks for a specific node -/// -/// Retrieves all stacks that are filled above a specified fraction threshold for a specific node. -/// -/// # Arguments -/// * `daemon_state` - The shared state containing the state manager -/// * `node_small_id` - The small ID of the node whose stacks should be retrieved -/// * `fraction` - The fraction threshold (0.0 to 100.0) to filter stacks -/// -/// # Returns -/// * `Result>>` - A JSON response containing a list of stacks -/// - `Ok(Json>)` - Successfully retrieved stacks -/// - `Err(StatusCode::INTERNAL_SERVER_ERROR)` - Failed to retrieve stacks from state manager -/// -/// # Example Response -/// Returns a JSON array of Stack objects for the specified node that are filled above the fraction threshold -#[utoipa::path( - get, - path = "/{id}/{fraction}", - params( - ("id" = i64, Path, description = "The small ID of the node whose stacks should be retrieved"), - ("fraction" = f64, Path, description = "The fraction threshold (0.0 to 100.0) to filter stacks") - ), - responses( - (status = OK, description = "List of node stacks", body = Vec), - (status = INTERNAL_SERVER_ERROR, description = "Internal server error") - ) -)] -pub async fn almost_filled_stacks_get( - State(daemon_state): State, - Path((node_small_id, fraction)): Path<(i64, f64)>, -) -> Result>, StatusCode> { - Ok(Json( - daemon_state - .atoma_state - .get_almost_filled_stacks(&[node_small_id], fraction) - .await - .map_err(|_| { - error!("Failed to get node almost filled stacks"); - StatusCode::INTERNAL_SERVER_ERROR - })?, - )) -} diff --git a/atoma-daemon/src/handlers/stacks.rs b/atoma-daemon/src/handlers/stacks.rs index 4ed93d9c..791061fd 100644 --- a/atoma-daemon/src/handlers/stacks.rs +++ b/atoma-daemon/src/handlers/stacks.rs @@ -1,160 +1,120 @@ use atoma_state::types::{Stack, StackSettlementTicket}; use axum::{ - extract::{Path, State}, + extract::{Path, Query, State}, http::StatusCode, routing::get, Json, Router, }; +use serde::Deserialize; use tracing::error; -use utoipa::OpenApi; +use utoipa::{OpenApi, ToSchema}; use crate::DaemonState; pub const STACKS_PATH: &str = "/stacks"; +#[derive(Deserialize, ToSchema)] +pub struct StackQuery { + node_id: Option, + min_fill_fraction: Option, +} + +#[derive(Deserialize, ToSchema)] +pub struct ClaimedStackQuery { + node_id: Option, +} + #[derive(OpenApi)] #[openapi( - paths(stacks_list, stacks_get, claimed_stacks_list, claimed_stacks_get), - components(schemas(Stack, StackSettlementTicket)) + paths(get_stacks, get_claimed_stacks), + components(schemas(Stack, StackSettlementTicket, StackQuery, ClaimedStackQuery)) )] pub(crate) struct StacksOpenApi; -//TODO: all of these endpoints should be deleted and replaced with the ones I suggested in the TODO of almost_filled_stacks.rs. If necessary, we can add additional filters to the endpoints to accomplish the functionality present in these endpoints. - -/// Router for handling stack-related endpoints -/// -/// This function sets up the routing for various stack-related operations, -/// including listing all stacks, retrieving specific stacks by ID, listing claimed stacks, -/// and retrieving specific claimed stacks by ID. Each route corresponds to a specific -/// operation that can be performed on stacks within the system. pub fn stacks_router() -> Router { Router::new() - .route(STACKS_PATH, get(stacks_list)) - .route(&format!("{STACKS_PATH}/:id"), get(stacks_get)) - .route("/claimed_stacks", get(claimed_stacks_list)) - .route("/claimed_stacks/:id", get(claimed_stacks_get)) + .route(STACKS_PATH, get(get_stacks)) + .route("/claimed_stacks", get(get_claimed_stacks)) } -/// List all stacks for currently registered nodes +/// Get stacks with optional filtering /// -/// Retrieves all stacks associated with the currently registered node badges. +/// Retrieves stacks with optional filtering by node ID and minimum fill fraction. #[utoipa::path( get, path = "/stacks", - responses( - (status = OK, description = "List of all Stack objects for all registered nodes", body = Vec), - (status = INTERNAL_SERVER_ERROR, description = "Internal server error") - ) -)] -pub async fn stacks_list( - State(daemon_state): State, -) -> Result>, StatusCode> { - Ok(Json( - daemon_state - .atoma_state - .get_stacks_by_node_small_ids( - &daemon_state - .node_badges - .iter() - .map(|(_, small_id)| *small_id as i64) - .collect::>(), - ) - .await - .map_err(|_| { - error!("Failed to get all node stacks"); - StatusCode::INTERNAL_SERVER_ERROR - })?, - )) -} - -/// List all stacks for a specific node -/// -/// Retrieves all stacks for a specific node identified by its small ID. -#[utoipa::path( - get, - path = "/stacks/{id}", params( - ("id" = i64, Path, description = "The small ID of the node whose stacks should be retrieved") + ("node_id" = Option, Query, description = "Optional node ID to filter by"), + ("min_fill_fraction" = Option, Query, description = "Optional minimum fill fraction (0.0 to 100.0)") ), responses( - (status = OK, description = "List of Stack objects for the specified node", body = Vec), + (status = OK, description = "List of Stack objects matching the criteria", body = Vec), (status = INTERNAL_SERVER_ERROR, description = "Internal server error") ) )] -pub async fn stacks_get( +pub async fn get_stacks( State(daemon_state): State, - Path(node_small_id): Path, + Query(query): Query, ) -> Result>, StatusCode> { - Ok(Json( + let node_ids = match query.node_id { + Some(id) => vec![id], + None => daemon_state + .node_badges + .iter() + .map(|(_, small_id)| *small_id as i64) + .collect(), + }; + + let stacks = if let Some(fraction) = query.min_fill_fraction { daemon_state .atoma_state - .get_stack_by_id(node_small_id) + .get_almost_filled_stacks(&node_ids, fraction) .await - .map_err(|_| { - error!("Failed to get node stack"); - StatusCode::INTERNAL_SERVER_ERROR - })?, - )) -} - -/// List all claimed stacks for currently registered nodes -/// -/// Retrieves all claimed stacks for the currently registered node badges. -#[utoipa::path( - get, - path = "/claimed_stacks", - responses( - (status = OK, description = "List of all StackSettlementTicket objects for all registered nodes", body = Vec), - (status = INTERNAL_SERVER_ERROR, description = "Internal server error") - ) -)] -pub async fn claimed_stacks_list( - State(daemon_state): State, -) -> Result>, StatusCode> { - Ok(Json( + } else { daemon_state .atoma_state - .get_claimed_stacks( - &daemon_state - .node_badges - .iter() - .map(|(_, small_id)| *small_id as i64) - .collect::>(), - ) + .get_stacks_by_node_small_ids(&node_ids) .await - .map_err(|_| { - error!("Failed to get all claimed stacks"); - StatusCode::INTERNAL_SERVER_ERROR - })?, - )) + }; + + stacks.map(Json).map_err(|_| { + error!("Failed to get stacks"); + StatusCode::INTERNAL_SERVER_ERROR + }) } -/// List all claimed stacks for a specific node -/// -/// Retrieves all claimed stacks for a specific node identified by its small ID. +/// Get claimed stacks with optional filtering #[utoipa::path( get, - path = "/claimed_stacks/{id}", + path = "/claimed_stacks", params( - ("id" = i64, Path, description = "The small ID of the node whose claimed stacks should be retrieved") + ("node_id" = Option, Query, description = "Optional node ID to filter by") ), responses( - (status = OK, description = "List of StackSettlementTicket objects for the specified node", body = Vec), + (status = OK, description = "List of claimed stacks matching the criteria", body = Vec), (status = INTERNAL_SERVER_ERROR, description = "Internal server error") ) )] -pub async fn claimed_stacks_get( +pub async fn get_claimed_stacks( State(daemon_state): State, - Path(node_small_id): Path, + Query(query): Query, ) -> Result>, StatusCode> { - Ok(Json( - daemon_state - .atoma_state - .get_claimed_stacks(&[node_small_id]) - .await - .map_err(|_| { - error!("Failed to get node claimed stacks"); - StatusCode::INTERNAL_SERVER_ERROR - })?, - )) + let node_ids = match query.node_id { + Some(id) => vec![id], + None => daemon_state + .node_badges + .iter() + .map(|(_, small_id)| *small_id as i64) + .collect(), + }; + + daemon_state + .atoma_state + .get_claimed_stacks(&node_ids) + .await + .map(Json) + .map_err(|_| { + error!("Failed to get claimed stacks"); + StatusCode::INTERNAL_SERVER_ERROR + }) } From c77a31f7567d550ff82a7f8847a69e57e2c8cf7a Mon Sep 17 00:00:00 2001 From: francis2tm Date: Thu, 12 Dec 2024 12:58:44 +0000 Subject: [PATCH 3/7] chore: fixed fmt --- atoma-service/src/handlers/chat_completions.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/atoma-service/src/handlers/chat_completions.rs b/atoma-service/src/handlers/chat_completions.rs index a66d6518..a2c00a38 100644 --- a/atoma-service/src/handlers/chat_completions.rs +++ b/atoma-service/src/handlers/chat_completions.rs @@ -88,7 +88,6 @@ const STREAM_KEEP_ALIVE_INTERVAL_IN_SECONDS: u64 = 15; pub(crate) struct ChatCompletionsOpenApi; /// Create chat completion -/// /// /// This handler performs several key operations: /// 1. Forwards the chat completion request to the inference service From bcfb4d411d3f101bf515ac4dcee3be3091492fc4 Mon Sep 17 00:00:00 2001 From: francis2tm Date: Thu, 12 Dec 2024 14:25:22 +0000 Subject: [PATCH 4/7] wip: refactor endpoints --- atoma-daemon/docs/openapi.yml | 227 +++++------------- atoma-daemon/src/components/openapi.rs | 7 +- .../src/handlers/almost_filled_stacks.rs | 1 - atoma-daemon/src/handlers/claimed_stacks.rs | 53 ++++ atoma-daemon/src/handlers/mod.rs | 2 +- atoma-daemon/src/handlers/nodes.rs | 15 +- atoma-daemon/src/handlers/stacks.rs | 75 ++---- atoma-daemon/src/handlers/subscriptions.rs | 56 +---- atoma-daemon/src/server.rs | 8 +- 9 files changed, 153 insertions(+), 291 deletions(-) delete mode 100644 atoma-daemon/src/handlers/almost_filled_stacks.rs create mode 100644 atoma-daemon/src/handlers/claimed_stacks.rs diff --git a/atoma-daemon/docs/openapi.yml b/atoma-daemon/docs/openapi.yml index fef6df95..56925a4c 100644 --- a/atoma-daemon/docs/openapi.yml +++ b/atoma-daemon/docs/openapi.yml @@ -9,79 +9,6 @@ servers: - url: http://localhost:8080 description: Local development server paths: - /almost_filled_stacks/{fraction}: - get: - tags: - - Almost filled stacks - summary: List almost filled stacks - description: Retrieves all stacks that are filled above a specified fraction threshold for all registered nodes. - operationId: almost_filled_stacks_fraction_get - parameters: - - name: fraction - in: path - description: The fraction threshold (0.0 to 100.0) to filter stacks - required: true - schema: - type: number - format: double - responses: - '200': - description: Stack objects that are filled above the specified fraction threshold - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/Stack' - '500': - description: Internal server error - /almost_filled_stacks/{id}/{fraction}: - get: - tags: - - Almost filled stacks - summary: List almost filled stacks for a specific node - description: |- - Retrieves all stacks that are filled above a specified fraction threshold for a specific node. - - # Arguments - * `daemon_state` - The shared state containing the state manager - * `node_small_id` - The small ID of the node whose stacks should be retrieved - * `fraction` - The fraction threshold (0.0 to 100.0) to filter stacks - - # Returns - * `Result>>` - A JSON response containing a list of stacks - - `Ok(Json>)` - Successfully retrieved stacks - - `Err(StatusCode::INTERNAL_SERVER_ERROR)` - Failed to retrieve stacks from state manager - - # Example Response - Returns a JSON array of Stack objects for the specified node that are filled above the fraction threshold - operationId: almost_filled_stacks_get - parameters: - - name: id - in: path - description: The small ID of the node whose stacks should be retrieved - required: true - schema: - type: integer - format: int64 - - name: fraction - in: path - description: The fraction threshold (0.0 to 100.0) to filter stacks - required: true - schema: - type: number - format: double - responses: - '200': - description: List of node stacks - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/Stack' - '500': - description: Internal server error /attestation_disputes/against: get: tags: @@ -170,6 +97,32 @@ paths: $ref: '#/components/schemas/StackAttestationDispute' '500': description: Internal server error + /claimed-stacks/claimed_stacks/nodes/{node_id}: + get: + tags: + - Claimed stacks + summary: List claimed stacks + description: Lists all claimed stacks for a specific node identified by its small ID. + operationId: claimed_stacks_nodes_list + parameters: + - name: node_id + in: path + description: Node small ID + required: true + schema: + type: integer + format: int64 + responses: + '200': + description: List of claimed stacks matching the criteria + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/StackSettlementTicket' + '500': + description: Internal server error /nodes/claim-funds: post: tags: @@ -281,10 +234,11 @@ paths: '500': description: Failed to submit task subscription transaction /nodes/task-unsubscribe: - post: + delete: tags: - Nodes - summary: Unsubscribes a node from a specific task. + summary: Delete task subscription + description: Unsubscribes a node from a specific task. operationId: nodes_task_unsubscribe requestBody: content: @@ -345,86 +299,33 @@ paths: $ref: '#/components/schemas/NodeTrySettleStacksResponse' '500': description: Failed to submit try settle stacks - /stacks/claimed_stacks: + /stacks/stacks/nodes/{node_id}: get: tags: - Stacks - summary: List all claimed stacks for currently registered nodes - description: Retrieves all claimed stacks for the currently registered node badges. - operationId: claimed_stacks_list - responses: - '200': - description: List of all StackSettlementTicket objects for all registered nodes - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/StackSettlementTicket' - '500': - description: Internal server error - /stacks/claimed_stacks/{id}: - get: - tags: - - Stacks - summary: List all claimed stacks for a specific node - description: Retrieves all claimed stacks for a specific node identified by its small ID. - operationId: claimed_stacks_get + summary: List stacks + description: Lists all stacks for a specific node identified by its small ID. + operationId: stacks_nodes_list parameters: - - name: id + - name: node_id in: path - description: The small ID of the node whose claimed stacks should be retrieved + description: Node small ID required: true schema: type: integer format: int64 - responses: - '200': - description: List of StackSettlementTicket objects for the specified node - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/StackSettlementTicket' - '500': - description: Internal server error - /stacks/stacks: - get: - tags: - - Stacks - summary: List all stacks for currently registered nodes - description: Retrieves all stacks associated with the currently registered node badges. - operationId: stacks_list - responses: - '200': - description: List of all Stack objects for all registered nodes - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/Stack' - '500': - description: Internal server error - /stacks/stacks/{id}: - get: - tags: - - Stacks - summary: List all stacks for a specific node - description: Retrieves all stacks for a specific node identified by its small ID. - operationId: stacks_get - parameters: - - name: id - in: path - description: The small ID of the node whose stacks should be retrieved - required: true + - name: min_fill_fraction + in: query + description: Optional minimum fill fraction (0.0 to 100.0) + required: false schema: - type: integer - format: int64 + type: + - number + - 'null' + format: double responses: '200': - description: List of Stack objects for the specified node + description: List of Stack objects matching the criteria content: application/json: schema: @@ -433,35 +334,15 @@ paths: $ref: '#/components/schemas/Stack' '500': description: Internal server error - /subscriptions/: - get: - tags: - - Subscriptions - summary: List all subscriptions for currently registered nodes - description: Retrieves all node subscriptions for the currently registered node badges. - operationId: subscriptions_list - responses: - '200': - description: List of all node subscriptions - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/NodeSubscription' - '404': - description: No node badges registered - '500': - description: Internal server error - /subscriptions/{id}: + /subscriptions/nodes/{node_id}: get: tags: - Subscriptions - summary: List all subscriptions for a specific node - description: Retrieves all subscriptions for a specific node identified by its small ID. - operationId: subscriptions_get + summary: List subscriptions + description: Lists all subscriptions for a specific node identified by its small ID. + operationId: subscriptions_nodes_list parameters: - - name: id + - name: node_id in: path description: The small ID of the node whose subscriptions should be retrieved required: true @@ -1110,6 +991,14 @@ components: type: integer format: int64 description: Unique small integer identifier for the stack involved in the dispute + StackQuery: + type: object + properties: + min_fill_fraction: + type: + - number + - 'null' + format: double StackSettlementTicket: type: object description: Represents a settlement ticket for a compute stack @@ -1227,6 +1116,8 @@ components: tags: - name: Almost filled stacks description: Almost filled stacks management +- name: Claimed stacks + description: Claimed stacks management - name: Attestation disputes description: Attestation disputes management - name: Nodes diff --git a/atoma-daemon/src/components/openapi.rs b/atoma-daemon/src/components/openapi.rs index de113107..4898be69 100644 --- a/atoma-daemon/src/components/openapi.rs +++ b/atoma-daemon/src/components/openapi.rs @@ -2,17 +2,15 @@ use axum::Router; use utoipa::OpenApi; use utoipa_swagger_ui::SwaggerUi; -use crate::handlers::{ - almost_filled_stacks, attestation_disputes, nodes, stacks, subscriptions, tasks, -}; +use crate::handlers::{attestation_disputes, claimed_stacks, nodes, stacks, subscriptions, tasks}; pub fn openapi_routes() -> Router { /// OpenAPI documentation for the Atoma daemon API. #[derive(OpenApi)] #[openapi( nest( - (path = almost_filled_stacks::ALMOST_FILLED_STACKS_PATH, api = almost_filled_stacks::AlmostFilledStacksOpenApi, tags = ["Almost filled stacks"]), (path = attestation_disputes::ATTESTATION_DISPUTES_PATH, api = attestation_disputes::AttestationDisputesOpenApi, tags = ["Attestation disputes"]), + (path = claimed_stacks::CLAIMED_STACKS_PATH, api = claimed_stacks::ClaimedStacksOpenApi, tags = ["Claimed stacks"]), (path = nodes::NODES_PATH, api = nodes::NodesOpenApi, tags = ["Nodes"]), (path = stacks::STACKS_PATH, api = stacks::StacksOpenApi, tags = ["Stacks"]), (path = subscriptions::SUBSCRIPTIONS_PATH, api = subscriptions::SubscriptionsOpenApi, tags = ["Subscriptions"]), @@ -20,6 +18,7 @@ pub fn openapi_routes() -> Router { ), tags( (name = "Almost filled stacks", description = "Almost filled stacks management"), + (name = "Claimed stacks", description = "Claimed stacks management"), (name = "Attestation disputes", description = "Attestation disputes management"), (name = "Nodes", description = "Nodes management"), (name = "Stacks", description = "Stacks management"), diff --git a/atoma-daemon/src/handlers/almost_filled_stacks.rs b/atoma-daemon/src/handlers/almost_filled_stacks.rs deleted file mode 100644 index 8b137891..00000000 --- a/atoma-daemon/src/handlers/almost_filled_stacks.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/atoma-daemon/src/handlers/claimed_stacks.rs b/atoma-daemon/src/handlers/claimed_stacks.rs new file mode 100644 index 00000000..7cc01d1e --- /dev/null +++ b/atoma-daemon/src/handlers/claimed_stacks.rs @@ -0,0 +1,53 @@ +use atoma_state::types::StackSettlementTicket; +use axum::{extract::State, http::StatusCode, routing::get, Json, Router}; +use tracing::error; +use utoipa::OpenApi; + +use crate::DaemonState; + +pub const CLAIMED_STACKS_PATH: &str = "/claimed-stacks"; + +#[derive(OpenApi)] +#[openapi( + paths(claimed_stacks_nodes_list), + components(schemas(StackSettlementTicket)) +)] +pub(crate) struct ClaimedStacksOpenApi; + +pub fn claimed_stacks_router() -> Router { + Router::new().route( + &format!("{CLAIMED_STACKS_PATH}/nodes/:node_id"), + get(claimed_stacks_nodes_list), + ) +} + +/// List claimed stacks +/// +/// Lists all claimed stacks for a specific node identified by its small ID. +#[utoipa::path( + get, + path = "/claimed_stacks/nodes/{node_id}", + params( + ("node_id" = i64, Path, description = "Node small ID") + ), + responses( + (status = OK, description = "List of claimed stacks matching the criteria", body = Vec), + (status = INTERNAL_SERVER_ERROR, description = "Internal server error") + ) +)] +pub async fn claimed_stacks_nodes_list( + State(daemon_state): State, + axum::extract::Path(node_id): axum::extract::Path, +) -> Result>, StatusCode> { + let node_ids = vec![node_id]; + + daemon_state + .atoma_state + .get_claimed_stacks(&node_ids) + .await + .map(Json) + .map_err(|_| { + error!("Failed to get claimed stacks"); + StatusCode::INTERNAL_SERVER_ERROR + }) +} diff --git a/atoma-daemon/src/handlers/mod.rs b/atoma-daemon/src/handlers/mod.rs index 05146822..4867faa0 100644 --- a/atoma-daemon/src/handlers/mod.rs +++ b/atoma-daemon/src/handlers/mod.rs @@ -1,5 +1,5 @@ -pub(crate) mod almost_filled_stacks; pub(crate) mod attestation_disputes; +pub(crate) mod claimed_stacks; pub(crate) mod nodes; pub(crate) mod stacks; pub(crate) mod subscriptions; diff --git a/atoma-daemon/src/handlers/nodes.rs b/atoma-daemon/src/handlers/nodes.rs index a5820c09..fdfdb79b 100644 --- a/atoma-daemon/src/handlers/nodes.rs +++ b/atoma-daemon/src/handlers/nodes.rs @@ -1,4 +1,9 @@ -use axum::{extract::State, http::StatusCode, routing::post, Json, Router}; +use axum::{ + extract::State, + http::StatusCode, + routing::{delete, post}, + Json, Router, +}; use tracing::{error, info}; use utoipa::OpenApi; @@ -74,7 +79,7 @@ pub fn nodes_router() -> Router { ) .route( &format!("{NODES_PATH}/task-unsubscribe"), - post(nodes_task_unsubscribe), + delete(nodes_task_unsubscribe), ) .route( &format!("{NODES_PATH}/try-settle-stacks"), @@ -259,11 +264,11 @@ pub async fn nodes_task_update_subscription( Ok(Json(NodeTaskUpdateSubscriptionResponse { tx_digest })) } -//TODO: change to delete - +/// Delete task subscription +/// /// Unsubscribes a node from a specific task. #[utoipa::path( - post, + delete, path = "/task-unsubscribe", request_body = NodeTaskUnsubscriptionRequest, responses( diff --git a/atoma-daemon/src/handlers/stacks.rs b/atoma-daemon/src/handlers/stacks.rs index 791061fd..31a8e7c8 100644 --- a/atoma-daemon/src/handlers/stacks.rs +++ b/atoma-daemon/src/handlers/stacks.rs @@ -1,6 +1,6 @@ use atoma_state::types::{Stack, StackSettlementTicket}; use axum::{ - extract::{Path, Query, State}, + extract::{Query, State}, http::StatusCode, routing::get, Json, Router, @@ -15,36 +15,31 @@ pub const STACKS_PATH: &str = "/stacks"; #[derive(Deserialize, ToSchema)] pub struct StackQuery { - node_id: Option, min_fill_fraction: Option, } -#[derive(Deserialize, ToSchema)] -pub struct ClaimedStackQuery { - node_id: Option, -} - #[derive(OpenApi)] #[openapi( - paths(get_stacks, get_claimed_stacks), - components(schemas(Stack, StackSettlementTicket, StackQuery, ClaimedStackQuery)) + paths(stacks_nodes_list), + components(schemas(Stack, StackSettlementTicket, StackQuery)) )] pub(crate) struct StacksOpenApi; pub fn stacks_router() -> Router { - Router::new() - .route(STACKS_PATH, get(get_stacks)) - .route("/claimed_stacks", get(get_claimed_stacks)) + Router::new().route( + &format!("{STACKS_PATH}/nodes/:node_id"), + get(stacks_nodes_list), + ) } -/// Get stacks with optional filtering +/// List stacks /// -/// Retrieves stacks with optional filtering by node ID and minimum fill fraction. +/// Lists all stacks for a specific node identified by its small ID. #[utoipa::path( get, - path = "/stacks", + path = "/stacks/nodes/{node_id}", params( - ("node_id" = Option, Query, description = "Optional node ID to filter by"), + ("node_id" = i64, Path, description = "Node small ID"), ("min_fill_fraction" = Option, Query, description = "Optional minimum fill fraction (0.0 to 100.0)") ), responses( @@ -52,18 +47,12 @@ pub fn stacks_router() -> Router { (status = INTERNAL_SERVER_ERROR, description = "Internal server error") ) )] -pub async fn get_stacks( +pub async fn stacks_nodes_list( State(daemon_state): State, + axum::extract::Path(node_id): axum::extract::Path, Query(query): Query, ) -> Result>, StatusCode> { - let node_ids = match query.node_id { - Some(id) => vec![id], - None => daemon_state - .node_badges - .iter() - .map(|(_, small_id)| *small_id as i64) - .collect(), - }; + let node_ids = vec![node_id]; let stacks = if let Some(fraction) = query.min_fill_fraction { daemon_state @@ -82,39 +71,3 @@ pub async fn get_stacks( StatusCode::INTERNAL_SERVER_ERROR }) } - -/// Get claimed stacks with optional filtering -#[utoipa::path( - get, - path = "/claimed_stacks", - params( - ("node_id" = Option, Query, description = "Optional node ID to filter by") - ), - responses( - (status = OK, description = "List of claimed stacks matching the criteria", body = Vec), - (status = INTERNAL_SERVER_ERROR, description = "Internal server error") - ) -)] -pub async fn get_claimed_stacks( - State(daemon_state): State, - Query(query): Query, -) -> Result>, StatusCode> { - let node_ids = match query.node_id { - Some(id) => vec![id], - None => daemon_state - .node_badges - .iter() - .map(|(_, small_id)| *small_id as i64) - .collect(), - }; - - daemon_state - .atoma_state - .get_claimed_stacks(&node_ids) - .await - .map(Json) - .map_err(|_| { - error!("Failed to get claimed stacks"); - StatusCode::INTERNAL_SERVER_ERROR - }) -} diff --git a/atoma-daemon/src/handlers/subscriptions.rs b/atoma-daemon/src/handlers/subscriptions.rs index 74981ee0..ae72baad 100644 --- a/atoma-daemon/src/handlers/subscriptions.rs +++ b/atoma-daemon/src/handlers/subscriptions.rs @@ -13,10 +13,7 @@ use crate::DaemonState; pub const SUBSCRIPTIONS_PATH: &str = "/subscriptions"; #[derive(OpenApi)] -#[openapi( - paths(subscriptions_list, subscriptions_get), - components(schemas(NodeSubscription)) -)] +#[openapi(paths(subscriptions_nodes_list), components(schemas(NodeSubscription)))] pub(crate) struct SubscriptionsOpenApi; /// Router for handling subscription-related endpoints @@ -26,62 +23,27 @@ pub(crate) struct SubscriptionsOpenApi; /// Each route corresponds to a specific operation that can be performed on subscriptions /// within the system. pub fn subscriptions_router() -> Router { - Router::new() - .route(SUBSCRIPTIONS_PATH, get(subscriptions_list)) - .route(&format!("{SUBSCRIPTIONS_PATH}/:id"), get(subscriptions_get)) -} - -/// List all subscriptions for currently registered nodes -/// -/// Retrieves all node subscriptions for the currently registered node badges. -#[utoipa::path( - get, - path = "/", - responses( - (status = OK, description = "List of all node subscriptions", body = Vec), - (status = NOT_FOUND, description = "No node badges registered"), - (status = INTERNAL_SERVER_ERROR, description = "Internal server error") + Router::new().route( + &format!("{SUBSCRIPTIONS_PATH}/nodes/:node_id"), + get(subscriptions_nodes_list), ) -)] -pub async fn subscriptions_list( - State(daemon_state): State, -) -> Result>, StatusCode> { - let current_node_badges = daemon_state.node_badges; - if current_node_badges.is_empty() { - return Err(StatusCode::NOT_FOUND); - } - let all_node_subscriptions = daemon_state - .atoma_state - .get_all_node_subscriptions( - ¤t_node_badges - .iter() - .map(|(_, small_id)| *small_id as i64) - .collect::>(), - ) - .await - .map_err(|_| { - error!("Failed to get all node subscriptions"); - StatusCode::INTERNAL_SERVER_ERROR - })?; - Ok(Json(all_node_subscriptions)) } -//TODO: this endpoint should be deleted and add filter on subscriptions_list. -/// List all subscriptions for a specific node +/// List subscriptions /// -/// Retrieves all subscriptions for a specific node identified by its small ID. +/// Lists all subscriptions for a specific node identified by its small ID. #[utoipa::path( get, - path = "/{id}", + path = "/nodes/{node_id}", params( - ("id" = i64, Path, description = "The small ID of the node whose subscriptions should be retrieved") + ("node_id" = i64, Path, description = "The small ID of the node whose subscriptions should be retrieved") ), responses( (status = OK, description = "List of node subscriptions", body = Vec), (status = INTERNAL_SERVER_ERROR, description = "Internal server error") ) )] -pub async fn subscriptions_get( +pub async fn subscriptions_nodes_list( State(daemon_state): State, Path(node_small_id): Path, ) -> Result>, StatusCode> { diff --git a/atoma-daemon/src/server.rs b/atoma-daemon/src/server.rs index 6cdb5e69..3b6bf754 100644 --- a/atoma-daemon/src/server.rs +++ b/atoma-daemon/src/server.rs @@ -12,9 +12,9 @@ use tracing::instrument; use crate::{ components::openapi::openapi_routes, handlers::{ - almost_filled_stacks::almost_filled_stacks_router, - attestation_disputes::attestation_disputes_router, nodes::nodes_router, - stacks::stacks_router, subscriptions::subscriptions_router, tasks::tasks_router, + attestation_disputes::attestation_disputes_router, claimed_stacks::claimed_stacks_router, + nodes::nodes_router, stacks::stacks_router, subscriptions::subscriptions_router, + tasks::tasks_router, }, }; @@ -176,8 +176,8 @@ pub async fn run_server( /// ``` pub fn create_router(daemon_state: DaemonState) -> Router { Router::new() - .merge(almost_filled_stacks_router()) .merge(attestation_disputes_router()) + .merge(claimed_stacks_router()) .merge(nodes_router()) .merge(stacks_router()) .merge(subscriptions_router()) From d85a2ef7d0a1fddd51f8edc5faf825131a75b6c3 Mon Sep 17 00:00:00 2001 From: francis2tm Date: Thu, 12 Dec 2024 14:35:56 +0000 Subject: [PATCH 5/7] refactor(atoma-daemon): endpoints --- atoma-daemon/docs/openapi.yml | 56 ++------- .../src/handlers/attestation_disputes.rs | 112 +++--------------- 2 files changed, 26 insertions(+), 142 deletions(-) diff --git a/atoma-daemon/docs/openapi.yml b/atoma-daemon/docs/openapi.yml index 56925a4c..f3e0875c 100644 --- a/atoma-daemon/docs/openapi.yml +++ b/atoma-daemon/docs/openapi.yml @@ -9,33 +9,15 @@ servers: - url: http://localhost:8080 description: Local development server paths: - /attestation_disputes/against: + /attestation_disputes/against/nodes/{node_id}: get: tags: - Attestation disputes - summary: List attestation disputes against currently registered nodes - description: Retrieves all attestation disputes against the currently registered nodes. - operationId: attestation_disputes_against_list - responses: - '200': - description: List of all against attestation disputes where the registered nodes are the defendants - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/StackAttestationDispute' - '500': - description: Internal server error - /attestation_disputes/against/{id}: - get: - tags: - - Attestation disputes - summary: List attestation disputes against a specific node - description: Retrieves all attestation disputes against a specific node. - operationId: attestation_disputes_against_get + summary: List against attestation disputes + description: Lists all attestation disputes against a specific node. + operationId: attestation_disputes_against_nodes_list parameters: - - name: id + - name: node_id in: path description: The small ID of the node whose disputes should be retrieved required: true @@ -53,33 +35,15 @@ paths: $ref: '#/components/schemas/StackAttestationDispute' '500': description: Internal server error - /attestation_disputes/own: + /attestation_disputes/own/nodes/{node_id}: get: tags: - Attestation disputes - summary: List attestation disputes initiated by currently registered nodes - description: Retrieves all attestation disputes initiated by the currently registered nodes. - operationId: attestation_disputes_own_list - responses: - '200': - description: List of all own attestation disputes where the registered nodes are the plaintiffs - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/StackAttestationDispute' - '500': - description: Internal server error - /attestation_disputes/own/{id}: - get: - tags: - - Attestation disputes - summary: List attestation disputes initiated by a specific node - description: Retrieves all attestation disputes initiated by a specific node. - operationId: attestation_disputes_own_get + summary: List own attestation disputes + description: Lists all attestation disputes initiated by a specific node. + operationId: attestation_disputes_own_nodes_list parameters: - - name: id + - name: node_id in: path description: The small ID of the node whose initiated disputes should be retrieved required: true diff --git a/atoma-daemon/src/handlers/attestation_disputes.rs b/atoma-daemon/src/handlers/attestation_disputes.rs index a2b09acb..ab73f775 100644 --- a/atoma-daemon/src/handlers/attestation_disputes.rs +++ b/atoma-daemon/src/handlers/attestation_disputes.rs @@ -15,10 +15,8 @@ pub const ATTESTATION_DISPUTES_PATH: &str = "/attestation_disputes"; #[derive(OpenApi)] #[openapi( paths( - attestation_disputes_against_list, - attestation_disputes_against_get, - attestation_disputes_own_list, - attestation_disputes_own_get + attestation_disputes_against_nodes_list, + attestation_disputes_own_nodes_list ), components(schemas(StackAttestationDispute)) )] @@ -27,79 +25,33 @@ pub(crate) struct AttestationDisputesOpenApi; //TODO: this endpoint can be merged into one (I think) through filters /// Router for handling attestation disputes endpoints -/// -/// Creates routes for: -/// - GET /attestation_disputes/against - Get all attestation disputes against the registered nodes -/// - GET /attestation_disputes/against/:id - Get attestation disputes against a specific node -/// - GET /attestation_disputes/own - Get all attestation disputes initiated by the registered nodes -/// - GET /attestation_disputes/own/:id - Get attestation disputes initiated by a specific node pub fn attestation_disputes_router() -> Router { Router::new() .route( - &format!("{ATTESTATION_DISPUTES_PATH}/against"), - get(attestation_disputes_against_list), - ) - .route( - &format!("{ATTESTATION_DISPUTES_PATH}/against/:id"), - get(attestation_disputes_against_get), + &format!("{ATTESTATION_DISPUTES_PATH}/against/nodes/:node_id"), + get(attestation_disputes_against_nodes_list), ) .route( - &format!("{ATTESTATION_DISPUTES_PATH}/own"), - get(attestation_disputes_own_list), - ) - .route( - &format!("{ATTESTATION_DISPUTES_PATH}/own/:id"), - get(attestation_disputes_own_get), + &format!("{ATTESTATION_DISPUTES_PATH}/own/nodes/:node_id"), + get(attestation_disputes_own_nodes_list), ) } -/// List attestation disputes against currently registered nodes +/// List against attestation disputes /// -/// Retrieves all attestation disputes against the currently registered nodes. +/// Lists all attestation disputes against a specific node. #[utoipa::path( get, - path = "/against", - responses( - (status = OK, description = "List of all against attestation disputes where the registered nodes are the defendants", body = Vec), - (status = INTERNAL_SERVER_ERROR, description = "Internal server error") - ) -)] -pub async fn attestation_disputes_against_list( - State(daemon_state): State, -) -> Result>, StatusCode> { - Ok(Json( - daemon_state - .atoma_state - .get_against_attestation_disputes( - &daemon_state - .node_badges - .iter() - .map(|(_, small_id)| *small_id as i64) - .collect::>(), - ) - .await - .map_err(|_| { - error!("Failed to get all attestation disputes"); - StatusCode::INTERNAL_SERVER_ERROR - })?, - )) -} - -/// List attestation disputes against a specific node -/// -/// Retrieves all attestation disputes against a specific node. -#[utoipa::path( - get, - path = "/against/{id}", + path = "/against/nodes/{node_id}", params( - ("id" = i64, Path, description = "The small ID of the node whose disputes should be retrieved") + ("node_id" = i64, Path, description = "The small ID of the node whose disputes should be retrieved") ), responses( (status = OK, description = "List of against attestation disputes where the specified node is the defendant", body = Vec), (status = INTERNAL_SERVER_ERROR, description = "Internal server error") ) )] -pub async fn attestation_disputes_against_get( +pub async fn attestation_disputes_against_nodes_list( State(daemon_state): State, Path(node_small_id): Path, ) -> Result>, StatusCode> { @@ -115,53 +67,21 @@ pub async fn attestation_disputes_against_get( )) } -/// List attestation disputes initiated by currently registered nodes -/// -/// Retrieves all attestation disputes initiated by the currently registered nodes. -#[utoipa::path( - get, - path = "/own", - responses( - (status = OK, description = "List of all own attestation disputes where the registered nodes are the plaintiffs", body = Vec), - (status = INTERNAL_SERVER_ERROR, description = "Internal server error") - ) -)] -pub async fn attestation_disputes_own_list( - State(daemon_state): State, -) -> Result>, StatusCode> { - Ok(Json( - daemon_state - .atoma_state - .get_own_attestation_disputes( - &daemon_state - .node_badges - .iter() - .map(|(_, small_id)| *small_id as i64) - .collect::>(), - ) - .await - .map_err(|_| { - error!("Failed to get all own attestation disputes"); - StatusCode::INTERNAL_SERVER_ERROR - })?, - )) -} - -/// List attestation disputes initiated by a specific node +/// List own attestation disputes /// -/// Retrieves all attestation disputes initiated by a specific node. +/// Lists all attestation disputes initiated by a specific node. #[utoipa::path( get, - path = "/own/{id}", + path = "/own/nodes/{node_id}", params( - ("id" = i64, Path, description = "The small ID of the node whose initiated disputes should be retrieved") + ("node_id" = i64, Path, description = "The small ID of the node whose initiated disputes should be retrieved") ), responses( (status = OK, description = "List of own attestation disputes where the specified node is the plaintiff", body = Vec), (status = INTERNAL_SERVER_ERROR, description = "Internal server error") ) )] -pub async fn attestation_disputes_own_get( +pub async fn attestation_disputes_own_nodes_list( State(daemon_state): State, Path(node_small_id): Path, ) -> Result>, StatusCode> { From 9716a765e809283e29342e7692ecb7c177f9a29c Mon Sep 17 00:00:00 2001 From: francis2tm Date: Thu, 12 Dec 2024 14:44:26 +0000 Subject: [PATCH 6/7] chore: update openapi.yml --- atoma-daemon/docs/openapi.yml | 51 +++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/atoma-daemon/docs/openapi.yml b/atoma-daemon/docs/openapi.yml index fd35795c..e42f05cb 100644 --- a/atoma-daemon/docs/openapi.yml +++ b/atoma-daemon/docs/openapi.yml @@ -92,9 +92,10 @@ paths: tags: - Nodes summary: Create claim funds transaction - description: Claims funds for completed stacks. - summary: Create claim funds transaction - description: Claims funds for completed stacks. + description: |- + Create claim funds transaction + + Claims funds for completed stacks. operationId: nodes_claim_funds requestBody: content: @@ -116,9 +117,10 @@ paths: tags: - Nodes summary: Create model subscription transaction - description: Subscribes a node to a specific model. - summary: Create model subscription transaction - description: Subscribes a node to a specific model. + description: |- + Create model subscription transaction + + Subscribes a node to a specific model. operationId: nodes_model_subscribe requestBody: content: @@ -140,9 +142,10 @@ paths: tags: - Nodes summary: Create node registration transaction - description: Registers a new node in the system. - summary: Create node registration transaction - description: Registers a new node in the system. + description: |- + Create node registration transaction + + Registers a new node in the system. operationId: nodes_register requestBody: content: @@ -164,9 +167,10 @@ paths: tags: - Nodes summary: Create attestation proof transaction - description: Submits attestations for stack settlement. - summary: Create attestation proof transaction - description: Submits attestations for stack settlement. + description: |- + Create attestation proof transaction + + Submits attestations for stack settlement. operationId: nodes_submit_attestations requestBody: content: @@ -188,9 +192,10 @@ paths: tags: - Nodes summary: Create task subscription transaction - description: Subscribes a node to a specific task. - summary: Create task subscription transaction - description: Subscribes a node to a specific task. + description: |- + Create task subscription transaction + + Subscribes a node to a specific task. operationId: nodes_task_subscribe requestBody: content: @@ -234,9 +239,10 @@ paths: tags: - Nodes summary: Modify task subscription - description: Updates an existing task subscription for a node. - summary: Modify task subscription - description: Updates an existing task subscription for a node. + description: |- + Modify task subscription + + Updates an existing task subscription for a node. operationId: nodes_task_update_subscription requestBody: content: @@ -258,9 +264,10 @@ paths: tags: - Nodes summary: Create try settle stacks transaction - description: Attempts to settle stacks for a node. - summary: Create try settle stacks transaction - description: Attempts to settle stacks for a node. + description: |- + Create try settle stacks transaction + + Attempts to settle stacks for a node. operationId: nodes_try_settle_stacks requestBody: content: @@ -344,8 +351,6 @@ paths: - Tasks summary: List all tasks description: Retrieves all tasks from the state manager. - summary: List all tasks - description: Retrieves all tasks from the state manager. operationId: tasks_list responses: '200': From 6ce5cc77c959acf1be21125683672c9e379bc300 Mon Sep 17 00:00:00 2001 From: francis2tm Date: Thu, 12 Dec 2024 14:58:33 +0000 Subject: [PATCH 7/7] chore: trim import path --- atoma-daemon/src/handlers/claimed_stacks.rs | 4 ++-- atoma-daemon/src/handlers/stacks.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/atoma-daemon/src/handlers/claimed_stacks.rs b/atoma-daemon/src/handlers/claimed_stacks.rs index 7cc01d1e..52a1a55b 100644 --- a/atoma-daemon/src/handlers/claimed_stacks.rs +++ b/atoma-daemon/src/handlers/claimed_stacks.rs @@ -1,5 +1,5 @@ use atoma_state::types::StackSettlementTicket; -use axum::{extract::State, http::StatusCode, routing::get, Json, Router}; +use axum::{extract::Path, extract::State, http::StatusCode, routing::get, Json, Router}; use tracing::error; use utoipa::OpenApi; @@ -37,7 +37,7 @@ pub fn claimed_stacks_router() -> Router { )] pub async fn claimed_stacks_nodes_list( State(daemon_state): State, - axum::extract::Path(node_id): axum::extract::Path, + Path(node_id): Path, ) -> Result>, StatusCode> { let node_ids = vec![node_id]; diff --git a/atoma-daemon/src/handlers/stacks.rs b/atoma-daemon/src/handlers/stacks.rs index 31a8e7c8..49eafe9a 100644 --- a/atoma-daemon/src/handlers/stacks.rs +++ b/atoma-daemon/src/handlers/stacks.rs @@ -1,6 +1,6 @@ use atoma_state::types::{Stack, StackSettlementTicket}; use axum::{ - extract::{Query, State}, + extract::{Path, Query, State}, http::StatusCode, routing::get, Json, Router, @@ -49,7 +49,7 @@ pub fn stacks_router() -> Router { )] pub async fn stacks_nodes_list( State(daemon_state): State, - axum::extract::Path(node_id): axum::extract::Path, + Path(node_id): Path, Query(query): Query, ) -> Result>, StatusCode> { let node_ids = vec![node_id];