mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-18 21:25:09 +01:00
Split the database trait into read and transactions. (#826)
* Split the database trait into read and transactions. The transaction traits will encapsulate all database changes and also expect READ-and-lock operations to read and lock records from the database for exclusive access, thereby avoiding race conditions. The Transaction trait expects a `rollback` operation on Drop unless the transaction has been committed. * fix: melt quote duplicate error This change stops a second melt quote from being created if there is an existing valid melt quote for an invoice already. If the first melt quote has expired then we allow for a new melt quote to be created. --------- Co-authored-by: thesimplekid <tsk@thesimplekid.com>
This commit is contained in:
@@ -6,14 +6,14 @@ use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use cdk_common::database::{self, MintAuthDatabase};
|
||||
use cdk_common::database::{self, MintAuthDatabase, MintAuthTransaction};
|
||||
use cdk_common::mint::MintKeySetInfo;
|
||||
use cdk_common::nuts::{AuthProof, BlindSignature, Id, PublicKey, State};
|
||||
use cdk_common::{AuthRequired, ProtectedEndpoint};
|
||||
use tracing::instrument;
|
||||
|
||||
use super::async_rusqlite::AsyncRusqlite;
|
||||
use super::{sqlite_row_to_blind_signature, sqlite_row_to_keyset_info};
|
||||
use super::{sqlite_row_to_blind_signature, sqlite_row_to_keyset_info, SqliteTransaction};
|
||||
use crate::column_as_string;
|
||||
use crate::common::{create_sqlite_pool, migrate};
|
||||
use crate::mint::async_rusqlite::query;
|
||||
@@ -56,11 +56,9 @@ impl MintSqliteAuthDatabase {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl MintAuthDatabase for MintSqliteAuthDatabase {
|
||||
type Err = database::Error;
|
||||
|
||||
impl MintAuthTransaction<database::Error> for SqliteTransaction<'_> {
|
||||
#[instrument(skip(self))]
|
||||
async fn set_active_keyset(&self, id: Id) -> Result<(), Self::Err> {
|
||||
async fn set_active_keyset(&mut self, id: Id) -> Result<(), database::Error> {
|
||||
tracing::info!("Setting auth keyset {id} active");
|
||||
query(
|
||||
r#"
|
||||
@@ -72,30 +70,13 @@ impl MintAuthDatabase for MintSqliteAuthDatabase {
|
||||
"#,
|
||||
)
|
||||
.bind(":id", id.to_string())
|
||||
.execute(&self.pool)
|
||||
.execute(&self.inner)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_active_keyset_id(&self) -> Result<Option<Id>, Self::Err> {
|
||||
Ok(query(
|
||||
r#"
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
keyset
|
||||
WHERE
|
||||
active = 1;
|
||||
"#,
|
||||
)
|
||||
.pluck(&self.pool)
|
||||
.await?
|
||||
.map(|id| Ok::<_, Error>(column_as_string!(id, Id::from_str, Id::from_bytes)))
|
||||
.transpose()?)
|
||||
}
|
||||
|
||||
async fn add_keyset_info(&self, keyset: MintKeySetInfo) -> Result<(), Self::Err> {
|
||||
async fn add_keyset_info(&mut self, keyset: MintKeySetInfo) -> Result<(), database::Error> {
|
||||
query(
|
||||
r#"
|
||||
INSERT INTO
|
||||
@@ -125,12 +106,159 @@ impl MintAuthDatabase for MintSqliteAuthDatabase {
|
||||
.bind(":derivation_path", keyset.derivation_path.to_string())
|
||||
.bind(":max_order", keyset.max_order)
|
||||
.bind(":derivation_path_index", keyset.derivation_path_index)
|
||||
.execute(&self.pool)
|
||||
.execute(&self.inner)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn add_proof(&mut self, proof: AuthProof) -> Result<(), database::Error> {
|
||||
if let Err(err) = query(
|
||||
r#"
|
||||
INSERT INTO proof
|
||||
(y, keyset_id, secret, c, state)
|
||||
VALUES
|
||||
(:y, :keyset_id, :secret, :c, :state)
|
||||
"#,
|
||||
)
|
||||
.bind(":y", proof.y()?.to_bytes().to_vec())
|
||||
.bind(":keyset_id", proof.keyset_id.to_string())
|
||||
.bind(":secret", proof.secret.to_string())
|
||||
.bind(":c", proof.c.to_bytes().to_vec())
|
||||
.bind(":state", "UNSPENT".to_string())
|
||||
.execute(&self.inner)
|
||||
.await
|
||||
{
|
||||
tracing::debug!("Attempting to add known proof. Skipping.... {:?}", err);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_proof_state(
|
||||
&mut self,
|
||||
y: &PublicKey,
|
||||
proofs_state: State,
|
||||
) -> Result<Option<State>, Self::Err> {
|
||||
let current_state = query(r#"SELECT state FROM proof WHERE y = :y"#)
|
||||
.bind(":y", y.to_bytes().to_vec())
|
||||
.pluck(&self.inner)
|
||||
.await?
|
||||
.map(|state| Ok::<_, Error>(column_as_string!(state, State::from_str)))
|
||||
.transpose()?;
|
||||
|
||||
query(r#"UPDATE proof SET state = :new_state WHERE state = :state AND y = :y"#)
|
||||
.bind(":y", y.to_bytes().to_vec())
|
||||
.bind(
|
||||
":state",
|
||||
current_state.as_ref().map(|state| state.to_string()),
|
||||
)
|
||||
.bind(":new_state", proofs_state.to_string())
|
||||
.execute(&self.inner)
|
||||
.await?;
|
||||
|
||||
Ok(current_state)
|
||||
}
|
||||
|
||||
async fn add_blind_signatures(
|
||||
&mut self,
|
||||
blinded_messages: &[PublicKey],
|
||||
blind_signatures: &[BlindSignature],
|
||||
) -> Result<(), database::Error> {
|
||||
for (message, signature) in blinded_messages.iter().zip(blind_signatures) {
|
||||
query(
|
||||
r#"
|
||||
INSERT
|
||||
INTO blind_signature
|
||||
(y, amount, keyset_id, c)
|
||||
VALUES
|
||||
(:y, :amount, :keyset_id, :c)
|
||||
"#,
|
||||
)
|
||||
.bind(":y", message.to_bytes().to_vec())
|
||||
.bind(":amount", u64::from(signature.amount) as i64)
|
||||
.bind(":keyset_id", signature.keyset_id.to_string())
|
||||
.bind(":c", signature.c.to_bytes().to_vec())
|
||||
.execute(&self.inner)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn add_protected_endpoints(
|
||||
&mut self,
|
||||
protected_endpoints: HashMap<ProtectedEndpoint, AuthRequired>,
|
||||
) -> Result<(), database::Error> {
|
||||
for (endpoint, auth) in protected_endpoints.iter() {
|
||||
if let Err(err) = query(
|
||||
r#"
|
||||
INSERT OR REPLACE INTO protected_endpoints
|
||||
(endpoint, auth)
|
||||
VALUES (:endpoint, :auth);
|
||||
"#,
|
||||
)
|
||||
.bind(":endpoint", serde_json::to_string(endpoint)?)
|
||||
.bind(":auth", serde_json::to_string(auth)?)
|
||||
.execute(&self.inner)
|
||||
.await
|
||||
{
|
||||
tracing::debug!(
|
||||
"Attempting to add protected endpoint. Skipping.... {:?}",
|
||||
err
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
async fn remove_protected_endpoints(
|
||||
&mut self,
|
||||
protected_endpoints: Vec<ProtectedEndpoint>,
|
||||
) -> Result<(), database::Error> {
|
||||
query(r#"DELETE FROM protected_endpoints WHERE endpoint IN (:endpoints)"#)
|
||||
.bind_vec(
|
||||
":endpoints",
|
||||
protected_endpoints
|
||||
.iter()
|
||||
.map(serde_json::to_string)
|
||||
.collect::<Result<_, _>>()?,
|
||||
)
|
||||
.execute(&self.inner)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl MintAuthDatabase for MintSqliteAuthDatabase {
|
||||
type Err = database::Error;
|
||||
|
||||
async fn begin_transaction<'a>(
|
||||
&'a self,
|
||||
) -> Result<Box<dyn MintAuthTransaction<database::Error> + Send + Sync + 'a>, database::Error>
|
||||
{
|
||||
Ok(Box::new(SqliteTransaction {
|
||||
inner: self.pool.begin().await?,
|
||||
}))
|
||||
}
|
||||
|
||||
async fn get_active_keyset_id(&self) -> Result<Option<Id>, Self::Err> {
|
||||
Ok(query(
|
||||
r#"
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
keyset
|
||||
WHERE
|
||||
active = 1;
|
||||
"#,
|
||||
)
|
||||
.pluck(&self.pool)
|
||||
.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> {
|
||||
Ok(query(
|
||||
r#"SELECT
|
||||
@@ -177,28 +305,6 @@ impl MintAuthDatabase for MintSqliteAuthDatabase {
|
||||
.collect::<Result<Vec<_>, _>>()?)
|
||||
}
|
||||
|
||||
async fn add_proof(&self, proof: AuthProof) -> Result<(), Self::Err> {
|
||||
if let Err(err) = query(
|
||||
r#"
|
||||
INSERT INTO proof
|
||||
(y, keyset_id, secret, c, state)
|
||||
VALUES
|
||||
(:y, :keyset_id, :secret, :c, :state)
|
||||
"#,
|
||||
)
|
||||
.bind(":y", proof.y()?.to_bytes().to_vec())
|
||||
.bind(":keyset_id", proof.keyset_id.to_string())
|
||||
.bind(":secret", proof.secret.to_string())
|
||||
.bind(":c", proof.c.to_bytes().to_vec())
|
||||
.bind(":state", "UNSPENT".to_string())
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
{
|
||||
tracing::debug!("Attempting to add known proof. Skipping.... {:?}", err);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err> {
|
||||
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())
|
||||
@@ -216,65 +322,6 @@ impl MintAuthDatabase for MintSqliteAuthDatabase {
|
||||
Ok(ys.iter().map(|y| current_states.remove(y)).collect())
|
||||
}
|
||||
|
||||
async fn update_proof_state(
|
||||
&self,
|
||||
y: &PublicKey,
|
||||
proofs_state: State,
|
||||
) -> Result<Option<State>, Self::Err> {
|
||||
let transaction = self.pool.begin().await?;
|
||||
|
||||
let current_state = query(r#"SELECT state FROM proof WHERE y = :y"#)
|
||||
.bind(":y", y.to_bytes().to_vec())
|
||||
.pluck(&transaction)
|
||||
.await?
|
||||
.map(|state| Ok::<_, Error>(column_as_string!(state, State::from_str)))
|
||||
.transpose()?;
|
||||
|
||||
query(r#"UPDATE proof SET state = :new_state WHERE state = :state AND y = :y"#)
|
||||
.bind(":y", y.to_bytes().to_vec())
|
||||
.bind(
|
||||
":state",
|
||||
current_state.as_ref().map(|state| state.to_string()),
|
||||
)
|
||||
.bind(":new_state", proofs_state.to_string())
|
||||
.execute(&transaction)
|
||||
.await?;
|
||||
|
||||
transaction.commit().await?;
|
||||
|
||||
Ok(current_state)
|
||||
}
|
||||
|
||||
async fn add_blind_signatures(
|
||||
&self,
|
||||
blinded_messages: &[PublicKey],
|
||||
blind_signatures: &[BlindSignature],
|
||||
) -> Result<(), Self::Err> {
|
||||
let transaction = self.pool.begin().await?;
|
||||
|
||||
for (message, signature) in blinded_messages.iter().zip(blind_signatures) {
|
||||
query(
|
||||
r#"
|
||||
INSERT
|
||||
INTO blind_signature
|
||||
(y, amount, keyset_id, c)
|
||||
VALUES
|
||||
(:y, :amount, :keyset_id, :c)
|
||||
"#,
|
||||
)
|
||||
.bind(":y", message.to_bytes().to_vec())
|
||||
.bind(":amount", u64::from(signature.amount) as i64)
|
||||
.bind(":keyset_id", signature.keyset_id.to_string())
|
||||
.bind(":c", signature.c.to_bytes().to_vec())
|
||||
.execute(&transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
transaction.commit().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_blind_signatures(
|
||||
&self,
|
||||
blinded_messages: &[PublicKey],
|
||||
@@ -319,53 +366,6 @@ impl MintAuthDatabase for MintSqliteAuthDatabase {
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn add_protected_endpoints(
|
||||
&self,
|
||||
protected_endpoints: HashMap<ProtectedEndpoint, AuthRequired>,
|
||||
) -> Result<(), Self::Err> {
|
||||
let transaction = self.pool.begin().await?;
|
||||
|
||||
for (endpoint, auth) in protected_endpoints.iter() {
|
||||
if let Err(err) = query(
|
||||
r#"
|
||||
INSERT OR REPLACE INTO protected_endpoints
|
||||
(endpoint, auth)
|
||||
VALUES (:endpoint, :auth);
|
||||
"#,
|
||||
)
|
||||
.bind(":endpoint", serde_json::to_string(endpoint)?)
|
||||
.bind(":auth", serde_json::to_string(auth)?)
|
||||
.execute(&transaction)
|
||||
.await
|
||||
{
|
||||
tracing::debug!(
|
||||
"Attempting to add protected endpoint. Skipping.... {:?}",
|
||||
err
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
transaction.commit().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
async fn remove_protected_endpoints(
|
||||
&self,
|
||||
protected_endpoints: Vec<ProtectedEndpoint>,
|
||||
) -> Result<(), Self::Err> {
|
||||
query(r#"DELETE FROM protected_endpoints WHERE endpoint IN (:endpoints)"#)
|
||||
.bind_vec(
|
||||
":endpoints",
|
||||
protected_endpoints
|
||||
.iter()
|
||||
.map(serde_json::to_string)
|
||||
.collect::<Result<_, _>>()?,
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_auth_for_endpoint(
|
||||
&self,
|
||||
protected_endpoint: ProtectedEndpoint,
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
//! In-memory database that is provided by the `cdk-sqlite` crate, mainly for testing purposes.
|
||||
use std::collections::HashMap;
|
||||
|
||||
use cdk_common::database::{
|
||||
self, MintDatabase, MintKeysDatabase, MintProofsDatabase, MintQuotesDatabase,
|
||||
};
|
||||
use cdk_common::database::{self, MintDatabase, MintKeysDatabase};
|
||||
use cdk_common::mint::{self, MintKeySetInfo, MintQuote};
|
||||
use cdk_common::nuts::{CurrencyUnit, Id, Proofs};
|
||||
use cdk_common::MintInfo;
|
||||
@@ -31,28 +29,32 @@ pub async fn new_with_state(
|
||||
mint_info: MintInfo,
|
||||
) -> Result<MintSqliteDatabase, database::Error> {
|
||||
let db = empty().await?;
|
||||
let mut tx = MintKeysDatabase::begin_transaction(&db).await?;
|
||||
|
||||
for active_keyset in active_keysets {
|
||||
db.set_active_keyset(active_keyset.0, active_keyset.1)
|
||||
tx.set_active_keyset(active_keyset.0, active_keyset.1)
|
||||
.await?;
|
||||
}
|
||||
|
||||
for keyset in keysets {
|
||||
db.add_keyset_info(keyset).await?;
|
||||
tx.add_keyset_info(keyset).await?;
|
||||
}
|
||||
tx.commit().await?;
|
||||
|
||||
let mut tx = MintDatabase::begin_transaction(&db).await?;
|
||||
|
||||
for quote in mint_quotes {
|
||||
db.add_mint_quote(quote).await?;
|
||||
tx.add_or_replace_mint_quote(quote).await?;
|
||||
}
|
||||
|
||||
for quote in melt_quotes {
|
||||
db.add_melt_quote(quote).await?;
|
||||
tx.add_melt_quote(quote).await?;
|
||||
}
|
||||
|
||||
db.add_proofs(pending_proofs, None).await?;
|
||||
db.add_proofs(spent_proofs, None).await?;
|
||||
|
||||
db.set_mint_info(mint_info).await?;
|
||||
tx.add_proofs(pending_proofs, None).await?;
|
||||
tx.add_proofs(spent_proofs, None).await?;
|
||||
tx.set_mint_info(mint_info).await?;
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(db)
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user