x 3 лет назад
Родитель
Сommit
a32d72754b
3 измененных файлов с 70 добавлено и 8 удалено
  1. 57 7
      example/dao2/contract/money/src/lib.rs
  2. 12 0
      example/dao2/src/main.rs
  3. 1 1
      src/runtime/vm_runtime.rs

+ 57 - 7
example/dao2/contract/money/src/lib.rs

@@ -4,7 +4,7 @@ use darkfi_sdk::{
     define_contract,
     msg,
     error::ContractResult,
-    pasta::pallas,
+    pasta::{pallas, group::Curve, arithmetic::CurveAffine},
     tx::ContractCall,
     util::set_return_data,
 };
@@ -92,14 +92,61 @@ fn init_contract(cid: ContractId, _ix: &[u8]) -> ContractResult {
     Ok(())
 }
 fn get_metadata(_cid: ContractId, ix: &[u8]) -> ContractResult {
-    let zk_public_values: Vec<(String, Vec<pallas::Base>)> = Vec::new();
-    let signature_public_keys: Vec<pallas::Point> = Vec::new();
+    let (call_idx, call): (u32, Vec<ContractCall>) = deserialize(ix)?;
+
+    assert!(call_idx < call.len() as u32);
+    let self_ = &call[call_idx as usize];
+
+    match MoneyFunction::from(self_.data[0]) {
+        MoneyFunction::Transfer => {
+            let data = &self_.data[1..];
+            let params: MoneyTransferParams = deserialize(data)?;
 
-    let mut metadata = Vec::new();
-    zk_public_values.encode(&mut metadata)?;
-    signature_public_keys.encode(&mut metadata)?;
-    set_return_data(&metadata)?;
+            let mut zk_public_values: Vec<(String, Vec<pallas::Base>)> = Vec::new();
+            let signature_public_keys: Vec<pallas::Point> = Vec::new();
 
+            for input in &params.inputs {
+                let value_coords = input.value_commit.to_affine().coordinates().unwrap();
+                let token_coords = input.token_commit.to_affine().coordinates().unwrap();
+                let (sig_x, sig_y) = input.signature_public.xy();
+
+                zk_public_values.push((
+                    "money-transfer-burn".to_string(),
+                    vec![
+                        input.nullifier,
+                        *value_coords.x(),
+                        *value_coords.y(),
+                        *token_coords.x(),
+                        *token_coords.y(),
+                        input.merkle_root,
+                        input.user_data_enc,
+                        sig_x,
+                        sig_y,
+                    ]
+                ));
+            }
+            for output in &params.outputs {
+                let value_coords = output.value_commit.to_affine().coordinates().unwrap();
+                let token_coords = output.token_commit.to_affine().coordinates().unwrap();
+
+                zk_public_values.push((
+                    "money-transfer-mint".to_string(),
+                    vec![
+                        output.coin,
+                        *value_coords.x(),
+                        *value_coords.y(),
+                        *token_coords.x(),
+                        *token_coords.y(),
+                    ]
+                ));
+            }
+
+            let mut metadata = Vec::new();
+            zk_public_values.encode(&mut metadata)?;
+            signature_public_keys.encode(&mut metadata)?;
+            set_return_data(&metadata)?;
+        }
+    }
     Ok(())
 }
 fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
@@ -110,6 +157,9 @@ fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
 
     match MoneyFunction::from(self_.data[0]) {
         MoneyFunction::Transfer => {
+            let data = &self_.data[1..];
+            let params: MoneyTransferParams = deserialize(data)?;
+
             let update = MoneyTransferUpdate {};
 
             let mut update_data = Vec::new();

+ 12 - 0
example/dao2/src/main.rs

@@ -440,6 +440,18 @@ async fn main() -> BoxResult<()> {
         Transaction { calls, proofs, signatures }
     };
 
+    //// Validator
+
+    validate(
+        &tx,
+        &dao_wasm_bytes,
+        dao_contract_id,
+        &money_wasm_bytes,
+        money_contract_id,
+        &blockchain,
+        &zk_bins,
+    )
+    .expect("validate failed");
 
     //let func_call = builder.build(&zk_bins)?;
     //let func_calls = vec![func_call];

+ 1 - 1
src/runtime/vm_runtime.rs

@@ -41,7 +41,7 @@ use crate::{blockchain::Blockchain, Error, Result};
 const MEMORY: &str = "memory";
 
 /// Gas limit for a contract
-const GAS_LIMIT: u64 = 200000;
+const GAS_LIMIT: u64 = 200000000;
 
 #[derive(Clone, Copy)]
 pub enum ContractSection {