Skip to content

Commit

Permalink
Merge pull request #2008 from CosmWasm/add-Reply-payload
Browse files Browse the repository at this point in the history
Add payload field to SubMsg and Reply
  • Loading branch information
webmaster128 authored Jan 30, 2024
2 parents 46201a6 + 7addcf5 commit 30f4854
Show file tree
Hide file tree
Showing 12 changed files with 288 additions and 13 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]

### Added

- cosmwasm-std: Add new field `payload` to `SubMsg` and `Reply`. This is binary
data the contract can set in a contract specific format and get back then the
`reply` entry point is called. `SubMsg::with_payload` allows setting the
payload on an existing `SubMsg`. ([#2008])

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

### Changed

- cosmwasm-vm: Limit total number of function parameters in
Expand Down
41 changes: 41 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,47 @@ major releases of `cosmwasm`. Note that you can also view the
+}))?;
```

- A new `payload` field allows you to send arbitrary data from the original
contract into the `reply`. If you construct `SubMsg` manually, add the
`payload` field:

```diff
SubMsg {
id: 12,
+ payload: Binary::default(),
msg: my_bank_send,
gas_limit: Some(12345u64),
reply_on: ReplyOn::Always,
},
```

or with data:

```diff
SubMsg {
id: 12,
+ payload: Binary::new(vec![9, 8, 7, 6, 5]),
msg: my_bank_send,
gas_limit: Some(12345u64),
reply_on: ReplyOn::Always,
},
```

If you use a constructor function, you can set the payload as follows:

```diff
SubMsg::new(BankMsg::Send {
to_address: payout,
amount: coins(123456u128,"gold")
})
+.with_payload(vec![9, 8, 7, 6, 5])
```

The payload data will then be available in the new field `Reply.payload` in
the `reply` entry point. This functionality is an optional addition introduced
in 2.0. To keep the CosmWasm 1.x behaviour, just set payload to
`Binary::default()`.

## 1.4.x -> 1.5.0

- Update `cosmwasm-*` dependencies in Cargo.toml (skip the ones you don't use):
Expand Down
4 changes: 4 additions & 0 deletions contracts/ibc-reflect/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,13 @@ mod tests {
res.events[0]
);
let id = res.messages[0].id;
let payload = res.messages[0].payload.clone();

// fake a reply and ensure this works
#[allow(deprecated)]
let response = Reply {
id,
payload,
gas_used: 1234567,
result: SubMsgResult::Ok(SubMsgResponse {
events: fake_events(&account),
Expand Down Expand Up @@ -461,6 +463,7 @@ mod tests {
// and set up a reflect account
assert_eq!(1, res.messages.len());
let id = res.messages[0].id;
let payload = res.messages[0].payload.clone();
if let CosmosMsg::Wasm(WasmMsg::Instantiate {
admin,
code_id,
Expand All @@ -486,6 +489,7 @@ mod tests {
#[allow(deprecated)]
let response = Reply {
id,
payload,
gas_used: 1234567,
result: SubMsgResult::Ok(SubMsgResponse {
events: fake_events(reflect_addr.as_str()),
Expand Down
4 changes: 4 additions & 0 deletions contracts/ibc-reflect/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ fn connect(
res.events[0]
);
let id = res.messages[0].id;
let payload = res.messages[0].payload.clone();

// fake a reply and ensure this works
#[allow(deprecated)]
let response = Reply {
id,
payload,
gas_used: 1234567,
result: SubMsgResult::Ok(SubMsgResponse {
events: fake_events(&account),
Expand Down Expand Up @@ -153,6 +155,7 @@ fn proper_handshake_flow() {
// and set up a reflect account
assert_eq!(1, res.messages.len());
let id = res.messages[0].id;
let payload = res.messages[0].payload.clone();
if let CosmosMsg::Wasm(WasmMsg::Instantiate {
admin,
code_id,
Expand All @@ -178,6 +181,7 @@ fn proper_handshake_flow() {
#[allow(deprecated)]
let response = Reply {
id,
payload,
gas_used: 1234567,
result: SubMsgResult::Ok(SubMsgResponse {
events: fake_events(reflect_addr.as_str()),
Expand Down
11 changes: 10 additions & 1 deletion contracts/reflect/schema/raw/execute.json
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@
],
"properties": {
"gas_limit": {
"description": "Gas limit measured in [Cosmos SDK gas](https://github.com/CosmWasm/cosmwasm/blob/main/docs/GAS.md).",
"description": "Gas limit measured in [Cosmos SDK gas](https://github.com/CosmWasm/cosmwasm/blob/main/docs/GAS.md).\n\nSetting this to `None` means unlimited. Then the submessage execution can consume all gas of the current execution context.",
"type": [
"integer",
"null"
Expand All @@ -715,6 +715,15 @@
"msg": {
"$ref": "#/definitions/CosmosMsg_for_CustomMsg"
},
"payload": {
"description": "Some arbirary data that the contract can set in an application specific way. This is just passed into the `reply` entry point and is not stored to state. Any encoding can be used. If `id` is used to identify a particular action, the encoding can also be different for each of those actions since you can match `id` first and then start processing the `payload`.\n\nThe environment restricts the length of this field in order to avoid abuse. The limit is environment specific and can change over time. The initial default is 128 KiB.\n\nUnset/nil/null cannot be differentiated from empty data.\n\nOn chains running CosmWasm 1.x this field will be ignored.",
"default": "",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"reply_on": {
"$ref": "#/definitions/ReplyOn"
}
Expand Down
9 changes: 9 additions & 0 deletions contracts/reflect/schema/raw/response_to_sub_msg_result.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
"format": "uint64",
"minimum": 0.0
},
"payload": {
"description": "Some arbirary data that the contract set when emitting the `SubMsg`. This is just passed into the `reply` entry point and is not stored to state.\n\nUnset/nil/null cannot be differentiated from empty data.\n\nOn chains running CosmWasm 1.x this field is never filled.",
"default": "",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"result": {
"$ref": "#/definitions/SubMsgResult"
}
Expand Down
20 changes: 19 additions & 1 deletion contracts/reflect/schema/reflect.json
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@
],
"properties": {
"gas_limit": {
"description": "Gas limit measured in [Cosmos SDK gas](https://github.com/CosmWasm/cosmwasm/blob/main/docs/GAS.md).",
"description": "Gas limit measured in [Cosmos SDK gas](https://github.com/CosmWasm/cosmwasm/blob/main/docs/GAS.md).\n\nSetting this to `None` means unlimited. Then the submessage execution can consume all gas of the current execution context.",
"type": [
"integer",
"null"
Expand All @@ -725,6 +725,15 @@
"msg": {
"$ref": "#/definitions/CosmosMsg_for_CustomMsg"
},
"payload": {
"description": "Some arbirary data that the contract can set in an application specific way. This is just passed into the `reply` entry point and is not stored to state. Any encoding can be used. If `id` is used to identify a particular action, the encoding can also be different for each of those actions since you can match `id` first and then start processing the `payload`.\n\nThe environment restricts the length of this field in order to avoid abuse. The limit is environment specific and can change over time. The initial default is 128 KiB.\n\nUnset/nil/null cannot be differentiated from empty data.\n\nOn chains running CosmWasm 1.x this field will be ignored.",
"default": "",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"reply_on": {
"$ref": "#/definitions/ReplyOn"
}
Expand Down Expand Up @@ -1875,6 +1884,15 @@
"format": "uint64",
"minimum": 0.0
},
"payload": {
"description": "Some arbirary data that the contract set when emitting the `SubMsg`. This is just passed into the `reply` entry point and is not stored to state.\n\nUnset/nil/null cannot be differentiated from empty data.\n\nOn chains running CosmWasm 1.x this field is never filled.",
"default": "",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"result": {
"$ref": "#/definitions/SubMsgResult"
}
Expand Down
6 changes: 4 additions & 2 deletions contracts/reflect/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ mod tests {
let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();

let id = 123u64;
let payload = Binary::from(b"my dear");
let data = Binary::from(b"foobar");
let events = vec![Event::new("message").add_attribute("signer", "caller-addr")];
let gas_used = 1234567u64;
Expand All @@ -443,12 +444,13 @@ mod tests {
data: Some(data.clone()),
msg_responses: vec![],
});
let subcall = Reply {
let the_reply = Reply {
id,
payload,
gas_used,
result,
};
let res = reply(deps.as_mut(), mock_env(), subcall).unwrap();
let res = reply(deps.as_mut(), mock_env(), the_reply).unwrap();
assert_eq!(0, res.messages.len());

// query for a non-existant id
Expand Down
6 changes: 4 additions & 2 deletions contracts/reflect/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ fn reply_and_query() {
let _res: Response = instantiate(&mut deps, mock_env(), info, msg).unwrap();

let id = 123u64;
let payload = Binary::from(b"my dear");
let data = Binary::from(b"foobar");
let events = vec![Event::new("message").add_attribute("signer", "caller-addr")];
let gas_used = 1234567u64;
Expand All @@ -277,12 +278,13 @@ fn reply_and_query() {
data: Some(data.clone()),
msg_responses: vec![],
});
let subcall = Reply {
let the_reply = Reply {
id,
payload,
gas_used,
result,
};
let res: Response = reply(&mut deps, mock_env(), subcall).unwrap();
let res: Response = reply(&mut deps, mock_env(), the_reply).unwrap();
assert_eq!(0, res.messages.len());

// query for a non-existant id
Expand Down
2 changes: 2 additions & 0 deletions packages/std/src/results/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ mod tests {
messages: vec![
SubMsg {
id: 12,
payload: Binary::new(vec![9, 8, 7, 6, 5]),
msg: BankMsg::Send {
to_address: String::from("checker"),
amount: coins(888, "moon"),
Expand All @@ -293,6 +294,7 @@ mod tests {
},
SubMsg {
id: UNUSED_MSG_ID,
payload: Binary::default(),
msg: BankMsg::Send {
to_address: String::from("you"),
amount: coins(1015, "earth"),
Expand Down
Loading

0 comments on commit 30f4854

Please sign in to comment.