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
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 @@ -65,6 +65,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 @@ -85,6 +93,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;
24 changes: 23 additions & 1 deletion 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 Down Expand Up @@ -45,24 +46,45 @@ 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,
) {
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);

Expand Down
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
21 changes: 17 additions & 4 deletions nox/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ 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).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