Repository: CosmWasm/cosmwasm (rust)

Issue:
Use new _contract_address in contracts
I got this error from `ibc-reflect` when I updated wasmd:

```
                                failed to execute message; message index: 0: channel open confirm callback failed: submessages: reply: Generic error: No contract_address found in callback events: execute wasm contract failed
```

We search for that event in reply to determine what the address of the contract is that we just created.
While this *can* be replaced by using prost and parsing the data field (this is demo'd in multitest), we can simply change the string it searches for, and then have the debate about pulling in more dependencies as a separate issue.

This currently is causing a failing test in https://github.com/CosmWasm/wasmd/pull/559

Pull request:
Use _contract_address rather than contract_address
Closes #1022 

Updates parse code and tests from ibc-reflect to match new format from wasmd main (0.18 pre-release)

Patch:
diff --git a/contracts/ibc-reflect/src/contract.rs b/contracts/ibc-reflect/src/contract.rs
index 86fc498ec..d4ef8d8e8 100644
--- a/contracts/ibc-reflect/src/contract.rs
+++ b/contracts/ibc-reflect/src/contract.rs
@@ -55,7 +55,7 @@ fn parse_contract_from_event(events: Vec<Event>) -> Option<String> {
         .and_then(|ev| {
             ev.attributes
                 .into_iter()
-                .find(|a| a.key == "contract_address")
+                .find(|a| a.key == "_contract_address")
         })
         .map(|a| a.value)
 }
@@ -72,7 +72,7 @@ pub fn handle_init_callback(
     let contract_addr = match parse_contract_from_event(response.events) {
         Some(addr) => deps.api.addr_validate(&addr),
         None => Err(StdError::generic_err(
-            "No contract_address found in callback events",
+            "No _contract_address found in callback events",
         )),
     }?;
 
@@ -363,7 +363,7 @@ mod tests {
     use cosmwasm_std::testing::{
         mock_dependencies, mock_env, mock_ibc_channel_close_init, mock_ibc_channel_connect_ack,
         mock_ibc_channel_open_init, mock_ibc_channel_open_try, mock_ibc_packet_recv, mock_info,
-        MockApi, MockQuerier, MockStorage, MOCK_CONTRACT_ADDR,
+        mock_wasmd_attr, MockApi, MockQuerier, MockStorage, MOCK_CONTRACT_ADDR,
     };
     use cosmwasm_std::{coin, coins, from_slice, BankMsg, OwnedDeps, WasmMsg};
 
@@ -391,7 +391,8 @@ mod tests {
                 attr("module", "wasm"),
                 attr("signer", MOCK_CONTRACT_ADDR),
                 attr("code_id", "17"),
-                attr("contract_address", reflect_addr),
+                // We have to force this one to avoid the debug assertion against _
+                mock_wasmd_attr("_contract_address", reflect_addr),
             ],
         };
         vec![event]
diff --git a/packages/std/src/lib.rs b/packages/std/src/lib.rs
index d62a9d45c..0acc5a027 100644
--- a/packages/std/src/lib.rs
+++ b/packages/std/src/lib.rs
@@ -101,8 +101,8 @@ pub mod testing {
     pub use crate::mock::StakingQuerier;
     pub use crate::mock::{
         digit_sum, mock_dependencies, mock_dependencies_with_balances, mock_env, mock_info,
-        riffle_shuffle, BankQuerier, MockApi, MockQuerier, MockQuerierCustomHandlerResult,
-        MockStorage, MOCK_CONTRACT_ADDR,
+        mock_wasmd_attr, riffle_shuffle, BankQuerier, MockApi, MockQuerier,
+        MockQuerierCustomHandlerResult, MockStorage, MOCK_CONTRACT_ADDR,
     };
     #[cfg(feature = "stargate")]
     pub use crate::mock::{
diff --git a/packages/std/src/mock.rs b/packages/std/src/mock.rs
index 6452dab01..7a574dd20 100644
--- a/packages/std/src/mock.rs
+++ b/packages/std/src/mock.rs
@@ -28,6 +28,7 @@ use crate::storage::MemoryStorage;
 use crate::timestamp::Timestamp;
 use crate::traits::{Api, Querier, QuerierResult};
 use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo};
+use crate::Attribute;
 
 pub const MOCK_CONTRACT_ADDR: &str = "cosmos2contract";
 
@@ -672,6 +673,15 @@ pub fn digit_sum(input: &[u8]) -> usize {
     input.iter().fold(0, |sum, val| sum + (*val as usize))
 }
 
+/// Only for test code. This bypasses assertions in new, allowing us to create _*
+/// Attributes to simulate responses from the blockchain
+pub fn mock_wasmd_attr(key: impl Into<String>, value: impl Into<String>) -> Attribute {
+    Attribute {
+        key: key.into(),
+        value: value.into(),
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
diff --git a/packages/vm/README.md b/packages/vm/README.md
index 6a380e73f..b987806b3 100644
--- a/packages/vm/README.md
+++ b/packages/vm/README.md
@@ -53,13 +53,13 @@ docker run --rm -v "$(pwd)":/code \
   --mount type=volume,source="devcontract_cache_hackatom",target=/code/contracts/hackatom/target \
   --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
   cosmwasm/rust-optimizer:0.11.4 ./contracts/hackatom \
-  && cp artifacts/hackatom.wasm packages/vm/testdata/hackatom_0.15.wasm
+  && cp artifacts/hackatom.wasm packages/vm/testdata/hackatom_0.16.wasm
 
 docker run --rm -v "$(pwd)":/code \
   --mount type=volume,source="devcontract_cache_ibc_reflect",target=/code/contracts/ibc-reflect/target \
   --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
   cosmwasm/rust-optimizer:0.11.4 ./contracts/ibc-reflect \
-  && cp artifacts/ibc_reflect.wasm packages/vm/testdata/ibc_reflect_0.15.wasm
+  && cp artifacts/ibc_reflect.wasm packages/vm/testdata/ibc_reflect_0.16.wasm
 
 docker run --rm -v "$(pwd)":/code \
   --mount type=volume,source="devcontract_cache_floaty",target=/code/contracts/floaty/target \
diff --git a/packages/vm/src/calls.rs b/packages/vm/src/calls.rs
index cb1bf58cd..b37abe407 100644
--- a/packages/vm/src/calls.rs
+++ b/packages/vm/src/calls.rs
@@ -680,11 +680,10 @@ mod tests {
         };
         use cosmwasm_std::testing::{
             mock_ibc_channel_close_init, mock_ibc_channel_connect_ack, mock_ibc_channel_open_init,
-            mock_ibc_packet_ack, mock_ibc_packet_recv, mock_ibc_packet_timeout,
+            mock_ibc_packet_ack, mock_ibc_packet_recv, mock_ibc_packet_timeout, mock_wasmd_attr,
         };
         use cosmwasm_std::{
-            attr, Empty, Event, IbcAcknowledgement, IbcOrder, Reply, ReplyOn,
-            SubMsgExecutionResponse,
+            Empty, Event, IbcAcknowledgement, IbcOrder, Reply, ReplyOn, SubMsgExecutionResponse,
         };
         static CONTRACT: &[u8] = include_bytes!("../testdata/ibc_reflect.wasm");
         const IBC_VERSION: &str = "ibc-reflect-v1";
@@ -722,7 +721,10 @@ mod tests {
             let id = res.messages[0].id;
             let event = Event {
                 ty: "message".into(),
-                attributes: vec![attr("contract_address", account)],
+                attributes: vec![
+                    // We have to force this one to avoid the debug assertion against _
+                    mock_wasmd_attr("_contract_address", account),
+                ],
             };
             // which creates a reflect account. here we get the callback
             let response = Reply {
diff --git a/packages/vm/src/environment.rs b/packages/vm/src/environment.rs
index ccefc5f1b..b6882f2c5 100644
--- a/packages/vm/src/environment.rs
+++ b/packages/vm/src/environment.rs
@@ -718,6 +718,7 @@ mod tests {
                 function_name,
                 expected,
                 actual,
+                ..
             } => {
                 assert_eq!(function_name, "allocate");
                 assert_eq!(expected, 0);
@@ -752,6 +753,7 @@ mod tests {
                 function_name,
                 expected,
                 actual,
+                ..
             } => {
                 assert_eq!(function_name, "deallocate");
                 assert_eq!(expected, 1);


Test patch:
diff --git a/contracts/ibc-reflect/tests/integration.rs b/contracts/ibc-reflect/tests/integration.rs
index 6cb1cbc3e..2c0ce89e5 100644
--- a/contracts/ibc-reflect/tests/integration.rs
+++ b/contracts/ibc-reflect/tests/integration.rs
@@ -19,7 +19,7 @@
 
 use cosmwasm_std::testing::{
     mock_ibc_channel_connect_ack, mock_ibc_channel_open_init, mock_ibc_channel_open_try,
-    mock_ibc_packet_recv,
+    mock_ibc_packet_recv, mock_wasmd_attr,
 };
 use cosmwasm_std::{
     attr, coins, BankMsg, ContractResult, CosmosMsg, Event, IbcBasicResponse, IbcOrder,
@@ -66,7 +66,8 @@ fn fake_events(reflect_addr: &str) -> Vec<Event> {
             attr("module", "wasm"),
             attr("signer", MOCK_CONTRACT_ADDR),
             attr("code_id", "17"),
-            attr("contract_address", reflect_addr),
+            // We have to force this one to avoid the debug assertion against _
+            mock_wasmd_attr("_contract_address", reflect_addr),
         ],
     };
     vec![event]