2021-12-14 07:31:42 -08:00
|
|
|
//! AccountInfo represents a reference to AccountSharedData in either an AppendVec or the write cache.
|
|
|
|
//! AccountInfo is not persisted anywhere between program runs.
|
|
|
|
//! AccountInfo is purely runtime state.
|
|
|
|
//! Note that AccountInfo is saved to disk buckets during runtime, but disk buckets are recreated at startup.
|
2021-12-13 15:35:07 -08:00
|
|
|
use crate::{
|
|
|
|
accounts_db::{AppendVecId, CACHE_VIRTUAL_OFFSET, CACHE_VIRTUAL_STORAGE_ID},
|
|
|
|
accounts_index::{IsCached, ZeroLamport},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// offset within an append vec to account data
|
|
|
|
pub type Offset = usize;
|
|
|
|
|
|
|
|
/// specify where account data is located
|
|
|
|
pub enum StorageLocation {
|
|
|
|
AppendVec(AppendVecId, Offset),
|
|
|
|
Cached,
|
|
|
|
}
|
2021-12-11 09:47:05 -08:00
|
|
|
|
|
|
|
#[derive(Default, Debug, PartialEq, Clone, Copy)]
|
|
|
|
pub struct AccountInfo {
|
|
|
|
/// index identifying the append storage
|
2021-12-13 19:35:30 -08:00
|
|
|
store_id: AppendVecId,
|
2021-12-11 09:47:05 -08:00
|
|
|
|
|
|
|
/// offset into the storage
|
2021-12-13 15:35:07 -08:00
|
|
|
offset: Offset,
|
2021-12-11 09:47:05 -08:00
|
|
|
|
|
|
|
/// needed to track shrink candidacy in bytes. Used to update the number
|
|
|
|
/// of alive bytes in an AppendVec as newer slots purge outdated entries
|
2021-12-14 07:31:42 -08:00
|
|
|
/// Note that highest bit is used for ZERO_LAMPORT_BIT
|
2021-12-13 08:10:06 -08:00
|
|
|
stored_size: usize,
|
2021-12-11 09:47:05 -08:00
|
|
|
}
|
|
|
|
|
2021-12-14 07:31:42 -08:00
|
|
|
/// presence of this bit in stored_size indicates this account info references an account with zero lamports
|
|
|
|
const ZERO_LAMPORT_BIT: usize = 1 << (usize::BITS - 1);
|
|
|
|
|
2021-12-11 09:47:05 -08:00
|
|
|
impl ZeroLamport for AccountInfo {
|
|
|
|
fn is_zero_lamport(&self) -> bool {
|
2021-12-14 07:31:42 -08:00
|
|
|
self.stored_size & ZERO_LAMPORT_BIT == ZERO_LAMPORT_BIT
|
2021-12-11 09:47:05 -08:00
|
|
|
}
|
|
|
|
}
|
2021-12-12 12:36:05 -08:00
|
|
|
|
2021-12-13 15:35:07 -08:00
|
|
|
impl IsCached for AccountInfo {
|
|
|
|
fn is_cached(&self) -> bool {
|
|
|
|
self.store_id == CACHE_VIRTUAL_STORAGE_ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 12:36:05 -08:00
|
|
|
impl AccountInfo {
|
2021-12-14 07:31:42 -08:00
|
|
|
pub fn new(storage_location: StorageLocation, mut stored_size: usize, lamports: u64) -> Self {
|
2021-12-13 15:35:07 -08:00
|
|
|
let (store_id, offset) = match storage_location {
|
|
|
|
StorageLocation::AppendVec(store_id, offset) => (store_id, offset),
|
|
|
|
StorageLocation::Cached => (CACHE_VIRTUAL_STORAGE_ID, CACHE_VIRTUAL_OFFSET),
|
|
|
|
};
|
2021-12-14 07:31:42 -08:00
|
|
|
assert!(stored_size < ZERO_LAMPORT_BIT);
|
|
|
|
if lamports == 0 {
|
|
|
|
stored_size |= ZERO_LAMPORT_BIT;
|
|
|
|
}
|
2021-12-12 12:36:05 -08:00
|
|
|
Self {
|
|
|
|
store_id,
|
|
|
|
offset,
|
|
|
|
stored_size,
|
|
|
|
}
|
|
|
|
}
|
2021-12-13 08:10:06 -08:00
|
|
|
|
2021-12-13 19:35:30 -08:00
|
|
|
pub fn store_id(&self) -> usize {
|
|
|
|
self.store_id
|
|
|
|
}
|
|
|
|
|
2021-12-13 12:43:26 -08:00
|
|
|
pub fn offset(&self) -> usize {
|
|
|
|
self.offset
|
|
|
|
}
|
|
|
|
|
2021-12-13 08:10:06 -08:00
|
|
|
pub fn stored_size(&self) -> usize {
|
2021-12-14 07:31:42 -08:00
|
|
|
// elminate the special bit that indicates the info references an account with zero lamports
|
|
|
|
self.stored_size & !ZERO_LAMPORT_BIT
|
2021-12-13 08:10:06 -08:00
|
|
|
}
|
2021-12-12 12:36:05 -08:00
|
|
|
}
|