Skip to content

Commit

Permalink
feat(ibc-query): define request/response domain types (cosmos#1129)
Browse files Browse the repository at this point in the history
* feat: Add domain definitions for request/response query types in ibc-query

* chore: add unclog

* feat: define serde & schema features

* fix: derive missing schemars

* docs: add remarks to README + additional unclog

* chore: some clean-ups

* chore: organize orders

* feat: add TryFrom for response types

* use invalid_argument error code

* rm redudant lint relaxing

* use proof_not_found method

* consistent QueryError enum structure

* Update ibc-query/README.md

Signed-off-by: Rano | Ranadeep <[email protected]>

* clippy::match_bool

* clippy::use_self

* clippy::or_fun_call

* use if-let-some over match

* clippy::redundant_closure_for_method_calls

* Into::into over T::from

* clippy::uninlined_format_args

* clippy::cast_lossless

* clippy::impl_trait_in_params

* clippy::shadow_reuse

* clippy::pattern_type_mismatch

* safe use of u64::MAX

* then over then_some

* rename IntoProto to IntoResponse

---------

Signed-off-by: Rano | Ranadeep <[email protected]>
Co-authored-by: Ranadeep Biswas <[email protected]>
Co-authored-by: Rano | Ranadeep <[email protected]>
  • Loading branch information
3 people authored Mar 18, 2024
1 parent bef3683 commit d773a23
Show file tree
Hide file tree
Showing 30 changed files with 2,965 additions and 455 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [ibc-core] Add `schemars` derivation on some more IBC core structs like
`Status`, `ConnectionEnd` and `ChannelEnd`
([\#1129](https://github.com/cosmos/ibc-rs/pull/1129))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [ibc-query] Add domain definitions for request/response query types and their
corresponding conversions to/from protos
([\#1128](https://github.com/cosmos/ibc-rs/issues/1128))
23 changes: 22 additions & 1 deletion ibc-core/ics02-client/types/src/status.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use core::fmt::{Debug, Display, Formatter};
use core::str::FromStr;

use ibc_primitives::prelude::*;

use crate::error::ClientError;

Expand All @@ -16,7 +19,9 @@ pub enum UpdateKind {
}

/// Represents the status of a client
#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum Status {
/// The client is active and allowed to be used
Active,
Expand Down Expand Up @@ -55,3 +60,19 @@ impl Display for Status {
write!(f, "{self:?}")
}
}

impl FromStr for Status {
type Err = ClientError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"ACTIVE" => Ok(Status::Active),
"FROZEN" => Ok(Status::Frozen),
"EXPIRED" => Ok(Status::Expired),
"UNAUTHORIZED" => Ok(Status::Unauthorized),
_ => Err(ClientError::Other {
description: format!("invalid status string: {s}"),
}),
}
}
}
3 changes: 3 additions & 0 deletions ibc-core/ics03-connection/types/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::version::Version;
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct IdentifiedConnectionEnd {
pub connection_id: ConnectionId,
Expand Down Expand Up @@ -100,6 +101,7 @@ impl From<IdentifiedConnectionEnd> for RawIdentifiedConnection {
derive(parity_scale_codec::Encode, parity_scale_codec::Decode,)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ConnectionEnd {
pub state: State,
Expand Down Expand Up @@ -466,6 +468,7 @@ impl Counterparty {
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum State {
Uninitialized = 0isize,
Expand Down
4 changes: 4 additions & 0 deletions ibc-core/ics04-channel/types/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::Version;
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IdentifiedChannelEnd {
pub port_id: PortId,
Expand Down Expand Up @@ -107,6 +108,7 @@ impl From<IdentifiedChannelEnd> for RawIdentifiedChannel {
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ChannelEnd {
pub state: State,
Expand Down Expand Up @@ -349,6 +351,7 @@ pub(crate) fn verify_connection_hops_length(
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Counterparty {
pub port_id: PortId,
Expand Down Expand Up @@ -514,6 +517,7 @@ impl FromStr for Order {
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum State {
Uninitialized = 0isize,
Expand Down
2 changes: 2 additions & 0 deletions ibc-core/ics04-channel/types/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::timeout::TimeoutHeight;
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PacketCommitment(Vec<u8>);

Expand Down Expand Up @@ -55,6 +56,7 @@ impl From<Vec<u8>> for PacketCommitment {
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AcknowledgementCommitment(Vec<u8>);

Expand Down
28 changes: 23 additions & 5 deletions ibc-query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,37 @@ repository = { workspace = true }
readme = "README.md"
keywords = ["blockchain", "cosmos", "ibc", "rpc", "grpc"]
description = """
Maintained by `ibc-rs`, contains essential IBC query methods and
gRPC query service implementations for the IBC core.
Maintained by `ibc-rs`, contains essential IBC query types, utility functions and
gRPC service implementations for the IBC core.
"""

[dependencies]
# external dependencies
displaydoc = { version = "0.2", default-features = false }
tonic = "0.10"
schemars = { workspace = true , optional = true }
serde = { workspace = true, optional = true }
tonic = "0.10"

# ibc dependencies
ibc = { workspace = true }
ibc = { workspace = true }
ibc-proto = { workspace = true, features = ["server"] }

[features]
default = ["std"]
std = ["ibc-proto/std", "ibc/std"]
std = [
"ibc/std",
"ibc-proto/std",
]
serde = [
"dep:serde",
"ibc/serde",
"ibc-proto/serde",
]
schema = [
"dep:schemars",
"ibc/schema",
"ibc-proto/json-schema",
"serde",
"std",
]

34 changes: 30 additions & 4 deletions ibc-query/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
# IBC Query

This crate provides a set of utility traits and implementations for querying the
state of an [`ibc-rs`](https://github.com/cosmos/ibc-rs) enabled chain.
## Overview

Implementations contain essential IBC query methods and gRPC query services for
each of the IBC core client, connection, and channel layers.
This crate offers a comprehensive set of utility types, traits, and functions
designed for integrating either a gRPC query server or implementing RPC methods
in hosts. It specifically facilitates querying the state of the IBC core client,
connection, and channel layers of a chain enabled with `ibc-rs`.

## Features

- Provides essential utility request/response domain types and their conversions
to the proto types for efficient integration.
- Provides convenient query objects with pre-implemented gRPC query services.
- Offers convenient objects on which query service has been implemented and
- Includes convenient `QueryContext` and `ProvableContext` traits that extend
the capabilities of an implemented IBC module, enabling the retrieval of state
from the chain.
- Derives `serde` and `schema` for all the domain types enabling easy
(de)serialization. This feature is particularly beneficial for JSON RPC
implementations.

## Remarks

- At present, the Protobuf representation of request types does not include
support for querying at a specific height. Consequently, the current state of
`ibc-query` allows conversion from protos as a compatible direction but does
not support conversion into protos due to the absence of the `query_height`
fields.

- Currently `ibc-query` does not support pagination. If pagination is a
requirement for your project, please open an issue and provide details about
your usage.
2 changes: 2 additions & 0 deletions ibc-query/src/core/channel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod query;
mod service;
mod types;

pub use query::*;
pub use service::*;
pub use types::*;
Loading

0 comments on commit d773a23

Please sign in to comment.