updates cluster-slots with root-bank instead of root-slot + bank-forks

ClusterSlots::update is taking both root-slot and bank-forks only to
later lookup root-bank from bank-forks, which is redundant. Also
potentially by the time bank-forks is locked to obtain root-bank,
root-slot may have already changed and so be inconsistent with the
root-slot passed in as the argument.
https://github.com/solana-labs/solana/blob/6d95d679c/core/src/cluster_slots.rs#L32-L39
https://github.com/solana-labs/solana/blob/6d95d679c/core/src/cluster_slots.rs#L122
This commit is contained in:
behzad nouri 2021-08-04 11:11:33 -04:00
parent 2fc112edcf
commit 40914de811
2 changed files with 7 additions and 13 deletions

View File

@ -3,7 +3,7 @@ use {
solana_gossip::{
cluster_info::ClusterInfo, contact_info::ContactInfo, crds::Cursor, epoch_slots::EpochSlots,
},
solana_runtime::{bank_forks::BankForks, epoch_stakes::NodeIdToVoteAccounts},
solana_runtime::{bank::Bank, epoch_stakes::NodeIdToVoteAccounts},
solana_sdk::{clock::Slot, pubkey::Pubkey},
std::{
collections::{BTreeMap, HashMap},
@ -30,18 +30,13 @@ impl ClusterSlots {
self.cluster_slots.read().unwrap().get(&slot).cloned()
}
pub(crate) fn update(
&self,
root: Slot,
cluster_info: &ClusterInfo,
bank_forks: &RwLock<BankForks>,
) {
self.update_peers(bank_forks);
pub(crate) fn update(&self, root_bank: &Bank, cluster_info: &ClusterInfo) {
self.update_peers(root_bank);
let epoch_slots = {
let mut cursor = self.cursor.lock().unwrap();
cluster_info.get_epoch_slots(&mut cursor)
};
self.update_internal(root, epoch_slots);
self.update_internal(root_bank.slot(), epoch_slots);
}
fn update_internal(&self, root: Slot, epoch_slots_list: Vec<EpochSlots>) {
@ -114,8 +109,7 @@ impl ClusterSlots {
slot_pubkeys.write().unwrap().insert(node_id, balance);
}
fn update_peers(&self, bank_forks: &RwLock<BankForks>) {
let root_bank = bank_forks.read().unwrap().root_bank();
fn update_peers(&self, root_bank: &Bank) {
let root_epoch = root_bank.epoch();
let my_epoch = *self.epoch.read().unwrap();

View File

@ -91,7 +91,6 @@ impl ClusterSlotsService {
break;
}
};
let new_root = bank_forks.read().unwrap().root();
let mut lowest_slot_elapsed = Measure::start("lowest_slot_elapsed");
let lowest_slot = blockstore.lowest_slot();
Self::update_lowest_slot(lowest_slot, &cluster_info);
@ -105,7 +104,8 @@ impl ClusterSlotsService {
&cluster_info,
);
}
cluster_slots.update(new_root, &cluster_info, &bank_forks);
let root_bank = bank_forks.read().unwrap().root_bank();
cluster_slots.update(&root_bank, &cluster_info);
process_cluster_slots_updates_elapsed.stop();
cluster_slots_service_timing.update(