Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http): added versions endpoint #1700

Merged
merged 11 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ itertools = "0.11.0"
humantime-serde = "1.1.1"
cid = "0.10.1"
libipld = "0.16.0"
axum = "0.6.18"

# Enable a small amount of optimization in debug mode
[profile.dev]
Expand Down
3 changes: 2 additions & 1 deletion crates/created-swarm/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ pub fn create_swarm_with_runtime<RT: AquaRuntime>(
listen_on: config.listen_on.clone(),
manager: management_peer_id,
});
let mut node = Node::new(resolved, vm_config, "some version").expect("create node");
let mut node =
Node::new(resolved, vm_config, "some version", "some version").expect("create node");
node.listen(vec![config.listen_on.clone()]).expect("listen");

(
Expand Down
16 changes: 16 additions & 0 deletions crates/system-services/src/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ pub struct Deployer {
config: SystemServicesConfig,
}

#[derive(Debug, Clone)]
pub struct Versions {
pub aqua_ipfs_version: &'static str,
pub trust_graph_version: &'static str,
pub registry_version: &'static str,
pub decider_version: &'static str,
}

impl Deployer {
pub fn new(
services: ParticleAppServices,
Expand All @@ -92,6 +100,14 @@ impl Deployer {
config,
}
}
pub fn versions(&self) -> Versions {
Versions {
aqua_ipfs_version: aqua_ipfs_distro::VERSION,
trust_graph_version: trust_graph_distro::VERSION,
registry_version: registry_distro::VERSION,
decider_version: decider_distro::VERSION,
}
}

async fn deploy_system_service(&self, key: &ServiceKey) -> eyre::Result<()> {
match key {
Expand Down
1 change: 1 addition & 0 deletions crates/system-services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
mod deployer;

pub use deployer::Deployer;
pub use deployer::Versions;
3 changes: 2 additions & 1 deletion nox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ humantime-serde = { workspace = true }
log = { workspace = true }
tracing-log = { version = "0.1.3" }
console-subscriber = { version = "0.1.10", features = ["parking_lot"] }
axum = { version = "0.6.18", features = ["macros"] }
axum = { workspace = true, features = ["macros"] }
itertools = { workspace = true }
eyre = { workspace = true }
base64 = { workspace = true }
Expand All @@ -75,6 +75,7 @@ rand = "0.8.5"
bs58 = { workspace = true }
connected-client = { path = "../crates/connected-client" }
log-utils = { workspace = true }
hyper = "0.14.10"

[[bench]]
name = "network_api_bench"
Expand Down
130 changes: 125 additions & 5 deletions nox/src/http.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::Versions;
use axum::body::Body;
use axum::http::header::CONTENT_TYPE;
use axum::{
Expand All @@ -13,6 +14,7 @@ use prometheus_client::registry::Registry;
use serde_json::json;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::sync::Notify;

async fn handler_404() -> impl IntoResponse {
(StatusCode::NOT_FOUND, "nothing to see here")
Expand Down Expand Up @@ -45,29 +47,147 @@ async fn handle_peer_id(State(state): State<RouteState>) -> Response {
.into_response()
}

async fn handle_versions(State(state): State<RouteState>) -> Response {
let versions = &state.0.versions;
Json(json!({
"node": versions.node_version,
"avm": versions.avm_version,
"spell": versions.spell_version,
"aqua_ipfs": versions.system_service.aqua_ipfs_version,
"trust_graph": versions.system_service.trust_graph_version,
"registry": versions.system_service.registry_version,
"decider": versions.system_service.decider_version,
}))
.into_response()
}

#[derive(Debug, Clone)]
struct RouteState(Arc<Inner>);

#[derive(Debug)]
struct Inner {
registry: Option<Registry>,
peer_id: PeerId,
versions: Versions,
}

pub async fn start_http_endpoint(
listen_addr: SocketAddr,
registry: Option<Registry>,
peer_id: PeerId,
versions: Versions,
notify: Arc<Notify>,
) {
let state = RouteState(Arc::new(Inner { registry, peer_id }));
let state = RouteState(Arc::new(Inner {
registry,
peer_id,
versions,
}));
let app: Router = Router::new()
.route("/metrics", get(handle_metrics))
.route("/peer_id", get(handle_peer_id))
.route("/versions", get(handle_versions))
gurinderu marked this conversation as resolved.
Show resolved Hide resolved
.fallback(handler_404)
.with_state(state);

axum::Server::bind(&listen_addr)
.serve(app.into_make_service())
.await
.expect("Could not make http endpoint")
let server = axum::Server::bind(&listen_addr).serve(app.into_make_service());
notify.notify_one();
server.await.expect("Could not make http endpoint")
}

#[cfg(test)]
mod tests {
use super::*;
use axum::http::Request;
use std::net::{SocketAddr, TcpListener};

fn get_free_tcp_port() -> u16 {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to port 0");
let socket_addr = listener
.local_addr()
.expect("Failed to retrieve local address");
let port = socket_addr.port();
drop(listener);
port
}

fn test_versions() -> Versions {
Versions {
node_version: "node_test_version".to_string(),
avm_version: "avm_test_version".to_string(),
spell_version: "spell_test_version".to_string(),
system_service: system_services::Versions {
aqua_ipfs_version: "aqua_ipfs_test_version",
trust_graph_version: "trust_graph_test_version",
registry_version: "registry_test_version",
decider_version: "decider_test_version",
},
}
}

#[tokio::test]
async fn test_version_route() {
// Create a test server
let port = get_free_tcp_port();
let addr = format!("127.0.0.1:{port}").parse::<SocketAddr>().unwrap();

let notify = Arc::new(Notify::new());
let cloned_notify = notify.clone();
tokio::spawn(async move {
start_http_endpoint(addr, None, PeerId::random(), test_versions(), cloned_notify).await;
});

notify.notified().await;

let client = hyper::Client::new();

let response = client
.request(
Request::builder()
.uri(format!("http://{}/versions", addr))
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
let status = response.status();
let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
assert_eq!(status, StatusCode::OK);
assert_eq!(&body[..], br#"{"node":"node_test_version","avm":"avm_test_version","spell":"spell_test_version","aqua_ipfs":"aqua_ipfs_test_version","trust_graph":"trust_graph_test_version","registry":"registry_test_version","decider":"decider_test_version"}"#);
}

#[tokio::test]
async fn test_peer_id_route() {
// Create a test server
let port = get_free_tcp_port();
let addr = format!("127.0.0.1:{port}").parse::<SocketAddr>().unwrap();
let peer_id = PeerId::random();

let notify = Arc::new(Notify::new());
let cloned_notify = notify.clone();
tokio::spawn(async move {
start_http_endpoint(addr, None, peer_id, test_versions(), cloned_notify).await;
});

notify.notified().await;

let client = hyper::Client::new();

let response = client
.request(
Request::builder()
.uri(format!("http://{}/peer_id", addr))
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
let status = response.status();
let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
assert_eq!(status, StatusCode::OK);
assert_eq!(
&body[..],
format!(r#"{{"peer_id":"{}"}}"#, peer_id).as_bytes()
folex marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
24 changes: 24 additions & 0 deletions nox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,27 @@ pub use kademlia::Command as KademliaCommand;
pub use layers::log_layer;
pub use layers::tokio_console_layer;
pub use layers::tracing_layer;

#[derive(Debug, Clone)]
pub struct Versions {
pub node_version: String,
pub avm_version: String,
pub spell_version: String,
pub system_service: system_services::Versions,
}

impl Versions {
pub fn new(
node_version: String,
avm_version: String,
spell_version: String,
system_service: system_services::Versions,
) -> Self {
Self {
node_version,
avm_version,
spell_version,
system_service,
}
}
}
3 changes: 2 additions & 1 deletion nox/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ async fn start_fluence(config: ResolvedConfig) -> eyre::Result<impl Stoppable> {
let vm_config = vm_config(&config);

let mut node: Box<Node<AVM<_>>> =
Node::new(config, vm_config, VERSION).wrap_err("error create node instance")?;
Node::new(config, vm_config, VERSION, air_interpreter_wasm::VERSION)
.wrap_err("error create node instance")?;
node.listen(listen_addrs).wrap_err("error on listen")?;

let node_exit_outlet = node.start(peer_id).await.wrap_err("node failed to start")?;
Expand Down
23 changes: 18 additions & 5 deletions nox/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ use sorcerer::Sorcerer;
use spell_event_bus::api::{PeerEvent, SpellEventBusApi, TriggerEvent};
use spell_event_bus::bus::SpellEventBus;
use system_services::Deployer;
use tokio::sync::{mpsc, oneshot};
use tokio::sync::{mpsc, oneshot, Notify};
use tokio::task;

use crate::builtins::make_peer_builtin;
use crate::dispatcher::Dispatcher;
use crate::effectors::Effectors;
use crate::Connectivity;
use crate::{Connectivity, Versions};

use super::behaviour::FluenceNetworkBehaviour;
use crate::behaviour::FluenceNetworkBehaviourEvent;
Expand Down Expand Up @@ -91,13 +91,15 @@ pub struct Node<RT: AquaRuntime> {
pub key_manager: KeyManager,

allow_local_addresses: bool,
versions: Versions,
}

impl<RT: AquaRuntime> Node<RT> {
pub fn new(
config: ResolvedConfig,
vm_config: RT::Config,
node_version: &'static str,
air_version: &'static str,
) -> eyre::Result<Box<Self>> {
let key_pair: Keypair = config.node_config.root_key_pair.clone().into();
let transport = config.transport_config.transport;
Expand Down Expand Up @@ -260,7 +262,7 @@ impl<RT: AquaRuntime> Node<RT> {
external_addresses: config.external_addresses(),
node_version: env!("CARGO_PKG_VERSION"),
air_version: air_interpreter_wasm::VERSION,
spell_version,
spell_version: spell_version.clone(),
allowed_binaries,
};
if let Some(m) = metrics_registry.as_mut() {
Expand Down Expand Up @@ -304,6 +306,13 @@ impl<RT: AquaRuntime> Node<RT> {
system_services_config,
);

let versions = Versions::new(
node_version.to_string(),
air_version.to_string(),
spell_version,
system_services_deployer.versions(),
);

Ok(Self::with(
particle_stream,
effects_in,
Expand All @@ -325,6 +334,7 @@ impl<RT: AquaRuntime> Node<RT> {
builtins_peer_id,
key_manager,
allow_local_addresses,
versions,
))
}

Expand Down Expand Up @@ -391,6 +401,7 @@ impl<RT: AquaRuntime> Node<RT> {
builtins_management_peer_id: PeerId,
key_manager: KeyManager,
allow_local_addresses: bool,
versions: Versions,
) -> Box<Self> {
let node_service = Self {
particle_stream,
Expand All @@ -415,6 +426,7 @@ impl<RT: AquaRuntime> Node<RT> {
builtins_management_peer_id,
key_manager,
allow_local_addresses,
versions,
};

Box::new(node_service)
Expand All @@ -441,11 +453,12 @@ impl<RT: AquaRuntime> Node<RT> {
let task_name = format!("node-{peer_id}");
let libp2p_metrics = self.libp2p_metrics;
let allow_local_addresses = self.allow_local_addresses;
let versions = self.versions;

task::Builder::new().name(&task_name.clone()).spawn(async move {
let mut http_server = if let Some(http_listen_addr) = http_listen_addr{
log::info!("Starting http endpoint at {}", http_listen_addr);
start_http_endpoint(http_listen_addr, registry, peer_id).boxed()
start_http_endpoint(http_listen_addr, registry, peer_id, versions, Arc::new(Notify::new())).boxed()
} else {
futures::future::pending().boxed()
};
Expand Down Expand Up @@ -559,7 +572,7 @@ mod tests {
None,
);
let mut node: Box<Node<AVM<_>>> =
Node::new(config, vm_config, "some version").expect("create node");
Node::new(config, vm_config, "some version", "some version").expect("create node");

let listening_address: Multiaddr = "/ip4/127.0.0.1/tcp/7777".parse().unwrap();
node.listen(vec![listening_address.clone()]).unwrap();
Expand Down