Skip to content

Commit

Permalink
Ripped out amino types and replaced them with protobuf types.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-szabo committed Aug 25, 2020
1 parent f5c2f13 commit dfb60c9
Show file tree
Hide file tree
Showing 14 changed files with 509 additions and 523 deletions.
6 changes: 4 additions & 2 deletions tendermint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ ed25519-dalek = { version = "= 1.0.0-pre.4", features = ["serde"] }
futures = "0.3"
k256 = { version = "0.4", optional = true, features = ["ecdsa"] }
once_cell = "1.3"
prost-amino = "0.6"
prost-amino-derive = "0.6"
prost = "0.6"
prost-derive = "0.6"
prost-types = "0.6"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_bytes = "0.11"
Expand All @@ -51,6 +52,7 @@ subtle = "2"
subtle-encoding = { version = "0.5", features = ["bech32-preview"] }
tai64 = { version = "3", features = ["chrono"] }
thiserror = "1"
tendermint-proto = { path = "../proto" }
toml = { version = "0.5" }
zeroize = { version = "1.1", features = ["zeroize_derive"] }
ripemd160 = { version = "0.9", optional = true }
Expand Down
41 changes: 4 additions & 37 deletions tendermint/src/amino_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,18 @@
pub mod block_id;
pub mod ed25519;
pub mod message;
pub mod ping;
pub mod proposal;
pub mod remote_error;
pub mod signature;
pub mod time;
pub mod validate;
pub mod version;
pub mod vote;

pub use self::{
block_id::{BlockId, CanonicalBlockId, CanonicalPartSetHeader, PartsSetHeader},
ed25519::{
PubKeyRequest, PubKeyResponse, AMINO_NAME as PUBKEY_AMINO_NAME,
AMINO_PREFIX as PUBKEY_PREFIX,
},
ping::{PingRequest, PingResponse, AMINO_NAME as PING_AMINO_NAME, AMINO_PREFIX as PING_PREFIX},
proposal::{
SignProposalRequest, SignedProposalResponse, AMINO_NAME as PROPOSAL_AMINO_NAME,
AMINO_PREFIX as PROPOSAL_PREFIX,
},
remote_error::RemoteError,
block_id::{BlockId, CanonicalBlockId, CanonicalPartSetHeader, PartSetHeader},
ed25519::{PubKeyRequest, PubKeyResponse},
proposal::{SignProposalRequest, SignedProposalResponse},
signature::{SignableMsg, SignedMsgType},
time::TimeMsg,
validate::ConsensusMessage,
version::ConsensusVersion,
vote::{
SignVoteRequest, SignedVoteResponse, AMINO_NAME as VOTE_AMINO_NAME,
AMINO_PREFIX as VOTE_PREFIX,
},
vote::{SignVoteRequest, SignedVoteResponse},
};

use sha2::{Digest, Sha256};

/// Compute the Amino prefix for the given registered type name
pub fn compute_prefix(name: &str) -> Vec<u8> {
let mut sh = Sha256::default();
sh.update(name.as_bytes());
let output = sh.finalize();

output
.iter()
.filter(|&x| *x != 0x00)
.skip(3)
.filter(|&x| *x != 0x00)
.cloned()
.take(4)
.collect()
}
92 changes: 50 additions & 42 deletions tendermint/src/amino_types/block_id.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
use super::validate::{ConsensusMessage, Kind::InvalidHashSize, Kind::NegativeTotal};
use super::validate::{ConsensusMessage, Kind::InvalidHashSize};
use crate::block::parts;
use crate::{
block,
error::Error,
hash,
hash::{Hash, SHA256_HASH_SIZE},
};
use prost_amino_derive::Message;

#[derive(Clone, PartialEq, Message)]
// Copied from tendermint_proto::types::BlockId
/// BlockID
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct BlockId {
#[prost_amino(bytes, tag = "1")]
#[prost(bytes, tag = "1")]
pub hash: Vec<u8>,
#[prost_amino(message, tag = "2")]
pub parts_header: Option<PartsSetHeader>,
#[prost(message, optional, tag = "2")]
pub part_set_header: ::std::option::Option<PartSetHeader>,
}

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

impl block::ParseId for BlockId {
fn parse_block_id(&self) -> Result<block::Id, Error> {
let hash = Hash::new(hash::Algorithm::Sha256, &self.hash)?;
let parts_header = self
.parts_header
let part_set_header = self
.part_set_header
.as_ref()
.and_then(PartsSetHeader::parse_parts_header);
Ok(block::Id::new(hash, parts_header))
.and_then(PartSetHeader::parse_part_set_header);
Ok(block::Id::new(hash, part_set_header))
}
}

Expand All @@ -38,7 +42,7 @@ impl From<&block::Id> for BlockId {
let bid_hash = bid.hash.as_bytes();
BlockId::new(
bid_hash.to_vec(),
bid.parts.as_ref().map(PartsSetHeader::from),
bid.parts.as_ref().map(PartSetHeader::from),
)
}
}
Expand All @@ -49,64 +53,67 @@ impl ConsensusMessage for BlockId {
if !self.hash.is_empty() && self.hash.len() != SHA256_HASH_SIZE {
return Err(InvalidHashSize.into());
}
self.parts_header
self.part_set_header
.as_ref()
.map_or(Ok(()), ConsensusMessage::validate_basic)
}
}

#[derive(Clone, PartialEq, Message)]
// Copied from tendermint_proto::types::CanonicalBlockId
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CanonicalBlockId {
#[prost_amino(bytes, tag = "1")]
#[prost(bytes, tag = "1")]
pub hash: Vec<u8>,
#[prost_amino(message, tag = "2")]
pub parts_header: Option<CanonicalPartSetHeader>,
#[prost(message, optional, tag = "2")]
pub part_set_header: ::std::option::Option<CanonicalPartSetHeader>,
}

impl block::ParseId for CanonicalBlockId {
fn parse_block_id(&self) -> Result<block::Id, Error> {
let hash = Hash::new(hash::Algorithm::Sha256, &self.hash)?;
let parts_header = self
.parts_header
let part_set_header = self
.part_set_header
.as_ref()
.and_then(CanonicalPartSetHeader::parse_parts_header);
Ok(block::Id::new(hash, parts_header))
.and_then(CanonicalPartSetHeader::parse_part_set_header);
Ok(block::Id::new(hash, part_set_header))
}
}

#[derive(Clone, PartialEq, Message)]
pub struct PartsSetHeader {
#[prost_amino(int64, tag = "1")]
pub total: i64,
#[prost_amino(bytes, tag = "2")]
// Copied from tendermint_proto::types::PartSetHeader
/// PartsetHeader
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct PartSetHeader {
#[prost(uint32, tag = "1")]
pub total: u32,
#[prost(bytes, tag = "2")]
pub hash: Vec<u8>,
}

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

impl From<&parts::Header> for PartsSetHeader {
impl From<&parts::Header> for PartSetHeader {
fn from(parts: &parts::Header) -> Self {
PartsSetHeader::new(parts.total as i64, parts.hash.as_bytes().to_vec())
PartSetHeader::new(parts.total as i64, parts.hash.as_bytes().to_vec())
}
}

impl PartsSetHeader {
fn parse_parts_header(&self) -> Option<block::parts::Header> {
impl PartSetHeader {
fn parse_part_set_header(&self) -> Option<block::parts::Header> {
Hash::new(hash::Algorithm::Sha256, &self.hash)
.map(|hash| block::parts::Header::new(self.total as u64, hash))
.ok()
}
}

impl ConsensusMessage for PartsSetHeader {
impl ConsensusMessage for PartSetHeader {
fn validate_basic(&self) -> Result<(), Error> {
if self.total < 0 {
return Err(NegativeTotal.into());
}
// Hash can be empty in case of POLBlockID.PartsHeader in Proposal.
if !self.hash.is_empty() && self.hash.len() != SHA256_HASH_SIZE {
return Err(InvalidHashSize.into());
Expand All @@ -115,16 +122,17 @@ impl ConsensusMessage for PartsSetHeader {
}
}

#[derive(Clone, PartialEq, Message)]
// Copied from tendermint_proto::types::CanonicalPartSetHeader
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CanonicalPartSetHeader {
#[prost_amino(bytes, tag = "1")]
#[prost(uint32, tag = "1")]
pub total: u32,
#[prost(bytes, tag = "2")]
pub hash: Vec<u8>,
#[prost_amino(int64, tag = "2")]
pub total: i64,
}

impl CanonicalPartSetHeader {
fn parse_parts_header(&self) -> Option<block::parts::Header> {
fn parse_part_set_header(&self) -> Option<block::parts::Header> {
Hash::new(hash::Algorithm::Sha256, &self.hash)
.map(|hash| block::parts::Header::new(self.total as u64, hash))
.ok()
Expand Down
Loading

0 comments on commit dfb60c9

Please sign in to comment.