Skip to content

Commit

Permalink
feat: check if db upgrade needed
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Apr 7, 2024
1 parent e63b43f commit 30d53f5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
3 changes: 3 additions & 0 deletions crates/cashu-sdk/src/mint/localstore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod memory;
pub mod redb_store;

use std::collections::HashMap;
use std::num::ParseIntError;

use async_trait::async_trait;
use cashu::nuts::nut02::mint::KeySet;
Expand Down Expand Up @@ -37,6 +38,8 @@ pub enum Error {
#[cfg(all(not(target_arch = "wasm32"), feature = "redb"))]
#[error("`{0}`")]
Serde(#[from] serde_json::Error),
#[error("`{0}`")]
ParseInt(#[from] ParseIntError),
#[error("Unknown Mint Info")]
UnknownMintInfo,
#[error("`{0}`")]
Expand Down
35 changes: 27 additions & 8 deletions crates/cashu-sdk/src/mint/localstore/redb_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const CONFIG_TABLE: TableDefinition<&str, &str> = TableDefinition::new("config")
// Key is hex blinded_message B_ value is blinded_signature
const BLINDED_SIGNATURES: TableDefinition<&[u8], &str> = TableDefinition::new("blinded_signatures");

const DATABASE_VERSION: u64 = 0;

#[derive(Debug, Clone)]
pub struct RedbLocalStore {
db: Arc<Mutex<Database>>,
Expand All @@ -35,18 +37,35 @@ impl RedbLocalStore {
let db = Database::create(path)?;

let write_txn = db.begin_write()?;
// Check database version
{
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(PENDING_PROOFS_TABLE)?;
let _ = write_txn.open_table(SPENT_PROOFS_TABLE)?;
let _ = write_txn.open_table(CONFIG_TABLE)?;
let _ = write_txn.open_table(BLINDED_SIGNATURES)?;
let mut table = write_txn.open_table(CONFIG_TABLE)?;

let db_version = table.get("db_version")?;
let db_version = db_version.map(|v| v.value().to_owned());

if let Some(db_version) = db_version {
let current_file_version = u64::from_str(&db_version)?;
if current_file_version.ne(&DATABASE_VERSION) {
// Database needs to be upgraded
todo!()
}
} else {
// 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(PENDING_PROOFS_TABLE)?;
let _ = write_txn.open_table(SPENT_PROOFS_TABLE)?;
let _ = write_txn.open_table(BLINDED_SIGNATURES)?;

table.insert("db_version", "0")?;
};
}
write_txn.commit()?;

write_txn.commit()?;
Ok(Self {
db: Arc::new(Mutex::new(db)),
})
Expand Down
3 changes: 3 additions & 0 deletions crates/cashu-sdk/src/wallet/localstore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod memory;
mod redb_store;

use std::collections::HashMap;
use std::num::ParseIntError;

use async_trait::async_trait;
use cashu::nuts::{Id, KeySetInfo, Keys, MintInfo, Proofs};
Expand Down Expand Up @@ -37,6 +38,8 @@ pub enum Error {
#[cfg(all(not(target_arch = "wasm32"), feature = "redb"))]
#[error("`{0}`")]
Serde(#[from] serde_json::Error),
#[error("`{0}`")]
ParseInt(#[from] ParseIntError),
}

#[async_trait]
Expand Down
39 changes: 31 additions & 8 deletions crates/cashu-sdk/src/wallet/localstore/redb_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ const MINT_KEYS_TABLE: TableDefinition<&str, &str> = TableDefinition::new("mint_
const PROOFS_TABLE: MultimapTableDefinition<&str, &str> = MultimapTableDefinition::new("proofs");
const PENDING_PROOFS_TABLE: MultimapTableDefinition<&str, &str> =
MultimapTableDefinition::new("pending_proofs");
const CONFIG_TABLE: TableDefinition<&str, &str> = TableDefinition::new("config");
#[cfg(feature = "nut13")]
const KEYSET_COUNTER: TableDefinition<&str, u64> = TableDefinition::new("keyset_counter");

const DATABASE_VERSION: u64 = 0;

#[derive(Debug, Clone)]
pub struct RedbLocalStore {
db: Arc<Mutex<Database>>,
Expand All @@ -33,15 +36,35 @@ impl RedbLocalStore {
let db = Database::create(path)?;

let write_txn = db.begin_write()?;

// Check database version
{
let _ = write_txn.open_table(MINTS_TABLE)?;
let _ = write_txn.open_multimap_table(MINT_KEYSETS_TABLE)?;
let _ = write_txn.open_table(MINT_QUOTES_TABLE)?;
let _ = write_txn.open_table(MELT_QUOTES_TABLE)?;
let _ = write_txn.open_table(MINT_KEYS_TABLE)?;
let _ = write_txn.open_multimap_table(PROOFS_TABLE)?;
#[cfg(feature = "nut13")]
let _ = write_txn.open_table(KEYSET_COUNTER)?;
let _ = write_txn.open_table(CONFIG_TABLE)?;
let mut table = write_txn.open_table(CONFIG_TABLE)?;

let db_version = table.get("db_version")?;
let db_version = db_version.map(|v| v.value().to_owned());

if let Some(db_version) = db_version {
let current_file_version = u64::from_str(&db_version)?;
if current_file_version.ne(&DATABASE_VERSION) {
// Database needs to be upgraded
todo!()
}
#[cfg(feature = "nut13")]
let _ = write_txn.open_table(KEYSET_COUNTER)?;
} else {
// Open all tables to init a new db
let _ = write_txn.open_table(MINTS_TABLE)?;
let _ = write_txn.open_multimap_table(MINT_KEYSETS_TABLE)?;
let _ = write_txn.open_table(MINT_QUOTES_TABLE)?;
let _ = write_txn.open_table(MELT_QUOTES_TABLE)?;
let _ = write_txn.open_table(MINT_KEYS_TABLE)?;
let _ = write_txn.open_multimap_table(PROOFS_TABLE)?;
#[cfg(feature = "nut13")]
let _ = write_txn.open_table(KEYSET_COUNTER)?;
table.insert("db_version", "0")?;
};
}
write_txn.commit()?;

Expand Down
3 changes: 3 additions & 0 deletions crates/cashu-sdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Cashu Wallet
use std::collections::{HashMap, HashSet};
use std::num::ParseIntError;
use std::str::FromStr;
use std::sync::Arc;

Expand Down Expand Up @@ -60,6 +61,8 @@ pub enum Error {
#[error("Unknown Key")]
UnknownKey,
#[error("`{0}`")]
ParseInt(#[from] ParseIntError),
#[error("`{0}`")]
Custom(String),
}

Expand Down

0 comments on commit 30d53f5

Please sign in to comment.