Skip to content

Commit

Permalink
Merge pull request #1977 from CosmWasm/1751-refactor-mock-querier
Browse files Browse the repository at this point in the history
Remove MockQuerier forwarding functions
  • Loading branch information
chipshort authored Dec 21, 2023
2 parents b14e902 + 90d0432 commit 0d28c68
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 71 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ and this project adheres to
- cosmwasm-std: Add `std` feature and make it a default feature. ([#1971])
- cosmwasm-std: Add `QueryRequest::Grpc` and deprecate `QueryRequest::Stargate`.
([#1973])
- cosmwasm-std: Remove `update_balance`, `set_denom_metadata`,
`set_withdraw_address`, `set_withdraw_addresses`, `clear_withdraw_addresses`,
`update_ibc` and `update_staking` from `MockQuerier` and expose the underlying
queriers directly. ([#1977])

[#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874
[#1876]: https://github.com/CosmWasm/cosmwasm/pull/1876
Expand All @@ -94,6 +98,7 @@ and this project adheres to
[#1967]: https://github.com/CosmWasm/cosmwasm/pull/1967
[#1971]: https://github.com/CosmWasm/cosmwasm/pull/1971
[#1973]: https://github.com/CosmWasm/cosmwasm/pull/1973
[#1977]: https://github.com/CosmWasm/cosmwasm/pull/1977

### Removed

Expand Down
15 changes: 15 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,21 @@ major releases of `cosmwasm`. Note that you can also view the
+};
```

- The `update_balance`, `set_denom_metadata`, `set_withdraw_address`,
`set_withdraw_addresses` and `clear_withdraw_addresses` functions were removed
from the `MockQuerier`. Use the newly exposed modules to access them directly:

```diff
-querier.update_balance("addr", coins(1000, "ATOM"));
+querier.bank.update_balance("addr", coins(1000, "ATOM"));
-querier.set_withdraw_address("delegator", "withdrawer");
+querier.distribution.set_withdraw_address("delegator", "withdrawer");
-querier.update_staking(denom, &[], &[]);
+querier.staking.update(denom, &[], &[]);
-querier.update_ibc(port_id, &[]);
+querier.ibc.update(port_id, &[]);
```

- If you were using `QueryRequest::Stargate`, you might want to enable the
`cosmwasm_2_0` cargo feature and migrate to `QueryRequest::Grpc` instead.
While the stargate query sometimes returns protobuf encoded data and sometimes
Expand Down
2 changes: 1 addition & 1 deletion contracts/cyberpunk/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ mod tests {
fn query_denoms_works() {
let mut deps = setup();

deps.querier.set_denom_metadata(
deps.querier.bank.set_denom_metadata(
&(0..98)
.map(|i| DenomMetadata {
symbol: format!("FOO{i}"),
Expand Down
8 changes: 6 additions & 2 deletions contracts/hackatom/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,9 @@ mod tests {
assert_eq!(init_res.messages.len(), 0);

// balance changed in init
deps.querier.update_balance(MOCK_CONTRACT_ADDR, init_amount);
deps.querier
.bank
.update_balance(MOCK_CONTRACT_ADDR, init_amount);

// beneficiary can release it
let execute_info = mock_info(verifier.as_str(), &[]);
Expand Down Expand Up @@ -490,7 +492,9 @@ mod tests {
assert_eq!(init_res.messages.len(), 0);

// balance changed in init
deps.querier.update_balance(MOCK_CONTRACT_ADDR, init_amount);
deps.querier
.bank
.update_balance(MOCK_CONTRACT_ADDR, init_amount);

// beneficiary cannot release it
let execute_info = mock_info(beneficiary.as_str(), &[]);
Expand Down
2 changes: 1 addition & 1 deletion contracts/ibc-reflect/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ mod tests {
connect(deps.as_mut(), channel_id, &account);
// assign it some funds
let funds = vec![coin(123456, "uatom"), coin(7654321, "tgrd")];
deps.querier.update_balance(&account, funds.clone());
deps.querier.bank.update_balance(&account, funds.clone());

// channel should be listed and have balance
let raw = query(deps.as_ref(), mock_env(), QueryMsg::ListAccounts {}).unwrap();
Expand Down
16 changes: 10 additions & 6 deletions contracts/staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub fn query_investment(deps: Deps) -> StdResult<InvestmentResponse> {
mod tests {
use super::*;
use cosmwasm_std::testing::{
mock_dependencies, mock_env, mock_info, MockQuerier, MOCK_CONTRACT_ADDR,
mock_dependencies, mock_env, mock_info, MockQuerier, StakingQuerier, MOCK_CONTRACT_ADDR,
};
use cosmwasm_std::{coins, Addr, Coin, CosmosMsg, Decimal, FullDelegation, Validator};
use std::str::FromStr;
Expand All @@ -431,11 +431,12 @@ mod tests {
}

fn set_validator(querier: &mut MockQuerier) {
querier.update_staking("ustake", &[sample_validator(DEFAULT_VALIDATOR)], &[]);
querier.staking =
StakingQuerier::new("ustake", &[sample_validator(DEFAULT_VALIDATOR)], &[]);
}

fn set_delegation(querier: &mut MockQuerier, amount: u128, denom: &str) {
querier.update_staking(
querier.staking.update(
"ustake",
&[sample_validator(DEFAULT_VALIDATOR)],
&[sample_delegation(DEFAULT_VALIDATOR, coin(amount, denom))],
Expand Down Expand Up @@ -467,7 +468,8 @@ mod tests {
fn initialization_with_missing_validator() {
let mut deps = mock_dependencies();
deps.querier
.update_staking("ustake", &[sample_validator("john")], &[]);
.staking
.update("ustake", &[sample_validator("john")], &[]);

let creator = deps.api.addr_make("creator").to_string();
let msg = InstantiateMsg {
Expand All @@ -493,7 +495,7 @@ mod tests {
#[test]
fn proper_initialization() {
let mut deps = mock_dependencies();
deps.querier.update_staking(
deps.querier.staking.update(
"ustake",
&[
sample_validator("john"),
Expand Down Expand Up @@ -608,6 +610,7 @@ mod tests {
let rebond_msg = ExecuteMsg::_BondAllTokens {};
let info = mock_info(MOCK_CONTRACT_ADDR, &[]);
deps.querier
.bank
.update_balance(MOCK_CONTRACT_ADDR, coins(500, "ustake"));
let _ = execute(deps.as_mut(), mock_env(), info, rebond_msg).unwrap();

Expand Down Expand Up @@ -699,12 +702,13 @@ mod tests {
let rebond_msg = ExecuteMsg::_BondAllTokens {};
let info = mock_info(MOCK_CONTRACT_ADDR, &[]);
deps.querier
.bank
.update_balance(MOCK_CONTRACT_ADDR, coins(500, "ustake"));
let _ = execute(deps.as_mut(), mock_env(), info, rebond_msg).unwrap();

// update the querier with new bond, lower balance
set_delegation(&mut deps.querier, 1500, "ustake");
deps.querier.update_balance(MOCK_CONTRACT_ADDR, vec![]);
deps.querier.bank.update_balance(MOCK_CONTRACT_ADDR, vec![]);

// creator now tries to unbond these tokens - this must fail
let unbond_msg = ExecuteMsg::Unbond {
Expand Down
82 changes: 23 additions & 59 deletions packages/std/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,14 @@ pub type MockQuerierCustomHandlerResult = SystemResult<ContractResult<Binary>>;
/// MockQuerier holds an immutable table of bank balances
/// and configurable handlers for Wasm queries and custom queries.
pub struct MockQuerier<C: DeserializeOwned = Empty> {
bank: BankQuerier,
pub bank: BankQuerier,
#[cfg(feature = "staking")]
staking: StakingQuerier,
pub staking: StakingQuerier,
#[cfg(feature = "cosmwasm_1_3")]
distribution: DistributionQuerier,
pub distribution: DistributionQuerier,
wasm: WasmQuerier,
#[cfg(feature = "stargate")]
ibc: IbcQuerier,
pub ibc: IbcQuerier,
/// A handler to handle custom queries. This is set to a dummy handler that
/// always errors by default. Update it via `with_custom_handler`.
///
Expand Down Expand Up @@ -497,61 +497,6 @@ impl<C: DeserializeOwned> MockQuerier<C> {
}
}

// set a new balance for the given address and return the old balance
pub fn update_balance(
&mut self,
addr: impl Into<String>,
balance: Vec<Coin>,
) -> Option<Vec<Coin>> {
self.bank.update_balance(addr, balance)
}

pub fn set_denom_metadata(&mut self, denom_metadata: &[DenomMetadata]) {
self.bank.set_denom_metadata(denom_metadata);
}

#[cfg(feature = "cosmwasm_1_3")]
pub fn set_withdraw_address(
&mut self,
delegator_address: impl Into<String>,
withdraw_address: impl Into<String>,
) {
self.distribution
.set_withdraw_address(delegator_address, withdraw_address);
}

/// Sets multiple withdraw addresses.
///
/// This allows passing multiple tuples of `(delegator_address, withdraw_address)`.
/// It does not overwrite existing entries.
#[cfg(feature = "cosmwasm_1_3")]
pub fn set_withdraw_addresses(
&mut self,
withdraw_addresses: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
) {
self.distribution.set_withdraw_addresses(withdraw_addresses);
}

#[cfg(feature = "cosmwasm_1_3")]
pub fn clear_withdraw_addresses(&mut self) {
self.distribution.clear_withdraw_addresses();
}

#[cfg(feature = "staking")]
pub fn update_staking(
&mut self,
denom: &str,
validators: &[crate::query::Validator],
delegations: &[crate::query::FullDelegation],
) {
self.staking = StakingQuerier::new(denom, validators, delegations);
}

#[cfg(feature = "stargate")]
pub fn update_ibc(&mut self, port_id: &str, channels: &[IbcChannel]) {
self.ibc = IbcQuerier::new(port_id, channels);
}

pub fn update_wasm<WH: 'static>(&mut self, handler: WH)
where
WH: Fn(&WasmQuery) -> QuerierResult,
Expand Down Expand Up @@ -690,6 +635,7 @@ impl BankQuerier {
}
}

/// set a new balance for the given address and return the old balance
pub fn update_balance(
&mut self,
addr: impl Into<String>,
Expand Down Expand Up @@ -839,6 +785,12 @@ impl IbcQuerier {
}
}

/// Update the querier's configuration
pub fn update(&mut self, port_id: impl Into<String>, channels: &[IbcChannel]) {
self.port_id = port_id.into();
self.channels = channels.to_vec();
}

pub fn query(&self, request: &IbcQuery) -> QuerierResult {
let contract_result: ContractResult<Binary> = match request {
IbcQuery::Channel {
Expand Down Expand Up @@ -902,6 +854,18 @@ impl StakingQuerier {
}
}

/// Update the querier's configuration
pub fn update(
&mut self,
denom: impl Into<String>,
validators: &[Validator],
delegations: &[FullDelegation],
) {
self.denom = denom.into();
self.validators = validators.to_vec();
self.delegations = delegations.to_vec();
}

pub fn query(&self, request: &StakingQuery) -> QuerierResult {
let contract_result: ContractResult<Binary> = match request {
StakingQuery::BondedDenom {} => {
Expand Down
4 changes: 2 additions & 2 deletions packages/vm/src/testing/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<C: CustomQuery + DeserializeOwned> MockQuerier<C> {
addr: impl Into<String>,
balance: Vec<Coin>,
) -> Option<Vec<Coin>> {
self.querier.update_balance(addr, balance)
self.querier.bank.update_balance(addr, balance)
}

#[cfg(feature = "staking")]
Expand All @@ -42,7 +42,7 @@ impl<C: CustomQuery + DeserializeOwned> MockQuerier<C> {
validators: &[cosmwasm_std::Validator],
delegations: &[cosmwasm_std::FullDelegation],
) {
self.querier.update_staking(denom, validators, delegations);
self.querier.staking.update(denom, validators, delegations);
}

pub fn update_wasm<WH: 'static>(&mut self, handler: WH)
Expand Down

0 comments on commit 0d28c68

Please sign in to comment.