فهرست منبع

feat: add unbalanced check

thesimplekid 6 ماه پیش
والد
کامیت
cf09a4c239
2فایلهای تغییر یافته به همراه20 افزوده شده و 1 حذف شده
  1. 3 0
      crates/cdk/src/mint/error.rs
  2. 17 1
      crates/cdk/src/mint/mod.rs

+ 3 - 0
crates/cdk/src/mint/error.rs

@@ -26,6 +26,9 @@ pub enum Error {
     /// Not engough inputs provided
     #[error("Inputs: `{0}`, Outputs: `{0}`, Fee: `{0}`")]
     InsufficientInputs(u64, u64, u64),
+    /// Transaction unbalanced
+    #[error("Inputs: `{0}`, Outputs: `{0}`, Fee: `{0}`")]
+    TransactionUnbalanced(u64, u64, u64),
     /// Duplicate proofs provided
     #[error("Duplicate proofs")]
     DuplicateProofs,

+ 17 - 1
crates/cdk/src/mint/mod.rs

@@ -711,7 +711,9 @@ impl Mint {
 
         let fee = self.get_proofs_fee(&swap_request.inputs).await?;
 
-        if proofs_total < output_total.checked_add(fee).ok_or(Error::AmountOverflow)? {
+        let total_with_fee = output_total.checked_add(fee).ok_or(Error::AmountOverflow)?;
+
+        if proofs_total < total_with_fee {
             tracing::info!(
                 "Swap request without enough inputs: {}, outputs {}, fee {}",
                 proofs_total,
@@ -725,6 +727,20 @@ impl Mint {
             ));
         }
 
+        if proofs_total != total_with_fee {
+            tracing::info!(
+                "Swap request unbalanced: {}, outputs {}, fee {}",
+                proofs_total,
+                output_total,
+                fee
+            );
+            return Err(Error::InsufficientInputs(
+                proofs_total.into(),
+                output_total.into(),
+                fee.into(),
+            ));
+        }
+
         let proof_count = swap_request.inputs.len();
 
         let input_ys = swap_request