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

fix(ibc-union): normalize event name, extend creation with counterparty chain-id #3488

Merged
merged 1 commit into from
Jan 12, 2025
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
9 changes: 6 additions & 3 deletions cosmwasm/ibc-union/app/ucs00-pingpong/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,18 @@ pub fn execute(
ExecuteMsg::IbcUnionMsg(IbcUnionMsg::OnRecvPacket { packet, .. }) => {
let ping_packet = UCS00PingPong::decode(&packet.data)?;
let config = CONFIG.load(deps.storage)?;
let msg =
ping_packet.reverse(&config, env.block.time.nanos(), packet.destination_channel);
let msg = ping_packet.reverse(
&config,
env.block.time.nanos(),
packet.destination_channel_id,
);

Ok(Response::default()
.add_message(wasm_execute(
&config.ibc_host,
&ibc_union_msg::msg::ExecuteMsg::WriteAcknowledgement(
MsgWriteAcknowledgement {
channel_id: packet.destination_channel,
channel_id: packet.destination_channel_id,
packet,
acknowledgement: ack_success().into(),
},
Expand Down
18 changes: 9 additions & 9 deletions cosmwasm/ibc-union/app/ucs03-zkgm/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ fn timeout_internal(
match instruction.opcode {
OP_FUNGIBLE_ASSET_ORDER => {
let order = FungibleAssetOrder::abi_decode_params(&instruction.operand, true)?;
refund(deps, packet.source_channel, order)
refund(deps, packet.source_channel_id, order)
}
_ => {
return Err(ContractError::UnknownOpcode {
Expand Down Expand Up @@ -366,7 +366,7 @@ fn acknowledge_fungible_asset_order(
let base_denom = String::from_utf8(order.base_token.to_vec())
.map_err(|_| ContractError::InvalidBaseToken)?;
// TODO: handle forward path
if order.base_token_path == packet.source_channel.try_into().unwrap() {
if order.base_token_path == packet.source_channel_id.try_into().unwrap() {
messages.push(
TokenFactoryMsg::MintTokens {
denom: base_denom,
Expand All @@ -393,7 +393,7 @@ fn acknowledge_fungible_asset_order(
Ok(Response::new().add_messages(messages))
}
// Transfer failed, refund
None => refund(deps, packet.source_channel, order),
None => refund(deps, packet.source_channel_id, order),
}
}

Expand Down Expand Up @@ -503,7 +503,7 @@ fn execute_fungible_asset_order(
}
let wrapped_denom = predict_wrapped_denom(
path,
packet.destination_channel,
packet.destination_channel_id,
Bytes::from(order.base_token.to_vec()),
);
let quote_amount =
Expand Down Expand Up @@ -551,7 +551,7 @@ fn execute_fungible_asset_order(
TOKEN_ORIGIN.save(
deps.storage,
subdenom.clone(),
&Uint256::from_u128(packet.destination_channel as _),
&Uint256::from_u128(packet.destination_channel_id as _),
)?;
};
messages.push(
Expand All @@ -573,12 +573,12 @@ fn execute_fungible_asset_order(
);
}
} else {
if order.base_token_path == packet.source_channel.try_into().unwrap() {
if order.base_token_path == packet.source_channel_id.try_into().unwrap() {
let quote_token = String::from_utf8(order.quote_token.to_vec())
.map_err(|_| ContractError::InvalidQuoteToken)?;
CHANNEL_BALANCE.update(
deps.storage,
(packet.destination_channel, quote_token.clone()),
(packet.destination_channel_id, quote_token.clone()),
|balance| match balance {
Some(value) => value
.checked_sub(quote_amount.into())
Expand Down Expand Up @@ -657,7 +657,7 @@ pub fn reply(
&ibc_host,
&ibc_union_msg::msg::ExecuteMsg::WriteAcknowledgement(
MsgWriteAcknowledgement {
channel_id: packet.destination_channel,
channel_id: packet.destination_channel_id,
packet,
acknowledgement: zkgm_ack.into(),
},
Expand All @@ -682,7 +682,7 @@ pub fn reply(
&ibc_host,
&ibc_union_msg::msg::ExecuteMsg::WriteAcknowledgement(
MsgWriteAcknowledgement {
channel_id: packet.destination_channel,
channel_id: packet.destination_channel_id,
packet,
acknowledgement: zkgm_ack.into(),
},
Expand Down
13 changes: 11 additions & 2 deletions cosmwasm/ibc-union/core/light-client-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cosmwasm_std::{
};
use cw_storage_plus::{Item, Map};
use ibc_union_msg::lightclient::{
MisbehaviourResponse, QueryMsg, Status, VerifyClientMessageUpdate,
MisbehaviourResponse, QueryMsg, Status, VerifyClientMessageUpdate, VerifyCreationResponse,
};
use msg::InstantiateMsg;
use state::IBC_HOST;
Expand Down Expand Up @@ -205,6 +205,9 @@ pub trait IbcClient: Sized {
/// Get the height
fn get_latest_height(client_state: &Self::ClientState) -> u64;

/// Get the tracked (counterparty) chain id.
fn get_counterparty_chain_id(client_state: &Self::ClientState) -> String;

/// Get the status of the client
fn status(client_state: &Self::ClientState) -> Status;

Expand Down Expand Up @@ -276,7 +279,13 @@ pub fn query<T: IbcClient>(
.map_err(|e| IbcClientError::Decode(DecodeError::ConsensusState(e)))?;

T::verify_creation(&client_state, &consensus_state)?;
to_json_binary(&T::get_latest_height(&client_state)).map_err(Into::into)

let response = VerifyCreationResponse {
latest_height: T::get_latest_height(&client_state),
counterparty_chain_id: T::get_counterparty_chain_id(&client_state),
};

to_json_binary(&response).map_err(Into::into)
}
QueryMsg::VerifyMembership {
client_id,
Expand Down
7 changes: 7 additions & 0 deletions cosmwasm/ibc-union/core/msg/src/lightclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ pub struct MisbehaviourResponse {
pub client_state: Bytes,
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub struct VerifyCreationResponse {
pub latest_height: u64,
pub counterparty_chain_id: String,
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub enum QueryMsg {
Expand Down
41 changes: 24 additions & 17 deletions cosmwasm/ibc-union/core/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use cosmwasm_std::{
use cw_storage_plus::Item;
use ibc_solidity::{Channel, ChannelState, Connection, ConnectionState, Packet};
use ibc_union_msg::{
lightclient::{QueryMsg as LightClientQuery, Status, VerifyClientMessageUpdate},
lightclient::{
QueryMsg as LightClientQuery, Status, VerifyClientMessageUpdate, VerifyCreationResponse,
},
module::{ExecuteMsg as ModuleMsg, IbcUnionMsg},
msg::{
ExecuteMsg, InitMsg, MsgBatchAcks, MsgBatchSend, MsgChannelCloseConfirm,
Expand Down Expand Up @@ -85,6 +87,7 @@ pub mod events {
pub const ACKNOWLEDGEMENT: &str = "acknowledgement";
pub const CLIENT_TYPE: &str = "client_type";
pub const CLIENT_ADDRESS: &str = "client_address";
pub const COUNTERPARTY_CHAIN_ID: &str = "counterparty_chain_id";
pub const COUNTERPARTY_CLIENT_ID: &str = "counterparty_client_id";
pub const COUNTERPARTY_CONNECTION_ID: &str = "counterparty_connection_id";
pub const PORT_ID: &str = "port_id";
Expand Down Expand Up @@ -559,8 +562,8 @@ fn timeout_packet(
proof_height: u64,
relayer: Addr,
) -> ContractResult {
let source_channel = packet.source_channel;
let destination_channel = packet.destination_channel;
let source_channel = packet.source_channel_id;
let destination_channel = packet.destination_channel_id;
let channel = ensure_channel_state(deps.as_ref(), source_channel)?;
let connection = ensure_connection_state(deps.as_ref(), channel.connection_id)?;

Expand Down Expand Up @@ -629,8 +632,8 @@ fn acknowledge_packet(
) -> ContractResult {
let first = packets.first().ok_or(ContractError::NotEnoughPackets)?;

let source_channel = first.source_channel;
let destination_channel = first.destination_channel;
let source_channel = first.source_channel_id;
let destination_channel = first.destination_channel_id;

let channel = ensure_channel_state(deps.as_ref(), source_channel)?;
let connection = ensure_connection_state(deps.as_ref(), channel.connection_id)?;
Expand Down Expand Up @@ -745,7 +748,7 @@ fn create_client(
let client_id = next_client_id(deps.branch())?;
CLIENT_TYPES.save(deps.storage, client_id, &client_type)?;
CLIENT_IMPLS.save(deps.storage, client_id, &client_impl)?;
let latest_height = deps.querier.query_wasm_smart(
let verify_creation_response = deps.querier.query_wasm_smart::<VerifyCreationResponse>(
&client_impl,
&LightClientQuery::VerifyCreation {
client_id,
Expand All @@ -756,7 +759,7 @@ fn create_client(
CLIENT_STATES.save(deps.storage, client_id, &client_state_bytes.to_vec().into())?;
CLIENT_CONSENSUS_STATES.save(
deps.storage,
(client_id, latest_height),
(client_id, verify_creation_response.latest_height),
&consensus_state_bytes.to_vec().into(),
)?;
store_commit(
Expand All @@ -768,7 +771,7 @@ fn create_client(
deps,
&ConsensusStatePath {
client_id,
height: latest_height,
height: verify_creation_response.latest_height,
}
.key(),
&commit(consensus_state_bytes),
Expand All @@ -777,6 +780,10 @@ fn create_client(
Response::new().add_event(Event::new(events::client::CREATE).add_attributes([
(events::attribute::CLIENT_TYPE, client_type),
(events::attribute::CLIENT_ID, client_id.to_string()),
(
events::attribute::COUNTERPARTY_CHAIN_ID,
verify_creation_response.counterparty_chain_id,
),
])),
)
}
Expand Down Expand Up @@ -1396,8 +1403,8 @@ fn process_receive(
intent: bool,
) -> Result<Response, ContractError> {
let first = packets.first().ok_or(ContractError::NotEnoughPackets)?;
let source_channel = first.source_channel;
let destination_channel = first.destination_channel;
let source_channel = first.source_channel_id;
let destination_channel = first.destination_channel_id;

let channel = ensure_channel_state(deps.as_ref(), destination_channel)?;
let connection = ensure_connection_state(deps.as_ref(), channel.connection_id)?;
Expand Down Expand Up @@ -1562,7 +1569,7 @@ fn write_acknowledgement(
fn send_packet(
mut deps: DepsMut,
sender: Addr,
source_channel: u32,
source_channel_id: u32,
timeout_height: u64,
timeout_timestamp: u64,
data: Vec<u8>,
Expand All @@ -1571,26 +1578,26 @@ fn send_packet(
return Err(ContractError::TimeoutMustBeSet);
}

let port_id = CHANNEL_OWNER.load(deps.storage, source_channel)?;
let port_id = CHANNEL_OWNER.load(deps.storage, source_channel_id)?;
if port_id != sender {
return Err(ContractError::Unauthorized {
channel_id: source_channel,
channel_id: source_channel_id,
owner: port_id,
caller: sender,
});
}

let channel = ensure_channel_state(deps.as_ref(), source_channel)?;
let channel = ensure_channel_state(deps.as_ref(), source_channel_id)?;
let packet = Packet {
source_channel,
destination_channel: channel.counterparty_channel_id,
source_channel_id,
destination_channel_id: channel.counterparty_channel_id,
data: data.into(),
timeout_height,
timeout_timestamp,
};

let commitment_key = BatchPacketsPath {
channel_id: source_channel,
channel_id: source_channel_id,
batch_hash: commit_packet(&packet),
}
.key();
Expand Down
4 changes: 4 additions & 0 deletions cosmwasm/ibc-union/light-clients/arbitrum/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ impl IbcClient for ArbitrumLightClient {
fn get_latest_height(client_state: &Self::ClientState) -> u64 {
client_state.l1_latest_slot
}

fn get_counterparty_chain_id(client_state: &Self::ClientState) -> String {
client_state.chain_id.to_string()
}
}

// #[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions cosmwasm/ibc-union/light-clients/berachain/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ impl IbcClient for BerachainLightClient {
client_state.latest_height
}

fn get_counterparty_chain_id(client_state: &Self::ClientState) -> String {
client_state.chain_id.to_string()
}

fn status(_client_state: &Self::ClientState) -> Status {
// FIXME: expose the ctx to this call to allow threading this call to L1
// client. generally, we want to thread if a client is an L2 so always
Expand Down
4 changes: 4 additions & 0 deletions cosmwasm/ibc-union/light-clients/cometbls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ impl<T: ZkpVerifier> ibc_union_light_client::IbcClient for CometblsLightClient<T
client_state.latest_height.height()
}

fn get_counterparty_chain_id(client_state: &Self::ClientState) -> String {
client_state.chain_id.clone().into_string()
}

// TODO(aeryz): pass ctx
fn status(client_state: &Self::ClientState) -> ibc_union_msg::lightclient::Status {
if client_state.frozen_height.height() != 0 {
Expand Down
4 changes: 4 additions & 0 deletions cosmwasm/ibc-union/light-clients/ethereum/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ impl ibc_union_light_client::IbcClient for EthereumLightClient {
client_state.latest_height
}

fn get_counterparty_chain_id(client_state: &Self::ClientState) -> String {
client_state.chain_id.to_string()
}

fn status(client_state: &Self::ClientState) -> Status {
if client_state.frozen_height.height() != 0 {
Status::Frozen
Expand Down
4 changes: 4 additions & 0 deletions cosmwasm/ibc-union/light-clients/movement/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ impl ibc_union_light_client::IbcClient for MovementLightClient {
client_state.latest_block_num
}

fn get_counterparty_chain_id(client_state: &Self::ClientState) -> String {
client_state.chain_id.clone()
}

fn status(client_state: &Self::ClientState) -> Status {
if client_state.frozen_height.height() != 0 {
Status::Frozen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl IbcClient for StateLensIcs23MptLightClient {
client_state.l2_latest_height
}

fn get_counterparty_chain_id(client_state: &Self::ClientState) -> String {
client_state.l2_chain_id.clone()
}

fn status(_client_state: &Self::ClientState) -> Status {
// FIXME: expose the ctx to this call to allow threading this call to L1
// client. generally, we want to thread if a client is an L2 so always
Expand Down
4 changes: 4 additions & 0 deletions cosmwasm/ibc-union/light-clients/tendermint/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ impl IbcClient for TendermintLightClient {
client_state.latest_height.height()
}

fn get_counterparty_chain_id(client_state: &Self::ClientState) -> String {
client_state.chain_id.clone()
}

fn verify_creation(
_client_state: &Self::ClientState,
_consensus_state: &Self::ConsensusState,
Expand Down
4 changes: 4 additions & 0 deletions lib/cometbls-light-client-types/src/chain_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ pub struct ChainId(String);
impl ChainId {
pub const MAX_LEN: usize = 31;

pub fn into_string(self) -> String {
self.0
}

pub fn from_string(s: impl Into<String>) -> Result<Self, InvalidLength> {
let s = s.into();

Expand Down
8 changes: 4 additions & 4 deletions lib/ibc-solidity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ maybe_sol_attr! {
serde(deny_unknown_fields)
)]
struct Packet {
uint32 source_channel;
uint32 destination_channel;
uint32 source_channel_id;
uint32 destination_channel_id;
bytes data;
uint64 timeout_height;
uint64 timeout_timestamp;
Expand Down Expand Up @@ -690,8 +690,8 @@ mod tests {
fn packet_hash() {
dbg!(keccak256(
Packet {
source_channel: 1,
destination_channel: 1,
source_channel_id: 1,
destination_channel_id: 1,
data: bytes!("0000000000000000000000000000000000000000000000000000000000000000"),
timeout_height: 0,
timeout_timestamp: 1733160153000000000
Expand Down
Loading
Loading