solana_accounts_db/tiered_storage/owners.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
use {
crate::tiered_storage::{
file::TieredWritableFile, footer::TieredStorageFooter, mmap_utils::get_pod,
TieredStorageResult,
},
indexmap::set::IndexSet,
memmap2::Mmap,
solana_sdk::pubkey::Pubkey,
};
/// The offset to an owner entry in the owners block.
/// This is used to obtain the address of the account owner.
///
/// Note that as its internal type is u32, it means the maximum number of
/// unique owners in one TieredStorageFile is 2^32.
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd)]
pub struct OwnerOffset(pub u32);
/// Owner block holds a set of unique addresses of account owners,
/// and an account meta has a owner_offset field for accessing
/// it's owner address.
#[repr(u16)]
#[derive(
Clone,
Copy,
Debug,
Default,
Eq,
Hash,
PartialEq,
num_enum::IntoPrimitive,
num_enum::TryFromPrimitive,
)]
pub enum OwnersBlockFormat {
/// This format persists OwnerBlock as a consecutive bytes of pubkeys
/// without any meta-data. For each account meta, it has a owner_offset
/// field to access its owner's address in the OwnersBlock.
#[default]
AddressesOnly = 0,
}
impl OwnersBlockFormat {
/// Persists the provided owners' addresses into the specified file.
pub fn write_owners_block(
&self,
file: &mut TieredWritableFile,
owners_table: &OwnersTable,
) -> TieredStorageResult<usize> {
match self {
Self::AddressesOnly => {
let mut bytes_written = 0;
for address in &owners_table.owners_set {
bytes_written += file.write_pod(address)?;
}
Ok(bytes_written)
}
}
}
/// Returns the owner address associated with the specified owner_offset
/// and footer inside the input mmap.
pub fn get_owner_address<'a>(
&self,
mmap: &'a Mmap,
footer: &TieredStorageFooter,
owner_offset: OwnerOffset,
) -> TieredStorageResult<&'a Pubkey> {
match self {
Self::AddressesOnly => {
let offset = footer.owners_block_offset as usize
+ (std::mem::size_of::<Pubkey>() * owner_offset.0 as usize);
let (pubkey, _) = get_pod::<Pubkey>(mmap, offset)?;
Ok(pubkey)
}
}
}
}
/// The in-memory representation of owners block for write.
/// It manages a set of unique addresses of account owners.
#[derive(Debug, Default)]
pub struct OwnersTable {
owners_set: IndexSet<Pubkey>,
}
/// OwnersBlock is persisted as a consecutive bytes of pubkeys without any
/// meta-data. For each account meta, it has a owner_offset field to
/// access its owner's address in the OwnersBlock.
impl OwnersTable {
/// Add the specified pubkey as the owner into the OwnersWriterTable
/// if the specified pubkey has not existed in the OwnersWriterTable
/// yet. In any case, the function returns its OwnerOffset.
pub fn insert(&mut self, pubkey: &Pubkey) -> OwnerOffset {
let (offset, _existed) = self.owners_set.insert_full(*pubkey);
OwnerOffset(offset as u32)
}
/// Returns the number of unique owner addresses in the table.
pub fn len(&self) -> usize {
self.owners_set.len()
}
/// Returns true if the OwnersTable is empty
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[cfg(test)]
mod tests {
use {
super::*, crate::tiered_storage::file::TieredWritableFile, memmap2::MmapOptions,
std::fs::OpenOptions, tempfile::TempDir,
};
#[test]
fn test_owners_block() {
// Generate a new temp path that is guaranteed to NOT already have a file.
let temp_dir = TempDir::new().unwrap();
let path = temp_dir.path().join("test_owners_block");
const NUM_OWNERS: u32 = 10;
let addresses: Vec<_> = std::iter::repeat_with(Pubkey::new_unique)
.take(NUM_OWNERS as usize)
.collect();
let footer = TieredStorageFooter {
// Set owners_block_offset to 0 as we didn't write any account
// meta/data nor index block.
owners_block_offset: 0,
..TieredStorageFooter::default()
};
{
let mut file = TieredWritableFile::new(&path).unwrap();
let mut owners_table = OwnersTable::default();
addresses.iter().for_each(|owner_address| {
owners_table.insert(owner_address);
});
footer
.owners_block_format
.write_owners_block(&mut file, &owners_table)
.unwrap();
// while the test only focuses on account metas, writing a footer
// here is necessary to make it a valid tiered-storage file.
footer.write_footer_block(&mut file).unwrap();
}
let file = OpenOptions::new().read(true).open(path).unwrap();
let mmap = unsafe { MmapOptions::new().map(&file).unwrap() };
for (i, address) in addresses.iter().enumerate() {
assert_eq!(
footer
.owners_block_format
.get_owner_address(&mmap, &footer, OwnerOffset(i as u32))
.unwrap(),
address
);
}
}
#[test]
fn test_owners_table() {
let mut owners_table = OwnersTable::default();
const NUM_OWNERS: usize = 99;
let addresses: Vec<_> = std::iter::repeat_with(Pubkey::new_unique)
.take(NUM_OWNERS)
.collect();
// as we insert sequentially, we expect each entry has same OwnerOffset
// as its index inside the Vector.
for (i, address) in addresses.iter().enumerate() {
assert_eq!(owners_table.insert(address), OwnerOffset(i as u32));
}
let cloned_addresses = addresses.clone();
// insert again and expect the same OwnerOffset
for (i, address) in cloned_addresses.iter().enumerate() {
assert_eq!(owners_table.insert(address), OwnerOffset(i as u32));
}
// make sure the size of the resulting owner table is the same
// as the input
assert_eq!(owners_table.owners_set.len(), addresses.len());
}
}