Skip to content

Commit

Permalink
node-data: change Quorum to use RatificationResult
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Feb 12, 2024
1 parent 60120f4 commit 3e39b3e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
46 changes: 45 additions & 1 deletion node-data/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -194,6 +194,45 @@ impl Serializable for StepVotes {
}
}

impl Serializable for RatificationResult {
fn write<W: 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: Read>(r: &mut R) -> io::Result<Self>
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<W: Write>(&self, w: &mut W) -> io::Result<()> {
let count = self.cert_list.len() as u8;
Expand Down Expand Up @@ -401,4 +440,9 @@ mod tests {
fn test_encoding_block() {
assert_serializable::<Block>();
}

#[test]
fn test_encoding_ratification_result() {
assert_serializable::<RatificationResult>();
}
}
41 changes: 36 additions & 5 deletions node-data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,18 +611,45 @@ 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<Vote> 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,
}

impl Serializable for Quorum {
fn write<W: 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)?;

Expand All @@ -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,
})
}
}
Expand All @@ -656,6 +683,10 @@ pub mod payload {
ratification: self.ratification,
}
}

pub fn vote(&self) -> &Vote {
self.result.vote()
}
}

#[derive(Debug, Clone, Default)]
Expand Down Expand Up @@ -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),
});
Expand Down

0 comments on commit 3e39b3e

Please sign in to comment.