From cc6b6d1cc95fecc856e0d7c55bb0528a9752686f Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Date: Fri, 10 Nov 2023 09:28:10 +0100 Subject: [PATCH] Refactor nomos api: (#521) * Extracted common service into services * Moved api implementation to nomos node crate --- Cargo.toml | 2 +- nodes/nomos-node-api/src/http/backend/mod.rs | 2 -- nodes/nomos-node/Cargo.toml | 10 +++++++++- .../src/http/backend/axum.rs => nomos-node/src/api.rs} | 2 +- nodes/nomos-node/src/config.rs | 3 ++- nodes/nomos-node/src/lib.rs | 5 +++-- .../nomos-node-api => nomos-services/api}/Cargo.toml | 2 +- .../api}/src/http/cl.rs | 0 .../api}/src/http/consensus.rs | 0 .../api}/src/http/da.rs | 0 .../api}/src/http/libp2p.rs | 0 .../api}/src/http/mempool.rs | 0 .../api}/src/http/mod.rs | 3 --- .../api}/src/http/storage.rs | 0 .../nomos-node-api => nomos-services/api}/src/lib.rs | 0 .../api}/tests/todo.rs | 2 +- tests/Cargo.toml | 2 +- tests/src/nodes/nomos.rs | 5 ++--- 18 files changed, 21 insertions(+), 17 deletions(-) delete mode 100644 nodes/nomos-node-api/src/http/backend/mod.rs rename nodes/{nomos-node-api/src/http/backend/axum.rs => nomos-node/src/api.rs} (99%) rename {nodes/nomos-node-api => nomos-services/api}/Cargo.toml (98%) rename {nodes/nomos-node-api => nomos-services/api}/src/http/cl.rs (100%) rename {nodes/nomos-node-api => nomos-services/api}/src/http/consensus.rs (100%) rename {nodes/nomos-node-api => nomos-services/api}/src/http/da.rs (100%) rename {nodes/nomos-node-api => nomos-services/api}/src/http/libp2p.rs (100%) rename {nodes/nomos-node-api => nomos-services/api}/src/http/mempool.rs (100%) rename {nodes/nomos-node-api => nomos-services/api}/src/http/mod.rs (89%) rename {nodes/nomos-node-api => nomos-services/api}/src/http/storage.rs (100%) rename {nodes/nomos-node-api => nomos-services/api}/src/lib.rs (100%) rename {nodes/nomos-node-api => nomos-services/api}/tests/todo.rs (99%) diff --git a/Cargo.toml b/Cargo.toml index ae540ffb..d428c116 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "nomos-core", "nomos-libp2p", + "nomos-services/api", "nomos-services/log", "nomos-services/metrics", "nomos-services/network", @@ -17,7 +18,6 @@ members = [ "nomos-cli", "nomos-utils", "nodes/nomos-node", - "nodes/nomos-node-api", "nodes/mixnode", "simulations", "consensus-engine", diff --git a/nodes/nomos-node-api/src/http/backend/mod.rs b/nodes/nomos-node-api/src/http/backend/mod.rs deleted file mode 100644 index 8545ecd7..00000000 --- a/nodes/nomos-node-api/src/http/backend/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -#[cfg(feature = "axum")] -pub mod axum; diff --git a/nodes/nomos-node/Cargo.toml b/nodes/nomos-node/Cargo.toml index a00ee9f6..0e2b4ba1 100644 --- a/nodes/nomos-node/Cargo.toml +++ b/nodes/nomos-node/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +async-trait = "0.1" blake2 = "0.10" bincode = "2.0.0-rc.2" bytes = "1.3" @@ -20,7 +21,7 @@ tracing = "0.1" multiaddr = "0.18" nomos-core = { path = "../../nomos-core" } nomos-network = { path = "../../nomos-services/network", features = ["libp2p"] } -nomos-node-api = { path = "../nomos-node-api" } +nomos-api = { path = "../../nomos-services/api" } nomos-log = { path = "../../nomos-services/log" } nomos-mempool = { path = "../../nomos-services/mempool", features = ["mock", "libp2p"] } nomos-http = { path = "../../nomos-services/http", features = ["http"] } @@ -38,3 +39,10 @@ serde_yaml = "0.9" color-eyre = "0.6.0" serde = "1" full-replication = { path = "../../nomos-da/full-replication" } +# openapi related dependencies +utoipa = "4.0" +utoipa-swagger-ui = { version = "4.0" } +# axum related dependencies +axum = { version = "0.6" } +hyper = { version = "0.14", features = ["full"] } +tower-http = { version = "0.4", features = ["cors", "trace"] } \ No newline at end of file diff --git a/nodes/nomos-node-api/src/http/backend/axum.rs b/nodes/nomos-node/src/api.rs similarity index 99% rename from nodes/nomos-node-api/src/http/backend/axum.rs rename to nodes/nomos-node/src/api.rs index 1dd95ae8..56a04927 100644 --- a/nodes/nomos-node-api/src/http/backend/axum.rs +++ b/nodes/nomos-node/src/api.rs @@ -23,7 +23,7 @@ use nomos_mempool::{network::adapters::libp2p::Libp2pAdapter, openapi::Status, M use nomos_network::backends::libp2p::Libp2p; use nomos_storage::backends::StorageSerde; -use crate::{ +use nomos_api::{ http::{cl, consensus, da, libp2p, mempool, storage}, Backend, }; diff --git a/nodes/nomos-node/src/config.rs b/nodes/nomos-node/src/config.rs index eb0f7bf2..3388a71f 100644 --- a/nodes/nomos-node/src/config.rs +++ b/nodes/nomos-node/src/config.rs @@ -4,6 +4,7 @@ use std::{ time::Duration, }; +use crate::api::AxumBackend; use crate::DataAvailability; use crate::{Carnot, Tx, Wire, MB16}; use clap::{Parser, ValueEnum}; @@ -11,11 +12,11 @@ use color_eyre::eyre::{self, eyre, Result}; use hex::FromHex; #[cfg(feature = "metrics")] use metrics::{backend::map::MapMetricsBackend, types::MetricsData, MetricsService}; +use nomos_api::ApiService; use nomos_libp2p::{secp256k1::SecretKey, Multiaddr}; use nomos_log::{Logger, LoggerBackend, LoggerFormat}; use nomos_network::backends::libp2p::Libp2p; use nomos_network::NetworkService; -use nomos_node_api::{http::backend::axum::AxumBackend, ApiService}; use overwatch_rs::services::ServiceData; use serde::{Deserialize, Serialize}; use tracing::Level; diff --git a/nodes/nomos-node/src/lib.rs b/nodes/nomos-node/src/lib.rs index 3b304910..6010cdd9 100644 --- a/nodes/nomos-node/src/lib.rs +++ b/nodes/nomos-node/src/lib.rs @@ -1,3 +1,4 @@ +pub mod api; mod config; mod tx; @@ -9,7 +10,9 @@ use full_replication::{AbsoluteNumber, Attestation, Blob, FullReplication}; use metrics::{backend::map::MapMetricsBackend, types::MetricsData, MetricsService}; use nomos_consensus::network::adapters::libp2p::Libp2pAdapter as ConsensusLibp2pAdapter; +use api::AxumBackend; use bytes::Bytes; +use nomos_api::ApiService; use nomos_consensus::CarnotConsensus; use nomos_core::{ da::{blob, certificate}, @@ -27,8 +30,6 @@ use nomos_mempool::{ Transaction as TxDiscriminant, }; use nomos_network::backends::libp2p::Libp2p; -use nomos_node_api::http::backend::axum::AxumBackend; -use nomos_node_api::ApiService; use nomos_storage::{ backends::{sled::SledBackend, StorageSerde}, StorageService, diff --git a/nodes/nomos-node-api/Cargo.toml b/nomos-services/api/Cargo.toml similarity index 98% rename from nodes/nomos-node-api/Cargo.toml rename to nomos-services/api/Cargo.toml index d348b7a4..3067146a 100644 --- a/nodes/nomos-node-api/Cargo.toml +++ b/nomos-services/api/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "nomos-node-api" +name = "nomos-api" version = "0.1.0" edition = "2021" diff --git a/nodes/nomos-node-api/src/http/cl.rs b/nomos-services/api/src/http/cl.rs similarity index 100% rename from nodes/nomos-node-api/src/http/cl.rs rename to nomos-services/api/src/http/cl.rs diff --git a/nodes/nomos-node-api/src/http/consensus.rs b/nomos-services/api/src/http/consensus.rs similarity index 100% rename from nodes/nomos-node-api/src/http/consensus.rs rename to nomos-services/api/src/http/consensus.rs diff --git a/nodes/nomos-node-api/src/http/da.rs b/nomos-services/api/src/http/da.rs similarity index 100% rename from nodes/nomos-node-api/src/http/da.rs rename to nomos-services/api/src/http/da.rs diff --git a/nodes/nomos-node-api/src/http/libp2p.rs b/nomos-services/api/src/http/libp2p.rs similarity index 100% rename from nodes/nomos-node-api/src/http/libp2p.rs rename to nomos-services/api/src/http/libp2p.rs diff --git a/nodes/nomos-node-api/src/http/mempool.rs b/nomos-services/api/src/http/mempool.rs similarity index 100% rename from nodes/nomos-node-api/src/http/mempool.rs rename to nomos-services/api/src/http/mempool.rs diff --git a/nodes/nomos-node-api/src/http/mod.rs b/nomos-services/api/src/http/mod.rs similarity index 89% rename from nodes/nomos-node-api/src/http/mod.rs rename to nomos-services/api/src/http/mod.rs index 369b16b3..789aeee9 100644 --- a/nodes/nomos-node-api/src/http/mod.rs +++ b/nomos-services/api/src/http/mod.rs @@ -1,7 +1,4 @@ pub type DynError = Box; - -pub mod backend; - pub mod cl; pub mod consensus; pub mod da; diff --git a/nodes/nomos-node-api/src/http/storage.rs b/nomos-services/api/src/http/storage.rs similarity index 100% rename from nodes/nomos-node-api/src/http/storage.rs rename to nomos-services/api/src/http/storage.rs diff --git a/nodes/nomos-node-api/src/lib.rs b/nomos-services/api/src/lib.rs similarity index 100% rename from nodes/nomos-node-api/src/lib.rs rename to nomos-services/api/src/lib.rs diff --git a/nodes/nomos-node-api/tests/todo.rs b/nomos-services/api/tests/todo.rs similarity index 99% rename from nodes/nomos-node-api/tests/todo.rs rename to nomos-services/api/tests/todo.rs index ec5df850..845d684d 100644 --- a/nodes/nomos-node-api/tests/todo.rs +++ b/nomos-services/api/tests/todo.rs @@ -6,7 +6,7 @@ use std::{ use axum::{routing, Router, Server}; use hyper::Error; -use nomos_node_api::{ApiService, ApiServiceSettings, Backend}; +use nomos_api::{ApiService, ApiServiceSettings, Backend}; use overwatch_derive::Services; use overwatch_rs::{ overwatch::{handle::OverwatchHandle, OverwatchRunner}, diff --git a/tests/Cargo.toml b/tests/Cargo.toml index eefcb578..9142fd6b 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -9,7 +9,7 @@ nomos-node = { path = "../nodes/nomos-node", default-features = false } nomos-consensus = { path = "../nomos-services/consensus" } nomos-network = { path = "../nomos-services/network", features = ["libp2p"]} nomos-log = { path = "../nomos-services/log" } -nomos-node-api = { path = "../nodes/nomos-node-api", features = ["axum"] } +nomos-api = { path = "../nomos-services/api" } overwatch-rs = { git = "https://github.com/logos-co/Overwatch", rev = "2f70806" } nomos-core = { path = "../nomos-core" } consensus-engine = { path = "../consensus-engine", features = ["serde"] } diff --git a/tests/src/nodes/nomos.rs b/tests/src/nodes/nomos.rs index 04276af6..cffc8597 100644 --- a/tests/src/nodes/nomos.rs +++ b/tests/src/nodes/nomos.rs @@ -17,8 +17,7 @@ use nomos_log::{LoggerBackend, LoggerFormat}; use nomos_mempool::MempoolMetrics; use nomos_network::backends::libp2p::Libp2pConfig; use nomos_network::NetworkConfig; -use nomos_node::{Config, Tx}; -use nomos_node_api::http::backend::axum::AxumBackendSettings; +use nomos_node::{api::AxumBackendSettings, Config, Tx}; // crates use fraction::Fraction; use once_cell::sync::Lazy; @@ -334,7 +333,7 @@ fn create_node_config( blob_selector_settings: (), }, log: Default::default(), - http: nomos_node_api::ApiServiceSettings { + http: nomos_api::ApiServiceSettings { backend_settings: AxumBackendSettings { address: format!("127.0.0.1:{}", get_available_port()) .parse()