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

tx & VP ctx and tests refactors #3787

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namada-eth-bridge = [
namada_core = {path = "../core", features = ["testing"]}
namada_sdk = {path = "../sdk", default-features=false, features = ["download-params", "testing", "wasm-runtime"]}
namada_test_utils = {path = "../test_utils"}
namada_tx_env = {path = "../tx_env"}
namada_tx_env = {path = "../tx_env", features = ["testing"]}
namada_tx_prelude = {path = "../tx_prelude"}
namada_vp = {path = "../vp"}
namada_vp_prelude = {path = "../vp_prelude"}
Expand Down
10 changes: 3 additions & 7 deletions crates/tests/src/integration/ledger_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use namada_node::shell::SnapshotSync;
use namada_node::storage::DbSnapshot;
use namada_sdk::account::AccountPublicKeysMap;
use namada_sdk::collections::HashMap;
use namada_sdk::governance::cli::onchain::{PgfFunding, StewardsUpdate};
use namada_sdk::governance::pgf::cli::steward::Commission;
use namada_sdk::governance::storage::proposal::{PGFInternalTarget, PGFTarget};
use namada_sdk::migrations;
use namada_sdk::queries::RPC;
use namada_sdk::token::{self, DenominatedAmount};
Expand All @@ -46,13 +49,6 @@ use crate::strings::{
TX_APPLIED_SUCCESS, TX_INSUFFICIENT_BALANCE, TX_REJECTED,
};
use crate::tendermint::abci::ApplySnapshotChunkResult;
use crate::tx::tx_host_env::gov_storage::proposal::{
PGFInternalTarget, PGFTarget,
};
use crate::tx::tx_host_env::governance::cli::onchain::{
PgfFunding, StewardsUpdate,
};
use crate::tx::tx_host_env::governance::pgf::cli::steward::Commission;

/// In this test we:
/// 1. Run the ledger node
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#![deny(rustdoc::private_intra_doc_links)]

mod vm_host_env;
pub use vm_host_env::{tx, vp};
pub use vm_host_env::{tx_env, vp};
#[cfg(test)]
mod e2e;
#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions crates/tests/src/native_vp/eth_bridge_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod test_bridge_pool_vp {
use namada_tx_prelude::BatchedTx;

use crate::native_vp::TestNativeVpEnv;
use crate::tx::{tx_host_env, TestTxEnv};
use crate::tx_env::{self, TestTxEnv, TestTxEnvExt};
const ASSET: EthAddress = EthAddress([1; 20]);
const BERTHA_WEALTH: u64 = 1_000_000;
const BERTHA_TOKENS: u64 = 10_000;
Expand Down Expand Up @@ -110,8 +110,8 @@ mod test_bridge_pool_vp {

fn run_vp(tx: BatchedTx) -> bool {
let env = setup_env(tx);
tx_host_env::set(env);
let mut tx_env = tx_host_env::take();
tx_env::set(env);
let mut tx_env = tx_env::take();
tx_env.execute_tx().expect("Test failed.");
let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
&tx_env.gas_meter.borrow(),
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/src/native_vp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use namada_vm::wasm::VpCache;
use namada_vm::WasmCacheRwAccess;
use namada_vp::native_vp::{self, Ctx, NativeVp};

use crate::tx::TestTxEnv;
use crate::tx_env::TestTxEnv;

type NativeVpCtx<'a> = Ctx<
'a,
Expand Down
45 changes: 25 additions & 20 deletions crates/tests/src/native_vp/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ use namada_sdk::proof_of_stake::parameters::{OwnedPosParams, PosParams};
use namada_sdk::proof_of_stake::test_utils::test_init_genesis as init_genesis;
use namada_sdk::proof_of_stake::types::GenesisValidator;

use crate::tx::tx_host_env;
use crate::tx_env;
use crate::tx_env::TestTxEnvExt;

/// initialize proof-of-stake genesis with the given list of validators and
/// parameters.
Expand All @@ -109,9 +110,9 @@ pub fn init_pos(
params: &OwnedPosParams,
start_epoch: Epoch,
) -> PosParams {
tx_host_env::init();
tx_env::init();

tx_host_env::with(|tx_env| {
tx_env::with(|tx_env| {
// Ensure that all the used
// addresses exist
let native_token = tx_env.state.in_mem().native_token.clone();
Expand Down Expand Up @@ -255,7 +256,7 @@ mod tests {
change.clone().apply(true)
}
// Commit the genesis block
tx_host_env::commit_tx_and_block();
tx_env::commit_tx_and_block();

Self {
// we only generate and apply valid actions in the initial state
Expand All @@ -272,19 +273,19 @@ mod tests {
Transition::CommitTx => {
if !test_state.is_current_tx_valid {
// Clear out the changes
tx_host_env::with(|env| {
tx_env::with(|env| {
env.state.drop_tx_batch();
});
}

// Commit the last transaction(s) changes, if any
tx_host_env::commit_tx_and_block();
tx_env::commit_tx_and_block();

// Starting a new tx
test_state.is_current_tx_valid = true;
}
Transition::NextEpoch => {
tx_host_env::with(|env| {
tx_env::with(|env| {
// Clear out the changes
if !test_state.is_current_tx_valid {
env.state.drop_tx_batch();
Expand Down Expand Up @@ -322,7 +323,7 @@ mod tests {
test_state.validate_transitions();

// Clear out the invalid changes
tx_host_env::with(|env| {
tx_env::with(|env| {
env.state.drop_tx_batch();
})
}
Expand Down Expand Up @@ -443,7 +444,7 @@ mod tests {
impl ConcretePosState {
fn validate_transitions(&self) {
// Use the tx_env to run PoS VP
let tx_env = tx_host_env::take();
let tx_env = tx_env::take();

let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter(
&tx_env.gas_meter.borrow(),
Expand All @@ -453,7 +454,7 @@ mod tests {
let result = vp_env.validate_tx(&vp);

// Put the tx_env back before checking the result
tx_host_env::set(vp_env.tx_env);
tx_env::set(vp_env.tx_env);

// The expected result depends on the current state
match (self.is_current_tx_valid, result) {
Expand Down Expand Up @@ -608,7 +609,7 @@ pub mod testing {
use namada_tx_prelude::{Address, StorageRead, StorageWrite};
use proptest::prelude::*;

use crate::tx::{self, tx_host_env};
use crate::tx_env;

#[derive(Clone, Debug, Default)]
pub struct TestValidator {
Expand Down Expand Up @@ -872,9 +873,10 @@ pub mod testing {
pub fn apply(self, is_current_tx_valid: bool) {
// Read the PoS parameters
let params =
read_pos_params::<_, governance::Store<_>>(tx::ctx()).unwrap();
read_pos_params::<_, governance::Store<_>>(tx_env::ctx())
.unwrap();

let current_epoch = tx_host_env::with(|env| {
let current_epoch = tx_env::with(|env| {
// Reset the gas meter on each change, so that we never run
// out in this test
let gas_limit = env.gas_meter.borrow().tx_gas_limit;
Expand Down Expand Up @@ -1079,7 +1081,7 @@ pub mod testing {
) {
match change {
PosStorageChange::SpawnAccount { address } => {
tx_host_env::with(move |env| {
tx_env::with(move |env| {
env.spawn_accounts([&address]);
});
}
Expand Down Expand Up @@ -1331,19 +1333,21 @@ pub mod testing {
}
PosStorageChange::StakingTokenPosBalance { delta } => {
let balance_key = token::storage_key::balance_key(
&tx::ctx().get_native_token().unwrap(),
&tx_env::ctx().get_native_token().unwrap(),
&POS_ADDRESS,
);
let mut balance: token::Amount =
tx::ctx().read(&balance_key).unwrap().unwrap_or_default();
let mut balance: token::Amount = tx_env::ctx()
.read(&balance_key)
.unwrap()
.unwrap_or_default();
if !delta.non_negative() {
let to_spend = token::Amount::from_change(delta);
balance.spend(&to_spend).unwrap();
} else {
let to_recv = token::Amount::from_change(delta);
balance.receive(&to_recv).unwrap();
}
tx::ctx().write(&balance_key, balance).unwrap();
tx_env::ctx().write(&balance_key, balance).unwrap();
}
PosStorageChange::WithdrawUnbond {
owner: _,
Expand Down Expand Up @@ -1587,7 +1591,8 @@ pub mod testing {
pub fn apply(self) {
// Read the PoS parameters
let params =
read_pos_params::<_, governance::Store<_>>(tx::ctx()).unwrap();
read_pos_params::<_, governance::Store<_>>(tx_env::ctx())
.unwrap();

for (epoch, changes) in self.changes {
for change in changes {
Expand All @@ -1603,7 +1608,7 @@ pub mod testing {
current_epoch: Epoch,
) -> bool {
let num_consensus_validators =
get_num_consensus_validators(tx::ctx(), current_epoch).unwrap();
get_num_consensus_validators(tx_env::ctx(), current_epoch).unwrap();
params.max_validator_slots > num_consensus_validators
}
}
16 changes: 8 additions & 8 deletions crates/tests/src/storage_api/collections/lazy_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod tests {
};
use test_log::test;

use crate::tx::tx_host_env;
use crate::tx_env;
use crate::vp::vp_host_env;

prop_state_machine! {
Expand Down Expand Up @@ -200,12 +200,12 @@ mod tests {
_initial_state: &<Self::Reference as ReferenceStateMachine>::State,
) -> Self::SystemUnderTest {
// Init transaction env in which we'll be applying the transitions
tx_host_env::init();
tx_env::init();

// The lazy_map's path must be prefixed by the address to be able
// to trigger a validity predicate on it
let address = address::testing::established_address_1();
tx_host_env::with(|env| env.spawn_accounts([&address]));
tx_env::with(|env| env.spawn_accounts([&address]));
let lazy_map_prefix: storage::Key = address.to_db_key().into();

Self {
Expand All @@ -224,7 +224,7 @@ mod tests {
transition: <Self::Reference as ReferenceStateMachine>::Transition,
) -> Self::SystemUnderTest {
// Apply transitions in transaction env
let ctx = tx_host_env::ctx();
let ctx = tx_env::ctx();

// Persist the transitions in the current tx, or clear previous ones
// if we're committing a tx
Expand All @@ -241,11 +241,11 @@ mod tests {
match &transition {
Transition::CommitTx => {
// commit the tx without committing the block
tx_host_env::with(|env| env.state.commit_tx_batch());
tx_env::with(|env| env.state.commit_tx_batch());
}
Transition::CommitTxAndBlock => {
// commit the tx and the block
tx_host_env::commit_tx_and_block();
tx_env::commit_tx_and_block();
}
Transition::Insert(key, value) => {
state.lazy_map.insert(ctx, *key, value.clone()).unwrap();
Expand Down Expand Up @@ -380,7 +380,7 @@ mod tests {
fn assert_validation_accepted(&self) {
// Init the VP env from tx env in which we applied the map
// transitions
let tx_env = tx_host_env::take();
let tx_env = tx_env::take();
vp_host_env::init_from_tx(self.address.clone(), tx_env, |_| {});

// Simulate a validity predicate run using the lazy map's validation
Expand Down Expand Up @@ -497,7 +497,7 @@ mod tests {
}

// Put the tx_env back before checking the result
tx_host_env::set_from_vp_env(vp_host_env::take());
tx_env::set_from_vp_env(vp_host_env::take());
}
}

Expand Down
16 changes: 8 additions & 8 deletions crates/tests/src/storage_api/collections/lazy_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod tests {
};
use test_log::test;

use crate::tx::tx_host_env;
use crate::tx_env;
use crate::vp::vp_host_env;

prop_state_machine! {
Expand Down Expand Up @@ -188,12 +188,12 @@ mod tests {
_initial_state: &<Self::Reference as ReferenceStateMachine>::State,
) -> Self::SystemUnderTest {
// Init transaction env in which we'll be applying the transitions
tx_host_env::init();
tx_env::init();

// The lazy_set's path must be prefixed by the address to be able
// to trigger a validity predicate on it
let address = address::testing::established_address_1();
tx_host_env::with(|env| env.spawn_accounts([&address]));
tx_env::with(|env| env.spawn_accounts([&address]));
let lazy_set_prefix: storage::Key = address.to_db_key().into();

Self {
Expand All @@ -212,7 +212,7 @@ mod tests {
transition: <Self::Reference as ReferenceStateMachine>::Transition,
) -> Self::SystemUnderTest {
// Apply transitions in transaction env
let ctx = tx_host_env::ctx();
let ctx = tx_env::ctx();

// Persist the transitions in the current tx, or clear previous ones
// if we're committing a tx
Expand All @@ -229,11 +229,11 @@ mod tests {
match &transition {
Transition::CommitTx => {
// commit the tx without committing the block
tx_host_env::with(|env| env.state.commit_tx_batch());
tx_env::with(|env| env.state.commit_tx_batch());
}
Transition::CommitTxAndBlock => {
// commit the tx and the block
tx_host_env::commit_tx_and_block();
tx_env::commit_tx_and_block();
}
Transition::Insert(key) => {
state.lazy_set.insert(ctx, *key).unwrap();
Expand Down Expand Up @@ -343,7 +343,7 @@ mod tests {
fn assert_validation_accepted(&self) {
// Init the VP env from tx env in which we applied the set
// transitions
let tx_env = tx_host_env::take();
let tx_env = tx_env::take();
vp_host_env::init_from_tx(self.address.clone(), tx_env, |_| {});

// Simulate a validity predicate run using the lazy set's validation
Expand Down Expand Up @@ -460,7 +460,7 @@ mod tests {
}

// Put the tx_env back before checking the result
tx_host_env::set_from_vp_env(vp_host_env::take());
tx_env::set_from_vp_env(vp_host_env::take());
}
}

Expand Down
Loading
Loading