From dcf4795cb3c440b0e9a8d45bcc48010c6236c066 Mon Sep 17 00:00:00 2001 From: Piotr Stachyra Date: Mon, 25 Nov 2024 09:24:40 +0100 Subject: [PATCH 1/4] Get genesis block data at mina_mesh start --- src/config.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index 1243e9e..6dc0e16 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,10 +3,15 @@ use std::time::Duration; use anyhow::Result; use clap::{Args, Parser}; use coinbase_mesh::models::BlockIdentifier; +use cynic::QueryBuilder; use dashmap::DashMap; use sqlx::postgres::PgPoolOptions; -use crate::{graphql::GraphQLClient, util::default_mina_proxy_url, MinaMesh, MinaMeshError}; +use crate::{ + graphql::{self, GraphQLClient}, + util::default_mina_proxy_url, + MinaMesh, MinaMeshError, +}; #[derive(Debug, Args)] pub struct MinaMeshConfig { @@ -56,9 +61,14 @@ impl MinaMeshConfig { if self.proxy_url.is_empty() { return Err(MinaMeshError::GraphqlUriNotSet); } + tracing::info!("Connecting to Mina GraphQL endpoint at {}", self.proxy_url); + let graphql_client = GraphQLClient::new(self.proxy_url.to_owned()); + let res = graphql_client.send(graphql::QueryGenesisBlockIdentifier::build(())).await?; + tracing::debug!("Genesis block identifier: {}", res.genesis_block.protocol_state.consensus_state.block_height.0); + tracing::debug!("Genesis block state hash: {}", res.genesis_block.state_hash.0); Ok(MinaMesh { - graphql_client: GraphQLClient::new(self.proxy_url.to_owned()), + graphql_client, pg_pool: PgPoolOptions::new() .max_connections(self.max_db_pool_size) .min_connections(0) @@ -66,8 +76,8 @@ impl MinaMeshConfig { .connect(self.archive_database_url.as_str()) .await?, genesis_block_identifier: BlockIdentifier::new( - self.genesis_block_identifier_height, - self.genesis_block_identifier_state_hash.to_owned(), + res.genesis_block.protocol_state.consensus_state.block_height.0.parse::().unwrap(), + res.genesis_block.state_hash.0.clone(), ), search_tx_optimized: self.use_search_tx_optimizations, cache: DashMap::new(), From 9e6fbe5a8a1b5f8c0dbdc84f68d67b1235570a44 Mon Sep 17 00:00:00 2001 From: Piotr Stachyra Date: Mon, 25 Nov 2024 10:03:36 +0100 Subject: [PATCH 2/4] remove fetch-genesis-block command --- .env.example | 3 +- .env.example.devnet | 2 -- src/bin/mina-mesh.rs | 4 +-- src/commands.rs | 2 -- .../fetch_genesis_block_identifier.rs | 28 ------------------- src/config.rs | 5 ---- tests/error.rs | 6 ---- 7 files changed, 2 insertions(+), 48 deletions(-) delete mode 100644 src/commands/fetch_genesis_block_identifier.rs diff --git a/.env.example b/.env.example index 2b28541..8498262 100644 --- a/.env.example +++ b/.env.example @@ -1,8 +1,7 @@ DATABASE_URL=postgres://mina:whatever@localhost:5432/archive # TODO: we only need this one during development MINAMESH_ARCHIVE_DATABASE_URL=postgres://mina:whatever@localhost:5432/archive -MINAMESH_GENESIS_BLOCK_IDENTIFIER_STATE_HASH=3NK4BpDSekaqsG6tx8Nse2zJchRft2JpnbvMiog55WCr5xJZaKeP -MINAMESH_GENESIS_BLOCK_IDENTIFIER_HEIGHT=359605 +MINAMESH_PROXY_URL=https://devnet.minaprotocol.network/graphql RUST_LOG=debug,error,mina_mesh=info RUST_ENV=production diff --git a/.env.example.devnet b/.env.example.devnet index 61bd5c0..f3dfa86 100644 --- a/.env.example.devnet +++ b/.env.example.devnet @@ -1,7 +1,5 @@ MINAMESH_ARCHIVE_DATABASE_URL=postgres://mina:whatever@localhost:5432/archive MINAMESH_PROXY_URL=https://devnet.minaprotocol.network/graphql -MINAMESH_GENESIS_BLOCK_IDENTIFIER_STATE_HASH=3NL93SipJfAMNDBRfQ8Uo8LPovC74mnJZfZYB5SK7mTtkL72dsPx -MINAMESH_GENESIS_BLOCK_IDENTIFIER_HEIGHT=296372 RUST_LOG=debug,error,mina_mesh=info diff --git a/src/bin/mina-mesh.rs b/src/bin/mina-mesh.rs index fec0112..ee7572a 100644 --- a/src/bin/mina-mesh.rs +++ b/src/bin/mina-mesh.rs @@ -3,13 +3,12 @@ use anyhow::Result; use clap::Parser; -use mina_mesh::{FetchGenesisBlockIdentifierCommand, SearchTxOptimizationsCommand, ServeCommand}; +use mina_mesh::{SearchTxOptimizationsCommand, ServeCommand}; #[derive(Debug, Parser)] #[command(name = "mina-mesh", version, about = "A Mesh-compliant Server for Mina", propagate_version = true, author)] enum Command { Serve(ServeCommand), - FetchGenesisBlockIdentifier(FetchGenesisBlockIdentifierCommand), SearchTxOptimizations(SearchTxOptimizationsCommand), } @@ -18,7 +17,6 @@ async fn main() -> Result<()> { dotenv::dotenv().ok(); match Command::parse() { Command::Serve(cmd) => cmd.run().await, - Command::FetchGenesisBlockIdentifier(cmd) => cmd.run().await, Command::SearchTxOptimizations(cmd) => cmd.run().await, } } diff --git a/src/commands.rs b/src/commands.rs index 890b5fa..e40ad13 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,7 +1,5 @@ -mod fetch_genesis_block_identifier; mod search_tx_optimizations; mod serve; -pub use fetch_genesis_block_identifier::*; pub use search_tx_optimizations::*; pub use serve::*; diff --git a/src/commands/fetch_genesis_block_identifier.rs b/src/commands/fetch_genesis_block_identifier.rs deleted file mode 100644 index f2e13d0..0000000 --- a/src/commands/fetch_genesis_block_identifier.rs +++ /dev/null @@ -1,28 +0,0 @@ -use anyhow::{bail, Result}; -use clap::Args; -use cynic::{http::ReqwestExt, QueryBuilder}; - -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, env = "MINAMESH_PROXY_URL", default_value_t = default_mina_proxy_url())] - proxy_url: String, -} - -impl FetchGenesisBlockIdentifierCommand { - pub async fn run(&self) -> Result<()> { - let client = reqwest::Client::new(); - let result = client.post(self.proxy_url.to_owned()).run_graphql(QueryGenesisBlockIdentifier::build(())).await?; - 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_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"); - } - Ok(()) - } -} diff --git a/src/config.rs b/src/config.rs index 6dc0e16..b0560d5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -33,11 +33,6 @@ pub struct MinaMeshConfig { #[arg(long, env = "MINAMESH_DB_POOL_IDLE_TIMEOUT", default_value_t = 1)] pub db_pool_idle_timeout: u64, - #[arg(long, env = "MINAMESH_GENESIS_BLOCK_IDENTIFIER_HEIGHT")] - pub genesis_block_identifier_height: i64, - #[arg(long, env = "MINAMESH_GENESIS_BLOCK_IDENTIFIER_STATE_HASH")] - pub genesis_block_identifier_state_hash: String, - /// Whether to use optimizations for searching transactions. Requires the /// optimizations to be enabled via the `mina-mesh search-tx-optimizations` /// command. diff --git a/tests/error.rs b/tests/error.rs index 37329b0..72aca41 100644 --- a/tests/error.rs +++ b/tests/error.rs @@ -125,13 +125,9 @@ async fn test_conversion_from_cynic_reqwest_error() -> Result<(), MinaMeshError> archive_database_url: env::var("MINAMESH_ARCHIVE_DATABASE_URL").unwrap(), max_db_pool_size: 10, db_pool_idle_timeout: 1, - genesis_block_identifier_height: 1, - genesis_block_identifier_state_hash: "test".to_string(), use_search_tx_optimizations: false, } .to_mina_mesh() - .await? - .network_list() .await; // Assert that the error matches MinaMeshError::GraphqlMinaQuery assert!(matches!(res, Err(MinaMeshError::GraphqlMinaQuery(_)))); @@ -154,8 +150,6 @@ async fn test_graphql_uri_not_set_error() -> Result<(), MinaMeshError> { archive_database_url: env::var("MINAMESH_ARCHIVE_DATABASE_URL").unwrap(), max_db_pool_size: 10, db_pool_idle_timeout: 1, - genesis_block_identifier_height: 1, - genesis_block_identifier_state_hash: "test".to_string(), use_search_tx_optimizations: false, } .to_mina_mesh() From 659b17e79c1cb25e96542f8756115cf7ec185beb Mon Sep 17 00:00:00 2001 From: Piotr Stachyra Date: Mon, 25 Nov 2024 10:17:54 +0100 Subject: [PATCH 3/4] test adn readme update --- README.md | 6 ------ tests/validate_network.rs | 11 +++++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9c1db62..0becdbf 100644 --- a/README.md +++ b/README.md @@ -30,12 +30,6 @@ The server depends on several environment variables. - `MINAMESH_PROXY_URL`: a Mina proxy (GraphQL) endpoint. The default is `https://mainnet.minaprotocol.network/graphql`. - `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. - - ```sh - mina-mesh fetch-genesis-block-identifier >> .env - ``` ## Instantiate the Server diff --git a/tests/validate_network.rs b/tests/validate_network.rs index 2d3d5ed..e7ad040 100644 --- a/tests/validate_network.rs +++ b/tests/validate_network.rs @@ -1,6 +1,17 @@ use anyhow::Result; use mina_mesh::{models::NetworkIdentifier, CacheKey::NetworkId, MinaMeshConfig, MinaMeshError}; +#[tokio::test] +async fn genesis_block_identifier() -> Result<()> { + let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?; + assert_eq!(mina_mesh.genesis_block_identifier.index, 296372, "Devnet genesis block index does not match"); + assert_eq!( + mina_mesh.genesis_block_identifier.hash, "3NL93SipJfAMNDBRfQ8Uo8LPovC74mnJZfZYB5SK7mTtkL72dsPx", + "Devnet genesis block hash does not match" + ); + Ok(()) +} + #[tokio::test] async fn validate_network_ok() -> Result<()> { let mina_mesh = MinaMeshConfig::from_env().to_mina_mesh().await?; From 88fe3b63adff7363a320355816d4a98b8d42fef2 Mon Sep 17 00:00:00 2001 From: Piotr Stachyra Date: Mon, 25 Nov 2024 10:42:03 +0100 Subject: [PATCH 4/4] adjust block id fetching --- src/config.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index b0560d5..fcb20d0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -59,8 +59,10 @@ impl MinaMeshConfig { tracing::info!("Connecting to Mina GraphQL endpoint at {}", self.proxy_url); let graphql_client = GraphQLClient::new(self.proxy_url.to_owned()); let res = graphql_client.send(graphql::QueryGenesisBlockIdentifier::build(())).await?; - tracing::debug!("Genesis block identifier: {}", res.genesis_block.protocol_state.consensus_state.block_height.0); - tracing::debug!("Genesis block state hash: {}", res.genesis_block.state_hash.0); + let block_height = res.genesis_block.protocol_state.consensus_state.block_height.0.parse::()?; + let state_hash = res.genesis_block.state_hash.0.clone(); + tracing::debug!("Genesis block identifier: {}", block_height); + tracing::debug!("Genesis block state hash: {}", state_hash); Ok(MinaMesh { graphql_client, @@ -70,10 +72,7 @@ impl MinaMeshConfig { .idle_timeout(Duration::from_secs(self.db_pool_idle_timeout)) .connect(self.archive_database_url.as_str()) .await?, - genesis_block_identifier: BlockIdentifier::new( - res.genesis_block.protocol_state.consensus_state.block_height.0.parse::().unwrap(), - res.genesis_block.state_hash.0.clone(), - ), + genesis_block_identifier: BlockIdentifier::new(block_height, state_hash), search_tx_optimized: self.use_search_tx_optimizations, cache: DashMap::new(), cache_ttl: Duration::from_secs(300),