Skip to content

Commit f275d78

Browse files
authored
Merge pull request #961 from CosmWasm/merge-messages-submessages
Merge messages submessages
2 parents 98ad67a + 096a9d1 commit f275d78

32 files changed

+333
-305
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ and this project adheres to
4040
in `cosmwasm-vm` itself instead of being imported from `cosmwasm-crypto`.
4141
- cosmwasm-vm: Filesystem storage layout now distinguishes clearly between state
4242
and cache.
43+
- cosmwasm-std: Add enum case `ReplyOn::Never`; Remove default implementation of
44+
`ReplyOn` as there is no natural default case anymore ([#961]).
45+
- cosmwasm-std: Merge `messages` and `submessages` into one list, using
46+
`ReplyOn::Never` to model the "fire and forget" semantics ([#961]).
47+
- cosmwasm-std: Add `SubMsg` constructors: `::new()`, `::reply_on_error()`,
48+
`::reply_on_success()`, `::reply_always()`; Add `with_gas_limit` to add a gas
49+
limit to any those constructors ([#961]).
50+
51+
[#961]: https://github.com/CosmWasm/cosmwasm/pull/961
4352

4453
### Fixed
4554

MIGRATING.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,64 @@ This guide explains what is needed to upgrade contracts when migrating over
44
major releases of `cosmwasm`. Note that you can also view the
55
[complete CHANGELOG](./CHANGELOG.md) to understand the differences.
66

7-
## 0.13 -> 0.14 (unreleased)
7+
## 0.14 -> 0.15 (unreleased)
8+
9+
- Combine `messages` and `submessages` on the `Response` object. The new format
10+
uses `messages: Vec<SubMsg<T>>`, so copy `submessages` content, and wrap old
11+
messages using `SubMsg::new`. Here is how to change messages:
12+
13+
```rust
14+
let send = BankMsg::Send { to_address, amount };
15+
16+
// before
17+
let res = Response {
18+
messages: vec![send.into()],
19+
..Response::default()
20+
}
21+
22+
// after
23+
let res = Response {
24+
messages: vec![SubMsg::new(send)],
25+
..Response::default()
26+
}
27+
28+
// alternate approach
29+
let mut res = Response::new();
30+
res.add_message(send);
31+
```
32+
33+
And here is how to change submessages:
34+
35+
```rust
36+
// before
37+
let sub_msg = SubMsg {
38+
id: INIT_CALLBACK_ID,
39+
msg: msg.into(),
40+
gas_limit: None,
41+
reply_on: ReplyOn::Success,
42+
};
43+
let res = Response {
44+
submessages: vec![sub_msg],
45+
..Response::default()
46+
};
47+
48+
// after
49+
let msg = SubMsg::reply_on_success(msg, INIT_CALLBACK_ID);
50+
let res = Response {
51+
messages: vec![msg],
52+
..Response::default()
53+
};
54+
55+
// alternate approach
56+
let msg = SubMsg::reply_on_success(msg, INIT_CALLBACK_ID);
57+
let mut res = Response::new();
58+
res.add_submessage(msg);
59+
```
60+
61+
Note that this means you can mix "messages" and "submessages" in any execution
62+
order. You are no more restricted to doing "submessages" first.
63+
64+
## 0.13 -> 0.14
865

966
- The minimum Rust supported version for 0.14 is 1.51.0. Verify your Rust
1067
version is >= 1.51.0 with: `rustc --version`

contracts/burner/src/contract.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use cosmwasm_std::{
22
attr, entry_point, BankMsg, DepsMut, Env, MessageInfo, Order, Response, StdError, StdResult,
3+
SubMsg,
34
};
45

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

4142
Ok(Response {
42-
submessages: vec![],
43-
messages: vec![send.into()],
43+
messages: vec![SubMsg::new(send)],
4444
attributes: vec![attr("action", "burn"), attr("payout", msg.payout)],
4545
data: Some(data_msg.into()),
4646
})
@@ -90,11 +90,10 @@ mod tests {
9090
let msg = res.messages.get(0).expect("no message");
9191
assert_eq!(
9292
msg,
93-
&BankMsg::Send {
93+
&SubMsg::new(BankMsg::Send {
9494
to_address: payout,
9595
amount: coins(123456, "gold"),
96-
}
97-
.into(),
96+
})
9897
);
9998

10099
// check there is no data in storage

contracts/burner/tests/integration.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! });
1818
//! 4. Anywhere you see query(&deps, ...) you must replace it with query(&mut deps, ...)
1919
20-
use cosmwasm_std::{coins, BankMsg, ContractResult, Order, Response};
20+
use cosmwasm_std::{coins, BankMsg, ContractResult, Order, Response, SubMsg};
2121
use cosmwasm_vm::testing::{instantiate, migrate, mock_env, mock_info, mock_instance};
2222

2323
use burner::msg::{InstantiateMsg, MigrateMsg};
@@ -70,11 +70,10 @@ fn migrate_cleans_up_data() {
7070
let msg = res.messages.get(0).expect("no message");
7171
assert_eq!(
7272
msg,
73-
&BankMsg::Send {
73+
&SubMsg::new(BankMsg::Send {
7474
to_address: payout,
7575
amount: coins(123456, "gold"),
76-
}
77-
.into(),
76+
}),
7877
);
7978

8079
// check there is no data in storage

contracts/hackatom/src/contract.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ mod tests {
285285
mock_dependencies, mock_dependencies_with_balances, mock_env, mock_info, MOCK_CONTRACT_ADDR,
286286
};
287287
// import trait Storage to get access to read
288-
use cosmwasm_std::{attr, coins, Binary, Storage};
288+
use cosmwasm_std::{attr, coins, Binary, Storage, SubMsg};
289289

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

400400
#[test]
@@ -446,11 +446,10 @@ mod tests {
446446
let msg = execute_res.messages.get(0).expect("no message");
447447
assert_eq!(
448448
msg,
449-
&BankMsg::Send {
449+
&SubMsg::new(BankMsg::Send {
450450
to_address: beneficiary,
451451
amount: coins(1000, "earth"),
452-
}
453-
.into(),
452+
}),
454453
);
455454
assert_eq!(
456455
execute_res.attributes,

contracts/hackatom/tests/integration.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
use cosmwasm_std::{
2121
attr, coins, from_binary, to_vec, Addr, AllBalanceResponse, BankMsg, Binary, ContractResult,
22-
Empty, Response,
22+
Empty, Response, SubMsg,
2323
};
2424
use cosmwasm_vm::{
2525
call_execute, from_slice,
@@ -173,7 +173,7 @@ fn sudo_can_steal_tokens() {
173173
let res: Response = sudo(&mut deps, mock_env(), sys_msg).unwrap();
174174
assert_eq!(1, res.messages.len());
175175
let msg = res.messages.get(0).expect("no message");
176-
assert_eq!(msg, &BankMsg::Send { to_address, amount }.into(),);
176+
assert_eq!(msg, &SubMsg::new(BankMsg::Send { to_address, amount }));
177177
}
178178

179179
#[test]
@@ -242,11 +242,10 @@ fn execute_release_works() {
242242
let msg = execute_res.messages.get(0).expect("no message");
243243
assert_eq!(
244244
msg,
245-
&BankMsg::Send {
245+
&SubMsg::new(BankMsg::Send {
246246
to_address: beneficiary,
247247
amount: coins(1000, "earth"),
248-
}
249-
.into(),
248+
}),
250249
);
251250
assert_eq!(
252251
execute_res.attributes,

contracts/ibc-reflect-send/src/contract.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ pub fn instantiate(
2323
config(deps.storage).save(&cfg)?;
2424

2525
Ok(Response {
26-
data: None,
27-
submessages: vec![],
28-
messages: vec![],
2926
attributes: vec![attr("action", "instantiate")],
27+
..Response::default()
3028
})
3129
}
3230

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

6361
Ok(Response {
64-
submessages: vec![],
65-
messages: vec![],
6662
attributes: vec![
6763
attr("action", "handle_update_admin"),
6864
attr("new_admin", cfg.admin),
6965
],
70-
data: None,
66+
..Response::default()
7167
})
7268
}
7369

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

97-
Ok(Response {
98-
submessages: vec![],
99-
messages: vec![msg.into()],
100-
attributes: vec![attr("action", "handle_send_msgs")],
101-
data: None,
102-
})
93+
let mut res = Response::new();
94+
res.add_message(msg);
95+
res.add_attribute("action", "handle_send_msgs");
96+
Ok(res)
10397
}
10498

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

127-
Ok(Response {
128-
submessages: vec![],
129-
messages: vec![msg.into()],
130-
attributes: vec![attr("action", "handle_check_remote_balance")],
131-
data: None,
132-
})
121+
let mut res = Response::new();
122+
res.add_message(msg);
123+
res.add_attribute("action", "handle_check_remote_balance");
124+
Ok(res)
133125
}
134126

135127
pub fn handle_send_funds(
@@ -174,12 +166,10 @@ pub fn handle_send_funds(
174166
timeout: env.block.time.plus_seconds(PACKET_LIFETIME).into(),
175167
};
176168

177-
Ok(Response {
178-
submessages: vec![],
179-
messages: vec![msg.into()],
180-
attributes: vec![attr("action", "handle_send_funds")],
181-
data: None,
182-
})
169+
let mut res = Response::new();
170+
res.add_message(msg);
171+
res.add_attribute("action", "handle_send_funds");
172+
Ok(res)
183173
}
184174

185175
#[entry_point]

contracts/ibc-reflect-send/src/ibc.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cosmwasm_std::{
22
attr, entry_point, from_slice, to_binary, DepsMut, Env, IbcAcknowledgement, IbcBasicResponse,
3-
IbcChannel, IbcMsg, IbcOrder, IbcPacket, IbcReceiveResponse, StdError, StdResult,
3+
IbcChannel, IbcMsg, IbcOrder, IbcPacket, IbcReceiveResponse, StdError, StdResult, SubMsg,
44
};
55

66
use crate::ibc_msg::{
@@ -62,8 +62,7 @@ pub fn ibc_channel_connect(
6262
};
6363

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

8584
Ok(IbcBasicResponse {
86-
submessages: vec![],
87-
messages: vec![],
8885
attributes: vec![attr("action", "ibc_close"), attr("channel_id", channel_id)],
86+
messages: vec![],
8987
})
9088
}
9189

@@ -98,7 +96,6 @@ pub fn ibc_packet_receive(
9896
) -> StdResult<IbcReceiveResponse> {
9997
Ok(IbcReceiveResponse {
10098
acknowledgement: b"{}".into(),
101-
submessages: vec![],
10299
messages: vec![],
103100
attributes: vec![attr("action", "ibc_packet_ack")],
104101
})
@@ -139,7 +136,6 @@ fn acknowledge_dispatch(
139136
) -> StdResult<IbcBasicResponse> {
140137
// TODO: actually handle success/error?
141138
Ok(IbcBasicResponse {
142-
submessages: vec![],
143139
messages: vec![],
144140
attributes: vec![attr("action", "acknowledge_dispatch")],
145141
})
@@ -157,7 +153,6 @@ fn acknowledge_who_am_i(
157153
AcknowledgementMsg::Ok(res) => res,
158154
AcknowledgementMsg::Err(e) => {
159155
return Ok(IbcBasicResponse {
160-
submessages: vec![],
161156
messages: vec![],
162157
attributes: vec![attr("action", "acknowledge_who_am_i"), attr("error", e)],
163158
})
@@ -178,7 +173,6 @@ fn acknowledge_who_am_i(
178173
})?;
179174

180175
Ok(IbcBasicResponse {
181-
submessages: vec![],
182176
messages: vec![],
183177
attributes: vec![attr("action", "acknowledge_who_am_i")],
184178
})
@@ -196,7 +190,6 @@ fn acknowledge_balances(
196190
AcknowledgementMsg::Ok(res) => res,
197191
AcknowledgementMsg::Err(e) => {
198192
return Ok(IbcBasicResponse {
199-
submessages: vec![],
200193
messages: vec![],
201194
attributes: vec![attr("action", "acknowledge_balances"), attr("error", e)],
202195
})
@@ -225,7 +218,6 @@ fn acknowledge_balances(
225218
})?;
226219

227220
Ok(IbcBasicResponse {
228-
submessages: vec![],
229221
messages: vec![],
230222
attributes: vec![attr("action", "acknowledge_balances")],
231223
})
@@ -239,7 +231,6 @@ pub fn ibc_packet_timeout(
239231
_packet: IbcPacket,
240232
) -> StdResult<IbcBasicResponse> {
241233
Ok(IbcBasicResponse {
242-
submessages: vec![],
243234
messages: vec![],
244235
attributes: vec![attr("action", "ibc_packet_timeout")],
245236
})
@@ -283,7 +274,7 @@ mod tests {
283274

284275
// this should send a WhoAmI request, which is received some blocks later
285276
assert_eq!(1, res.messages.len());
286-
match &res.messages[0] {
277+
match &res.messages[0].msg {
287278
CosmosMsg::Ibc(IbcMsg::SendPacket {
288279
channel_id: packet_channel,
289280
..
@@ -376,7 +367,7 @@ mod tests {
376367
let info = mock_info(CREATOR, &[]);
377368
let mut res = execute(deps.as_mut(), mock_env(), info, handle_msg).unwrap();
378369
assert_eq!(1, res.messages.len());
379-
let packet = match res.messages.swap_remove(0) {
370+
let packet = match res.messages.swap_remove(0).msg {
380371
CosmosMsg::Ibc(IbcMsg::SendPacket {
381372
channel_id, data, ..
382373
}) => {
@@ -435,7 +426,7 @@ mod tests {
435426
let info = mock_info(CREATOR, &coins(12344, "utrgd"));
436427
let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();
437428
assert_eq!(1, res.messages.len());
438-
match &res.messages[0] {
429+
match &res.messages[0].msg {
439430
CosmosMsg::Ibc(IbcMsg::Transfer {
440431
channel_id,
441432
to_address,

0 commit comments

Comments
 (0)