diff --git a/tendermint/src/amino_types/block_id.rs b/tendermint/src/amino_types/block_id.rs index 430649632..88eebbf3c 100644 --- a/tendermint/src/amino_types/block_id.rs +++ b/tendermint/src/amino_types/block_id.rs @@ -1,4 +1,5 @@ use super::validate::{ConsensusMessage, ValidationError, ValidationErrorKind::*}; +use crate::block::parts; use crate::{ block, error::Error, @@ -14,6 +15,12 @@ pub struct BlockId { pub parts_header: Option, } +impl BlockId { + pub fn new(hash: Vec, parts_header: Option) -> Self { + BlockId { hash, parts_header } + } +} + impl block::ParseId for BlockId { fn parse_block_id(&self) -> Result { let hash = Hash::new(hash::Algorithm::Sha256, &self.hash)?; @@ -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. @@ -64,6 +81,18 @@ pub struct PartsSetHeader { pub hash: Vec, } +impl PartsSetHeader { + pub fn new(total: i64, hash: Vec) -> 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 { Hash::new(hash::Algorithm::Sha256, &self.hash) diff --git a/tendermint/src/block/header.rs b/tendermint/src/block/header.rs index d1a9c9c3e..a12db843e 100644 --- a/tendermint/src/block/header.rs +++ b/tendermint/src/block/header.rs @@ -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), diff --git a/tendermint/src/chain/id.rs b/tendermint/src/chain/id.rs index 624f973f3..4d2bbb920 100644 --- a/tendermint/src/chain/id.rs +++ b/tendermint/src/chain/id.rs @@ -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 for Id { diff --git a/tendermint/src/lite/types.rs b/tendermint/src/lite/types.rs index 41a7e1fc5..85cb84f07 100644 --- a/tendermint/src/lite/types.rs +++ b/tendermint/src/lite/types.rs @@ -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, diff --git a/tendermint/src/lite/verifier.rs b/tendermint/src/lite/verifier.rs index 118256d01..25068c820 100644 --- a/tendermint/src/lite/verifier.rs +++ b/tendermint/src/lite/verifier.rs @@ -1,4 +1,3 @@ -#[allow(clippy::all)] use crate::lite::{Commit, Error, Header, Validator, ValidatorSet, ValidatorSetLookup, Vote}; use crate::Time; use std::time::Duration;