From bfcbf835ffe19164682605f69d389b05be684ab1 Mon Sep 17 00:00:00 2001 From: Thane Thomson Date: Tue, 17 Nov 2020 12:47:00 -0500 Subject: [PATCH] proto: Rename DomainType trait to Protobuf (#672) * Rename DomainType trait to Protobuf Signed-off-by: Thane Thomson * Remove domaintype.rs - no longer necessary Signed-off-by: Thane Thomson * Update CHANGELOG Signed-off-by: Thane Thomson * Fix clippy error Signed-off-by: Thane Thomson * Update documentation to reflect DomainType rename Signed-off-by: Thane Thomson * Make error identifier more Protobuf-specific Signed-off-by: Thane Thomson * Rename DomainTypeError to ProtobufError Signed-off-by: Thane Thomson * Clarify docs for Protobuf trait methods Signed-off-by: Thane Thomson --- CHANGELOG.md | 7 + proto/src/domaintype.rs | 139 ------------- proto/src/error.rs | 6 +- proto/src/lib.rs | 184 +++++++++++++++++- proto/tests/unit.rs | 16 +- tendermint/src/account.rs | 4 +- tendermint/src/block.rs | 4 +- tendermint/src/block/header.rs | 6 +- tendermint/src/block/height.rs | 4 +- tendermint/src/block/id.rs | 6 +- tendermint/src/block/parts.rs | 4 +- tendermint/src/block/size.rs | 4 +- tendermint/src/chain/id.rs | 4 +- tendermint/src/consensus/params.rs | 8 +- tendermint/src/evidence.rs | 6 +- tendermint/src/hash.rs | 6 +- tendermint/src/merkle/proof.rs | 6 +- tendermint/src/proposal.rs | 10 +- tendermint/src/proposal/canonical_proposal.rs | 4 +- tendermint/src/proposal/msg_type.rs | 4 +- tendermint/src/proposal/sign_proposal.rs | 12 +- tendermint/src/public_key.rs | 6 +- tendermint/src/public_key/pub_key_request.rs | 6 +- tendermint/src/public_key/pub_key_response.rs | 4 +- tendermint/src/signature.rs | 4 +- tendermint/src/time.rs | 4 +- tendermint/src/validator.rs | 6 +- tendermint/src/vote.rs | 10 +- tendermint/src/vote/canonical_vote.rs | 4 +- tendermint/src/vote/sign_vote.rs | 14 +- 30 files changed, 274 insertions(+), 228 deletions(-) delete mode 100644 proto/src/domaintype.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 5327a0403..ac0cea71c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,18 @@ ## Unreleased +### BREAKING CHANGES: + +- `[tendermint-proto]` The `DomainType` trait has now been renamed to + `Protobuf` to clarify its purpose throughout the codebase. ([#672]) + ### BUG FIXES: - `[tendermint]` (Since v0.17.0-rc2) Fix abci::Data serialization to base64-encoded string. ([#667]) - `[tendermint]` (Since v0.17.0-rc2) Simplify abci::Log serialization ([#667]) [#667]: https://github.com/informalsystems/tendermint-rs/issues/667 +[#672]: https://github.com/informalsystems/tendermint-rs/pull/672 + ## v0.17.0-rc2 diff --git a/proto/src/domaintype.rs b/proto/src/domaintype.rs deleted file mode 100644 index 59c9868e0..000000000 --- a/proto/src/domaintype.rs +++ /dev/null @@ -1,139 +0,0 @@ -//! DomainType trait -//! -//! The DomainType trait allows separation of the data sent on the wire (currently encoded using -//! protobuf) from the structures used in Rust. The structures used to encode/decode from/to the -//! wire are called "Raw" types (they mirror the definitions in the specifications) and the Rust -//! types we use internally are called the "Domain" types. These Domain types can implement -//! additional checks and conversions to consume the incoming data easier for a Rust developer. -//! -//! The benefits include decoding the wire into a struct that is inherently valid as well as hiding -//! the encoding and decoding details from the developer. This latter is important if/when we decide -//! to exchange the underlying Prost library with something else. (Another protobuf implementation -//! or a completely different encoding.) Encoding is not the core product it's a necessary -//! dependency. -//! -//! -//! Decode: bytestream -> Raw -> Domain -//! The `decode` function takes two steps to decode from a bytestream to a DomainType: -//! -//! 1. Decode the bytestream into a Raw type using the Prost library, -//! 2. Transform that Raw type into a Domain type using the TryFrom trait of the DomainType. -//! -//! -//! Encode: Domain -> Raw -> bytestream -//! The `encode` function takes two steps to encode a DomainType into a bytestream: -//! -//! 1. Transform the Domain type into a Raw type using the From trait of the DomainType, -//! 2. Encode the Raw type into a bytestream using the Prost library. -//! -//! -//! Note that in the case of encode, the transformation to Raw type is infallible: -//! Rust structs should always be ready to be encoded to the wire. -//! -//! Note that the Prost library and the TryFrom method have their own set of errors. These are -//! merged into a custom Error type defined in this crate for easier handling. -//! -//! Requirements: -//! * The DomainType trait requires the struct to implement the Clone trait. -//! * Any RawType structure implements the prost::Message trait. (protobuf struct) -//! * The DomainType trait requires that the TryFrom implemented on the structure has an -//! error type that implements Into. (The current implementations with anomaly are -//! fine.) -//! -//! How to implement a DomainType struct: -//! 1. Implement your struct based on your expectations for the developer -//! 2. Add `impl DomainType for MyDomainType {}` blanket implementation of the trait -//! 4. Implement the `TryFrom for MyDomainType` trait -//! 5. Implement the `From for MyRawType` trait - -use crate::{Error, Kind}; -use anomaly::BoxError; -use bytes::{Buf, BufMut}; -use prost::{encoding::encoded_len_varint, Message}; -use std::convert::{TryFrom, TryInto}; - -/// DomainType trait allows protobuf encoding and decoding for domain types -pub trait DomainType + Default> -where - Self: Sized + Clone + TryFrom, - >::Error: Into, -{ - /// Encodes the DomainType into a buffer. - /// - /// This function replaces the Prost::Message encode() function for DomainTypes. - fn encode(&self, buf: &mut B) -> Result<(), Error> { - T::from(self.clone()) - .encode(buf) - .map_err(|e| Kind::EncodeMessage.context(e).into()) - } - - /// Encodes the DomainType with a length-delimiter to a buffer. - /// - /// An error will be returned if the buffer does not have sufficient capacity. - /// - /// This function replaces the Prost::Message encode_length_delimited() function for - /// DomainTypes. - fn encode_length_delimited(&self, buf: &mut B) -> Result<(), Error> { - T::from(self.clone()) - .encode_length_delimited(buf) - .map_err(|e| Kind::EncodeMessage.context(e).into()) - } - - /// Decodes an instance of the message from a buffer and then converts it into DomainType. - /// - /// The entire buffer will be consumed. - /// - /// This function replaces the Prost::Message decode() function for DomainTypes. - fn decode(buf: B) -> Result { - T::decode(buf).map_or_else( - |e| Err(Kind::DecodeMessage.context(e).into()), - |t| Self::try_from(t).map_err(|e| Kind::TryIntoDomainType.context(e).into()), - ) - } - - /// Decodes a length-delimited instance of the message from the buffer. - /// - /// The entire buffer will be consumed. - /// - /// This function replaces the Prost::Message decode_length_delimited() function for - /// DomainTypes. - fn decode_length_delimited(buf: B) -> Result { - T::decode_length_delimited(buf).map_or_else( - |e| Err(Kind::DecodeMessage.context(e).into()), - |t| Self::try_from(t).map_err(|e| Kind::TryIntoDomainType.context(e).into()), - ) - } - - /// Returns the encoded length of the message without a length delimiter. - /// - /// This function replaces the Prost::Message encoded_len() function for DomainTypes. - fn encoded_len(&self) -> usize { - T::from(self.clone()).encoded_len() - } - - /// Encodes the DomainType into a protobuf-encoded Vec - fn encode_vec(&self) -> Result, Error> { - let mut wire = Vec::with_capacity(self.encoded_len()); - self.encode(&mut wire).map(|_| wire) - } - - /// Decodes a protobuf-encoded instance of the message from a Vec and then converts it into - /// DomainType. - fn decode_vec(v: &[u8]) -> Result { - Self::decode(v) - } - - /// Encodes the DomainType with a length-delimiter to a Vec protobuf-encoded message. - fn encode_length_delimited_vec(&self) -> Result, Error> { - let len = self.encoded_len(); - let lenu64 = len.try_into().map_err(|e| Kind::EncodeMessage.context(e))?; - let mut wire = Vec::with_capacity(len + encoded_len_varint(lenu64)); - self.encode_length_delimited(&mut wire).map(|_| wire) - } - - /// Decodes a protobuf-encoded instance of the message with a length-delimiter from a Vec - /// and then converts it into DomainType. - fn decode_length_delimited_vec(v: &[u8]) -> Result { - Self::decode_length_delimited(v) - } -} diff --git a/proto/src/error.rs b/proto/src/error.rs index 4b6da606d..da6ec7970 100644 --- a/proto/src/error.rs +++ b/proto/src/error.rs @@ -1,9 +1,9 @@ -//! This module defines the various errors that be raised during DomainType conversions. +//! This module defines the various errors that be raised during Protobuf conversions. use anomaly::{BoxError, Context}; use thiserror::Error; -/// An error that can be raised by the DomainType conversions. +/// An error that can be raised by the Protobuf conversions. pub type Error = anomaly::Error; /// Various kinds of errors that can be raised. @@ -11,7 +11,7 @@ pub type Error = anomaly::Error; pub enum Kind { /// TryFrom Prost Message failed during decoding #[error("error converting message type into domain type")] - TryIntoDomainType, + TryFromProtobuf, /// encoding prost Message into buffer failed #[error("error encoding message into buffer")] diff --git a/proto/src/lib.rs b/proto/src/lib.rs index df7f4d4c9..685b51bb3 100644 --- a/proto/src/lib.rs +++ b/proto/src/lib.rs @@ -17,10 +17,188 @@ pub mod google { mod tendermint; pub use tendermint::*; -mod domaintype; -pub use domaintype::DomainType; - mod error; +use anomaly::BoxError; +use bytes::{Buf, BufMut}; pub use error::{Error, Kind}; +use prost::encoding::encoded_len_varint; +use prost::Message; +use std::convert::{TryFrom, TryInto}; pub mod serializers; + +/// Allows for easy Google Protocol Buffers encoding and decoding of domain +/// types with validation. +/// +/// ## Examples +/// +/// ```rust +/// use bytes::BufMut; +/// use prost::Message; +/// use std::convert::TryFrom; +/// use tendermint_proto::Protobuf; +/// +/// // This struct would ordinarily be automatically generated by prost. +/// #[derive(Clone, PartialEq, Message)] +/// pub struct MyRawType { +/// #[prost(uint64, tag="1")] +/// pub a: u64, +/// #[prost(string, tag="2")] +/// pub b: String, +/// } +/// +/// #[derive(Clone)] +/// pub struct MyDomainType { +/// a: u64, +/// b: String, +/// } +/// +/// impl MyDomainType { +/// /// Trivial constructor with basic validation logic. +/// pub fn new(a: u64, b: String) -> Result { +/// if a < 1 { +/// return Err("a must be greater than 0".to_owned()); +/// } +/// Ok(Self { a, b }) +/// } +/// } +/// +/// impl TryFrom for MyDomainType { +/// type Error = String; +/// +/// fn try_from(value: MyRawType) -> Result { +/// Self::new(value.a, value.b) +/// } +/// } +/// +/// impl From for MyRawType { +/// fn from(value: MyDomainType) -> Self { +/// Self { a: value.a, b: value.b } +/// } +/// } +/// +/// impl Protobuf for MyDomainType {} +/// +/// +/// // Simulate an incoming valid raw message +/// let valid_raw = MyRawType { a: 1, b: "Hello!".to_owned() }; +/// let mut valid_raw_bytes: Vec = Vec::new(); +/// valid_raw.encode(&mut valid_raw_bytes).unwrap(); +/// assert!(!valid_raw_bytes.is_empty()); +/// +/// // Try to decode the simulated incoming message +/// let valid_domain = MyDomainType::decode(valid_raw_bytes.clone().as_ref()).unwrap(); +/// assert_eq!(1, valid_domain.a); +/// assert_eq!("Hello!".to_owned(), valid_domain.b); +/// +/// // Encode it to compare the serialized form to what we received +/// let mut valid_domain_bytes: Vec = Vec::new(); +/// valid_domain.encode(&mut valid_domain_bytes).unwrap(); +/// assert_eq!(valid_raw_bytes, valid_domain_bytes); +/// +/// // Simulate an incoming invalid raw message +/// let invalid_raw = MyRawType { a: 0, b: "Hello!".to_owned() }; +/// let mut invalid_raw_bytes: Vec = Vec::new(); +/// invalid_raw.encode(&mut invalid_raw_bytes).unwrap(); +/// +/// // We expect a validation error here +/// assert!(MyDomainType::decode(invalid_raw_bytes.as_ref()).is_err()); +/// ``` +pub trait Protobuf + Default> +where + Self: Sized + Clone + TryFrom, + >::Error: Into, +{ + /// Encode into a buffer in Protobuf format. + /// + /// Uses [`prost::Message::encode`] after converting into its counterpart + /// Protobuf data structure. + /// + /// [`prost::Message::encode`]: https://docs.rs/prost/*/prost/trait.Message.html#method.encode + fn encode(&self, buf: &mut B) -> Result<(), Error> { + T::from(self.clone()) + .encode(buf) + .map_err(|e| Kind::EncodeMessage.context(e).into()) + } + + /// Encode with a length-delimiter to a buffer in Protobuf format. + /// + /// An error will be returned if the buffer does not have sufficient capacity. + /// + /// Uses [`prost::Message::encode_length_delimited`] after converting into + /// its counterpart Protobuf data structure. + /// + /// [`prost::Message::encode_length_delimited`]: https://docs.rs/prost/*/prost/trait.Message.html#method.encode_length_delimited + fn encode_length_delimited(&self, buf: &mut B) -> Result<(), Error> { + T::from(self.clone()) + .encode_length_delimited(buf) + .map_err(|e| Kind::EncodeMessage.context(e).into()) + } + + /// Constructor that attempts to decode an instance from a buffer. + /// + /// The entire buffer will be consumed. + /// + /// Similar to [`prost::Message::decode`] but with additional validation + /// prior to constructing the destination type. + /// + /// [`prost::Message::decode`]: https://docs.rs/prost/*/prost/trait.Message.html#method.decode + fn decode(buf: B) -> Result { + T::decode(buf).map_or_else( + |e| Err(Kind::DecodeMessage.context(e).into()), + |t| Self::try_from(t).map_err(|e| Kind::TryFromProtobuf.context(e).into()), + ) + } + + /// Constructor that attempts to decode a length-delimited instance from + /// the buffer. + /// + /// The entire buffer will be consumed. + /// + /// Similar to [`prost::Message::decode_length_delimited`] but with + /// additional validation prior to constructing the destination type. + /// + /// [`prost::Message::decode_length_delimited`]: https://docs.rs/prost/*/prost/trait.Message.html#method.decode_length_delimited + fn decode_length_delimited(buf: B) -> Result { + T::decode_length_delimited(buf).map_or_else( + |e| Err(Kind::DecodeMessage.context(e).into()), + |t| Self::try_from(t).map_err(|e| Kind::TryFromProtobuf.context(e).into()), + ) + } + + /// Returns the encoded length of the message without a length delimiter. + /// + /// Uses [`prost::Message::encoded_len`] after converting to its + /// counterpart Protobuf data structure. + /// + /// [`prost::Message::encoded_len`]: https://docs.rs/prost/*/prost/trait.Message.html#method.encoded_len + fn encoded_len(&self) -> usize { + T::from(self.clone()).encoded_len() + } + + /// Encodes into a Protobuf-encoded `Vec`. + fn encode_vec(&self) -> Result, Error> { + let mut wire = Vec::with_capacity(self.encoded_len()); + self.encode(&mut wire).map(|_| wire) + } + + /// Constructor that attempts to decode a Protobuf-encoded instance from a + /// `Vec` (or equivalent). + fn decode_vec(v: &[u8]) -> Result { + Self::decode(v) + } + + /// Encode with a length-delimiter to a `Vec` Protobuf-encoded message. + fn encode_length_delimited_vec(&self) -> Result, Error> { + let len = self.encoded_len(); + let lenu64 = len.try_into().map_err(|e| Kind::EncodeMessage.context(e))?; + let mut wire = Vec::with_capacity(len + encoded_len_varint(lenu64)); + self.encode_length_delimited(&mut wire).map(|_| wire) + } + + /// Constructor that attempts to decode a Protobuf-encoded instance with a + /// length-delimiter from a `Vec` or equivalent. + fn decode_length_delimited_vec(v: &[u8]) -> Result { + Self::decode_length_delimited(v) + } +} diff --git a/proto/tests/unit.rs b/proto/tests/unit.rs index 95c878dc3..8a6fa7b9d 100644 --- a/proto/tests/unit.rs +++ b/proto/tests/unit.rs @@ -1,18 +1,18 @@ use std::convert::TryFrom; use tendermint_proto::types::BlockId as RawBlockId; use tendermint_proto::types::PartSetHeader as RawPartSetHeader; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; -impl DomainType for BlockId {} +impl Protobuf for BlockId {} -// Example implementation of a protobuf struct using DomainType. +// Example implementation of a protobuf struct using Protobuf. #[derive(Clone, Debug)] pub struct BlockId { hash: String, part_set_header_exists: bool, } -// DomainTypes MUST have the TryFrom trait to convert from RawTypes. +// Domain types MUST have the TryFrom trait to convert from Protobuf messages. impl TryFrom for BlockId { type Error = &'static str; @@ -25,7 +25,7 @@ impl TryFrom for BlockId { } } -// DomainTypes MUST be able to convert to RawTypes without errors using the From trait. +// Domain types MUST be able to convert to Protobuf messages without errors using the From trait. impl From for RawBlockId { fn from(value: BlockId) -> Self { RawBlockId { @@ -49,7 +49,7 @@ impl PartialEq for BlockId { } #[test] -pub fn domaintype_struct_example() { +pub fn protobuf_struct_example() { let my_domain_type = BlockId { hash: "Hello world!".to_string(), part_set_header_exists: false, @@ -68,7 +68,7 @@ pub fn domaintype_struct_example() { } #[test] -pub fn domaintype_struct_length_delimited_example() { +pub fn protobuf_struct_length_delimited_example() { let my_domain_type = BlockId { hash: "Hello world!".to_string(), part_set_header_exists: false, @@ -87,7 +87,7 @@ pub fn domaintype_struct_length_delimited_example() { } #[test] -pub fn domaintype_struct_conveniences_example() { +pub fn protobuf_struct_conveniences_example() { let my_domain_type = BlockId { hash: "Hello world!".to_string(), part_set_header_exists: false, diff --git a/tendermint/src/account.rs b/tendermint/src/account.rs index 36694df51..a9ee5c2d0 100644 --- a/tendermint/src/account.rs +++ b/tendermint/src/account.rs @@ -20,7 +20,7 @@ use crate::public_key::Secp256k1; #[cfg(feature = "secp256k1")] use ripemd160::Ripemd160; use std::convert::TryFrom; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Size of an account ID in bytes pub const LENGTH: usize = 20; @@ -29,7 +29,7 @@ pub const LENGTH: usize = 20; #[derive(Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)] pub struct Id([u8; LENGTH]); // JSON custom serialization for priv_validator_key.json -impl DomainType> for Id {} +impl Protobuf> for Id {} impl TryFrom> for Id { type Error = Error; diff --git a/tendermint/src/block.rs b/tendermint/src/block.rs index 5e68b0bf4..e3e49485a 100644 --- a/tendermint/src/block.rs +++ b/tendermint/src/block.rs @@ -25,7 +25,7 @@ use crate::{abci::transaction, evidence, Error, Kind}; use serde::{Deserialize, Serialize}; use std::convert::{TryFrom, TryInto}; use tendermint_proto::types::Block as RawBlock; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Blocks consist of a header, transactions, votes (the commit), and a list of /// evidence of malfeasance (i.e. signing conflicting votes). @@ -49,7 +49,7 @@ pub struct Block { pub last_commit: Option, } -impl DomainType for Block {} +impl Protobuf for Block {} impl TryFrom for Block { type Error = Error; diff --git a/tendermint/src/block/header.rs b/tendermint/src/block/header.rs index 9bc4449bd..26fffeeef 100644 --- a/tendermint/src/block/header.rs +++ b/tendermint/src/block/header.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; use std::convert::{TryFrom, TryInto}; use tendermint_proto::types::Header as RawHeader; use tendermint_proto::version::Consensus as RawConsensusVersion; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Block `Header` values contain metadata about the block and about the /// consensus, as well as commitments to the data in the current block, the @@ -59,7 +59,7 @@ pub struct Header { pub proposer_address: account::Id, } -impl DomainType for Header {} +impl Protobuf for Header {} impl TryFrom for Header { type Error = Error; @@ -209,7 +209,7 @@ pub struct Version { pub app: u64, } -impl DomainType for Version {} +impl Protobuf for Version {} impl TryFrom for Version { type Error = anomaly::BoxError; diff --git a/tendermint/src/block/height.rs b/tendermint/src/block/height.rs index f943c1e26..f0554c3e8 100644 --- a/tendermint/src/block/height.rs +++ b/tendermint/src/block/height.rs @@ -6,7 +6,7 @@ use std::{ fmt::{self, Debug, Display}, str::FromStr, }; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Block height for a particular chain (i.e. number of blocks created since /// the chain began) @@ -15,7 +15,7 @@ use tendermint_proto::DomainType; #[derive(Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)] pub struct Height(u64); -impl DomainType for Height {} +impl Protobuf for Height {} impl TryFrom for Height { type Error = Error; diff --git a/tendermint/src/block/id.rs b/tendermint/src/block/id.rs index 472c1ad25..b1b27fe0b 100644 --- a/tendermint/src/block/id.rs +++ b/tendermint/src/block/id.rs @@ -13,7 +13,7 @@ use tendermint_proto::types::{ BlockId as RawBlockId, CanonicalBlockId as RawCanonicalBlockId, PartSetHeader as RawPartSetHeader, }; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Length of a block ID prefix displayed for debugging purposes pub const PREFIX_LENGTH: usize = 10; @@ -53,11 +53,11 @@ pub struct Id { /// /// PartSetHeader in protobuf is defined as never nil using the gogoproto /// annotations. This does not translate to Rust, but we can indicate this - /// in the DomainType. + /// in the domain type. pub part_set_header: PartSetHeader, } -impl DomainType for Id {} +impl Protobuf for Id {} impl TryFrom for Id { type Error = Error; diff --git a/tendermint/src/block/parts.rs b/tendermint/src/block/parts.rs index 83f6ed0c4..f5ed01a53 100644 --- a/tendermint/src/block/parts.rs +++ b/tendermint/src/block/parts.rs @@ -8,7 +8,7 @@ use std::convert::TryFrom; use tendermint_proto::types::{ CanonicalPartSetHeader as RawCanonicalPartSetHeader, PartSetHeader as RawPartSetHeader, }; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Block parts header #[derive(Clone, Copy, Debug, Default, Hash, Eq, PartialEq, PartialOrd, Ord)] @@ -21,7 +21,7 @@ pub struct Header { pub hash: Hash, } -impl DomainType for Header {} +impl Protobuf for Header {} impl TryFrom for Header { type Error = Error; diff --git a/tendermint/src/block/size.rs b/tendermint/src/block/size.rs index e4bd02182..f816e02e2 100644 --- a/tendermint/src/block/size.rs +++ b/tendermint/src/block/size.rs @@ -2,7 +2,7 @@ use crate::{Error, Kind}; use std::convert::{TryFrom, TryInto}; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; use { crate::serializers, serde::{Deserialize, Serialize}, @@ -21,7 +21,7 @@ pub struct Size { pub max_gas: i64, } -impl DomainType for Size {} +impl Protobuf for Size {} impl TryFrom for Size { type Error = Error; diff --git a/tendermint/src/chain/id.rs b/tendermint/src/chain/id.rs index badd616c4..d770fe089 100644 --- a/tendermint/src/chain/id.rs +++ b/tendermint/src/chain/id.rs @@ -9,7 +9,7 @@ use std::{ hash::{Hash, Hasher}, str::{self, FromStr}, }; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Maximum length of a `chain::Id` name. Matches `MaxChainIDLen` from: /// @@ -20,7 +20,7 @@ pub const MAX_LENGTH: usize = 50; #[derive(Clone)] pub struct Id(String); -impl DomainType for Id {} +impl Protobuf for Id {} impl TryFrom for Id { type Error = Error; diff --git a/tendermint/src/consensus/params.rs b/tendermint/src/consensus/params.rs index c98535587..47f3e5bd7 100644 --- a/tendermint/src/consensus/params.rs +++ b/tendermint/src/consensus/params.rs @@ -7,7 +7,7 @@ use std::convert::{TryFrom, TryInto}; use tendermint_proto::abci::ConsensusParams as RawParams; use tendermint_proto::types::ValidatorParams as RawValidatorParams; use tendermint_proto::types::VersionParams as RawVersionParams; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Tendermint consensus parameters #[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)] @@ -26,7 +26,7 @@ pub struct Params { pub version: Option, } -impl DomainType for Params {} +impl Protobuf for Params {} impl TryFrom for Params { type Error = Error; @@ -66,7 +66,7 @@ pub struct ValidatorParams { pub pub_key_types: Vec, } -impl DomainType for ValidatorParams {} +impl Protobuf for ValidatorParams {} impl TryFrom for ValidatorParams { type Error = Error; @@ -111,7 +111,7 @@ pub struct VersionParams { app_version: u64, } -impl DomainType for VersionParams {} +impl Protobuf for VersionParams {} impl TryFrom for VersionParams { type Error = Error; diff --git a/tendermint/src/evidence.rs b/tendermint/src/evidence.rs index 8c88820b3..8ff0da2c7 100644 --- a/tendermint/src/evidence.rs +++ b/tendermint/src/evidence.rs @@ -11,7 +11,7 @@ use tendermint_proto::types::DuplicateVoteEvidence as RawDuplicateVoteEvidence; use tendermint_proto::types::Evidence as RawEvidence; use tendermint_proto::types::EvidenceData as RawEvidenceData; use tendermint_proto::types::EvidenceParams as RawEvidenceParams; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Evidence of malfeasance by validators (i.e. signing conflicting votes). /// encoded using an Amino prefix. There is currently only a single type of @@ -196,7 +196,7 @@ pub struct Params { pub max_num: i64, } -impl DomainType for Params {} +impl Protobuf for Params {} impl TryFrom for Params { type Error = Error; @@ -240,7 +240,7 @@ impl From for std::time::Duration { } } -impl DomainType for Duration {} +impl Protobuf for Duration {} impl TryFrom for Duration { type Error = Error; diff --git a/tendermint/src/hash.rs b/tendermint/src/hash.rs index 6d29671c0..cd6aab49f 100644 --- a/tendermint/src/hash.rs +++ b/tendermint/src/hash.rs @@ -9,7 +9,7 @@ use std::{ str::FromStr, }; use subtle_encoding::{Encoding, Hex}; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Output size for the SHA-256 hash function pub const SHA256_HASH_SIZE: usize = 32; @@ -30,7 +30,7 @@ pub enum Hash { None, } -impl DomainType> for Hash {} +impl Protobuf> for Hash {} /// Default conversion from Vec is SHA256 Hash or None impl TryFrom> for Hash { @@ -167,7 +167,7 @@ impl Serialize for Hash { #[derive(Clone)] pub struct AppHash(Vec); -impl DomainType> for AppHash {} +impl Protobuf> for AppHash {} impl TryFrom> for AppHash { type Error = Error; diff --git a/tendermint/src/merkle/proof.rs b/tendermint/src/merkle/proof.rs index a770cb540..9ae747f66 100644 --- a/tendermint/src/merkle/proof.rs +++ b/tendermint/src/merkle/proof.rs @@ -4,7 +4,7 @@ use std::convert::TryFrom; use tendermint_proto::crypto::ProofOp as RawProofOp; use tendermint_proto::crypto::ProofOps as RawProofOps; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; use crate::serializers; use crate::Error; @@ -34,7 +34,7 @@ pub struct ProofOp { pub data: Vec, } -impl DomainType for ProofOp {} +impl Protobuf for ProofOp {} impl TryFrom for ProofOp { type Error = Error; @@ -58,7 +58,7 @@ impl From for RawProofOp { } } -impl DomainType for Proof {} +impl Protobuf for Proof {} impl TryFrom for Proof { type Error = Error; diff --git a/tendermint/src/proposal.rs b/tendermint/src/proposal.rs index aea7c6e47..7c3a84685 100644 --- a/tendermint/src/proposal.rs +++ b/tendermint/src/proposal.rs @@ -17,7 +17,7 @@ use crate::{Error, Kind}; use bytes::BufMut; use std::convert::{TryFrom, TryInto}; use tendermint_proto::types::Proposal as RawProposal; -use tendermint_proto::{DomainType, Error as DomainTypeError}; +use tendermint_proto::{Error as ProtobufError, Protobuf}; /// Proposal #[derive(Clone, PartialEq, Debug)] @@ -38,7 +38,7 @@ pub struct Proposal { pub signature: Signature, } -impl DomainType for Proposal {} +impl Protobuf for Proposal {} impl TryFrom for Proposal { type Error = Error; @@ -83,7 +83,7 @@ impl Proposal { &self, chain_id: ChainId, sign_bytes: &mut B, - ) -> Result + ) -> Result where B: BufMut, { @@ -92,7 +92,7 @@ impl Proposal { } /// Create signable vector from Proposal. - pub fn to_signable_vec(&self, chain_id: ChainId) -> Result, DomainTypeError> { + pub fn to_signable_vec(&self, chain_id: ChainId) -> Result, ProtobufError> { CanonicalProposal::new(self.clone(), chain_id).encode_length_delimited_vec() } @@ -123,7 +123,7 @@ mod tests { use crate::{proposal::Type, Proposal, Signature}; use chrono::{DateTime, Utc}; use std::str::FromStr; - use tendermint_proto::DomainType; + use tendermint_proto::Protobuf; #[test] fn test_serialization() { diff --git a/tendermint/src/proposal/canonical_proposal.rs b/tendermint/src/proposal/canonical_proposal.rs index b46b8b2ee..04ad1074d 100644 --- a/tendermint/src/proposal/canonical_proposal.rs +++ b/tendermint/src/proposal/canonical_proposal.rs @@ -7,7 +7,7 @@ use crate::Time; use crate::{Error, Kind}; use std::convert::{TryFrom, TryInto}; use tendermint_proto::types::CanonicalProposal as RawCanonicalProposal; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// CanonicalProposal for signing #[derive(Clone, PartialEq)] @@ -28,7 +28,7 @@ pub struct CanonicalProposal { pub chain_id: ChainId, } -impl DomainType for CanonicalProposal {} +impl Protobuf for CanonicalProposal {} impl TryFrom for CanonicalProposal { type Error = Error; diff --git a/tendermint/src/proposal/msg_type.rs b/tendermint/src/proposal/msg_type.rs index 4275c480e..32433dda4 100644 --- a/tendermint/src/proposal/msg_type.rs +++ b/tendermint/src/proposal/msg_type.rs @@ -2,7 +2,7 @@ use crate::{Error, Kind}; use serde::de::Error as _; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::convert::TryFrom; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Types of proposals #[repr(u8)] @@ -12,7 +12,7 @@ pub enum Type { Proposal = 32, } -impl DomainType for Type {} +impl Protobuf for Type {} impl TryFrom for Type { type Error = Error; diff --git a/tendermint/src/proposal/sign_proposal.rs b/tendermint/src/proposal/sign_proposal.rs index 89eb055d4..ccb78398e 100644 --- a/tendermint/src/proposal/sign_proposal.rs +++ b/tendermint/src/proposal/sign_proposal.rs @@ -6,8 +6,8 @@ use std::convert::{TryFrom, TryInto}; use tendermint_proto::privval::RemoteSignerError; use tendermint_proto::privval::SignProposalRequest as RawSignProposalRequest; use tendermint_proto::privval::SignedProposalResponse as RawSignedProposalResponse; -use tendermint_proto::DomainType; -use tendermint_proto::Error as DomainTypeError; +use tendermint_proto::Error as ProtobufError; +use tendermint_proto::Protobuf; /// SignProposalRequest is a request to sign a proposal #[derive(Clone, PartialEq, Debug)] @@ -18,8 +18,8 @@ pub struct SignProposalRequest { pub chain_id: ChainId, } -impl DomainType for SignProposalRequest {} -impl DomainType for SignedProposalResponse {} +impl Protobuf for SignProposalRequest {} +impl Protobuf for SignedProposalResponse {} impl TryFrom for SignProposalRequest { type Error = Error; @@ -46,7 +46,7 @@ impl From for RawSignProposalRequest { impl SignProposalRequest { /// Create signable bytes from Proposal. - pub fn to_signable_bytes(&self, sign_bytes: &mut B) -> Result + pub fn to_signable_bytes(&self, sign_bytes: &mut B) -> Result where B: BufMut, { @@ -55,7 +55,7 @@ impl SignProposalRequest { } /// Create signable vector from Proposal. - pub fn to_signable_vec(&self) -> Result, DomainTypeError> { + pub fn to_signable_vec(&self) -> Result, ProtobufError> { self.proposal.to_signable_vec(self.chain_id.clone()) } } diff --git a/tendermint/src/public_key.rs b/tendermint/src/public_key.rs index 40754cf0c..654879c24 100644 --- a/tendermint/src/public_key.rs +++ b/tendermint/src/public_key.rs @@ -21,7 +21,7 @@ use std::{cmp::Ordering, fmt, ops::Deref, str::FromStr}; use subtle_encoding::{base64, bech32, hex}; use tendermint_proto::crypto::public_key::Sum; use tendermint_proto::crypto::PublicKey as RawPublicKey; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; // Note:On the golang side this is generic in the sense that it could everything that implements // github.com/tendermint/tendermint/crypto.PubKey @@ -57,7 +57,7 @@ pub enum PublicKey { Secp256k1(Secp256k1), } -impl DomainType for PublicKey {} +impl Protobuf for PublicKey {} impl TryFrom for PublicKey { type Error = Error; @@ -353,7 +353,7 @@ mod tests { use super::{PublicKey, TendermintKey}; use crate::public_key::PubKeyResponse; use subtle_encoding::hex; - use tendermint_proto::DomainType; + use tendermint_proto::Protobuf; const EXAMPLE_CONSENSUS_KEY: &str = "4A25C6640A1F72B9C975338294EF51B6D1C33158BB6ECBA69FBC3FB5A33C9DCE"; diff --git a/tendermint/src/public_key/pub_key_request.rs b/tendermint/src/public_key/pub_key_request.rs index ccc7e759c..876cf9f86 100644 --- a/tendermint/src/public_key/pub_key_request.rs +++ b/tendermint/src/public_key/pub_key_request.rs @@ -2,7 +2,7 @@ use crate::chain::Id as ChainId; use crate::Error; use std::convert::TryFrom; use tendermint_proto::privval::PubKeyRequest as RawPubKeyRequest; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// PubKeyRequest requests the consensus public key from the remote signer. #[derive(Clone, PartialEq, Debug)] @@ -11,7 +11,7 @@ pub struct PubKeyRequest { pub chain_id: ChainId, } -impl DomainType for PubKeyRequest {} +impl Protobuf for PubKeyRequest {} impl TryFrom for PubKeyRequest { type Error = Error; @@ -36,7 +36,7 @@ mod tests { use super::PubKeyRequest; use crate::chain::Id as ChainId; use std::str::FromStr; - use tendermint_proto::DomainType; + use tendermint_proto::Protobuf; #[test] fn test_empty_pubkey_msg() { diff --git a/tendermint/src/public_key/pub_key_response.rs b/tendermint/src/public_key/pub_key_response.rs index 4ee24c393..b58292df1 100644 --- a/tendermint/src/public_key/pub_key_response.rs +++ b/tendermint/src/public_key/pub_key_response.rs @@ -1,7 +1,7 @@ use crate::{Error, PublicKey}; use std::convert::{TryFrom, TryInto}; use tendermint_proto::privval::{PubKeyResponse as RawPubKeyResponse, RemoteSignerError}; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// PubKeyResponse #[derive(Clone, PartialEq, Debug)] @@ -14,7 +14,7 @@ pub struct PubKeyResponse { pub error: Option, } -impl DomainType for PubKeyResponse {} +impl Protobuf for PubKeyResponse {} impl TryFrom for PubKeyResponse { type Error = Error; diff --git a/tendermint/src/signature.rs b/tendermint/src/signature.rs index 6aebd0633..99be6e01d 100644 --- a/tendermint/src/signature.rs +++ b/tendermint/src/signature.rs @@ -8,7 +8,7 @@ pub use k256::ecdsa::Signature as Secp256k1; use crate::{Error, Kind}; use std::convert::TryFrom; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Signatures #[derive(Copy, Clone, Debug, PartialEq)] @@ -21,7 +21,7 @@ pub enum Signature { * outside the scope of this enum. */ } -impl DomainType> for Signature {} +impl Protobuf> for Signature {} impl TryFrom> for Signature { type Error = Error; diff --git a/tendermint/src/time.rs b/tendermint/src/time.rs index 6a96dba87..d1d33df7a 100644 --- a/tendermint/src/time.rs +++ b/tendermint/src/time.rs @@ -11,7 +11,7 @@ use std::ops::{Add, Sub}; use std::str::FromStr; use std::time::{Duration, SystemTime, UNIX_EPOCH}; use tendermint_proto::google::protobuf::Timestamp; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Tendermint timestamps /// @@ -19,7 +19,7 @@ use tendermint_proto::DomainType; #[serde(try_from = "Timestamp", into = "Timestamp")] pub struct Time(DateTime); -impl DomainType for Time {} +impl Protobuf for Time {} impl TryFrom for Time { type Error = anomaly::BoxError; diff --git a/tendermint/src/validator.rs b/tendermint/src/validator.rs index 243a4c06c..96f276677 100644 --- a/tendermint/src/validator.rs +++ b/tendermint/src/validator.rs @@ -9,7 +9,7 @@ use std::convert::{TryFrom, TryInto}; use tendermint_proto::types::SimpleValidator as RawSimpleValidator; use tendermint_proto::types::Validator as RawValidator; use tendermint_proto::types::ValidatorSet as RawValidatorSet; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// Validator set contains a vector of validators #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] @@ -21,7 +21,7 @@ pub struct Set { total_voting_power: vote::Power, } -impl DomainType for Set {} +impl Protobuf for Set {} impl TryFrom for Set { type Error = Error; @@ -211,7 +211,7 @@ pub struct SimpleValidator { pub voting_power: vote::Power, } -impl DomainType for SimpleValidator {} +impl Protobuf for SimpleValidator {} impl TryFrom for SimpleValidator { type Error = Error; diff --git a/tendermint/src/vote.rs b/tendermint/src/vote.rs index 0b22b5339..0ea2aedba 100644 --- a/tendermint/src/vote.rs +++ b/tendermint/src/vote.rs @@ -20,7 +20,7 @@ use ed25519::SIGNATURE_LENGTH as ed25519SignatureLength; use serde::{Deserialize, Serialize}; use std::convert::{TryFrom, TryInto}; use tendermint_proto::types::Vote as RawVote; -use tendermint_proto::{DomainType, Error as DomainTypeError}; +use tendermint_proto::{Error as ProtobufError, Protobuf}; use crate::signature::Signature::Ed25519; @@ -56,7 +56,7 @@ pub struct Vote { pub signature: Signature, } -impl DomainType for Vote {} +impl Protobuf for Vote {} impl TryFrom for Vote { type Error = Error; @@ -125,7 +125,7 @@ impl Vote { &self, chain_id: ChainId, sign_bytes: &mut B, - ) -> Result + ) -> Result where B: BufMut, { @@ -134,7 +134,7 @@ impl Vote { } /// Create signable vector from Vote. - pub fn to_signable_vec(&self, chain_id: ChainId) -> Result, DomainTypeError> { + pub fn to_signable_vec(&self, chain_id: ChainId) -> Result, ProtobufError> { CanonicalVote::new(self.clone(), chain_id).encode_length_delimited_vec() } @@ -220,7 +220,7 @@ pub enum Type { Precommit = 2, } -impl DomainType for Type {} +impl Protobuf for Type {} impl TryFrom for Type { type Error = Error; diff --git a/tendermint/src/vote/canonical_vote.rs b/tendermint/src/vote/canonical_vote.rs index da055f21e..170af775b 100644 --- a/tendermint/src/vote/canonical_vote.rs +++ b/tendermint/src/vote/canonical_vote.rs @@ -4,7 +4,7 @@ use crate::{Error, Kind::*}; use serde::{Deserialize, Serialize}; use std::convert::{TryFrom, TryInto}; use tendermint_proto::types::CanonicalVote as RawCanonicalVote; -use tendermint_proto::DomainType; +use tendermint_proto::Protobuf; /// CanonicalVote is used for protobuf encoding a Vote #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] @@ -30,7 +30,7 @@ pub struct CanonicalVote { pub chain_id: ChainId, } -impl DomainType for CanonicalVote {} +impl Protobuf for CanonicalVote {} impl TryFrom for CanonicalVote { type Error = Error; diff --git a/tendermint/src/vote/sign_vote.rs b/tendermint/src/vote/sign_vote.rs index afd7d5f60..89742e07d 100644 --- a/tendermint/src/vote/sign_vote.rs +++ b/tendermint/src/vote/sign_vote.rs @@ -5,8 +5,8 @@ use bytes::BufMut; use std::convert::TryFrom; use tendermint_proto::privval::SignedVoteResponse as RawSignedVoteResponse; use tendermint_proto::privval::{RemoteSignerError, SignVoteRequest as RawSignVoteRequest}; -use tendermint_proto::DomainType; -use tendermint_proto::Error as DomainTypeError; +use tendermint_proto::Error as ProtobufError; +use tendermint_proto::Protobuf; /// SignVoteRequest is a request to sign a vote #[derive(Clone, PartialEq, Debug)] @@ -17,7 +17,7 @@ pub struct SignVoteRequest { pub chain_id: chain::Id, } -impl DomainType for SignVoteRequest {} +impl Protobuf for SignVoteRequest {} impl TryFrom for SignVoteRequest { type Error = Error; @@ -44,7 +44,7 @@ impl From for RawSignVoteRequest { impl SignVoteRequest { /// Create signable bytes from Vote. - pub fn to_signable_bytes(&self, sign_bytes: &mut B) -> Result + pub fn to_signable_bytes(&self, sign_bytes: &mut B) -> Result where B: BufMut, { @@ -53,7 +53,7 @@ impl SignVoteRequest { } /// Create signable vector from Vote. - pub fn to_signable_vec(&self) -> Result, DomainTypeError> { + pub fn to_signable_vec(&self) -> Result, ProtobufError> { self.vote.to_signable_vec(self.chain_id.clone()) } } @@ -67,7 +67,7 @@ pub struct SignedVoteResponse { pub error: Option, } -impl DomainType for SignedVoteResponse {} +impl Protobuf for SignedVoteResponse {} impl TryFrom for SignedVoteResponse { type Error = Error; @@ -106,7 +106,7 @@ mod tests { use chrono::{DateTime, Utc}; use std::convert::TryFrom; use std::str::FromStr; - use tendermint_proto::DomainType; + use tendermint_proto::Protobuf; #[test] fn test_vote_serialization() {