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

src/consensus: updated structures with new serialization derives.

aggstam 4 лет назад
Родитель
Сommit
5d94a1406e
5 измененных файлов с 16 добавлено и 179 удалено
  1. 5 55
      src/consensus/block.rs
  2. 4 65
      src/consensus/metadata.rs
  3. 2 17
      src/consensus/tx.rs
  4. 3 18
      src/consensus/util.rs
  5. 2 24
      src/consensus/vote.rs

+ 5 - 55
src/consensus/block.rs

@@ -1,16 +1,12 @@
 use serde::{Deserialize, Serialize};
-use std::{
-    hash::{Hash, Hasher},
-    io,
-};
+use std::hash::{Hash, Hasher};
 
 use super::{metadata::Metadata, tx::Tx};
 
 use crate::{
     crypto::{keypair::PublicKey, schnorr::Signature},
     net,
-    util::serial::{Decodable, Encodable},
-    Result,
+    util::serial::{SerialDecodable, SerialEncodable},
 };
 
 /// This struct represents a tuple of the form (st, sl, txs, metadata).
@@ -18,7 +14,7 @@ use crate::{
 #[derive(Debug, Clone, Deserialize, Serialize)]
 pub struct Block {
     /// Previous block hash
-    pub st: String,
+    pub st: String, // Change this to a proper hash type
     /// Slot uid, generated by the beacon
     pub sl: u64,
     /// Transactions payload
@@ -45,28 +41,7 @@ impl Hash for Block {
     }
 }
 
-impl Encodable for Block {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        let mut len = 0;
-        len += self.st.encode(&mut s).unwrap();
-        len += self.sl.encode(&mut s).unwrap();
-        len += self.txs.encode(&mut s).unwrap();
-        len += self.metadata.encode(&mut s).unwrap();
-        Ok(len)
-    }
-}
-
-impl Decodable for Block {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        let st = Decodable::decode(&mut d)?;
-        let sl = Decodable::decode(&mut d)?;
-        let txs = Decodable::decode(&mut d)?;
-        let metadata = Decodable::decode(&mut d)?;
-        Ok(Self { st, sl, txs, metadata })
-    }
-}
-
-#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
+#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, SerialEncodable, SerialDecodable)]
 pub struct BlockProposal {
     /// leader public key
     pub public_key: PublicKey,
@@ -75,7 +50,7 @@ pub struct BlockProposal {
     /// leader id
     pub id: u64,
     /// Previous block hash
-    pub st: String,
+    pub st: String, // Change this to a proper hash type
     /// Slot uid, generated by the beacon
     pub sl: u64,
     /// Transactions payload
@@ -101,31 +76,6 @@ impl net::Message for BlockProposal {
     }
 }
 
-impl Encodable for BlockProposal {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        let mut len = 0;
-        len += self.public_key.encode(&mut s).unwrap();
-        len += self.signature.encode(&mut s).unwrap();
-        len += self.id.encode(&mut s).unwrap();
-        len += self.st.encode(&mut s).unwrap();
-        len += self.sl.encode(&mut s).unwrap();
-        len += self.txs.encode(&mut s).unwrap();
-        Ok(len)
-    }
-}
-
-impl Decodable for BlockProposal {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        let public_key = Decodable::decode(&mut d)?;
-        let signature = Decodable::decode(&mut d)?;
-        let id = Decodable::decode(&mut d)?;
-        let st = Decodable::decode(&mut d)?;
-        let sl = Decodable::decode(&mut d)?;
-        let txs = Decodable::decode(&mut d)?;
-        Ok(Self { public_key, signature, id, st, sl, txs })
-    }
-}
-
 pub fn proposal_eq_block(proposal: &BlockProposal, block: &Block) -> bool {
     proposal.st == block.st && proposal.sl == block.sl && proposal.txs == block.txs
 }

+ 4 - 65
src/consensus/metadata.rs

@@ -1,18 +1,14 @@
 use serde::{Deserialize, Serialize};
-use std::io;
 
 use super::{
     util::{get_current_time, Timestamp},
     vote::Vote,
 };
 
-use crate::{
-    util::serial::{Decodable, Encodable},
-    Result,
-};
+use crate::util::serial::{SerialDecodable, SerialEncodable};
 
 /// This struct represents additional Block information used by the consensus protocol.
-#[derive(Debug, Clone, Deserialize, Serialize)]
+#[derive(Debug, Clone, Deserialize, Serialize, SerialEncodable, SerialDecodable)]
 pub struct Metadata {
     /// Block information used by Ouroboros consensus
     pub om: OuroborosMetadata,
@@ -32,27 +28,8 @@ impl Metadata {
     }
 }
 
-impl Encodable for Metadata {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        let mut len = 0;
-        len += self.om.encode(&mut s).unwrap();
-        len += self.sm.encode(&mut s).unwrap();
-        len += self.timestamp.encode(&mut s).unwrap();
-        Ok(len)
-    }
-}
-
-impl Decodable for Metadata {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        let om = Decodable::decode(&mut d)?;
-        let sm = Decodable::decode(&mut d)?;
-        let timestamp = Decodable::decode(&mut d)?;
-        Ok(Self { om, sm, timestamp })
-    }
-}
-
 /// This struct represents Block information used by Ouroboros consensus protocol.
-#[derive(Debug, Clone, Deserialize, Serialize)]
+#[derive(Debug, Clone, Deserialize, Serialize, SerialEncodable, SerialDecodable)]
 pub struct OuroborosMetadata {
     /// Proof the stakeholder is the block owner
     pub proof: String,
@@ -68,27 +45,8 @@ impl OuroborosMetadata {
     }
 }
 
-impl Encodable for OuroborosMetadata {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        let mut len = 0;
-        len += self.proof.encode(&mut s).unwrap();
-        len += self.r.encode(&mut s).unwrap();
-        len += self.s.encode(&mut s).unwrap();
-        Ok(len)
-    }
-}
-
-impl Decodable for OuroborosMetadata {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        let proof = Decodable::decode(&mut d)?;
-        let r = Decodable::decode(&mut d)?;
-        let s = Decodable::decode(&mut d)?;
-        Ok(Self { proof, r, s })
-    }
-}
-
 /// This struct represents Block information used by Streamlet consensus protocol.
-#[derive(Debug, Clone, Deserialize, Serialize)]
+#[derive(Debug, Clone, Deserialize, Serialize, SerialEncodable, SerialDecodable)]
 pub struct StreamletMetadata {
     /// Epoch votes
     pub votes: Vec<Vote>,
@@ -103,22 +61,3 @@ impl StreamletMetadata {
         StreamletMetadata { votes: Vec::new(), notarized: false, finalized: false }
     }
 }
-
-impl Encodable for StreamletMetadata {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        let mut len = 0;
-        len += self.votes.encode(&mut s).unwrap();
-        len += self.notarized.encode(&mut s).unwrap();
-        len += self.finalized.encode(&mut s).unwrap();
-        Ok(len)
-    }
-}
-
-impl Decodable for StreamletMetadata {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        let votes = Decodable::decode(&mut d)?;
-        let notarized = Decodable::decode(&mut d)?;
-        let finalized = Decodable::decode(&mut d)?;
-        Ok(Self { votes, notarized, finalized })
-    }
-}

+ 2 - 17
src/consensus/tx.rs

@@ -3,11 +3,11 @@ use std::io;
 
 use crate::{
     impl_vec, net,
-    util::serial::{Decodable, Encodable, VarInt},
+    util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
     Result,
 };
 
-#[derive(Debug, Clone, Deserialize, Serialize, Eq, Hash, PartialEq)]
+#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, SerialEncodable, SerialDecodable)]
 pub struct Tx {
     pub hash: u32, // Change this to a proper hash type
     pub payload: String,
@@ -19,19 +19,4 @@ impl net::Message for Tx {
     }
 }
 
-impl Encodable for Tx {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        let mut len = 0;
-        len += self.hash.encode(&mut s)?;
-        len += self.payload.encode(&mut s)?;
-        Ok(len)
-    }
-}
-
-impl Decodable for Tx {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        Ok(Self { hash: Decodable::decode(&mut d)?, payload: Decodable::decode(&mut d)? })
-    }
-}
-
 impl_vec!(Tx);

+ 3 - 18
src/consensus/util.rs

@@ -1,9 +1,9 @@
 use chrono::{NaiveDateTime, Utc};
 use serde::{de::DeserializeOwned, Deserialize, Serialize};
-use std::{fs::File, io, io::BufReader, path::PathBuf};
+use std::{fs::File, io::BufReader, path::PathBuf};
 
 use crate::{
-    util::serial::{Decodable, Encodable},
+    util::serial::{SerialDecodable, SerialEncodable},
     Result,
 };
 
@@ -24,7 +24,7 @@ pub fn save<T: Serialize>(path: &PathBuf, value: &T) -> Result<()> {
 }
 
 /// Util structure to represend chrono UTC timestamps.
-#[derive(Debug, Clone, Serialize, Deserialize)]
+#[derive(Debug, Clone, Serialize, Deserialize, SerialDecodable, SerialEncodable)]
 pub struct Timestamp(pub i64);
 
 impl Timestamp {
@@ -37,21 +37,6 @@ impl Timestamp {
     }
 }
 
-impl Encodable for Timestamp {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        let mut len = 0;
-        len += self.0.encode(&mut s).unwrap();
-        Ok(len)
-    }
-}
-
-impl Decodable for Timestamp {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        let timestamp = Decodable::decode(&mut d)?;
-        Ok(Timestamp(timestamp))
-    }
-}
-
 /// Util function to generate a Timestamp of current time.
 pub fn get_current_time() -> Timestamp {
     Timestamp(Utc::now().timestamp())

+ 2 - 24
src/consensus/vote.rs

@@ -6,12 +6,12 @@ use super::block::BlockProposal;
 use crate::{
     crypto::{keypair::PublicKey, schnorr::Signature},
     impl_vec, net,
-    util::serial::{Decodable, Encodable, VarInt},
+    util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
     Result,
 };
 
 /// This struct represents a tuple of the form (vote, B, id).
-#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
+#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, SerialDecodable, SerialEncodable)]
 pub struct Vote {
     /// Node public key
     pub node_public_key: PublicKey,
@@ -35,26 +35,4 @@ impl net::Message for Vote {
     }
 }
 
-impl Encodable for Vote {
-    fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
-        let mut len = 0;
-        len += self.node_public_key.encode(&mut s)?;
-        len += self.vote.encode(&mut s)?;
-        len += self.block.encode(&mut s)?;
-        len += self.id.encode(&mut s)?;
-        Ok(len)
-    }
-}
-
-impl Decodable for Vote {
-    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
-        Ok(Self {
-            node_public_key: Decodable::decode(&mut d)?,
-            vote: Decodable::decode(&mut d)?,
-            block: Decodable::decode(&mut d)?,
-            id: Decodable::decode(&mut d)?,
-        })
-    }
-}
-
 impl_vec!(Vote);