Skip to content

Commit

Permalink
Merge pull request #1833 from CosmWasm/refactor-acknowledgement-msg
Browse files Browse the repository at this point in the history
Refactor AcknowledgementMsg in ibc-reflect and ibc-reflect-send
  • Loading branch information
webmaster128 authored Aug 23, 2023
2 parents fb2e664 + 793bc1c commit f0ab9bb
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "AcknowledgementMsgBalances",
"description": "A custom acknowledgement type. The success type `T` depends on the PacketMsg variant.\n\nThis could be refactored to use [StdAck] at some point. However, it has a different success variant name (\"ok\" vs. \"result\") and a JSON payload instead of a binary payload.\n\n[StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512",
"oneOf": [
{
"type": "object",
"required": [
"ok"
],
"properties": {
"ok": {
"$ref": "#/definitions/BalancesResponse"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"error"
],
"properties": {
"error": {
"type": "string"
}
},
"additionalProperties": false
}
],
"definitions": {
"BalancesResponse": {
"description": "This is the success response we send on ack for PacketMsg::Balance. Just acknowledge success or error",
"type": "object",
"required": [
"account",
"balances"
],
"properties": {
"account": {
"type": "string"
},
"balances": {
"type": "array",
"items": {
"$ref": "#/definitions/Coin"
}
}
}
},
"Coin": {
"type": "object",
"required": [
"amount",
"denom"
],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
},
"denom": {
"type": "string"
}
}
},
"Uint128": {
"description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```",
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "AcknowledgementMsgDispatch",
"description": "A custom acknowledgement type. The success type `T` depends on the PacketMsg variant.\n\nThis could be refactored to use [StdAck] at some point. However, it has a different success variant name (\"ok\" vs. \"result\") and a JSON payload instead of a binary payload.\n\n[StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512",
"oneOf": [
{
"type": "object",
"required": [
"ok"
],
"properties": {
"ok": {
"type": "null"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"error"
],
"properties": {
"error": {
"type": "string"
}
},
"additionalProperties": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "AcknowledgementMsgWhoAmI",
"description": "A custom acknowledgement type. The success type `T` depends on the PacketMsg variant.\n\nThis could be refactored to use [StdAck] at some point. However, it has a different success variant name (\"ok\" vs. \"result\") and a JSON payload instead of a binary payload.\n\n[StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512",
"oneOf": [
{
"type": "object",
"required": [
"ok"
],
"properties": {
"ok": {
"$ref": "#/definitions/WhoAmIResponse"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"error"
],
"properties": {
"error": {
"type": "string"
}
},
"additionalProperties": false
}
],
"definitions": {
"WhoAmIResponse": {
"description": "This is the success response we send on ack for PacketMsg::WhoAmI. Return the caller's account address on the remote chain",
"type": "object",
"required": [
"account"
],
"properties": {
"account": {
"type": "string"
}
}
}
}
}
22 changes: 20 additions & 2 deletions contracts/ibc-reflect-send/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::env::current_dir;

use cosmwasm_schema::{export_schema, schema_for, write_api};
use cosmwasm_schema::{export_schema, export_schema_with_title, schema_for, write_api};

use ibc_reflect_send::ibc_msg::PacketMsg;
use ibc_reflect_send::ibc_msg::{
AcknowledgementMsg, BalancesResponse, DispatchResponse, PacketMsg, WhoAmIResponse,
};
use ibc_reflect_send::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

fn main() {
Expand All @@ -16,5 +18,21 @@ fn main() {
// Schemas for inter-contract communication
let mut out_dir = current_dir().unwrap();
out_dir.push("schema");
out_dir.push("ibc");
export_schema(&schema_for!(PacketMsg), &out_dir);
export_schema_with_title(
&schema_for!(AcknowledgementMsg<BalancesResponse>),
&out_dir,
"AcknowledgementMsgBalances",
);
export_schema_with_title(
&schema_for!(AcknowledgementMsg<DispatchResponse>),
&out_dir,
"AcknowledgementMsgDispatch",
);
export_schema_with_title(
&schema_for!(AcknowledgementMsg<WhoAmIResponse>),
&out_dir,
"AcknowledgementMsgWhoAmI",
);
}
4 changes: 2 additions & 2 deletions contracts/ibc-reflect-send/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn acknowledge_who_am_i(
// ignore errors (but mention in log)
let WhoAmIResponse { account } = match ack {
AcknowledgementMsg::Ok(res) => res,
AcknowledgementMsg::Err(e) => {
AcknowledgementMsg::Error(e) => {
return Ok(IbcBasicResponse::new()
.add_attribute("action", "acknowledge_who_am_i")
.add_attribute("error", e))
Expand Down Expand Up @@ -176,7 +176,7 @@ fn acknowledge_balances(
// ignore errors (but mention in log)
let BalancesResponse { account, balances } = match ack {
AcknowledgementMsg::Ok(res) => res,
AcknowledgementMsg::Err(e) => {
AcknowledgementMsg::Error(e) => {
return Ok(IbcBasicResponse::new()
.add_attribute("action", "acknowledge_balances")
.add_attribute("error", e))
Expand Down
35 changes: 31 additions & 4 deletions contracts/ibc-reflect-send/src/ibc_msg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cosmwasm_std::{Coin, ContractResult, CosmosMsg};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Coin, CosmosMsg};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand All @@ -11,9 +12,35 @@ pub enum PacketMsg {
Balances {},
}

/// All IBC acknowledgements are wrapped in `ContractResult`.
/// The success value depends on the PacketMsg variant.
pub type AcknowledgementMsg<T> = ContractResult<T>;
/// A custom acknowledgement type.
/// The success type `T` depends on the PacketMsg variant.
///
/// This could be refactored to use [StdAck] at some point. However,
/// it has a different success variant name ("ok" vs. "result") and
/// a JSON payload instead of a binary payload.
///
/// [StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512
#[cw_serde]
pub enum AcknowledgementMsg<S> {
Ok(S),
Error(String),
}

impl<S> AcknowledgementMsg<S> {
pub fn unwrap(self) -> S {
match self {
AcknowledgementMsg::Ok(data) => data,
AcknowledgementMsg::Error(err) => panic!("{}", err),
}
}

pub fn unwrap_err(self) -> String {
match self {
AcknowledgementMsg::Ok(_) => panic!("not an error"),
AcknowledgementMsg::Error(err) => err,
}
}
}

/// This is the success response we send on ack for PacketMsg::Dispatch.
/// Just acknowledge success or error
Expand Down
32 changes: 0 additions & 32 deletions contracts/ibc-reflect/schema/acknowledgement_msg_dispatch.json

This file was deleted.

47 changes: 0 additions & 47 deletions contracts/ibc-reflect/schema/acknowledgement_msg_who_am_i.json

This file was deleted.

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}}\"#); ```\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\"}\"#); ```",
"description": "A custom acknowledgement type. The success type `T` depends on the PacketMsg variant.\n\nThis could be refactored to use [StdAck] at some point. However, it has a different success variant name (\"ok\" vs. \"result\") and a JSON payload instead of a binary payload.\n\n[StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512",
"oneOf": [
{
"type": "object",
Expand All @@ -16,7 +16,6 @@
"additionalProperties": false
},
{
"description": "An error type that every custom error created by contract developers can be converted to. This could potientially have more structure, but String is the easiest.",
"type": "object",
"required": [
"error"
Expand Down
31 changes: 31 additions & 0 deletions contracts/ibc-reflect/schema/ibc/acknowledgement_msg_dispatch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "AcknowledgementMsgDispatch",
"description": "A custom acknowledgement type. The success type `T` depends on the PacketMsg variant.\n\nThis could be refactored to use [StdAck] at some point. However, it has a different success variant name (\"ok\" vs. \"result\") and a JSON payload instead of a binary payload.\n\n[StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512",
"oneOf": [
{
"type": "object",
"required": [
"ok"
],
"properties": {
"ok": {
"type": "null"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"error"
],
"properties": {
"error": {
"type": "string"
}
},
"additionalProperties": false
}
]
}
Loading

0 comments on commit f0ab9bb

Please sign in to comment.