From c6d916b398eb1c7714454049a9f9677c6ae5c560 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Mon, 28 Oct 2024 21:27:17 +0100 Subject: [PATCH] provide counterparty msg --- .../ics02-client/context/src/context.rs | 2 +- .../ics02-client/src/handler/mod.rs | 1 + .../src/handler/provide_counterparty.rs | 57 +++++++++++++++++++ .../ics02-client/types/src/msgs/mod.rs | 3 + .../types/src/msgs/provide_counterparty.rs | 38 +++++++++++++ .../src/handler/acknowledgement.rs | 5 +- .../ics04-channel/src/handler/recv_packet.rs | 5 +- .../ics04-channel/src/handler/send_packet.rs | 5 +- .../ics04-channel/src/handler/timeout.rs | 5 +- .../ics25-handler/src/entrypoint.rs | 6 +- 10 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 ibc-eureka-core/ics02-client/src/handler/provide_counterparty.rs create mode 100644 ibc-eureka-core/ics02-client/types/src/msgs/provide_counterparty.rs diff --git a/ibc-eureka-core/ics02-client/context/src/context.rs b/ibc-eureka-core/ics02-client/context/src/context.rs index 25c3b8e57..dc134e1fd 100644 --- a/ibc-eureka-core/ics02-client/context/src/context.rs +++ b/ibc-eureka-core/ics02-client/context/src/context.rs @@ -45,7 +45,7 @@ pub trait ClientValidationContext: Sized { fn counterparty_meta( &self, client_id: &ClientId, - ) -> Result<(ClientId, CommitmentPrefix), HostError>; + ) -> Result, HostError>; } /// Defines the methods that all client `ExecutionContext`s (precisely the diff --git a/ibc-eureka-core/ics02-client/src/handler/mod.rs b/ibc-eureka-core/ics02-client/src/handler/mod.rs index 791b5ce39..cd5a9e65c 100644 --- a/ibc-eureka-core/ics02-client/src/handler/mod.rs +++ b/ibc-eureka-core/ics02-client/src/handler/mod.rs @@ -1,6 +1,7 @@ //! This module implements the processing logic for ICS2 (client abstractions and functions) msgs. pub mod create_client; +pub mod provide_counterparty; pub mod recover_client; pub mod update_client; pub mod upgrade_client; diff --git a/ibc-eureka-core/ics02-client/src/handler/provide_counterparty.rs b/ibc-eureka-core/ics02-client/src/handler/provide_counterparty.rs new file mode 100644 index 000000000..b4154f3c6 --- /dev/null +++ b/ibc-eureka-core/ics02-client/src/handler/provide_counterparty.rs @@ -0,0 +1,57 @@ +//! Protocol logic specific to processing ICS2 messages of type `MsgProvideCouterparty`. + +use ibc_eureka_core_client_context::prelude::*; +use ibc_eureka_core_client_types::error::ClientError; +use ibc_eureka_core_client_types::msgs::MsgProvideCouterparty; +use ibc_eureka_core_host::{ExecutionContext, ValidationContext}; +use ibc_primitives::prelude::*; + +pub fn validate(ctx: &Ctx, msg: MsgProvideCouterparty) -> Result<(), ClientError> +where + Ctx: ValidationContext, +{ + let MsgProvideCouterparty { + client_id, signer, .. + } = &msg; + + ctx.validate_message_signer(signer)?; + + let client_val_ctx = ctx.get_client_validation_context(); + + // Read client state from the host chain store. The client should already exist. + let client_state = client_val_ctx.client_state(client_id)?; + + client_state + .status(client_val_ctx, client_id)? + .verify_is_active()?; + + if client_val_ctx.counterparty_meta(client_id)?.is_some() { + return Err(ClientError::ClientSpecific { + description: "counterparty is already provided".into(), + }); + } + + Ok(()) +} + +pub fn execute(ctx: &mut Ctx, msg: MsgProvideCouterparty) -> Result<(), ClientError> +where + Ctx: ExecutionContext, +{ + let MsgProvideCouterparty { + client_id, + counterparty_client_id, + counterparty_commitment_prefix, + .. + } = &msg; + + let client_exec_ctx = ctx.get_client_execution_context(); + + client_exec_ctx.store_counterparty_meta( + client_id, + counterparty_client_id, + counterparty_commitment_prefix, + )?; + + Ok(()) +} diff --git a/ibc-eureka-core/ics02-client/types/src/msgs/mod.rs b/ibc-eureka-core/ics02-client/types/src/msgs/mod.rs index 7c02331c3..0ba422b1c 100644 --- a/ibc-eureka-core/ics02-client/types/src/msgs/mod.rs +++ b/ibc-eureka-core/ics02-client/types/src/msgs/mod.rs @@ -9,12 +9,14 @@ use ibc_proto::google::protobuf::Any; mod create_client; mod misbehaviour; +mod provide_counterparty; mod recover_client; mod update_client; mod upgrade_client; pub use create_client::*; pub use misbehaviour::*; +pub use provide_counterparty::*; pub use recover_client::*; pub use update_client::*; pub use upgrade_client::*; @@ -33,6 +35,7 @@ pub enum ClientMsg { Misbehaviour(MsgSubmitMisbehaviour), UpgradeClient(MsgUpgradeClient), RecoverClient(MsgRecoverClient), + ProvideCounterparty(MsgProvideCouterparty), } pub enum MsgUpdateOrMisbehaviour { diff --git a/ibc-eureka-core/ics02-client/types/src/msgs/provide_counterparty.rs b/ibc-eureka-core/ics02-client/types/src/msgs/provide_counterparty.rs new file mode 100644 index 000000000..8c3d97fe5 --- /dev/null +++ b/ibc-eureka-core/ics02-client/types/src/msgs/provide_counterparty.rs @@ -0,0 +1,38 @@ +//! Definition of domain type message `MsgProvideCouterparty`. + +use ibc_eureka_core_commitment_types::commitment::CommitmentPrefix; +use ibc_eureka_core_host_types::identifiers::ClientId; +use ibc_primitives::prelude::*; +use ibc_primitives::Signer; + +pub const _PROVIDE_COUNTERPARTY_TYPE_URL: &str = "/ibc.core.client.v1.MsgProvideCouterparty"; + +/// A type of message that links an on-chain (IBC) client to its counterparty. +#[cfg_attr( + feature = "borsh", + derive(borsh::BorshSerialize, borsh::BorshDeserialize) +)] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct MsgProvideCouterparty { + pub client_id: ClientId, + pub counterparty_client_id: ClientId, + pub counterparty_commitment_prefix: CommitmentPrefix, + pub signer: Signer, +} + +impl MsgProvideCouterparty { + pub fn new( + client_id: ClientId, + counterparty_client_id: ClientId, + counterparty_commitment_prefix: CommitmentPrefix, + signer: Signer, + ) -> Self { + MsgProvideCouterparty { + client_id, + counterparty_client_id, + counterparty_commitment_prefix, + signer, + } + } +} diff --git a/ibc-eureka-core/ics04-channel/src/handler/acknowledgement.rs b/ibc-eureka-core/ics04-channel/src/handler/acknowledgement.rs index 17b863d27..6c2f1a5ec 100644 --- a/ibc-eureka-core/ics04-channel/src/handler/acknowledgement.rs +++ b/ibc-eureka-core/ics04-channel/src/handler/acknowledgement.rs @@ -139,8 +139,9 @@ where let client_val_ctx_a = ctx_a.get_client_validation_context(); - let (stored_id_source_client_on_target, target_prefix) = - client_val_ctx_a.counterparty_meta(id_target_client_on_source)?; + let (stored_id_source_client_on_target, target_prefix) = client_val_ctx_a + .counterparty_meta(id_target_client_on_source)? + .ok_or(ChannelError::MissingCounterparty)?; if &stored_id_source_client_on_target != id_source_client_on_target { return Err(ChannelError::MismatchCounterparty { diff --git a/ibc-eureka-core/ics04-channel/src/handler/recv_packet.rs b/ibc-eureka-core/ics04-channel/src/handler/recv_packet.rs index 4d4701f25..1a3eca9ad 100644 --- a/ibc-eureka-core/ics04-channel/src/handler/recv_packet.rs +++ b/ibc-eureka-core/ics04-channel/src/handler/recv_packet.rs @@ -145,8 +145,9 @@ where let client_val_ctx_b = ctx_b.get_client_validation_context(); - let (stored_id_target_client_on_source, source_prefix) = - client_val_ctx_b.counterparty_meta(id_source_client_on_target)?; + let (stored_id_target_client_on_source, source_prefix) = client_val_ctx_b + .counterparty_meta(id_source_client_on_target)? + .ok_or(ChannelError::MissingCounterparty)?; if &stored_id_target_client_on_source != id_target_client_on_source { return Err(ChannelError::MismatchCounterparty { diff --git a/ibc-eureka-core/ics04-channel/src/handler/send_packet.rs b/ibc-eureka-core/ics04-channel/src/handler/send_packet.rs index e8196aa5a..b1a15110d 100644 --- a/ibc-eureka-core/ics04-channel/src/handler/send_packet.rs +++ b/ibc-eureka-core/ics04-channel/src/handler/send_packet.rs @@ -42,8 +42,9 @@ pub fn send_packet_validate( let id_target_client_on_source = channel_target_client_on_source.as_ref(); let id_source_client_on_target: &ClientId = channel_source_client_on_target.as_ref(); - let (stored_id_source_client_on_target, _) = - client_val_ctx_a.counterparty_meta(id_target_client_on_source)?; + let (stored_id_source_client_on_target, _) = client_val_ctx_a + .counterparty_meta(id_target_client_on_source)? + .ok_or(ChannelError::MissingCounterparty)?; if &stored_id_source_client_on_target != id_source_client_on_target { return Err(ChannelError::MismatchCounterparty { diff --git a/ibc-eureka-core/ics04-channel/src/handler/timeout.rs b/ibc-eureka-core/ics04-channel/src/handler/timeout.rs index d96feaa82..24ac17f31 100644 --- a/ibc-eureka-core/ics04-channel/src/handler/timeout.rs +++ b/ibc-eureka-core/ics04-channel/src/handler/timeout.rs @@ -130,8 +130,9 @@ where let client_val_ctx_a = ctx_a.get_client_validation_context(); - let (stored_id_source_client_on_target, target_prefix) = - client_val_ctx_a.counterparty_meta(id_target_client_on_source)?; + let (stored_id_source_client_on_target, target_prefix) = client_val_ctx_a + .counterparty_meta(id_target_client_on_source)? + .ok_or(ChannelError::MissingCounterparty)?; if &stored_id_source_client_on_target != id_source_client_on_target { return Err(ChannelError::MismatchCounterparty { diff --git a/ibc-eureka-core/ics25-handler/src/entrypoint.rs b/ibc-eureka-core/ics25-handler/src/entrypoint.rs index 69010d5d6..6044cab33 100644 --- a/ibc-eureka-core/ics25-handler/src/entrypoint.rs +++ b/ibc-eureka-core/ics25-handler/src/entrypoint.rs @@ -4,7 +4,9 @@ use ibc_eureka_core_channel::handler::{ }; use ibc_eureka_core_channel::types::msgs::{packet_msg_to_port_id, PacketMsg}; use ibc_eureka_core_client::context::{ClientExecutionContext, ClientValidationContext}; -use ibc_eureka_core_client::handler::{create_client, update_client, upgrade_client}; +use ibc_eureka_core_client::handler::{ + create_client, provide_counterparty, update_client, upgrade_client, +}; use ibc_eureka_core_client::types::error::ClientError; use ibc_eureka_core_client::types::msgs::{ClientMsg, MsgUpdateOrMisbehaviour}; use ibc_eureka_core_handler_types::error::HandlerError; @@ -60,6 +62,7 @@ where // Recover client messages are not dispatched by ibc-rs as they can only be // authorized via a passing governance proposal } + ClientMsg::ProvideCounterparty(msg) => provide_counterparty::validate(ctx, msg)?, }, MsgEnvelope::Packet(msg) => { let port_id = packet_msg_to_port_id(&msg); @@ -105,6 +108,7 @@ where // Recover client messages are not dispatched by ibc-rs as they can only be // authorized via a passing governance proposal } + ClientMsg::ProvideCounterparty(msg) => provide_counterparty::execute(ctx, msg)?, }, MsgEnvelope::Packet(msg) => { let port_id = packet_msg_to_port_id(&msg);