Skip to content

Commit 15d0e82

Browse files
committed
feat(json_rpc): new method to fetch stake entries ordered by rank for a given capability
1 parent a7777f7 commit 15d0e82

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

data_structures/src/capabilities.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use serde::{Deserialize, Serialize};
22

33
#[repr(u8)]
4-
#[derive(Clone, Copy, Debug)]
4+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
5+
#[serde(rename_all = "lowercase")]
56
pub enum Capability {
67
/// The base block mining and superblock voting capability
8+
#[default]
79
Mining = 0,
810
/// The universal HTTP GET / HTTP POST / WIP-0019 RNG capability
911
Witnessing = 1,

node/src/actors/chain_manager/handlers.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ use crate::{
3737
GetDataRequestInfo, GetHighestCheckpointBeacon, GetMemoryTransaction, GetMempool,
3838
GetMempoolResult, GetNodeStats, GetProtocolInfo, GetReputation, GetReputationResult,
3939
GetSignalingInfo, GetState, GetSuperBlockVotes, GetSupplyInfo, GetUtxoInfo,
40-
IsConfirmedBlock, PeersBeacons, QueryStake, ReputationStats, Rewind, SendLastBeacon,
41-
SendProtocolVersions, SessionUnitResult, SetEpochConstants, SetLastBeacon,
42-
SetPeersLimits, SignalingInfo, SnapshotExport, SnapshotImport, TryMineBlock,
40+
IsConfirmedBlock, PeersBeacons, QueryStakePowers, QueryStakes, ReputationStats, Rewind,
41+
SendLastBeacon, SendProtocolVersions, SessionUnitResult, SetEpochConstants,
42+
SetLastBeacon, SetPeersLimits, SignalingInfo, SnapshotExport, SnapshotImport,
43+
TryMineBlock,
4344
},
4445
sessions_manager::SessionsManager,
4546
},
@@ -1464,6 +1465,17 @@ impl Handler<QueryStakes> for ChainManager {
14641465
}
14651466
}
14661467

1468+
impl Handler<QueryStakePowers> for ChainManager {
1469+
type Result = <QueryStakePowers as Message>::Result;
1470+
1471+
fn handle(&mut self, msg: QueryStakePowers, _ctx: &mut Self::Context) -> Self::Result {
1472+
self.chain_state
1473+
.stakes
1474+
.by_rank(msg.capability, self.current_epoch.unwrap())
1475+
.collect()
1476+
}
1477+
}
1478+
14671479
impl Handler<BuildDrt> for ChainManager {
14681480
type Result = ResponseActFuture<Self, Result<DRTransaction, failure::Error>>;
14691481

node/src/actors/json_rpc/api.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use crate::{
4747
GetHighestCheckpointBeacon, GetItemBlock, GetItemSuperblock, GetItemTransaction,
4848
GetKnownPeers, GetMemoryTransaction, GetMempool, GetNodeStats, GetProtocolInfo,
4949
GetReputation, GetSignalingInfo, GetState, GetSupplyInfo, GetUtxoInfo, InitializePeers,
50-
IsConfirmedBlock, QueryStakes, QueryStakesFilter, Rewind,
50+
IsConfirmedBlock, QueryStakePowers, QueryStakes, QueryStakesFilter, Rewind,
5151
SnapshotExport, SnapshotImport, StakeAuthorization,
5252
},
5353
peers_manager::PeersManager,
@@ -145,6 +145,9 @@ pub fn attach_regular_methods<H>(
145145
server.add_actix_method(system, "queryStakes", |params: Params| {
146146
Box::pin(query_stakes(params.parse()))
147147
});
148+
server.add_actix_method(system, "ranks", |params: Params| {
149+
Box::pin(query_ranks(params.parse()))
150+
});
148151
}
149152

150153
/// Attach the sensitive JSON-RPC methods to a multi-transport server.
@@ -2239,6 +2242,46 @@ pub async fn query_stakes(params: Result<Option<QueryStakesParams>, Error>) -> J
22392242
.await
22402243
}
22412244

2245+
/// Format of the output of getTransaction
2246+
#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
2247+
pub struct QueryRanksRecord {
2248+
/// Current power
2249+
pub power: u64,
2250+
/// Validator's stringified pkh
2251+
pub validator: String,
2252+
/// Withdrawer's stringified pkh
2253+
pub withdrawer: String,
2254+
}
2255+
2256+
/// Query the amount of nanowits staked by an address.
2257+
pub async fn query_ranks(params: Result<(Capability,), Error>) -> JsonRpcResult {
2258+
let capability = match params {
2259+
Ok(x) => x.0,
2260+
Err(_) => Capability::Mining,
2261+
};
2262+
ChainManager::from_registry()
2263+
.send(QueryStakePowers { capability })
2264+
.map(|res| match res {
2265+
Ok(candidates) => {
2266+
let candidates: Vec<QueryRanksRecord> = candidates
2267+
.iter()
2268+
.map(|(key, power)| QueryRanksRecord {
2269+
power: *power,
2270+
validator: key.validator.to_string(),
2271+
withdrawer: key.withdrawer.to_string(),
2272+
})
2273+
.collect();
2274+
let candidates = serde_json::to_value(candidates);
2275+
candidates.map_err(internal_error)
2276+
}
2277+
Err(e) => {
2278+
let err = internal_error_s(e);
2279+
Err(err)
2280+
}
2281+
})
2282+
.await
2283+
}
2284+
22422285
#[cfg(test)]
22432286
mod mock_actix {
22442287
use actix::{MailboxError, Message};

node/src/actors/messages.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,17 @@ impl Message for StakeAuthorization {
344344
type Result = Result<String, failure::Error>;
345345
}
346346

347+
/// Message for querying stakes
348+
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
349+
pub struct QueryStakePowers {
350+
/// capability being searched for
351+
pub capability: Capability,
352+
}
353+
354+
impl Message for QueryStakePowers {
355+
type Result = Vec<(StakeKey<PublicKeyHash>, Power)>;
356+
}
357+
347358
/// Stake key for quering stakes
348359
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
349360
pub enum QueryStakesFilter {

0 commit comments

Comments
 (0)