Explorar el Código

Update examples.

Luther Blissett hace 3 años
padre
commit
bcf031fb8a

+ 1 - 0
Cargo.lock

@@ -1550,6 +1550,7 @@ dependencies = [
  "darkfi",
  "easy-parallel",
  "fxhash",
+ "hex",
  "log",
  "num_cpus",
  "rand",

+ 1 - 1
example/dchat/src/dchatmsg.rs

@@ -2,7 +2,7 @@ use async_std::sync::{Arc, Mutex};
 
 use darkfi::{
     net,
-    util::serial::{SerialDecodable, SerialEncodable},
+    serial::{SerialDecodable, SerialEncodable},
 };
 
 pub type DchatMsgsBuffer = Arc<Mutex<Vec<DchatMsg>>>;

+ 4 - 16
example/lead.rs

@@ -1,3 +1,4 @@
+use futures::executor::block_on;
 use halo2_proofs::{arithmetic::Field, dev::MockProver};
 use incrementalmerkletree::{bridgetree::BridgeTree, Tree};
 use pasta_curves::{
@@ -5,29 +6,16 @@ use pasta_curves::{
     group::{ff::PrimeField, Curve},
     pallas,
 };
-
-use futures::executor::block_on;
-
-use darkfi::crypto::proof::{ProvingKey, VerifyingKey};
 use url::Url;
 
 use darkfi::{
     blockchain::{
         epoch::{Epoch, EpochItem},
-        Blockchain, EpochConsensus,
-    },
-    consensus::{BlockInfo, StakeholderMetadata, StreamletMetadata, TransactionLeadProof},
-    crypto::{
-        constants::MERKLE_DEPTH_ORCHARD,
-        lead_proof,
-        leadcoin::{LeadCoin, LEAD_PUBLIC_INPUT_LEN},
-        merkle_node::MerkleNode,
+        EpochConsensus,
     },
-    net::{P2p, Settings, SettingsPtr},
+    crypto::leadcoin::{LeadCoin, LEAD_PUBLIC_INPUT_LEN},
+    net::Settings,
     stakeholder::stakeholder::Stakeholder,
-    tx::Transaction,
-    util::time::Timestamp,
-    zk::circuit::lead_contract::LeadContract,
 };
 
 fn main() {

+ 2 - 0
example/smart-contract/Cargo.toml

@@ -11,6 +11,7 @@ crate-type = ["cdylib", "rlib"]
 [dependencies]
 borsh = "0.9.3"
 drk-sdk = { path = "../../src/sdk" }
+darkfi = { path = "../../", features = ["serial"]}
 
 [dependencies.pasta_curves]
 git = "https://github.com/darkrenaissance/pasta_curves"
@@ -19,6 +20,7 @@ features = ["borsh"]
 
 [dev-dependencies]
 darkfi = { path = "../../", features = ["wasm-runtime"] }
+simplelog = "0.12.0"
 
 [profile.release]
 lto = true

+ 9 - 3
example/smart-contract/Makefile

@@ -5,12 +5,18 @@ SRC = $(shell find src -type f)
 # Cargo binary
 CARGO = cargo
 
-DEPS = smart_contract.wasm
+BIN = smart_contract.wasm
 
-all: $(DEPS)
+all: $(BIN)
 
 smart_contract.wasm: $(SRC)
 	$(CARGO) build --release --lib --target wasm32-unknown-unknown
 	cp -f target/wasm32-unknown-unknown/release/$@ $@
 
-.PHONY: all
+test: all
+	$(CARGO) test --release -- --nocapture
+
+clean:
+	rm -f $(BIN)
+
+.PHONY: all test clean

+ 10 - 1
example/smart-contract/src/lib.rs

@@ -6,15 +6,24 @@ use drk_sdk::{
 };
 use pasta_curves::pallas;
 
+use darkfi::serial::{deserialize, SerialDecodable, SerialEncodable};
+
 #[derive(BorshSerialize, BorshDeserialize)]
 pub struct Args {
     pub a: pallas::Base,
     pub b: pallas::Base,
 }
 
+#[derive(SerialEncodable, SerialDecodable)]
+pub struct Foo {
+    pub a: u64,
+    pub b: u64,
+}
+
 entrypoint!(process_instruction);
 fn process_instruction(ix: &[u8]) -> ContractResult {
-    let args = Args::try_from_slice(ix)?;
+    //let args = Args::try_from_slice(ix)?;
+    let args: Foo = deserialize(ix)?;
 
     if args.a < args.b {
         return Err(ContractError::Custom(69))

+ 14 - 3
example/smart-contract/tests/runtime.rs

@@ -1,19 +1,30 @@
 use borsh::BorshSerialize;
 use darkfi::{
     runtime::{util::serialize_payload, vm_runtime::Runtime},
+    serial::serialize,
     Result,
 };
 use pasta_curves::pallas;
 
-use smart_contract::Args;
+use smart_contract::{Args, Foo};
 
 #[test]
 fn run_contract() -> Result<()> {
+    simplelog::TermLogger::init(
+        simplelog::LevelFilter::Debug,
+        simplelog::Config::default(),
+        simplelog::TerminalMode::Mixed,
+        simplelog::ColorChoice::Auto,
+    )?;
+
     let wasm_bytes = std::fs::read("smart_contract.wasm")?;
     let mut runtime = Runtime::new(&wasm_bytes)?;
 
-    let args = Args { a: pallas::Base::from(777), b: pallas::Base::from(666) };
-    let payload = args.try_to_vec()?;
+    let _args = Args { a: pallas::Base::from(777), b: pallas::Base::from(666) };
+    let _payload = _args.try_to_vec()?;
+
+    let args = Foo { a: 777, b: 666 };
+    let payload = serialize(&args);
 
     let input = serialize_payload(&payload);
 

+ 2 - 2
example/tx.rs

@@ -5,13 +5,13 @@ use rand::rngs::OsRng;
 
 use darkfi::{
     crypto::{
+        coin::OwnCoin,
         constants::MERKLE_DEPTH,
         keypair::{Keypair, PublicKey, SecretKey},
         merkle_node::MerkleNode,
         note::{EncryptedNote, Note},
         nullifier::Nullifier,
         proof::{ProvingKey, VerifyingKey},
-        OwnCoin, OwnCoins,
     },
     node::state::{state_transition, ProgramState, StateUpdate},
     tx::builder::{
@@ -36,7 +36,7 @@ struct MemoryState {
     // spent. Maybe the spend field links to a tx hash:input index.
     // We should also keep track of the tx hash:output index where
     // this coin was received.
-    own_coins: OwnCoins,
+    own_coins: Vec<OwnCoin>,
     /// Verifying key for the mint zk circuit.
     mint_vk: VerifyingKey,
     /// Verifying key for the burn zk circuit.