Skip to content

Commit b4ab089

Browse files
committed
Add StakingQuerier to MockQuerier
1 parent 21b29df commit b4ab089

File tree

2 files changed

+83
-11
lines changed

2 files changed

+83
-11
lines changed

packages/std/src/mock.rs

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::coins::Coin;
66
use crate::encoding::Binary;
77
use crate::errors::{contract_err, StdResult, Utf8StringErr};
88
use crate::query::{AllBalanceResponse, BalanceResponse, BankQuery, QueryRequest, WasmQuery};
9-
use crate::serde::to_vec;
9+
use crate::serde::to_binary;
1010
use crate::storage::MemoryStorage;
1111
use crate::traits::{Api, Extern, Querier};
1212
use crate::types::{BlockInfo, CanonicalAddr, ContractInfo, Env, HumanAddr, MessageInfo};
@@ -127,14 +127,34 @@ pub fn mock_env<T: Api, U: Into<HumanAddr>>(api: &T, sender: U, sent: &[Coin]) -
127127
#[derive(Clone)]
128128
pub struct MockQuerier {
129129
bank: BankQuerier,
130+
#[cfg(feature = "staking")]
131+
staking: staking::StakingQuerier,
130132
}
131133

132134
impl MockQuerier {
135+
#[cfg(not(feature = "staking"))]
133136
pub fn new(balances: &[(&HumanAddr, &[Coin])]) -> Self {
134137
MockQuerier {
135138
bank: BankQuerier::new(balances),
136139
}
137140
}
141+
142+
#[cfg(feature = "staking")]
143+
pub fn new(balances: &[(&HumanAddr, &[Coin])]) -> Self {
144+
MockQuerier {
145+
bank: BankQuerier::new(balances),
146+
staking: staking::StakingQuerier::new(&[], &[]),
147+
}
148+
}
149+
150+
#[cfg(feature = "staking")]
151+
pub fn with_staking(
152+
&mut self,
153+
validators: &[crate::query::Validator],
154+
delegations: &[crate::query::Delegation],
155+
) {
156+
self.staking = staking::StakingQuerier::new(validators, delegations);
157+
}
138158
}
139159

140160
#[derive(Clone)]
@@ -166,16 +186,70 @@ impl BankQuerier {
166186
denom: denom.to_string(),
167187
},
168188
};
169-
let api_res = to_vec(&bank_res).map(Binary).map_err(|e| e.into());
170-
Ok(api_res)
189+
Ok(to_binary(&bank_res).map_err(|e| e.into()))
171190
}
172191
BankQuery::AllBalances { address } => {
173192
// proper error on not found, serialize result on found
174193
let bank_res = AllBalanceResponse {
175194
amount: self.balances.get(address).cloned().unwrap_or_default(),
176195
};
177-
let api_res = to_vec(&bank_res).map(Binary).map_err(|e| e.into());
178-
Ok(api_res)
196+
Ok(to_binary(&bank_res).map_err(|e| e.into()))
197+
}
198+
}
199+
}
200+
}
201+
202+
#[cfg(feature = "staking")]
203+
mod staking {
204+
use crate::api::{ApiError, ApiSystemError};
205+
use crate::encoding::Binary;
206+
use crate::query::{
207+
Delegation, DelegationsResponse, StakingQuery, Validator, ValidatorsResponse,
208+
};
209+
use crate::to_binary;
210+
211+
#[derive(Clone)]
212+
pub struct StakingQuerier {
213+
validators: Vec<Validator>,
214+
delegations: Vec<Delegation>,
215+
}
216+
217+
impl StakingQuerier {
218+
pub fn new(validators: &[Validator], delegations: &[Delegation]) -> Self {
219+
StakingQuerier {
220+
validators: validators.to_vec(),
221+
delegations: delegations.to_vec(),
222+
}
223+
}
224+
225+
pub fn query(
226+
&self,
227+
request: &StakingQuery,
228+
) -> Result<Result<Binary, ApiError>, ApiSystemError> {
229+
match request {
230+
StakingQuery::Validators {} => {
231+
let val_res = ValidatorsResponse {
232+
validators: self.validators.clone(),
233+
};
234+
Ok(to_binary(&val_res).map_err(|e| e.into()))
235+
}
236+
StakingQuery::Delegations {
237+
delegator,
238+
validator,
239+
} => {
240+
let matches = |d: &&Delegation| {
241+
if let Some(val) = validator {
242+
if val != &d.validator {
243+
return false;
244+
}
245+
}
246+
&d.delegator == delegator
247+
};
248+
let delegations: Vec<_> =
249+
self.delegations.iter().filter(matches).cloned().collect();
250+
let val_res = DelegationsResponse { delegations };
251+
Ok(to_binary(&val_res).map_err(|e| e.into()))
252+
}
179253
}
180254
}
181255
}
@@ -186,9 +260,7 @@ impl Querier for MockQuerier {
186260
match request {
187261
QueryRequest::Bank(bank_query) => self.bank.query(bank_query),
188262
#[cfg(feature = "staking")]
189-
QueryRequest::Staking(_) => Err(ApiSystemError::InvalidRequest {
190-
error: "staking not yet implemented".to_string(),
191-
}),
263+
QueryRequest::Staking(staking_query) => self.staking.query(staking_query),
192264
QueryRequest::Wasm(msg) => {
193265
let addr = match msg {
194266
WasmQuery::Smart { contract_addr, .. } => contract_addr,

packages/std/src/query.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub type QueryResult = ApiResult<QueryResponse>;
1515
pub enum QueryRequest {
1616
Bank(BankQuery),
1717
#[cfg(feature = "staking")]
18-
Staking(StakingRequest),
18+
Staking(StakingQuery),
1919
Wasm(WasmQuery),
2020
}
2121

@@ -65,7 +65,7 @@ pub struct AllBalanceResponse {
6565
}
6666

6767
#[cfg(feature = "staking")]
68-
pub use staking::{Delegation, DelegationsResponse, StakingRequest, Validator, ValidatorsResponse};
68+
pub use staking::{Delegation, DelegationsResponse, StakingQuery, Validator, ValidatorsResponse};
6969

7070
#[cfg(feature = "staking")]
7171
mod staking {
@@ -77,7 +77,7 @@ mod staking {
7777

7878
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
7979
#[serde(rename_all = "snake_case")]
80-
pub enum StakingRequest {
80+
pub enum StakingQuery {
8181
Validators {},
8282
// Delegations will return all delegations by the delegator,
8383
// or just those to the given validator (if set)

0 commit comments

Comments
 (0)