Skip to content

Commit

Permalink
Merge branch 'tomas/refactor-testing-addrs' (#2507)
Browse files Browse the repository at this point in the history
* tomas/refactor-testing-addrs:
  changelog: add #2507
  core: prevent from using addresses for testing in non-test code
  • Loading branch information
tzemanovic committed Feb 22, 2024
2 parents b7c04aa + d44bf1e commit 005ac91
Show file tree
Hide file tree
Showing 35 changed files with 201 additions and 180 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Hid addresses used for testing from public API.
([\#2507](https://github.com/anoma/namada/pull/2507))
6 changes: 3 additions & 3 deletions crates/apps/src/lib/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,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('.')
Expand Down Expand Up @@ -1035,7 +1035,7 @@ impl BenchShieldedCtx {
&namada,
&source,
&target,
&address::nam(),
&address::testing::nam(),
denominated_amount,
),
)
Expand Down Expand Up @@ -1064,7 +1064,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,
Expand Down
2 changes: 1 addition & 1 deletion crates/apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
23 changes: 14 additions & 9 deletions crates/apps/src/lib/config/genesis/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -120,26 +120,31 @@ fn get_tx_to_sign(tag: impl AsRef<str>, 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,
);
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,
Expand Down Expand Up @@ -741,7 +746,7 @@ impl<T> Signed<T> {
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();
Expand Down Expand Up @@ -769,7 +774,7 @@ impl<T> Signed<T> {
_parts: HashSet<namada_sdk::signing::Signable>,
_user: (),
) -> Result<Tx, namada_sdk::error::Error> {
if pubkey == get_sentinel_pubkey() {
if pubkey == genesis_fee_payer_pk() {
Ok(tx)
} else {
Err(namada_sdk::error::Error::Other(format!(
Expand Down
29 changes: 19 additions & 10 deletions crates/apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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::<PersistentDB, PersistentStorageHasher>::new(
config::Ledger::new(
base_dir.clone(),
Expand Down Expand Up @@ -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(
Expand All @@ -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),
Expand Down
17 changes: 9 additions & 8 deletions crates/apps/src/lib/node/ledger/shell/prepare_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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),
Expand Down Expand Up @@ -1237,17 +1237,18 @@ 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 {
amount_per_gas_unit: DenominatedAmount::new(
100.into(),
apfel_denom,
),
token: address::apfel(),
token: address::testing::apfel(),
},
crate::wallet::defaults::albert_keypair().ref_to(),
Epoch(0),
Expand Down Expand Up @@ -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),
)]),
});
Expand Down
9 changes: 5 additions & 4 deletions crates/apps/src/lib/node/ledger/shell/process_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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),
Expand Down
20 changes: 10 additions & 10 deletions crates/apps/src/lib/node/ledger/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions crates/apps/src/lib/wallet/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/benches/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/benches/native_vps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions crates/benches/process_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down
Loading

0 comments on commit 005ac91

Please sign in to comment.