Skip to content

Commit

Permalink
feat: allow CustomQuery in Context (#2)
Browse files Browse the repository at this point in the history
* add type bounded by CustomQuery

* short first paragraph in doc

* use lint tables

* lint use_self

* deny rust_2018_idioms

* org local dep

* update cargo lockfile
  • Loading branch information
rnbguy authored Sep 27, 2024
1 parent d96b68f commit ae3aa29
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 191 deletions.
315 changes: 161 additions & 154 deletions Cargo.lock

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,26 @@ cosmwasm-std = { version = "2.1.0" }
cosmwasm-schema = { version = "2.1.0" }
cw-storage-plus = { version = "2.0.0" }

# local dependencies
ibc-client-cw = { version = "0.54.0", path = "./ibc-clients/cw-context", default-features = false }

# ibc dependencies
ibc-core = { version = "0.54.0", default-features = false }
ibc-client-cw = { version = "0.54.0", default-features = false }
ibc-client-tendermint = { version = "0.54.0", default-features = false }
ibc-client-wasm-types = { version = "0.54.0", default-features = false }
ibc-testkit = { version = "0.54.0", default-features = false }

# cosmos dependencies
tendermint = { version = "0.38.0", default-features = false }
tendermint-testgen = { version = "0.38.0", default-features = false }

[workspace.lints.rust]
trivial_casts = "deny"
trivial_numeric_casts = "deny"
unused_import_braces = "deny"
unused_qualifications = "deny"
rust_2018_idioms = { level = "deny", priority = 2 }
unsafe_code = "forbid"

[workspace.lints.clippy]
use_self = "warn"
3 changes: 3 additions & 0 deletions ibc-clients/cw-context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ std = [
"ibc-core/std",
"ibc-client-wasm-types/std",
]

[lints]
workspace = true
26 changes: 17 additions & 9 deletions ibc-clients/cw-context/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod custom_ctx;

use std::str::FromStr;

use cosmwasm_std::{Binary, Deps, DepsMut, Empty, Env, Order, Storage};
use cosmwasm_std::{Binary, CustomQuery, Deps, DepsMut, Empty, Env, Order, Storage};
use cw_storage_plus::{Bound, Map};
use ibc_client_wasm_types::client_state::ClientState as WasmClientState;
use ibc_core::client::context::client_state::ClientStateCommon;
Expand All @@ -30,27 +30,31 @@ pub const CONSENSUS_STATE_HEIGHT_MAP: Map<(u64, u64), Empty> =

/// Context is a wrapper around the deps and env that provides access
/// to the methods under the ibc-rs Validation and Execution traits.
pub struct Context<'a, C: ClientType<'a>>
pub struct Context<'a, C, Q = Empty>
where
C: ClientType<'a>,
Q: CustomQuery,
<C::ClientState as TryFrom<Any>>::Error: Into<ClientError>,
<C::ConsensusState as TryFrom<Any>>::Error: Into<ClientError>,
{
deps: Option<Deps<'a>>,
deps_mut: Option<DepsMut<'a>>,
deps: Option<Deps<'a, Q>>,
deps_mut: Option<DepsMut<'a, Q>>,
env: Env,
client_id: ClientId,
checksum: Option<Binary>,
migration_prefix: MigrationPrefix,
client_type: std::marker::PhantomData<C>,
}

impl<'a, C: ClientType<'a>> Context<'a, C>
impl<'a, C, Q> Context<'a, C, Q>
where
C: ClientType<'a>,
Q: CustomQuery,
<C::ClientState as TryFrom<Any>>::Error: Into<ClientError>,
<C::ConsensusState as TryFrom<Any>>::Error: Into<ClientError>,
{
/// Constructs a new Context object with the given deps and env.
pub fn new_ref(deps: Deps<'a>, env: Env) -> Result<Self, ContractError> {
pub fn new_ref(deps: Deps<'a, Q>, env: Env) -> Result<Self, ContractError> {
let client_id = ClientId::from_str(env.contract.address.as_str())?;

Ok(Self {
Expand All @@ -65,7 +69,7 @@ where
}

/// Constructs a new Context object with the given deps_mut and env.
pub fn new_mut(deps_mut: DepsMut<'a>, env: Env) -> Result<Self, ContractError> {
pub fn new_mut(deps_mut: DepsMut<'a, Q>, env: Env) -> Result<Self, ContractError> {
let client_id = ClientId::from_str(env.contract.address.as_str())?;

Ok(Self {
Expand Down Expand Up @@ -256,8 +260,10 @@ pub trait StorageRef {
fn storage_ref(&self) -> &dyn Storage;
}

impl<'a, C: ClientType<'a>> StorageRef for Context<'a, C>
impl<'a, C, Q> StorageRef for Context<'a, C, Q>
where
C: ClientType<'a>,
Q: CustomQuery,
<C::ClientState as TryFrom<Any>>::Error: Into<ClientError>,
<C::ConsensusState as TryFrom<Any>>::Error: Into<ClientError>,
{
Expand All @@ -276,8 +282,10 @@ pub trait StorageMut: StorageRef {
fn storage_mut(&mut self) -> &mut dyn Storage;
}

impl<'a, C: ClientType<'a>> StorageMut for Context<'a, C>
impl<'a, C, Q> StorageMut for Context<'a, C, Q>
where
C: ClientType<'a>,
Q: CustomQuery,
<C::ClientState as TryFrom<Any>>::Error: Into<ClientError>,
<C::ConsensusState as TryFrom<Any>>::Error: Into<ClientError>,
{
Expand Down
9 changes: 0 additions & 9 deletions ibc-clients/cw-context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
//! `ibc-go` that supports the `08-wasm` proxy light client.
#![cfg_attr(not(test), deny(clippy::unwrap_used))]
#![deny(
warnings,
trivial_casts,
trivial_numeric_casts,
unused_import_braces,
unused_qualifications,
rust_2018_idioms
)]
#![forbid(unsafe_code)]

pub mod api;
pub mod context;
Expand Down
8 changes: 4 additions & 4 deletions ibc-clients/cw-context/src/types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ pub enum ContractError {
}

impl From<ContractError> for StdError {
fn from(err: ContractError) -> StdError {
StdError::generic_err(err.to_string())
fn from(err: ContractError) -> Self {
Self::generic_err(err.to_string())
}
}

impl From<ClientError> for ContractError {
fn from(err: ClientError) -> ContractError {
ContractError::Context(ContextError::ClientError(err))
fn from(err: ClientError) -> Self {
Self::Context(ContextError::ClientError(err))
}
}
7 changes: 4 additions & 3 deletions ibc-clients/cw-context/src/types/helper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ibc_client_wasm_types::{SUBJECT_PREFIX, SUBSTITUTE_PREFIX};

/// The MigrationPrefix enumerates the prefix type used during migration mode.
///
/// The migration mode is activated when there is an incoming
/// [`MigrateClientStoreMsg`](crate::types::msgs::MigrateClientStoreMsg)
/// message. It specifies the prefix key for either the
Expand All @@ -15,9 +16,9 @@ pub enum MigrationPrefix {
impl MigrationPrefix {
pub fn key(&self) -> &[u8] {
match self {
MigrationPrefix::Subject => SUBJECT_PREFIX,
MigrationPrefix::Substitute => SUBSTITUTE_PREFIX,
MigrationPrefix::None => b"",
Self::Subject => SUBJECT_PREFIX,
Self::Substitute => SUBSTITUTE_PREFIX,
Self::None => b"",
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ibc-clients/cw-context/src/types/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl TryFrom<VerifyUpgradeAndUpdateStateMsgRaw> for VerifyUpgradeAndUpdateStateM

let upgrade_consensus_state = Any::decode(&mut raw.upgrade_consensus_state.as_slice())?;

Ok(VerifyUpgradeAndUpdateStateMsg {
Ok(Self {
upgrade_client_state,
upgrade_consensus_state,
proof_upgrade_client: CommitmentProofBytes::try_from(
Expand Down
3 changes: 3 additions & 0 deletions ibc-clients/ics07-tendermint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ std = [
"ibc-client-cw/std",
"ibc-client-tendermint/std",
]

[lints]
workspace = true
9 changes: 0 additions & 9 deletions ibc-clients/ics07-tendermint/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
//! The CosmWasm contract implementation of the ICS-07 Tendermint light client
//! built using `ibc-rs`.
#![cfg_attr(not(test), deny(clippy::unwrap_used))]
#![deny(
warnings,
trivial_casts,
trivial_numeric_casts,
unused_import_braces,
unused_qualifications,
rust_2018_idioms
)]
#![forbid(unsafe_code)]

pub mod client_type;
pub mod entrypoint;
Expand Down
2 changes: 1 addition & 1 deletion ibc-clients/ics07-tendermint/src/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct Fixture {

impl Default for Fixture {
fn default() -> Self {
Fixture {
Self {
chain_id: ChainId::new("test-chain").unwrap(),
trusted_timestamp: Timestamp::now(),
trusted_height: Height::new(0, 5).unwrap(),
Expand Down

0 comments on commit ae3aa29

Please sign in to comment.