Просмотр исходного кода

Change import references in library to the new serial module.

Luther Blissett 3 лет назад
Родитель
Сommit
76c25f06a1
52 измененных файлов с 206 добавлено и 287 удалено
  1. 2 4
      src/blockchain/blockstore.rs
  2. 2 4
      src/blockchain/metadatastore.rs
  3. 1 1
      src/blockchain/nfstore.rs
  4. 1 1
      src/blockchain/rootstore.rs
  5. 1 1
      src/blockchain/txstore.rs
  6. 2 4
      src/consensus/block.rs
  7. 1 1
      src/consensus/metadata.rs
  8. 1 1
      src/consensus/participant.rs
  9. 2 4
      src/consensus/state.rs
  10. 1 1
      src/consensus/task/proposal.rs
  11. 1 1
      src/consensus/vote.rs
  12. 5 18
      src/crypto/address.rs
  13. 1 1
      src/crypto/burn_proof.rs
  14. 10 20
      src/crypto/coin.rs
  15. 20 20
      src/crypto/keypair.rs
  16. 4 17
      src/crypto/merkle_node.rs
  17. 1 1
      src/crypto/mint_proof.rs
  18. 0 14
      src/crypto/mod.rs
  19. 3 2
      src/crypto/note.rs
  20. 2 21
      src/crypto/nullifier.rs
  21. 4 20
      src/crypto/proof.rs
  22. 11 24
      src/crypto/schnorr.rs
  23. 1 1
      src/crypto/token_id.rs
  24. 1 1
      src/crypto/token_list.rs
  25. 2 1
      src/dht/dht.rs
  26. 1 1
      src/dht/messages.rs
  27. 12 0
      src/lib.rs
  28. 1 1
      src/net/channel.rs
  29. 1 1
      src/net/message.rs
  30. 50 55
      src/net/message_subscriber.rs
  31. 23 0
      src/net/mod.rs
  32. 1 1
      src/net/p2p.rs
  33. 1 1
      src/net/protocol/protocol_ping.rs
  34. 1 1
      src/net/session/manual_session.rs
  35. 2 3
      src/node/client.rs
  36. 1 2
      src/node/state.rs
  37. 2 4
      src/raft/consensus.rs
  38. 1 1
      src/raft/consensus_candidate.rs
  39. 1 1
      src/raft/consensus_follower.rs
  40. 1 1
      src/raft/consensus_leader.rs
  41. 1 1
      src/raft/datastore.rs
  42. 2 2
      src/raft/mod.rs
  43. 3 3
      src/raft/primitives.rs
  44. 1 1
      src/raft/protocol_raft.rs
  45. 1 0
      src/serial/mod.rs
  46. 10 11
      src/stakeholder/stakeholder.rs
  47. 1 1
      src/tx/builder.rs
  48. 1 1
      src/tx/mod.rs
  49. 1 1
      src/tx/partial.rs
  50. 4 7
      src/wallet/walletdb.rs
  51. 1 1
      src/zkas/compiler.rs
  52. 1 1
      src/zkas/decoder.rs

+ 2 - 4
src/blockchain/blockstore.rs

@@ -1,9 +1,7 @@
 use crate::{
     consensus::{Block, Header},
-    util::{
-        serial::{deserialize, serialize},
-        time::Timestamp,
-    },
+    serial::{deserialize, serialize},
+    util::time::Timestamp,
     Error, Result,
 };
 

+ 2 - 4
src/blockchain/metadatastore.rs

@@ -1,9 +1,7 @@
 use crate::{
     consensus::{Block, OuroborosMetadata, StreamletMetadata, TransactionLeadProof},
-    util::{
-        serial::{deserialize, serialize},
-        time::Timestamp,
-    },
+    serial::{deserialize, serialize},
+    util::time::Timestamp,
     Error, Result,
 };
 

+ 1 - 1
src/blockchain/nfstore.rs

@@ -1,6 +1,6 @@
 use crate::{
     crypto::nullifier::Nullifier,
-    util::serial::{deserialize, serialize},
+    serial::{deserialize, serialize},
     Result,
 };
 

+ 1 - 1
src/blockchain/rootstore.rs

@@ -1,6 +1,6 @@
 use crate::{
     crypto::merkle_node::MerkleNode,
-    util::serial::{deserialize, serialize},
+    serial::{deserialize, serialize},
     Result,
 };
 

+ 1 - 1
src/blockchain/txstore.rs

@@ -1,6 +1,6 @@
 use crate::{
+    serial::{deserialize, serialize},
     tx::Transaction,
-    util::serial::{deserialize, serialize},
     Error, Result,
 };
 

+ 2 - 4
src/consensus/block.rs

@@ -10,11 +10,9 @@ use pasta_curves::pallas;
 use crate::{
     crypto::{constants::MERKLE_DEPTH, merkle_node::MerkleNode},
     net,
+    serial::{serialize, SerialDecodable, SerialEncodable},
     tx::Transaction,
-    util::{
-        serial::{serialize, SerialDecodable, SerialEncodable},
-        time::Timestamp,
-    },
+    util::time::Timestamp,
 };
 
 /// This struct represents a tuple of the form (version, state, epoch, slot, timestamp, merkle_root).

+ 1 - 1
src/consensus/metadata.rs

@@ -11,7 +11,7 @@ use crate::{
         schnorr::Signature,
         types::*,
     },
-    util::serial::{SerialDecodable, SerialEncodable},
+    serial::{SerialDecodable, SerialEncodable},
     VerifyResult,
 };
 

+ 1 - 1
src/consensus/participant.rs

@@ -1,7 +1,7 @@
 use crate::{
     crypto::{address::Address, keypair::PublicKey},
     net,
-    util::serial::{SerialDecodable, SerialEncodable},
+    serial::{SerialDecodable, SerialEncodable},
 };
 
 /// This struct represents a tuple of the form:

+ 2 - 4
src/consensus/state.rs

@@ -32,11 +32,9 @@ use crate::{
         state::{state_transition, ProgramState, StateUpdate},
         Client, MemoryState, State,
     },
+    serial::{serialize, Encodable, SerialDecodable, SerialEncodable},
     tx::Transaction,
-    util::{
-        serial::{serialize, Encodable, SerialDecodable, SerialEncodable},
-        time::Timestamp,
-    },
+    util::time::Timestamp,
     Result,
 };
 

+ 1 - 1
src/consensus/task/proposal.rs

@@ -6,7 +6,7 @@ use super::consensus_sync_task;
 use crate::{
     consensus::{Participant, ValidatorStatePtr},
     net::P2pPtr,
-    util::sleep,
+    util::async_util::sleep,
 };
 
 /// async task used for participating in the consensus protocol

+ 1 - 1
src/consensus/vote.rs

@@ -1,7 +1,7 @@
 use crate::{
     crypto::{address::Address, schnorr::Signature},
     net,
-    util::serial::{SerialDecodable, SerialEncodable},
+    serial::{SerialDecodable, SerialEncodable},
 };
 
 /// This struct represents a `Vote` used by the Streamlet consensus

+ 5 - 18
src/crypto/address.rs

@@ -1,10 +1,10 @@
-use std::{io, str::FromStr};
+use std::str::FromStr;
 
 use sha2::Digest;
 
 use crate::{
     crypto::keypair::PublicKey,
-    util::serial::{Decodable, Encodable, ReadExt, WriteExt},
+    serial::{SerialDecodable, SerialEncodable},
     Error, Result,
 };
 
@@ -12,7 +12,9 @@ enum AddressType {
     Payment = 0,
 }
 
-#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
+#[derive(
+    Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash, SerialEncodable, SerialDecodable,
+)]
 pub struct Address(pub [u8; 37]);
 
 impl Address {
@@ -81,21 +83,6 @@ impl From<PublicKey> for Address {
     }
 }
 
-impl Encodable for Address {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        s.write_slice(&self.0)?;
-        Ok(37)
-    }
-}
-
-impl Decodable for Address {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        let mut bytes = [0u8; 37];
-        d.read_slice(&mut bytes)?;
-        Ok(Self(bytes))
-    }
-}
-
 #[cfg(test)]
 mod tests {
     use rand::rngs::OsRng;

+ 1 - 1
src/crypto/burn_proof.rs

@@ -20,7 +20,7 @@ use crate::{
         },
         util::poseidon_hash,
     },
-    util::serial::{SerialDecodable, SerialEncodable},
+    serial::{SerialDecodable, SerialEncodable},
     zk::circuit::burn_contract::BurnContract,
     Result,
 };

+ 10 - 20
src/crypto/coin.rs

@@ -1,13 +1,9 @@
-use std::io;
-
 use pasta_curves::{group::ff::PrimeField, pallas};
 
-use crate::{
-    util::serial::{Decodable, Encodable, ReadExt, WriteExt},
-    Result,
-};
+use super::{keypair::SecretKey, note::Note, nullifier::Nullifier};
+use crate::serial::{SerialDecodable, SerialEncodable};
 
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+#[derive(Clone, Copy, PartialEq, Eq, Debug, SerialEncodable, SerialDecodable)]
 pub struct Coin(pub pallas::Base);
 
 impl Coin {
@@ -20,17 +16,11 @@ impl Coin {
     }
 }
 
-impl Encodable for Coin {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        s.write_slice(&self.to_bytes()[..])?;
-        Ok(32)
-    }
-}
-
-impl Decodable for Coin {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        let mut bytes = [0u8; 32];
-        d.read_slice(&mut bytes)?;
-        Ok(Self::from_bytes(bytes))
-    }
+#[derive(Clone, Debug, PartialEq, Eq, SerialEncodable, SerialDecodable)]
+pub struct OwnCoin {
+    pub coin: Coin,
+    pub note: Note,
+    pub secret: SecretKey,
+    pub nullifier: Nullifier,
+    pub leaf_position: incrementalmerkletree::Position,
 }

+ 20 - 20
src/crypto/keypair.rs

@@ -13,7 +13,7 @@ use rand::RngCore;
 
 use crate::{
     crypto::{address::Address, constants::NullifierK, util::mod_r_p},
-    util::serial::{Decodable, Encodable, ReadExt, SerialDecodable, SerialEncodable, WriteExt},
+    serial::{Decodable, Encodable, ReadExt, SerialDecodable, SerialEncodable, WriteExt},
     Error, Result,
 };
 
@@ -97,7 +97,7 @@ impl FromStr for PublicKey {
     type Err = crate::Error;
 
     /// Tries to create a `PublicKey` instance from a base58 encoded string.
-    fn from_str(encoded: &str) -> std::result::Result<Self, crate::Error> {
+    fn from_str(encoded: &str) -> core::result::Result<Self, crate::Error> {
         let decoded = bs58::decode(encoded).into_vec()?;
         if decoded.len() != 32 {
             return Err(Error::PublicKeyFromStr)
@@ -117,68 +117,68 @@ impl TryFrom<Address> for PublicKey {
 }
 
 impl Encodable for pallas::Base {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
+    fn encode<S: io::Write>(&self, mut s: S) -> core::result::Result<usize, io::Error> {
         s.write_slice(&self.to_repr()[..])?;
         Ok(32)
     }
 }
 
 impl Decodable for pallas::Base {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
+    fn decode<D: io::Read>(mut d: D) -> core::result::Result<Self, io::Error> {
         let mut bytes = [0u8; 32];
         d.read_slice(&mut bytes)?;
         let result = pallas::Base::from_repr(bytes);
         if result.is_some().into() {
             Ok(result.unwrap())
         } else {
-            Err(Error::BadOperationType)
+            Err(io::Error::new(io::ErrorKind::Other, "Failed to deserialize pallas::Base"))
         }
     }
 }
 
 impl Encodable for pallas::Scalar {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
+    fn encode<S: io::Write>(&self, mut s: S) -> core::result::Result<usize, io::Error> {
         s.write_slice(&self.to_repr()[..])?;
         Ok(32)
     }
 }
 
 impl Decodable for pallas::Scalar {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
+    fn decode<D: io::Read>(mut d: D) -> core::result::Result<Self, io::Error> {
         let mut bytes = [0u8; 32];
         d.read_slice(&mut bytes)?;
         let result = pallas::Scalar::from_repr(bytes);
         if result.is_some().into() {
             Ok(result.unwrap())
         } else {
-            Err(Error::BadOperationType)
+            Err(io::Error::new(io::ErrorKind::Other, "Failed to deserialize pallas::Scalar"))
         }
     }
 }
 
 impl Encodable for pallas::Point {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
+    fn encode<S: io::Write>(&self, mut s: S) -> core::result::Result<usize, io::Error> {
         s.write_slice(&self.to_bytes()[..])?;
         Ok(32)
     }
 }
 
 impl Decodable for pallas::Point {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
+    fn decode<D: io::Read>(mut d: D) -> core::result::Result<Self, io::Error> {
         let mut bytes = [0u8; 32];
         d.read_slice(&mut bytes)?;
         let result = Self::from_bytes(&bytes);
         if result.is_some().into() {
             Ok(result.unwrap())
         } else {
-            Err(Error::BadOperationType)
+            Err(io::Error::new(io::ErrorKind::Other, "Failed to deserialize pallas::Point"))
         }
     }
 }
 
 #[cfg(feature = "serde")]
 impl serde::Serialize for SecretKey {
-    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
+    fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
     where
         S: serde::Serializer,
     {
@@ -196,11 +196,11 @@ struct SecretKeyVisitor;
 impl<'de> serde::de::Visitor<'de> for SecretKeyVisitor {
     type Value = SecretKey;
 
-    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
+    fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
         formatter.write_str("hex string")
     }
 
-    fn visit_str<E>(self, value: &str) -> std::result::Result<SecretKey, E>
+    fn visit_str<E>(self, value: &str) -> core::result::Result<SecretKey, E>
     where
         E: serde::de::Error,
     {
@@ -213,7 +213,7 @@ impl<'de> serde::de::Visitor<'de> for SecretKeyVisitor {
 
 #[cfg(feature = "serde")]
 impl<'de> serde::Deserialize<'de> for SecretKey {
-    fn deserialize<D>(deserializer: D) -> std::result::Result<SecretKey, D::Error>
+    fn deserialize<D>(deserializer: D) -> core::result::Result<SecretKey, D::Error>
     where
         D: serde::Deserializer<'de>,
     {
@@ -224,7 +224,7 @@ impl<'de> serde::Deserialize<'de> for SecretKey {
 
 #[cfg(feature = "serde")]
 impl serde::Serialize for PublicKey {
-    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
+    fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
     where
         S: serde::Serializer,
     {
@@ -242,11 +242,11 @@ struct PublicKeyVisitor;
 impl<'de> serde::de::Visitor<'de> for PublicKeyVisitor {
     type Value = PublicKey;
 
-    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
+    fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
         formatter.write_str("hex string")
     }
 
-    fn visit_str<E>(self, value: &str) -> std::result::Result<PublicKey, E>
+    fn visit_str<E>(self, value: &str) -> core::result::Result<PublicKey, E>
     where
         E: serde::de::Error,
     {
@@ -259,7 +259,7 @@ impl<'de> serde::de::Visitor<'de> for PublicKeyVisitor {
 
 #[cfg(feature = "serde")]
 impl<'de> serde::Deserialize<'de> for PublicKey {
-    fn deserialize<D>(deserializer: D) -> std::result::Result<PublicKey, D::Error>
+    fn deserialize<D>(deserializer: D) -> core::result::Result<PublicKey, D::Error>
     where
         D: serde::Deserializer<'de>,
     {
@@ -273,7 +273,7 @@ mod tests {
     use super::*;
     use crate::{
         crypto::util::pedersen_commitment_base,
-        util::serial::{deserialize, serialize},
+        serial::{deserialize, serialize},
     };
 
     #[test]

+ 4 - 17
src/crypto/merkle_node.rs

@@ -22,8 +22,7 @@ use crate::{
             MERKLE_DEPTH_ORCHARD,
         },
     },
-    util::serial::{Decodable, Encodable},
-    Result,
+    serial::{Decodable, Encodable, SerialDecodable, SerialEncodable},
 };
 
 lazy_static! {
@@ -40,7 +39,7 @@ lazy_static! {
     };
 }
 
-#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
+#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, SerialEncodable, SerialDecodable)]
 pub struct MerkleNode(pub pallas::Base);
 
 impl MerkleNode {
@@ -118,26 +117,14 @@ impl Hashable for MerkleNode {
     }
 }
 
-impl Encodable for MerkleNode {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        self.0.encode(&mut s)
-    }
-}
-
-impl Decodable for MerkleNode {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        Ok(Self(Decodable::decode(&mut d)?))
-    }
-}
-
 impl Encodable for incrementalmerkletree::Position {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
+    fn encode<S: io::Write>(&self, mut s: S) -> core::result::Result<usize, io::Error> {
         u64::from(*self).encode(&mut s)
     }
 }
 
 impl Decodable for incrementalmerkletree::Position {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
+    fn decode<D: io::Read>(mut d: D) -> core::result::Result<Self, io::Error> {
         let dec: u64 = Decodable::decode(&mut d)?;
         Ok(Self::try_from(dec).unwrap())
     }

+ 1 - 1
src/crypto/mint_proof.rs

@@ -16,7 +16,7 @@ use crate::{
         },
         util::{pedersen_commitment_base, pedersen_commitment_u64, poseidon_hash},
     },
-    util::serial::{SerialDecodable, SerialEncodable},
+    serial::{SerialDecodable, SerialEncodable},
     zk::circuit::mint_contract::MintContract,
     Result,
 };

+ 0 - 14
src/crypto/mod.rs

@@ -26,17 +26,3 @@ pub use proof::Proof;
 
 pub mod lead_proof;
 pub mod leadcoin;
-
-use crate::util::serial::{SerialDecodable, SerialEncodable};
-use keypair::SecretKey;
-
-#[derive(Clone, Debug, PartialEq, Eq, SerialEncodable, SerialDecodable)]
-pub struct OwnCoin {
-    pub coin: coin::Coin,
-    pub note: note::Note,
-    pub secret: SecretKey,
-    pub nullifier: nullifier::Nullifier,
-    pub leaf_position: incrementalmerkletree::Position,
-}
-
-pub type OwnCoins = Vec<OwnCoin>;

+ 3 - 2
src/crypto/note.rs

@@ -7,7 +7,7 @@ use crate::{
         keypair::{PublicKey, SecretKey},
         types::{DrkCoinBlind, DrkSerial, DrkTokenId, DrkValueBlind},
     },
-    util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable},
+    serial::{Decodable, Encodable, SerialDecodable, SerialEncodable},
     Error, Result,
 };
 
@@ -65,7 +65,8 @@ impl EncryptedNote {
             self.ciphertext.len() - AEAD_TAG_SIZE
         );
 
-        Note::decode(&plaintext[..])
+        let note = Note::decode(&plaintext[..])?;
+        Ok(note)
     }
 }
 

+ 2 - 21
src/crypto/nullifier.rs

@@ -1,14 +1,11 @@
-use std::io;
-
 use pasta_curves::{group::ff::PrimeField, pallas};
 
 use crate::{
     crypto::{keypair::SecretKey, util::poseidon_hash},
-    util::serial::{Decodable, Encodable, ReadExt, WriteExt},
-    Result,
+    serial::{SerialDecodable, SerialEncodable},
 };
 
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+#[derive(Clone, Copy, Debug, PartialEq, Eq, SerialEncodable, SerialDecodable)]
 pub struct Nullifier(pub pallas::Base);
 
 impl Nullifier {
@@ -29,19 +26,3 @@ impl Nullifier {
         self.0
     }
 }
-
-impl Encodable for Nullifier {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        s.write_slice(&self.to_bytes()[..])?;
-        Ok(32)
-    }
-}
-
-impl Decodable for Nullifier {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        let mut bytes = [0u8; 32];
-        d.read_slice(&mut bytes)?;
-        let result = Self::from_bytes(bytes);
-        Ok(result)
-    }
-}

+ 4 - 20
src/crypto/proof.rs

@@ -1,5 +1,3 @@
-use std::io;
-
 use halo2_proofs::{
     plonk,
     plonk::{Circuit, SingleVerifier},
@@ -11,8 +9,7 @@ use rand::RngCore;
 
 use crate::{
     crypto::types::DrkCircuitField,
-    util::serial::{encode_with_size, Decodable, Encodable, ReadExt, VarInt},
-    Result,
+    serial::{SerialDecodable, SerialEncodable},
 };
 
 // TODO: this API needs rework. It's not very good.
@@ -51,7 +48,7 @@ impl ProvingKey {
     }
 }
 
-#[derive(Clone, Default, Debug, PartialEq, Eq)]
+#[derive(Clone, Default, Debug, PartialEq, Eq, SerialEncodable, SerialDecodable)]
 pub struct Proof(Vec<u8>);
 
 impl AsRef<[u8]> for Proof {
@@ -95,21 +92,6 @@ impl Proof {
     }
 }
 
-impl Encodable for Proof {
-    fn encode<S: io::Write>(&self, s: S) -> Result<usize> {
-        encode_with_size(self.as_ref(), s)
-    }
-}
-
-impl Decodable for Proof {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        let len = VarInt::decode(&mut d)?.0 as usize;
-        let mut r = vec![0u8; len];
-        d.read_slice(&mut r)?;
-        Ok(Proof::new(r))
-    }
-}
-
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -121,7 +103,9 @@ mod tests {
                 DrkCoinBlind, DrkSerial, DrkSpendHook, DrkTokenId, DrkUserData, DrkValueBlind,
             },
         },
+        serial::{Decodable, Encodable},
         zk::circuit::MintContract,
+        Result,
     };
     use group::ff::Field;
     use rand::rngs::OsRng;

+ 11 - 24
src/crypto/schnorr.rs

@@ -1,5 +1,3 @@
-use std::io;
-
 use halo2_gadgets::ecc::chip::FixedPoint;
 use pasta_curves::{
     group::{ff::Field, Group, GroupEncoding},
@@ -13,11 +11,10 @@ use crate::{
         keypair::{PublicKey, SecretKey},
         util::{hash_to_scalar, mod_r_p},
     },
-    util::serial::{Decodable, Encodable},
-    Result,
+    serial::{Decodable, Encodable, SerialDecodable, SerialEncodable},
 };
 
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
 pub struct Signature {
     commit: pallas::Point,
     response: pallas::Scalar,
@@ -56,24 +53,9 @@ impl SchnorrPublic for PublicKey {
     }
 }
 
-impl Encodable for Signature {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        let mut len = 0;
-        len += self.commit.encode(&mut s)?;
-        len += self.response.encode(s)?;
-        Ok(len)
-    }
-}
-
-impl Decodable for Signature {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        Ok(Self { commit: Decodable::decode(&mut d)?, response: Decodable::decode(d)? })
-    }
-}
-
 #[cfg(feature = "serde")]
 impl serde::Serialize for Signature {
-    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
+    fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
     where
         S: serde::Serializer,
     {
@@ -91,11 +73,11 @@ struct SignatureVisitor;
 impl<'de> serde::de::Visitor<'de> for SignatureVisitor {
     type Value = Signature;
 
-    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
+    fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
         formatter.write_str("hex string")
     }
 
-    fn visit_str<E>(self, value: &str) -> std::result::Result<Signature, E>
+    fn visit_str<E>(self, value: &str) -> core::result::Result<Signature, E>
     where
         E: serde::de::Error,
     {
@@ -108,7 +90,7 @@ impl<'de> serde::de::Visitor<'de> for SignatureVisitor {
 
 #[cfg(feature = "serde")]
 impl<'de> serde::Deserialize<'de> for Signature {
-    fn deserialize<D>(deserializer: D) -> std::result::Result<Signature, D::Error>
+    fn deserialize<D>(deserializer: D) -> core::result::Result<Signature, D::Error>
     where
         D: serde::Deserializer<'de>,
     {
@@ -120,6 +102,7 @@ impl<'de> serde::Deserialize<'de> for Signature {
 #[cfg(test)]
 mod tests {
     use super::*;
+    use crate::serial::{deserialize, serialize};
 
     #[test]
     fn test_schnorr() {
@@ -128,5 +111,9 @@ mod tests {
         let signature = secret.sign(&message[..]);
         let public = PublicKey::from_secret(secret);
         assert!(public.verify(&message[..], &signature));
+
+        let ser = serialize(&signature);
+        let de = deserialize(&ser).unwrap();
+        assert!(public.verify(&message[..], &de));
     }
 }

+ 1 - 1
src/crypto/token_id.rs

@@ -1,7 +1,7 @@
 use group::ff::PrimeField;
 
 use super::types::DrkTokenId;
-use crate::{util::NetworkName, Error, Result};
+use crate::{util::net_name::NetworkName, Error, Result};
 
 pub fn generate_id(network: &NetworkName, token_str: &str) -> Result<DrkTokenId> {
     let mut net_bytes: Vec<u8> = network.to_string().as_bytes().to_vec();

+ 1 - 1
src/crypto/token_list.rs

@@ -5,7 +5,7 @@ use group::ff::PrimeField;
 use serde_json::Value;
 
 use super::{token_id::generate_id, types::DrkTokenId};
-use crate::{util::NetworkName, Result};
+use crate::{util::net_name::NetworkName, Result};
 
 #[derive(Clone, Debug)]
 pub struct TokenInfo {

+ 2 - 1
src/dht/dht.rs

@@ -10,7 +10,8 @@ use std::collections::HashSet;
 use crate::{
     net,
     net::P2pPtr,
-    util::{serial::serialize, sleep},
+    serial::serialize,
+    util::async_util::sleep,
     Error::{NetworkNotConnected, UnknownKey},
     Result,
 };

+ 1 - 1
src/dht/messages.rs

@@ -4,7 +4,7 @@ use std::collections::HashSet;
 
 use crate::{
     net,
-    util::serial::{serialize, SerialDecodable, SerialEncodable},
+    serial::{serialize, SerialDecodable, SerialEncodable},
 };
 
 /// This struct represents a DHT key request

+ 12 - 0
src/lib.rs

@@ -51,3 +51,15 @@ pub mod runtime;
 
 #[cfg(feature = "zkas")]
 pub mod zkas;
+
+pub const ANSI_LOGO: &str = include_str!("../contrib/darkfi.ansi");
+
+#[macro_export]
+macro_rules! cli_desc {
+    () => {{
+        let mut desc = env!("CARGO_PKG_DESCRIPTION").to_string();
+        desc.push_str("\n");
+        desc.push_str(darkfi::ANSI_LOGO);
+        Box::leak(desc.into_boxed_str()) as &'static str
+    }};
+}

+ 1 - 1
src/net/channel.rs

@@ -12,7 +12,7 @@ use url::Url;
 
 use crate::{
     system::{StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription},
-    util::NanoTimestamp,
+    util::time::NanoTimestamp,
     Error, Result,
 };
 

+ 1 - 1
src/net/message.rs

@@ -3,7 +3,7 @@ use log::debug;
 use url::Url;
 
 use crate::{
-    util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
+    serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
     Error, Result,
 };
 

+ 50 - 55
src/net/message_subscriber.rs

@@ -1,15 +1,12 @@
 use async_std::sync::Mutex;
-use std::{any::Any, io, io::Cursor, sync::Arc};
+use std::{any::Any, io::Cursor, sync::Arc};
 
 use async_trait::async_trait;
 use fxhash::FxHashMap;
 use log::{debug, warn};
 use rand::Rng;
 
-use crate::{
-    util::serial::{Decodable, Encodable},
-    Error, Result,
-};
+use crate::{Error, Result};
 
 use super::message::Message;
 
@@ -239,70 +236,68 @@ impl Default for MessageSubsystem {
 // Normall we would use the #[test] macro but cannot since it is async code
 // Instead we call it using smol::block_on() in the unit test code after this
 // func
-async fn _do_message_subscriber_test() {
-    struct MyVersionMessage {
-        x: u32,
-    }
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::serial::{Decodable, Encodable};
+    use std::io;
 
-    impl Message for MyVersionMessage {
-        fn name() -> &'static str {
-            "verver"
+    #[async_std::test]
+    async fn message_subscriber_test() {
+        struct MyVersionMessage {
+            x: u32,
         }
-    }
 
-    impl Encodable for MyVersionMessage {
-        fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-            let mut len = 0;
-            len += self.x.encode(&mut s)?;
-            Ok(len)
+        impl Message for MyVersionMessage {
+            fn name() -> &'static str {
+                "verver"
+            }
         }
-    }
 
-    impl Decodable for MyVersionMessage {
-        fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-            Ok(Self { x: Decodable::decode(&mut d)? })
+        impl Encodable for MyVersionMessage {
+            fn encode<S: io::Write>(&self, mut s: S) -> core::result::Result<usize, io::Error> {
+                let mut len = 0;
+                len += self.x.encode(&mut s)?;
+                Ok(len)
+            }
         }
-    }
-    println!("hello");
-
-    let subsystem = MessageSubsystem::new();
-    subsystem.add_dispatch::<MyVersionMessage>().await;
 
-    // subscribe
-    //   1. get dispatcher
-    //   2. cast to specific type
-    //   3. do sub, return sub
-    let sub = subsystem.subscribe::<MyVersionMessage>().await.unwrap();
+        impl Decodable for MyVersionMessage {
+            fn decode<D: io::Read>(mut d: D) -> core::result::Result<Self, io::Error> {
+                Ok(Self { x: Decodable::decode(&mut d)? })
+            }
+        }
+        println!("hello");
 
-    let msg = MyVersionMessage { x: 110 };
-    let mut payload = Vec::new();
-    msg.encode(&mut payload).unwrap();
+        let subsystem = MessageSubsystem::new();
+        subsystem.add_dispatch::<MyVersionMessage>().await;
 
-    // receive message and publish
-    //   1. based on string, lookup relevant dispatcher interface
-    //   2. publish data there
-    subsystem.notify("verver", payload).await;
+        // subscribe
+        //   1. get dispatcher
+        //   2. cast to specific type
+        //   3. do sub, return sub
+        let sub = subsystem.subscribe::<MyVersionMessage>().await.unwrap();
 
-    // receive
-    //    1. do a get easy
-    let msg2 = sub.receive().await.unwrap();
-    assert_eq!(msg2.x, 110);
-    println!("{}", msg2.x);
+        let msg = MyVersionMessage { x: 110 };
+        let mut payload = Vec::new();
+        msg.encode(&mut payload).unwrap();
 
-    subsystem.trigger_error(Error::ChannelStopped).await;
+        // receive message and publish
+        //   1. based on string, lookup relevant dispatcher interface
+        //   2. publish data there
+        subsystem.notify("verver", payload).await;
 
-    let msg2 = sub.receive().await;
-    assert!(msg2.is_err());
+        // receive
+        //    1. do a get easy
+        let msg2 = sub.receive().await.unwrap();
+        assert_eq!(msg2.x, 110);
+        println!("{}", msg2.x);
 
-    sub.unsubscribe().await;
-}
+        subsystem.trigger_error(Error::ChannelStopped).await;
 
-#[cfg(test)]
-mod tests {
-    use super::*;
+        let msg2 = sub.receive().await;
+        assert!(msg2.is_err());
 
-    #[test]
-    fn test_message_subscriber() {
-        smol::block_on(_do_message_subscriber_test());
+        sub.unsubscribe().await;
     }
 }

+ 23 - 0
src/net/mod.rs

@@ -105,3 +105,26 @@ pub use transport::{
     TcpTransport, TorTransport, Transport, TransportListener, TransportName, TransportStream,
     UnixTransport,
 };
+
+// Relevant serializations
+use crate::serial::{Decodable, Encodable};
+use std::io;
+
+impl Encodable for url::Url {
+    fn encode<S: io::Write>(&self, s: S) -> core::result::Result<usize, io::Error> {
+        let mut len = 0;
+        len += self.as_str().to_string().encode(s)?;
+        Ok(len)
+    }
+}
+
+impl Decodable for url::Url {
+    fn decode<D: io::Read>(mut d: D) -> core::result::Result<Self, io::Error> {
+        let url_str: String = Decodable::decode(&mut d)?;
+        let url = match url::Url::parse(&url_str) {
+            Ok(v) => v,
+            Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
+        };
+        Ok(url)
+    }
+}

+ 1 - 1
src/net/p2p.rs

@@ -11,7 +11,7 @@ use url::Url;
 
 use crate::{
     system::{Subscriber, SubscriberPtr, Subscription},
-    util::sleep,
+    util::async_util::sleep,
     Result,
 };
 

+ 1 - 1
src/net/protocol/protocol_ping.rs

@@ -5,7 +5,7 @@ use log::{debug, error};
 use rand::Rng;
 use smol::Executor;
 
-use crate::{util::sleep, Error, Result};
+use crate::{util::async_util::sleep, Error, Result};
 
 use super::{
     super::{message, message_subscriber::MessageSubscription, ChannelPtr, P2pPtr, SettingsPtr},

+ 1 - 1
src/net/session/manual_session.rs

@@ -9,7 +9,7 @@ use url::Url;
 use crate::{
     net::TransportName,
     system::{StoppableTask, StoppableTaskPtr, Subscriber, SubscriberPtr, Subscription},
-    util::sleep,
+    util::async_util::sleep,
     Error, Result,
 };
 

+ 2 - 3
src/node/client.rs

@@ -8,14 +8,14 @@ use super::state::{state_transition, State};
 use crate::{
     crypto::{
         address::Address,
-        coin::Coin,
+        coin::{Coin, OwnCoin},
         constants::MERKLE_DEPTH,
         keypair::{Keypair, PublicKey},
         merkle_node::MerkleNode,
         proof::ProvingKey,
         types::DrkTokenId,
-        OwnCoin,
     },
+    serial::Encodable,
     tx::{
         builder::{
             TransactionBuilder, TransactionBuilderClearInputInfo, TransactionBuilderInputInfo,
@@ -23,7 +23,6 @@ use crate::{
         },
         Transaction,
     },
-    util::serial::Encodable,
     wallet::walletdb::{Balances, WalletPtr},
     zk::circuit::{BurnContract, MintContract},
     ClientFailed, ClientResult, Result,

+ 1 - 2
src/node/state.rs

@@ -5,14 +5,13 @@ use log::{debug, error};
 use crate::{
     blockchain::{nfstore::NullifierStore, rootstore::RootStore},
     crypto::{
-        coin::Coin,
+        coin::{Coin, OwnCoin},
         constants::MERKLE_DEPTH,
         keypair::{PublicKey, SecretKey},
         merkle_node::MerkleNode,
         note::{EncryptedNote, Note},
         nullifier::Nullifier,
         proof::VerifyingKey,
-        OwnCoin,
     },
     tx::Transaction,
     wallet::walletdb::WalletPtr,

+ 2 - 4
src/raft/consensus.rs

@@ -13,10 +13,8 @@ use rand::{rngs::OsRng, Rng, RngCore};
 
 use crate::{
     net,
-    util::{
-        gen_id,
-        serial::{deserialize, serialize, Decodable, Encodable},
-    },
+    serial::{deserialize, serialize, Decodable, Encodable},
+    util::gen_id,
     Error, Result,
 };
 

+ 1 - 1
src/raft/consensus_candidate.rs

@@ -2,7 +2,7 @@ use chrono::Utc;
 use log::info;
 
 use crate::{
-    util::serial::{serialize, Decodable, Encodable},
+    serial::{serialize, Decodable, Encodable},
     Result,
 };
 

+ 1 - 1
src/raft/consensus_follower.rs

@@ -3,7 +3,7 @@ use std::cmp::min;
 use log::debug;
 
 use crate::{
-    util::serial::{serialize, Decodable, Encodable},
+    serial::{serialize, Decodable, Encodable},
     Result,
 };
 

+ 1 - 1
src/raft/consensus_leader.rs

@@ -1,7 +1,7 @@
 use fxhash::FxHashMap;
 
 use crate::{
-    util::serial::{serialize, Decodable, Encodable},
+    serial::{serialize, Decodable, Encodable},
     Result,
 };
 

+ 1 - 1
src/raft/datastore.rs

@@ -4,7 +4,7 @@ use log::debug;
 use sled::Batch;
 
 use crate::{
-    util::serial::{deserialize, serialize, Decodable, Encodable},
+    serial::{deserialize, serialize, Decodable, Encodable},
     Error, Result,
 };
 

+ 2 - 2
src/raft/mod.rs

@@ -3,7 +3,7 @@ use async_std::sync::{Arc, Mutex};
 use chrono::Utc;
 use log::{debug, error};
 
-use crate::{net, util, Result};
+use crate::{net, util::async_util, Result};
 
 mod consensus;
 mod consensus_candidate;
@@ -26,7 +26,7 @@ async fn prune_map<T: Clone + Eq + std::hash::Hash>(
     seen_duration: i64,
 ) {
     loop {
-        util::sleep(seen_duration as u64).await;
+        async_util::sleep(seen_duration as u64).await;
         debug!(target: "raft", "Pruning item in map");
 
         let now = Utc::now().timestamp();

+ 3 - 3
src/raft/primitives.rs

@@ -3,7 +3,7 @@ use std::io;
 use fxhash::FxHashMap;
 
 use crate::{
-    util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable},
+    serial::{Decodable, Encodable, SerialDecodable, SerialEncodable},
     Error, Result,
 };
 
@@ -164,7 +164,7 @@ pub enum NetMsgMethod {
 }
 
 impl Encodable for NetMsgMethod {
-    fn encode<S: io::Write>(&self, s: S) -> Result<usize> {
+    fn encode<S: io::Write>(&self, s: S) -> core::result::Result<usize, io::Error> {
         let len: usize = match self {
             Self::LogResponse => 0,
             Self::LogRequest => 1,
@@ -178,7 +178,7 @@ impl Encodable for NetMsgMethod {
 }
 
 impl Decodable for NetMsgMethod {
-    fn decode<D: io::Read>(d: D) -> Result<Self> {
+    fn decode<D: io::Read>(d: D) -> core::result::Result<Self, io::Error> {
         let com: u8 = Decodable::decode(d)?;
         Ok(match com {
             0 => Self::LogResponse,

+ 1 - 1
src/raft/protocol_raft.rs

@@ -7,7 +7,7 @@ use fxhash::FxHashMap;
 use log::debug;
 use rand::{rngs::OsRng, RngCore};
 
-use crate::{net, util::serial::serialize, Result};
+use crate::{net, serial::serialize, Result};
 
 use super::primitives::{NetMsg, NetMsgMethod, NodeId, NodeIdMsg};
 

+ 1 - 0
src/serial/mod.rs

@@ -4,6 +4,7 @@ pub use darkfi_derive::{SerialDecodable, SerialEncodable};
 
 #[cfg(feature = "async-serial")]
 mod async_serial;
+
 mod encoding_types;
 mod endian;
 

+ 10 - 11
src/stakeholder/stakeholder.rs

@@ -1,6 +1,6 @@
 use async_executor::Executor;
 use async_std::sync::Arc;
-use log::{debug,info,error};
+use log::{debug, error, info};
 use std::fmt;
 
 use rand::rngs::OsRng;
@@ -22,12 +22,11 @@ use crate::{
         proof::{Proof, ProvingKey, VerifyingKey},
         schnorr::{SchnorrPublic, SchnorrSecret, Signature},
     },
-    net::{ChannelPtr, MessageSubscription, P2p, Settings, SettingsPtr},
-    system::Subscription,
+    net::{MessageSubscription, P2p, Settings, SettingsPtr},
     tx::Transaction,
     util::{
         clock::{Clock, Ticks},
-        expand_path,
+        path::expand_path,
         time::Timestamp,
     },
     Result,
@@ -39,7 +38,7 @@ use pasta_curves::pallas;
 
 use group::ff::PrimeField;
 
-const LOG_T : &str = "stakeholder";
+const LOG_T: &str = "stakeholder";
 
 #[derive(Debug)]
 pub struct SlotWorkspace {
@@ -172,7 +171,7 @@ impl Stakeholder {
             settings.peers,
         );
         let keypair = Keypair::random(&mut OsRng);
-        debug!(target:LOG_T, "stakeholder constructed");
+        debug!(target: LOG_T, "stakeholder constructed");
         Ok(Self {
             blockchain: bc,
             net: p2p,
@@ -229,7 +228,7 @@ impl Stakeholder {
         self.net.clone().start(exec.clone()).await?;
         //TODO (fix) await blocks
         self.net.clone().run(exec);
-        info!(target:LOG_T, "net initialized");
+        info!(target: LOG_T, "net initialized");
         Ok(())
     }
 
@@ -276,7 +275,7 @@ impl Stakeholder {
     /// validate the block proof, and the transactions,
     /// if so add the proof to metadata if stakeholder isn't the lead.
     pub async fn sync_block(&self) {
-        info!(target:LOG_T, "syncing blocks");
+        info!(target: LOG_T, "syncing blocks");
         for chanptr in self.net.channels().lock().await.values() {
             let message_subsytem = chanptr.get_message_subsystem();
             message_subsytem.add_dispatch::<BlockInfo>().await;
@@ -284,7 +283,7 @@ impl Stakeholder {
             //let info = chanptr.get_info();
             let msg_sub: MessageSubscription<BlockInfo> =
                 chanptr.subscribe_msg::<BlockInfo>().await.expect("missing blockinfo");
-            
+
             let res = msg_sub.receive().await.unwrap();
             let blk: BlockInfo = (*res).to_owned();
             //TODO validate the block proof, and transactions.
@@ -321,7 +320,7 @@ impl Stakeholder {
                 }
                 Ticks::NEWSLOT { e, sl } => self.new_slot(e, sl),
                 Ticks::TOCKS => {
-                    info!(target:LOG_T, "tocks");
+                    info!(target: LOG_T, "tocks");
                     // slot is about to end.
                     // sync, and validate.
                     // no more transactions to be received/send to the end of slot.
@@ -357,7 +356,7 @@ impl Stakeholder {
     /// assuming static stake during the epoch, enforced by the commitment to competing coins
     /// in the epoch's gen2esis data.
     fn new_epoch(&mut self) {
-        info!(target:LOG_T, "[new epoch] 4 {}", self);
+        info!(target: LOG_T, "[new epoch] 4 {}", self);
         let eta = self.get_eta();
         let mut epoch = Epoch::new(self.epoch_consensus, eta);
         //TODO calculate total stake

+ 1 - 1
src/tx/builder.rs

@@ -19,7 +19,7 @@ use crate::{
             DrkValueBlind,
         },
     },
-    util::serial::Encodable,
+    serial::Encodable,
     Result,
 };
 

+ 1 - 1
src/tx/mod.rs

@@ -15,7 +15,7 @@ use crate::{
         util::{pedersen_commitment_base, pedersen_commitment_u64},
         BurnRevealedValues, MintRevealedValues, Proof,
     },
-    util::serial::{Encodable, SerialDecodable, SerialEncodable, VarInt},
+    serial::{Encodable, SerialDecodable, SerialEncodable, VarInt},
     Result, VerifyFailed, VerifyResult,
 };
 

+ 1 - 1
src/tx/partial.rs

@@ -5,7 +5,7 @@ use crate::{
         types::{DrkTokenId, DrkValueBlind},
         BurnRevealedValues, Proof,
     },
-    util::serial::{SerialDecodable, SerialEncodable},
+    serial::{SerialDecodable, SerialEncodable},
 };
 
 #[derive(Clone, SerialEncodable, SerialDecodable)]

+ 4 - 7
src/wallet/walletdb.rs

@@ -13,19 +13,16 @@ use sqlx::{
 use crate::{
     crypto::{
         address::Address,
-        coin::Coin,
+        coin::{Coin, OwnCoin},
         constants::MERKLE_DEPTH,
         keypair::{Keypair, PublicKey, SecretKey},
         merkle_node::MerkleNode,
         note::Note,
         nullifier::Nullifier,
         types::DrkTokenId,
-        OwnCoin, OwnCoins,
-    },
-    util::{
-        expand_path,
-        serial::{deserialize, serialize},
     },
+    serial::{deserialize, serialize},
+    util::path::expand_path,
     Error::{WalletEmptyPassword, WalletTreeExists},
     Result,
 };
@@ -249,7 +246,7 @@ impl WalletDb {
         Ok(())
     }
 
-    pub async fn get_own_coins(&self) -> Result<OwnCoins> {
+    pub async fn get_own_coins(&self) -> Result<Vec<OwnCoin>> {
         debug!("Finding own coins");
         let is_spent = 0;
 

+ 1 - 1
src/zkas/compiler.rs

@@ -5,7 +5,7 @@ use super::{
     error::ErrorEmitter,
     types::StackType,
 };
-use crate::util::serial::{serialize, VarInt};
+use crate::serial::{serialize, VarInt};
 
 /// Version of the binary
 pub const BINARY_VERSION: u8 = 2;

+ 1 - 1
src/zkas/decoder.rs

@@ -1,6 +1,6 @@
 use super::{compiler::MAGIC_BYTES, types::StackType, LitType, Opcode, VarType};
 use crate::{
-    util::serial::{deserialize_partial, VarInt},
+    serial::{deserialize_partial, VarInt},
     Error::ZkasDecoderError as ZkasErr,
     Result,
 };