Skip to content

Commit 4f0ab6a

Browse files
imp(api)!: refactor client relevant APIs to allow independent ICS-02 integration (informalsystems#1115)
* imp: refactor client-related APIs to allow independent ICS-02 integration * chore: add unclog * imp: keep self methods in ICS24 context + better signature for validate_self_client * nit: revert naming to host_consensus_state * imp: use client prelude for context relevant imports * nit: rename to HostClient/ConsensusState * imp: use trait bounds instead of ClientStateMut * imp: move away from AnyConsensusState assoc type at ICS07 * docs: add a docstring for client_update_meta * chore: further clean-ups * fix: revert to the initial design approach using ClientStateMut
1 parent 65d8446 commit 4f0ab6a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+582
-581
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- [ibc] Refactor client relevant APIs for improved modularity and allow
2+
standalone ICS-02 integration
3+
([\#1114](https://github.com/cosmos/ibc-rs/issues/1114))

ci/no-std-check/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ibc-clients/ics07-tendermint/src/client_state/execution.rs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
use ibc_client_tendermint_types::{
22
ClientState as ClientStateType, ConsensusState as ConsensusStateType, Header as TmHeader,
33
};
4-
use ibc_core_client::context::client_state::ClientStateExecution;
5-
use ibc_core_client::context::ClientExecutionContext;
4+
use ibc_core_client::context::prelude::*;
65
use ibc_core_client::types::error::ClientError;
76
use ibc_core_client::types::Height;
87
use ibc_core_host::types::identifiers::ClientId;
98
use ibc_core_host::types::path::{ClientConsensusStatePath, ClientStatePath};
10-
use ibc_core_host::ExecutionContext;
119
use ibc_primitives::prelude::*;
1210
use ibc_primitives::proto::Any;
1311

1412
use super::ClientState;
15-
use crate::consensus_state::ConsensusState as TmConsensusState;
16-
use crate::context::{CommonContext, ExecutionContext as TmExecutionContext};
13+
use crate::context::{
14+
ConsensusStateConverter, ExecutionContext as TmExecutionContext,
15+
ValidationContext as TmValidationContext,
16+
};
1717

1818
impl<E> ClientStateExecution<E> for ClientState
1919
where
20-
E: TmExecutionContext + ExecutionContext,
21-
<E as ClientExecutionContext>::AnyClientState: From<ClientStateType>,
22-
<E as ClientExecutionContext>::AnyConsensusState: From<ConsensusStateType>,
20+
E: TmExecutionContext,
21+
E::ClientStateRef: From<ClientStateType>,
22+
E::ConsensusStateRef: ConsensusStateConverter,
2323
{
2424
fn initialise(
2525
&self,
@@ -77,12 +77,12 @@ pub fn initialise<E>(
7777
consensus_state: Any,
7878
) -> Result<(), ClientError>
7979
where
80-
E: TmExecutionContext + ExecutionContext,
81-
<E as ClientExecutionContext>::AnyClientState: From<ClientStateType>,
82-
<E as ClientExecutionContext>::AnyConsensusState: From<ConsensusStateType>,
80+
E: TmExecutionContext,
81+
E::ClientStateRef: From<ClientStateType>,
82+
E::ConsensusStateRef: ConsensusStateConverter,
8383
{
84-
let host_timestamp = CommonContext::host_timestamp(ctx)?;
85-
let host_height = CommonContext::host_height(ctx)?;
84+
let host_timestamp = TmValidationContext::host_timestamp(ctx)?;
85+
let host_height = TmValidationContext::host_height(ctx)?;
8686

8787
let tm_consensus_state = ConsensusStateType::try_from(consensus_state)?;
8888

@@ -122,9 +122,9 @@ pub fn update_state<E>(
122122
header: Any,
123123
) -> Result<Vec<Height>, ClientError>
124124
where
125-
E: TmExecutionContext + ExecutionContext,
126-
<E as ClientExecutionContext>::AnyClientState: From<ClientStateType>,
127-
<E as ClientExecutionContext>::AnyConsensusState: From<ConsensusStateType>,
125+
E: TmExecutionContext,
126+
E::ClientStateRef: From<ClientStateType>,
127+
E::ConsensusStateRef: ConsensusStateConverter,
128128
{
129129
let header = TmHeader::try_from(header)?;
130130
let header_height = header.height();
@@ -138,7 +138,7 @@ where
138138
header_height.revision_height(),
139139
);
140140

141-
CommonContext::consensus_state(ctx, &path_at_header_height).ok()
141+
ctx.consensus_state(&path_at_header_height).ok()
142142
};
143143

144144
if maybe_existing_consensus_state.is_some() {
@@ -147,8 +147,8 @@ where
147147
//
148148
// Do nothing.
149149
} else {
150-
let host_timestamp = CommonContext::host_timestamp(ctx)?;
151-
let host_height = CommonContext::host_height(ctx)?;
150+
let host_timestamp = TmValidationContext::host_timestamp(ctx)?;
151+
let host_height = TmValidationContext::host_height(ctx)?;
152152

153153
let new_consensus_state = ConsensusStateType::from(header.clone());
154154
let new_client_state = client_state.clone().with_header(header)?;
@@ -189,9 +189,9 @@ pub fn update_on_misbehaviour<E>(
189189
_client_message: Any,
190190
) -> Result<(), ClientError>
191191
where
192-
E: TmExecutionContext + ExecutionContext,
193-
<E as ClientExecutionContext>::AnyClientState: From<ClientStateType>,
194-
<E as ClientExecutionContext>::AnyConsensusState: From<ConsensusStateType>,
192+
E: TmExecutionContext,
193+
E::ClientStateRef: From<ClientStateType>,
194+
E::ConsensusStateRef: ConsensusStateConverter,
195195
{
196196
// NOTE: frozen height is set to `Height {revision_height: 0,
197197
// revision_number: 1}` and it is the same for all misbehaviour. This
@@ -221,12 +221,12 @@ pub fn update_on_upgrade<E>(
221221
upgraded_consensus_state: Any,
222222
) -> Result<Height, ClientError>
223223
where
224-
E: TmExecutionContext + ExecutionContext,
225-
<E as ClientExecutionContext>::AnyClientState: From<ClientStateType>,
226-
<E as ClientExecutionContext>::AnyConsensusState: From<ConsensusStateType>,
224+
E: TmExecutionContext,
225+
E::ClientStateRef: From<ClientStateType>,
226+
E::ConsensusStateRef: ConsensusStateConverter,
227227
{
228228
let mut upgraded_tm_client_state = ClientState::try_from(upgraded_client_state)?;
229-
let upgraded_tm_cons_state = TmConsensusState::try_from(upgraded_consensus_state)?;
229+
let upgraded_tm_cons_state = ConsensusStateType::try_from(upgraded_consensus_state)?;
230230

231231
upgraded_tm_client_state.0.zero_custom_fields();
232232

@@ -263,12 +263,12 @@ where
263263
let new_consensus_state = ConsensusStateType::new(
264264
sentinel_root.into(),
265265
upgraded_tm_cons_state.timestamp(),
266-
upgraded_tm_cons_state.next_validators_hash(),
266+
upgraded_tm_cons_state.next_validators_hash,
267267
);
268268

269269
let latest_height = new_client_state.latest_height;
270-
let host_timestamp = CommonContext::host_timestamp(ctx)?;
271-
let host_height = CommonContext::host_height(ctx)?;
270+
let host_timestamp = TmValidationContext::host_timestamp(ctx)?;
271+
let host_height = TmValidationContext::host_height(ctx)?;
272272

273273
ctx.store_client_state(
274274
ClientStatePath::new(client_id.clone()),
@@ -301,7 +301,9 @@ pub fn prune_oldest_consensus_state<E>(
301301
client_id: &ClientId,
302302
) -> Result<(), ClientError>
303303
where
304-
E: ClientExecutionContext + CommonContext,
304+
E: ClientExecutionContext + TmValidationContext,
305+
E::ClientStateRef: From<ClientStateType>,
306+
E::ConsensusStateRef: ConsensusStateConverter,
305307
{
306308
let mut heights = ctx.consensus_state_heights(client_id)?;
307309

@@ -313,12 +315,8 @@ where
313315
height.revision_number(),
314316
height.revision_height(),
315317
);
316-
let consensus_state = CommonContext::consensus_state(ctx, &client_consensus_state_path)?;
317-
let tm_consensus_state = consensus_state
318-
.try_into()
319-
.map_err(|err| ClientError::Other {
320-
description: err.to_string(),
321-
})?;
318+
let consensus_state = ctx.consensus_state(&client_consensus_state_path)?;
319+
let tm_consensus_state = consensus_state.try_into()?;
322320

323321
let host_timestamp =
324322
ctx.host_timestamp()?

ibc-clients/ics07-tendermint/src/client_state/misbehaviour.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ibc_primitives::Timestamp;
1111
use tendermint_light_client_verifier::Verifier;
1212

1313
use super::TmValidationContext;
14-
use crate::context::TmVerifier;
14+
use crate::context::{ConsensusStateConverter, TmVerifier};
1515

1616
/// Determines whether or not two conflicting headers at the same height would
1717
/// have convinced the light client.
@@ -24,6 +24,7 @@ pub fn verify_misbehaviour<V>(
2424
) -> Result<(), ClientError>
2525
where
2626
V: TmValidationContext,
27+
V::ConsensusStateRef: ConsensusStateConverter,
2728
{
2829
misbehaviour.validate_basic()?;
2930

@@ -36,11 +37,7 @@ where
3637
);
3738
let consensus_state = ctx.consensus_state(&consensus_state_path)?;
3839

39-
consensus_state
40-
.try_into()
41-
.map_err(|err| ClientError::Other {
42-
description: err.to_string(),
43-
})?
40+
consensus_state.try_into()?
4441
};
4542

4643
let header_2 = misbehaviour.header2();
@@ -52,26 +49,22 @@ where
5249
);
5350
let consensus_state = ctx.consensus_state(&consensus_state_path)?;
5451

55-
consensus_state
56-
.try_into()
57-
.map_err(|err| ClientError::Other {
58-
description: err.to_string(),
59-
})?
52+
consensus_state.try_into()?
6053
};
6154

6255
let current_timestamp = ctx.host_timestamp()?;
6356

6457
verify_misbehaviour_header(
6558
client_state,
6659
header_1,
67-
trusted_consensus_state_1.inner(),
60+
&trusted_consensus_state_1,
6861
current_timestamp,
6962
verifier,
7063
)?;
7164
verify_misbehaviour_header(
7265
client_state,
7366
header_2,
74-
trusted_consensus_state_2.inner(),
67+
&trusted_consensus_state_2,
7568
current_timestamp,
7669
verifier,
7770
)

ibc-clients/ics07-tendermint/src/client_state/update_client.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use ibc_primitives::prelude::*;
99
use tendermint_light_client_verifier::types::{TrustedBlockState, UntrustedBlockState};
1010
use tendermint_light_client_verifier::Verifier;
1111

12-
use crate::consensus_state::ConsensusState as TmConsensusState;
13-
use crate::context::{TmVerifier, ValidationContext as TmValidationContext};
12+
use crate::context::{
13+
ConsensusStateConverter, TmVerifier, ValidationContext as TmValidationContext,
14+
};
1415

1516
pub fn verify_header<V>(
1617
client_state: &ClientStateType,
@@ -21,6 +22,7 @@ pub fn verify_header<V>(
2122
) -> Result<(), ClientError>
2223
where
2324
V: TmValidationContext,
25+
V::ConsensusStateRef: ConsensusStateConverter,
2426
{
2527
// Checks that the header fields are valid.
2628
header.validate_basic()?;
@@ -38,14 +40,11 @@ where
3840
header.trusted_height.revision_number(),
3941
header.trusted_height.revision_height(),
4042
);
41-
let trusted_consensus_state: TmConsensusState = ctx
43+
let trusted_consensus_state = ctx
4244
.consensus_state(&trusted_client_cons_state_path)?
43-
.try_into()
44-
.map_err(|err| ClientError::Other {
45-
description: err.to_string(),
46-
})?;
45+
.try_into()?;
4746

48-
header.check_trusted_next_validator_set(trusted_consensus_state.inner())?;
47+
header.check_trusted_next_validator_set(&trusted_consensus_state)?;
4948

5049
TrustedBlockState {
5150
chain_id: &client_state.chain_id.to_string().try_into().map_err(|e| {
@@ -65,7 +64,7 @@ where
6564
.to_string(),
6665
})?,
6766
next_validators: &header.trusted_next_validator_set,
68-
next_validators_hash: trusted_consensus_state.next_validators_hash(),
67+
next_validators_hash: trusted_consensus_state.next_validators_hash,
6968
}
7069
};
7170

@@ -106,6 +105,7 @@ pub fn check_for_misbehaviour_update_client<V>(
106105
) -> Result<bool, ClientError>
107106
where
108107
V: TmValidationContext,
108+
V::ConsensusStateRef: ConsensusStateConverter,
109109
{
110110
let maybe_existing_consensus_state = {
111111
let path_at_header_height = ClientConsensusStatePath::new(
@@ -119,15 +119,9 @@ where
119119

120120
match maybe_existing_consensus_state {
121121
Some(existing_consensus_state) => {
122-
let existing_consensus_state =
123-
existing_consensus_state
124-
.try_into()
125-
.map_err(|err| ClientError::Other {
126-
description: err.to_string(),
127-
})?;
122+
let existing_consensus_state = existing_consensus_state.try_into()?;
128123

129-
let header_consensus_state =
130-
TmConsensusState::from(ConsensusStateType::from(header.clone()));
124+
let header_consensus_state = ConsensusStateType::from(header.clone());
131125

132126
// There is evidence of misbehaviour if the stored consensus state
133127
// is different from the new one we received.
@@ -144,10 +138,7 @@ where
144138
if let Some(prev_cs) = maybe_prev_cs {
145139
// New header timestamp cannot occur *before* the
146140
// previous consensus state's height
147-
let prev_cs: TmConsensusState =
148-
prev_cs.try_into().map_err(|err| ClientError::Other {
149-
description: err.to_string(),
150-
})?;
141+
let prev_cs = prev_cs.try_into()?;
151142

152143
if header.signed_header.header().time <= prev_cs.timestamp() {
153144
return Ok(true);
@@ -163,9 +154,7 @@ where
163154
if let Some(next_cs) = maybe_next_cs {
164155
// New (untrusted) header timestamp cannot occur *after* next
165156
// consensus state's height
166-
let next_cs = next_cs.try_into().map_err(|err| ClientError::Other {
167-
description: err.to_string(),
168-
})?;
157+
let next_cs = next_cs.try_into()?;
169158

170159
if header.signed_header.header().time >= next_cs.timestamp() {
171160
return Ok(true);

0 commit comments

Comments
 (0)