2022-04-20 18:20:17 -07:00
|
|
|
//! Configuration for network [rent].
|
|
|
|
//!
|
|
|
|
//! [rent]: https://docs.solana.com/implemented-proposals/rent
|
|
|
|
|
2023-09-01 00:26:13 -07:00
|
|
|
#![allow(clippy::arithmetic_side_effects)]
|
2022-05-16 12:30:37 -07:00
|
|
|
|
2022-11-09 09:15:42 -08:00
|
|
|
use {crate::clock::DEFAULT_SLOTS_PER_EPOCH, solana_sdk_macro::CloneZeroed};
|
2019-07-20 16:28:17 -07:00
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Configuration of network rent.
|
2019-10-30 16:25:12 -07:00
|
|
|
#[repr(C)]
|
2022-11-09 09:15:42 -08:00
|
|
|
#[derive(Serialize, Deserialize, PartialEq, CloneZeroed, Copy, Debug, AbiExample)]
|
2019-10-30 16:25:12 -07:00
|
|
|
pub struct Rent {
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Rental rate in lamports/byte-year.
|
2019-07-20 16:28:17 -07:00
|
|
|
pub lamports_per_byte_year: u64,
|
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Amount of time (in years) a balance must include rent for the account to
|
|
|
|
/// be rent exempt.
|
2019-07-20 16:28:17 -07:00
|
|
|
pub exemption_threshold: f64,
|
2019-08-23 14:04:53 -07:00
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// The percentage of collected rent that is burned.
|
|
|
|
///
|
|
|
|
/// Valid values are in the range [0, 100]. The remaining percentage is
|
|
|
|
/// distributed to validators.
|
2019-08-23 14:04:53 -07:00
|
|
|
pub burn_percent: u8,
|
2019-07-20 16:28:17 -07:00
|
|
|
}
|
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Default rental rate in lamports/byte-year.
|
|
|
|
///
|
|
|
|
/// This calculation is based on:
|
|
|
|
/// - 10^9 lamports per SOL
|
|
|
|
/// - $1 per SOL
|
|
|
|
/// - $0.01 per megabyte day
|
|
|
|
/// - $3.65 per megabyte year
|
2019-12-09 21:56:43 -08:00
|
|
|
pub const DEFAULT_LAMPORTS_PER_BYTE_YEAR: u64 = 1_000_000_000 / 100 * 365 / (1024 * 1024);
|
2019-07-20 16:28:17 -07:00
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Default amount of time (in years) the balance has to include rent for the
|
|
|
|
/// account to be rent exempt.
|
2019-07-20 16:28:17 -07:00
|
|
|
pub const DEFAULT_EXEMPTION_THRESHOLD: f64 = 2.0;
|
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Default percentage of collected rent that is burned.
|
|
|
|
///
|
|
|
|
/// Valid values are in the range [0, 100]. The remaining percentage is
|
|
|
|
/// distributed to validators.
|
2020-10-09 12:19:50 -07:00
|
|
|
pub const DEFAULT_BURN_PERCENT: u8 = 50;
|
2019-08-23 14:04:53 -07:00
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Account storage overhead for calculation of base rent.
|
|
|
|
///
|
|
|
|
/// This is the number of bytes required to store an account with no data. It is
|
|
|
|
/// added to an accounts data length when calculating [`Rent::minimum_balance`].
|
2019-11-13 21:26:49 -08:00
|
|
|
pub const ACCOUNT_STORAGE_OVERHEAD: u64 = 128;
|
|
|
|
|
2019-10-30 16:25:12 -07:00
|
|
|
impl Default for Rent {
|
2019-07-20 16:28:17 -07:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
lamports_per_byte_year: DEFAULT_LAMPORTS_PER_BYTE_YEAR,
|
|
|
|
exemption_threshold: DEFAULT_EXEMPTION_THRESHOLD,
|
2019-08-23 14:04:53 -07:00
|
|
|
burn_percent: DEFAULT_BURN_PERCENT,
|
2019-07-20 16:28:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:25:12 -07:00
|
|
|
impl Rent {
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Calculate how much rent to burn from the collected rent.
|
|
|
|
///
|
|
|
|
/// The first value returned is the amount burned. The second is the amount
|
|
|
|
/// to distribute to validators.
|
2019-12-05 06:10:34 -08:00
|
|
|
pub fn calculate_burn(&self, rent_collected: u64) -> (u64, u64) {
|
|
|
|
let burned_portion = (rent_collected * u64::from(self.burn_percent)) / 100;
|
|
|
|
(burned_portion, rent_collected - burned_portion)
|
|
|
|
}
|
2022-08-15 11:19:46 -07:00
|
|
|
|
|
|
|
/// Minimum balance due for rent-exemption of a given account data size.
|
2019-07-20 16:28:17 -07:00
|
|
|
pub fn minimum_balance(&self, data_len: usize) -> u64 {
|
|
|
|
let bytes = data_len as u64;
|
2019-11-13 21:26:49 -08:00
|
|
|
(((ACCOUNT_STORAGE_OVERHEAD + bytes) * self.lamports_per_byte_year) as f64
|
|
|
|
* self.exemption_threshold) as u64
|
2019-07-20 16:28:17 -07:00
|
|
|
}
|
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Whether a given balance and data length would be exempt.
|
2019-07-20 16:28:17 -07:00
|
|
|
pub fn is_exempt(&self, balance: u64, data_len: usize) -> bool {
|
|
|
|
balance >= self.minimum_balance(data_len)
|
|
|
|
}
|
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Rent due on account's data length with balance.
|
2022-01-08 07:03:46 -08:00
|
|
|
pub fn due(&self, balance: u64, data_len: usize, years_elapsed: f64) -> RentDue {
|
2019-07-20 16:28:17 -07:00
|
|
|
if self.is_exempt(balance, data_len) {
|
2022-01-08 07:03:46 -08:00
|
|
|
RentDue::Exempt
|
2019-07-20 16:28:17 -07:00
|
|
|
} else {
|
2022-02-28 07:42:42 -08:00
|
|
|
RentDue::Paying(self.due_amount(data_len, years_elapsed))
|
2019-07-20 16:28:17 -07:00
|
|
|
}
|
|
|
|
}
|
2019-11-12 12:33:40 -08:00
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Rent due for account that is known to be not exempt.
|
2022-02-28 07:42:42 -08:00
|
|
|
pub fn due_amount(&self, data_len: usize, years_elapsed: f64) -> u64 {
|
|
|
|
let actual_data_len = data_len as u64 + ACCOUNT_STORAGE_OVERHEAD;
|
|
|
|
let lamports_per_year = self.lamports_per_byte_year * actual_data_len;
|
|
|
|
(lamports_per_year as f64 * years_elapsed) as u64
|
|
|
|
}
|
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Creates a `Rent` that charges no lamports.
|
|
|
|
///
|
|
|
|
/// This is used for testing.
|
2019-11-12 12:33:40 -08:00
|
|
|
pub fn free() -> Self {
|
|
|
|
Self {
|
|
|
|
lamports_per_byte_year: 0,
|
|
|
|
..Rent::default()
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 19:54:13 -07:00
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Creates a `Rent` that is scaled based on the number of slots in an epoch.
|
|
|
|
///
|
|
|
|
/// This is used for testing.
|
2021-06-09 19:54:13 -07:00
|
|
|
pub fn with_slots_per_epoch(slots_per_epoch: u64) -> Self {
|
|
|
|
let ratio = slots_per_epoch as f64 / DEFAULT_SLOTS_PER_EPOCH as f64;
|
2022-11-09 11:39:38 -08:00
|
|
|
let exemption_threshold = DEFAULT_EXEMPTION_THRESHOLD * ratio;
|
2021-06-09 19:54:13 -07:00
|
|
|
let lamports_per_byte_year = (DEFAULT_LAMPORTS_PER_BYTE_YEAR as f64 / ratio) as u64;
|
|
|
|
Self {
|
|
|
|
lamports_per_byte_year,
|
|
|
|
exemption_threshold,
|
|
|
|
..Self::default()
|
|
|
|
}
|
|
|
|
}
|
2019-07-20 16:28:17 -07:00
|
|
|
}
|
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// The return value of [`Rent::due`].
|
2022-01-08 07:03:46 -08:00
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
|
|
|
pub enum RentDue {
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Used to indicate the account is rent exempt.
|
2022-01-08 07:03:46 -08:00
|
|
|
Exempt,
|
2022-08-15 11:19:46 -07:00
|
|
|
/// The account owes this much rent.
|
2022-01-08 07:03:46 -08:00
|
|
|
Paying(u64),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RentDue {
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Return the lamports due for rent.
|
2022-01-08 07:03:46 -08:00
|
|
|
pub fn lamports(&self) -> u64 {
|
|
|
|
match self {
|
|
|
|
RentDue::Exempt => 0,
|
|
|
|
RentDue::Paying(x) => *x,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 11:19:46 -07:00
|
|
|
/// Return 'true' if rent exempt.
|
2022-01-08 07:03:46 -08:00
|
|
|
pub fn is_exempt(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
RentDue::Exempt => true,
|
|
|
|
RentDue::Paying(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 16:28:17 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_due() {
|
2019-11-13 21:26:49 -08:00
|
|
|
let default_rent = Rent::default();
|
2019-07-20 16:28:17 -07:00
|
|
|
|
2019-08-23 14:04:53 -07:00
|
|
|
assert_eq!(
|
2019-11-13 21:26:49 -08:00
|
|
|
default_rent.due(0, 2, 1.2),
|
2022-01-08 07:03:46 -08:00
|
|
|
RentDue::Paying(
|
2019-11-13 21:26:49 -08:00
|
|
|
(((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) as f64 * 1.2)
|
2022-01-08 07:03:46 -08:00
|
|
|
as u64
|
|
|
|
),
|
2019-08-23 14:04:53 -07:00
|
|
|
);
|
2019-07-20 16:28:17 -07:00
|
|
|
assert_eq!(
|
2019-11-13 21:26:49 -08:00
|
|
|
default_rent.due(
|
|
|
|
(((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) as f64
|
|
|
|
* DEFAULT_EXEMPTION_THRESHOLD) as u64,
|
|
|
|
2,
|
|
|
|
1.2
|
|
|
|
),
|
2022-01-08 07:03:46 -08:00
|
|
|
RentDue::Exempt,
|
2019-11-13 21:26:49 -08:00
|
|
|
);
|
|
|
|
|
2020-12-13 17:26:34 -08:00
|
|
|
let custom_rent = Rent {
|
|
|
|
lamports_per_byte_year: 5,
|
|
|
|
exemption_threshold: 2.5,
|
|
|
|
..Rent::default()
|
|
|
|
};
|
2019-11-13 21:26:49 -08:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
custom_rent.due(0, 2, 1.2),
|
2022-01-08 07:03:46 -08:00
|
|
|
RentDue::Paying(
|
2019-11-13 21:26:49 -08:00
|
|
|
(((2 + ACCOUNT_STORAGE_OVERHEAD) * custom_rent.lamports_per_byte_year) as f64 * 1.2)
|
|
|
|
as u64,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
custom_rent.due(
|
|
|
|
(((2 + ACCOUNT_STORAGE_OVERHEAD) * custom_rent.lamports_per_byte_year) as f64
|
|
|
|
* custom_rent.exemption_threshold) as u64,
|
|
|
|
2,
|
|
|
|
1.2
|
2019-07-20 16:28:17 -07:00
|
|
|
),
|
2022-01-08 07:03:46 -08:00
|
|
|
RentDue::Exempt
|
2019-07-20 16:28:17 -07:00
|
|
|
);
|
|
|
|
}
|
2019-08-23 14:04:53 -07:00
|
|
|
|
2019-12-10 14:50:55 -08:00
|
|
|
#[test]
|
2022-01-08 07:03:46 -08:00
|
|
|
fn test_rent_due_lamports() {
|
|
|
|
assert_eq!(RentDue::Exempt.lamports(), 0);
|
|
|
|
|
|
|
|
let amount = 123;
|
|
|
|
assert_eq!(RentDue::Paying(amount).lamports(), amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rent_due_is_exempt() {
|
|
|
|
assert!(RentDue::Exempt.is_exempt());
|
|
|
|
assert!(!RentDue::Paying(0).is_exempt());
|
2019-12-10 14:50:55 -08:00
|
|
|
}
|
2022-05-16 12:30:37 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_clone() {
|
|
|
|
let rent = Rent {
|
|
|
|
lamports_per_byte_year: 1,
|
|
|
|
exemption_threshold: 2.2,
|
|
|
|
burn_percent: 3,
|
|
|
|
};
|
|
|
|
#[allow(clippy::clone_on_copy)]
|
|
|
|
let cloned_rent = rent.clone();
|
|
|
|
assert_eq!(cloned_rent, rent);
|
|
|
|
}
|
2019-07-20 16:28:17 -07:00
|
|
|
}
|