Skip to content

Commit

Permalink
working towards hashing the header: conversion between fields and their
Browse files Browse the repository at this point in the history
respective amino types, and, directly encode some
  • Loading branch information
liamsi committed Sep 25, 2019
1 parent 4b44d6a commit 360a37a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
29 changes: 29 additions & 0 deletions tendermint/src/amino_types/block_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::validate::{ConsensusMessage, ValidationError, ValidationErrorKind::*};
use crate::block::parts;
use crate::{
block,
error::Error,
Expand All @@ -14,6 +15,12 @@ pub struct BlockId {
pub parts_header: Option<PartsSetHeader>,
}

impl BlockId {
pub fn new(hash: Vec<u8>, parts_header: Option<PartsSetHeader>) -> Self {
BlockId { hash, parts_header }
}
}

impl block::ParseId for BlockId {
fn parse_block_id(&self) -> Result<block::Id, Error> {
let hash = Hash::new(hash::Algorithm::Sha256, &self.hash)?;
Expand All @@ -25,6 +32,16 @@ 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 ConsensusMessage for BlockId {
fn validate_basic(&self) -> Result<(), ValidationError> {
// Hash can be empty in case of POLBlockID in Proposal.
Expand Down Expand Up @@ -64,6 +81,18 @@ pub struct PartsSetHeader {
pub hash: Vec<u8>,
}

impl PartsSetHeader {
pub fn new(total: i64, hash: Vec<u8>) -> Self {
PartsSetHeader { total, hash }
}
}

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 PartsSetHeader {
fn parse_parts_header(&self) -> Option<block::parts::Header> {
Hash::new(hash::Algorithm::Sha256, &self.hash)
Expand Down
27 changes: 26 additions & 1 deletion tendermint/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,34 @@ impl lite::Header for Header {
amino_types::ConsensusVersion::from(&self.version)
.encode(&mut version_enc)
.unwrap();
let mut height_enc = vec![];
prost_amino::encoding::encode_varint(self.height.value(), &mut height_enc);
let mut time_enc = vec![];
amino_types::TimeMsg::from(self.time)
.encode(&mut time_enc)
.unwrap();
let chain_id_bytes = self.chain_id.as_bytes();
let mut chain_id_enc = vec![];
prost_amino::encode_length_delimiter(chain_id_bytes.len(), &mut chain_id_enc).unwrap();
chain_id_enc.append(&mut chain_id_bytes.to_vec());
let mut num_tx_enc = vec![];
prost_amino::encoding::encode_varint(self.num_txs, &mut num_tx_enc);
let mut total_tx_enc = vec![];
prost_amino::encoding::encode_varint(self.total_txs, &mut total_tx_enc);
let mut last_block_id_enc = vec![];
amino_types::BlockId::from(&self.last_block_id)
.encode(&mut last_block_id_enc)
.unwrap();

let mut byteslices: Vec<&[u8]> = vec![];
byteslices.push(&mut version_enc);
byteslices.push(version_enc.as_slice());
byteslices.push(chain_id_enc.as_slice());
byteslices.push(height_enc.as_slice());
byteslices.push(time_enc.as_slice());
byteslices.push(num_tx_enc.as_slice());
byteslices.push(total_tx_enc.as_slice());
byteslices.push(last_block_id_enc.as_slice());

// cdcEncode(h.Version),
// cdcEncode(h.ChainID),
// cdcEncode(h.Height),
Expand Down
5 changes: 5 additions & 0 deletions tendermint/src/chain/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ impl Id {
// so in theory this should never panic
str::from_utf8(byte_slice).unwrap()
}

/// Get the chain ID as a raw bytes.
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
}

impl AsRef<str> for Id {
Expand Down
1 change: 0 additions & 1 deletion tendermint/src/lite/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::account::Id;
use crate::block::Height;
use crate::Hash;
#[allow(clippy::all)]
use crate::Time;

/// TrustedState stores the latest state trusted by a lite client,
Expand Down
1 change: 0 additions & 1 deletion tendermint/src/lite/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[allow(clippy::all)]
use crate::lite::{Commit, Error, Header, Validator, ValidatorSet, ValidatorSetLookup, Vote};
use crate::Time;
use std::time::Duration;
Expand Down

0 comments on commit 360a37a

Please sign in to comment.