Skip to content

Commit

Permalink
Merge branch 'tomas/flakey-e2e-tests' (#661)
Browse files Browse the repository at this point in the history
* tomas/flakey-e2e-tests:
  test/e2e/helpers: add a helper to query and parse block height
  changelog: add #658
  client: add a block query to print hash, height and time of a block
  • Loading branch information
tzemanovic committed Oct 26, 2022
2 parents a8b577c + ea2d533 commit a5f8d57
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/features/658-add-client-block-query.md
Original file line number Diff line number Diff line change
@@ -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))
3 changes: 3 additions & 0 deletions apps/src/bin/anoma-client/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 =
Expand All @@ -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)
Expand Down Expand Up @@ -283,6 +286,7 @@ pub mod cmds {
Unbond(Unbond),
Withdraw(Withdraw),
QueryEpoch(QueryEpoch),
QueryBlock(QueryBlock),
QueryBalance(QueryBalance),
QueryBonds(QueryBonds),
QueryVotingPower(QueryVotingPower),
Expand Down Expand Up @@ -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<Self> {
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::<args::Query>()
}
}

#[derive(Clone, Debug)]
pub struct QueryBalance(pub args::QueryBalance);

Expand Down
15 changes: 15 additions & 0 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ pub async fn query_epoch(args: args::Query) -> Epoch {
epoch
}

/// 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();
Expand Down
60 changes: 59 additions & 1 deletion tests/src/e2e/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -148,6 +149,63 @@ pub fn get_epoch(test: &Test, ledger_address: &str) -> Result<Epoch> {
Ok(Epoch(epoch))
}

/// Get the last committed block height.
pub fn get_height(test: &Test, ledger_address: &str) -> Result<u64> {
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",
Expand Down
51 changes: 44 additions & 7 deletions tests/src/e2e/ledger_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -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))?;
Expand Down Expand Up @@ -1847,7 +1854,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));
Expand Down Expand Up @@ -1878,12 +1885,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(())
}
Expand Down

0 comments on commit a5f8d57

Please sign in to comment.