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

Commit

Permalink
tendermint-rs: /blockchain RPC endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-iqlusion committed Apr 21, 2019
1 parent fc36806 commit ee55ffd
Show file tree
Hide file tree
Showing 6 changed files with 447 additions and 17 deletions.
12 changes: 3 additions & 9 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,9 @@ fn client_loop(config: ValidatorConfig, should_term: &Arc<AtomicBool>) {
ref host,
port,
} => match &secret_key {
Some(path) => tcp_session(
chain_id,
max_height,
*peer_id,
host,
*port,
path,
should_term,
),
Some(path) => {
tcp_session(chain_id, max_height, peer_id, host, port, path, should_term)
}
None => {
error!(
"config error: missing field `secret_key` for validator {}",
Expand Down
1 change: 1 addition & 0 deletions tendermint-rs/src/rpc/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub mod abci_info;
pub mod block;
pub mod blockchain;
pub mod commit;
pub mod genesis;
pub mod net_info;
Expand Down
1 change: 1 addition & 0 deletions tendermint-rs/src/rpc/endpoint/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};

/// Get information about a specific block
pub struct Request {
/// Height of the block to request
height: block::Height,
}

Expand Down
50 changes: 50 additions & 0 deletions tendermint-rs/src/rpc/endpoint/blockchain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! `/block` endpoint JSONRPC wrapper

use crate::{block, rpc};
use serde::{Deserialize, Serialize};
use std::ops::Range;

/// Get information about a specific block
pub struct Request {
/// First block in the sequence to request info about
min: block::Height,

/// Last block in the sequence to request info about
max: block::Height,
}

impl Request {
/// Request information about a sequence of blocks
pub fn new(min: block::Height, max: block::Height) -> Self {
Self { min, max }
}
}

impl From<Range<block::Height>> for Request {
fn from(range: Range<block::Height>) -> Request {
Request::new(range.start, range.end)
}
}

impl rpc::Request for Request {
type Response = Response;

fn path(&self) -> rpc::request::Path {
// TODO(tarcieri): use a `uri` crate to construct this?
format!("/block?minHeight={}&maxHeight={}", self.min, self.max)
.parse()
.unwrap()
}
}

/// Block responses
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Response {
/// Last block height for this particular chain
pub last_height: block::Height,

/// Block metadata
pub block_metas: Vec<block::Meta>,
}

impl rpc::Response for Response {}
31 changes: 23 additions & 8 deletions tendermint-rs/tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ mod endpoints {
use std::{fs, path::PathBuf};
use tendermint::rpc::{endpoint, Response};

const EXAMPLE_APP: &str = "GaiaApp";
const EXAMPLE_CHAIN: &str = "cosmoshub-1";

fn read_json_fixture(name: &str) -> String {
fs::read_to_string(PathBuf::from("./tests/support/rpc/").join(name.to_owned() + ".json"))
.unwrap()
Expand All @@ -16,23 +19,23 @@ mod endpoints {
.unwrap()
.response;

assert_eq!(response.data.as_str(), "GaiaApp");
assert_eq!(response.data.as_str(), EXAMPLE_APP);
assert_eq!(response.last_block_height.value(), 488120);
}

#[test]
fn block() {
let block = endpoint::block::Response::from_json(&read_json_fixture("block")).unwrap();
let response = endpoint::block::Response::from_json(&read_json_fixture("block")).unwrap();

let tendermint::Block {
header,
data,
evidence,
last_commit,
} = block.block;
} = response.block;

assert_eq!(header.version.block, 10);
assert_eq!(header.chain_id.as_str(), "cosmoshub-1");
assert_eq!(header.chain_id.as_str(), EXAMPLE_CHAIN);
assert_eq!(header.height.value(), 15);
assert_eq!(header.num_txs, 2);

Expand All @@ -41,12 +44,24 @@ mod endpoints {
assert_eq!(last_commit.precommits.len(), 65);
}

#[test]
fn blockchain() {
let response =
endpoint::blockchain::Response::from_json(&read_json_fixture("blockchain")).unwrap();

assert_eq!(response.last_height.value(), 488556);
assert_eq!(response.block_metas.len(), 10);

let block_meta = &response.block_metas[0];
assert_eq!(block_meta.header.chain_id.as_str(), EXAMPLE_CHAIN)
}

#[test]
fn commit() {
let response = endpoint::commit::Response::from_json(&read_json_fixture("commit")).unwrap();
let header = response.signed_header.header;

assert_eq!(header.chain_id.as_ref(), "cosmoshub-1");
assert_eq!(header.chain_id.as_ref(), EXAMPLE_CHAIN);
}

#[test]
Expand All @@ -60,7 +75,7 @@ mod endpoints {
..
} = response.genesis;

assert_eq!(chain_id.as_str(), "cosmoshub-1");
assert_eq!(chain_id.as_str(), EXAMPLE_CHAIN);
assert_eq!(consensus_params.block_size.max_bytes, 150000);
}

Expand All @@ -70,14 +85,14 @@ mod endpoints {
endpoint::net_info::Response::from_json(&read_json_fixture("net_info")).unwrap();

assert_eq!(response.n_peers, 2);
assert_eq!(response.peers[0].node_info.network.as_str(), "cosmoshub-1");
assert_eq!(response.peers[0].node_info.network.as_str(), EXAMPLE_CHAIN);
}

#[test]
fn status() {
let response = endpoint::status::Response::from_json(&read_json_fixture("status")).unwrap();

assert_eq!(response.node_info.network.as_str(), "cosmoshub-1");
assert_eq!(response.node_info.network.as_str(), EXAMPLE_CHAIN);
assert_eq!(response.sync_info.latest_block_height.value(), 410744);
assert_eq!(response.validator_info.voting_power.value(), 0);
}
Expand Down
Loading

0 comments on commit ee55ffd

Please sign in to comment.