Skip to content

Commit

Permalink
StorableAccounts::hash() returns &AccountHash (solana-labs#33748)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored Oct 18, 2023
1 parent d337581 commit 3a580f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
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())
}
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

0 comments on commit 3a580f4

Please sign in to comment.