ibc_core_connection/handler/
mod.rs

1use ibc_core_client::types::error::ClientError;
2use ibc_core_connection_types::error::ConnectionError;
3#[cfg(feature = "wasm-client")]
4use ibc_core_host::types::error::DecodingError;
5use ibc_core_host::types::identifiers::ClientId;
6use ibc_primitives::proto::Any;
7
8pub mod conn_open_ack;
9pub mod conn_open_confirm;
10pub mod conn_open_init;
11pub mod conn_open_try;
12
13/// Unpacks the client state from the format that is stored at the counterparty chain.
14///
15/// Currently, the IBC-go enabled chains stores Wasm LightClient states in a WasmClientState
16/// wrapper. This function unpacks the client state from the WasmClientState wrapper
17/// if the client identifier at counterparty is of Wasm client type.
18pub(crate) fn unpack_host_client_state<CS>(
19    value: Any,
20    host_client_id_at_counterparty: &ClientId,
21) -> Result<CS, ConnectionError>
22where
23    CS: TryFrom<Any>,
24    <CS as TryFrom<Any>>::Error: Into<ClientError>,
25{
26    #[cfg(feature = "wasm-client")]
27    if host_client_id_at_counterparty.is_wasm_client_id() {
28        use ibc_client_wasm_types::client_state::ClientState as WasmClientState;
29        use prost::Message;
30
31        let wasm_client_state = WasmClientState::try_from(value)?;
32
33        let any_client_state = <Any as Message>::decode(wasm_client_state.data.as_slice())
34            .map_err(|e| ConnectionError::Decoding(DecodingError::Prost(e)))?;
35
36        Ok(CS::try_from(any_client_state).map_err(Into::<ClientError>::into)?)
37    } else {
38        Ok(CS::try_from(value).map_err(Into::<ClientError>::into)?)
39    }
40
41    #[cfg(not(feature = "wasm-client"))]
42    {
43        // this avoids lint warning for unused variable.
44        let _ = host_client_id_at_counterparty;
45        Ok(CS::try_from(value).map_err(Into::<ClientError>::into)?)
46    }
47}
48
49/// Pack the host consensus state in the expected format stored at the counterparty chain.
50///
51/// Currently, the IBC-go enabled chains stores Wasm LightClient states in a WasmConsensusState
52/// wrapper. This function packs the consensus state in the WasmConsensusState wrapper
53/// if the client identifier at counterparty is of Wasm client type.
54pub(crate) fn pack_host_consensus_state<CS>(
55    value: CS,
56    host_client_id_at_counterparty: &ClientId,
57) -> Any
58where
59    CS: Into<Any>,
60{
61    let any_value = value.into();
62
63    #[cfg(feature = "wasm-client")]
64    if host_client_id_at_counterparty.is_wasm_client_id() {
65        use ibc_client_wasm_types::consensus_state::ConsensusState as WasmConsensusState;
66        use prost::Message;
67
68        let wasm_consensus_state = WasmConsensusState::new(any_value.encode_to_vec());
69
70        wasm_consensus_state.into()
71    } else {
72        any_value
73    }
74
75    #[cfg(not(feature = "wasm-client"))]
76    {
77        // this avoids lint warning for unused variable.
78        let _ = host_client_id_at_counterparty;
79        any_value
80    }
81}