solana_program/rent.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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
//! Configuration for network [rent].
//!
//! [rent]: https://docs.solanalabs.com/implemented-proposals/rent
#![allow(clippy::arithmetic_side_effects)]
use {crate::clock::DEFAULT_SLOTS_PER_EPOCH, solana_sdk_macro::CloneZeroed};
/// Configuration of network rent.
#[repr(C)]
#[derive(Serialize, Deserialize, PartialEq, CloneZeroed, Copy, Debug, AbiExample)]
pub struct Rent {
/// Rental rate in lamports/byte-year.
pub lamports_per_byte_year: u64,
/// Amount of time (in years) a balance must include rent for the account to
/// be rent exempt.
pub exemption_threshold: f64,
/// The percentage of collected rent that is burned.
///
/// Valid values are in the range [0, 100]. The remaining percentage is
/// distributed to validators.
pub burn_percent: u8,
}
/// 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
pub const DEFAULT_LAMPORTS_PER_BYTE_YEAR: u64 = 1_000_000_000 / 100 * 365 / (1024 * 1024);
/// Default amount of time (in years) the balance has to include rent for the
/// account to be rent exempt.
pub const DEFAULT_EXEMPTION_THRESHOLD: f64 = 2.0;
/// Default percentage of collected rent that is burned.
///
/// Valid values are in the range [0, 100]. The remaining percentage is
/// distributed to validators.
pub const DEFAULT_BURN_PERCENT: u8 = 50;
/// 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`].
pub const ACCOUNT_STORAGE_OVERHEAD: u64 = 128;
impl Default for Rent {
fn default() -> Self {
Self {
lamports_per_byte_year: DEFAULT_LAMPORTS_PER_BYTE_YEAR,
exemption_threshold: DEFAULT_EXEMPTION_THRESHOLD,
burn_percent: DEFAULT_BURN_PERCENT,
}
}
}
impl Rent {
/// 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.
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)
}
/// Minimum balance due for rent-exemption of a given account data size.
pub fn minimum_balance(&self, data_len: usize) -> u64 {
let bytes = data_len as u64;
(((ACCOUNT_STORAGE_OVERHEAD + bytes) * self.lamports_per_byte_year) as f64
* self.exemption_threshold) as u64
}
/// Whether a given balance and data length would be exempt.
pub fn is_exempt(&self, balance: u64, data_len: usize) -> bool {
balance >= self.minimum_balance(data_len)
}
/// Rent due on account's data length with balance.
pub fn due(&self, balance: u64, data_len: usize, years_elapsed: f64) -> RentDue {
if self.is_exempt(balance, data_len) {
RentDue::Exempt
} else {
RentDue::Paying(self.due_amount(data_len, years_elapsed))
}
}
/// Rent due for account that is known to be not exempt.
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
}
/// Creates a `Rent` that charges no lamports.
///
/// This is used for testing.
pub fn free() -> Self {
Self {
lamports_per_byte_year: 0,
..Rent::default()
}
}
/// Creates a `Rent` that is scaled based on the number of slots in an epoch.
///
/// This is used for testing.
pub fn with_slots_per_epoch(slots_per_epoch: u64) -> Self {
let ratio = slots_per_epoch as f64 / DEFAULT_SLOTS_PER_EPOCH as f64;
let exemption_threshold = DEFAULT_EXEMPTION_THRESHOLD * ratio;
let lamports_per_byte_year = (DEFAULT_LAMPORTS_PER_BYTE_YEAR as f64 / ratio) as u64;
Self {
lamports_per_byte_year,
exemption_threshold,
..Self::default()
}
}
}
/// The return value of [`Rent::due`].
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum RentDue {
/// Used to indicate the account is rent exempt.
Exempt,
/// The account owes this much rent.
Paying(u64),
}
impl RentDue {
/// Return the lamports due for rent.
pub fn lamports(&self) -> u64 {
match self {
RentDue::Exempt => 0,
RentDue::Paying(x) => *x,
}
}
/// Return 'true' if rent exempt.
pub fn is_exempt(&self) -> bool {
match self {
RentDue::Exempt => true,
RentDue::Paying(_) => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_due() {
let default_rent = Rent::default();
assert_eq!(
default_rent.due(0, 2, 1.2),
RentDue::Paying(
(((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) as f64 * 1.2)
as u64
),
);
assert_eq!(
default_rent.due(
(((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) as f64
* DEFAULT_EXEMPTION_THRESHOLD) as u64,
2,
1.2
),
RentDue::Exempt,
);
let custom_rent = Rent {
lamports_per_byte_year: 5,
exemption_threshold: 2.5,
..Rent::default()
};
assert_eq!(
custom_rent.due(0, 2, 1.2),
RentDue::Paying(
(((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
),
RentDue::Exempt
);
}
#[test]
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());
}
#[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);
}
}