2021-12-28 03:14:48 -08:00
|
|
|
//! The accounts data space has a maximum size it is permitted to grow to. This module contains
|
|
|
|
//! the constants and types for tracking and metering the accounts data space during program
|
|
|
|
//! runtime.
|
|
|
|
|
|
|
|
/// The maximum allowed size, in bytes, of the accounts data
|
|
|
|
/// 128 GB was chosen because it is the RAM amount listed under Hardware Recommendations on
|
|
|
|
/// [Validator Requirements](https://docs.solana.com/running-validator/validator-reqs), and
|
|
|
|
/// validators often put the ledger on a RAM disk (i.e. tmpfs).
|
|
|
|
pub const MAX_ACCOUNTS_DATA_LEN: u64 = 128_000_000_000;
|
|
|
|
|
|
|
|
/// Meter and track the amount of available accounts data space
|
|
|
|
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
|
|
|
|
pub struct AccountsDataMeter {
|
2022-02-08 17:24:47 -08:00
|
|
|
/// The initial amount of accounts data space used (in bytes)
|
|
|
|
initial: u64,
|
|
|
|
|
|
|
|
/// The amount of accounts data space that has changed since `initial` (in bytes)
|
|
|
|
delta: i64,
|
2021-12-28 03:14:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AccountsDataMeter {
|
|
|
|
/// Make a new AccountsDataMeter
|
2022-02-08 17:24:47 -08:00
|
|
|
#[must_use]
|
|
|
|
pub fn new(initial_accounts_data_len: u64) -> Self {
|
2022-08-08 08:05:25 -07:00
|
|
|
Self {
|
2022-02-08 17:24:47 -08:00
|
|
|
initial: initial_accounts_data_len,
|
|
|
|
delta: 0,
|
2022-08-08 08:05:25 -07:00
|
|
|
}
|
2021-12-28 03:14:48 -08:00
|
|
|
}
|
|
|
|
|
2022-02-08 17:24:47 -08:00
|
|
|
/// Return the initial amount of accounts data space used (in bytes)
|
|
|
|
pub fn initial(&self) -> u64 {
|
|
|
|
self.initial
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the amount of accounts data space that has changed (in bytes)
|
|
|
|
pub fn delta(&self) -> i64 {
|
|
|
|
self.delta
|
|
|
|
}
|
|
|
|
|
2021-12-28 03:14:48 -08:00
|
|
|
/// Return the current amount of accounts data space used (in bytes)
|
|
|
|
pub fn current(&self) -> u64 {
|
2022-02-08 17:24:47 -08:00
|
|
|
/// NOTE: Mixed integer ops currently not stable, so copying the impl.
|
|
|
|
/// * https://github.com/rust-lang/rust/issues/87840
|
|
|
|
/// * https://github.com/a1phyr/rust/blob/47edde1086412b36e9efd6098b191ec15a2a760a/library/core/src/num/uint_macros.rs#L1039-L1048
|
|
|
|
const fn saturating_add_signed(lhs: u64, rhs: i64) -> u64 {
|
|
|
|
let (res, overflow) = lhs.overflowing_add(rhs as u64);
|
|
|
|
if overflow == (rhs < 0) {
|
|
|
|
res
|
|
|
|
} else if overflow {
|
|
|
|
u64::MAX
|
|
|
|
} else {
|
|
|
|
u64::MIN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
saturating_add_signed(self.initial, self.delta)
|
2021-12-28 03:14:48 -08:00
|
|
|
}
|
|
|
|
|
2022-03-03 10:23:06 -08:00
|
|
|
/// Adjust the space used by accounts data by `amount` (in bytes).
|
|
|
|
pub fn adjust_delta_unchecked(&mut self, amount: i64) {
|
2022-02-10 09:57:00 -08:00
|
|
|
self.delta = self.delta.saturating_add(amount);
|
|
|
|
}
|
2021-12-28 03:14:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new() {
|
2022-02-08 17:24:47 -08:00
|
|
|
let initial = 1234;
|
|
|
|
let accounts_data_meter = AccountsDataMeter::new(initial);
|
|
|
|
assert_eq!(accounts_data_meter.initial, initial);
|
2021-12-28 03:14:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_can_use_max_len() {
|
|
|
|
let _ = AccountsDataMeter::new(MAX_ACCOUNTS_DATA_LEN);
|
|
|
|
}
|
|
|
|
}
|