Skip to content

Commit

Permalink
factor out get_epoch_reward_calculate_param_info to share results (#3…
Browse files Browse the repository at this point in the history
…2065)

* factor out get_epoch_reward_calculate_param_info to share results

* remove extra comment
  • Loading branch information
jeffwashington authored Jun 12, 2023
1 parent 2e5b062 commit 7e7c286
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
41 changes: 35 additions & 6 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ use {
storable_accounts::StorableAccounts,
transaction_batch::TransactionBatch,
transaction_error_metrics::TransactionErrorMetrics,
vote_account::{VoteAccount, VoteAccountsHashMap},
vote_account::{VoteAccount, VoteAccounts, VoteAccountsHashMap},
},
byteorder::{ByteOrder, LittleEndian},
dashmap::{DashMap, DashSet},
Expand Down Expand Up @@ -1115,6 +1115,14 @@ struct VoteRewardsAccounts {
/// None if to be skipped.
accounts_to_store: Vec<Option<AccountSharedData>>,
}

/// hold reward calc info to avoid recalculation across functions
struct EpochRewardCalculateParamInfo<'a> {
stake_history: StakeHistory,
stake_delegations: Vec<(&'a Pubkey, &'a StakeAccount<Delegation>)>,
cached_vote_accounts: &'a VoteAccounts,
}

pub(crate) type StakeRewards = Vec<StakeReward>;

#[derive(Debug, Default)]
Expand Down Expand Up @@ -2803,6 +2811,25 @@ impl Bank {
}
}

#[allow(dead_code)]
/// calculate and return some reward calc info to avoid recalculation across functions
fn get_epoch_reward_calculate_param_info<'a>(
&self,
stakes: &'a Stakes<StakeAccount<Delegation>>,
) -> EpochRewardCalculateParamInfo<'a> {
let stake_history = self.stakes_cache.stakes().history().clone();

let stake_delegations = self.filter_stake_delegations(stakes);

let cached_vote_accounts = stakes.vote_accounts();

EpochRewardCalculateParamInfo {
stake_history,
stake_delegations,
cached_vote_accounts,
}
}

/// Load, calculate and payout epoch rewards for stake and vote accounts
fn pay_validator_rewards_with_thread_pool(
&mut self,
Expand Down Expand Up @@ -2894,14 +2921,16 @@ impl Bank {
#[allow(dead_code)]
fn calculate_reward_points_partitioned(
&self,
reward_calculate_params: &EpochRewardCalculateParamInfo,
rewards: u64,
thread_pool: &ThreadPool,
metrics: &mut RewardsMetrics,
) -> Option<PointValue> {
let stakes = self.stakes_cache.stakes();
let stake_history = stakes.history().clone();
let stake_delegations = self.filter_stake_delegations(&stakes);
let cached_vote_accounts = stakes.vote_accounts();
let EpochRewardCalculateParamInfo {
stake_history,
stake_delegations,
cached_vote_accounts,
} = reward_calculate_params;

let solana_vote_program: Pubkey = solana_vote_program::id();

Expand Down Expand Up @@ -2943,7 +2972,7 @@ impl Bank {
stake_state::calculate_points(
stake_account.stake_state(),
vote_state,
Some(&stake_history),
Some(stake_history),
)
.unwrap_or(0)
})
Expand Down
7 changes: 7 additions & 0 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12540,7 +12540,11 @@ fn test_rewards_point_calculation() {
let mut rewards_metrics = RewardsMetrics::default();
let expected_rewards = 100_000_000_000;

let stakes: RwLockReadGuard<Stakes<StakeAccount<Delegation>>> = bank.stakes_cache.stakes();
let reward_calculate_param = bank.get_epoch_reward_calculate_param_info(&stakes);

let point_value = bank.calculate_reward_points_partitioned(
&reward_calculate_param,
expected_rewards,
&thread_pool,
&mut rewards_metrics,
Expand All @@ -12562,8 +12566,11 @@ fn test_rewards_point_calculation_empty() {
let thread_pool = ThreadPoolBuilder::new().num_threads(1).build().unwrap();
let mut rewards_metrics: RewardsMetrics = RewardsMetrics::default();
let expected_rewards = 100_000_000_000;
let stakes: RwLockReadGuard<Stakes<StakeAccount<Delegation>>> = bank.stakes_cache.stakes();
let reward_calculate_param = bank.get_epoch_reward_calculate_param_info(&stakes);

let point_value = bank.calculate_reward_points_partitioned(
&reward_calculate_param,
expected_rewards,
&thread_pool,
&mut rewards_metrics,
Expand Down

0 comments on commit 7e7c286

Please sign in to comment.