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

Commit

Permalink
AcctIdx: create test fn get_test() to isolate changes to AcctIdx::get()
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington committed Dec 14, 2021
1 parent 2a6dcb2 commit 2c65f8e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 33 deletions.
36 changes: 24 additions & 12 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8685,7 +8685,7 @@ pub mod tests {
let id = {
let (lock, idx) = accounts
.accounts_index
.get(&pubkey, Some(&ancestors), None)
.get_test(&pubkey, Some(&ancestors), None)
.unwrap();
lock.slot_list()[idx].1.store_id()
};
Expand Down Expand Up @@ -8767,12 +8767,12 @@ pub mod tests {
let ancestors = vec![(0, 1)].into_iter().collect();
let (slot1, account_info1) = accounts
.accounts_index
.get(&pubkey1, Some(&ancestors), None)
.get_test(&pubkey1, Some(&ancestors), None)
.map(|(account_list1, index1)| account_list1.slot_list()[index1])
.unwrap();
let (slot2, account_info2) = accounts
.accounts_index
.get(&pubkey2, Some(&ancestors), None)
.get_test(&pubkey2, Some(&ancestors), None)
.map(|(account_list2, index2)| account_list2.slot_list()[index2])
.unwrap();
assert_eq!(slot1, 0);
Expand Down Expand Up @@ -8885,7 +8885,10 @@ pub mod tests {

// zero lamport account, should no longer exist in accounts index
// because it has been removed
assert!(accounts.accounts_index.get(&pubkey, None, None).is_none());
assert!(accounts
.accounts_index
.get_test(&pubkey, None, None)
.is_none());
}

#[test]
Expand Down Expand Up @@ -9073,7 +9076,10 @@ pub mod tests {

// `pubkey1`, a zero lamport account, should no longer exist in accounts index
// because it has been removed by the clean
assert!(accounts.accounts_index.get(&pubkey1, None, None).is_none());
assert!(accounts
.accounts_index
.get_test(&pubkey1, None, None)
.is_none());

// Secondary index should have purged `pubkey1` as well
let mut found_accounts = vec![];
Expand Down Expand Up @@ -9115,7 +9121,10 @@ pub mod tests {
accounts.clean_accounts(Some(0), false, None);
assert_eq!(accounts.alive_account_count_in_slot(0), 1);
assert_eq!(accounts.alive_account_count_in_slot(1), 1);
assert!(accounts.accounts_index.get(&pubkey, None, None).is_some());
assert!(accounts
.accounts_index
.get_test(&pubkey, None, None)
.is_some());

// Now the account can be cleaned up
accounts.clean_accounts(Some(1), false, None);
Expand All @@ -9124,7 +9133,10 @@ pub mod tests {

// The zero lamport account, should no longer exist in accounts index
// because it has been removed
assert!(accounts.accounts_index.get(&pubkey, None, None).is_none());
assert!(accounts
.accounts_index
.get_test(&pubkey, None, None)
.is_none());
}

#[test]
Expand Down Expand Up @@ -9328,12 +9340,12 @@ pub mod tests {
accounts.add_root(current_slot);
let (slot1, account_info1) = accounts
.accounts_index
.get(&pubkey, None, None)
.get_test(&pubkey, None, None)
.map(|(account_list1, index1)| account_list1.slot_list()[index1])
.unwrap();
let (slot2, account_info2) = accounts
.accounts_index
.get(&pubkey2, None, None)
.get_test(&pubkey2, None, None)
.map(|(account_list2, index2)| account_list2.slot_list()[index2])
.unwrap();
assert_eq!(slot1, current_slot);
Expand Down Expand Up @@ -11061,11 +11073,11 @@ pub mod tests {
accounts_index.add_root(2, false);
accounts_index.add_root(3, false);
let mut purges = HashMap::new();
let (key0_entry, _) = accounts_index.get(&key0, None, None).unwrap();
let (key0_entry, _) = accounts_index.get_test(&key0, None, None).unwrap();
purges.insert(key0, accounts_index.roots_and_ref_count(&key0_entry, None));
let (key1_entry, _) = accounts_index.get(&key1, None, None).unwrap();
let (key1_entry, _) = accounts_index.get_test(&key1, None, None).unwrap();
purges.insert(key1, accounts_index.roots_and_ref_count(&key1_entry, None));
let (key2_entry, _) = accounts_index.get(&key2, None, None).unwrap();
let (key2_entry, _) = accounts_index.get_test(&key2, None, None).unwrap();
purges.insert(key2, accounts_index.roots_and_ref_count(&key2_entry, None));
for (key, (list, ref_count)) in &purges {
info!(" purge {} ref_count {} =>", key, ref_count);
Expand Down
73 changes: 52 additions & 21 deletions runtime/src/accounts_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2099,6 +2099,18 @@ pub mod tests {
}
}

impl<T: IndexValue> AccountsIndex<T> {
/// provides the ability to refactor this function on the api without bloody changes
pub fn get_test(
&self,
pubkey: &Pubkey,
ancestors: Option<&Ancestors>,
max_root: Option<Slot>,
) -> AccountIndexGetResult<T> {
self.get(pubkey, ancestors, max_root)
}
}

#[test]
fn test_bitfield_delete_non_excess() {
solana_logger::setup();
Expand Down Expand Up @@ -2746,8 +2758,9 @@ pub mod tests {
let key = Keypair::new();
let index = AccountsIndex::<bool>::default_for_tests();
let ancestors = Ancestors::default();
assert!(index.get(&key.pubkey(), Some(&ancestors), None).is_none());
assert!(index.get(&key.pubkey(), None, None).is_none());
let key = &key.pubkey();
assert!(index.get_test(key, Some(&ancestors), None).is_none());
assert!(index.get_test(key, None, None).is_none());

let mut num = 0;
index.unchecked_scan_accounts(
Expand Down Expand Up @@ -2824,8 +2837,10 @@ pub mod tests {
assert!(gc.is_empty());

let ancestors = Ancestors::default();
assert!(index.get(&key.pubkey(), Some(&ancestors), None).is_none());
assert!(index.get(&key.pubkey(), None, None).is_none());
assert!(index
.get_test(&key.pubkey(), Some(&ancestors), None)
.is_none());
assert!(index.get_test(&key.pubkey(), None, None).is_none());

let mut num = 0;
index.unchecked_scan_accounts(
Expand Down Expand Up @@ -2863,8 +2878,8 @@ pub mod tests {
index.insert_new_if_missing_into_primary_index(slot, items.len(), items.into_iter());

let mut ancestors = Ancestors::default();
assert!(index.get(pubkey, Some(&ancestors), None).is_none());
assert!(index.get(pubkey, None, None).is_none());
assert!(index.get_test(pubkey, Some(&ancestors), None).is_none());
assert!(index.get_test(pubkey, None, None).is_none());

let mut num = 0;
index.unchecked_scan_accounts(
Expand All @@ -2875,7 +2890,7 @@ pub mod tests {
);
assert_eq!(num, 0);
ancestors.insert(slot, 0);
assert!(index.get(pubkey, Some(&ancestors), None).is_some());
assert!(index.get_test(pubkey, Some(&ancestors), None).is_some());
assert_eq!(index.ref_count_from_storage(pubkey), 1);
index.unchecked_scan_accounts(
"",
Expand All @@ -2892,8 +2907,8 @@ pub mod tests {
index.insert_new_if_missing_into_primary_index(slot, items.len(), items.into_iter());

let mut ancestors = Ancestors::default();
assert!(index.get(pubkey, Some(&ancestors), None).is_none());
assert!(index.get(pubkey, None, None).is_none());
assert!(index.get_test(pubkey, Some(&ancestors), None).is_none());
assert!(index.get_test(pubkey, None, None).is_none());

let mut num = 0;
index.unchecked_scan_accounts(
Expand All @@ -2904,7 +2919,7 @@ pub mod tests {
);
assert_eq!(num, 0);
ancestors.insert(slot, 0);
assert!(index.get(pubkey, Some(&ancestors), None).is_some());
assert!(index.get_test(pubkey, Some(&ancestors), None).is_some());
assert_eq!(index.ref_count_from_storage(pubkey), 0); // cached, so 0
index.unchecked_scan_accounts(
"",
Expand Down Expand Up @@ -3132,8 +3147,10 @@ pub mod tests {
assert_eq!(1, account_maps_stats_len(&index));

let mut ancestors = Ancestors::default();
assert!(index.get(&key.pubkey(), Some(&ancestors), None).is_none());
assert!(index.get(&key.pubkey(), None, None).is_none());
assert!(index
.get_test(&key.pubkey(), Some(&ancestors), None)
.is_none());
assert!(index.get_test(&key.pubkey(), None, None).is_none());

let mut num = 0;
index.unchecked_scan_accounts(
Expand All @@ -3144,7 +3161,9 @@ pub mod tests {
);
assert_eq!(num, 0);
ancestors.insert(slot, 0);
assert!(index.get(&key.pubkey(), Some(&ancestors), None).is_some());
assert!(index
.get_test(&key.pubkey(), Some(&ancestors), None)
.is_some());
index.unchecked_scan_accounts(
"",
&ancestors,
Expand Down Expand Up @@ -3172,7 +3191,9 @@ pub mod tests {
assert!(gc.is_empty());

let ancestors = vec![(1, 1)].into_iter().collect();
assert!(index.get(&key.pubkey(), Some(&ancestors), None).is_none());
assert!(index
.get_test(&key.pubkey(), Some(&ancestors), None)
.is_none());

let mut num = 0;
index.unchecked_scan_accounts(
Expand Down Expand Up @@ -3202,7 +3223,9 @@ pub mod tests {
assert!(gc.is_empty());

let ancestors = vec![(0, 0)].into_iter().collect();
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors), None).unwrap();
let (list, idx) = index
.get_test(&key.pubkey(), Some(&ancestors), None)
.unwrap();
assert_eq!(list.slot_list()[idx], (0, true));

let mut num = 0;
Expand Down Expand Up @@ -3427,7 +3450,7 @@ pub mod tests {
assert!(gc.is_empty());

index.add_root(0, false);
let (list, idx) = index.get(&key.pubkey(), None, None).unwrap();
let (list, idx) = index.get_test(&key.pubkey(), None, None).unwrap();
assert_eq!(list.slot_list()[idx], (0, true));
}

Expand Down Expand Up @@ -3540,7 +3563,9 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
assert!(gc.is_empty());
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors), None).unwrap();
let (list, idx) = index
.get_test(&key.pubkey(), Some(&ancestors), None)
.unwrap();
assert_eq!(list.slot_list()[idx], (0, true));
drop(list);

Expand All @@ -3556,7 +3581,9 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
assert_eq!(gc, vec![(0, true)]);
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors), None).unwrap();
let (list, idx) = index
.get_test(&key.pubkey(), Some(&ancestors), None)
.unwrap();
assert_eq!(list.slot_list()[idx], (0, false));
}

Expand Down Expand Up @@ -3589,10 +3616,14 @@ pub mod tests {
UPSERT_PREVIOUS_SLOT_ENTRY_WAS_CACHED_FALSE,
);
assert!(gc.is_empty());
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors), None).unwrap();
let (list, idx) = index
.get_test(&key.pubkey(), Some(&ancestors), None)
.unwrap();
assert_eq!(list.slot_list()[idx], (0, true));
let ancestors = vec![(1, 0)].into_iter().collect();
let (list, idx) = index.get(&key.pubkey(), Some(&ancestors), None).unwrap();
let (list, idx) = index
.get_test(&key.pubkey(), Some(&ancestors), None)
.unwrap();
assert_eq!(list.slot_list()[idx], (1, false));
}

Expand Down Expand Up @@ -3659,7 +3690,7 @@ pub mod tests {
// Updating index should not purge older roots, only purges
// previous updates within the same slot
assert_eq!(gc, vec![]);
let (list, idx) = index.get(&key.pubkey(), None, None).unwrap();
let (list, idx) = index.get_test(&key.pubkey(), None, None).unwrap();
assert_eq!(list.slot_list()[idx], (3, true));

let mut num = 0;
Expand Down

0 comments on commit 2c65f8e

Please sign in to comment.