From 0f550377a2198c69372732fd26e01fcdeba91174 Mon Sep 17 00:00:00 2001 From: ok300 <106775972+ok300@users.noreply.github.com> Date: Thu, 6 Feb 2025 09:41:12 +0100 Subject: [PATCH] Mint Red DB: extract migrate() logic to own function --- crates/cdk-redb/src/migrations.rs | 11 +- crates/cdk-redb/src/mint/migrations.rs | 13 +- crates/cdk-redb/src/mint/mod.rs | 169 +++++++++++++------------ 3 files changed, 96 insertions(+), 97 deletions(-) diff --git a/crates/cdk-redb/src/migrations.rs b/crates/cdk-redb/src/migrations.rs index 0a40068d..66c25b85 100644 --- a/crates/cdk-redb/src/migrations.rs +++ b/crates/cdk-redb/src/migrations.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::sync::Arc; use cdk_common::mint_url::MintUrl; use cdk_common::{Amount, CurrencyUnit, MeltQuoteState, MintQuoteState}; @@ -105,7 +104,7 @@ impl From for V1MeltQuote { } } -fn migrate_mint_quotes_00_to_01(db: Arc) -> Result<(), Error> { +fn migrate_mint_quotes_00_to_01(db: &Database) -> Result<(), Error> { let read_txn = db.begin_read().map_err(Error::from)?; let table = read_txn .open_table(MINT_QUOTES_TABLE) @@ -158,7 +157,7 @@ fn migrate_mint_quotes_00_to_01(db: Arc) -> Result<(), Error> { Ok(()) } -fn migrate_melt_quotes_00_to_01(db: Arc) -> Result<(), Error> { +fn migrate_melt_quotes_00_to_01(db: &Database) -> Result<(), Error> { let read_txn = db.begin_read().map_err(Error::from)?; let table = read_txn .open_table(MELT_QUOTES_TABLE) @@ -211,13 +210,13 @@ fn migrate_melt_quotes_00_to_01(db: Arc) -> Result<(), Error> { Ok(()) } -pub(crate) fn migrate_00_to_01(db: Arc) -> Result { +pub(crate) fn migrate_00_to_01(db: &Database) -> Result { tracing::info!("Starting Migrations of mint quotes from 00 to 01"); - migrate_mint_quotes_00_to_01(Arc::clone(&db))?; + migrate_mint_quotes_00_to_01(db)?; tracing::info!("Finished Migrations of mint quotes from 00 to 01"); tracing::info!("Starting Migrations of melt quotes from 00 to 01"); - migrate_melt_quotes_00_to_01(Arc::clone(&db))?; + migrate_melt_quotes_00_to_01(db)?; tracing::info!("Finished Migrations of melt quotes from 00 to 01"); Ok(1) } diff --git a/crates/cdk-redb/src/mint/migrations.rs b/crates/cdk-redb/src/mint/migrations.rs index b7025588..f7eefdc5 100644 --- a/crates/cdk-redb/src/mint/migrations.rs +++ b/crates/cdk-redb/src/mint/migrations.rs @@ -1,7 +1,6 @@ use core::str; use std::collections::HashMap; use std::str::FromStr; -use std::sync::Arc; use cdk_common::mint::MintQuote; use cdk_common::mint_url::MintUrl; @@ -22,23 +21,23 @@ const SPENT_PROOFS_TABLE: TableDefinition<[u8; 33], &str> = TableDefinition::new const QUOTE_PROOFS_TABLE: MultimapTableDefinition<&str, [u8; 33]> = MultimapTableDefinition::new("quote_proofs"); -pub fn migrate_01_to_02(db: Arc) -> Result { +pub fn migrate_01_to_02(db: &Database) -> Result { migrate_mint_quotes_01_to_02(db)?; Ok(2) } -pub fn migrate_02_to_03(db: Arc) -> Result { +pub fn migrate_02_to_03(db: &Database) -> Result { migrate_mint_proofs_02_to_03(db)?; Ok(3) } -pub fn migrate_03_to_04(db: Arc) -> Result { +pub fn migrate_03_to_04(db: &Database) -> Result { let write_txn = db.begin_write()?; let _ = write_txn.open_multimap_table(QUOTE_PROOFS_TABLE)?; let _ = write_txn.open_multimap_table(QUOTE_SIGNATURES_TABLE)?; Ok(4) } -pub fn migrate_04_to_05(db: Arc) -> Result { +pub fn migrate_04_to_05(db: &Database) -> Result { let write_txn = db.begin_write()?; // Mint quotes @@ -213,7 +212,7 @@ impl From for MintQuote { } } -fn migrate_mint_quotes_01_to_02(db: Arc) -> Result<(), Error> { +fn migrate_mint_quotes_01_to_02(db: &Database) -> Result<(), Error> { let read_txn = db.begin_read().map_err(Error::from)?; let table = read_txn .open_table(ID_STR_MINT_QUOTES_TABLE) @@ -266,7 +265,7 @@ fn migrate_mint_quotes_01_to_02(db: Arc) -> Result<(), Error> { Ok(()) } -fn migrate_mint_proofs_02_to_03(db: Arc) -> Result<(), Error> { +fn migrate_mint_proofs_02_to_03(db: &Database) -> Result<(), Error> { let pending_proofs: Vec<([u8; 33], Option)>; let spent_proofs: Vec<([u8; 33], Option)>; diff --git a/crates/cdk-redb/src/mint/mod.rs b/crates/cdk-redb/src/mint/mod.rs index dbc75d7b..1d4679b8 100644 --- a/crates/cdk-redb/src/mint/mod.rs +++ b/crates/cdk-redb/src/mint/mod.rs @@ -57,70 +57,53 @@ impl MintRedbDatabase { pub fn new(work_dir: &Path, _backups_to_keep: u8) -> Result { let db_file_path = work_dir.join("cdk-mintd.redb"); - { - let db = Arc::new(Database::create(&db_file_path)?); - - // Check database version - let read_txn = db.begin_read()?; - let table = read_txn.open_table(CONFIG_TABLE); - - let db_version = match table { - Ok(table) => table.get("db_version")?.map(|v| v.value().to_owned()), - Err(_) => None, - }; - match db_version { - Some(db_version) => { - let mut current_file_version = u32::from_str(&db_version)?; - match current_file_version.cmp(&DATABASE_VERSION) { - Ordering::Less => { - tracing::info!( - "Database needs to be upgraded at {} current is {}", - current_file_version, - DATABASE_VERSION - ); - if current_file_version == 0 { - current_file_version = migrate_00_to_01(Arc::clone(&db))?; - } - - if current_file_version == 1 { - current_file_version = migrate_01_to_02(Arc::clone(&db))?; - } - - if current_file_version == 2 { - current_file_version = migrate_02_to_03(Arc::clone(&db))?; - } - - if current_file_version == 3 { - current_file_version = migrate_03_to_04(Arc::clone(&db))?; - } - - if current_file_version == 4 { - current_file_version = migrate_04_to_05(Arc::clone(&db))?; - } - - if current_file_version != DATABASE_VERSION { - tracing::warn!( - "Database upgrade did not complete at {} current is {}", - current_file_version, - DATABASE_VERSION - ); - return Err(Error::UnknownDatabaseVersion); - } - - let write_txn = db.begin_write()?; - { - let mut table = write_txn.open_table(CONFIG_TABLE)?; - - table - .insert("db_version", DATABASE_VERSION.to_string().as_str())?; - } - - write_txn.commit()?; + let db = Database::create(db_file_path)?; + + Self::migrate(&db)?; + + Ok(Self { db: Arc::new(db) }) + } + + fn migrate(db: &Database) -> Result<(), Error> { + // Check database version + let read_txn = db.begin_read()?; + let table = read_txn.open_table(CONFIG_TABLE); + + let db_version = match table { + Ok(table) => table.get("db_version")?.map(|v| v.value().to_owned()), + Err(_) => None, + }; + match db_version { + Some(db_version) => { + let mut current_file_version = u32::from_str(&db_version)?; + match current_file_version.cmp(&DATABASE_VERSION) { + Ordering::Less => { + tracing::info!( + "Database needs to be upgraded at {} current is {}", + current_file_version, + DATABASE_VERSION + ); + if current_file_version == 0 { + current_file_version = migrate_00_to_01(db)?; + } + + if current_file_version == 1 { + current_file_version = migrate_01_to_02(db)?; + } + + if current_file_version == 2 { + current_file_version = migrate_02_to_03(db)?; + } + + if current_file_version == 3 { + current_file_version = migrate_03_to_04(db)?; } - Ordering::Equal => { - tracing::info!("Database is at current version {}", DATABASE_VERSION); + + if current_file_version == 4 { + current_file_version = migrate_04_to_05(db)?; } - Ordering::Greater => { + + if current_file_version != DATABASE_VERSION { tracing::warn!( "Database upgrade did not complete at {} current is {}", current_file_version, @@ -128,34 +111,52 @@ impl MintRedbDatabase { ); return Err(Error::UnknownDatabaseVersion); } + + let write_txn = db.begin_write()?; + { + let mut table = write_txn.open_table(CONFIG_TABLE)?; + + table.insert("db_version", DATABASE_VERSION.to_string().as_str())?; + } + + write_txn.commit()?; } - } - None => { - let write_txn = db.begin_write()?; - { - let mut table = write_txn.open_table(CONFIG_TABLE)?; - // Open all tables to init a new db - let _ = write_txn.open_table(ACTIVE_KEYSETS_TABLE)?; - let _ = write_txn.open_table(KEYSETS_TABLE)?; - let _ = write_txn.open_table(MINT_QUOTES_TABLE)?; - let _ = write_txn.open_table(MELT_QUOTES_TABLE)?; - let _ = write_txn.open_table(PROOFS_TABLE)?; - let _ = write_txn.open_table(PROOFS_STATE_TABLE)?; - let _ = write_txn.open_table(BLINDED_SIGNATURES)?; - let _ = write_txn.open_multimap_table(QUOTE_PROOFS_TABLE)?; - let _ = write_txn.open_multimap_table(QUOTE_SIGNATURES_TABLE)?; - - table.insert("db_version", DATABASE_VERSION.to_string().as_str())?; + Ordering::Equal => { + tracing::info!("Database is at current version {}", DATABASE_VERSION); } - - write_txn.commit()?; + Ordering::Greater => { + tracing::warn!( + "Database upgrade did not complete at {} current is {}", + current_file_version, + DATABASE_VERSION + ); + return Err(Error::UnknownDatabaseVersion); + } + } + } + None => { + let write_txn = db.begin_write()?; + { + let mut table = write_txn.open_table(CONFIG_TABLE)?; + // Open all tables to init a new db + let _ = write_txn.open_table(ACTIVE_KEYSETS_TABLE)?; + let _ = write_txn.open_table(KEYSETS_TABLE)?; + let _ = write_txn.open_table(MINT_QUOTES_TABLE)?; + let _ = write_txn.open_table(MELT_QUOTES_TABLE)?; + let _ = write_txn.open_table(PROOFS_TABLE)?; + let _ = write_txn.open_table(PROOFS_STATE_TABLE)?; + let _ = write_txn.open_table(BLINDED_SIGNATURES)?; + let _ = write_txn.open_multimap_table(QUOTE_PROOFS_TABLE)?; + let _ = write_txn.open_multimap_table(QUOTE_SIGNATURES_TABLE)?; + + table.insert("db_version", DATABASE_VERSION.to_string().as_str())?; } + + write_txn.commit()?; } - drop(db); } - let db = Database::create(db_file_path)?; - Ok(Self { db: Arc::new(db) }) + Ok(()) } }