diff --git a/.changelog/unreleased/testing/893-namada-test-utils-wasms.md b/.changelog/unreleased/testing/893-namada-test-utils-wasms.md new file mode 100644 index 0000000000..a345f0b8e5 --- /dev/null +++ b/.changelog/unreleased/testing/893-namada-test-utils-wasms.md @@ -0,0 +1,2 @@ +- Add utility code for working with test wasms + ([#893](https://github.com/anoma/namada/pull/893)) \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 0b9b113b97..ad02dbfb54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2518,6 +2518,12 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.1.19" @@ -3647,6 +3653,7 @@ dependencies = [ "masp_proofs", "namada_core", "namada_proof_of_stake", + "namada_test_utils", "parity-wasm", "paste", "pretty_assertions", @@ -3716,6 +3723,7 @@ dependencies = [ "masp_primitives", "masp_proofs", "namada", + "namada_test_utils", "num-derive", "num-rational", "num-traits 0.2.15", @@ -3868,6 +3876,7 @@ version = "0.14.3" dependencies = [ "borsh", "namada_core", + "strum", ] [[package]] @@ -4726,7 +4735,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62941722fb675d463659e49c4f3fe1fe792ff24fe5bbaa9c08cd3b98a1c354f5" dependencies = [ "bytes 1.2.1", - "heck", + "heck 0.3.3", "itertools", "lazy_static", "log 0.4.17", @@ -6007,6 +6016,28 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "strum" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "subproductdomain" version = "0.1.0" diff --git a/apps/Cargo.toml b/apps/Cargo.toml index 8a45d19090..37283a10a0 100644 --- a/apps/Cargo.toml +++ b/apps/Cargo.toml @@ -156,6 +156,7 @@ rust_decimal_macros = "1.26.1" [dev-dependencies] namada = {path = "../shared", default-features = false, features = ["testing", "wasm-runtime"]} +namada_test_utils = {path = "../test_utils"} bit-set = "0.5.2" # A fork with state machime testing proptest = {git = "https://github.com/heliaxdev/proptest", branch = "tomas/sm"} diff --git a/apps/src/lib/node/ledger/shell/finalize_block.rs b/apps/src/lib/node/ledger/shell/finalize_block.rs index c1983e8b7d..2a7c4ca789 100644 --- a/apps/src/lib/node/ledger/shell/finalize_block.rs +++ b/apps/src/lib/node/ledger/shell/finalize_block.rs @@ -846,6 +846,7 @@ mod test_finalize_block { InitProposalData, ProposalType, VoteProposalData, }; use namada::types::transaction::{EncryptionKey, Fee, WrapperTx, MIN_FEE}; + use namada_test_utils::TestWasms; use rust_decimal_macros::dec; use test_log::test; @@ -1080,10 +1081,7 @@ mod test_finalize_block { .unwrap(); // create two decrypted txs - let mut wasm_path = top_level_directory(); - wasm_path.push("wasm_for_tests/tx_no_op.wasm"); - let tx_code = std::fs::read(wasm_path) - .expect("Expected a file at given code path"); + let tx_code = TestWasms::TxNoOp.read_bytes(); for i in 0..2 { let raw_tx = Tx::new( tx_code.clone(), diff --git a/shared/Cargo.toml b/shared/Cargo.toml index d89929baf0..0ad899981a 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -136,6 +136,7 @@ assert_matches = "1.5.0" async-trait = {version = "0.1.51"} byte-unit = "4.0.13" libsecp256k1 = {git = "https://github.com/heliaxdev/libsecp256k1", rev = "bbb3bd44a49db361f21d9db80f9a087c194c0ae9"} +namada_test_utils = {path = "../test_utils"} pretty_assertions = "0.7.2" # A fork with state machine testing proptest = {git = "https://github.com/heliaxdev/proptest", branch = "tomas/sm"} diff --git a/shared/src/ledger/queries/shell.rs b/shared/src/ledger/queries/shell.rs index 43a51a1b0f..b81c0542c4 100644 --- a/shared/src/ledger/queries/shell.rs +++ b/shared/src/ledger/queries/shell.rs @@ -344,6 +344,7 @@ where #[cfg(test)] mod test { use borsh::BorshDeserialize; + use namada_test_utils::TestWasms; use crate::ledger::queries::testing::TestClient; use crate::ledger::queries::RPC; @@ -351,8 +352,6 @@ mod test { use crate::proto::Tx; use crate::types::{address, token}; - const TX_NO_OP_WASM: &str = "../wasm_for_tests/tx_no_op.wasm"; - #[test] fn test_shell_queries_router_paths() { let path = RPC.shell().epoch_path(); @@ -386,7 +385,7 @@ mod test { assert_eq!(current_epoch, read_epoch); // Request dry run tx - let tx_no_op = std::fs::read(TX_NO_OP_WASM).expect("cannot load wasm"); + let tx_no_op = TestWasms::TxNoOp.read_bytes(); let tx = Tx::new(tx_no_op, None); let tx_bytes = tx.to_bytes(); let result = RPC diff --git a/shared/src/vm/wasm/compilation_cache/common.rs b/shared/src/vm/wasm/compilation_cache/common.rs index 9f25d573c4..9aa34b0601 100644 --- a/shared/src/vm/wasm/compilation_cache/common.rs +++ b/shared/src/vm/wasm/compilation_cache/common.rs @@ -557,23 +557,18 @@ mod test { use std::cmp::max; use byte_unit::Byte; + use namada_test_utils::TestWasms; use tempfile::{tempdir, TempDir}; use test_log::test; use super::*; use crate::vm::WasmCacheRwAccess; - const TX_NO_OP: &str = "../wasm_for_tests/tx_no_op.wasm"; - const TX_READ_STORAGE_KEY: &str = - "../wasm_for_tests/tx_read_storage_key.wasm"; - const VP_ALWAYS_TRUE: &str = "../wasm_for_tests/vp_always_true.wasm"; - const VP_EVAL: &str = "../wasm_for_tests/vp_eval.wasm"; - #[test] fn test_fetch_or_compile_valid_wasm() { // Load some WASMs and find their hashes and in-memory size - let tx_read_storage_key = load_wasm(TX_READ_STORAGE_KEY); - let tx_no_op = load_wasm(TX_NO_OP); + let tx_read_storage_key = load_wasm(TestWasms::TxReadStorageKey.path()); + let tx_no_op = load_wasm(TestWasms::TxNoOp.path()); // Create a new cache with the limit set to // `max(tx_read_storage_key.size, tx_no_op.size) + 1` @@ -789,8 +784,8 @@ mod test { #[test] fn test_pre_compile_valid_wasm() { // Load some WASMs and find their hashes and in-memory size - let vp_always_true = load_wasm(VP_ALWAYS_TRUE); - let vp_eval = load_wasm(VP_EVAL); + let vp_always_true = load_wasm(TestWasms::VpAlwaysTrue.path()); + let vp_eval = load_wasm(TestWasms::VpEval.path()); // Create a new cache with the limit set to // `max(vp_always_true.size, vp_eval.size) + 1 + extra_bytes` @@ -933,7 +928,7 @@ mod test { } /// Get the WASM code bytes, its hash and find the compiled module's size - fn load_wasm(file: impl AsRef) -> WasmWithMeta { + fn load_wasm(file: impl AsRef) -> WasmWithMeta { // When `WeightScale` calls `loupe::size_of_val` in the cache, for some // reason it returns 8 bytes more than the same call in here. let extra_bytes = 8; @@ -952,7 +947,7 @@ mod test { }; println!( "Compiled module {} size including the hash: {} ({})", - file, + file.to_string_lossy(), Byte::from_bytes(size as u128).get_appropriate_unit(true), size, ); diff --git a/shared/src/vm/wasm/run.rs b/shared/src/vm/wasm/run.rs index 0efdac499c..5bb33441c1 100644 --- a/shared/src/vm/wasm/run.rs +++ b/shared/src/vm/wasm/run.rs @@ -409,6 +409,7 @@ fn get_gas_rules() -> rules::Set { mod tests { use borsh::BorshSerialize; use itertools::Either; + use namada_test_utils::TestWasms; use test_log::test; use wasmer_vm::TrapCode; @@ -417,16 +418,6 @@ mod tests { use crate::types::validity_predicate::EvalVp; use crate::vm::wasm; - const TX_MEMORY_LIMIT_WASM: &str = "../wasm_for_tests/tx_memory_limit.wasm"; - const TX_NO_OP_WASM: &str = "../wasm_for_tests/tx_no_op.wasm"; - const TX_READ_STORAGE_KEY_WASM: &str = - "../wasm_for_tests/tx_read_storage_key.wasm"; - const VP_ALWAYS_TRUE_WASM: &str = "../wasm_for_tests/vp_always_true.wasm"; - const VP_EVAL_WASM: &str = "../wasm_for_tests/vp_eval.wasm"; - const VP_MEMORY_LIMIT_WASM: &str = "../wasm_for_tests/vp_memory_limit.wasm"; - const VP_READ_STORAGE_KEY_WASM: &str = - "../wasm_for_tests/vp_read_storage_key.wasm"; - /// Test that when a transaction wasm goes over the stack-height limit, the /// execution is aborted. #[test] @@ -477,8 +468,7 @@ mod tests { let tx_index = TxIndex::default(); // This code will allocate memory of the given size - let tx_code = - std::fs::read(TX_MEMORY_LIMIT_WASM).expect("cannot load wasm"); + let tx_code = TestWasms::TxMemoryLimit.read_bytes(); // Assuming 200 pages, 12.8 MiB limit assert_eq!(memory::TX_MEMORY_MAX_PAGES, 200); @@ -534,10 +524,9 @@ mod tests { let tx_index = TxIndex::default(); // This code will call `eval` with the other VP below - let vp_eval = std::fs::read(VP_EVAL_WASM).expect("cannot load wasm"); + let vp_eval = TestWasms::VpEval.read_bytes(); // This code will allocate memory of the given size - let vp_memory_limit = - std::fs::read(VP_MEMORY_LIMIT_WASM).expect("cannot load wasm"); + let vp_memory_limit = TestWasms::VpMemoryLimit.read_bytes(); // Assuming 200 pages, 12.8 MiB limit assert_eq!(memory::VP_MEMORY_MAX_PAGES, 200); @@ -615,8 +604,7 @@ mod tests { let tx_index = TxIndex::default(); // This code will allocate memory of the given size - let vp_code = - std::fs::read(VP_MEMORY_LIMIT_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpMemoryLimit.read_bytes(); // Assuming 200 pages, 12.8 MiB limit assert_eq!(memory::VP_MEMORY_MAX_PAGES, 200); @@ -674,7 +662,7 @@ mod tests { let mut gas_meter = BlockGasMeter::default(); let tx_index = TxIndex::default(); - let tx_no_op = std::fs::read(TX_NO_OP_WASM).expect("cannot load wasm"); + let tx_no_op = TestWasms::TxNoOp.read_bytes(); // Assuming 200 pages, 12.8 MiB limit assert_eq!(memory::TX_MEMORY_MAX_PAGES, 200); @@ -728,8 +716,7 @@ mod tests { let verifiers = BTreeSet::new(); let tx_index = TxIndex::default(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // Assuming 200 pages, 12.8 MiB limit assert_eq!(memory::VP_MEMORY_MAX_PAGES, 200); @@ -785,8 +772,7 @@ mod tests { let mut gas_meter = BlockGasMeter::default(); let tx_index = TxIndex::default(); - let tx_read_key = - std::fs::read(TX_READ_STORAGE_KEY_WASM).expect("cannot load wasm"); + let tx_read_key = TestWasms::TxReadStorageKey.read_bytes(); // Allocating `2^24` (16 MiB) for a value in storage that the tx // attempts to read should be above the memory limit and should @@ -832,8 +818,7 @@ mod tests { let verifiers = BTreeSet::new(); let tx_index = TxIndex::default(); - let vp_read_key = - std::fs::read(VP_READ_STORAGE_KEY_WASM).expect("cannot load wasm"); + let vp_read_key = TestWasms::VpReadStorageKey.read_bytes(); // Allocating `2^24` (16 MiB) for a value in storage that the tx // attempts to read should be above the memory limit and should @@ -883,10 +868,9 @@ mod tests { let tx_index = TxIndex::default(); // This code will call `eval` with the other VP below - let vp_eval = std::fs::read(VP_EVAL_WASM).expect("cannot load wasm"); + let vp_eval = TestWasms::VpEval.read_bytes(); // This code will read value from the storage - let vp_read_key = - std::fs::read(VP_READ_STORAGE_KEY_WASM).expect("cannot load wasm"); + let vp_read_key = TestWasms::VpReadStorageKey.read_bytes(); // Allocating `2^24` (16 MiB) for a value in storage that the tx // attempts to read should be above the memory limit and should diff --git a/test_utils/Cargo.toml b/test_utils/Cargo.toml index 3839a36f08..09e12c6806 100644 --- a/test_utils/Cargo.toml +++ b/test_utils/Cargo.toml @@ -9,3 +9,4 @@ version = "0.14.3" [dependencies] borsh = "0.9.0" namada_core = { path = "../core" } +strum = {version = "0.24", features = ["derive"]} diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs index c2a3b13653..6b0352bcc7 100644 --- a/test_utils/src/lib.rs +++ b/test_utils/src/lib.rs @@ -1 +1,96 @@ +//! Utilities for use in tests. + pub mod tx_data; + +use std::env; +use std::path::PathBuf; + +use strum::EnumIter; + +/// Path from the root of the Git repo to the directory under which built test +/// wasms can be found. +pub const WASM_FOR_TESTS_DIR: &str = "wasm_for_tests"; + +/// Corresponds to wasms that we build for tests, under [`WASM_FOR_TESTS_DIR`]. +/// See the `wasm_for_tests/wasm_source` crate for documentation on what these +/// wasms do. +#[allow(missing_docs)] +#[derive(Debug, Clone, Copy, EnumIter)] +pub enum TestWasms { + TxMemoryLimit, + TxMintTokens, + TxNoOp, + TxProposalCode, + TxReadStorageKey, + TxWriteStorageKey, + VpAlwaysFalse, + VpAlwaysTrue, + VpEval, + VpMemoryLimit, + VpReadStorageKey, +} + +impl TestWasms { + /// Get the path to where this test wasm is expected to be, or panic if not + /// able to. + pub fn path(&self) -> PathBuf { + let filename = match self { + TestWasms::TxMemoryLimit => "tx_memory_limit.wasm", + TestWasms::TxMintTokens => "tx_mint_tokens.wasm", + TestWasms::TxNoOp => "tx_no_op.wasm", + TestWasms::TxProposalCode => "tx_proposal_code.wasm", + TestWasms::TxReadStorageKey => "tx_read_storage_key.wasm", + TestWasms::TxWriteStorageKey => "tx_write.wasm", + TestWasms::VpAlwaysFalse => "vp_always_false.wasm", + TestWasms::VpAlwaysTrue => "vp_always_true.wasm", + TestWasms::VpEval => "vp_eval.wasm", + TestWasms::VpMemoryLimit => "vp_memory_limit.wasm", + TestWasms::VpReadStorageKey => "vp_read_storage_key.wasm", + }; + let cwd = + env::current_dir().expect("Couldn't get current working directory"); + // crudely find the root of the repo, we can't rely on the `.git` + // directory being present, so look instead for the presence of a + // CHANGELOG.md file + let repo_root = cwd + .ancestors() + .find(|path| path.join("CHANGELOG.md").exists()) + .unwrap_or_else(|| { + panic!( + "Couldn't find the root of the repository for the current \ + working directory {}", + cwd.to_string_lossy() + ) + }); + repo_root.join(WASM_FOR_TESTS_DIR).join(filename) + } + + /// Attempts to read the contents of this test wasm. Panics if it is not + /// able to for any reason. + pub fn read_bytes(&self) -> Vec { + let path = self.path(); + std::fs::read(&path).unwrap_or_else(|err| { + panic!( + "Could not read wasm at path {}: {:?}", + path.to_string_lossy(), + err + ) + }) + } +} + +#[cfg(test)] +mod tests { + use strum::IntoEnumIterator; + + use super::*; + + #[test] + /// Tests that all expected test wasms are present on disk. + fn test_wasms_path() { + for test_wasm in TestWasms::iter() { + let path = test_wasm.path(); + assert!(path.exists()); + } + } +} diff --git a/tests/src/e2e/eth_bridge_tests.rs b/tests/src/e2e/eth_bridge_tests.rs index 9aec8ec96f..89532df10b 100644 --- a/tests/src/e2e/eth_bridge_tests.rs +++ b/tests/src/e2e/eth_bridge_tests.rs @@ -4,10 +4,11 @@ use borsh::BorshSerialize; use namada::ledger::eth_bridge; use namada_core::types::storage; use namada_test_utils::tx_data::TxWriteData; +use namada_test_utils::TestWasms; use crate::e2e::helpers::get_actor_rpc; use crate::e2e::setup; -use crate::e2e::setup::constants::{wasm_abs_path, ALBERT, TX_WRITE_WASM}; +use crate::e2e::setup::constants::ALBERT; use crate::e2e::setup::{Bin, Who}; use crate::{run, run_as}; @@ -56,7 +57,7 @@ fn test_unauthorized_tx_cannot_write_storage() { ) .unwrap(); - let tx_code_path = wasm_abs_path(TX_WRITE_WASM); + let tx_code_path = TestWasms::TxWriteStorageKey.path(); let tx_data_path = tx_data_path.to_string_lossy().to_string(); let tx_code_path = tx_code_path.to_string_lossy().to_string(); diff --git a/tests/src/e2e/ledger_tests.rs b/tests/src/e2e/ledger_tests.rs index 3fe9eb1c0b..4d6300ad1c 100644 --- a/tests/src/e2e/ledger_tests.rs +++ b/tests/src/e2e/ledger_tests.rs @@ -27,6 +27,7 @@ use namada_apps::client::tx::ShieldedContext; use namada_apps::config::genesis::genesis_config::{ GenesisConfig, ParametersConfig, PosParamsConfig, }; +use namada_test_utils::TestWasms; use serde_json::json; use setup::constants::*; @@ -290,7 +291,7 @@ fn ledger_txs_and_queries() -> Result<()> { let vp_user = wasm_abs_path(VP_USER_WASM); let vp_user = vp_user.to_string_lossy(); - let tx_no_op = wasm_abs_path(TX_NO_OP_WASM); + let tx_no_op = TestWasms::TxNoOp.path(); let tx_no_op = tx_no_op.to_string_lossy(); let validator_one_rpc = get_actor_rpc(&test, &Who::Validator(0)); @@ -1675,7 +1676,7 @@ fn invalid_transactions() -> Result<()> { let data = transfer .try_to_vec() .expect("Encoding unsigned transfer shouldn't fail"); - let tx_wasm_path = wasm_abs_path(TX_MINT_TOKENS_WASM); + let tx_wasm_path = TestWasms::TxMintTokens.path(); std::fs::write(&tx_data_path, data).unwrap(); let tx_wasm_path = tx_wasm_path.to_string_lossy(); let tx_data_path = tx_data_path.to_string_lossy(); @@ -2481,7 +2482,10 @@ fn proposal_submission() -> Result<()> { &test, albert, ProposalType::Default(Some( - wasm_abs_path(TX_PROPOSAL_CODE).to_str().unwrap().to_owned(), + TestWasms::TxProposalCode + .path() + .to_string_lossy() + .to_string(), )), ); let validator_one_rpc = get_actor_rpc(&test, &Who::Validator(0)); diff --git a/tests/src/e2e/multitoken_tests/helpers.rs b/tests/src/e2e/multitoken_tests/helpers.rs index fb1138ca82..7008910b5e 100644 --- a/tests/src/e2e/multitoken_tests/helpers.rs +++ b/tests/src/e2e/multitoken_tests/helpers.rs @@ -8,13 +8,14 @@ use eyre::Context; use namada_core::types::address::Address; use namada_core::types::{storage, token}; use namada_test_utils::tx_data::TxWriteData; +use namada_test_utils::TestWasms; use namada_tx_prelude::storage::KeySeg; use rand::Rng; use regex::Regex; -use super::setup::constants::{wasm_abs_path, NAM, VP_ALWAYS_TRUE_WASM}; +use super::setup::constants::NAM; use super::setup::{Bin, NamadaCmd, Test}; -use crate::e2e::setup::constants::{ALBERT, TX_WRITE_WASM}; +use crate::e2e::setup::constants::ALBERT; use crate::run; const MULTITOKEN_KEY_SEGMENT: &str = "tokens"; @@ -29,9 +30,8 @@ pub fn init_multitoken_vp(test: &Test, rpc_addr: &str) -> Result { // we use a VP that always returns true for the multitoken VP here, as we // are testing out the VPs of the sender and receiver of multitoken // transactions here - not any multitoken VP itself - let multitoken_vp_wasm_path = wasm_abs_path(VP_ALWAYS_TRUE_WASM) - .to_string_lossy() - .to_string(); + let multitoken_vp_wasm_path = + TestWasms::VpAlwaysTrue.path().to_string_lossy().to_string(); let multitoken_alias = "multitoken"; let init_account_args = vec![ @@ -99,7 +99,7 @@ pub fn mint_red_tokens( .push(&BALANCE_KEY_SEGMENT.to_owned())? .push(owner)?; - let tx_code_path = wasm_abs_path(TX_WRITE_WASM); + let tx_code_path = TestWasms::TxWriteStorageKey.path(); let tx_data_path = write_test_file( test, TxWriteData { diff --git a/tests/src/e2e/setup.rs b/tests/src/e2e/setup.rs index 39438b92db..1d6938ca4b 100644 --- a/tests/src/e2e/setup.rs +++ b/tests/src/e2e/setup.rs @@ -832,15 +832,7 @@ pub mod constants { // Paths to the WASMs used for tests pub const TX_TRANSFER_WASM: &str = "wasm/tx_transfer.wasm"; pub const VP_USER_WASM: &str = "wasm/vp_user.wasm"; - pub const TX_NO_OP_WASM: &str = "wasm_for_tests/tx_no_op.wasm"; - pub const TX_INIT_PROPOSAL: &str = "wasm_for_tests/tx_init_proposal.wasm"; - pub const TX_WRITE_WASM: &str = "wasm_for_tests/tx_write.wasm"; pub const TX_IBC_WASM: &str = "wasm/tx_ibc.wasm"; - pub const VP_ALWAYS_TRUE_WASM: &str = "wasm_for_tests/vp_always_true.wasm"; - pub const VP_ALWAYS_FALSE_WASM: &str = - "wasm_for_tests/vp_always_false.wasm"; - pub const TX_MINT_TOKENS_WASM: &str = "wasm_for_tests/tx_mint_tokens.wasm"; - pub const TX_PROPOSAL_CODE: &str = "wasm_for_tests/tx_proposal_code.wasm"; /// Find the absolute path to one of the WASM files above pub fn wasm_abs_path(file_name: &str) -> PathBuf { diff --git a/tests/src/vm_host_env/ibc.rs b/tests/src/vm_host_env/ibc.rs index 7755627c6b..384b1b608b 100644 --- a/tests/src/vm_host_env/ibc.rs +++ b/tests/src/vm_host_env/ibc.rs @@ -68,11 +68,11 @@ use namada::types::ibc::data::{FungibleTokenPacketData, PacketAck}; use namada::types::storage::{self, BlockHash, BlockHeight, Key, TxIndex}; use namada::types::token::{self, Amount}; use namada::vm::{wasm, WasmCacheRwAccess}; +use namada_test_utils::TestWasms; use namada_tx_prelude::StorageWrite; use crate::tx::{self, *}; -const VP_ALWAYS_TRUE_WASM: &str = "../wasm_for_tests/vp_always_true.wasm"; const ADDRESS: Address = Address::Internal(InternalAddress::Ibc); pub struct TestIbcVp<'a> { @@ -196,7 +196,7 @@ pub fn init_storage() -> (Address, Address) { }); // initialize a token - let code = std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let code = TestWasms::VpAlwaysTrue.read_bytes(); let token = tx::ctx().init_account(code.clone()).unwrap(); // initialize an account diff --git a/tests/src/vm_host_env/mod.rs b/tests/src/vm_host_env/mod.rs index 04a545e8b1..cbdfe08342 100644 --- a/tests/src/vm_host_env/mod.rs +++ b/tests/src/vm_host_env/mod.rs @@ -35,6 +35,7 @@ mod tests { use namada::types::time::DateTimeUtc; use namada::types::token::{self, Amount}; use namada::types::{address, key}; + use namada_test_utils::TestWasms; use namada_tx_prelude::{ BorshDeserialize, BorshSerialize, StorageRead, StorageWrite, }; @@ -46,10 +47,6 @@ mod tests { use crate::tx::{tx_host_env, TestTxEnv}; use crate::vp::{vp_host_env, TestVpEnv}; - // paths to the WASMs used for tests - const VP_ALWAYS_TRUE_WASM: &str = "../wasm_for_tests/vp_always_true.wasm"; - const VP_ALWAYS_FALSE_WASM: &str = "../wasm_for_tests/vp_always_false.wasm"; - #[test] fn test_tx_read_write() { // The environment must be initialized first @@ -220,8 +217,7 @@ mod tests { // The environment must be initialized first tx_host_env::init(); - let code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let code = TestWasms::VpAlwaysTrue.read_bytes(); tx::ctx().init_account(code).unwrap(); } @@ -528,16 +524,14 @@ mod tests { assert!(!result); // evaluating the VP template which always returns `true` should pass - let code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let code = TestWasms::VpAlwaysTrue.read_bytes(); let input_data = vec![]; let result = vp::CTX.eval(code, input_data).unwrap(); assert!(result); // evaluating the VP template which always returns `false` shouldn't // pass - let code = - std::fs::read(VP_ALWAYS_FALSE_WASM).expect("cannot load wasm"); + let code = TestWasms::VpAlwaysFalse.read_bytes(); let input_data = vec![]; let result = vp::CTX.eval(code, input_data).unwrap(); assert!(!result); diff --git a/wasm/Cargo.lock b/wasm/Cargo.lock index 03fc87cd5b..0cfc340622 100644 --- a/wasm/Cargo.lock +++ b/wasm/Cargo.lock @@ -1712,6 +1712,12 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.1.19" @@ -2573,6 +2579,7 @@ version = "0.14.3" dependencies = [ "borsh", "namada_core", + "strum", ] [[package]] @@ -2655,6 +2662,7 @@ dependencies = [ "masp_primitives", "masp_proofs", "namada", + "namada_test_utils", "namada_tests", "namada_tx_prelude", "namada_vp_prelude", @@ -3092,7 +3100,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62941722fb675d463659e49c4f3fe1fe792ff24fe5bbaa9c08cd3b98a1c354f5" dependencies = [ "bytes", - "heck", + "heck 0.3.3", "itertools", "lazy_static", "log", @@ -4015,6 +4023,28 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strum" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "subtle" version = "2.4.1" diff --git a/wasm/wasm_source/Cargo.toml b/wasm/wasm_source/Cargo.toml index 0b4295b673..66520c10a1 100644 --- a/wasm/wasm_source/Cargo.toml +++ b/wasm/wasm_source/Cargo.toml @@ -46,6 +46,7 @@ masp_primitives = { git = "https://github.com/anoma/masp", rev = "bee40fc465f6af [dev-dependencies] namada = {path = "../../shared"} namada_tests = {path = "../../tests"} +namada_test_utils = {path = "../../test_utils"} namada_tx_prelude = {path = "../../tx_prelude"} namada_vp_prelude = {path = "../../vp_prelude"} # A fork with state machine testing diff --git a/wasm/wasm_source/src/vp_implicit.rs b/wasm/wasm_source/src/vp_implicit.rs index 31a920a540..377a1b8de3 100644 --- a/wasm/wasm_source/src/vp_implicit.rs +++ b/wasm/wasm_source/src/vp_implicit.rs @@ -203,6 +203,7 @@ mod tests { // Use this as `#[test]` annotation to enable logging use namada::ledger::pos::{GenesisValidator, PosParams}; use namada::types::storage::Epoch; + use namada_test_utils::TestWasms; use namada_tests::log::test; use namada_tests::native_vp::pos::init_pos; use namada_tests::tx::{self, tx_host_env, TestTxEnv}; @@ -215,9 +216,6 @@ mod tests { use super::*; - const VP_ALWAYS_TRUE_WASM: &str = - "../../wasm_for_tests/vp_always_true.wasm"; - /// Test that no-op transaction (i.e. no storage modifications) accepted. #[test] fn test_no_op_transaction() { @@ -765,8 +763,7 @@ mod tests { let secret_key = key::testing::keypair_1(); let public_key = secret_key.ref_to(); let vp_owner: Address = (&public_key).into(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // Spawn the accounts to be able to modify their storage tx_env.spawn_accounts([&vp_owner]); @@ -800,8 +797,7 @@ mod tests { let secret_key = key::testing::keypair_1(); let public_key = secret_key.ref_to(); let vp_owner: Address = (&public_key).into(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); let vp_hash = sha256(&vp_code); tx_env.init_parameters( @@ -846,8 +842,7 @@ mod tests { let secret_key = key::testing::keypair_1(); let public_key = secret_key.ref_to(); let vp_owner: Address = (&public_key).into(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // hardcoded hash of VP_ALWAYS_TRUE_WASM tx_env.init_parameters(None, None, Some(vec!["E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855".to_string()])); diff --git a/wasm/wasm_source/src/vp_testnet_faucet.rs b/wasm/wasm_source/src/vp_testnet_faucet.rs index 1b8802df6e..69dd886e01 100644 --- a/wasm/wasm_source/src/vp_testnet_faucet.rs +++ b/wasm/wasm_source/src/vp_testnet_faucet.rs @@ -109,6 +109,7 @@ fn validate_tx( #[cfg(test)] mod tests { use address::testing::arb_non_internal_address; + use namada_test_utils::TestWasms; // Use this as `#[test]` annotation to enable logging use namada_tests::log::test; use namada_tests::tx::{self, tx_host_env, TestTxEnv}; @@ -121,9 +122,6 @@ mod tests { use super::*; - const VP_ALWAYS_TRUE_WASM: &str = - "../../wasm_for_tests/vp_always_true.wasm"; - /// Allows anyone to withdraw up to 1_000 tokens in a single tx pub const MAX_FREE_DEBIT: i128 = 1_000_000_000; // in micro units @@ -197,8 +195,7 @@ mod tests { let mut tx_env = TestTxEnv::default(); let vp_owner = address::testing::established_address_1(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // Spawn the accounts to be able to modify their storage tx_env.spawn_accounts([&vp_owner]); @@ -233,8 +230,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let keypair = key::testing::keypair_1(); let public_key = &keypair.ref_to(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // Spawn the accounts to be able to modify their storage tx_env.spawn_accounts([&vp_owner]); diff --git a/wasm/wasm_source/src/vp_user.rs b/wasm/wasm_source/src/vp_user.rs index b8cbc20982..efcae94d0f 100644 --- a/wasm/wasm_source/src/vp_user.rs +++ b/wasm/wasm_source/src/vp_user.rs @@ -193,6 +193,7 @@ mod tests { use address::testing::arb_non_internal_address; use namada::ledger::pos::{GenesisValidator, PosParams}; use namada::types::storage::Epoch; + use namada_test_utils::TestWasms; // Use this as `#[test]` annotation to enable logging use namada_tests::log::test; use namada_tests::native_vp::pos::init_pos; @@ -206,9 +207,6 @@ mod tests { use super::*; - const VP_ALWAYS_TRUE_WASM: &str = - "../../wasm_for_tests/vp_always_true.wasm"; - /// Test that no-op transaction (i.e. no storage modifications) accepted. #[test] fn test_no_op_transaction() { @@ -657,8 +655,7 @@ mod tests { let mut tx_env = TestTxEnv::default(); let vp_owner = address::testing::established_address_1(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // Spawn the accounts to be able to modify their storage tx_env.spawn_accounts([&vp_owner]); @@ -694,8 +691,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let keypair = key::testing::keypair_1(); let public_key = keypair.ref_to(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // Spawn the accounts to be able to modify their storage tx_env.spawn_accounts([&vp_owner]); @@ -735,8 +731,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let keypair = key::testing::keypair_1(); let public_key = keypair.ref_to(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // Spawn the accounts to be able to modify their storage tx_env.spawn_accounts([&vp_owner]); @@ -775,8 +770,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let keypair = key::testing::keypair_1(); let public_key = keypair.ref_to(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); let vp_hash = sha256(&vp_code); tx_env.init_parameters(None, Some(vec![vp_hash.to_string()]), None); @@ -818,8 +812,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let keypair = key::testing::keypair_1(); let public_key = keypair.ref_to(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); let vp_hash = sha256(&vp_code); tx_env.init_parameters( @@ -864,8 +857,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let keypair = key::testing::keypair_1(); let public_key = keypair.ref_to(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // hardcoded hash of VP_ALWAYS_TRUE_WASM tx_env.init_parameters(None, None, Some(vec!["E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855".to_string()])); diff --git a/wasm/wasm_source/src/vp_validator.rs b/wasm/wasm_source/src/vp_validator.rs index c9e4700d8e..755af2be83 100644 --- a/wasm/wasm_source/src/vp_validator.rs +++ b/wasm/wasm_source/src/vp_validator.rs @@ -201,6 +201,7 @@ mod tests { use address::testing::arb_non_internal_address; use namada::ledger::pos::{GenesisValidator, PosParams}; use namada::types::storage::Epoch; + use namada_test_utils::TestWasms; // Use this as `#[test]` annotation to enable logging use namada_tests::log::test; use namada_tests::native_vp::pos::init_pos; @@ -215,9 +216,6 @@ mod tests { use super::*; - const VP_ALWAYS_TRUE_WASM: &str = - "../../wasm_for_tests/vp_always_true.wasm"; - /// Test that no-op transaction (i.e. no storage modifications) accepted. #[test] fn test_no_op_transaction() { @@ -678,8 +676,7 @@ mod tests { let mut tx_env = TestTxEnv::default(); let vp_owner = address::testing::established_address_1(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // Spawn the accounts to be able to modify their storage tx_env.spawn_accounts([&vp_owner]); @@ -715,8 +712,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let keypair = key::testing::keypair_1(); let public_key = keypair.ref_to(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // Spawn the accounts to be able to modify their storage tx_env.spawn_accounts([&vp_owner]); @@ -756,8 +752,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let keypair = key::testing::keypair_1(); let public_key = keypair.ref_to(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // Spawn the accounts to be able to modify their storage tx_env.spawn_accounts([&vp_owner]); @@ -796,8 +791,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let keypair = key::testing::keypair_1(); let public_key = keypair.ref_to(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); let vp_hash = sha256(&vp_code); tx_env.init_parameters(None, Some(vec![vp_hash.to_string()]), None); @@ -839,8 +833,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let keypair = key::testing::keypair_1(); let public_key = keypair.ref_to(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); let vp_hash = sha256(&vp_code); tx_env.init_parameters( @@ -885,8 +878,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let keypair = key::testing::keypair_1(); let public_key = keypair.ref_to(); - let vp_code = - std::fs::read(VP_ALWAYS_TRUE_WASM).expect("cannot load wasm"); + let vp_code = TestWasms::VpAlwaysTrue.read_bytes(); // hardcoded hash of VP_ALWAYS_TRUE_WASM tx_env.init_parameters(None, None, Some(vec!["E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855".to_string()])); diff --git a/wasm_for_tests/wasm_source/Cargo.lock b/wasm_for_tests/wasm_source/Cargo.lock index 850d88de2a..1966f111a6 100644 --- a/wasm_for_tests/wasm_source/Cargo.lock +++ b/wasm_for_tests/wasm_source/Cargo.lock @@ -1712,6 +1712,12 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.1.19" @@ -2573,6 +2579,7 @@ version = "0.14.3" dependencies = [ "borsh", "namada_core", + "strum", ] [[package]] @@ -3085,7 +3092,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62941722fb675d463659e49c4f3fe1fe792ff24fe5bbaa9c08cd3b98a1c354f5" dependencies = [ "bytes", - "heck", + "heck 0.3.3", "itertools", "lazy_static", "log", @@ -4008,6 +4015,28 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strum" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "subtle" version = "2.4.1"