From 40914de8118eabc711ec0d1735d9372cf20083d7 Mon Sep 17 00:00:00 2001 From: behzad nouri Date: Wed, 4 Aug 2021 11:11:33 -0400 Subject: [PATCH] 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 --- core/src/cluster_slots.rs | 16 +++++----------- core/src/cluster_slots_service.rs | 4 ++-- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/core/src/cluster_slots.rs b/core/src/cluster_slots.rs index 3984f97111..9f6d893866 100644 --- a/core/src/cluster_slots.rs +++ b/core/src/cluster_slots.rs @@ -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, - ) { - 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) { @@ -114,8 +109,7 @@ impl ClusterSlots { slot_pubkeys.write().unwrap().insert(node_id, balance); } - fn update_peers(&self, bank_forks: &RwLock) { - 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(); diff --git a/core/src/cluster_slots_service.rs b/core/src/cluster_slots_service.rs index 09a3042011..251ac39999 100644 --- a/core/src/cluster_slots_service.rs +++ b/core/src/cluster_slots_service.rs @@ -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(