Skip to content

Commit

Permalink
Merge pull request #881 from CosmWasm/flag-staking-query
Browse files Browse the repository at this point in the history
Hide staking query symbols behind staking feature flag
  • Loading branch information
ethanfrey authored Apr 15, 2021
2 parents 4f74d68 + 2fa131a commit afe6466
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 252 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,11 @@ and this project adheres to
`HumanAddr` to `String` ([#802]).
- cosmwasm-std: `Api::addr_humanize` now returns `Addr` instead of `HumanAddr`
([#802]).
- cosmwasm-std: Hide `StakingMsg` and `CosmosMsg::Staking` behind the `staking`
feature flag to make those only available in contracts built for PoS chains.
- cosmwasm-std: Hide `StakingMsg`, `CosmosMsg::Staking`,
`AllDelegationsResponse`, `BondedDenomResponse`, `Delegation`,
`FullDelegation`, `StakingQuery`, `Validator`, `ValidatorsResponse` and
`testing::StakingQuerier` behind the `staking` feature flag to make those only
available in contracts built for PoS chains.
- cosmwasm-std: Remove `StakingMsg::Withdraw` in favour of
`DistributionMsg::SetWithdrawAddress` and
`DistributionMsg::WithdrawDelegatorReward` ([#848]).
Expand Down
13 changes: 9 additions & 4 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ pub use crate::ibc::{
pub use crate::iterator::{Order, Pair, KV};
pub use crate::math::{Decimal, Fraction, Uint128};
pub use crate::query::{
AllBalanceResponse, AllDelegationsResponse, BalanceResponse, BankQuery, BondedDenomResponse,
CustomQuery, Delegation, FullDelegation, QueryRequest, StakingQuery, Validator,
ValidatorsResponse, WasmQuery,
AllBalanceResponse, BalanceResponse, BankQuery, CustomQuery, QueryRequest, WasmQuery,
};
#[cfg(feature = "staking")]
pub use crate::query::{
AllDelegationsResponse, BondedDenomResponse, Delegation, FullDelegation, StakingQuery,
Validator, ValidatorsResponse,
};
pub use crate::results::{
attr, wasm_execute, wasm_instantiate, Attribute, BankMsg, ContractResult, CosmosMsg, Empty,
Expand Down Expand Up @@ -88,10 +91,12 @@ pub use crate::ibc_exports::{
mod mock;
#[cfg(not(target_arch = "wasm32"))]
pub mod testing {
#[cfg(feature = "staking")]
pub use crate::mock::StakingQuerier;
pub use crate::mock::{
digit_sum, mock_dependencies, mock_dependencies_with_balances, mock_env, mock_info,
riffle_shuffle, BankQuerier, MockApi, MockQuerier, MockQuerierCustomHandlerResult,
MockStorage, StakingQuerier, MOCK_CONTRACT_ADDR,
MockStorage, MOCK_CONTRACT_ADDR,
};
#[cfg(feature = "stargate")]
pub use crate::mock::{mock_ibc_channel, mock_ibc_packet_ack, mock_ibc_packet_recv};
Expand Down
23 changes: 18 additions & 5 deletions packages/std/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ use crate::errors::{RecoverPubkeyError, StdError, StdResult, SystemError, Verifi
#[cfg(feature = "stargate")]
use crate::ibc::{IbcChannel, IbcEndpoint, IbcOrder, IbcPacket, IbcTimeoutBlock};
use crate::query::{
AllBalanceResponse, AllDelegationsResponse, BalanceResponse, BankQuery, BondedDenomResponse,
CustomQuery, DelegationResponse, FullDelegation, QueryRequest, StakingQuery, Validator,
ValidatorsResponse, WasmQuery,
AllBalanceResponse, BalanceResponse, BankQuery, CustomQuery, QueryRequest, WasmQuery,
};
#[cfg(feature = "staking")]
use crate::query::{
AllDelegationsResponse, BondedDenomResponse, DelegationResponse, FullDelegation, StakingQuery,
Validator, ValidatorsResponse,
};
use crate::results::{ContractResult, Empty, SystemResult};
use crate::serde::{from_slice, to_binary};
Expand Down Expand Up @@ -280,6 +283,7 @@ pub type MockQuerierCustomHandlerResult = SystemResult<ContractResult<Binary>>;
/// TODO: also allow querying contracts
pub struct MockQuerier<C: DeserializeOwned = Empty> {
bank: BankQuerier,
#[cfg(feature = "staking")]
staking: StakingQuerier,
// placeholder to add support later
wasm: NoWasmQuerier,
Expand All @@ -294,6 +298,7 @@ impl<C: DeserializeOwned> MockQuerier<C> {
pub fn new(balances: &[(&str, &[Coin])]) -> Self {
MockQuerier {
bank: BankQuerier::new(balances),
#[cfg(feature = "staking")]
staking: StakingQuerier::default(),
wasm: NoWasmQuerier {},
// strange argument notation suggested as a workaround here: https://github.com/rust-lang/rust/issues/41078#issuecomment-294296365
Expand Down Expand Up @@ -353,6 +358,7 @@ impl<C: CustomQuery + DeserializeOwned> MockQuerier<C> {
match &request {
QueryRequest::Bank(bank_query) => self.bank.query(bank_query),
QueryRequest::Custom(custom_query) => (*self.custom_handler)(custom_query),
#[cfg(feature = "staking")]
QueryRequest::Staking(staking_query) => self.staking.query(staking_query),
QueryRequest::Wasm(msg) => self.wasm.query(msg),
#[cfg(feature = "stargate")]
Expand Down Expand Up @@ -427,13 +433,15 @@ impl BankQuerier {
}
}

#[cfg(feature = "staking")]
#[derive(Clone, Default)]
pub struct StakingQuerier {
denom: String,
validators: Vec<Validator>,
delegations: Vec<FullDelegation>,
}

#[cfg(feature = "staking")]
impl StakingQuerier {
pub fn new(denom: &str, validators: &[Validator], delegations: &[FullDelegation]) -> Self {
StakingQuerier {
Expand Down Expand Up @@ -513,8 +521,9 @@ pub fn digit_sum(input: &[u8]) -> usize {
#[cfg(test)]
mod tests {
use super::*;
use crate::query::Delegation;
use crate::{coin, coins, from_binary, Decimal};
use crate::{coin, coins, from_binary};
#[cfg(feature = "staking")]
use crate::{Decimal, Delegation};
use hex_literal::hex;

const SECP256K1_MSG_HASH_HEX: &str =
Expand Down Expand Up @@ -863,6 +872,7 @@ mod tests {
assert_eq!(res.amount, coin(0, "ELF"));
}

#[cfg(feature = "staking")]
#[test]
fn staking_querier_validators() {
let val1 = Validator {
Expand All @@ -889,6 +899,7 @@ mod tests {
assert_eq!(vals.validators, vec![val1, val2]);
}

#[cfg(feature = "staking")]
// gets delegators from query or panic
fn get_all_delegators<U: Into<String>>(
staking: &StakingQuerier,
Expand All @@ -904,6 +915,7 @@ mod tests {
dels.delegations
}

#[cfg(feature = "staking")]
// gets full delegators from query or panic
fn get_delegator<U: Into<String>, V: Into<String>>(
staking: &StakingQuerier,
Expand All @@ -921,6 +933,7 @@ mod tests {
dels.delegation
}

#[cfg(feature = "staking")]
#[test]
fn staking_querier_delegations() {
let val1 = String::from("validator-one");
Expand Down
241 changes: 0 additions & 241 deletions packages/std/src/query.rs

This file was deleted.

Loading

0 comments on commit afe6466

Please sign in to comment.