Skip to content

Commit

Permalink
Merge pull request #1690 from CosmWasm/chipshort/ibc_panic
Browse files Browse the repository at this point in the history
contract: support more test scenarios
  • Loading branch information
webmaster128 authored May 23, 2023
2 parents 444eb7e + 9412b13 commit 9f64af5
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
58 changes: 58 additions & 0 deletions contracts/ibc-reflect/schema/packet_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
25 changes: 23 additions & 2 deletions contracts/ibc-reflect/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
load_account, load_item, may_load_account, range_accounts, remove_account, save_account,
Expand Down Expand Up @@ -246,6 +247,9 @@ 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),
PacketMsg::ReturnMsgs { msgs } => execute_return_msgs(msgs),
}
})()
.or_else(|e| {
Expand Down Expand Up @@ -310,6 +314,23 @@ fn receive_dispatch(
.add_attribute("action", "receive_dispatch"))
}

fn execute_panic() -> StdResult<IbcReceiveResponse> {
panic!("This page intentionally faulted");
}

fn execute_error(text: String) -> StdResult<IbcReceiveResponse> {
Err(StdError::generic_err(text))
}

fn execute_return_msgs(msgs: Vec<CosmosMsg>) -> StdResult<IbcReceiveResponse> {
let acknowledgement = to_binary(&AcknowledgementMsg::<ReturnMsgsResponse>::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(
Expand Down Expand Up @@ -578,7 +599,7 @@ mod tests {
assert_eq!(0, res.messages.len());
// acknowledgement is an error
let ack: AcknowledgementMsg<DispatchResponse> = 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]
Expand Down
7 changes: 7 additions & 0 deletions contracts/ibc-reflect/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub enum PacketMsg {
Dispatch { msgs: Vec<CosmosMsg> },
WhoAmI {},
Balances {},
Panic {},
ReturnErr { text: String },
ReturnMsgs { msgs: Vec<CosmosMsg> },
}

/// All acknowledgements are wrapped in `ContractResult`.
Expand All @@ -70,3 +73,7 @@ pub struct BalancesResponse {
pub account: String,
pub balances: Vec<Coin>,
}

/// This is the success response we send on ack for PacketMsg::ReturnMsgs.
/// Just acknowledge success or error
pub type ReturnMsgsResponse = ();
2 changes: 1 addition & 1 deletion contracts/ibc-reflect/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,5 +287,5 @@ fn handle_dispatch_packet() {
// acknowledgement is an error
let ack: AcknowledgementMsg<DispatchResponse> =
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`");
}

0 comments on commit 9f64af5

Please sign in to comment.