Skip to content

Commit

Permalink
Address cast possible wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
xla committed Jan 20, 2020
1 parent 99e323c commit 7750986
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 21 deletions.
8 changes: 7 additions & 1 deletion tendermint/src/amino_types/block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
hash,
hash::{Hash, SHA256_HASH_SIZE},
};
use std::convert::TryFrom;

#[derive(Clone, PartialEq, Message)]
pub struct BlockId {
Expand Down Expand Up @@ -93,9 +94,14 @@ impl PartsSetHeader {
}
}

// TODO(xla): Rework into TryFrom.
#[allow(clippy::fallible_impl_from)]
impl From<&parts::Header> for PartsSetHeader {
fn from(parts: &parts::Header) -> Self {
Self::new(parts.total as i64, parts.hash.as_bytes().to_vec())
Self::new(
i64::try_from(parts.total).expect("overflow"),
parts.hash.as_bytes().to_vec(),
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/amino_types/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl From<PubKeyResponse> for PublicKey {
}

// TODO(xla): Either complete implementation or convert to TryFrom.
#[allow(clippy::fallible_impl_from)]
#[allow(clippy::fallible_impl_from, clippy::unimplemented)]
impl From<PublicKey> for PubKeyResponse {
fn from(public_key: PublicKey) -> Self {
match public_key {
Expand Down
4 changes: 2 additions & 2 deletions tendermint/src/amino_types/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ mod tests {
let dt = "2018-02-11T07:09:22.765Z".parse::<DateTime<Utc>>().unwrap();
let t = TimeMsg {
seconds: dt.timestamp(),
nanos: dt.timestamp_subsec_nanos() as i32,
nanos: i32::try_from(dt.timestamp_subsec_nanos()).expect("overflow"),
};
let proposal = Proposal {
msg_type: SignedMsgType::Proposal.to_u32(),
Expand Down Expand Up @@ -260,7 +260,7 @@ mod tests {
let dt = "2018-02-11T07:09:22.765Z".parse::<DateTime<Utc>>().unwrap();
let t = TimeMsg {
seconds: dt.timestamp(),
nanos: dt.timestamp_subsec_nanos() as i32,
nanos: i32::try_from(dt.timestamp_subsec_nanos()).expect("overflow"),
};
let proposal = Proposal {
msg_type: SignedMsgType::Proposal.to_u32(),
Expand Down
6 changes: 3 additions & 3 deletions tendermint/src/amino_types/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
time::{ParseTimestamp, Time},
};
use chrono::{TimeZone, Utc};
use std::convert::TryFrom;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

#[derive(Clone, PartialEq, Message)]
Expand All @@ -28,12 +29,11 @@ impl ParseTimestamp for Msg {
#[allow(clippy::fallible_impl_from)]
impl From<Time> for Msg {
fn from(ts: Time) -> Self {
// TODO: non-panicking method for getting this?
let duration = ts
.duration_since(Time::unix_epoch())
.expect("unable to get duration from epoch");
let seconds = duration.as_secs() as i64;
let nanos = duration.subsec_nanos() as i32;
let seconds = i64::try_from(duration.as_secs()).expect("overflow");
let nanos = i32::try_from(duration.subsec_nanos()).expect("overflow");

Self { seconds, nanos }
}
Expand Down
14 changes: 8 additions & 6 deletions tendermint/src/amino_types/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,21 @@ impl Vote {
}
}

// TODO(xla): Rework into TryFrom.
#[allow(clippy::fallible_impl_from)]
impl From<&vote::Vote> for Vote {
fn from(vote: &vote::Vote) -> Self {
Self {
vote_type: vote.vote_type.to_u32(),
height: vote.height.value() as i64, // TODO potential overflow :-/
round: vote.round as i64,
height: i64::try_from(vote.height.value()).expect("overflow"),
round: i64::try_from(vote.round).expect("overflow"),
block_id: Some(BlockId {
hash: vote.block_id.hash.as_bytes().to_vec(),
parts_header: vote.block_id.parts.as_ref().map(PartsSetHeader::from),
}),
timestamp: Some(TimeMsg::from(vote.timestamp)),
validator_address: vote.validator_address.as_bytes().to_vec(),
validator_index: vote.validator_index as i64, // TODO potential overflow :-/
validator_index: i64::try_from(vote.validator_index).expect("overflow"),
signature: vote.signature.as_bytes().to_vec(),
}
}
Expand Down Expand Up @@ -248,7 +250,7 @@ mod tests {
let dt = "2017-12-25T03:00:01.234Z".parse::<DateTime<Utc>>().unwrap();
let t = TimeMsg {
seconds: dt.timestamp(),
nanos: dt.timestamp_subsec_nanos() as i32,
nanos: i32::try_from(dt.timestamp_subsec_nanos()).unwrap(),
};
let vote = Vote {
vote_type: SignedMsgType::PreVote.to_u32(),
Expand Down Expand Up @@ -416,7 +418,7 @@ mod tests {
let dt = "2017-12-25T03:00:01.234Z".parse::<DateTime<Utc>>().unwrap();
let t = TimeMsg {
seconds: dt.timestamp(),
nanos: dt.timestamp_subsec_nanos() as i32,
nanos: i32::try_from(dt.timestamp_subsec_nanos()).unwrap(),
};
let vote = Vote {
validator_address: vec![
Expand Down Expand Up @@ -472,7 +474,7 @@ mod tests {
let dt = "2017-12-25T03:00:01.234Z".parse::<DateTime<Utc>>().unwrap();
let t = TimeMsg {
seconds: dt.timestamp(),
nanos: dt.timestamp_subsec_nanos() as i32,
nanos: i32::try_from(dt.timestamp_subsec_nanos()).unwrap(),
};
let vote = Vote {
validator_address: vec![
Expand Down
6 changes: 0 additions & 6 deletions tendermint/src/block/height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ impl From<Height> for u64 {
}
}

impl From<Height> for i64 {
fn from(height: Height) -> Self {
height.0 as Self
}
}

impl FromStr for Height {
type Err = Error;

Expand Down
1 change: 1 addition & 0 deletions tendermint/src/config/node_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl NodeKey {

// TODO(xla): Should return a Result or be implemented for all variants.
/// Get node ID for this keypair
#[allow(clippy::unimplemented)]
pub fn node_id(&self) -> node::Id {
match &self.public_key() {
PublicKey::Ed25519(key) => node::Id::from(*key),
Expand Down
3 changes: 1 addition & 2 deletions tendermint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
unused_qualifications,
warnings
)]
#![allow(
#![deny(
clippy::all,
clippy::cargo,
clippy::nursery,
clippy::pedantic,
clippy::restriction
)]
#![deny(clippy::needless_borrow)]
// TODO(xla): Review all uses of expect and try to rework into proper error handling, this crate
// should ideally never panic.
#![allow(
Expand Down

0 comments on commit 7750986

Please sign in to comment.