From dd30175e55eb4902585843b04f395ec4c426ebd1 Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang <93241502+yhchiang-sol@users.noreply.github.com> Date: Fri, 2 Feb 2024 11:18:52 -0800 Subject: [PATCH] [TieredStorage] TieredStorageReader:: and HotStorageReader:: accounts() (#35031) #### Problem HotStorageReader and TieredStorageReader haven't implemented accounts() that is required by AcocuntsFile. #### Summary of Changes This PR implements accounts() for both HotStorageReader and TieredStorageReader #### Test Plan Extend the existing test to cover accounts(). --- accounts-db/src/tiered_storage/hot.rs | 34 ++++++++++++++++++++++ accounts-db/src/tiered_storage/readable.rs | 11 +++++++ 2 files changed, 45 insertions(+) diff --git a/accounts-db/src/tiered_storage/hot.rs b/accounts-db/src/tiered_storage/hot.rs index 805b50f2f..54091313c 100644 --- a/accounts-db/src/tiered_storage/hot.rs +++ b/accounts-db/src/tiered_storage/hot.rs @@ -458,6 +458,24 @@ impl HotStorageReader { IndexOffset(index_offset.0.saturating_add(1)), ))) } + + /// Return a vector of account metadata for each account, starting from + /// `index_offset` + pub fn accounts( + &self, + mut index_offset: IndexOffset, + ) -> TieredStorageResult> { + let mut accounts = Vec::with_capacity( + self.footer + .account_entry_count + .saturating_sub(index_offset.0) as usize, + ); + while let Some((account, next)) = self.get_account(index_offset)? { + accounts.push(account); + index_offset = next; + } + Ok(accounts) + } } fn write_optional_fields( @@ -1402,5 +1420,21 @@ pub mod tests { storable_accounts.get(stored_info.offset); verify_account(&stored_meta, account, address, account_hash); } + + // verify get_accounts + let accounts = hot_storage.accounts(IndexOffset(0)).unwrap(); + + // first, we verify everything + for (i, stored_meta) in accounts.iter().enumerate() { + let (account, address, account_hash, _write_version) = storable_accounts.get(i); + verify_account(stored_meta, account, address, account_hash); + } + + // second, we verify various initial position + let total_stored_accounts = accounts.len(); + for i in 0..total_stored_accounts { + let partial_accounts = hot_storage.accounts(IndexOffset(i as u32)).unwrap(); + assert_eq!(&partial_accounts, &accounts[i..]); + } } } diff --git a/accounts-db/src/tiered_storage/readable.rs b/accounts-db/src/tiered_storage/readable.rs index b6d841b65..12c4a8224 100644 --- a/accounts-db/src/tiered_storage/readable.rs +++ b/accounts-db/src/tiered_storage/readable.rs @@ -148,4 +148,15 @@ impl TieredStorageReader { } } } + + /// Return a vector of account metadata for each account, starting from + /// `index_offset` + pub fn accounts( + &self, + index_offset: IndexOffset, + ) -> TieredStorageResult> { + match self { + Self::Hot(hot) => hot.accounts(index_offset), + } + } }