mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-19 13:44:55 +01:00
Time time series (#708)
* feat: Add created_time and paid_time fields to MintQuote struct * feat: Add serde default of 0 for created_time in MintQuote * feat: Add created_time and paid_time to MintQuote and MeltQuote structs * feat: Add paid_time update when setting melt quote state to Paid * fix: Update melt quote state with current Unix timestamp * feat: Add paid_time update for mint quote when state is set to Paid * feat: Add issued_time field to MintQuote conversion from SQLite row * feat: Add issued_time tracking for MintQuoteState::Issued state * feat: Add migration script for mint time of quotes * feat: Add timestamp columns to mint_quote and melt_quote tables * feat: Add timestamp columns to `add_mint_quote` method * refactor: Improve code formatting and readability in mint quote state update logic * feat: Add created_time and paid_time columns to melt_quote query * feat: time on mint and melt quotes * feat: Add migration script for mint created time signature feat: Add created_time column to blind_signature table feat: Add created_time to blind_signature insertion feat: Add created_time column to proof table and update insert query feat: time on mint and melt quotes * feat: Add new table to track blind signature creation time * feat: Add timestamp tracking for proofs in ReDB database * feat: redb proof time * chore: fmt
This commit is contained in:
@@ -15,6 +15,7 @@ use cdk_common::mint::{self, MintKeySetInfo, MintQuote};
|
||||
use cdk_common::nut00::ProofsMethods;
|
||||
use cdk_common::nut05::QuoteState;
|
||||
use cdk_common::secret::Secret;
|
||||
use cdk_common::util::unix_time;
|
||||
use cdk_common::{
|
||||
Amount, BlindSignature, BlindSignatureDleq, CurrencyUnit, Id, MeltBolt11Request,
|
||||
MeltQuoteState, MintInfo, MintQuoteState, PaymentMethod, Proof, Proofs, PublicKey, SecretKey,
|
||||
@@ -373,22 +374,28 @@ impl MintQuotesDatabase for MintSqliteDatabase {
|
||||
let res = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO mint_quote
|
||||
(id, amount, unit, request, state, expiry, request_lookup_id, pubkey)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
(id, amount, unit, request, state, expiry, request_lookup_id, pubkey, created_time, paid_time, issued_time)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
amount = excluded.amount,
|
||||
unit = excluded.unit,
|
||||
request = excluded.request,
|
||||
state = excluded.state,
|
||||
expiry = excluded.expiry,
|
||||
request_lookup_id = excluded.request_lookup_id
|
||||
request_lookup_id = excluded.request_lookup_id,
|
||||
created_time = excluded.created_time,
|
||||
paid_time = excluded.paid_time,
|
||||
issued_time = excluded.issued_time
|
||||
ON CONFLICT(request_lookup_id) DO UPDATE SET
|
||||
amount = excluded.amount,
|
||||
unit = excluded.unit,
|
||||
request = excluded.request,
|
||||
state = excluded.state,
|
||||
expiry = excluded.expiry,
|
||||
id = excluded.id
|
||||
id = excluded.id,
|
||||
created_time = excluded.created_time,
|
||||
paid_time = excluded.paid_time,
|
||||
issued_time = excluded.issued_time
|
||||
"#,
|
||||
)
|
||||
.bind(quote.id.to_string())
|
||||
@@ -399,6 +406,9 @@ ON CONFLICT(request_lookup_id) DO UPDATE SET
|
||||
.bind(quote.expiry as i64)
|
||||
.bind(quote.request_lookup_id)
|
||||
.bind(quote.pubkey.map(|p| p.to_string()))
|
||||
.bind(quote.created_time as i64)
|
||||
.bind(quote.paid_time.map(|t| t as i64))
|
||||
.bind(quote.issued_time.map(|t| t as i64))
|
||||
.execute(&mut *transaction)
|
||||
.await;
|
||||
|
||||
@@ -554,15 +564,43 @@ WHERE id=?;
|
||||
}
|
||||
};
|
||||
|
||||
let update = sqlx::query(
|
||||
r#"
|
||||
UPDATE mint_quote SET state = ? WHERE id = ?
|
||||
"#,
|
||||
)
|
||||
.bind(state.to_string())
|
||||
.bind(quote_id.as_hyphenated())
|
||||
.execute(&mut *transaction)
|
||||
.await;
|
||||
let update_query = match state {
|
||||
MintQuoteState::Paid => {
|
||||
r#"UPDATE mint_quote SET state = ?, paid_time = ? WHERE id = ?"#
|
||||
}
|
||||
MintQuoteState::Issued => {
|
||||
r#"UPDATE mint_quote SET state = ?, issued_time = ? WHERE id = ?"#
|
||||
}
|
||||
_ => r#"UPDATE mint_quote SET state = ? WHERE id = ?"#,
|
||||
};
|
||||
|
||||
let current_time = unix_time();
|
||||
|
||||
let update = match state {
|
||||
MintQuoteState::Paid => {
|
||||
sqlx::query(update_query)
|
||||
.bind(state.to_string())
|
||||
.bind(current_time as i64)
|
||||
.bind(quote_id.as_hyphenated())
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
}
|
||||
MintQuoteState::Issued => {
|
||||
sqlx::query(update_query)
|
||||
.bind(state.to_string())
|
||||
.bind(current_time as i64)
|
||||
.bind(quote_id.as_hyphenated())
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
}
|
||||
_ => {
|
||||
sqlx::query(update_query)
|
||||
.bind(state.to_string())
|
||||
.bind(quote_id.as_hyphenated())
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
}
|
||||
};
|
||||
|
||||
match update {
|
||||
Ok(_) => {
|
||||
@@ -684,8 +722,8 @@ WHERE id=?
|
||||
let res = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO melt_quote
|
||||
(id, unit, amount, request, fee_reserve, state, expiry, payment_preimage, request_lookup_id, msat_to_pay)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
(id, unit, amount, request, fee_reserve, state, expiry, payment_preimage, request_lookup_id, msat_to_pay, created_time, paid_time)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
unit = excluded.unit,
|
||||
amount = excluded.amount,
|
||||
@@ -695,7 +733,9 @@ ON CONFLICT(id) DO UPDATE SET
|
||||
expiry = excluded.expiry,
|
||||
payment_preimage = excluded.payment_preimage,
|
||||
request_lookup_id = excluded.request_lookup_id,
|
||||
msat_to_pay = excluded.msat_to_pay
|
||||
msat_to_pay = excluded.msat_to_pay,
|
||||
created_time = excluded.created_time,
|
||||
paid_time = excluded.paid_time
|
||||
ON CONFLICT(request_lookup_id) DO UPDATE SET
|
||||
unit = excluded.unit,
|
||||
amount = excluded.amount,
|
||||
@@ -704,7 +744,9 @@ ON CONFLICT(request_lookup_id) DO UPDATE SET
|
||||
state = excluded.state,
|
||||
expiry = excluded.expiry,
|
||||
payment_preimage = excluded.payment_preimage,
|
||||
id = excluded.id;
|
||||
id = excluded.id,
|
||||
created_time = excluded.created_time,
|
||||
paid_time = excluded.paid_time;
|
||||
"#,
|
||||
)
|
||||
.bind(quote.id.to_string())
|
||||
@@ -717,6 +759,8 @@ ON CONFLICT(request_lookup_id) DO UPDATE SET
|
||||
.bind(quote.payment_preimage)
|
||||
.bind(quote.request_lookup_id)
|
||||
.bind(quote.msat_to_pay.map(|a| u64::from(a) as i64))
|
||||
.bind(quote.created_time as i64)
|
||||
.bind(quote.paid_time.map(|t| t as i64))
|
||||
.execute(&mut *transaction)
|
||||
.await;
|
||||
|
||||
@@ -831,15 +875,28 @@ WHERE id=?;
|
||||
}
|
||||
};
|
||||
|
||||
let rec = sqlx::query(
|
||||
r#"
|
||||
UPDATE melt_quote SET state = ? WHERE id = ?
|
||||
"#,
|
||||
)
|
||||
.bind(state.to_string())
|
||||
.bind(quote_id.as_hyphenated())
|
||||
.execute(&mut *transaction)
|
||||
.await;
|
||||
let update_query = if state == MeltQuoteState::Paid {
|
||||
r#"UPDATE melt_quote SET state = ?, paid_time = ? WHERE id = ?"#
|
||||
} else {
|
||||
r#"UPDATE melt_quote SET state = ? WHERE id = ?"#
|
||||
};
|
||||
|
||||
let current_time = unix_time();
|
||||
|
||||
let rec = if state == MeltQuoteState::Paid {
|
||||
sqlx::query(update_query)
|
||||
.bind(state.to_string())
|
||||
.bind(current_time as i64)
|
||||
.bind(quote_id.as_hyphenated())
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
} else {
|
||||
sqlx::query(update_query)
|
||||
.bind(state.to_string())
|
||||
.bind(quote_id.as_hyphenated())
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
};
|
||||
|
||||
match rec {
|
||||
Ok(_) => {
|
||||
@@ -978,12 +1035,14 @@ impl MintProofsDatabase for MintSqliteDatabase {
|
||||
|
||||
async fn add_proofs(&self, proofs: Proofs, quote_id: Option<Uuid>) -> Result<(), Self::Err> {
|
||||
let mut transaction = self.pool.begin().await.map_err(Error::from)?;
|
||||
let current_time = unix_time();
|
||||
|
||||
for proof in proofs {
|
||||
let result = sqlx::query(
|
||||
r#"
|
||||
INSERT OR IGNORE INTO proof
|
||||
(y, amount, keyset_id, secret, c, witness, state, quote_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
|
||||
(y, amount, keyset_id, secret, c, witness, state, quote_id, created_time)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
|
||||
"#,
|
||||
)
|
||||
.bind(proof.y()?.to_bytes().to_vec())
|
||||
@@ -994,6 +1053,7 @@ VALUES (?, ?, ?, ?, ?, ?, ?, ?);
|
||||
.bind(proof.witness.map(|w| serde_json::to_string(&w).unwrap()))
|
||||
.bind("UNSPENT")
|
||||
.bind(quote_id.map(|q| q.hyphenated()))
|
||||
.bind(current_time as i64)
|
||||
.execute(&mut *transaction)
|
||||
.await;
|
||||
|
||||
@@ -1292,12 +1352,14 @@ impl MintSignaturesDatabase for MintSqliteDatabase {
|
||||
quote_id: Option<Uuid>,
|
||||
) -> Result<(), Self::Err> {
|
||||
let mut transaction = self.pool.begin().await.map_err(Error::from)?;
|
||||
let current_time = unix_time();
|
||||
|
||||
for (message, signature) in blinded_messages.iter().zip(blinded_signatures) {
|
||||
let res = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO blind_signature
|
||||
(y, amount, keyset_id, c, quote_id, dleq_e, dleq_s)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?);
|
||||
(y, amount, keyset_id, c, quote_id, dleq_e, dleq_s, created_time)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
|
||||
"#,
|
||||
)
|
||||
.bind(message.to_bytes().to_vec())
|
||||
@@ -1307,6 +1369,7 @@ VALUES (?, ?, ?, ?, ?, ?, ?);
|
||||
.bind(quote_id.map(|q| q.hyphenated()))
|
||||
.bind(signature.dleq.as_ref().map(|dleq| dleq.e.to_secret_hex()))
|
||||
.bind(signature.dleq.as_ref().map(|dleq| dleq.s.to_secret_hex()))
|
||||
.bind(current_time as i64)
|
||||
.execute(&mut *transaction)
|
||||
.await;
|
||||
|
||||
@@ -1624,6 +1687,10 @@ fn sqlite_row_to_mint_quote(row: SqliteRow) -> Result<MintQuote, Error> {
|
||||
row.try_get("request_lookup_id").map_err(Error::from)?;
|
||||
let row_pubkey: Option<String> = row.try_get("pubkey").map_err(Error::from)?;
|
||||
|
||||
let row_created_time: i64 = row.try_get("created_time").map_err(Error::from)?;
|
||||
let row_paid_time: Option<i64> = row.try_get("paid_time").map_err(Error::from)?;
|
||||
let row_issued_time: Option<i64> = row.try_get("issued_time").map_err(Error::from)?;
|
||||
|
||||
let request_lookup_id = match row_request_lookup_id {
|
||||
Some(id) => id,
|
||||
None => match Bolt11Invoice::from_str(&row_request) {
|
||||
@@ -1645,6 +1712,9 @@ fn sqlite_row_to_mint_quote(row: SqliteRow) -> Result<MintQuote, Error> {
|
||||
expiry: row_expiry as u64,
|
||||
request_lookup_id,
|
||||
pubkey,
|
||||
created_time: row_created_time as u64,
|
||||
paid_time: row_paid_time.map(|p| p as u64),
|
||||
issued_time: row_issued_time.map(|p| p as u64),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1664,6 +1734,9 @@ fn sqlite_row_to_melt_quote(row: SqliteRow) -> Result<mint::MeltQuote, Error> {
|
||||
|
||||
let row_msat_to_pay: Option<i64> = row.try_get("msat_to_pay").map_err(Error::from)?;
|
||||
|
||||
let row_created_time: i64 = row.try_get("created_time").map_err(Error::from)?;
|
||||
let row_paid_time: Option<i64> = row.try_get("paid_time").map_err(Error::from)?;
|
||||
|
||||
Ok(mint::MeltQuote {
|
||||
id: row_id.into_uuid(),
|
||||
amount: Amount::from(row_amount as u64),
|
||||
@@ -1675,6 +1748,8 @@ fn sqlite_row_to_melt_quote(row: SqliteRow) -> Result<mint::MeltQuote, Error> {
|
||||
payment_preimage: row_preimage,
|
||||
request_lookup_id,
|
||||
msat_to_pay: row_msat_to_pay.map(|a| Amount::from(a as u64)),
|
||||
created_time: row_created_time as u64,
|
||||
paid_time: row_paid_time.map(|p| p as u64),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user