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

Commit

Permalink
tendermint-rs: /abci_info RPC endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-iqlusion committed Apr 21, 2019
1 parent c6c3077 commit fc36806
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 38 deletions.
2 changes: 1 addition & 1 deletion tendermint-rs/src/block/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{block, Vote};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Last commit to a particular blockchain.
/// Last commit to a particular blockchain: +2/3 precommit signatures.
///
/// <https://github.com/tendermint/tendermint/blob/master/docs/spec/blockchain/blockchain.md#lastcommit>
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down
2 changes: 1 addition & 1 deletion tendermint-rs/src/block/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mod tests {
fn parses_hex_strings() {
let id = Id::from_str(EXAMPLE_SHA256_ID).unwrap();
assert_eq!(
id.hash.as_slice().unwrap(),
id.hash.as_bytes().unwrap(),
b"\x26\xC0\xA4\x1F\x32\x43\xC6\xBC\xD7\xAD\x2D\xFF\x8A\x8D\x83\xA7\
\x1D\x29\xD3\x07\xB5\x32\x6C\x22\x7F\x73\x4A\x1A\x51\x2F\xE4\x7D"
.as_ref()
Expand Down
2 changes: 1 addition & 1 deletion tendermint-rs/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Hash {
}

/// Borrow the `Hash` as a byte slice
pub fn as_slice(&self) -> Option<&[u8]> {
pub fn as_bytes(&self) -> Option<&[u8]> {
match self {
Hash::Sha256(ref h) => Some(h.as_ref()),
Hash::Null => None,
Expand Down
1 change: 1 addition & 0 deletions tendermint-rs/src/rpc/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Tendermint JSONRPC endpoints

pub mod abci_info;
pub mod block;
pub mod commit;
pub mod genesis;
Expand Down
70 changes: 70 additions & 0 deletions tendermint-rs/src/rpc/endpoint/abci_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! `/abci_info` endpoint JSONRPC wrapper

use crate::{block, hash, rpc, Hash};
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize, Serializer};
use subtle_encoding::base64;

/// Request ABCI information from a node
#[derive(Debug, Default)]
pub struct Request;

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

fn path(&self) -> rpc::request::Path {
"/abci_info".parse().unwrap()
}
}

/// ABCI information response
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Response {
/// ABCI info
pub response: AbciInfoResponse,
}

impl rpc::Response for Response {}

/// ABCI information
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AbciInfoResponse {
/// Name of the application
pub data: String,

/// Version
pub version: Option<String>,

/// Last block height
pub last_block_height: block::Height,

/// Last app hash for the block
#[serde(
serialize_with = "serialize_app_hash",
deserialize_with = "parse_app_hash"
)]
pub last_block_app_hash: Hash,
}

/// Parse Base64-encoded app hash
#[cfg(feature = "rpc")]
pub(crate) fn parse_app_hash<'de, D>(deserializer: D) -> Result<Hash, D::Error>
where
D: Deserializer<'de>,
{
let bytes = base64::decode(String::deserialize(deserializer)?.as_bytes())
.map_err(|e| D::Error::custom(format!("{}", e)))?;

Hash::new(hash::Algorithm::Sha256, &bytes).map_err(|e| D::Error::custom(format!("{}", e)))
}

/// Serialize Base64-encoded app hash
#[cfg(feature = "rpc")]
pub(crate) fn serialize_app_hash<S>(hash: &Hash, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
hash.as_bytes()
.map(|bytes| String::from_utf8(base64::encode(bytes)).unwrap())
.unwrap_or_default()
.serialize(serializer)
}
4 changes: 2 additions & 2 deletions tendermint-rs/src/rpc/endpoint/net_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
time::Duration,
};

/// Request the status of the node
/// Request network information from a node
#[derive(Debug, Default)]
pub struct Request;

Expand All @@ -20,7 +20,7 @@ impl rpc::Request for Request {
}
}

/// Status responses
/// Net info responses
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Response {
/// Are we presently listening?
Expand Down
6 changes: 3 additions & 3 deletions tendermint-rs/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ impl Transaction {
}

/// Borrow the contents of this transaction as a byte slice
pub fn as_slice(&self) -> &[u8] {
pub fn as_bytes(&self) -> &[u8] {
self.0.as_slice()
}
}

impl AsRef<[u8]> for Transaction {
fn as_ref(&self) -> &[u8] {
self.as_slice()
self.as_bytes()
}
}

Expand All @@ -51,7 +51,7 @@ impl<'de> Deserialize<'de> for Transaction {
#[cfg(feature = "serde")]
impl Serialize for Transaction {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
String::from_utf8(base64::encode(self.as_slice()))
String::from_utf8(base64::encode(self.as_bytes()))
.unwrap()
.serialize(serializer)
}
Expand Down
64 changes: 34 additions & 30 deletions tendermint-rs/tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,26 @@ mod endpoints {
.unwrap()
}

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

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

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

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

assert_eq!(header.version.block, 10);
assert_eq!(header.chain_id.as_str(), "cosmoshub-1");
Expand All @@ -34,58 +43,53 @@ mod endpoints {

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

println!("commit_response: {:?}", commit_response);
assert_eq!(header.chain_id.as_ref(), "cosmoshub-1");
}

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

let tendermint::Genesis {
chain_id,
consensus_params,
..
} = genesis_response.genesis;
} = response.genesis;

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

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

assert_eq!(net_info_response.n_peers, 2);
assert_eq!(
net_info_response.peers[0].node_info.network.as_str(),
"cosmoshub-1"
);
let response =
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");
}

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

assert_eq!(status_response.node_info.network.as_str(), "cosmoshub-1");
assert_eq!(
status_response.sync_info.latest_block_height.value(),
410744
);
assert_eq!(status_response.validator_info.voting_power.value(), 0);
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.sync_info.latest_block_height.value(), 410744);
assert_eq!(response.validator_info.voting_power.value(), 0);
}

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

assert_eq!(response.block_height.value(), 42);
let validators = response.validators;

println!("validators: {:?}", validators_response);
assert_eq!(validators.len(), 65);
}
}
11 changes: 11 additions & 0 deletions tendermint-rs/tests/support/rpc/abci_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"jsonrpc": "2.0",
"id": "",
"result": {
"response": {
"data": "GaiaApp",
"last_block_height": "488120",
"last_block_app_hash": "2LnCw0fN+Zq/gs5SOuya/GRHUmtWftAqAkTUuoxl4g4="
}
}
}

0 comments on commit fc36806

Please sign in to comment.