Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
chugga for less indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-solana committed Jan 22, 2020
1 parent 14da9fa commit 48694ee
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 30 deletions.
27 changes: 27 additions & 0 deletions programs/stake/src/stake_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,33 @@ impl<'a> StakeAccount for KeyedAccount<'a> {
}
}

// utility function, used by runtime
pub fn redeem_rewards(
stake_account: &mut Account,
vote_account: &mut Account,
point_value: f64,
stake_history: Option<&StakeHistory>,
) -> Result<u64, InstructionError> {
if let StakeState::Stake(meta, mut stake) = stake_account.state()? {
let vote_state = vote_account.state()?;

if let Some((voters_reward, stakers_reward)) =
stake.redeem_rewards(point_value, &vote_state, stake_history)
{
stake_account.lamports += stakers_reward;
vote_account.lamports += voters_reward;

stake_account.set_state(&StakeState::Stake(meta, stake))?;

Ok(stakers_reward + voters_reward)
} else {
Err(StakeError::NoCreditsToRedeem.into())
}
} else {
Err(InstructionError::InvalidAccountData)
}
}

// utility function, used by runtime::Stakes, tests
pub fn new_stake_history_entry<'a, I>(
epoch: Epoch,
Expand Down
49 changes: 19 additions & 30 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use solana_metrics::{
};
use solana_sdk::{
account::Account,
account_utils::State,
clock::{get_segment_from_slot, Epoch, Slot, UnixTimestamp, MAX_RECENT_BLOCKHASHES},
epoch_schedule::EpochSchedule,
fee_calculator::FeeCalculator,
Expand All @@ -49,7 +48,7 @@ use solana_sdk::{
timing::years_as_slots,
transaction::{Result, Transaction, TransactionError},
};
use solana_stake_program::stake_state::{Delegation, StakeState};
use solana_stake_program::stake_state::{self, Delegation};
use solana_vote_program::vote_state::VoteState;
use std::{
cell::RefCell,
Expand Down Expand Up @@ -641,7 +640,7 @@ impl Bank {

/// iterate over all stakes, redeem vote credits for each stake we can
/// successfully load and parse, return total payout
fn pay_validator_rewards(&self, validator_point_value: f64) -> u64 {
fn pay_validator_rewards(&self, point_value: f64) -> u64 {
let stake_history = self.stakes.read().unwrap().history().clone();
self.stake_delegations()
.iter()
Expand All @@ -651,32 +650,22 @@ impl Bank {
self.get_account(&delegation.voter_pubkey),
) {
(Some(mut stake_account), Some(mut vote_account)) => {
match (stake_account.state(), vote_account.state()) {
(Ok(StakeState::Stake(meta, mut stake)), Ok(vote_state)) => {
// we've recovered everything we need to do redemption
if let Some((voters_reward, stakers_reward)) = stake.redeem_rewards(
validator_point_value,
&vote_state,
Some(&stake_history),
) {
stake_account.lamports += stakers_reward;
vote_account.lamports += voters_reward;

if stake_account
.set_state(&StakeState::Stake(meta, stake))
.is_ok()
{
self.store_account(&stake_pubkey, &stake_account);
self.store_account(&delegation.voter_pubkey, &vote_account);
stakers_reward + voters_reward
} else {
0
}
} else {
0
}
}
(_, _) => 0,
let rewards = stake_state::redeem_rewards(
&mut stake_account,
&mut vote_account,
point_value,
Some(&stake_history),
);
if let Ok(rewards) = rewards {
self.store_account(&stake_pubkey, &stake_account);
self.store_account(&delegation.voter_pubkey, &vote_account);
rewards
} else {
debug!(
"stake_state::redeem_rewards() failed for {}: {:?}",
stake_pubkey, rewards
);
0
}
}
(_, _) => 0,
Expand Down Expand Up @@ -2976,7 +2965,7 @@ mod tests {
crate::storage_utils::tests::create_storage_accounts_with_credits(100);

// set up stakes, vote, and storage accounts
bank.store_account(&stake_id, &stake_account.borrow());
bank.store_account(&stake_id, &stake_account);
bank.store_account(&validator_id, &validator_account.borrow());
bank.store_account(&archiver_id, &archiver_account.borrow());

Expand Down

0 comments on commit 48694ee

Please sign in to comment.