[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.
This commit is contained in:
Yueh-Hsuan Chiang 2023-06-23 01:42:02 +08:00 committed by GitHub
parent 1c618f2479
commit d95e976a71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 0 deletions

View File

@ -103,6 +103,23 @@ impl AccountMetaOptionalFields {
.write_version
.map_or(0, |_| std::mem::size_of::<StoredMetaWriteVersion>())
}
/// 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::<Epoch>();
}
if flags.has_account_hash() {
fields_size += std::mem::size_of::<Hash>();
}
if flags.has_write_version() {
fields_size += std::mem::size_of::<StoredMetaWriteVersion>();
}
fields_size
}
}
#[cfg(test)]
@ -206,6 +223,12 @@ pub mod tests {
+ write_version
.map_or(0, |_| std::mem::size_of::<StoredMetaWriteVersion>())
);
assert_eq!(
opt_fields.size(),
AccountMetaOptionalFields::size_from_flags(&AccountMetaFlags::new_from(
&opt_fields
))
);
}
}
}