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

Merge messages submessages #961

Merged
merged 25 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b7cdec3
Add helpers around SubMsg construction
ethanfrey Jun 16, 2021
1f07ad0
Remove submessages
ethanfrey Jun 16, 2021
578ad95
Fix up tests in std
ethanfrey Jun 16, 2021
7229b8c
Update burner and reflect contracts
ethanfrey Jun 16, 2021
241b08a
Remove submessages from Ibc Responses as well
ethanfrey Jun 16, 2021
30d2b3b
Update ibc-reflect contract
ethanfrey Jun 16, 2021
6c9cb1f
Replace subcall -> SubMsg::new
ethanfrey Jun 16, 2021
4863a24
Update Response.messages rustdoc
ethanfrey Jun 16, 2021
8642a4b
Update reflect schema
ethanfrey Jun 16, 2021
e61e759
Rebuild test contracts for vm package
ethanfrey Jun 16, 2021
a47488c
Fix gas prices in vm tests
ethanfrey Jun 16, 2021
fee113a
More vm fixes
ethanfrey Jun 16, 2021
606a41f
Cleaner SubMsg API
ethanfrey Jun 17, 2021
989d4b7
Update burner contract
ethanfrey Jun 17, 2021
2b590e6
Refine add_message / add_submessage helpers
ethanfrey Jun 17, 2021
6bf24d6
Update hackatom contract
ethanfrey Jun 17, 2021
3dbf7be
Update reflect contract
ethanfrey Jun 17, 2021
24a1668
Update ibc-reflect contract
ethanfrey Jun 17, 2021
950a3d2
Update remaining contracts
ethanfrey Jun 17, 2021
dc11fd9
[skip ci] Add CHANGELOG entry and make spellcheck happy
ethanfrey Jun 17, 2021
7813dc1
Cleanup from PR comments
ethanfrey Jun 17, 2021
b937b23
Fix broken test
ethanfrey Jun 17, 2021
98d0cb6
Update test contracts for vm
ethanfrey Jun 17, 2021
7a0cc95
[skip ci] Add migration instructions
ethanfrey Jun 17, 2021
096a9d1
Polish some text
webmaster128 Jun 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ and this project adheres to
in `cosmwasm-vm` itself instead of being imported from `cosmwasm-crypto`.
- cosmwasm-vm: Filesystem storage layout now distinguishes clearly between state
and cache.
- cosmwasm-std: Add enum case `ReplyOn::Never`; Remove default implementation of
`ReplyOn` as there is no natural default case anymore ([#961]).
- cosmwasm-std: Merge `messages` and `submessages` into one list, using
`ReplyOn::Never` to model the "fire and forget" semantics ([#961]).
- cosmwasm-std: Add `SubMsg` constructors: `::new()`, `::reply_on_error()`,
`::reply_on_success()`, `::reply_always()`; Add `with_gas_limit` to add a gas
limit to any those constructors ([#961]).

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

### Fixed

Expand Down
59 changes: 58 additions & 1 deletion MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,64 @@ This guide explains what is needed to upgrade contracts when migrating over
major releases of `cosmwasm`. Note that you can also view the
[complete CHANGELOG](./CHANGELOG.md) to understand the differences.

## 0.13 -> 0.14 (unreleased)
## 0.14 -> 0.15 (unreleased)

- Combine `messages` and `submessages` on the `Response` object. The new format
uses `messages: Vec<SubMsg<T>>`, so copy `submessages` content, and wrap old
messages using `SubMsg::new`. Here is how to change messages:

```rust
let send = BankMsg::Send { to_address, amount };

// before
let res = Response {
messages: vec![send.into()],
..Response::default()
}

// after
let res = Response {
messages: vec![SubMsg::new(send)],
..Response::default()
}

// alternate approach
let mut res = Response::new();
res.add_message(send);
```

And here is how to change submessages:

```rust
// before
let sub_msg = SubMsg {
id: INIT_CALLBACK_ID,
msg: msg.into(),
gas_limit: None,
reply_on: ReplyOn::Success,
};
let res = Response {
submessages: vec![sub_msg],
..Response::default()
};

// after
let msg = SubMsg::reply_on_success(msg, INIT_CALLBACK_ID);
let res = Response {
messages: vec![msg],
..Response::default()
};

// alternate approach
let msg = SubMsg::reply_on_success(msg, INIT_CALLBACK_ID);
let mut res = Response::new();
res.add_submessage(msg);
```

Note that this means you can mix "messages" and "submessages" in any execution
order. You are no more restricted to doing "submessages" first.

## 0.13 -> 0.14

- The minimum Rust supported version for 0.14 is 1.51.0. Verify your Rust
version is >= 1.51.0 with: `rustc --version`
Expand Down
9 changes: 4 additions & 5 deletions contracts/burner/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cosmwasm_std::{
attr, entry_point, BankMsg, DepsMut, Env, MessageInfo, Order, Response, StdError, StdResult,
SubMsg,
};

use crate::msg::{InstantiateMsg, MigrateMsg};
Expand Down Expand Up @@ -39,8 +40,7 @@ pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> StdResult<Response>
let data_msg = format!("burnt {} keys", count).into_bytes();

Ok(Response {
submessages: vec![],
messages: vec![send.into()],
messages: vec![SubMsg::new(send)],
attributes: vec![attr("action", "burn"), attr("payout", msg.payout)],
data: Some(data_msg.into()),
})
Expand Down Expand Up @@ -90,11 +90,10 @@ mod tests {
let msg = res.messages.get(0).expect("no message");
assert_eq!(
msg,
&BankMsg::Send {
&SubMsg::new(BankMsg::Send {
to_address: payout,
amount: coins(123456, "gold"),
}
.into(),
})
);

// check there is no data in storage
Expand Down
7 changes: 3 additions & 4 deletions contracts/burner/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! });
//! 4. Anywhere you see query(&deps, ...) you must replace it with query(&mut deps, ...)

use cosmwasm_std::{coins, BankMsg, ContractResult, Order, Response};
use cosmwasm_std::{coins, BankMsg, ContractResult, Order, Response, SubMsg};
use cosmwasm_vm::testing::{instantiate, migrate, mock_env, mock_info, mock_instance};

use burner::msg::{InstantiateMsg, MigrateMsg};
Expand Down Expand Up @@ -70,11 +70,10 @@ fn migrate_cleans_up_data() {
let msg = res.messages.get(0).expect("no message");
assert_eq!(
msg,
&BankMsg::Send {
&SubMsg::new(BankMsg::Send {
to_address: payout,
amount: coins(123456, "gold"),
}
.into(),
}),
);

// check there is no data in storage
Expand Down
9 changes: 4 additions & 5 deletions contracts/hackatom/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ mod tests {
mock_dependencies, mock_dependencies_with_balances, mock_env, mock_info, MOCK_CONTRACT_ADDR,
};
// import trait Storage to get access to read
use cosmwasm_std::{attr, coins, Binary, Storage};
use cosmwasm_std::{attr, coins, Binary, Storage, SubMsg};

#[test]
fn proper_initialization() {
Expand Down Expand Up @@ -394,7 +394,7 @@ mod tests {
let res = sudo(deps.as_mut(), mock_env(), sys_msg).unwrap();
assert_eq!(1, res.messages.len());
let msg = res.messages.get(0).expect("no message");
assert_eq!(msg, &BankMsg::Send { to_address, amount }.into(),);
assert_eq!(msg, &SubMsg::new(BankMsg::Send { to_address, amount }));
}

#[test]
Expand Down Expand Up @@ -446,11 +446,10 @@ mod tests {
let msg = execute_res.messages.get(0).expect("no message");
assert_eq!(
msg,
&BankMsg::Send {
&SubMsg::new(BankMsg::Send {
to_address: beneficiary,
amount: coins(1000, "earth"),
}
.into(),
}),
);
assert_eq!(
execute_res.attributes,
Expand Down
9 changes: 4 additions & 5 deletions contracts/hackatom/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use cosmwasm_std::{
attr, coins, from_binary, to_vec, Addr, AllBalanceResponse, BankMsg, Binary, ContractResult,
Empty, Response,
Empty, Response, SubMsg,
};
use cosmwasm_vm::{
call_execute, from_slice,
Expand Down Expand Up @@ -173,7 +173,7 @@ fn sudo_can_steal_tokens() {
let res: Response = sudo(&mut deps, mock_env(), sys_msg).unwrap();
assert_eq!(1, res.messages.len());
let msg = res.messages.get(0).expect("no message");
assert_eq!(msg, &BankMsg::Send { to_address, amount }.into(),);
assert_eq!(msg, &SubMsg::new(BankMsg::Send { to_address, amount }));
}

#[test]
Expand Down Expand Up @@ -242,11 +242,10 @@ fn execute_release_works() {
let msg = execute_res.messages.get(0).expect("no message");
assert_eq!(
msg,
&BankMsg::Send {
&SubMsg::new(BankMsg::Send {
to_address: beneficiary,
amount: coins(1000, "earth"),
}
.into(),
}),
);
assert_eq!(
execute_res.attributes,
Expand Down
38 changes: 14 additions & 24 deletions contracts/ibc-reflect-send/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ pub fn instantiate(
config(deps.storage).save(&cfg)?;

Ok(Response {
data: None,
submessages: vec![],
messages: vec![],
attributes: vec![attr("action", "instantiate")],
..Response::default()
})
}

Expand Down Expand Up @@ -61,13 +59,11 @@ pub fn handle_update_admin(
config(deps.storage).save(&cfg)?;

Ok(Response {
submessages: vec![],
messages: vec![],
attributes: vec![
attr("action", "handle_update_admin"),
attr("new_admin", cfg.admin),
],
data: None,
..Response::default()
})
}

Expand All @@ -94,12 +90,10 @@ pub fn handle_send_msgs(
timeout: env.block.time.plus_seconds(PACKET_LIFETIME).into(),
};

Ok(Response {
submessages: vec![],
messages: vec![msg.into()],
attributes: vec![attr("action", "handle_send_msgs")],
data: None,
})
let mut res = Response::new();
res.add_message(msg);
res.add_attribute("action", "handle_send_msgs");
Ok(res)
}

pub fn handle_check_remote_balance(
Expand All @@ -124,12 +118,10 @@ pub fn handle_check_remote_balance(
timeout: env.block.time.plus_seconds(PACKET_LIFETIME).into(),
};

Ok(Response {
submessages: vec![],
messages: vec![msg.into()],
attributes: vec![attr("action", "handle_check_remote_balance")],
data: None,
})
let mut res = Response::new();
res.add_message(msg);
res.add_attribute("action", "handle_check_remote_balance");
Ok(res)
}

pub fn handle_send_funds(
Expand Down Expand Up @@ -174,12 +166,10 @@ pub fn handle_send_funds(
timeout: env.block.time.plus_seconds(PACKET_LIFETIME).into(),
};

Ok(Response {
submessages: vec![],
messages: vec![msg.into()],
attributes: vec![attr("action", "handle_send_funds")],
data: None,
})
let mut res = Response::new();
res.add_message(msg);
res.add_attribute("action", "handle_send_funds");
Ok(res)
}

#[entry_point]
Expand Down
21 changes: 6 additions & 15 deletions contracts/ibc-reflect-send/src/ibc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
attr, entry_point, from_slice, to_binary, DepsMut, Env, IbcAcknowledgement, IbcBasicResponse,
IbcChannel, IbcMsg, IbcOrder, IbcPacket, IbcReceiveResponse, StdError, StdResult,
IbcChannel, IbcMsg, IbcOrder, IbcPacket, IbcReceiveResponse, StdError, StdResult, SubMsg,
};

use crate::ibc_msg::{
Expand Down Expand Up @@ -62,8 +62,7 @@ pub fn ibc_channel_connect(
};

Ok(IbcBasicResponse {
submessages: vec![],
messages: vec![msg.into()],
messages: vec![SubMsg::new(msg)],
attributes: vec![
attr("action", "ibc_connect"),
attr("channel_id", channel_id),
Expand All @@ -83,9 +82,8 @@ pub fn ibc_channel_close(
accounts(deps.storage).remove(channel_id.as_bytes());

Ok(IbcBasicResponse {
submessages: vec![],
messages: vec![],
attributes: vec![attr("action", "ibc_close"), attr("channel_id", channel_id)],
messages: vec![],
})
}

Expand All @@ -98,7 +96,6 @@ pub fn ibc_packet_receive(
) -> StdResult<IbcReceiveResponse> {
Ok(IbcReceiveResponse {
acknowledgement: b"{}".into(),
submessages: vec![],
messages: vec![],
attributes: vec![attr("action", "ibc_packet_ack")],
})
Expand Down Expand Up @@ -139,7 +136,6 @@ fn acknowledge_dispatch(
) -> StdResult<IbcBasicResponse> {
// TODO: actually handle success/error?
Ok(IbcBasicResponse {
submessages: vec![],
messages: vec![],
attributes: vec![attr("action", "acknowledge_dispatch")],
})
Expand All @@ -157,7 +153,6 @@ fn acknowledge_who_am_i(
AcknowledgementMsg::Ok(res) => res,
AcknowledgementMsg::Err(e) => {
return Ok(IbcBasicResponse {
submessages: vec![],
messages: vec![],
attributes: vec![attr("action", "acknowledge_who_am_i"), attr("error", e)],
})
Expand All @@ -178,7 +173,6 @@ fn acknowledge_who_am_i(
})?;

Ok(IbcBasicResponse {
submessages: vec![],
messages: vec![],
attributes: vec![attr("action", "acknowledge_who_am_i")],
})
Expand All @@ -196,7 +190,6 @@ fn acknowledge_balances(
AcknowledgementMsg::Ok(res) => res,
AcknowledgementMsg::Err(e) => {
return Ok(IbcBasicResponse {
submessages: vec![],
messages: vec![],
attributes: vec![attr("action", "acknowledge_balances"), attr("error", e)],
})
Expand Down Expand Up @@ -225,7 +218,6 @@ fn acknowledge_balances(
})?;

Ok(IbcBasicResponse {
submessages: vec![],
messages: vec![],
attributes: vec![attr("action", "acknowledge_balances")],
})
Expand All @@ -239,7 +231,6 @@ pub fn ibc_packet_timeout(
_packet: IbcPacket,
) -> StdResult<IbcBasicResponse> {
Ok(IbcBasicResponse {
submessages: vec![],
messages: vec![],
attributes: vec![attr("action", "ibc_packet_timeout")],
})
Expand Down Expand Up @@ -283,7 +274,7 @@ mod tests {

// this should send a WhoAmI request, which is received some blocks later
assert_eq!(1, res.messages.len());
match &res.messages[0] {
match &res.messages[0].msg {
CosmosMsg::Ibc(IbcMsg::SendPacket {
channel_id: packet_channel,
..
Expand Down Expand Up @@ -376,7 +367,7 @@ mod tests {
let info = mock_info(CREATOR, &[]);
let mut res = execute(deps.as_mut(), mock_env(), info, handle_msg).unwrap();
assert_eq!(1, res.messages.len());
let packet = match res.messages.swap_remove(0) {
let packet = match res.messages.swap_remove(0).msg {
CosmosMsg::Ibc(IbcMsg::SendPacket {
channel_id, data, ..
}) => {
Expand Down Expand Up @@ -435,7 +426,7 @@ mod tests {
let info = mock_info(CREATOR, &coins(12344, "utrgd"));
let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();
assert_eq!(1, res.messages.len());
match &res.messages[0] {
match &res.messages[0].msg {
CosmosMsg::Ibc(IbcMsg::Transfer {
channel_id,
to_address,
Expand Down
Loading