ibc_query/core/client/
query.rs

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
//! Provides utility functions for querying IBC client states.

use ibc::core::client::context::client_state::ClientStateValidation;
use ibc::core::client::context::ClientValidationContext;
use ibc::core::client::types::error::ClientError;
use ibc::core::host::types::path::{
    ClientConsensusStatePath, ClientStatePath, Path, UpgradeClientStatePath,
    UpgradeConsensusStatePath, UPGRADED_IBC_STATE,
};
use ibc::core::host::{ConsensusStateRef, ValidationContext};
use ibc::cosmos_host::upgrade_proposal::{UpgradeValidationContext, UpgradedConsensusStateRef};
use ibc::primitives::prelude::{format, ToString};
use ibc::primitives::proto::Any;

use super::{
    ConsensusStateWithHeight, IdentifiedClientState, QueryClientStateResponse,
    QueryClientStatesRequest, QueryClientStatesResponse, QueryClientStatusRequest,
    QueryClientStatusResponse, QueryConsensusStateHeightsRequest,
    QueryConsensusStateHeightsResponse, QueryConsensusStateRequest, QueryConsensusStateResponse,
    QueryConsensusStatesRequest, QueryConsensusStatesResponse, QueryUpgradedClientStateRequest,
    QueryUpgradedClientStateResponse, QueryUpgradedConsensusStateRequest,
    QueryUpgradedConsensusStateResponse,
};
use crate::core::client::QueryClientStateRequest;
use crate::core::context::{ProvableContext, QueryContext};
use crate::error::QueryError;

/// Queries for the client state of a given client id.
pub fn query_client_state<I>(
    ibc_ctx: &I,
    request: &QueryClientStateRequest,
) -> Result<QueryClientStateResponse, QueryError>
where
    I: QueryContext,
{
    let client_id = request.client_id.clone();

    let client_val_ctx = ibc_ctx.get_client_validation_context();

    let client_state = client_val_ctx.client_state(&client_id)?;

    let proof_height = match request.query_height {
        Some(height) => height,
        None => ibc_ctx.host_height()?,
    };

    let proof = ibc_ctx
        .get_proof(
            proof_height,
            &Path::ClientState(ClientStatePath::new(client_id.clone())),
        )
        .ok_or_else(|| {
            QueryError::missing_proof(format!(
                "Proof not found for client state path: {client_id:?}"
            ))
        })?;

    Ok(QueryClientStateResponse::new(
        client_state.into(),
        proof,
        proof_height,
    ))
}

/// Queries for all the existing client states.
pub fn query_client_states<I>(
    ibc_ctx: &I,
    _request: &QueryClientStatesRequest,
) -> Result<QueryClientStatesResponse, QueryError>
where
    I: QueryContext,
{
    let client_states = ibc_ctx.client_states()?;

    Ok(QueryClientStatesResponse::new(
        client_states
            .into_iter()
            .map(|(id, state)| IdentifiedClientState::new(id, state.into()))
            .collect(),
        // no support for pagination yet
        None,
    ))
}

/// Queries for the consensus state of a given client id and height.
pub fn query_consensus_state<I>(
    ibc_ctx: &I,
    request: &QueryConsensusStateRequest,
) -> Result<QueryConsensusStateResponse, QueryError>
where
    I: QueryContext,
    ConsensusStateRef<I>: Into<Any>,
{
    let client_id = request.client_id.clone();

    let (height, consensus_state) = if let Some(height) = request.consensus_height {
        let client_val_ctx = ibc_ctx.get_client_validation_context();

        let consensus_state = client_val_ctx.consensus_state(&ClientConsensusStatePath::new(
            client_id.clone(),
            height.revision_number(),
            height.revision_height(),
        ))?;

        (height, consensus_state)
    } else {
        ibc_ctx
            .consensus_states(&client_id)?
            .into_iter()
            .max_by_key(|&(h, _)| h)
            .ok_or_else(|| {
                QueryError::missing_proof(format!(
                    "No consensus state found for client: {client_id:?}"
                ))
            })?
    };

    let proof_height = match request.query_height {
        Some(height) => height,
        None => ibc_ctx.host_height()?,
    };

    let proof = ibc_ctx
        .get_proof(
            proof_height,
            &Path::ClientConsensusState(ClientConsensusStatePath::new(
                client_id.clone(),
                height.revision_number(),
                height.revision_height(),
            )),
        )
        .ok_or_else(|| {
            QueryError::missing_proof(format!(
                "Proof not found for consensus state path: {client_id:?}"
            ))
        })?;

    Ok(QueryConsensusStateResponse::new(
        consensus_state.into(),
        proof,
        proof_height,
    ))
}

/// Queries for all the consensus states of a given client id.
pub fn query_consensus_states<I>(
    ibc_ctx: &I,
    request: &QueryConsensusStatesRequest,
) -> Result<QueryConsensusStatesResponse, QueryError>
where
    I: QueryContext,
    ConsensusStateRef<I>: Into<Any>,
{
    let consensus_states = ibc_ctx.consensus_states(&request.client_id)?;

    Ok(QueryConsensusStatesResponse::new(
        consensus_states
            .into_iter()
            .map(|(height, state)| ConsensusStateWithHeight::new(height, state.into()))
            .collect(),
        // no support for pagination yet,
        None,
    ))
}

/// Queries for the heights of all the consensus states of a given client id.
pub fn query_consensus_state_heights<I>(
    ibc_ctx: &I,
    request: &QueryConsensusStateHeightsRequest,
) -> Result<QueryConsensusStateHeightsResponse, QueryError>
where
    I: QueryContext,
{
    let consensus_state_heights = ibc_ctx.consensus_state_heights(&request.client_id)?;

    Ok(QueryConsensusStateHeightsResponse::new(
        consensus_state_heights,
        // no support for pagination yet
        None,
    ))
}

/// Queries for the status (Active, Frozen, Expired, Unauthorized) of a given client.
pub fn query_client_status<I>(
    ibc_ctx: &I,
    request: &QueryClientStatusRequest,
) -> Result<QueryClientStatusResponse, QueryError>
where
    I: ValidationContext,
{
    let client_val_ctx = ibc_ctx.get_client_validation_context();
    let client_state = client_val_ctx.client_state(&request.client_id)?;
    let client_validation_ctx = ibc_ctx.get_client_validation_context();
    let client_status = client_state.status(client_validation_ctx, &request.client_id)?;

    Ok(QueryClientStatusResponse::new(client_status))
}

/// Queries for the upgraded client state.
pub fn query_upgraded_client_state<I, U>(
    ibc_ctx: &I,
    upgrade_ctx: &U,
    request: &QueryUpgradedClientStateRequest,
) -> Result<QueryUpgradedClientStateResponse, QueryError>
where
    I: ValidationContext,
    U: UpgradeValidationContext + ProvableContext,
{
    let upgrade_path = match &request.upgrade_path {
        Some(path) if !path.is_empty() => path,
        _ => UPGRADED_IBC_STATE,
    }
    .to_string();

    let upgrade_revision_height = match request.upgrade_height {
        Some(height) => height.revision_height(),
        None => {
            upgrade_ctx
                .upgrade_plan()
                .map_err(ClientError::from)?
                .height
        }
    };

    let upgraded_client_state_path = UpgradeClientStatePath {
        upgrade_path,
        height: upgrade_revision_height,
    };

    let upgraded_client_state = upgrade_ctx
        .upgraded_client_state(&upgraded_client_state_path)
        .map_err(ClientError::from)?;

    let proof_height = match request.query_height {
        Some(height) => height,
        None => ibc_ctx.host_height()?,
    };

    let proof = upgrade_ctx
        .get_proof(
            proof_height,
            &Path::UpgradeClientState(upgraded_client_state_path),
        )
        .ok_or_else(|| {
            QueryError::missing_proof(format!(
                "Proof not found for upgraded client state at: {proof_height:?}"
            ))
        })?;

    Ok(QueryUpgradedClientStateResponse::new(
        upgraded_client_state.into(),
        proof,
        proof_height,
    ))
}

/// Queries for the upgraded consensus state.
pub fn query_upgraded_consensus_state<I, U>(
    ibc_ctx: &I,
    upgrade_ctx: &U,
    request: &QueryUpgradedConsensusStateRequest,
) -> Result<QueryUpgradedConsensusStateResponse, QueryError>
where
    I: ValidationContext,
    U: UpgradeValidationContext + ProvableContext,
    UpgradedConsensusStateRef<U>: Into<Any>,
{
    let upgrade_path = match &request.upgrade_path {
        Some(path) if !path.is_empty() => path,
        _ => UPGRADED_IBC_STATE,
    }
    .to_string();

    let upgrade_revision_height = match request.upgrade_height {
        Some(height) => height.revision_height(),
        None => {
            upgrade_ctx
                .upgrade_plan()
                .map_err(ClientError::from)?
                .height
        }
    };

    let upgraded_consensus_state_path = UpgradeConsensusStatePath {
        upgrade_path,
        height: upgrade_revision_height,
    };

    let upgraded_consensus_state = upgrade_ctx
        .upgraded_consensus_state(&upgraded_consensus_state_path)
        .map_err(ClientError::from)?;

    let proof_height = match request.query_height {
        Some(height) => height,
        None => ibc_ctx.host_height()?,
    };

    let proof = upgrade_ctx
        .get_proof(
            proof_height,
            &Path::UpgradeConsensusState(upgraded_consensus_state_path),
        )
        .ok_or_else(|| {
            QueryError::missing_proof(format!(
                "Proof not found for upgraded consensus state at: {proof_height:?}"
            ))
        })?;

    Ok(QueryUpgradedConsensusStateResponse::new(
        upgraded_consensus_state.into(),
        proof,
        proof_height,
    ))
}