Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement block index builder #8

Merged
merged 15 commits into from
Jul 26, 2024
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
resolver = "2"

members = ["chain", "shared", "orm", "webserver"]
members = ["block-index", "chain", "shared", "orm", "webserver"]

[workspace.package]
authors = ["Heliax AG <[email protected]>"]
Expand All @@ -15,6 +15,7 @@ anyhow = "1.0.75"
axum = { version = "0.6.20", features = [ "tower-log" ] }
axum-macros = "0.3.8"
axum-trace-id = "0.1.0"
bincode = "1.3.3"
clap = { version = "4.4.2", features = [ "derive", "env" ] }
clap-verbosity-flag = "2.1.1"
deadpool-diesel = { version = "0.5.0", features = ["postgres"] }
Expand All @@ -38,5 +39,7 @@ tokio-retry = "0.3"
tower = { version = "0.4", features = [ "util", "timeout", "load-shed", "limit", "buffer" ] }
tower-http = { version = "0.4.4", features = [ "compression-full", "limit", "trace", "cors" ] }
tracing = "0.1"
tracing-appender = "0.2.0"
tracing-subscriber = { version = "0.3", features = [ "env-filter" ] }
validator = { version = "0.16.0", features = ["derive"] }
xorf = { version = "0.11.0", features = ["serde"]}
32 changes: 32 additions & 0 deletions block-index/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "block-index"
description = "Namada masp indexer block index builder."
resolver = "2"
authors.workspace = true
edition.workspace = true
license.workspace = true
readme.workspace = true
version.workspace = true

[[bin]]
name = "block-index-builder"
path = "src/main.rs"

[dependencies]
anyhow.workspace = true
bincode.workspace = true
clap-verbosity-flag.workspace = true
clap.workspace = true
deadpool-diesel.workspace = true
diesel.workspace = true
diesel_migrations.workspace = true
orm.workspace = true
shared.workspace = true
tokio.workspace = true
tracing-appender.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
xorf.workspace = true

[build-dependencies]
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }
8 changes: 8 additions & 0 deletions block-index/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::error::Error;

use vergen::EmitBuilder;

fn main() -> Result<(), Box<dyn Error>> {
EmitBuilder::builder().all_git().emit()?;
Ok(())
}
38 changes: 38 additions & 0 deletions block-index/src/appstate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::env;

use anyhow::Context;
use deadpool_diesel::postgres::{Object, Pool as DbPool};

#[derive(Clone)]
pub struct AppState {
db: DbPool,
}

impl AppState {
pub fn new(db_url: String) -> anyhow::Result<Self> {
let max_pool_size = env::var("DATABASE_POOL_SIZE")
.unwrap_or_else(|_| 8.to_string())
.parse::<usize>()
.unwrap_or(8_usize);
let pool_manager = deadpool_diesel::Manager::from_config(
db_url,
deadpool_diesel::Runtime::Tokio1,
deadpool_diesel::ManagerConfig {
recycling_method: deadpool_diesel::RecyclingMethod::Verified,
},
);
let pool = DbPool::builder(pool_manager)
.max_size(max_pool_size)
.build()
.context("Failed to build Postgres db pool")?;

Ok(Self { db: pool })
}

pub async fn get_db_connection(&self) -> anyhow::Result<Object> {
self.db
.get()
.await
.context("Failed to get db connection handle from deadpool")
}
}
41 changes: 41 additions & 0 deletions block-index/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::num::NonZeroU64;

use clap_verbosity_flag::{InfoLevel, LevelFilter, Verbosity};
use tracing::Level;
use tracing_appender::non_blocking::NonBlocking;
use tracing_subscriber::FmtSubscriber;

#[derive(clap::Parser)]
pub struct AppConfig {
/// Link to the Postgres database
#[clap(long, env)]
pub database_url: String,

/// How often (in seconds) a new block index is built
#[clap(long, env)]
pub interval: Option<NonZeroU64>,

#[command(flatten)]
pub verbosity: Verbosity<InfoLevel>,
}

pub fn install_tracing_subscriber(
verbosity: Verbosity<InfoLevel>,
non_blocking_logger: NonBlocking,
) {
let log_level = match verbosity.log_level_filter() {
LevelFilter::Off => None,
LevelFilter::Error => Some(Level::ERROR),
LevelFilter::Warn => Some(Level::WARN),
LevelFilter::Info => Some(Level::INFO),
LevelFilter::Debug => Some(Level::DEBUG),
LevelFilter::Trace => Some(Level::TRACE),
};
if let Some(log_level) = log_level {
let subscriber = FmtSubscriber::builder()
.with_max_level(log_level)
.with_writer(non_blocking_logger)
.finish();
tracing::subscriber::set_global_default(subscriber).unwrap();
}
}
Loading
Loading