Skip to content

Commit

Permalink
Merge pull request #1954 from CosmWasm/1611-reply-gas-used
Browse files Browse the repository at this point in the history
Add `gas_used` to `Reply`
  • Loading branch information
chipshort authored Nov 20, 2023
2 parents 8a652d7 + 4a0fc53 commit b0ff68d
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ and this project adheres to

- cosmwasm-std: Add `SubMsg:reply_never` constructor ([#1929])
- cosmwasm-std: Add optional memo field to `IbcMsg::Transfer`. ([#1878])
- cosmwasm-std: Add `Reply::gas_used`. ([#1954])

[#1878]: https://github.com/CosmWasm/cosmwasm/pull/1878
[#1929]: https://github.com/CosmWasm/cosmwasm/pull/1929
[#1954]: https://github.com/CosmWasm/cosmwasm/pull/1954

### Changed

Expand Down
7 changes: 4 additions & 3 deletions SEMANTICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,12 @@ pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractEr

pub struct Reply {
pub id: u64,
/// ContractResult is just a nicely serializable version of `Result<SubcallResponse, String>`
pub result: ContractResult<SubcallResponse>,
pub gas_used: u64,
/// SubMsgResult is just a nicely serializable version of `Result<SubMsgResponse, String>`
pub result: SubMsgResult,
}

pub struct SubcallResponse {
pub struct SubMsgResponse {
pub events: Vec<Event>,
pub data: Option<Binary>,
}
Expand Down
2 changes: 2 additions & 0 deletions contracts/ibc-reflect/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ mod tests {
// fake a reply and ensure this works
let response = Reply {
id,
gas_used: 1234567,
result: SubMsgResult::Ok(SubMsgResponse {
events: fake_events(&account),
data: None,
Expand Down Expand Up @@ -481,6 +482,7 @@ mod tests {
// fake a reply and ensure this works
let response = Reply {
id,
gas_used: 1234567,
result: SubMsgResult::Ok(SubMsgResponse {
events: fake_events(REFLECT_ADDR),
data: None,
Expand Down
2 changes: 2 additions & 0 deletions contracts/ibc-reflect/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ fn connect(
// fake a reply and ensure this works
let response = Reply {
id,
gas_used: 1234567,
result: SubMsgResult::Ok(SubMsgResponse {
events: fake_events(&account),
data: None,
Expand Down Expand Up @@ -173,6 +174,7 @@ fn proper_handshake_flow() {
// we get the callback from reflect
let response = Reply {
id,
gas_used: 1234567,
result: SubMsgResult::Ok(SubMsgResponse {
events: fake_events(REFLECT_ADDR),
data: None,
Expand Down
7 changes: 7 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 @@ -4,10 +4,17 @@
"description": "The result object returned to `reply`. We always get the ID from the submessage back and then must handle success and error cases ourselves.",
"type": "object",
"required": [
"gas_used",
"id",
"result"
],
"properties": {
"gas_used": {
"description": "The amount of gas used by the submessage, measured in [Cosmos SDK gas](https://github.com/CosmWasm/cosmwasm/blob/main/docs/GAS.md).",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"id": {
"description": "The ID that the contract set when emitting the `SubMsg`. Use this to identify which submessage triggered the `reply`.",
"type": "integer",
Expand Down
7 changes: 7 additions & 0 deletions contracts/reflect/schema/reflect.json
Original file line number Diff line number Diff line change
Expand Up @@ -1885,10 +1885,17 @@
"description": "The result object returned to `reply`. We always get the ID from the submessage back and then must handle success and error cases ourselves.",
"type": "object",
"required": [
"gas_used",
"id",
"result"
],
"properties": {
"gas_used": {
"description": "The amount of gas used by the submessage, measured in [Cosmos SDK gas](https://github.com/CosmWasm/cosmwasm/blob/main/docs/GAS.md).",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"id": {
"description": "The ID that the contract set when emitting the `SubMsg`. Use this to identify which submessage triggered the `reply`.",
"type": "integer",
Expand Down
7 changes: 6 additions & 1 deletion contracts/reflect/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,16 @@ mod tests {
let id = 123u64;
let data = Binary::from(b"foobar");
let events = vec![Event::new("message").add_attribute("signer", "caller-addr")];
let gas_used = 1234567u64;
let result = SubMsgResult::Ok(SubMsgResponse {
events: events.clone(),
data: Some(data.clone()),
});
let subcall = Reply { id, result };
let subcall = Reply {
id,
gas_used,
result,
};
let res = reply(deps.as_mut(), mock_env(), subcall).unwrap();
assert_eq!(0, res.messages.len());

Expand Down
7 changes: 6 additions & 1 deletion contracts/reflect/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,16 @@ 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 gas_used = 1234567u64;
let result = SubMsgResult::Ok(SubMsgResponse {
events: events.clone(),
data: Some(data.clone()),
});
let subcall = Reply { id, result };
let subcall = Reply {
id,
gas_used,
result,
};
let res: Response = reply(&mut deps, mock_env(), subcall).unwrap();
assert_eq!(0, res.messages.len());

Expand Down
2 changes: 1 addition & 1 deletion packages/go-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod schema;
mod utils;

fn main() -> Result<()> {
let root = cosmwasm_schema::schema_for!(cosmwasm_std::BankQuery);
let root = cosmwasm_schema::schema_for!(cosmwasm_std::Reply);

let code = generate_go(root)?;
println!("{}", code);
Expand Down
3 changes: 3 additions & 0 deletions packages/std/src/results/submessages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ pub struct Reply {
/// The ID that the contract set when emitting the `SubMsg`.
/// Use this to identify which submessage triggered the `reply`.
pub id: u64,
/// The amount of gas used by the submessage,
/// measured in [Cosmos SDK gas](https://github.com/CosmWasm/cosmwasm/blob/main/docs/GAS.md).
pub gas_used: u64,
pub result: SubMsgResult,
}

Expand Down
1 change: 1 addition & 0 deletions packages/vm/src/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ mod tests {
// which creates a reflect account. here we get the callback
let response = Reply {
id,
gas_used: 1234567,
result: SubMsgResult::Ok(SubMsgResponse {
events: vec![event],
data: None,
Expand Down

0 comments on commit b0ff68d

Please sign in to comment.