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

Commit

Permalink
tendermint-rs: /genesis RPC endpoint and related types
Browse files Browse the repository at this point in the history
Adds support for parsing genesis files from RPC responses.

Genesis files are generic over the underlying application, and by
default use `serde_json::Value` to model arbitrary JSON data.
  • Loading branch information
tony-iqlusion committed Apr 20, 2019
1 parent 030109d commit 3051870
Show file tree
Hide file tree
Showing 24 changed files with 19,543 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
rustc --version
cargo --version
cargo test --all --all-features -- --test-threads 1
cd tendermint-rs && cargo test --release --features=rpc
cd tendermint-rs && cargo test --release --all-features
- run:
name: audit
command: |
Expand Down
23 changes: 13 additions & 10 deletions src/chain/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use std::{
io::{self, prelude::*},
path::{Path, PathBuf},
};
use tendermint::chain::ConsensusState;
use tendermint::chain::consensus;

/// State tracking for double signing prevention
pub struct State {
consensus_state: ConsensusState,
consensus_state: consensus::State,
state_file_path: PathBuf,
}

Expand All @@ -30,7 +30,7 @@ impl State {
P: AsRef<Path>,
{
let mut lst = State {
consensus_state: ConsensusState::default(),
consensus_state: consensus::State::default(),
state_file_path: path.as_ref().to_owned(),
};

Expand Down Expand Up @@ -58,8 +58,11 @@ impl State {
}

/// Check and update the chain's height, round, and step
pub fn update_consensus_state(&mut self, new_state: ConsensusState) -> Result<(), StateError> {
// TODO(tarcieri): impl `PartialOrd` on `ConsensusState` to simplify this logic?
pub fn update_consensus_state(
&mut self,
new_state: consensus::State,
) -> Result<(), StateError> {
// TODO(tarcieri): rewrite this using `Ord` impl on `consensus::State`
if new_state.height < self.consensus_state.height {
fail!(
StateErrorKind::HeightRegression,
Expand Down Expand Up @@ -127,7 +130,7 @@ impl State {
let delta = hook_height - last_height;

if delta < hook::BLOCK_HEIGHT_SANITY_LIMIT {
let mut new_state = ConsensusState::default();
let mut new_state = consensus::State::default();
new_state.height = output.latest_block_height;
self.consensus_state = new_state;

Expand Down Expand Up @@ -178,7 +181,7 @@ mod tests {
#[test]
fn hrs_test() {
let mut last_sign_state = State {
consensus_state: ConsensusState {
consensus_state: consensus::State {
height: 1i64.into(),
round: 1,
step: 0,
Expand All @@ -189,7 +192,7 @@ mod tests {

assert_eq!(
last_sign_state
.update_consensus_state(ConsensusState {
.update_consensus_state(consensus::State {
height: 2i64.into(),
round: 0,
step: 0,
Expand All @@ -203,7 +206,7 @@ mod tests {
#[test]
fn hrs_test_double_sign() {
let mut last_sign_state = State {
consensus_state: ConsensusState {
consensus_state: consensus::State {
height: 1i64.into(),
round: 1,
step: 0,
Expand All @@ -212,7 +215,7 @@ mod tests {
state_file_path: EXAMPLE_PATH.into(),
};
let double_sign_block = block::Id::from_str(EXAMPLE_DOUBLE_SIGN_BLOCK_ID).unwrap();
let err = last_sign_state.update_consensus_state(ConsensusState {
let err = last_sign_state.update_consensus_state(consensus::State {
height: 1i64.into(),
round: 1,
step: 1,
Expand Down
2 changes: 1 addition & 1 deletion tendermint-rs/src/amino_types/ed25519.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::public_keys::PublicKey;
use crate::public_key::PublicKey;
use signatory::ed25519::PUBLIC_KEY_SIZE;

// Note:On the golang side this is generic in the sense that it could everything that implements
Expand Down
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 @@ -7,7 +7,7 @@ use super::{
};
use crate::{
block::{self, ParseId},
chain::{self, ConsensusState},
chain, consensus,
error::Error,
};
use bytes::BufMut;
Expand Down Expand Up @@ -133,9 +133,9 @@ impl SignableMsg for SignProposalRequest {
None => Err(MissingConsensusMessage.into()),
}
}
fn consensus_state(&self) -> Option<ConsensusState> {
fn consensus_state(&self) -> Option<consensus::State> {
match self.proposal {
Some(ref p) => Some(ConsensusState {
Some(ref p) => Some(consensus::State {
height: match block::Height::try_from_i64(p.height) {
Ok(h) => h,
Err(_err) => return None, // TODO(tarcieri): return an error?
Expand Down
4 changes: 2 additions & 2 deletions tendermint-rs/src/amino_types/signature.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::validate::ValidationError;
use crate::chain::{self, ConsensusState};
use crate::{chain, consensus};
use bytes::BufMut;
use prost::{DecodeError, EncodeError};
use signatory::ed25519;
Expand All @@ -16,7 +16,7 @@ pub trait SignableMsg {
/// Set the Ed25519 signature on the underlying message
fn set_signature(&mut self, sig: &ed25519::Signature);
fn validate(&self) -> Result<(), ValidationError>;
fn consensus_state(&self) -> Option<ConsensusState>;
fn consensus_state(&self) -> Option<consensus::State>;
}

/// Signed message types. This follows:
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 @@ -8,7 +8,7 @@ use super::{
};
use crate::{
block::{self, ParseId},
chain::{self, ConsensusState},
chain, consensus,
error::Error,
};
use bytes::BufMut;
Expand Down Expand Up @@ -154,9 +154,9 @@ impl SignableMsg for SignVoteRequest {
None => Err(MissingConsensusMessage.into()),
}
}
fn consensus_state(&self) -> Option<ConsensusState> {
fn consensus_state(&self) -> Option<consensus::State> {
match self.vote {
Some(ref v) => Some(ConsensusState {
Some(ref v) => Some(consensus::State {
height: match block::Height::try_from_i64(v.height) {
Ok(h) => h,
Err(_err) => return None, // TODO(tarcieri): return an error?
Expand Down
11 changes: 8 additions & 3 deletions tendermint-rs/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ mod height;
mod id;
mod meta;
pub mod parts;
mod size;

pub use self::height::*;
pub use self::id::{Id, ParseId};
pub use self::{header::Header, meta::Meta};
pub use self::{
header::Header,
height::*,
id::{Id, ParseId},
meta::Meta,
size::Size,
};
use crate::{commit::LastCommit, evidence, transaction};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down
32 changes: 32 additions & 0 deletions tendermint-rs/src/block/size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//! Block size parameters

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

/// Block size parameters
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Size {
/// Maximum number of bytes in a block
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "serializers::serialize_u64",
deserialize_with = "serializers::parse_u64"
)
)]
pub max_bytes: u64,

/// Maximum amount of gas which can be spent on a block
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "serializers::serialize_u64",
deserialize_with = "serializers::parse_u64"
)
)]
pub max_gas: u64,
}
2 changes: 0 additions & 2 deletions tendermint-rs/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

pub mod id;
mod info;
pub mod state;

pub use self::{
id::{Id, ParseId},
info::*,
state::ConsensusState,
};
22 changes: 0 additions & 22 deletions tendermint-rs/src/chain/state.rs

This file was deleted.

6 changes: 6 additions & 0 deletions tendermint-rs/src/consensus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! Tendermint consensus

mod params;
mod state;

pub use self::{params::Params, state::State};
27 changes: 27 additions & 0 deletions tendermint-rs/src/consensus/params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Tendermint consensus parameters

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

/// Tendermint consensus parameters
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Params {
/// Block size parameters
pub block_size: block::Size,

/// Evidence parameters
pub evidence: evidence::Params,

/// Validator parameters
pub validator: ValidatorParams,
}

/// Validator consensus parameters
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ValidatorParams {
/// Allowed algorithms for validator signing
pub pub_key_types: Vec<public_key::Algorithm>,
}
99 changes: 99 additions & 0 deletions tendermint-rs/src/consensus/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//! Tendermint consensus state

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

/// Tendermint consensus state
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct State {
/// Current block height
pub height: block::Height,

/// Current consensus round
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "serializers::serialize_i64",
deserialize_with = "serializers::parse_i64"
)
)]
pub round: i64,

/// Current consensus step
pub step: i8,

/// Block ID being proposed (if available)
pub block_id: Option<block::Id>,
}

impl Ord for State {
fn cmp(&self, other: &State) -> Ordering {
if self.height < other.height {
Ordering::Less
} else if self.height == other.height {
if self.round < other.round {
Ordering::Less
} else if self.round == other.round {
self.step.cmp(&other.step)
} else {
Ordering::Greater
}
} else {
Ordering::Greater
}
}
}

impl PartialOrd for State {
fn partial_cmp(&self, other: &State) -> Option<Ordering> {
Some(self.cmp(other))
}
}

#[cfg(test)]
mod tests {
use super::State;
use crate::block;

#[test]
fn state_ord_test() {
let new = State {
height: block::Height::from(9001u64),
round: 0,
step: 0,
block_id: None,
};

let old = State {
height: block::Height::from(1001u64),
round: 1,
step: 0,
block_id: None,
};

let older = State {
height: block::Height::from(1001u64),
round: 0,
step: 0,
block_id: None,
};

let oldest = State {
height: block::Height::default(),
round: 0,
step: 0,
block_id: None,
};

assert!(old < new);
assert!(older < old);
assert!(oldest < older);
assert!(oldest < new);
}
}
Loading

0 comments on commit 3051870

Please sign in to comment.