From 5fbfb8821e65898819a1c6ffe010af8186a83f9b Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 27 Mar 2024 13:48:27 +0100 Subject: [PATCH 01/12] Initialized branch for task. --- src/transactions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transactions.rs b/src/transactions.rs index 59ac0b0f..e396dfb6 100644 --- a/src/transactions.rs +++ b/src/transactions.rs @@ -7,7 +7,7 @@ use std::iter; use std::iter::Peekable; use std::ops::{Bound, RangeBounds}; -/// The BTreeMap specific key-value pair reference type, as returned by `BTreeMap, T>::range`. +/// The [BTreeMap] specific key-value pair reference type, as returned by `BTreeMap, T>::range`. /// This is internal as it can change any time if the map implementation is swapped out. type BTreeMapPairRef<'a, T = Vec> = (&'a Vec, &'a T); From 262551b32e467f854e76faef778a514ea9ba7506 Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Tue, 2 Apr 2024 12:25:59 +0200 Subject: [PATCH 02/12] Added transaction info to app. --- Cargo.lock | 18 +++++++++--------- src/app.rs | 14 ++++++++++++-- src/app_builder.rs | 28 ++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 14931905..96c85029 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -295,9 +295,9 @@ dependencies = [ [[package]] name = "der" -version = "0.7.8" +version = "0.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fffa369a668c8af7dbf8b5e56c9f744fbd399949ed171606040001947de40b1c" +checksum = "f55bf8e7b65898637379c1b74eb1551107c8294ed26d855ceb9fd1a09cfc9bc0" dependencies = [ "const-oid", "zeroize", @@ -526,9 +526,9 @@ checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "memchr" -version = "2.7.1" +version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" [[package]] name = "miniz_oxide" @@ -599,7 +599,7 @@ dependencies = [ "itertools 0.11.0", "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.57", ] [[package]] @@ -712,7 +712,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.57", ] [[package]] @@ -806,9 +806,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.55" +version = "2.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "002a1b3dbf967edfafc32655d0f377ab0bb7b994aa1d32c8cc7e9b8bf3ebb8f0" +checksum = "11a6ae1e52eb25aab8f3fb9fca13be982a373b8f1157ca14b897a825ba4a2d35" dependencies = [ "proc-macro2", "quote", @@ -832,7 +832,7 @@ checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.55", + "syn 2.0.57", ] [[package]] diff --git a/src/app.rs b/src/app.rs index 6608ffd0..47544514 100644 --- a/src/app.rs +++ b/src/app.rs @@ -17,7 +17,7 @@ use cosmwasm_std::testing::{MockApi, MockStorage}; use cosmwasm_std::{ from_json, to_json_binary, Addr, Api, Binary, BlockInfo, ContractResult, CosmosMsg, CustomMsg, CustomQuery, Empty, Querier, QuerierResult, QuerierWrapper, QueryRequest, Record, Storage, - SystemError, SystemResult, + SystemError, SystemResult, TransactionInfo, }; use serde::{de::DeserializeOwned, Serialize}; use std::fmt::Debug; @@ -65,6 +65,7 @@ pub struct App< pub(crate) api: Api, pub(crate) storage: Storage, pub(crate) block: BlockInfo, + pub(crate) transaction: TransactionInfo, } /// No-op application initialization function. @@ -420,13 +421,19 @@ where .process_queue(&self.api, &mut self.storage, &self.router, &self.block) .unwrap(); action(&mut self.block); + self.transaction.index = 0; } - /// Returns a copy of the current block_info + /// Returns a copy of the current [BlockInfo]. pub fn block_info(&self) -> BlockInfo { self.block.clone() } + /// Returns a copy of the current [TransactionInfo]. + pub fn transaction_info(&self) -> TransactionInfo { + self.transaction.clone() + } + /// Simple helper so we get access to all the QuerierWrapper helpers, /// e.g. wrap().query_wasm_smart, query_all_balances, ... pub fn wrap(&self) -> QuerierWrapper { @@ -447,6 +454,7 @@ where let Self { block, + transaction: _, router, api, storage, @@ -474,6 +482,7 @@ where let Self { block, + transaction: _, router, api, storage, @@ -493,6 +502,7 @@ where // returns a success do we flush it (otherwise drop it) let Self { block, + transaction: _, router, api, storage, diff --git a/src/app_builder.rs b/src/app_builder.rs index 56434c17..85f3dc43 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -6,7 +6,7 @@ use crate::{ Wasm, WasmKeeper, }; use cosmwasm_std::testing::{mock_env, MockApi, MockStorage}; -use cosmwasm_std::{Api, BlockInfo, CustomMsg, CustomQuery, Empty, Storage}; +use cosmwasm_std::{Api, BlockInfo, CustomMsg, CustomQuery, Empty, Storage, TransactionInfo}; use serde::de::DeserializeOwned; use std::fmt::Debug; @@ -46,6 +46,7 @@ pub type BasicAppBuilder = AppBuilder< pub struct AppBuilder { api: Api, block: BlockInfo, + transaction: TransactionInfo, storage: Storage, bank: Bank, wasm: Wasm, @@ -95,6 +96,7 @@ impl AppBuilder { api: MockApi::default(), block: mock_env().block, + transaction: mock_env().transaction.unwrap(), storage: MockStorage::new(), bank: BankKeeper::new(), wasm: WasmKeeper::new(), @@ -131,6 +133,7 @@ where AppBuilder { api: MockApi::default(), block: mock_env().block, + transaction: mock_env().transaction.unwrap(), storage: MockStorage::new(), bank: BankKeeper::new(), wasm: WasmKeeper::new(), @@ -166,6 +169,7 @@ where storage, custom, block, + transaction, staking, distribution, ibc, @@ -177,6 +181,7 @@ where AppBuilder { api, block, + transaction, storage, bank, wasm, @@ -201,6 +206,7 @@ where storage, custom, block, + transaction, staking, distribution, ibc, @@ -212,6 +218,7 @@ where AppBuilder { api, block, + transaction, storage, bank, wasm, @@ -236,6 +243,7 @@ where storage, custom, block, + transaction, staking, distribution, ibc, @@ -247,6 +255,7 @@ where AppBuilder { api, block, + transaction, storage, bank, wasm, @@ -271,6 +280,7 @@ where bank, custom, block, + transaction, staking, distribution, ibc, @@ -282,6 +292,7 @@ where AppBuilder { api, block, + transaction, storage, bank, wasm, @@ -310,6 +321,7 @@ where api, storage, block, + transaction, staking, distribution, ibc, @@ -321,6 +333,7 @@ where AppBuilder { api, block, + transaction, storage, bank, wasm, @@ -345,6 +358,7 @@ where storage, custom, block, + transaction, bank, distribution, ibc, @@ -356,6 +370,7 @@ where AppBuilder { api, block, + transaction, storage, bank, wasm, @@ -390,6 +405,7 @@ where storage, custom, block, + transaction, staking, bank, ibc, @@ -401,6 +417,7 @@ where AppBuilder { api, block, + transaction, storage, bank, wasm, @@ -431,6 +448,7 @@ where storage, custom, block, + transaction, staking, bank, distribution, @@ -442,6 +460,7 @@ where AppBuilder { api, block, + transaction, storage, bank, wasm, @@ -466,6 +485,7 @@ where storage, custom, block, + transaction, staking, bank, distribution, @@ -477,6 +497,7 @@ where AppBuilder { api, block, + transaction, storage, bank, wasm, @@ -501,6 +522,7 @@ where storage, custom, block, + transaction, staking, bank, distribution, @@ -512,6 +534,7 @@ where AppBuilder { api, block, + transaction, storage, bank, wasm, @@ -568,8 +591,9 @@ where let mut app = App { router, api: self.api, - block: self.block, storage: self.storage, + block: self.block, + transaction: self.transaction, }; app.init_modules(init_fn); app From f011e82ea8071ef7b0c0dd44e85a2a989a56810f Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Tue, 2 Apr 2024 15:19:36 +0200 Subject: [PATCH 03/12] Added transaction info to wasm keeper. --- src/app.rs | 45 +++-- src/app_builder.rs | 26 +-- src/tests/test_app.rs | 34 ++-- src/transactions.rs | 4 +- src/wasm.rs | 377 ++++++++++++++++++++++++------------------ 5 files changed, 270 insertions(+), 216 deletions(-) diff --git a/src/app.rs b/src/app.rs index 47544514..aa9bfd60 100644 --- a/src/app.rs +++ b/src/app.rs @@ -65,7 +65,6 @@ pub struct App< pub(crate) api: Api, pub(crate) storage: Storage, pub(crate) block: BlockInfo, - pub(crate) transaction: TransactionInfo, } /// No-op application initialization function. @@ -411,6 +410,7 @@ where .staking .process_queue(&self.api, &mut self.storage, &self.router, &self.block) .unwrap(); + self.router.wasm.reset_transaction_index(); self.block = block; } @@ -420,8 +420,8 @@ where .staking .process_queue(&self.api, &mut self.storage, &self.router, &self.block) .unwrap(); + self.router.wasm.reset_transaction_index(); action(&mut self.block); - self.transaction.index = 0; } /// Returns a copy of the current [BlockInfo]. @@ -431,7 +431,7 @@ where /// Returns a copy of the current [TransactionInfo]. pub fn transaction_info(&self) -> TransactionInfo { - self.transaction.clone() + self.router.wasm.transaction_info() } /// Simple helper so we get access to all the QuerierWrapper helpers, @@ -454,17 +454,22 @@ where let Self { block, - transaction: _, router, api, storage, } = self; - transactional(&mut *storage, |write_cache, _| { - msgs.into_iter() - .map(|msg| router.execute(&*api, write_cache, block, sender.clone(), msg)) - .collect() - }) + router.wasm.increment_transaction_index(); + + transactional( + &mut *storage, + |write_cache, _| { + msgs.into_iter() + .map(|msg| router.execute(&*api, write_cache, block, sender.clone(), msg)) + .collect() + }, + || router.wasm.increment_transaction_index(), + ) } /// Call a smart contract in "sudo" mode. @@ -482,15 +487,18 @@ where let Self { block, - transaction: _, router, api, storage, } = self; - transactional(&mut *storage, |write_cache, _| { - router.wasm.sudo(&*api, write_cache, router, block, msg) - }) + router.wasm.increment_transaction_index(); + + transactional( + &mut *storage, + |write_cache, _| router.wasm.sudo(&*api, write_cache, router, block, msg), + || router.wasm.increment_transaction_index(), + ) } /// Runs arbitrary SudoMsg. @@ -502,15 +510,18 @@ where // returns a success do we flush it (otherwise drop it) let Self { block, - transaction: _, router, api, storage, } = self; - transactional(&mut *storage, |write_cache, _| { - router.sudo(&*api, write_cache, block, msg) - }) + router.wasm.increment_transaction_index(); + + transactional( + &mut *storage, + |write_cache, _| router.sudo(&*api, write_cache, block, msg), + || router.wasm.increment_transaction_index(), + ) } } /// The Router plays a critical role in managing and directing diff --git a/src/app_builder.rs b/src/app_builder.rs index 85f3dc43..1e6b6a4b 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -6,7 +6,7 @@ use crate::{ Wasm, WasmKeeper, }; use cosmwasm_std::testing::{mock_env, MockApi, MockStorage}; -use cosmwasm_std::{Api, BlockInfo, CustomMsg, CustomQuery, Empty, Storage, TransactionInfo}; +use cosmwasm_std::{Api, BlockInfo, CustomMsg, CustomQuery, Empty, Storage}; use serde::de::DeserializeOwned; use std::fmt::Debug; @@ -46,7 +46,6 @@ pub type BasicAppBuilder = AppBuilder< pub struct AppBuilder { api: Api, block: BlockInfo, - transaction: TransactionInfo, storage: Storage, bank: Bank, wasm: Wasm, @@ -96,7 +95,6 @@ impl AppBuilder { api: MockApi::default(), block: mock_env().block, - transaction: mock_env().transaction.unwrap(), storage: MockStorage::new(), bank: BankKeeper::new(), wasm: WasmKeeper::new(), @@ -133,7 +131,6 @@ where AppBuilder { api: MockApi::default(), block: mock_env().block, - transaction: mock_env().transaction.unwrap(), storage: MockStorage::new(), bank: BankKeeper::new(), wasm: WasmKeeper::new(), @@ -169,7 +166,6 @@ where storage, custom, block, - transaction, staking, distribution, ibc, @@ -181,7 +177,6 @@ where AppBuilder { api, block, - transaction, storage, bank, wasm, @@ -206,7 +201,6 @@ where storage, custom, block, - transaction, staking, distribution, ibc, @@ -218,7 +212,6 @@ where AppBuilder { api, block, - transaction, storage, bank, wasm, @@ -243,7 +236,6 @@ where storage, custom, block, - transaction, staking, distribution, ibc, @@ -255,7 +247,6 @@ where AppBuilder { api, block, - transaction, storage, bank, wasm, @@ -280,7 +271,6 @@ where bank, custom, block, - transaction, staking, distribution, ibc, @@ -292,7 +282,6 @@ where AppBuilder { api, block, - transaction, storage, bank, wasm, @@ -321,7 +310,6 @@ where api, storage, block, - transaction, staking, distribution, ibc, @@ -333,7 +321,6 @@ where AppBuilder { api, block, - transaction, storage, bank, wasm, @@ -358,7 +345,6 @@ where storage, custom, block, - transaction, bank, distribution, ibc, @@ -370,7 +356,6 @@ where AppBuilder { api, block, - transaction, storage, bank, wasm, @@ -405,7 +390,6 @@ where storage, custom, block, - transaction, staking, bank, ibc, @@ -417,7 +401,6 @@ where AppBuilder { api, block, - transaction, storage, bank, wasm, @@ -448,7 +431,6 @@ where storage, custom, block, - transaction, staking, bank, distribution, @@ -460,7 +442,6 @@ where AppBuilder { api, block, - transaction, storage, bank, wasm, @@ -485,7 +466,6 @@ where storage, custom, block, - transaction, staking, bank, distribution, @@ -497,7 +477,6 @@ where AppBuilder { api, block, - transaction, storage, bank, wasm, @@ -522,7 +501,6 @@ where storage, custom, block, - transaction, staking, bank, distribution, @@ -534,7 +512,6 @@ where AppBuilder { api, block, - transaction, storage, bank, wasm, @@ -593,7 +570,6 @@ where api: self.api, storage: self.storage, block: self.block, - transaction: self.transaction, }; app.init_modules(init_fn); app diff --git a/src/tests/test_app.rs b/src/tests/test_app.rs index 685234eb..61b25d50 100644 --- a/src/tests/test_app.rs +++ b/src/tests/test_app.rs @@ -143,22 +143,26 @@ fn multi_level_bank_cache() { assert_eq!(router_rcpt, vec![]); // now, second level cache - transactional(&mut cache, |cache2, read| { - let msg = BankMsg::Send { - to_address: recipient_addr.clone().into(), - amount: coins(12, "eth"), - }; - app.router() - .execute(app.api(), cache2, &app.block_info(), owner_addr, msg.into()) - .unwrap(); + transactional( + &mut cache, + |cache2, read| { + let msg = BankMsg::Send { + to_address: recipient_addr.clone().into(), + amount: coins(12, "eth"), + }; + app.router() + .execute(app.api(), cache2, &app.block_info(), owner_addr, msg.into()) + .unwrap(); - // shows up in 2nd cache - let cached_rcpt = query_router(app.router(), app.api(), read, &recipient_addr); - assert_eq!(coins(25, "eth"), cached_rcpt); - let cached2_rcpt = query_router(app.router(), app.api(), cache2, &recipient_addr); - assert_eq!(coins(37, "eth"), cached2_rcpt); - Ok(()) - }) + // shows up in 2nd cache + let cached_rcpt = query_router(app.router(), app.api(), read, &recipient_addr); + assert_eq!(coins(25, "eth"), cached_rcpt); + let cached2_rcpt = query_router(app.router(), app.api(), cache2, &recipient_addr); + assert_eq!(coins(37, "eth"), cached2_rcpt); + Ok(()) + }, + || app.router.wasm.increment_transaction_index(), + ) .unwrap(); // apply first to router diff --git a/src/transactions.rs b/src/transactions.rs index e396dfb6..a4cc5c54 100644 --- a/src/transactions.rs +++ b/src/transactions.rs @@ -11,13 +11,15 @@ use std::ops::{Bound, RangeBounds}; /// This is internal as it can change any time if the map implementation is swapped out. type BTreeMapPairRef<'a, T = Vec> = (&'a Vec, &'a T); -pub fn transactional(base: &mut dyn Storage, action: F) -> AnyResult +pub fn transactional(base: &mut dyn Storage, action: F, increment: G) -> AnyResult where F: FnOnce(&mut dyn Storage, &dyn Storage) -> AnyResult, + G: FnOnce(), { let mut cache = StorageTransaction::new(base); let res = action(&mut cache, base)?; cache.prepare().commit(base); + increment(); Ok(res) } diff --git a/src/wasm.rs b/src/wasm.rs index 35bf331b..a2f37908 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -19,6 +19,7 @@ use schemars::JsonSchema; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use std::borrow::Borrow; +use std::cell::RefCell; use std::collections::BTreeMap; use std::fmt::Debug; @@ -167,6 +168,17 @@ pub trait Wasm { let storage = PrefixedStorage::multilevel(storage, &[NAMESPACE_WASM, &namespace]); Box::new(storage) } + + /// Increments transaction index. + fn increment_transaction_index(&self) {} + + /// Resets transaction index to zero. + fn reset_transaction_index(&self) {} + + /// Returns a copy of the current transaction info. + fn transaction_info(&self) -> TransactionInfo { + TransactionInfo { index: 0 } + } } /// A structure representing a default wasm keeper. @@ -179,6 +191,8 @@ pub struct WasmKeeper { address_generator: Box, /// Contract's code checksum generator. checksum_generator: Box, + /// Transaction info. + transaction: RefCell, /// Just markers to make type elision fork when using it as `Wasm` trait _p: std::marker::PhantomData, } @@ -192,6 +206,7 @@ impl Default for WasmKeeper { address_generator: Box::new(SimpleAddressGenerator), checksum_generator: Box::new(SimpleChecksumGenerator), _p: std::marker::PhantomData, + transaction: RefCell::new(TransactionInfo { index: 0 }), } } } @@ -336,6 +351,18 @@ where let storage = self.contract_storage(storage, address); storage.range(None, None, Order::Ascending).collect() } + + fn increment_transaction_index(&self) { + self.transaction.borrow_mut().index += 1; + } + + fn reset_transaction_index(&self) { + self.transaction.borrow_mut().index = 0; + } + + fn transaction_info(&self) -> TransactionInfo { + self.transaction.borrow().clone() + } } impl WasmKeeper { @@ -817,9 +844,11 @@ where } = msg; // execute in cache - let res = transactional(storage, |write_cache, _| { - router.execute(api, write_cache, block, contract.clone(), msg) - }); + let res = transactional( + storage, + |write_cache, _| router.execute(api, write_cache, block, contract.clone(), msg), + || self.increment_transaction_index(), + ); // call reply if meaningful if let Ok(mut r) = res { @@ -1173,18 +1202,22 @@ where // execute_submsg or App.execute_multi. // However, we need to get write and read access to the same storage in two different objects, // and this is the only way I know how to do so. - transactional(storage, |write_cache, read_store| { - let mut contract_storage = self.contract_storage_mut(write_cache, &address); - let querier = RouterQuerier::new(router, api, read_store, block); - let env = self.get_env(address, block); + transactional( + storage, + |write_cache, read_store| { + let mut contract_storage = self.contract_storage_mut(write_cache, &address); + let querier = RouterQuerier::new(router, api, read_store, block); + let env = self.get_env(address, block); - let deps = DepsMut { - storage: contract_storage.as_mut(), - api, - querier: QuerierWrapper::new(&querier), - }; - action(handler, deps, env) - }) + let deps = DepsMut { + storage: contract_storage.as_mut(), + api, + querier: QuerierWrapper::new(&querier), + }; + action(handler, deps, env) + }, + || {}, + ) } /// Saves contract data in a storage under specified address. @@ -1310,34 +1343,42 @@ mod test { let block = mock_env().block; let code_id = wasm_keeper.store_code(creator_addr, error::contract(false)); - transactional(&mut wasm_storage, |cache, _| { - // cannot register contract with unregistered codeId - wasm_keeper.register_contract( - &api, - cache, - code_id + 1, - user_addr.clone(), - admin_addr.clone(), - "label".to_owned(), - 1000, - None, - ) - }) + transactional( + &mut wasm_storage, + |cache, _| { + // cannot register contract with unregistered codeId + wasm_keeper.register_contract( + &api, + cache, + code_id + 1, + user_addr.clone(), + admin_addr.clone(), + "label".to_owned(), + 1000, + None, + ) + }, + || wasm_keeper.increment_transaction_index(), + ) .unwrap_err(); - let contract_addr = transactional(&mut wasm_storage, |cache, _| { - // we can register a new instance of this code - wasm_keeper.register_contract( - &api, - cache, - code_id, - user_addr.clone(), - admin_addr.clone(), - "label".to_owned(), - 1000, - None, - ) - }) + let contract_addr = transactional( + &mut wasm_storage, + |cache, _| { + // we can register a new instance of this code + wasm_keeper.register_contract( + &api, + cache, + code_id, + user_addr.clone(), + admin_addr.clone(), + "label".to_owned(), + 1000, + None, + ) + }, + || wasm_keeper.increment_transaction_index(), + ) .unwrap(); // verify contract data are as expected @@ -1356,19 +1397,23 @@ mod test { } ); - let err = transactional(&mut wasm_storage, |cache, _| { - // now, we call this contract and see the error message from the contract - let info = mock_info(user_addr.as_str(), &[]); - wasm_keeper.call_instantiate( - contract_addr.clone(), - &api, - cache, - &mock_router(), - &block, - info, - b"{}".to_vec(), - ) - }) + let err = transactional( + &mut wasm_storage, + |cache, _| { + // now, we call this contract and see the error message from the contract + let info = mock_info(user_addr.as_str(), &[]); + wasm_keeper.call_instantiate( + contract_addr.clone(), + &api, + cache, + &mock_router(), + &block, + info, + b"{}".to_vec(), + ) + }, + || wasm_keeper.increment_transaction_index(), + ) .unwrap_err(); // StdError from contract_error auto-converted to string @@ -1377,19 +1422,23 @@ mod test { err.downcast().unwrap() ); - let err = transactional(&mut wasm_storage, |cache, _| { - // and the error for calling an unregistered contract - let info = mock_info(user_addr.as_str(), &[]); - wasm_keeper.call_instantiate( - unregistered_addr, - &api, - cache, - &mock_router(), - &block, - info, - b"{}".to_vec(), - ) - }) + let err = transactional( + &mut wasm_storage, + |cache, _| { + // and the error for calling an unregistered contract + let info = mock_info(user_addr.as_str(), &[]); + wasm_keeper.call_instantiate( + unregistered_addr, + &api, + cache, + &mock_router(), + &block, + info, + b"{}".to_vec(), + ) + }, + || wasm_keeper.increment_transaction_index(), + ) .unwrap_err(); // Default error message from router when not found @@ -1697,132 +1746,144 @@ mod test { let payout1 = coin(100, "TGD"); // set contract 1 and commit (on router) - let contract1 = transactional(&mut wasm_storage, |cache, _| { - let contract = wasm_keeper - .register_contract( - &api, - cache, - code_id, - user_addr.clone(), - None, - "".to_string(), - 1000, - None, - ) - .unwrap(); - let info = mock_info(user_addr.as_str(), &[]); - let init_msg = to_json_vec(&payout::InstantiateMessage { - payout: payout1.clone(), - }) - .unwrap(); - wasm_keeper - .call_instantiate( - contract.clone(), - &api, - cache, - &mock_router(), - &block, - info, - init_msg, - ) + let contract1 = transactional( + &mut wasm_storage, + |cache, _| { + let contract = wasm_keeper + .register_contract( + &api, + cache, + code_id, + user_addr.clone(), + None, + "".to_string(), + 1000, + None, + ) + .unwrap(); + let info = mock_info(user_addr.as_str(), &[]); + let init_msg = to_json_vec(&payout::InstantiateMessage { + payout: payout1.clone(), + }) .unwrap(); + wasm_keeper + .call_instantiate( + contract.clone(), + &api, + cache, + &mock_router(), + &block, + info, + init_msg, + ) + .unwrap(); - Ok(contract) - }) + Ok(contract) + }, + || wasm_keeper.increment_transaction_index(), + ) .unwrap(); let payout2 = coin(50, "BTC"); let payout3 = coin(1234, "ATOM"); // create a new cache and check we can use contract 1 - let (contract2, contract3) = transactional(&mut wasm_storage, |cache, wasm_reader| { - assert_payout(&wasm_keeper, cache, &contract1, &payout1); + let (contract2, contract3) = transactional( + &mut wasm_storage, + |cache, wasm_reader| { + assert_payout(&wasm_keeper, cache, &contract1, &payout1); - // create contract 2 and use it - let contract2 = wasm_keeper - .register_contract( - &api, - cache, - code_id, - user_addr.clone(), - None, - "".to_owned(), - 1000, - None, - ) - .unwrap(); - let info = mock_info(user_addr.as_str(), &[]); - let init_msg = to_json_vec(&payout::InstantiateMessage { - payout: payout2.clone(), - }) - .unwrap(); - let _res = wasm_keeper - .call_instantiate( - contract2.clone(), - &api, - cache, - &mock_router(), - &block, - info, - init_msg, - ) - .unwrap(); - assert_payout(&wasm_keeper, cache, &contract2, &payout2); - - // create a level2 cache and check we can use contract 1 and contract 2 - let contract3 = transactional(cache, |cache2, read| { - assert_payout(&wasm_keeper, cache2, &contract1, &payout1); - assert_payout(&wasm_keeper, cache2, &contract2, &payout2); - - // create a contract on level 2 - let contract3 = wasm_keeper + // create contract 2 and use it + let contract2 = wasm_keeper .register_contract( &api, - cache2, + cache, code_id, - user_addr, + user_addr.clone(), None, "".to_owned(), 1000, None, ) .unwrap(); - let info = mock_info("johnny", &[]); + let info = mock_info(user_addr.as_str(), &[]); let init_msg = to_json_vec(&payout::InstantiateMessage { - payout: payout3.clone(), + payout: payout2.clone(), }) .unwrap(); let _res = wasm_keeper .call_instantiate( - contract3.clone(), + contract2.clone(), &api, - cache2, + cache, &mock_router(), &block, info, init_msg, ) .unwrap(); - assert_payout(&wasm_keeper, cache2, &contract3, &payout3); + assert_payout(&wasm_keeper, cache, &contract2, &payout2); - // ensure first cache still doesn't see this contract - assert_no_contract(read, &contract3); - Ok(contract3) - }) - .unwrap(); + // create a level2 cache and check we can use contract 1 and contract 2 + let contract3 = transactional( + cache, + |cache2, read| { + assert_payout(&wasm_keeper, cache2, &contract1, &payout1); + assert_payout(&wasm_keeper, cache2, &contract2, &payout2); + + // create a contract on level 2 + let contract3 = wasm_keeper + .register_contract( + &api, + cache2, + code_id, + user_addr, + None, + "".to_owned(), + 1000, + None, + ) + .unwrap(); + let info = mock_info("johnny", &[]); + let init_msg = to_json_vec(&payout::InstantiateMessage { + payout: payout3.clone(), + }) + .unwrap(); + let _res = wasm_keeper + .call_instantiate( + contract3.clone(), + &api, + cache2, + &mock_router(), + &block, + info, + init_msg, + ) + .unwrap(); + assert_payout(&wasm_keeper, cache2, &contract3, &payout3); + + // ensure first cache still doesn't see this contract + assert_no_contract(read, &contract3); + Ok(contract3) + }, + || wasm_keeper.increment_transaction_index(), + ) + .unwrap(); - // after applying transaction, all contracts present on cache - assert_payout(&wasm_keeper, cache, &contract1, &payout1); - assert_payout(&wasm_keeper, cache, &contract2, &payout2); - assert_payout(&wasm_keeper, cache, &contract3, &payout3); + // after applying transaction, all contracts present on cache + assert_payout(&wasm_keeper, cache, &contract1, &payout1); + assert_payout(&wasm_keeper, cache, &contract2, &payout2); + assert_payout(&wasm_keeper, cache, &contract3, &payout3); - // but not yet the root router - assert_no_contract(wasm_reader, &contract1); - assert_no_contract(wasm_reader, &contract2); - assert_no_contract(wasm_reader, &contract3); + // but not yet the root router + assert_no_contract(wasm_reader, &contract1); + assert_no_contract(wasm_reader, &contract2); + assert_no_contract(wasm_reader, &contract3); - Ok((contract2, contract3)) - }) + Ok((contract2, contract3)) + }, + || wasm_keeper.increment_transaction_index(), + ) .unwrap(); // ensure that it is now applied to the router From 60b3803e36d3f4d9a1cd758300c17fefddbdac94 Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Tue, 2 Apr 2024 15:24:44 +0200 Subject: [PATCH 04/12] Removed unnecessary code. --- src/app.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/app.rs b/src/app.rs index aa9bfd60..b9f0c4f5 100644 --- a/src/app.rs +++ b/src/app.rs @@ -459,8 +459,6 @@ where storage, } = self; - router.wasm.increment_transaction_index(); - transactional( &mut *storage, |write_cache, _| { @@ -492,8 +490,6 @@ where storage, } = self; - router.wasm.increment_transaction_index(); - transactional( &mut *storage, |write_cache, _| router.wasm.sudo(&*api, write_cache, router, block, msg), @@ -515,8 +511,6 @@ where storage, } = self; - router.wasm.increment_transaction_index(); - transactional( &mut *storage, |write_cache, _| router.sudo(&*api, write_cache, block, msg), From 588ce6dcaa2abdf1d536010a7c7e1a42bd1f33bd Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Tue, 2 Apr 2024 15:26:12 +0200 Subject: [PATCH 05/12] Reverted unnecessary changes. --- src/app_builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app_builder.rs b/src/app_builder.rs index 1e6b6a4b..56434c17 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -568,8 +568,8 @@ where let mut app = App { router, api: self.api, - storage: self.storage, block: self.block, + storage: self.storage, }; app.init_modules(init_fn); app From 557f2486887888c7c24bd90cc6a64e01b4422696 Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Tue, 2 Apr 2024 15:41:57 +0200 Subject: [PATCH 06/12] Refactored Wasm trait. --- src/app.rs | 14 +++++++++----- src/app_builder.rs | 8 +++++++- src/tests/test_app.rs | 2 +- src/wasm.rs | 28 ++++++++++++++-------------- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/app.rs b/src/app.rs index b9f0c4f5..e4609ede 100644 --- a/src/app.rs +++ b/src/app.rs @@ -410,7 +410,9 @@ where .staking .process_queue(&self.api, &mut self.storage, &self.router, &self.block) .unwrap(); - self.router.wasm.reset_transaction_index(); + self.router + .wasm + .set_transaction_info(TransactionInfo { index: 0 }); self.block = block; } @@ -420,7 +422,9 @@ where .staking .process_queue(&self.api, &mut self.storage, &self.router, &self.block) .unwrap(); - self.router.wasm.reset_transaction_index(); + self.router + .wasm + .set_transaction_info(TransactionInfo { index: 0 }); action(&mut self.block); } @@ -466,7 +470,7 @@ where .map(|msg| router.execute(&*api, write_cache, block, sender.clone(), msg)) .collect() }, - || router.wasm.increment_transaction_index(), + || router.wasm.inc_transaction_index(), ) } @@ -493,7 +497,7 @@ where transactional( &mut *storage, |write_cache, _| router.wasm.sudo(&*api, write_cache, router, block, msg), - || router.wasm.increment_transaction_index(), + || router.wasm.inc_transaction_index(), ) } @@ -514,7 +518,7 @@ where transactional( &mut *storage, |write_cache, _| router.sudo(&*api, write_cache, block, msg), - || router.wasm.increment_transaction_index(), + || router.wasm.inc_transaction_index(), ) } } diff --git a/src/app_builder.rs b/src/app_builder.rs index 56434c17..64abc99d 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -6,7 +6,7 @@ use crate::{ Wasm, WasmKeeper, }; use cosmwasm_std::testing::{mock_env, MockApi, MockStorage}; -use cosmwasm_std::{Api, BlockInfo, CustomMsg, CustomQuery, Empty, Storage}; +use cosmwasm_std::{Api, BlockInfo, CustomMsg, CustomQuery, Empty, Storage, TransactionInfo}; use serde::de::DeserializeOwned; use std::fmt::Debug; @@ -530,6 +530,12 @@ where self } + /// Overwrites the initial transaction. + pub fn with_transaction(self, transaction: TransactionInfo) -> Self { + self.wasm.set_transaction_info(transaction); + self + } + /// Builds final `App`. At this point all components type have to be properly related to each /// other. If there are some generics related compilation errors, make sure that all components /// are properly relating to each other. diff --git a/src/tests/test_app.rs b/src/tests/test_app.rs index 61b25d50..99c685b2 100644 --- a/src/tests/test_app.rs +++ b/src/tests/test_app.rs @@ -161,7 +161,7 @@ fn multi_level_bank_cache() { assert_eq!(coins(37, "eth"), cached2_rcpt); Ok(()) }, - || app.router.wasm.increment_transaction_index(), + || app.router.wasm.inc_transaction_index(), ) .unwrap(); diff --git a/src/wasm.rs b/src/wasm.rs index a2f37908..7823c916 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -170,10 +170,10 @@ pub trait Wasm { } /// Increments transaction index. - fn increment_transaction_index(&self) {} + fn inc_transaction_index(&self) {} - /// Resets transaction index to zero. - fn reset_transaction_index(&self) {} + /// Sets custom transaction info. + fn set_transaction_info(&self, _transaction: TransactionInfo) {} /// Returns a copy of the current transaction info. fn transaction_info(&self) -> TransactionInfo { @@ -352,12 +352,12 @@ where storage.range(None, None, Order::Ascending).collect() } - fn increment_transaction_index(&self) { + fn inc_transaction_index(&self) { self.transaction.borrow_mut().index += 1; } - fn reset_transaction_index(&self) { - self.transaction.borrow_mut().index = 0; + fn set_transaction_info(&self, transaction: TransactionInfo) { + self.transaction.borrow_mut().index = transaction.index; } fn transaction_info(&self) -> TransactionInfo { @@ -847,7 +847,7 @@ where let res = transactional( storage, |write_cache, _| router.execute(api, write_cache, block, contract.clone(), msg), - || self.increment_transaction_index(), + || self.inc_transaction_index(), ); // call reply if meaningful @@ -1358,7 +1358,7 @@ mod test { None, ) }, - || wasm_keeper.increment_transaction_index(), + || wasm_keeper.inc_transaction_index(), ) .unwrap_err(); @@ -1377,7 +1377,7 @@ mod test { None, ) }, - || wasm_keeper.increment_transaction_index(), + || wasm_keeper.inc_transaction_index(), ) .unwrap(); @@ -1412,7 +1412,7 @@ mod test { b"{}".to_vec(), ) }, - || wasm_keeper.increment_transaction_index(), + || wasm_keeper.inc_transaction_index(), ) .unwrap_err(); @@ -1437,7 +1437,7 @@ mod test { b"{}".to_vec(), ) }, - || wasm_keeper.increment_transaction_index(), + || wasm_keeper.inc_transaction_index(), ) .unwrap_err(); @@ -1780,7 +1780,7 @@ mod test { Ok(contract) }, - || wasm_keeper.increment_transaction_index(), + || wasm_keeper.inc_transaction_index(), ) .unwrap(); @@ -1866,7 +1866,7 @@ mod test { assert_no_contract(read, &contract3); Ok(contract3) }, - || wasm_keeper.increment_transaction_index(), + || wasm_keeper.inc_transaction_index(), ) .unwrap(); @@ -1882,7 +1882,7 @@ mod test { Ok((contract2, contract3)) }, - || wasm_keeper.increment_transaction_index(), + || wasm_keeper.inc_transaction_index(), ) .unwrap(); From 7d2783b2ed339c7eeaff5cb00e8e797c38749982 Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Tue, 2 Apr 2024 16:15:19 +0200 Subject: [PATCH 07/12] Added test for app builder. --- tests/test_app_builder/mod.rs | 1 + tests/test_app_builder/test_with_transaction.rs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 tests/test_app_builder/test_with_transaction.rs diff --git a/tests/test_app_builder/mod.rs b/tests/test_app_builder/mod.rs index d2dcc5c2..098253cc 100644 --- a/tests/test_app_builder/mod.rs +++ b/tests/test_app_builder/mod.rs @@ -14,6 +14,7 @@ mod test_with_ibc; mod test_with_staking; mod test_with_stargate; mod test_with_storage; +mod test_with_transaction; mod test_with_wasm; const NO_MESSAGE: &str = ""; diff --git a/tests/test_app_builder/test_with_transaction.rs b/tests/test_app_builder/test_with_transaction.rs new file mode 100644 index 00000000..b7e76a10 --- /dev/null +++ b/tests/test_app_builder/test_with_transaction.rs @@ -0,0 +1,17 @@ +use cosmwasm_std::TransactionInfo; +use cw_multi_test::{no_init, AppBuilder}; + +#[test] +fn building_app_with_custom_transaction_info_should_work() { + // prepare custom transaction info + let transaction_info = TransactionInfo { index: 21 }; + + // build the application with custom transaction info + let app_builder = AppBuilder::default(); + let app = app_builder + .with_transaction(transaction_info) + .build(no_init); + + // index should be the same value as provided during initialization + assert_eq!(21, app.transaction_info().index); +} From 97558a8473493d6c9352bfc9f781f89f6fa0cc6d Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Tue, 2 Apr 2024 16:42:20 +0200 Subject: [PATCH 08/12] Added tests. --- tests/mod.rs | 4 +- tests/test_transaction_info/mod.rs | 1 + .../test_transaction_index.rs | 68 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 tests/test_transaction_info/mod.rs create mode 100644 tests/test_transaction_info/test_transaction_index.rs diff --git a/tests/mod.rs b/tests/mod.rs index edf9910d..1c449ef8 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -6,6 +6,7 @@ mod test_app_builder; mod test_contract_storage; mod test_module; mod test_prefixed_storage; +mod test_transaction_info; mod test_wasm; mod test_contracts { @@ -13,7 +14,6 @@ mod test_contracts { pub mod counter { use cosmwasm_std::{ to_json_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response, StdError, - WasmMsg, }; use cw_multi_test::{Contract, ContractWrapper}; use cw_storage_plus::Item; @@ -46,7 +46,7 @@ mod test_contracts { deps: DepsMut, _env: Env, _info: MessageInfo, - _msg: WasmMsg, + _msg: Empty, ) -> Result { if let Some(mut counter) = COUNTER.may_load(deps.storage).unwrap() { counter += 1; diff --git a/tests/test_transaction_info/mod.rs b/tests/test_transaction_info/mod.rs new file mode 100644 index 00000000..870b702a --- /dev/null +++ b/tests/test_transaction_info/mod.rs @@ -0,0 +1 @@ +mod test_transaction_index; diff --git a/tests/test_transaction_info/test_transaction_index.rs b/tests/test_transaction_info/test_transaction_index.rs new file mode 100644 index 00000000..bc6c006f --- /dev/null +++ b/tests/test_transaction_info/test_transaction_index.rs @@ -0,0 +1,68 @@ +use crate::test_contracts::counter; +use cosmwasm_std::Empty; +use cw_multi_test::{App, Executor}; + +#[test] +fn default_transaction_index_should_be_zero() { + let app = App::default(); + assert_eq!(0, app.transaction_info().index); +} + +#[test] +fn instantiate_should_increment_transaction_index() { + let mut app = App::default(); + let sender_addr = app.api().addr_make("sender"); + let code_id = app.store_code(counter::contract()); + app.instantiate_contract(code_id, sender_addr, &Empty {}, &[], "counter", None) + .unwrap(); + assert_eq!(1, app.transaction_info().index); +} + +#[test] +fn execute_should_increment_transaction_index() { + let mut app = App::default(); + let sender_addr = app.api().addr_make("sender"); + let code_id = app.store_code(counter::contract()); + let contract_addr = app + .instantiate_contract( + code_id, + sender_addr.clone(), + &Empty {}, + &[], + "counter", + None, + ) + .unwrap(); + assert_eq!(1, app.transaction_info().index); + app.execute_contract(sender_addr, contract_addr, &Empty {}, &[]) + .unwrap(); + assert_eq!(2, app.transaction_info().index); +} + +#[test] +fn updating_block_should_reset_transaction_index() { + let mut app = App::default(); + let sender_addr = app.api().addr_make("sender"); + let code_id = app.store_code(counter::contract()); + let contract_addr = app + .instantiate_contract( + code_id, + sender_addr.clone(), + &Empty {}, + &[], + "counter", + None, + ) + .unwrap(); + assert_eq!(1, app.transaction_info().index); + app.execute_contract(sender_addr.clone(), contract_addr.clone(), &Empty {}, &[]) + .unwrap(); + assert_eq!(2, app.transaction_info().index); + app.update_block(|block| { + block.height += 1; + block.time = block.time.plus_seconds(60); + }); + app.execute_contract(sender_addr, contract_addr, &Empty {}, &[]) + .unwrap(); + assert_eq!(1, app.transaction_info().index); +} From 186cc6a524509e97ffeef35889b335cce987bf5e Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Tue, 2 Apr 2024 16:53:08 +0200 Subject: [PATCH 09/12] Added tests. --- .../test_transaction_index.rs | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/test_transaction_info/test_transaction_index.rs b/tests/test_transaction_info/test_transaction_index.rs index bc6c006f..d9930a11 100644 --- a/tests/test_transaction_info/test_transaction_index.rs +++ b/tests/test_transaction_info/test_transaction_index.rs @@ -1,5 +1,5 @@ use crate::test_contracts::counter; -use cosmwasm_std::Empty; +use cosmwasm_std::{BlockInfo, Empty, Timestamp}; use cw_multi_test::{App, Executor}; #[test] @@ -39,6 +39,37 @@ fn execute_should_increment_transaction_index() { assert_eq!(2, app.transaction_info().index); } +#[test] +fn setting_block_should_reset_transaction_index() { + let mut app = App::default(); + let sender_addr = app.api().addr_make("sender"); + let code_id = app.store_code(counter::contract()); + let contract_addr = app + .instantiate_contract( + code_id, + sender_addr.clone(), + &Empty {}, + &[], + "counter", + None, + ) + .unwrap(); + assert_eq!(1, app.transaction_info().index); + app.execute_contract(sender_addr.clone(), contract_addr.clone(), &Empty {}, &[]) + .unwrap(); + assert_eq!(2, app.transaction_info().index); + // prepare custom block properties + let block = BlockInfo { + height: 20, + time: Timestamp::from_nanos(1_571_797_419_879_305_544), + chain_id: "my-testnet".to_string(), + }; + app.set_block(block); + app.execute_contract(sender_addr, contract_addr, &Empty {}, &[]) + .unwrap(); + assert_eq!(1, app.transaction_info().index); +} + #[test] fn updating_block_should_reset_transaction_index() { let mut app = App::default(); From 8b36cad9055e6f4eaec1ab13cc9c64c74c41bf9e Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Tue, 2 Apr 2024 17:23:11 +0200 Subject: [PATCH 10/12] Added test for wasm defaults (transaction info). --- tests/test_app_builder/test_with_wasm.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/test_app_builder/test_with_wasm.rs b/tests/test_app_builder/test_with_wasm.rs index 390c8684..9d2fc836 100644 --- a/tests/test_app_builder/test_with_wasm.rs +++ b/tests/test_app_builder/test_with_wasm.rs @@ -1,7 +1,8 @@ -use crate::test_app_builder::MyKeeper; +use crate::test_app_builder::{MyKeeper, NO_MESSAGE}; use crate::test_contracts; use cosmwasm_std::{ - Addr, Api, Binary, BlockInfo, Empty, Querier, Record, Storage, WasmMsg, WasmQuery, + Addr, Api, Binary, BlockInfo, Empty, Querier, Record, Storage, TransactionInfo, WasmMsg, + WasmQuery, }; use cw_multi_test::error::{bail, AnyResult}; use cw_multi_test::{ @@ -165,3 +166,14 @@ fn compiling_with_wasm_keeper_should_work() { let app_builder = AppBuilder::default(); let _ = app_builder.with_wasm(WasmKeeper::default()).build(no_init); } + +#[test] +fn default_transaction_info_should_work() { + let wasm_keeper: Box> = + Box::new(MyWasmKeeper::new(NO_MESSAGE, NO_MESSAGE, NO_MESSAGE)); + assert_eq!(0, wasm_keeper.transaction_info().index); + wasm_keeper.inc_transaction_index(); + assert_eq!(0, wasm_keeper.transaction_info().index); + wasm_keeper.set_transaction_info(TransactionInfo { index: 15 }); + assert_eq!(0, wasm_keeper.transaction_info().index); +} From 998f9909c6f02a552a5e3f5b0c8086b699322dcc Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 3 Apr 2024 11:14:18 +0200 Subject: [PATCH 11/12] Refactored to have no transactions at the beginning of the block. --- Cargo.lock | 10 +++---- src/app.rs | 10 ++----- src/app_builder.rs | 2 +- src/wasm.rs | 25 ++++++++++------- .../test_app_builder/test_with_transaction.rs | 4 +-- tests/test_app_builder/test_with_wasm.rs | 8 +++--- .../test_transaction_index.rs | 28 +++++++++++-------- 7 files changed, 47 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 96c85029..dd639dde 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -599,7 +599,7 @@ dependencies = [ "itertools 0.11.0", "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -712,7 +712,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] @@ -806,9 +806,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.57" +version = "2.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11a6ae1e52eb25aab8f3fb9fca13be982a373b8f1157ca14b897a825ba4a2d35" +checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" dependencies = [ "proc-macro2", "quote", @@ -832,7 +832,7 @@ checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.57", + "syn 2.0.58", ] [[package]] diff --git a/src/app.rs b/src/app.rs index e4609ede..b4e644f2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -410,9 +410,7 @@ where .staking .process_queue(&self.api, &mut self.storage, &self.router, &self.block) .unwrap(); - self.router - .wasm - .set_transaction_info(TransactionInfo { index: 0 }); + self.router.wasm.set_transaction_info(None); self.block = block; } @@ -422,9 +420,7 @@ where .staking .process_queue(&self.api, &mut self.storage, &self.router, &self.block) .unwrap(); - self.router - .wasm - .set_transaction_info(TransactionInfo { index: 0 }); + self.router.wasm.set_transaction_info(None); action(&mut self.block); } @@ -434,7 +430,7 @@ where } /// Returns a copy of the current [TransactionInfo]. - pub fn transaction_info(&self) -> TransactionInfo { + pub fn transaction_info(&self) -> Option { self.router.wasm.transaction_info() } diff --git a/src/app_builder.rs b/src/app_builder.rs index 64abc99d..a9b95cf7 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -531,7 +531,7 @@ where } /// Overwrites the initial transaction. - pub fn with_transaction(self, transaction: TransactionInfo) -> Self { + pub fn with_transaction(self, transaction: Option) -> Self { self.wasm.set_transaction_info(transaction); self } diff --git a/src/wasm.rs b/src/wasm.rs index 7823c916..59492081 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -173,11 +173,11 @@ pub trait Wasm { fn inc_transaction_index(&self) {} /// Sets custom transaction info. - fn set_transaction_info(&self, _transaction: TransactionInfo) {} + fn set_transaction_info(&self, _transaction: Option) {} /// Returns a copy of the current transaction info. - fn transaction_info(&self) -> TransactionInfo { - TransactionInfo { index: 0 } + fn transaction_info(&self) -> Option { + None } } @@ -191,8 +191,8 @@ pub struct WasmKeeper { address_generator: Box, /// Contract's code checksum generator. checksum_generator: Box, - /// Transaction info. - transaction: RefCell, + /// Optional transaction info, filled when some transaction was already executed. + transaction: RefCell>, /// Just markers to make type elision fork when using it as `Wasm` trait _p: std::marker::PhantomData, } @@ -205,8 +205,8 @@ impl Default for WasmKeeper { code_data: BTreeMap::default(), address_generator: Box::new(SimpleAddressGenerator), checksum_generator: Box::new(SimpleChecksumGenerator), + transaction: RefCell::new(None), _p: std::marker::PhantomData, - transaction: RefCell::new(TransactionInfo { index: 0 }), } } } @@ -353,14 +353,19 @@ where } fn inc_transaction_index(&self) { - self.transaction.borrow_mut().index += 1; + let mut transaction_ref = self.transaction.borrow_mut(); + let Some(transaction) = transaction_ref.as_mut() else { + *transaction_ref = Some(TransactionInfo { index: 0 }); + return; + }; + transaction.index += 1; } - fn set_transaction_info(&self, transaction: TransactionInfo) { - self.transaction.borrow_mut().index = transaction.index; + fn set_transaction_info(&self, transaction: Option) { + *self.transaction.borrow_mut() = transaction; } - fn transaction_info(&self) -> TransactionInfo { + fn transaction_info(&self) -> Option { self.transaction.borrow().clone() } } diff --git a/tests/test_app_builder/test_with_transaction.rs b/tests/test_app_builder/test_with_transaction.rs index b7e76a10..19667dcc 100644 --- a/tests/test_app_builder/test_with_transaction.rs +++ b/tests/test_app_builder/test_with_transaction.rs @@ -9,9 +9,9 @@ fn building_app_with_custom_transaction_info_should_work() { // build the application with custom transaction info let app_builder = AppBuilder::default(); let app = app_builder - .with_transaction(transaction_info) + .with_transaction(Some(transaction_info)) .build(no_init); // index should be the same value as provided during initialization - assert_eq!(21, app.transaction_info().index); + assert_eq!(21, app.transaction_info().unwrap().index); } diff --git a/tests/test_app_builder/test_with_wasm.rs b/tests/test_app_builder/test_with_wasm.rs index 9d2fc836..cbb451e2 100644 --- a/tests/test_app_builder/test_with_wasm.rs +++ b/tests/test_app_builder/test_with_wasm.rs @@ -171,9 +171,9 @@ fn compiling_with_wasm_keeper_should_work() { fn default_transaction_info_should_work() { let wasm_keeper: Box> = Box::new(MyWasmKeeper::new(NO_MESSAGE, NO_MESSAGE, NO_MESSAGE)); - assert_eq!(0, wasm_keeper.transaction_info().index); + assert_eq!(None, wasm_keeper.transaction_info()); wasm_keeper.inc_transaction_index(); - assert_eq!(0, wasm_keeper.transaction_info().index); - wasm_keeper.set_transaction_info(TransactionInfo { index: 15 }); - assert_eq!(0, wasm_keeper.transaction_info().index); + assert_eq!(None, wasm_keeper.transaction_info()); + wasm_keeper.set_transaction_info(Some(TransactionInfo { index: 15 })); + assert_eq!(None, wasm_keeper.transaction_info()); } diff --git a/tests/test_transaction_info/test_transaction_index.rs b/tests/test_transaction_info/test_transaction_index.rs index d9930a11..a7c07c57 100644 --- a/tests/test_transaction_info/test_transaction_index.rs +++ b/tests/test_transaction_info/test_transaction_index.rs @@ -3,9 +3,9 @@ use cosmwasm_std::{BlockInfo, Empty, Timestamp}; use cw_multi_test::{App, Executor}; #[test] -fn default_transaction_index_should_be_zero() { +fn default_transaction_index_should_be_none() { let app = App::default(); - assert_eq!(0, app.transaction_info().index); + assert_eq!(None, app.transaction_info()); } #[test] @@ -13,9 +13,10 @@ fn instantiate_should_increment_transaction_index() { let mut app = App::default(); let sender_addr = app.api().addr_make("sender"); let code_id = app.store_code(counter::contract()); + assert_eq!(None, app.transaction_info()); app.instantiate_contract(code_id, sender_addr, &Empty {}, &[], "counter", None) .unwrap(); - assert_eq!(1, app.transaction_info().index); + assert_eq!(0, app.transaction_info().unwrap().index); } #[test] @@ -23,6 +24,7 @@ fn execute_should_increment_transaction_index() { let mut app = App::default(); let sender_addr = app.api().addr_make("sender"); let code_id = app.store_code(counter::contract()); + assert_eq!(None, app.transaction_info()); let contract_addr = app .instantiate_contract( code_id, @@ -33,10 +35,10 @@ fn execute_should_increment_transaction_index() { None, ) .unwrap(); - assert_eq!(1, app.transaction_info().index); + assert_eq!(0, app.transaction_info().unwrap().index); app.execute_contract(sender_addr, contract_addr, &Empty {}, &[]) .unwrap(); - assert_eq!(2, app.transaction_info().index); + assert_eq!(1, app.transaction_info().unwrap().index); } #[test] @@ -44,6 +46,7 @@ fn setting_block_should_reset_transaction_index() { let mut app = App::default(); let sender_addr = app.api().addr_make("sender"); let code_id = app.store_code(counter::contract()); + assert_eq!(None, app.transaction_info()); let contract_addr = app .instantiate_contract( code_id, @@ -54,10 +57,10 @@ fn setting_block_should_reset_transaction_index() { None, ) .unwrap(); - assert_eq!(1, app.transaction_info().index); + assert_eq!(0, app.transaction_info().unwrap().index); app.execute_contract(sender_addr.clone(), contract_addr.clone(), &Empty {}, &[]) .unwrap(); - assert_eq!(2, app.transaction_info().index); + assert_eq!(1, app.transaction_info().unwrap().index); // prepare custom block properties let block = BlockInfo { height: 20, @@ -65,9 +68,10 @@ fn setting_block_should_reset_transaction_index() { chain_id: "my-testnet".to_string(), }; app.set_block(block); + assert_eq!(None, app.transaction_info()); app.execute_contract(sender_addr, contract_addr, &Empty {}, &[]) .unwrap(); - assert_eq!(1, app.transaction_info().index); + assert_eq!(0, app.transaction_info().unwrap().index); } #[test] @@ -75,6 +79,7 @@ fn updating_block_should_reset_transaction_index() { let mut app = App::default(); let sender_addr = app.api().addr_make("sender"); let code_id = app.store_code(counter::contract()); + assert_eq!(None, app.transaction_info()); let contract_addr = app .instantiate_contract( code_id, @@ -85,15 +90,16 @@ fn updating_block_should_reset_transaction_index() { None, ) .unwrap(); - assert_eq!(1, app.transaction_info().index); + assert_eq!(0, app.transaction_info().unwrap().index); app.execute_contract(sender_addr.clone(), contract_addr.clone(), &Empty {}, &[]) .unwrap(); - assert_eq!(2, app.transaction_info().index); + assert_eq!(1, app.transaction_info().unwrap().index); app.update_block(|block| { block.height += 1; block.time = block.time.plus_seconds(60); }); + assert_eq!(None, app.transaction_info()); app.execute_contract(sender_addr, contract_addr, &Empty {}, &[]) .unwrap(); - assert_eq!(1, app.transaction_info().index); + assert_eq!(0, app.transaction_info().unwrap().index); } From 7805e1203249aa52f5840389ef3fbd1f129ce4cd Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Thu, 4 Apr 2024 13:47:46 +0200 Subject: [PATCH 12/12] Removed CLA.md --- CLA.md | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 CLA.md diff --git a/CLA.md b/CLA.md deleted file mode 100644 index 17a701ee..00000000 --- a/CLA.md +++ /dev/null @@ -1,23 +0,0 @@ -## Contributor License Agreement -The following terms are used throughout this agreement: - -* You - the person or legal entity including its affiliates asked to accept this agreement. An affiliate is any entity that controls or is controlled by the legal entity, or is under common control with it. -* Project - is an umbrella term that refers to any and all Confio OÜ open source projects. -* Contribution - any type of work that is submitted to a Project, including any modifications or additions to existing work. -* Submitted - conveyed to a Project via a pull request, commit, issue, or any form of electronic, written, or verbal communication with Confio OÜ, contributors or maintainers. - -## 1. Grant of Copyright License. - -Subject to the terms and conditions of this agreement, You grant to the Projects’ maintainers, contributors, users and to Confio OÜ a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute Your contributions and such derivative works. Except for this license, You reserve all rights, title, and interest in your contributions. - -## 2. Grant of Patent License. - -Subject to the terms and conditions of this agreement, You grant to the Projects’ maintainers, contributors, users and to Confio OÜ a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer your contributions, where such license applies only to those patent claims licensable by you that are necessarily infringed by your contribution or by combination of your contribution with the project to which this contribution was submitted. - -If any entity institutes patent litigation - including cross-claim or counterclaim in a lawsuit - against You alleging that your contribution or any project it was submitted to constitutes or is responsible for direct or contributory patent infringement, then any patent licenses granted to that entity under this agreement shall terminate as of the date such litigation is filed. - -## 3. Source of Contribution. - -Your contribution is either your original creation, based upon previous work that, to the best of your knowledge, is covered under an appropriate open source license and you have the right under that license to submit that work with modifications, whether created in whole or in part by you, or you have clearly identified the source of the contribution and any license or other restriction (like related patents, trademarks, and license agreements) of which you are personally aware. - -_Based in [GitHub's CLA](https://cla.github.com/agreement)__ \ No newline at end of file