Procházet zdrojové kódy

contract/test-harness: fix issue with benchmark_wasm_calls and let it silently fail when given an invalid tx

oars před 8 měsíci
rodič
revize
12a18503a2
1 změnil soubory, kde provedl 16 přidání a 13 odebrání
  1. 16 13
      src/contract/test-harness/src/lib.rs

+ 16 - 13
src/contract/test-harness/src/lib.rs

@@ -46,7 +46,7 @@ use darkfi_sdk::{
     },
     },
     pasta::pallas,
     pasta::pallas,
 };
 };
-use darkfi_serial::Encodable;
+use darkfi_serial::{serialize, Encodable};
 use num_bigint::BigUint;
 use num_bigint::BigUint;
 use sled_overlay::sled;
 use sled_overlay::sled;
 use tracing::warn;
 use tracing::warn;
@@ -211,7 +211,7 @@ impl Wallet {
         block_height: u32,
         block_height: u32,
     ) -> Result<()> {
     ) -> Result<()> {
         if self.bench_wasm {
         if self.bench_wasm {
-            benchmark_wasm_calls(callname, &self.validator, &tx, block_height).await;
+            let _ = benchmark_wasm_calls(callname, &self.validator, &tx, block_height).await;
         }
         }
 
 
         self.validator
         self.validator
@@ -325,12 +325,12 @@ async fn benchmark_wasm_calls(
     validator: &Validator,
     validator: &Validator,
     tx: &Transaction,
     tx: &Transaction,
     block_height: u32,
     block_height: u32,
-) {
-    let mut file = std::fs::OpenOptions::new().create(true).append(true).open("bench.csv").unwrap();
+) -> Result<()> {
+    let mut file = std::fs::OpenOptions::new().create(true).append(true).open("bench.csv")?;
 
 
     for (idx, call) in tx.calls.iter().enumerate() {
     for (idx, call) in tx.calls.iter().enumerate() {
         let overlay = BlockchainOverlay::new(&validator.blockchain).expect("blockchain overlay");
         let overlay = BlockchainOverlay::new(&validator.blockchain).expect("blockchain overlay");
-        let wasm = overlay.lock().unwrap().contracts.get(call.data.contract_id).unwrap();
+        let wasm = overlay.lock().unwrap().contracts.get(call.data.contract_id)?;
         let mut runtime = Runtime::new(
         let mut runtime = Runtime::new(
             &wasm,
             &wasm,
             overlay.clone(),
             overlay.clone(),
@@ -344,31 +344,34 @@ async fn benchmark_wasm_calls(
 
 
         // Write call data
         // Write call data
         let mut payload = vec![];
         let mut payload = vec![];
-        tx.calls.encode(&mut payload).unwrap();
+        tx.calls.encode(&mut payload)?;
 
 
         let mut times = [0; 3];
         let mut times = [0; 3];
         let now = std::time::Instant::now();
         let now = std::time::Instant::now();
-        let _metadata = runtime.metadata(&payload).expect("metadata");
+        let _metadata = runtime.metadata(&payload)?;
         times[0] = now.elapsed().as_micros();
         times[0] = now.elapsed().as_micros();
 
 
         let now = std::time::Instant::now();
         let now = std::time::Instant::now();
-        let update = runtime.exec(&payload).expect("exec");
+        let mut update = vec![call.data.data[0]];
+        update.append(&mut runtime.exec(&payload)?);
         times[1] = now.elapsed().as_micros();
         times[1] = now.elapsed().as_micros();
 
 
         let now = std::time::Instant::now();
         let now = std::time::Instant::now();
-        runtime.apply(&update).expect("update");
+        runtime.apply(&update)?;
         times[2] = now.elapsed().as_micros();
         times[2] = now.elapsed().as_micros();
 
 
         writeln!(
         writeln!(
             file,
             file,
-            "{}, {}, {}, {}, {}, {}",
+            "{}, {}, {}, {}, {}, {}, {}",
             callname,
             callname,
             tx.hash(),
             tx.hash(),
             idx,
             idx,
             times[0],
             times[0],
             times[1],
             times[1],
-            times[2]
-        )
-        .unwrap();
+            times[2],
+            serialize(tx).len()
+        )?;
     }
     }
+
+    Ok(())
 }
 }