diff --git a/src/consensus/block.rs b/src/consensus/block.rs index c793379b7..8c115e67e 100644 --- a/src/consensus/block.rs +++ b/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(&self, mut s: S) -> Result { - 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(mut d: D) -> Result { - 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(&self, mut s: S) -> Result { - 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(mut d: D) -> Result { - 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 } diff --git a/src/consensus/metadata.rs b/src/consensus/metadata.rs index 6a7a9f376..1602de6dd 100644 --- a/src/consensus/metadata.rs +++ b/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(&self, mut s: S) -> Result { - 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(mut d: D) -> Result { - 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(&self, mut s: S) -> Result { - 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(mut d: D) -> Result { - 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, @@ -103,22 +61,3 @@ impl StreamletMetadata { StreamletMetadata { votes: Vec::new(), notarized: false, finalized: false } } } - -impl Encodable for StreamletMetadata { - fn encode(&self, mut s: S) -> Result { - 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(mut d: D) -> Result { - let votes = Decodable::decode(&mut d)?; - let notarized = Decodable::decode(&mut d)?; - let finalized = Decodable::decode(&mut d)?; - Ok(Self { votes, notarized, finalized }) - } -} diff --git a/src/consensus/tx.rs b/src/consensus/tx.rs index b7f64a9e8..289be376a 100644 --- a/src/consensus/tx.rs +++ b/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(&self, mut s: S) -> Result { - let mut len = 0; - len += self.hash.encode(&mut s)?; - len += self.payload.encode(&mut s)?; - Ok(len) - } -} - -impl Decodable for Tx { - fn decode(mut d: D) -> Result { - Ok(Self { hash: Decodable::decode(&mut d)?, payload: Decodable::decode(&mut d)? }) - } -} - impl_vec!(Tx); diff --git a/src/consensus/util.rs b/src/consensus/util.rs index cfc74af4e..8e3e6eb84 100644 --- a/src/consensus/util.rs +++ b/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(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(&self, mut s: S) -> Result { - let mut len = 0; - len += self.0.encode(&mut s).unwrap(); - Ok(len) - } -} - -impl Decodable for Timestamp { - fn decode(mut d: D) -> Result { - 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()) diff --git a/src/consensus/vote.rs b/src/consensus/vote.rs index 26d98ddd4..0147a2765 100644 --- a/src/consensus/vote.rs +++ b/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(&self, mut s: S) -> Result { - 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(mut d: D) -> Result { - 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);