use {
crate::{account_storage::meta::StoredAccountMeta, accounts_hash::AccountHash},
solana_sdk::{account::ReadableAccount, clock::Slot, pubkey::Pubkey},
};
pub trait StorableAccounts<'a, T: ReadableAccount + Sync>: Sync {
fn pubkey(&self, index: usize) -> &Pubkey;
fn account(&self, index: usize) -> &T;
fn account_default_if_zero_lamport(&self, index: usize) -> Option<&T> {
let account = self.account(index);
(account.lamports() != 0).then_some(account)
}
fn slot(&self, index: usize) -> Slot;
fn target_slot(&self) -> Slot;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn len(&self) -> usize;
fn contains_multiple_slots(&self) -> bool {
false
}
fn has_hash_and_write_version(&self) -> bool {
false
}
fn hash(&self, _index: usize) -> &AccountHash {
unimplemented!();
}
fn write_version(&self, _index: usize) -> u64 {
unimplemented!();
}
}
pub struct StorableAccountsMovingSlots<'a, T: ReadableAccount + Sync> {
pub accounts: &'a [(&'a Pubkey, &'a T)],
pub target_slot: Slot,
pub old_slot: Slot,
}
impl<'a, T: ReadableAccount + Sync> StorableAccounts<'a, T> for StorableAccountsMovingSlots<'a, T> {
fn pubkey(&self, index: usize) -> &Pubkey {
self.accounts[index].0
}
fn account(&self, index: usize) -> &T {
self.accounts[index].1
}
fn slot(&self, _index: usize) -> Slot {
self.old_slot
}
fn target_slot(&self) -> Slot {
self.target_slot
}
fn len(&self) -> usize {
self.accounts.len()
}
}
impl<'a, T: ReadableAccount + Sync> StorableAccounts<'a, T> for (Slot, &'a [(&'a Pubkey, &'a T)]) {
fn pubkey(&self, index: usize) -> &Pubkey {
self.1[index].0
}
fn account(&self, index: usize) -> &T {
self.1[index].1
}
fn slot(&self, _index: usize) -> Slot {
self.target_slot()
}
fn target_slot(&self) -> Slot {
self.0
}
fn len(&self) -> usize {
self.1.len()
}
}
impl<'a, T: ReadableAccount + Sync> StorableAccounts<'a, T> for (Slot, &'a [&'a (Pubkey, T)]) {
fn pubkey(&self, index: usize) -> &Pubkey {
&self.1[index].0
}
fn account(&self, index: usize) -> &T {
&self.1[index].1
}
fn slot(&self, _index: usize) -> Slot {
self.target_slot()
}
fn target_slot(&self) -> Slot {
self.0
}
fn len(&self) -> usize {
self.1.len()
}
}
impl<'a> StorableAccounts<'a, StoredAccountMeta<'a>> for (Slot, &'a [&'a StoredAccountMeta<'a>]) {
fn pubkey(&self, index: usize) -> &Pubkey {
self.account(index).pubkey()
}
fn account(&self, index: usize) -> &StoredAccountMeta<'a> {
self.1[index]
}
fn slot(&self, _index: usize) -> Slot {
self.0
}
fn target_slot(&self) -> Slot {
self.0
}
fn len(&self) -> usize {
self.1.len()
}
fn has_hash_and_write_version(&self) -> bool {
true
}
fn hash(&self, index: usize) -> &AccountHash {
self.account(index).hash()
}
fn write_version(&self, index: usize) -> u64 {
self.account(index).write_version()
}
}
pub struct StorableAccountsBySlot<'a> {
target_slot: Slot,
slots_and_accounts: &'a [(Slot, &'a [&'a StoredAccountMeta<'a>])],
starting_offsets: Vec<usize>,
contains_multiple_slots: bool,
len: usize,
}
impl<'a> StorableAccountsBySlot<'a> {
pub fn new(
target_slot: Slot,
slots_and_accounts: &'a [(Slot, &'a [&'a StoredAccountMeta<'a>])],
) -> Self {
let mut cumulative_len = 0usize;
let mut starting_offsets = Vec::with_capacity(slots_and_accounts.len());
let first_slot = slots_and_accounts
.first()
.map(|(slot, _)| *slot)
.unwrap_or_default();
let mut contains_multiple_slots = false;
for (slot, accounts) in slots_and_accounts {
cumulative_len = cumulative_len.saturating_add(accounts.len());
starting_offsets.push(cumulative_len);
contains_multiple_slots |= &first_slot != slot;
}
Self {
target_slot,
slots_and_accounts,
starting_offsets,
contains_multiple_slots,
len: cumulative_len,
}
}
fn find_internal_index(&self, index: usize) -> (usize, usize) {
for (offset_index, next_offset) in self.starting_offsets.iter().enumerate() {
if next_offset > &index {
let prior_offset = if offset_index > 0 {
self.starting_offsets[offset_index.saturating_sub(1)]
} else {
0
};
return (offset_index, index - prior_offset);
}
}
panic!("failed");
}
}
impl<'a> StorableAccounts<'a, StoredAccountMeta<'a>> for StorableAccountsBySlot<'a> {
fn pubkey(&self, index: usize) -> &Pubkey {
self.account(index).pubkey()
}
fn account(&self, index: usize) -> &StoredAccountMeta<'a> {
let indexes = self.find_internal_index(index);
self.slots_and_accounts[indexes.0].1[indexes.1]
}
fn slot(&self, index: usize) -> Slot {
let indexes = self.find_internal_index(index);
self.slots_and_accounts[indexes.0].0
}
fn target_slot(&self) -> Slot {
self.target_slot
}
fn len(&self) -> usize {
self.len
}
fn contains_multiple_slots(&self) -> bool {
self.contains_multiple_slots
}
fn has_hash_and_write_version(&self) -> bool {
true
}
fn hash(&self, index: usize) -> &AccountHash {
self.account(index).hash()
}
fn write_version(&self, index: usize) -> u64 {
self.account(index).write_version()
}
}
impl<'a> StorableAccounts<'a, StoredAccountMeta<'a>>
for (Slot, &'a [&'a StoredAccountMeta<'a>], Slot)
{
fn pubkey(&self, index: usize) -> &Pubkey {
self.account(index).pubkey()
}
fn account(&self, index: usize) -> &StoredAccountMeta<'a> {
self.1[index]
}
fn slot(&self, _index: usize) -> Slot {
self.2
}
fn target_slot(&self) -> Slot {
self.0
}
fn len(&self) -> usize {
self.1.len()
}
fn has_hash_and_write_version(&self) -> bool {
true
}
fn hash(&self, index: usize) -> &AccountHash {
self.account(index).hash()
}
fn write_version(&self, index: usize) -> u64 {
self.account(index).write_version()
}
}
#[cfg(test)]
pub mod tests {
use {
super::*,
crate::{
account_storage::meta::{AccountMeta, StoredAccountMeta, StoredMeta},
append_vec::AppendVecStoredAccountMeta,
},
solana_sdk::{
account::{accounts_equal, AccountSharedData, WritableAccount},
hash::Hash,
},
};
fn compare<
'a,
T: ReadableAccount + Sync + PartialEq + std::fmt::Debug,
U: ReadableAccount + Sync + PartialEq + std::fmt::Debug,
>(
a: &impl StorableAccounts<'a, T>,
b: &impl StorableAccounts<'a, U>,
) {
assert_eq!(a.target_slot(), b.target_slot());
assert_eq!(a.len(), b.len());
assert_eq!(a.is_empty(), b.is_empty());
(0..a.len()).for_each(|i| {
assert_eq!(a.pubkey(i), b.pubkey(i));
assert!(accounts_equal(a.account(i), b.account(i)));
})
}
#[test]
fn test_contains_multiple_slots() {
let pk = Pubkey::from([1; 32]);
let slot = 0;
let lamports = 1;
let owner = Pubkey::default();
let executable = false;
let rent_epoch = 0;
let meta = StoredMeta {
write_version_obsolete: 5,
pubkey: pk,
data_len: 7,
};
let account_meta = AccountMeta {
lamports,
owner,
executable,
rent_epoch,
};
let data = Vec::default();
let offset = 99;
let stored_size = 101;
let hash = AccountHash(Hash::new_unique());
let stored_account = StoredAccountMeta::AppendVec(AppendVecStoredAccountMeta {
meta: &meta,
account_meta: &account_meta,
data: &data,
offset,
stored_size,
hash: &hash,
});
let test3 = (slot, &vec![&stored_account, &stored_account][..], slot);
assert!(!test3.contains_multiple_slots());
}
#[test]
fn test_storable_accounts() {
let max_slots = 3_u64;
for target_slot in 0..max_slots {
for entries in 0..2 {
for starting_slot in 0..max_slots {
let data = Vec::default();
let hash = AccountHash(Hash::new_unique());
let mut raw = Vec::new();
let mut raw2 = Vec::new();
let mut raw4 = Vec::new();
for entry in 0..entries {
let pk = Pubkey::from([entry; 32]);
let account = AccountSharedData::create(
(entry as u64) * starting_slot,
Vec::default(),
Pubkey::default(),
false,
0,
);
raw.push((
pk,
account.clone(),
starting_slot % max_slots,
StoredMeta {
write_version_obsolete: 0, pubkey: pk,
data_len: u64::MAX, },
AccountMeta {
lamports: account.lamports(),
owner: *account.owner(),
executable: account.executable(),
rent_epoch: account.rent_epoch(),
},
));
}
for entry in 0..entries {
let offset = 99;
let stored_size = 101;
let raw = &raw[entry as usize];
raw2.push(StoredAccountMeta::AppendVec(AppendVecStoredAccountMeta {
meta: &raw.3,
account_meta: &raw.4,
data: &data,
offset,
stored_size,
hash: &hash,
}));
raw4.push((raw.0, raw.1.clone()));
}
let mut two = Vec::new();
let mut three = Vec::new();
let mut four_pubkey_and_account_value = Vec::new();
raw.iter()
.zip(raw2.iter().zip(raw4.iter()))
.for_each(|(raw, (raw2, raw4))| {
two.push((&raw.0, &raw.1)); three.push(raw2);
four_pubkey_and_account_value.push(raw4);
});
let test2 = (target_slot, &two[..]);
let test4 = (target_slot, &four_pubkey_and_account_value[..]);
let source_slot = starting_slot % max_slots;
let test3 = (target_slot, &three[..], source_slot);
let old_slot = starting_slot;
let test_moving_slots = StorableAccountsMovingSlots {
accounts: &two[..],
target_slot,
old_slot,
};
let for_slice = [(old_slot, &three[..])];
let test_moving_slots2 = StorableAccountsBySlot::new(target_slot, &for_slice);
compare(&test2, &test3);
compare(&test2, &test4);
compare(&test2, &test_moving_slots);
compare(&test2, &test_moving_slots2);
for (i, raw) in raw.iter().enumerate() {
assert_eq!(raw.0, *test3.pubkey(i));
assert!(accounts_equal(&raw.1, test3.account(i)));
assert_eq!(raw.2, test3.slot(i));
assert_eq!(target_slot, test4.slot(i));
assert_eq!(target_slot, test2.slot(i));
assert_eq!(old_slot, test_moving_slots.slot(i));
assert_eq!(old_slot, test_moving_slots2.slot(i));
}
assert_eq!(target_slot, test3.target_slot());
assert_eq!(target_slot, test4.target_slot());
assert_eq!(target_slot, test_moving_slots2.target_slot());
assert!(!test2.contains_multiple_slots());
assert!(!test4.contains_multiple_slots());
assert!(!test_moving_slots.contains_multiple_slots());
assert_eq!(test3.contains_multiple_slots(), entries > 1);
}
}
}
}
#[test]
fn test_storable_accounts_by_slot() {
solana_logger::setup();
for entries in 0..6 {
let data = Vec::default();
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 {
let pk = Pubkey::from([entry; 32]);
let account = AccountSharedData::create(
entry as u64,
Vec::default(),
Pubkey::default(),
false,
0,
);
raw.push((
pk,
account.clone(),
StoredMeta {
write_version_obsolete: 500 + (entry * 3) as u64, pubkey: pk,
data_len: (entry * 2) as u64, },
AccountMeta {
lamports: account.lamports(),
owner: *account.owner(),
executable: account.executable(),
rent_epoch: account.rent_epoch(),
},
));
}
for entry in 0..entries {
let offset = 99;
let stored_size = 101;
raw2.push(StoredAccountMeta::AppendVec(AppendVecStoredAccountMeta {
meta: &raw[entry as usize].2,
account_meta: &raw[entry as usize].3,
data: &data,
offset,
stored_size,
hash: &hashes[entry as usize],
}));
}
let raw2_refs = raw2.iter().collect::<Vec<_>>();
for entries0 in 0..=entries {
let remaining1 = entries.saturating_sub(entries0);
for entries1 in 0..=remaining1 {
let remaining2 = entries.saturating_sub(entries0 + entries1);
for entries2 in 0..=remaining2 {
let remaining3 = entries.saturating_sub(entries0 + entries1 + entries2);
let entries_by_level = [entries0, entries1, entries2, remaining3];
let mut overall_index = 0;
let mut expected_slots = Vec::default();
let slots_and_accounts = entries_by_level
.iter()
.enumerate()
.filter_map(|(slot, count)| {
let slot = slot as Slot;
let count = *count as usize;
(overall_index < raw2.len()).then(|| {
let range = overall_index..(overall_index + count);
let result = &raw2_refs[range.clone()];
range.for_each(|_| expected_slots.push(slot));
overall_index += count;
(slot, result)
})
})
.collect::<Vec<_>>();
let storable = StorableAccountsBySlot::new(99, &slots_and_accounts[..]);
assert!(storable.has_hash_and_write_version());
assert_eq!(99, storable.target_slot());
assert_eq!(entries0 != entries, storable.contains_multiple_slots());
(0..entries).for_each(|index| {
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.slot(index), expected_slots[index]);
assert_eq!(storable.write_version(index), raw2[index].write_version());
})
}
}
}
}
}
}