Skip to content

Commit

Permalink
Merge pull request #1940 from CosmWasm/1649-ack-required
Browse files Browse the repository at this point in the history
Make ack required in `IbcReceiveResponse::new`
  • Loading branch information
chipshort authored Nov 8, 2023
2 parents 0294194 + 3bee728 commit edada3e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ and this project adheres to
- cosmwasm-std: Upgrade to `serde-json-wasm` 1.0. This means `u128` and `i128`
are now serialized as numbers instead of strings. Use `Uint128` and `Int128`
instead. ([#1939])
- cosmwasm-std: Add `ack` parameter to `IbcReceiveResponse::new` and remove
`IbcReceiveResponse::set_ack` ([#1940])
- cosmwasm-std: Make `BalanceResponse`, `AllBalanceResponse`,
`DelegationRewardsResponse`, `DelegatorReward`, `DelegatorValidatorsResponse`,
`PortIdResponse`, `ListChannelsResponse`, `ChannelResponse`,
Expand All @@ -51,6 +53,7 @@ and this project adheres to
[#1898]: https://github.com/CosmWasm/cosmwasm/pull/1898
[#1902]: https://github.com/CosmWasm/cosmwasm/pull/1902
[#1939]: https://github.com/CosmWasm/cosmwasm/pull/1939
[#1940]: https://github.com/CosmWasm/cosmwasm/pull/1940

### Removed

Expand Down
8 changes: 8 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ major releases of `cosmwasm`. Note that you can also view the
+const item: Item<Uint128> = Item::new("item");
```

- Replace all uses of `IbcReceiveResponse::set_ack` with calls to
`IbcReceiveResponse::new`:

```diff
- Ok(IbcReceiveResponse::new().set_ack(b"{}"))
+ Ok(IbcReceiveResponse::new(b"{}"))
```

## 1.4.x -> 1.5.0

- Update `cosmwasm-*` dependencies in Cargo.toml (skip the ones you don't use):
Expand Down
4 changes: 1 addition & 3 deletions contracts/ibc-reflect-send/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ pub fn ibc_packet_receive(
_env: Env,
_packet: IbcPacketReceiveMsg,
) -> Result<IbcReceiveResponse, Never> {
Ok(IbcReceiveResponse::new()
.set_ack(b"{}")
.add_attribute("action", "ibc_packet_ack"))
Ok(IbcReceiveResponse::new(b"{}").add_attribute("action", "ibc_packet_ack"))
}

#[entry_point]
Expand Down
17 changes: 5 additions & 12 deletions contracts/ibc-reflect/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ pub fn ibc_packet_receive(
// we try to capture all app-level errors and convert them into
// acknowledgement packets that contain an error code.
let acknowledgement = encode_ibc_error(format!("invalid packet: {e}"));
Ok(IbcReceiveResponse::new()
.set_ack(acknowledgement)
Ok(IbcReceiveResponse::new(acknowledgement)
.add_event(Event::new("ibc").add_attribute("packet", "receive")))
})
}
Expand All @@ -269,9 +268,7 @@ fn receive_who_am_i(deps: DepsMut, caller: String) -> StdResult<IbcReceiveRespon
};
let acknowledgement = to_json_binary(&AcknowledgementMsg::Ok(response))?;
// and we are golden
Ok(IbcReceiveResponse::new()
.set_ack(acknowledgement)
.add_attribute("action", "receive_who_am_i"))
Ok(IbcReceiveResponse::new(acknowledgement).add_attribute("action", "receive_who_am_i"))
}

// processes PacketMsg::Balances variant
Expand All @@ -284,9 +281,7 @@ fn receive_balances(deps: DepsMut, caller: String) -> StdResult<IbcReceiveRespon
};
let acknowledgement = to_json_binary(&AcknowledgementMsg::Ok(response))?;
// and we are golden
Ok(IbcReceiveResponse::new()
.set_ack(acknowledgement)
.add_attribute("action", "receive_balances"))
Ok(IbcReceiveResponse::new(acknowledgement).add_attribute("action", "receive_balances"))
}

// processes PacketMsg::Dispatch variant
Expand All @@ -307,8 +302,7 @@ fn receive_dispatch(
// we wrap it in a submessage to properly report errors
let msg = SubMsg::reply_on_error(wasm_msg, RECEIVE_DISPATCH_ID);

Ok(IbcReceiveResponse::new()
.set_ack(acknowledgement)
Ok(IbcReceiveResponse::new(acknowledgement)
.add_submessage(msg)
.add_attribute("action", "receive_dispatch"))
}
Expand All @@ -324,8 +318,7 @@ fn execute_error(text: String) -> StdResult<IbcReceiveResponse> {
fn execute_return_msgs(msgs: Vec<CosmosMsg>) -> StdResult<IbcReceiveResponse> {
let acknowledgement = to_json_binary(&AcknowledgementMsg::<ReturnMsgsResponse>::Ok(()))?;

Ok(IbcReceiveResponse::new()
.set_ack(acknowledgement)
Ok(IbcReceiveResponse::new(acknowledgement)
.add_messages(msgs)
.add_attribute("action", "receive_dispatch"))
}
Expand Down
35 changes: 17 additions & 18 deletions packages/std/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,25 +636,24 @@ impl<T> Default for IbcReceiveResponse<T> {
}

impl<T> IbcReceiveResponse<T> {
pub fn new() -> Self {
Self::default()
}

/// Set the acknowledgement for this response.
/// Create a new response with the given acknowledgement.
///
/// ## Examples
///
/// ```
/// use cosmwasm_std::{StdAck, IbcReceiveResponse};
///
/// fn make_response_with_ack() -> IbcReceiveResponse {
/// let ack = StdAck::success(b"\x01"); // 0x01 is a FungibleTokenPacketSuccess from ICS-20.
/// IbcReceiveResponse::new().set_ack(ack)
/// }
/// // 0x01 is a FungibleTokenPacketSuccess from ICS-20.
/// let resp: IbcReceiveResponse = IbcReceiveResponse::new(StdAck::success(b"\x01"));
/// assert_eq!(resp.acknowledgement, b"{\"result\":\"AQ==\"}");
/// ```
pub fn set_ack(mut self, ack: impl Into<Binary>) -> Self {
self.acknowledgement = ack.into();
self
pub fn new(ack: impl Into<Binary>) -> Self {
Self {
acknowledgement: ack.into(),
messages: vec![],
attributes: vec![],
events: vec![],
}
}

/// Add an attribute included in the main `wasm` event.
Expand Down Expand Up @@ -695,14 +694,14 @@ impl<T> IbcReceiveResponse<T> {
/// ## Examples
///
/// ```
/// use cosmwasm_std::{attr, IbcReceiveResponse};
/// use cosmwasm_std::{attr, IbcReceiveResponse, StdAck};
///
/// let attrs = vec![
/// ("action", "reaction"),
/// ("answer", "42"),
/// ("another", "attribute"),
/// ];
/// let res: IbcReceiveResponse = IbcReceiveResponse::new().add_attributes(attrs.clone());
/// let res: IbcReceiveResponse = IbcReceiveResponse::new(StdAck::success(b"\x01")).add_attributes(attrs.clone());
/// assert_eq!(res.attributes, attrs);
/// ```
pub fn add_attributes<A: Into<Attribute>>(
Expand All @@ -718,10 +717,10 @@ impl<T> IbcReceiveResponse<T> {
/// ## Examples
///
/// ```
/// use cosmwasm_std::{CosmosMsg, IbcReceiveResponse};
/// use cosmwasm_std::{CosmosMsg, IbcReceiveResponse, StdAck};
///
/// fn make_response_with_msgs(msgs: Vec<CosmosMsg>) -> IbcReceiveResponse {
/// IbcReceiveResponse::new().add_messages(msgs)
/// IbcReceiveResponse::new(StdAck::success(b"\x01")).add_messages(msgs)
/// }
/// ```
pub fn add_messages<M: Into<CosmosMsg<T>>>(self, msgs: impl IntoIterator<Item = M>) -> Self {
Expand All @@ -733,10 +732,10 @@ impl<T> IbcReceiveResponse<T> {
/// ## Examples
///
/// ```
/// use cosmwasm_std::{SubMsg, IbcReceiveResponse};
/// use cosmwasm_std::{SubMsg, StdAck, IbcReceiveResponse};
///
/// fn make_response_with_submsgs(msgs: Vec<SubMsg>) -> IbcReceiveResponse {
/// IbcReceiveResponse::new().add_submessages(msgs)
/// IbcReceiveResponse::new(StdAck::success(b"\x01")).add_submessages(msgs)
/// }
/// ```
pub fn add_submessages(mut self, msgs: impl IntoIterator<Item = SubMsg<T>>) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions packages/std/src/stdack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ impl StdAck {
///
/// let ack = StdAck::success(b"\x01"); // 0x01 is a FungibleTokenPacketSuccess from ICS-20.
///
/// let res: IbcReceiveResponse = IbcReceiveResponse::new().set_ack(ack.to_binary());
/// let res: IbcReceiveResponse = IbcReceiveResponse::new().set_ack(ack); // Does the same but consumes the instance
/// let res: IbcReceiveResponse = IbcReceiveResponse::new(ack.to_binary());
/// let res: IbcReceiveResponse = IbcReceiveResponse::new(ack); // Does the same but consumes the instance
/// ```
pub fn to_binary(&self) -> Binary {
// We need a non-failing StdAck -> Binary conversion to allow using StdAck in
Expand Down

0 comments on commit edada3e

Please sign in to comment.