Skip to content

Commit

Permalink
get height from grpc metadata for relevant ibc queries
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Oct 18, 2024
1 parent 87adc8d commit d8de569
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 6 deletions.
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
41 changes: 39 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 @@ -13,6 +13,7 @@ use ibc_proto::ibc::core::client::v1::{
};
use prost::Message;

use crate::component::rpc::utils::height_from_str;
use ibc_types::core::client::ClientId;
use ibc_types::core::client::Height;
use ibc_types::path::ClientConsensusStatePath;
Expand All @@ -34,7 +35,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")))?
};

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 {
Expand Down Expand Up @@ -109,7 +128,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 Down
21 changes: 20 additions & 1 deletion crates/core/component/ibc/src/component/rpc/connection_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use ibc_types::DomainType;
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 @@ -33,7 +34,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 Down
59 changes: 56 additions & 3 deletions crates/core/component/ibc/src/component/rpc/consensus_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use prost::Message;

use std::str::FromStr;

use crate::component::rpc::utils::height_from_str;
use crate::component::{ChannelStateReadExt, ConnectionStateReadExt, HostInterface};

use super::IbcQuery;
Expand Down Expand Up @@ -349,7 +350,24 @@ impl<HI: HostInterface + Send + Sync + 'static> ConsensusQuery for IbcQuery<HI>
&self,
request: tonic::Request<QueryPacketCommitmentRequest>,
) -> std::result::Result<tonic::Response<QueryPacketCommitmentResponse>, 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 port_id = PortId::from_str(&request.get_ref().port_id)
.map_err(|e| tonic::Status::aborted(format!("invalid port id: {e}")))?;
Expand Down Expand Up @@ -453,7 +471,24 @@ impl<HI: HostInterface + Send + Sync + 'static> ConsensusQuery for IbcQuery<HI>
&self,
request: tonic::Request<QueryPacketReceiptRequest>,
) -> std::result::Result<tonic::Response<QueryPacketReceiptResponse>, 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 port_id = PortId::from_str(&request.get_ref().port_id)
.map_err(|e| tonic::Status::aborted(format!("invalid port id: {e}")))?;
Expand Down Expand Up @@ -488,7 +523,25 @@ impl<HI: HostInterface + Send + Sync + 'static> ConsensusQuery for IbcQuery<HI>
request: tonic::Request<QueryPacketAcknowledgementRequest>,
) -> std::result::Result<tonic::Response<QueryPacketAcknowledgementResponse>, 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 channel_id = ChannelId::from_str(request.get_ref().channel_id.as_str())
.map_err(|e| tonic::Status::aborted(format!("invalid channel id: {e}")))?;
let port_id = PortId::from_str(request.get_ref().port_id.as_str())
Expand Down
27 changes: 27 additions & 0 deletions crates/core/component/ibc/src/component/rpc/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use anyhow::anyhow;
use ibc_proto::ibc::core::client::v1::Height;

pub(crate) fn height_from_str(value: &str) -> anyhow::Result<Height> {
let split: Vec<&str> = value.split('-').collect();

if split.len() != 2 {
return Err(anyhow!("invalid height string"));
}

let revision_number = split[0]
.parse::<u64>()
.map_err(|e| anyhow!("failed to parse revision number"))?;

let revision_height = split[1]
.parse::<u64>()
.map_err(|e| anyhow!("failed to parse revision height"))?;

if revision_number == 0 && revision_height == 0 {
return Err(anyhow!("height is zero"));
}

Ok(Height {
revision_number,
revision_height,
})
}

0 comments on commit d8de569

Please sign in to comment.