Skip to content

Commit fa22b0c

Browse files
fix(ics02)!: modify MsgUpgradeClient to utilize CommitmentProofBytes (#748)
* fix(ics02): modify MsgUpgradeClient to utilize CommitmentProofBytes * fix: convert upgrade_path properly into vec
1 parent 30a2e5d commit fa22b0c

File tree

10 files changed

+74
-104
lines changed

10 files changed

+74
-104
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Modify `MsgUpgradeClient` struct to utilize `CommitmentProofBytes` and
2+
apply some refinements around upgrade client methods and impls respectively.
3+
([#739](https://github.com/cosmos/ibc-rs/issues/739))

crates/ibc-derive/src/client_state/traits/client_state_common.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ pub(crate) fn impl_ClientStateCommon(
6868
let ClientType = Imports::ClientType();
6969
let ClientError = Imports::ClientError();
7070
let Height = Imports::Height();
71-
let MerkleProof = Imports::MerkleProof();
7271
let Path = Imports::Path();
7372

7473
quote! {
@@ -112,8 +111,8 @@ pub(crate) fn impl_ClientStateCommon(
112111
&self,
113112
upgraded_client_state: #Any,
114113
upgraded_consensus_state: #Any,
115-
proof_upgrade_client: #MerkleProof,
116-
proof_upgrade_consensus_state: #MerkleProof,
114+
proof_upgrade_client: #CommitmentProofBytes,
115+
proof_upgrade_consensus_state: #CommitmentProofBytes,
117116
root: &#CommitmentRoot,
118117
) -> core::result::Result<(), #ClientError> {
119118
match self {

crates/ibc-derive/src/client_state/traits/client_state_execution.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub(crate) fn impl_ClientStateExecution(
3939
client_state_enum_name,
4040
enum_variants.iter(),
4141
opts,
42-
quote! { update_state_with_upgrade_client(cs, ctx, client_id, upgraded_client_state, upgraded_consensus_state) },
42+
quote! { update_state_on_upgrade(cs, ctx, client_id, upgraded_client_state, upgraded_consensus_state) },
4343
);
4444

4545
let HostClientState = client_state_enum_name;
@@ -88,7 +88,7 @@ pub(crate) fn impl_ClientStateExecution(
8888
}
8989
}
9090

91-
fn update_state_with_upgrade_client(
91+
fn update_state_on_upgrade(
9292
&self,
9393
ctx: &mut #ClientExecutionContext,
9494
client_id: &#ClientId,

crates/ibc-derive/src/utils.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ impl Imports {
6161
quote! {ibc_proto::google::protobuf::Any}
6262
}
6363

64-
pub fn MerkleProof() -> TokenStream {
65-
quote! {ibc::core::ics23_commitment::merkle::MerkleProof}
66-
}
67-
6864
pub fn Timestamp() -> TokenStream {
6965
quote! {ibc::core::timestamp::Timestamp}
7066
}

crates/ibc/src/clients/ics07_tendermint/client_state.rs

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use core::time::Duration;
1212

1313
use ibc_proto::google::protobuf::Any;
1414
use ibc_proto::ibc::core::client::v1::Height as RawHeight;
15-
use ibc_proto::ibc::core::commitment::v1::{MerklePath, MerkleProof as RawMerkleProof};
15+
use ibc_proto::ibc::core::commitment::v1::MerkleProof as RawMerkleProof;
1616
use ibc_proto::ibc::lightclients::tendermint::v1::ClientState as RawTmClientState;
1717
use ibc_proto::protobuf::Protobuf;
1818
use prost::Message;
@@ -318,12 +318,12 @@ impl ClientStateCommon for ClientState {
318318
&self,
319319
upgraded_client_state: Any,
320320
upgraded_consensus_state: Any,
321-
proof_upgrade_client: MerkleProof,
322-
proof_upgrade_consensus_state: MerkleProof,
321+
proof_upgrade_client: CommitmentProofBytes,
322+
proof_upgrade_consensus_state: CommitmentProofBytes,
323323
root: &CommitmentRoot,
324324
) -> Result<(), ClientError> {
325325
// Make sure that the client type is of Tendermint type `ClientState`
326-
let mut upgraded_tm_client_state = Self::try_from(upgraded_client_state.clone())?;
326+
let upgraded_tm_client_state = Self::try_from(upgraded_client_state.clone())?;
327327

328328
// Make sure that the consensus type is of Tendermint type `ConsensusState`
329329
TmConsensusState::try_from(upgraded_consensus_state.clone())?;
@@ -346,57 +346,38 @@ impl ClientStateCommon for ClientState {
346346
});
347347
};
348348

349-
let last_height = self.latest_height().revision_height();
350-
351-
// Construct the merkle path for the client state
352-
let mut client_upgrade_path = upgrade_path.clone();
353-
client_upgrade_path.push(UpgradeClientPath::UpgradedClientState(last_height).to_string());
354-
355-
let client_upgrade_merkle_path = MerklePath {
356-
key_path: client_upgrade_path,
357-
};
349+
let upgrade_path_prefix = CommitmentPrefix::try_from(upgrade_path[0].clone().into_bytes())
350+
.map_err(ClientError::InvalidCommitmentProof)?;
358351

359-
upgraded_tm_client_state.zero_custom_fields();
352+
let last_height = self.latest_height().revision_height();
360353

361354
let mut client_state_value = Vec::new();
362355
upgraded_client_state
363356
.encode(&mut client_state_value)
364357
.map_err(ClientError::Encode)?;
365358

366359
// Verify the proof of the upgraded client state
367-
proof_upgrade_client
368-
.verify_membership(
369-
&self.proof_specs,
370-
root.clone().into(),
371-
client_upgrade_merkle_path,
372-
client_state_value,
373-
0,
374-
)
375-
.map_err(ClientError::Ics23Verification)?;
376-
377-
// Construct the merkle path for the consensus state
378-
let mut cons_upgrade_path = upgrade_path;
379-
cons_upgrade_path
380-
.push(UpgradeClientPath::UpgradedClientConsensusState(last_height).to_string());
381-
let cons_upgrade_merkle_path = MerklePath {
382-
key_path: cons_upgrade_path,
383-
};
360+
self.verify_membership(
361+
&upgrade_path_prefix,
362+
&proof_upgrade_client,
363+
root,
364+
Path::UpgradeClient(UpgradeClientPath::UpgradedClientState(last_height)),
365+
client_state_value,
366+
)?;
384367

385368
let mut cons_state_value = Vec::new();
386369
upgraded_consensus_state
387370
.encode(&mut cons_state_value)
388371
.map_err(ClientError::Encode)?;
389372

390373
// Verify the proof of the upgraded consensus state
391-
proof_upgrade_consensus_state
392-
.verify_membership(
393-
&self.proof_specs,
394-
root.clone().into(),
395-
cons_upgrade_merkle_path,
396-
cons_state_value,
397-
0,
398-
)
399-
.map_err(ClientError::Ics23Verification)?;
374+
self.verify_membership(
375+
&upgrade_path_prefix,
376+
&proof_upgrade_consensus_state,
377+
root,
378+
Path::UpgradeClient(UpgradeClientPath::UpgradedClientConsensusState(last_height)),
379+
cons_state_value,
380+
)?;
400381

401382
Ok(())
402383
}
@@ -559,16 +540,18 @@ where
559540
}
560541

561542
// Commit the new client state and consensus state to the store
562-
fn update_state_with_upgrade_client(
543+
fn update_state_on_upgrade(
563544
&self,
564545
ctx: &mut E,
565546
client_id: &ClientId,
566547
upgraded_client_state: Any,
567548
upgraded_consensus_state: Any,
568549
) -> Result<Height, ClientError> {
569-
let upgraded_tm_client_state = Self::try_from(upgraded_client_state)?;
550+
let mut upgraded_tm_client_state = Self::try_from(upgraded_client_state)?;
570551
let upgraded_tm_cons_state = TmConsensusState::try_from(upgraded_consensus_state)?;
571552

553+
upgraded_tm_client_state.zero_custom_fields();
554+
572555
// Construct new client state and consensus state relayer chosen client
573556
// parameters are ignored. All chain-chosen parameters come from
574557
// committed client, all client-chosen parameters come from current

crates/ibc/src/core/ics02_client/client_state.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::core::ics02_client::ClientExecutionContext;
1212
use crate::core::ics23_commitment::commitment::{
1313
CommitmentPrefix, CommitmentProofBytes, CommitmentRoot,
1414
};
15-
use crate::core::ics23_commitment::merkle::MerkleProof;
1615
use crate::core::ics24_host::identifier::ClientId;
1716
use crate::core::ics24_host::path::Path;
1817
use crate::prelude::*;
@@ -72,8 +71,8 @@ pub trait ClientStateCommon {
7271
&self,
7372
upgraded_client_state: Any,
7473
upgraded_consensus_state: Any,
75-
proof_upgrade_client: MerkleProof,
76-
proof_upgrade_consensus_state: MerkleProof,
74+
proof_upgrade_client: CommitmentProofBytes,
75+
proof_upgrade_consensus_state: CommitmentProofBytes,
7776
root: &CommitmentRoot,
7877
) -> Result<(), ClientError>;
7978

@@ -193,7 +192,7 @@ where
193192
) -> Result<(), ClientError>;
194193

195194
// Update the client state and consensus state in the store with the upgraded ones.
196-
fn update_state_with_upgrade_client(
195+
fn update_state_on_upgrade(
197196
&self,
198197
ctx: &mut E,
199198
client_id: &ClientId,

crates/ibc/src/core/ics02_client/handler/upgrade_client.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::core::ics02_client::consensus_state::ConsensusState;
1010
use crate::core::ics02_client::error::ClientError;
1111
use crate::core::ics02_client::events::UpgradeClient;
1212
use crate::core::ics02_client::msgs::upgrade_client::MsgUpgradeClient;
13-
use crate::core::ics23_commitment::merkle::MerkleProof;
1413
use crate::core::ics24_host::path::ClientConsensusStatePath;
1514
use crate::core::{ExecutionContext, ValidationContext};
1615

@@ -58,17 +57,12 @@ where
5857
));
5958
};
6059

61-
// Note: verification of proofs that unmarshalled correctly has been done
62-
// while decoding the proto message into a `MsgEnvelope` domain type
63-
let merkle_proof_upgrade_client = MerkleProof::from(msg.proof_upgrade_client.clone());
64-
let merkle_proof_upgrade_cons_state = MerkleProof::from(msg.proof_upgrade_consensus_state);
65-
6660
// Validate the upgraded client state and consensus state and verify proofs against the root
6761
old_client_state.verify_upgrade_client(
68-
msg.client_state.clone(),
69-
msg.consensus_state,
70-
merkle_proof_upgrade_client,
71-
merkle_proof_upgrade_cons_state,
62+
msg.upgraded_client_state.clone(),
63+
msg.upgraded_consensus_state,
64+
msg.proof_upgrade_client,
65+
msg.proof_upgrade_consensus_state,
7266
old_consensus_state.root(),
7367
)?;
7468

@@ -83,11 +77,11 @@ where
8377

8478
let old_client_state = ctx.client_state(&client_id)?;
8579

86-
let latest_height = old_client_state.update_state_with_upgrade_client(
80+
let latest_height = old_client_state.update_state_on_upgrade(
8781
ctx.get_client_execution_context(),
8882
&client_id,
89-
msg.client_state.clone(),
90-
msg.consensus_state,
83+
msg.upgraded_client_state.clone(),
84+
msg.upgraded_consensus_state,
9185
)?;
9286

9387
let event = IbcEvent::UpgradeClient(UpgradeClient::new(
@@ -148,8 +142,10 @@ mod tests {
148142
let msg_with_low_upgrade_height = MsgUpgradeClient::new_dummy(low_upgrade_height);
149143

150144
let msg_with_unknown_upgraded_cs = MsgUpgradeClient {
151-
client_state: TmClientState::new_dummy_from_header(get_dummy_tendermint_header())
152-
.into(),
145+
upgraded_client_state: TmClientState::new_dummy_from_header(
146+
get_dummy_tendermint_header(),
147+
)
148+
.into(),
153149
..msg_default.clone()
154150
};
155151

@@ -204,7 +200,7 @@ mod tests {
204200

205201
let client_state = fxt.ctx.client_state(&fxt.msg.client_id).unwrap();
206202
let msg_client_state: AnyClientState =
207-
fxt.msg.client_state.clone().try_into().unwrap();
203+
fxt.msg.upgraded_client_state.clone().try_into().unwrap();
208204
assert_eq!(client_state, msg_client_state);
209205

210206
let consensus_state = fxt
@@ -215,7 +211,7 @@ mod tests {
215211
))
216212
.unwrap();
217213
let msg_consensus_state: AnyConsensusState =
218-
fxt.msg.consensus_state.clone().try_into().unwrap();
214+
fxt.msg.upgraded_consensus_state.clone().try_into().unwrap();
219215
assert_eq!(consensus_state, msg_consensus_state);
220216
}
221217
};

crates/ibc/src/core/ics02_client/msgs/upgrade_client.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use core::str::FromStr;
66

77
use ibc_proto::google::protobuf::Any;
88
use ibc_proto::ibc::core::client::v1::MsgUpgradeClient as RawMsgUpgradeClient;
9-
use ibc_proto::ibc::core::commitment::v1::MerkleProof as RawMerkleProof;
109
use ibc_proto::protobuf::Protobuf;
1110

1211
use crate::core::ics02_client::error::{ClientError, UpgradeClientError};
@@ -24,14 +23,14 @@ pub struct MsgUpgradeClient {
2423
// client unique identifier
2524
pub client_id: ClientId,
2625
// Upgraded client state
27-
pub client_state: Any,
26+
pub upgraded_client_state: Any,
2827
// Upgraded consensus state, only contains enough information
2928
// to serve as a basis of trust in update logic
30-
pub consensus_state: Any,
29+
pub upgraded_consensus_state: Any,
3130
// proof that old chain committed to new client
32-
pub proof_upgrade_client: RawMerkleProof,
31+
pub proof_upgrade_client: CommitmentProofBytes,
3332
// proof that old chain committed to new consensus state
34-
pub proof_upgrade_consensus_state: RawMerkleProof,
33+
pub proof_upgrade_consensus_state: CommitmentProofBytes,
3534
// signer address
3635
pub signer: Signer,
3736
}
@@ -48,17 +47,12 @@ impl Protobuf<RawMsgUpgradeClient> for MsgUpgradeClient {}
4847

4948
impl From<MsgUpgradeClient> for RawMsgUpgradeClient {
5049
fn from(dm_msg: MsgUpgradeClient) -> RawMsgUpgradeClient {
51-
let c_bytes = CommitmentProofBytes::try_from(dm_msg.proof_upgrade_client)
52-
.map_or(vec![], |c| c.into());
53-
let cs_bytes = CommitmentProofBytes::try_from(dm_msg.proof_upgrade_consensus_state)
54-
.map_or(vec![], |c| c.into());
55-
5650
RawMsgUpgradeClient {
5751
client_id: dm_msg.client_id.to_string(),
58-
client_state: Some(dm_msg.client_state),
59-
consensus_state: Some(dm_msg.consensus_state),
60-
proof_upgrade_client: c_bytes,
61-
proof_upgrade_consensus_state: cs_bytes,
52+
client_state: Some(dm_msg.upgraded_client_state),
53+
consensus_state: Some(dm_msg.upgraded_consensus_state),
54+
proof_upgrade_client: dm_msg.proof_upgrade_client.into(),
55+
proof_upgrade_consensus_state: dm_msg.proof_upgrade_consensus_state.into(),
6256
signer: dm_msg.signer.to_string(),
6357
}
6458
}
@@ -90,12 +84,10 @@ impl TryFrom<RawMsgUpgradeClient> for MsgUpgradeClient {
9084
Ok(MsgUpgradeClient {
9185
client_id: ClientId::from_str(&proto_msg.client_id)
9286
.map_err(ClientError::InvalidClientIdentifier)?,
93-
client_state: raw_client_state,
94-
consensus_state: raw_consensus_state,
95-
proof_upgrade_client: RawMerkleProof::try_from(c_bytes)
96-
.map_err(UpgradeClientError::InvalidUpgradeClientProof)?,
97-
proof_upgrade_consensus_state: RawMerkleProof::try_from(cs_bytes)
98-
.map_err(UpgradeClientError::InvalidUpgradeConsensusStateProof)?,
87+
upgraded_client_state: raw_client_state,
88+
upgraded_consensus_state: raw_consensus_state,
89+
proof_upgrade_client: c_bytes,
90+
proof_upgrade_consensus_state: cs_bytes,
9991
signer: proto_msg.signer.into(),
10092
})
10193
}
@@ -105,7 +97,7 @@ impl TryFrom<RawMsgUpgradeClient> for MsgUpgradeClient {
10597
pub mod test_util {
10698
use super::*;
10799

108-
use crate::core::ics23_commitment::commitment::test_util::get_dummy_merkle_proof;
100+
use crate::core::ics23_commitment::commitment::test_util::get_dummy_commitment_proof_bytes;
109101
use crate::core::{ics02_client::height::Height, ics24_host::identifier::ClientId};
110102
use crate::mock::client_state::client_type as mock_client_type;
111103
use crate::mock::{
@@ -118,10 +110,11 @@ pub mod test_util {
118110
pub fn new_dummy(upgrade_height: Height) -> Self {
119111
MsgUpgradeClient {
120112
client_id: ClientId::new(mock_client_type(), 0).unwrap(),
121-
client_state: MockClientState::new(MockHeader::new(upgrade_height)).into(),
122-
consensus_state: MockConsensusState::new(MockHeader::new(upgrade_height)).into(),
123-
proof_upgrade_client: get_dummy_merkle_proof(),
124-
proof_upgrade_consensus_state: get_dummy_merkle_proof(),
113+
upgraded_client_state: MockClientState::new(MockHeader::new(upgrade_height)).into(),
114+
upgraded_consensus_state: MockConsensusState::new(MockHeader::new(upgrade_height))
115+
.into(),
116+
proof_upgrade_client: get_dummy_commitment_proof_bytes(),
117+
proof_upgrade_consensus_state: get_dummy_commitment_proof_bytes(),
125118
signer: get_dummy_account_id(),
126119
}
127120
}

crates/ibc/src/core/ics23_commitment/commitment.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,16 @@ impl serde::Serialize for CommitmentPrefix {
194194

195195
#[cfg(test)]
196196
pub mod test_util {
197+
use super::CommitmentProofBytes;
197198
use crate::prelude::*;
198199
use ibc_proto::ibc::core::commitment::v1::MerkleProof as RawMerkleProof;
199200
use ibc_proto::ics23::CommitmentProof;
200201

201-
/// Returns a dummy `RawMerkleProof`, for testing only!
202-
pub fn get_dummy_merkle_proof() -> RawMerkleProof {
202+
/// Returns a dummy `CommitmentProofBytes`, for testing only!
203+
pub fn get_dummy_commitment_proof_bytes() -> CommitmentProofBytes {
203204
let parsed = CommitmentProof { proof: None };
204205
let mproofs: Vec<CommitmentProof> = vec![parsed];
205-
RawMerkleProof { proofs: mproofs }
206+
let raw_mp = RawMerkleProof { proofs: mproofs };
207+
raw_mp.try_into().unwrap()
206208
}
207209
}

0 commit comments

Comments
 (0)