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

@@ -23,6 +23,7 @@ cdk-common = { workspace = true, features = ["test"] }
bitcoin.workspace = true
thiserror.workspace = true
tracing.workspace = true
tokio.workspace = true
serde.workspace = true
serde_json.workspace = true
lightning-invoice.workspace = true

View File

@@ -3,11 +3,14 @@ use crate::stmt::query;
/// Migrates the migration generated by `build.rs`
#[inline(always)]
pub async fn migrate<C: DatabaseExecutor>(
pub async fn migrate<C>(
conn: &C,
db_prefix: &str,
migrations: &[(&str, &str, &str)],
) -> Result<(), cdk_common::database::Error> {
) -> Result<(), cdk_common::database::Error>
where
C: DatabaseExecutor,
{
query(
r#"
CREATE TABLE IF NOT EXISTS migrations (

View File

@@ -1,10 +1,12 @@
//! Database traits definition
use std::fmt::Debug;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use cdk_common::database::Error;
use crate::stmt::{Column, Statement};
use crate::stmt::{query, Column, Statement};
/// Database Executor
///
@@ -32,22 +34,170 @@ pub trait DatabaseExecutor: Debug + Sync + Send {
/// Database transaction trait
#[async_trait::async_trait]
pub trait DatabaseTransaction<'a>: Debug + DatabaseExecutor + Send + Sync {
pub trait DatabaseTransaction<DB>
where
DB: DatabaseExecutor,
{
/// Consumes the current transaction committing the changes
async fn commit(self) -> Result<(), Error>;
async fn commit(conn: &mut DB) -> Result<(), Error>;
/// Begin a transaction
async fn begin(conn: &mut DB) -> Result<(), Error>;
/// Consumes the transaction rolling back all changes
async fn rollback(self) -> Result<(), Error>;
async fn rollback(conn: &mut DB) -> Result<(), Error>;
}
/// Database connection with a transaction
#[derive(Debug)]
pub struct ConnectionWithTransaction<DB, W>
where
DB: DatabaseConnector + 'static,
W: Debug + Deref<Target = DB> + DerefMut<Target = DB> + Send + Sync + 'static,
{
inner: Option<W>,
}
impl<DB, W> ConnectionWithTransaction<DB, W>
where
DB: DatabaseConnector,
W: Debug + Deref<Target = DB> + DerefMut<Target = DB> + Send + Sync + 'static,
{
/// Creates a new transaction
pub async fn new(mut inner: W) -> Result<Self, Error> {
DB::Transaction::begin(inner.deref_mut()).await?;
Ok(Self { inner: Some(inner) })
}
/// Commits the transaction consuming it and releasing the connection back to the pool (or
/// disconnecting)
pub async fn commit(mut self) -> Result<(), Error> {
let mut conn = self
.inner
.take()
.ok_or(Error::Internal("Missing connection".to_owned()))?;
DB::Transaction::commit(&mut conn).await?;
Ok(())
}
/// Rollback the transaction consuming it and releasing the connection back to the pool (or
/// disconnecting)
pub async fn rollback(mut self) -> Result<(), Error> {
let mut conn = self
.inner
.take()
.ok_or(Error::Internal("Missing connection".to_owned()))?;
DB::Transaction::rollback(&mut conn).await?;
Ok(())
}
}
impl<DB, W> Drop for ConnectionWithTransaction<DB, W>
where
DB: DatabaseConnector,
W: Debug + Deref<Target = DB> + DerefMut<Target = DB> + Send + Sync + 'static,
{
fn drop(&mut self) {
if let Some(mut conn) = self.inner.take() {
tokio::spawn(async move {
let _ = DB::Transaction::rollback(conn.deref_mut()).await;
});
}
}
}
#[async_trait::async_trait]
impl<DB, W> DatabaseExecutor for ConnectionWithTransaction<DB, W>
where
DB: DatabaseConnector,
W: Debug + Deref<Target = DB> + DerefMut<Target = DB> + Send + Sync + 'static,
{
fn name() -> &'static str {
"Transaction"
}
/// Executes a query and returns the affected rows
async fn execute(&self, statement: Statement) -> Result<usize, Error> {
self.inner
.as_ref()
.ok_or(Error::Internal("Missing internal connection".to_owned()))?
.execute(statement)
.await
}
/// Runs the query and returns the first row or None
async fn fetch_one(&self, statement: Statement) -> Result<Option<Vec<Column>>, Error> {
self.inner
.as_ref()
.ok_or(Error::Internal("Missing internal connection".to_owned()))?
.fetch_one(statement)
.await
}
/// Runs the query and returns the first row or None
async fn fetch_all(&self, statement: Statement) -> Result<Vec<Vec<Column>>, Error> {
self.inner
.as_ref()
.ok_or(Error::Internal("Missing internal connection".to_owned()))?
.fetch_all(statement)
.await
}
/// Fetches the first row and column from a query
async fn pluck(&self, statement: Statement) -> Result<Option<Column>, Error> {
self.inner
.as_ref()
.ok_or(Error::Internal("Missing internal connection".to_owned()))?
.pluck(statement)
.await
}
/// Batch execution
async fn batch(&self, statement: Statement) -> Result<(), Error> {
self.inner
.as_ref()
.ok_or(Error::Internal("Missing internal connection".to_owned()))?
.batch(statement)
.await
}
}
/// Generic transaction handler for SQLite
pub struct GenericTransactionHandler<W>(PhantomData<W>);
#[async_trait::async_trait]
impl<W> DatabaseTransaction<W> for GenericTransactionHandler<W>
where
W: DatabaseExecutor,
{
/// Consumes the current transaction committing the changes
async fn commit(conn: &mut W) -> Result<(), Error> {
query("COMMIT")?.execute(conn).await?;
Ok(())
}
/// Begin a transaction
async fn begin(conn: &mut W) -> Result<(), Error> {
query("BEGIN")?.execute(conn).await?;
Ok(())
}
/// Consumes the transaction rolling back all changes
async fn rollback(conn: &mut W) -> Result<(), Error> {
query("ROLLBACK")?.execute(conn).await?;
Ok(())
}
}
/// Database connector
#[async_trait::async_trait]
pub trait DatabaseConnector: Debug + DatabaseExecutor + Send + Sync {
/// Transaction type for this database connection
type Transaction<'a>: DatabaseTransaction<'a>
/// Database static trait for the database
type Transaction: DatabaseTransaction<Self>
where
Self: 'a;
/// Begin a new transaction
async fn begin(&self) -> Result<Self::Transaction<'_>, Error>;
Self: Sized;
}

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| {

View File

@@ -9,8 +9,9 @@
//! clients in a pool and expose them to an asynchronous environment, making them compatible with
//! Mint.
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 bitcoin::bip32::DerivationPath;
@@ -38,7 +39,8 @@ use tracing::instrument;
use uuid::Uuid;
use crate::common::migrate;
use crate::database::{DatabaseConnector, DatabaseExecutor, DatabaseTransaction};
use crate::database::{ConnectionWithTransaction, DatabaseExecutor};
use crate::pool::{DatabasePool, Pool, PooledResource};
use crate::stmt::{query, Column};
use crate::{
column_as_nullable_number, column_as_nullable_string, column_as_number, column_as_string,
@@ -57,20 +59,19 @@ pub use auth::SQLMintAuthDatabase;
/// Mint SQL Database
#[derive(Debug, Clone)]
pub struct SQLMintDatabase<DB>
pub struct SQLMintDatabase<RM>
where
DB: DatabaseConnector,
RM: DatabasePool + 'static,
{
db: DB,
pool: Arc<Pool<RM>>,
}
/// SQL Transaction Writer
pub struct SQLTransaction<'a, T>
pub struct SQLTransaction<RM>
where
T: DatabaseTransaction<'a>,
RM: DatabasePool + 'static,
{
inner: T,
_phantom: PhantomData<&'a ()>,
inner: ConnectionWithTransaction<RM::Connection, PooledResource<RM>>,
}
#[inline(always)]
@@ -115,24 +116,26 @@ where
Ok(())
}
impl<DB> SQLMintDatabase<DB>
impl<RM> SQLMintDatabase<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(())
}
@@ -142,9 +145,10 @@ where
where
R: serde::de::DeserializeOwned,
{
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let value = column_as_string!(query(r#"SELECT value FROM config WHERE id = :id LIMIT 1"#)?
.bind("id", id.to_owned())
.pluck(&self.db)
.pluck(&*conn)
.await?
.ok_or(Error::UnknownQuoteTTL)?);
@@ -153,9 +157,9 @@ where
}
#[async_trait]
impl<'a, T> database::MintProofsTransaction<'a> for SQLTransaction<'a, T>
impl<RM> database::MintProofsTransaction<'_> for SQLTransaction<RM>
where
T: DatabaseTransaction<'a>,
RM: DatabasePool + 'static,
{
type Err = Error;
@@ -267,9 +271,9 @@ where
}
#[async_trait]
impl<'a, T> database::MintTransaction<'a, Error> for SQLTransaction<'a, T>
impl<RM> database::MintTransaction<'_, Error> for SQLTransaction<RM>
where
T: DatabaseTransaction<'a>,
RM: DatabasePool + 'static,
{
async fn set_mint_info(&mut self, mint_info: MintInfo) -> Result<(), Error> {
Ok(set_to_config(&self.inner, "mint_info", &mint_info).await?)
@@ -281,18 +285,18 @@ where
}
#[async_trait]
impl<'a, T> MintDbWriterFinalizer for SQLTransaction<'a, T>
impl<RM> MintDbWriterFinalizer for SQLTransaction<RM>
where
T: DatabaseTransaction<'a>,
RM: DatabasePool + 'static,
{
type Err = Error;
async fn commit(self: Box<Self>) -> Result<(), Error> {
Ok(self.inner.commit().await?)
self.inner.commit().await
}
async fn rollback(self: Box<Self>) -> Result<(), Error> {
Ok(self.inner.rollback().await?)
self.inner.rollback().await
}
}
@@ -357,9 +361,9 @@ WHERE quote_id=:quote_id
}
#[async_trait]
impl<'a, T> MintKeyDatabaseTransaction<'a, Error> for SQLTransaction<'a, T>
impl<RM> MintKeyDatabaseTransaction<'_, Error> for SQLTransaction<RM>
where
T: DatabaseTransaction<'a>,
RM: DatabasePool + 'static,
{
async fn add_keyset_info(&mut self, keyset: MintKeySetInfo) -> Result<(), Error> {
query(
@@ -416,9 +420,9 @@ where
}
#[async_trait]
impl<DB> MintKeysDatabase for SQLMintDatabase<DB>
impl<RM> MintKeysDatabase for SQLMintDatabase<RM>
where
DB: DatabaseConnector,
RM: DatabasePool + 'static,
{
type Err = Error;
@@ -426,16 +430,19 @@ where
&'a self,
) -> Result<Box<dyn MintKeyDatabaseTransaction<'a, Error> + Send + Sync + 'a>, 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, unit: &CurrencyUnit) -> Result<Option<Id>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(
query(r#" SELECT id FROM keyset WHERE active = 1 AND unit IS :unit"#)?
.bind("unit", unit.to_string())
.pluck(&self.db)
.pluck(&*conn)
.await?
.map(|id| match id {
Column::Text(text) => Ok(Id::from_str(&text)?),
@@ -447,8 +454,9 @@ where
}
async fn get_active_keysets(&self) -> Result<HashMap<CurrencyUnit, Id>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(r#"SELECT id, unit FROM keyset WHERE active = 1"#)?
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(|row| {
@@ -461,6 +469,7 @@ where
}
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,
@@ -477,13 +486,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,
@@ -499,7 +509,7 @@ where
keyset
"#,
)?
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(sql_row_to_keyset_info)
@@ -508,9 +518,9 @@ where
}
#[async_trait]
impl<'a, T> MintQuotesTransaction<'a> for SQLTransaction<'a, T>
impl<RM> MintQuotesTransaction<'_> for SQLTransaction<RM>
where
T: DatabaseTransaction<'a>,
RM: DatabasePool + 'static,
{
type Err = Error;
@@ -1028,15 +1038,17 @@ VALUES (:quote_id, :amount, :timestamp);
}
#[async_trait]
impl<DB> MintQuotesDatabase for SQLMintDatabase<DB>
impl<RM> MintQuotesDatabase for SQLMintDatabase<RM>
where
DB: DatabaseConnector,
RM: DatabasePool + 'static,
{
type Err = Error;
async fn get_mint_quote(&self, quote_id: &Uuid) -> Result<Option<MintQuote>, Self::Err> {
let payments = get_mint_quote_payments(&self.db, quote_id).await?;
let issuance = get_mint_quote_issuance(&self.db, quote_id).await?;
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let payments = get_mint_quote_payments(&*conn, quote_id).await?;
let issuance = get_mint_quote_issuance(&*conn, quote_id).await?;
Ok(query(
r#"
@@ -1058,7 +1070,7 @@ where
WHERE id = :id"#,
)?
.bind("id", quote_id.as_hyphenated().to_string())
.fetch_one(&self.db)
.fetch_one(&*conn)
.await?
.map(|row| sql_row_to_mint_quote(row, payments, issuance))
.transpose()?)
@@ -1068,6 +1080,7 @@ where
&self,
request: &str,
) -> Result<Option<MintQuote>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let mut mint_quote = query(
r#"
SELECT
@@ -1088,14 +1101,14 @@ where
WHERE request = :request"#,
)?
.bind("request", request.to_owned())
.fetch_one(&self.db)
.fetch_one(&*conn)
.await?
.map(|row| sql_row_to_mint_quote(row, vec![], vec![]))
.transpose()?;
if let Some(quote) = mint_quote.as_mut() {
let payments = get_mint_quote_payments(&self.db, &quote.id).await?;
let issuance = get_mint_quote_issuance(&self.db, &quote.id).await?;
let payments = get_mint_quote_payments(&*conn, &quote.id).await?;
let issuance = get_mint_quote_issuance(&*conn, &quote.id).await?;
quote.issuance = issuance;
quote.payments = payments;
}
@@ -1107,6 +1120,7 @@ where
&self,
request_lookup_id: &PaymentIdentifier,
) -> Result<Option<MintQuote>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let mut mint_quote = query(
r#"
SELECT
@@ -1130,15 +1144,15 @@ where
)?
.bind("request_lookup_id", request_lookup_id.to_string())
.bind("request_lookup_id_kind", request_lookup_id.kind())
.fetch_one(&self.db)
.fetch_one(&*conn)
.await?
.map(|row| sql_row_to_mint_quote(row, vec![], vec![]))
.transpose()?;
// TODO: these should use an sql join so they can be done in one query
if let Some(quote) = mint_quote.as_mut() {
let payments = get_mint_quote_payments(&self.db, &quote.id).await?;
let issuance = get_mint_quote_issuance(&self.db, &quote.id).await?;
let payments = get_mint_quote_payments(&*conn, &quote.id).await?;
let issuance = get_mint_quote_issuance(&*conn, &quote.id).await?;
quote.issuance = issuance;
quote.payments = payments;
}
@@ -1147,6 +1161,7 @@ where
}
async fn get_mint_quotes(&self) -> Result<Vec<MintQuote>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let mut mint_quotes = query(
r#"
SELECT
@@ -1166,15 +1181,15 @@ where
mint_quote
"#,
)?
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(|row| sql_row_to_mint_quote(row, vec![], vec![]))
.collect::<Result<Vec<_>, _>>()?;
for quote in mint_quotes.as_mut_slice() {
let payments = get_mint_quote_payments(&self.db, &quote.id).await?;
let issuance = get_mint_quote_issuance(&self.db, &quote.id).await?;
let payments = get_mint_quote_payments(&*conn, &quote.id).await?;
let issuance = get_mint_quote_issuance(&*conn, &quote.id).await?;
quote.issuance = issuance;
quote.payments = payments;
}
@@ -1183,6 +1198,7 @@ where
}
async fn get_melt_quote(&self, quote_id: &Uuid) -> Result<Option<mint::MeltQuote>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -1207,13 +1223,14 @@ where
"#,
)?
.bind("id", quote_id.as_hyphenated().to_string())
.fetch_one(&self.db)
.fetch_one(&*conn)
.await?
.map(sql_row_to_melt_quote)
.transpose()?)
}
async fn get_melt_quotes(&self) -> Result<Vec<mint::MeltQuote>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -1235,7 +1252,7 @@ where
melt_quote
"#,
)?
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(sql_row_to_melt_quote)
@@ -1244,13 +1261,14 @@ where
}
#[async_trait]
impl<DB> MintProofsDatabase for SQLMintDatabase<DB>
impl<RM> MintProofsDatabase for SQLMintDatabase<RM>
where
DB: DatabaseConnector,
RM: DatabasePool + 'static,
{
type Err = Error;
async fn get_proofs_by_ys(&self, ys: &[PublicKey]) -> Result<Vec<Option<Proof>>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let mut proofs = query(
r#"
SELECT
@@ -1267,7 +1285,7 @@ where
"#,
)?
.bind_vec("ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(|mut row| {
@@ -1286,6 +1304,7 @@ where
}
async fn get_proof_ys_by_quote_id(&self, quote_id: &Uuid) -> Result<Vec<PublicKey>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -1301,7 +1320,7 @@ where
"#,
)?
.bind("quote_id", quote_id.as_hyphenated().to_string())
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(sql_row_to_proof)
@@ -1310,7 +1329,8 @@ where
}
async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err> {
let mut current_states = get_current_states(&self.db, ys).await?;
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let mut current_states = get_current_states(&*conn, ys).await?;
Ok(ys.iter().map(|y| current_states.remove(y)).collect())
}
@@ -1319,6 +1339,7 @@ where
&self,
keyset_id: &Id,
) -> Result<(Proofs, Vec<Option<State>>), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -1335,7 +1356,7 @@ where
"#,
)?
.bind("keyset_id", keyset_id.to_string())
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(sql_row_to_proof_with_state)
@@ -1346,9 +1367,9 @@ where
}
#[async_trait]
impl<'a, T> MintSignatureTransaction<'a> for SQLTransaction<'a, T>
impl<RM> MintSignatureTransaction<'_> for SQLTransaction<RM>
where
T: DatabaseTransaction<'a>,
RM: DatabasePool + 'static,
{
type Err = Error;
@@ -1436,9 +1457,9 @@ where
}
#[async_trait]
impl<DB> MintSignaturesDatabase for SQLMintDatabase<DB>
impl<RM> MintSignaturesDatabase for SQLMintDatabase<RM>
where
DB: DatabaseConnector,
RM: DatabasePool + 'static,
{
type Err = Error;
@@ -1446,6 +1467,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,
@@ -1466,7 +1488,7 @@ where
.map(|b_| b_.to_bytes().to_vec())
.collect(),
)
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(|mut row| {
@@ -1490,6 +1512,7 @@ where
&self,
keyset_id: &Id,
) -> Result<Vec<BlindSignature>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -1505,7 +1528,7 @@ where
"#,
)?
.bind("keyset_id", keyset_id.to_string())
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(sql_row_to_blind_signature)
@@ -1517,6 +1540,7 @@ where
&self,
quote_id: &Uuid,
) -> Result<Vec<BlindSignature>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -1532,7 +1556,7 @@ where
"#,
)?
.bind("quote_id", quote_id.to_string())
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(sql_row_to_blind_signature)
@@ -1541,16 +1565,18 @@ where
}
#[async_trait]
impl<DB> MintDatabase<Error> for SQLMintDatabase<DB>
impl<RM> MintDatabase<Error> for SQLMintDatabase<RM>
where
DB: DatabaseConnector,
RM: DatabasePool + 'static,
{
async fn begin_transaction<'a>(
&'a self,
) -> Result<Box<dyn database::MintTransaction<'a, Error> + Send + Sync + 'a>, 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?,
}))
}

View File

@@ -8,9 +8,14 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;
use crate::database::DatabaseConnector;
/// Pool error
#[derive(thiserror::Error, Debug)]
pub enum Error<E> {
#[derive(Debug, thiserror::Error)]
pub enum Error<E>
where
E: std::error::Error + Send + Sync + 'static,
{
/// Mutex Poison Error
#[error("Internal: PoisonError")]
Poison,
@@ -24,16 +29,25 @@ pub enum Error<E> {
Resource(#[from] E),
}
/// Configuration
pub trait DatabaseConfig: Clone + Debug + Send + Sync {
/// Max resource sizes
fn max_size(&self) -> usize;
/// Default timeout
fn default_timeout(&self) -> Duration;
}
/// Trait to manage resources
pub trait ResourceManager: Debug {
pub trait DatabasePool: Debug {
/// The resource to be pooled
type Resource: Debug;
type Connection: DatabaseConnector;
/// The configuration that is needed in order to create the resource
type Config: Clone + Debug;
type Config: DatabaseConfig;
/// The error the resource may return when creating a new instance
type Error: Debug;
type Error: Debug + std::error::Error + Send + Sync + 'static;
/// Creates a new resource with a given config.
///
@@ -43,20 +57,20 @@ pub trait ResourceManager: Debug {
config: &Self::Config,
stale: Arc<AtomicBool>,
timeout: Duration,
) -> Result<Self::Resource, Error<Self::Error>>;
) -> Result<Self::Connection, Error<Self::Error>>;
/// The object is dropped
fn drop(_resource: Self::Resource) {}
fn drop(_resource: Self::Connection) {}
}
/// Generic connection pool of resources R
#[derive(Debug)]
pub struct Pool<RM>
where
RM: ResourceManager,
RM: DatabasePool,
{
config: RM::Config,
queue: Mutex<Vec<(Arc<AtomicBool>, RM::Resource)>>,
queue: Mutex<Vec<(Arc<AtomicBool>, RM::Connection)>>,
in_use: AtomicUsize,
max_size: usize,
default_timeout: Duration,
@@ -66,15 +80,24 @@ where
/// The pooled resource
pub struct PooledResource<RM>
where
RM: ResourceManager,
RM: DatabasePool,
{
resource: Option<(Arc<AtomicBool>, RM::Resource)>,
resource: Option<(Arc<AtomicBool>, RM::Connection)>,
pool: Arc<Pool<RM>>,
}
impl<RM> Debug for PooledResource<RM>
where
RM: DatabasePool,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Resource: {:?}", self.resource)
}
}
impl<RM> Drop for PooledResource<RM>
where
RM: ResourceManager,
RM: DatabasePool,
{
fn drop(&mut self) {
if let Some(resource) = self.resource.take() {
@@ -90,9 +113,9 @@ where
impl<RM> Deref for PooledResource<RM>
where
RM: ResourceManager,
RM: DatabasePool,
{
type Target = RM::Resource;
type Target = RM::Connection;
fn deref(&self) -> &Self::Target {
&self.resource.as_ref().expect("resource already dropped").1
@@ -101,7 +124,7 @@ where
impl<RM> DerefMut for PooledResource<RM>
where
RM: ResourceManager,
RM: DatabasePool,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.resource.as_mut().expect("resource already dropped").1
@@ -110,17 +133,17 @@ where
impl<RM> Pool<RM>
where
RM: ResourceManager,
RM: DatabasePool,
{
/// Creates a new pool
pub fn new(config: RM::Config, max_size: usize, default_timeout: Duration) -> Arc<Self> {
pub fn new(config: RM::Config) -> Arc<Self> {
Arc::new(Self {
default_timeout: config.default_timeout(),
max_size: config.max_size(),
config,
queue: Default::default(),
in_use: Default::default(),
waiter: Default::default(),
default_timeout,
max_size,
})
}
@@ -185,7 +208,7 @@ where
impl<RM> Drop for Pool<RM>
where
RM: ResourceManager,
RM: DatabasePool,
{
fn drop(&mut self) {
if let Ok(mut resources) = self.queue.lock() {

View File

@@ -1,7 +1,9 @@
//! SQLite Wallet Database
use std::collections::HashMap;
use std::fmt::Debug;
use std::str::FromStr;
use std::sync::Arc;
use async_trait::async_trait;
use cdk_common::common::ProofInfo;
@@ -17,7 +19,8 @@ use cdk_common::{
use tracing::instrument;
use crate::common::migrate;
use crate::database::DatabaseExecutor;
use crate::database::{ConnectionWithTransaction, DatabaseExecutor};
use crate::pool::{DatabasePool, Pool, PooledResource};
use crate::stmt::{query, Column};
use crate::{
column_as_binary, column_as_nullable_binary, column_as_nullable_number,
@@ -29,36 +32,43 @@ mod migrations;
/// Wallet SQLite Database
#[derive(Debug, Clone)]
pub struct SQLWalletDatabase<T>
pub struct SQLWalletDatabase<RM>
where
T: DatabaseExecutor,
RM: DatabasePool + 'static,
{
db: T,
pool: Arc<Pool<RM>>,
}
impl<DB> SQLWalletDatabase<DB>
impl<RM> SQLWalletDatabase<RM>
where
DB: DatabaseExecutor,
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 [`WalletSqliteDatabase`]
async fn migrate(conn: &DB) -> Result<(), Error> {
migrate(conn, DB::name(), migrations::MIGRATIONS).await?;
async fn migrate(conn: PooledResource<RM>) -> Result<(), Error> {
let tx = ConnectionWithTransaction::new(conn).await?;
migrate(&tx, RM::Connection::name(), migrations::MIGRATIONS).await?;
// Update any existing keys with missing keyset_u32 values
Self::add_keyset_u32(conn).await?;
Self::add_keyset_u32(&tx).await?;
tx.commit().await?;
Ok(())
}
async fn add_keyset_u32(conn: &DB) -> Result<(), Error> {
async fn add_keyset_u32<T>(conn: &T) -> Result<(), Error>
where
T: DatabaseExecutor,
{
// First get the keysets where keyset_u32 on key is null
let keys_without_u32: Vec<Vec<Column>> = query(
r#"
@@ -126,14 +136,16 @@ where
}
#[async_trait]
impl<T> WalletDatabase for SQLWalletDatabase<T>
impl<RM> WalletDatabase for SQLWalletDatabase<RM>
where
T: DatabaseExecutor,
RM: DatabasePool + 'static,
{
type Err = database::Error;
#[instrument(skip(self))]
async fn get_melt_quotes(&self) -> Result<Vec<wallet::MeltQuote>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -149,7 +161,7 @@ where
melt_quote
"#,
)?
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(sql_row_to_melt_quote)
@@ -212,6 +224,8 @@ where
),
};
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
query(
r#"
INSERT INTO mint
@@ -253,7 +267,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
.bind("motd", motd)
.bind("mint_time", time.map(|v| v as i64))
.bind("tos_url", tos_url)
.execute(&self.db)
.execute(&*conn)
.await?;
Ok(())
@@ -261,9 +275,11 @@ ON CONFLICT(mint_url) DO UPDATE SET
#[instrument(skip(self))]
async fn remove_mint(&self, mint_url: MintUrl) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
query(r#"DELETE FROM mint WHERE mint_url=:mint_url"#)?
.bind("mint_url", mint_url.to_string())
.execute(&self.db)
.execute(&*conn)
.await?;
Ok(())
@@ -271,6 +287,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
#[instrument(skip(self))]
async fn get_mint(&self, mint_url: MintUrl) -> Result<Option<MintInfo>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -292,7 +309,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
"#,
)?
.bind("mint_url", mint_url.to_string())
.fetch_one(&self.db)
.fetch_one(&*conn)
.await?
.map(sql_row_to_mint_info)
.transpose()?)
@@ -300,6 +317,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
#[instrument(skip(self))]
async fn get_mints(&self) -> Result<HashMap<MintUrl, Option<MintInfo>>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -320,7 +338,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
mint
"#,
)?
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(|mut row| {
@@ -340,6 +358,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
old_mint_url: MintUrl,
new_mint_url: MintUrl,
) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let tables = ["mint_quote", "proof"];
for table in &tables {
@@ -352,7 +371,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
))?
.bind("new_mint_url", new_mint_url.to_string())
.bind("old_mint_url", old_mint_url.to_string())
.execute(&self.db)
.execute(&*conn)
.await?;
}
@@ -365,6 +384,8 @@ ON CONFLICT(mint_url) DO UPDATE SET
mint_url: MintUrl,
keysets: Vec<KeySetInfo>,
) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
for keyset in keysets {
query(
r#"
@@ -384,7 +405,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
.bind("input_fee_ppk", keyset.input_fee_ppk as i64)
.bind("final_expiry", keyset.final_expiry.map(|v| v as i64))
.bind("keyset_u32", u32::from(keyset.id))
.execute(&self.db)
.execute(&*conn)
.await?;
}
@@ -396,6 +417,8 @@ ON CONFLICT(mint_url) DO UPDATE SET
&self,
mint_url: MintUrl,
) -> Result<Option<Vec<KeySetInfo>>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let keysets = query(
r#"
SELECT
@@ -410,7 +433,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
"#,
)?
.bind("mint_url", mint_url.to_string())
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(sql_row_to_keyset)
@@ -424,6 +447,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
#[instrument(skip(self), fields(keyset_id = %keyset_id))]
async fn get_keyset_by_id(&self, keyset_id: &Id) -> Result<Option<KeySetInfo>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -438,7 +462,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
"#,
)?
.bind("id", keyset_id.to_string())
.fetch_one(&self.db)
.fetch_one(&*conn)
.await?
.map(sql_row_to_keyset)
.transpose()?)
@@ -446,6 +470,7 @@ ON CONFLICT(mint_url) DO UPDATE SET
#[instrument(skip_all)]
async fn add_mint_quote(&self, quote: MintQuote) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
query(
r#"
INSERT INTO mint_quote
@@ -477,13 +502,14 @@ ON CONFLICT(id) DO UPDATE SET
.bind("payment_method", quote.payment_method.to_string())
.bind("amount_issued", quote.amount_issued.to_i64())
.bind("amount_paid", quote.amount_paid.to_i64())
.execute(&self.db).await?;
.execute(&*conn).await?;
Ok(())
}
#[instrument(skip(self))]
async fn get_mint_quote(&self, quote_id: &str) -> Result<Option<MintQuote>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -505,7 +531,7 @@ ON CONFLICT(id) DO UPDATE SET
"#,
)?
.bind("id", quote_id.to_string())
.fetch_one(&self.db)
.fetch_one(&*conn)
.await?
.map(sql_row_to_mint_quote)
.transpose()?)
@@ -513,6 +539,7 @@ ON CONFLICT(id) DO UPDATE SET
#[instrument(skip(self))]
async fn get_mint_quotes(&self) -> Result<Vec<MintQuote>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -528,7 +555,7 @@ ON CONFLICT(id) DO UPDATE SET
mint_quote
"#,
)?
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.map(sql_row_to_mint_quote)
@@ -537,9 +564,10 @@ ON CONFLICT(id) DO UPDATE SET
#[instrument(skip(self))]
async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
query(r#"DELETE FROM mint_quote WHERE id=:id"#)?
.bind("id", quote_id.to_string())
.execute(&self.db)
.execute(&*conn)
.await?;
Ok(())
@@ -547,6 +575,7 @@ ON CONFLICT(id) DO UPDATE SET
#[instrument(skip_all)]
async fn add_melt_quote(&self, quote: wallet::MeltQuote) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
query(
r#"
INSERT INTO melt_quote
@@ -570,7 +599,7 @@ ON CONFLICT(id) DO UPDATE SET
.bind("fee_reserve", u64::from(quote.fee_reserve) as i64)
.bind("state", quote.state.to_string())
.bind("expiry", quote.expiry as i64)
.execute(&self.db)
.execute(&*conn)
.await?;
Ok(())
@@ -578,6 +607,7 @@ ON CONFLICT(id) DO UPDATE SET
#[instrument(skip(self))]
async fn get_melt_quote(&self, quote_id: &str) -> Result<Option<wallet::MeltQuote>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -596,7 +626,7 @@ ON CONFLICT(id) DO UPDATE SET
"#,
)?
.bind("id", quote_id.to_owned())
.fetch_one(&self.db)
.fetch_one(&*conn)
.await?
.map(sql_row_to_melt_quote)
.transpose()?)
@@ -604,9 +634,10 @@ ON CONFLICT(id) DO UPDATE SET
#[instrument(skip(self))]
async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
query(r#"DELETE FROM melt_quote WHERE id=:id"#)?
.bind("id", quote_id.to_owned())
.execute(&self.db)
.execute(&*conn)
.await?;
Ok(())
@@ -614,6 +645,8 @@ ON CONFLICT(id) DO UPDATE SET
#[instrument(skip_all)]
async fn add_keys(&self, keyset: KeySet) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
// Recompute ID for verification
keyset.verify_id()?;
@@ -631,7 +664,7 @@ ON CONFLICT(id) DO UPDATE SET
serde_json::to_string(&keyset.keys).map_err(Error::from)?,
)
.bind("keyset_u32", u32::from(keyset.id))
.execute(&self.db)
.execute(&*conn)
.await?;
Ok(())
@@ -639,6 +672,7 @@ ON CONFLICT(id) DO UPDATE SET
#[instrument(skip(self), fields(keyset_id = %keyset_id))]
async fn get_keys(&self, keyset_id: &Id) -> Result<Option<Keys>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -648,7 +682,7 @@ ON CONFLICT(id) DO UPDATE SET
"#,
)?
.bind("id", keyset_id.to_string())
.pluck(&self.db)
.pluck(&*conn)
.await?
.map(|keys| {
let keys = column_as_string!(keys);
@@ -659,9 +693,10 @@ ON CONFLICT(id) DO UPDATE SET
#[instrument(skip(self))]
async fn remove_keys(&self, id: &Id) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
query(r#"DELETE FROM key WHERE id = :id"#)?
.bind("id", id.to_string())
.pluck(&self.db)
.pluck(&*conn)
.await?;
Ok(())
@@ -672,6 +707,10 @@ ON CONFLICT(id) DO UPDATE SET
added: Vec<ProofInfo>,
removed_ys: Vec<PublicKey>,
) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let tx = ConnectionWithTransaction::new(conn).await?;
// TODO: Use a transaction for all these operations
for proof in added {
query(
@@ -729,7 +768,7 @@ ON CONFLICT(id) DO UPDATE SET
"dleq_r",
proof.proof.dleq.as_ref().map(|dleq| dleq.r.to_secret_bytes().to_vec()),
)
.execute(&self.db).await?;
.execute(&tx).await?;
}
query(r#"DELETE FROM proof WHERE y IN (:ys)"#)?
@@ -737,9 +776,11 @@ ON CONFLICT(id) DO UPDATE SET
"ys",
removed_ys.iter().map(|y| y.to_bytes().to_vec()).collect(),
)
.execute(&self.db)
.execute(&tx)
.await?;
tx.commit().await?;
Ok(())
}
@@ -751,6 +792,7 @@ ON CONFLICT(id) DO UPDATE SET
state: Option<Vec<State>>,
spending_conditions: Option<Vec<SpendingConditions>>,
) -> Result<Vec<ProofInfo>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -770,7 +812,7 @@ ON CONFLICT(id) DO UPDATE SET
FROM proof
"#,
)?
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.filter_map(|row| {
@@ -786,10 +828,11 @@ ON CONFLICT(id) DO UPDATE SET
}
async fn update_proofs_state(&self, ys: Vec<PublicKey>, state: State) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
query("UPDATE proof SET state = :state WHERE y IN (:ys)")?
.bind_vec("ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())
.bind("state", state.to_string())
.execute(&self.db)
.execute(&*conn)
.await?;
Ok(())
@@ -797,6 +840,7 @@ ON CONFLICT(id) DO UPDATE SET
#[instrument(skip(self), fields(keyset_id = %keyset_id))]
async fn increment_keyset_counter(&self, keyset_id: &Id, count: u32) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
query(
r#"
UPDATE keyset
@@ -806,7 +850,7 @@ ON CONFLICT(id) DO UPDATE SET
)?
.bind("count", count)
.bind("id", keyset_id.to_string())
.execute(&self.db)
.execute(&*conn)
.await?;
Ok(())
@@ -814,6 +858,7 @@ ON CONFLICT(id) DO UPDATE SET
#[instrument(skip(self), fields(keyset_id = %keyset_id))]
async fn get_keyset_counter(&self, keyset_id: &Id) -> Result<Option<u32>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -825,7 +870,7 @@ ON CONFLICT(id) DO UPDATE SET
"#,
)?
.bind("id", keyset_id.to_string())
.pluck(&self.db)
.pluck(&*conn)
.await?
.map(|n| Ok::<_, Error>(column_as_number!(n)))
.transpose()?)
@@ -833,6 +878,7 @@ ON CONFLICT(id) DO UPDATE SET
#[instrument(skip(self))]
async fn add_transaction(&self, transaction: Transaction) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
let mint_url = transaction.mint_url.to_string();
let direction = transaction.direction.to_string();
let unit = transaction.unit.to_string();
@@ -876,7 +922,7 @@ ON CONFLICT(id) DO UPDATE SET
"metadata",
serde_json::to_string(&transaction.metadata).map_err(Error::from)?,
)
.execute(&self.db)
.execute(&*conn)
.await?;
Ok(())
@@ -887,6 +933,7 @@ ON CONFLICT(id) DO UPDATE SET
&self,
transaction_id: TransactionId,
) -> Result<Option<Transaction>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -906,7 +953,7 @@ ON CONFLICT(id) DO UPDATE SET
"#,
)?
.bind("id", transaction_id.as_slice().to_vec())
.fetch_one(&self.db)
.fetch_one(&*conn)
.await?
.map(sql_row_to_transaction)
.transpose()?)
@@ -919,6 +966,8 @@ ON CONFLICT(id) DO UPDATE SET
direction: Option<TransactionDirection>,
unit: Option<CurrencyUnit>,
) -> Result<Vec<Transaction>, Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
Ok(query(
r#"
SELECT
@@ -935,7 +984,7 @@ ON CONFLICT(id) DO UPDATE SET
transactions
"#,
)?
.fetch_all(&self.db)
.fetch_all(&*conn)
.await?
.into_iter()
.filter_map(|row| {
@@ -952,9 +1001,11 @@ ON CONFLICT(id) DO UPDATE SET
#[instrument(skip(self))]
async fn remove_transaction(&self, transaction_id: TransactionId) -> Result<(), Self::Err> {
let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
query(r#"DELETE FROM transactions WHERE id=:id"#)?
.bind("id", transaction_id.as_slice().to_vec())
.execute(&self.db)
.execute(&*conn)
.await?;
Ok(())