From c73f22695845d9163fe9e9b937a00fb17103a556 Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang <93241502+yhchiang-sol@users.noreply.github.com> Date: Mon, 20 Nov 2023 14:57:15 -0800 Subject: [PATCH] [TieredStorage] Make IndexOffset use u32 (#34152) #### Problem IndexOffset currently uses `usize`, which size is platform dependent. We want a fixed size type that is consist to what we persist in the tiered-storage file. #### Summary of Changes This PR makes IndexOffset use u32. --- accounts-db/src/tiered_storage/hot.rs | 8 ++++++-- accounts-db/src/tiered_storage/index.rs | 11 ++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/accounts-db/src/tiered_storage/hot.rs b/accounts-db/src/tiered_storage/hot.rs index 6cab467fd6..a08fa56830 100644 --- a/accounts-db/src/tiered_storage/hot.rs +++ b/accounts-db/src/tiered_storage/hot.rs @@ -531,10 +531,14 @@ pub mod tests { let hot_storage = HotStorageReader::new_from_path(&path).unwrap(); for (i, index_writer_entry) in index_writer_entries.iter().enumerate() { - let account_offset = hot_storage.get_account_offset(IndexOffset(i)).unwrap(); + let account_offset = hot_storage + .get_account_offset(IndexOffset(i as u32)) + .unwrap(); assert_eq!(account_offset.block as u32, index_writer_entry.block_offset); - let account_address = hot_storage.get_account_address(IndexOffset(i)).unwrap(); + let account_address = hot_storage + .get_account_address(IndexOffset(i as u32)) + .unwrap(); assert_eq!(account_address, index_writer_entry.address); } } diff --git a/accounts-db/src/tiered_storage/index.rs b/accounts-db/src/tiered_storage/index.rs index cdaa828e9f..108134cc3c 100644 --- a/accounts-db/src/tiered_storage/index.rs +++ b/accounts-db/src/tiered_storage/index.rs @@ -30,7 +30,7 @@ pub struct AccountOffset { /// This can be used to obtain the AccountOffset and address by looking through /// the accounts index block. #[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct IndexOffset(pub usize); +pub struct IndexOffset(pub u32); /// The index format of a tiered accounts file. #[repr(u16)] @@ -84,7 +84,8 @@ impl IndexBlockFormat { ) -> TieredStorageResult<&'a Pubkey> { let account_offset = match self { Self::AddressAndBlockOffsetOnly => { - footer.index_block_offset as usize + std::mem::size_of::() * index_offset.0 + footer.index_block_offset as usize + + std::mem::size_of::() * (index_offset.0 as usize) } }; let (address, _) = get_type::(mmap, account_offset)?; @@ -102,7 +103,7 @@ impl IndexBlockFormat { Self::AddressAndBlockOffsetOnly => { let account_offset = footer.index_block_offset as usize + std::mem::size_of::() * footer.account_entry_count as usize - + index_offset.0 * std::mem::size_of::(); + + std::mem::size_of::() * index_offset.0 as usize; let (block_offset, _) = get_type::(mmap, account_offset)?; Ok(AccountOffset { @@ -166,11 +167,11 @@ mod tests { let mmap = unsafe { MmapOptions::new().map(&file).unwrap() }; for (i, index_entry) in index_entries.iter().enumerate() { let account_offset = indexer - .get_account_offset(&mmap, &footer, IndexOffset(i)) + .get_account_offset(&mmap, &footer, IndexOffset(i as u32)) .unwrap(); assert_eq!(index_entry.block_offset, account_offset.block as u32); let address = indexer - .get_account_address(&mmap, &footer, IndexOffset(i)) + .get_account_address(&mmap, &footer, IndexOffset(i as u32)) .unwrap(); assert_eq!(index_entry.address, address); }