Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get height from grpc metadata for relevant ibc queries #3

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/core/component/ibc/src/component/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::HostInterface;
mod client_query;
mod connection_query;
mod consensus_query;
mod utils;

use std::marker::PhantomData;

Expand Down
93 changes: 65 additions & 28 deletions crates/core/component/ibc/src/component/rpc/client_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use ibc_types::DomainType;
use std::str::FromStr;
use tonic::{Response, Status};

use crate::component::rpc::utils::height_from_str;
use crate::component::{ClientStateReadExt, HostInterface};
use crate::prefix::MerklePrefixExt;
use crate::IBC_COMMITMENT_PREFIX;
Expand All @@ -35,7 +36,25 @@ impl<HI: HostInterface + Send + Sync + 'static> ClientQuery for IbcQuery<HI> {
&self,
request: tonic::Request<QueryClientStateRequest>,
) -> std::result::Result<Response<QueryClientStateResponse>, Status> {
let snapshot = self.storage.latest_snapshot();
let Some(height_val) = request.metadata().get("height") else {
return Err(tonic::Status::aborted("missing height"));
};

let height_str: &str = height_val
.to_str()
.map_err(|e| tonic::Status::aborted(format!("invalid height: {e}")))?;

let snapshot = if height_str == "0" {
self.storage.latest_snapshot()
} else {
let height = height_from_str(height_str)
.map_err(|e| tonic::Status::aborted(format!("couldn't get snapshot: {e}")))?;

self.storage
.snapshot(height.revision_height as u64)
.ok_or(tonic::Status::aborted(format!("invalid height")))?
};

Comment on lines +39 to +57

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code is duplicated 6 times overall? In the new utils module, this could be shortened with a fn snapshot_from_metadata or something like that?

let client_id = ClientId::from_str(&request.get_ref().client_id)
.map_err(|e| tonic::Status::invalid_argument(format!("invalid client id: {e}")))?;

Expand All @@ -55,19 +74,19 @@ impl<HI: HostInterface + Send + Sync + 'static> ClientQuery for IbcQuery<HI> {
.transpose()
.map_err(|e| tonic::Status::aborted(format!("couldn't decode client state: {e}")))?;

let res =
QueryClientStateResponse {
client_state,
proof: proof.encode_to_vec(),
proof_height: Some(ibc_proto::ibc::core::client::v1::Height {
revision_height: snapshot.get_block_height().await.map_err(|e| {
tonic::Status::aborted(format!("couldn't decode height: {e}"))
})? + 1,
revision_number: HI::get_revision_number(&snapshot).await.map_err(|e| {
tonic::Status::aborted(format!("couldn't decode height: {e}"))
})?,
}),
};
let res = QueryClientStateResponse {
client_state,
proof: proof.encode_to_vec(),
proof_height: Some(ibc_proto::ibc::core::client::v1::Height {
revision_height: HI::get_block_height(&snapshot)
.await
.map_err(|e| tonic::Status::aborted(format!("couldn't decode height: {e}")))?
+ 1,
revision_number: HI::get_revision_number(&snapshot)
.await
.map_err(|e| tonic::Status::aborted(format!("couldn't decode height: {e}")))?,
}),
};

Ok(tonic::Response::new(res))
}
Expand Down Expand Up @@ -112,7 +131,25 @@ impl<HI: HostInterface + Send + Sync + 'static> ClientQuery for IbcQuery<HI> {
&self,
request: tonic::Request<QueryConsensusStateRequest>,
) -> std::result::Result<tonic::Response<QueryConsensusStateResponse>, tonic::Status> {
let snapshot = self.storage.latest_snapshot();
let Some(height_val) = request.metadata().get("height") else {
return Err(tonic::Status::aborted("missing height"));
};

let height_str: &str = height_val
.to_str()
.map_err(|e| tonic::Status::aborted(format!("invalid height: {e}")))?;

let snapshot = if height_str == "0" {
self.storage.latest_snapshot()
} else {
let height = height_from_str(height_str)
.map_err(|e| tonic::Status::aborted(format!("couldn't get snapshot: {e}")))?;

self.storage
.snapshot(height.revision_height as u64)
.ok_or(tonic::Status::aborted(format!("invalid height")))?
};

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 = if request.get_ref().latest_height {
Expand All @@ -139,19 +176,19 @@ impl<HI: HostInterface + Send + Sync + 'static> ClientQuery for IbcQuery<HI> {
.transpose()
.map_err(|e| tonic::Status::aborted(format!("couldn't decode consensus state: {e}")))?;

let res =
QueryConsensusStateResponse {
consensus_state,
proof: proof.encode_to_vec(),
proof_height: Some(ibc_proto::ibc::core::client::v1::Height {
revision_height: snapshot.get_block_height().await.map_err(|e| {
tonic::Status::aborted(format!("couldn't decode height: {e}"))
})? + 1,
revision_number: HI::get_revision_number(&snapshot).await.map_err(|e| {
tonic::Status::aborted(format!("couldn't decode height: {e}"))
})?,
}),
};
let res = QueryConsensusStateResponse {
consensus_state,
proof: proof.encode_to_vec(),
proof_height: Some(ibc_proto::ibc::core::client::v1::Height {
revision_height: HI::get_block_height(&snapshot)
.await
.map_err(|e| tonic::Status::aborted(format!("couldn't decode height: {e}")))?
+ 1,
revision_number: HI::get_revision_number(&snapshot)
.await
.map_err(|e| tonic::Status::aborted(format!("couldn't decode height: {e}")))?,
}),
};

Ok(tonic::Response::new(res))
}
Expand Down
55 changes: 36 additions & 19 deletions crates/core/component/ibc/src/component/rpc/connection_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use penumbra_sct::component::clock::EpochRead as _;
use prost::Message;
use std::str::FromStr;

use crate::component::rpc::utils::height_from_str;
use crate::component::{ConnectionStateReadExt, HostInterface};
use crate::prefix::MerklePrefixExt;
use crate::IBC_COMMITMENT_PREFIX;
Expand All @@ -34,7 +35,25 @@ impl<HI: HostInterface + Send + Sync + 'static> ConnectionQuery for IbcQuery<HI>
request: tonic::Request<QueryConnectionRequest>,
) -> std::result::Result<tonic::Response<QueryConnectionResponse>, tonic::Status> {
tracing::debug!("querying connection {:?}", request);
let snapshot = self.storage.latest_snapshot();
let Some(height_val) = request.metadata().get("height") else {
return Err(tonic::Status::aborted("missing height"));
};

let height_str: &str = height_val
.to_str()
.map_err(|e| tonic::Status::aborted(format!("invalid height: {e}")))?;

let snapshot = if height_str == "0" {
self.storage.latest_snapshot()
} else {
let height = height_from_str(height_str)
.map_err(|e| tonic::Status::aborted(format!("couldn't get snapshot: {e}")))?;

self.storage
.snapshot(height.revision_height as u64)
.ok_or(tonic::Status::aborted(format!("invalid height")))?
};

let connection_id = &ConnectionId::from_str(&request.get_ref().connection_id)
.map_err(|e| tonic::Status::aborted(format!("invalid connection id: {e}")))?;

Expand All @@ -59,19 +78,19 @@ impl<HI: HostInterface + Send + Sync + 'static> ConnectionQuery for IbcQuery<HI>
let conn =
conn.map_err(|e| tonic::Status::aborted(format!("couldn't decode connection: {e}")))?;

let res =
QueryConnectionResponse {
connection: conn,
proof: proof.encode_to_vec(),
proof_height: Some(ibc_proto::ibc::core::client::v1::Height {
revision_height: snapshot.get_block_height().await.map_err(|e| {
tonic::Status::aborted(format!("couldn't decode height: {e}"))
})? + 1,
revision_number: HI::get_revision_number(&snapshot).await.map_err(|e| {
tonic::Status::aborted(format!("couldn't decode height: {e}"))
})?,
}),
};
let res = QueryConnectionResponse {
connection: conn,
proof: proof.encode_to_vec(),
proof_height: Some(ibc_proto::ibc::core::client::v1::Height {
revision_height: HI::get_block_height(&snapshot)
.await
.map_err(|e| tonic::Status::aborted(format!("couldn't decode height: {e}")))?
+ 1,
revision_number: HI::get_revision_number(&snapshot)
.await
.map_err(|e| tonic::Status::aborted(format!("couldn't decode height: {e}")))?,
}),
};

Ok(tonic::Response::new(res))
}
Expand Down Expand Up @@ -168,8 +187,7 @@ impl<HI: HostInterface + Send + Sync + 'static> ConnectionQuery for IbcQuery<HI>
connection_paths,
proof: proof.encode_to_vec(),
proof_height: Some(ibc_proto::ibc::core::client::v1::Height {
revision_height: snapshot
.get_block_height()
revision_height: HI::get_block_height(&snapshot)
.await
.map_err(|e| tonic::Status::aborted(format!("couldn't decode height: {e}")))?
+ 1,
Expand Down Expand Up @@ -222,8 +240,7 @@ impl<HI: HostInterface + Send + Sync + 'static> ConnectionQuery for IbcQuery<HI>
identified_client_state: Some(identified_client_state),
proof: proof.encode_to_vec(),
proof_height: Some(ibc_proto::ibc::core::client::v1::Height {
revision_height: snapshot
.get_block_height()
revision_height: HI::get_block_height(&snapshot)
.await
.map_err(|e| tonic::Status::aborted(format!("couldn't decode height: {e}")))?
+ 1,
Expand Down Expand Up @@ -280,7 +297,7 @@ impl<HI: HostInterface + Send + Sync + 'static> ConnectionQuery for IbcQuery<HI>
client_id: client_id.to_string(),
proof: proof.encode_to_vec(),
proof_height: Some(ibc_proto::ibc::core::client::v1::Height {
revision_height: snapshot.get_block_height().await.map_err(|e| {
revision_height: HI::get_block_height(&snapshot).await.map_err(|e| {
tonic::Status::aborted(format!("couldn't decode height: {e}"))
})? + 1,
revision_number: HI::get_revision_number(&snapshot).await.map_err(|e| {
Expand Down
Loading
Loading