2021-03-12 00:25:52 -07:00
|
|
|
#![allow(clippy::integer_arithmetic)]
|
2019-07-20 16:28:17 -07:00
|
|
|
//! configuration for network rent
|
2021-06-09 20:54:13 -06:00
|
|
|
use crate::clock::DEFAULT_SLOTS_PER_EPOCH;
|
2019-07-20 16:28:17 -07:00
|
|
|
|
2019-10-30 16:25:12 -07:00
|
|
|
#[repr(C)]
|
2020-07-06 20:22:23 +09:00
|
|
|
#[derive(Serialize, Deserialize, PartialEq, Clone, Copy, Debug, AbiExample)]
|
2019-10-30 16:25:12 -07:00
|
|
|
pub struct Rent {
|
2019-07-20 16:28:17 -07:00
|
|
|
/// Rental rate
|
|
|
|
pub lamports_per_byte_year: u64,
|
|
|
|
|
|
|
|
/// exemption threshold, in years
|
|
|
|
pub exemption_threshold: f64,
|
2019-08-23 14:04:53 -07:00
|
|
|
|
|
|
|
// What portion of collected rent are to be destroyed, percentage-wise
|
|
|
|
pub burn_percent: u8,
|
2019-07-20 16:28:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// default rental rate in lamports/byte-year, based on:
|
2019-10-30 17:59:44 -04:00
|
|
|
/// 10^9 lamports per SOL
|
|
|
|
/// $1 per SOL
|
2019-07-20 16:28:17 -07:00
|
|
|
/// $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
|
|
|
|
|
|
|
/// default amount of time (in years) the balance has to include rent for
|
|
|
|
pub const DEFAULT_EXEMPTION_THRESHOLD: f64 = 2.0;
|
|
|
|
|
2019-12-04 00:54:01 +05:30
|
|
|
/// default percentage of rent to burn (Valid values are 0 to 100)
|
2020-10-09 12:19:50 -07:00
|
|
|
pub const DEFAULT_BURN_PERCENT: u8 = 50;
|
2019-08-23 14:04:53 -07:00
|
|
|
|
2019-11-14 08:55:09 -07:00
|
|
|
/// account storage overhead for calculation of base rent
|
2019-11-14 10:56:49 +05:30
|
|
|
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 {
|
2019-12-05 19:40:34 +05:30
|
|
|
/// calculate how much rent to burn from the collected rent
|
|
|
|
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)
|
|
|
|
}
|
2019-07-20 16:28:17 -07:00
|
|
|
/// minimum balance due for a given size Account::data.len()
|
2020-11-06 13:32:05 -07:00
|
|
|
///
|
|
|
|
/// Note: a stripped-down version of this calculation is used in
|
|
|
|
/// calculate_split_rent_exempt_reserve in the stake program. When this function is updated, --
|
|
|
|
/// eg. when making rent variable -- the stake program will need to be refactored
|
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-14 10:56:49 +05:30
|
|
|
(((ACCOUNT_STORAGE_OVERHEAD + bytes) * self.lamports_per_byte_year) as f64
|
|
|
|
* self.exemption_threshold) as u64
|
2019-07-20 16:28:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// whether a given balance and data_len 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_len with balance
|
2019-08-23 14:04:53 -07:00
|
|
|
pub fn due(&self, balance: u64, data_len: usize, years_elapsed: f64) -> (u64, bool) {
|
2019-07-20 16:28:17 -07:00
|
|
|
if self.is_exempt(balance, data_len) {
|
2019-08-23 14:04:53 -07:00
|
|
|
(0, true)
|
2019-07-20 16:28:17 -07:00
|
|
|
} else {
|
2019-08-23 14:04:53 -07:00
|
|
|
(
|
2019-11-14 10:56:49 +05:30
|
|
|
((self.lamports_per_byte_year * (data_len as u64 + ACCOUNT_STORAGE_OVERHEAD))
|
|
|
|
as f64
|
|
|
|
* years_elapsed) as u64,
|
2019-08-23 14:04:53 -07:00
|
|
|
false,
|
|
|
|
)
|
2019-07-20 16:28:17 -07:00
|
|
|
}
|
|
|
|
}
|
2019-11-12 12:33:40 -08:00
|
|
|
|
|
|
|
pub fn free() -> Self {
|
|
|
|
Self {
|
|
|
|
lamports_per_byte_year: 0,
|
|
|
|
..Rent::default()
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 20:54:13 -06: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;
|
|
|
|
let exemption_threshold = DEFAULT_EXEMPTION_THRESHOLD as f64 * 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()
|
|
|
|
}
|
|
|
|
}
|
2019-07-20 16:28:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_due() {
|
2019-11-14 10:56:49 +05:30
|
|
|
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-14 10:56:49 +05:30
|
|
|
default_rent.due(0, 2, 1.2),
|
2019-08-23 14:04:53 -07:00
|
|
|
(
|
2019-11-14 10:56:49 +05:30
|
|
|
(((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) as f64 * 1.2)
|
|
|
|
as u64,
|
2019-08-23 14:04:53 -07:00
|
|
|
DEFAULT_LAMPORTS_PER_BYTE_YEAR == 0
|
|
|
|
)
|
|
|
|
);
|
2019-07-20 16:28:17 -07:00
|
|
|
assert_eq!(
|
2019-11-14 10:56:49 +05:30
|
|
|
default_rent.due(
|
|
|
|
(((2 + ACCOUNT_STORAGE_OVERHEAD) * DEFAULT_LAMPORTS_PER_BYTE_YEAR) as f64
|
|
|
|
* DEFAULT_EXEMPTION_THRESHOLD) as u64,
|
|
|
|
2,
|
|
|
|
1.2
|
|
|
|
),
|
|
|
|
(0, true)
|
|
|
|
);
|
|
|
|
|
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-14 10:56:49 +05:30
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
custom_rent.due(0, 2, 1.2),
|
|
|
|
(
|
|
|
|
(((2 + ACCOUNT_STORAGE_OVERHEAD) * custom_rent.lamports_per_byte_year) as f64 * 1.2)
|
|
|
|
as u64,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
),
|
2019-08-23 14:04:53 -07:00
|
|
|
(0, true)
|
2019-07-20 16:28:17 -07:00
|
|
|
);
|
|
|
|
}
|
2019-08-23 14:04:53 -07:00
|
|
|
|
2019-12-10 14:50:55 -08:00
|
|
|
#[ignore]
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn show_rent_model() {
|
2020-10-19 13:19:24 -07:00
|
|
|
use crate::{clock::*, sysvar::Sysvar};
|
2019-12-10 14:50:55 -08:00
|
|
|
|
2020-10-19 13:19:24 -07:00
|
|
|
const SECONDS_PER_YEAR: f64 = 365.242_199 * 24.0 * 60.0 * 60.0;
|
2021-01-07 09:49:24 -06:00
|
|
|
const SLOTS_PER_YEAR: f64 = SECONDS_PER_YEAR / DEFAULT_S_PER_SLOT;
|
2019-12-10 14:50:55 -08:00
|
|
|
|
|
|
|
let rent = Rent::default();
|
|
|
|
panic!(
|
|
|
|
"\n\n\
|
|
|
|
==================================================\n\
|
|
|
|
empty account, no data:\n\
|
|
|
|
\t{} lamports per epoch, {} lamports to be rent_exempt\n\n\
|
|
|
|
stake_history, which is {}kB of data:\n\
|
|
|
|
\t{} lamports per epoch, {} lamports to be rent_exempt\n\
|
|
|
|
==================================================\n\n",
|
|
|
|
rent.due(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(1.0 / SLOTS_PER_YEAR) * DEFAULT_SLOTS_PER_EPOCH as f64,
|
|
|
|
)
|
|
|
|
.0,
|
|
|
|
rent.minimum_balance(0),
|
|
|
|
crate::sysvar::stake_history::StakeHistory::size_of() / 1024,
|
|
|
|
rent.due(
|
|
|
|
0,
|
|
|
|
crate::sysvar::stake_history::StakeHistory::size_of(),
|
|
|
|
(1.0 / SLOTS_PER_YEAR) * DEFAULT_SLOTS_PER_EPOCH as f64,
|
|
|
|
)
|
|
|
|
.0,
|
|
|
|
rent.minimum_balance(crate::sysvar::stake_history::StakeHistory::size_of()),
|
|
|
|
);
|
|
|
|
}
|
2019-07-20 16:28:17 -07:00
|
|
|
}
|