Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cli-output/src/cli_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ impl fmt::Display for CliKeyedStakeState {
}
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CliEpochReward {
pub epoch: Epoch,
Expand Down Expand Up @@ -652,7 +652,7 @@ fn show_epoch_rewards(
Ok(())
}

#[derive(Default, Serialize, Deserialize)]
#[derive(Default, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CliStakeState {
pub stake_type: CliStakeType,
Expand Down Expand Up @@ -838,7 +838,7 @@ impl fmt::Display for CliStakeState {
}
}

#[derive(Serialize, Deserialize, PartialEq)]
#[derive(Serialize, Debug, Deserialize, PartialEq)]
pub enum CliStakeType {
Stake,
RewardsPool,
Expand Down Expand Up @@ -914,7 +914,7 @@ pub struct CliStakeHistoryEntry {
pub deactivating_stake: u64,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CliAuthorized {
pub staker: String,
Expand All @@ -930,7 +930,7 @@ impl From<&Authorized> for CliAuthorized {
}
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CliLockup {
pub unix_timestamp: UnixTimestamp,
Expand Down
16 changes: 4 additions & 12 deletions cli/src/cluster_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ use solana_sdk::{
native_token::lamports_to_sol,
pubkey::{self, Pubkey},
signature::Signature,
system_instruction, system_program,
sysvar::{
self,
stake_history::{self},
},
system_instruction, system_program, sysvar,
transaction::Transaction,
};
use solana_transaction_status::UiTransactionEncoding;
Expand Down Expand Up @@ -1339,20 +1335,16 @@ pub fn process_show_stakes(
}
let all_stake_accounts = rpc_client
.get_program_accounts_with_config(&solana_stake_program::id(), program_accounts_config)?;
let stake_history_account = rpc_client.get_account(&stake_history::id())?;
let clock_account = rpc_client.get_account(&sysvar::clock::id())?;
let clock: Clock = from_account(&clock_account).ok_or_else(|| {
CliError::RpcRequestError("Failed to deserialize clock sysvar".to_string())
})?;
progress_bar.finish_and_clear();

let stake_history = from_account(&stake_history_account).ok_or_else(|| {
CliError::RpcRequestError("Failed to deserialize stake history".to_string())
})?;

let mut stake_accounts: Vec<CliKeyedStakeState> = vec![];
for (stake_pubkey, stake_account) in all_stake_accounts {
if let Ok(stake_state) = stake_account.state() {
let stake_activation = rpc_client.get_stake_activation(&stake_pubkey)?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this doesn't quite work. If a stake account is undelegated, getStakeActivation returns a JSON-rpc error: {"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid param: stake account has not been delegated"},"id":1}
...which errors out here and breaks the loop (or in the case of solana stake-account, prevents the account info being shown).

A simple (naive) fix might be to write this as:

Suggested change
let stake_activation = rpc_client.get_stake_activation(&stake_pubkey)?;
let stake_activation = rpc_client.get_stake_activation(&stake_pubkey).unwrap_or(
RpcStakeActivation {
state: StakeActivationState::Inactive,
active: 0,
inactive: 0,
}
);

although that would disguise other types of errors.

match stake_state {
StakeState::Initialized(_) => {
if vote_account_pubkeys.is_none() {
Expand All @@ -1362,7 +1354,7 @@ pub fn process_show_stakes(
stake_account.lamports,
&stake_state,
use_lamports_unit,
&stake_history,
&stake_activation,
&clock,
),
});
Expand All @@ -1380,7 +1372,7 @@ pub fn process_show_stakes(
stake_account.lamports,
&stake_state,
use_lamports_unit,
&stake_history,
&stake_activation,
&clock,
),
});
Expand Down
24 changes: 15 additions & 9 deletions cli/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use solana_cli_output::{
return_signers, CliEpochReward, CliStakeHistory, CliStakeHistoryEntry, CliStakeState,
CliStakeType,
};
use solana_client::rpc_response::{RpcStakeActivation, StakeActivationState};
use solana_client::{
blockhash_query::BlockhashQuery,
client_error::{ClientError, ClientErrorKind},
Expand Down Expand Up @@ -1499,7 +1500,7 @@ pub fn build_stake_state(
account_balance: u64,
stake_state: &StakeState,
use_lamports_unit: bool,
stake_history: &StakeHistory,
stake_activation: &RpcStakeActivation,
clock: &Clock,
) -> CliStakeState {
match stake_state {
Expand All @@ -1512,9 +1513,17 @@ pub fn build_stake_state(
stake,
) => {
let current_epoch = clock.epoch;
let (active_stake, activating_stake, deactivating_stake) = stake
.delegation
.stake_activating_and_deactivating(current_epoch, Some(stake_history));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋

let (active_stake, activating_stake, deactivating_stake) = match stake_activation.state
{
StakeActivationState::Active => (stake_activation.active, 0, 0),
StakeActivationState::Inactive => (0, 0, 0),
StakeActivationState::Activating => {
(stake_activation.active, stake_activation.inactive, 0)
}
StakeActivationState::Deactivating => {
(stake_activation.active, 0, stake_activation.inactive)
}
Comment on lines +1518 to +1525
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shameless inverse conversion from RpcStakeActivation back to the original stake tuple ((u64, u64, u64)). If I had more time, I would fix CliStakeState itself... ;)

};
let lockup = if lockup.is_in_force(clock, None) {
Some(lockup.into())
} else {
Expand Down Expand Up @@ -1695,20 +1704,17 @@ pub fn process_show_stake_account(
}
match stake_account.state() {
Ok(stake_state) => {
let stake_history_account = rpc_client.get_account(&stake_history::id())?;
let stake_history = from_account(&stake_history_account).ok_or_else(|| {
CliError::RpcRequestError("Failed to deserialize stake history".to_string())
})?;
let clock_account = rpc_client.get_account(&clock::id())?;
let clock: Clock = from_account(&clock_account).ok_or_else(|| {
CliError::RpcRequestError("Failed to deserialize clock sysvar".to_string())
})?;
let stake_activation = rpc_client.get_stake_activation(stake_account_address)?;

let mut state = build_stake_state(
stake_account.lamports,
&stake_state,
use_lamports_unit,
&stake_history,
&stake_activation,
&clock,
);

Expand Down
7 changes: 7 additions & 0 deletions client/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,13 @@ impl RpcClient {
self.get_epoch_info_with_commitment(CommitmentConfig::default())
}

pub fn get_stake_activation(&self, pubkey: &Pubkey) -> ClientResult<RpcStakeActivation> {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding new fns is ok in terms of compatibility even with a patch release.

self.send(
RpcRequest::GetStakeActivation,
json!([pubkey.to_string(), CommitmentConfig::default()]),
)
}

pub fn get_epoch_info_with_commitment(
&self,
commitment_config: CommitmentConfig,
Expand Down
2 changes: 2 additions & 0 deletions client/src/rpc_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub enum RpcRequest {
GetStorageTurn,
GetStorageTurnRate,
GetSlotsPerSegment,
GetStakeActivation,
GetStoragePubkeysForSlot,
GetSupply,
GetTokenAccountBalance,
Expand Down Expand Up @@ -95,6 +96,7 @@ impl fmt::Display for RpcRequest {
RpcRequest::GetStorageTurn => "getStorageTurn",
RpcRequest::GetStorageTurnRate => "getStorageTurnRate",
RpcRequest::GetSlotsPerSegment => "getSlotsPerSegment",
RpcRequest::GetStakeActivation => "getStakeActivation",
RpcRequest::GetStoragePubkeysForSlot => "getStoragePubkeysForSlot",
RpcRequest::GetSupply => "getSupply",
RpcRequest::GetTokenAccountBalance => "getTokenAccountBalance",
Expand Down