Skip to content

Commit

Permalink
Merge remote-tracking branch 'namada/james/mainline/namada-test-utils…
Browse files Browse the repository at this point in the history
…-wasms' (#893) into main

* namada/james/mainline/namada-test-utils-wasms:
  Update shared crate to use test utils
  Update namada_tests crate to use test utils
  Don't rely on the presence of .git directory
  Test that all test wasm files are present
  Update wasm crate to use test utils
  Update namada_apps to use test wasm utility code
  Add changelog
  Add test wasm utility code to test_utils
  • Loading branch information
juped committed Apr 13, 2023
2 parents 4de6ded + 6e35287 commit 31dd7cf
Show file tree
Hide file tree
Showing 23 changed files with 259 additions and 126 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/testing/893-namada-test-utils-wasms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add utility code for working with test wasms
([#893](https://github.com/anoma/namada/pull/893))
33 changes: 32 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
6 changes: 2 additions & 4 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
5 changes: 2 additions & 3 deletions shared/src/ledger/queries/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,14 @@ where
#[cfg(test)]
mod test {
use borsh::BorshDeserialize;
use namada_test_utils::TestWasms;

use crate::ledger::queries::testing::TestClient;
use crate::ledger::queries::RPC;
use crate::ledger::storage_api::{self, StorageWrite};
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();
Expand Down Expand Up @@ -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
Expand Down
19 changes: 7 additions & 12 deletions shared/src/vm/wasm/compilation_cache/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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<str>) -> WasmWithMeta {
fn load_wasm(file: impl AsRef<Path>) -> 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;
Expand All @@ -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,
);
Expand Down
38 changes: 11 additions & 27 deletions shared/src/vm/wasm/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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]
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ version = "0.14.3"
[dependencies]
borsh = "0.9.0"
namada_core = { path = "../core" }
strum = {version = "0.24", features = ["derive"]}
Loading

0 comments on commit 31dd7cf

Please sign in to comment.