use async_trait::async_trait;
use cdk_common::database::DynMintDatabase;
use cdk_common::{Error, PublicKey};
use tracing::instrument;
use uuid::Uuid;
use crate::mint::subscription::PubSubManager;
#[async_trait]
pub trait CompensatingAction: Send + Sync {
async fn execute(&self, db: &DynMintDatabase, pubsub: &PubSubManager) -> Result<(), Error>;
fn name(&self) -> &'static str;
}
pub struct RemoveSwapSetup {
pub blinded_secrets: Vec<PublicKey>,
pub input_ys: Vec<PublicKey>,
pub operation_id: Uuid,
}
#[async_trait]
impl CompensatingAction for RemoveSwapSetup {
#[instrument(skip_all)]
async fn execute(&self, db: &DynMintDatabase, _pubsub: &PubSubManager) -> Result<(), Error> {
if self.blinded_secrets.is_empty() && self.input_ys.is_empty() {
return Ok(());
}
tracing::info!(
"Compensation: Removing swap setup ({} blinded messages, {} proofs, saga {})",
self.blinded_secrets.len(),
self.input_ys.len(),
self.operation_id
);
let mut tx = db.begin_transaction().await?;
if !self.blinded_secrets.is_empty() {
tx.delete_blinded_messages(&self.blinded_secrets).await?;
}
if !self.input_ys.is_empty() {
tx.remove_proofs(&self.input_ys, None).await?;
}
if let Err(e) = tx.delete_saga(&self.operation_id).await {
tracing::warn!(
"Failed to delete saga {} during compensation: {}",
self.operation_id,
e
);
}
tx.commit().await?;
Ok(())
}
fn name(&self) -> &'static str {
"RemoveSwapSetup"
}
}