From d95e976a718a464cd3662e0ee22287648dbceefc Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang <93241502+yhchiang-sol@users.noreply.github.com> Date: Fri, 23 Jun 2023 01:42:02 +0800 Subject: [PATCH] [TieredStorage] AccountMetaOptionalFields::size_from_flags() (#32242) #### Summary of Changes This PR adds AccountMetaOptionalFields::size_from_flags that takes `&AccountMegaFlags` and returns the size of the AccountMetaOptionalFields based on the input AccountMegaFlags. This function is needed because the reader of the TieredAccountMeta directly extract all the Some fields of AccountMetaOptionalFields from its account block without constructing the AccountMetaOptionalFields instance. #### Test plan Improve existing unit tests that further verify the correctness of the function. --- runtime/src/tiered_storage/meta.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/runtime/src/tiered_storage/meta.rs b/runtime/src/tiered_storage/meta.rs index fb67a3a8f..a402d79e5 100644 --- a/runtime/src/tiered_storage/meta.rs +++ b/runtime/src/tiered_storage/meta.rs @@ -103,6 +103,23 @@ impl AccountMetaOptionalFields { .write_version .map_or(0, |_| std::mem::size_of::()) } + + /// Given the specified AccountMetaFlags, returns the size of its + /// associated AccountMetaOptionalFields. + pub fn size_from_flags(flags: &AccountMetaFlags) -> usize { + let mut fields_size = 0; + if flags.has_rent_epoch() { + fields_size += std::mem::size_of::(); + } + if flags.has_account_hash() { + fields_size += std::mem::size_of::(); + } + if flags.has_write_version() { + fields_size += std::mem::size_of::(); + } + + fields_size + } } #[cfg(test)] @@ -206,6 +223,12 @@ pub mod tests { + write_version .map_or(0, |_| std::mem::size_of::()) ); + assert_eq!( + opt_fields.size(), + AccountMetaOptionalFields::size_from_flags(&AccountMetaFlags::new_from( + &opt_fields + )) + ); } } }