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

Increment transaction index #158

Closed
wants to merge 13 commits into from
23 changes: 0 additions & 23 deletions CLA.md

This file was deleted.

18 changes: 9 additions & 9 deletions Cargo.lock

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

41 changes: 28 additions & 13 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -410,6 +410,7 @@ where
.staking
.process_queue(&self.api, &mut self.storage, &self.router, &self.block)
.unwrap();
self.router.wasm.set_transaction_info(None);
self.block = block;
}

Expand All @@ -419,14 +420,20 @@ where
.staking
.process_queue(&self.api, &mut self.storage, &self.router, &self.block)
.unwrap();
self.router.wasm.set_transaction_info(None);
action(&mut self.block);
}

/// 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) -> Option<TransactionInfo> {
self.router.wasm.transaction_info()
}

/// 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<CustomT::QueryT> {
Expand All @@ -452,11 +459,15 @@ where
storage,
} = self;

transactional(&mut *storage, |write_cache, _| {
msgs.into_iter()
.map(|msg| router.execute(&*api, write_cache, block, sender.clone(), msg))
.collect()
})
transactional(
&mut *storage,
|write_cache, _| {
msgs.into_iter()
.map(|msg| router.execute(&*api, write_cache, block, sender.clone(), msg))
.collect()
},
|| router.wasm.inc_transaction_index(),
)
}

/// Call a smart contract in "sudo" mode.
Expand All @@ -479,9 +490,11 @@ where
storage,
} = self;

transactional(&mut *storage, |write_cache, _| {
router.wasm.sudo(&*api, write_cache, router, block, msg)
})
transactional(
&mut *storage,
|write_cache, _| router.wasm.sudo(&*api, write_cache, router, block, msg),
|| router.wasm.inc_transaction_index(),
)
}

/// Runs arbitrary SudoMsg.
Expand All @@ -498,9 +511,11 @@ where
storage,
} = self;

transactional(&mut *storage, |write_cache, _| {
router.sudo(&*api, write_cache, block, msg)
})
transactional(
&mut *storage,
|write_cache, _| router.sudo(&*api, write_cache, block, msg),
|| router.wasm.inc_transaction_index(),
)
}
}
/// The Router plays a critical role in managing and directing
Expand Down
8 changes: 7 additions & 1 deletion src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -530,6 +530,12 @@ where
self
}

/// Overwrites the initial transaction.
pub fn with_transaction(self, transaction: Option<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.
Expand Down
34 changes: 19 additions & 15 deletions src/tests/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.inc_transaction_index(),
)
.unwrap();

// apply first to router
Expand Down
6 changes: 4 additions & 2 deletions src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ use std::iter;
use std::iter::Peekable;
use std::ops::{Bound, RangeBounds};

/// The BTreeMap specific key-value pair reference type, as returned by `BTreeMap<Vec<u8>, T>::range`.
/// The [BTreeMap] specific key-value pair reference type, as returned by `BTreeMap<Vec<u8>, T>::range`.
/// This is internal as it can change any time if the map implementation is swapped out.
type BTreeMapPairRef<'a, T = Vec<u8>> = (&'a Vec<u8>, &'a T);

pub fn transactional<F, T>(base: &mut dyn Storage, action: F) -> AnyResult<T>
pub fn transactional<F, G, T>(base: &mut dyn Storage, action: F, increment: G) -> AnyResult<T>
where
F: FnOnce(&mut dyn Storage, &dyn Storage) -> AnyResult<T>,
G: FnOnce(),
{
let mut cache = StorageTransaction::new(base);
let res = action(&mut cache, base)?;
cache.prepare().commit(base);
increment();
Ok(res)
}

Expand Down
Loading