Skip to content

Commit

Permalink
Merge pull request #1232 from CosmWasm/SubMsgResult
Browse files Browse the repository at this point in the history
Create dedicated SubMsgResult
  • Loading branch information
webmaster128 authored Mar 1, 2022
2 parents 84ecdd3 + e6c14b2 commit 13a04e2
Show file tree
Hide file tree
Showing 16 changed files with 294 additions and 30 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ and this project adheres to

## [Unreleased]

### Changed

- cosmwasm-std: Change type of `Reply::result` from `ContractResult` to the new
`SubMsgResult`. Both types are equal when serialized but `ContractResult` is
documented to be the result of a contract execution, which is not the case
here. ([#1232])

[#1232]: https://github.com/CosmWasm/cosmwasm/pull/1232

## [1.0.0-beta5] - 2022-02-08

### Changed
Expand Down
18 changes: 18 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ major releases of `cosmwasm`. Note that you can also view the
deps.storage.set(b"foo", b"bar");
```

- Replace `ContractResult` with `SubMsgResult` in `Reply` handling:

```diff
@@ -35,10 +35,10 @@ pub fn instantiate(
#[entry_point]
pub fn reply(deps: DepsMut, _env: Env, reply: Reply) -> StdResult<Response> {
match (reply.id, reply.result) {
- (RECEIVE_DISPATCH_ID, ContractResult::Err(err)) => {
+ (RECEIVE_DISPATCH_ID, SubMsgResult::Err(err)) => {
Ok(Response::new().set_data(encode_ibc_error(err)))
}
- (INIT_CALLBACK_ID, ContractResult::Ok(response)) => handle_init_callback(deps, response),
+ (INIT_CALLBACK_ID, SubMsgResult::Ok(response)) => handle_init_callback(deps, response),
_ => Err(StdError::generic_err("invalid reply id or result")),
}
}
```

## 0.16 -> 1.0.0-beta

- Update CosmWasm dependencies in Cargo.toml (skip the ones you don't use):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "AcknowledgementMsgBalances",
"description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult<Response> = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"messages\":[],\"attributes\":[],\"events\":[],\"data\":null}}\"#.to_vec()); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult<Response> = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#.to_vec()); ```",
"description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult<Response> = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"messages\":[],\"attributes\":[],\"events\":[],\"data\":null}}\"#); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult<Response> = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#); ```",
"oneOf": [
{
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "AcknowledgementMsgDispatch",
"description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult<Response> = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"messages\":[],\"attributes\":[],\"events\":[],\"data\":null}}\"#.to_vec()); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult<Response> = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#.to_vec()); ```",
"description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult<Response> = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"messages\":[],\"attributes\":[],\"events\":[],\"data\":null}}\"#); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult<Response> = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#); ```",
"oneOf": [
{
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "AcknowledgementMsgWhoAmI",
"description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult<Response> = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"messages\":[],\"attributes\":[],\"events\":[],\"data\":null}}\"#.to_vec()); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult<Response> = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#.to_vec()); ```",
"description": "This is the final result type that is created and serialized in a contract for every init/execute/migrate call. The VM then deserializes this type to distinguish between successful and failed executions.\n\nWe use a custom type here instead of Rust's Result because we want to be able to define the serialization, which is a public interface. Every language that compiles to Wasm and runs in the ComsWasm VM needs to create the same JSON representation.\n\n# Examples\n\nSuccess:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let response: Response = Response::default(); let result: ContractResult<Response> = ContractResult::Ok(response); assert_eq!(to_vec(&result).unwrap(), br#\"{\"ok\":{\"messages\":[],\"attributes\":[],\"events\":[],\"data\":null}}\"#); ```\n\nFailure:\n\n``` # use cosmwasm_std::{to_vec, ContractResult, Response}; let error_msg = String::from(\"Something went wrong\"); let result: ContractResult<Response> = ContractResult::Err(error_msg); assert_eq!(to_vec(&result).unwrap(), br#\"{\"error\":\"Something went wrong\"}\"#); ```",
"oneOf": [
{
"type": "object",
Expand Down
14 changes: 7 additions & 7 deletions contracts/ibc-reflect/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use cosmwasm_std::{
entry_point, from_slice, to_binary, wasm_execute, BankMsg, Binary, ContractResult, CosmosMsg,
Deps, DepsMut, Empty, Env, Event, IbcBasicResponse, IbcChannelCloseMsg, IbcChannelConnectMsg,
entry_point, from_slice, to_binary, wasm_execute, BankMsg, Binary, CosmosMsg, Deps, DepsMut,
Empty, Env, Event, IbcBasicResponse, IbcChannelCloseMsg, IbcChannelConnectMsg,
IbcChannelOpenMsg, IbcOrder, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg,
IbcReceiveResponse, MessageInfo, Order, QueryResponse, Reply, Response, StdError, StdResult,
SubMsg, SubMsgExecutionResponse, WasmMsg,
SubMsg, SubMsgExecutionResponse, SubMsgResult, WasmMsg,
};

use crate::msg::{
Expand Down Expand Up @@ -35,10 +35,10 @@ pub fn instantiate(
#[entry_point]
pub fn reply(deps: DepsMut, _env: Env, reply: Reply) -> StdResult<Response> {
match (reply.id, reply.result) {
(RECEIVE_DISPATCH_ID, ContractResult::Err(err)) => {
(RECEIVE_DISPATCH_ID, SubMsgResult::Err(err)) => {
Ok(Response::new().set_data(encode_ibc_error(err)))
}
(INIT_CALLBACK_ID, ContractResult::Ok(response)) => handle_init_callback(deps, response),
(INIT_CALLBACK_ID, SubMsgResult::Ok(response)) => handle_init_callback(deps, response),
_ => Err(StdError::generic_err("invalid reply id or result")),
}
}
Expand Down Expand Up @@ -387,7 +387,7 @@ mod tests {
// fake a reply and ensure this works
let response = Reply {
id,
result: ContractResult::Ok(SubMsgExecutionResponse {
result: SubMsgResult::Ok(SubMsgExecutionResponse {
events: fake_events(&account),
data: None,
}),
Expand Down Expand Up @@ -462,7 +462,7 @@ mod tests {
// fake a reply and ensure this works
let response = Reply {
id,
result: ContractResult::Ok(SubMsgExecutionResponse {
result: SubMsgResult::Ok(SubMsgExecutionResponse {
events: fake_events(REFLECT_ADDR),
data: None,
}),
Expand Down
6 changes: 3 additions & 3 deletions contracts/ibc-reflect/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use cosmwasm_std::testing::{
};
use cosmwasm_std::{
attr, coins, BankMsg, ContractResult, CosmosMsg, Event, IbcBasicResponse, IbcOrder,
IbcReceiveResponse, Reply, Response, SubMsgExecutionResponse, WasmMsg,
IbcReceiveResponse, Reply, Response, SubMsgExecutionResponse, SubMsgResult, WasmMsg,
};
use cosmwasm_vm::testing::{
ibc_channel_connect, ibc_channel_open, ibc_packet_receive, instantiate, mock_env, mock_info,
Expand Down Expand Up @@ -95,7 +95,7 @@ fn connect(
// fake a reply and ensure this works
let response = Reply {
id,
result: ContractResult::Ok(SubMsgExecutionResponse {
result: SubMsgResult::Ok(SubMsgExecutionResponse {
events: fake_events(&account),
data: None,
}),
Expand Down Expand Up @@ -171,7 +171,7 @@ fn proper_handshake_flow() {
// we get the callback from reflect
let response = Reply {
id,
result: ContractResult::Ok(SubMsgExecutionResponse {
result: SubMsgResult::Ok(SubMsgExecutionResponse {
events: fake_events(REFLECT_ADDR),
data: None,
}),
Expand Down
6 changes: 3 additions & 3 deletions contracts/reflect/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ mod tests {
use crate::testing::mock_dependencies_with_custom_querier;
use cosmwasm_std::testing::{mock_env, mock_info, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{
coin, coins, from_binary, AllBalanceResponse, BankMsg, BankQuery, Binary, ContractResult,
Event, StakingMsg, StdError, SubMsgExecutionResponse,
coin, coins, from_binary, AllBalanceResponse, BankMsg, BankQuery, Binary, Event,
StakingMsg, StdError, SubMsgExecutionResponse, SubMsgResult,
};

#[test]
Expand Down Expand Up @@ -435,7 +435,7 @@ mod tests {
let id = 123u64;
let data = Binary::from(b"foobar");
let events = vec![Event::new("message").add_attribute("signer", "caller-addr")];
let result = ContractResult::Ok(SubMsgExecutionResponse {
let result = SubMsgResult::Ok(SubMsgExecutionResponse {
events: events.clone(),
data: Some(data.clone()),
});
Expand Down
4 changes: 2 additions & 2 deletions contracts/reflect/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use cosmwasm_std::{
coin, coins, from_binary, BankMsg, Binary, Coin, ContractResult, Event, Reply, Response,
StakingMsg, SubMsg, SubMsgExecutionResponse, SystemResult,
StakingMsg, SubMsg, SubMsgExecutionResponse, SubMsgResult, SystemResult,
};
use cosmwasm_vm::{
testing::{
Expand Down Expand Up @@ -226,7 +226,7 @@ fn reply_and_query() {
let id = 123u64;
let data = Binary::from(b"foobar");
let events = vec![Event::new("message").add_attribute("signer", "caller-addr")];
let result = ContractResult::Ok(SubMsgExecutionResponse {
let result = SubMsgResult::Ok(SubMsgExecutionResponse {
events: events.clone(),
data: Some(data.clone()),
});
Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub use crate::query::{ChannelResponse, IbcQuery, ListChannelsResponse, PortIdRe
pub use crate::results::{
attr, wasm_execute, wasm_instantiate, Attribute, BankMsg, ContractResult, CosmosMsg, CustomMsg,
Empty, Event, QueryResponse, Reply, ReplyOn, Response, SubMsg, SubMsgExecutionResponse,
SystemResult, WasmMsg,
SubMsgResult, SystemResult, WasmMsg,
};
#[cfg(feature = "staking")]
pub use crate::results::{DistributionMsg, StakingMsg};
Expand Down
6 changes: 3 additions & 3 deletions packages/std/src/results/contract_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::fmt;
/// # use cosmwasm_std::{to_vec, ContractResult, Response};
/// let response: Response = Response::default();
/// let result: ContractResult<Response> = ContractResult::Ok(response);
/// assert_eq!(to_vec(&result).unwrap(), br#"{"ok":{"messages":[],"attributes":[],"events":[],"data":null}}"#.to_vec());
/// assert_eq!(to_vec(&result).unwrap(), br#"{"ok":{"messages":[],"attributes":[],"events":[],"data":null}}"#);
/// ```
///
/// Failure:
Expand All @@ -27,7 +27,7 @@ use std::fmt;
/// # use cosmwasm_std::{to_vec, ContractResult, Response};
/// let error_msg = String::from("Something went wrong");
/// let result: ContractResult<Response> = ContractResult::Err(error_msg);
/// assert_eq!(to_vec(&result).unwrap(), br#"{"error":"Something went wrong"}"#.to_vec());
/// assert_eq!(to_vec(&result).unwrap(), br#"{"error":"Something went wrong"}"#);
/// ```
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -101,7 +101,7 @@ mod tests {
let result: ContractResult<Response> = ContractResult::Ok(Response::default());
assert_eq!(
to_vec(&result).unwrap(),
br#"{"ok":{"messages":[],"attributes":[],"events":[],"data":null}}"#.to_vec()
br#"{"ok":{"messages":[],"attributes":[],"events":[],"data":null}}"#
);

let result: ContractResult<Response> = ContractResult::Err("broken".to_string());
Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/results/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ pub use empty::Empty;
pub use events::{attr, Attribute, Event};
pub use query::QueryResponse;
pub use response::Response;
pub use submessages::{Reply, ReplyOn, SubMsg, SubMsgExecutionResponse};
pub use submessages::{Reply, ReplyOn, SubMsg, SubMsgExecutionResponse, SubMsgResult};
pub use system_result::SystemResult;
18 changes: 17 additions & 1 deletion packages/std/src/results/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ mod tests {
use super::super::BankMsg;
use super::*;
use crate::results::submessages::{ReplyOn, UNUSED_MSG_ID};
use crate::{coins, from_slice, to_vec};
use crate::{coins, from_slice, to_vec, ContractResult};

#[test]
fn can_serialize_and_deserialize_init_response() {
Expand Down Expand Up @@ -248,4 +248,20 @@ mod tests {
let deserialized: Response = from_slice(&serialized).expect("decode contract result");
assert_eq!(deserialized, original);
}

#[test]
fn contract_result_is_ok_works() {
let success = ContractResult::<()>::Ok(());
let failure = ContractResult::<()>::Err("broken".to_string());
assert!(success.is_ok());
assert!(!failure.is_ok());
}

#[test]
fn contract_result_is_err_works() {
let success = ContractResult::<()>::Ok(());
let failure = ContractResult::<()>::Err("broken".to_string());
assert!(failure.is_err());
assert!(!success.is_err());
}
}
Loading

0 comments on commit 13a04e2

Please sign in to comment.