Skip to content

Commit

Permalink
fixup! Merge branch 'brent/improve-wasm-build' (#2795)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Apr 10, 2024
1 parent d70d49f commit cb6dcf6
Show file tree
Hide file tree
Showing 27 changed files with 302 additions and 89 deletions.
1 change: 0 additions & 1 deletion wasm/vp_implicit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namada_tx_prelude.workspace = true
namada_vp_prelude.workspace = true
wee_alloc.workspace = true
getrandom.workspace = true
once_cell.workspace = true

[dev-dependencies]
namada = {path = "../../crates/namada"}
Expand Down
1 change: 0 additions & 1 deletion wasm/vp_user/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namada_tx_prelude.workspace = true
namada_vp_prelude.workspace = true
wee_alloc.workspace = true
getrandom.workspace = true
once_cell.workspace = true

[dev-dependencies]
namada = {path = "../../crates/namada"}
Expand Down
13 changes: 9 additions & 4 deletions wasm_for_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
resolver = "2"

members = [
"tx_fail",
"tx_infinite_guest_gas",
"tx_infinite_host_gas",
"tx_invalid_data",
"tx_memory_limit",
"tx_no_op",
"tx_fail",
"tx_proposal_code",
"tx_proposal_ibc_token_inflation",
"tx_proposal_masp_reward",
"tx_read_storage_key",
"tx_write",
"vp_always_false",
"vp_always_true",
"vp_eval",
"vp_infinite_guest_gas",
"vp_infinite_host_gas",
"vp_memory_limit",
"vp_read_storage_key",
"tx_proposal_code",
"tx_proposal_masp_reward",
"tx_proposal_ibc_token_inflation",
]

[workspace.package]
Expand Down
15 changes: 10 additions & 5 deletions wasm_for_tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ nightly := $(shell cat ../rust-nightly-version)

# All the wasms that can be built from this source, switched via Cargo features
# Wasms can be added via the Cargo.toml `[features]` list.
wasms := tx_memory_limit
wasms := tx_fail
wasms += tx_infinite_guest_gas
wasms += tx_infinite_host_gas
wasms += tx_invalid_data
wasms += tx_memory_limit
wasms += tx_no_op
wasms += tx_fail
wasms += tx_proposal_code
wasms += tx_proposal_ibc_token_inflation
wasms += tx_proposal_masp_reward
wasms += tx_read_storage_key
wasms += tx_write
wasms += vp_always_false
wasms += vp_always_true
wasms += vp_eval
wasms += vp_infinite_guest_gas
wasms += vp_infinite_host_gas
wasms += vp_memory_limit
wasms += vp_read_storage_key
wasms += tx_proposal_code
wasms += tx_proposal_masp_reward
wasms += tx_proposal_ibc_token_inflation


# Build all wasms in release mode
Expand Down
4 changes: 2 additions & 2 deletions wasm_for_tests/tx_fail/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use namada_tx_prelude::*;

#[transaction(gas = 1000)]
#[transaction]
fn apply_tx(_ctx: &mut Ctx, _tx_data: Tx) -> TxResult {
Err(Error::SimpleMessage("failed tx"))
}
}
29 changes: 29 additions & 0 deletions wasm_for_tests/tx_infinite_guest_gas/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "tx_infinite_guest_gas"
description = "Wasm transaction used for testing."
authors.workspace = true
edition.workspace = true
license.workspace = true
version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
namada_test_utils.workspace = true
namada_tx_prelude.workspace = true
namada_vp_prelude.workspace = true
wee_alloc.workspace = true
getrandom.workspace = true

[dev-dependencies]
namada_tests = { path = "../../crates/tests", default-features = false, features = [
"wasm-runtime",
] }

proptest = "1.4.0"
test-log = {version = "0.2.14", default-features = false, features = ["trace"]}
tracing = "0.1.30"
tracing-subscriber = {version = "0.3.7", default-features = false, features = ["env-filter", "fmt"]}

[lib]
crate-type = ["cdylib"]
8 changes: 8 additions & 0 deletions wasm_for_tests/tx_infinite_guest_gas/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use namada_tx_prelude::*;

/// A tx that endlessly charges gas from the guest environment
#[transaction]
fn apply_tx(_ctx: &mut Ctx, _tx_data: Tx) -> TxResult {
#[allow(clippy::empty_loop)]
loop {}
}
29 changes: 29 additions & 0 deletions wasm_for_tests/tx_infinite_host_gas/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "tx_infinite_host_gas"
description = "Wasm transaction used for testing."
authors.workspace = true
edition.workspace = true
license.workspace = true
version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
namada_test_utils.workspace = true
namada_tx_prelude.workspace = true
namada_vp_prelude.workspace = true
wee_alloc.workspace = true
getrandom.workspace = true

[dev-dependencies]
namada_tests = { path = "../../crates/tests", default-features = false, features = [
"wasm-runtime",
] }

proptest = "1.4.0"
test-log = {version = "0.2.14", default-features = false, features = ["trace"]}
tracing = "0.1.30"
tracing-subscriber = {version = "0.3.7", default-features = false, features = ["env-filter", "fmt"]}

[lib]
crate-type = ["cdylib"]
12 changes: 12 additions & 0 deletions wasm_for_tests/tx_infinite_host_gas/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use namada_tx_prelude::*;

/// A tx that endlessly charges gas from the host environment
#[transaction]
fn apply_tx(ctx: &mut Ctx, _tx_data: Tx) -> TxResult {
let target_key = parameters_storage::get_tx_allowlist_storage_key();
loop {
// NOTE: don't propagate the error to verify that execution abortion
// is done in host and does not require guest cooperation
let _ = ctx.write(&target_key, vec!["hash"]);
}
}
29 changes: 29 additions & 0 deletions wasm_for_tests/tx_invalid_data/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "tx_invalid_data"
description = "Wasm transaction used for testing."
authors.workspace = true
edition.workspace = true
license.workspace = true
version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
namada_test_utils.workspace = true
namada_tx_prelude.workspace = true
namada_vp_prelude.workspace = true
wee_alloc.workspace = true
getrandom.workspace = true

[dev-dependencies]
namada_tests = { path = "../../crates/tests", default-features = false, features = [
"wasm-runtime",
] }

proptest = "1.4.0"
test-log = {version = "0.2.14", default-features = false, features = ["trace"]}
tracing = "0.1.30"
tracing-subscriber = {version = "0.3.7", default-features = false, features = ["env-filter", "fmt"]}

[lib]
crate-type = ["cdylib"]
11 changes: 11 additions & 0 deletions wasm_for_tests/tx_invalid_data/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use namada_tx_prelude::*;

#[transaction]
fn apply_tx(ctx: &mut Ctx, tx_data: Tx) -> TxResult {
let signed = tx_data;
let _data = signed.data().ok_or_err_msg("Missing data").map_err(|err| {
ctx.set_commitment_sentinel();
err
})?;
Ok(())
}
2 changes: 1 addition & 1 deletion wasm_for_tests/tx_memory_limit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use namada_tx_prelude::*;

#[transaction(gas = 1000)]
#[transaction]
fn apply_tx(_ctx: &mut Ctx, tx_data: Tx) -> TxResult {
let len =
usize::try_from_slice(&tx_data.data().as_ref().unwrap()[..]).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion wasm_for_tests/tx_no_op/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use namada_tx_prelude::*;

#[transaction(gas = 1000)]
#[transaction]
fn apply_tx(_ctx: &mut Ctx, _tx_data: Tx) -> TxResult {
Ok(())
}
8 changes: 4 additions & 4 deletions wasm_for_tests/tx_proposal_code/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use namada_tx_prelude::*;

#[transaction(gas = 1000)]
#[transaction]
fn apply_tx(ctx: &mut Ctx, _tx_data: Tx) -> TxResult {
// governance
let target_key = gov_storage::keys::get_min_proposal_grace_epoch_key();
let target_key = gov_storage::keys::get_min_proposal_grace_epochs_key();
ctx.write(&target_key, 9_u64)?;

// parameters
let target_key = parameters_storage::get_tx_allowlist_storage_key();
let target_key = parameters_storage::get_vp_allowlist_storage_key();
ctx.write(&target_key, vec!["hash"])?;
Ok(())
}
}
12 changes: 5 additions & 7 deletions wasm_for_tests/tx_proposal_ibc_token_inflation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const IBC_TOKEN_DENOM: u8 = 0;
const CHANNEL_ID: &str = "channel-0";
const BASE_TOKEN: &str = "tnam1qyvfwdkz8zgs9n3qn9xhp8scyf8crrxwuq26r6gy";

#[transaction(gas = 1000)]
#[transaction]
fn apply_tx(ctx: &mut Ctx, _tx_data: Tx) -> TxResult {
let ibc_denom = format!("transfer/{CHANNEL_ID}/{BASE_TOKEN}");
let ibc_token = ibc::ibc_token(&ibc_denom);
Expand All @@ -27,14 +27,12 @@ fn apply_tx(ctx: &mut Ctx, _tx_data: Tx) -> TxResult {
token::storage_key::masp_kd_gain_key(&ibc_token);

let token_map_key = token::storage_key::masp_token_map_key();
let mut token_map: masp::TokenMap = ctx.read(&token_map_key)?.unwrap_or_default();
let mut token_map: masp::TokenMap =
ctx.read(&token_map_key)?.unwrap_or_default();
token_map.insert(ibc_denom, ibc_token);
ctx.write(&token_map_key, token_map)?;

ctx.write(
&shielded_token_last_inflation_key,
token::Amount::zero(),
)?;
ctx.write(&shielded_token_last_inflation_key, token::Amount::zero())?;
ctx.write(
&shielded_token_last_locked_amount_key,
token::Amount::zero(),
Expand All @@ -56,4 +54,4 @@ fn apply_tx(ctx: &mut Ctx, _tx_data: Tx) -> TxResult {
Dec::from_str("120000").unwrap(),
)?;
Ok(())
}
}
4 changes: 2 additions & 2 deletions wasm_for_tests/tx_proposal_masp_reward/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;
use dec::Dec;
use namada_tx_prelude::*;

#[transaction(gas = 1000)]
#[transaction]
fn apply_tx(ctx: &mut Ctx, _tx_data: Tx) -> TxResult {
let native_token = ctx.get_native_token()?;
let shielded_rewards_key =
Expand All @@ -12,4 +12,4 @@ fn apply_tx(ctx: &mut Ctx, _tx_data: Tx) -> TxResult {
ctx.write(&shielded_rewards_key, Dec::from_str("0.05").unwrap())?;

Ok(())
}
}
4 changes: 2 additions & 2 deletions wasm_for_tests/tx_read_storage_key/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use namada_tx_prelude::*;

#[transaction(gas = 1000)]
#[transaction]
fn apply_tx(ctx: &mut Ctx, tx_data: Tx) -> TxResult {
// Allocates a memory of size given from the `tx_data (usize)`
let key =
Expand All @@ -9,4 +9,4 @@ fn apply_tx(ctx: &mut Ctx, tx_data: Tx) -> TxResult {
log_string(format!("key {}", key));
let _result: Vec<u8> = ctx.read(&key)?.unwrap();
Ok(())
}
}
Loading

0 comments on commit cb6dcf6

Please sign in to comment.