Skip to content

Commit

Permalink
add amino type for header Version
Browse files Browse the repository at this point in the history
  • Loading branch information
liamsi committed Sep 25, 2019
1 parent d3ce237 commit 0862130
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
2 changes: 2 additions & 0 deletions tendermint/src/amino_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod signature;
pub mod time;
pub mod validate;
pub mod vote;
pub mod version;

pub use self::{
block_id::{BlockId, CanonicalBlockId, CanonicalPartSetHeader, PartsSetHeader},
Expand All @@ -24,5 +25,6 @@ pub use self::{
signature::{SignableMsg, SignedMsgType},
time::TimeMsg,
validate::ConsensusMessage,
version::ConsensusVersion,
vote::{SignVoteRequest, SignedVoteResponse, AMINO_NAME as VOTE_AMINO_NAME},
};
21 changes: 21 additions & 0 deletions tendermint/src/amino_types/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::block::*;

#[derive(Clone, Message)]
pub struct ConsensusVersion {
/// Block version
#[prost(uint64, tag = "1")]
pub block: u64,

/// App version
#[prost(uint64, tag = "2")]
pub app: u64,
}

impl From<&header::Version> for ConsensusVersion {
fn from(version: &header::Version) -> Self {
ConsensusVersion {
block: version.block,
app: version.app,
}
}
}
38 changes: 34 additions & 4 deletions tendermint/src/block/header.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Block headers
use crate::{account, block, chain, lite, Hash, Time};
use crate::{account, amino_types, block, chain, lite, Hash, Time};
use {
crate::serializers,
serde::{Deserialize, Serialize},
};
use prost::Message;
use crate::merkle::simple_hash_from_byte_slices;

/// Block `Header` values contain metadata about the block and about the
/// consensus, as well as commitments to the data in the current block, the
Expand Down Expand Up @@ -83,31 +85,59 @@ impl lite::Header for Header {
}

fn next_validators_hash(&self) -> Hash {
unimplemented!()
self.next_validators_hash
}

fn hash(&self) -> Hash {
unimplemented!()
let mut version_enc = vec![];
// TODO: if there is an encoding problem this will
// panic (as the golang code would):
// https://github.com/tendermint/tendermint/blob/134fe2896275bb926b49743c1e25493f6b24cc31/types/block.go#L393
// https://github.com/tendermint/tendermint/blob/134fe2896275bb926b49743c1e25493f6b24cc31/types/encoding_helper.go#L9:6
// Instead, handle errors gracefully here.
amino_types::ConsensusVersion::from(&self.version).encode(&mut version_enc).unwrap();

let mut byteslices: Vec<&[u8]> = vec!();
byteslices.push(&mut version_enc);
// cdcEncode(h.Version),
// cdcEncode(h.ChainID),
// cdcEncode(h.Height),
// cdcEncode(h.Time),
// cdcEncode(h.NumTxs),
// cdcEncode(h.TotalTxs),
// cdcEncode(h.LastBlockID),
// cdcEncode(h.LastCommitHash),
// cdcEncode(h.DataHash),
// cdcEncode(h.ValidatorsHash),
// cdcEncode(h.NextValidatorsHash),
// cdcEncode(h.ConsensusHash),
// cdcEncode(h.AppHash),
// cdcEncode(h.LastResultsHash),
// cdcEncode(h.EvidenceHash),
// cdcEncode(h.ProposerAddress),
Hash::Sha256(simple_hash_from_byte_slices(byteslices.as_slice()))
}
}

/// `Version` contains the protocol version for the blockchain and the
/// application.
///
/// <https://github.com/tendermint/tendermint/blob/master/docs/spec/blockchain/blockchain.md#version>
#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Clone, Message)]
pub struct Version {
/// Block version
#[serde(
serialize_with = "serializers::serialize_u64",
deserialize_with = "serializers::parse_u64"
)]
#[prost(uint64, tag = "1")] // TODO: probably better to introduce an amino_types equiv. here (clear separation of concerns) instead of implicitly making this encodable using prost macros
pub block: u64,

/// App version
#[serde(
serialize_with = "serializers::serialize_u64",
deserialize_with = "serializers::parse_u64"
)]
#[prost(uint64, tag = "2")]
pub app: u64,
}
3 changes: 2 additions & 1 deletion tendermint/src/lite.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use self::types::*;
pub mod types;
pub mod verifier;

pub use self::types::*;
pub use self::verifier::*;

0 comments on commit 0862130

Please sign in to comment.