From f787667bd91a8e1d0de0d3029ff08a1b3d875317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Thu, 20 Oct 2022 15:22:18 +0200 Subject: [PATCH 1/3] client: add a block query to print hash, height and time of a block --- apps/src/bin/anoma-client/cli.rs | 3 +++ apps/src/lib/cli.rs | 23 +++++++++++++++++++++++ apps/src/lib/client/rpc.rs | 15 +++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/apps/src/bin/anoma-client/cli.rs b/apps/src/bin/anoma-client/cli.rs index b87cdb5c661..cadc215bd46 100644 --- a/apps/src/bin/anoma-client/cli.rs +++ b/apps/src/bin/anoma-client/cli.rs @@ -52,6 +52,9 @@ pub async fn main() -> Result<()> { Sub::QueryEpoch(QueryEpoch(args)) => { rpc::query_epoch(args).await; } + Sub::QueryBlock(QueryBlock(args)) => { + rpc::query_block(args).await; + } Sub::QueryBalance(QueryBalance(args)) => { rpc::query_balance(ctx, args).await; } diff --git a/apps/src/lib/cli.rs b/apps/src/lib/cli.rs index 8e4c7f78c94..f31d84b526e 100644 --- a/apps/src/lib/cli.rs +++ b/apps/src/lib/cli.rs @@ -167,6 +167,7 @@ pub mod cmds { .subcommand(Withdraw::def().display_order(2)) // Queries .subcommand(QueryEpoch::def().display_order(3)) + .subcommand(QueryBlock::def().display_order(3)) .subcommand(QueryBalance::def().display_order(3)) .subcommand(QueryBonds::def().display_order(3)) .subcommand(QueryVotingPower::def().display_order(3)) @@ -198,6 +199,7 @@ pub mod cmds { let unbond = Self::parse_with_ctx(matches, Unbond); let withdraw = Self::parse_with_ctx(matches, Withdraw); let query_epoch = Self::parse_with_ctx(matches, QueryEpoch); + let query_block = Self::parse_with_ctx(matches, QueryBlock); let query_balance = Self::parse_with_ctx(matches, QueryBalance); let query_bonds = Self::parse_with_ctx(matches, QueryBonds); let query_voting_power = @@ -224,6 +226,7 @@ pub mod cmds { .or(unbond) .or(withdraw) .or(query_epoch) + .or(query_block) .or(query_balance) .or(query_bonds) .or(query_voting_power) @@ -283,6 +286,7 @@ pub mod cmds { Unbond(Unbond), Withdraw(Withdraw), QueryEpoch(QueryEpoch), + QueryBlock(QueryBlock), QueryBalance(QueryBalance), QueryBonds(QueryBonds), QueryVotingPower(QueryVotingPower), @@ -936,6 +940,25 @@ pub mod cmds { } } + #[derive(Clone, Debug)] + pub struct QueryBlock(pub args::Query); + + impl SubCmd for QueryBlock { + const CMD: &'static str = "block"; + + fn parse(matches: &ArgMatches) -> Option { + matches + .subcommand_matches(Self::CMD) + .map(|matches| QueryBlock(args::Query::parse(matches))) + } + + fn def() -> App { + App::new(Self::CMD) + .about("Query the last committed block.") + .add_args::() + } + } + #[derive(Clone, Debug)] pub struct QueryBalance(pub args::QueryBalance); diff --git a/apps/src/lib/client/rpc.rs b/apps/src/lib/client/rpc.rs index 6c1e3fb5f31..7eb580d2129 100644 --- a/apps/src/lib/client/rpc.rs +++ b/apps/src/lib/client/rpc.rs @@ -72,6 +72,21 @@ pub async fn query_epoch(args: args::Query) -> Epoch { cli::safe_exit(1) } +/// Query the last committed block +pub async fn query_block( + args: args::Query, +) -> tendermint_rpc::endpoint::block::Response { + let client = HttpClient::new(args.ledger_address).unwrap(); + let response = client.latest_block().await.unwrap(); + println!( + "Last committed block ID: {}, height: {}, time: {}", + response.block_id, + response.block.header.height, + response.block.header.time + ); + response +} + /// Query the raw bytes of given storage key pub async fn query_raw_bytes(_ctx: Context, args: args::QueryRawBytes) { let client = HttpClient::new(args.query.ledger_address).unwrap(); From ca3608dd528443e6e3c93932aab9559dcb786a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Thu, 20 Oct 2022 15:26:48 +0200 Subject: [PATCH 2/3] changelog: add #658 --- .changelog/unreleased/features/658-add-client-block-query.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/features/658-add-client-block-query.md diff --git a/.changelog/unreleased/features/658-add-client-block-query.md b/.changelog/unreleased/features/658-add-client-block-query.md new file mode 100644 index 00000000000..29584d3a0fc --- /dev/null +++ b/.changelog/unreleased/features/658-add-client-block-query.md @@ -0,0 +1,2 @@ +- Client: Add a command to query the last committed block's hash, height and + timestamp. ([#658](https://github.com/anoma/namada/issues/658)) From ea2d533065106118931eec9f740ca9477c7a65ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Thu, 20 Oct 2022 15:35:01 +0200 Subject: [PATCH 3/3] test/e2e/helpers: add a helper to query and parse block height --- tests/src/e2e/helpers.rs | 60 ++++++++++++++++++++++++++++++++++- tests/src/e2e/ledger_tests.rs | 51 +++++++++++++++++++++++++---- 2 files changed, 103 insertions(+), 8 deletions(-) diff --git a/tests/src/e2e/helpers.rs b/tests/src/e2e/helpers.rs index 705c8227609..d0dd60a6885 100644 --- a/tests/src/e2e/helpers.rs +++ b/tests/src/e2e/helpers.rs @@ -3,6 +3,7 @@ use std::path::Path; use std::process::Command; use std::str::FromStr; +use std::time::{Duration, Instant}; use std::{env, time}; use color_eyre::eyre::Result; @@ -14,7 +15,7 @@ use namada::types::key::*; use namada::types::storage::Epoch; use namada_apps::config::{Config, TendermintMode}; -use super::setup::{Test, ENV_VAR_DEBUG, ENV_VAR_USE_PREBUILT_BINARIES}; +use super::setup::{sleep, Test, ENV_VAR_DEBUG, ENV_VAR_USE_PREBUILT_BINARIES}; use crate::e2e::setup::{Bin, Who, APPS_PACKAGE}; use crate::run; @@ -148,6 +149,63 @@ pub fn get_epoch(test: &Test, ledger_address: &str) -> Result { Ok(Epoch(epoch)) } +/// Get the last committed block height. +pub fn get_height(test: &Test, ledger_address: &str) -> Result { + let mut find = run!( + test, + Bin::Client, + &["block", "--ledger-address", ledger_address], + Some(10) + )?; + let (unread, matched) = find.exp_regex("Last committed block ID: .*")?; + // Expected `matched` string is e.g.: + // + // ``` + // Last committed block F10B5E77F972F68CA051D289474B6E75574B446BF713A7B7B71D7ECFC61A3B21, height: 4, time: 2022-10-20T10:52:28.828745Z + // ``` + let height_str = strip_trailing_newline(&matched) + .trim() + // Find the height part ... + .split_once("height: ") + .unwrap() + // ... take what's after it ... + .1 + // ... find the next comma ... + .rsplit_once(',') + .unwrap() + // ... and take what's before it. + .0; + u64::from_str(height_str).map_err(|e| { + eyre!(format!( + "Height parsing failed from {} trimmed from {}, Error: \ + {}\n\nUnread output: {}", + height_str, matched, e, unread + )) + }) +} + +/// Sleep until the given height is reached or panic when time out is reached +/// before the height +pub fn wait_for_block_height( + test: &Test, + ledger_address: &str, + height: u64, + timeout_secs: u64, +) -> Result<()> { + let start = Instant::now(); + let loop_timeout = Duration::new(timeout_secs, 0); + loop { + let current = get_height(test, ledger_address)?; + if current >= height { + break Ok(()); + } + if Instant::now().duration_since(start) > loop_timeout { + panic!("Timed out waiting for height {height}, current {current}"); + } + sleep(1); + } +} + pub fn generate_bin_command(bin_name: &str, manifest_path: &Path) -> Command { let use_prebuilt_binaries = match env::var(ENV_VAR_USE_PREBUILT_BINARIES) { Ok(var) => var.to_ascii_lowercase() != "false", diff --git a/tests/src/e2e/ledger_tests.rs b/tests/src/e2e/ledger_tests.rs index a998844764e..c3e94b9238b 100644 --- a/tests/src/e2e/ledger_tests.rs +++ b/tests/src/e2e/ledger_tests.rs @@ -23,6 +23,7 @@ use namada_apps::config::genesis::genesis_config::{ use serde_json::json; use setup::constants::*; +use super::helpers::{get_height, wait_for_block_height}; use super::setup::get_all_wasms_hashes; use crate::e2e::helpers::{ find_address, find_voting_power, get_actor_rpc, get_epoch, @@ -123,6 +124,16 @@ fn test_node_connectivity() -> Result<()> { let _bg_validator_0 = validator_0.background(); let _bg_validator_1 = validator_1.background(); + let validator_0_rpc = get_actor_rpc(&test, &Who::Validator(0)); + let validator_1_rpc = get_actor_rpc(&test, &Who::Validator(1)); + let non_validator_rpc = get_actor_rpc(&test, &Who::NonValidator); + + // Find the block height on the validator + let after_tx_height = get_height(&test, &validator_0_rpc)?; + + // Wait for the non-validator to be synced to at least the same height + wait_for_block_height(&test, &non_validator_rpc, after_tx_height, 10)?; + let query_balance_args = |ledger_rpc| { vec![ "balance", @@ -134,10 +145,6 @@ fn test_node_connectivity() -> Result<()> { ledger_rpc, ] }; - - let validator_0_rpc = get_actor_rpc(&test, &Who::Validator(0)); - let validator_1_rpc = get_actor_rpc(&test, &Who::Validator(1)); - let non_validator_rpc = get_actor_rpc(&test, &Who::NonValidator); for ledger_rpc in &[validator_0_rpc, validator_1_rpc, non_validator_rpc] { let mut client = run!(test, Bin::Client, query_balance_args(ledger_rpc), Some(40))?; @@ -1848,7 +1855,7 @@ fn test_genesis_validators() -> Result<()> { let bg_validator_0 = validator_0.background(); let bg_validator_1 = validator_1.background(); - let bg_non_validator = non_validator.background(); + let _bg_non_validator = non_validator.background(); // 4. Submit a valid token transfer tx let validator_one_rpc = get_actor_rpc(&test, &Who::Validator(0)); @@ -1879,12 +1886,42 @@ fn test_genesis_validators() -> Result<()> { // 3. Check that all the nodes processed the tx with the same result let mut validator_0 = bg_validator_0.foreground(); let mut validator_1 = bg_validator_1.foreground(); - let mut non_validator = bg_non_validator.foreground(); let expected_result = "all VPs accepted transaction"; + // We cannot check this on non-validator node as it might sync without + // applying the tx itself, but its state should be the same, checked below. validator_0.exp_string(expected_result)?; validator_1.exp_string(expected_result)?; - non_validator.exp_string(expected_result)?; + let _bg_validator_0 = validator_0.background(); + let _bg_validator_1 = validator_1.background(); + + let validator_0_rpc = get_actor_rpc(&test, &Who::Validator(0)); + let validator_1_rpc = get_actor_rpc(&test, &Who::Validator(1)); + let non_validator_rpc = get_actor_rpc(&test, &Who::NonValidator); + + // Find the block height on the validator + let after_tx_height = get_height(&test, &validator_0_rpc)?; + + // Wait for the non-validator to be synced to at least the same height + wait_for_block_height(&test, &non_validator_rpc, after_tx_height, 10)?; + + let query_balance_args = |ledger_rpc| { + vec![ + "balance", + "--owner", + validator_1_alias, + "--token", + XAN, + "--ledger-address", + ledger_rpc, + ] + }; + for ledger_rpc in &[validator_0_rpc, validator_1_rpc, non_validator_rpc] { + let mut client = + run!(test, Bin::Client, query_balance_args(ledger_rpc), Some(40))?; + client.exp_string("XAN: 1000000000010.1")?; + client.assert_success(); + } Ok(()) }