From 31b901c1d620affe185010382258ff8d93806356 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sat, 8 Jun 2024 16:43:31 +0100 Subject: [PATCH] refactor: allow db to be choosen in config --- Cargo.lock | 25 +++++++++++++++++++++++++ Cargo.toml | 5 +++-- config.toml | 8 ++++++-- src/config.rs | 15 +++++++++++++++ src/main.rs | 17 +++++++++++++---- 5 files changed, 62 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4047cab..b5238ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -407,6 +407,7 @@ dependencies = [ "bitcoin 0.30.2", "bitcoin_hashes 0.12.0", "cdk", + "cdk-redb", "cdk-sqlite", "chrono", "clap", @@ -458,6 +459,21 @@ dependencies = [ "uuid", ] +[[package]] +name = "cdk-redb" +version = "0.1.0" +source = "git+https://github.com/cashubtc/cdk?rev=e1506c4#e1506c4e34b034235fc0ea44912f745e9866c934" +dependencies = [ + "async-trait", + "cdk", + "redb", + "serde", + "serde_json", + "thiserror", + "tokio", + "tracing", +] + [[package]] name = "cdk-sqlite" version = "0.1.0" @@ -1725,6 +1741,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redb" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed7508e692a49b6b2290b56540384ccae9b1fb4d77065640b165835b56ffe3bb" +dependencies = [ + "libc", +] + [[package]] name = "redox_syscall" version = "0.2.16" diff --git a/Cargo.toml b/Cargo.toml index edb016a..a3d7e6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,9 +35,10 @@ clap = { version = "4.3.14", features = ["env", "default", "derive"]} anyhow = "1.0.71" cdk = { git = "https://github.com/cashubtc/cdk", rev = "e1506c4", default-features = false, features = ["mint"] } cdk-sqlite = { git = "https://github.com/cashubtc/cdk", rev = "e1506c4", default-features = false, features = ["mint"] } +cdk-redb = { git = "https://github.com/cashubtc/cdk", rev = "e1506c4", default-features = false, features = ["mint"] } # cdk = { path = "../../cdk/sql/crates/cdk", default-features = false, features = ["mint"] } - #cdk-sqlite = { path = "../../cdk/sql/crates/cdk-sqlite", default-features = false, features = ["mint"] } -# cdk-redb = { path = "../../cdk/nut14/crates/cdk-redb" } +# cdk-sqlite = { path = "../../cdk/sql/crates/cdk-sqlite", default-features = false, features = ["mint"] } +# cdk-redb = { path = "../../cdk/nut14/crates/cdk-redb", default-features = false, features = ["mint"] } bitcoin = { version = "0.30.0", features = ["no-std"] } serde = "1.0.164" diff --git a/config.toml b/config.toml index fbe021e..7f90384 100644 --- a/config.toml +++ b/config.toml @@ -16,10 +16,14 @@ last_pay_path = "/tmp/cashu-rs-mint/last_pay.txt" [contact] email = "me@example.com" +[database] +# Database engine (sqlite/redb) defaults to sqlite +# engine = "sqlite" + [ln] -# Required ln backend `Cln`, `Ldk`, `Greenlight` -ln_backend = "Cln" +# Required ln backend `cln`, `ldk`, `greenlight` +ln_backend = "cln" # CLN cln_path = "/tmp/cashu-rs-mint/lighting/ln_1/regtest/lightning-rpc" diff --git a/src/config.rs b/src/config.rs index 7710685..734f029 100644 --- a/src/config.rs +++ b/src/config.rs @@ -69,6 +69,7 @@ fn last_pay_path() -> String { } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)] +#[serde(rename_all = "lowercase")] pub enum LnBackend { #[default] Cln, @@ -86,11 +87,25 @@ pub struct Ln { pub reserve_fee_min: Amount, } +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)] +#[serde(rename_all = "lowercase")] +pub enum DatabaseEngine { + #[default] + Sqlite, + Redb, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct Database { + pub engine: DatabaseEngine, +} + #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct Settings { pub info: Info, // pub mint_info: MintInfo, pub ln: Ln, + pub database: Database, } impl Settings { diff --git a/src/main.rs b/src/main.rs index fdc38e9..e8526ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use axum::routing::{get, post}; use axum::Router; use bip39::Mnemonic; use cdk::amount::Amount; +use cdk::cdk_database::{self, MintDatabase}; use cdk::mint::Mint; use cdk::nuts::nut02::Id; use cdk::nuts::{ @@ -23,6 +24,7 @@ use cdk::nuts::{ MintBolt11Request, MintBolt11Response, SwapRequest, SwapResponse, *, }; use cdk::types::MintQuote; +use cdk_redb::MintRedbDatabase; use cdk_sqlite::MintSqliteDatabase; use clap::Parser; use error::into_response; @@ -33,9 +35,10 @@ use tower_http::cors::CorsLayer; use tracing::{debug, warn}; use utils::unix_time; -pub const CARGO_PKG_VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); - use crate::cli::CLIArgs; +use crate::config::DatabaseEngine; + +pub const CARGO_PKG_VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); mod cli; mod config; @@ -68,7 +71,13 @@ async fn main() -> anyhow::Result<()> { None => settings.info.clone().db_path, }; - let localstore = MintSqliteDatabase::new(db_path.to_str().unwrap()).await?; + let localstore: Arc + Send + Sync> = + match settings.database.engine { + DatabaseEngine::Sqlite => { + Arc::new(MintSqliteDatabase::new(db_path.to_str().unwrap()).await?) + } + DatabaseEngine::Redb => Arc::new(MintRedbDatabase::new(db_path.to_str().unwrap())?), + }; let mint_info = MintInfo::default(); let mnemonic = Mnemonic::from_str(&settings.info.mnemonic)?; @@ -76,7 +85,7 @@ async fn main() -> anyhow::Result<()> { let mint = Mint::new( &mnemonic.to_seed_normalized(""), mint_info, - Arc::new(localstore), + localstore, Amount::ZERO, 0.0, )