Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Commit

Permalink
tendermint-rs: Fixes, cleanups, and updates
Browse files Browse the repository at this point in the history
- Fix --no-default-features build (and test in CI)
- Eliminate most usages of (deprecated) `extern crate`
- Use the `derive` cargo feature of `serde` feature instead of `serde_derive`
- Rename `serializers` cargo feature of tendermint-rs to `serde`
- Use 2018 edition module names (`modname.rs` instead of `modname/mod.rs`)
- Update `x25519-dalek` to 0.5.2
- Update `zeroize` to 0.6
  • Loading branch information
tony-iqlusion committed Apr 15, 2019
1 parent 4c4eb06 commit 9c7ceb8
Show file tree
Hide file tree
Showing 23 changed files with 96 additions and 83 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
cargo build --features=yubihsm
cargo build --features=ledgertm
cargo build --features=yubihsm,ledgertm,softsign
cd tendermint-rs && cargo build --no-default-features
- run:
name: build --release
command: |
Expand Down
33 changes: 27 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ signatory-dalek = "0.11"
signatory-secp256k1 = "0.11"
subtle = "2"
subtle-encoding = { version = "0.3", features = ["bech32-preview"] }
tendermint = { version = "0.5.0-alpha1", path = "tendermint-rs" }
tendermint = { version = "0.5", path = "tendermint-rs" }
tiny-bip39 = "0.6"
wait-timeout = "0.2"
yubihsm = { version = "0.22", features = ["setup", "usb"], optional = true }
Expand Down
9 changes: 4 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use std::{
fmt::{self, Display},
io,
};
use tendermint;
use tendermint::amino_types::validate::ValidationError as TmValidationError;
use tendermint::amino_types::validate::ValidationError;

/// Error type
#[derive(Debug)]
Expand Down Expand Up @@ -142,7 +141,7 @@ impl From<signatory::Error> for KmsError {
}

impl From<tendermint::Error> for KmsError {
fn from(other: tendermint::Error) -> Self {
fn from(other: tendermint::error::Error) -> Self {
let kind = match other {
tendermint::Error::Crypto => KmsErrorKind::CryptoError,
tendermint::Error::InvalidKey => KmsErrorKind::InvalidKey,
Expand All @@ -158,8 +157,8 @@ impl From<tendermint::Error> for KmsError {
}
}

impl From<TmValidationError> for KmsError {
fn from(other: TmValidationError) -> Self {
impl From<ValidationError> for KmsError {
fn from(other: ValidationError) -> Self {
err!(KmsErrorKind::InvalidMessageError, other).into()
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::{
};
use subtle::ConstantTimeEq;
use tendermint::{
amino_types::{PingRequest, PingResponse, PubKeyRequest},
amino_types::{PingRequest, PingResponse, PubKeyRequest, PubKeyResponse},
secret_connection::{self, SecretConnection},
};

Expand Down Expand Up @@ -170,8 +170,8 @@ where

/// Get the public key for (the only) public key in the keyring
fn get_public_key(&mut self, _request: &PubKeyRequest) -> Result<Response, KmsError> {
Ok(Response::PublicKey(
KeyRing::default_pubkey()?.to_response(),
))
Ok(Response::PublicKey(PubKeyResponse::from(
*KeyRing::default_pubkey()?,
)))
}
}
18 changes: 7 additions & 11 deletions tendermint-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,33 @@ bytes = "0.4"
chrono = { version = "0.4", features = ["serde"] }
digest = "0.8"
failure = "0.1"
failure_derive = "0.1"
hkdf = { version = "0.7", optional = true }
prost-amino = { version = "0.4.0", optional = true }
prost-amino-derive = { version = "0.4.0", optional = true }
rand_os = { version = "0.1", optional = true }
ring = { version = "0.14", optional = true }
serde = { version = "1", optional = true }
serde_derive = { version = "1", optional = true }
signatory = { version = "0.11.2", optional = true, features = ["ed25519", "ecdsa"] }
serde = { version = "1", optional = true, features = ["derive"] }
signatory = { version = "0.11.2", features = ["ed25519", "ecdsa"] }
signatory-dalek = { version = "0.11", optional = true }
sha2 = { version = "0.8", optional = true, default-features = false }
subtle = { version = "2", optional = true }
subtle-encoding = { version = "0.3", features = ["bech32-preview"] }
tai64 = { version = "1", optional = true, features = ["chrono"] }
x25519-dalek = { version = "0.4.4", optional = true, default-features = false, features = ["u64_backend"] }
zeroize = { version = "0.5.2", optional = true }
x25519-dalek = { version = "0.5", optional = true, default-features = false, features = ["u64_backend"] }
zeroize = { version = "0.6", optional = true }

[features]
default = ["secret-connection", "serializers", "tai64"]
default = ["secret-connection", "serde", "tai64"]
amino-types = ["prost-amino", "prost-amino-derive"]
secret-connection = [
"amino-types",
"byteorder",
"hkdf",
"prost-amino",
"prost-amino-derive",
"rand_os",
"ring",
"signatory",
"signatory-dalek",
"sha2",
"subtle",
"x25519-dalek",
"zeroize"
]
serializers = ["serde", "serde_derive"]
File renamed without changes.
2 changes: 1 addition & 1 deletion tendermint-rs/src/amino_types/block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl ConsensusMessage for BlockId {
}
self.parts_header
.as_ref()
.map_or(Ok(()), |psh| psh.validate_basic())
.map_or(Ok(()), ConsensusMessage::validate_basic)
}
}

Expand Down
25 changes: 14 additions & 11 deletions tendermint-rs/src/amino_types/ed25519.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use signatory::ed25519::{PublicKey, PUBLIC_KEY_SIZE};
use crate::public_keys::PublicKey;
use signatory::ed25519::PUBLIC_KEY_SIZE;

// Note:On the golang side this is generic in the sense that it could everything that implements
// github.com/tendermint/tendermint/crypto.PubKey
Expand All @@ -19,21 +20,23 @@ pub struct PubKeyResponse {
#[amino_name = "tendermint/remotesigner/PubKeyRequest"]
pub struct PubKeyRequest {}

impl Into<PublicKey> for PubKeyResponse {
impl From<PubKeyResponse> for PublicKey {
// This does not check if the underlying pub_key_ed25519 has the right size.
// The caller needs to make sure that this is actually the case.
fn into(self) -> PublicKey {
fn from(response: PubKeyResponse) -> PublicKey {
let mut public_key = [0u8; PUBLIC_KEY_SIZE];
public_key.copy_from_slice(self.pub_key_ed25519.as_ref());
PublicKey(public_key)
public_key.copy_from_slice(response.pub_key_ed25519.as_ref());
PublicKey::Ed25519(signatory::ed25519::PublicKey::new(public_key))
}
}

impl Into<PubKeyResponse> for PublicKey {
fn into(self) -> PubKeyResponse {
let pk = self.0.to_vec();
PubKeyResponse {
pub_key_ed25519: pk,
impl From<PublicKey> for PubKeyResponse {
fn from(public_key: PublicKey) -> PubKeyResponse {
match public_key {
PublicKey::Ed25519(ref pk) => PubKeyResponse {
pub_key_ed25519: pk.as_bytes().to_vec(),
},
PublicKey::Secp256k1(_) => panic!("secp256k1 PubKeyResponse unimplemented"),
}
}
}
Expand Down Expand Up @@ -137,7 +140,7 @@ mod tests {
0xe7, 0xc1, 0xd4, 0x69, 0xc3, 0x44, 0x26, 0xec, 0xef, 0xc0, 0x72, 0xa, 0x52, 0x4d,
0x37, 0x32, 0xef, 0xed,
];
let want = PublicKey(raw_pk);
let want = PublicKey::Ed25519(signatory::ed25519::PublicKey::new(raw_pk));
let pk = PubKeyResponse {
pub_key_ed25519: vec![
0x79, 0xce, 0xd, 0xe0, 0x43, 0x33, 0x4a, 0xec, 0xe0, 0x8b, 0x7b, 0xb5, 0x61, 0xbc,
Expand Down
2 changes: 2 additions & 0 deletions tendermint-rs/src/amino_types/validate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use failure::*;

pub trait ConsensusMessage {
fn validate_basic(&self) -> Result<(), ValidationError>;
}
Expand Down
2 changes: 1 addition & 1 deletion tendermint-rs/src/amino_types/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl ConsensusMessage for Vote {

self.block_id
.as_ref()
.map_or(Ok(()), |bid| bid.validate_basic())
.map_or(Ok(()), ConsensusMessage::validate_basic)

// signature will be missing as the KMS provides it
}
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion tendermint-rs/src/block/height.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::error::Error;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug, Display};

/// Block height for a particular chain (i.e. number of blocks created since
/// the chain began)
#[cfg_attr(feature = "serializers", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Height(pub u64);

Expand Down
6 changes: 3 additions & 3 deletions tendermint-rs/src/block/id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(feature = "serializers")]
#[cfg(feature = "serde")]
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize, Serializer};
use std::{
fmt::{self, Display},
Expand Down Expand Up @@ -37,14 +37,14 @@ impl FromStr for Id {
}
}

#[cfg(feature = "serializers")]
#[cfg(feature = "serde")]
impl Serialize for Id {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.to_string().serialize(serializer)
}
}

#[cfg(feature = "serializers")]
#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Id {
fn deserialize<De: Deserializer<'de>>(deserializer: De) -> Result<Self, De::Error> {
Self::from_str(&String::deserialize(deserializer)?)
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions tendermint-rs/src/chain/id.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Tendermint blockchain identifiers

use crate::error::Error;
#[cfg(feature = "serializers")]
#[cfg(feature = "serde")]
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize, Serializer};
use std::{
cmp::Ordering,
Expand Down Expand Up @@ -104,14 +104,14 @@ impl PartialEq for Id {

impl Eq for Id {}

#[cfg(feature = "serializers")]
#[cfg(feature = "serde")]
impl Serialize for Id {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_str().serialize(serializer)
}
}

#[cfg(feature = "serializers")]
#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Id {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Self::from_str(&String::deserialize(deserializer)?)
Expand Down
5 changes: 4 additions & 1 deletion tendermint-rs/src/chain/state.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//! State of a particular Tendermint network (a.k.a. chain)

use crate::block;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Tendermint consensus state
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ConsensusState {
/// Current block height
pub height: block::Height,
Expand Down
10 changes: 3 additions & 7 deletions tendermint-rs/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! Error types

use failure::*;
use std::io;
#[cfg(feature = "secret-connection")]
use {
chrono, prost, signatory,
std::{self, io},
subtle_encoding,
};
use {chrono, prost, subtle_encoding};

/// Kinds of errors
#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
Expand Down Expand Up @@ -72,14 +70,12 @@ impl From<prost::EncodeError> for Error {
}
}

#[cfg(feature = "secret-connection")]
impl From<subtle_encoding::Error> for Error {
fn from(_: subtle_encoding::Error) -> Error {
Error::Parse
}
}

#[cfg(feature = "secret-connection")]
impl From<signatory::Error> for Error {
fn from(other: signatory::Error) -> Self {
match other.kind() {
Expand Down
Loading

0 comments on commit 9c7ceb8

Please sign in to comment.