From 0d1be8634d870063000fb5d1dec163909132bb42 Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Thu, 4 May 2023 17:52:16 +0200 Subject: [PATCH 1/7] contact: support more test scenarios --- contracts/ibc-reflect/src/contract.rs | 11 +++++++++++ contracts/ibc-reflect/src/msg.rs | 2 ++ 2 files changed, 13 insertions(+) diff --git a/contracts/ibc-reflect/src/contract.rs b/contracts/ibc-reflect/src/contract.rs index 04750f00ec..1c46da8265 100644 --- a/contracts/ibc-reflect/src/contract.rs +++ b/contracts/ibc-reflect/src/contract.rs @@ -244,6 +244,8 @@ pub fn ibc_packet_receive( PacketMsg::Dispatch { msgs } => receive_dispatch(deps, caller, msgs), PacketMsg::WhoAmI {} => receive_who_am_i(deps, caller), PacketMsg::Balances {} => receive_balances(deps, caller), + PacketMsg::Panic {} => execute_panic(), + PacketMsg::ReturnErr {text} => execute_error(text), } })() .or_else(|e| { @@ -308,6 +310,15 @@ fn receive_dispatch( .add_attribute("action", "receive_dispatch")) } +fn execute_panic() -> StdResult { + panic!("This page intentionally faulted"); +} + +fn execute_error(text: String) -> StdResult { + return Err(StdError::generic_err(text).into()); +} + + #[entry_point] /// never should be called as we do not send packets pub fn ibc_packet_ack( diff --git a/contracts/ibc-reflect/src/msg.rs b/contracts/ibc-reflect/src/msg.rs index 9e9551be00..4c65aba127 100644 --- a/contracts/ibc-reflect/src/msg.rs +++ b/contracts/ibc-reflect/src/msg.rs @@ -46,6 +46,8 @@ pub enum PacketMsg { Dispatch { msgs: Vec }, WhoAmI {}, Balances {}, + Panic {}, + ReturnErr {text: String}, } /// All acknowledgements are wrapped in `ContractResult`. From df7ca1611f7b12a6c67b2db2b612338665fd2e15 Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Fri, 5 May 2023 08:33:43 +0200 Subject: [PATCH 2/7] Formatting only --- contracts/ibc-reflect/src/contract.rs | 3 +-- contracts/ibc-reflect/src/msg.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/contracts/ibc-reflect/src/contract.rs b/contracts/ibc-reflect/src/contract.rs index 1c46da8265..c161bd7cc5 100644 --- a/contracts/ibc-reflect/src/contract.rs +++ b/contracts/ibc-reflect/src/contract.rs @@ -245,7 +245,7 @@ pub fn ibc_packet_receive( PacketMsg::WhoAmI {} => receive_who_am_i(deps, caller), PacketMsg::Balances {} => receive_balances(deps, caller), PacketMsg::Panic {} => execute_panic(), - PacketMsg::ReturnErr {text} => execute_error(text), + PacketMsg::ReturnErr { text } => execute_error(text), } })() .or_else(|e| { @@ -318,7 +318,6 @@ fn execute_error(text: String) -> StdResult { return Err(StdError::generic_err(text).into()); } - #[entry_point] /// never should be called as we do not send packets pub fn ibc_packet_ack( diff --git a/contracts/ibc-reflect/src/msg.rs b/contracts/ibc-reflect/src/msg.rs index 4c65aba127..723b21478b 100644 --- a/contracts/ibc-reflect/src/msg.rs +++ b/contracts/ibc-reflect/src/msg.rs @@ -47,7 +47,7 @@ pub enum PacketMsg { WhoAmI {}, Balances {}, Panic {}, - ReturnErr {text: String}, + ReturnErr { text: String }, } /// All acknowledgements are wrapped in `ContractResult`. From b0bb92824fa4420d584a2644247d8a8f32f62133 Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Mon, 8 May 2023 14:42:30 +0200 Subject: [PATCH 3/7] Add new test method to ibc-reflect --- contracts/ibc-reflect/src/contract.rs | 14 ++++++++++++++ contracts/ibc-reflect/src/msg.rs | 1 + 2 files changed, 15 insertions(+) diff --git a/contracts/ibc-reflect/src/contract.rs b/contracts/ibc-reflect/src/contract.rs index c161bd7cc5..c99ae67d89 100644 --- a/contracts/ibc-reflect/src/contract.rs +++ b/contracts/ibc-reflect/src/contract.rs @@ -246,6 +246,8 @@ pub fn ibc_packet_receive( PacketMsg::Balances {} => receive_balances(deps, caller), PacketMsg::Panic {} => execute_panic(), PacketMsg::ReturnErr { text } => execute_error(text), + PacketMsg::ReturnMsgs { msgs } => execute_return_msgs(msgs), + } })() .or_else(|e| { @@ -318,6 +320,18 @@ fn execute_error(text: String) -> StdResult { return Err(StdError::generic_err(text).into()); } +fn execute_return_msgs( + msgs: Vec, +) -> StdResult { + let acknowledgement = to_binary(&AcknowledgementMsg::::Ok(()))?; + + Ok(IbcReceiveResponse::new() + .set_ack(acknowledgement) + .add_messages(msgs) + .add_attribute("action", "receive_dispatch")) +} + + #[entry_point] /// never should be called as we do not send packets pub fn ibc_packet_ack( diff --git a/contracts/ibc-reflect/src/msg.rs b/contracts/ibc-reflect/src/msg.rs index 723b21478b..d50ffa5c48 100644 --- a/contracts/ibc-reflect/src/msg.rs +++ b/contracts/ibc-reflect/src/msg.rs @@ -48,6 +48,7 @@ pub enum PacketMsg { Balances {}, Panic {}, ReturnErr { text: String }, + ReturnMsgs { msgs: Vec }, } /// All acknowledgements are wrapped in `ContractResult`. From 80cba9ad862d668f69517dc60e26dfa24be0c1fd Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Mon, 22 May 2023 16:26:52 +0200 Subject: [PATCH 4/7] Add ReturnMsgResponse to ibc-reflect contract --- contracts/ibc-reflect/src/contract.rs | 11 ++++------- contracts/ibc-reflect/src/msg.rs | 4 ++++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/contracts/ibc-reflect/src/contract.rs b/contracts/ibc-reflect/src/contract.rs index c99ae67d89..8130473bdf 100644 --- a/contracts/ibc-reflect/src/contract.rs +++ b/contracts/ibc-reflect/src/contract.rs @@ -9,7 +9,8 @@ use cosmwasm_std::{ use crate::msg::{ AccountInfo, AccountResponse, AcknowledgementMsg, BalancesResponse, DispatchResponse, - InstantiateMsg, ListAccountsResponse, PacketMsg, QueryMsg, ReflectExecuteMsg, WhoAmIResponse, + InstantiateMsg, ListAccountsResponse, PacketMsg, QueryMsg, ReflectExecuteMsg, + ReturnMsgsResponse, WhoAmIResponse, }; use crate::state::{accounts, accounts_read, config, pending_channel, Config}; @@ -247,7 +248,6 @@ pub fn ibc_packet_receive( PacketMsg::Panic {} => execute_panic(), PacketMsg::ReturnErr { text } => execute_error(text), PacketMsg::ReturnMsgs { msgs } => execute_return_msgs(msgs), - } })() .or_else(|e| { @@ -320,10 +320,8 @@ fn execute_error(text: String) -> StdResult { return Err(StdError::generic_err(text).into()); } -fn execute_return_msgs( - msgs: Vec, -) -> StdResult { - let acknowledgement = to_binary(&AcknowledgementMsg::::Ok(()))?; +fn execute_return_msgs(msgs: Vec) -> StdResult { + let acknowledgement = to_binary(&AcknowledgementMsg::::Ok(()))?; Ok(IbcReceiveResponse::new() .set_ack(acknowledgement) @@ -331,7 +329,6 @@ fn execute_return_msgs( .add_attribute("action", "receive_dispatch")) } - #[entry_point] /// never should be called as we do not send packets pub fn ibc_packet_ack( diff --git a/contracts/ibc-reflect/src/msg.rs b/contracts/ibc-reflect/src/msg.rs index d50ffa5c48..acd49baf63 100644 --- a/contracts/ibc-reflect/src/msg.rs +++ b/contracts/ibc-reflect/src/msg.rs @@ -73,3 +73,7 @@ pub struct BalancesResponse { pub account: String, pub balances: Vec, } + +/// This is the success response we send on ack for PacketMsg::ReturnMsgs. +/// Just acknowledge success or error +pub type ReturnMsgsResponse = (); From 81fe3a08559a9212503ea7064ab184dff50e1cb1 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Mon, 22 May 2023 16:32:50 +0200 Subject: [PATCH 5/7] Fix clippy lints --- contracts/ibc-reflect/src/contract.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/ibc-reflect/src/contract.rs b/contracts/ibc-reflect/src/contract.rs index 8130473bdf..3b31aab4f4 100644 --- a/contracts/ibc-reflect/src/contract.rs +++ b/contracts/ibc-reflect/src/contract.rs @@ -317,7 +317,7 @@ fn execute_panic() -> StdResult { } fn execute_error(text: String) -> StdResult { - return Err(StdError::generic_err(text).into()); + Err(StdError::generic_err(text)) } fn execute_return_msgs(msgs: Vec) -> StdResult { From fe6e8b3fb6d19f7339add7f9f1bcf608d4e2e590 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Mon, 22 May 2023 16:49:59 +0200 Subject: [PATCH 6/7] Fix test --- contracts/ibc-reflect/src/contract.rs | 2 +- contracts/ibc-reflect/tests/integration.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/ibc-reflect/src/contract.rs b/contracts/ibc-reflect/src/contract.rs index 3b31aab4f4..11e9049ebf 100644 --- a/contracts/ibc-reflect/src/contract.rs +++ b/contracts/ibc-reflect/src/contract.rs @@ -597,7 +597,7 @@ mod tests { assert_eq!(0, res.messages.len()); // acknowledgement is an error let ack: AcknowledgementMsg = from_slice(&res.acknowledgement).unwrap(); - assert_eq!(ack.unwrap_err(), "invalid packet: Error parsing into type ibc_reflect::msg::PacketMsg: unknown variant `reflect_code_id`, expected one of `dispatch`, `who_am_i`, `balances`"); + assert_eq!(ack.unwrap_err(), "invalid packet: Error parsing into type ibc_reflect::msg::PacketMsg: unknown variant `reflect_code_id`, expected one of `dispatch`, `who_am_i`, `balances`, `panic`, `return_err`, `return_msgs`"); } #[test] diff --git a/contracts/ibc-reflect/tests/integration.rs b/contracts/ibc-reflect/tests/integration.rs index d28e3369a4..66dc430026 100644 --- a/contracts/ibc-reflect/tests/integration.rs +++ b/contracts/ibc-reflect/tests/integration.rs @@ -287,5 +287,5 @@ fn handle_dispatch_packet() { // acknowledgement is an error let ack: AcknowledgementMsg = from_slice(&res.acknowledgement, DESERIALIZATION_LIMIT).unwrap(); - assert_eq!(ack.unwrap_err(), "invalid packet: Error parsing into type ibc_reflect::msg::PacketMsg: unknown variant `reflect_code_id`, expected one of `dispatch`, `who_am_i`, `balances`"); + assert_eq!(ack.unwrap_err(), "invalid packet: Error parsing into type ibc_reflect::msg::PacketMsg: unknown variant `reflect_code_id`, expected one of `dispatch`, `who_am_i`, `balances`, `panic`, `return_err`, `return_msgs`"); } From 9412b136e53dcf0b774b0e1660ecdd694f65ea04 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Mon, 22 May 2023 17:03:51 +0200 Subject: [PATCH 7/7] Update schema --- contracts/ibc-reflect/schema/packet_msg.json | 58 ++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/contracts/ibc-reflect/schema/packet_msg.json b/contracts/ibc-reflect/schema/packet_msg.json index bd940ab4f0..547c6fe054 100644 --- a/contracts/ibc-reflect/schema/packet_msg.json +++ b/contracts/ibc-reflect/schema/packet_msg.json @@ -51,6 +51,64 @@ } }, "additionalProperties": false + }, + { + "type": "object", + "required": [ + "panic" + ], + "properties": { + "panic": { + "type": "object", + "additionalProperties": false + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "return_err" + ], + "properties": { + "return_err": { + "type": "object", + "required": [ + "text" + ], + "properties": { + "text": { + "type": "string" + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "return_msgs" + ], + "properties": { + "return_msgs": { + "type": "object", + "required": [ + "msgs" + ], + "properties": { + "msgs": { + "type": "array", + "items": { + "$ref": "#/definitions/CosmosMsg_for_Empty" + } + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false } ], "definitions": {