From 4a8c2d21e3e0c356023a4a06642b9521e8936cec Mon Sep 17 00:00:00 2001 From: mario4tier Date: Fri, 23 Dec 2022 23:33:47 -0500 Subject: [PATCH] (#12) dtp-server,telemetry-subscribers,clap,colored --- Cargo.lock | 51 ++++++++++----- Cargo.toml | 12 +++- crates/dtp-dev-app/src/main.rs | 31 ---------- crates/{dtp-dev-app => dtp-server}/Cargo.toml | 11 +++- crates/dtp-server/src/main.rs | 62 +++++++++++++++++++ 5 files changed, 118 insertions(+), 49 deletions(-) delete mode 100644 crates/dtp-dev-app/src/main.rs rename crates/{dtp-dev-app => dtp-server}/Cargo.toml (57%) create mode 100644 crates/dtp-server/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 43e6494..113796a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2319,26 +2319,29 @@ dependencies = [ ] [[package]] -name = "dtp-dev-app" +name = "dtp-sdk" version = "0.1.0" dependencies = [ "anyhow", "cargo-husky", - "dtp-sdk", + "dtp-core", + "dtp-test-helper", + "serial_test", "sui-sdk", "tokio", ] [[package]] -name = "dtp-sdk" +name = "dtp-server" version = "0.1.0" dependencies = [ "anyhow", "cargo-husky", - "dtp-core", - "dtp-test-helper", - "serial_test", + "clap 3.2.23", + "colored", + "dtp-sdk", "sui-sdk", + "telemetry-subscribers 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio", ] @@ -5226,7 +5229,7 @@ dependencies = [ "prometheus", "rand 0.8.5", "serde 1.0.151", - "telemetry-subscribers", + "telemetry-subscribers 0.2.0 (git+https://github.com/MystenLabs/sui?branch=devnet)", "thiserror", "tokio", "tracing", @@ -5300,7 +5303,7 @@ dependencies = [ "prometheus", "rand 0.8.5", "serde 1.0.151", - "telemetry-subscribers", + "telemetry-subscribers 0.2.0 (git+https://github.com/MystenLabs/sui?branch=devnet)", "thiserror", "tokio", "tokio-util 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -5377,7 +5380,7 @@ dependencies = [ "prometheus", "rand 0.8.5", "reqwest", - "telemetry-subscribers", + "telemetry-subscribers 0.2.0 (git+https://github.com/MystenLabs/sui?branch=devnet)", "thiserror", "tokio", "tokio-stream", @@ -5427,7 +5430,7 @@ dependencies = [ "roaring", "serde 1.0.151", "tap", - "telemetry-subscribers", + "telemetry-subscribers 0.2.0 (git+https://github.com/MystenLabs/sui?branch=devnet)", "thiserror", "tokio", "tokio-util 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -5532,7 +5535,7 @@ dependencies = [ "rand 0.8.5", "serde 1.0.151", "tap", - "telemetry-subscribers", + "telemetry-subscribers 0.2.0 (git+https://github.com/MystenLabs/sui?branch=devnet)", "tokio", "tokio-stream", "tokio-util 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -8430,7 +8433,7 @@ dependencies = [ "sui-storage", "sui-types", "tap", - "telemetry-subscribers", + "telemetry-subscribers 0.2.0 (git+https://github.com/MystenLabs/sui?branch=devnet)", "tempfile", "thiserror", "tokio", @@ -8737,7 +8740,7 @@ dependencies = [ "msim", "sui-framework", "sui-types", - "telemetry-subscribers", + "telemetry-subscribers 0.2.0 (git+https://github.com/MystenLabs/sui?branch=devnet)", "tracing", "workspace-hack", ] @@ -8767,7 +8770,7 @@ dependencies = [ "sui-json-rpc-types", "sui-types", "tap", - "telemetry-subscribers", + "telemetry-subscribers 0.2.0 (git+https://github.com/MystenLabs/sui?branch=devnet)", "tempfile", "tokio", "tokio-stream", @@ -8954,6 +8957,26 @@ dependencies = [ "target-lexicon", ] +[[package]] +name = "telemetry-subscribers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2a01f8161eb6d52700ec8f82a9432b425db5d1729ac747b52d34dfc9f4f0902" +dependencies = [ + "crossterm 0.25.0", + "once_cell", + "opentelemetry 0.18.0", + "opentelemetry-jaeger", + "prometheus", + "tokio", + "tracing", + "tracing-appender", + "tracing-bunyan-formatter", + "tracing-chrome", + "tracing-opentelemetry", + "tracing-subscriber 0.3.15", +] + [[package]] name = "telemetry-subscribers" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 28aff66..453d413 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "2" members = [ "crates/dtp-core", "crates/dtp-sdk", - "crates/dtp-dev-app", + "crates/dtp-server", "crates/dtp-test-helper", ] @@ -13,6 +13,7 @@ edition = "2021" homepage = "https://dtp.dev" license = "Apache-2.0" documentation = "https://docs.dtp.dev" +default-run = "dtp-server" [workspace.dependencies] # By default, DTP is built using the remote git Sui (devnet branch). @@ -36,8 +37,15 @@ sui-json-rpc-types = { git = "https://github.com/MystenLabs/sui", branch = "devn sui-keys = { git = "https://github.com/MystenLabs/sui", branch = "devnet", package = "sui-keys" } sui-types = { git = "https://github.com/MystenLabs/sui", branch = "devnet", package = "sui-types" } + tokio = { version = "1.22.0", features = ["full"] } -anyhow = "1.0.64" +telemetry-subscribers = { version = "0.2.0" } + +anyhow = "1.0.68" +clap = { version = "3.2.22", features = [ + "derive", +] } # No upgrade to v4 until color are back. +colored = { version = "2.0.0" } [workspace.dependencies.serial_test] # Intended to be used as [dev-dependencies] only. diff --git a/crates/dtp-dev-app/src/main.rs b/crates/dtp-dev-app/src/main.rs deleted file mode 100644 index 63de8c8..0000000 --- a/crates/dtp-dev-app/src/main.rs +++ /dev/null @@ -1,31 +0,0 @@ -use std::str::FromStr; -use sui_sdk::types::base_types::SuiAddress; -use sui_sdk::SuiClient; - -use dtp_sdk::{Host, Localhost, DTP}; - -#[tokio::main] -async fn main() -> Result<(), anyhow::Error> { - // Test localnet with the pre-funded wallet. - let sui = SuiClient::new("http://0.0.0.0:9000", None, None).await?; - let address = SuiAddress::from_str("0xcfed50a652b8fce7a7917a8a736a7c2b1d646ba2")?; - let objects = sui.read_api().get_objects_owned_by_address(address).await?; - println!("{:?}", objects); - - let own_address = SuiAddress::from_str("0xcfed50a652b8fce7a7917a8a736a7c2b1d646ba2")?; - - let mut dtp: DTP = DTP::new(own_address, None).await?; - dtp.add_rpc("http://0.0.0.0:9000", None, None).await?; - - let peer_address = SuiAddress::from_str("0xcfed50a652b8fce7a7917a8a736a7c2b1d646ba2")?; - - let peer_node: Host = dtp.get_host_by_address(peer_address).await?; - let own_node: Localhost = dtp.get_localhost_by_address(own_address).await?; - - println!( - "Ping result is {:?}", - dtp.ping(&own_node, &peer_node).await? - ); - - Ok(()) -} diff --git a/crates/dtp-dev-app/Cargo.toml b/crates/dtp-server/Cargo.toml similarity index 57% rename from crates/dtp-dev-app/Cargo.toml rename to crates/dtp-server/Cargo.toml index b4906bc..423f25c 100644 --- a/crates/dtp-dev-app/Cargo.toml +++ b/crates/dtp-server/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "dtp-dev-app" +name = "dtp-server" version.workspace = true edition.workspace = true homepage.workspace = true @@ -10,8 +10,15 @@ documentation.workspace = true tokio.workspace = true anyhow.workspace = true sui-sdk.workspace = true +clap.workspace = true +colored.workspace = true +telemetry-subscribers.workspace = true + +# tracing.workspace = true + +# tracing-subscriber = { version = "0.3.16" } dtp-sdk = { path = "../dtp-sdk/" } [dev-dependencies] -cargo-husky.workspace = true \ No newline at end of file +cargo-husky.workspace = true diff --git a/crates/dtp-server/src/main.rs b/crates/dtp-server/src/main.rs new file mode 100644 index 0000000..8cd873d --- /dev/null +++ b/crates/dtp-server/src/main.rs @@ -0,0 +1,62 @@ +// DTP Server executable +// +// Typically intended to be deployed as a daemon (Service) +// +use clap::*; +use colored::Colorize; +use std::path::PathBuf; +use telemetry_subscribers; + +#[allow(clippy::large_enum_variant)] +#[derive(Parser)] +#[clap( + name = "dtp-server", + about = "Server for Decentralized Transport Protocol over Sui network", + rename_all = "kebab-case", + author, + version +)] +pub enum Command { + #[clap(name = "localnet")] + Localnet { + #[clap(long = "path")] + path: Option, + }, +} + +impl Command { + pub async fn execute(self) -> Result<(), anyhow::Error> { + match self { + Command::Localnet { path } => { + if let Some(x) = path { + println!("{}", x.into_os_string().into_string().unwrap()); + } else { + println!("Path not provided"); + } + Ok(()) + } + } + } +} + +#[tokio::main] +async fn main() { + #[cfg(windows)] + colored::control::set_virtual_terminal(true).unwrap(); + + // TODO Socket for external dtp CLI binary (this is the server not the CLI!) + // TODO Look into https://crates.io/crates/sentry-tracing for bail/panic logging. + let _guard = telemetry_subscribers::TelemetryConfig::new("dtp-server") + .with_env() + .init(); + + let cmd: Command = Command::parse(); + + match cmd.execute().await { + Ok(_) => (), + Err(err) => { + println!("{}", err.to_string().red()); + std::process::exit(1); + } + } +}