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

Allow creating IbcReceiveResponse without ack #1892

Merged
merged 4 commits into from
Dec 13, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ and this project adheres to
([#1967])
- cosmwasm-std: Replace `MockApi` with bech32 implementation. ([#1914])
- cosmwasm-vm: Replace `MockApi` with bech32 implementation. ([#1914])
- cosmwasm-std: Make `IbcReceiveResponse::acknowledgement` optional and add
`IbcReceiveResponse::without_ack` constructor. ([#1892])

[#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874
[#1876]: https://github.com/CosmWasm/cosmwasm/pull/1876
[#1879]: https://github.com/CosmWasm/cosmwasm/pull/1879
[#1883]: https://github.com/CosmWasm/cosmwasm/pull/1883
[#1884]: https://github.com/CosmWasm/cosmwasm/pull/1884
[#1892]: https://github.com/CosmWasm/cosmwasm/pull/1892
[#1898]: https://github.com/CosmWasm/cosmwasm/pull/1898
[#1902]: https://github.com/CosmWasm/cosmwasm/pull/1902
[#1914]: https://github.com/CosmWasm/cosmwasm/pull/1914
Expand Down
4 changes: 2 additions & 2 deletions contracts/ibc-reflect-send/src/ibc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{
entry_point, from_json, to_json_binary, DepsMut, Env, Ibc3ChannelOpenResponse,
entry_point, from_json, to_json_binary, Binary, DepsMut, Env, Ibc3ChannelOpenResponse,
IbcBasicResponse, IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg,
IbcChannelOpenResponse, IbcMsg, IbcOrder, IbcPacketAckMsg, IbcPacketReceiveMsg,
IbcPacketTimeoutMsg, IbcReceiveResponse, Never, StdError, StdResult,
Expand Down Expand Up @@ -101,7 +101,7 @@ pub fn ibc_packet_receive(
_env: Env,
_packet: IbcPacketReceiveMsg,
) -> Result<IbcReceiveResponse, Never> {
Ok(IbcReceiveResponse::new(b"{}").add_attribute("action", "ibc_packet_ack"))
Ok(IbcReceiveResponse::new(Binary::from(b"{}")).add_attribute("action", "ibc_packet_ack"))
}

#[entry_point]
Expand Down
8 changes: 5 additions & 3 deletions contracts/ibc-reflect/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,8 @@ mod tests {
res.events[0]
);
// acknowledgement is an error
let ack: AcknowledgementMsg<DispatchResponse> = from_json(res.acknowledgement).unwrap();
let ack: AcknowledgementMsg<DispatchResponse> =
from_json(res.acknowledgement.unwrap()).unwrap();
assert_eq!(
ack.unwrap_err(),
"invalid packet: account channel-123 not found"
Expand All @@ -560,7 +561,7 @@ mod tests {
let res = ibc_packet_receive(deps.as_mut(), mock_env(), msg).unwrap();

// assert app-level success
let ack: AcknowledgementMsg<()> = from_json(res.acknowledgement).unwrap();
let ack: AcknowledgementMsg<()> = from_json(res.acknowledgement.unwrap()).unwrap();
ack.unwrap();

// and we dispatch the BankMsg via submessage
Expand Down Expand Up @@ -597,7 +598,8 @@ mod tests {
// we didn't dispatch anything
assert_eq!(0, res.messages.len());
// acknowledgement is an error
let ack: AcknowledgementMsg<DispatchResponse> = from_json(res.acknowledgement).unwrap();
let ack: AcknowledgementMsg<DispatchResponse> =
from_json(res.acknowledgement.unwrap()).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`, `panic`, `return_err`, `return_msgs`");
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/ibc-reflect/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn handle_dispatch_packet() {
);
// acknowledgement is an error
let ack: AcknowledgementMsg<DispatchResponse> =
from_slice(&res.acknowledgement, DESERIALIZATION_LIMIT).unwrap();
from_slice(&res.acknowledgement.unwrap(), DESERIALIZATION_LIMIT).unwrap();
assert_eq!(
ack.unwrap_err(),
"invalid packet: account channel-123 not found"
Expand All @@ -255,7 +255,7 @@ fn handle_dispatch_packet() {

// assert app-level success
let ack: AcknowledgementMsg<DispatchResponse> =
from_slice(&res.acknowledgement, DESERIALIZATION_LIMIT).unwrap();
from_slice(&res.acknowledgement.unwrap(), DESERIALIZATION_LIMIT).unwrap();
ack.unwrap();

// and we dispatch the BankMsg
Expand Down Expand Up @@ -293,6 +293,6 @@ fn handle_dispatch_packet() {
assert_eq!(0, res.messages.len());
// acknowledgement is an error
let ack: AcknowledgementMsg<DispatchResponse> =
from_slice(&res.acknowledgement, DESERIALIZATION_LIMIT).unwrap();
from_slice(&res.acknowledgement.unwrap(), 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`, `panic`, `return_err`, `return_msgs`");
}
22 changes: 18 additions & 4 deletions packages/std/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,9 @@ impl<T> IbcBasicResponse<T> {
#[non_exhaustive]
pub struct IbcReceiveResponse<T = Empty> {
/// The bytes we return to the contract that sent the packet.
/// This may represent a success or error of execution
pub acknowledgement: Binary,
/// This may represent a success or error of execution.
/// In case of `None`, no acknowledgement is written.
pub acknowledgement: Option<Binary>,
/// Optional list of messages to pass. These will be executed in order.
/// If the ReplyOn member is set, they will invoke this contract's `reply` entry point
/// after execution. Otherwise, they act like "fire and forget".
Expand Down Expand Up @@ -633,11 +634,24 @@ impl<T> IbcReceiveResponse<T> {
///
/// // 0x01 is a FungibleTokenPacketSuccess from ICS-20.
/// let resp: IbcReceiveResponse = IbcReceiveResponse::new(StdAck::success(b"\x01"));
/// assert_eq!(resp.acknowledgement, b"{\"result\":\"AQ==\"}");
/// assert_eq!(resp.acknowledgement.unwrap(), b"{\"result\":\"AQ==\"}");
/// ```
pub fn new(ack: impl Into<Binary>) -> Self {
Self {
acknowledgement: ack.into(),
acknowledgement: Some(ack.into()),
messages: vec![],
attributes: vec![],
events: vec![],
}
}

/// Creates a new response without an acknowledgement.
///
/// This allows you to send the acknowledgement asynchronously later using [`IbcMsg::WriteAcknowledgement`].
/// If you want to send the acknowledgement immediately, use [`IbcReceiveResponse::new`].
pub fn without_ack() -> Self {
Self {
acknowledgement: None,
messages: vec![],
attributes: vec![],
events: vec![],
Expand Down