Skip to content

Commit

Permalink
test/e2e/ledger: add mainnet phases test
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Nov 13, 2024
1 parent 7a0770d commit 9d3c8a8
Show file tree
Hide file tree
Showing 18 changed files with 1,006 additions and 107 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/scripts/e2e.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
"e2e::wallet_tests::wallet_unencrypted_key_cmds": 1,
"e2e::ledger_tests::masp_txs_and_queries": 82,
"e2e::ledger_tests::test_genesis_chain_id_change": 35,
"e2e::ledger_tests::test_genesis_manipulation": 103
"e2e::ledger_tests::test_genesis_manipulation": 103,
"e2e::ledger_tests::test_mainnet_phases": 1057
}
10 changes: 9 additions & 1 deletion crates/test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ pub enum TestWasms {
TxInfiniteGuestGas,
TxInfiniteHostGas,
TxProposalCode,
TxProposalMaspRewards,
TxProposalIbcTokenInflation,
TxProposalMaspRewards,
TxProposalPhase2,
TxProposalPhase3,
TxProposalPhase4,
TxProposalPhase5,
TxProposalTokenGas,
TxReadStorageKey,
TxWriteStorageKey,
Expand All @@ -55,6 +59,10 @@ impl TestWasms {
TestWasms::TxInfiniteGuestGas => "tx_infinite_guest_gas.wasm",
TestWasms::TxInfiniteHostGas => "tx_infinite_host_gas.wasm",
TestWasms::TxProposalCode => "tx_proposal_code.wasm",
TestWasms::TxProposalPhase2 => "tx_proposal_phase2.wasm",
TestWasms::TxProposalPhase3 => "tx_proposal_phase3.wasm",
TestWasms::TxProposalPhase4 => "tx_proposal_phase4.wasm",
TestWasms::TxProposalPhase5 => "tx_proposal_phase5.wasm",
TestWasms::TxProposalMaspRewards => "tx_proposal_masp_reward.wasm",
TestWasms::TxProposalIbcTokenInflation => {
"tx_proposal_ibc_token_inflation.wasm"
Expand Down
88 changes: 62 additions & 26 deletions crates/tests/src/e2e/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ use std::str::FromStr;
use std::time::{Duration, Instant};
use std::{env, time};

use borsh::BorshDeserialize;
use color_eyre::eyre::Result;
use color_eyre::owo_colors::OwoColorize;
use data_encoding::HEXLOWER;
use escargot::CargoBuild;
use eyre::eyre;
use namada_apps_lib::cli::context::ENV_VAR_CHAIN_ID;
use namada_apps_lib::config::utils::convert_tm_addr_to_socket_addr;
use namada_apps_lib::config::{Config, TendermintMode};
use namada_apps_lib::wallet;
use namada_core::token::NATIVE_MAX_DECIMAL_PLACES;
use namada_sdk::address::Address;
use namada_sdk::chain::Epoch;
Expand Down Expand Up @@ -128,38 +127,32 @@ pub fn find_address(test: &Test, alias: impl AsRef<str>) -> Result<Address> {
Ok(address)
}

/// Find the balance of specific token for an account.
#[allow(dead_code)]
/// Find balance of specific token for an account.
pub fn find_balance(
test: &Test,
node: Who,
token: &Address,
owner: &Address,
token: &str,
owner: &str,
denom: Option<u8>,
balance_pattern: Option<&str>,
) -> Result<token::Amount> {
let ledger_address = get_actor_rpc(test, node);
let balance_key = token::storage_key::balance_key(token, owner);
let mut bytes = run!(
test,
Bin::Client,
&[
"query-bytes",
"--storage-key",
&balance_key.to_string(),
"--ledger-address",
&ledger_address,
],
Some(10)
)?;
let (_, matched) = bytes.exp_regex("Found data: 0x.*")?;
let rpc = get_actor_rpc(test, node);
let query_args = vec![
"balance", "--owner", &owner, "--token", &token, "--node", &rpc,
];
let mut client = run!(test, Bin::Client, query_args, Some(40))?;
let token = wallet::Alias::from(token).to_string();
let balance_pattern = balance_pattern.unwrap_or(&token);
let (_unread, matched) =
client.exp_regex(&format!(r"{balance_pattern}: [0-9.]+"))?;
dbg!(&matched);
let data_str = strip_trailing_newline(&matched)
.trim()
.rsplit_once(' ')
.unwrap()
.1[2..]
.to_string();
let amount =
token::Amount::try_from_slice(&HEXLOWER.decode(data_str.as_bytes())?)?;
bytes.assert_success();
.1;
dbg!(&data_str);
let amount = token::Amount::from_str(data_str, denom.unwrap_or(6)).unwrap();
Ok(amount)
}

Expand Down Expand Up @@ -726,3 +719,46 @@ pub fn get_gaia_gov_address(test: &Test) -> Result<String> {

Ok(matched.trim().to_string())
}

pub fn check_balance(
test: &Test,
owner: impl AsRef<str>,
token: impl AsRef<str>,
expected_amount: u64,
) -> Result<()> {
let rpc = get_actor_rpc(test, Who::Validator(0));

if owner.as_ref().starts_with("zvk") {
shielded_sync(test, owner.as_ref())?;
}

let query_args = vec![
"balance",
"--owner",
owner.as_ref(),
"--token",
token.as_ref(),
"--node",
&rpc,
];
let mut client = run!(test, Bin::Client, query_args, Some(40))?;
let expected =
format!("{}: {expected_amount}", token.as_ref().to_lowercase());
client.exp_string(&expected)?;
client.assert_success();
Ok(())
}

pub fn shielded_sync(test: &Test, viewing_key: impl AsRef<str>) -> Result<()> {
let rpc = get_actor_rpc(test, Who::Validator(0));
let tx_args = vec![
"shielded-sync",
"--viewing-keys",
viewing_key.as_ref(),
"--node",
&rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(120))?;
client.assert_success();
Ok(())
}
80 changes: 23 additions & 57 deletions crates/tests/src/e2e/ibc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ use prost::Message;
use setup::constants::*;
use sha2::{Digest, Sha256};

use super::helpers::check_balance;
use crate::e2e::helpers::{
epoch_sleep, epochs_per_year_from_min_duration, find_address,
find_gaia_address, get_actor_rpc, get_epoch, get_gaia_gov_address,
shielded_sync,
};
use crate::e2e::ledger_tests::{
start_namada_ledger_node_wait_wasm, write_json_file,
Expand Down Expand Up @@ -90,7 +92,8 @@ fn ibc_transfers() -> Result<()> {
.default_per_epoch_throughput_limit = Amount::max_signed();
setup::set_validators(1, genesis, base_dir, |_| 0, vec![])
};
let (ledger, gaia, test, test_gaia) = run_namada_gaia(update_genesis)?;
let (ledger, gaia, test, test_gaia) =
run_namada_gaia(update_genesis, None)?;
let _bg_ledger = ledger.background();
let _bg_gaia = gaia.background();

Expand Down Expand Up @@ -457,7 +460,8 @@ fn pgf_over_ibc() -> Result<()> {
.default_per_epoch_throughput_limit = Amount::max_signed();
setup::set_validators(1, genesis, base_dir, |_| 0, vec![])
};
let (ledger, gaia, test, test_gaia) = run_namada_gaia(update_genesis)?;
let (ledger, gaia, test, test_gaia) =
run_namada_gaia(update_genesis, None)?;
let _bg_ledger = ledger.background();
let _bg_gaia = gaia.background();

Expand Down Expand Up @@ -549,7 +553,8 @@ fn fee_payment_with_ibc_token() -> Result<()> {
genesis.parameters.parameters.gas_scale = 10_000_000;
setup::set_validators(1, genesis, base_dir, |_| 0, vec![])
};
let (ledger, gaia, test, test_gaia) = run_namada_gaia(update_genesis)?;
let (ledger, gaia, test, test_gaia) =
run_namada_gaia(update_genesis, None)?;
let _bg_ledger = ledger.background();
let _bg_gaia = gaia.background();

Expand Down Expand Up @@ -650,7 +655,8 @@ fn ibc_token_inflation() -> Result<()> {
.default_per_epoch_throughput_limit = Amount::max_signed();
setup::set_validators(1, genesis, base_dir, |_| 0, vec![])
};
let (ledger, gaia, test, test_gaia) = run_namada_gaia(update_genesis)?;
let (ledger, gaia, test, test_gaia) =
run_namada_gaia(update_genesis, None)?;
let _bg_ledger = ledger.background();
let _bg_gaia = gaia.background();

Expand Down Expand Up @@ -735,7 +741,8 @@ fn ibc_upgrade_client() -> Result<()> {
epochs_per_year_from_min_duration(1800);
setup::set_validators(1, genesis, base_dir, |_| 0, vec![])
};
let (ledger, gaia, test, test_gaia) = run_namada_gaia(update_genesis)?;
let (ledger, gaia, test, test_gaia) =
run_namada_gaia(update_genesis, None)?;
let _bg_ledger = ledger.background();
let _bg_gaia = gaia.background();

Expand Down Expand Up @@ -798,7 +805,8 @@ fn ibc_rate_limit() -> Result<()> {
.default_per_epoch_throughput_limit = Amount::from_u64(1_000_000);
setup::set_validators(1, genesis, base_dir, |_| 0, vec![])
};
let (ledger, gaia, test, test_gaia) = run_namada_gaia(update_genesis)?;
let (ledger, gaia, test, test_gaia) =
run_namada_gaia(update_genesis, None)?;
let _bg_ledger = ledger.background();
let _bg_gaia = gaia.background();

Expand Down Expand Up @@ -913,11 +921,12 @@ fn ibc_rate_limit() -> Result<()> {
Ok(())
}

fn run_namada_gaia(
pub fn run_namada_gaia(
mut update_genesis: impl FnMut(
templates::All<templates::Unvalidated>,
&Path,
) -> templates::All<templates::Unvalidated>,
gaia_user_balance: Option<u64>,
) -> Result<(NamadaCmd, NamadaCmd, Test, Test)> {
let test = setup::network(&mut update_genesis, None)?;

Expand All @@ -932,14 +941,14 @@ fn run_namada_gaia(
let ledger = start_namada_ledger_node_wait_wasm(&test, Some(0), Some(40))?;

// gaia
let test_gaia = setup_gaia()?;
let test_gaia = setup_gaia(gaia_user_balance)?;
let gaia = run_gaia(&test_gaia)?;
sleep(5);

Ok((ledger, gaia, test, test_gaia))
}

fn create_channel_with_hermes(
pub fn create_channel_with_hermes(
test_a: &Test,
test_b: &Test,
) -> Result<(ChannelId, ChannelId)> {
Expand Down Expand Up @@ -980,7 +989,7 @@ fn get_channel_ids_from_hermes_output(
Ok((channel_id_a, channel_id_b))
}

fn run_hermes(test: &Test) -> Result<NamadaCmd> {
pub fn run_hermes(test: &Test) -> Result<NamadaCmd> {
let args = ["start"];
let mut hermes = run_hermes_cmd(test, args, Some(40))?;
hermes.exp_string("Hermes has started")?;
Expand All @@ -1005,7 +1014,7 @@ fn run_gaia(test: &Test) -> Result<NamadaCmd> {
Ok(gaia)
}

fn wait_for_packet_relay(
pub fn wait_for_packet_relay(
port_id: &PortId,
channel_id: &ChannelId,
test: &Test,
Expand Down Expand Up @@ -1590,7 +1599,7 @@ fn submit_votes(test: &Test) -> Result<()> {
}

#[allow(clippy::too_many_arguments)]
fn transfer_from_gaia(
pub fn transfer_from_gaia(
test: &Test,
sender: impl AsRef<str>,
receiver: impl AsRef<str>,
Expand Down Expand Up @@ -1691,43 +1700,14 @@ fn query_height(test: &Test) -> Result<Height> {
Ok(Height::new(0, status.sync_info.latest_block_height.into()).unwrap())
}

fn check_balance(
test: &Test,
owner: impl AsRef<str>,
token: impl AsRef<str>,
expected_amount: u64,
) -> Result<()> {
let rpc = get_actor_rpc(test, Who::Validator(0));

if owner.as_ref().starts_with("zvk") {
shielded_sync(test, owner.as_ref())?;
}

let query_args = vec![
"balance",
"--owner",
owner.as_ref(),
"--token",
token.as_ref(),
"--node",
&rpc,
];
let mut client = run!(test, Bin::Client, query_args, Some(40))?;
let expected =
format!("{}: {expected_amount}", token.as_ref().to_lowercase());
client.exp_string(&expected)?;
client.assert_success();
Ok(())
}

fn get_gaia_denom_hash(denom: impl AsRef<str>) -> String {
let mut hasher = Sha256::new();
hasher.update(denom.as_ref());
let hash = hasher.finalize();
format!("ibc/{hash:X}")
}

fn check_gaia_balance(
pub fn check_gaia_balance(
test: &Test,
owner: impl AsRef<str>,
denom: impl AsRef<str>,
Expand Down Expand Up @@ -1780,23 +1760,9 @@ fn check_inflated_balance(
Ok(())
}

fn shielded_sync(test: &Test, viewing_key: impl AsRef<str>) -> Result<()> {
let rpc = get_actor_rpc(test, Who::Validator(0));
let tx_args = vec![
"shielded-sync",
"--viewing-keys",
viewing_key.as_ref(),
"--node",
&rpc,
];
let mut client = run!(test, Bin::Client, tx_args, Some(120))?;
client.assert_success();
Ok(())
}

/// Get IBC shielding data for the following IBC transfer from the destination
/// chain
fn gen_ibc_shielding_data(
pub fn gen_ibc_shielding_data(
dst_test: &Test,
receiver: impl AsRef<str>,
token: impl AsRef<str>,
Expand Down
Loading

0 comments on commit 9d3c8a8

Please sign in to comment.