From e12c250cf57aa7bd15c646f43f545b2b94441b88 Mon Sep 17 00:00:00 2001 From: HaoranYi Date: Mon, 10 Apr 2023 16:10:45 -0500 Subject: [PATCH] Extract new_epoch processing out of new_bank (#31127) * extract new epoch processing out of new_bank * review feedbacks * remove epoch_schedule param * use RewardCalcTracer --- runtime/src/bank.rs | 134 +++++++++++++++++++++++++------------------- 1 file changed, 75 insertions(+), 59 deletions(-) diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index b5865ffb64..ff7d9ad153 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -918,7 +918,14 @@ pub enum RewardCalculationEvent<'a, 'b> { Staking(&'a Pubkey, &'b InflationPointCalculationEvent), } -fn null_tracer() -> Option { +/// type alias is not supported for trait in rust yet. As a workaround, we define the +/// `RewardCalcTracer` trait explicitly and implement it on any type that implement +/// `Fn(&RewardCalculationEvent) + Send + Sync`. +pub trait RewardCalcTracer: Fn(&RewardCalculationEvent) + Send + Sync {} + +impl RewardCalcTracer for T {} + +fn null_tracer() -> Option { None:: } @@ -1478,7 +1485,7 @@ impl Bank { parent: &Arc, collector_id: &Pubkey, slot: Slot, - reward_calc_tracer: impl Fn(&RewardCalculationEvent) + Send + Sync, + reward_calc_tracer: impl RewardCalcTracer, ) -> Self { Self::_new_from_parent( parent, @@ -1497,7 +1504,7 @@ impl Bank { parent: &Arc, collector_id: &Pubkey, slot: Slot, - reward_calc_tracer: Option, + reward_calc_tracer: Option, new_bank_options: NewBankOptions, ) -> Self { let mut time = Measure::start("bank::new_from_parent"); @@ -1665,62 +1672,7 @@ impl Bank { let parent_epoch = parent.epoch(); let (_, update_epoch_time_us) = measure_us!({ if parent_epoch < new.epoch() { - let (thread_pool, thread_pool_time) = measure!( - ThreadPoolBuilder::new().build().unwrap(), - "thread_pool_creation", - ); - - let (_, apply_feature_activations_time) = measure!( - new.apply_feature_activations( - ApplyFeatureActivationsCaller::NewFromParent, - false - ), - "apply_feature_activation", - ); - - // Add new entry to stakes.stake_history, set appropriate epoch and - // update vote accounts with warmed up stakes before saving a - // snapshot of stakes in epoch stakes - let (_, activate_epoch_time) = measure!( - new.stakes_cache.activate_epoch(epoch, &thread_pool), - "activate_epoch", - ); - - // Save a snapshot of stakes for use in consensus and stake weighted networking - let leader_schedule_epoch = epoch_schedule.get_leader_schedule_epoch(slot); - let (_, update_epoch_stakes_time) = measure!( - new.update_epoch_stakes(leader_schedule_epoch), - "update_epoch_stakes", - ); - - let mut rewards_metrics = RewardsMetrics::default(); - // After saving a snapshot of stakes, apply stake rewards and commission - let (_, update_rewards_with_thread_pool_time) = measure!( - { - new.update_rewards_with_thread_pool( - parent_epoch, - reward_calc_tracer, - &thread_pool, - &mut rewards_metrics, - ) - }, - "update_rewards_with_thread_pool", - ); - - report_new_epoch_metrics( - new.epoch(), - slot, - parent.slot(), - NewEpochTimings { - thread_pool_time_us: thread_pool_time.as_us(), - apply_feature_activations_time_us: apply_feature_activations_time.as_us(), - activate_epoch_time_us: activate_epoch_time.as_us(), - update_epoch_stakes_time_us: update_epoch_stakes_time.as_us(), - update_rewards_with_thread_pool_time_us: - update_rewards_with_thread_pool_time.as_us(), - }, - rewards_metrics, - ); + new.process_new_epoch(parent_epoch, parent.slot(), reward_calc_tracer); } else { // Save a snapshot of stakes for use in consensus and stake weighted networking let leader_schedule_epoch = epoch_schedule.get_leader_schedule_epoch(slot); @@ -1774,6 +1726,70 @@ impl Bank { new } + /// process for the start of a new epoch + fn process_new_epoch( + &mut self, + parent_epoch: Epoch, + parent_slot: Slot, + reward_calc_tracer: Option, + ) { + let epoch = self.epoch(); + let slot = self.slot(); + let (thread_pool, thread_pool_time) = measure!( + ThreadPoolBuilder::new().build().unwrap(), + "thread_pool_creation", + ); + + let (_, apply_feature_activations_time) = measure!( + self.apply_feature_activations(ApplyFeatureActivationsCaller::NewFromParent, false), + "apply_feature_activation", + ); + + // Add new entry to stakes.stake_history, set appropriate epoch and + // update vote accounts with warmed up stakes before saving a + // snapshot of stakes in epoch stakes + let (_, activate_epoch_time) = measure!( + self.stakes_cache.activate_epoch(epoch, &thread_pool), + "activate_epoch", + ); + + // Save a snapshot of stakes for use in consensus and stake weighted networking + let leader_schedule_epoch = self.epoch_schedule.get_leader_schedule_epoch(slot); + let (_, update_epoch_stakes_time) = measure!( + self.update_epoch_stakes(leader_schedule_epoch), + "update_epoch_stakes", + ); + + let mut rewards_metrics = RewardsMetrics::default(); + // After saving a snapshot of stakes, apply stake rewards and commission + let (_, update_rewards_with_thread_pool_time) = measure!( + { + self.update_rewards_with_thread_pool( + parent_epoch, + reward_calc_tracer, + &thread_pool, + &mut rewards_metrics, + ) + }, + "update_rewards_with_thread_pool", + ); + + report_new_epoch_metrics( + epoch, + slot, + parent_slot, + NewEpochTimings { + thread_pool_time_us: thread_pool_time.as_us(), + apply_feature_activations_time_us: apply_feature_activations_time.as_us(), + activate_epoch_time_us: activate_epoch_time.as_us(), + update_epoch_stakes_time_us: update_epoch_stakes_time.as_us(), + update_rewards_with_thread_pool_time_us: update_rewards_with_thread_pool_time + .as_us(), + }, + rewards_metrics, + ); + } + pub fn byte_limit_for_scans(&self) -> Option { self.rc .accounts