From a673458c61634d91d23ed9627c26b3c88cfb47b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 11 Jan 2023 17:01:49 +0100 Subject: [PATCH] tests: update tests to use WlStorage --- .../storage_api/collections/lazy_map.rs | 4 +- .../storage_api/collections/lazy_vec.rs | 4 +- core/src/ledger/testnet_pow.rs | 5 +- tests/src/native_vp/mod.rs | 4 +- tests/src/native_vp/pos.rs | 21 +- tests/src/storage_api/collections/lazy_map.rs | 2 +- tests/src/storage_api/collections/lazy_vec.rs | 2 +- .../collections/nested_lazy_map.rs | 2 +- tests/src/vm_host_env/ibc.rs | 20 +- tests/src/vm_host_env/mod.rs | 184 +++++++++++++----- tests/src/vm_host_env/tx.rs | 77 ++++---- tests/src/vm_host_env/vp.rs | 44 +++-- wasm/wasm_source/src/tx_bond.rs | 2 +- wasm/wasm_source/src/tx_unbond.rs | 2 +- wasm/wasm_source/src/tx_withdraw.rs | 7 +- wasm/wasm_source/src/vp_testnet_faucet.rs | 10 +- 16 files changed, 246 insertions(+), 144 deletions(-) diff --git a/core/src/ledger/storage_api/collections/lazy_map.rs b/core/src/ledger/storage_api/collections/lazy_map.rs index ec4a958002..28802b79eb 100644 --- a/core/src/ledger/storage_api/collections/lazy_map.rs +++ b/core/src/ledger/storage_api/collections/lazy_map.rs @@ -512,11 +512,11 @@ where #[cfg(test)] mod test { use super::*; - use crate::ledger::storage::testing::TestStorage; + use crate::ledger::storage::testing::TestWlStorage; #[test] fn test_lazy_map_basics() -> storage_api::Result<()> { - let mut storage = TestStorage::default(); + let mut storage = TestWlStorage::default(); let key = storage::Key::parse("test").unwrap(); let lazy_map = LazyMap::::open(key); diff --git a/core/src/ledger/storage_api/collections/lazy_vec.rs b/core/src/ledger/storage_api/collections/lazy_vec.rs index 0e0a5ab03b..47b5c95c75 100644 --- a/core/src/ledger/storage_api/collections/lazy_vec.rs +++ b/core/src/ledger/storage_api/collections/lazy_vec.rs @@ -476,11 +476,11 @@ where #[cfg(test)] mod test { use super::*; - use crate::ledger::storage::testing::TestStorage; + use crate::ledger::storage::testing::TestWlStorage; #[test] fn test_lazy_vec_basics() -> storage_api::Result<()> { - let mut storage = TestStorage::default(); + let mut storage = TestWlStorage::default(); let key = storage::Key::parse("test").unwrap(); let lazy_vec = LazyVec::::open(key); diff --git a/core/src/ledger/testnet_pow.rs b/core/src/ledger/testnet_pow.rs index e40eb7a96e..bed2273a70 100644 --- a/core/src/ledger/testnet_pow.rs +++ b/core/src/ledger/testnet_pow.rs @@ -523,14 +523,15 @@ mod test_with_tx_and_vp_env { tx_env.spawn_accounts([&faucet_address, &source]); init_faucet_storage( - &mut tx_env.storage, + &mut tx_env.wl_storage, &faucet_address, difficulty, withdrawal_limit, )?; + tx_env.commit_genesis(); let challenge = Challenge::new( - &mut tx_env.storage, + &mut tx_env.wl_storage, &faucet_address, source.clone(), )?; diff --git a/tests/src/native_vp/mod.rs b/tests/src/native_vp/mod.rs index 28dad14557..6baf214a3b 100644 --- a/tests/src/native_vp/mod.rs +++ b/tests/src/native_vp/mod.rs @@ -49,8 +49,8 @@ impl TestNativeVpEnv { let ctx = Ctx { iterators: Default::default(), gas_meter: Default::default(), - storage: &self.tx_env.storage, - write_log: &self.tx_env.write_log, + storage: &self.tx_env.wl_storage.storage, + write_log: &self.tx_env.wl_storage.write_log, tx: &self.tx_env.tx, tx_index: &self.tx_env.tx_index, vp_wasm_cache: self.tx_env.vp_wasm_cache.clone(), diff --git a/tests/src/native_vp/pos.rs b/tests/src/native_vp/pos.rs index 69b7502a50..29460107bb 100644 --- a/tests/src/native_vp/pos.rs +++ b/tests/src/native_vp/pos.rs @@ -117,17 +117,20 @@ pub fn init_pos( tx_host_env::with(|tx_env| { // Ensure that all the used // addresses exist - let native_token = tx_env.storage.native_token.clone(); + let native_token = tx_env.wl_storage.storage.native_token.clone(); tx_env.spawn_accounts([&native_token]); for validator in genesis_validators { tx_env.spawn_accounts([&validator.address]); } - tx_env.storage.block.epoch = start_epoch; + tx_env.wl_storage.storage.block.epoch = start_epoch; // Initialize PoS storage tx_env - .storage + .wl_storage .init_genesis(params, genesis_validators.iter(), start_epoch) .unwrap(); + + // Commit changes in WL to genesis state + tx_env.commit_genesis(); }); } @@ -248,7 +251,7 @@ mod tests { if !test_state.is_current_tx_valid { // Clear out the changes tx_host_env::with(|env| { - env.write_log.drop_tx(); + env.wl_storage.drop_tx(); }); } @@ -262,13 +265,13 @@ mod tests { tx_host_env::with(|env| { // Clear out the changes if !test_state.is_current_tx_valid { - env.write_log.drop_tx(); + env.wl_storage.drop_tx(); } // Also commit the last transaction(s) changes, if any env.commit_tx_and_block(); - env.storage.block.epoch = - env.storage.block.epoch.next(); + env.wl_storage.storage.block.epoch = + env.wl_storage.storage.block.epoch.next(); }); // Starting a new tx @@ -298,7 +301,7 @@ mod tests { // Clear out the invalid changes tx_host_env::with(|env| { - env.write_log.drop_tx(); + env.wl_storage.drop_tx(); }) } } @@ -851,7 +854,7 @@ pub mod testing { // Reset the gas meter on each change, so that we never run // out in this test env.gas_meter.reset(); - env.storage.block.epoch + env.wl_storage.storage.block.epoch }); println!("Current epoch {}", current_epoch); diff --git a/tests/src/storage_api/collections/lazy_map.rs b/tests/src/storage_api/collections/lazy_map.rs index afff09bbf1..5268cf8b10 100644 --- a/tests/src/storage_api/collections/lazy_map.rs +++ b/tests/src/storage_api/collections/lazy_map.rs @@ -241,7 +241,7 @@ mod tests { match &transition { Transition::CommitTx => { // commit the tx without committing the block - tx_host_env::with(|env| env.write_log.commit_tx()); + tx_host_env::with(|env| env.wl_storage.commit_tx()); } Transition::CommitTxAndBlock => { // commit the tx and the block diff --git a/tests/src/storage_api/collections/lazy_vec.rs b/tests/src/storage_api/collections/lazy_vec.rs index 65e08b4ca7..b93c04885c 100644 --- a/tests/src/storage_api/collections/lazy_vec.rs +++ b/tests/src/storage_api/collections/lazy_vec.rs @@ -235,7 +235,7 @@ mod tests { match &transition { Transition::CommitTx => { // commit the tx without committing the block - tx_host_env::with(|env| env.write_log.commit_tx()); + tx_host_env::with(|env| env.wl_storage.commit_tx()); } Transition::CommitTxAndBlock => { // commit the tx and the block diff --git a/tests/src/storage_api/collections/nested_lazy_map.rs b/tests/src/storage_api/collections/nested_lazy_map.rs index 9951eb318b..b0ff35094c 100644 --- a/tests/src/storage_api/collections/nested_lazy_map.rs +++ b/tests/src/storage_api/collections/nested_lazy_map.rs @@ -254,7 +254,7 @@ mod tests { match &transition { Transition::CommitTx => { // commit the tx without committing the block - tx_host_env::with(|env| env.write_log.commit_tx()); + tx_host_env::with(|env| env.wl_storage.commit_tx()); } Transition::CommitTxAndBlock => { // commit the tx and the block diff --git a/tests/src/vm_host_env/ibc.rs b/tests/src/vm_host_env/ibc.rs index 88f2d205a9..1e60480186 100644 --- a/tests/src/vm_host_env/ibc.rs +++ b/tests/src/vm_host_env/ibc.rs @@ -115,6 +115,7 @@ pub fn validate_ibc_vp_from_tx<'a>( tx: &'a Tx, ) -> std::result::Result { let (verifiers, keys_changed) = tx_env + .wl_storage .write_log .verifiers_and_changed_keys(&tx_env.verifiers); let addr = Address::Internal(InternalAddress::Ibc); @@ -129,8 +130,8 @@ pub fn validate_ibc_vp_from_tx<'a>( let ctx = Ctx::new( &ADDRESS, - &tx_env.storage, - &tx_env.write_log, + &tx_env.wl_storage.storage, + &tx_env.wl_storage.write_log, tx, &TxIndex(0), VpGasMeter::new(0), @@ -150,6 +151,7 @@ pub fn validate_token_vp_from_tx<'a>( target: &Key, ) -> std::result::Result { let (verifiers, keys_changed) = tx_env + .wl_storage .write_log .verifiers_and_changed_keys(&tx_env.verifiers); if !keys_changed.contains(target) { @@ -164,8 +166,8 @@ pub fn validate_token_vp_from_tx<'a>( let ctx = Ctx::new( &ADDRESS, - &tx_env.storage, - &tx_env.write_log, + &tx_env.wl_storage.storage, + &tx_env.wl_storage.write_log, tx, &TxIndex(0), VpGasMeter::new(0), @@ -181,10 +183,14 @@ pub fn validate_token_vp_from_tx<'a>( /// Initialize the test storage. Requires initialized [`tx_host_env::ENV`]. pub fn init_storage() -> (Address, Address) { tx_host_env::with(|env| { - init_genesis_storage(&mut env.storage); + init_genesis_storage(&mut env.wl_storage.storage); // block header to check timeout timestamp - env.storage.set_header(tm_dummy_header()).unwrap(); - env.storage + env.wl_storage + .storage + .set_header(tm_dummy_header()) + .unwrap(); + env.wl_storage + .storage .begin_block(BlockHash::default(), BlockHeight(1)) .unwrap(); }); diff --git a/tests/src/vm_host_env/mod.rs b/tests/src/vm_host_env/mod.rs index 1ef6c9bc22..3f4adf6df5 100644 --- a/tests/src/vm_host_env/mod.rs +++ b/tests/src/vm_host_env/mod.rs @@ -166,10 +166,8 @@ mod tests { tx_host_env::with(|env| { for i in sub_keys.iter() { let key = prefix.push(i).unwrap(); - let value = i.try_to_vec().unwrap(); - env.storage.write(&key, value).unwrap(); + env.wl_storage.write(&key, i).unwrap(); } - env.storage.commit_block().unwrap(); }); // Then try to iterate over their prefix @@ -234,23 +232,35 @@ mod tests { assert_eq!( tx::ctx().get_chain_id().unwrap(), - tx_host_env::with(|env| env.storage.get_chain_id().0) + tx_host_env::with(|env| env.wl_storage.storage.get_chain_id().0) ); assert_eq!( tx::ctx().get_block_height().unwrap(), - tx_host_env::with(|env| env.storage.get_block_height().0) + tx_host_env::with(|env| env + .wl_storage + .storage + .get_block_height() + .0) ); assert_eq!( tx::ctx().get_block_hash().unwrap(), - tx_host_env::with(|env| env.storage.get_block_hash().0) + tx_host_env::with(|env| env.wl_storage.storage.get_block_hash().0) ); assert_eq!( tx::ctx().get_block_epoch().unwrap(), - tx_host_env::with(|env| env.storage.get_current_epoch().0) + tx_host_env::with(|env| env + .wl_storage + .storage + .get_current_epoch() + .0) ); assert_eq!( tx::ctx().get_native_token().unwrap(), - tx_host_env::with(|env| env.storage.native_token.clone()) + tx_host_env::with(|env| env + .wl_storage + .storage + .native_token + .clone()) ); } @@ -264,10 +274,7 @@ mod tests { let key_raw = "key"; let key = storage::Key::parse(key_raw).unwrap(); let value = "test".to_string(); - let value_raw = value.try_to_vec().unwrap(); - vp_host_env::with(|env| { - env.write_log.write(&key, value_raw.clone()).unwrap() - }); + vp_host_env::with(|env| env.wl_storage.write(&key, &value).unwrap()); let read_pre_value: Option = vp::CTX.read_pre(&key).unwrap(); assert_eq!(None, read_pre_value); @@ -282,16 +289,16 @@ mod tests { let addr = address::testing::established_address_1(); let addr_key = storage::Key::from(addr.to_db_key()); - // Write some value to storage + // Write some value to storage ... let existing_key = addr_key.join(&Key::parse("existing_key_raw").unwrap()); let existing_value = vec![2_u8; 1000]; - // Values written to storage have to be encoded with Borsh - let existing_value_encoded = existing_value.try_to_vec().unwrap(); tx_env - .storage - .write(&existing_key, existing_value_encoded) + .wl_storage + .write(&existing_key, &existing_value) .unwrap(); + // ... and commit it + tx_env.wl_storage.commit_tx(); // In a transaction, write override the existing key's value and add // another key-value @@ -371,13 +378,13 @@ mod tests { // We'll write sub-key in some random order to check prefix iter's order let sub_keys = [2_i32, 1, i32::MAX, -1, 260, -2, i32::MIN, 5, 0]; - // Write some values to storage + // Write some values to storage ... for i in sub_keys.iter() { let key = prefix.push(i).unwrap(); - let value = i.try_to_vec().unwrap(); - tx_env.storage.write(&key, value).unwrap(); + tx_env.wl_storage.write(&key, i).unwrap(); } - tx_env.storage.commit_block().unwrap(); + // ... and commit them + tx_env.wl_storage.commit_tx(); // In a transaction, write override the existing key's value and add // another key-value @@ -431,7 +438,7 @@ mod tests { let pk_key = key::pk_key(&addr); let keypair = key::testing::keypair_1(); let pk = keypair.ref_to(); - env.storage + env.wl_storage .write(&pk_key, pk.try_to_vec().unwrap()) .unwrap(); // Initialize the environment @@ -478,23 +485,35 @@ mod tests { assert_eq!( vp::CTX.get_chain_id().unwrap(), - vp_host_env::with(|env| env.storage.get_chain_id().0) + vp_host_env::with(|env| env.wl_storage.storage.get_chain_id().0) ); assert_eq!( vp::CTX.get_block_height().unwrap(), - vp_host_env::with(|env| env.storage.get_block_height().0) + vp_host_env::with(|env| env + .wl_storage + .storage + .get_block_height() + .0) ); assert_eq!( vp::CTX.get_block_hash().unwrap(), - vp_host_env::with(|env| env.storage.get_block_hash().0) + vp_host_env::with(|env| env.wl_storage.storage.get_block_hash().0) ); assert_eq!( vp::CTX.get_block_epoch().unwrap(), - vp_host_env::with(|env| env.storage.get_current_epoch().0) + vp_host_env::with(|env| env + .wl_storage + .storage + .get_current_epoch() + .0) ); assert_eq!( vp::CTX.get_native_token().unwrap(), - vp_host_env::with(|env| env.storage.native_token.clone()) + vp_host_env::with(|env| env + .wl_storage + .storage + .native_token + .clone()) ); } @@ -569,7 +588,7 @@ mod tests { IbcError::ClientError(_), )); // drop the transaction - env.write_log.drop_tx(); + env.wl_storage.drop_tx(); // Start a transaction to create a new client tx_host_env::set(env); @@ -596,10 +615,14 @@ mod tests { // Commit env.commit_tx_and_block(); // update the block height for the following client update - env.storage + env.wl_storage + .storage .begin_block(BlockHash::default(), BlockHeight(2)) .unwrap(); - env.storage.set_header(tm_dummy_header()).unwrap(); + env.wl_storage + .storage + .set_header(tm_dummy_header()) + .unwrap(); // Start an invalid transaction tx_host_env::set(env); @@ -647,7 +670,7 @@ mod tests { IbcError::ClientError(_), )); // drop the transaction - env.write_log.drop_tx(); + env.wl_storage.drop_tx(); // Start a transaction to update the client tx_host_env::set(env); @@ -673,10 +696,14 @@ mod tests { // Commit env.commit_tx_and_block(); // update the block height for the following client update - env.storage + env.wl_storage + .storage .begin_block(BlockHash::default(), BlockHeight(3)) .unwrap(); - env.storage.set_header(tm_dummy_header()).unwrap(); + env.wl_storage + .storage + .set_header(tm_dummy_header()) + .unwrap(); // Start a transaction to upgrade the client tx_host_env::set(env); @@ -710,7 +737,10 @@ mod tests { let (client_id, client_state, writes) = ibc::prepare_client(); writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }); }); @@ -751,7 +781,7 @@ mod tests { IbcError::ConnectionError(_), )); // drop the transaction - env.write_log.drop_tx(); + env.wl_storage.drop_tx(); // Start a transaction for ConnectionOpenInit tx_host_env::set(env); @@ -777,7 +807,10 @@ mod tests { // Commit env.commit_tx_and_block(); // set a block header again - env.storage.set_header(tm_dummy_header()).unwrap(); + env.wl_storage + .storage + .set_header(tm_dummy_header()) + .unwrap(); // Start the next transaction for ConnectionOpenAck tx_host_env::set(env); @@ -812,7 +845,10 @@ mod tests { let mut env = tx_host_env::take(); let (client_id, client_state, writes) = ibc::prepare_client(); writes.into_iter().for_each(|(key, val)| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }); // Start a transaction for ConnectionOpenTry @@ -839,7 +875,10 @@ mod tests { // Commit env.commit_tx_and_block(); // set a block header again - env.storage.set_header(tm_dummy_header()).unwrap(); + env.wl_storage + .storage + .set_header(tm_dummy_header()) + .unwrap(); // Start the next transaction for ConnectionOpenConfirm tx_host_env::set(env); @@ -876,7 +915,10 @@ mod tests { writes.extend(conn_writes); writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }); }); @@ -918,7 +960,7 @@ mod tests { IbcError::ChannelError(_), )); // drop the transaction - env.write_log.drop_tx(); + env.wl_storage.drop_tx(); // Start an invalid transaction tx_host_env::set(env); @@ -965,7 +1007,7 @@ mod tests { IbcError::ChannelError(_), )); // drop the transaction - env.write_log.drop_tx(); + env.wl_storage.drop_tx(); // Start a transaction for ChannelOpenInit tx_host_env::set(env); @@ -1026,7 +1068,10 @@ mod tests { writes.extend(conn_writes); writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }); }); @@ -1092,7 +1137,10 @@ mod tests { writes.extend(channel_writes); writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }); }); @@ -1132,7 +1180,10 @@ mod tests { writes.extend(channel_writes); writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }); }); @@ -1173,7 +1224,10 @@ mod tests { writes.extend(channel_writes); writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }); }); @@ -1263,7 +1317,10 @@ mod tests { writes.insert(key, init_bal.try_to_vec().unwrap()); writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }); }); @@ -1322,7 +1379,10 @@ mod tests { writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }); }); @@ -1380,7 +1440,10 @@ mod tests { writes.extend(channel_writes); writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }); }); // escrow in advance @@ -1392,7 +1455,10 @@ mod tests { ); let val = Amount::from(1_000_000_000u64).try_to_vec().unwrap(); tx_host_env::with(|env| { - env.storage.write(&escrow, &val).expect("write error"); + env.wl_storage + .storage + .write(&escrow, &val) + .expect("write error"); }); // Set this chain as the source zone @@ -1451,7 +1517,10 @@ mod tests { writes.extend(channel_writes); writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }); }); @@ -1526,7 +1595,10 @@ mod tests { writes.extend(channel_writes); writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }); }); @@ -1577,7 +1649,10 @@ mod tests { writes.extend(channel_writes); writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }) }); @@ -1650,7 +1725,10 @@ mod tests { writes.extend(channel_writes); writes.into_iter().for_each(|(key, val)| { tx_host_env::with(|env| { - env.storage.write(&key, &val).expect("write error"); + env.wl_storage + .storage + .write(&key, &val) + .expect("write error"); }) }); diff --git a/tests/src/vm_host_env/tx.rs b/tests/src/vm_host_env/tx.rs index 0f7040941d..6c3ccd55ae 100644 --- a/tests/src/vm_host_env/tx.rs +++ b/tests/src/vm_host_env/tx.rs @@ -1,12 +1,12 @@ use std::borrow::Borrow; use std::collections::BTreeSet; -use derivative::Derivative; use namada::ledger::gas::BlockGasMeter; use namada::ledger::parameters::{self, EpochDuration}; use namada::ledger::storage::mockdb::MockDB; use namada::ledger::storage::testing::TestStorage; use namada::ledger::storage::write_log::WriteLog; +use namada::ledger::storage::{Sha256Hasher, WlStorage}; use namada::proto::Tx; use namada::types::address::Address; use namada::types::storage::{Key, TxIndex}; @@ -41,12 +41,9 @@ pub mod tx_host_env { } /// Host environment structures required for transactions. -#[derive(Derivative)] -#[derivative(Debug)] +#[derive(Debug)] pub struct TestTxEnv { - #[derivative(Debug = "ignore")] - pub storage: TestStorage, - pub write_log: WriteLog, + pub wl_storage: WlStorage, pub iterators: PrefixIterators<'static, MockDB>, pub verifiers: BTreeSet
, pub gas_meter: BlockGasMeter, @@ -66,8 +63,10 @@ impl Default for TestTxEnv { wasm::compilation_cache::common::testing::cache(); Self { - storage: TestStorage::default(), - write_log: WriteLog::default(), + wl_storage: WlStorage { + storage: TestStorage::default(), + write_log: WriteLog::default(), + }, iterators: PrefixIterators::default(), gas_meter: BlockGasMeter::default(), tx_index: TxIndex::default(), @@ -84,11 +83,14 @@ impl Default for TestTxEnv { impl TestTxEnv { pub fn all_touched_storage_keys(&self) -> BTreeSet { - self.write_log.get_keys() + self.wl_storage.write_log.get_keys() } pub fn get_verifiers(&self) -> BTreeSet
{ - self.write_log.verifiers_and_changed_keys(&self.verifiers).0 + self.wl_storage + .write_log + .verifiers_and_changed_keys(&self.verifiers) + .0 } pub fn init_parameters( @@ -98,18 +100,18 @@ impl TestTxEnv { tx_whitelist: Option>, ) { let _ = parameters::update_epoch_parameter( - &mut self.storage, + &mut self.wl_storage.storage, &epoch_duration.unwrap_or(EpochDuration { min_num_of_blocks: 1, min_duration: DurationSecs(5), }), ); let _ = parameters::update_tx_whitelist_parameter( - &mut self.storage, + &mut self.wl_storage.storage, tx_whitelist.unwrap_or_default(), ); let _ = parameters::update_vp_whitelist_parameter( - &mut self.storage, + &mut self.wl_storage.storage, vp_whitelist.unwrap_or_default(), ); } @@ -133,16 +135,23 @@ impl TestTxEnv { } let key = Key::validity_predicate(address.borrow()); let vp_code = vec![]; - self.storage + self.wl_storage + .storage .write(&key, vp_code) .expect("Unable to write VP"); } } + /// Commit the genesis state. Typically, you'll want to call this after + /// setting up the initial state, before running a transaction. + pub fn commit_genesis(&mut self) { + self.wl_storage.commit_genesis().unwrap(); + } + pub fn commit_tx_and_block(&mut self) { - self.write_log.commit_tx(); - self.write_log - .commit_block(&mut self.storage) + self.wl_storage.commit_tx(); + self.wl_storage + .commit_block() .map_err(|err| println!("{:?}", err)) .ok(); self.iterators = PrefixIterators::default(); @@ -166,7 +175,8 @@ impl TestTxEnv { } None => token::balance_key(token, target), }; - self.storage + self.wl_storage + .storage .write(&storage_key, amount.try_to_vec().unwrap()) .unwrap(); } @@ -178,7 +188,8 @@ impl TestTxEnv { public_key: &key::common::PublicKey, ) { let storage_key = key::pk_key(address); - self.storage + self.wl_storage + .storage .write(&storage_key, public_key.try_to_vec().unwrap()) .unwrap(); } @@ -187,8 +198,8 @@ impl TestTxEnv { pub fn execute_tx(&mut self) -> Result<(), Error> { let empty_data = vec![]; wasm::run::tx( - &self.storage, - &mut self.write_log, + &self.wl_storage.storage, + &mut self.wl_storage.write_log, &mut self.gas_meter, &self.tx_index, &self.tx.code, @@ -278,16 +289,14 @@ mod native_tx_host_env { /// changes. pub fn set_from_vp_env(vp_env: TestVpEnv) { let TestVpEnv { - storage, - write_log, + wl_storage, tx, vp_wasm_cache, vp_cache_dir, .. } = vp_env; let tx_env = TestTxEnv { - storage, - write_log, + wl_storage, vp_wasm_cache, vp_cache_dir, tx, @@ -306,13 +315,12 @@ mod native_tx_host_env { #[no_mangle] extern "C" fn extern_fn_name( $($arg: $type),* ) { with(|TestTxEnv { - storage, - write_log, + wl_storage, iterators, verifiers, gas_meter, - result_buffer, - tx_index, + result_buffer, + tx_index, vp_wasm_cache, vp_cache_dir: _, tx_wasm_cache, @@ -321,8 +329,8 @@ mod native_tx_host_env { }: &mut TestTxEnv| { let tx_env = vm::host_env::testing::tx_env( - storage, - write_log, + &wl_storage.storage, + &mut wl_storage.write_log, iterators, verifiers, gas_meter, @@ -347,8 +355,7 @@ mod native_tx_host_env { extern "C" fn extern_fn_name( $($arg: $type),* ) -> $ret { with(|TestTxEnv { tx_index, - storage, - write_log, + wl_storage, iterators, verifiers, gas_meter, @@ -361,8 +368,8 @@ mod native_tx_host_env { }: &mut TestTxEnv| { let tx_env = vm::host_env::testing::tx_env( - storage, - write_log, + &wl_storage.storage, + &mut wl_storage.write_log, iterators, verifiers, gas_meter, diff --git a/tests/src/vm_host_env/vp.rs b/tests/src/vm_host_env/vp.rs index 88d9d6ca3c..4d5bbf3dde 100644 --- a/tests/src/vm_host_env/vp.rs +++ b/tests/src/vm_host_env/vp.rs @@ -4,6 +4,7 @@ use namada::ledger::gas::VpGasMeter; use namada::ledger::storage::mockdb::MockDB; use namada::ledger::storage::testing::TestStorage; use namada::ledger::storage::write_log::WriteLog; +use namada::ledger::storage::{Sha256Hasher, WlStorage}; use namada::proto::Tx; use namada::types::address::{self, Address}; use namada::types::storage::{self, Key, TxIndex}; @@ -35,10 +36,10 @@ pub mod vp_host_env { } /// Host environment structures required for transactions. +#[derive(Debug)] pub struct TestVpEnv { pub addr: Address, - pub storage: TestStorage, - pub write_log: WriteLog, + pub wl_storage: WlStorage, pub iterators: PrefixIterators<'static, MockDB>, pub gas_meter: VpGasMeter, pub tx: Tx, @@ -65,8 +66,10 @@ impl Default for TestVpEnv { Self { addr: address::testing::established_address_1(), - storage: TestStorage::default(), - write_log: WriteLog::default(), + wl_storage: WlStorage { + storage: TestStorage::default(), + write_log: WriteLog::default(), + }, iterators: PrefixIterators::default(), gas_meter: VpGasMeter::default(), tx: Tx::new(vec![], None), @@ -85,11 +88,14 @@ impl Default for TestVpEnv { impl TestVpEnv { pub fn all_touched_storage_keys(&self) -> BTreeSet { - self.write_log.get_keys() + self.wl_storage.write_log.get_keys() } pub fn get_verifiers(&self) -> BTreeSet
{ - self.write_log.verifiers_and_changed_keys(&self.verifiers).0 + self.wl_storage + .write_log + .verifiers_and_changed_keys(&self.verifiers) + .0 } } @@ -185,7 +191,7 @@ mod native_vp_host_env { // Write an empty validity predicate for the address, because it's used // to check if the address exists when we write into its storage let vp_key = Key::validity_predicate(&addr); - tx_env.storage.write(&vp_key, vec![]).unwrap(); + tx_env.wl_storage.storage.write(&vp_key, vec![]).unwrap(); tx_host_env::set(tx_env); apply_tx(&addr); @@ -193,6 +199,7 @@ mod native_vp_host_env { let tx_env = tx_host_env::take(); let verifiers_from_tx = &tx_env.verifiers; let (verifiers, keys_changed) = tx_env + .wl_storage .write_log .verifiers_and_changed_keys(verifiers_from_tx); if !verifiers.contains(&addr) { @@ -205,8 +212,7 @@ mod native_vp_host_env { let vp_env = TestVpEnv { addr, - storage: tx_env.storage, - write_log: tx_env.write_log, + wl_storage: tx_env.wl_storage, keys_changed, verifiers, ..Default::default() @@ -246,8 +252,7 @@ mod native_vp_host_env { extern "C" fn extern_fn_name( $($arg: $type),* ) { with(|TestVpEnv { addr, - storage, - write_log, + wl_storage, iterators, gas_meter, tx, @@ -264,8 +269,8 @@ mod native_vp_host_env { let env = vm::host_env::testing::vp_env( addr, - storage, - write_log, + &wl_storage.storage, + &wl_storage.write_log, iterators, gas_meter, tx, @@ -294,8 +299,7 @@ mod native_vp_host_env { extern "C" fn extern_fn_name( $($arg: $type),* ) -> $ret { with(|TestVpEnv { addr, - storage, - write_log, + wl_storage, iterators, gas_meter, tx, @@ -312,8 +316,8 @@ mod native_vp_host_env { let env = vm::host_env::testing::vp_env( addr, - storage, - write_log, + &wl_storage.storage, + &wl_storage.write_log, iterators, gas_meter, tx, @@ -343,9 +347,9 @@ mod native_vp_host_env { native_host_fn!(vp_result_buffer(result_ptr: u64)); native_host_fn!(vp_has_key_pre(key_ptr: u64, key_len: u64) -> i64); native_host_fn!(vp_has_key_post(key_ptr: u64, key_len: u64) -> i64); - native_host_fn!(vp_iter_prefix(prefix_ptr: u64, prefix_len: u64) -> u64); - native_host_fn!(vp_iter_pre_next(iter_id: u64) -> i64); - native_host_fn!(vp_iter_post_next(iter_id: u64) -> i64); + native_host_fn!(vp_iter_prefix_pre(prefix_ptr: u64, prefix_len: u64) -> u64); + native_host_fn!(vp_iter_prefix_post(prefix_ptr: u64, prefix_len: u64) -> u64); + native_host_fn!(vp_iter_next(iter_id: u64) -> i64); native_host_fn!(vp_get_chain_id(result_ptr: u64)); native_host_fn!(vp_get_block_height() -> u64); native_host_fn!(vp_get_tx_index() -> u32); diff --git a/wasm/wasm_source/src/tx_bond.rs b/wasm/wasm_source/src/tx_bond.rs index 219a612630..d91868028c 100644 --- a/wasm/wasm_source/src/tx_bond.rs +++ b/wasm/wasm_source/src/tx_bond.rs @@ -85,7 +85,7 @@ mod tests { // Ensure that the bond's source has enough tokens for the bond let target = bond.source.as_ref().unwrap_or(&bond.validator); - let native_token = tx_env.storage.native_token.clone(); + let native_token = tx_env.wl_storage.storage.native_token.clone(); tx_env.credit_tokens(target, &native_token, None, bond.amount); native_token }); diff --git a/wasm/wasm_source/src/tx_unbond.rs b/wasm/wasm_source/src/tx_unbond.rs index 70033286bf..3392d6c174 100644 --- a/wasm/wasm_source/src/tx_unbond.rs +++ b/wasm/wasm_source/src/tx_unbond.rs @@ -83,7 +83,7 @@ mod tests { init_pos(&genesis_validators[..], &pos_params, Epoch(0)); let native_token = tx_host_env::with(|tx_env| { - let native_token = tx_env.storage.native_token.clone(); + let native_token = tx_env.wl_storage.storage.native_token.clone(); if is_delegation { let source = unbond.source.as_ref().unwrap(); tx_env.spawn_accounts([source]); diff --git a/wasm/wasm_source/src/tx_withdraw.rs b/wasm/wasm_source/src/tx_withdraw.rs index e29415f800..89ede199d4 100644 --- a/wasm/wasm_source/src/tx_withdraw.rs +++ b/wasm/wasm_source/src/tx_withdraw.rs @@ -92,7 +92,7 @@ mod tests { init_pos(&genesis_validators[..], &pos_params, Epoch(0)); let native_token = tx_host_env::with(|tx_env| { - let native_token = tx_env.storage.native_token.clone(); + let native_token = tx_env.wl_storage.storage.native_token.clone(); if is_delegation { let source = withdraw.source.as_ref().unwrap(); tx_env.spawn_accounts([source]); @@ -134,11 +134,12 @@ mod tests { // withdraw the unbonded tokens tx_host_env::with(|env| { for _ in 0..pos_params.unbonding_len { - env.storage.block.epoch = env.storage.block.epoch.next(); + env.wl_storage.storage.block.epoch = + env.wl_storage.storage.block.epoch.next(); } }); assert_eq!( - tx_host_env::with(|env| env.storage.block.epoch), + tx_host_env::with(|env| env.wl_storage.storage.block.epoch), Epoch(pos_params.unbonding_len) ); diff --git a/wasm/wasm_source/src/vp_testnet_faucet.rs b/wasm/wasm_source/src/vp_testnet_faucet.rs index 8e4e79719f..1b8802df6e 100644 --- a/wasm/wasm_source/src/vp_testnet_faucet.rs +++ b/wasm/wasm_source/src/vp_testnet_faucet.rs @@ -291,7 +291,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let difficulty = testnet_pow::Difficulty::try_new(0).unwrap(); let withdrawal_limit = token::Amount::from(MAX_FREE_DEBIT as u64); - testnet_pow::init_faucet_storage(&mut tx_env.storage, &vp_owner, difficulty, withdrawal_limit).unwrap(); + testnet_pow::init_faucet_storage(&mut tx_env.wl_storage, &vp_owner, difficulty, withdrawal_limit).unwrap(); let target = address::testing::established_address_2(); let token = address::nam(); @@ -303,6 +303,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, None, amount); + tx_env.commit_genesis(); // Initialize VP environment from a transaction vp_host_env::init_from_tx(vp_owner.clone(), tx_env, |address| { @@ -330,7 +331,7 @@ mod tests { let vp_owner = address::testing::established_address_1(); let difficulty = testnet_pow::Difficulty::try_new(0).unwrap(); let withdrawal_limit = token::Amount::from(MAX_FREE_DEBIT as u64); - testnet_pow::init_faucet_storage(&mut tx_env.storage, &vp_owner, difficulty, withdrawal_limit).unwrap(); + testnet_pow::init_faucet_storage(&mut tx_env.wl_storage, &vp_owner, difficulty, withdrawal_limit).unwrap(); let target = address::testing::established_address_2(); let target_key = key::testing::keypair_1(); @@ -343,9 +344,10 @@ 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, None, amount); + tx_env.commit_genesis(); // Construct a PoW solution like a client would - let challenge = testnet_pow::Challenge::new(&mut tx_env.storage, &vp_owner, target.clone()).unwrap(); + let challenge = testnet_pow::Challenge::new(&mut tx_env.wl_storage, &vp_owner, target.clone()).unwrap(); let solution = challenge.solve(); let solution_bytes = solution.try_to_vec().unwrap(); // The signature itself doesn't matter and is not being checked in this @@ -391,7 +393,7 @@ mod tests { // Init the VP let difficulty = testnet_pow::Difficulty::try_new(0).unwrap(); let withdrawal_limit = token::Amount::from(MAX_FREE_DEBIT as u64); - testnet_pow::init_faucet_storage(&mut tx_env.storage, &vp_owner, difficulty, withdrawal_limit).unwrap(); + testnet_pow::init_faucet_storage(&mut tx_env.wl_storage, &vp_owner, difficulty, withdrawal_limit).unwrap(); let keypair = key::testing::keypair_1(); let public_key = &keypair.ref_to();