|
| 1 | +#[cfg(not(feature = "library"))] |
| 2 | +use cosmwasm_std::entry_point; |
| 3 | + |
| 4 | +use cosmwasm_std::{ |
| 5 | + from_slice, to_binary, DepsMut, Env, Event, Ibc3ChannelOpenResponse, IbcBasicResponse, |
| 6 | + IbcChannel, IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, |
| 7 | + IbcChannelOpenResponse, IbcMsg, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, |
| 8 | + IbcReceiveResponse, IbcTimeout, |
| 9 | +}; |
| 10 | +use cw_storage_plus::Item; |
| 11 | + |
| 12 | +use mesh_apis::ibc::{validate_channel_order, AckWrapper, ConsumerPacket, ProtocolVersion}; |
| 13 | +use sylvia::types::ExecCtx; |
| 14 | + |
| 15 | +use crate::error::ContractError; |
| 16 | + |
| 17 | +const PROTOCOL_NAME: &str = "mesh-security-price-feed"; |
| 18 | +/// This is the maximum version of the price feed protocol that we support |
| 19 | +const SUPPORTED_IBC_PROTOCOL_VERSION: &str = "0.1.0"; |
| 20 | +/// This is the minimum version that we are compatible with |
| 21 | +const MIN_IBC_PROTOCOL_VERSION: &str = "0.1.0"; |
| 22 | + |
| 23 | +// IBC specific state |
| 24 | +pub const IBC_CHANNEL: Item<IbcChannel> = Item::new("ibc_channel"); |
| 25 | + |
| 26 | +const TIMEOUT: u64 = 60 * 60; |
| 27 | + |
| 28 | +pub fn packet_timeout(env: &Env) -> IbcTimeout { |
| 29 | + let timeout = env.block.time.plus_seconds(TIMEOUT); |
| 30 | + IbcTimeout::with_timestamp(timeout) |
| 31 | +} |
| 32 | + |
| 33 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 34 | +/// enforces ordering and versioning constraints |
| 35 | +pub fn ibc_channel_open( |
| 36 | + deps: DepsMut, |
| 37 | + _env: Env, |
| 38 | + msg: IbcChannelOpenMsg, |
| 39 | +) -> Result<IbcChannelOpenResponse, ContractError> { |
| 40 | + // ensure we have no channel yet |
| 41 | + if IBC_CHANNEL.may_load(deps.storage)?.is_some() { |
| 42 | + return Err(ContractError::IbcChannelAlreadyOpen); |
| 43 | + } |
| 44 | + // ensure we are called with OpenInit |
| 45 | + let channel = match msg { |
| 46 | + IbcChannelOpenMsg::OpenInit { channel } => channel, |
| 47 | + IbcChannelOpenMsg::OpenTry { .. } => return Err(ContractError::IbcOpenTryDisallowed), |
| 48 | + }; |
| 49 | + |
| 50 | + // verify the ordering is correct |
| 51 | + validate_channel_order(&channel.order)?; |
| 52 | + |
| 53 | + // Check the version. If provided, ensure it is compatible. |
| 54 | + // If not provided, use our most recent version. |
| 55 | + let version = if channel.version.is_empty() { |
| 56 | + ProtocolVersion { |
| 57 | + protocol: PROTOCOL_NAME.to_string(), |
| 58 | + version: SUPPORTED_IBC_PROTOCOL_VERSION.to_string(), |
| 59 | + } |
| 60 | + } else { |
| 61 | + let v: ProtocolVersion = from_slice(channel.version.as_bytes())?; |
| 62 | + // if we can build a response to this, then it is compatible. And we use the highest version there |
| 63 | + v.build_response(SUPPORTED_IBC_PROTOCOL_VERSION, MIN_IBC_PROTOCOL_VERSION)? |
| 64 | + }; |
| 65 | + |
| 66 | + let response = Ibc3ChannelOpenResponse { |
| 67 | + version: version.to_string()?, |
| 68 | + }; |
| 69 | + Ok(Some(response)) |
| 70 | +} |
| 71 | + |
| 72 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 73 | +/// once it's established, we store data |
| 74 | +pub fn ibc_channel_connect( |
| 75 | + deps: DepsMut, |
| 76 | + env: Env, |
| 77 | + msg: IbcChannelConnectMsg, |
| 78 | +) -> Result<IbcBasicResponse, ContractError> { |
| 79 | + // ensure we have no channel yet |
| 80 | + if IBC_CHANNEL.may_load(deps.storage)?.is_some() { |
| 81 | + return Err(ContractError::IbcChannelAlreadyOpen); |
| 82 | + } |
| 83 | + // ensure we are called with OpenAck |
| 84 | + let (channel, counterparty_version) = match msg { |
| 85 | + IbcChannelConnectMsg::OpenAck { |
| 86 | + channel, |
| 87 | + counterparty_version, |
| 88 | + } => (channel, counterparty_version), |
| 89 | + IbcChannelConnectMsg::OpenConfirm { .. } => { |
| 90 | + return Err(ContractError::IbcOpenTryDisallowed) |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + // Ensure the counterparty responded with a version we support. |
| 95 | + // Note: here, we error if it is higher than what we proposed originally |
| 96 | + let v: ProtocolVersion = from_slice(counterparty_version.as_bytes())?; |
| 97 | + v.verify_compatibility(SUPPORTED_IBC_PROTOCOL_VERSION, MIN_IBC_PROTOCOL_VERSION)?; |
| 98 | + |
| 99 | + todo!("store the channel in subscriptions"); |
| 100 | + |
| 101 | + Ok(IbcBasicResponse::new()) |
| 102 | +} |
| 103 | + |
| 104 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 105 | +pub fn ibc_channel_close( |
| 106 | + _deps: DepsMut, |
| 107 | + _env: Env, |
| 108 | + _msg: IbcChannelCloseMsg, |
| 109 | +) -> Result<IbcBasicResponse, ContractError> { |
| 110 | + todo!("remove subscription"); |
| 111 | +} |
| 112 | + |
| 113 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 114 | +pub fn ibc_packet_receive( |
| 115 | + deps: DepsMut, |
| 116 | + _env: Env, |
| 117 | + msg: IbcPacketReceiveMsg, |
| 118 | +) -> Result<IbcReceiveResponse, ContractError> { |
| 119 | + // this contract only sends out update packets over IBC - it's not meant to receive any |
| 120 | + Err(ContractError::IbcPacketRecvDisallowed) |
| 121 | +} |
| 122 | + |
| 123 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 124 | +/// We get ACKs on sync state without much to do. |
| 125 | +/// If it succeeded, take no action. If it errored, we can't do anything else and let it go. |
| 126 | +/// We just log the error cases so they can be detected. |
| 127 | +pub fn ibc_packet_ack( |
| 128 | + _deps: DepsMut, |
| 129 | + _env: Env, |
| 130 | + msg: IbcPacketAckMsg, |
| 131 | +) -> Result<IbcBasicResponse, ContractError> { |
| 132 | + let ack: AckWrapper = from_slice(&msg.acknowledgement.data)?; |
| 133 | + let mut res = IbcBasicResponse::new(); |
| 134 | + match ack { |
| 135 | + AckWrapper::Result(_) => {} |
| 136 | + AckWrapper::Error(e) => { |
| 137 | + // The wasmd framework will label this with the contract_addr, which helps us find the port and issue. |
| 138 | + // Provide info to find the actual packet. |
| 139 | + let event = Event::new("mesh_price_feed_ibc_error") |
| 140 | + .add_attribute("error", e) |
| 141 | + .add_attribute("channel", msg.original_packet.src.channel_id) |
| 142 | + .add_attribute("sequence", msg.original_packet.sequence.to_string()); |
| 143 | + res = res.add_event(event); |
| 144 | + } |
| 145 | + } |
| 146 | + Ok(res) |
| 147 | +} |
| 148 | + |
| 149 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 150 | +/// The most we can do here is retry the packet, hoping it will eventually arrive. |
| 151 | +pub fn ibc_packet_timeout( |
| 152 | + _deps: DepsMut, |
| 153 | + env: Env, |
| 154 | + msg: IbcPacketTimeoutMsg, |
| 155 | +) -> Result<IbcBasicResponse, ContractError> { |
| 156 | + // Play it again, Sam. |
| 157 | + let msg = IbcMsg::SendPacket { |
| 158 | + channel_id: msg.packet.src.channel_id, |
| 159 | + data: msg.packet.data, |
| 160 | + timeout: packet_timeout(&env), |
| 161 | + }; |
| 162 | + Ok(IbcBasicResponse::new().add_message(msg)) |
| 163 | +} |
| 164 | + |
| 165 | +pub(crate) fn make_ibc_packet( |
| 166 | + ctx: &mut ExecCtx, |
| 167 | + packet: ConsumerPacket, |
| 168 | +) -> Result<IbcMsg, ContractError> { |
| 169 | + let channel = IBC_CHANNEL.load(ctx.deps.storage)?; |
| 170 | + Ok(IbcMsg::SendPacket { |
| 171 | + channel_id: channel.endpoint.channel_id, |
| 172 | + data: to_binary(&packet)?, |
| 173 | + timeout: packet_timeout(&ctx.env), |
| 174 | + }) |
| 175 | +} |
0 commit comments