Skip to content

Commit ef1f93b

Browse files
committed
runtime: Collect stake delegations only once during epoch activation
Processing new epoch (`Bank::process_new_epoch`) involves collecting stake delegations twice: 1) In `Bank::compute_new_epoch_caches_and_rewards`, to create a stake history entry and refresh vote accounts. 2) In `Bank::get_epoch_reward_calculate_param_info`, which is then used in `Bank::calculate_stake_vote_rewards` to calculate rewards for stakers and voters. Reduce that to just one collect by passing the vector 1) with freshly computed stake history and vote accounts to `Bank::begin_partitioned_rewards`. This way, we can avoid calling `Bank::get_epoch_reward_calculate_param_info`.
1 parent c9f6cfa commit ef1f93b

File tree

2 files changed

+147
-42
lines changed

2 files changed

+147
-42
lines changed

runtime/src/bank.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,9 @@ impl AtomicBankHashStats {
10521052
struct NewEpochBundle {
10531053
stake_history: CowStakeHistory,
10541054
vote_accounts: VoteAccounts,
1055+
rewards_calculation: Arc<PartitionedRewardsCalculation>,
10551056
calculate_activated_stake_time_us: u64,
1057+
update_rewards_with_thread_pool_time_us: u64,
10561058
}
10571059

10581060
impl Bank {
@@ -1598,7 +1600,15 @@ impl Bank {
15981600

15991601
/// Returns updated stake history and vote accounts that includes new
16001602
/// activated stake from the last epoch.
1601-
fn compute_new_epoch_caches_and_rewards(&self, thread_pool: &ThreadPool) -> NewEpochBundle {
1603+
fn compute_new_epoch_caches_and_rewards(
1604+
&self,
1605+
thread_pool: &ThreadPool,
1606+
parent_epoch: Epoch,
1607+
parent_slot: Slot,
1608+
parent_height: u64,
1609+
reward_calc_tracer: Option<impl RewardCalcTracer>,
1610+
rewards_metrics: &mut RewardsMetrics,
1611+
) -> NewEpochBundle {
16021612
let stakes = self.stakes_cache.stakes();
16031613
let stake_delegations = stakes.stake_delegations_vec();
16041614
let ((stake_history, vote_accounts), calculate_activated_stake_time_us) =
@@ -1608,10 +1618,25 @@ impl Bank {
16081618
self.new_warmup_cooldown_rate_epoch(),
16091619
&stake_delegations
16101620
));
1621+
// Apply stake rewards and commission using new snapshots.
1622+
let (rewards_calculation, update_rewards_with_thread_pool_time_us) = measure_us!(self
1623+
.begin_partitioned_rewards(
1624+
&stake_history,
1625+
&stake_delegations,
1626+
&vote_accounts,
1627+
reward_calc_tracer,
1628+
thread_pool,
1629+
parent_epoch,
1630+
parent_slot,
1631+
parent_height,
1632+
rewards_metrics,
1633+
));
16111634
NewEpochBundle {
16121635
stake_history,
16131636
vote_accounts,
1637+
rewards_calculation,
16141638
calculate_activated_stake_time_us,
1639+
update_rewards_with_thread_pool_time_us,
16151640
}
16161641
}
16171642

@@ -1637,11 +1662,21 @@ impl Bank {
16371662
// Add new entry to stakes.stake_history, set appropriate epoch and
16381663
// update vote accounts with warmed up stakes before saving a
16391664
// snapshot of stakes in epoch stakes
1665+
let mut rewards_metrics = RewardsMetrics::default();
16401666
let NewEpochBundle {
16411667
stake_history,
16421668
vote_accounts,
1669+
rewards_calculation,
16431670
calculate_activated_stake_time_us,
1644-
} = self.compute_new_epoch_caches_and_rewards(&thread_pool);
1671+
update_rewards_with_thread_pool_time_us,
1672+
} = self.compute_new_epoch_caches_and_rewards(
1673+
&thread_pool,
1674+
parent_epoch,
1675+
parent_slot,
1676+
parent_height,
1677+
reward_calc_tracer,
1678+
&mut rewards_metrics,
1679+
);
16451680
self.stakes_cache
16461681
.activate_epoch(epoch, stake_history, vote_accounts);
16471682

@@ -1650,17 +1685,7 @@ impl Bank {
16501685
let (_, update_epoch_stakes_time_us) =
16511686
measure_us!(self.update_epoch_stakes(leader_schedule_epoch));
16521687

1653-
let mut rewards_metrics = RewardsMetrics::default();
16541688
// After saving a snapshot of stakes, apply stake rewards and commission
1655-
let (rewards_calculation, update_rewards_with_thread_pool_time_us) = measure_us!(self
1656-
.begin_partitioned_rewards(
1657-
reward_calc_tracer,
1658-
&thread_pool,
1659-
parent_epoch,
1660-
parent_slot,
1661-
parent_height,
1662-
&mut rewards_metrics,
1663-
));
16641689
self.save_rewards(rewards_calculation, &rewards_metrics);
16651690

16661691
report_new_epoch_metrics(

0 commit comments

Comments
 (0)