Skip to content

Commit

Permalink
Rename Querier types, for cleaner public Api
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Oct 26, 2020
1 parent 5e24955 commit a7efaa3
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 32 deletions.
7 changes: 1 addition & 6 deletions contracts/burner/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ use cosmwasm_std::{

use crate::msg::{HandleMsg, InitMsg, MigrateMsg, QueryMsg};

pub fn init(
_deps: Deps,
_env: Env,
_info: MessageInfo,
_msg: InitMsg,
) -> StdResult<InitResponse> {
pub fn init(_deps: Deps, _env: Env, _info: MessageInfo, _msg: InitMsg) -> StdResult<InitResponse> {
Err(StdError::generic_err(
"You can only use this contract for migrations",
))
Expand Down
4 changes: 2 additions & 2 deletions contracts/staking/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
attr, coin, to_binary, BankMsg, Binary, Decimal, Deps, DepsRef, Env, HandleResponse, HumanAddr,
InitResponse, MessageInfo, QuerierWrapper, StakingMsg, StdError, StdResult, Uint128, WasmMsg,
InitResponse, MessageInfo, Querier, StakingMsg, StdError, StdResult, Uint128, WasmMsg,
};

use crate::errors::{StakingError, Unauthorized};
Expand Down Expand Up @@ -100,7 +100,7 @@ pub fn transfer(

// get_bonded returns the total amount of delegations from contract
// it ensures they are all the same denom
fn get_bonded(querier: &QuerierWrapper, contract: &HumanAddr) -> StdResult<Uint128> {
fn get_bonded(querier: &Querier, contract: &HumanAddr) -> StdResult<Uint128> {
let bonds = querier.query_all_delegations(contract)?;
if bonds.is_empty() {
return Ok(Uint128(0));
Expand Down
16 changes: 8 additions & 8 deletions packages/std/src/deps.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::traits::{Api, Querier, Storage};
use crate::QuerierWrapper;
use crate::traits::{Api, QuerierTrait, Storage};
use crate::Querier;

/// Holds all external dependencies of the contract.
/// Designed to allow easy dependency injection at runtime.
/// This cannot be copied or cloned since it would behave differently
/// for mock storages and a bridge storage in the VM.
pub struct OwnedDeps<S: Storage, A: Api, Q: Querier> {
pub struct OwnedDeps<S: Storage, A: Api, Q: QuerierTrait> {
pub storage: S,
pub api: A,
pub querier: Q,
Expand All @@ -14,30 +14,30 @@ pub struct OwnedDeps<S: Storage, A: Api, Q: Querier> {
pub struct Deps<'a> {
pub storage: &'a mut dyn Storage,
pub api: &'a dyn Api,
pub querier: QuerierWrapper<'a>,
pub querier: Querier<'a>,
}

#[derive(Copy, Clone)]
pub struct DepsRef<'a> {
pub storage: &'a dyn Storage,
pub api: &'a dyn Api,
pub querier: QuerierWrapper<'a>,
pub querier: Querier<'a>,
}

impl<S: Storage, A: Api, Q: Querier> OwnedDeps<S, A, Q> {
impl<S: Storage, A: Api, Q: QuerierTrait> OwnedDeps<S, A, Q> {
pub fn as_ref(&'_ self) -> DepsRef<'_> {
DepsRef {
storage: &self.storage,
api: &self.api,
querier: QuerierWrapper::new(&self.querier),
querier: Querier::new(&self.querier),
}
}

pub fn as_mut(&'_ mut self) -> Deps<'_> {
Deps {
storage: &mut self.storage,
api: &self.api,
querier: QuerierWrapper::new(&self.querier),
querier: Querier::new(&self.querier),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/std/src/entry_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// The second module should export three functions with the following signatures:
/// ```
/// # use cosmwasm_std::{
/// # Storage, Api, Querier, Deps, DepsRef, Env, StdResult, Binary, MessageInfo,
/// # Storage, Api, QuerierTrait, Deps, DepsRef, Env, StdResult, Binary, MessageInfo,
/// # InitResult, HandleResult, QueryResult,
/// # };
/// #
Expand Down Expand Up @@ -94,7 +94,7 @@ macro_rules! create_entry_points {
/// This macro is very similar to the `create_entry_points` macro, except it also requires the `migrate` method:
/// ```
/// # use cosmwasm_std::{
/// # Storage, Api, Querier, Deps, Env, StdResult, Binary, MigrateResult, MessageInfo,
/// # Storage, Api, QuerierTrait, Deps, Env, StdResult, Binary, MigrateResult, MessageInfo,
/// # };
/// # type MigrateMsg = ();
/// pub fn migrate(
Expand Down
4 changes: 2 additions & 2 deletions packages/std/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::errors::{StdError, StdResult, SystemError};
use crate::memory::{alloc, build_region, consume_region, Region};
use crate::results::SystemResult;
use crate::serde::from_slice;
use crate::traits::{Api, Querier, QuerierResult, ReadonlyStorage, Storage};
use crate::traits::{Api, QuerierResult, QuerierTrait, ReadonlyStorage, Storage};
#[cfg(feature = "iterator")]
use crate::{
iterator::{Order, KV},
Expand Down Expand Up @@ -217,7 +217,7 @@ impl ExternalQuerier {
}
}

impl Querier for ExternalQuerier {
impl QuerierTrait for ExternalQuerier {
fn raw_query(&self, bin_request: &[u8]) -> QuerierResult {
let req = build_region(bin_request);
let request_ptr = &*req as *const Region as u32;
Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub use crate::results::{
};
pub use crate::serde::{from_binary, from_slice, to_binary, to_vec};
pub use crate::storage::MemoryStorage;
pub use crate::traits::{Api, Querier, QuerierResult, QuerierWrapper, ReadonlyStorage, Storage};
pub use crate::traits::{Api, Querier, QuerierResult, QuerierTrait, ReadonlyStorage, Storage};
pub use crate::types::{BlockInfo, ContractInfo, Empty, Env, MessageInfo};

// Exposed in wasm build only
Expand Down
4 changes: 2 additions & 2 deletions packages/std/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::query::{
use crate::results::{ContractResult, SystemResult};
use crate::serde::{from_slice, to_binary};
use crate::storage::MemoryStorage;
use crate::traits::{Api, Querier, QuerierResult};
use crate::traits::{Api, QuerierResult, QuerierTrait};
use crate::types::{BlockInfo, ContractInfo, Empty, Env, MessageInfo};

pub const MOCK_CONTRACT_ADDR: &str = "cosmos2contract";
Expand Down Expand Up @@ -220,7 +220,7 @@ impl<C: DeserializeOwned> MockQuerier<C> {
}
}

impl<C: CustomQuery + DeserializeOwned> Querier for MockQuerier<C> {
impl<C: CustomQuery + DeserializeOwned> QuerierTrait for MockQuerier<C> {
fn raw_query(&self, bin_request: &[u8]) -> QuerierResult {
let request: QueryRequest<C> = match from_slice(bin_request) {
Ok(v) => v,
Expand Down
14 changes: 6 additions & 8 deletions packages/std/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub trait Api {
/// A short-hand alias for the two-level query result (1. accessing the contract, 2. executing query in the contract)
pub type QuerierResult = SystemResult<ContractResult<Binary>>;

pub trait Querier {
pub trait QuerierTrait {
/// raw_query is all that must be implemented for the Querier.
/// This allows us to pass through binary queries from one level to another without
/// knowing the custom format, or we can decode it, with the knowledge of the allowed
Expand All @@ -91,20 +91,18 @@ pub trait Querier {
}

#[derive(Copy, Clone)]
pub struct QuerierWrapper<'a> {
querier: &'a dyn Querier,
}
pub struct Querier<'a>(&'a dyn QuerierTrait);

impl<'a> QuerierWrapper<'a> {
pub fn new(querier: &'a dyn Querier) -> Self {
QuerierWrapper { querier }
impl<'a> Querier<'a> {
pub fn new(querier: &'a dyn QuerierTrait) -> Self {
Querier(querier)
}

/// This allows us to pass through binary queries from one level to another without
/// knowing the custom format, or we can decode it, with the knowledge of the allowed
/// types. You probably want one of the simpler auto-generated helper methods
pub fn raw_query(&self, bin_request: &[u8]) -> QuerierResult {
self.querier.raw_query(bin_request)
self.0.raw_query(bin_request)
}

/// query is a shorthand for custom_query when we are not using a custom type,
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/testing/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::de::DeserializeOwned;

use cosmwasm_std::testing::{MockQuerier as StdMockQuerier, MockQuerierCustomHandlerResult};
use cosmwasm_std::{
to_binary, to_vec, Binary, Coin, ContractResult, CustomQuery, Empty, HumanAddr, Querier as _,
to_binary, to_vec, Binary, Coin, ContractResult, CustomQuery, Empty, HumanAddr, QuerierTrait as _,
QueryRequest, SystemError, SystemResult,
};

Expand Down

0 comments on commit a7efaa3

Please sign in to comment.