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
This commit is contained in:
HaoranYi 2023-04-10 16:10:45 -05:00 committed by GitHub
parent 4f0adcde89
commit e12c250cf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 75 additions and 59 deletions

View File

@ -918,7 +918,14 @@ pub enum RewardCalculationEvent<'a, 'b> {
Staking(&'a Pubkey, &'b InflationPointCalculationEvent),
}
fn null_tracer() -> Option<impl Fn(&RewardCalculationEvent) + Send + Sync> {
/// 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<T: Fn(&RewardCalculationEvent) + Send + Sync> RewardCalcTracer for T {}
fn null_tracer() -> Option<impl RewardCalcTracer> {
None::<fn(&RewardCalculationEvent)>
}
@ -1478,7 +1485,7 @@ impl Bank {
parent: &Arc<Bank>,
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<Bank>,
collector_id: &Pubkey,
slot: Slot,
reward_calc_tracer: Option<impl Fn(&RewardCalculationEvent) + Send + Sync>,
reward_calc_tracer: Option<impl RewardCalcTracer>,
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<impl RewardCalcTracer>,
) {
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<usize> {
self.rc
.accounts