Skip to content

Commit

Permalink
feat(jsonrpc): allow to query all stake entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommytrg committed Dec 17, 2024
1 parent 69ceea3 commit 13eea38
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
12 changes: 12 additions & 0 deletions data_structures/src/staking/stakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum QueryStakesKey<Address: Default + Ord> {
Withdrawer(Address),
/// Query stakes by validator and withdrawer addresses
Key(StakeKey<Address>),
/// Query all stake entries
All,
}

impl<Address> Default for QueryStakesKey<Address>
Expand Down Expand Up @@ -597,6 +599,7 @@ where
Ok(QueryStakesKey::Key(key)) => self.query_by_key(key).map(|stake| vec![stake]),
Ok(QueryStakesKey::Validator(validator)) => self.query_by_validator(validator),
Ok(QueryStakesKey::Withdrawer(withdrawer)) => self.query_by_withdrawer(withdrawer),
Ok(QueryStakesKey::All) => self.query_all(),
Err(_) => Err(StakesError::EmptyQuery),
}
}
Expand Down Expand Up @@ -695,6 +698,15 @@ where

Ok(withdrawer.iter().map(SyncStakeEntry::read_entry).collect())
}

/// Query all stake entries.
#[inline(always)]
fn query_all(&self) -> StakeEntryVecResult<UNIT, Address, Coins, Epoch, Nonce, Power> {
self.by_key
.values()
.map(|entry| Ok(entry.read_entry()))
.collect()
}
}

/// The default concrete type for tracking stakes in the node software.
Expand Down
3 changes: 3 additions & 0 deletions node/src/actors/json_rpc/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2190,6 +2190,8 @@ pub enum QueryStakesArgument {
Withdrawer(String),
/// To query by stake validator and withdrawer
Key((String, String)),
/// To query all stake entries
All,
}

/// Query the amount of nanowits staked by an address.
Expand All @@ -2200,6 +2202,7 @@ pub async fn query_stakes(params: Result<Option<QueryStakesArgument>, Error>) ->
// If a withdrawer address is not specified, default to local node address
let key: QueryStakesParams = if let Some(address) = params {
match address {
QueryStakesArgument::All => QueryStakesParams::All,
QueryStakesArgument::Validator(validator) => QueryStakesParams::Validator(
PublicKeyHash::from_bech32(get_environment(), &validator)
.map_err(internal_error)?,
Expand Down
3 changes: 3 additions & 0 deletions node/src/actors/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ pub enum QueryStakesParams {
Withdrawer(PublicKeyHash),
/// To search by validator and withdrawer public key hashes
Key((PublicKeyHash, PublicKeyHash)),
/// To query all stake entries
All,
}

impl Default for QueryStakesParams {
Expand Down Expand Up @@ -384,6 +386,7 @@ where
}),
QueryStakesParams::Validator(v) => QueryStakesKey::Validator(v.into()),
QueryStakesParams::Withdrawer(w) => QueryStakesKey::Withdrawer(w.into()),
QueryStakesParams::All => QueryStakesKey::All,
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/cli/node/json_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1933,16 +1933,20 @@ pub fn query_stakes(
addr: SocketAddr,
validator: Option<String>,
withdrawer: Option<String>,
all: bool,
) -> Result<(), failure::Error> {
let mut stream = start_client(addr)?;

let params = match (validator, withdrawer) {
(Some(validator), Some(withdrawer)) => {
Some(QueryStakesArgument::Key((validator, withdrawer)))
let params = if all {
Some(QueryStakesArgument::All)
} else {
match (validator, withdrawer) {
(Some(validator), Some(withdrawer)) => {
Some(QueryStakesArgument::Key((validator, withdrawer)))
}
(Some(validator), _) => Some(QueryStakesArgument::Validator(validator)),
(_, Some(withdrawer)) => Some(QueryStakesArgument::Withdrawer(withdrawer)),
(None, None) => None,
}
(Some(validator), _) => Some(QueryStakesArgument::Validator(validator)),
(_, Some(withdrawer)) => Some(QueryStakesArgument::Withdrawer(withdrawer)),
(None, None) => None,
};

let response = send_request(
Expand Down
5 changes: 4 additions & 1 deletion src/cli/node/with_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ pub fn exec_cmd(
node,
validator,
withdrawer,
} => rpc::query_stakes(node.unwrap_or(default_jsonrpc), validator, withdrawer),
all,
} => rpc::query_stakes(node.unwrap_or(default_jsonrpc), validator, withdrawer, all),
Command::Unstake {
node,
operator,
Expand Down Expand Up @@ -832,6 +833,8 @@ pub enum Command {
validator: Option<String>,
#[structopt(short = "w", long = "withdrawer")]
withdrawer: Option<String>,
#[structopt(short = "a", long = "all")]
all: bool,
},
#[structopt(name = "unstake", about = "Create an unstake transaction")]
Unstake {
Expand Down

0 comments on commit 13eea38

Please sign in to comment.