Skip to content

Commit

Permalink
[TieredStorage] AccountMetaOptionalFields::size_from_flags() (solana-…
Browse files Browse the repository at this point in the history
…labs#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.
  • Loading branch information
yhchiang-sol authored and wen-coding committed Aug 15, 2023
1 parent e298969 commit 35d37b2
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions runtime/src/tiered_storage/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
))
);
}
}
}
Expand Down

0 comments on commit 35d37b2

Please sign in to comment.