Add explicit rollback. (#947)

The proof_writer implicit rollback on Drop is too slow for our tests causing
race conditions with Postgres.

This commit enhances the rollback logic and makes it explicit
This commit is contained in:
C
2025-08-11 19:54:37 -03:00
committed by GitHub
parent b48604e02c
commit 7af054520e
2 changed files with 18 additions and 2 deletions

View File

@@ -662,6 +662,7 @@ impl Mint {
"Lightning payment for quote {} failed.",
melt_request.quote()
);
proof_writer.rollback().await?;
return Err(Error::PaymentFailed);
}
MeltQuoteState::Pending => {

View File

@@ -143,13 +143,28 @@ impl ProofWriter {
}
/// Rollback all changes in this ProofWriter consuming it.
pub async fn rollback(mut self, tx: &mut Tx<'_, '_>) -> Result<(), Error> {
pub async fn rollback(mut self) -> Result<(), Error> {
let db = if let Some(db) = self.db.take() {
db
} else {
return Ok(());
};
let mut tx = db.begin_transaction().await?;
let (ys, original_states) = if let Some(proofs) = self.proof_original_states.take() {
proofs.into_iter().unzip::<_, _, Vec<_>, Vec<_>>()
} else {
return Ok(());
};
reset_proofs_to_original_state(tx, &ys, original_states).await?;
tracing::info!(
"Rollback {} proofs to their original states {:?}",
ys.len(),
original_states
);
reset_proofs_to_original_state(&mut tx, &ys, original_states).await?;
tx.commit().await?;
Ok(())
}
}