diff --git a/.github/workflows/audit.yml b/.github/workflows/audit.yml index 573cdf3..4671fe1 100644 --- a/.github/workflows/audit.yml +++ b/.github/workflows/audit.yml @@ -11,6 +11,7 @@ jobs: runs-on: zondax-runners steps: - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@stable # - uses: EmbarkStudios/cargo-deny-action@v1 - name: Scan for vulnerabilities run: cargo deny check advisories diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fc5d463..1d3ce07 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -42,7 +42,7 @@ jobs: protoc --version - name: Tests saving blocks run: | - cargo test save_block -- --test-threads=1 + cargo test --test save_block - name: Tests getting blocks run: | - cargo test block_tests + cargo test --test block_tests diff --git a/Cargo.lock b/Cargo.lock index 6bcf7b5..6adcbe0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -107,12 +107,54 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" +[[package]] +name = "anstream" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + [[package]] name = "anstyle" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "anyhow" version = "1.0.75" @@ -812,6 +854,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfaff671f6b22ca62406885ece523383b9b64022e341e53e009a62ebc47a45f2" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] @@ -820,8 +863,22 @@ version = "4.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a216b506622bb1d316cd51328dce24e07bdff4a6128a47c7e7fad11878d5adbb" dependencies = [ + "anstream", "anstyle", "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.41", ] [[package]] @@ -882,6 +939,12 @@ dependencies = [ "thiserror", ] +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "config" version = "0.13.4" @@ -3758,6 +3821,7 @@ dependencies = [ "axum", "axum-prometheus", "bytes", + "clap", "config", "criterion", "futures", @@ -6827,6 +6891,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "uuid" version = "0.8.2" diff --git a/Cargo.toml b/Cargo.toml index e7869d9..ca20668 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,7 @@ tendermint = "0.34.0" tendermint-config = "0.34.0" tendermint-rpc = { version = "0.34.0", features = ["http-client"] } tendermint-proto = "0.34.0" +clap = { version = "4.4.2", features = ["derive", "env"] } [dev-dependencies] criterion = { version = "0.5.1", features = [ diff --git a/src/config.rs b/src/config.rs index 7748002..d05cd01 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ use crate::error::Error; +use clap::{ArgAction, Parser}; use config::{Config, File}; use serde::Deserialize; use std::{env, net::SocketAddr}; @@ -117,6 +118,42 @@ impl Default for DatabaseConfig { } } +#[derive(Debug, Deserialize, clap::Parser)] +pub struct CliSettings { + #[clap(long, env, default_value = "")] + pub log_level: String, + #[clap(long, env, default_value = DEFAULT_NETWORK)] + pub network: String, + #[clap(long, env, default_value = SERVER_ADDR)] + pub server_serve_at: String, + #[clap(long, env, default_value_t = SERVER_PORT)] + pub server_port: u16, + #[clap(long, env, default_value = "localhost")] + pub database_host: String, + #[clap(long, env, default_value = "postgres")] + pub database_user: String, + #[clap(long, env, default_value = "wow")] + pub database_password: String, + #[clap(long, env, default_value = "blockchain")] + pub database_dbname: String, + #[clap(long, env)] + pub database_connection_timeout: Option, + #[clap(long, env, default_value = TENDERMINT_ADDR)] + pub indexer_tendermint_addr: String, + #[clap(long, env, default_value_t = INDEXER_PORT)] + pub indexer_port: u16, + #[clap(long, env, action=ArgAction::SetFalse)] + pub jaeger_enable: bool, + #[clap(long, env, default_value = JAEGER_HOST)] + pub jaeger_host: String, + #[clap(long, env, default_value_t = JAEGER_PORT)] + pub jaeger_port: u16, + #[clap(long, env, default_value = PROMETHEUS_HOST)] + pub prometheus_host: String, + #[clap(long, env, default_value_t = PROMETHEUS_PORT)] + pub prometheus_port: u16, +} + #[derive(Debug, Deserialize)] pub struct Settings { pub log_level: String, @@ -142,6 +179,39 @@ impl Default for Settings { } } +impl From for Settings { + fn from(value: CliSettings) -> Self { + Self { + log_level: value.log_level, + network: value.network, + database: DatabaseConfig { + host: value.database_host, + user: value.database_user, + password: value.database_password, + dbname: value.database_dbname, + connection_timeout: value.database_connection_timeout, + }, + server: ServerConfig { + serve_at: value.server_serve_at, + port: value.server_port, + }, + indexer: IndexerConfig { + tendermint_addr: value.indexer_tendermint_addr, + port: value.indexer_port, + }, + jaeger: JaegerConfig { + enable: value.jaeger_enable, + host: value.jaeger_host, + port: value.jaeger_port, + }, + prometheus: PrometheusConfig { + host: value.prometheus_host, + port: value.prometheus_port, + }, + } + } +} + impl Settings { #[instrument(level = "debug")] pub fn new() -> Result { @@ -164,7 +234,10 @@ impl Settings { return Ok(settings); } - Ok(Self::default()) + let cli_settings = CliSettings::parse(); + let settings = Settings::from(cli_settings); + + Ok(settings) } pub fn server_config(&self) -> &ServerConfig {