Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AccountsIndex::get_cloned() *must* add entry to in-mem cache #35322

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions accounts-db/src/accounts_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,9 +1158,11 @@ impl<T: IndexValue, U: DiskIndexValue + From<T> + Into<T>> AccountsIndex<T, U> {
/// Gets the index's entry for `pubkey` and clones it
///
/// Prefer `get_and_then()` whenever possible.
/// NOTE: The entry is *not* added to the in-mem cache.
pub fn get_cloned(&self, pubkey: &Pubkey) -> Option<AccountMapEntry<T>> {
self.get_and_then(pubkey, |entry| (false, entry.cloned()))
// We *must* add the index entry to the in-mem cache!
// If the index entry is only on-disk, returning a clone would allow the entry
// to be modified, but those modifications would be lost on drop!
self.get_and_then(pubkey, |entry| (true, entry.cloned()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the caller is only reading the entry? I wonder if we should add a bool arg to the fn and let the caller decide whether the entry need to be added to in-mem cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a follow-up PR in the works that'll enable this safely. Right now, the caller can use get_and_then() to clone and decide to add to the in-mem cache or not. If they do this wrong, it's still unsafe.

}

/// Is `pubkey` in the index?
Expand Down
Loading