Przeglądaj źródła

contract/money: WIP stuff

Luther Blissett 3 lat temu
rodzic
commit
b2bcfa994f

+ 1 - 0
contract/money/.gitignore

@@ -0,0 +1 @@
+target/*

+ 6 - 1
contract/money/Cargo.toml

@@ -17,4 +17,9 @@ overflow-checks = true
 
 [dependencies]
 darkfi-sdk = { path = "../../src/sdk" }
-darkfi = { path = "../../", features = ["serial"] }
+# TODO: serial/crypto feature that enables all this stuff
+darkfi = { path = "../../", features = ["serial", "pasta_curves", "incrementalmerkletree"] }
+incrementalmerkletree = "0.3.0"
+
+# TODO: Dummy getrandomndom = { version = "0.2.7", features = ["custom"] }
+getrandom = { version = "0.2.7", features = ["custom"] }

+ 39 - 0
contract/money/Makefile

@@ -0,0 +1,39 @@
+.POSIX:
+
+WASM_SRC = $(shell find src -type f)
+PROOF_SRC = $(shell find proof -type f -name '*.zk')
+
+# Cargo binary
+CARGO = cargo
+
+# zkas binary
+ZKAS = ../../zkas
+
+# wasm-strip binary (Part of https://github.com/WebAssembly/wabt)
+WASM_STRIP = wasm-strip
+
+# Contract WASM binary
+WASM_BIN = contract.wasm
+
+# ZK circuit binaries
+PROOF_BIN = $(PROOF_SRC:=.bin)
+
+all: $(PROOF_BIN) $(WASM_BIN)
+
+strip: $(WASM_BIN)
+	$(WASM_STRIP) $<
+
+$(WASM_BIN): $(WASM_SRC)
+	$(CARGO) build --release --lib --target wasm32-unknown-unknown
+	cp -f target/wasm32-unknown-unknown/release/*.wasm $@
+
+$(PROOF_BIN): $(PROOF_SRC)
+	$(ZKAS) $(basename $@) -o $@
+
+test: all
+	$(CARGO) test --release -- --nocapture
+
+clean:
+	rm -f $(PROOF_BIN) $(WASM_BIN)
+
+.PHONY: all test clean

+ 12 - 4
contract/money/src/lib.rs

@@ -1,3 +1,11 @@
+use darkfi::serial::{deserialize, SerialDecodable, SerialEncodable};
+use darkfi_sdk::{
+    crypto::{MerkleNode, Nullifier},
+    entrypoint,
+    error::ContractResult,
+};
+use incrementalmerkletree::bridgetree::BridgeTree;
+
 /// Available functions for this contract.
 /// We identify them with the first byte passed in through the payload.
 #[repr(u8)]
@@ -27,7 +35,7 @@ pub mod transfer;
 #[derive(Clone, SerialEncodable, SerialDecodable)]
 pub struct State {
     /// The Merkle tree of all coins used by this contract.
-    pub tree: BridgeTree<MerkleNode, MERKLE_DEPTH>,
+    pub tree: BridgeTree<MerkleNode, 32>,
     /// List of all previous and current Merkle roots.
     pub merkle_roots: Vec<MerkleNode>,
     /// Published nullifiers that have been seen.
@@ -46,7 +54,7 @@ impl State {
 
 #[cfg(not(feature = "no-entrypoint"))]
 entrypoint!(process_instruction);
-fn process_instruction(contract_id: &ContractId, ix: &[u8]) -> ContractResult {
+fn process_instruction(state: &[u8], ix: &[u8]) -> ContractResult {
     // This is the entrypoint function of the smart contract which gets executed
     // by the wasm runtime. The `contract_id` passed in is used to lookup the
     // current state from the ledger using the `lookup_state` function.
@@ -54,7 +62,7 @@ fn process_instruction(contract_id: &ContractId, ix: &[u8]) -> ContractResult {
     // first byte of the payload is a pointer to a function we with to run, and
     // the remainter is a serialized `Transaction` object we'll try to deserialize
     // and work with.
-    let mut state: State = deserialize(&lookup_state(contract_id)?)?;
+    let mut state: State = deserialize(state)?;
 
     match Function::from(ix[0]) {
         Function::Transfer => {
@@ -65,7 +73,7 @@ fn process_instruction(contract_id: &ContractId, ix: &[u8]) -> ContractResult {
             // host. Then if everything else outside of the wasm execution is
             // valid, the host can reference this new state and update it on the
             // ledger.
-            apply_state(&serialize(&state))?;
+            //apply_state(&serialize(&state))?;
         }
     }
 

+ 13 - 2
contract/money/src/transfer.rs

@@ -1,3 +1,14 @@
+use darkfi_sdk::{
+    crypto::{
+        pedersen::{pedersen_commitment_base, pedersen_commitment_u64, ValueCommit},
+        MerkleNode,
+    },
+    error::{ContractError, ContractResult},
+    msg,
+    pasta::pallas,
+    state::Verification,
+};
+
 use super::State;
 
 /// This function is the execution of the `Transfer` functionality.
@@ -54,7 +65,7 @@ pub fn exec(state: &mut State, tx: Transaction) -> ContractResult {
     msg!("Applying state update");
     state.nullifiers.extend_from_slice(&nullifiers);
     for output in tx.outputs {
-        state.tree.append(&MerkleNode(coin.inner()));
+        state.tree.append(&MerkleNode::from(output.coin.inner()));
         state.merkle_roots.push(state.tree.root(0).unwrap());
     }
 }
@@ -62,7 +73,7 @@ pub fn exec(state: &mut State, tx: Transaction) -> ContractResult {
 // `Verification` could be a generic trait we implement for doing
 // arbitrary verification in contracts.
 impl Verification for Transaction {
-    pub fn verify(&self) -> ContractResult {
+    fn verify(&self) -> ContractResult {
         // Must have minimum 1 clear or anon input
         if self.clear_inputs.len() + self.inputs.len() == 0 {
             msg!("Error: Missing inputs in transaction");