Skip to content

Commit ac579fa

Browse files
authored
fix(ibc-core): pack host consensus state into wasm consensus state for 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
1 parent 478b4c6 commit ac579fa

File tree

5 files changed

+55
-14
lines changed

5 files changed

+55
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
- Unpack Wasm client state at `ConnectionOpenTry` and `ConnectionOpenAck` for
2-
host client state. ([#1237](https://github.com/cosmos/ibc-rs/issues/1237)).
1+
- Unpack/pack Wasm client/consensus state at `ConnectionOpenTry` and
2+
`ConnectionOpenAck` for host client/consensus state.
3+
([#1237](https://github.com/cosmos/ibc-rs/issues/1237)).

ibc-core/ics03-connection/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ std = [
3535
"ibc-core-host/std",
3636
"ibc-core-handler-types/std",
3737
"ibc-primitives/std",
38-
"wasm-wrapped-client-state",
38+
"wasm-client",
3939
]
4040
serde = [
4141
"ibc-core-client/serde",
@@ -67,7 +67,7 @@ parity-scale-codec = [
6767
"ibc-core-handler-types/parity-scale-codec",
6868
"ibc-primitives/parity-scale-codec",
6969
]
70-
wasm-wrapped-client-state = [
70+
wasm-client = [
7171
"dep:ibc-client-wasm-types",
7272
"dep:prost",
7373
]

ibc-core/ics03-connection/src/handler/conn_open_ack.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ibc_primitives::prelude::*;
1515
use ibc_primitives::proto::{Any, Protobuf};
1616
use ibc_primitives::ToVec;
1717

18-
use crate::handler::unpack_host_client_state;
18+
use crate::handler::{pack_host_consensus_state, unpack_host_client_state};
1919

2020
pub fn validate<Ctx>(ctx_a: &Ctx, msg: MsgConnectionOpenAck) -> Result<(), ContextError>
2121
where
@@ -123,6 +123,9 @@ where
123123
let expected_consensus_state_of_a_on_b =
124124
ctx_a.host_consensus_state(&msg.consensus_height_of_a_on_b)?;
125125

126+
let stored_consensus_state_of_a_on_b =
127+
pack_host_consensus_state(expected_consensus_state_of_a_on_b, vars.client_id_on_b());
128+
126129
let client_cons_state_path_on_b = ClientConsensusStatePath::new(
127130
vars.client_id_on_b().clone(),
128131
msg.consensus_height_of_a_on_b.revision_number(),
@@ -135,7 +138,7 @@ where
135138
&msg.proof_consensus_state_of_a_on_b,
136139
consensus_state_of_b_on_a.root(),
137140
Path::ClientConsensusState(client_cons_state_path_on_b),
138-
expected_consensus_state_of_a_on_b.into().to_vec(),
141+
stored_consensus_state_of_a_on_b.to_vec(),
139142
)
140143
.map_err(|e| ConnectionError::ConsensusStateVerificationFailure {
141144
height: msg.proofs_height_on_b,

ibc-core/ics03-connection/src/handler/conn_open_try.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ibc_primitives::prelude::*;
1616
use ibc_primitives::proto::{Any, Protobuf};
1717
use ibc_primitives::ToVec;
1818

19-
use crate::handler::unpack_host_client_state;
19+
use crate::handler::{pack_host_consensus_state, unpack_host_client_state};
2020

2121
pub fn validate<Ctx>(ctx_b: &Ctx, msg: MsgConnectionOpenTry) -> Result<(), ContextError>
2222
where
@@ -119,6 +119,9 @@ where
119119
let expected_consensus_state_of_b_on_a =
120120
ctx_b.host_consensus_state(&msg.consensus_height_of_b_on_a)?;
121121

122+
let stored_consensus_state_of_b_on_a =
123+
pack_host_consensus_state(expected_consensus_state_of_b_on_a, &vars.client_id_on_a);
124+
122125
let client_cons_state_path_on_a = ClientConsensusStatePath::new(
123126
client_id_on_a.clone(),
124127
msg.consensus_height_of_b_on_a.revision_number(),
@@ -131,7 +134,7 @@ where
131134
&msg.proof_consensus_state_of_b_on_a,
132135
consensus_state_of_a_on_b.root(),
133136
Path::ClientConsensusState(client_cons_state_path_on_a),
134-
expected_consensus_state_of_b_on_a.into().to_vec(),
137+
stored_consensus_state_of_b_on_a.to_vec(),
135138
)
136139
.map_err(|e| ConnectionError::ConsensusStateVerificationFailure {
137140
height: msg.proofs_height_on_a,

ibc-core/ics03-connection/src/handler/mod.rs

+40-6
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ pub mod conn_open_confirm;
88
pub mod conn_open_init;
99
pub mod conn_open_try;
1010

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

49-
#[cfg(not(feature = "wasm-wrapped-client-state"))]
49+
#[cfg(not(feature = "wasm-client"))]
5050
{
5151
// this avoids lint warning for unused variable.
5252
let _ = host_client_id_at_counterparty;
5353
Ok(CS::try_from(value).map_err(Into::<ClientError>::into)?)
5454
}
5555
}
56+
57+
/// Pack the host consensus state in the expected format stored at the counterparty chain.
58+
///
59+
/// Currently, the IBC-go enabled chains stores Wasm LightClient states in a WasmConsensusState
60+
/// wrapper. This function packs the consensus state in the WasmConsensusState wrapper
61+
/// if the client identifier at counterparty is of Wasm client type.
62+
pub(crate) fn pack_host_consensus_state<CS>(
63+
value: CS,
64+
host_client_id_at_counterparty: &ClientId,
65+
) -> Any
66+
where
67+
CS: Into<Any>,
68+
{
69+
let any_value = value.into();
70+
71+
#[cfg(feature = "wasm-client")]
72+
if host_client_id_at_counterparty.is_wasm_client_id() {
73+
use ibc_client_wasm_types::consensus_state::ConsensusState as WasmConsensusState;
74+
use prost::Message;
75+
76+
let wasm_consensus_state = WasmConsensusState::new(any_value.encode_to_vec());
77+
78+
wasm_consensus_state.into()
79+
} else {
80+
any_value
81+
}
82+
83+
#[cfg(not(feature = "wasm-client"))]
84+
{
85+
// this avoids lint warning for unused variable.
86+
let _ = host_client_id_at_counterparty;
87+
any_value
88+
}
89+
}

0 commit comments

Comments
 (0)