Skip to content

Commit

Permalink
fix(ibc-core): pack host consensus state into wasm consensus state fo…
Browse files Browse the repository at this point in the history
…r wasm clients (#1252)

* add custom consensus state packing

* change feature flag

* change doc comments

* add pack_host_consensus_state fn

* fix imports

* update changelog entry
  • Loading branch information
rnbguy authored Jun 12, 2024
1 parent 478b4c6 commit ac579fa
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Unpack Wasm client state at `ConnectionOpenTry` and `ConnectionOpenAck` for
host client state. ([#1237](https://github.com/cosmos/ibc-rs/issues/1237)).
- Unpack/pack Wasm client/consensus state at `ConnectionOpenTry` and
`ConnectionOpenAck` for host client/consensus state.
([#1237](https://github.com/cosmos/ibc-rs/issues/1237)).
4 changes: 2 additions & 2 deletions ibc-core/ics03-connection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ std = [
"ibc-core-host/std",
"ibc-core-handler-types/std",
"ibc-primitives/std",
"wasm-wrapped-client-state",
"wasm-client",
]
serde = [
"ibc-core-client/serde",
Expand Down Expand Up @@ -67,7 +67,7 @@ parity-scale-codec = [
"ibc-core-handler-types/parity-scale-codec",
"ibc-primitives/parity-scale-codec",
]
wasm-wrapped-client-state = [
wasm-client = [
"dep:ibc-client-wasm-types",
"dep:prost",
]
7 changes: 5 additions & 2 deletions ibc-core/ics03-connection/src/handler/conn_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use ibc_primitives::prelude::*;
use ibc_primitives::proto::{Any, Protobuf};
use ibc_primitives::ToVec;

use crate::handler::unpack_host_client_state;
use crate::handler::{pack_host_consensus_state, unpack_host_client_state};

pub fn validate<Ctx>(ctx_a: &Ctx, msg: MsgConnectionOpenAck) -> Result<(), ContextError>
where
Expand Down Expand Up @@ -123,6 +123,9 @@ where
let expected_consensus_state_of_a_on_b =
ctx_a.host_consensus_state(&msg.consensus_height_of_a_on_b)?;

let stored_consensus_state_of_a_on_b =
pack_host_consensus_state(expected_consensus_state_of_a_on_b, vars.client_id_on_b());

let client_cons_state_path_on_b = ClientConsensusStatePath::new(
vars.client_id_on_b().clone(),
msg.consensus_height_of_a_on_b.revision_number(),
Expand All @@ -135,7 +138,7 @@ where
&msg.proof_consensus_state_of_a_on_b,
consensus_state_of_b_on_a.root(),
Path::ClientConsensusState(client_cons_state_path_on_b),
expected_consensus_state_of_a_on_b.into().to_vec(),
stored_consensus_state_of_a_on_b.to_vec(),
)
.map_err(|e| ConnectionError::ConsensusStateVerificationFailure {
height: msg.proofs_height_on_b,
Expand Down
7 changes: 5 additions & 2 deletions ibc-core/ics03-connection/src/handler/conn_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ibc_primitives::prelude::*;
use ibc_primitives::proto::{Any, Protobuf};
use ibc_primitives::ToVec;

use crate::handler::unpack_host_client_state;
use crate::handler::{pack_host_consensus_state, unpack_host_client_state};

pub fn validate<Ctx>(ctx_b: &Ctx, msg: MsgConnectionOpenTry) -> Result<(), ContextError>
where
Expand Down Expand Up @@ -119,6 +119,9 @@ where
let expected_consensus_state_of_b_on_a =
ctx_b.host_consensus_state(&msg.consensus_height_of_b_on_a)?;

let stored_consensus_state_of_b_on_a =
pack_host_consensus_state(expected_consensus_state_of_b_on_a, &vars.client_id_on_a);

let client_cons_state_path_on_a = ClientConsensusStatePath::new(
client_id_on_a.clone(),
msg.consensus_height_of_b_on_a.revision_number(),
Expand All @@ -131,7 +134,7 @@ where
&msg.proof_consensus_state_of_b_on_a,
consensus_state_of_a_on_b.root(),
Path::ClientConsensusState(client_cons_state_path_on_a),
expected_consensus_state_of_b_on_a.into().to_vec(),
stored_consensus_state_of_b_on_a.to_vec(),
)
.map_err(|e| ConnectionError::ConsensusStateVerificationFailure {
height: msg.proofs_height_on_a,
Expand Down
46 changes: 40 additions & 6 deletions ibc-core/ics03-connection/src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ pub mod conn_open_confirm;
pub mod conn_open_init;
pub mod conn_open_try;

/// Unpacks the client state from the provided [`Any`] type.
/// Unpacks the client state from the format that is stored at the counterparty chain.
///
/// Currently, the IBC-go enabled chains stores Wasm LightClient states in a
/// WasmClientState wrapper. This function unpacks the client state from
/// the WasmClientState wrapper if the client identifier is of Wasm client type.
/// Currently, the IBC-go enabled chains stores Wasm LightClient states in a WasmClientState
/// wrapper. This function unpacks the client state from the WasmClientState wrapper
/// if the client identifier at counterparty is of Wasm client type.
pub(crate) fn unpack_host_client_state<CS>(
value: Any,
host_client_id_at_counterparty: &ClientId,
Expand All @@ -21,7 +21,7 @@ where
CS: TryFrom<Any>,
<CS as TryFrom<Any>>::Error: Into<ClientError>,
{
#[cfg(feature = "wasm-wrapped-client-state")]
#[cfg(feature = "wasm-client")]
if host_client_id_at_counterparty.is_wasm_client_id() {
use ibc_client_wasm_types::client_state::ClientState as WasmClientState;
use ibc_core_connection_types::error::ConnectionError;
Expand All @@ -46,10 +46,44 @@ where
Ok(CS::try_from(value).map_err(Into::<ClientError>::into)?)
}

#[cfg(not(feature = "wasm-wrapped-client-state"))]
#[cfg(not(feature = "wasm-client"))]
{
// this avoids lint warning for unused variable.
let _ = host_client_id_at_counterparty;
Ok(CS::try_from(value).map_err(Into::<ClientError>::into)?)
}
}

/// Pack the host consensus state in the expected format stored at the counterparty chain.
///
/// Currently, the IBC-go enabled chains stores Wasm LightClient states in a WasmConsensusState
/// wrapper. This function packs the consensus state in the WasmConsensusState wrapper
/// if the client identifier at counterparty is of Wasm client type.
pub(crate) fn pack_host_consensus_state<CS>(
value: CS,
host_client_id_at_counterparty: &ClientId,
) -> Any
where
CS: Into<Any>,
{
let any_value = value.into();

#[cfg(feature = "wasm-client")]
if host_client_id_at_counterparty.is_wasm_client_id() {
use ibc_client_wasm_types::consensus_state::ConsensusState as WasmConsensusState;
use prost::Message;

let wasm_consensus_state = WasmConsensusState::new(any_value.encode_to_vec());

wasm_consensus_state.into()
} else {
any_value
}

#[cfg(not(feature = "wasm-client"))]
{
// this avoids lint warning for unused variable.
let _ = host_client_id_at_counterparty;
any_value
}
}

0 comments on commit ac579fa

Please sign in to comment.