[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.
This commit is contained in:
Yueh-Hsuan Chiang 2023-06-01 14:48:27 -07:00 committed by GitHub
parent e0389ba90f
commit 2b04f28c7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 0 deletions

View File

@ -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;

View File

@ -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::<AccountMetaFlags>(),
std::mem::size_of::<u32>()
);
}
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);
}
}