Skip to content

Commit

Permalink
Treat empty block id as None
Browse files Browse the repository at this point in the history
and correctly compute header hash when block id is empty.
  • Loading branch information
yihuang committed Nov 19, 2019
1 parent 23042bb commit 54da372
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
29 changes: 20 additions & 9 deletions tendermint/src/amino_types/block_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryFrom;

use super::validate::{ConsensusMessage, ValidationError, ValidationErrorKind::*};
use crate::block::parts;
use crate::{
Expand Down Expand Up @@ -32,12 +34,18 @@ impl block::ParseId for BlockId {
}
}

impl From<&block::Id> for BlockId {
fn from(bid: &block::Id) -> Self {
let bid_hash = bid.hash.as_bytes().unwrap().to_vec();
match &bid.parts {
Some(parts) => BlockId::new(bid_hash, Some(PartsSetHeader::from(parts))),
None => BlockId::new(bid_hash, None),
impl TryFrom<&block::Id> for BlockId {
type Error = ();
fn try_from(bid: &block::Id) -> Result<Self, ()> {
match bid.hash.as_bytes() {
None => Err(()),
Some(bid_hash) => match &bid.parts {
Some(parts) => Ok(BlockId::new(
bid_hash.to_vec(),
PartsSetHeader::try_from(parts).ok(),
)),
None => Ok(BlockId::new(bid_hash.to_vec(), None)),
},
}
}
}
Expand Down Expand Up @@ -87,9 +95,12 @@ impl PartsSetHeader {
}
}

impl From<&parts::Header> for PartsSetHeader {
fn from(parts: &parts::Header) -> Self {
PartsSetHeader::new(parts.total as i64, parts.hash.as_bytes().unwrap().to_vec())
impl TryFrom<&parts::Header> for PartsSetHeader {
type Error = ();
fn try_from(parts: &parts::Header) -> Result<Self, ()> {
parts.hash.as_bytes().map_or(Err(()), |bytes| {
Ok(PartsSetHeader::new(parts.total as i64, bytes.to_vec()))
})
}
}

Expand Down
4 changes: 3 additions & 1 deletion tendermint/src/amino_types/vote.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryFrom;

use super::{
block_id::{BlockId, CanonicalBlockId, CanonicalPartSetHeader},
remote_error::RemoteError,
Expand Down Expand Up @@ -63,7 +65,7 @@ impl From<&vote::Vote> for Vote {
_ => vec![],
},
parts_header: match &vote.block_id.parts {
Some(parts) => Some(PartsSetHeader::from(parts)),
Some(parts) => PartsSetHeader::try_from(parts).ok(),
None => None,
},
}),
Expand Down
10 changes: 9 additions & 1 deletion tendermint/src/block/header.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Block headers
use std::convert::TryFrom;

use crate::merkle::simple_hash_from_byte_slices;
use crate::{account, amino_types, block, chain, lite, Hash, Time};
use amino_types::{message::AminoMessage, BlockId, ConsensusVersion, TimeMsg};
Expand Down Expand Up @@ -103,7 +105,13 @@ impl lite::Header for Header {
byteslices.push(AminoMessage::bytes_vec(&TimeMsg::from(self.time)));
byteslices.push(encode_varint(self.num_txs));
byteslices.push(encode_varint(self.total_txs));
byteslices.push(AminoMessage::bytes_vec(&BlockId::from(&self.last_block_id)));
byteslices.push(
if let Ok(block_id) = BlockId::try_from(&self.last_block_id) {
AminoMessage::bytes_vec(&block_id)
} else {
vec![]
},
);
byteslices.push(encode_hash(self.last_commit_hash));
byteslices.push(encode_hash(self.data_hash));
byteslices.push(encode_hash(self.validators_hash));
Expand Down

0 comments on commit 54da372

Please sign in to comment.