Skip to content

Commit

Permalink
begin implementing ibc clientstate query
Browse files Browse the repository at this point in the history
  • Loading branch information
conorsch committed Oct 6, 2023
1 parent 38e1d7a commit 94f1106
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
11 changes: 10 additions & 1 deletion crates/bin/pcli/src/command/query/ibc_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,16 @@ impl IbcCmd {
.value
.context("Client {client_id} not found")?;

let client_state = TendermintClientState::decode(value.value.as_ref())?;
let mut ibc_client = ClientQueryClient::new(app.pd_channel().await?);
let req = QueryClientStateRequest {
client_id: client_id.to_string(),
};
let c = ibc_client
.client_state(req)
.await?
.into_inner()
.client_state;
let client_state = TendermintClientState::decode(c)?;
let client_state_json = serde_json::to_string_pretty(&client_state)?;
println!("{}", client_state_json.to_colored_json_auto()?);
}
Expand Down
38 changes: 36 additions & 2 deletions crates/core/component/ibc/src/component/rpc/client_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_trait::async_trait;

use ibc_proto::ibc::core::client::v1::query_server::Query as ClientQuery;
use ibc_proto::ibc::core::client::v1::{
IdentifiedClientState, QueryClientParamsRequest, QueryClientParamsResponse,
Height, IdentifiedClientState, QueryClientParamsRequest, QueryClientParamsResponse,
QueryClientStateRequest, QueryClientStateResponse, QueryClientStatesRequest,
QueryClientStatesResponse, QueryClientStatusRequest, QueryClientStatusResponse,
QueryConsensusStateHeightsRequest, QueryConsensusStateHeightsResponse,
Expand All @@ -12,21 +12,55 @@ use ibc_proto::ibc::core::client::v1::{
QueryUpgradedConsensusStateResponse,
};

use ibc_proto::ibc::lightclients::tendermint::v1::ClientState as TendermintClientState;
use ibc_types::core::client::ClientId;

use std::str::FromStr;
use tonic::{Response, Status};

use crate::component::ClientStateReadExt;
use prost::Message;

use super::IbcQuery;

#[async_trait]
impl ClientQuery for IbcQuery {
async fn client_state(
&self,
_request: tonic::Request<QueryClientStateRequest>,
request: tonic::Request<QueryClientStateRequest>,
) -> std::result::Result<Response<QueryClientStateResponse>, Status> {
let snapshot = self.0.latest_snapshot();
let client_id = ClientId::from_str(&request.get_ref().client_id)
.map_err(|e| tonic::Status::invalid_argument(format!("invalid client id: {e}")))?;
let height = Height {
revision_number: 0,
revision_height: snapshot.version(),
};

let (client_state, proof) = snapshot
.get_with_proof(client_id.to_string().as_bytes().to_vec())
.await
.map_err(|e| tonic::Status::aborted(format!("couldn't get client: {e}")))?;

let client_state = match client_state {
Some(c) => c,
None => {
return Err(tonic::Status::aborted(format!(
"couldn't get client: {client_id}"
)))
}
};

let client_state = TendermintClientState::try_from(client_state).map_err(|e| {
tonic::Status::aborted(format!("couldn't parse client state: {client_id}"))
})?;

let res = QueryClientStateResponse {
client_state: Some(client_state.into()),
proof: proof.encode_to_vec(),
proof_height: Some(height),
};

Err(tonic::Status::unimplemented("not implemented"))
}
/// ClientStates queries all the IBC light clients of a chain.
Expand Down

0 comments on commit 94f1106

Please sign in to comment.