message revamp

This commit is contained in:
keorn 2016-11-08 18:01:31 +00:00
parent ea960f1c27
commit 4e36550890

View File

@ -16,67 +16,95 @@
//! Tendermint message handling. //! Tendermint message handling.
use super::{Height, Round, BlockHash}; use std::cmp::Ordering;
use super::{Height, Round, BlockHash, Step};
use rlp::{View, DecoderError, Decodable, Decoder, Encodable, RlpStream, Stream}; use rlp::{View, DecoderError, Decodable, Decoder, Encodable, RlpStream, Stream};
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub enum ConsensusMessage { pub enum Step {
Prevote(Height, Round, BlockHash), Prevote,
Precommit(Height, Round, BlockHash), Precommit
Commit(Height, BlockHash),
} }
/// (height, step, ...) #[derive(Debug, PartialEq, Eq)]
pub struct ConsensusMessage {
signature: H520,
height: Height,
round: Round,
step: Step,
block_hash: BlockHash
}
impl PartialOrd for ConsensusMessage {
fn partial_cmp(&self, m: &ConsensusMessage) -> Option<Ordering> {
Some(self.cmp(m))
}
}
impl Ord for ConsensusMessage {
fn cmp(&self, m: &ConsensusMessage) -> Ordering {
if self.height != m.height {
self.height.cmp(&m.height)
} else if self.round != m.round {
self.round.cmp(&m.round)
} else if self.step != m.step {
match self.step {
Step::Prevote => Ordering::Less,
Step::Precommit => Ordering::Greater,
}
} else {
self.block_hash.cmp(&m.block_hash)
}
}
}
impl Decodable for Step {
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
match try!(decoder.as_rlp().as_val()) {
0u8 => Ok(Step::Prevote),
1 => Ok(Step::Precommit),
_ => Err(DecoderError::Custom("Unknown step.")),
}
}
}
impl Encodable for Step {
fn rlp_append(&self, s: &mut RlpStream) {
match *self {
Step::Prevote => s.append(&0u8),
Step::Precommit => s.append(&1u8),
_ => panic!("No encoding needed for other steps"),
};
}
}
/// (signature, height, round, step, block_hash)
impl Decodable for ConsensusMessage { impl Decodable for ConsensusMessage {
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder { fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
// Handle according to step.
let rlp = decoder.as_rlp(); let rlp = decoder.as_rlp();
if decoder.as_raw().len() != try!(rlp.payload_info()).total() { if decoder.as_raw().len() != try!(rlp.payload_info()).total() {
return Err(DecoderError::RlpIsTooBig); return Err(DecoderError::RlpIsTooBig);
} }
let height = try!(rlp.val_at(0)); let m = rlp.at(1);
Ok(match try!(rlp.val_at(1)) { Ok(ConsensusMessage {
0u8 => ConsensusMessage::Prevote( signature: try!(rlp.val_at(0)),
height, height: try!(m.val_at(0)),
try!(rlp.val_at(2)), round: try!(m.val_at(1)),
try!(rlp.val_at(3)) step: try!(m.val_at(2)),
), block_hash: try!(m.val_at(3))
1 => ConsensusMessage::Precommit(
height,
try!(rlp.val_at(2)),
try!(rlp.val_at(3))
),
2 => ConsensusMessage::Commit(
height,
try!(rlp.val_at(2))),
_ => return Err(DecoderError::Custom("Unknown step.")),
}) })
} }
} }
impl Encodable for ConsensusMessage { impl Encodable for ConsensusMessage {
fn rlp_append(&self, s: &mut RlpStream) { fn rlp_append(&self, s: &mut RlpStream) {
match *self { s.begin_list(2);
ConsensusMessage::Prevote(h, r, hash) => { s.append(&self.signature);
s.begin_list(4); s.begin_list(4);
s.append(&h); s.append(&self.height);
s.append(&0u8); s.append(&self.round);
s.append(&r); s.append(&self.step);
s.append(&hash); s.append(&self.block_hash);
},
ConsensusMessage::Precommit(h, r, hash) => {
s.begin_list(4);
s.append(&h);
s.append(&1u8);
s.append(&r);
s.append(&hash);
},
ConsensusMessage::Commit(h, hash) => {
s.begin_list(3);
s.append(&h);
s.append(&2u8);
s.append(&hash);
},
}
} }
} }