[TieredStorage] Add AccountOffset type (#33927)

#### Problem
TieredStorage conceptually has different offsets.  However, the current code directly
uses the same primitive type for accessing offsets, which is error-prone as one could
easily use one offset to access data that should be accessed with a different offset
type.

#### Summary of Changes
This PR introduces the AccountOffset type, which allows static-check to on different
type of TieredStorage offsets.
This commit is contained in:
Yueh-Hsuan Chiang 2023-11-06 12:21:08 -08:00 committed by GitHub
parent 70d97d3261
commit da130b87d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 9 deletions

View File

@ -17,6 +17,15 @@ pub struct AccountIndexWriterEntry<'a> {
pub intra_block_offset: u64,
}
/// The offset to an account stored inside its accounts block.
/// This struct is used to access the meta and data of an account by looking through
/// its accounts block.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AccountOffset {
/// The offset to the accounts block that contains the account meta/data.
pub block: usize,
}
/// The index format of a tiered accounts file.
#[repr(u16)]
#[derive(
@ -76,21 +85,22 @@ impl IndexBlockFormat {
Ok(address)
}
/// Returns the offset to the account block that contains the account
/// associated with the specified index to the index block.
pub fn get_account_block_offset(
/// Returns the offset to the account given the specified index.
pub fn get_account_offset(
&self,
map: &Mmap,
footer: &TieredStorageFooter,
index: usize,
) -> TieredStorageResult<u64> {
) -> TieredStorageResult<AccountOffset> {
match self {
Self::AddressAndOffset => {
let offset = footer.index_block_offset as usize
+ std::mem::size_of::<Pubkey>() * footer.account_entry_count as usize
+ index * std::mem::size_of::<u64>();
let (account_block_offset, _) = get_type(map, offset)?;
Ok(*account_block_offset)
Ok(AccountOffset {
block: *account_block_offset,
})
}
}
}
@ -146,10 +156,8 @@ mod tests {
.unwrap();
let map = unsafe { MmapOptions::new().map(&file).unwrap() };
for (i, index_entry) in index_entries.iter().enumerate() {
assert_eq!(
index_entry.block_offset,
indexer.get_account_block_offset(&map, &footer, i).unwrap()
);
let account_offset = indexer.get_account_offset(&map, &footer, i).unwrap();
assert_eq!(index_entry.block_offset, account_offset.block as u64);
let address = indexer.get_account_address(&map, &footer, i).unwrap();
assert_eq!(index_entry.address, address);
}