Enhance add_proofs to fail with a custom error when the proof already exists

This commit is contained in:
Cesar Rodas
2025-06-17 17:49:31 -03:00
parent 7146cb8934
commit 150f2596e7
4 changed files with 36 additions and 10 deletions

View File

@@ -816,6 +816,26 @@ impl MintProofsDatabase for MintSqliteDatabase {
let current_time = unix_time();
// Check any previous proof, this query should return None in order to proceed storing
// Any result here would error
match query(r#"SELECT state FROM proof WHERE y IN (:ys) LIMIT 1"#)
.bind_vec(
":ys",
proofs
.iter()
.map(|y| y.y().map(|y| y.to_bytes().to_vec()))
.collect::<Result<_, _>>()?,
)
.pluck(&transaction)
.await?
.map(|state| Ok::<_, Error>(column_as_string!(&state, State::from_str)))
.transpose()?
{
Some(State::Spent) => Err(database::Error::AttemptUpdateSpentProof),
Some(_) => Err(database::Error::Duplicate),
None => Ok(()), // no previous record
}?;
for proof in proofs {
query(
r#"