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

Allow testing wasm with Native VPs #738

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions tests/src/vm_host_env/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use namada::types::storage::Key;
use namada::types::time::DurationSecs;
use namada::types::{key, token};
use namada::vm::prefix_iter::PrefixIterators;
use namada::vm::wasm::run::Error;
use namada::vm::wasm::{self, TxCache, VpCache};
use namada::vm::{self, WasmCacheRwAccess};
use namada_tx_prelude::{BorshSerialize, Ctx};
Expand Down Expand Up @@ -111,14 +112,23 @@ impl TestTxEnv {
);
}

/// Fake accounts existence by initializating their VP storage.
/// Fake accounts' existence by initializing their VP storage.
/// This is needed for accounts that are being modified by a tx test to
/// pass account existence check in `tx_write` function.
/// pass account existence check in `tx_write` function. Only established
/// addresses ([`Address::Established`]) have their VP storage initialized,
/// as other types of accounts should not have wasm VPs in storage in any
/// case.
pub fn spawn_accounts(
&mut self,
addresses: impl IntoIterator<Item = impl Borrow<Address>>,
) {
for address in addresses {
if matches!(
address.borrow(),
Address::Internal(_) | Address::Implicit(_)
) {
continue;
}
let key = Key::validity_predicate(address.borrow());
let vp_code = vec![];
self.storage
Expand All @@ -143,9 +153,17 @@ impl TestTxEnv {
&mut self,
target: &Address,
token: &Address,
sub_prefix: Option<Key>,
amount: token::Amount,
) {
let storage_key = token::balance_key(token, target);
let storage_key = match &sub_prefix {
Some(sub_prefix) => {
let prefix =
token::multitoken_balance_prefix(token, sub_prefix);
token::multitoken_balance_key(&prefix, target)
}
None => token::balance_key(token, target),
};
self.storage
.write(&storage_key, amount.try_to_vec().unwrap())
.unwrap();
Expand All @@ -162,6 +180,21 @@ impl TestTxEnv {
.write(&storage_key, public_key.try_to_vec().unwrap())
.unwrap();
}

/// Apply the tx changes to the write log.
pub fn execute_tx(&mut self) -> Result<(), Error> {
let empty_data = vec![];
wasm::run::tx(
&self.storage,
&mut self.write_log,
&mut self.gas_meter,
&self.tx.code,
self.tx.data.as_ref().unwrap_or(&empty_data),
&mut self.vp_wasm_cache,
&mut self.tx_wasm_cache,
)
.and(Ok(()))
}
}

/// This module allows to test code with tx host environment functions.
Expand Down
7 changes: 6 additions & 1 deletion wasm/wasm_source/src/tx_bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ mod tests {

// Ensure that the bond's source has enough tokens for the bond
let target = bond.source.as_ref().unwrap_or(&bond.validator);
tx_env.credit_tokens(target, &staking_token_address(), bond.amount);
tx_env.credit_tokens(
target,
&staking_token_address(),
None,
bond.amount,
);
});

let tx_code = vec![];
Expand Down
1 change: 1 addition & 0 deletions wasm/wasm_source/src/tx_unbond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ mod tests {
tx_env.credit_tokens(
source,
&staking_token_address(),
None,
initial_stake,
);
}
Expand Down
1 change: 1 addition & 0 deletions wasm/wasm_source/src/tx_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ mod tests {
tx_env.credit_tokens(
source,
&staking_token_address(),
None,
initial_stake,
);
}
Expand Down
6 changes: 3 additions & 3 deletions wasm/wasm_source/src/vp_testnet_faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ mod tests {

// Credit the tokens to the source before running the transaction to be
// able to transfer from it
tx_env.credit_tokens(&source, &token, amount);
tx_env.credit_tokens(&source, &token, None, amount);

// Initialize VP environment from a transaction
vp_host_env::init_from_tx(vp_owner.clone(), tx_env, |address| {
Expand Down Expand Up @@ -275,7 +275,7 @@ mod tests {

// Credit the tokens to the VP owner before running the transaction to
// be able to transfer from it
tx_env.credit_tokens(&vp_owner, &token, amount);
tx_env.credit_tokens(&vp_owner, &token, None, amount);

// Initialize VP environment from a transaction
vp_host_env::init_from_tx(vp_owner.clone(), tx_env, |address| {
Expand Down Expand Up @@ -308,7 +308,7 @@ mod tests {

// Credit the tokens to the VP owner before running the transaction to
// be able to transfer from it
tx_env.credit_tokens(&vp_owner, &token, amount);
tx_env.credit_tokens(&vp_owner, &token, None, amount);

// Initialize VP environment from a transaction
vp_host_env::init_from_tx(vp_owner.clone(), tx_env, |address| {
Expand Down
8 changes: 4 additions & 4 deletions wasm/wasm_source/src/vp_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ mod tests {

// Credit the tokens to the source before running the transaction to be
// able to transfer from it
tx_env.credit_tokens(&source, &token, amount);
tx_env.credit_tokens(&source, &token, None, amount);

// Initialize VP environment from a transaction
vp_host_env::init_from_tx(vp_owner.clone(), tx_env, |address| {
Expand Down Expand Up @@ -273,7 +273,7 @@ mod tests {

// Credit the tokens to the VP owner before running the transaction to
// be able to transfer from it
tx_env.credit_tokens(&vp_owner, &token, amount);
tx_env.credit_tokens(&vp_owner, &token, None, amount);

// Initialize VP environment from a transaction
vp_host_env::init_from_tx(vp_owner.clone(), tx_env, |address| {
Expand Down Expand Up @@ -319,7 +319,7 @@ mod tests {

// Credit the tokens to the VP owner before running the transaction to
// be able to transfer from it
tx_env.credit_tokens(&vp_owner, &token, amount);
tx_env.credit_tokens(&vp_owner, &token, None, amount);

tx_env.write_public_key(&vp_owner, &public_key);

Expand Down Expand Up @@ -369,7 +369,7 @@ mod tests {

// Credit the tokens to the VP owner before running the transaction to
// be able to transfer from it
tx_env.credit_tokens(&source, &token, amount);
tx_env.credit_tokens(&source, &token, None, amount);

// Initialize VP environment from a transaction
vp_host_env::init_from_tx(vp_owner.clone(), tx_env, |address| {
Expand Down