Skip to content

Commit

Permalink
adds test
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo committed Dec 11, 2024
1 parent ed25c03 commit eb1b7a0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
57 changes: 56 additions & 1 deletion accounts-db/src/accounts_db/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use {
},
std::{
hash::DefaultHasher,
iter::FromIterator,
iter::{self, FromIterator},
str::FromStr,
sync::{atomic::AtomicBool, RwLock},
thread::{self, Builder, JoinHandle},
Expand Down Expand Up @@ -8146,3 +8146,58 @@ fn compute_merkle_root(hashes: impl IntoIterator<Item = Hash>) -> Hash {
let hashes = hashes.into_iter().collect();
AccountsHasher::compute_merkle_root_recurse(hashes, MERKLE_FANOUT)
}

/// Test that `clean` reclaims old accounts when cleaning old storages
///
/// When `clean` constructs candidates from old storages, pubkeys in these storages may have other
/// newer versions of the accounts in other newer storages *not* explicitly marked to be visited by
/// `clean`. In this case, `clean` should still reclaim the old versions of these accounts.
#[test]
fn test_clean_old_storages_with_reclaims() {
let accounts_db = AccountsDb::new_single_for_tests();
let pubkey = Pubkey::new_unique();
let old_slot = 11;
let new_slot = 22;
let slots = [old_slot, new_slot];
for &slot in &slots {
let account = AccountSharedData::new(slot, 0, &Pubkey::new_unique());
// store `pubkey` into multiple slots, and also store another unique pubkey
// to prevent the whole storage from being marked as dead by `clean`.
accounts_db.store_for_tests(
slot,
&[(&pubkey, &account), (&Pubkey::new_unique(), &account)],
);
accounts_db.calculate_accounts_delta_hash(slot);
accounts_db.add_root_and_flush_write_cache(slot);
}

// ensure the slot list for `pubkey` has both the old and new slots
let slot_list = accounts_db
.accounts_index
.get_bin(&pubkey)
.slot_list_mut(&pubkey, |slot_list| slot_list.clone())
.unwrap();
assert_eq!(slot_list.len(), slots.len());
assert!(slot_list.iter().map(|(slot, _)| slot).eq(slots.iter()));

// for this test, `new_slot` must not be in the uncleaned_roots list
accounts_db.accounts_index.remove_uncleaned_root(new_slot);
assert!(accounts_db.accounts_index.is_uncleaned_root(old_slot));
assert!(!accounts_db.accounts_index.is_uncleaned_root(new_slot));

// `clean` should now reclaim the account in `old_slot`, even though `new_slot` is not
// explicitly being cleaned
accounts_db.clean_accounts_for_tests();

// ensure we've reclaimed the account `old_slot`
let slot_list = accounts_db
.accounts_index
.get_bin(&pubkey)
.slot_list_mut(&pubkey, |slot_list| slot_list.clone())
.unwrap();
assert_eq!(slot_list.len(), 1);
assert!(slot_list
.iter()
.map(|(slot, _)| slot)
.eq(iter::once(&new_slot)));
}
10 changes: 10 additions & 0 deletions accounts-db/src/accounts_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,16 @@ impl<T: IndexValue, U: DiskIndexValue + From<T> + Into<T>> AccountsIndex<T, U> {
w_roots_tracker.uncleaned_roots.extend(roots);
}

/// Removes `root` from `uncleaned_roots` and returns whether it was previously present
#[cfg(feature = "dev-context-only-utils")]
pub fn remove_uncleaned_root(&self, root: Slot) -> bool {
self.roots_tracker
.write()
.unwrap()
.uncleaned_roots
.remove(&root)
}

pub fn max_root_inclusive(&self) -> Slot {
self.roots_tracker
.read()
Expand Down

0 comments on commit eb1b7a0

Please sign in to comment.