Skip to content

Commit

Permalink
Use the correct logic for ibc channel open and connect
Browse files Browse the repository at this point in the history
  • Loading branch information
trinitys7 committed Sep 19, 2024
1 parent 79bd951 commit da560f3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 56 deletions.
11 changes: 0 additions & 11 deletions contracts/consumer/band-price-feed/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ impl RemotePriceFeedContract {
ctx: InstantiateCtx,
trading_pair: TradingPair,
client_id: String,
connection_id: String,
channel_id: String,
port_id: String,
oracle_script_id: Uint64,
ask_count: Uint64,
min_count: Uint64,
Expand All @@ -81,11 +78,6 @@ impl RemotePriceFeedContract {
ctx.deps.storage,
&Config {
client_id,
connection_id,
endpoint: IbcEndpoint {
port_id,
channel_id,
},
oracle_script_id,
ask_count,
min_count,
Expand Down Expand Up @@ -195,9 +187,6 @@ mod tests {
},
trading_pair,
"07-tendermint-0".to_string(),
"connection-0".to_string(),
"channel-0".to_string(),
"transfer".to_string(),
Uint64::new(1),
Uint64::new(10),
Uint64::new(50),
Expand Down
8 changes: 5 additions & 3 deletions contracts/consumer/band-price-feed/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use cosmwasm_std::StdError;
use cw_utils::PaymentError;
use mesh_apis::ibc::VersionError;
use thiserror::Error;

use mesh_price_feed::PriceKeeperError;
Expand All @@ -26,8 +25,11 @@ pub enum ContractError {
#[error("Request didn't suceess")]
RequestNotSuccess {},

#[error("{0}")]
IbcVersion(#[from] VersionError),
#[error("Only supports channel with ibc version bandchain-1, got {version}")]
InvalidIbcVersion { version: String },

#[error("Only supports unordered channel")]
OnlyUnorderedChannel {},

#[error("The provided IBC channel is not open")]
IbcChannelNotOpen,
Expand Down
78 changes: 40 additions & 38 deletions contracts/consumer/band-price-feed/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
use cosmwasm_std::entry_point;

use cosmwasm_std::{
from_json, Decimal, DepsMut, Env, Ibc3ChannelOpenResponse, IbcBasicResponse,
IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcPacket,
IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse, StdError,
Uint128,
from_json, Decimal, DepsMut, Env, Ibc3ChannelOpenResponse, IbcBasicResponse, IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcOrder, IbcPacket, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse, StdError, Uint128
};
use cw_band::{OracleResponsePacketData, Output, ResolveStatus};
use mesh_apis::ibc::{
Expand All @@ -16,10 +13,7 @@ use obi::OBIDecode;
use crate::contract::RemotePriceFeedContract;
use crate::error::ContractError;

/// This is the maximum version of the Mesh Security protocol that we support
const SUPPORTED_IBC_PROTOCOL_VERSION: &str = "0.1.0";
/// This is the minimum version that we are compatible with
const MIN_IBC_PROTOCOL_VERSION: &str = "0.1.0";
pub const IBC_APP_VERSION: &str = "bandchain-1";

#[cfg_attr(not(feature = "library"), entry_point)]
/// enforces ordering and versioning constraints
Expand All @@ -34,33 +28,27 @@ pub fn ibc_channel_open(
return Err(ContractError::IbcChannelAlreadyOpen);
}
// ensure we are called with OpenInit
let (channel, counterparty_version) = match msg {
IbcChannelOpenMsg::OpenInit { .. } => return Err(ContractError::IbcOpenInitDisallowed),
IbcChannelOpenMsg::OpenTry {
channel,
counterparty_version,
} => (channel, counterparty_version),
};

// verify the ordering is correct
validate_channel_order(&channel.order)?;

// assert expected endpoint
let config = contract.config.load(deps.storage)?;
if config.connection_id != channel.connection_id
|| config.endpoint.port_id != channel.counterparty_endpoint.port_id
{
// FIXME: do we need a better error here?
return Err(ContractError::Unauthorized);
let channel = msg.channel();
let counterparty_version = msg.counterparty_version();

if channel.version != IBC_APP_VERSION {
return Err(ContractError::InvalidIbcVersion {
version: channel.version.clone(),
});
}
if let Some(version) = counterparty_version {
if version != IBC_APP_VERSION {
return Err(ContractError::InvalidIbcVersion {
version: version.to_string(),
});
}
}
if channel.order != IbcOrder::Unordered {
return Err(ContractError::OnlyUnorderedChannel {});
}

// we handshake with the counterparty version, it must not be empty
let v: ProtocolVersion = from_json(counterparty_version.as_bytes())?;
// if we can build a response to this, then it is compatible. And we use the highest version there
let version = v.build_response(SUPPORTED_IBC_PROTOCOL_VERSION, MIN_IBC_PROTOCOL_VERSION)?;

let response = Ibc3ChannelOpenResponse {
version: version.to_string()?,
version: channel.version.clone(),
};
Ok(Some(response))
}
Expand All @@ -78,11 +66,25 @@ pub fn ibc_channel_connect(
if contract.channel.may_load(deps.storage)?.is_some() {
return Err(ContractError::IbcChannelAlreadyOpen);
}
// ensure we are called with OpenConfirm
let channel = match msg {
IbcChannelConnectMsg::OpenConfirm { channel } => channel,
IbcChannelConnectMsg::OpenAck { .. } => return Err(ContractError::IbcOpenInitDisallowed),
};

let channel = msg.channel();
let counterparty_version = msg.counterparty_version();

if channel.version != IBC_APP_VERSION {
return Err(ContractError::InvalidIbcVersion {
version: channel.version.clone(),
});
}
if let Some(version) = counterparty_version {
if version != IBC_APP_VERSION {
return Err(ContractError::InvalidIbcVersion {
version: version.to_string(),
});
}
}
if channel.order != IbcOrder::Unordered {
return Err(ContractError::OnlyUnorderedChannel {});
}

// Version negotiation over, we can only store the channel
let contract = RemotePriceFeedContract::new();
Expand All @@ -97,7 +99,7 @@ pub fn ibc_channel_close(
_env: Env,
_msg: IbcChannelCloseMsg,
) -> Result<IbcBasicResponse, ContractError> {
todo!();
unimplemented!();
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down
5 changes: 1 addition & 4 deletions contracts/consumer/band-price-feed/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Coin, IbcEndpoint, Uint64};
use cosmwasm_std::{Coin, Uint64};

#[cw_serde]
pub struct Config {
// A unique ID for the oracle request
pub client_id: String,
pub connection_id: String,
// Endpoint to validate when open channel
pub endpoint: IbcEndpoint,
// The oracle script ID to query
pub oracle_script_id: Uint64,
// The number of validators that are requested to respond
Expand Down

0 comments on commit da560f3

Please sign in to comment.