From ee55ffd99e3391403bf91dbffc554724440152ed Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 21 Apr 2019 09:32:00 -0700 Subject: [PATCH] tendermint-rs: /blockchain RPC endpoint --- src/client.rs | 12 +- tendermint-rs/src/rpc/endpoint.rs | 1 + tendermint-rs/src/rpc/endpoint/block.rs | 1 + tendermint-rs/src/rpc/endpoint/blockchain.rs | 50 +++ tendermint-rs/tests/rpc.rs | 31 +- .../tests/support/rpc/blockchain.json | 369 ++++++++++++++++++ 6 files changed, 447 insertions(+), 17 deletions(-) create mode 100644 tendermint-rs/src/rpc/endpoint/blockchain.rs create mode 100644 tendermint-rs/tests/support/rpc/blockchain.json diff --git a/src/client.rs b/src/client.rs index cb5e4ad..5092470 100644 --- a/src/client.rs +++ b/src/client.rs @@ -76,15 +76,9 @@ fn client_loop(config: ValidatorConfig, should_term: &Arc) { 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 {}", diff --git a/tendermint-rs/src/rpc/endpoint.rs b/tendermint-rs/src/rpc/endpoint.rs index 20d4144..dc7b482 100644 --- a/tendermint-rs/src/rpc/endpoint.rs +++ b/tendermint-rs/src/rpc/endpoint.rs @@ -2,6 +2,7 @@ pub mod abci_info; pub mod block; +pub mod blockchain; pub mod commit; pub mod genesis; pub mod net_info; diff --git a/tendermint-rs/src/rpc/endpoint/block.rs b/tendermint-rs/src/rpc/endpoint/block.rs index 96ba1a3..230e632 100644 --- a/tendermint-rs/src/rpc/endpoint/block.rs +++ b/tendermint-rs/src/rpc/endpoint/block.rs @@ -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, } diff --git a/tendermint-rs/src/rpc/endpoint/blockchain.rs b/tendermint-rs/src/rpc/endpoint/blockchain.rs new file mode 100644 index 0000000..14d9652 --- /dev/null +++ b/tendermint-rs/src/rpc/endpoint/blockchain.rs @@ -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> for Request { + fn from(range: Range) -> 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, +} + +impl rpc::Response for Response {} diff --git a/tendermint-rs/tests/rpc.rs b/tendermint-rs/tests/rpc.rs index be23f11..5b3811c 100644 --- a/tendermint-rs/tests/rpc.rs +++ b/tendermint-rs/tests/rpc.rs @@ -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() @@ -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); @@ -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] @@ -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); } @@ -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); } diff --git a/tendermint-rs/tests/support/rpc/blockchain.json b/tendermint-rs/tests/support/rpc/blockchain.json new file mode 100644 index 0000000..db302cb --- /dev/null +++ b/tendermint-rs/tests/support/rpc/blockchain.json @@ -0,0 +1,369 @@ +{ + "jsonrpc": "2.0", + "id": "", + "result": { + "last_height": "488556", + "block_metas": [ + { + "block_id": { + "hash": "0BC81BF032E1251D2BF45FB7FAD70BA1333080FD6DC52FE8A18A09C3859DB99D", + "parts": { + "total": "1", + "hash": "5E97E2B494BF58C02BE385AE80F08A7DDBFAE922ED0363C892D17DB549DAF4AA" + } + }, + "header": { + "version": { + "block": "10", + "app": "0" + }, + "chain_id": "cosmoshub-1", + "height": "10", + "time": "2019-03-13T23:08:45.915941777Z", + "num_txs": "1", + "total_txs": "2", + "last_block_id": { + "hash": "B6F22387BD2A7A012BFF88ACE131E871660A8D7CAD756D22B911FA408C9B9FB0", + "parts": { + "total": "1", + "hash": "99043B16957F2BFF5C1A24B290AD44CBCD95B0DC164E18063FA566240B7B3AFC" + } + }, + "last_commit_hash": "483C77DD363242ABB481D474C5DA618D9EB715B3C464D813C844714C9DD16EED", + "data_hash": "37900B11A5BBF82EE275519DAA11BDEFD9A889F51D02A2B975AD506CF42A48A3", + "validators_hash": "87DA6D0C99DEDEE69605561721821C7DF4170D39E45D68BC9BE54F96351563F4", + "next_validators_hash": "87DA6D0C99DEDEE69605561721821C7DF4170D39E45D68BC9BE54F96351563F4", + "consensus_hash": "29C5629148426FB74676BE07F40F2ED79674A67F5833E4C9CCBF759C9372E99C", + "app_hash": "B50AA0AEA259C913680A50FF426F61B02E281E512F6941C7D933F566FB299E52", + "last_results_hash": "", + "evidence_hash": "", + "proposer_address": "E800740C68C81B30345C3AE2BA638FA56FF67EEF" + } + }, + { + "block_id": { + "hash": "B6F22387BD2A7A012BFF88ACE131E871660A8D7CAD756D22B911FA408C9B9FB0", + "parts": { + "total": "1", + "hash": "99043B16957F2BFF5C1A24B290AD44CBCD95B0DC164E18063FA566240B7B3AFC" + } + }, + "header": { + "version": { + "block": "10", + "app": "0" + }, + "chain_id": "cosmoshub-1", + "height": "9", + "time": "2019-03-13T23:08:38.954781954Z", + "num_txs": "0", + "total_txs": "1", + "last_block_id": { + "hash": "C1D3D762700AD5CDDCB724A55CA239B92FC934C4AD163FFDCAC476A42A97320A", + "parts": { + "total": "1", + "hash": "C5FAA5B7738D7010CA712FBC2B2355605D388990DE0FDE731A854FE279325C1A" + } + }, + "last_commit_hash": "AE94F604694CDD4DF798A8B0FE06A14B7C094C3E867E5C7266EFDB9E9F979FF3", + "data_hash": "", + "validators_hash": "87DA6D0C99DEDEE69605561721821C7DF4170D39E45D68BC9BE54F96351563F4", + "next_validators_hash": "87DA6D0C99DEDEE69605561721821C7DF4170D39E45D68BC9BE54F96351563F4", + "consensus_hash": "29C5629148426FB74676BE07F40F2ED79674A67F5833E4C9CCBF759C9372E99C", + "app_hash": "A4DBDDF3CD1C274A1E0D2018F57B8EEA9E5CF6FE45896285B1E4BD9B19892C97", + "last_results_hash": "", + "evidence_hash": "", + "proposer_address": "C2356622B495725961B5B201A382DD57CD3305EC" + } + }, + { + "block_id": { + "hash": "C1D3D762700AD5CDDCB724A55CA239B92FC934C4AD163FFDCAC476A42A97320A", + "parts": { + "total": "1", + "hash": "C5FAA5B7738D7010CA712FBC2B2355605D388990DE0FDE731A854FE279325C1A" + } + }, + "header": { + "version": { + "block": "10", + "app": "0" + }, + "chain_id": "cosmoshub-1", + "height": "8", + "time": "2019-03-13T23:08:31.890373101Z", + "num_txs": "0", + "total_txs": "1", + "last_block_id": { + "hash": "954B81C860ABF508F272C2A402AC81BFBBDA8BA4B2A627A60730FA36915C53AF", + "parts": { + "total": "1", + "hash": "842ADFA93717FDBE551BDAB96BED2E50D67FCA216421C97DE1A47BC61FF50E58" + } + }, + "last_commit_hash": "5CA6FCF16094FCABD00F456E282F1FFB5B12275E32D964EE362C067DAD4492DB", + "data_hash": "", + "validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "next_validators_hash": "87DA6D0C99DEDEE69605561721821C7DF4170D39E45D68BC9BE54F96351563F4", + "consensus_hash": "29C5629148426FB74676BE07F40F2ED79674A67F5833E4C9CCBF759C9372E99C", + "app_hash": "F15C16B293EB09F4D95EE863D1CC49A18BAFA06578EC46CC0B8636F2A890C48C", + "last_results_hash": "6E340B9CFFB37A989CA544E6BB780A2C78901D3FB33738768511A30617AFA01D", + "evidence_hash": "", + "proposer_address": "9C17C94F7313BB4D6E064287BEEDE5D3888E8855" + } + }, + { + "block_id": { + "hash": "954B81C860ABF508F272C2A402AC81BFBBDA8BA4B2A627A60730FA36915C53AF", + "parts": { + "total": "1", + "hash": "842ADFA93717FDBE551BDAB96BED2E50D67FCA216421C97DE1A47BC61FF50E58" + } + }, + "header": { + "version": { + "block": "10", + "app": "0" + }, + "chain_id": "cosmoshub-1", + "height": "7", + "time": "2019-03-13T23:08:19.589587064Z", + "num_txs": "1", + "total_txs": "1", + "last_block_id": { + "hash": "A5873B845F3CD93203D8E1306F756F3BD93E1E3828A2343FD9B898D4DD66BD55", + "parts": { + "total": "1", + "hash": "3E5A9C4ACC1C217DAEBD68E29B99B013603D2902BF974F9695FCA08E1E4E8867" + } + }, + "last_commit_hash": "D164CF0C566EEB93829EF72FE8C1921FA6EB2CFBFC578433F684B95FDEDAEFC9", + "data_hash": "E9734E910DFA6A6B806DC96E76E9D8D260E938E1E4B4B9D215C38422BF22AAB3", + "validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "next_validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "consensus_hash": "29C5629148426FB74676BE07F40F2ED79674A67F5833E4C9CCBF759C9372E99C", + "app_hash": "B48429AC69D3B1878D50F80D51EFD7085E9998B2570DF535A59E62FF1EE8F3DC", + "last_results_hash": "", + "evidence_hash": "", + "proposer_address": "9C17C94F7313BB4D6E064287BEEDE5D3888E8855" + } + }, + { + "block_id": { + "hash": "A5873B845F3CD93203D8E1306F756F3BD93E1E3828A2343FD9B898D4DD66BD55", + "parts": { + "total": "1", + "hash": "3E5A9C4ACC1C217DAEBD68E29B99B013603D2902BF974F9695FCA08E1E4E8867" + } + }, + "header": { + "version": { + "block": "10", + "app": "0" + }, + "chain_id": "cosmoshub-1", + "height": "6", + "time": "2019-03-13T23:08:00.786335199Z", + "num_txs": "0", + "total_txs": "0", + "last_block_id": { + "hash": "5EE242F6FF4EF8D2BD2EBD7428100848438E4D3FD0C9BF01109ACF2338352AD2", + "parts": { + "total": "1", + "hash": "21ED492846DFF04D65E445A56C38AAAE6151F0099B09C9FC9498EF154C46A8AD" + } + }, + "last_commit_hash": "86707BE9A4BDD123101A2695DA73E0A99D11D69CE6196642DCA964DCF2ABA1E2", + "data_hash": "", + "validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "next_validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "consensus_hash": "29C5629148426FB74676BE07F40F2ED79674A67F5833E4C9CCBF759C9372E99C", + "app_hash": "91B4964882D58F32E77EA93478AC8BBDCE905230718440DFFC48661D3B1350E2", + "last_results_hash": "", + "evidence_hash": "", + "proposer_address": "9C17C94F7313BB4D6E064287BEEDE5D3888E8855" + } + }, + { + "block_id": { + "hash": "5EE242F6FF4EF8D2BD2EBD7428100848438E4D3FD0C9BF01109ACF2338352AD2", + "parts": { + "total": "1", + "hash": "21ED492846DFF04D65E445A56C38AAAE6151F0099B09C9FC9498EF154C46A8AD" + } + }, + "header": { + "version": { + "block": "10", + "app": "0" + }, + "chain_id": "cosmoshub-1", + "height": "5", + "time": "2019-03-13T23:07:54.139565804Z", + "num_txs": "0", + "total_txs": "0", + "last_block_id": { + "hash": "C764D3F013CAFBF9CDCCB718DAFDF15C1B23C3D8ADC5EE811C6CC45E0711F04B", + "parts": { + "total": "1", + "hash": "2B51E708767543A566AA1FDBC279E06FB59C3F7658D2389DCAF389A95242E623" + } + }, + "last_commit_hash": "F5D6303B6B99788DA89612C4B6110AF7EE990390201F32A48A94A3F712D17A31", + "data_hash": "", + "validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "next_validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "consensus_hash": "29C5629148426FB74676BE07F40F2ED79674A67F5833E4C9CCBF759C9372E99C", + "app_hash": "380DA3186FE28DCDC765EEA59CE1A3C131709B5A631D70E9DDB9614EAACFA1E9", + "last_results_hash": "", + "evidence_hash": "", + "proposer_address": "2B19594437F1920B5AF6461FAB81AEC99790FEB1" + } + }, + { + "block_id": { + "hash": "C764D3F013CAFBF9CDCCB718DAFDF15C1B23C3D8ADC5EE811C6CC45E0711F04B", + "parts": { + "total": "1", + "hash": "2B51E708767543A566AA1FDBC279E06FB59C3F7658D2389DCAF389A95242E623" + } + }, + "header": { + "version": { + "block": "10", + "app": "0" + }, + "chain_id": "cosmoshub-1", + "height": "4", + "time": "2019-03-13T23:07:47.021138899Z", + "num_txs": "0", + "total_txs": "0", + "last_block_id": { + "hash": "9E0DF66B93354C474987ED69ADEDD5B2B1DC557A71A391B002E8BEE558C397F1", + "parts": { + "total": "1", + "hash": "529D1FFB543793F8B999AC4E801C8A14CA2DC6E873DC7759945C4B960CAC77DC" + } + }, + "last_commit_hash": "9FEA6AD9DAED8B067D59BB5A03CF81835D6FB719F463C32234028A06BEC40C43", + "data_hash": "", + "validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "next_validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "consensus_hash": "29C5629148426FB74676BE07F40F2ED79674A67F5833E4C9CCBF759C9372E99C", + "app_hash": "C3C0EC26BB6DA5FD0935354B4CB513ED9D0D9829E37C45A6F95A61CE73C40F55", + "last_results_hash": "", + "evidence_hash": "", + "proposer_address": "2199EAE894CA391FA82F01C2C614BFEB103D056C" + } + }, + { + "block_id": { + "hash": "9E0DF66B93354C474987ED69ADEDD5B2B1DC557A71A391B002E8BEE558C397F1", + "parts": { + "total": "1", + "hash": "529D1FFB543793F8B999AC4E801C8A14CA2DC6E873DC7759945C4B960CAC77DC" + } + }, + "header": { + "version": { + "block": "10", + "app": "0" + }, + "chain_id": "cosmoshub-1", + "height": "3", + "time": "2019-03-13T23:07:40.055158729Z", + "num_txs": "0", + "total_txs": "0", + "last_block_id": { + "hash": "F7888681D07FB5AEAF416D00C22E06CFD9A25DBD65519E97823F979206163CF1", + "parts": { + "total": "1", + "hash": "3D4BB2D494CF343011AF2ADE01D6CC88BE6FE26CE5756840931507554BF64F0B" + } + }, + "last_commit_hash": "6A875DCD138D0A76B63E8BD6FF7FD2A4209755B66B5C841BEE913DC69D17C8AB", + "data_hash": "", + "validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "next_validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "consensus_hash": "29C5629148426FB74676BE07F40F2ED79674A67F5833E4C9CCBF759C9372E99C", + "app_hash": "48731CCFBB44B3DD1BA1798D380FBF52F910B173BEB7C8C583FBC5DB80B55D84", + "last_results_hash": "", + "evidence_hash": "", + "proposer_address": "064CF05857B556FED63AC32821FF904312D0F2C8" + } + }, + { + "block_id": { + "hash": "F7888681D07FB5AEAF416D00C22E06CFD9A25DBD65519E97823F979206163CF1", + "parts": { + "total": "1", + "hash": "3D4BB2D494CF343011AF2ADE01D6CC88BE6FE26CE5756840931507554BF64F0B" + } + }, + "header": { + "version": { + "block": "10", + "app": "0" + }, + "chain_id": "cosmoshub-1", + "height": "2", + "time": "2019-03-13T23:07:33.018721592Z", + "num_txs": "0", + "total_txs": "0", + "last_block_id": { + "hash": "0D9BB9FA6EB9D64E80CF920EB917B1124F298B12C92BE7FD5328564C6D85D087", + "parts": { + "total": "1", + "hash": "E110DA5AEC7F6D8285F2F8A5CE732D8995E6B35C7B685BA819D3CBD903CB2330" + } + }, + "last_commit_hash": "D6359ED003DA81E68DD39588B40590F796B26A88F3B171B31758773D186233AE", + "data_hash": "", + "validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "next_validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "consensus_hash": "29C5629148426FB74676BE07F40F2ED79674A67F5833E4C9CCBF759C9372E99C", + "app_hash": "95EA0FDE227F235284FCCA4D0B6A281ABC3E15A14B62E4E01E4E03F8135C2AB3", + "last_results_hash": "", + "evidence_hash": "", + "proposer_address": "02A248C86C78ED6A824D510A8B7AA4C1D290D2DC" + } + }, + { + "block_id": { + "hash": "0D9BB9FA6EB9D64E80CF920EB917B1124F298B12C92BE7FD5328564C6D85D087", + "parts": { + "total": "1", + "hash": "E110DA5AEC7F6D8285F2F8A5CE732D8995E6B35C7B685BA819D3CBD903CB2330" + } + }, + "header": { + "version": { + "block": "10", + "app": "0" + }, + "chain_id": "cosmoshub-1", + "height": "1", + "time": "2019-03-13T23:00:00Z", + "num_txs": "0", + "total_txs": "0", + "last_block_id": { + "hash": "", + "parts": { + "total": "0", + "hash": "" + } + }, + "last_commit_hash": "", + "data_hash": "", + "validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "next_validators_hash": "50006FF803B9ED42EB8A3BAD658917FAD1472AD35C0560F05913F1144F07FEEB", + "consensus_hash": "29C5629148426FB74676BE07F40F2ED79674A67F5833E4C9CCBF759C9372E99C", + "app_hash": "", + "last_results_hash": "", + "evidence_hash": "", + "proposer_address": "064CF05857B556FED63AC32821FF904312D0F2C8" + } + } + ] + } +}