Skip to content

Commit 0d28c68

Browse files
authored
Merge pull request #1977 from CosmWasm/1751-refactor-mock-querier
Remove MockQuerier forwarding functions
2 parents b14e902 + 90d0432 commit 0d28c68

File tree

8 files changed

+63
-71
lines changed

8 files changed

+63
-71
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ and this project adheres to
7474
- cosmwasm-std: Add `std` feature and make it a default feature. ([#1971])
7575
- cosmwasm-std: Add `QueryRequest::Grpc` and deprecate `QueryRequest::Stargate`.
7676
([#1973])
77+
- cosmwasm-std: Remove `update_balance`, `set_denom_metadata`,
78+
`set_withdraw_address`, `set_withdraw_addresses`, `clear_withdraw_addresses`,
79+
`update_ibc` and `update_staking` from `MockQuerier` and expose the underlying
80+
queriers directly. ([#1977])
7781

7882
[#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874
7983
[#1876]: https://github.com/CosmWasm/cosmwasm/pull/1876
@@ -94,6 +98,7 @@ and this project adheres to
9498
[#1967]: https://github.com/CosmWasm/cosmwasm/pull/1967
9599
[#1971]: https://github.com/CosmWasm/cosmwasm/pull/1971
96100
[#1973]: https://github.com/CosmWasm/cosmwasm/pull/1973
101+
[#1977]: https://github.com/CosmWasm/cosmwasm/pull/1977
97102

98103
### Removed
99104

MIGRATING.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,21 @@ major releases of `cosmwasm`. Note that you can also view the
189189
+};
190190
```
191191

192+
- The `update_balance`, `set_denom_metadata`, `set_withdraw_address`,
193+
`set_withdraw_addresses` and `clear_withdraw_addresses` functions were removed
194+
from the `MockQuerier`. Use the newly exposed modules to access them directly:
195+
196+
```diff
197+
-querier.update_balance("addr", coins(1000, "ATOM"));
198+
+querier.bank.update_balance("addr", coins(1000, "ATOM"));
199+
-querier.set_withdraw_address("delegator", "withdrawer");
200+
+querier.distribution.set_withdraw_address("delegator", "withdrawer");
201+
-querier.update_staking(denom, &[], &[]);
202+
+querier.staking.update(denom, &[], &[]);
203+
-querier.update_ibc(port_id, &[]);
204+
+querier.ibc.update(port_id, &[]);
205+
```
206+
192207
- If you were using `QueryRequest::Stargate`, you might want to enable the
193208
`cosmwasm_2_0` cargo feature and migrate to `QueryRequest::Grpc` instead.
194209
While the stargate query sometimes returns protobuf encoded data and sometimes

contracts/cyberpunk/src/contract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ mod tests {
254254
fn query_denoms_works() {
255255
let mut deps = setup();
256256

257-
deps.querier.set_denom_metadata(
257+
deps.querier.bank.set_denom_metadata(
258258
&(0..98)
259259
.map(|i| DenomMetadata {
260260
symbol: format!("FOO{i}"),

contracts/hackatom/src/contract.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,9 @@ mod tests {
444444
assert_eq!(init_res.messages.len(), 0);
445445

446446
// balance changed in init
447-
deps.querier.update_balance(MOCK_CONTRACT_ADDR, init_amount);
447+
deps.querier
448+
.bank
449+
.update_balance(MOCK_CONTRACT_ADDR, init_amount);
448450

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

492494
// balance changed in init
493-
deps.querier.update_balance(MOCK_CONTRACT_ADDR, init_amount);
495+
deps.querier
496+
.bank
497+
.update_balance(MOCK_CONTRACT_ADDR, init_amount);
494498

495499
// beneficiary cannot release it
496500
let execute_info = mock_info(beneficiary.as_str(), &[]);

contracts/ibc-reflect/src/contract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ mod tests {
614614
connect(deps.as_mut(), channel_id, &account);
615615
// assign it some funds
616616
let funds = vec![coin(123456, "uatom"), coin(7654321, "tgrd")];
617-
deps.querier.update_balance(&account, funds.clone());
617+
deps.querier.bank.update_balance(&account, funds.clone());
618618

619619
// channel should be listed and have balance
620620
let raw = query(deps.as_ref(), mock_env(), QueryMsg::ListAccounts {}).unwrap();

contracts/staking/src/contract.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ pub fn query_investment(deps: Deps) -> StdResult<InvestmentResponse> {
406406
mod tests {
407407
use super::*;
408408
use cosmwasm_std::testing::{
409-
mock_dependencies, mock_env, mock_info, MockQuerier, MOCK_CONTRACT_ADDR,
409+
mock_dependencies, mock_env, mock_info, MockQuerier, StakingQuerier, MOCK_CONTRACT_ADDR,
410410
};
411411
use cosmwasm_std::{coins, Addr, Coin, CosmosMsg, Decimal, FullDelegation, Validator};
412412
use std::str::FromStr;
@@ -431,11 +431,12 @@ mod tests {
431431
}
432432

433433
fn set_validator(querier: &mut MockQuerier) {
434-
querier.update_staking("ustake", &[sample_validator(DEFAULT_VALIDATOR)], &[]);
434+
querier.staking =
435+
StakingQuerier::new("ustake", &[sample_validator(DEFAULT_VALIDATOR)], &[]);
435436
}
436437

437438
fn set_delegation(querier: &mut MockQuerier, amount: u128, denom: &str) {
438-
querier.update_staking(
439+
querier.staking.update(
439440
"ustake",
440441
&[sample_validator(DEFAULT_VALIDATOR)],
441442
&[sample_delegation(DEFAULT_VALIDATOR, coin(amount, denom))],
@@ -467,7 +468,8 @@ mod tests {
467468
fn initialization_with_missing_validator() {
468469
let mut deps = mock_dependencies();
469470
deps.querier
470-
.update_staking("ustake", &[sample_validator("john")], &[]);
471+
.staking
472+
.update("ustake", &[sample_validator("john")], &[]);
471473

472474
let creator = deps.api.addr_make("creator").to_string();
473475
let msg = InstantiateMsg {
@@ -493,7 +495,7 @@ mod tests {
493495
#[test]
494496
fn proper_initialization() {
495497
let mut deps = mock_dependencies();
496-
deps.querier.update_staking(
498+
deps.querier.staking.update(
497499
"ustake",
498500
&[
499501
sample_validator("john"),
@@ -608,6 +610,7 @@ mod tests {
608610
let rebond_msg = ExecuteMsg::_BondAllTokens {};
609611
let info = mock_info(MOCK_CONTRACT_ADDR, &[]);
610612
deps.querier
613+
.bank
611614
.update_balance(MOCK_CONTRACT_ADDR, coins(500, "ustake"));
612615
let _ = execute(deps.as_mut(), mock_env(), info, rebond_msg).unwrap();
613616

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

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

709713
// creator now tries to unbond these tokens - this must fail
710714
let unbond_msg = ExecuteMsg::Unbond {

packages/std/src/testing/mock.rs

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -462,14 +462,14 @@ pub type MockQuerierCustomHandlerResult = SystemResult<ContractResult<Binary>>;
462462
/// MockQuerier holds an immutable table of bank balances
463463
/// and configurable handlers for Wasm queries and custom queries.
464464
pub struct MockQuerier<C: DeserializeOwned = Empty> {
465-
bank: BankQuerier,
465+
pub bank: BankQuerier,
466466
#[cfg(feature = "staking")]
467-
staking: StakingQuerier,
467+
pub staking: StakingQuerier,
468468
#[cfg(feature = "cosmwasm_1_3")]
469-
distribution: DistributionQuerier,
469+
pub distribution: DistributionQuerier,
470470
wasm: WasmQuerier,
471471
#[cfg(feature = "stargate")]
472-
ibc: IbcQuerier,
472+
pub ibc: IbcQuerier,
473473
/// A handler to handle custom queries. This is set to a dummy handler that
474474
/// always errors by default. Update it via `with_custom_handler`.
475475
///
@@ -497,61 +497,6 @@ impl<C: DeserializeOwned> MockQuerier<C> {
497497
}
498498
}
499499

500-
// set a new balance for the given address and return the old balance
501-
pub fn update_balance(
502-
&mut self,
503-
addr: impl Into<String>,
504-
balance: Vec<Coin>,
505-
) -> Option<Vec<Coin>> {
506-
self.bank.update_balance(addr, balance)
507-
}
508-
509-
pub fn set_denom_metadata(&mut self, denom_metadata: &[DenomMetadata]) {
510-
self.bank.set_denom_metadata(denom_metadata);
511-
}
512-
513-
#[cfg(feature = "cosmwasm_1_3")]
514-
pub fn set_withdraw_address(
515-
&mut self,
516-
delegator_address: impl Into<String>,
517-
withdraw_address: impl Into<String>,
518-
) {
519-
self.distribution
520-
.set_withdraw_address(delegator_address, withdraw_address);
521-
}
522-
523-
/// Sets multiple withdraw addresses.
524-
///
525-
/// This allows passing multiple tuples of `(delegator_address, withdraw_address)`.
526-
/// It does not overwrite existing entries.
527-
#[cfg(feature = "cosmwasm_1_3")]
528-
pub fn set_withdraw_addresses(
529-
&mut self,
530-
withdraw_addresses: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
531-
) {
532-
self.distribution.set_withdraw_addresses(withdraw_addresses);
533-
}
534-
535-
#[cfg(feature = "cosmwasm_1_3")]
536-
pub fn clear_withdraw_addresses(&mut self) {
537-
self.distribution.clear_withdraw_addresses();
538-
}
539-
540-
#[cfg(feature = "staking")]
541-
pub fn update_staking(
542-
&mut self,
543-
denom: &str,
544-
validators: &[crate::query::Validator],
545-
delegations: &[crate::query::FullDelegation],
546-
) {
547-
self.staking = StakingQuerier::new(denom, validators, delegations);
548-
}
549-
550-
#[cfg(feature = "stargate")]
551-
pub fn update_ibc(&mut self, port_id: &str, channels: &[IbcChannel]) {
552-
self.ibc = IbcQuerier::new(port_id, channels);
553-
}
554-
555500
pub fn update_wasm<WH: 'static>(&mut self, handler: WH)
556501
where
557502
WH: Fn(&WasmQuery) -> QuerierResult,
@@ -690,6 +635,7 @@ impl BankQuerier {
690635
}
691636
}
692637

638+
/// set a new balance for the given address and return the old balance
693639
pub fn update_balance(
694640
&mut self,
695641
addr: impl Into<String>,
@@ -839,6 +785,12 @@ impl IbcQuerier {
839785
}
840786
}
841787

788+
/// Update the querier's configuration
789+
pub fn update(&mut self, port_id: impl Into<String>, channels: &[IbcChannel]) {
790+
self.port_id = port_id.into();
791+
self.channels = channels.to_vec();
792+
}
793+
842794
pub fn query(&self, request: &IbcQuery) -> QuerierResult {
843795
let contract_result: ContractResult<Binary> = match request {
844796
IbcQuery::Channel {
@@ -902,6 +854,18 @@ impl StakingQuerier {
902854
}
903855
}
904856

857+
/// Update the querier's configuration
858+
pub fn update(
859+
&mut self,
860+
denom: impl Into<String>,
861+
validators: &[Validator],
862+
delegations: &[FullDelegation],
863+
) {
864+
self.denom = denom.into();
865+
self.validators = validators.to_vec();
866+
self.delegations = delegations.to_vec();
867+
}
868+
905869
pub fn query(&self, request: &StakingQuery) -> QuerierResult {
906870
let contract_result: ContractResult<Binary> = match request {
907871
StakingQuery::BondedDenom {} => {

packages/vm/src/testing/querier.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<C: CustomQuery + DeserializeOwned> MockQuerier<C> {
3232
addr: impl Into<String>,
3333
balance: Vec<Coin>,
3434
) -> Option<Vec<Coin>> {
35-
self.querier.update_balance(addr, balance)
35+
self.querier.bank.update_balance(addr, balance)
3636
}
3737

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

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

0 commit comments

Comments
 (0)