Skip to content

Commit

Permalink
feat(ibc-union): migrate clients support (#3449)
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo authored Jan 7, 2025
2 parents de0f54d + 12e8fb2 commit e5ac36a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
10 changes: 10 additions & 0 deletions cosmwasm/ibc-union/core/msg/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ pub enum ExecuteMsg {
BatchAcks(MsgBatchAcks),
PacketSend(MsgSendPacket),
WriteAcknowledgement(MsgWriteAcknowledgement),
MigrateState(MsgMigrateState),
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MsgMigrateState {
pub client_id: u32,
pub client_state: Bytes,
pub consensus_state: Bytes,
pub height: u64,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
Expand Down
62 changes: 59 additions & 3 deletions cosmwasm/ibc-union/core/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use ibc_union_msg::{
ExecuteMsg, InitMsg, MsgBatchAcks, MsgBatchSend, MsgChannelCloseConfirm,
MsgChannelCloseInit, MsgChannelOpenAck, MsgChannelOpenConfirm, MsgChannelOpenInit,
MsgChannelOpenTry, MsgConnectionOpenAck, MsgConnectionOpenConfirm, MsgConnectionOpenInit,
MsgConnectionOpenTry, MsgCreateClient, MsgIntentPacketRecv, MsgPacketAcknowledgement,
MsgPacketRecv, MsgPacketTimeout, MsgRegisterClient, MsgSendPacket, MsgUpdateClient,
MsgWriteAcknowledgement,
MsgConnectionOpenTry, MsgCreateClient, MsgIntentPacketRecv, MsgMigrateState,
MsgPacketAcknowledgement, MsgPacketRecv, MsgPacketTimeout, MsgRegisterClient,
MsgSendPacket, MsgUpdateClient, MsgWriteAcknowledgement,
},
query::QueryMsg,
};
Expand Down Expand Up @@ -391,7 +391,63 @@ pub fn execute(
packets,
acks.into_iter().map(Into::into).collect(),
),
ExecuteMsg::MigrateState(MsgMigrateState {
client_id,
client_state,
consensus_state,
height,
}) => migrate_state(
deps,
info.sender,
client_id,
client_state,
consensus_state,
height,
),
}
}

fn migrate_state(
mut deps: DepsMut,
sender: Addr,
client_id: u32,
client_state: unionlabs::primitives::Bytes,
consensus_state: unionlabs::primitives::Bytes,
height: u64,
) -> Result<Response, ContractError> {
let client_addr = CLIENT_IMPLS.load(deps.storage, client_id)?;

if client_addr != sender {
return Err(ContractError::UnauthorizedMigration {
client_id,
caller: sender,
client: client_addr,
});
}

CLIENT_STATES.update(deps.storage, client_id, |s| {
let _ = s.ok_or(ContractError::CannotMigrateWithNoClientState { client_id })?;
Ok::<Binary, ContractError>(client_state.to_vec().into())
})?;

store_commit(
deps.branch(),
&ClientStatePath { client_id }.key(),
&commit(client_state),
)?;

CLIENT_CONSENSUS_STATES.update(deps.storage, (client_id, height), |s| {
let _ = s.ok_or(ContractError::CannotMigrateWithNoConsensusState { client_id, height })?;
Ok::<Binary, ContractError>(consensus_state.to_vec().into())
})?;

store_commit(
deps.branch(),
&ConsensusStatePath { client_id, height }.key(),
&commit(consensus_state),
)?;

Ok(Response::new())
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
17 changes: 17 additions & 0 deletions cosmwasm/ibc-union/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ pub enum ContractError {
AcknowledgementMismatch { found: Bytes, expected: Bytes },
#[error("{} the packet already exist", ContractErrorKind::from(self))]
PacketCommitmentAlreadyExist,
#[error(
"{} caller {caller} don't have permission to migrate the client {client} with id {client_id}", ContractErrorKind::from(self)
)]
UnauthorizedMigration {
client_id: u32,
caller: Addr,
client: Addr,
},
#[error(
"{} cannot migrate the client {client_id} when there's no client state",
ContractErrorKind::from(self)
)]
CannotMigrateWithNoClientState { client_id: u32 },
#[error(
"{} cannot migrate the client {client_id} when there's no consensus state at height {height}", ContractErrorKind::from(self)
)]
CannotMigrateWithNoConsensusState { client_id: u32, height: u64 },
}

impl ContractErrorKind {
Expand Down

0 comments on commit e5ac36a

Please sign in to comment.