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

Commit

Permalink
tendermint-rs: Disallow a block height of 0
Browse files Browse the repository at this point in the history
Tendermint itself says: "Height must be greater than 0" (code -32603)
  • Loading branch information
tony-iqlusion committed Apr 19, 2019
1 parent e061b10 commit 954d0c4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions tendermint-rs/src/amino_types/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct Proposal {
// TODO(tony): custom derive proc macro for this e.g. `derive(ParseBlockHeight)`
impl block::ParseHeight for Proposal {
fn parse_block_height(&self) -> Result<block::Height, Error> {
block::Height::parse(self.height)
block::Height::try_from_i64(self.height)
}
}

Expand Down Expand Up @@ -74,7 +74,7 @@ impl chain::ParseId for CanonicalProposal {

impl block::ParseHeight for CanonicalProposal {
fn parse_block_height(&self) -> Result<block::Height, Error> {
block::Height::parse(self.height)
block::Height::try_from_i64(self.height)
}
}

Expand Down Expand Up @@ -136,7 +136,7 @@ impl SignableMsg for SignProposalRequest {
fn consensus_state(&self) -> Option<ConsensusState> {
match self.proposal {
Some(ref p) => Some(ConsensusState {
height: match block::Height::parse(p.height) {
height: match block::Height::try_from_i64(p.height) {
Ok(h) => h,
Err(_err) => return None, // TODO(tarcieri): return an error?
},
Expand Down
6 changes: 3 additions & 3 deletions tendermint-rs/src/amino_types/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Vote {

impl block::ParseHeight for Vote {
fn parse_block_height(&self) -> Result<block::Height, Error> {
block::Height::parse(self.height)
block::Height::try_from_i64(self.height)
}
}

Expand Down Expand Up @@ -92,7 +92,7 @@ impl chain::ParseId for CanonicalVote {

impl block::ParseHeight for CanonicalVote {
fn parse_block_height(&self) -> Result<block::Height, Error> {
block::Height::parse(self.height)
block::Height::try_from_i64(self.height)
}
}

Expand Down Expand Up @@ -157,7 +157,7 @@ impl SignableMsg for SignVoteRequest {
fn consensus_state(&self) -> Option<ConsensusState> {
match self.vote {
Some(ref v) => Some(ConsensusState {
height: match block::Height::parse(v.height) {
height: match block::Height::try_from_i64(v.height) {
Ok(h) => h,
Err(_err) => return None, // TODO(tarcieri): return an error?
},
Expand Down
9 changes: 5 additions & 4 deletions tendermint-rs/src/block/height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ pub struct Height(pub u64);

impl Height {
/// Parse height from the integer type used in Amino messages
pub fn parse(n: i64) -> Result<Self, Error> {
if n >= 0 {
pub fn try_from_i64(n: i64) -> Result<Self, Error> {
// Minimum height is 1
if n > 0 {
Ok(Height(n as u64))
} else {
Err(Error::OutOfRange)
Expand Down Expand Up @@ -46,7 +47,7 @@ impl Display for Height {

impl From<i64> for Height {
fn from(n: i64) -> Height {
Self::parse(n).unwrap()
Self::try_from_i64(n).unwrap()
}
}

Expand All @@ -72,7 +73,7 @@ impl FromStr for Height {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Error> {
Ok(Self::from(s.parse::<u64>().map_err(|_| Error::Parse)?))
Self::try_from_i64(s.parse::<i64>().map_err(|_| Error::Parse)?)
}
}

Expand Down

0 comments on commit 954d0c4

Please sign in to comment.