Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Oct 1, 2024
1 parent 394d4d9 commit e4d423c
Showing 1 changed file with 147 additions and 22 deletions.
169 changes: 147 additions & 22 deletions crates/tests/src/e2e/ibc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use core::str::FromStr;
use core::time::Duration;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use color_eyre::eyre::Result;
Expand All @@ -33,7 +34,7 @@ use namada_sdk::ibc::core::host::types::identifiers::{
use namada_sdk::ibc::primitives::proto::Any;
use namada_sdk::ibc::storage::*;
use namada_sdk::ibc::trace::ibc_token;
use namada_sdk::token::Amount;
use namada_sdk::token::{Amount, DenominatedAmount};
use namada_test_utils::TestWasms;
use prost::Message;
use setup::constants::*;
Expand All @@ -58,26 +59,31 @@ const IBC_REFUND_TARGET_ALIAS: &str = "ibc-refund-target";
const IBC_CLINET_ID: &str = "07-tendermint-0";
const UPGRADED_CHAIN_ID: &str = "upgraded-chain";

/// IBC transfer tests:
/// 1. Transparent transfers
/// - Namada -> Gaia -> Namada
/// - Gaia -> Namada -> Gaia
/// 2. Invalid transfers
/// 3. Shielding/Unshielding transfers
/// - Gaia -> Namada -> (shielded transfer) -> Namada -> Gaia
/// 4. Shielding transfer the received token back to a shielded account on
/// Namada
/// 5. Refunding when transfer failure
/// - Ack with an error (invalid receiver)
/// - Timeout
/// - When unshielding transfer failure,
/// - Mint the IBC token for the refund
/// - Unescrow the token for the refund
/// 6. Malformed shielded actions
/// - Missing memo
/// - Wrong memo
// IBC transfer tests:
// 1. Transparent transfers
// - Namada -> Gaia -> Namada
// - Gaia -> Namada -> Gaia
// 2. Invalid transfers
// 3. Shielding/Unshielding transfers
// - Gaia -> Namada -> (shielded transfer) -> Namada -> Gaia
// 4. Shielding transfer the received token back to a shielded account on
// Namada
// 5. Refunding when transfer failure
// - Ack with an error (invalid receiver)
// - Timeout
// - When unshielding transfer failure,
// - Mint the IBC token for the refund
// - Unescrow the token for the refund
// 6. Malformed shielded actions
// - Missing memo
// - Wrong memo
// 7. Transparent transfer in Namada paying fees with the IBC token
#[test]
fn ibc_transfers() -> Result<()> {
const IBC_TOKEN_DENOM: u8 = 6;
let port_id_namada = "transfer".parse().unwrap();
let port_id_gaia = "transfer".parse().unwrap();

let update_genesis =
|mut genesis: templates::All<templates::Unvalidated>, base_dir: &_| {
genesis.parameters.parameters.epochs_per_year =
Expand All @@ -88,15 +94,27 @@ fn ibc_transfers() -> Result<()> {
.parameters
.ibc_params
.default_per_epoch_throughput_limit = Amount::max_signed();
// Whitelist the ibc token for gas payment
let whitelisted_token =
format!("{port_id_namada}/channel-0/{GAIA_COIN}");
genesis.tokens.token.insert(
whitelisted_token.clone().into(),
templates::TokenConfig {
denom: IBC_TOKEN_DENOM.into(),
masp_params: None,
},
);
genesis.parameters.parameters.minimum_gas_price.insert(
whitelisted_token.into(),
DenominatedAmount::new(1.into(), IBC_TOKEN_DENOM.into()),
);
setup::set_validators(1, genesis, base_dir, |_| 0, vec![])
};
let (ledger, gaia, test, test_gaia) = run_namada_gaia(update_genesis)?;
let _bg_ledger = ledger.background();
let _bg_gaia = gaia.background();

setup_hermes(&test, &test_gaia)?;
let port_id_namada = "transfer".parse().unwrap();
let port_id_gaia = "transfer".parse().unwrap();
let (channel_id_namada, channel_id_gaia) =
create_channel_with_hermes(&test, &test_gaia)?;

Expand Down Expand Up @@ -234,6 +252,7 @@ fn ibc_transfers() -> Result<()> {
&ibc_denom_on_namada,
50,
ALBERT_KEY,
&[],
)?;
check_balance(&test, AA_VIEWING_KEY, &ibc_denom_on_namada, 50)?;
check_balance(&test, AB_VIEWING_KEY, &ibc_denom_on_namada, 50)?;
Expand Down Expand Up @@ -437,6 +456,22 @@ fn ibc_transfers() -> Result<()> {
check_balance(&test, AA_VIEWING_KEY, &ibc_denom_on_namada, 40)?;
check_gaia_balance(&test_gaia, GAIA_USER, GAIA_COIN, 810)?;

// 7. Transparent transfer in Namada paying gas with samoleans
transfer_on_chain(
&test,
"transparent-transfer",
ALBERT,
BERTHA,
NAM,
50,
ALBERT_KEY,
&["--gas-token", &ibc_denom_on_namada, "--gas-price", "100"],
)?;
check_balance(&test, ALBERT, NAM, 1_999_950)?;
check_balance(&test, BERTHA, NAM, 2_000_050)?;
check_balance(&test, ALBERT_KEY, &ibc_denom_on_namada, 475)?;
check_balance(&test, "validator-0", &ibc_denom_on_namada, 25)?;

Ok(())
}

Expand Down Expand Up @@ -479,6 +514,7 @@ fn pgf_over_ibc() -> Result<()> {
NAM,
100,
ALBERT_KEY,
&[],
)?;

// Proposal on Namada
Expand Down Expand Up @@ -526,6 +562,92 @@ fn pgf_over_ibc() -> Result<()> {
Ok(())
}

//FIXME: remove
// //FIXME: add this to the e2e.json file
// //FIXME: try to merge this test with the previous one?
// // Test fee payment with an ibc token
// #[test]
// fn fee_payment_with_ibc_token() -> Result<()> {
// const IBC_TOKEN_DENOM: u8 = 6;
// let port_id_gaia = "transfer".parse().unwrap();
// let port_id_namada = "transfer";

// let update_genesis =
// |mut genesis: templates::All<templates::Unvalidated>, base_dir: &_| {
// genesis.parameters.ibc_params.default_mint_limit =
// Amount::max_signed();
// genesis
// .parameters
// .ibc_params
// .default_per_epoch_throughput_limit = Amount::max_signed();
// let whitelisted_token =
// format!("{port_id_namada}/channel-0/{GAIA_COIN}");
// genesis.tokens.token.insert(
// whitelisted_token.clone().into(),
// templates::TokenConfig {
// denom: IBC_TOKEN_DENOM.into(),
// masp_params: None,
// },
// );
// genesis.parameters.parameters.minimum_gas_price.insert(
// whitelisted_token.into(),
// DenominatedAmount::new(1.into(), IBC_TOKEN_DENOM.into()),
// );

// setup::set_validators(1, genesis, base_dir, |_| 0, vec![])
// };
// let (ledger, gaia, test, test_gaia) = run_namada_gaia(update_genesis)?;
// let _bg_ledger = ledger.background();
// let _bg_gaia = gaia.background();

// setup_hermes(&test, &test_gaia)?;
// let (channel_id_namada, channel_id_gaia) =
// create_channel_with_hermes(&test, &test_gaia)?;
// let ibc_denom_on_namada =
// format!("{port_id_namada}/{channel_id_namada}/{GAIA_COIN}");

// // Start relaying
// let hermes = run_hermes(&test)?;
// let _bg_hermes = hermes.background();

// // Transfer 500 samoleans from Gaia to Namada
// let namada_receiver = find_address(&test, ALBERT_KEY)?.to_string();
// transfer_from_gaia(
// &test_gaia,
// GAIA_USER,
// &namada_receiver,
// GAIA_COIN,
// 500,
// &port_id_gaia,
// &channel_id_gaia,
// None,
// None,
// )?;
// wait_for_packet_relay(&port_id_gaia, &channel_id_gaia, &test)?;

// // Check the token on Namada
// check_balance(&test, ALBERT_KEY, &ibc_denom_on_namada, 500)?;
// check_gaia_balance(&test_gaia, GAIA_USER, GAIA_COIN, 500)?;

// // Transparent transfer in Namada paying gas with samoleans
// transfer_on_chain(
// &test,
// "transparent-transfer",
// ALBERT,
// BERTHA,
// NAM,
// 50,
// ALBERT_KEY,
// &["--gas-token", &ibc_denom_on_namada, "--gas-price", "100"],
// )?;
// check_balance(&test, ALBERT, NAM, 1_999_950)?;
// check_balance(&test, BERTHA, NAM, 2_000_050)?;
// check_balance(&test, ALBERT_KEY, &ibc_denom_on_namada, 475)?;
// check_balance(&test, "validator-0", &ibc_denom_on_namada, 25)?;

// Ok(())
// }

/// IBC token inflation test
/// - Propose the inflation of an IBC token received from Gaia
/// - Shielding transfer of the token from Gaia
Expand Down Expand Up @@ -1052,10 +1174,12 @@ fn transfer_on_chain(
token: impl AsRef<str>,
amount: u64,
signer: impl AsRef<str>,
//FIXME: review this arg
extra_args: &[&str],
) -> Result<()> {
let rpc = get_actor_rpc(test, Who::Validator(0));
let amount = amount.to_string();
let tx_args = apply_use_device(vec![
let mut tx_args = apply_use_device(vec![
kind.as_ref(),
"--source",
sender.as_ref(),
Expand All @@ -1070,6 +1194,7 @@ fn transfer_on_chain(
"--node",
&rpc,
]);
tx_args.extend_from_slice(extra_args);
let mut client = run!(test, Bin::Client, tx_args, Some(120))?;
client.exp_string(TX_APPLIED_SUCCESS)?;
client.assert_success();
Expand Down

0 comments on commit e4d423c

Please sign in to comment.