Skip to content

Commit

Permalink
Merge remote-tracking branches 'james/mainline/dont-spawn-internal-ac…
Browse files Browse the repository at this point in the history
…count-vps' (#694) and 'bat/native-vp-test-tooling' (#738)

* james/mainline/dont-spawn-internal-account-vps:
  Remove comment
  Update spawn_accounts to ignore implicit addresses
  Add changelog
  TestTxEnv::spawn_accounts should ignore internal addresses

* bat/native-vp-test-tooling:
  Update tests/src/vm_host_env/tx.rs
  [feat]: Add multitoken suppor to the TestTxEnv. Add ability to execute wasm blobs on a TestTxEnv in order to test them with native vps
  • Loading branch information
tzemanovic committed Nov 17, 2022
3 parents 48cf0d3 + f6e4dbc + 490b9e5 commit 79a26e9
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Don't fake a wasm VP for internal addresses in tx tests
([#694](https://github.com/anoma/namada/pull/694))
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, TxIndex};
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 @@ -113,14 +114,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 @@ -145,9 +155,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 @@ -164,6 +182,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 @@ -277,7 +277,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 @@ -310,7 +310,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 @@ -234,7 +234,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 @@ -280,7 +280,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 @@ -328,7 +328,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 @@ -380,7 +380,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

0 comments on commit 79a26e9

Please sign in to comment.