Working on a better database abstraction (#931)

* Working on a better database abstraction

After [this question in the chat](https://matrix.to/#/!oJFtttFHGfnTGrIjvD:matrix.cashu.space/$oJFtttFHGfnTGrIjvD:matrix.cashu.space/$I5ZtjJtBM0ctltThDYpoCwClZFlM6PHzf8q2Rjqmso8)
regarding a database transaction within the same function, I realized a few
design flaws in our SQL database abstraction, particularly regarding
transactions.

1. Our upper abstraction got it right, where a transaction is bound with `&mut
   self`, so Rust knows how to handle its lifetime with' async/await'.
2. The raw database does not; instead, it returns &self, and beginning a
   transaction takes &self as well, which is problematic for Rust, but that's not
   all. It is fundamentally wrong. A transaction should take &mut self when
   beginning a transaction, as that connection is bound to a transaction and
   should not be returned to the pool. Currently, that responsibility lies with
   the implementor. If a mistake is made, a transaction could be executed in two
   or more connections.
3. The way a database is bound to our store layer is through a single struct,
   which may or may not internally utilize our connection pool. This is also
   another design flow, in this PR, a connection pool is owned, and to use a
   connection, it should be requested, and that connection is reference with
   mutable when beginning a transaction

* Improve the abstraction with fewer generics

As suggested by @thesimplekid

* Add BEGIN IMMEDIATE for SQLite
This commit is contained in:
C
2025-08-06 03:58:03 -03:00
committed by GitHub
parent cceea654fe
commit ad8f1ece5c
13 changed files with 721 additions and 1122 deletions

View File

@@ -1,8 +1,9 @@
//! SQL Mint Auth
use std::collections::HashMap;
use std::marker::PhantomData;
use std::fmt::Debug;
use std::str::FromStr;
use std::sync::Arc;
use async_trait::async_trait;
use cdk_common::database::{self, MintAuthDatabase, MintAuthTransaction};
@@ -15,37 +16,38 @@ use tracing::instrument;
use super::{sql_row_to_blind_signature, sql_row_to_keyset_info, SQLTransaction};
use crate::column_as_string;
use crate::common::migrate;
use crate::database::{DatabaseConnector, DatabaseTransaction};
use crate::database::{ConnectionWithTransaction, DatabaseExecutor};
use crate::mint::Error;
use crate::pool::{DatabasePool, Pool, PooledResource};
use crate::stmt::query;
/// Mint SQL Database
#[derive(Debug, Clone)]
pub struct SQLMintAuthDatabase<DB>
pub struct SQLMintAuthDatabase<RM>
where
DB: DatabaseConnector,
RM: DatabasePool + 'static,
{
db: DB,
pool: Arc<Pool<RM>>,
}
impl<DB> SQLMintAuthDatabase<DB>
impl<RM> SQLMintAuthDatabase<RM>
where
DB: DatabaseConnector,
RM: DatabasePool + 'static,
{
/// Creates a new instance
pub async fn new<X>(db: X) -> Result<Self, Error>
where
X: Into<DB>,
X: Into<RM::Config>,
{
let db = db.into();
Self::migrate(&db).await?;
Ok(Self { db })
let pool = Pool::new(db.into());
Self::migrate(pool.get().map_err(|e| Error::Database(Box::new(e)))?).await?;
Ok(Self { pool })
}
/// Migrate
async fn migrate(conn: &DB) -> Result<(), Error> {
let tx = conn.begin().await?;
migrate(&tx, DB::name(), MIGRATIONS).await?;
async fn migrate(conn: PooledResource<RM>) -> Result<(), Error> {
let tx = ConnectionWithTransaction::new(conn).await?;
migrate(&tx, RM::Connection::name(), MIGRATIONS).await?;
tx.commit().await?;
Ok(())
}
@@ -56,9 +58,9 @@ mod migrations;
#[async_trait]
impl<'a, T> MintAuthTransaction<database::Error> for SQLTransaction<'a, T>
impl<RM> MintAuthTransaction<database::Error> for SQLTransaction<RM>
where
T: DatabaseTransaction<'a>,
RM: DatabasePool + 'static,
{
#[instrument(skip(self))]
async fn set_active_keyset(&mut self, id: Id) -> Result<(), database::Error> {
@@ -233,9 +235,9 @@ where
}
#[async_trait]
impl<DB> MintAuthDatabase for SQLMintAuthDatabase<DB>
impl<RM> MintAuthDatabase for SQLMintAuthDatabase<RM>
where
DB: DatabaseConnector,
RM: DatabasePool + 'static,
{
type Err = database::Error;
@@ -244,12 +246,15 @@ where
) -> Result<Box<dyn MintAuthTransaction<database::Error> + Send + Sync + 'a>, database::Error>
{
Ok(Box::new(SQLTransaction {
inner: self.db.begin().await?,
_phantom: PhantomData,
inner: ConnectionWithTransaction::new(
self.pool.get().map_err(|e| Error::Database(Box::new(e)))?,
)
.await?,
}))
}
async fn get_active_keyset_id(&self) -> Result<Option<Id>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -260,13 +265,14 @@ where
active = 1;
"#,
)?
.pluck(&self.db)
.pluck(&*conn)
.await?
.map(|id| Ok::<_, Error>(column_as_string!(id, Id::from_str, Id::from_bytes)))
.transpose()?)
}
async fn get_keyset_info(&self, id: &Id) -> Result<Option<MintKeySetInfo>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"SELECT
id,
@@ -283,13 +289,14 @@ where
WHERE id=:id"#,
)?
.bind("id", id.to_string())
.fetch_one(&self.db)
.fetch_one(&*conn)
.await?
.map(sql_row_to_keyset_info)
.transpose()?)
}
async fn get_keyset_infos(&self) -> Result<Vec<MintKeySetInfo>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"SELECT
id,
@@ -305,7 +312,7 @@ where
keyset
WHERE id=:id"#,
)?
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(sql_row_to_keyset_info)
@@ -313,9 +320,10 @@ where
}
async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let mut current_states = query(r#"SELECT y, state FROM proof WHERE y IN (:ys)"#)?
.bind_vec("ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(|row| {
@@ -333,6 +341,7 @@ where
&self,
blinded_messages: &[PublicKey],
) -> Result<Vec<Option<BlindSignature>>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let mut blinded_signatures = query(
r#"SELECT
keyset_id,
@@ -353,7 +362,7 @@ where
.map(|y| y.to_bytes().to_vec())
.collect(),
)
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(|mut row| {
@@ -377,10 +386,11 @@ where
&self,
protected_endpoint: ProtectedEndpoint,
) -> Result<Option<AuthRequired>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(
query(r#"SELECT auth FROM protected_endpoints WHERE endpoint = :endpoint"#)?
.bind("endpoint", serde_json::to_string(&protected_endpoint)?)
.pluck(&self.db)
.pluck(&*conn)
.await?
.map(|auth| {
Ok::<_, Error>(column_as_string!(
@@ -396,8 +406,9 @@ where
async fn get_auth_for_endpoints(
&self,
) -> Result<HashMap<ProtectedEndpoint, Option<AuthRequired>>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(r#"SELECT endpoint, auth FROM protected_endpoints"#)?
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(|row| {