Parcourir la source

Clean up and enable working examples.

parazyd il y a 4 ans
Parent
commit
78f6a89e20
7 fichiers modifiés avec 107 ajouts et 63 suppressions
  1. 76 49
      Cargo.toml
  2. 2 1
      example/tx.rs
  3. 3 2
      example/vm.rs
  4. 3 2
      example/vm_burn.rs
  5. 1 0
      src/crypto/mod.rs
  6. 11 0
      src/crypto/token_id.rs
  7. 11 9
      src/error.rs

+ 76 - 49
Cargo.toml

@@ -16,12 +16,12 @@ log = "0.4.14"
 thiserror = "1.0.30"
 
 # async-runtime
+smol = {version = "1.2.5", optional = true}
+futures = {version = "0.3.19", optional = true}
 async-std = {version = "1.10.0", optional = true}
+async-trait = {version = "0.1.52", optional = true}
 async-channel = {version = "1.6.1", optional = true}
 async-executor = {version = "1.4.1", optional = true}
-async-trait = {version = "0.1.52", optional = true}
-futures = {version = "0.3.19", optional = true}
-smol = {version = "1.2.5", optional = true}
 
 # async-net
 async-native-tls = {version = "0.4.0", optional = true}
@@ -32,20 +32,21 @@ hex = {version = "0.4.3", optional = true}
 bs58 = {version = "0.4.0", optional = true}
 toml = {version = "0.5.8", optional = true}
 bytes = {version = "1.1.0", optional = true}
-serde = {version = "1.0.133", features = ["derive"], optional = true}
-serde_json = {version = "1.0.74", optional = true}
 bincode = {version = "1.3.3", optional = true}
 num-bigint = {version = "0.4.3", optional = true}
+serde_json = {version = "1.0.74", optional = true}
+serde = {version = "1.0.133", features = ["derive"], optional = true}
 
 # Utilities
-clap = {version = "3.0.7", features = ["derive"], optional = true}
-dirs = {version = "4.0.0", optional = true}
 url = {version = "2.2.2", optional = true}
-lazy_static = {version = "1.4.0", optional = true}
+dirs = {version = "4.0.0", optional = true}
 subtle = {version = "2.4.1", optional = true}
+lazy_static = {version = "1.4.0", optional = true}
+clap = {version = "3.0.7", features = ["derive"], optional = true}
 
 # Misc
 termion = {version = "1.5.6", optional = true}
+simplelog = {version = "0.11.2", optional = true}
 
 # Websockets
 tungstenite = {version = "0.16.0", optional = true}
@@ -53,26 +54,30 @@ async-tungstenite = {version = "0.16.1", optional = true}
 
 # Crypto
 rand = {version = "0.8.4", optional = true}
+sha2 = {version = "0.10.1", optional = true}
+group = {version = "0.11.0", optional = true}
+arrayvec = {version = "0.7.2", optional = true}
 blake2b_simd = {version = "1.0.0", optional = true}
 pasta_curves = {version = "0.2.1", optional = true}
-halo2 = {version = "=0.1.0-beta.1", features = ["dev-graph", "gadget-traces", "sanity-checks"], optional = true}
-incrementalmerkletree = {version = "0.2.0", optional = true}
-arrayvec = {version = "0.7.2", optional = true}
-group = {version = "0.11.0", optional = true}
-sha2 = {version = "0.10.1", optional = true}
 crypto_api_chachapoly = {version = "0.5.0", optional = true}
+incrementalmerkletree = {version = "0.2.0", optional = true}
+halo2 = {version = "=0.1.0-beta.1", features = ["dev-graph", "gadget-traces", "sanity-checks"], optional = true}
 
 # Wallet management
 sqlx = {version = "0.5.10", features = ["runtime-async-std-native-tls", "sqlite"], optional = true}
 libsqlite3-sys = {version = "0.23.1", features = ["bundled-sqlcipher"],  optional = true }
 
-# Node networking
-zeromq = {version = "0.3.3", default-features = false, features = ["async-std-runtime", "all-transport"],  optional = true }
-
 # Node utilities
 signal-hook = {version = "0.3.13", optional = true}
 signal-hook-async-std = {version = "0.2.2", optional = true}
 
+# Node protocol
+[dependencies.zeromq]
+version = "0.3.3"
+default-features = false
+features = ["async-std-runtime", "all-transport"]
+optional = true
+
 [dependencies.rocksdb]
 # TODO: Revert to upstream after bd966750ec861d687913d59a9939a1408ac53131 is merged.
 git = "https://github.com/parazyd/rust-rocksdb"
@@ -97,14 +102,17 @@ async-runtime = [
     "futures",
     "smol",
 ]
+
 async-net = [
     "async-native-tls",
     "native-tls",
 ]
+
 websockets = [
     "async-tungstenite",
     "tungstenite",
 ]
+
 util = [
     "hex",
     "bincode",
@@ -113,36 +121,48 @@ util = [
     "dirs",
     "num-bigint",
 ]
+
 rpc = [
-    "async-runtime",
-    "async-net",
     "rand",
-    "util",
-    "websockets",
     "url",
+
+    "async-net",
+    "async-runtime",
+    "websockets",
+    "util",
 ]
+
 blockchain = [
-    "async-runtime",
     "rocksdb",
+
+    "async-runtime",
     "util",
 ]
+
 tui = [
     "async-std",
     "termion",
 ]
+
 system = [
-    "async-runtime",
     "rand",
+
+    "async-runtime",
 ]
+
 cli = [
+    "simplelog",
     "toml",
     "clap",
+
     "util",
 ]
+
 net = [
     "util",
     "system",
 ]
+
 crypto = [
     "rand",
     "pasta_curves",
@@ -153,53 +173,60 @@ crypto = [
     "subtle",
     "lazy_static",
     "group",
-    "util",
     "arrayvec",
     "crypto_api_chachapoly",
     "sha2",
     "bs58",
+
+    "util",
 ]
+
 wallet = [
     "sqlx",
     "libsqlite3-sys"
 ]
+
 node = [
     "url",
-    "async-runtime",
     "bytes",
-    "crypto",
-    "wallet",
-    "blockchain",
-    "util",
     "zeromq",
     "signal-hook",
     "signal-hook-async-std",
+
+    "async-runtime",
+    "blockchain",
+    "crypto",
+    "wallet",
+    "util",
 ]
 
 
-# [[example]]
-# name = "net"
-# path = "example/net.rs"
-# required-features = ["node", "chain"]
+[[example]]
+name = "net"
+path = "example/net.rs"
+required-features = ["async-runtime", "cli", "net"]
 
-# [[example]]
-# name = "tui"
-# path = "example/tui_ex.rs"
-# required-features = ["tui"]
+[[example]]
+name = "tui"
+path = "example/tui_ex.rs"
+required-features = ["async-runtime", "tui"]
 
-# [[example]]
-# name = "vm"
-# path = "example/vm.rs"
+[[example]]
+name = "vm"
+path = "example/vm.rs"
+required-features = ["crypto"]
 
-# [[example]]
-# name = "vm_burn"
-# path = "example/vm_burn.rs"
+[[example]]
+name = "vm_burn"
+path = "example/vm_burn.rs"
+required-features = ["crypto"]
 
-# [[example]]
-# name = "tx"
-# path = "example/tx.rs"
-# required-features = ["node"]
+[[example]]
+name = "tx"
+path = "example/tx.rs"
+required-features = ["node"]
 
-# [[example]]
-# name = "tree"
-# path = "example/tree.rs"
+[[example]]
+name = "tree"
+path = "example/tree.rs"
+required-features = ["crypto"]

+ 2 - 1
example/tx.rs

@@ -9,10 +9,11 @@ use darkfi::{
         note::{EncryptedNote, Note},
         nullifier::Nullifier,
         proof::{ProvingKey, VerifyingKey},
+        token_id::generate_id2,
     },
     node::state::{state_transition, ProgramState, StateUpdate},
     tx,
-    util::{generate_id2, NetworkName},
+    util::NetworkName,
     zk::circuit::{mint_contract::MintContract, spend_contract::SpendContract},
     Result,
 };

+ 3 - 2
example/vm.rs

@@ -17,11 +17,12 @@ use darkfi::{
         proof::{Proof, ProvingKey, VerifyingKey},
         util::pedersen_commitment_u64,
     },
-    serial::Decodable,
+    util::serial::Decodable,
     zk::vm,
+    Error,
 };
 
-fn main() -> std::result::Result<(), failure::Error> {
+fn main() -> std::result::Result<(), Error> {
     // The number of rows in our circuit cannot exceed 2^k
     let k: u32 = 11;
 

+ 3 - 2
example/vm_burn.rs

@@ -26,8 +26,9 @@ use darkfi::{
         proof::{Proof, ProvingKey, VerifyingKey},
         util::pedersen_commitment_u64,
     },
-    serial::Decodable,
+    util::serial::Decodable,
     zk::vm,
+    Error,
 };
 
 fn root(path: [pallas::Base; 32], leaf_pos: u32, leaf: pallas::Base) -> pallas::Base {
@@ -52,7 +53,7 @@ fn root(path: [pallas::Base; 32], leaf_pos: u32, leaf: pallas::Base) -> pallas::
     node
 }
 
-fn main() -> std::result::Result<(), failure::Error> {
+fn main() -> std::result::Result<(), Error> {
     // The number of rows in our circuit cannot exceed 2^k
     let k: u32 = 11;
 

+ 1 - 0
src/crypto/mod.rs

@@ -11,6 +11,7 @@ pub mod nullifier;
 pub mod proof;
 pub mod schnorr;
 pub mod spend_proof;
+pub mod token_id;
 pub mod types;
 pub mod util;
 

+ 11 - 0
src/crypto/token_id.rs

@@ -1,3 +1,14 @@
+use sha2::{Digest, Sha256};
+
+use super::types::DrkTokenId;
+use crate::{
+    util::{
+        serial::{deserialize, serialize},
+        NetworkName,
+    },
+    Result,
+};
+
 /// Hash the external token ID and NetworkName param.
 /// If it fails, change the last 4 bytes and hash it again.
 /// Keep repeating until it works.

+ 11 - 9
src/error.rs

@@ -4,8 +4,9 @@ pub type Result<T> = std::result::Result<T, Error>;
 pub enum Error {
     #[error("io error: `{0:?}`")]
     Io(std::io::ErrorKind),
-    // #[error("Infallible Error: `{0}`")]
-    // InfallibleError(String),
+
+    #[error("Infallible Error: `{0}`")]
+    InfallibleError(String),
 
     // #[error("Cannot find home directory")]
     // PathNotFound,
@@ -38,8 +39,9 @@ pub enum Error {
 
     #[error("No url found")]
     NoUrlFound,
-    // #[error(transparent)]
-    // AddrParseError(#[from] std::net::AddrParseError),
+
+    #[error(transparent)]
+    AddrParseError(#[from] std::net::AddrParseError),
     // #[error(transparent)]
     // Base58EncodeError(#[from] bs58::encode::Error),
     // #[error(transparent)]
@@ -271,8 +273,8 @@ impl From<Box<bincode::ErrorKind>> for Error {
     }
 }
 
-// impl From<std::convert::Infallible> for Error {
-// fn from(err: std::convert::Infallible) -> Error {
-// Error::InfallibleError(err.to_string())
-// }
-// }
+impl From<std::convert::Infallible> for Error {
+    fn from(err: std::convert::Infallible) -> Error {
+        Error::InfallibleError(err.to_string())
+    }
+}