Skip to content

Commit

Permalink
Adjust source chain callback type
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Apr 19, 2024
1 parent 2b44ac7 commit c0c32c3
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 121 deletions.
78 changes: 38 additions & 40 deletions contracts/ibc-callbacks/schema/ibc-callbacks.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"ibc_ack_callbacks": {
"type": "array",
"items": {
"$ref": "#/definitions/IbcPacketAckMsg"
"$ref": "#/definitions/IbcAckCallbackMsg"
}
},
"ibc_destination_callbacks": {
Expand All @@ -121,7 +121,7 @@
"ibc_timeout_callbacks": {
"type": "array",
"items": {
"$ref": "#/definitions/IbcPacketTimeoutMsg"
"$ref": "#/definitions/IbcTimeoutCallbackMsg"
}
}
},
Expand All @@ -135,6 +135,26 @@
"description": "Binary is a wrapper around Vec<u8> to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.\n\nThis is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>. See also <https://github.com/CosmWasm/cosmwasm/blob/main/docs/MESSAGE_TYPES.md>.",
"type": "string"
},
"IbcAckCallbackMsg": {
"type": "object",
"required": [
"acknowledgement",
"original_packet",
"relayer"
],
"properties": {
"acknowledgement": {
"$ref": "#/definitions/IbcAcknowledgement"
},
"original_packet": {
"$ref": "#/definitions/IbcPacket"
},
"relayer": {
"$ref": "#/definitions/Addr"
}
},
"additionalProperties": false
},
"IbcAcknowledgement": {
"type": "object",
"required": [
Expand Down Expand Up @@ -249,44 +269,6 @@
},
"additionalProperties": false
},
"IbcPacketAckMsg": {
"description": "The message that is passed into `ibc_packet_ack`",
"type": "object",
"required": [
"acknowledgement",
"original_packet",
"relayer"
],
"properties": {
"acknowledgement": {
"$ref": "#/definitions/IbcAcknowledgement"
},
"original_packet": {
"$ref": "#/definitions/IbcPacket"
},
"relayer": {
"$ref": "#/definitions/Addr"
}
},
"additionalProperties": false
},
"IbcPacketTimeoutMsg": {
"description": "The message that is passed into `ibc_packet_timeout`",
"type": "object",
"required": [
"packet",
"relayer"
],
"properties": {
"packet": {
"$ref": "#/definitions/IbcPacket"
},
"relayer": {
"$ref": "#/definitions/Addr"
}
},
"additionalProperties": false
},
"IbcTimeout": {
"description": "In IBC each package must set at least one type of timeout: the timestamp or the block height. Using this rather complex enum instead of two timeout fields we ensure that at least one timeout is set.",
"type": "object",
Expand Down Expand Up @@ -337,6 +319,22 @@
},
"additionalProperties": false
},
"IbcTimeoutCallbackMsg": {
"type": "object",
"required": [
"packet",
"relayer"
],
"properties": {
"packet": {
"$ref": "#/definitions/IbcPacket"
},
"relayer": {
"$ref": "#/definitions/Addr"
}
},
"additionalProperties": false
},
"Timestamp": {
"description": "A point in time in nanosecond precision.\n\nThis type can represent times from 1970-01-01T00:00:00Z to 2554-07-21T23:34:33Z.\n\n## Examples\n\n``` # use cosmwasm_std::Timestamp; let ts = Timestamp::from_nanos(1_000_000_202); assert_eq!(ts.nanos(), 1_000_000_202); assert_eq!(ts.seconds(), 1); assert_eq!(ts.subsec_nanos(), 202);\n\nlet ts = ts.plus_seconds(2); assert_eq!(ts.nanos(), 3_000_000_202); assert_eq!(ts.seconds(), 3); assert_eq!(ts.subsec_nanos(), 202); ```",
"allOf": [
Expand Down
78 changes: 38 additions & 40 deletions contracts/ibc-callbacks/schema/raw/response_to_callback_stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"ibc_ack_callbacks": {
"type": "array",
"items": {
"$ref": "#/definitions/IbcPacketAckMsg"
"$ref": "#/definitions/IbcAckCallbackMsg"
}
},
"ibc_destination_callbacks": {
Expand All @@ -24,7 +24,7 @@
"ibc_timeout_callbacks": {
"type": "array",
"items": {
"$ref": "#/definitions/IbcPacketTimeoutMsg"
"$ref": "#/definitions/IbcTimeoutCallbackMsg"
}
}
},
Expand All @@ -38,6 +38,26 @@
"description": "Binary is a wrapper around Vec<u8> to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.\n\nThis is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>. See also <https://github.com/CosmWasm/cosmwasm/blob/main/docs/MESSAGE_TYPES.md>.",
"type": "string"
},
"IbcAckCallbackMsg": {
"type": "object",
"required": [
"acknowledgement",
"original_packet",
"relayer"
],
"properties": {
"acknowledgement": {
"$ref": "#/definitions/IbcAcknowledgement"
},
"original_packet": {
"$ref": "#/definitions/IbcPacket"
},
"relayer": {
"$ref": "#/definitions/Addr"
}
},
"additionalProperties": false
},
"IbcAcknowledgement": {
"type": "object",
"required": [
Expand Down Expand Up @@ -152,44 +172,6 @@
},
"additionalProperties": false
},
"IbcPacketAckMsg": {
"description": "The message that is passed into `ibc_packet_ack`",
"type": "object",
"required": [
"acknowledgement",
"original_packet",
"relayer"
],
"properties": {
"acknowledgement": {
"$ref": "#/definitions/IbcAcknowledgement"
},
"original_packet": {
"$ref": "#/definitions/IbcPacket"
},
"relayer": {
"$ref": "#/definitions/Addr"
}
},
"additionalProperties": false
},
"IbcPacketTimeoutMsg": {
"description": "The message that is passed into `ibc_packet_timeout`",
"type": "object",
"required": [
"packet",
"relayer"
],
"properties": {
"packet": {
"$ref": "#/definitions/IbcPacket"
},
"relayer": {
"$ref": "#/definitions/Addr"
}
},
"additionalProperties": false
},
"IbcTimeout": {
"description": "In IBC each package must set at least one type of timeout: the timestamp or the block height. Using this rather complex enum instead of two timeout fields we ensure that at least one timeout is set.",
"type": "object",
Expand Down Expand Up @@ -240,6 +222,22 @@
},
"additionalProperties": false
},
"IbcTimeoutCallbackMsg": {
"type": "object",
"required": [
"packet",
"relayer"
],
"properties": {
"packet": {
"$ref": "#/definitions/IbcPacket"
},
"relayer": {
"$ref": "#/definitions/Addr"
}
},
"additionalProperties": false
},
"Timestamp": {
"description": "A point in time in nanosecond precision.\n\nThis type can represent times from 1970-01-01T00:00:00Z to 2554-07-21T23:34:33Z.\n\n## Examples\n\n``` # use cosmwasm_std::Timestamp; let ts = Timestamp::from_nanos(1_000_000_202); assert_eq!(ts.nanos(), 1_000_000_202); assert_eq!(ts.seconds(), 1); assert_eq!(ts.subsec_nanos(), 202);\n\nlet ts = ts.plus_seconds(2); assert_eq!(ts.nanos(), 3_000_000_202); assert_eq!(ts.seconds(), 3); assert_eq!(ts.subsec_nanos(), 202); ```",
"allOf": [
Expand Down
25 changes: 6 additions & 19 deletions contracts/ibc-callbacks/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use cosmwasm_std::{
entry_point, to_json_binary, to_json_string, Binary, Deps, DepsMut, Empty, Env,
IbcBasicResponse, IbcCallbackRequest, IbcDestinationChainCallbackMsg, IbcDstCallback, IbcMsg,
IbcPacketAckMsg, IbcPacketTimeoutMsg, IbcSourceChainCallbackMsg, IbcSrcCallback, IbcTimeout,
MessageInfo, Response, StdError, StdResult,
IbcSourceChainCallbackMsg, IbcSrcCallback, IbcTimeout, MessageInfo, Response, StdError,
StdResult,
};

use crate::msg::{CallbackType, ExecuteMsg, QueryMsg};
Expand Down Expand Up @@ -82,26 +82,13 @@ pub fn ibc_source_chain_callback(
let mut counts = load_stats(deps.storage)?;

match msg {
IbcSourceChainCallbackMsg::Acknowledgement {
acknowledgement,
original_packet,
relayer,
..
} => {
IbcSourceChainCallbackMsg::Acknowledgement(ack) => {
// save the ack
counts.ibc_ack_callbacks.push(IbcPacketAckMsg::new(
acknowledgement,
original_packet,
relayer,
));
counts.ibc_ack_callbacks.push(ack);
}
IbcSourceChainCallbackMsg::Timeout {
packet, relayer, ..
} => {
IbcSourceChainCallbackMsg::Timeout(timeout) => {
// save the timeout
counts
.ibc_timeout_callbacks
.push(IbcPacketTimeoutMsg::new(packet, relayer));
counts.ibc_timeout_callbacks.push(timeout);
}
}

Expand Down
8 changes: 4 additions & 4 deletions contracts/ibc-callbacks/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::any::type_name;

use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
from_json, storage_keys::to_length_prefixed, to_json_vec, IbcDestinationChainCallbackMsg,
IbcPacketAckMsg, IbcPacketTimeoutMsg, StdError, StdResult, Storage,
from_json, storage_keys::to_length_prefixed, to_json_vec, IbcAckCallbackMsg,
IbcDestinationChainCallbackMsg, IbcTimeoutCallbackMsg, StdError, StdResult, Storage,
};
use serde::{de::DeserializeOwned, Serialize};

Expand All @@ -13,8 +13,8 @@ pub const KEY_STATS: &[u8] = b"counts";
#[cw_serde]
#[derive(Default)]
pub struct CallbackStats {
pub ibc_ack_callbacks: Vec<IbcPacketAckMsg>,
pub ibc_timeout_callbacks: Vec<IbcPacketTimeoutMsg>,
pub ibc_ack_callbacks: Vec<IbcAckCallbackMsg>,
pub ibc_timeout_callbacks: Vec<IbcTimeoutCallbackMsg>,
pub ibc_destination_callbacks: Vec<IbcDestinationChainCallbackMsg>,
}

Expand Down
39 changes: 34 additions & 5 deletions packages/std/src/ibc/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,43 @@ pub struct IbcDstCallback {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum IbcSourceChainCallbackMsg {
#[non_exhaustive]
Acknowledgement {
Acknowledgement(IbcAckCallbackMsg),
Timeout(IbcTimeoutCallbackMsg),
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[non_exhaustive]
pub struct IbcAckCallbackMsg {
acknowledgement: IbcAcknowledgement,
original_packet: IbcPacket,
relayer: Addr,
}

impl IbcAckCallbackMsg {
pub fn new(
acknowledgement: IbcAcknowledgement,
original_packet: IbcPacket,
relayer: Addr,
},
#[non_exhaustive]
Timeout { packet: IbcPacket, relayer: Addr },
) -> Self {
Self {
acknowledgement,
original_packet,
relayer,
}
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[non_exhaustive]
pub struct IbcTimeoutCallbackMsg {
packet: IbcPacket,
relayer: Addr,
}

impl IbcTimeoutCallbackMsg {
pub fn new(packet: IbcPacket, relayer: Addr) -> Self {
Self { packet, relayer }
}
}

/// The message type of the IBC destination chain callback.
Expand Down
11 changes: 6 additions & 5 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ pub use crate::coins::Coins;
pub use crate::deps::{Deps, DepsMut, OwnedDeps};
pub use crate::ibc::IbcChannelOpenResponse;
pub use crate::ibc::{
Ibc3ChannelOpenResponse, IbcAcknowledgement, IbcBasicResponse, IbcCallbackRequest, IbcChannel,
IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcDestinationChainCallbackMsg,
IbcDstCallback, IbcEndpoint, IbcFullAcknowledgement, IbcMsg, IbcOrder, IbcPacket,
IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse,
IbcSourceChainCallbackMsg, IbcSrcCallback, IbcTimeout, IbcTimeoutBlock,
Ibc3ChannelOpenResponse, IbcAckCallbackMsg, IbcAcknowledgement, IbcBasicResponse,
IbcCallbackRequest, IbcChannel, IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg,
IbcDestinationChainCallbackMsg, IbcDstCallback, IbcEndpoint, IbcFullAcknowledgement, IbcMsg,
IbcOrder, IbcPacket, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg,
IbcReceiveResponse, IbcSourceChainCallbackMsg, IbcSrcCallback, IbcTimeout, IbcTimeoutBlock,
IbcTimeoutCallbackMsg,
};
#[cfg(feature = "iterator")]
pub use crate::iterator::{Order, Record};
Expand Down
22 changes: 14 additions & 8 deletions packages/vm/src/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,8 @@ mod tests {
mock_ibc_packet_ack, mock_ibc_packet_recv, mock_wasmd_attr,
};
use cosmwasm_std::{
Event, IbcAcknowledgement, IbcOrder, ReplyOn, SubMsgResponse, SubMsgResult,
Event, IbcAckCallbackMsg, IbcAcknowledgement, IbcOrder, IbcTimeoutCallbackMsg, ReplyOn,
SubMsgResponse, SubMsgResult,
};
const CONTRACT: &[u8] = include_bytes!("../testdata/ibc_reflect.wasm");
const IBC_CALLBACKS: &[u8] = include_bytes!("../testdata/ibc_callbacks.wasm");
Expand Down Expand Up @@ -1088,10 +1089,13 @@ mod tests {
}

// send ack callback
let ack = IbcAcknowledgement::new(br#"{}"#);
let msg = IbcSourceChainCallbackMsg::Acknowledgement(
mock_ibc_packet_ack(CHANNEL_ID, br#"{}"#, ack).unwrap(),
);
let ack = mock_ibc_packet_ack(CHANNEL_ID, br#"{}"#, IbcAcknowledgement::new(br#"{}"#))
.unwrap();
let msg = IbcSourceChainCallbackMsg::Acknowledgement(IbcAckCallbackMsg::new(
ack.acknowledgement,
ack.original_packet,
ack.relayer,
));
call_ibc_source_chain_callback::<_, _, _, Empty>(&mut instance, &mock_env(), &msg)
.unwrap()
.unwrap();
Expand All @@ -1106,9 +1110,11 @@ mod tests {
assert_eq!(0, stats.ibc_timeout_callbacks.len());

// send timeout callback
let msg = IbcSourceChainCallbackMsg::Timeout(
mock_ibc_packet_timeout(CHANNEL_ID, br#"{}"#).unwrap(),
);
let timeout = mock_ibc_packet_timeout(CHANNEL_ID, br#"{}"#).unwrap();
let msg = IbcSourceChainCallbackMsg::Timeout(IbcTimeoutCallbackMsg::new(
timeout.packet,
timeout.relayer,
));
call_ibc_source_chain_callback::<_, _, _, Empty>(&mut instance, &mock_env(), &msg)
.unwrap()
.unwrap();
Expand Down

0 comments on commit c0c32c3

Please sign in to comment.