|
@@ -23,42 +23,92 @@ pub mod test;
|
|
|
#[cfg(feature = "auth")]
|
|
|
pub use auth::MintAuthDatabase;
|
|
|
|
|
|
+/// KeysDatabaseWriter
|
|
|
+#[async_trait]
|
|
|
+pub trait KeysDatabaseTransaction<'a, Error>: DbTransactionFinalizer<Err = Error> {
|
|
|
+ /// Add Active Keyset
|
|
|
+ async fn set_active_keyset(&mut self, unit: CurrencyUnit, id: Id) -> Result<(), Error>;
|
|
|
+
|
|
|
+ /// Add [`MintKeySetInfo`]
|
|
|
+ async fn add_keyset_info(&mut self, keyset: MintKeySetInfo) -> Result<(), Error>;
|
|
|
+}
|
|
|
+
|
|
|
/// Mint Keys Database trait
|
|
|
#[async_trait]
|
|
|
pub trait KeysDatabase {
|
|
|
/// Mint Keys Database Error
|
|
|
type Err: Into<Error> + From<Error>;
|
|
|
|
|
|
- /// Add Active Keyset
|
|
|
- async fn set_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Self::Err>;
|
|
|
+ /// Beings a transaction
|
|
|
+ async fn begin_transaction<'a>(
|
|
|
+ &'a self,
|
|
|
+ ) -> Result<Box<dyn KeysDatabaseTransaction<'a, Self::Err> + Send + Sync + 'a>, Error>;
|
|
|
+
|
|
|
/// Get Active Keyset
|
|
|
async fn get_active_keyset_id(&self, unit: &CurrencyUnit) -> Result<Option<Id>, Self::Err>;
|
|
|
+
|
|
|
/// Get all Active Keyset
|
|
|
async fn get_active_keysets(&self) -> Result<HashMap<CurrencyUnit, Id>, Self::Err>;
|
|
|
- /// Add [`MintKeySetInfo`]
|
|
|
- async fn add_keyset_info(&self, keyset: MintKeySetInfo) -> Result<(), Self::Err>;
|
|
|
+
|
|
|
/// Get [`MintKeySetInfo`]
|
|
|
async fn get_keyset_info(&self, id: &Id) -> Result<Option<MintKeySetInfo>, Self::Err>;
|
|
|
+
|
|
|
/// Get [`MintKeySetInfo`]s
|
|
|
async fn get_keyset_infos(&self) -> Result<Vec<MintKeySetInfo>, Self::Err>;
|
|
|
}
|
|
|
|
|
|
-/// Mint Quote Database trait
|
|
|
+/// Mint Quote Database writer trait
|
|
|
#[async_trait]
|
|
|
-pub trait QuotesDatabase {
|
|
|
+pub trait QuotesTransaction<'a> {
|
|
|
/// Mint Quotes Database Error
|
|
|
type Err: Into<Error> + From<Error>;
|
|
|
|
|
|
+ /// Get [`MintMintQuote`] and lock it for update in this transaction
|
|
|
+ async fn get_mint_quote(&mut self, quote_id: &Uuid)
|
|
|
+ -> Result<Option<MintMintQuote>, Self::Err>;
|
|
|
/// Add [`MintMintQuote`]
|
|
|
- async fn add_mint_quote(&self, quote: MintMintQuote) -> Result<(), Self::Err>;
|
|
|
- /// Get [`MintMintQuote`]
|
|
|
- async fn get_mint_quote(&self, quote_id: &Uuid) -> Result<Option<MintMintQuote>, Self::Err>;
|
|
|
+ async fn add_mint_quote(&mut self, quote: MintMintQuote) -> Result<(), Self::Err>;
|
|
|
/// Update state of [`MintMintQuote`]
|
|
|
async fn update_mint_quote_state(
|
|
|
- &self,
|
|
|
+ &mut self,
|
|
|
quote_id: &Uuid,
|
|
|
state: MintQuoteState,
|
|
|
) -> Result<MintQuoteState, Self::Err>;
|
|
|
+ /// Remove [`MintMintQuote`]
|
|
|
+ async fn remove_mint_quote(&mut self, quote_id: &Uuid) -> Result<(), Self::Err>;
|
|
|
+ /// Get [`mint::MeltQuote`] and lock it for update in this transaction
|
|
|
+ async fn get_melt_quote(
|
|
|
+ &mut self,
|
|
|
+ quote_id: &Uuid,
|
|
|
+ ) -> Result<Option<mint::MeltQuote>, Self::Err>;
|
|
|
+ /// Add [`mint::MeltQuote`]
|
|
|
+ async fn add_melt_quote(&mut self, quote: mint::MeltQuote) -> Result<(), Self::Err>;
|
|
|
+ /// Update [`mint::MeltQuote`] state
|
|
|
+ ///
|
|
|
+ /// It is expected for this function to fail if the state is already set to the new state
|
|
|
+ async fn update_melt_quote_state(
|
|
|
+ &mut self,
|
|
|
+ quote_id: &Uuid,
|
|
|
+ new_state: MeltQuoteState,
|
|
|
+ ) -> Result<(MeltQuoteState, mint::MeltQuote), Self::Err>;
|
|
|
+ /// Remove [`mint::MeltQuote`]
|
|
|
+ async fn remove_melt_quote(&mut self, quote_id: &Uuid) -> Result<(), Self::Err>;
|
|
|
+ /// Get all [`MintMintQuote`]s and lock it for update in this transaction
|
|
|
+ async fn get_mint_quote_by_request(
|
|
|
+ &mut self,
|
|
|
+ request: &str,
|
|
|
+ ) -> Result<Option<MintMintQuote>, Self::Err>;
|
|
|
+}
|
|
|
+
|
|
|
+/// Mint Quote Database trait
|
|
|
+#[async_trait]
|
|
|
+pub trait QuotesDatabase {
|
|
|
+ /// Mint Quotes Database Error
|
|
|
+ type Err: Into<Error> + From<Error>;
|
|
|
+
|
|
|
+ /// Get [`MintMintQuote`]
|
|
|
+ async fn get_mint_quote(&self, quote_id: &Uuid) -> Result<Option<MintMintQuote>, Self::Err>;
|
|
|
+
|
|
|
/// Get all [`MintMintQuote`]s
|
|
|
async fn get_mint_quote_by_request(
|
|
|
&self,
|
|
@@ -76,30 +126,15 @@ pub trait QuotesDatabase {
|
|
|
&self,
|
|
|
state: MintQuoteState,
|
|
|
) -> Result<Vec<MintMintQuote>, Self::Err>;
|
|
|
- /// Remove [`MintMintQuote`]
|
|
|
- async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err>;
|
|
|
-
|
|
|
- /// Add [`mint::MeltQuote`]
|
|
|
- async fn add_melt_quote(&self, quote: mint::MeltQuote) -> Result<(), Self::Err>;
|
|
|
/// Get [`mint::MeltQuote`]
|
|
|
async fn get_melt_quote(&self, quote_id: &Uuid) -> Result<Option<mint::MeltQuote>, Self::Err>;
|
|
|
- /// Update [`mint::MeltQuote`] state
|
|
|
- ///
|
|
|
- /// It is expected for this function to fail if the state is already set to the new state
|
|
|
- async fn update_melt_quote_state(
|
|
|
- &self,
|
|
|
- quote_id: &Uuid,
|
|
|
- new_state: MeltQuoteState,
|
|
|
- ) -> Result<(MeltQuoteState, mint::MeltQuote), Self::Err>;
|
|
|
/// Get all [`mint::MeltQuote`]s
|
|
|
async fn get_melt_quotes(&self) -> Result<Vec<mint::MeltQuote>, Self::Err>;
|
|
|
- /// Remove [`mint::MeltQuote`]
|
|
|
- async fn remove_melt_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err>;
|
|
|
}
|
|
|
|
|
|
-/// Mint Proof Database trait
|
|
|
+/// Mint Proof Transaction trait
|
|
|
#[async_trait]
|
|
|
-pub trait ProofsDatabase {
|
|
|
+pub trait ProofsTransaction<'a> {
|
|
|
/// Mint Proof Database Error
|
|
|
type Err: Into<Error> + From<Error>;
|
|
|
|
|
@@ -107,7 +142,21 @@ pub trait ProofsDatabase {
|
|
|
///
|
|
|
/// Adds proofs to the database. The database should error if the proof already exits, with a
|
|
|
/// `AttemptUpdateSpentProof` if the proof is already spent or a `Duplicate` error otherwise.
|
|
|
- async fn add_proofs(&self, proof: Proofs, quote_id: Option<Uuid>) -> Result<(), Self::Err>;
|
|
|
+ async fn add_proofs(&mut self, proof: Proofs, quote_id: Option<Uuid>) -> Result<(), Self::Err>;
|
|
|
+ /// Updates the proofs to a given states and return the previous states
|
|
|
+ async fn update_proofs_states(
|
|
|
+ &mut self,
|
|
|
+ ys: &[PublicKey],
|
|
|
+ proofs_state: State,
|
|
|
+ ) -> Result<Vec<Option<State>>, Self::Err>;
|
|
|
+}
|
|
|
+
|
|
|
+/// Mint Proof Database trait
|
|
|
+#[async_trait]
|
|
|
+pub trait ProofsDatabase {
|
|
|
+ /// Mint Proof Database Error
|
|
|
+ type Err: Into<Error> + From<Error>;
|
|
|
+
|
|
|
/// Remove [`Proofs`]
|
|
|
async fn remove_proofs(
|
|
|
&self,
|
|
@@ -120,12 +169,6 @@ pub trait ProofsDatabase {
|
|
|
async fn get_proof_ys_by_quote_id(&self, quote_id: &Uuid) -> Result<Vec<PublicKey>, Self::Err>;
|
|
|
/// Get [`Proofs`] state
|
|
|
async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err>;
|
|
|
- /// Get [`Proofs`] state
|
|
|
- async fn update_proofs_states(
|
|
|
- &self,
|
|
|
- ys: &[PublicKey],
|
|
|
- proofs_state: State,
|
|
|
- ) -> Result<Vec<Option<State>>, Self::Err>;
|
|
|
/// Get [`Proofs`] by state
|
|
|
async fn get_proofs_by_keyset_id(
|
|
|
&self,
|
|
@@ -134,18 +177,32 @@ pub trait ProofsDatabase {
|
|
|
}
|
|
|
|
|
|
#[async_trait]
|
|
|
-/// Mint Signatures Database trait
|
|
|
-pub trait SignaturesDatabase {
|
|
|
+/// Mint Signatures Transaction trait
|
|
|
+pub trait SignaturesTransaction<'a> {
|
|
|
/// Mint Signature Database Error
|
|
|
type Err: Into<Error> + From<Error>;
|
|
|
|
|
|
/// Add [`BlindSignature`]
|
|
|
async fn add_blind_signatures(
|
|
|
- &self,
|
|
|
+ &mut self,
|
|
|
blinded_messages: &[PublicKey],
|
|
|
blind_signatures: &[BlindSignature],
|
|
|
quote_id: Option<Uuid>,
|
|
|
) -> Result<(), Self::Err>;
|
|
|
+
|
|
|
+ /// Get [`BlindSignature`]s
|
|
|
+ async fn get_blind_signatures(
|
|
|
+ &mut self,
|
|
|
+ blinded_messages: &[PublicKey],
|
|
|
+ ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
|
|
|
+}
|
|
|
+
|
|
|
+#[async_trait]
|
|
|
+/// Mint Signatures Database trait
|
|
|
+pub trait SignaturesDatabase {
|
|
|
+ /// Mint Signature Database Error
|
|
|
+ type Err: Into<Error> + From<Error>;
|
|
|
+
|
|
|
/// Get [`BlindSignature`]s
|
|
|
async fn get_blind_signatures(
|
|
|
&self,
|
|
@@ -163,13 +220,42 @@ pub trait SignaturesDatabase {
|
|
|
) -> Result<Vec<BlindSignature>, Self::Err>;
|
|
|
}
|
|
|
|
|
|
+#[async_trait]
|
|
|
+/// Commit and Rollback
|
|
|
+pub trait DbTransactionFinalizer {
|
|
|
+ /// Mint Signature Database Error
|
|
|
+ type Err: Into<Error> + From<Error>;
|
|
|
+
|
|
|
+ /// Commits all the changes into the database
|
|
|
+ async fn commit(self: Box<Self>) -> Result<(), Self::Err>;
|
|
|
+
|
|
|
+ /// Rollbacks the write transaction
|
|
|
+ async fn rollback(self: Box<Self>) -> Result<(), Self::Err>;
|
|
|
+}
|
|
|
+
|
|
|
+/// Base database writer
|
|
|
+#[async_trait]
|
|
|
+pub trait Transaction<'a, Error>:
|
|
|
+ DbTransactionFinalizer<Err = Error>
|
|
|
+ + QuotesTransaction<'a, Err = Error>
|
|
|
+ + SignaturesTransaction<'a, Err = Error>
|
|
|
+ + ProofsTransaction<'a, Err = Error>
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
/// Mint Database trait
|
|
|
#[async_trait]
|
|
|
pub trait Database<Error>:
|
|
|
QuotesDatabase<Err = Error> + ProofsDatabase<Err = Error> + SignaturesDatabase<Err = Error>
|
|
|
{
|
|
|
+ /// Beings a transaction
|
|
|
+ async fn begin_transaction<'a>(
|
|
|
+ &'a self,
|
|
|
+ ) -> Result<Box<dyn Transaction<'a, Error> + Send + Sync + 'a>, Error>;
|
|
|
+
|
|
|
/// Set [`MintInfo`]
|
|
|
async fn set_mint_info(&self, mint_info: MintInfo) -> Result<(), Error>;
|
|
|
+
|
|
|
/// Get [`MintInfo`]
|
|
|
async fn get_mint_info(&self) -> Result<MintInfo, Error>;
|
|
|
|