From 8fdd83aaddd39bb7d21de46e9698e11c50cd61da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 2 Feb 2024 10:32:48 +0000 Subject: [PATCH 1/2] core: prevent from using addresses for testing in non-test code --- crates/apps/src/lib/bench_utils.rs | 6 +- crates/apps/src/lib/config/genesis.rs | 2 +- .../src/lib/config/genesis/transactions.rs | 23 +-- crates/apps/src/lib/node/ledger/shell/mod.rs | 29 ++-- .../lib/node/ledger/shell/prepare_proposal.rs | 17 ++- .../lib/node/ledger/shell/process_proposal.rs | 9 +- .../apps/src/lib/node/ledger/storage/mod.rs | 20 +-- crates/apps/src/lib/wallet/defaults.rs | 5 +- crates/benches/host_env.rs | 2 +- crates/benches/native_vps.rs | 2 +- crates/benches/process_wrapper.rs | 4 +- crates/benches/vps.rs | 12 +- crates/core/src/address.rs | 141 +++++++++--------- crates/core/src/eth_bridge_pool.rs | 3 +- .../transactions/ethereum_events/events.rs | 4 +- crates/ethereum_bridge/src/storage/mod.rs | 2 +- .../src/storage/wrapped_erc20s.rs | 3 +- crates/ethereum_bridge/src/test_utils.rs | 3 +- crates/merkle_tree/Cargo.toml | 2 + crates/merkle_tree/src/eth_bridge_pool.rs | 3 +- .../ethereum_bridge/bridge_pool_vp.rs | 6 +- .../ledger/native_vp/ethereum_bridge/vp.rs | 3 +- crates/namada/src/ledger/native_vp/ibc/mod.rs | 4 +- .../namada/src/ledger/native_vp/multitoken.rs | 3 +- crates/sdk/src/eth_bridge/bridge_pool.rs | 16 +- crates/sdk/src/queries/shell/eth_bridge.rs | 3 +- crates/shielded_token/src/conversion.rs | 14 +- crates/state/src/lib.rs | 2 +- crates/storage/src/lib.rs | 2 +- crates/tests/src/native_vp/eth_bridge_pool.rs | 2 +- crates/trans_token/src/storage.rs | 2 +- crates/tx/src/data/mod.rs | 2 +- wasm/wasm_source/src/vp_implicit.rs | 12 +- wasm/wasm_source/src/vp_user.rs | 16 +- 34 files changed, 199 insertions(+), 180 deletions(-) diff --git a/crates/apps/src/lib/bench_utils.rs b/crates/apps/src/lib/bench_utils.rs index 1db7a07411..9a242a920c 100644 --- a/crates/apps/src/lib/bench_utils.rs +++ b/crates/apps/src/lib/bench_utils.rs @@ -350,7 +350,7 @@ impl BenchShell { pub fn generate_ibc_transfer_tx(&self) -> Tx { let token = PrefixedCoin { - denom: address::nam().to_string().parse().unwrap(), + denom: address::testing::nam().to_string().parse().unwrap(), amount: Amount::native_whole(1000) .to_string_native() .split('.') @@ -1005,7 +1005,7 @@ impl BenchShieldedCtx { &namada, &source, &target, - &address::nam(), + &address::testing::nam(), denominated_amount, ), ) @@ -1034,7 +1034,7 @@ impl BenchShieldedCtx { Transfer { source: source.effective_address(), target: target.effective_address(), - token: address::nam(), + token: address::testing::nam(), amount: DenominatedAmount::native(amount), key: None, shielded: shielded_section_hash, diff --git a/crates/apps/src/lib/config/genesis.rs b/crates/apps/src/lib/config/genesis.rs index 074d17966a..321f48a6ef 100644 --- a/crates/apps/src/lib/config/genesis.rs +++ b/crates/apps/src/lib/config/genesis.rs @@ -313,7 +313,7 @@ pub fn make_dev_genesis( use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::time::Duration; - use namada::core::address::wnam; + use namada::core::address::testing::wnam; use namada::core::chain::ChainIdPrefix; use namada::core::ethereum_events::EthAddress; use namada::core::key::*; diff --git a/crates/apps/src/lib/config/genesis/transactions.rs b/crates/apps/src/lib/config/genesis/transactions.rs index 8d3e6facfc..d53203c705 100644 --- a/crates/apps/src/lib/config/genesis/transactions.rs +++ b/crates/apps/src/lib/config/genesis/transactions.rs @@ -11,7 +11,7 @@ use ledger_namada_rs::NamadaApp; use ledger_transport_hid::hidapi::HidApi; use ledger_transport_hid::TransportNativeHID; use namada::account::AccountPublicKeysMap; -use namada::core::address::{nam, Address, EstablishedAddress}; +use namada::core::address::{Address, EstablishedAddress}; use namada::core::chain::ChainId; use namada::core::dec::Dec; use namada::core::key::{ @@ -81,7 +81,7 @@ fn get_tx_args(use_device: bool) -> TxArgs { wallet_alias_force: false, fee_amount: None, wrapper_fee_payer: None, - fee_token: nam(), + fee_token: genesis_fee_token_address(), fee_unshield: None, gas_limit: Default::default(), expiration: None, @@ -120,13 +120,13 @@ fn get_tx_to_sign(tag: impl AsRef, data: impl BorshSerialize) -> Tx { salt: [0; 8], data: data.serialize_to_vec(), }); - let pk = get_sentinel_pubkey(); + let fee_payer = genesis_fee_payer_pk(); tx.add_wrapper( Fee { amount_per_gas_unit: DenominatedAmount::native(0.into()), - token: Address::from(&pk), + token: genesis_fee_token_address(), }, - pk, + fee_payer, Default::default(), Default::default(), None, @@ -134,12 +134,17 @@ fn get_tx_to_sign(tag: impl AsRef, data: impl BorshSerialize) -> Tx { tx } -/// Get a dummy public key. +/// Get a dummy public key for a fee payer - there are no fees for genesis tx #[inline] -fn get_sentinel_pubkey() -> common::PublicKey { +fn genesis_fee_payer_pk() -> common::PublicKey { common::SecretKey::Ed25519(ed25519::SigScheme::from_bytes([0; 32])).ref_to() } +/// Dummy genesis fee token address - there are no fees for genesis tx +fn genesis_fee_token_address() -> Address { + Address::from(&genesis_fee_payer_pk()) +} + pub struct GenesisValidatorData { pub address: EstablishedAddress, pub commission_rate: Dec, @@ -741,7 +746,7 @@ impl Signed { account_public_keys_map: Some(pks.iter().cloned().collect()), public_keys: pks.clone(), threshold, - fee_payer: get_sentinel_pubkey(), + fee_payer: genesis_fee_payer_pk(), }; let mut tx = self.data.tx_to_sign(); @@ -769,7 +774,7 @@ impl Signed { _parts: HashSet, _user: (), ) -> Result { - if pubkey == get_sentinel_pubkey() { + if pubkey == genesis_fee_payer_pk() { Ok(tx) } else { Err(namada_sdk::error::Error::Other(format!( diff --git a/crates/apps/src/lib/node/ledger/shell/mod.rs b/crates/apps/src/lib/node/ledger/shell/mod.rs index 977387b280..d0d12ac014 100644 --- a/crates/apps/src/lib/node/ledger/shell/mod.rs +++ b/crates/apps/src/lib/node/ledger/shell/mod.rs @@ -410,16 +410,24 @@ where std::fs::create_dir(&base_dir) .expect("Creating directory for Namada should not fail"); } - let native_token = if cfg!(feature = "integration") - || (!cfg!(test) && !cfg!(feature = "benches")) - { + + // For all tests except integration use hard-coded native token addr ... + #[cfg(all( + any(test, feature = "testing", feature = "benches"), + not(feature = "integration"), + ))] + let native_token = address::testing::nam(); + // ... Otherwise, look it up from the genesis file + #[cfg(not(all( + any(test, feature = "testing", feature = "benches"), + not(feature = "integration"), + )))] + let native_token = { let chain_dir = base_dir.join(chain_id.as_str()); let genesis = genesis::chain::Finalized::read_toml_files(&chain_dir) .expect("Missing genesis files"); genesis.get_native_token().clone() - } else { - address::nam() }; // load last state from storage @@ -1935,7 +1943,7 @@ mod test_utils { ); let vp_wasm_compilation_cache = 50 * 1024 * 1024; // 50 kiB let tx_wasm_compilation_cache = 50 * 1024 * 1024; // 50 kiB - let native_token = address::nam(); + let native_token = address::testing::nam(); let mut shell = Shell::::new( config::Ledger::new( base_dir.clone(), @@ -2742,9 +2750,10 @@ mod shell_tests { #[test] fn test_fee_non_whitelisted_token() { let (shell, _recv, _, _) = test_utils::setup(); - let apfel_denom = read_denom(&shell.wl_storage, &address::apfel()) - .expect("unable to read denomination from storage") - .expect("unable to find denomination of apfels"); + let apfel_denom = + read_denom(&shell.wl_storage, &address::testing::apfel()) + .expect("unable to read denomination from storage") + .expect("unable to find denomination of apfels"); let mut wrapper = Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new( @@ -2753,7 +2762,7 @@ mod shell_tests { 100.into(), apfel_denom, ), - token: address::apfel(), + token: address::testing::apfel(), }, crate::wallet::defaults::albert_keypair().ref_to(), Epoch(0), diff --git a/crates/apps/src/lib/node/ledger/shell/prepare_proposal.rs b/crates/apps/src/lib/node/ledger/shell/prepare_proposal.rs index 9b56878f16..a8860c1607 100644 --- a/crates/apps/src/lib/node/ledger/shell/prepare_proposal.rs +++ b/crates/apps/src/lib/node/ledger/shell/prepare_proposal.rs @@ -1183,13 +1183,13 @@ mod test_prepare_proposal { // Remove the allowed btc *local_config = Some(ValidatorLocalConfig { accepted_gas_tokens: std::collections::HashMap::from([( - namada::core::address::nam(), + namada::core::address::testing::nam(), Amount::from(1), )]), }); } - let btc_denom = read_denom(&shell.wl_storage, &address::btc()) + let btc_denom = read_denom(&shell.wl_storage, &address::testing::btc()) .expect("unable to read denomination from storage") .expect("unable to find denomination of btcs"); @@ -1199,7 +1199,7 @@ mod test_prepare_proposal { 100.into(), btc_denom, ), - token: address::btc(), + token: address::testing::btc(), }, crate::wallet::defaults::albert_keypair().ref_to(), Epoch(0), @@ -1237,9 +1237,10 @@ mod test_prepare_proposal { fn test_fee_non_whitelisted_token() { let (shell, _recv, _, _) = test_utils::setup(); - let apfel_denom = read_denom(&shell.wl_storage, &address::apfel()) - .expect("unable to read denomination from storage") - .expect("unable to find denomination of apfels"); + let apfel_denom = + read_denom(&shell.wl_storage, &address::testing::apfel()) + .expect("unable to read denomination from storage") + .expect("unable to find denomination of apfels"); let wrapper = WrapperTx::new( Fee { @@ -1247,7 +1248,7 @@ mod test_prepare_proposal { 100.into(), apfel_denom, ), - token: address::apfel(), + token: address::testing::apfel(), }, crate::wallet::defaults::albert_keypair().ref_to(), Epoch(0), @@ -1289,7 +1290,7 @@ mod test_prepare_proposal { // Remove btc and increase minimum for nam *local_config = Some(ValidatorLocalConfig { accepted_gas_tokens: std::collections::HashMap::from([( - namada::core::address::nam(), + namada::core::address::testing::nam(), Amount::from(100), )]), }); diff --git a/crates/apps/src/lib/node/ledger/shell/process_proposal.rs b/crates/apps/src/lib/node/ledger/shell/process_proposal.rs index 4f4e3eb132..2414ed4757 100644 --- a/crates/apps/src/lib/node/ledger/shell/process_proposal.rs +++ b/crates/apps/src/lib/node/ledger/shell/process_proposal.rs @@ -1868,9 +1868,10 @@ mod test_process_proposal { fn test_fee_non_whitelisted_token() { let (shell, _recv, _, _) = test_utils::setup(); - let apfel_denom = read_denom(&shell.wl_storage, &address::apfel()) - .expect("unable to read denomination from storage") - .expect("unable to find denomination of apfels"); + let apfel_denom = + read_denom(&shell.wl_storage, &address::testing::apfel()) + .expect("unable to read denomination from storage") + .expect("unable to find denomination of apfels"); let mut wrapper = Tx::from_type(TxType::Wrapper(Box::new(WrapperTx::new( @@ -1879,7 +1880,7 @@ mod test_process_proposal { 100.into(), apfel_denom, ), - token: address::apfel(), + token: address::testing::apfel(), }, crate::wallet::defaults::albert_keypair().ref_to(), Epoch(0), diff --git a/crates/apps/src/lib/node/ledger/storage/mod.rs b/crates/apps/src/lib/node/ledger/storage/mod.rs index 8f9aef8476..551cf41628 100644 --- a/crates/apps/src/lib/node/ledger/storage/mod.rs +++ b/crates/apps/src/lib/node/ledger/storage/mod.rs @@ -87,7 +87,7 @@ mod tests { let mut storage = PersistentStorage::open( db_path.path(), ChainId::default(), - address::nam(), + address::testing::nam(), None, None, is_merklized_storage_key, @@ -139,7 +139,7 @@ mod tests { let mut storage = PersistentStorage::open( db_path.path(), ChainId::default(), - address::nam(), + address::testing::nam(), None, None, is_merklized_storage_key, @@ -201,7 +201,7 @@ mod tests { let mut storage = PersistentStorage::open( db_path.path(), ChainId::default(), - address::nam(), + address::testing::nam(), None, None, is_merklized_storage_key, @@ -226,7 +226,7 @@ mod tests { let mut storage = PersistentStorage::open( db_path.path(), ChainId::default(), - address::nam(), + address::testing::nam(), None, None, is_merklized_storage_key, @@ -273,7 +273,7 @@ mod tests { let mut storage = PersistentStorage::open( db_path.path(), ChainId::default(), - address::nam(), + address::testing::nam(), None, None, is_merklized_storage_key, @@ -341,7 +341,7 @@ mod tests { let mut storage = PersistentStorage::open( db_path.path(), ChainId::default(), - address::nam(), + address::testing::nam(), None, None, is_merklized_storage_key, @@ -433,7 +433,7 @@ mod tests { let mut storage = PersistentStorage::open( db_path.path(), ChainId::default(), - address::nam(), + address::testing::nam(), None, None, is_merklized_storage_key, @@ -552,7 +552,7 @@ mod tests { let mut storage = PersistentStorage::open( db_path.path(), ChainId::default(), - address::nam(), + address::testing::nam(), None, Some(5), is_merklized_storage_key, @@ -654,7 +654,7 @@ mod tests { let storage = PersistentStorage::open( db_path.path(), ChainId::default(), - address::nam(), + address::testing::nam(), None, None, is_merklized_storage_key, @@ -769,7 +769,7 @@ mod tests { let storage = PersistentStorage::open( db_path.path(), ChainId::default(), - address::nam(), + address::testing::nam(), None, None, merkle_tree_key_filter, diff --git a/crates/apps/src/lib/wallet/defaults.rs b/crates/apps/src/lib/wallet/defaults.rs index 3b06bdfb13..1885c2ade7 100644 --- a/crates/apps/src/lib/wallet/defaults.rs +++ b/crates/apps/src/lib/wallet/defaults.rs @@ -13,9 +13,10 @@ mod dev { use std::collections::HashMap; use lazy_static::lazy_static; - use namada::core::address::{ - apfel, btc, dot, eth, kartoffel, nam, schnitzel, Address, + use namada::core::address::testing::{ + apfel, btc, dot, eth, kartoffel, nam, schnitzel, }; + use namada::core::address::Address; use namada::core::key::*; use namada::ledger::{governance, pgf, pos}; use namada_sdk::wallet::alias::Alias; diff --git a/crates/benches/host_env.rs b/crates/benches/host_env.rs index a659e7aa51..1e97a5936d 100644 --- a/crates/benches/host_env.rs +++ b/crates/benches/host_env.rs @@ -21,7 +21,7 @@ fn tx_section_signature_validation(c: &mut Criterion) { let transfer_data = Transfer { source: defaults::albert_address(), target: defaults::bertha_address(), - token: address::nam(), + token: address::testing::nam(), amount: Amount::native_whole(500).native_denominated(), key: None, shielded: None, diff --git a/crates/benches/native_vps.rs b/crates/benches/native_vps.rs index e4d94d764f..3806d4f7c7 100644 --- a/crates/benches/native_vps.rs +++ b/crates/benches/native_vps.rs @@ -415,7 +415,7 @@ fn vp_multitoken(c: &mut Criterion) { Transfer { source: defaults::albert_address(), target: defaults::bertha_address(), - token: address::nam(), + token: address::testing::nam(), amount: Amount::native_whole(1000).native_denominated(), key: None, shielded: None, diff --git a/crates/benches/process_wrapper.rs b/crates/benches/process_wrapper.rs index 4264748e53..4dad8ae840 100644 --- a/crates/benches/process_wrapper.rs +++ b/crates/benches/process_wrapper.rs @@ -23,7 +23,7 @@ fn process_tx(c: &mut Criterion) { Transfer { source: defaults::albert_address(), target: defaults::bertha_address(), - token: address::nam(), + token: address::testing::nam(), amount: Amount::native_whole(1).native_denominated(), key: None, shielded: None, @@ -36,7 +36,7 @@ fn process_tx(c: &mut Criterion) { tx.update_header(namada::tx::data::TxType::Wrapper(Box::new( WrapperTx::new( Fee { - token: address::nam(), + token: address::testing::nam(), amount_per_gas_unit: DenominatedAmount::native(1.into()), }, defaults::albert_keypair().ref_to(), diff --git a/crates/benches/vps.rs b/crates/benches/vps.rs index 352fe39f25..d552c32a9a 100644 --- a/crates/benches/vps.rs +++ b/crates/benches/vps.rs @@ -41,7 +41,7 @@ fn vp_user(c: &mut Criterion) { Transfer { source: defaults::albert_address(), target: defaults::bertha_address(), - token: address::nam(), + token: address::testing::nam(), amount: Amount::native_whole(1000).native_denominated(), key: None, shielded: None, @@ -56,7 +56,7 @@ fn vp_user(c: &mut Criterion) { Transfer { source: defaults::bertha_address(), target: defaults::albert_address(), - token: address::nam(), + token: address::testing::nam(), amount: Amount::native_whole(1000).native_denominated(), key: None, shielded: None, @@ -187,7 +187,7 @@ fn vp_implicit(c: &mut Criterion) { Transfer { source: Address::from(&implicit_account.to_public()), target: defaults::bertha_address(), - token: address::nam(), + token: address::testing::nam(), amount: Amount::native_whole(500).native_denominated(), key: None, shielded: None, @@ -202,7 +202,7 @@ fn vp_implicit(c: &mut Criterion) { Transfer { source: defaults::bertha_address(), target: Address::from(&implicit_account.to_public()), - token: address::nam(), + token: address::testing::nam(), amount: Amount::native_whole(1000).native_denominated(), key: None, shielded: None, @@ -330,7 +330,7 @@ fn vp_validator(c: &mut Criterion) { Transfer { source: defaults::validator_address(), target: defaults::bertha_address(), - token: address::nam(), + token: address::testing::nam(), amount: Amount::native_whole(1000).native_denominated(), key: None, shielded: None, @@ -345,7 +345,7 @@ fn vp_validator(c: &mut Criterion) { Transfer { source: defaults::bertha_address(), target: defaults::validator_address(), - token: address::nam(), + token: address::testing::nam(), amount: Amount::native_whole(1000).native_denominated(), key: None, shielded: None, diff --git a/crates/core/src/address.rs b/crates/core/src/address.rs index 9cd8f3c64d..3b1655f84c 100644 --- a/crates/core/src/address.rs +++ b/crates/core/src/address.rs @@ -3,7 +3,6 @@ mod raw; -use std::collections::HashMap; use std::fmt::{Debug, Display}; use std::hash::Hash; use std::str::FromStr; @@ -18,7 +17,6 @@ use crate::ethereum_events::EthAddress; use crate::ibc::primitives::Signer; use crate::ibc::IbcTokenHash; use crate::key::PublicKeyHash; -use crate::token::Denomination; use crate::{impl_display_and_from_str_via_format, key, string_encoding}; /// The length of an established [`Address`] encoded with Borsh. @@ -586,74 +584,6 @@ impl InternalAddress { } } -/// Temporary helper for testing -pub fn nam() -> Address { - Address::decode("tnam1q99c37u38grkdcc2qze0hz4zjjd8zr3yucd3mzgz") - .expect("The token address decoding shouldn't fail") -} - -/// Temporary helper for testing -pub fn btc() -> Address { - Address::decode("tnam1qy7jxng788scr4fdqxqxtc2ze2guq5478cml9cd9") - .expect("The token address decoding shouldn't fail") -} - -/// Temporary helper for testing -pub fn eth() -> Address { - Address::decode("tnam1qyr9vd8ltunq72qc7pk58v7jdsedt4mggqqpxs03") - .expect("The token address decoding shouldn't fail") -} - -/// Temporary helper for testing -pub fn dot() -> Address { - Address::decode("tnam1qx6k4wau5t6m8g2hjq55fje2ynpvh5t27s8p3p0l") - .expect("The token address decoding shouldn't fail") -} - -/// Temporary helper for testing -pub fn schnitzel() -> Address { - Address::decode("tnam1q9euzsu2qfv4y6p0dqaga20n0u0yp8c3ec006yg2") - .expect("The token address decoding shouldn't fail") -} - -/// Temporary helper for testing -pub fn apfel() -> Address { - Address::decode("tnam1qxlmdmw2y6hzvjg34zca8r6d4s6zmtkhty8myzu4") - .expect("The token address decoding shouldn't fail") -} - -/// Temporary helper for testing -pub fn kartoffel() -> Address { - Address::decode("tnam1q87teqzjytwa9xd9qk8u558xxnrwuzdjzs7zvhzr") - .expect("The token address decoding shouldn't fail") -} - -/// Temporary helper for testing -pub const fn wnam() -> EthAddress { - // TODO: Replace this with the real wNam ERC20 address once it exists - // "DEADBEEF DEADBEEF DEADBEEF DEADBEEF DEADBEEF" - EthAddress([ - 222, 173, 190, 239, 222, 173, 190, 239, 222, 173, 190, 239, 222, 173, - 190, 239, 222, 173, 190, 239, - ]) -} - -/// Temporary helper for testing, a hash map of tokens addresses with their -/// informal currency codes and number of decimal places. -pub fn tokens() -> HashMap<&'static str, Denomination> { - vec![ - ("nam", 6.into()), - ("btc", 8.into()), - ("eth", 18.into()), - ("dot", 10.into()), - ("schnitzel", 6.into()), - ("apfel", 6.into()), - ("kartoffel", 6.into()), - ] - .into_iter() - .collect() -} - #[cfg(test)] pub mod tests { use proptest::prelude::*; @@ -745,10 +675,13 @@ pub fn gen_deterministic_established_address(seed: impl AsRef) -> Address { /// Helpers for testing with addresses. #[cfg(any(test, feature = "testing"))] pub mod testing { + use std::collections::HashMap; + use proptest::prelude::*; use super::*; use crate::key::*; + use crate::token::Denomination; /// Generate a new established address. pub fn gen_established_address() -> Address { @@ -907,4 +840,72 @@ pub mod testing { // TODO: generate random erc20 addr data InternalAddress::Nut(arbitrary_eth_address()) } + + /// NAM token address for testing + pub fn nam() -> Address { + Address::decode("tnam1q99c37u38grkdcc2qze0hz4zjjd8zr3yucd3mzgz") + .expect("The token address decoding shouldn't fail") + } + + /// BTC token address for testing + pub fn btc() -> Address { + Address::decode("tnam1qy7jxng788scr4fdqxqxtc2ze2guq5478cml9cd9") + .expect("The token address decoding shouldn't fail") + } + + /// ETH token address for testing + pub fn eth() -> Address { + Address::decode("tnam1qyr9vd8ltunq72qc7pk58v7jdsedt4mggqqpxs03") + .expect("The token address decoding shouldn't fail") + } + + /// DOT token address for testing + pub fn dot() -> Address { + Address::decode("tnam1qx6k4wau5t6m8g2hjq55fje2ynpvh5t27s8p3p0l") + .expect("The token address decoding shouldn't fail") + } + + /// Imaginary token address for testing + pub fn schnitzel() -> Address { + Address::decode("tnam1q9euzsu2qfv4y6p0dqaga20n0u0yp8c3ec006yg2") + .expect("The token address decoding shouldn't fail") + } + + /// Imaginary token address for testing + pub fn apfel() -> Address { + Address::decode("tnam1qxlmdmw2y6hzvjg34zca8r6d4s6zmtkhty8myzu4") + .expect("The token address decoding shouldn't fail") + } + + /// Imaginary token address for testing + pub fn kartoffel() -> Address { + Address::decode("tnam1q87teqzjytwa9xd9qk8u558xxnrwuzdjzs7zvhzr") + .expect("The token address decoding shouldn't fail") + } + + /// Imaginary eth address for testing + pub const fn wnam() -> EthAddress { + // TODO: Replace this with the real wNam ERC20 address once it exists + // "DEADBEEF DEADBEEF DEADBEEF DEADBEEF DEADBEEF" + EthAddress([ + 222, 173, 190, 239, 222, 173, 190, 239, 222, 173, 190, 239, 222, + 173, 190, 239, 222, 173, 190, 239, + ]) + } + + /// A hash map of tokens addresses with their informal currency codes and + /// number of decimal places. + pub fn tokens() -> HashMap<&'static str, Denomination> { + vec![ + ("nam", 6.into()), + ("btc", 8.into()), + ("eth", 18.into()), + ("dot", 10.into()), + ("schnitzel", 6.into()), + ("apfel", 6.into()), + ("kartoffel", 6.into()), + ] + .into_iter() + .collect() + } } diff --git a/crates/core/src/eth_bridge_pool.rs b/crates/core/src/eth_bridge_pool.rs index b2d9c82986..86fa7daaea 100644 --- a/crates/core/src/eth_bridge_pool.rs +++ b/crates/core/src/eth_bridge_pool.rs @@ -434,8 +434,7 @@ pub mod testing { #[cfg(test)] mod test_eth_bridge_pool_types { use super::*; - use crate::address::nam; - use crate::address::testing::established_address_1; + use crate::address::testing::{established_address_1, nam}; /// Test that [`PendingTransfer`] and [`TransferToEthereum`] /// have the same keccak hash, after being ABI encoded. diff --git a/crates/ethereum_bridge/src/protocol/transactions/ethereum_events/events.rs b/crates/ethereum_bridge/src/protocol/transactions/ethereum_events/events.rs index 09980ad331..b1f3c7ad00 100644 --- a/crates/ethereum_bridge/src/protocol/transactions/ethereum_events/events.rs +++ b/crates/ethereum_bridge/src/protocol/transactions/ethereum_events/events.rs @@ -585,8 +585,8 @@ mod tests { use assert_matches::assert_matches; use eyre::Result; - use namada_core::address::testing::gen_implicit_address; - use namada_core::address::{gen_established_address, nam, wnam}; + use namada_core::address::gen_established_address; + use namada_core::address::testing::{gen_implicit_address, nam, wnam}; use namada_core::borsh::BorshSerializeExt; use namada_core::eth_bridge_pool::GasFee; use namada_core::ethereum_events::testing::{ diff --git a/crates/ethereum_bridge/src/storage/mod.rs b/crates/ethereum_bridge/src/storage/mod.rs index b4e4470944..3adcd0efb3 100644 --- a/crates/ethereum_bridge/src/storage/mod.rs +++ b/crates/ethereum_bridge/src/storage/mod.rs @@ -69,7 +69,7 @@ pub fn bridge_contract_key() -> Key { #[cfg(test)] mod test { use namada_core::address; - use namada_core::address::nam; + use namada_core::address::testing::nam; use namada_core::ethereum_events::testing::arbitrary_eth_address; use super::*; diff --git a/crates/ethereum_bridge/src/storage/wrapped_erc20s.rs b/crates/ethereum_bridge/src/storage/wrapped_erc20s.rs index 551da0a8ad..a5f31d6c31 100644 --- a/crates/ethereum_bridge/src/storage/wrapped_erc20s.rs +++ b/crates/ethereum_bridge/src/storage/wrapped_erc20s.rs @@ -110,7 +110,8 @@ mod test { use std::str::FromStr; use assert_matches::assert_matches; - use namada_core::address::{nam, Address}; + use namada_core::address::testing::nam; + use namada_core::address::Address; use namada_core::ethereum_events::testing::DAI_ERC20_ETH_ADDRESS; use namada_core::storage::DbKeySeg; diff --git a/crates/ethereum_bridge/src/test_utils.rs b/crates/ethereum_bridge/src/test_utils.rs index f823cc7f47..e6ab489c41 100644 --- a/crates/ethereum_bridge/src/test_utils.rs +++ b/crates/ethereum_bridge/src/test_utils.rs @@ -4,7 +4,8 @@ use std::collections::HashMap; use std::num::NonZeroU64; use namada_account::protocol_pk_key; -use namada_core::address::{self, wnam, Address}; +use namada_core::address::testing::wnam; +use namada_core::address::{self, Address}; use namada_core::dec::Dec; use namada_core::ethereum_events::EthAddress; use namada_core::keccak::KeccakHash; diff --git a/crates/merkle_tree/Cargo.toml b/crates/merkle_tree/Cargo.toml index 329389a0d8..4fc284deb9 100644 --- a/crates/merkle_tree/Cargo.toml +++ b/crates/merkle_tree/Cargo.toml @@ -23,6 +23,8 @@ prost.workspace = true thiserror.workspace = true [dev-dependencies] +namada_core = { path = "../core", features = ["testing"] } + assert_matches.workspace = true proptest.workspace = true itertools.workspace = true diff --git a/crates/merkle_tree/src/eth_bridge_pool.rs b/crates/merkle_tree/src/eth_bridge_pool.rs index 32d0f0462c..12f0e9303c 100644 --- a/crates/merkle_tree/src/eth_bridge_pool.rs +++ b/crates/merkle_tree/src/eth_bridge_pool.rs @@ -373,7 +373,8 @@ mod test_bridge_pool_tree { use assert_matches::assert_matches; use itertools::Itertools; - use namada_core::address::{nam, Address}; + use namada_core::address::testing::nam; + use namada_core::address::Address; use namada_core::eth_bridge_pool::{ GasFee, TransferToEthereum, TransferToEthereumKind, }; diff --git a/crates/namada/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs b/crates/namada/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs index 09aef4623d..60e846fb44 100644 --- a/crates/namada/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs +++ b/crates/namada/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs @@ -642,7 +642,6 @@ mod test_bridge_pool_vp { use std::env::temp_dir; use borsh::BorshDeserialize; - use namada_core::address; use namada_core::borsh::BorshSerializeExt; use namada_ethereum_bridge::storage::bridge_pool::get_signed_root_key; use namada_ethereum_bridge::storage::parameters::{ @@ -654,7 +653,8 @@ mod test_bridge_pool_vp { use namada_tx::data::TxType; use super::*; - use crate::address::{nam, wnam, InternalAddress}; + use crate::address::testing::{nam, wnam}; + use crate::address::InternalAddress; use crate::chain::ChainId; use crate::eth_bridge_pool::{GasFee, TransferToEthereum}; use crate::hash::Hash; @@ -917,7 +917,7 @@ mod test_bridge_pool_vp { storage: State::::open( std::path::Path::new(""), ChainId::default(), - address::nam(), + nam(), None, None, namada_sdk::state::merklize_all_keys, diff --git a/crates/namada/src/ledger/native_vp/ethereum_bridge/vp.rs b/crates/namada/src/ledger/native_vp/ethereum_bridge/vp.rs index 289769eb88..3f72e60d5c 100644 --- a/crates/namada/src/ledger/native_vp/ethereum_bridge/vp.rs +++ b/crates/namada/src/ledger/native_vp/ethereum_bridge/vp.rs @@ -176,8 +176,7 @@ mod tests { use rand::Rng; use super::*; - use crate::address::testing::established_address_1; - use crate::address::{nam, wnam}; + use crate::address::testing::{established_address_1, nam, wnam}; use crate::ethereum_bridge::storage::bridge_pool::BRIDGE_POOL_ADDRESS; use crate::ethereum_bridge::storage::parameters::{ Contracts, EthereumBridgeParams, UpgradeableContract, diff --git a/crates/namada/src/ledger/native_vp/ibc/mod.rs b/crates/namada/src/ledger/native_vp/ibc/mod.rs index db0ed077e1..bf54ed1088 100644 --- a/crates/namada/src/ledger/native_vp/ibc/mod.rs +++ b/crates/namada/src/ledger/native_vp/ibc/mod.rs @@ -328,9 +328,9 @@ mod tests { use super::*; use crate::core::address::testing::{ - established_address_1, established_address_2, + established_address_1, established_address_2, nam, }; - use crate::core::address::{nam, InternalAddress}; + use crate::core::address::InternalAddress; use crate::core::storage::Epoch; use crate::ibc::apps::transfer::types::events::{ AckEvent, DenomTraceEvent, RecvEvent, TimeoutEvent, TransferEvent, diff --git a/crates/namada/src/ledger/native_vp/multitoken.rs b/crates/namada/src/ledger/native_vp/multitoken.rs index 4a4cdeadf8..9e3783a17a 100644 --- a/crates/namada/src/ledger/native_vp/multitoken.rs +++ b/crates/namada/src/ledger/native_vp/multitoken.rs @@ -224,9 +224,8 @@ mod tests { use super::*; use crate::address::{Address, InternalAddress}; - use crate::core::address::nam; use crate::core::address::testing::{ - established_address_1, established_address_2, + established_address_1, established_address_2, nam, }; use crate::key::testing::keypair_1; use crate::ledger::gas::VpGasMeter; diff --git a/crates/sdk/src/eth_bridge/bridge_pool.rs b/crates/sdk/src/eth_bridge/bridge_pool.rs index 8189953dfb..e15ce4a674 100644 --- a/crates/sdk/src/eth_bridge/bridge_pool.rs +++ b/crates/sdk/src/eth_bridge/bridge_pool.rs @@ -1213,7 +1213,7 @@ mod recommendations { #[cfg(test)] mod test_recommendations { - use namada_core::address::Address; + use namada_core::address; use super::*; use crate::io::StdIo; @@ -1236,7 +1236,7 @@ mod recommendations { amount: Default::default(), }, gas_fee: GasFee { - token: namada_core::address::nam(), + token: address::testing::nam(), amount: gas_amount.into(), payer: bertha_address(), }, @@ -1279,7 +1279,7 @@ mod recommendations { /// Add ETH to a conversion table. fn add_eth_to_conversion_table(&mut self) { self.conversion_table.insert( - namada_core::address::eth(), + address::testing::eth(), args::BpConversionTableEntry { alias: "ETH".into(), conversion_rate: 1e9, // 1 ETH = 1e9 GWEI @@ -1304,7 +1304,7 @@ mod recommendations { amount: Default::default(), }, gas_fee: GasFee { - token: namada_core::address::eth(), + token: address::testing::eth(), amount: 1_000_000_000_u64.into(), // 1 GWEI payer: bertha_address(), }, @@ -1537,14 +1537,14 @@ mod recommendations { let conversion_table = { let mut t = HashMap::new(); t.insert( - namada_core::address::apfel(), + address::testing::apfel(), args::BpConversionTableEntry { alias: APFEL.into(), conversion_rate: APF_RATE, }, ); t.insert( - namada_core::address::schnitzel(), + address::testing::schnitzel(), args::BpConversionTableEntry { alias: SCHNITZEL.into(), conversion_rate: SCH_RATE, @@ -1559,13 +1559,13 @@ mod recommendations { let transfer_paid_in_apfel = { let mut pending = ctx.pending.clone(); pending.transfer.amount = 1.into(); - pending.gas_fee.token = namada_core::address::apfel(); + pending.gas_fee.token = address::testing::apfel(); pending }; let transfer_paid_in_schnitzel = { let mut pending = ctx.pending.clone(); pending.transfer.amount = 2.into(); - pending.gas_fee.token = namada_core::address::schnitzel(); + pending.gas_fee.token = address::testing::schnitzel(); pending }; // add the transfers to the pool, and expect them to diff --git a/crates/sdk/src/queries/shell/eth_bridge.rs b/crates/sdk/src/queries/shell/eth_bridge.rs index 913155d5b2..0962f08d3b 100644 --- a/crates/sdk/src/queries/shell/eth_bridge.rs +++ b/crates/sdk/src/queries/shell/eth_bridge.rs @@ -834,8 +834,7 @@ mod test_ethbridge_router { use std::collections::BTreeMap; use assert_matches::assert_matches; - use namada_core::address::nam; - use namada_core::address::testing::established_address_1; + use namada_core::address::testing::{established_address_1, nam}; use namada_core::eth_abi::Encode; use namada_core::eth_bridge_pool::{ GasFee, PendingTransfer, TransferToEthereum, TransferToEthereumKind, diff --git a/crates/shielded_token/src/conversion.rs b/crates/shielded_token/src/conversion.rs index d247b65026..c71205bb6f 100644 --- a/crates/shielded_token/src/conversion.rs +++ b/crates/shielded_token/src/conversion.rs @@ -631,13 +631,13 @@ mod tests { pub fn tokens() -> HashMap { vec![ - (address::nam(), ("nam", 6.into())), - (address::btc(), ("btc", 8.into())), - (address::eth(), ("eth", 18.into())), - (address::dot(), ("dot", 10.into())), - (address::schnitzel(), ("schnitzel", 6.into())), - (address::apfel(), ("apfel", 6.into())), - (address::kartoffel(), ("kartoffel", 6.into())), + (address::testing::nam(), ("nam", 6.into())), + (address::testing::btc(), ("btc", 8.into())), + (address::testing::eth(), ("eth", 18.into())), + (address::testing::dot(), ("dot", 10.into())), + (address::testing::schnitzel(), ("schnitzel", 6.into())), + (address::testing::apfel(), ("apfel", 6.into())), + (address::testing::kartoffel(), ("kartoffel", 6.into())), ] .into_iter() .collect() diff --git a/crates/state/src/lib.rs b/crates/state/src/lib.rs index f111212c5e..f27d8fbeb0 100644 --- a/crates/state/src/lib.rs +++ b/crates/state/src/lib.rs @@ -1145,7 +1145,7 @@ pub mod testing { conversion_state: ConversionState::default(), tx_queue: TxQueue::default(), expired_txs_queue: ExpiredTxsQueue::default(), - native_token: address::nam(), + native_token: address::testing::nam(), ethereum_height: None, eth_events_queue: EthEventsQueue::default(), storage_read_past_height_limit: Some(1000), diff --git a/crates/storage/src/lib.rs b/crates/storage/src/lib.rs index 9b8e478d93..e39bb485a2 100644 --- a/crates/storage/src/lib.rs +++ b/crates/storage/src/lib.rs @@ -324,7 +324,7 @@ pub mod testing { height: BlockHeight::first(), epoch: Epoch::default(), pred_epochs: Epochs::default(), - native_token: address::nam(), + native_token: address::testing::nam(), conversion_state: ConversionState::default(), merkle_tree_key_filter: merklize_all_keys, } diff --git a/crates/tests/src/native_vp/eth_bridge_pool.rs b/crates/tests/src/native_vp/eth_bridge_pool.rs index 18d707cf66..93b5ea35ed 100644 --- a/crates/tests/src/native_vp/eth_bridge_pool.rs +++ b/crates/tests/src/native_vp/eth_bridge_pool.rs @@ -4,7 +4,7 @@ mod test_bridge_pool_vp { use borsh::BorshDeserialize; use borsh_ext::BorshSerializeExt; - use namada::core::address::{nam, wnam}; + use namada::core::address::testing::{nam, wnam}; use namada::core::chain::ChainId; use namada::core::eth_bridge_pool::{ GasFee, PendingTransfer, TransferToEthereum, TransferToEthereumKind, diff --git a/crates/trans_token/src/storage.rs b/crates/trans_token/src/storage.rs index 2e38e97fa4..ca429b7f5b 100644 --- a/crates/trans_token/src/storage.rs +++ b/crates/trans_token/src/storage.rs @@ -237,7 +237,7 @@ mod testing { #[test] fn test_burn_native_tokens() { let mut storage = TestStorage::default(); - let native_token = address::nam(); + let native_token = address::testing::nam(); // Get some addresses let addr1 = address::testing::gen_implicit_address(); diff --git a/crates/tx/src/data/mod.rs b/crates/tx/src/data/mod.rs index 61b10896ab..e187bdd9fb 100644 --- a/crates/tx/src/data/mod.rs +++ b/crates/tx/src/data/mod.rs @@ -341,7 +341,7 @@ impl TxSentinel { #[cfg(test)] mod test_process_tx { use assert_matches::assert_matches; - use namada_core::address::nam; + use namada_core::address::testing::nam; use namada_core::key::*; use namada_core::storage::Epoch; use namada_core::token::{Amount, DenominatedAmount}; diff --git a/wasm/wasm_source/src/vp_implicit.rs b/wasm/wasm_source/src/vp_implicit.rs index c8fdf07455..0f47ddbb19 100644 --- a/wasm/wasm_source/src/vp_implicit.rs +++ b/wasm/wasm_source/src/vp_implicit.rs @@ -433,7 +433,7 @@ mod tests { let public_key = secret_key.ref_to(); let vp_owner: Address = (&public_key).into(); let source = address::testing::established_address_2(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); // Spawn the accounts to be able to modify their storage tx_env.spawn_accounts([&vp_owner, &source, &token]); @@ -517,7 +517,7 @@ mod tests { let public_key = secret_key.ref_to(); let vp_owner: Address = (&public_key).into(); let target = address::testing::established_address_2(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); let bond_amount = token::Amount::from_uint(5_098_123, 0).unwrap(); let unbond_amount = token::Amount::from_uint(3_098_123, 0).unwrap(); @@ -601,7 +601,7 @@ mod tests { let public_key = secret_key.ref_to(); let vp_owner: Address = (&public_key).into(); let target = address::testing::established_address_2(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); let bond_amount = token::Amount::from_uint(5_098_123, 0).unwrap(); let unbond_amount = token::Amount::from_uint(3_098_123, 0).unwrap(); @@ -666,7 +666,7 @@ mod tests { let public_key = secret_key.ref_to(); let vp_owner: Address = (&public_key).into(); let target = address::testing::established_address_2(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); // Spawn the accounts to be able to modify their storage @@ -725,7 +725,7 @@ mod tests { let public_key = secret_key.ref_to(); let vp_owner: Address = (&public_key).into(); let target = address::testing::established_address_2(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); tx_env.init_parameters(None, None, None, None); @@ -798,7 +798,7 @@ mod tests { let vp_owner: Address = (&public_key).into(); let source = address::testing::established_address_2(); let target = address::testing::established_address_3(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); // Spawn the accounts to be able to modify their storage diff --git a/wasm/wasm_source/src/vp_user.rs b/wasm/wasm_source/src/vp_user.rs index fef53b4304..ddaf25caee 100644 --- a/wasm/wasm_source/src/vp_user.rs +++ b/wasm/wasm_source/src/vp_user.rs @@ -377,7 +377,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let source = address::testing::established_address_2(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); // Spawn the accounts to be able to modify their storage @@ -432,7 +432,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let target = address::testing::established_address_2(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); // Spawn the accounts to be able to modify their storage @@ -489,7 +489,7 @@ mod tests { let keypair = key::testing::keypair_1(); let public_key = keypair.ref_to(); let target = address::testing::established_address_2(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); // Spawn the accounts to be able to modify their storage @@ -584,7 +584,7 @@ mod tests { let public_key = secret_key.ref_to(); let vp_owner: Address = address::testing::established_address_2(); let target = address::testing::established_address_3(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); let bond_amount = token::Amount::from_uint(5_098_123, 0).unwrap(); let unbond_amount = token::Amount::from_uint(3_098_123, 0).unwrap(); @@ -748,7 +748,7 @@ mod tests { let secret_key = key::testing::keypair_1(); let public_key = secret_key.ref_to(); let target = address::testing::established_address_3(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); let bond_amount = token::Amount::from_uint(5_098_123, 0).unwrap(); let unbond_amount = token::Amount::from_uint(3_098_123, 0).unwrap(); @@ -844,7 +844,7 @@ mod tests { let public_key = secret_key.ref_to(); let vp_owner: Address = address::testing::established_address_2(); let target = address::testing::established_address_3(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); let bond_amount = token::Amount::from_uint(5_098_123, 0).unwrap(); let unbond_amount = token::Amount::from_uint(3_098_123, 0).unwrap(); @@ -1033,7 +1033,7 @@ mod tests { let secret_key = key::testing::keypair_1(); let public_key = secret_key.ref_to(); let target = address::testing::established_address_3(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); let bond_amount = token::Amount::from_uint(5_098_123, 0).unwrap(); let unbond_amount = token::Amount::from_uint(3_098_123, 0).unwrap(); @@ -1109,7 +1109,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let source = address::testing::established_address_2(); let target = address::testing::established_address_3(); - let token = address::nam(); + let token = address::testing::nam(); let amount = token::Amount::from_uint(10_098_123, 0).unwrap(); // Spawn the accounts to be able to modify their storage From d44bf1e2c242e076ca96e23c1e27dfba663c12b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 2 Feb 2024 10:36:30 +0000 Subject: [PATCH 2/2] changelog: add #2507 --- .../unreleased/improvements/2507-refactor-testing-addrs.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/improvements/2507-refactor-testing-addrs.md diff --git a/.changelog/unreleased/improvements/2507-refactor-testing-addrs.md b/.changelog/unreleased/improvements/2507-refactor-testing-addrs.md new file mode 100644 index 0000000000..a52905d84f --- /dev/null +++ b/.changelog/unreleased/improvements/2507-refactor-testing-addrs.md @@ -0,0 +1,2 @@ +- Hid addresses used for testing from public API. + ([\#2507](https://github.com/anoma/namada/pull/2507)) \ No newline at end of file