1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use ibc_client_tendermint_types::{
    ClientState as ClientStateType, ConsensusState as ConsensusStateType, Header as TmHeader,
};
use ibc_core_client::context::prelude::*;
use ibc_core_client::types::error::ClientError;
use ibc_core_client::types::Height;
use ibc_core_host::types::identifiers::ClientId;
use ibc_core_host::types::path::{ClientConsensusStatePath, ClientStatePath};
use ibc_primitives::prelude::*;
use ibc_primitives::proto::Any;

use super::ClientState;

impl<E> ClientStateExecution<E> for ClientState
where
    E: ExtClientExecutionContext,
    E::ClientStateRef: From<ClientStateType>,
    ConsensusStateType: Convertible<E::ConsensusStateRef>,
    <ConsensusStateType as TryFrom<E::ConsensusStateRef>>::Error: Into<ClientError>,
{
    fn initialise(
        &self,
        ctx: &mut E,
        client_id: &ClientId,
        consensus_state: Any,
    ) -> Result<(), ClientError> {
        initialise(self.inner(), ctx, client_id, consensus_state)
    }

    fn update_state(
        &self,
        ctx: &mut E,
        client_id: &ClientId,
        header: Any,
    ) -> Result<Vec<Height>, ClientError> {
        update_state(self.inner(), ctx, client_id, header)
    }

    fn update_state_on_misbehaviour(
        &self,
        ctx: &mut E,
        client_id: &ClientId,
        client_message: Any,
    ) -> Result<(), ClientError> {
        update_on_misbehaviour(self.inner(), ctx, client_id, client_message)
    }

    fn update_state_on_upgrade(
        &self,
        ctx: &mut E,
        client_id: &ClientId,
        upgraded_client_state: Any,
        upgraded_consensus_state: Any,
    ) -> Result<Height, ClientError> {
        update_on_upgrade(
            self.inner(),
            ctx,
            client_id,
            upgraded_client_state,
            upgraded_consensus_state,
        )
    }

    fn update_on_recovery(
        &self,
        ctx: &mut E,
        subject_client_id: &ClientId,
        substitute_client_state: Any,
        substitute_consensus_state: Any,
    ) -> Result<(), ClientError> {
        let subject_client_state = self.inner().clone();

        update_on_recovery(
            subject_client_state,
            ctx,
            subject_client_id,
            substitute_client_state,
            substitute_consensus_state,
        )
    }
}

/// Seed the host store with initial client and consensus states.
///
/// Note that this function is typically implemented as part of the
/// [`ClientStateExecution`] trait, but has been made a standalone function
/// in order to make the ClientState APIs more flexible.
pub fn initialise<E>(
    client_state: &ClientStateType,
    ctx: &mut E,
    client_id: &ClientId,
    consensus_state: Any,
) -> Result<(), ClientError>
where
    E: ExtClientExecutionContext,
    E::ClientStateRef: From<ClientStateType>,
    ConsensusStateType: Convertible<E::ConsensusStateRef>,
{
    let host_timestamp = ExtClientValidationContext::host_timestamp(ctx)?;
    let host_height = ExtClientValidationContext::host_height(ctx)?;

    let tm_consensus_state: ConsensusStateType = consensus_state.try_into()?;

    ctx.store_client_state(
        ClientStatePath::new(client_id.clone()),
        client_state.clone().into(),
    )?;
    ctx.store_consensus_state(
        ClientConsensusStatePath::new(
            client_id.clone(),
            client_state.latest_height.revision_number(),
            client_state.latest_height.revision_height(),
        ),
        tm_consensus_state.into(),
    )?;

    ctx.store_update_meta(
        client_id.clone(),
        client_state.latest_height,
        host_timestamp,
        host_height,
    )?;

    Ok(())
}

/// Update the host store with a new client state, pruning old states from the
/// store if need be.
///
/// Note that this function is typically implemented as part of the
/// [`ClientStateExecution`] trait, but has been made a standalone function
/// in order to make the ClientState APIs more flexible.
pub fn update_state<E>(
    client_state: &ClientStateType,
    ctx: &mut E,
    client_id: &ClientId,
    header: Any,
) -> Result<Vec<Height>, ClientError>
where
    E: ExtClientExecutionContext,
    E::ClientStateRef: From<ClientStateType>,
    ConsensusStateType: Convertible<E::ConsensusStateRef>,
    <ConsensusStateType as TryFrom<E::ConsensusStateRef>>::Error: Into<ClientError>,
{
    let header = TmHeader::try_from(header)?;
    let header_height = header.height();

    prune_oldest_consensus_state(client_state, ctx, client_id)?;

    let maybe_existing_consensus_state = {
        let path_at_header_height = ClientConsensusStatePath::new(
            client_id.clone(),
            header_height.revision_number(),
            header_height.revision_height(),
        );

        ctx.consensus_state(&path_at_header_height).ok()
    };

    if maybe_existing_consensus_state.is_some() {
        // if we already had the header installed by a previous relayer
        // then this is a no-op.
        //
        // Do nothing.
    } else {
        let host_timestamp = ExtClientValidationContext::host_timestamp(ctx)?;
        let host_height = ExtClientValidationContext::host_height(ctx)?;

        let new_consensus_state = ConsensusStateType::from(header.clone());
        let new_client_state = client_state.clone().with_header(header)?;

        ctx.store_consensus_state(
            ClientConsensusStatePath::new(
                client_id.clone(),
                header_height.revision_number(),
                header_height.revision_height(),
            ),
            new_consensus_state.into(),
        )?;
        ctx.store_client_state(
            ClientStatePath::new(client_id.clone()),
            new_client_state.into(),
        )?;
        ctx.store_update_meta(
            client_id.clone(),
            header_height,
            host_timestamp,
            host_height,
        )?;
    }

    Ok(vec![header_height])
}

/// Commit a frozen client state, which was frozen as a result of having exhibited
/// misbehaviour, to the store.
///
/// Note that this function is typically implemented as part of the
/// [`ClientStateExecution`] trait, but has been made a standalone function
/// in order to make the ClientState APIs more flexible.
pub fn update_on_misbehaviour<E>(
    client_state: &ClientStateType,
    ctx: &mut E,
    client_id: &ClientId,
    _client_message: Any,
) -> Result<(), ClientError>
where
    E: ExtClientExecutionContext,
    E::ClientStateRef: From<ClientStateType>,
{
    // NOTE: frozen height is  set to `Height {revision_height: 0,
    // revision_number: 1}` and it is the same for all misbehaviour. This
    // aligns with the
    // [`ibc-go`](https://github.com/cosmos/ibc-go/blob/0e3f428e66d6fc0fc6b10d2f3c658aaa5000daf7/modules/light-clients/07-tendermint/misbehaviour.go#L18-L19)
    // implementation.
    let frozen_client_state = client_state.clone().with_frozen_height(Height::min(0));

    ctx.store_client_state(
        ClientStatePath::new(client_id.clone()),
        frozen_client_state.into(),
    )?;

    Ok(())
}

/// Commit the new client state and consensus state to the store upon a
/// successful client upgrade.
///
/// Note that this function is typically implemented as part of the
/// [`ClientStateExecution`] trait, but has been made a standalone function
/// in order to make the ClientState APIs more flexible.
pub fn update_on_upgrade<E>(
    client_state: &ClientStateType,
    ctx: &mut E,
    client_id: &ClientId,
    upgraded_client_state: Any,
    upgraded_consensus_state: Any,
) -> Result<Height, ClientError>
where
    E: ExtClientExecutionContext,
    E::ClientStateRef: From<ClientStateType>,
    ConsensusStateType: Convertible<E::ConsensusStateRef>,
{
    let mut upgraded_tm_client_state = ClientState::try_from(upgraded_client_state)?;
    let upgraded_tm_cons_state: ConsensusStateType = upgraded_consensus_state.try_into()?;

    upgraded_tm_client_state.0.zero_custom_fields();

    // Construct new client state and consensus state relayer chosen client
    // parameters are ignored. All chain-chosen parameters come from
    // committed client, all client-chosen parameters come from current
    // client.
    let new_client_state = ClientStateType::new(
        upgraded_tm_client_state.0.chain_id,
        client_state.trust_level,
        client_state.trusting_period,
        upgraded_tm_client_state.0.unbonding_period,
        client_state.max_clock_drift,
        upgraded_tm_client_state.0.latest_height,
        upgraded_tm_client_state.0.proof_specs,
        upgraded_tm_client_state.0.upgrade_path,
        client_state.allow_update,
    )?;

    // The new consensus state is merely used as a trusted kernel against
    // which headers on the new chain can be verified. The root is just a
    // stand-in sentinel value as it cannot be known in advance, thus no
    // proof verification will pass. The timestamp and the
    // NextValidatorsHash of the consensus state is the blocktime and
    // NextValidatorsHash of the last block committed by the old chain. This
    // will allow the first block of the new chain to be verified against
    // the last validators of the old chain so long as it is submitted
    // within the TrustingPeriod of this client.
    // NOTE: We do not set processed time for this consensus state since
    // this consensus state should not be used for packet verification as
    // the root is empty. The next consensus state submitted using update
    // will be usable for packet-verification.
    let sentinel_root = b"sentinel_root".to_vec();
    let new_consensus_state = ConsensusStateType::new(
        sentinel_root.into(),
        upgraded_tm_cons_state.timestamp(),
        upgraded_tm_cons_state.next_validators_hash,
    );

    let latest_height = new_client_state.latest_height;
    let host_timestamp = ExtClientValidationContext::host_timestamp(ctx)?;
    let host_height = ExtClientValidationContext::host_height(ctx)?;

    ctx.store_client_state(
        ClientStatePath::new(client_id.clone()),
        new_client_state.into(),
    )?;
    ctx.store_consensus_state(
        ClientConsensusStatePath::new(
            client_id.clone(),
            latest_height.revision_number(),
            latest_height.revision_height(),
        ),
        new_consensus_state.into(),
    )?;
    ctx.store_update_meta(
        client_id.clone(),
        latest_height,
        host_timestamp,
        host_height,
    )?;

    Ok(latest_height)
}

/// Removes consensus states from the client store whose timestamps
/// are less than or equal to the host timestamp. This ensures that
/// the client store does not amass a buildup of stale consensus states.
pub fn prune_oldest_consensus_state<E>(
    client_state: &ClientStateType,
    ctx: &mut E,
    client_id: &ClientId,
) -> Result<(), ClientError>
where
    E: ClientExecutionContext + ExtClientValidationContext,
    E::ClientStateRef: From<ClientStateType>,
    ConsensusStateType: Convertible<E::ConsensusStateRef>,
    <ConsensusStateType as TryFrom<E::ConsensusStateRef>>::Error: Into<ClientError>,
{
    let mut heights = ctx.consensus_state_heights(client_id)?;

    heights.sort();

    for height in heights {
        let client_consensus_state_path = ClientConsensusStatePath::new(
            client_id.clone(),
            height.revision_number(),
            height.revision_height(),
        );
        let consensus_state = ctx.consensus_state(&client_consensus_state_path)?;
        let tm_consensus_state: ConsensusStateType =
            consensus_state.try_into().map_err(Into::into)?;

        let host_timestamp =
            ctx.host_timestamp()?
                .into_tm_time()
                .ok_or_else(|| ClientError::Other {
                    description: String::from("host timestamp is not a valid TM timestamp"),
                })?;

        let tm_consensus_state_timestamp = tm_consensus_state.timestamp();
        let tm_consensus_state_expiry = (tm_consensus_state_timestamp
            + client_state.trusting_period)
            .map_err(|_| ClientError::Other {
                description: String::from(
                    "Timestamp overflow error occurred while attempting to parse TmConsensusState",
                ),
            })?;

        if tm_consensus_state_expiry > host_timestamp {
            break;
        }

        ctx.delete_consensus_state(client_consensus_state_path)?;
        ctx.delete_update_meta(client_id.clone(), height)?;
    }

    Ok(())
}

/// Update the `client_state`'s ID, trusting period, latest height, processed height,
/// and processed time metadata values to those values provided by a verified substitute
/// client state in response to a successful client recovery.
///
/// Note that unlike the `update_on_upgrade` function, `update_on_recovery` assumes
/// that the client being updated has already been re-initialised such that its original
/// client and consensus states have been overwritten to their new states.
///
/// This function is typically implemented as part of the [`ClientStateExecution`]
/// trait, but has been made standalone in order to enable greater flexibility
/// of the ClientState APIs.
pub fn update_on_recovery<E>(
    subject_client_state: ClientStateType,
    ctx: &mut E,
    subject_client_id: &ClientId,
    substitute_client_state: Any,
    substitute_consensus_state: Any,
) -> Result<(), ClientError>
where
    E: ExtClientExecutionContext,
    E::ClientStateRef: From<ClientStateType>,
    ConsensusStateType: Convertible<E::ConsensusStateRef>,
{
    let substitute_client_state = ClientState::try_from(substitute_client_state)?
        .inner()
        .clone();

    let chain_id = substitute_client_state.chain_id;
    let trusting_period = substitute_client_state.trusting_period;
    let latest_height = substitute_client_state.latest_height;

    let new_client_state = ClientStateType {
        chain_id,
        trusting_period,
        latest_height,
        frozen_height: None,
        ..subject_client_state
    };

    let host_timestamp = E::host_timestamp(ctx)?;
    let host_height = E::host_height(ctx)?;

    let tm_consensus_state: ConsensusStateType = substitute_consensus_state.try_into()?;

    ctx.store_consensus_state(
        ClientConsensusStatePath::new(
            subject_client_id.clone(),
            new_client_state.latest_height.revision_number(),
            new_client_state.latest_height.revision_height(),
        ),
        tm_consensus_state.into(),
    )?;

    ctx.store_client_state(
        ClientStatePath::new(subject_client_id.clone()),
        new_client_state.into(),
    )?;

    ctx.store_update_meta(
        subject_client_id.clone(),
        latest_height,
        host_timestamp,
        host_height,
    )?;

    Ok(())
}