From 6ae99949e386f37c5e17bcb043a93978b3cec510 Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Thu, 3 Oct 2024 11:42:43 -0400 Subject: [PATCH] fix env var / args handling --- .env.example | 5 +- .github/workflows/checks.yaml | 1 + .vscode/launch.json | 17 ++----- README.md | 2 +- .../fetch_genesis_block_identifier.rs | 8 +-- src/config.rs | 50 +++++++------------ src/util.rs | 4 ++ tests/account_balance.rs | 2 +- tests/block.rs | 4 +- tests/network_list.rs | 2 +- 10 files changed, 40 insertions(+), 55 deletions(-) diff --git a/.env.example b/.env.example index 2335ae7..2097017 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,6 @@ -DATABASE_URL=postgres://mina:whatever@localhost:5432/archive +MINAMESH_ARCHIVE_DATABASE_URL=postgres://mina:whatever@localhost:5432/archive +MINAMESH_GENESIS_BLOCK_IDENTIFIER_STATE_HASH=3NK4BpDSekaqsG6tx8Nse2zJchRft2JpnbvMiog55WCr5xJZaKeP +MINAMESH_GENESIS_BLOCK_IDENTIFIER_HEIGHT=359605 + RUST_LOG=debug,error,mina_mesh=info RUST_ENV=production diff --git a/.github/workflows/checks.yaml b/.github/workflows/checks.yaml index 731d913..4a5dc91 100644 --- a/.github/workflows/checks.yaml +++ b/.github/workflows/checks.yaml @@ -56,6 +56,7 @@ jobs: key: ${{ runner.os }}-test-${{ hashFiles('**/Cargo.lock') }} - name: Setup run: | + cp .env.example .env just get-mainnet-archive-db just pg just wait-for-pg diff --git a/.vscode/launch.json b/.vscode/launch.json index a1209aa..884d3ca 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,24 +4,17 @@ { "type": "lldb", "request": "launch", - "name": "Debug unit tests in library", + "name": "Debug Cargo tests", "cargo": { - "args": [ - "test", - "--no-run", - "--lib", - "--package=${workspaceFolderBasename}" - ], - "filter": { - "name": "${workspaceFolderBasename}", - "kind": "lib" - } + "args": ["test", "--no-run", "--lib"] }, "args": ["--nocapture"], "cwd": "${workspaceFolder}", "env": { "RUST_LOG": "debug" - } + }, + "envFile": "${workspaceFolder}/.env", + "console": "integratedTerminal" } ] } diff --git a/README.md b/README.md index aca1198..86f0dd6 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ The server depends on several environment variables. - `MINAMESH_PROXY_URL`: a Mina proxy (GraphQL) endpoint. The default is `https://mainnet.minaprotocol.network/graphql`. -- `MINAMESH_DATABASE_URL`: a connection string referencing a Mina archive database. +- `MINAMESH_ARCHIVE_DATABASE_URL`: a connection string referencing a Mina archive database. - `MINAMESH_GENESIS_BLOCK_IDENTIFIER_HEIGHT` and `MINAMESH_GENESIS_BLOCK_IDENTIFIER_STATE_HASH`: we can retrieve these using the `fetch-genesis-block-identifier` command. diff --git a/src/commands/fetch_genesis_block_identifier.rs b/src/commands/fetch_genesis_block_identifier.rs index 7eacce3..f2e13d0 100644 --- a/src/commands/fetch_genesis_block_identifier.rs +++ b/src/commands/fetch_genesis_block_identifier.rs @@ -2,12 +2,12 @@ use anyhow::{bail, Result}; use clap::Args; use cynic::{http::ReqwestExt, QueryBuilder}; -use crate::graphql::QueryGenesisBlockIdentifier; +use crate::{graphql::QueryGenesisBlockIdentifier, util::default_mina_proxy_url}; #[derive(Debug, Args)] #[command(about = "Retrieve the genesis block identifier via a proxy node GraphQL endpoint.")] pub struct FetchGenesisBlockIdentifierCommand { - #[arg(long, short = 'n', default_value = "https://mainnet.minaprotocol.network/graphql")] + #[arg(long, env = "MINAMESH_PROXY_URL", default_value_t = default_mina_proxy_url())] proxy_url: String, } @@ -18,8 +18,8 @@ impl FetchGenesisBlockIdentifierCommand { if let Some(inner) = result.data { let genesis_block_hash = inner.genesis_block.state_hash.0; let genesis_block_index = inner.genesis_block.protocol_state.consensus_state.block_height.0; - println!("MINAMESH_GENESIS_BLOCK_HASH = {}", genesis_block_hash); - println!("MINAMESH_GENESIS_BLOCK_INDEX = {}", genesis_block_index); + println!("MINAMESH_GENESIS_BLOCK_IDENTIFIER_STATE_HASH = {genesis_block_hash}"); + println!("MINAMESH_GENESIS_BLOCK_IDENTIFIER_HEIGHT = {genesis_block_index}"); } else { bail!("No genesis block identifier found in the response"); } diff --git a/src/config.rs b/src/config.rs index 2a7de9e..0aac911 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,23 +1,34 @@ use anyhow::Result; -use clap::Args; +use clap::{Args, Parser}; use mesh::models::BlockIdentifier; use sqlx::PgPool; -use crate::{graphql::GraphQLClient, MinaMesh}; +use crate::{graphql::GraphQLClient, util::default_mina_proxy_url, MinaMesh}; #[derive(Debug, Args)] pub struct MinaMeshConfig { - #[arg(long, env = "MINA_PROXY_URL", default_value_t = mina_proxy_url())] + #[arg(long, env = "MINAMESH_PROXY_URL", default_value_t = default_mina_proxy_url())] pub proxy_url: String, - #[arg(long, env = "MINA_ARCHIVE_DATABASE_URL", default_value_t = database_url())] + #[arg(long, env = "MINAMESH_ARCHIVE_DATABASE_URL")] pub archive_database_url: String, - #[arg(long, env = "MINA_GENESIS_BLOCK_IDENTIFIER_HEIGHT", default_value_t = genesis_block_identifier_height())] + #[arg(long, env = "MINAMESH_GENESIS_BLOCK_IDENTIFIER_HEIGHT")] pub genesis_block_identifier_height: i64, - #[arg(long, env = "MINA_GENESIS_BLOCK_IDENTIFIER_STATE_HASH", default_value_t = genesis_block_identifier_state_hash())] + #[arg(long, env = "MINAMESH_GENESIS_BLOCK_IDENTIFIER_STATE_HASH")] pub genesis_block_identifier_state_hash: String, } impl MinaMeshConfig { + pub fn from_env() -> Self { + dotenv::dotenv().ok(); + return MinaMeshConfigParser::parse().config; + + #[derive(Parser)] + struct MinaMeshConfigParser { + #[command(flatten)] + config: MinaMeshConfig, + } + } + pub async fn to_mina_mesh(self) -> Result { Ok(MinaMesh { graphql_client: GraphQLClient::new(self.proxy_url.to_owned()), @@ -29,30 +40,3 @@ impl MinaMeshConfig { }) } } - -impl Default for MinaMeshConfig { - fn default() -> Self { - Self { - proxy_url: mina_proxy_url(), - archive_database_url: database_url(), - genesis_block_identifier_height: genesis_block_identifier_height(), - genesis_block_identifier_state_hash: genesis_block_identifier_state_hash(), - } - } -} - -fn mina_proxy_url() -> String { - "https://mainnet.minaprotocol.network/graphql".to_string() -} - -fn database_url() -> String { - "postgres://mina:whatever@localhost:5432/archive".to_string() -} - -fn genesis_block_identifier_height() -> i64 { - 359605 -} - -fn genesis_block_identifier_state_hash() -> String { - "3NK4BpDSekaqsG6tx8Nse2zJchRft2JpnbvMiog55WCr5xJZaKeP".to_string() -} diff --git a/src/util.rs b/src/util.rs index 27a233b..3b001cd 100644 --- a/src/util.rs +++ b/src/util.rs @@ -47,3 +47,7 @@ impl ToString for Wrapper<&PartialBlockIdentifier> { } } } + +pub fn default_mina_proxy_url() -> String { + "https://mainnet.minaprotocol.network/graphql".to_string() +} diff --git a/tests/account_balance.rs b/tests/account_balance.rs index 687da1e..f475c23 100644 --- a/tests/account_balance.rs +++ b/tests/account_balance.rs @@ -8,7 +8,7 @@ use mina_mesh::{ #[tokio::test] async fn responses() -> Result<()> { - let mina_mesh = MinaMeshConfig::default().to_mina_mesh().await?; + let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?; let futures: Vec<_> = [ // cspell:disable "B62qmjJeM4Fd4FVghfhgwoE1fkEexK2Rre8WYKMnbxVwB5vtKUwvgMv", diff --git a/tests/block.rs b/tests/block.rs index c154a2e..67eac9f 100644 --- a/tests/block.rs +++ b/tests/block.rs @@ -6,7 +6,7 @@ use mina_mesh::{BlockMetadata, MinaMeshConfig, PartialBlockIdentifier}; #[tokio::test] async fn specified() -> Result<()> { - let mina_mesh = MinaMeshConfig::default().to_mina_mesh().await?; + let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?; let mut metadata_futures = specified_identifiers().iter().map(|item| mina_mesh.block_metadata(item)).collect::>(); let mut maybe_prev: Option> = None; @@ -38,7 +38,7 @@ fn specified_identifiers() -> &'static [PartialBlockIdentifier; 3] { #[tokio::test] async fn unspecified() -> Result<()> { - let mina_mesh = MinaMeshConfig::default().to_mina_mesh().await?; + let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?; let result = mina_mesh.block_metadata(&PartialBlockIdentifier { hash: None, index: None }).await; assert!(result.is_ok()); Ok(()) diff --git a/tests/network_list.rs b/tests/network_list.rs index 63d5712..324b87c 100644 --- a/tests/network_list.rs +++ b/tests/network_list.rs @@ -5,7 +5,7 @@ use mina_mesh::MinaMeshConfig; #[tokio::test] async fn mainnet_test() -> Result<()> { // Create a MinaMesh instance using the default configuration - let mina_mesh = MinaMeshConfig::default().to_mina_mesh().await?; + let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?; // Call the network_list function let result = mina_mesh.network_list().await?;