Explorar o código

fix: dont update proof state if already spent

thesimplekid hai 9 meses
pai
achega
6705761aec
Modificáronse 2 ficheiros con 39 adicións e 18 borrados
  1. 22 7
      crates/cdk-redb/src/mint/mod.rs
  2. 17 11
      crates/cdk-sqlite/src/mint/mod.rs

+ 22 - 7
crates/cdk-redb/src/mint/mod.rs

@@ -590,14 +590,29 @@ impl MintDatabase for MintRedbDatabase {
                 .map_err(Error::from)?;
 
             for y in ys {
-                match table
-                    .insert(y.to_bytes(), state_str.as_str())
-                    .map_err(Error::from)?
+                let current_state;
+
                 {
-                    Some(state) => states.push(Some(
-                        serde_json::from_str(state.value()).map_err(Error::from)?,
-                    )),
-                    None => states.push(None),
+                    match table.get(y.to_bytes()).map_err(Error::from)? {
+                        Some(state) => {
+                            current_state =
+                                Some(serde_json::from_str(state.value()).map_err(Error::from)?)
+                        }
+                        None => current_state = None,
+                    }
+                }
+                states.push(current_state);
+
+                if current_state != Some(State::Spent) {
+                    match table
+                        .insert(y.to_bytes(), state_str.as_str())
+                        .map_err(Error::from)?
+                    {
+                        Some(state) => states.push(Some(
+                            serde_json::from_str(state.value()).map_err(Error::from)?,
+                        )),
+                        None => states.push(None),
+                    }
                 }
             }
         }

+ 17 - 11
crates/cdk-sqlite/src/mint/mod.rs

@@ -584,6 +584,7 @@ WHERE y=?;
 
         let proofs_state = proofs_state.to_string();
         for y in ys {
+            let current_state;
             let y = y.to_bytes().to_vec();
             let rec = sqlx::query(
                 r#"
@@ -599,25 +600,30 @@ WHERE y=?;
             match rec {
                 Ok(rec) => {
                     let state: String = rec.get("state");
-                    let state = State::from_str(&state).map_err(Error::from)?;
-                    states.push(Some(state));
+                    current_state = Some(State::from_str(&state).map_err(Error::from)?);
                 }
                 Err(err) => match err {
-                    sqlx::Error::RowNotFound => states.push(None),
+                    sqlx::Error::RowNotFound => {
+                        current_state = None;
+                    }
                     _ => return Err(Error::SQLX(err).into()),
                 },
             };
 
-            sqlx::query(
-                r#"
+            states.push(current_state);
+
+            if current_state != Some(State::Spent) {
+                sqlx::query(
+                    r#"
         UPDATE proof SET state = ? WHERE y = ?
         "#,
-            )
-            .bind(&proofs_state)
-            .bind(y)
-            .execute(&mut transaction)
-            .await
-            .map_err(Error::from)?;
+                )
+                .bind(&proofs_state)
+                .bind(y)
+                .execute(&mut transaction)
+                .await
+                .map_err(Error::from)?;
+            }
         }
 
         transaction.commit().await.map_err(Error::from)?;