Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contract: support more test scenarios #1690

Merged
merged 7 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::{accounts, accounts_read, config, pending_channel, Config};

Expand Down Expand Up @@ -244,6 +245,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 @@ -308,6 +312,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 @@ -576,7 +597,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`");
}