Skip to content

Commit

Permalink
fix env var / args handling
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay committed Oct 3, 2024
1 parent 56ba99e commit 6ae9994
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 55 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 5 additions & 12 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 4 additions & 4 deletions src/commands/fetch_genesis_block_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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");
}
Expand Down
50 changes: 17 additions & 33 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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<MinaMesh> {
Ok(MinaMesh {
graphql_client: GraphQLClient::new(self.proxy_url.to_owned()),
Expand All @@ -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()
}
4 changes: 4 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ impl ToString for Wrapper<&PartialBlockIdentifier> {
}
}
}

pub fn default_mina_proxy_url() -> String {
"https://mainnet.minaprotocol.network/graphql".to_string()
}
2 changes: 1 addition & 1 deletion tests/account_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions tests/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<FuturesUnordered<_>>();
let mut maybe_prev: Option<Option<BlockMetadata>> = None;
Expand Down Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion tests/network_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down

0 comments on commit 6ae9994

Please sign in to comment.