Просмотр исходного кода

dao_demo: quick error handling for proof verification

lunar-mining 3 лет назад
Родитель
Сommit
3f01a0bd1c
3 измененных файлов с 15 добавлено и 4 удалено
  1. 2 0
      bin/dao/daod/src/error.rs
  2. 1 1
      bin/dao/daod/src/main.rs
  3. 12 3
      bin/dao/daod/src/util.rs

+ 2 - 0
bin/dao/daod/src/error.rs

@@ -16,6 +16,8 @@ pub enum DaoError {
     StateNotFound,
     #[error("InternalError")]
     Darkfi(#[from] darkfi::error::Error),
+    #[error("Verify proof failed: '{0}', '{0}'")]
+    VerifyProofFailed(usize, String),
 }
 
 pub type DaoResult<T> = std::result::Result<T, DaoError>;

+ 1 - 1
bin/dao/daod/src/main.rs

@@ -371,7 +371,7 @@ impl Client {
             update.apply(&mut self.states);
         }
 
-        tx.zk_verify(&self.zk_bins);
+        tx.zk_verify(&self.zk_bins)?;
         tx.verify_sigs();
 
         Ok(())

+ 12 - 3
bin/dao/daod/src/util.rs

@@ -22,6 +22,8 @@ use darkfi::{
     Error,
 };
 
+use crate::error::{DaoError, DaoResult};
+
 /// Parse pallas::Base from a base58-encoded string
 pub fn parse_b58(s: &str) -> std::result::Result<pallas::Base, darkfi::Error> {
     let bytes = bs58::decode(s).into_vec()?;
@@ -128,7 +130,7 @@ impl Transaction {
     /// Verify ZK contracts for the entire tx
     /// In real code, we could parallelize this for loop
     /// TODO: fix use of unwrap with Result type stuff
-    pub fn zk_verify(&self, zk_bins: &ZkContractTable) {
+    pub fn zk_verify(&self, zk_bins: &ZkContractTable) -> DaoResult<()> {
         for func_call in &self.func_calls {
             let proofs_public_vals = &func_call.call_data.zk_public_values();
 
@@ -146,17 +148,24 @@ impl Transaction {
                     ZkContractInfo::Binary(info) => {
                         let verifying_key = &info.verifying_key;
                         let verify_result = proof.verify(&verifying_key, public_vals);
-                        assert!(verify_result.is_ok(), "verify proof[{}]='{}' failed", i, key);
+                        if verify_result.is_err() {
+                            return Err(DaoError::VerifyProofFailed(i, key.to_string()))
+                        }
+                        //assert!(verify_result.is_ok(), "verify proof[{}]='{}' failed", i, key);
                     }
                     ZkContractInfo::Native(info) => {
                         let verifying_key = &info.verifying_key;
                         let verify_result = proof.verify(&verifying_key, public_vals);
-                        assert!(verify_result.is_ok(), "verify proof[{}]='{}' failed", i, key);
+                        if verify_result.is_err() {
+                            return Err(DaoError::VerifyProofFailed(i, key.to_string()))
+                        }
+                        //assert!(verify_result.is_ok(), "verify proof[{}]='{}' failed", i, key);
                     }
                 };
                 debug!(target: "demo", "zk_verify({}) passed [i={}]", key, i);
             }
         }
+        Ok(())
     }
 
     pub fn verify_sigs(&self) {