Skip to content

Commit

Permalink
sui-node: introduce admin interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Jul 18, 2022
1 parent 11d8eb3 commit e4c3b1b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions crates/sui-config/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl<R: ::rand::RngCore + ::rand::CryptoRng> ConfigBuilder<R> {
db_path,
network_address,
metrics_address: utils::available_local_socket_address(),
admin_interface_port: utils::get_available_port(),
json_rpc_address: utils::available_local_socket_address(),
websocket_address: None,
consensus_config: Some(consensus_config),
Expand Down
11 changes: 9 additions & 2 deletions crates/sui-config/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ pub struct NodeConfig {
pub db_path: PathBuf,
#[serde(default = "default_grpc_address")]
pub network_address: Multiaddr,
#[serde(default = "default_metrics_address")]
pub metrics_address: SocketAddr,
#[serde(default = "default_json_rpc_address")]
pub json_rpc_address: SocketAddr,
#[serde(default = "default_websocket_address")]
pub websocket_address: Option<SocketAddr>,

#[serde(default = "default_metrics_address")]
pub metrics_address: SocketAddr,
#[serde(default = "default_admin_interface_port")]
pub admin_interface_port: u16,

#[serde(skip_serializing_if = "Option::is_none")]
pub consensus_config: Option<ConsensusConfig>,

Expand Down Expand Up @@ -59,6 +62,10 @@ fn default_metrics_address() -> SocketAddr {
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9184)
}

pub fn default_admin_interface_port() -> u16 {
1337
}

pub fn default_json_rpc_address() -> SocketAddr {
use std::net::{IpAddr, Ipv4Addr};
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9000)
Expand Down
1 change: 1 addition & 0 deletions crates/sui-config/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl NetworkConfig {
db_path: db_path.join(FULL_NODE_DB_PATH),
network_address: utils::new_network_address(),
metrics_address: utils::available_local_socket_address(),
admin_interface_port: utils::get_available_port(),
json_rpc_address: utils::available_local_socket_address(),
websocket_address: Some(utils::available_local_socket_address()),
consensus_config: None,
Expand Down
48 changes: 48 additions & 0 deletions crates/sui-node/src/admin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use axum::{
extract::Extension,
http::StatusCode,
routing::{get, post},
Router,
};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use telemetry_subscribers::FilterHandle;
use tracing::info;

const LOGGING_ROUTE: &str = "/logging";

pub fn start_admin_server(port: u16, filter_handle: FilterHandle) {
let app = Router::new()
.route(LOGGING_ROUTE, get(get_filter))
.route(LOGGING_ROUTE, post(set_filter))
.layer(Extension(filter_handle));

let socket_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port);
info!("starting admin server on {}", socket_address);

tokio::spawn(async move {
axum::Server::bind(&socket_address)
.serve(app.into_make_service())
.await
.unwrap();
});
}

async fn get_filter(Extension(filter_handle): Extension<FilterHandle>) -> (StatusCode, String) {
match filter_handle.get() {
Ok(filter) => (StatusCode::OK, filter),
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()),
}
}

async fn set_filter(
Extension(filter_handle): Extension<FilterHandle>,
new_filter: String,
) -> (StatusCode, String) {
match filter_handle.update(new_filter) {
Ok(()) => (StatusCode::OK, "".into()),
Err(err) => (StatusCode::BAD_REQUEST, err.to_string()),
}
}
1 change: 1 addition & 0 deletions crates/sui-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use sui_json_rpc::read_api::FullNodeApi;
use sui_json_rpc::read_api::ReadApi;
use sui_types::crypto::PublicKeyBytes;

pub mod admin;
pub mod metrics;

pub struct SuiNode {
Expand Down
9 changes: 6 additions & 3 deletions crates/sui-node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ static GLOBAL: Jemalloc = Jemalloc;
#[tokio::main]
async fn main() -> Result<()> {
// Initialize logging
let _guard = telemetry_subscribers::TelemetryConfig::new(env!("CARGO_BIN_NAME"))
.with_env()
.init();
let (_guard, filter_handle) =
telemetry_subscribers::TelemetryConfig::new(env!("CARGO_BIN_NAME"))
.with_env()
.init();

let args = Args::parse();

Expand Down Expand Up @@ -71,6 +72,8 @@ async fn main() -> Result<()> {
});
}

sui_node::admin::start_admin_server(config.admin_interface_port, filter_handle);

let node = sui_node::SuiNode::start(&config).await?;
node.wait().await?;

Expand Down

0 comments on commit e4c3b1b

Please sign in to comment.