Skip to content

Commit 1085fce

Browse files
guidiazaesedepece
authored andcommitted
feat(json_rpc): implement limits on queryStakes
1 parent 325a38b commit 1085fce

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

node/src/actors/chain_manager/handlers.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{
99
use actix::{prelude::*, ActorFutureExt, WrapFuture};
1010
use futures::future::Either;
1111

12+
use itertools::Itertools;
1213
use witnet_data_structures::{
1314
chain::{
1415
tapi::ActiveWips, Block, ChainInfo, ChainState, CheckpointBeacon, ConsensusConstantsWit2,
@@ -1542,10 +1543,39 @@ impl Handler<QueryStakes> for ChainManager {
15421543
type Result = <QueryStakes as Message>::Result;
15431544

15441545
fn handle(&mut self, msg: QueryStakes, _ctx: &mut Self::Context) -> Self::Result {
1545-
// build address from public key hash
1546-
let stakes = self.chain_state.stakes.query_stakes(msg.filter);
1546+
// query stakes from current chain state
1547+
let mut stakes = self
1548+
.chain_state
1549+
.stakes
1550+
.query_stakes(msg.filter)
1551+
.map_err(StakesError::from)?;
1552+
// filter out stake entries whose last mining and witnessing epochs are previous to `epoch`
1553+
let epoch: u32 = if msg.limits.since >= 0 {
1554+
msg.limits.since.try_into().inspect_err(|&e| {
1555+
log::warn!("Invalid 'since' limit on QueryStakes: {}", e);
1556+
})?
1557+
} else {
1558+
(self.current_epoch.unwrap() as i64 + msg.limits.since)
1559+
.try_into()
1560+
.unwrap_or_default()
1561+
};
1562+
stakes.retain(|stake| {
1563+
let nonce: u32 = stake.value.nonce.try_into().unwrap_or_default();
1564+
stake.value.epochs.mining >= epoch && stake.value.epochs.mining != nonce
1565+
|| (stake.value.epochs.witnessing >= epoch
1566+
&& stake.value.epochs.witnessing != nonce)
1567+
});
1568+
if msg.limits.distinct {
1569+
// if only distinct validators are required, retain only first appearence in the stakes array:
1570+
let stakes = stakes
1571+
.into_iter()
1572+
.unique_by(|stake| stake.key.validator)
1573+
.collect();
15471574

1548-
stakes.map_err(StakesError::from).map_err(Into::into)
1575+
Ok(stakes)
1576+
} else {
1577+
Ok(stakes)
1578+
}
15491579
}
15501580
}
15511581

node/src/actors/json_rpc/api.rs

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,32 +2247,10 @@ pub async fn authorize_stake(params: Result<AuthorizeStake, Error>) -> JsonRpcRe
22472247
pub async fn query_stakes(params: Result<Option<QueryStakes>, Error>) -> JsonRpcResult {
22482248
// Short-circuit if parameters are wrong
22492249
let params = params?;
2250-
// If a withdrawer address is not specified, default to local node address
2251-
let filter: QueryStakesFilter = if let Some(address) = params {
2252-
match address {
2253-
QueryStakesParams::All(_) => QueryStakesFilter::All,
2254-
QueryStakesParams::Validator(validator) => QueryStakesFilter::Validator(
2255-
PublicKeyHash::from_bech32(get_environment(), &validator)
2256-
.map_err(internal_error)?,
2257-
),
2258-
QueryStakesParams::Withdrawer(withdrawer) => QueryStakesFilter::Withdrawer(
2259-
PublicKeyHash::from_bech32(get_environment(), &withdrawer)
2260-
.map_err(internal_error)?,
2261-
),
2262-
QueryStakesParams::Key((validator, withdrawer)) => QueryStakesFilter::Key((
2263-
PublicKeyHash::from_bech32(get_environment(), &validator)
2264-
.map_err(internal_error)?,
2265-
PublicKeyHash::from_bech32(get_environment(), &withdrawer)
2266-
.map_err(internal_error)?,
2267-
)),
2268-
}
2269-
} else {
2270-
let pk = signature_mngr::public_key().await.map_err(internal_error)?;
2271-
2272-
QueryStakesFilter::Validator(PublicKeyHash::from_public_key(&pk))
2273-
};
2250+
// Parse params or defaults:
2251+
let msg = params.unwrap_or(QueryStakes::default());
22742252
ChainManager::from_registry()
2275-
.send(QueryStakes { filter })
2253+
.send(msg)
22762254
.map(|res| match res {
22772255
Ok(Ok(stakes)) => serde_json::to_value(stakes).map_err(internal_error),
22782256
Ok(Err(e)) => {

0 commit comments

Comments
 (0)