Forráskód Böngészése

serial: implement Encodable/ Decodable for Vec<T>. deprecates impl_vec! macro

lunar-mining 4 éve
szülő
commit
11a71fc1c8

+ 3 - 10
src/consensus/block.rs

@@ -1,4 +1,4 @@
-use std::{fmt, io};
+use std::fmt;
 
 use incrementalmerkletree::{bridgetree::BridgeTree, Tree};
 use log::debug;
@@ -12,13 +12,12 @@ use crate::{
         address::Address, constants::MERKLE_DEPTH, keypair::Keypair, merkle_node::MerkleNode,
         schnorr::SchnorrSecret,
     },
-    impl_vec, net,
+    net,
     tx::Transaction,
     util::{
-        serial::{serialize, Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
+        serial::{serialize, SerialDecodable, SerialEncodable},
         time::Timestamp,
     },
-    Result,
 };
 
 /// This struct represents a tuple of the form (version, state, epoch, slot, timestamp, merkle_root).
@@ -146,8 +145,6 @@ impl net::Message for BlockInfo {
     }
 }
 
-impl_vec!(BlockInfo);
-
 /// Auxiliary structure used for blockchain syncing
 #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
 pub struct BlockResponse {
@@ -208,8 +205,6 @@ impl net::Message for BlockProposal {
     }
 }
 
-impl_vec!(BlockProposal);
-
 impl From<BlockProposal> for BlockInfo {
     fn from(block: BlockProposal) -> BlockInfo {
         block.block
@@ -279,5 +274,3 @@ impl ProposalChain {
         true
     }
 }
-
-impl_vec!(ProposalChain);

+ 2 - 7
src/consensus/participant.rs

@@ -1,10 +1,7 @@
-use std::io;
-
 use crate::{
     crypto::{address::Address, keypair::PublicKey},
-    impl_vec, net,
-    util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
-    Result,
+    net,
+    util::serial::{SerialDecodable, SerialEncodable},
 };
 
 /// This struct represents a tuple of the form:
@@ -34,5 +31,3 @@ impl net::Message for Participant {
         "participant"
     }
 }
-
-impl_vec!(Participant);

+ 2 - 7
src/consensus/vote.rs

@@ -1,10 +1,7 @@
-use std::io;
-
 use crate::{
     crypto::{address::Address, schnorr::Signature},
-    impl_vec, net,
-    util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
-    Result,
+    net,
+    util::serial::{SerialDecodable, SerialEncodable},
 };
 
 /// This struct represents a `Vote` used by the Streamlet consensus
@@ -31,5 +28,3 @@ impl net::Message for Vote {
         "vote"
     }
 }
-
-impl_vec!(Vote);

+ 1 - 7
src/crypto/mod.rs

@@ -24,13 +24,8 @@ pub use proof::Proof;
 //pub mod lead_proof;
 //pub mod leadcoin;
 
-use crate::{
-    impl_vec,
-    util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
-    Result,
-};
+use crate::util::serial::{SerialDecodable, SerialEncodable};
 use keypair::SecretKey;
-use std::io;
 
 #[derive(Clone, Debug, PartialEq, Eq, SerialEncodable, SerialDecodable)]
 pub struct OwnCoin {
@@ -42,4 +37,3 @@ pub struct OwnCoin {
 }
 
 pub type OwnCoins = Vec<OwnCoin>;
-impl_vec!(OwnCoin);

+ 1 - 4
src/raft/primitives.rs

@@ -3,8 +3,7 @@ use std::io;
 use fxhash::FxHashMap;
 
 use crate::{
-    impl_vec,
-    util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
+    util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable},
     Error, Result,
 };
 
@@ -191,5 +190,3 @@ impl Decodable for NetMsgMethod {
         })
     }
 }
-
-impl_vec!(Log);

+ 1 - 7
src/tx/mod.rs

@@ -16,8 +16,7 @@ use crate::{
         util::{pedersen_commitment_base, pedersen_commitment_u64},
         BurnRevealedValues, MintRevealedValues, Proof,
     },
-    impl_vec,
-    util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
+    util::serial::{Encodable, SerialDecodable, SerialEncodable, VarInt},
     Result, VerifyFailed, VerifyResult,
 };
 
@@ -240,10 +239,5 @@ macro_rules! impl_vec_without_signature {
         }
     };
 }
-
 impl_vec_without_signature!(TransactionClearInput);
 impl_vec_without_signature!(TransactionInput);
-impl_vec!(TransactionClearInput);
-impl_vec!(TransactionInput);
-impl_vec!(TransactionOutput);
-impl_vec!(Transaction);

+ 1 - 8
src/tx/partial.rs

@@ -1,5 +1,3 @@
-use std::io;
-
 use super::TransactionOutput;
 use crate::{
     crypto::{
@@ -7,9 +5,7 @@ use crate::{
         types::{DrkTokenId, DrkValueBlind},
         BurnRevealedValues, Proof,
     },
-    impl_vec,
-    util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
-    Result,
+    util::serial::{SerialDecodable, SerialEncodable},
 };
 
 #[derive(Clone, SerialEncodable, SerialDecodable)]
@@ -33,6 +29,3 @@ pub struct PartialTransactionInput {
     pub burn_proof: Proof,
     pub revealed: BurnRevealedValues,
 }
-
-impl_vec!(PartialTransactionClearInput);
-impl_vec!(PartialTransactionInput);

+ 2 - 53
src/util/serial.rs

@@ -451,7 +451,7 @@ impl<T: Decodable> Decodable for Option<T> {
     }
 }
 
-impl<T: Encodable> Encodable for Vec<Option<T>> {
+impl<T: Encodable> Encodable for Vec<T> {
     fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
         let mut len = 0;
         len += VarInt(self.len() as u64).encode(&mut s)?;
@@ -461,7 +461,7 @@ impl<T: Encodable> Encodable for Vec<Option<T>> {
         Ok(len)
     }
 }
-impl<T: Decodable> Decodable for Vec<Option<T>> {
+impl<T: Decodable> Decodable for Vec<T> {
     fn decode<D: io::Read>(mut d: D) -> Result<Self> {
         let len = VarInt::decode(&mut d)?.0;
         let mut ret = Vec::with_capacity(len as usize);
@@ -472,40 +472,6 @@ impl<T: Decodable> Decodable for Vec<Option<T>> {
     }
 }
 
-// Vectors
-#[macro_export]
-macro_rules! impl_vec {
-    ($type: ty) => {
-        impl Encodable for Vec<$type> {
-            #[inline]
-            fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-                let mut len = 0;
-                len += VarInt(self.len() as u64).encode(&mut s)?;
-                for c in self.iter() {
-                    len += c.encode(&mut s)?;
-                }
-                Ok(len)
-            }
-        }
-        impl Decodable for Vec<$type> {
-            #[inline]
-            fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-                let len = VarInt::decode(&mut d)?.0;
-                let mut ret = Vec::with_capacity(len as usize);
-                for _ in 0..len {
-                    ret.push(Decodable::decode(&mut d)?);
-                }
-                Ok(ret)
-            }
-        }
-    };
-}
-
-impl_vec!(SocketAddr);
-impl_vec!(Url);
-impl_vec!([u8; 32]);
-impl_vec!(blake3::Hash);
-
 impl Encodable for IpAddr {
     fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
         let mut len = 0;
@@ -601,23 +567,6 @@ pub fn encode_with_size<S: io::Write>(data: &[u8], mut s: S) -> Result<usize> {
     Ok(vi_len + data.len())
 }
 
-impl Encodable for Vec<u8> {
-    #[inline]
-    fn encode<S: io::Write>(&self, s: S) -> Result<usize> {
-        encode_with_size(self, s)
-    }
-}
-
-impl Decodable for Vec<u8> {
-    #[inline]
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        let len = VarInt::decode(&mut d)?.0 as usize;
-        let mut ret = vec![0u8; len];
-        d.read_slice(&mut ret)?;
-        Ok(ret)
-    }
-}
-
 impl Encodable for Box<[u8]> {
     #[inline]
     fn encode<S: io::Write>(&self, s: S) -> Result<usize> {