Skip to content

Commit

Permalink
Uses AccountHash in CalculateHashIntermediate (#33822)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored Oct 24, 2023
1 parent b0dcaf2 commit 2f024f0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
20 changes: 10 additions & 10 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2419,7 +2419,7 @@ impl<'a> AppendVecScan for ScanState<'a> {
}
}
let source_item = CalculateHashIntermediate {
hash: loaded_hash.0,
hash: loaded_hash,
lamports: balance,
pubkey: *pubkey,
};
Expand Down Expand Up @@ -10400,22 +10400,22 @@ pub mod tests {

let mut raw_expected = vec![
CalculateHashIntermediate {
hash: Hash::default(),
hash: AccountHash(Hash::default()),
lamports: 1,
pubkey: pubkey0,
},
CalculateHashIntermediate {
hash: Hash::default(),
hash: AccountHash(Hash::default()),
lamports: 128,
pubkey: pubkey127,
},
CalculateHashIntermediate {
hash: Hash::default(),
hash: AccountHash(Hash::default()),
lamports: 129,
pubkey: pubkey128,
},
CalculateHashIntermediate {
hash: Hash::default(),
hash: AccountHash(Hash::default()),
lamports: 256,
pubkey: pubkey255,
},
Expand All @@ -10438,7 +10438,7 @@ pub mod tests {
));
let hash = AccountsDb::hash_account(&raw_accounts[i], &raw_expected[i].pubkey);
assert_eq!(hash, expected_hashes[i]);
raw_expected[i].hash = hash.0;
raw_expected[i].hash = hash;
}

let to_store = raw_accounts
Expand Down Expand Up @@ -10925,7 +10925,7 @@ pub mod tests {
let (storages, raw_expected) = sample_storages_and_accounts(&db);
let expected_hash =
AccountsHasher::compute_merkle_root_loop(raw_expected.clone(), MERKLE_FANOUT, |item| {
&item.hash
&item.hash.0
});
let sum = raw_expected.iter().map(|item| item.lamports).sum();
let result = db
Expand Down Expand Up @@ -10974,7 +10974,7 @@ pub mod tests {
assert_eq!(loaded_account.pubkey(), &self.pubkey);
assert_eq!(self.slot_expected, self.current_slot);
self.accum.push(vec![CalculateHashIntermediate {
hash: Hash::default(),
hash: AccountHash(Hash::default()),
lamports: self.value_to_use_for_lamports,
pubkey: self.pubkey,
}]);
Expand Down Expand Up @@ -11035,7 +11035,7 @@ pub mod tests {
assert_scan(
result2,
vec![vec![vec![CalculateHashIntermediate {
hash: Hash::default(),
hash: AccountHash(Hash::default()),
lamports: expected,
pubkey,
}]]],
Expand Down Expand Up @@ -11263,7 +11263,7 @@ pub mod tests {
assert_eq!(self.accum.len(), 1);
}
self.accum.push(vec![CalculateHashIntermediate {
hash: Hash::default(),
hash: AccountHash(Hash::default()),
lamports: loaded_account.lamports(),
pubkey: Pubkey::default(),
}]);
Expand Down
55 changes: 29 additions & 26 deletions accounts-db/src/accounts_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,19 @@ impl HashStats {
/// Note this can be saved/loaded during hash calculation to a memory mapped file whose contents are
/// [CalculateHashIntermediate]
#[repr(C)]
#[derive(Default, Debug, PartialEq, Eq, Clone, Copy, Pod, Zeroable)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Pod, Zeroable)]
pub struct CalculateHashIntermediate {
pub hash: Hash,
pub hash: AccountHash,
pub lamports: u64,
pub pubkey: Pubkey,
}

// In order to safely guarantee CalculateHashIntermediate is Pod, it cannot have any padding
const _: () = assert!(
std::mem::size_of::<CalculateHashIntermediate>()
== std::mem::size_of::<Hash>() + std::mem::size_of::<u64>() + std::mem::size_of::<Pubkey>(),
== std::mem::size_of::<AccountHash>()
+ std::mem::size_of::<u64>()
+ std::mem::size_of::<Pubkey>(),
"CalculateHashIntermediate cannot have any padding"
);

Expand Down Expand Up @@ -1168,7 +1170,7 @@ impl<'a> AccountsHasher<'a> {
overall_sum = Self::checked_cast_for_capitalization(
item.lamports as u128 + overall_sum as u128,
);
hashes.write(&item.hash);
hashes.write(&item.hash.0);
}
} else {
// if lamports == 0, check if they should be included
Expand Down Expand Up @@ -1406,7 +1408,7 @@ mod tests {
(0..*count).map(move |_| {
let binner = PubkeyBinCalculator24::new(bins);
CalculateHashIntermediate {
hash: Hash::default(),
hash: AccountHash(Hash::default()),
lamports: 0,
pubkey: binner.lowest_pubkey_from_bin(bin, bins),
}
Expand Down Expand Up @@ -1558,7 +1560,7 @@ mod tests {
let mut account_maps = Vec::new();

let pubkey = Pubkey::from([11u8; 32]);
let hash = Hash::new(&[1u8; 32]);
let hash = AccountHash(Hash::new(&[1u8; 32]));
let val = CalculateHashIntermediate {
hash,
lamports: 88,
Expand All @@ -1568,7 +1570,7 @@ mod tests {

// 2nd key - zero lamports, so will be removed
let pubkey = Pubkey::from([12u8; 32]);
let hash = Hash::new(&[2u8; 32]);
let hash = AccountHash(Hash::new(&[2u8; 32]));
let val = CalculateHashIntermediate {
hash,
lamports: 0,
Expand All @@ -1585,7 +1587,7 @@ mod tests {

// 3rd key - with pubkey value before 1st key so it will be sorted first
let pubkey = Pubkey::from([10u8; 32]);
let hash = Hash::new(&[2u8; 32]);
let hash = AccountHash(Hash::new(&[2u8; 32]));
let val = CalculateHashIntermediate {
hash,
lamports: 20,
Expand All @@ -1600,7 +1602,7 @@ mod tests {

// 3rd key - with later slot
let pubkey = Pubkey::from([10u8; 32]);
let hash = Hash::new(&[99u8; 32]);
let hash = AccountHash(Hash::new(&[99u8; 32]));
let val = CalculateHashIntermediate {
hash,
lamports: 30,
Expand All @@ -1626,7 +1628,8 @@ mod tests {
fn test_accountsdb_de_dup_accounts_zero_chunks() {
let vec = vec![vec![CalculateHashIntermediate {
lamports: 1,
..CalculateHashIntermediate::default()
hash: AccountHash(Hash::default()),
pubkey: Pubkey::default(),
}]];
let temp_vec = vec.to_vec();
let slice = convert_to_slice(&temp_vec);
Expand Down Expand Up @@ -1692,7 +1695,7 @@ mod tests {
let key_b = Pubkey::from([2u8; 32]);
let key_c = Pubkey::from([3u8; 32]);
const COUNT: usize = 6;
let hashes = (0..COUNT).map(|i| Hash::new(&[i as u8; 32]));
let hashes = (0..COUNT).map(|i| AccountHash(Hash::new(&[i as u8; 32])));
// create this vector
// abbbcc
let keys = [key_a, key_b, key_b, key_b, key_c, key_c];
Expand Down Expand Up @@ -1856,15 +1859,15 @@ mod tests {
fn test_accountsdb_compare_two_hash_entries() {
solana_logger::setup();
let pubkey = Pubkey::new_unique();
let hash = Hash::new_unique();
let hash = AccountHash(Hash::new_unique());
let val = CalculateHashIntermediate {
hash,
lamports: 1,
pubkey,
};

// slot same, version <
let hash2 = Hash::new_unique();
let hash2 = AccountHash(Hash::new_unique());
let val2 = CalculateHashIntermediate {
hash: hash2,
lamports: 4,
Expand All @@ -1876,7 +1879,7 @@ mod tests {
);

// slot same, vers =
let hash3 = Hash::new_unique();
let hash3 = AccountHash(Hash::new_unique());
let val3 = CalculateHashIntermediate {
hash: hash3,
lamports: 2,
Expand All @@ -1888,7 +1891,7 @@ mod tests {
);

// slot same, vers >
let hash4 = Hash::new_unique();
let hash4 = AccountHash(Hash::new_unique());
let val4 = CalculateHashIntermediate {
hash: hash4,
lamports: 6,
Expand All @@ -1900,7 +1903,7 @@ mod tests {
);

// slot >, version <
let hash5 = Hash::new_unique();
let hash5 = AccountHash(Hash::new_unique());
let val5 = CalculateHashIntermediate {
hash: hash5,
lamports: 8,
Expand All @@ -1925,7 +1928,7 @@ mod tests {
solana_logger::setup();

let pubkey = Pubkey::new_unique();
let hash = Hash::new_unique();
let hash = AccountHash(Hash::new_unique());
let mut account_maps = Vec::new();
let val = CalculateHashIntermediate {
hash,
Expand All @@ -1939,7 +1942,7 @@ mod tests {
let (hashfile, lamports) = test_de_dup_accounts_in_parallel(&slice);
assert_eq!(
(get_vec(hashfile), lamports),
(vec![val.hash], val.lamports)
(vec![val.hash.0], val.lamports)
);

// zero original lamports, higher version
Expand All @@ -1962,7 +1965,7 @@ mod tests {
for reverse in [false, true] {
let key = Pubkey::new_from_array([1; 32]); // key is BEFORE key2
let key2 = Pubkey::new_from_array([2; 32]);
let hash = Hash::new_unique();
let hash = AccountHash(Hash::new_unique());
let mut account_maps = Vec::new();
let mut account_maps2 = Vec::new();
let val = CalculateHashIntermediate {
Expand Down Expand Up @@ -1993,7 +1996,7 @@ mod tests {
assert_eq!(
(get_vec(hashfile), lamports),
(
vec![val.hash, if reverse { val2.hash } else { val3.hash }],
vec![val.hash.0, if reverse { val2.hash.0 } else { val3.hash.0 }],
val.lamports
+ if reverse {
val2.lamports
Expand All @@ -2012,7 +2015,7 @@ mod tests {
for reverse in [false, true] {
let key = Pubkey::new_from_array([3; 32]); // key is AFTER key2
let key2 = Pubkey::new_from_array([2; 32]);
let hash = Hash::new_unique();
let hash = AccountHash(Hash::new_unique());
let mut account_maps = Vec::new();
let mut account_maps2 = Vec::new();
let val2 = CalculateHashIntermediate {
Expand Down Expand Up @@ -2043,7 +2046,7 @@ mod tests {
assert_eq!(
(get_vec(hashfile), lamports),
(
vec![if reverse { val2.hash } else { val3.hash }, val.hash],
vec![if reverse { val2.hash.0 } else { val3.hash.0 }, val.hash.0],
val.lamports
+ if reverse {
val2.lamports
Expand Down Expand Up @@ -2399,12 +2402,12 @@ mod tests {
let offset = 2;
let input = vec![
CalculateHashIntermediate {
hash: Hash::new(&[1u8; 32]),
hash: AccountHash(Hash::new(&[1u8; 32])),
lamports: u64::MAX - offset,
pubkey: Pubkey::new_unique(),
},
CalculateHashIntermediate {
hash: Hash::new(&[2u8; 32]),
hash: AccountHash(Hash::new(&[2u8; 32])),
lamports: offset + 1,
pubkey: Pubkey::new_unique(),
},
Expand Down Expand Up @@ -2433,12 +2436,12 @@ mod tests {
let offset = 2;
let input = vec![
vec![CalculateHashIntermediate {
hash: Hash::new(&[1u8; 32]),
hash: AccountHash(Hash::new(&[1u8; 32])),
lamports: u64::MAX - offset,
pubkey: Pubkey::new_unique(),
}],
vec![CalculateHashIntermediate {
hash: Hash::new(&[2u8; 32]),
hash: AccountHash(Hash::new(&[2u8; 32])),
lamports: offset + 1,
pubkey: Pubkey::new_unique(),
}],
Expand Down
4 changes: 2 additions & 2 deletions accounts-db/src/cache_hash_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl CacheHashData {

#[cfg(test)]
mod tests {
use {super::*, rand::Rng};
use {super::*, crate::accounts_hash::AccountHash, rand::Rng};

impl CacheHashData {
/// load from 'file_name' into 'accumulator'
Expand Down Expand Up @@ -503,7 +503,7 @@ mod tests {
}

CalculateHashIntermediate {
hash: solana_sdk::hash::Hash::new_unique(),
hash: AccountHash(solana_sdk::hash::Hash::new_unique()),
lamports: ct as u64,
pubkey: pk,
}
Expand Down

0 comments on commit 2f024f0

Please sign in to comment.