diff --git a/node-data/src/encoding.rs b/node-data/src/encoding.rs index 95dbff3572..f470aea502 100644 --- a/node-data/src/encoding.rs +++ b/node-data/src/encoding.rs @@ -10,7 +10,7 @@ use crate::ledger::{ StepVotes, Transaction, }; use crate::message::payload::{ - QuorumType, Ratification, ValidationResult, Vote, + QuorumType, Ratification, RatificationResult, ValidationResult, Vote, }; use crate::message::{ConsensusHeader, SignInfo}; use crate::Serializable; @@ -194,6 +194,45 @@ impl Serializable for StepVotes { } } +impl Serializable for RatificationResult { + fn write(&self, w: &mut W) -> io::Result<()> { + match self { + RatificationResult::Fail(v) => { + w.write_all(&[0])?; + v.write(w)?; + } + + RatificationResult::Success(v) => { + w.write_all(&[1])?; + v.write(w)?; + } + } + + Ok(()) + } + + fn read(r: &mut R) -> io::Result + where + Self: Sized, + { + let result = match Self::read_u8(r)? { + 0 => { + let vote = Vote::read(r)?; + Self::Fail(vote) + } + 1 => { + let vote = Vote::read(r)?; + Self::Success(vote) + } + _ => Err(io::Error::new( + io::ErrorKind::InvalidData, + "Invalid RatificationResult", + ))?, + }; + Ok(result) + } +} + impl Serializable for IterationsInfo { fn write(&self, w: &mut W) -> io::Result<()> { let count = self.cert_list.len() as u8; @@ -401,4 +440,9 @@ mod tests { fn test_encoding_block() { assert_serializable::(); } + + #[test] + fn test_encoding_ratification_result() { + assert_serializable::(); + } } diff --git a/node-data/src/message.rs b/node-data/src/message.rs index fc83a4d6d2..62e9174c7a 100644 --- a/node-data/src/message.rs +++ b/node-data/src/message.rs @@ -611,10 +611,37 @@ pub mod payload { } } + #[derive(Debug, Clone, Eq, PartialEq)] + #[cfg_attr(any(feature = "faker", test), derive(fake::Dummy))] + pub enum RatificationResult { + Fail(Vote), + Success(Vote), + } + + impl From for RatificationResult { + fn from(vote: Vote) -> Self { + match vote { + Vote::Valid(hash) => { + RatificationResult::Success(Vote::Valid(hash)) + } + fail => RatificationResult::Fail(fail), + } + } + } + + impl RatificationResult { + pub fn vote(&self) -> &Vote { + match self { + Self::Success(v) => v, + Self::Fail(v) => v, + } + } + } + #[derive(Debug, Clone, Eq, PartialEq)] pub struct Quorum { pub header: ConsensusHeader, - pub vote: Vote, + pub result: RatificationResult, pub validation: StepVotes, pub ratification: StepVotes, } @@ -622,7 +649,7 @@ pub mod payload { impl Serializable for Quorum { fn write(&self, w: &mut W) -> io::Result<()> { self.header.write(w)?; - self.vote.write(w)?; + self.result.write(w)?; self.validation.write(w)?; self.ratification.write(w)?; @@ -634,16 +661,16 @@ pub mod payload { Self: Sized, { let header = ConsensusHeader::read(r)?; - let vote = Vote::read(r)?; + let result = RatificationResult::read(r)?; let validation = StepVotes::read(r)?; let ratification = StepVotes::read(r)?; Ok(Quorum { header, - vote, validation, ratification, + result, }) } } @@ -656,6 +683,10 @@ pub mod payload { ratification: self.ratification, } } + + pub fn vote(&self) -> &Vote { + self.result.vote() + } } #[derive(Debug, Clone, Default)] @@ -1184,7 +1215,7 @@ mod tests { assert_serialize(payload::Quorum { header: consensus_header.clone(), - vote: payload::Vote::Valid([4; 32]), + result: payload::Vote::Valid([4; 32]).into(), validation: ledger::StepVotes::new([1; 48], 12345), ratification: ledger::StepVotes::new([2; 48], 98765), });