Skip to content

Commit

Permalink
Uses AccountHash in StorableAccountsWithHashesAndWriteVersions
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo committed Oct 18, 2023
1 parent f16d46a commit c5370a5
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 64 deletions.
12 changes: 7 additions & 5 deletions accounts-db/benches/append_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use {
StorableAccountsWithHashesAndWriteVersions, StoredAccountInfo, StoredMeta,
},
accounts_db::INCLUDE_SLOT_IN_HASH_TESTS,
accounts_hash::AccountHash,
append_vec::{
test_utils::{create_test_account, get_append_vec_path},
AppendVec,
Expand All @@ -33,7 +34,7 @@ fn append_account(
vec: &AppendVec,
storage_meta: StoredMeta,
account: &AccountSharedData,
hash: Hash,
hash: AccountHash,
) -> Option<StoredAccountInfo> {
let slot_ignored = Slot::MAX;
let accounts = [(&storage_meta.pubkey, account)];
Expand All @@ -55,7 +56,7 @@ fn append_vec_append(bencher: &mut Bencher) {
let vec = AppendVec::new(&path.path, true, 64 * 1024);
bencher.iter(|| {
let (meta, account) = create_test_account(0);
if append_account(&vec, meta, &account, Hash::default()).is_none() {
if append_account(&vec, meta, &account, AccountHash(Hash::default())).is_none() {
vec.reset();
}
});
Expand All @@ -65,7 +66,8 @@ fn add_test_accounts(vec: &AppendVec, size: usize) -> Vec<(usize, usize)> {
(0..size)
.filter_map(|sample| {
let (meta, account) = create_test_account(sample);
append_account(vec, meta, &account, Hash::default()).map(|info| (sample, info.offset))
append_account(vec, meta, &account, AccountHash(Hash::default()))
.map(|info| (sample, info.offset))
})
.collect()
}
Expand Down Expand Up @@ -110,7 +112,7 @@ fn append_vec_concurrent_append_read(bencher: &mut Bencher) {
spawn(move || loop {
let sample = indexes1.lock().unwrap().len();
let (meta, account) = create_test_account(sample);
if let Some(info) = append_account(&vec1, meta, &account, Hash::default()) {
if let Some(info) = append_account(&vec1, meta, &account, AccountHash(Hash::default())) {
indexes1.lock().unwrap().push((sample, info.offset))
} else {
break;
Expand Down Expand Up @@ -150,7 +152,7 @@ fn append_vec_concurrent_read_append(bencher: &mut Bencher) {
bencher.iter(|| {
let sample: usize = thread_rng().gen_range(0..256);
let (meta, account) = create_test_account(sample);
if let Some(info) = append_account(&vec, meta, &account, Hash::default()) {
if let Some(info) = append_account(&vec, meta, &account, AccountHash(Hash::default())) {
indexes.lock().unwrap().push((sample, info.offset))
}
});
Expand Down
27 changes: 17 additions & 10 deletions accounts-db/src/account_storage/meta.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
crate::{
accounts_hash::AccountHash,
append_vec::AppendVecStoredAccountMeta,
storable_accounts::StorableAccounts,
tiered_storage::{hot::HotAccountMeta, readable::TieredReadableAccount},
Expand All @@ -17,7 +18,7 @@ pub struct StoredAccountInfo {
}

lazy_static! {
static ref DEFAULT_ACCOUNT_HASH: Hash = Hash::default();
static ref DEFAULT_ACCOUNT_HASH: AccountHash = AccountHash(Hash::default());
}

/// Goal is to eliminate copies and data reshaping given various code paths that store accounts.
Expand All @@ -30,7 +31,7 @@ pub struct StorableAccountsWithHashesAndWriteVersions<
'b,
T: ReadableAccount + Sync + 'b,
U: StorableAccounts<'a, T>,
V: Borrow<Hash>,
V: Borrow<AccountHash>,
> {
/// accounts to store
/// always has pubkey and account
Expand All @@ -41,8 +42,13 @@ pub struct StorableAccountsWithHashesAndWriteVersions<
_phantom: PhantomData<&'a T>,
}

impl<'a: 'b, 'b, T: ReadableAccount + Sync + 'b, U: StorableAccounts<'a, T>, V: Borrow<Hash>>
StorableAccountsWithHashesAndWriteVersions<'a, 'b, T, U, V>
impl<
'a: 'b,
'b,
T: ReadableAccount + Sync + 'b,
U: StorableAccounts<'a, T>,
V: Borrow<AccountHash>,
> StorableAccountsWithHashesAndWriteVersions<'a, 'b, T, U, V>
{
/// used when accounts contains hash and write version already
pub fn new(accounts: &'b U) -> Self {
Expand Down Expand Up @@ -71,12 +77,12 @@ impl<'a: 'b, 'b, T: ReadableAccount + Sync + 'b, U: StorableAccounts<'a, T>, V:
}

/// get all account fields at 'index'
pub fn get(&self, index: usize) -> (Option<&T>, &Pubkey, &Hash, StoredMetaWriteVersion) {
pub fn get(&self, index: usize) -> (Option<&T>, &Pubkey, &AccountHash, StoredMetaWriteVersion) {
let account = self.accounts.account_default_if_zero_lamport(index);
let pubkey = self.accounts.pubkey(index);
let (hash, write_version) = if self.accounts.has_hash_and_write_version() {
(
&self.accounts.hash(index).0,
self.accounts.hash(index),
self.accounts.write_version(index),
)
} else {
Expand Down Expand Up @@ -119,11 +125,12 @@ impl<'storage> StoredAccountMeta<'storage> {
}
}

pub fn hash(&self) -> &'storage Hash {
match self {
pub fn hash(&self) -> &'storage AccountHash {
let hash = match self {
Self::AppendVec(av) => av.hash(),
Self::Hot(hot) => hot.hash().unwrap_or(&DEFAULT_ACCOUNT_HASH),
}
Self::Hot(hot) => hot.hash().unwrap_or(&DEFAULT_ACCOUNT_HASH.0),
};
bytemuck::cast_ref(hash)
}

pub fn stored_size(&self) -> usize {
Expand Down
46 changes: 26 additions & 20 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ impl CurrentAncientAppendVec {
INCLUDE_SLOT_IN_HASH_IRRELEVANT_APPEND_VEC_OPERATION,
accounts_to_store.slot,
),
None::<Vec<Hash>>,
None::<Vec<AccountHash>>,
self.append_vec(),
None,
StoreReclaims::Ignore,
Expand Down Expand Up @@ -896,7 +896,7 @@ pub enum LoadedAccount<'a> {
impl<'a> LoadedAccount<'a> {
pub fn loaded_hash(&self) -> AccountHash {
match self {
LoadedAccount::Stored(stored_account_meta) => AccountHash(*stored_account_meta.hash()),
LoadedAccount::Stored(stored_account_meta) => *stored_account_meta.hash(),
LoadedAccount::Cached(cached_account) => cached_account.hash(),
}
}
Expand Down Expand Up @@ -4126,7 +4126,7 @@ impl AccountsDb {
&shrink_collect.alive_accounts.alive_accounts()[..],
INCLUDE_SLOT_IN_HASH_IRRELEVANT_APPEND_VEC_OPERATION,
),
None::<Vec<&Hash>>,
None::<Vec<AccountHash>>,
shrink_in_progress.new_storage(),
None,
StoreReclaims::Ignore,
Expand Down Expand Up @@ -6245,7 +6245,7 @@ impl AccountsDb {
'b,
T: ReadableAccount + Sync,
U: StorableAccounts<'a, T>,
V: Borrow<Hash>,
V: Borrow<AccountHash>,
>(
&self,
slot: Slot,
Expand Down Expand Up @@ -6612,7 +6612,7 @@ impl AccountsDb {
let pubkeys = self.get_filler_account_pubkeys(filler_accounts as usize);
pubkeys.iter().for_each(|key| {
accounts.push((key, &account));
hashes.push(hash);
hashes.push(AccountHash(hash));
});
self.store_accounts_frozen(
(slot, &accounts[..], include_slot_in_hash),
Expand Down Expand Up @@ -6684,7 +6684,9 @@ impl AccountsDb {
INCLUDE_SLOT_IN_HASH_IRRELEVANT_APPEND_VEC_OPERATION,
);
let storable =
StorableAccountsWithHashesAndWriteVersions::<'_, '_, _, _, &Hash>::new(&to_store);
StorableAccountsWithHashesAndWriteVersions::<'_, '_, _, _, &AccountHash>::new(
&to_store,
);
storage.accounts.append_accounts(&storable, 0);

Arc::new(storage)
Expand Down Expand Up @@ -6796,7 +6798,7 @@ impl AccountsDb {
>(
&self,
accounts: &'c impl StorableAccounts<'b, T>,
hashes: Option<Vec<impl Borrow<Hash>>>,
hashes: Option<Vec<impl Borrow<AccountHash>>>,
mut write_version_producer: P,
store_to: &StoreTo,
transactions: Option<&[Option<&'a SanitizedTransaction>]>,
Expand Down Expand Up @@ -6836,7 +6838,7 @@ impl AccountsDb {
self.write_accounts_to_storage(
slot,
storage,
&StorableAccountsWithHashesAndWriteVersions::<'_, '_, _, _, &Hash>::new(
&StorableAccountsWithHashesAndWriteVersions::<'_, '_, _, _, &AccountHash>::new(
accounts,
),
)
Expand Down Expand Up @@ -8544,7 +8546,7 @@ impl AccountsDb {
// we use default hashes for now since the same account may be stored to the cache multiple times
self.store_accounts_unfrozen(
accounts,
None::<Vec<Hash>>,
None::<Vec<AccountHash>>,
store_to,
transactions,
reclaim,
Expand Down Expand Up @@ -8698,7 +8700,7 @@ impl AccountsDb {
fn store_accounts_unfrozen<'a, T: ReadableAccount + Sync + ZeroLamport + 'a>(
&self,
accounts: impl StorableAccounts<'a, T>,
hashes: Option<Vec<impl Borrow<Hash>>>,
hashes: Option<Vec<impl Borrow<AccountHash>>>,
store_to: &StoreTo,
transactions: Option<&'a [Option<&'a SanitizedTransaction>]>,
reclaim: StoreReclaims,
Expand Down Expand Up @@ -8727,7 +8729,7 @@ impl AccountsDb {
pub fn store_accounts_frozen<'a, T: ReadableAccount + Sync + ZeroLamport + 'a>(
&self,
accounts: impl StorableAccounts<'a, T>,
hashes: Option<Vec<impl Borrow<Hash>>>,
hashes: Option<Vec<impl Borrow<AccountHash>>>,
storage: &Arc<AccountStorageEntry>,
write_version_producer: Option<Box<dyn Iterator<Item = StoredMetaWriteVersion>>>,
reclaim: StoreReclaims,
Expand All @@ -8751,7 +8753,7 @@ impl AccountsDb {
fn store_accounts_custom<'a, T: ReadableAccount + Sync + ZeroLamport + 'a>(
&self,
accounts: impl StorableAccounts<'a, T>,
hashes: Option<Vec<impl Borrow<Hash>>>,
hashes: Option<Vec<impl Borrow<AccountHash>>>,
write_version_producer: Option<Box<dyn Iterator<Item = u64>>>,
store_to: &StoreTo,
reset_accounts: bool,
Expand Down Expand Up @@ -10114,7 +10116,10 @@ pub mod tests {
let expected_accounts_data_len = data.last().unwrap().1.data().len();
let expected_alive_bytes = aligned_stored_size(expected_accounts_data_len);
let storable = (slot0, &data[..], INCLUDE_SLOT_IN_HASH_TESTS);
let hashes = data.iter().map(|_| Hash::default()).collect::<Vec<_>>();
let hashes = data
.iter()
.map(|_| AccountHash(Hash::default()))
.collect::<Vec<_>>();
let write_versions = data.iter().map(|_| 0).collect::<Vec<_>>();
let append =
StorableAccountsWithHashesAndWriteVersions::new_with_hashes_and_write_versions(
Expand Down Expand Up @@ -10608,7 +10613,7 @@ pub mod tests {
accounts_db.storage.remove(&storage.slot(), false);
});

let hash = Hash::default();
let hash = AccountHash(Hash::default());

// replace the sample storages, storing default hash values so that we rehash during scan
let storages = storages
Expand Down Expand Up @@ -10655,7 +10660,8 @@ pub mod tests {
sample_storages_and_accounts(&accounts_db, INCLUDE_SLOT_IN_HASH_TESTS);
let max_slot = storages.iter().map(|storage| storage.slot()).max().unwrap();

let hash = Hash::from_str("7JcmM6TFZMkcDkZe6RKVkGaWwN5dXciGC4fa3RxvqQc9").unwrap();
let hash =
AccountHash(Hash::from_str("7JcmM6TFZMkcDkZe6RKVkGaWwN5dXciGC4fa3RxvqQc9").unwrap());

// replace the sample storages, storing bogus hash values so that we trigger the hash mismatch
let storages = storages
Expand Down Expand Up @@ -11158,7 +11164,7 @@ pub mod tests {
let accounts = [(pubkey, account)];
let slice = &accounts[..];
let account_data = (slot, slice);
let hash = Hash::default();
let hash = AccountHash(Hash::default());
let storable_accounts =
StorableAccountsWithHashesAndWriteVersions::new_with_hashes_and_write_versions(
&account_data,
Expand Down Expand Up @@ -12758,7 +12764,7 @@ pub mod tests {
// put wrong hash value in store so we get a mismatch
db.store_accounts_unfrozen(
(some_slot, &[(&key, &account)][..]),
Some(vec![&Hash::default()]),
Some(vec![&AccountHash(Hash::default())]),
&StoreTo::Storage(&db.find_storage_candidate(some_slot, 1)),
None,
StoreReclaims::Default,
Expand Down Expand Up @@ -12992,7 +12998,7 @@ pub mod tests {
db.update_accounts_hash_for_tests(some_slot, &ancestors, false, false);

// provide bogus account hashes
let some_hash = Hash::new(&[0xca; HASH_BYTES]);
let some_hash = AccountHash(Hash::new(&[0xca; HASH_BYTES]));
db.store_accounts_unfrozen(
(some_slot, accounts),
Some(vec![&some_hash]),
Expand Down Expand Up @@ -15830,7 +15836,7 @@ pub mod tests {
accounts.accounts_index.set_startup(Startup::Startup);

let storage = accounts.create_and_insert_store(slot0, 4_000, "flush_slot_cache");
let hashes = vec![Hash::default(); 1];
let hashes = vec![AccountHash(Hash::default()); 1];
let write_version = vec![0; 1];
storage.accounts.append_accounts(
&StorableAccountsWithHashesAndWriteVersions::new_with_hashes_and_write_versions(
Expand Down Expand Up @@ -15895,7 +15901,7 @@ pub mod tests {
let account_big = AccountSharedData::new(1, 1000, AccountSharedData::default().owner());
let slot0 = 0;
let storage = accounts.create_and_insert_store(slot0, 4_000, "flush_slot_cache");
let hashes = vec![Hash::default(); 2];
let hashes = vec![AccountHash(Hash::default()); 2];
let write_version = vec![0; 2];
storage.accounts.append_accounts(
&StorableAccountsWithHashesAndWriteVersions::new_with_hashes_and_write_versions(
Expand Down
5 changes: 3 additions & 2 deletions accounts-db/src/accounts_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use {
account_storage::meta::{
StorableAccountsWithHashesAndWriteVersions, StoredAccountInfo, StoredAccountMeta,
},
accounts_hash::AccountHash,
append_vec::{AppendVec, AppendVecError, MatchAccountOwnerError},
storable_accounts::StorableAccounts,
tiered_storage::error::TieredStorageError,
},
solana_sdk::{account::ReadableAccount, clock::Slot, hash::Hash, pubkey::Pubkey},
solana_sdk::{account::ReadableAccount, clock::Slot, pubkey::Pubkey},
std::{
borrow::Borrow,
mem,
Expand Down Expand Up @@ -154,7 +155,7 @@ impl AccountsFile {
'b,
T: ReadableAccount + Sync,
U: StorableAccounts<'a, T>,
V: Borrow<Hash>,
V: Borrow<AccountHash>,
>(
&self,
accounts: &StorableAccountsWithHashesAndWriteVersions<'a, 'b, T, U, V>,
Expand Down
6 changes: 0 additions & 6 deletions accounts-db/src/accounts_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,12 +1248,6 @@ pub struct AccountHash(pub Hash);
// This also ensures there are no padding bytes, which is requried to safely implement Pod
const _: () = assert!(std::mem::size_of::<AccountHash>() == std::mem::size_of::<Hash>());

impl Borrow<Hash> for AccountHash {
fn borrow(&self) -> &Hash {
&self.0
}
}

/// Hash of accounts
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum AccountsHashKind {
Expand Down
7 changes: 3 additions & 4 deletions accounts-db/src/ancient_append_vecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {
INCLUDE_SLOT_IN_HASH_IRRELEVANT_APPEND_VEC_OPERATION,
},
accounts_file::AccountsFile,
accounts_hash::AccountHash,
accounts_index::{AccountsIndexScanResult, ZeroLamport},
active_stats::ActiveStatItem,
append_vec::aligned_stored_size,
Expand All @@ -20,9 +21,7 @@ use {
rand::{thread_rng, Rng},
rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator},
solana_measure::measure_us,
solana_sdk::{
account::ReadableAccount, clock::Slot, hash::Hash, pubkey::Pubkey, saturating_add_assign,
},
solana_sdk::{account::ReadableAccount, clock::Slot, pubkey::Pubkey, saturating_add_assign},
std::{
collections::HashMap,
num::NonZeroU64,
Expand Down Expand Up @@ -375,7 +374,7 @@ impl AccountsDb {
measure_us!(self.get_store_for_shrink(target_slot, bytes));
let (store_accounts_timing, rewrite_elapsed_us) = measure_us!(self.store_accounts_frozen(
accounts_to_write,
None::<Vec<Hash>>,
None::<Vec<AccountHash>>,
shrink_in_progress.new_storage(),
None,
StoreReclaims::Ignore,
Expand Down
Loading

0 comments on commit c5370a5

Please sign in to comment.