Răsfoiți Sursa

daod/ money_contract: implement CallDataBase for CallData and cleanup

lunar-mining 4 ani în urmă
părinte
comite
cb60ad3dfc

+ 1 - 68
bin/daod/src/money_contract/mod.rs

@@ -3,71 +3,4 @@
 pub mod state;
 pub mod state;
 pub mod transfer;
 pub mod transfer;
 
 
-/*
- money-contract/
- state.apply()
-      transfer/
-          Builder
-          Partial *
-          FuncCall
-
-/////////////////////////////////////////////////
-
-let token_id = pallas::Base::random(&mut OsRng);
-
-let builder = TransactionBuilder {
-    clear_inputs: vec![TransactionBuilderClearInputInfo {
-        value: 110,
-        token_id,
-        signature_secret: cashier_signature_secret,
-    }],
-    inputs: vec![],
-    outputs: vec![TransactionBuilderOutputInfo {
-        value: 110,
-        token_id,
-        public: keypair.public,
-    }],
-};
-
-let start = Instant::now();
-let mint_pk = ProvingKey::build(11, &MintContract::default());
-debug!("Mint PK: [{:?}]", start.elapsed());
-let start = Instant::now();
-let burn_pk = ProvingKey::build(11, &BurnContract::default());
-debug!("Burn PK: [{:?}]", start.elapsed());
-let tx = builder.build(&mint_pk, &burn_pk)?;
-
-tx.verify(&money_state.mint_vk, &money_state.burn_vk)?;
-
-let _note = tx.outputs[0].enc_note.decrypt(&keypair.secret)?;
-
-let update = state_transition(&money_state, tx)?;
-money_state.apply(update);
-
-// Now spend
-let owncoin = &money_state.own_coins[0];
-let note = &owncoin.note;
-let leaf_position = owncoin.leaf_position;
-let root = money_state.tree.root(0).unwrap();
-let merkle_path = money_state.tree.authentication_path(leaf_position, &root).unwrap();
-
-let builder = TransactionBuilder {
-    clear_inputs: vec![],
-    inputs: vec![TransactionBuilderInputInfo {
-        leaf_position,
-        merkle_path,
-        secret: keypair.secret,
-        note: note.clone(),
-    }],
-    outputs: vec![TransactionBuilderOutputInfo {
-        value: 110,
-        token_id,
-        public: keypair.public,
-    }],
-};
-
-let tx = builder.build(&mint_pk, &burn_pk)?;
-
-let update = state_transition(&money_state, tx)?;
-money_state.apply(update);
-*/
+pub use state::State;

+ 4 - 21
bin/daod/src/money_contract/state.rs

@@ -9,14 +9,14 @@ use darkfi::{
 };
 };
 
 
 /// The state machine, held in memory.
 /// The state machine, held in memory.
-struct State {
+pub struct State {
     /// The entire Merkle tree state
     /// The entire Merkle tree state
-    tree: BridgeTree<MerkleNode, MERKLE_DEPTH>,
+    pub tree: BridgeTree<MerkleNode, MERKLE_DEPTH>,
     /// List of all previous and the current Merkle roots.
     /// List of all previous and the current Merkle roots.
     /// This is the hashed value of all the children.
     /// This is the hashed value of all the children.
-    merkle_roots: Vec<MerkleNode>,
+    pub merkle_roots: Vec<MerkleNode>,
     /// Nullifiers prevent double spending
     /// Nullifiers prevent double spending
-    nullifiers: Vec<Nullifier>,
+    pub nullifiers: Vec<Nullifier>,
     /// Verifying key for the mint zk circuit.
     /// Verifying key for the mint zk circuit.
     mint_vk: VerifyingKey,
     mint_vk: VerifyingKey,
     /// Verifying key for the burn zk circuit.
     /// Verifying key for the burn zk circuit.
@@ -54,20 +54,3 @@ impl ProgramState for State {
         &self.burn_vk
         &self.burn_vk
     }
     }
 }
 }
-
-impl State {
-    fn apply(&mut self, mut update: StateUpdate) {
-        // Extend our list of nullifiers with the ones from the update
-        self.nullifiers.append(&mut update.nullifiers);
-
-        // Update merkle tree and witnesses
-        for (coin, enc_note) in update.coins.into_iter().zip(update.enc_notes.into_iter()) {
-            // Add the new coins to the Merkle tree
-            let node = MerkleNode(coin.0);
-            self.tree.append(&node);
-
-            // Keep track of all Merkle roots that have existed
-            self.merkle_roots.push(self.tree.root(0).unwrap());
-        }
-    }
-}

+ 17 - 3
bin/daod/src/money_contract/transfer/mod.rs

@@ -24,6 +24,7 @@ use crate::demo::CallDataBase;
 
 
 pub mod builder;
 pub mod builder;
 pub mod partial;
 pub mod partial;
+pub mod validate;
 
 
 /// A DarkFi transaction
 /// A DarkFi transaction
 #[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
 #[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
@@ -37,13 +38,26 @@ pub struct CallData {
 }
 }
 
 
 impl CallDataBase for CallData {
 impl CallDataBase for CallData {
-    // TODO: Unimplemented
     fn zk_public_values(&self) -> Vec<Vec<DrkCircuitField>> {
     fn zk_public_values(&self) -> Vec<Vec<DrkCircuitField>> {
-        vec![]
+        let mut public_values = Vec::new();
+        for input in &self.inputs {
+            public_values.push(input.revealed.make_outputs());
+        }
+        for output in &self.outputs {
+            public_values.push(output.revealed.make_outputs());
+        }
+        public_values
     }
     }
 
 
     fn zk_proof_addrs(&self) -> Vec<String> {
     fn zk_proof_addrs(&self) -> Vec<String> {
-        vec!["money-transfer".to_string()]
+        let mut result = Vec::new();
+        for _ in &self.inputs {
+            result.push("money-transfer-burn".to_string());
+        }
+        for _ in &self.outputs {
+            result.push("money-transfer-mint".to_string());
+        }
+        result
     }
     }
 
 
     fn as_any(&self) -> &dyn Any {
     fn as_any(&self) -> &dyn Any {