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

StorableAccounts::hash() returns &AccountHash #33748

Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion accounts-db/src/account_storage/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<'a: 'b, 'b, T: ReadableAccount + Sync + 'b, U: StorableAccounts<'a, T>, V:
let pubkey = self.accounts.pubkey(index);
let (hash, write_version) = if self.accounts.has_hash_and_write_version() {
(
self.accounts.hash(index),
&self.accounts.hash(index).0,
self.accounts.write_version(index),
)
} else {
Expand Down
29 changes: 17 additions & 12 deletions accounts-db/src/storable_accounts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! trait for abstracting underlying storage of pubkey and account pairs to be written
use {
crate::{account_storage::meta::StoredAccountMeta, accounts_db::IncludeSlotInHash},
solana_sdk::{account::ReadableAccount, clock::Slot, hash::Hash, pubkey::Pubkey},
crate::{
account_storage::meta::StoredAccountMeta, accounts_db::IncludeSlotInHash,
accounts_hash::AccountHash,
},
solana_sdk::{account::ReadableAccount, clock::Slot, pubkey::Pubkey},
};

/// abstract access to pubkey, account, slot, target_slot of either:
Expand Down Expand Up @@ -45,7 +48,7 @@ pub trait StorableAccounts<'a, T: ReadableAccount + Sync>: Sync {

/// return hash for account at 'index'
/// Should only be called if 'has_hash_and_write_version' = true
fn hash(&self, _index: usize) -> &Hash {
fn hash(&self, _index: usize) -> &AccountHash {
// this should never be called if has_hash_and_write_version returns false
unimplemented!();
}
Expand Down Expand Up @@ -174,8 +177,8 @@ impl<'a> StorableAccounts<'a, StoredAccountMeta<'a>>
fn has_hash_and_write_version(&self) -> bool {
true
}
fn hash(&self, index: usize) -> &Hash {
self.account(index).hash()
fn hash(&self, index: usize) -> &AccountHash {
bytemuck::cast_ref(self.account(index).hash())
}
fn write_version(&self, index: usize) -> u64 {
self.account(index).write_version()
Expand Down Expand Up @@ -278,8 +281,8 @@ impl<'a> StorableAccounts<'a, StoredAccountMeta<'a>> for StorableAccountsBySlot<
fn has_hash_and_write_version(&self) -> bool {
true
}
fn hash(&self, index: usize) -> &Hash {
self.account(index).hash()
fn hash(&self, index: usize) -> &AccountHash {
bytemuck::cast_ref(self.account(index).hash())
}
fn write_version(&self, index: usize) -> u64 {
self.account(index).write_version()
Expand Down Expand Up @@ -318,8 +321,8 @@ impl<'a> StorableAccounts<'a, StoredAccountMeta<'a>>
fn has_hash_and_write_version(&self) -> bool {
true
}
fn hash(&self, index: usize) -> &Hash {
self.account(index).hash()
fn hash(&self, index: usize) -> &AccountHash {
bytemuck::cast_ref(self.account(index).hash())
Copy link
Contributor Author

@brooksprumo brooksprumo Oct 18, 2023

Choose a reason for hiding this comment

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

A subsequent PR will handle returning AccountHash from this account's hash() function. Until then, we can safely cast it.

}
fn write_version(&self, index: usize) -> u64 {
self.account(index).write_version()
Expand Down Expand Up @@ -522,7 +525,9 @@ pub mod tests {
// each one containing a subset of the overall # of entries (0..4)
for entries in 0..6 {
let data = Vec::default();
let hashes = (0..entries).map(|_| Hash::new_unique()).collect::<Vec<_>>();
let hashes = (0..entries)
.map(|_| AccountHash(Hash::new_unique()))
.collect::<Vec<_>>();
let mut raw = Vec::new();
let mut raw2 = Vec::new();
for entry in 0..entries {
Expand Down Expand Up @@ -559,7 +564,7 @@ pub mod tests {
data: &data,
offset,
stored_size,
hash: &hashes[entry as usize],
hash: &hashes[entry as usize].0,
}));
}
let raw2_refs = raw2.iter().collect::<Vec<_>>();
Expand Down Expand Up @@ -601,7 +606,7 @@ pub mod tests {
let index = index as usize;
assert_eq!(storable.account(index), &raw2[index]);
assert_eq!(storable.pubkey(index), raw2[index].pubkey());
assert_eq!(storable.hash(index), raw2[index].hash());
assert_eq!(&storable.hash(index).0, raw2[index].hash());
assert_eq!(storable.slot(index), expected_slots[index]);
assert_eq!(storable.write_version(index), raw2[index].write_version());
})
Expand Down