Skip to content

Commit

Permalink
refactor(atoma-daemon): endpoints (#282)
Browse files Browse the repository at this point in the history
* chore: change endpoint names

* wip

* chore: fixed fmt

* wip: refactor endpoints

* refactor(atoma-daemon): endpoints

* chore: update openapi.yml

* chore: trim import path
  • Loading branch information
francis2tm authored Dec 13, 2024
1 parent 893a486 commit 88b06ec
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 609 deletions.
294 changes: 85 additions & 209 deletions atoma-daemon/docs/openapi.yml

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions atoma-daemon/src/components/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ 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"]),
(path = tasks::TASKS_PATH, api = tasks::TasksOpenApi, tags = ["Tasks"])
),
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"),
Expand Down
123 changes: 0 additions & 123 deletions atoma-daemon/src/handlers/almost_filled_stacks.rs

This file was deleted.

114 changes: 18 additions & 96 deletions atoma-daemon/src/handlers/attestation_disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,91 +15,45 @@ 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))
)]
pub(crate) struct AttestationDisputesOpenApi;

//TODO: this endpoint can be merged into one (I think) through filters

//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<DaemonState> {
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),
)
.route(
&format!("{ATTESTATION_DISPUTES_PATH}/own"),
get(attestation_disputes_own_list),
&format!("{ATTESTATION_DISPUTES_PATH}/against/nodes/:node_id"),
get(attestation_disputes_against_nodes_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<StackAttestationDispute>),
(status = INTERNAL_SERVER_ERROR, description = "Internal server error")
)
)]
pub async fn attestation_disputes_against_list(
State(daemon_state): State<DaemonState>,
) -> Result<Json<Vec<StackAttestationDispute>>, StatusCode> {
Ok(Json(
daemon_state
.atoma_state
.get_against_attestation_disputes(
&daemon_state
.node_badges
.iter()
.map(|(_, small_id)| *small_id as i64)
.collect::<Vec<_>>(),
)
.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<StackAttestationDispute>),
(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<DaemonState>,
Path(node_small_id): Path<i64>,
) -> Result<Json<Vec<StackAttestationDispute>>, StatusCode> {
Expand All @@ -115,53 +69,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<StackAttestationDispute>),
(status = INTERNAL_SERVER_ERROR, description = "Internal server error")
)
)]
pub async fn attestation_disputes_own_list(
State(daemon_state): State<DaemonState>,
) -> Result<Json<Vec<StackAttestationDispute>>, StatusCode> {
Ok(Json(
daemon_state
.atoma_state
.get_own_attestation_disputes(
&daemon_state
.node_badges
.iter()
.map(|(_, small_id)| *small_id as i64)
.collect::<Vec<_>>(),
)
.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<StackAttestationDispute>),
(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<DaemonState>,
Path(node_small_id): Path<i64>,
) -> Result<Json<Vec<StackAttestationDispute>>, StatusCode> {
Expand Down
53 changes: 53 additions & 0 deletions atoma-daemon/src/handlers/claimed_stacks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use atoma_state::types::StackSettlementTicket;
use axum::{extract::Path, 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<DaemonState> {
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<StackSettlementTicket>),
(status = INTERNAL_SERVER_ERROR, description = "Internal server error")
)
)]
pub async fn claimed_stacks_nodes_list(
State(daemon_state): State<DaemonState>,
Path(node_id): Path<i64>,
) -> Result<Json<Vec<StackSettlementTicket>>, 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
})
}
2 changes: 1 addition & 1 deletion atoma-daemon/src/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading

0 comments on commit 88b06ec

Please sign in to comment.