Skip to content

Commit e925246

Browse files
committed
Update mock_env and MOCK_CONTRACT_ADDR in cosmwasm-vm
1 parent 9284e36 commit e925246

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ and this project adheres to
3232
- cosmwasm-std: Let `mock_env` return a contract address that is valid bech32
3333
and uses the same bech32 prefix as `MockApi`; Change `MOCK_CONTRACT_ADDR`
3434
value to match the contract address from `mock_env`. ([#2211])
35+
- cosmwasm-vm: Let `mock_env` return a contract address that is valid bech32 and
36+
uses the same bech32 prefix as `MockApi`; Change `MOCK_CONTRACT_ADDR` value to
37+
match the contract address from `mock_env`. ([#2211])
3538

3639
[#2118]: https://github.com/CosmWasm/cosmwasm/pull/2118
3740
[#2211]: https://github.com/CosmWasm/cosmwasm/issues/2211

packages/vm/src/instance.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ mod tests {
914914

915915
let report2 = instance.create_gas_report();
916916
assert_eq!(report2.used_externally, 251);
917-
assert_eq!(report2.used_internally, 16995280);
917+
assert_eq!(report2.used_internally, 17457465);
918918
assert_eq!(report2.limit, LIMIT);
919919
assert_eq!(
920920
report2.remaining,
@@ -1105,7 +1105,7 @@ mod tests {
11051105
.unwrap();
11061106

11071107
let init_used = orig_gas - instance.get_gas_left();
1108-
assert_eq!(init_used, 16995531);
1108+
assert_eq!(init_used, 17457716);
11091109
}
11101110

11111111
#[test]
@@ -1130,7 +1130,7 @@ mod tests {
11301130
.unwrap();
11311131

11321132
let execute_used = gas_before_execute - instance.get_gas_left();
1133-
assert_eq!(execute_used, 19589666);
1133+
assert_eq!(execute_used, 21041196);
11341134
}
11351135

11361136
#[test]
@@ -1173,6 +1173,6 @@ mod tests {
11731173
);
11741174

11751175
let query_used = gas_before_query - instance.get_gas_left();
1176-
assert_eq!(query_used, 11942871);
1176+
assert_eq!(query_used, 12631261);
11771177
}
11781178
}

packages/vm/src/testing/mock.rs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use super::storage::MockStorage;
1010
use crate::backend::unwrap_or_return_with_gas;
1111
use crate::{Backend, BackendApi, BackendError, BackendResult, GasInfo};
1212

13-
pub const MOCK_CONTRACT_ADDR: &str = "cosmwasmcontract"; // TODO: use correct address
13+
pub const MOCK_CONTRACT_ADDR: &str =
14+
"cosmwasm1jpev2csrppg792t22rn8z8uew8h3sjcpglcd0qv9g8gj8ky922tscp8avs";
15+
1416
/// Default gas multiplier in wasmd.
1517
/// See https://github.com/CosmWasm/wasmd/blob/v0.51.0/x/wasm/types/gas_register.go#L34
1618
const WASMD_GAS_MULTIPLIER: u64 = 140_000;
@@ -216,12 +218,62 @@ fn validate_length(bytes: &[u8]) -> Result<(), BackendError> {
216218
}
217219
}
218220

219-
/// Returns a default environment with height, time, chain_id, and contract address
221+
/// Returns a default environment with height, time, chain_id, and contract address.
220222
/// You can submit as is to most contracts, or modify height/time if you want to
221223
/// test for expiration.
222224
///
223225
/// This is intended for use in test code only.
226+
///
227+
/// The contract address uses the same bech32 prefix as [`MockApi`](crate::testing::MockApi). While
228+
/// this is good for the majority of users, you might need to create your `Env`s
229+
/// differently if you need a valid address using a different prefix.
230+
///
231+
/// ## Examples
232+
///
233+
/// Create an env:
234+
///
235+
/// ```
236+
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, Timestamp, TransactionInfo};
237+
/// use cosmwasm_vm::testing::mock_env;
238+
///
239+
/// let env = mock_env();
240+
/// assert_eq!(env, Env {
241+
/// block: BlockInfo {
242+
/// height: 12_345,
243+
/// time: Timestamp::from_nanos(1_571_797_419_879_305_533),
244+
/// chain_id: "cosmos-testnet-14002".to_string(),
245+
/// },
246+
/// transaction: Some(TransactionInfo { index: 3 }),
247+
/// contract: ContractInfo {
248+
/// address: Addr::unchecked("cosmwasm1jpev2csrppg792t22rn8z8uew8h3sjcpglcd0qv9g8gj8ky922tscp8avs"),
249+
/// },
250+
/// });
251+
/// ```
252+
///
253+
/// Mutate and reuse environment:
254+
///
255+
/// ```
256+
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, Timestamp, TransactionInfo};
257+
/// use cosmwasm_vm::testing::mock_env;
258+
///
259+
/// let env1 = mock_env();
260+
///
261+
/// // First test with `env1`
262+
///
263+
/// let mut env2 = env1.clone();
264+
/// env2.block.height += 1;
265+
/// env2.block.time = env1.block.time.plus_seconds(6);
266+
///
267+
/// // `env2` is one block and 6 seconds later
268+
///
269+
/// let mut env3 = env2.clone();
270+
/// env3.block.height += 1;
271+
/// env3.block.time = env2.block.time.plus_nanos(5_500_000_000);
272+
///
273+
/// // `env3` is one block and 5.5 seconds later
274+
/// ```
224275
pub fn mock_env() -> Env {
276+
let contract_addr = MockApi::default().addr_make("cosmos2contract");
225277
Env {
226278
block: BlockInfo {
227279
height: 12_345,
@@ -230,7 +282,7 @@ pub fn mock_env() -> Env {
230282
},
231283
transaction: Some(TransactionInfo { index: 3 }),
232284
contract: ContractInfo {
233-
address: Addr::unchecked(MOCK_CONTRACT_ADDR),
285+
address: Addr::unchecked(contract_addr),
234286
},
235287
}
236288
}
@@ -249,6 +301,12 @@ mod tests {
249301
use super::*;
250302
use cosmwasm_std::coins;
251303

304+
#[test]
305+
fn mock_env_matches_mock_contract_addr() {
306+
let contract_address = mock_env().contract.address;
307+
assert_eq!(contract_address, Addr::unchecked(MOCK_CONTRACT_ADDR));
308+
}
309+
252310
#[test]
253311
fn mock_info_works() {
254312
let info = mock_info("my name", &coins(100, "atom"));

0 commit comments

Comments
 (0)