Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the test wasm tx_write_storage_key to be able to write any arbitrary value #894

Merged
merged 12 commits into from
Feb 7, 2023
Merged
2 changes: 2 additions & 0 deletions .changelog/unreleased/testing/894-tx-write-storage-key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Modify tx_write_storage_key test wasm to be able to modify any arbitrary value
([#894](https://github.com/anoma/namada/pull/894))
10 changes: 10 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"core",
"proof_of_stake",
"shared",
"test_utils",
"tests",
"tx_prelude",
"vm_env",
Expand Down
11 changes: 11 additions & 0 deletions test_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
authors = ["Heliax AG <[email protected]>"]
edition = "2021"
license = "GPL-3.0"
name = "namada_test_utils"
resolver = "2"
version = "0.12.2"

[dependencies]
borsh = "0.9.0"
namada_core = { path = "../core" }
3 changes: 3 additions & 0 deletions test_utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# test_utils

Contains helper functionality and fixtures for use in tests. This crate must be wasm-buildable by default as it may be used from within our test wasms.
1 change: 1 addition & 0 deletions test_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod tx_data;
25 changes: 25 additions & 0 deletions test_utils/src/tx_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! Types that are meant to be serialized and used as the data component of a
//! Namada transaction.

use borsh::{BorshDeserialize, BorshSerialize};
use namada_core::types::storage;

/// Represents an arbitrary write to storage at the specified key. This should
/// be used alongside the test `tx_write.wasm`.
#[derive(
Clone,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
BorshSerialize,
BorshDeserialize,
)]
pub struct TxWriteData {
/// The storage key to be written to.
pub key: storage::Key,
/// The bytes to be written.
pub value: Vec<u8>,
}
2 changes: 2 additions & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ wasm-runtime = ["namada/wasm-runtime"]

[dependencies]
namada = {path = "../shared", default-features = false, features = ["testing"]}
namada_core = {path = "../core", default-features = false, features = ["testing"]}
namada_test_utils = {path = "../test_utils"}
namada_vp_prelude = {path = "../vp_prelude", default-features = false}
namada_tx_prelude = {path = "../tx_prelude", default-features = false}
chrono = {version = "0.4.22", default-features = false, features = ["clock", "std"]}
Expand Down
33 changes: 18 additions & 15 deletions tests/src/e2e/eth_bridge_tests.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
use borsh::BorshSerialize;
use namada::ledger::eth_bridge;
use namada_core::types::storage;
use namada_core::types::storage::KeySeg;
use namada_test_utils::tx_data::TxWriteData;

use crate::e2e::helpers::get_actor_rpc;
use crate::e2e::setup;
use crate::e2e::setup::constants::{
wasm_abs_path, ALBERT, TX_WRITE_STORAGE_KEY_WASM,
};
use crate::e2e::setup::constants::{wasm_abs_path, ALBERT, TX_WRITE_WASM};
use crate::e2e::setup::{Bin, Who};
use crate::{run, run_as};

const ETH_BRIDGE_ADDRESS: &str = "atest1v9hx7w36g42ysgzzwf5kgem9ypqkgerjv4ehxgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpq8f99ew";

/// # Examples
///
/// ```
/// let storage_key = storage_key("queue");
/// assert_eq!(storage_key, "#atest1v9hx7w36g42ysgzzwf5kgem9ypqkgerjv4ehxgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpq8f99ew/queue");
/// ```
fn storage_key(path: &str) -> String {
format!("#{ETH_BRIDGE_ADDRESS}/{}", path)
}

#[test]
fn everything() {
const LEDGER_STARTUP_TIMEOUT_SECONDS: u64 = 30;
Expand All @@ -44,9 +38,18 @@ fn everything() {
let _bg_ledger = namadan_ledger.background();

let tx_data_path = test.test_dir.path().join("queue_storage_key.txt");
std::fs::write(&tx_data_path, &storage_key("queue")[..]).unwrap();
std::fs::write(
&tx_data_path,
TxWriteData {
key: storage::Key::from(eth_bridge::vp::ADDRESS.to_db_key()),
value: b"arbitrary value".to_vec(),
}
.try_to_vec()
.unwrap(),
)
.unwrap();

let tx_code_path = wasm_abs_path(TX_WRITE_STORAGE_KEY_WASM);
let tx_code_path = wasm_abs_path(TX_WRITE_WASM);

let tx_data_path = tx_data_path.to_string_lossy().to_string();
let tx_code_path = tx_code_path.to_string_lossy().to_string();
Expand Down
3 changes: 1 addition & 2 deletions tests/src/e2e/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,8 +834,7 @@ pub mod constants {
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_STORAGE_KEY_WASM: &str =
"wasm_for_tests/tx_write_storage_key.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 =
Expand Down
10 changes: 10 additions & 0 deletions wasm/Cargo.lock

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

Binary file added wasm_for_tests/tx_write.wasm
Binary file not shown.
Binary file removed wasm_for_tests/tx_write_storage_key.wasm
Binary file not shown.
11 changes: 11 additions & 0 deletions wasm_for_tests/wasm_source/Cargo.lock

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

3 changes: 2 additions & 1 deletion wasm_for_tests/wasm_source/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tx_memory_limit = []
tx_mint_tokens = []
tx_no_op = []
tx_read_storage_key = []
tx_write_storage_key = []
tx_write = []
vp_always_false = []
vp_always_true = []
vp_eval = []
Expand All @@ -25,6 +25,7 @@ vp_read_storage_key = []
tx_proposal_code = []

[dependencies]
namada_test_utils = {path = "../../test_utils"}
namada_tx_prelude = {path = "../../tx_prelude"}
namada_vp_prelude = {path = "../../vp_prelude"}
borsh = "0.9.1"
Expand Down
2 changes: 1 addition & 1 deletion wasm_for_tests/wasm_source/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ wasms := tx_memory_limit
wasms += tx_mint_tokens
wasms += tx_no_op
wasms += tx_read_storage_key
wasms += tx_write_storage_key
wasms += tx_write
wasms += vp_always_false
wasms += vp_always_true
wasms += vp_eval
Expand Down
50 changes: 28 additions & 22 deletions wasm_for_tests/wasm_source/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ pub mod main {
}

/// A tx that attempts to write arbitrary data to the given key
#[cfg(feature = "tx_write_storage_key")]
#[cfg(feature = "tx_write")]
pub mod main {
use namada_tx_prelude::*;
use borsh::BorshDeserialize;
use namada_test_utils::tx_data::TxWriteData;
use namada_tx_prelude::{
log_string, transaction, Ctx, ResultExt, SignedTxData, StorageRead,
StorageWrite, TxResult,
};

const TX_NAME: &str = "tx_write";

const ARBITRARY_VALUE: &str = "arbitrary value";

fn log(msg: &str) {
log_string(format!("[{}] {}", TX_NAME, msg))
}
Expand Down Expand Up @@ -94,28 +97,31 @@ pub mod main {
fatal_msg("no data provided");
}
};
let key = match String::from_utf8(data) {
Ok(key) => {
let key = storage::Key::parse(key).unwrap();
log(&format!("parsed key from data: {}", key));
key
}
Err(error) => fatal("getting key", error),
};
let val: Option<String> = ctx.read(&key)?;
match val {
Some(val) => {
log(&format!("preexisting val is {}", val));
let TxWriteData { key, value } =
match TxWriteData::try_from_slice(&data[..]) {
Ok(write_op) => {
log(&format!(
"parsed WriteOp to key {} ({} bytes)",
&write_op.key,
&write_op.value.len(),
));
write_op
}
Err(error) => fatal("deserializing WriteOp", error),
};
let existing_value: Option<String> = ctx.read(&key)?;
match existing_value {
Some(existing_value) => {
log(&format!("already present value is {}", existing_value));
}
None => {
log("no preexisting val");
log("no already present value");
}
}
log(&format!(
"attempting to write new value {} to key {}",
ARBITRARY_VALUE, key
));
ctx.write(&key, ARBITRARY_VALUE)?;
log(&format!("attempting to write new value to key {}", key));
// using `ctx.write_bytes` instead of `ctx.write` here, as we want to
// write the actual bytes, not a Borsh-serialization of a `Vec<u8>`
ctx.write_bytes(&key, &value[..])?;
Ok(())
}
}
Expand Down