Skip to content

Commit

Permalink
Fetch genesis block information automatically (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-iohk authored Nov 25, 2024
1 parent 124e271 commit 4c85cf9
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 60 deletions.
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions .env.example.devnet
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions src/bin/mina-mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand All @@ -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,
}
}
2 changes: 0 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -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::*;
28 changes: 0 additions & 28 deletions src/commands/fetch_genesis_block_identifier.rs

This file was deleted.

26 changes: 15 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,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.
Expand All @@ -56,19 +56,23 @@ 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?;
let block_height = res.genesis_block.protocol_state.consensus_state.block_height.0.parse::<i64>()?;
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: GraphQLClient::new(self.proxy_url.to_owned()),
graphql_client,
pg_pool: PgPoolOptions::new()
.max_connections(self.max_db_pool_size)
.min_connections(0)
.idle_timeout(Duration::from_secs(self.db_pool_idle_timeout))
.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(),
),
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),
Expand Down
6 changes: 0 additions & 6 deletions tests/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_))));
Expand All @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions tests/validate_network.rs
Original file line number Diff line number Diff line change
@@ -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?;
Expand Down

0 comments on commit 4c85cf9

Please sign in to comment.