|
@@ -9,8 +9,9 @@
|
|
|
//! clients in a pool and expose them to an asynchronous environment, making them compatible with
|
|
//! clients in a pool and expose them to an asynchronous environment, making them compatible with
|
|
|
//! Mint.
|
|
//! Mint.
|
|
|
use std::collections::HashMap;
|
|
use std::collections::HashMap;
|
|
|
-use std::marker::PhantomData;
|
|
|
|
|
|
|
+use std::fmt::Debug;
|
|
|
use std::str::FromStr;
|
|
use std::str::FromStr;
|
|
|
|
|
+use std::sync::Arc;
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
use async_trait::async_trait;
|
|
|
use bitcoin::bip32::DerivationPath;
|
|
use bitcoin::bip32::DerivationPath;
|
|
@@ -38,7 +39,8 @@ use tracing::instrument;
|
|
|
use uuid::Uuid;
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::common::migrate;
|
|
use crate::common::migrate;
|
|
|
-use crate::database::{DatabaseConnector, DatabaseExecutor, DatabaseTransaction};
|
|
|
|
|
|
|
+use crate::database::{ConnectionWithTransaction, DatabaseConnector, DatabaseExecutor};
|
|
|
|
|
+use crate::pool::{Pool, PooledResource, ResourceManager};
|
|
|
use crate::stmt::{query, Column};
|
|
use crate::stmt::{query, Column};
|
|
|
use crate::{
|
|
use crate::{
|
|
|
column_as_nullable_number, column_as_nullable_string, column_as_number, column_as_string,
|
|
column_as_nullable_number, column_as_nullable_string, column_as_number, column_as_string,
|
|
@@ -57,20 +59,25 @@ pub use auth::SQLMintAuthDatabase;
|
|
|
|
|
|
|
|
/// Mint SQL Database
|
|
/// Mint SQL Database
|
|
|
#[derive(Debug, Clone)]
|
|
#[derive(Debug, Clone)]
|
|
|
-pub struct SQLMintDatabase<DB>
|
|
|
|
|
|
|
+pub struct SQLMintDatabase<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
DB: DatabaseConnector,
|
|
DB: DatabaseConnector,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E>,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
- db: DB,
|
|
|
|
|
|
|
+ pool: Arc<Pool<RM>>,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// SQL Transaction Writer
|
|
/// SQL Transaction Writer
|
|
|
-pub struct SQLTransaction<'a, T>
|
|
|
|
|
|
|
+pub struct SQLTransaction<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
- T: DatabaseTransaction<'a>,
|
|
|
|
|
|
|
+ DB: DatabaseConnector + 'static,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E> + 'static,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
- inner: T,
|
|
|
|
|
- _phantom: PhantomData<&'a ()>,
|
|
|
|
|
|
|
+ inner: ConnectionWithTransaction<DB, PooledResource<RM>>,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
#[inline(always)]
|
|
@@ -115,23 +122,28 @@ where
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-impl<DB> SQLMintDatabase<DB>
|
|
|
|
|
|
|
+impl<DB, RM, C, E> SQLMintDatabase<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
- DB: DatabaseConnector,
|
|
|
|
|
|
|
+ DB: DatabaseConnector + 'static,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E> + 'static,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
/// Creates a new instance
|
|
/// Creates a new instance
|
|
|
pub async fn new<X>(db: X) -> Result<Self, Error>
|
|
pub async fn new<X>(db: X) -> Result<Self, Error>
|
|
|
where
|
|
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
|
|
/// Migrate
|
|
|
- async fn migrate(conn: &DB) -> Result<(), Error> {
|
|
|
|
|
- let tx = conn.begin().await?;
|
|
|
|
|
|
|
+ async fn migrate(conn: PooledResource<RM>) -> Result<(), Error> {
|
|
|
|
|
+ let tx = ConnectionWithTransaction::new(conn).await?;
|
|
|
migrate(&tx, DB::name(), MIGRATIONS).await?;
|
|
migrate(&tx, DB::name(), MIGRATIONS).await?;
|
|
|
tx.commit().await?;
|
|
tx.commit().await?;
|
|
|
Ok(())
|
|
Ok(())
|
|
@@ -142,9 +154,10 @@ where
|
|
|
where
|
|
where
|
|
|
R: serde::de::DeserializeOwned,
|
|
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"#)?
|
|
let value = column_as_string!(query(r#"SELECT value FROM config WHERE id = :id LIMIT 1"#)?
|
|
|
.bind("id", id.to_owned())
|
|
.bind("id", id.to_owned())
|
|
|
- .pluck(&self.db)
|
|
|
|
|
|
|
+ .pluck(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.ok_or(Error::UnknownQuoteTTL)?);
|
|
.ok_or(Error::UnknownQuoteTTL)?);
|
|
|
|
|
|
|
@@ -153,9 +166,12 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
-impl<'a, T> database::MintProofsTransaction<'a> for SQLTransaction<'a, T>
|
|
|
|
|
|
|
+impl<DB, RM, C, E> database::MintProofsTransaction<'_> for SQLTransaction<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
- T: DatabaseTransaction<'a>,
|
|
|
|
|
|
|
+ DB: DatabaseConnector,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E>,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
type Err = Error;
|
|
type Err = Error;
|
|
|
|
|
|
|
@@ -267,9 +283,12 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
-impl<'a, T> database::MintTransaction<'a, Error> for SQLTransaction<'a, T>
|
|
|
|
|
|
|
+impl<DB, RM, C, E> database::MintTransaction<'_, Error> for SQLTransaction<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
- T: DatabaseTransaction<'a>,
|
|
|
|
|
|
|
+ DB: DatabaseConnector,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E>,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
async fn set_mint_info(&mut self, mint_info: MintInfo) -> Result<(), Error> {
|
|
async fn set_mint_info(&mut self, mint_info: MintInfo) -> Result<(), Error> {
|
|
|
Ok(set_to_config(&self.inner, "mint_info", &mint_info).await?)
|
|
Ok(set_to_config(&self.inner, "mint_info", &mint_info).await?)
|
|
@@ -281,18 +300,21 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
-impl<'a, T> MintDbWriterFinalizer for SQLTransaction<'a, T>
|
|
|
|
|
|
|
+impl<DB, RM, C, E> MintDbWriterFinalizer for SQLTransaction<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
- T: DatabaseTransaction<'a>,
|
|
|
|
|
|
|
+ DB: DatabaseConnector,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E>,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
type Err = Error;
|
|
type Err = Error;
|
|
|
|
|
|
|
|
async fn commit(self: Box<Self>) -> Result<(), 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> {
|
|
async fn rollback(self: Box<Self>) -> Result<(), Error> {
|
|
|
- Ok(self.inner.rollback().await?)
|
|
|
|
|
|
|
+ self.inner.rollback().await
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -357,9 +379,12 @@ WHERE quote_id=:quote_id
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
-impl<'a, T> MintKeyDatabaseTransaction<'a, Error> for SQLTransaction<'a, T>
|
|
|
|
|
|
|
+impl<DB, RM, C, E> MintKeyDatabaseTransaction<'_, Error> for SQLTransaction<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
- T: DatabaseTransaction<'a>,
|
|
|
|
|
|
|
+ DB: DatabaseConnector,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E>,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
async fn add_keyset_info(&mut self, keyset: MintKeySetInfo) -> Result<(), Error> {
|
|
async fn add_keyset_info(&mut self, keyset: MintKeySetInfo) -> Result<(), Error> {
|
|
|
query(
|
|
query(
|
|
@@ -416,9 +441,12 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
-impl<DB> MintKeysDatabase for SQLMintDatabase<DB>
|
|
|
|
|
|
|
+impl<DB, RM, C, E> MintKeysDatabase for SQLMintDatabase<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
- DB: DatabaseConnector,
|
|
|
|
|
|
|
+ DB: DatabaseConnector + 'static,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E> + 'static,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
type Err = Error;
|
|
type Err = Error;
|
|
|
|
|
|
|
@@ -426,16 +454,19 @@ where
|
|
|
&'a self,
|
|
&'a self,
|
|
|
) -> Result<Box<dyn MintKeyDatabaseTransaction<'a, Error> + Send + Sync + 'a>, Error> {
|
|
) -> Result<Box<dyn MintKeyDatabaseTransaction<'a, Error> + Send + Sync + 'a>, Error> {
|
|
|
Ok(Box::new(SQLTransaction {
|
|
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> {
|
|
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(
|
|
Ok(
|
|
|
query(r#" SELECT id FROM keyset WHERE active = 1 AND unit IS :unit"#)?
|
|
query(r#" SELECT id FROM keyset WHERE active = 1 AND unit IS :unit"#)?
|
|
|
.bind("unit", unit.to_string())
|
|
.bind("unit", unit.to_string())
|
|
|
- .pluck(&self.db)
|
|
|
|
|
|
|
+ .pluck(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.map(|id| match id {
|
|
.map(|id| match id {
|
|
|
Column::Text(text) => Ok(Id::from_str(&text)?),
|
|
Column::Text(text) => Ok(Id::from_str(&text)?),
|
|
@@ -447,8 +478,9 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async fn get_active_keysets(&self) -> Result<HashMap<CurrencyUnit, Id>, Self::Err> {
|
|
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"#)?
|
|
Ok(query(r#"SELECT id, unit FROM keyset WHERE active = 1"#)?
|
|
|
- .fetch_all(&self.db)
|
|
|
|
|
|
|
+ .fetch_all(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.into_iter()
|
|
.into_iter()
|
|
|
.map(|row| {
|
|
.map(|row| {
|
|
@@ -461,6 +493,7 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async fn get_keyset_info(&self, id: &Id) -> Result<Option<MintKeySetInfo>, Self::Err> {
|
|
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(
|
|
Ok(query(
|
|
|
r#"SELECT
|
|
r#"SELECT
|
|
|
id,
|
|
id,
|
|
@@ -477,13 +510,14 @@ where
|
|
|
WHERE id=:id"#,
|
|
WHERE id=:id"#,
|
|
|
)?
|
|
)?
|
|
|
.bind("id", id.to_string())
|
|
.bind("id", id.to_string())
|
|
|
- .fetch_one(&self.db)
|
|
|
|
|
|
|
+ .fetch_one(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.map(sql_row_to_keyset_info)
|
|
.map(sql_row_to_keyset_info)
|
|
|
.transpose()?)
|
|
.transpose()?)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async fn get_keyset_infos(&self) -> Result<Vec<MintKeySetInfo>, Self::Err> {
|
|
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(
|
|
Ok(query(
|
|
|
r#"SELECT
|
|
r#"SELECT
|
|
|
id,
|
|
id,
|
|
@@ -499,7 +533,7 @@ where
|
|
|
keyset
|
|
keyset
|
|
|
"#,
|
|
"#,
|
|
|
)?
|
|
)?
|
|
|
- .fetch_all(&self.db)
|
|
|
|
|
|
|
+ .fetch_all(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.into_iter()
|
|
.into_iter()
|
|
|
.map(sql_row_to_keyset_info)
|
|
.map(sql_row_to_keyset_info)
|
|
@@ -508,9 +542,12 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
-impl<'a, T> MintQuotesTransaction<'a> for SQLTransaction<'a, T>
|
|
|
|
|
|
|
+impl<DB, RM, C, E> MintQuotesTransaction<'_> for SQLTransaction<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
- T: DatabaseTransaction<'a>,
|
|
|
|
|
|
|
+ DB: DatabaseConnector,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E>,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
type Err = Error;
|
|
type Err = Error;
|
|
|
|
|
|
|
@@ -1028,15 +1065,20 @@ VALUES (:quote_id, :amount, :timestamp);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
-impl<DB> MintQuotesDatabase for SQLMintDatabase<DB>
|
|
|
|
|
|
|
+impl<DB, RM, C, E> MintQuotesDatabase for SQLMintDatabase<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
DB: DatabaseConnector,
|
|
DB: DatabaseConnector,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E>,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
type Err = Error;
|
|
type Err = Error;
|
|
|
|
|
|
|
|
async fn get_mint_quote(&self, quote_id: &Uuid) -> Result<Option<MintQuote>, Self::Err> {
|
|
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(
|
|
Ok(query(
|
|
|
r#"
|
|
r#"
|
|
@@ -1058,7 +1100,7 @@ where
|
|
|
WHERE id = :id"#,
|
|
WHERE id = :id"#,
|
|
|
)?
|
|
)?
|
|
|
.bind("id", quote_id.as_hyphenated().to_string())
|
|
.bind("id", quote_id.as_hyphenated().to_string())
|
|
|
- .fetch_one(&self.db)
|
|
|
|
|
|
|
+ .fetch_one(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.map(|row| sql_row_to_mint_quote(row, payments, issuance))
|
|
.map(|row| sql_row_to_mint_quote(row, payments, issuance))
|
|
|
.transpose()?)
|
|
.transpose()?)
|
|
@@ -1068,6 +1110,7 @@ where
|
|
|
&self,
|
|
&self,
|
|
|
request: &str,
|
|
request: &str,
|
|
|
) -> Result<Option<MintQuote>, Self::Err> {
|
|
) -> Result<Option<MintQuote>, Self::Err> {
|
|
|
|
|
+ let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
|
|
|
let mut mint_quote = query(
|
|
let mut mint_quote = query(
|
|
|
r#"
|
|
r#"
|
|
|
SELECT
|
|
SELECT
|
|
@@ -1088,14 +1131,14 @@ where
|
|
|
WHERE request = :request"#,
|
|
WHERE request = :request"#,
|
|
|
)?
|
|
)?
|
|
|
.bind("request", request.to_owned())
|
|
.bind("request", request.to_owned())
|
|
|
- .fetch_one(&self.db)
|
|
|
|
|
|
|
+ .fetch_one(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.map(|row| sql_row_to_mint_quote(row, vec![], vec![]))
|
|
.map(|row| sql_row_to_mint_quote(row, vec![], vec![]))
|
|
|
.transpose()?;
|
|
.transpose()?;
|
|
|
|
|
|
|
|
if let Some(quote) = mint_quote.as_mut() {
|
|
if let Some(quote) = mint_quote.as_mut() {
|
|
|
- let payments = get_mint_quote_payments(&self.db, "e.id).await?;
|
|
|
|
|
- let issuance = get_mint_quote_issuance(&self.db, "e.id).await?;
|
|
|
|
|
|
|
+ let payments = get_mint_quote_payments(&*conn, "e.id).await?;
|
|
|
|
|
+ let issuance = get_mint_quote_issuance(&*conn, "e.id).await?;
|
|
|
quote.issuance = issuance;
|
|
quote.issuance = issuance;
|
|
|
quote.payments = payments;
|
|
quote.payments = payments;
|
|
|
}
|
|
}
|
|
@@ -1107,6 +1150,7 @@ where
|
|
|
&self,
|
|
&self,
|
|
|
request_lookup_id: &PaymentIdentifier,
|
|
request_lookup_id: &PaymentIdentifier,
|
|
|
) -> Result<Option<MintQuote>, Self::Err> {
|
|
) -> Result<Option<MintQuote>, Self::Err> {
|
|
|
|
|
+ let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
|
|
|
let mut mint_quote = query(
|
|
let mut mint_quote = query(
|
|
|
r#"
|
|
r#"
|
|
|
SELECT
|
|
SELECT
|
|
@@ -1130,15 +1174,15 @@ where
|
|
|
)?
|
|
)?
|
|
|
.bind("request_lookup_id", request_lookup_id.to_string())
|
|
.bind("request_lookup_id", request_lookup_id.to_string())
|
|
|
.bind("request_lookup_id_kind", request_lookup_id.kind())
|
|
.bind("request_lookup_id_kind", request_lookup_id.kind())
|
|
|
- .fetch_one(&self.db)
|
|
|
|
|
|
|
+ .fetch_one(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.map(|row| sql_row_to_mint_quote(row, vec![], vec![]))
|
|
.map(|row| sql_row_to_mint_quote(row, vec![], vec![]))
|
|
|
.transpose()?;
|
|
.transpose()?;
|
|
|
|
|
|
|
|
// TODO: these should use an sql join so they can be done in one query
|
|
// TODO: these should use an sql join so they can be done in one query
|
|
|
if let Some(quote) = mint_quote.as_mut() {
|
|
if let Some(quote) = mint_quote.as_mut() {
|
|
|
- let payments = get_mint_quote_payments(&self.db, "e.id).await?;
|
|
|
|
|
- let issuance = get_mint_quote_issuance(&self.db, "e.id).await?;
|
|
|
|
|
|
|
+ let payments = get_mint_quote_payments(&*conn, "e.id).await?;
|
|
|
|
|
+ let issuance = get_mint_quote_issuance(&*conn, "e.id).await?;
|
|
|
quote.issuance = issuance;
|
|
quote.issuance = issuance;
|
|
|
quote.payments = payments;
|
|
quote.payments = payments;
|
|
|
}
|
|
}
|
|
@@ -1147,6 +1191,7 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async fn get_mint_quotes(&self) -> Result<Vec<MintQuote>, Self::Err> {
|
|
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(
|
|
let mut mint_quotes = query(
|
|
|
r#"
|
|
r#"
|
|
|
SELECT
|
|
SELECT
|
|
@@ -1166,15 +1211,15 @@ where
|
|
|
mint_quote
|
|
mint_quote
|
|
|
"#,
|
|
"#,
|
|
|
)?
|
|
)?
|
|
|
- .fetch_all(&self.db)
|
|
|
|
|
|
|
+ .fetch_all(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.into_iter()
|
|
.into_iter()
|
|
|
.map(|row| sql_row_to_mint_quote(row, vec![], vec![]))
|
|
.map(|row| sql_row_to_mint_quote(row, vec![], vec![]))
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
|
|
|
|
for quote in mint_quotes.as_mut_slice() {
|
|
for quote in mint_quotes.as_mut_slice() {
|
|
|
- let payments = get_mint_quote_payments(&self.db, "e.id).await?;
|
|
|
|
|
- let issuance = get_mint_quote_issuance(&self.db, "e.id).await?;
|
|
|
|
|
|
|
+ let payments = get_mint_quote_payments(&*conn, "e.id).await?;
|
|
|
|
|
+ let issuance = get_mint_quote_issuance(&*conn, "e.id).await?;
|
|
|
quote.issuance = issuance;
|
|
quote.issuance = issuance;
|
|
|
quote.payments = payments;
|
|
quote.payments = payments;
|
|
|
}
|
|
}
|
|
@@ -1183,6 +1228,7 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async fn get_melt_quote(&self, quote_id: &Uuid) -> Result<Option<mint::MeltQuote>, Self::Err> {
|
|
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(
|
|
Ok(query(
|
|
|
r#"
|
|
r#"
|
|
|
SELECT
|
|
SELECT
|
|
@@ -1207,13 +1253,14 @@ where
|
|
|
"#,
|
|
"#,
|
|
|
)?
|
|
)?
|
|
|
.bind("id", quote_id.as_hyphenated().to_string())
|
|
.bind("id", quote_id.as_hyphenated().to_string())
|
|
|
- .fetch_one(&self.db)
|
|
|
|
|
|
|
+ .fetch_one(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.map(sql_row_to_melt_quote)
|
|
.map(sql_row_to_melt_quote)
|
|
|
.transpose()?)
|
|
.transpose()?)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async fn get_melt_quotes(&self) -> Result<Vec<mint::MeltQuote>, Self::Err> {
|
|
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(
|
|
Ok(query(
|
|
|
r#"
|
|
r#"
|
|
|
SELECT
|
|
SELECT
|
|
@@ -1235,7 +1282,7 @@ where
|
|
|
melt_quote
|
|
melt_quote
|
|
|
"#,
|
|
"#,
|
|
|
)?
|
|
)?
|
|
|
- .fetch_all(&self.db)
|
|
|
|
|
|
|
+ .fetch_all(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.into_iter()
|
|
.into_iter()
|
|
|
.map(sql_row_to_melt_quote)
|
|
.map(sql_row_to_melt_quote)
|
|
@@ -1244,13 +1291,17 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
-impl<DB> MintProofsDatabase for SQLMintDatabase<DB>
|
|
|
|
|
|
|
+impl<DB, RM, C, E> MintProofsDatabase for SQLMintDatabase<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
DB: DatabaseConnector,
|
|
DB: DatabaseConnector,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E>,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
type Err = Error;
|
|
type Err = Error;
|
|
|
|
|
|
|
|
async fn get_proofs_by_ys(&self, ys: &[PublicKey]) -> Result<Vec<Option<Proof>>, Self::Err> {
|
|
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(
|
|
let mut proofs = query(
|
|
|
r#"
|
|
r#"
|
|
|
SELECT
|
|
SELECT
|
|
@@ -1267,7 +1318,7 @@ where
|
|
|
"#,
|
|
"#,
|
|
|
)?
|
|
)?
|
|
|
.bind_vec("ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())
|
|
.bind_vec("ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())
|
|
|
- .fetch_all(&self.db)
|
|
|
|
|
|
|
+ .fetch_all(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.into_iter()
|
|
.into_iter()
|
|
|
.map(|mut row| {
|
|
.map(|mut row| {
|
|
@@ -1286,6 +1337,7 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async fn get_proof_ys_by_quote_id(&self, quote_id: &Uuid) -> Result<Vec<PublicKey>, Self::Err> {
|
|
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(
|
|
Ok(query(
|
|
|
r#"
|
|
r#"
|
|
|
SELECT
|
|
SELECT
|
|
@@ -1301,7 +1353,7 @@ where
|
|
|
"#,
|
|
"#,
|
|
|
)?
|
|
)?
|
|
|
.bind("quote_id", quote_id.as_hyphenated().to_string())
|
|
.bind("quote_id", quote_id.as_hyphenated().to_string())
|
|
|
- .fetch_all(&self.db)
|
|
|
|
|
|
|
+ .fetch_all(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.into_iter()
|
|
.into_iter()
|
|
|
.map(sql_row_to_proof)
|
|
.map(sql_row_to_proof)
|
|
@@ -1310,7 +1362,8 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err> {
|
|
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())
|
|
Ok(ys.iter().map(|y| current_states.remove(y)).collect())
|
|
|
}
|
|
}
|
|
@@ -1319,6 +1372,7 @@ where
|
|
|
&self,
|
|
&self,
|
|
|
keyset_id: &Id,
|
|
keyset_id: &Id,
|
|
|
) -> Result<(Proofs, Vec<Option<State>>), Self::Err> {
|
|
) -> Result<(Proofs, Vec<Option<State>>), Self::Err> {
|
|
|
|
|
+ let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
|
|
|
Ok(query(
|
|
Ok(query(
|
|
|
r#"
|
|
r#"
|
|
|
SELECT
|
|
SELECT
|
|
@@ -1335,7 +1389,7 @@ where
|
|
|
"#,
|
|
"#,
|
|
|
)?
|
|
)?
|
|
|
.bind("keyset_id", keyset_id.to_string())
|
|
.bind("keyset_id", keyset_id.to_string())
|
|
|
- .fetch_all(&self.db)
|
|
|
|
|
|
|
+ .fetch_all(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.into_iter()
|
|
.into_iter()
|
|
|
.map(sql_row_to_proof_with_state)
|
|
.map(sql_row_to_proof_with_state)
|
|
@@ -1346,9 +1400,12 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
-impl<'a, T> MintSignatureTransaction<'a> for SQLTransaction<'a, T>
|
|
|
|
|
|
|
+impl<DB, RM, C, E> MintSignatureTransaction<'_> for SQLTransaction<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
- T: DatabaseTransaction<'a>,
|
|
|
|
|
|
|
+ DB: DatabaseConnector,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E>,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
type Err = Error;
|
|
type Err = Error;
|
|
|
|
|
|
|
@@ -1436,9 +1493,12 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
-impl<DB> MintSignaturesDatabase for SQLMintDatabase<DB>
|
|
|
|
|
|
|
+impl<DB, RM, C, E> MintSignaturesDatabase for SQLMintDatabase<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
DB: DatabaseConnector,
|
|
DB: DatabaseConnector,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E>,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
type Err = Error;
|
|
type Err = Error;
|
|
|
|
|
|
|
@@ -1446,6 +1506,7 @@ where
|
|
|
&self,
|
|
&self,
|
|
|
blinded_messages: &[PublicKey],
|
|
blinded_messages: &[PublicKey],
|
|
|
) -> Result<Vec<Option<BlindSignature>>, Self::Err> {
|
|
) -> Result<Vec<Option<BlindSignature>>, Self::Err> {
|
|
|
|
|
+ let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
|
|
|
let mut blinded_signatures = query(
|
|
let mut blinded_signatures = query(
|
|
|
r#"SELECT
|
|
r#"SELECT
|
|
|
keyset_id,
|
|
keyset_id,
|
|
@@ -1466,7 +1527,7 @@ where
|
|
|
.map(|b_| b_.to_bytes().to_vec())
|
|
.map(|b_| b_.to_bytes().to_vec())
|
|
|
.collect(),
|
|
.collect(),
|
|
|
)
|
|
)
|
|
|
- .fetch_all(&self.db)
|
|
|
|
|
|
|
+ .fetch_all(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.into_iter()
|
|
.into_iter()
|
|
|
.map(|mut row| {
|
|
.map(|mut row| {
|
|
@@ -1490,6 +1551,7 @@ where
|
|
|
&self,
|
|
&self,
|
|
|
keyset_id: &Id,
|
|
keyset_id: &Id,
|
|
|
) -> Result<Vec<BlindSignature>, Self::Err> {
|
|
) -> Result<Vec<BlindSignature>, Self::Err> {
|
|
|
|
|
+ let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
|
|
|
Ok(query(
|
|
Ok(query(
|
|
|
r#"
|
|
r#"
|
|
|
SELECT
|
|
SELECT
|
|
@@ -1505,7 +1567,7 @@ where
|
|
|
"#,
|
|
"#,
|
|
|
)?
|
|
)?
|
|
|
.bind("keyset_id", keyset_id.to_string())
|
|
.bind("keyset_id", keyset_id.to_string())
|
|
|
- .fetch_all(&self.db)
|
|
|
|
|
|
|
+ .fetch_all(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.into_iter()
|
|
.into_iter()
|
|
|
.map(sql_row_to_blind_signature)
|
|
.map(sql_row_to_blind_signature)
|
|
@@ -1517,6 +1579,7 @@ where
|
|
|
&self,
|
|
&self,
|
|
|
quote_id: &Uuid,
|
|
quote_id: &Uuid,
|
|
|
) -> Result<Vec<BlindSignature>, Self::Err> {
|
|
) -> Result<Vec<BlindSignature>, Self::Err> {
|
|
|
|
|
+ let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
|
|
|
Ok(query(
|
|
Ok(query(
|
|
|
r#"
|
|
r#"
|
|
|
SELECT
|
|
SELECT
|
|
@@ -1532,7 +1595,7 @@ where
|
|
|
"#,
|
|
"#,
|
|
|
)?
|
|
)?
|
|
|
.bind("quote_id", quote_id.to_string())
|
|
.bind("quote_id", quote_id.to_string())
|
|
|
- .fetch_all(&self.db)
|
|
|
|
|
|
|
+ .fetch_all(&*conn)
|
|
|
.await?
|
|
.await?
|
|
|
.into_iter()
|
|
.into_iter()
|
|
|
.map(sql_row_to_blind_signature)
|
|
.map(sql_row_to_blind_signature)
|
|
@@ -1541,16 +1604,21 @@ where
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
|
-impl<DB> MintDatabase<Error> for SQLMintDatabase<DB>
|
|
|
|
|
|
|
+impl<DB, RM, C, E> MintDatabase<Error> for SQLMintDatabase<DB, RM, C, E>
|
|
|
where
|
|
where
|
|
|
- DB: DatabaseConnector,
|
|
|
|
|
|
|
+ DB: DatabaseConnector + 'static,
|
|
|
|
|
+ RM: ResourceManager<Resource = DB, Config = C, Error = E> + 'static,
|
|
|
|
|
+ C: Debug + Clone + Send + Sync,
|
|
|
|
|
+ E: Debug + std::error::Error + Send + Sync + 'static,
|
|
|
{
|
|
{
|
|
|
async fn begin_transaction<'a>(
|
|
async fn begin_transaction<'a>(
|
|
|
&'a self,
|
|
&'a self,
|
|
|
) -> Result<Box<dyn database::MintTransaction<'a, Error> + Send + Sync + 'a>, Error> {
|
|
) -> Result<Box<dyn database::MintTransaction<'a, Error> + Send + Sync + 'a>, Error> {
|
|
|
Ok(Box::new(SQLTransaction {
|
|
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?,
|
|
|
}))
|
|
}))
|
|
|
}
|
|
}
|
|
|
|
|
|