From 2b04f28c7b6984ab241812ac3228879e96dd6c30 Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang <93241502+yhchiang-sol@users.noreply.github.com> Date: Thu, 1 Jun 2023 14:48:27 -0700 Subject: [PATCH] [TieredStorage] AccountMetaFlags (#31913) #### Summary of Changes This PR implements AccountMetaFlags, which will later be used in the tiered accounts storage to support optional fields. #### Test Plan Unit tests are included in this PR. --- runtime/src/tiered_storage.rs | 1 + runtime/src/tiered_storage/meta.rs | 72 ++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 runtime/src/tiered_storage/meta.rs diff --git a/runtime/src/tiered_storage.rs b/runtime/src/tiered_storage.rs index ab9a779fd..2c8478363 100644 --- a/runtime/src/tiered_storage.rs +++ b/runtime/src/tiered_storage.rs @@ -2,6 +2,7 @@ pub mod byte_block; pub mod error; pub mod file; pub mod footer; +pub mod meta; pub mod mmap_utils; use crate::tiered_storage::error::TieredStorageError; diff --git a/runtime/src/tiered_storage/meta.rs b/runtime/src/tiered_storage/meta.rs new file mode 100644 index 000000000..ed95d2f68 --- /dev/null +++ b/runtime/src/tiered_storage/meta.rs @@ -0,0 +1,72 @@ +#![allow(dead_code)] +//! The account meta and related structs for the tiered storage. + +use modular_bitfield::prelude::*; + +/// The struct that handles the account meta flags. +#[bitfield(bits = 32)] +#[repr(C)] +#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)] +pub(crate) struct AccountMetaFlags { + /// whether the account meta has rent epoch + has_rent_epoch: bool, + /// whether the account meta has account hash + has_account_hash: bool, + /// whether the account meta has write version + has_write_version: bool, + /// the reserved bits. + reserved: B29, +} + +#[cfg(test)] +pub mod tests { + use crate::tiered_storage::meta::*; + + #[test] + fn test_account_meta_flags_new() { + let flags = AccountMetaFlags::new(); + + assert!(!flags.has_rent_epoch()); + assert!(!flags.has_account_hash()); + assert!(!flags.has_write_version()); + assert_eq!(flags.reserved(), 0u32); + + assert_eq!( + std::mem::size_of::(), + std::mem::size_of::() + ); + } + + fn verify_flags_serialization(flags: &AccountMetaFlags) { + assert_eq!(AccountMetaFlags::from_bytes(flags.into_bytes()), *flags); + } + + #[test] + fn test_account_meta_flags_set() { + let mut flags = AccountMetaFlags::new(); + + flags.set_has_rent_epoch(true); + + assert!(flags.has_rent_epoch()); + assert!(!flags.has_account_hash()); + assert!(!flags.has_write_version()); + verify_flags_serialization(&flags); + + flags.set_has_account_hash(true); + + assert!(flags.has_rent_epoch()); + assert!(flags.has_account_hash()); + assert!(!flags.has_write_version()); + verify_flags_serialization(&flags); + + flags.set_has_write_version(true); + + assert!(flags.has_rent_epoch()); + assert!(flags.has_account_hash()); + assert!(flags.has_write_version()); + verify_flags_serialization(&flags); + + // make sure the reserved bits are untouched. + assert_eq!(flags.reserved(), 0u32); + } +}