Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pgarg66 committed Aug 4, 2023
1 parent 853dcb4 commit 380e8e2
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 26 deletions.
109 changes: 86 additions & 23 deletions program-runtime/src/loaded_programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,12 @@ pub struct LoadedProgramsForTxBatch {
slot: Slot,
}

pub struct ExtractedPrograms {
pub loaded: LoadedProgramsForTxBatch,
pub missing: Vec<(Pubkey, u64)>,
pub unloaded: Vec<(Pubkey, u64)>,
}

impl LoadedProgramsForTxBatch {
pub fn new(slot: Slot) -> Self {
Self {
Expand Down Expand Up @@ -608,11 +614,7 @@ impl LoadedPrograms {
&self,
working_slot: &S,
keys: impl Iterator<Item = (Pubkey, (LoadedProgramMatchCriteria, u64))>,
) -> (
LoadedProgramsForTxBatch,
Vec<(Pubkey, u64)>,
Vec<(Pubkey, u64)>,
) {
) -> ExtractedPrograms {
let mut missing = Vec::new();
let mut unloaded = Vec::new();
let found = keys
Expand Down Expand Up @@ -671,14 +673,14 @@ impl LoadedPrograms {
self.stats
.hits
.fetch_add(found.len() as u64, Ordering::Relaxed);
(
LoadedProgramsForTxBatch {
ExtractedPrograms {
loaded: LoadedProgramsForTxBatch {
entries: found,
slot: working_slot.current_slot(),
},
missing,
unloaded,
)
}
}

pub fn merge(&mut self, tx_batch_cache: &LoadedProgramsForTxBatch) {
Expand Down Expand Up @@ -815,6 +817,7 @@ impl solana_frozen_abi::abi_example::AbiExample for LoadedPrograms {

#[cfg(test)]
mod tests {
use crate::loaded_programs::ExtractedPrograms;
use {
crate::loaded_programs::{
BlockRelation, ForkGraph, LoadedProgram, LoadedProgramMatchCriteria, LoadedProgramType,
Expand Down Expand Up @@ -1452,7 +1455,11 @@ mod tests {

// Testing fork 0 - 10 - 12 - 22 with current slot at 22
let working_slot = TestWorkingSlot::new(22, &[0, 10, 20, 22]);
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand All @@ -1471,7 +1478,11 @@ mod tests {

// Testing fork 0 - 5 - 11 - 15 - 16 with current slot at 16
let mut working_slot = TestWorkingSlot::new(15, &[0, 5, 11, 15, 16, 18, 19, 23]);
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand All @@ -1498,7 +1509,11 @@ mod tests {

// Testing the same fork above, but current slot is now 18 (equal to effective slot of program4).
working_slot.update_slot(18);
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand All @@ -1519,7 +1534,11 @@ mod tests {

// Testing the same fork above, but current slot is now 23 (future slot than effective slot of program4).
working_slot.update_slot(23);
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand All @@ -1540,7 +1559,11 @@ mod tests {

// Testing fork 0 - 5 - 11 - 15 - 16 with current slot at 11
let working_slot = TestWorkingSlot::new(11, &[0, 5, 11, 15, 16]);
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand Down Expand Up @@ -1577,7 +1600,11 @@ mod tests {

// Testing fork 0 - 5 - 11 - 15 - 16 - 19 - 21 - 23 with current slot at 19
let working_slot = TestWorkingSlot::new(19, &[0, 5, 11, 15, 16, 18, 19, 21, 23]);
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand All @@ -1598,7 +1625,11 @@ mod tests {
// Testing fork 0 - 5 - 11 - 15 - 16 - 19 - 21 - 23 with current slot at 21
// This would cause program4 deployed at slot 19 to be expired.
let working_slot = TestWorkingSlot::new(21, &[0, 5, 11, 15, 16, 18, 19, 21, 23]);
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand Down Expand Up @@ -1639,7 +1670,11 @@ mod tests {

// Testing fork 11 - 15 - 16- 19 - 22 with root at 5 and current slot at 22
let working_slot = TestWorkingSlot::new(22, &[5, 11, 15, 16, 19, 22, 23]);
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand All @@ -1659,7 +1694,11 @@ mod tests {

// Testing fork 0 - 5 - 11 - 25 - 27 with current slot at 27
let working_slot = TestWorkingSlot::new(27, &[11, 25, 27]);
let (found, _missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing: _,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand Down Expand Up @@ -1694,7 +1733,11 @@ mod tests {

// Testing fork 16, 19, 23, with root at 15, current slot at 23
let working_slot = TestWorkingSlot::new(23, &[16, 19, 23]);
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand Down Expand Up @@ -1750,7 +1793,11 @@ mod tests {

// Testing fork 0 - 5 - 11 - 15 - 16 - 19 - 21 - 23 with current slot at 19
let working_slot = TestWorkingSlot::new(12, &[0, 5, 11, 12, 15, 16, 18, 19, 21, 23]);
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand All @@ -1766,7 +1813,11 @@ mod tests {
assert!(missing.contains(&(program3, 1)));

// Test the same fork, but request the program modified at a later slot than what's in the cache.
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(
Expand Down Expand Up @@ -1837,7 +1888,11 @@ mod tests {

// Testing fork 0 - 5 - 11 - 15 - 16 - 19 - 21 - 23 with current slot at 19
let working_slot = TestWorkingSlot::new(12, &[0, 5, 11, 12, 15, 16, 18, 19, 21, 23]);
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand All @@ -1856,7 +1911,11 @@ mod tests {
// Testing fork 0 - 5 - 11 - 12 - 15 - 16 - 19 - 21 - 23 with current slot at 15
// This would cause program4 deployed at slot 15 to be expired.
let working_slot = TestWorkingSlot::new(15, &[0, 5, 11, 15, 16, 18, 19, 21, 23]);
let (found, missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing,
unloaded: _,
} = cache.extract(
&working_slot,
vec![
(program1, (LoadedProgramMatchCriteria::NoCriteria, 1)),
Expand Down Expand Up @@ -1922,7 +1981,11 @@ mod tests {
cache.prune(&fork_graph, 10);

let working_slot = TestWorkingSlot::new(20, &[0, 10, 20]);
let (found, _missing, _) = cache.extract(
let ExtractedPrograms {
loaded: found,
missing: _,
unloaded: _,
} = cache.extract(
&working_slot,
vec![(program1, (LoadedProgramMatchCriteria::NoCriteria, 1))].into_iter(),
);
Expand Down
11 changes: 8 additions & 3 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ pub struct BankRc {

#[cfg(RUSTC_WITH_SPECIALIZATION)]
use solana_frozen_abi::abi_example::AbiExample;
use solana_program_runtime::loaded_programs::ExtractedPrograms;

#[cfg(RUSTC_WITH_SPECIALIZATION)]
impl AbiExample for BankRc {
Expand Down Expand Up @@ -4922,14 +4923,18 @@ impl Bank {
.collect()
};

let (mut loaded_programs_for_txs, missing_programs, unloaded_programs) = {
let ExtractedPrograms {
loaded: mut loaded_programs_for_txs,
missing,
unloaded,
} = {
// Lock the global cache to figure out which programs need to be loaded
let loaded_programs_cache = self.loaded_programs_cache.read().unwrap();
loaded_programs_cache.extract(self, programs_and_slots.into_iter())
};

// Load missing programs while global cache is unlocked
let missing_programs: Vec<(Pubkey, Arc<LoadedProgram>)> = missing_programs
let missing_programs: Vec<(Pubkey, Arc<LoadedProgram>)> = missing
.iter()
.map(|(key, count)| {
let program = self.load_program(key, false);
Expand All @@ -4939,7 +4944,7 @@ impl Bank {
.collect();

// Reload unloaded programs while global cache is unlocked
let unloaded_programs: Vec<(Pubkey, Arc<LoadedProgram>)> = unloaded_programs
let unloaded_programs: Vec<(Pubkey, Arc<LoadedProgram>)> = unloaded
.iter()
.map(|(key, count)| {
let program = self.load_program(key, true);
Expand Down

0 comments on commit 380e8e2

Please sign in to comment.